feat(audio-rooms): subscribeCatalog on NestsListener (moq-lite only)

Surface moq-lite's `catalog.json` track on the NestsListener API.
The catalog publishes one JSON object per group describing the
broadcast (codec, sample rate, optional speaker-side hints) and
is the canonical channel a watcher reads first to discover
available tracks.

Wire-up:
  * NestsListener.subscribeCatalog(pubkey) — interface method with
    a default body that throws UnsupportedOperationException, so
    the IETF DefaultNestsListener fails loud rather than returning
    a flow that never delivers data.
  * MoqLiteNestsListener overrides it to wrap
    `session.subscribe(broadcast = pubkey, track = "catalog.json")`
    through the same MoqObject mapping the audio path uses.
  * ReconnectingNestsListener routes both subscribeSpeaker and
    subscribeCatalog through a shared `reissuingSubscribe` helper —
    catalog handles also survive session swaps via the
    MutableSharedFlow re-issuance pump.

Tests:
  * NestsListenerCatalogTest — verifies the interface default
    rejects with UnsupportedOperationException so a caller wired
    against the IETF reference path fails fast.

VM/UI consumers (e.g. "speaker codec" tooltip) are intentionally
out of scope for this commit; this exposes the capability so a
follow-up can plug in a JSON parser + per-pubkey catalog cache.
This commit is contained in:
Claude
2026-04-27 01:20:22 +00:00
parent 308b0bf86b
commit e484e3b210
4 changed files with 101 additions and 25 deletions
@@ -59,16 +59,20 @@ class MoqLiteNestsListener internal constructor(
) : NestsListener {
override val state: StateFlow<NestsListenerState> = mutableState.asStateFlow()
override suspend fun subscribeSpeaker(speakerPubkeyHex: String): SubscribeHandle {
override suspend fun subscribeSpeaker(speakerPubkeyHex: String): SubscribeHandle = wrapSubscription(broadcast = speakerPubkeyHex, track = AUDIO_TRACK)
override suspend fun subscribeCatalog(speakerPubkeyHex: String): SubscribeHandle = wrapSubscription(broadcast = speakerPubkeyHex, track = CATALOG_TRACK)
private suspend fun wrapSubscription(
broadcast: String,
track: String,
): SubscribeHandle {
check(state.value is NestsListenerState.Connected) {
"NestsListener.subscribeSpeaker requires Connected state, was ${state.value}"
"NestsListener.subscribe requires Connected state, was ${state.value}"
}
val handle =
try {
session.subscribe(
broadcast = speakerPubkeyHex,
track = AUDIO_TRACK,
)
session.subscribe(broadcast = broadcast, track = track)
} catch (e: MoqLiteSubscribeException) {
// The IETF SubscribeHandle path conventionally surfaces
// protocol-level rejections through the same exception
@@ -52,6 +52,26 @@ interface NestsListener {
*/
suspend fun subscribeSpeaker(speakerPubkeyHex: String): SubscribeHandle
/**
* Subscribe to a speaker's `catalog.json` track — moq-lite's
* canonical channel for "what is this broadcast publishing" JSON
* metadata (codec, sample rate, optional speaker-side hints).
* The publisher emits one JSON object per group; consumers
* typically read the latest frame and parse it.
*
* Throws on the IETF reference path because moq-transport-17
* doesn't define a catalog convention. Callers that want
* cross-protocol behavior should `runCatching` this.
*
* @throws UnsupportedOperationException on the IETF listener.
* @throws MoqProtocolException if the publisher rejects the subscription.
* @throws IllegalStateException if the listener is not in [NestsListenerState.Connected].
*/
suspend fun subscribeCatalog(speakerPubkeyHex: String): SubscribeHandle =
throw UnsupportedOperationException(
"subscribeCatalog is moq-lite-only; IETF listener has no catalog channel.",
)
/** Tear down the MoQ session + underlying transport. Idempotent. */
suspend fun close()
}
@@ -156,7 +156,20 @@ private class ReconnectingHandle(
) : NestsListener {
override val state: StateFlow<NestsListenerState> = mutableState.asStateFlow()
override suspend fun subscribeSpeaker(speakerPubkeyHex: String): SubscribeHandle {
override suspend fun subscribeSpeaker(speakerPubkeyHex: String): SubscribeHandle = reissuingSubscribe { listener -> listener.subscribeSpeaker(speakerPubkeyHex) }
override suspend fun subscribeCatalog(speakerPubkeyHex: String): SubscribeHandle = reissuingSubscribe { listener -> listener.subscribeCatalog(speakerPubkeyHex) }
/**
* Open a SubscribeHandle whose `objects` flow survives session
* swaps. Each time the wrapper opens a fresh inner listener, the
* pump cancels the prior subscription and calls [opener] against
* the new session, forwarding its frames into the consumer-facing
* [MutableSharedFlow]. Same shape for both `subscribeSpeaker`
* and `subscribeCatalog`; the only difference is which inner
* subscribe call to make.
*/
private fun reissuingSubscribe(opener: suspend (NestsListener) -> SubscribeHandle): SubscribeHandle {
// Require a live (or just-connected) session at call time —
// matches the existing IETF / moq-lite listener contract so
// a caller can't accidentally subscribe in Idle and stall
@@ -170,10 +183,6 @@ private class ReconnectingHandle(
// Re-subscribe pump: every time activeListener changes, drop
// the prior subscription (collectLatest cancels the inner
// body) and open a new one against the fresh session.
// Wait for the inner listener to reach Connected before
// calling subscribeSpeaker — IETF + moq-lite listeners both
// throw IllegalStateException if subscribed before Connected,
// and a fresh session may go through Connecting first.
val pumpJob =
scope.launch {
activeListener.collectLatest { listener ->
@@ -186,36 +195,23 @@ private class ReconnectingHandle(
}
if (terminalOrConnected !is NestsListenerState.Connected) return@collectLatest
val handle =
runCatching { listener.subscribeSpeaker(speakerPubkeyHex) }
runCatching { opener(listener) }
.getOrNull() ?: return@collectLatest
liveHandleRef.set(handle)
try {
handle.objects.collect { frames.emit(it) }
} finally {
// Either the inner session ended, or we're
// being replaced because activeListener
// changed. Drop the dead reference; the
// inner unsubscribe is handled by the
// session's own teardown.
if (liveHandleRef.get() === handle) liveHandleRef.set(null)
}
}
}
return SubscribeHandle(
// Synthetic ids — the consumer-facing handle is logical,
// not bound to any one session's wire ids. -1 is a
// reserved sentinel callers shouldn't compare against.
subscribeId = -1L,
trackAlias = -1L,
ok = SYNTH_OK,
objects = frames.asSharedFlow(),
unsubscribeAction = {
// Capture the live underlying handle BEFORE cancelling
// the pump — the pump's finally clears `liveHandleRef`
// on cancellation, and we need a stable reference to
// forward the user-initiated unsubscribe to the live
// session's wire SUBSCRIBE_DONE.
val live = liveHandleRef.getAndSet(null)
pumpJob.cancel()
live?.let { runCatching { it.unsubscribe() } }