fix(playback): force a transcode when the webview cannot decode the audio (DR-149, 0.4.8)

Advertising a webview-shaped profile (DR-148) was necessary but not
sufficient. Probing the server directly showed Jellyfin 10.11.5 enforces a
DirectPlayProfile's Container and VideoCodec — excluding either returns
SupportsDirectPlay:false with TranscodeReasons=ContainerNotSupported /
VideoCodecNotSupported — but ignores its AudioCodec entirely: an E-AC-3
track is still offered for direct play against a profile listing only
aac,flac,mp3,opus,vorbis. Neither a VideoAudio CodecProfile forbidding the
codec nor MaxAudioChannels:2 against a 6-channel track changes the answer,
so no profile the client can send fixes this and the picture plays silent.

The client therefore stops delegating a question it can answer itself. The
negotiated source's audio is checked against what the webview decodes, and
an undecodable track forces the existing h264/aac HLS transcode regardless
of the server calling direct play fine; direct_play and needs_transcoding
are corrected to match so the frontend and the reporting path agree with
the URL actually used. The track judged is the one that would be served —
the default, else the first — since a supported track further down is not
the one that plays. A source with no audio, or a codec the server did not
name, is left alone rather than transcoded on a guess.

Test-first: the new tests failed against the old behaviour before the
decision existed. Verified on a motorola edge 30 by the audio HAL, not by
ear — the same E-AC-3 episode logged isMusicActive=true once and 58
ACDB-LOADER lines under this build, against 0 and 0 on 0.4.6, where an AAC
file in the same session produced 16 and 116. No FATAL EXCEPTION, so R8 on
the signed release build is unaffected.

Also carries in-flight subtitle-track work authored in a parallel session
(subtitleTracks, VideoPlayer, player/media, bindings) at the user's
request, so the tag matches the APK verified on device.
This commit is contained in:
2026-08-11 20:07:11 +02:00
parent 6a712c46cb
commit acddcdd6fa
8 changed files with 136 additions and 11 deletions
@@ -100,10 +100,99 @@ pub fn video_audio_codecs(detected: &str) -> String {
}
}
/// Whether the webview `<video>` element can decode this audio codec.
///
/// TRACES: UR-004 | DR-149 | UT-143
pub fn webview_can_decode_audio(codec: &str) -> bool {
WEBVIEW_AUDIO_CODECS
.iter()
.any(|supported| supported.eq_ignore_ascii_case(codec.trim()))
}
/// Decide whether we must transcode *regardless of what the server negotiated*,
/// given the source's audio streams as `(codec, is_default)` in source order.
///
/// Advertising a narrow profile ([`video_audio_codecs`]) is necessary but not
/// sufficient: Jellyfin 10.11.5 enforces a `DirectPlayProfile`'s container and
/// video codec but **ignores its audio codec** — an E-AC-3 track is offered for
/// direct play even when the profile lists only AAC, and neither a `VideoAudio`
/// `CodecProfile` nor `MaxAudioChannels` changes that. So the client cannot
/// delegate this decision; it knows what its own renderer can decode and must
/// apply that itself.
///
/// The track that matters is the one the server will actually serve: the
/// default, or the first when none is marked. An unknown codec is left alone —
/// forcing a transcode on a guess would burn server CPU for files that play.
///
/// TRACES: UR-004 | DR-149 | UT-143
pub fn audio_forces_transcode(streams: &[(Option<&str>, bool)]) -> bool {
let served = streams
.iter()
.find(|(_, is_default)| *is_default)
.or_else(|| streams.first());
match served {
Some((Some(codec), _)) => !webview_can_decode_audio(codec),
// No audio at all, or a codec the server did not name: leave it alone.
Some((None, _)) | None => false,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn an_undecodable_default_track_forces_a_transcode() {
// The reported bug: one E-AC-3 track, which the webview cannot decode.
assert!(audio_forces_transcode(&[(Some("eac3"), false)]));
assert!(audio_forces_transcode(&[(Some("ac3"), true)]));
}
#[test]
fn a_decodable_track_is_left_to_direct_play() {
// Never spend server CPU on a file that already plays.
assert!(!audio_forces_transcode(&[(Some("aac"), true)]));
assert!(!audio_forces_transcode(&[(Some("mp3"), false)]));
}
#[test]
fn the_default_track_decides_not_the_first() {
// The webview plays the default track, so that is the one that has to be
// decodable — a supported track further down does not save us.
assert!(audio_forces_transcode(&[
(Some("aac"), false),
(Some("eac3"), true)
]));
assert!(!audio_forces_transcode(&[
(Some("eac3"), false),
(Some("aac"), true)
]));
}
#[test]
fn with_no_default_marked_the_first_track_decides() {
// Jellyfin leaves IsDefault false on every stream for some files; the
// server then serves the first, so judge that one.
assert!(audio_forces_transcode(&[
(Some("eac3"), false),
(Some("aac"), false)
]));
}
#[test]
fn a_source_with_no_audio_is_not_transcoded() {
// Nothing to rescue, and a transcode would not create audio.
assert!(!audio_forces_transcode(&[]));
}
#[test]
fn an_unknown_codec_is_not_second_guessed() {
// The server did not tell us the codec; assuming the worst would
// transcode files that play perfectly.
assert!(!audio_forces_transcode(&[(None, true)]));
}
#[test]
fn a_dolby_device_does_not_advertise_dolby_for_video() {
// The bug: a Motorola reporting c2.dolby.eac3.decoder direct-played