use std::collections::BTreeSet;
use crate::codec::{
AudioCodec, ClientCapabilities, Container, Delivery, DisplayCapabilities, HdrFormat,
SubtitleFormat, VideoCodec, VideoLimits,
};
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Decoders {
pub video: BTreeSet<VideoCodec>,
pub hardware_video: Option<BTreeSet<VideoCodec>>,
pub audio: BTreeSet<AudioCodec>,
pub containers: BTreeSet<Container>,
pub subtitles: BTreeSet<SubtitleFormat>,
pub delivery: Vec<Delivery>,
pub max_audio_channels: Option<u32>,
pub video_codecs: Option<BTreeSet<String>>,
}
impl DisplayCapabilities {
pub fn for_hdr(show: bool, dolby_vision: bool) -> Self {
let mut hdr = BTreeSet::new();
if show {
hdr.extend([HdrFormat::Hdr10, HdrFormat::Hlg]);
if dolby_vision {
hdr.insert(HdrFormat::DolbyVision);
}
}
Self {
bit_depth: None,
hdr: Some(hdr),
video_codecs: None,
height: None,
frame_rate: None,
}
}
}
impl ClientCapabilities {
pub fn from_decoders(decoders: Decoders, display: &DisplayCapabilities) -> Self {
let Decoders {
video,
hardware_video,
mut audio,
containers,
subtitles,
delivery,
max_audio_channels,
video_codecs,
} = decoders;
audio.insert(AudioCodec::Pcm);
let hardware_video =
hardware_video.map(|hw| hw.intersection(&video).copied().collect::<BTreeSet<_>>());
let delivery = match delivery.is_empty() {
true => vec![Delivery::Range, Delivery::Progressive],
false => delivery,
};
Self {
video,
audio,
containers,
subtitles,
delivery,
max_video: Some(VideoLimits {
height: None,
frame_rate: None,
bit_depth: display.bit_depth,
hdr: display.hdr.clone(),
}),
max_audio_channels,
max_bitrate: None,
hardware_video,
video_codecs: display.video_codecs.clone().or(video_codecs),
}
}
pub fn probe(
probe_codec: &dyn CodecProbe,
display: &DisplayCapabilities,
player: Decoders,
) -> Self {
let hardware = player.hardware_video.clone().unwrap_or_default();
let mut video = player.video;
video.extend(hardware.iter().copied());
let video_codecs = candidate_codec_strings()
.into_iter()
.filter(|s| probe_codec.plays(s))
.collect();
let bit_depth = match hardware.contains(&VideoCodec::Hevc) {
true => 10,
false => 8,
};
let display = DisplayCapabilities {
bit_depth: display.bit_depth.or(Some(bit_depth)),
..display.clone()
};
Self::from_decoders(
Decoders {
video,
hardware_video: Some(hardware),
video_codecs: Some(video_codecs),
..player
},
&display,
)
}
}
pub trait CodecProbe {
fn plays(&self, codec: &str) -> bool;
}
impl<F: Fn(&str) -> bool> CodecProbe for F {
fn plays(&self, codec: &str) -> bool {
self(codec)
}
}
pub fn candidate_codec_strings() -> Vec<String> {
let mut out = Vec::new();
for level in ["29", "2A", "32", "33", "34"] {
out.push(format!("avc1.6400{level}"));
}
for tag in ["hvc1", "hev1"] {
for profile in ["1.6", "2.4"] {
for tier in ["L", "H"] {
for level in [123, 150, 153, 156, 180, 183, 186] {
out.push(format!("{tag}.{profile}.{tier}{level}.B0"));
}
}
}
}
for tag in ["dvh1", "dvhe"] {
for profile in ["05", "07", "08"] {
for level in 1..=13 {
out.push(format!("{tag}.{profile}.{level:02}"));
}
}
}
for tier in ["M", "H"] {
for level in ["08", "09", "12", "13", "14", "16", "17"] {
for depth in ["08", "10"] {
out.push(format!("av01.0.{level}{tier}.{depth}"));
}
}
}
out
}
impl VideoCodec {
pub fn for_mime(mime: &str) -> Option<Self> {
Some(match mime {
"video/avc" => Self::H264,
"video/hevc" => Self::Hevc,
"video/av01" => Self::Av1,
"video/x-vnd.on2.vp9" => Self::Vp9,
"video/x-vnd.on2.vp8" => Self::Vp8,
"video/mpeg2" => Self::Mpeg2,
"video/mp4v-es" => Self::Mpeg4,
"video/wvc1" => Self::Vc1,
_ => return None,
})
}
}
impl AudioCodec {
pub fn for_mime(mime: &str) -> Option<Self> {
Some(match mime {
"audio/mp4a-latm" => Self::Aac,
"audio/ac3" => Self::Ac3,
"audio/eac3" | "audio/eac3-joc" => Self::Eac3,
"audio/vnd.dts" | "audio/vnd.dts.hd" => Self::Dts,
"audio/true-hd" => Self::TrueHd,
"audio/flac" => Self::Flac,
"audio/alac" => Self::Alac,
"audio/opus" => Self::Opus,
"audio/vorbis" => Self::Vorbis,
"audio/mpeg" => Self::Mp3,
"audio/raw" => Self::Pcm,
_ => return None,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn android_s_decoder_types_name_their_codecs() {
assert_eq!(
(
VideoCodec::for_mime("video/hevc"),
VideoCodec::for_mime("video/dolby-vision"),
AudioCodec::for_mime("audio/eac3-joc"),
AudioCodec::for_mime("audio/vnd.dts.hd"),
AudioCodec::for_mime("audio/3gpp"),
),
(
Some(VideoCodec::Hevc),
None,
Some(AudioCodec::Eac3),
Some(AudioCodec::Dts),
None
)
);
}
#[test]
fn a_probe_leaves_the_screen_s_mode_to_each_plan() {
let display = DisplayCapabilities {
height: Some(1080),
frame_rate: Some(60),
..DisplayCapabilities::for_hdr(false, false)
};
let limits = ClientCapabilities::probe(&|_: &str| true, &display, apple_tv())
.max_video
.unwrap();
assert_eq!((limits.height, limits.frame_rate), (None, None));
}
#[test]
fn a_screen_switched_up_since_the_sign_in_plays_its_new_mode() {
let hello = ClientCapabilities::probe(
&|_: &str| true,
&DisplayCapabilities {
height: Some(1080),
..DisplayCapabilities::for_hdr(false, false)
},
apple_tv(),
);
let at_2160 = DisplayCapabilities {
height: Some(2160),
..DisplayCapabilities::for_hdr(false, false)
};
let limits = hello.with_display(Some(&at_2160)).max_video.unwrap();
assert_eq!(limits.height, Some(2160));
}
#[test]
fn a_display_without_a_mode_keeps_hello_s_height_and_frame_rate() {
let mut hello = ClientCapabilities::from_decoders(
apple_tv(),
&DisplayCapabilities::for_hdr(false, false),
);
hello.max_video.as_mut().unwrap().height = Some(2160);
let at_1080 = DisplayCapabilities {
height: Some(1080),
..DisplayCapabilities::for_hdr(true, false)
};
let heights = [&DisplayCapabilities::for_hdr(true, false), &at_1080]
.map(|display| hello.with_display(Some(display)).max_video.unwrap().height);
assert_eq!(heights, [Some(2160), Some(1080)]);
}
#[test]
fn a_display_s_mode_never_raises_hello_s_limits() {
let mut hello = ClientCapabilities::from_decoders(
apple_tv(),
&DisplayCapabilities::for_hdr(false, false),
);
let limits = hello.max_video.as_mut().unwrap();
limits.height = Some(1080);
limits.frame_rate = Some(30);
let at_2160_60 = DisplayCapabilities {
height: Some(2160),
frame_rate: Some(60),
..DisplayCapabilities::for_hdr(true, false)
};
let limits = hello.with_display(Some(&at_2160_60)).max_video.unwrap();
assert_eq!((limits.height, limits.frame_rate), (Some(1080), Some(30)));
}
#[test]
fn hdr_on_is_hdr10_and_hlg_and_dolby_vision_only_when_asked() {
assert_eq!(
DisplayCapabilities::for_hdr(false, true).hdr,
Some(BTreeSet::new())
);
assert_eq!(
DisplayCapabilities::for_hdr(true, false).hdr,
Some([HdrFormat::Hdr10, HdrFormat::Hlg].into())
);
assert_eq!(
DisplayCapabilities::for_hdr(true, true).hdr,
Some([HdrFormat::Hdr10, HdrFormat::Hlg, HdrFormat::DolbyVision].into())
);
}
#[test]
fn decoders_always_play_pcm_and_default_to_range_then_progressive() {
let caps = ClientCapabilities::from_decoders(
Decoders {
video: [VideoCodec::H264].into(),
hardware_video: Some([VideoCodec::H264, VideoCodec::Av1].into()),
audio: [AudioCodec::Aac].into(),
..Decoders::default()
},
&DisplayCapabilities::for_hdr(false, false),
);
assert!(caps.audio.contains(&AudioCodec::Pcm));
assert_eq!(caps.deliveries(), [Delivery::Range, Delivery::Progressive]);
assert_eq!(
caps.hardware_video,
Some([VideoCodec::H264].into()),
"only what it plays"
);
let limits = caps.max_video.unwrap();
assert_eq!(
(limits.height, limits.bit_depth, limits.hdr),
(None, None, Some(BTreeSet::new()))
);
assert_eq!(caps.max_bitrate, None);
}
fn hardware(player: Decoders, codecs: &[VideoCodec]) -> Decoders {
Decoders {
hardware_video: Some(codecs.iter().copied().collect()),
..player
}
}
fn apple_tv() -> Decoders {
Decoders {
video: [VideoCodec::H264, VideoCodec::Hevc].into(),
audio: [AudioCodec::Aac, AudioCodec::Eac3].into(),
containers: [Container::Mp4].into(),
subtitles: [SubtitleFormat::WebVtt].into(),
delivery: vec![Delivery::Hls, Delivery::Range],
max_audio_channels: Some(8),
..Decoders::default()
}
}
#[test]
fn a_probe_asks_the_player_for_each_codec_string() {
let caps = ClientCapabilities::probe(
&|codec: &str| codec.starts_with("hvc1.2.4.L"),
&DisplayCapabilities::for_hdr(true, false),
hardware(
apple_tv(),
&[VideoCodec::H264, VideoCodec::Hevc, VideoCodec::Av1],
),
);
assert_eq!(
caps.video,
[VideoCodec::H264, VideoCodec::Hevc, VideoCodec::Av1].into(),
"AV1 is played where it decodes in hardware"
);
assert!(caps.decodes_in_hardware(VideoCodec::Av1));
assert_eq!(
caps.audio,
[AudioCodec::Aac, AudioCodec::Eac3, AudioCodec::Pcm].into()
);
assert_eq!(caps.deliveries(), [Delivery::Hls, Delivery::Range]);
let strings = caps.video_codecs.unwrap();
assert_eq!(strings.len(), 7);
assert!(strings.contains("hvc1.2.4.L153.B0"));
assert_eq!(caps.max_audio_channels, Some(8));
}
#[test]
fn a_probe_is_10_bit_only_where_hevc_decodes_in_hardware() {
let depth = |decoded: &[VideoCodec]| {
ClientCapabilities::probe(
&|_: &str| true,
&DisplayCapabilities::for_hdr(true, false),
hardware(apple_tv(), decoded),
)
.max_video
.unwrap()
.bit_depth
};
assert_eq!(depth(&[VideoCodec::H264, VideoCodec::Hevc]), Some(10));
assert_eq!(depth(&[VideoCodec::H264]), Some(8));
assert!(
!ClientCapabilities::probe(
&|_: &str| true,
&DisplayCapabilities::for_hdr(true, false),
hardware(apple_tv(), &[VideoCodec::H264]),
)
.decodes_in_hardware(VideoCodec::Hevc)
);
}
#[test]
fn the_candidates_are_the_ios_list() {
let all = candidate_codec_strings();
assert_eq!(all.len(), 5 + 56 + 78 + 28);
assert!(all.contains(&"dvh1.08.06".to_owned()));
assert!(all.contains(&"av01.0.12M.10".to_owned()));
}
}