Modern Apps git

← Modern-Apps.git

cast: negotiate audio, so an audio-only session can be refused by name

Commit
a421333b0995ce8438f23557e1dbd731c8b057e2
Author
vayun-mathur <[email protected]>
When
2026-08-23 18:11 UTC (3 months ago)
Parents
9361d4d

Diff

cast/protocol/src/main/java/com/vayunmathur/cast/protocol/CodecNegotiation.kt
diff --git a/cast/protocol/src/main/java/com/vayunmathur/cast/protocol/CodecNegotiation.kt b/cast/protocol/src/main/java/com/vayunmathur/cast/protocol/CodecNegotiation.ktindex e2bb2ab691..8d2f231dc2 100644--- a/cast/protocol/src/main/java/com/vayunmathur/cast/protocol/CodecNegotiation.kt+++ b/cast/protocol/src/main/java/com/vayunmathur/cast/protocol/CodecNegotiation.kt@@ -112,4 +112,14 @@ object CodecNegotiation {         theirs <= 0 -> mine         else -> minOf(mine, theirs)     }++    /**+     * Whether the TV can decode the one audio codec this protocol carries.+     *+     * Not part of [choose], because it is not a choice: there is one audio codec, so the only+     * question is whether the receiver has it. It matters on its own because an audio-only session+     * stands or falls on the answer - and a TV that cannot decode Opus has to be refused by name+     * rather than sent a stream it will silently drop.+     */+    fun canPlayAudio(receiver: DecoderLimits): Boolean = AudioCodec.Opus in receiver.audioCodecs }
cast/protocol/src/main/java/com/vayunmathur/cast/protocol/HandshakeMessages.kt
diff --git a/cast/protocol/src/main/java/com/vayunmathur/cast/protocol/HandshakeMessages.kt b/cast/protocol/src/main/java/com/vayunmathur/cast/protocol/HandshakeMessages.ktindex acdc83beb2..18d37b086f 100644--- a/cast/protocol/src/main/java/com/vayunmathur/cast/protocol/HandshakeMessages.kt+++ b/cast/protocol/src/main/java/com/vayunmathur/cast/protocol/HandshakeMessages.kt@@ -102,12 +102,46 @@ data class TvIdentity( @Serializable data class DecoderLimits(     val videoCodecs: List<CodecLimits> = emptyList(),+    /**+     * The audio codecs this TV can decode, which until now nothing asked about.+     *+     * Audio used to need no negotiation because it was never the only thing in a session: a TV with+     * no Opus decoder simply played no sound over a picture that still worked. An audio-only session+     * has no picture to carry it, so the same TV would sit in silence with nothing to explain it -+     * and there was no failure state to report, because there was never a question.+     *+     * Empty means the TV enumerated no audio decoder, and an audio-only session is refused by name.+     * Defaulted so a TV built against the older contract still parses; it will advertise nothing,+     * which is the honest reading of a receiver that was never asked.+     */+    val audioCodecs: List<AudioCodec> = emptyList(), ) {     fun forCodec(codec: VideoCodec): CodecLimits? = videoCodecs.firstOrNull { it.codec == codec }      val codecs: List<VideoCodec> get() = videoCodecs.map { it.codec } } +/**+ * The audio codecs this protocol carries, which is one.+ *+ * Every audio path in the repo already ends at 48 kHz Opus - `:musicbrainz` normalises its downloads+ * to it, the RTP timebase *is* its sample rate, and the phone's encoder produces nothing else - so a+ * list of one is not a placeholder for a negotiation that never happened. It is an enum rather than+ * a boolean because it is the wire form of "which", and a boolean would have to be renamed the day a+ * second codec appears.+ *+ * It is also the single definition of the Opus MIME type, which was previously written out+ * independently on each side: the phone's encoder support and the TV's decoder lookup are the two+ * ends of one agreement, and two literals could drift.+ *+ * [mimeType] is spelled out rather than taken from `MediaFormat`, for the reason [VideoCodec] gives.+ */+@Serializable+enum class AudioCodec(val mimeType: String, val label: String) {+    @SerialName("OPUS")+    Opus("audio/opus", "Opus"),+}+ /**  * The video codecs this protocol carries, in no particular order - [CodecNegotiation] holds the  * preference.@@ -246,14 +280,15 @@ data class StreamConfig(     val audioSsrc: Long,     val videoSsrc: Long,     /**-     * The codec the payload is in.+     * The codec the payload is in, or null when the session carries no video.      *-     * Required, with no default: the version bump is what makes that safe, and a defaulted codec-     * would be a build silently agreeing to decode something it was never told about. The receiver-     * builds its decoder from this, so it is also what decides whether a [VideoCodecConfig] has to-     * arrive before the picture can start.+     * Non-null exactly when [video] is set. Nullable rather than absent because an audio-only+     * session has no video codec to name and a defaulted one would be a receiver silently agreeing+     * to decode something nobody mentioned - the failure the required field was added to prevent.+     * The receiver builds its decoder from this, so it is also what decides whether a+     * [VideoCodecConfig] has to arrive before the picture can start.      */-    val videoCodec: VideoCodec,+    val videoCodec: VideoCodec? = null,     /**      * The name of the app whose content this is, or empty for screen mirroring.      *
cast/protocol/src/test/java/com/vayunmathur/cast/protocol/AudioNegotiationTest.kt
diff --git a/cast/protocol/src/test/java/com/vayunmathur/cast/protocol/AudioNegotiationTest.kt b/cast/protocol/src/test/java/com/vayunmathur/cast/protocol/AudioNegotiationTest.ktnew file mode 100644index 0000000000..afa22ef42a--- /dev/null+++ b/cast/protocol/src/test/java/com/vayunmathur/cast/protocol/AudioNegotiationTest.kt@@ -0,0 +1,178 @@+package com.vayunmathur.cast.protocol++import kotlinx.serialization.encodeToString+import kotlin.test.Test+import kotlin.test.assertEquals+import kotlin.test.assertFalse+import kotlin.test.assertIs+import kotlin.test.assertNotNull+import kotlin.test.assertNull+import kotlin.test.assertTrue++/**+ * Tests for negotiating audio, which until now was never negotiated at all.+ *+ * Audio needed no agreement while every session had a picture: a TV with no Opus decoder played a+ * silent video, which is a poor outcome but a legible one. An audio-only session has no picture to+ * fall back to, so the same TV would sit in silence with nothing to explain it and no failure state+ * to report - and that is the failure these tests exist to keep out.+ *+ * The other half is a session with no video codec, which the wire format could not previously+ * express. A codec field that is absent when there is no video and present when there is has to+ * round-trip both ways, because a receiver that read a default would be agreeing to decode something+ * nobody named.+ */+class AudioNegotiationTest {++    private val secret = ByteArray(SessionKeys.SECRET_BYTES) { (it * 7 + 3).toByte() }++    // ------------------------------------------------------------------+    // Advertisement+    // ------------------------------------------------------------------++    @Test+    fun `a TV that was never asked advertises no audio codec`() {+        // The default is what a receiver built against the older contract sends, and reading it as+        // "no Opus" is the honest interpretation: it did not say it had one.+        assertEquals(emptyList(), DecoderLimits().audioCodecs)+        assertFalse(CodecNegotiation.canPlayAudio(DecoderLimits()))+    }++    @Test+    fun `a TV advertising Opus can be sent audio`() {+        val limits = DecoderLimits(audioCodecs = listOf(AudioCodec.Opus))+        assertTrue(CodecNegotiation.canPlayAudio(limits))+    }++    @Test+    fun `advertising a video decoder says nothing about audio`() {+        // The two are independent hardware questions, and a TV with an AV1 decoder and no Opus+        // decoder is exactly the device this failure has to be reportable on.+        val videoOnly = DecoderLimits(+            videoCodecs = listOf(CodecLimits(VideoCodec.Av1, 3840, 2160, 60, 20_000_000)),+        )+        assertFalse(CodecNegotiation.canPlayAudio(videoOnly))+        assertEquals(listOf(VideoCodec.Av1), videoOnly.codecs)+    }++    @Test+    fun `the audio codec's wire name is pinned`() {+        // Both ends read this string. A rename that looked like a refactor would make every TV+        // report no audio decoder at all, which is indistinguishable from a TV that has none.+        val json = ControlJson.encodeToString(+            TvIdentity(+                version = PROTOCOL_VERSION,+                receiverName = "Living room",+                receiverId = "tv",+                publicBundle = "",+                limits = DecoderLimits(audioCodecs = listOf(AudioCodec.Opus)),+            ) as ControlMessage,+        )+        assertTrue(json.contains("\"OPUS\""), "got $json")+    }++    @Test+    fun `audio limits survive a round trip alongside video limits`() {+        val limits = DecoderLimits(+            videoCodecs = listOf(CodecLimits(VideoCodec.Hevc, 4096, 4096, 60, 40_000_000)),+            audioCodecs = listOf(AudioCodec.Opus),+        )+        val message = TvIdentity(+            version = PROTOCOL_VERSION,+            receiverName = "Living room",+            receiverId = "tv",+            publicBundle = "bundle",+            limits = limits,+        )+        val back = assertIs<TvIdentity>(+            ControlJson.decodeFromString<ControlMessage>(+                ControlJson.encodeToString(message as ControlMessage),+            ),+        )+        assertEquals(limits, back.limits)+    }++    @Test+    fun `a TV that omits the audio field entirely still parses`() {+        // Forwards compatibility in the direction that matters: an older TV's TV_IDENTITY has no+        // audioCodecs at all, and a phone that refused to parse it would refuse to cast anything.+        val json = """+            {"type":"TV_IDENTITY","version":$PROTOCOL_VERSION,"receiverName":"Old TV",+             "receiverId":"tv","publicBundle":"b","limits":{"videoCodecs":[]}}+        """.trimIndent()+        val back = assertIs<TvIdentity>(ControlJson.decodeFromString<ControlMessage>(json))+        assertEquals(emptyList(), back.limits.audioCodecs)+    }++    // ------------------------------------------------------------------+    // A session with no video+    // ------------------------------------------------------------------++    @Test+    fun `an audio-only stream config carries no video codec`() {+        val config = audioOnlyConfig()+        assertNull(config.videoCodec)+        val back = assertIs<StreamConfig>(+            ControlJson.decodeFromString<ControlMessage>(+                ControlJson.encodeToString(config as ControlMessage),+            ),+        )+        assertNull(back.videoCodec, "a defaulted codec would be a TV agreeing to decode a guess")+        assertFalse(back.video)+        assertTrue(back.audio)+    }++    @Test+    fun `a video stream config still requires its codec to survive the trip`() {+        val config = audioOnlyConfig().copy(video = true, videoCodec = VideoCodec.Av1)+        val back = assertIs<StreamConfig>(+            ControlJson.decodeFromString<ControlMessage>(+                ControlJson.encodeToString(config as ControlMessage),+            ),+        )+        assertEquals(VideoCodec.Av1, back.videoCodec)+    }++    @Test+    fun `an audio-only session negotiates one stream and reports no video`() {+        // `ReceiverController.pump` gates its whole decoder path on hasVideo, so this is the flag+        // that keeps an audio-only session from waiting for a picture that was never coming.+        val negotiation = Negotiation.of(+            config = audioOnlyConfig(),+            ready = StreamReady(udpPort = 4711, audioSsrc = 2, videoSsrc = 50_002),+            keys = SessionKeys.of(secret),+        )+        assertFalse(negotiation.hasVideo)+        assertNull(negotiation.video)+        val audio = assertNotNull(negotiation.audio)+        assertEquals(StreamKind.Audio, audio.kind)+        assertEquals(StreamConstants.AUDIO_TIMEBASE, audio.timebase)+        assertEquals(1, negotiation.streams.size)+    }++    // ------------------------------------------------------------------+    // The format is stated once+    // ------------------------------------------------------------------++    @Test+    fun `the audio format has one definition on this side of the broker`() {+        // The phone's encoder and the TV's decoder used to state the sample rate, the channel count+        // and the MIME type independently. Two of those are the RTP timebase and the Opus header's+        // own fields, so a drift would have been a stream neither end could explain.+        assertEquals(48_000, StreamConstants.AUDIO_TIMEBASE)+        assertEquals(2, StreamConstants.AUDIO_CHANNELS)+        assertEquals("audio/opus", AudioCodec.Opus.mimeType)+    }++    private fun audioOnlyConfig() = StreamConfig(+        width = 0,+        height = 0,+        frameRate = 0,+        bitRate = 0,+        audio = true,+        video = false,+        audioSsrc = 1,+        videoSsrc = 50_001,+        videoCodec = null,+    )+}
cast/src/main/java/com/vayunmathur/cast/platform/mirror/EncoderSupport.kt
diff --git a/cast/src/main/java/com/vayunmathur/cast/platform/mirror/EncoderSupport.kt b/cast/src/main/java/com/vayunmathur/cast/platform/mirror/EncoderSupport.ktindex d158dc8408..56f727baf7 100644--- a/cast/src/main/java/com/vayunmathur/cast/platform/mirror/EncoderSupport.kt+++ b/cast/src/main/java/com/vayunmathur/cast/platform/mirror/EncoderSupport.kt@@ -2,8 +2,8 @@ package com.vayunmathur.cast.platform.mirror  import android.media.MediaCodecInfo import android.media.MediaCodecList-import android.media.MediaFormat import android.util.Log+import com.vayunmathur.cast.protocol.AudioCodec import com.vayunmathur.cast.protocol.CodecLimits import com.vayunmathur.cast.protocol.CodecNegotiation import com.vayunmathur.cast.protocol.VideoCodec@@ -29,7 +29,14 @@ private const val TAG = "EncoderSupport"  */ object EncoderSupport { -    const val AUDIO_MIME = MediaFormat.MIMETYPE_AUDIO_OPUS+    /**+     * From the protocol rather than from `MediaFormat`.+     *+     * The TV looks up its decoder by the same name, and the two used to be written out+     * independently on each side - one agreement kept in two places, which is one place too many.+     * A `val` rather than a `const` because the protocol's enum is where it now lives.+     */+    val AUDIO_MIME = AudioCodec.Opus.mimeType      fun audioEncoderName(): String? {         val codecs = MediaCodecList(MediaCodecList.REGULAR_CODECS).codecInfos
cast/src/main/java/com/vayunmathur/cast/platform/mirror/OpusEncoder.kt
diff --git a/cast/src/main/java/com/vayunmathur/cast/platform/mirror/OpusEncoder.kt b/cast/src/main/java/com/vayunmathur/cast/platform/mirror/OpusEncoder.ktindex 7177c4018f..d19998ff92 100644--- a/cast/src/main/java/com/vayunmathur/cast/platform/mirror/OpusEncoder.kt+++ b/cast/src/main/java/com/vayunmathur/cast/platform/mirror/OpusEncoder.kt@@ -3,6 +3,7 @@ package com.vayunmathur.cast.platform.mirror import android.media.MediaCodec import android.media.MediaFormat import android.util.Log+import com.vayunmathur.cast.protocol.StreamConstants  private const val TAG = "OpusEncoder" @@ -111,10 +112,18 @@ class OpusEncoder {         get() = samplesWritten * 1_000_000L / SAMPLE_RATE      companion object {-        /** Matches the OFFER's `1/48000` timebase, and `CastContract.AUDIO_SAMPLE_RATE`. */-        const val SAMPLE_RATE = 48_000-        const val CHANNELS = 2-        const val BIT_RATE = 128_000+        /**+         * The protocol's audio format, not a third copy of it.+         *+         * `StreamConstants` already had to state the sample rate because the RTP timebase *is* the+         * sample rate, and the channel count and bitrate with it. This encoder producing its own+         * numbers meant two definitions that had to agree and nothing that would notice if they+         * stopped. `CastContract` still states them separately, and has to: `:sdk:cast` may not+         * depend on the protocol, which is the whole point of brokering.+         */+        const val SAMPLE_RATE = StreamConstants.AUDIO_TIMEBASE+        const val CHANNELS = StreamConstants.AUDIO_CHANNELS+        const val BIT_RATE = StreamConstants.AUDIO_BITRATE          /** 20 ms of stereo 16-bit PCM, which is the frame size Opus wants. */         const val FRAME_BYTES = SAMPLE_RATE / 50 * 2 * CHANNELS
cast/tv/src/main/java/com/vayunmathur/cast/tv/platform/AudioPlayer.kt
diff --git a/cast/tv/src/main/java/com/vayunmathur/cast/tv/platform/AudioPlayer.kt b/cast/tv/src/main/java/com/vayunmathur/cast/tv/platform/AudioPlayer.ktindex d076a66c13..3b41f319bf 100644--- a/cast/tv/src/main/java/com/vayunmathur/cast/tv/platform/AudioPlayer.kt+++ b/cast/tv/src/main/java/com/vayunmathur/cast/tv/platform/AudioPlayer.kt@@ -7,6 +7,7 @@ import android.media.MediaCodec import android.media.MediaCodecList import android.media.MediaFormat import android.util.Log+import com.vayunmathur.cast.protocol.AudioCodec import com.vayunmathur.cast.protocol.StreamConstants  private const val TAG = "AudioPlayer"@@ -221,8 +222,17 @@ class AudioPlayer {         codec = null     } -    private companion object {-        const val AUDIO_MIME = MediaFormat.MIMETYPE_AUDIO_OPUS+    /**+     * Not private, unlike it used to be.+     *+     * Everything in here is still an implementation detail except the two capability lookups at the+     * bottom, and those had to become reachable: `ReceiverController` cannot advertise an Opus+     * decoder it is not allowed to ask about, and until it could, an audio-only session had no way+     * to be refused.+     */+    companion object {+        /** 200 ms of stereo 16-bit at 48 kHz, as a floor under whatever the platform asks for. */+        private const val MIN_TRACK_BYTES = 48_000 / 5 * 2 * 2          /**          * How many times the decoder may be rebuilt before audio is given up on.@@ -231,10 +241,7 @@ class AudioPlayer {          * on its first use, which one rebuild would have cleared. A codec broken structurally fails          * again at once, so this is spent in milliseconds and the log stays short either way.          */-        const val MAX_RESTARTS = 3--        /** 200 ms of stereo 16-bit at 48 kHz, as a floor under whatever the platform asks for. */-        const val MIN_TRACK_BYTES = 48_000 / 5 * 2 * 2+        private const val MAX_RESTARTS = 3          /**          * `csd-1` and `csd-2`: the codec delay and the seek pre-roll, in little-endian nanoseconds.@@ -244,7 +251,7 @@ class AudioPlayer {          * them. They are supplied rather than omitted because the decoder reads three csd buffers and          * silently tolerates being given one.          */-        fun nanosecondsLe(value: Long): ByteArray = ByteArray(8) { i ->+        private fun nanosecondsLe(value: Long): ByteArray = ByteArray(8) { i ->             ((value ushr (8 * i)) and 0xff).toByte()         } @@ -258,7 +265,7 @@ class AudioPlayer {          * Pre-skip 0 and output gain 0 because the phone's encoder applies neither, and channel          * mapping family 0 because two channels are plain stereo.          */-        fun opusIdentificationHeader(): ByteArray {+        private fun opusIdentificationHeader(): ByteArray {             val rate = StreamConstants.AUDIO_TIMEBASE             return byteArrayOf(                 'O'.code.toByte(), 'p'.code.toByte(), 'u'.code.toByte(), 's'.code.toByte(),@@ -275,6 +282,20 @@ class AudioPlayer {             )         } +        /** From the protocol, so the phone's encoder and this decoder cannot look for different things. */+        val AUDIO_MIME = AudioCodec.Opus.mimeType++        /**+         * What this TV can decode, for `TV_IDENTITY`.+         *+         * Mirrors `VideoDecoder.limits()`, and exists for the same reason: the phone has to be able+         * to refuse a session it cannot serve, by name, before starting one. Until audio-only was+         * possible nobody asked - a TV with no Opus decoder just played a silent picture - so an+         * audio-only session would have sat in silence with no failure to report.+         */+        fun limits(): List<AudioCodec> =+            if (decoderName() != null) listOf(AudioCodec.Opus) else emptyList()+         fun decoderName(): String? {             val codecs = MediaCodecList(MediaCodecList.REGULAR_CODECS).codecInfos             for (info in codecs) {
cast/tv/src/main/java/com/vayunmathur/cast/tv/platform/ReceiverController.kt
diff --git a/cast/tv/src/main/java/com/vayunmathur/cast/tv/platform/ReceiverController.kt b/cast/tv/src/main/java/com/vayunmathur/cast/tv/platform/ReceiverController.ktindex cbeb361510..0e847d77a1 100644--- a/cast/tv/src/main/java/com/vayunmathur/cast/tv/platform/ReceiverController.kt+++ b/cast/tv/src/main/java/com/vayunmathur/cast/tv/platform/ReceiverController.kt@@ -259,7 +259,11 @@ object ReceiverController {         val identity = PqcIdentity.loadOrCreate(store, prefix = "castTv")         val deviceId = store.deviceId()         val name = store.friendlyName(fallback = defaultName())-        val limits = VideoDecoder.limits()+        // Audio is advertised alongside video for the first time. It never needed negotiating while+        // every session had a picture - a TV with no Opus decoder simply played silence - but an+        // audio-only session stands or falls on it, and the phone can only refuse by name if it was+        // told.+        val limits = VideoDecoder.limits().copy(audioCodecs = AudioPlayer.limits())          val server = try {             ServerSocket(0)@@ -446,6 +450,13 @@ object ReceiverController {      * record (marker 1, version 1); `0x0a` leads a sequence header OBU with a size field.      */     private fun onVideoCodecConfig(message: VideoCodecConfig, config: StreamConfig) {+        val codec = config.videoCodec+        if (codec == null) {+            // An audio-only session has no decoder to configure. Ignored rather than treated as a+            // fault: it means the phone sent one anyway, which is harmless and says nothing useful.+            Log.w(TAG, "a codec config arrived for a session with no video")+            return+        }         val csd = ProtocolBase64.decode(message.csd)         if (csd == null || csd.isEmpty()) {             Log.w(TAG, "the phone sent an unreadable codec config")@@ -458,7 +469,7 @@ object ReceiverController {         }         Log.i(             TAG,-            "codec config for ${config.videoCodec.label}: ${csd.size} bytes, $shape, " ++            "codec config for ${codec.label}: ${csd.size} bytes, $shape, " +                 "[${csd.take(16).joinToString(" ") { "%02x".format(it) }}]; " +                 "the stream is ${config.width}x${config.height}",         )@@ -597,12 +608,12 @@ object ReceiverController {         }         Log.i(             TAG,-            "receiving ${config.videoCodec.label} ${config.width}x${config.height} @ " ++            "receiving ${config.videoCodec?.label ?: "audio only"} ${config.width}x${config.height} @ " +                 "${config.frameRate}fps (${config.bitRate / 1_000_000.0} Mbit/s) on udp " +                 "${socket.localPort}, " +                 "rcvbuf=${runCatching { socket.receiveBufferSize }.getOrDefault(0)}B, " +                 "playout=${StreamConstants.TARGET_DELAY_MS}ms" +-                if (config.videoCodec.needsCodecConfig) "; waiting for its codec config" else "",+                if (config.videoCodec?.needsCodecConfig == true) "; waiting for its codec config" else "",         )         mediaJob = scope.launch { pump(socket, negotiation, config, channel) }     }@@ -632,6 +643,10 @@ object ReceiverController {         channel: ControlChannel,     ) {         var decoder: VideoDecoder? = null+        // Hoisted into a local because the property comes from another module, where Kotlin will not+        // smart-cast it - and every use below is inside a branch that has already established there+        // is video.+        val videoCodec = config.videoCodec         val player = if (config.audio) AudioPlayer().takeIf { it.start() } else null         val playout = PlayoutQueue(             targetDelayMs = StreamConstants.TARGET_DELAY_MS.toLong(),@@ -676,13 +691,13 @@ object ReceiverController {                         playout.clear()                         media.requestKeyFrame(StreamKind.Video)                     }-                } else if (decoder == null && negotiation.hasVideo) {+                } else if (decoder == null && negotiation.hasVideo && videoCodec != null) {                     // Only consulted for a codec that needs it. An H.265 session finds its parameter                     // sets in the stream, and installing a `csd-0` it was not expecting would fail the                     // configure - so the field is read through the codec's own contract rather than                     // passed on because it happened to be set.-                    val codecConfig = videoCodecConfig.takeIf { config.videoCodec.needsCodecConfig }-                    if (config.videoCodec.needsCodecConfig && codecConfig == null) {+                    val codecConfig = videoCodecConfig.takeIf { videoCodec.needsCodecConfig }+                    if (videoCodec.needsCodecConfig && codecConfig == null) {                         // **The one genuinely new wait.** This widens the entry condition of a state                         // the pipeline already survives - `decoder == null`, which `MediaReceiver`'s                         // pre-session video drop, `ReceiverSession.synchronised` and PLI already make@@ -692,12 +707,12 @@ object ReceiverController {                             codecConfigWaitStartedAt = waiting                             Log.i(                                 TAG,-                                "surface ready; waiting for ${config.videoCodec.label}'s codec config",+                                "surface ready; waiting for ${videoCodec.label}'s codec config",                             )                         } else if (waiting - codecConfigWaitStartedAt >= CODEC_CONFIG_TIMEOUT_MS) {                             Log.w(                                 TAG,-                                "no ${config.videoCodec.label} codec config after " ++                                "no ${videoCodec.label} codec config after " +                                     "${CODEC_CONFIG_TIMEOUT_MS}ms; nothing can be decoded",                             )                             // Named so the phone can remember it and start the next session on the@@ -715,7 +730,7 @@ object ReceiverController {                             return                         }                     } else {-                        val started = VideoDecoder(activeSurface, config.videoCodec)+                        val started = VideoDecoder(activeSurface, videoCodec)                         if (started.start(config.width, config.height, codecConfig)) {                             decoder = started                             Log.i(TAG, "decoder up; the picture starts at the next key frame")
cast/tv/src/main/java/com/vayunmathur/cast/tv/platform/ReceiverUiContract.kt
diff --git a/cast/tv/src/main/java/com/vayunmathur/cast/tv/platform/ReceiverUiContract.kt b/cast/tv/src/main/java/com/vayunmathur/cast/tv/platform/ReceiverUiContract.ktindex 01324c57ba..a1a3e0626d 100644--- a/cast/tv/src/main/java/com/vayunmathur/cast/tv/platform/ReceiverUiContract.kt+++ b/cast/tv/src/main/java/com/vayunmathur/cast/tv/platform/ReceiverUiContract.kt@@ -61,6 +61,17 @@ enum class ReceiverFailure {     /** No hardware H.265 or AV1 decoder. Nothing about the phone or the network will help. */     NoDecoder, +    /**+     * No Opus decoder, on a session that is nothing but audio.+     *+     * Its own failure rather than [NoDecoder]'s, because the answer is different and because the+     * alternative was the worst outcome available: before audio was negotiated at all, this TV+     * would have accepted the session and then sat in total silence with nothing on screen to say+     * why. A video session with no sound is still worth having, so this is reported only when sound+     * was the whole session.+     */+    NoAudioDecoder,+     /**      * There is a decoder and somewhere to draw, and the codec settings it needed never arrived.      *
cast/tv/src/main/java/com/vayunmathur/cast/tv/ui/ReceiverContent.kt
diff --git a/cast/tv/src/main/java/com/vayunmathur/cast/tv/ui/ReceiverContent.kt b/cast/tv/src/main/java/com/vayunmathur/cast/tv/ui/ReceiverContent.ktindex b97fe7c3d4..7a27636641 100644--- a/cast/tv/src/main/java/com/vayunmathur/cast/tv/ui/ReceiverContent.kt+++ b/cast/tv/src/main/java/com/vayunmathur/cast/tv/ui/ReceiverContent.kt@@ -106,6 +106,7 @@ fun ReceiverContent(state: ReceiverUiState, modifier: Modifier = Modifier) {                 stringResource(                     when (phase.reason) {                         ReceiverFailure.NoDecoder -> R.string.tv_no_decoder+                        ReceiverFailure.NoAudioDecoder -> R.string.tv_no_audio_decoder                         ReceiverFailure.MissingCodecConfig ->                             R.string.tv_missing_codec_config                         ReceiverFailure.Handshake -> R.string.tv_stream_failed
cast/tv/src/main/res/values/strings.xml
diff --git a/cast/tv/src/main/res/values/strings.xml b/cast/tv/src/main/res/values/strings.xmlindex 87c39187b2..801feb6e39 100644--- a/cast/tv/src/main/res/values/strings.xml+++ b/cast/tv/src/main/res/values/strings.xml@@ -25,6 +25,7 @@     <!-- Mirroring -->     <string name="tv_mirroring">Receiving from %1$s</string>     <string name="tv_no_decoder">This TV has no usable H.265 or AV1 decoder</string>+    <string name="tv_no_audio_decoder">This TV has no Opus audio decoder, so it cannot play music from your phone</string>     <string name="tv_missing_codec_config">Your phone did not send the video settings this TV needed. Try mirroring again.</string>     <string name="tv_stream_failed">The stream stopped</string>     <!-- Transport overlay, driven from the TV remote -->