Skip to content

fix(audio): count the silent-bridge gate in encoder frames, not source packets (#474) - #475

Merged
superuser404notfound merged 1 commit into
mainfrom
fix/ae396-silent-feed-startup-false-positive
Sep 3, 2026
Merged

superuser404notfound merged 1 commit into
mainfrom
fix/ae396-silent-feed-startup-false-positive

Conversation

@superuser404notfound

Copy link
Copy Markdown
Owner

Fixes the start-up false positive reported by @cmcpherson274 in #462, filed as #474.

What was wrong

AudioBridge.noteSilentFeedIfNeeded (AE#396) fired at 64 source packets, but the quantity that bounds its encoder arm is one encoder frame: drainFIFOIntoEncoder(requireFull: true) cannot encode below frame_size, so no output is possible until that many samples have been enqueued.

The threshold's own comment named the two constants it was derived from, and the FLAC arm breaks both:

encoder frame source packet packets before the first output is possible
E-AC-3 arm 1536 512 (DTS) … 1536 3
FLAC arm 4608 40 (TrueHD access unit) 116

The enqueued=2560 in the reporter's line is the proof: short of 4608, so the drain had correctly declined to encode and nothing could have been emitted. The FLAC mode has been in the same file since ec4723dc (2026-05-22) and predates the gate (9870892c, 2026-08-18).

It is not confined to the opt-in .lossless mode: .surroundCompat encodes any source of two channels or fewer to FLAC, so a stereo TrueHD file reproduces it on the default setting.

The fix

Each arm is counted in the unit that bounds it:

  • Encoder arm: samplesEnqueued >= frame_size * 4. Four frames because the first is when output becomes possible and the second covers an encoder that holds it; four is still inside the first half second of source audio on every arm (128 ms on E-AC-3, 384 ms on FLAC at 48 kHz), which is sooner in wall clock than the packet threshold it replaces.
  • Decoder arm: packets, because where the FIFO never moved there is no sample count to bound anything with. Counted from the last accepted sample rather than from the start, so a decoder that answers for a while and then stops is the same shape as one that never answered, which the old gate could not express at all.

silenceIsStructural(stats:encoderFrameSize:) is pure and static so the boundary is pinned against the frame sizes the bridge really opens rather than whichever one a fixture happens to reach. The ERROR line now also names the encoder it is talking about (it said "AC-3/E-AC-3 sample entry" on a FLAC session) and the frame boundary it judged.

Measured

Fixture, reproducible from scratch:

ffmpeg -f lavfi -i testsrc=size=1280x720:rate=25:duration=90 -f lavfi -i sine=frequency=440:duration=90 \
  -map 0:v -map 1:a -c:v libx264 -preset ultrafast -g 50 -pix_fmt yuv420p \
  -c:a truehd -strict -2 -ac 2 -ar 48000 truehd2.mkv
aetherctl play --seconds 20 file://$PWD/truehd2.mkv

before

[AudioBridge] init: mode=surroundCompat encoder=flac srcCodec=86060 sampleRate=48000 sourceChannels=2 …
[AudioBridge] ERROR: AE#396 the bridge has produced no encoded audio at all (mode=surroundCompat):
              fed=64 decoded=64 enqueued=2560 emitted=0. …
[AetherEngine] AE#462: audio delivery = bridged
[NativeAVPlayerHost] #1 item.audioTrack codec='flac' enabled=true sr=48000 ch=2 (readyToPlay)
final t=20.20s state=playing cues=0

after: same session, same counters reached, no ERROR line, final t=20.30s state=playing.

The counters in the before arm are identical to the reporter's device log, on the default mode.

Tests

Four new tests in Issue396SilentAudioBridgeTests, all four failing against the old rule and passing against the new one, with the four pre-existing tests passing in both arms:

  • 64 raw-PCM packets of 40 samples each through the FLAC arm reproduce enqueued=2560 emitted=0 and must not report
  • the same feed continued past the frame produces audio and still reports nothing
  • 80 undecodable MP3 packets still report, from the packet arm
  • the gate itself, against 4608 and against 512 with the same counters, plus the trickle shape the old gate could not express

Full suite green on both runners: XCTest 606 executed / 0 failures, swift-testing 2600 tests in 355 suites.

Limit

The encoder arm is measured on media. The decoder arm is pinned through the real bridge in tests rather than in a session, because no corruption that could be built here (noise bsf at every byte, on MP3, AC-3 and TrueHD) makes libavcodec stop producing frames for a whole session; each of those sources decoded and played.

Blast radius

None beyond the line. Both sites that classify a silent bridge are gated on an actual failure (HLSVideoEngine+LiveReopen.swift:206 on a pump death with muxerFailed, :632 on the exhausted revive), so a healthy start-up never reached them, and $audioDelivery (#462) is derived independently and read bridged correctly throughout.

…e packets (#474)

The AE#396 detector fired during a healthy start-up on every TrueHD session
and announced a failure that was not happening:

  [AudioBridge] ERROR: AE#396 the bridge has produced no encoded audio at all
  (mode=surroundCompat): fed=64 decoded=64 enqueued=2560 emitted=0. ... this
  session will fail its first segment cut unless output starts.

The threshold was 64 SOURCE PACKETS, but the quantity that bounds the encoder
arm is one ENCODER FRAME: drainFIFOIntoEncoder(requireFull:) cannot encode
below frame_size, so nothing can be emitted before that many samples have been
enqueued. Its derivation named its two constants, an E-AC-3 frame of 1536 and a
DTS packet of 512, and the FLAC arm breaks both: a FLAC frame is 4608 samples
and a TrueHD access unit is 40, so the first output needs 116 packets. The
enqueued=2560 in the reporter's own line is the proof, short of 4608 and short
of any output being possible. Not confined to the opt-in .lossless mode either,
.surroundCompat encodes two channels or fewer to FLAC.

Each arm is now counted in the unit that bounds it: the encoder's own
frame_size where PCM reached the FIFO, packets only where the FIFO never moved,
and the latter from the LAST accepted sample rather than from the start, so a
decoder that answers for a while and then stops is the same shape as one that
never answered. Four frames rather than one, because the first is when output
becomes possible and the second covers an encoder that holds it. The line
arrives sooner than before on the arm it was written for (128 ms of source
audio on E-AC-3 against 64 packets) and now names the encoder and the frame
boundary it judged.

Measured on media, both arms of the same fixture family: a stereo TrueHD MKV on
the default mode printed the ERROR at fed=64 before, prints nothing after, and
plays 20 s clean in both. The decoder arm is pinned through the real bridge in
tests (80 undecodable MP3 packets still report), because no corruption that
could be built here makes libavcodec stop producing frames for a whole session.

Nothing else read the threshold: both sites that classify a silent bridge are
gated on an actual failure (a pump death with muxerFailed, an exhausted revive),
so a healthy start-up never reached them, and $audioDelivery is derived
independently and read `bridged` correctly throughout.

Reported by @cmcpherson274 in #462.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Claude-Session: https://claude.ai/code/session_01Q4QpZDsHPJfbz5gkxxPXF7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant