feat(media): live channels across relay instances; viewers hear when a channel goes live

With several relays sharing pins, a viewer and the publisher could land on
different instances and never meet. LiveRelayOptions.mesh: each relay
claims the channels whose publisher it holds (LivePublisherLocator,
Postgres table shade_live_publishers, heartbeat 10 s). A relay whose
viewers want a channel published elsewhere subscribes to that relay once,
with a viewer's own read token, and fans out locally, verifying every
frame against the pin (a peer or a poisoned locator row cannot forge
one). Re-following replays a backlog; frames are passed on once (seq).
Viewers follow a publisher that moves. A token that fails the in-memory
pin is checked against the shared pin before refusal, so a publisher
back with a new read token is honoured on every instance.
SHADE_MEDIA_RELAY_URL / `media serve --advertise`.

Relays now send {"type":"state","live"} when a channel goes live or quiet
after a viewer joined; handled by the TS client (browser) and Android's
LiveWatcher.

Fixed on the way:
- The relay never saw its publisher leave: Hono gives each event its own
  WSContext and the close handler compared identity. Channels stayed
  "live" with nobody publishing; a replaced publisher was never closed.
- FileMediaStore on an unusable directory raised an unhandled rejection
  at start; now each call fails with the reason.

Verified: 5 mesh tests (follow, late join, publisher moving A→B with
dedupe, forging peer refused, new read token) + state test, each fix
mutation-checked; Postgres locator + two media services over Postgres;
CLI relays with --advertise behind a round-robin HTTP+WS balancer:
publisher on A, Chromium viewer on B, "live", 208 lines. Suites: media
93, storage-postgres 20, server 55; Kotlin 92 (1 skipped), 0 failures.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-10-18 22:44:19 +02:00
parent aaec92429d
commit f1b4c1ade1
21 changed files with 704 additions and 36 deletions

View File

@@ -87,6 +87,8 @@ class LiveWatcher(
}
listener.onState(if (msg.optBoolean("live")) State.LIVE else State.WAITING)
}
// The publisher arrived or left after we joined.
"state" -> listener.onState(if (msg.optBoolean("live")) State.LIVE else State.WAITING)
"error" -> listener.onError(LiveRelayException(msg.optString("message", "relay error")))
}
}

View File

@@ -218,4 +218,42 @@ class ShadeLiveTest {
assertTrue("states: $states", states.count { it == LiveWatcher.State.LIVE } == 2 && LiveWatcher.State.RECONNECTING in states)
assertEquals(LiveWatcher.State.CLOSED, states.last())
}
@Test
fun watcherFollowsThePublisherArrivingAndLeavingAfterItJoined() {
val server = MockWebServer()
server.start()
val pub = LivePublisher(random(16), random(32), null)
val grant = pub.grant(from = "now", url = server.url("/v1/live/x").toString().replace("http", "ws"), readToken = random(32))
val first = pub.frame(LiveContent.Chunk(byteArrayOf(1)), keyframe = true)
server.enqueue(
MockResponse().withWebSocketUpgrade(object : WebSocketListener() {
override fun onClosing(webSocket: WebSocket, code: Int, reason: String) {
webSocket.close(1000, null)
}
override fun onMessage(webSocket: WebSocket, text: String) {
// Joins an empty channel; the publisher arrives, sends, leaves.
webSocket.send("""{"type":"ready","live":false}""")
webSocket.send("""{"type":"state","live":true}""")
webSocket.send(first.toByteString())
webSocket.send("""{"type":"state","live":false}""")
}
}),
)
val states = Collections.synchronizedList(ArrayList<LiveWatcher.State>())
val left = CountDownLatch(1)
val watcher = LiveWatcher(grant, object : LiveWatcher.Listener {
override fun onFrame(frame: ReceivedFrame) {}
override fun onState(state: LiveWatcher.State) {
states += state
if (state == LiveWatcher.State.WAITING && LiveWatcher.State.LIVE in states) left.countDown()
}
})
assertTrue("states: $states", left.await(10, TimeUnit.SECONDS))
watcher.close()
server.shutdown()
val seen = states.filter { it == LiveWatcher.State.WAITING || it == LiveWatcher.State.LIVE }
assertEquals(listOf(LiveWatcher.State.WAITING, LiveWatcher.State.LIVE, LiveWatcher.State.WAITING), seen)
}
}