cast: recover audio from a decoder fault, refit after a mode change
- Commit
40147f300cc61e5122029c5784143e35809cdf21- Author
- vayun-mathur <[email protected]>
- When
- 2026-08-23 05:10 UTC (3 months ago)
- Parents
cc6e5ea
Diff
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 3c2afd5490..e0ece9dac5 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@@ -24,16 +24,24 @@ private const val TIMEOUT_US = 10_000L * `MODE_STREAM` with `PERFORMANCE_MODE_LOW_LATENCY`: the phone's frames are already 20 ms and * arriving in real time, so the only thing extra buffering would buy is lip-sync error. *- * **No `csd-0`.** Android's Opus decoder wants an identification header, and the encoder on the phone- * does not send its codec-config buffer over the wire. So the header is synthesised here from the+ * **The codec configuration is synthesised.** Android's Opus decoder wants its headers, and the encoder+ * on the phone does not send its codec-config buffer over the wire. So they are rebuilt here from the * parameters both ends already agree on - they are fixed constants in [StreamConstants], not- * negotiated, which is what makes reconstructing it sound rather than a guess.+ * negotiated, which is what makes reconstructing them sound rather than a guess. *- * **A failure here is terminal, and that is the fix for a measured disaster.** [play] used to catch- * every exception and retry on the next packet, so a codec that entered its released state a few- * seconds in logged a stack trace per 20 ms packet from then on: 2,634 traces in 38,233 logcat lines- * in one session, on the thread that has to be reading datagrams. The storm cost more than the lost- * audio ever did, and it was voluminous enough to evict its own root cause from the log buffer.+ * **Three csd buffers, not one.** The decoder expects `csd-0` = `OpusHead`, `csd-1` = the codec delay+ * and `csd-2` = the seek pre-roll, the latter two as little-endian nanoseconds - the same three+ * ExoPlayer builds in `OpusUtil.buildInitializationData`. Only `csd-0` used to be supplied, which+ * `configure()` accepts without complaint and then leaves the component to make the best of.+ *+ * **A failure is survivable, and bounded.** [play] used to catch every exception and retry on the next+ * packet, so a codec that entered its released state a few seconds in logged a stack trace per 20 ms+ * packet from then on: 2,634 traces in 38,233 logcat lines in one session, on the thread that has to be+ * reading datagrams. Giving up for good stopped the storm but overcorrected: one transient error became+ * a silent session with no way back, which is what hardware then measured - audio died 3 seconds in and+ * stayed dead while 487 packets arrived to be decoded into nothing. The codec is rebuilt instead, at+ * most [MAX_RESTARTS] times, and only then given up on. The log stays bounded because the number of+ * attempts is. */ class AudioPlayer { @@ -50,6 +58,17 @@ class AudioPlayer { var failed: Boolean = false private set + /**+ * How many times the codec has been rebuilt after a failure.+ *+ * Counted rather than latched on the first error, so a transient fault costs a gap in the sound+ * instead of the rest of the session - but bounded, because a codec that is broken for a structural+ * reason will fail again immediately and rebuilding it per packet would be the log storm the old+ * latch existed to prevent.+ */+ var restarts: Int = 0+ private set+ /** False when this TV has no Opus decoder, which leaves the picture but no sound. */ fun start(): Boolean { val name = decoderName()@@ -64,7 +83,11 @@ class AudioPlayer { StreamConstants.AUDIO_CHANNELS, ).apply { setInteger(MediaFormat.KEY_LOW_LATENCY, 1)+ // All three, in the order the decoder reads them. csd-0 alone configures without+ // complaint, which is what made its absence easy to miss. setByteBuffer("csd-0", java.nio.ByteBuffer.wrap(opusIdentificationHeader()))+ setByteBuffer("csd-1", java.nio.ByteBuffer.wrap(nanosecondsLe(0)))+ setByteBuffer("csd-2", java.nio.ByteBuffer.wrap(nanosecondsLe(0))) } val created = MediaCodec.createByCodecName(name) // Published before the AudioTrack is built, because the catch below releases through the@@ -110,8 +133,8 @@ class AudioPlayer { /** * Queue one Opus packet and write out whatever the decoder finished. *- * Called from the socket loop, so the cost of failing has to be bounded: the first exception- * gives up on audio for the rest of the session rather than being retried per packet.+ * Called from the socket loop, so the cost of failing has to be bounded - see the class comment for+ * why that is a rebuild rather than either a per-packet retry or a permanent surrender. */ fun play(data: ByteArray, presentationTimeUs: Long) { if (failed) return@@ -140,11 +163,34 @@ class AudioPlayer { activeCodec.releaseOutputBuffer(out, false) } } catch (e: Exception) {- // One trace, once. Whatever put the codec in this state will not be undone by the next- // packet, and a realtime path may not log per packet.+ recover(e)+ }+ }++ /**+ * Rebuild the codec, or give up if it has already been rebuilt too often.+ *+ * One trace per attempt, never per packet. The trace matters as much as the recovery does: the+ * hardware failure this replaces was a codec found in its *released* state on the very first+ * `dequeueInputBuffer`, with nothing in the log to say what had released it, so the attempt count+ * and the codec's own name go in the line to narrow that down next time.+ */+ private fun recover(cause: Exception) {+ release()+ if (restarts >= MAX_RESTARTS) { failed = true- Log.w(TAG, "audio playback failed for good; the rest of this session is silent", e)- release()+ Log.w(+ TAG,+ "audio failed $MAX_RESTARTS times; the rest of this session is silent",+ cause,+ )+ return+ }+ restarts+++ Log.w(TAG, "audio playback failed; rebuilding the decoder (attempt $restarts)", cause)+ if (!start()) {+ failed = true+ Log.w(TAG, "the rebuilt audio decoder would not start; this session is silent") } } @@ -160,9 +206,30 @@ class AudioPlayer { private companion object { const val AUDIO_MIME = MediaFormat.MIMETYPE_AUDIO_OPUS + /**+ * How many times the decoder may be rebuilt before audio is given up on.+ *+ * Three, because the failure this exists for was a single one: a codec found already released+ * 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 + /**+ * `csd-1` and `csd-2`: the codec delay and the seek pre-roll, in little-endian nanoseconds.+ *+ * Both zero, for the same reason the identification header's pre-skip is: the phone's encoder+ * applies neither, and there is no container here whose timestamps would need adjusting for+ * 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 ->+ ((value ushr (8 * i)) and 0xff).toByte()+ }+ /** * The 19-byte `OpusHead`, per RFC 7845 §5.1. *
cast/tv/src/main/java/com/vayunmathur/cast/tv/platform/MirrorActivity.kt
diff --git a/cast/tv/src/main/java/com/vayunmathur/cast/tv/platform/MirrorActivity.kt b/cast/tv/src/main/java/com/vayunmathur/cast/tv/platform/MirrorActivity.ktindex fd70be6fab..91dfd183ed 100644--- a/cast/tv/src/main/java/com/vayunmathur/cast/tv/platform/MirrorActivity.kt+++ b/cast/tv/src/main/java/com/vayunmathur/cast/tv/platform/MirrorActivity.kt@@ -34,6 +34,19 @@ class MirrorActivity : ComponentActivity(), SurfaceHolder.Callback { private lateinit var container: FrameLayout private lateinit var surfaceView: SurfaceView + /**+ * The frame size the sender said it would send, remembered so the letterbox can be recomputed.+ *+ * **Load-bearing.** The letterbox used to be computed only when `ReceiverController.state` emitted,+ * which is once per session - and on this box that emission lands a few milliseconds after+ * [preferLargestDisplayMode] has asked for 3840x2160 and before the switch has taken effect, so the+ * panel still measures 1920x1080. The state never changes again, so a stale layout was final: a+ * 1920x1080 surface left sitting on a 4K panel. Holding the frame size here is what lets the panel+ * tell us when it has finished resizing.+ */+ private var frameWidth = 0+ private var frameHeight = 0+ override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) surfaceView = SurfaceView(this).apply { holder.addCallback(this@MirrorActivity) }@@ -50,6 +63,13 @@ class MirrorActivity : ComponentActivity(), SurfaceHolder.Callback { } setContentView(container) preferLargestDisplayMode()+ // The display-mode change above is asynchronous, and so is the first layout pass. Either one+ // finishing is a reason to redo the letterbox against the size the panel actually ended up.+ container.addOnLayoutChangeListener { _, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom ->+ if (right - left != oldRight - oldLeft || bottom - top != oldBottom - oldTop) {+ fitToFrame(frameWidth, frameHeight)+ }+ } // A TV has no navigation bar to keep clear of, but some launchers still overlay a status bar, // and a mirrored screen should be the whole panel. WindowCompat.getInsetsController(window, container).apply {@@ -85,6 +105,8 @@ class MirrorActivity : ComponentActivity(), SurfaceHolder.Callback { // A rotation or a resolution change hands back a *different* surface, so the controller has // to be told again - re-attaching the same one is harmless. ReceiverController.attachSurface(holder.surface)+ // And the panel may be a different size than when the letterbox was last worked out.+ fitToFrame(frameWidth, frameHeight) } override fun surfaceDestroyed(holder: SurfaceHolder) {@@ -109,13 +131,15 @@ class MirrorActivity : ComponentActivity(), SurfaceHolder.Callback { * Whether that last step actually happens is the composer's decision, not ours, so it has to be * checked on the device with `dumpsys SurfaceFlinger` rather than assumed. */- private fun fitToFrame(frameWidth: Int, frameHeight: Int) {- if (frameWidth <= 0 || frameHeight <= 0) return+ private fun fitToFrame(sourceWidth: Int, sourceHeight: Int) {+ if (sourceWidth <= 0 || sourceHeight <= 0) return+ frameWidth = sourceWidth+ frameHeight = sourceHeight container.post { val availableWidth = container.width val availableHeight = container.height if (availableWidth == 0 || availableHeight == 0) return@post- val frameAspect = frameWidth.toDouble() / frameHeight+ val frameAspect = sourceWidth.toDouble() / sourceHeight val screenAspect = availableWidth.toDouble() / availableHeight val (width, height) = if (frameAspect > screenAspect) { // Wider than the panel: full width, bars top and bottom.@@ -123,13 +147,21 @@ class MirrorActivity : ComponentActivity(), SurfaceHolder.Callback { } else { (availableHeight * frameAspect).toInt() to availableHeight }- surfaceView.holder.setFixedSize(frameWidth, frameHeight)+ // Nothing to do, and worth checking: this is now called from a layout listener, so+ // re-applying the same layoutParams would schedule another layout pass and loop.+ val current = surfaceView.layoutParams+ if (current != null && current.width == width && current.height == height) return@post+ surfaceView.holder.setFixedSize(sourceWidth, sourceHeight) surfaceView.layoutParams = FrameLayout.LayoutParams( width, height, android.view.Gravity.CENTER, )- Log.i(TAG, "buffer ${frameWidth}x$frameHeight shown as ${width}x$height")+ Log.i(+ TAG,+ "buffer ${sourceWidth}x$sourceHeight shown as ${width}x$height " ++ "on a ${availableWidth}x$availableHeight panel",+ ) } }
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 65e4d444a0..49980111ac 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@@ -664,7 +664,7 @@ object ReceiverController { media.throughputSummary() + " playout=${playout.depth}f/rebased=${playout.rebases}" + " dropped=${active?.framesDropped ?: 0}" +- if (player?.failed == true) " audio=failed" else "",+ audioHealth(player), ) } }@@ -707,6 +707,20 @@ object ReceiverController { private fun rtpToMicros(rtpTimestamp: Long): Long = rtpTimestamp * 1_000_000L / StreamConstants.VIDEO_TIMEBASE + /**+ * What the audio path is doing, in the one line that reports everything else.+ *+ * A rebuild is worth saying out loud even though it recovered: silence that came back is a fault+ * that will happen again, and a session that reports nothing looks identical to one that never had+ * a problem.+ */+ private fun audioHealth(player: AudioPlayer?): String = when {+ player == null -> ""+ player.failed -> " audio=failed after ${player.restarts} rebuilds"+ player.restarts > 0 -> " audio=recovered/${player.restarts} rebuilds"+ else -> ""+ }+ /** Audio is timestamped in samples, so its divisor is the sample rate. */ private fun audioRtpToMicros(rtpTimestamp: Long): Long = rtpTimestamp * 1_000_000L / StreamConstants.AUDIO_TIMEBASE