fix(playback): bound the device profile by the audio route's channels (DR-141)
MediaCodecList answers "can this device decode 5.1", which is not the question that decides whether the user hears anything: a phone decodes an AC-3 5.1 track happily and still has two channels to play it out of. The DeviceProfile carried no MaxAudioChannels, so Jellyfin was free to direct-play the multichannel track to a two-channel sink — silence or dialogue folded into surround channels that go nowhere, depending on the device. Report media3 AudioCapabilities.maxChannelCount for the current route over JNI alongside the codec lists, and bound the direct-play and transcoding profiles (and the HLS URL's TranscodingMaxAudioChannels, previously hardcoded to 2) by it. No codec is ever removed, so a device with genuine surround output keeps direct-playing it. A missing or zero reading means "route not yet established", not "no audio", and falls back to stereo.
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
//! Device-profile policy: turning what a device *reports* about its audio
|
||||
//! output into the constraints we send Jellyfin.
|
||||
//!
|
||||
//! The platform layer reports raw facts (what `MediaCodecList` enumerates, how
|
||||
//! many channels the current audio route accepts); deciding what those facts
|
||||
//! mean for a `DeviceProfile` is domain logic and lives here, on the Rust side
|
||||
//! of the boundary, where it is testable without a device.
|
||||
|
||||
/// Channel count assumed when the platform cannot tell us — every audio route
|
||||
/// can voice stereo, so it is the only safe floor.
|
||||
const FALLBACK_AUDIO_CHANNELS: u32 = 2;
|
||||
|
||||
/// Upper bound we are willing to claim. Jellyfin profiles top out at 7.1, and a
|
||||
/// nonsense reading from a driver should not become a nonsense profile.
|
||||
const MAX_SUPPORTED_AUDIO_CHANNELS: u32 = 8;
|
||||
|
||||
/// Decide the `MaxAudioChannels` to advertise, given what the current audio
|
||||
/// route reported.
|
||||
///
|
||||
/// Without this constraint Jellyfin is free to direct-play a 5.1 or 7.1 track to
|
||||
/// a sink that only has two channels. What the user hears then is device
|
||||
/// dependent and rarely correct — a failed `AudioSink` configuration (silence),
|
||||
/// or centre-channel dialogue folded away to near-inaudibility. Naming the real
|
||||
/// channel count makes the server downmix instead, which is always audible.
|
||||
///
|
||||
/// A missing or zero reading means "route not established yet", not "no audio":
|
||||
/// fall back to stereo rather than claiming a capability we have not seen.
|
||||
///
|
||||
/// TRACES: UR-004 | DR-141 | UT-131
|
||||
pub fn clamp_max_audio_channels(reported: Option<u32>) -> u32 {
|
||||
match reported {
|
||||
Some(channels) if channels >= 1 => channels.min(MAX_SUPPORTED_AUDIO_CHANNELS),
|
||||
_ => FALLBACK_AUDIO_CHANNELS,
|
||||
}
|
||||
}
|
||||
|
||||
/// The channel cap for this device, reading the platform's report where one
|
||||
/// exists.
|
||||
///
|
||||
/// TRACES: UR-004 | DR-141 | UT-131
|
||||
pub fn max_audio_channels() -> u32 {
|
||||
#[cfg(target_os = "android")]
|
||||
let reported = crate::player::get_detected_codecs().and_then(|(_, _, channels)| channels);
|
||||
|
||||
// Desktop plays video through the WebKitGTK HTML5 <video> element, which we
|
||||
// do not interrogate for a channel count; stereo is the safe assumption.
|
||||
#[cfg(not(target_os = "android"))]
|
||||
let reported: Option<u32> = None;
|
||||
|
||||
clamp_max_audio_channels(reported)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn an_unknown_route_falls_back_to_stereo() {
|
||||
// Codec detection has not run yet, or the platform has no answer. Never
|
||||
// claim surround we have not seen — every sink can do stereo.
|
||||
assert_eq!(clamp_max_audio_channels(None), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_zero_reading_is_not_a_capability() {
|
||||
// A route that has not been established reports 0; taking that literally
|
||||
// would advertise a device with no audio at all.
|
||||
assert_eq!(clamp_max_audio_channels(Some(0)), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_stereo_sink_is_reported_as_stereo() {
|
||||
// The phone speaker / Bluetooth headset case: the server must downmix
|
||||
// 5.1 rather than direct-play it.
|
||||
assert_eq!(clamp_max_audio_channels(Some(2)), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_surround_route_keeps_its_channels() {
|
||||
// HDMI to an AVR: 5.1 and 7.1 direct play stay available.
|
||||
assert_eq!(clamp_max_audio_channels(Some(6)), 6);
|
||||
assert_eq!(clamp_max_audio_channels(Some(8)), 8);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_absurd_reading_is_capped_rather_than_forwarded() {
|
||||
// Some drivers report the AudioTrack maximum rather than the route's.
|
||||
assert_eq!(clamp_max_audio_channels(Some(32)), 8);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_mono_route_is_taken_at_its_word() {
|
||||
assert_eq!(clamp_max_audio_channels(Some(1)), 1);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user