use std::sync::Mutex;
use serde::{Deserialize, Serialize};
use super::types::VideoCodec;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum VideoCodecPreference {
#[default]
Automatic,
PreferH264,
PreferVp8,
}
impl VideoCodecPreference {
pub fn as_str(self) -> &'static str {
match self {
VideoCodecPreference::Automatic => "automatic",
VideoCodecPreference::PreferH264 => "prefer_h264",
VideoCodecPreference::PreferVp8 => "prefer_vp8",
}
}
pub fn from_name(name: &str) -> Option<VideoCodecPreference> {
match name {
"automatic" => Some(VideoCodecPreference::Automatic),
"prefer_h264" => Some(VideoCodecPreference::PreferH264),
"prefer_vp8" => Some(VideoCodecPreference::PreferVp8),
_ => None,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum H264Detection {
Usable,
NotDetected,
Unconfirmed,
NoPlatformCheck,
}
impl H264Detection {
pub fn is_usable(self) -> bool {
self == H264Detection::Usable
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum CodecPreferenceReason {
NotDetected,
Platform,
Policy,
}
impl CodecPreferenceReason {
pub fn as_str(self) -> &'static str {
match self {
CodecPreferenceReason::NotDetected => "not_detected",
CodecPreferenceReason::Platform => "platform",
CodecPreferenceReason::Policy => "policy",
}
}
}
pub fn effective_publish_codec(
pref: VideoCodecPreference,
detected: H264Detection,
) -> (VideoCodec, Option<CodecPreferenceReason>) {
match (pref, detected) {
(VideoCodecPreference::PreferVp8, _) => (VideoCodec::Vp8, None),
(_, H264Detection::Usable) => (VideoCodec::H264, None),
(VideoCodecPreference::PreferH264, H264Detection::NotDetected) => {
(VideoCodec::Vp8, Some(CodecPreferenceReason::NotDetected))
}
(VideoCodecPreference::PreferH264, _) => {
(VideoCodec::Vp8, Some(CodecPreferenceReason::Platform))
}
_ => (VideoCodec::Vp8, None),
}
}
pub fn effective_publish_codec_under_policy(
pref: VideoCodecPreference,
detected: H264Detection,
h264_allowed: bool,
) -> (VideoCodec, Option<CodecPreferenceReason>) {
match effective_publish_codec(pref, detected) {
(VideoCodec::H264, _) if !h264_allowed => {
let reason =
(pref == VideoCodecPreference::PreferH264).then_some(CodecPreferenceReason::Policy);
(VideoCodec::Vp8, reason)
}
out => out,
}
}
#[cfg(feature = "video-core")]
const NO_H264_BACKENDS: [&str; 4] = ["linux", "android", "windows", "null"];
#[cfg(feature = "video-core")]
pub fn h264_encode_detection(caps: &kessel_video::Capabilities) -> H264Detection {
use kessel_video::{CodecDirection, Implementation};
if NO_H264_BACKENDS.contains(&caps.backend) {
return H264Detection::NoPlatformCheck;
}
let hardware = caps.codecs.iter().any(|c| {
c.codec == kessel_video::VideoCodec::H264
&& c.direction == CodecDirection::Encode
&& c.implementation == Implementation::Hardware
});
if hardware {
H264Detection::Usable
} else if ios_before_17_4(&caps.os) {
H264Detection::Unconfirmed
} else {
H264Detection::NotDetected
}
}
#[cfg(feature = "video-core")]
impl H264Detection {
pub fn from_session(implementation: kessel_video::Implementation) -> H264Detection {
match implementation {
kessel_video::Implementation::Hardware => H264Detection::Usable,
kessel_video::Implementation::Software => H264Detection::NotDetected,
_ => H264Detection::Unconfirmed,
}
}
}
#[cfg(feature = "video-core")]
fn ios_before_17_4(os: &str) -> bool {
let Some(rest) = os.strip_prefix("iOS") else {
return false;
};
let Some(version) = rest
.split(|c: char| !(c.is_ascii_digit() || c == '.'))
.find(|t| t.contains(|c: char| c.is_ascii_digit()))
else {
return false;
};
let mut parts = version.split('.').map(|p| p.parse::<u32>().ok());
match (parts.next().flatten(), parts.next().flatten().unwrap_or(0)) {
(Some(major), minor) => (major, minor) < (17, 4),
_ => false,
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct CodecSet {
h264: bool,
}
impl CodecSet {
#[cfg_attr(not(test), allow(dead_code))]
pub(crate) const VP8_ONLY: CodecSet = CodecSet { h264: false };
#[cfg_attr(not(test), allow(dead_code))]
pub(crate) const ALL: CodecSet = CodecSet { h264: true };
pub(crate) fn contains(self, codec: VideoCodec) -> bool {
match codec {
VideoCodec::Vp8 => true,
VideoCodec::H264 => self.h264,
}
}
pub(crate) fn codecs(self) -> Vec<VideoCodec> {
let mut v = vec![VideoCodec::Vp8];
if self.h264 {
v.push(VideoCodec::H264);
}
v
}
pub(crate) fn negotiated(
cfg_h264: bool,
declared: Option<&[VideoCodec]>,
pref: VideoCodecPreference,
) -> CodecSet {
let declared_ok = match declared {
None => true,
Some(d) => !cfg!(target_os = "linux") && d.contains(&VideoCodec::H264),
};
CodecSet {
h264: cfg_h264 && pref != VideoCodecPreference::PreferVp8 && declared_ok,
}
}
}
#[derive(Debug, Default)]
pub(crate) struct CodecSettings {
pref: Mutex<VideoCodecPreference>,
decode: Mutex<Option<Vec<VideoCodec>>>,
}
fn lock<T>(m: &Mutex<T>) -> std::sync::MutexGuard<'_, T> {
m.lock().unwrap_or_else(std::sync::PoisonError::into_inner)
}
impl CodecSettings {
pub(crate) fn preference(&self) -> VideoCodecPreference {
*lock(&self.pref)
}
pub(crate) fn set_preference(&self, pref: VideoCodecPreference) {
*lock(&self.pref) = pref;
}
pub(crate) fn decode(&self) -> Option<Vec<VideoCodec>> {
lock(&self.decode).clone()
}
pub(crate) fn set_decode(&self, codecs: &[VideoCodec]) {
let mut v = codecs.to_vec();
v.sort_by_key(|c| c.as_str());
v.dedup();
*lock(&self.decode) = Some(v);
}
pub(crate) fn negotiated(&self, cfg_h264: bool) -> CodecSet {
CodecSet::negotiated(cfg_h264, self.decode().as_deref(), self.preference())
}
}
#[cfg(test)]
mod tests {
use super::*;
const PREFS: [VideoCodecPreference; 3] = [
VideoCodecPreference::Automatic,
VideoCodecPreference::PreferH264,
VideoCodecPreference::PreferVp8,
];
const DETECTIONS: [H264Detection; 4] = [
H264Detection::Usable,
H264Detection::NotDetected,
H264Detection::Unconfirmed,
H264Detection::NoPlatformCheck,
];
#[test]
fn the_effective_codec_follows_the_table() {
use CodecPreferenceReason as R;
use H264Detection as D;
use VideoCodec::{H264, Vp8};
use VideoCodecPreference as P;
let want = [
(P::Automatic, D::Usable, (H264, None)),
(P::Automatic, D::NotDetected, (Vp8, None)),
(P::Automatic, D::Unconfirmed, (Vp8, None)),
(P::Automatic, D::NoPlatformCheck, (Vp8, None)),
(P::PreferH264, D::Usable, (H264, None)),
(P::PreferH264, D::NotDetected, (Vp8, Some(R::NotDetected))),
(P::PreferH264, D::Unconfirmed, (Vp8, Some(R::Platform))),
(P::PreferH264, D::NoPlatformCheck, (Vp8, Some(R::Platform))),
(P::PreferVp8, D::Usable, (Vp8, None)),
(P::PreferVp8, D::NotDetected, (Vp8, None)),
(P::PreferVp8, D::Unconfirmed, (Vp8, None)),
(P::PreferVp8, D::NoPlatformCheck, (Vp8, None)),
];
assert_eq!(want.len(), PREFS.len() * DETECTIONS.len());
for (p, d, out) in want {
assert_eq!(effective_publish_codec(p, d), out, "{p:?} {d:?}");
assert_eq!(effective_publish_codec_under_policy(p, d, true), out);
}
assert_eq!(
effective_publish_codec_under_policy(P::PreferH264, D::Usable, false),
(Vp8, Some(R::Policy))
);
assert_eq!(
effective_publish_codec_under_policy(P::Automatic, D::Usable, false),
(Vp8, None)
);
assert_eq!(
effective_publish_codec_under_policy(P::PreferH264, D::NoPlatformCheck, false),
(Vp8, Some(R::Platform))
);
}
#[test]
fn h264_is_published_only_when_detected() {
for p in PREFS {
for d in DETECTIONS {
let (codec, _) = effective_publish_codec(p, d);
if codec == VideoCodec::H264 {
assert!(d.is_usable(), "{p:?} {d:?}");
assert_ne!(p, VideoCodecPreference::PreferVp8);
}
}
}
}
#[test]
fn the_preference_names_round_trip() {
for p in PREFS {
assert_eq!(VideoCodecPreference::from_name(p.as_str()), Some(p));
let json = serde_json::to_string(&p).unwrap_or_default();
assert_eq!(json, format!("\"{}\"", p.as_str()));
}
assert_eq!(VideoCodecPreference::from_name("h264"), None);
assert_eq!(
VideoCodecPreference::default(),
VideoCodecPreference::Automatic
);
for r in [
CodecPreferenceReason::NotDetected,
CodecPreferenceReason::Platform,
CodecPreferenceReason::Policy,
] {
let json = serde_json::to_string(&r).unwrap_or_default();
assert_eq!(json, format!("\"{}\"", r.as_str()));
}
}
#[cfg(feature = "video-core")]
#[test]
fn ios_versions() {
assert!(ios_before_17_4("iOS Version 17.2 (Build 21C62)"));
assert!(ios_before_17_4("iOS Version 16.7.8 (Build 20H343)"));
assert!(ios_before_17_4("iOS 17"));
assert!(!ios_before_17_4("iOS Version 17.4 (Build 21E219)"));
assert!(!ios_before_17_4("iOS Version 18.1 (Build 22B83)"));
assert!(!ios_before_17_4("macOS Version 14.2 (Build 23C64)"));
assert!(!ios_before_17_4("iOS"));
assert!(!ios_before_17_4("fake"));
}
#[test]
fn prefer_vp8_and_a_declaration_without_h264_offer_vp8_only() {
use VideoCodec::{H264, Vp8};
for p in PREFS {
assert_eq!(
CodecSet::negotiated(false, None, p),
CodecSet::VP8_ONLY,
"{p:?}"
);
assert_eq!(
CodecSet::negotiated(true, Some(&[Vp8][..]), p),
CodecSet::VP8_ONLY
);
assert_eq!(
CodecSet::negotiated(true, Some(&[][..]), p),
CodecSet::VP8_ONLY
);
}
for declared in [None, Some(&[Vp8, H264][..])] {
assert_eq!(
CodecSet::negotiated(true, declared, VideoCodecPreference::PreferVp8),
CodecSet::VP8_ONLY
);
}
assert_eq!(
CodecSet::negotiated(true, None, VideoCodecPreference::Automatic),
CodecSet::ALL
);
assert!(CodecSet::VP8_ONLY.contains(Vp8));
assert!(!CodecSet::VP8_ONLY.contains(H264));
assert_eq!(CodecSet::ALL.codecs(), vec![Vp8, H264]);
}
#[cfg(target_os = "linux")]
#[test]
fn linux_never_offers_h264_once_declared() {
use VideoCodec::{H264, Vp8};
for p in PREFS {
for cfg_h264 in [false, true] {
for declared in [&[][..], &[Vp8][..], &[H264][..], &[Vp8, H264][..]] {
let set = CodecSet::negotiated(cfg_h264, Some(declared), p);
assert!(!set.contains(H264), "{p:?} {cfg_h264} {declared:?}");
assert!(set.contains(Vp8));
}
}
let cfg = crate::ClientConfig::new("t", std::env::temp_dir());
assert!(!CodecSet::negotiated(cfg.video_h264, None, p).contains(H264));
}
let s = CodecSettings::default();
s.set_decode(&[H264, Vp8, H264]);
assert_eq!(s.decode(), Some(vec![H264, Vp8]));
assert!(!s.negotiated(true).contains(H264));
}
#[cfg(not(target_os = "linux"))]
#[test]
fn a_declared_h264_decoder_is_offered() {
use VideoCodec::{H264, Vp8};
let s = CodecSettings::default();
assert_eq!(s.negotiated(true), CodecSet::ALL);
s.set_decode(&[Vp8]);
assert_eq!(s.negotiated(true), CodecSet::VP8_ONLY);
s.set_decode(&[Vp8, H264]);
assert_eq!(s.negotiated(true), CodecSet::ALL);
assert_eq!(s.negotiated(false), CodecSet::VP8_ONLY);
s.set_preference(VideoCodecPreference::PreferVp8);
assert_eq!(s.negotiated(true), CodecSet::VP8_ONLY);
}
#[cfg(feature = "video-core")]
#[test]
fn detection_from_capabilities() {
use kessel_video::{Capabilities, CodecCapability, CodecDirection, Implementation};
let entry = |dir, imp| {
CodecCapability::new(
kessel_video::VideoCodec::H264,
dir,
imp,
"t",
1920,
1080,
30,
)
};
let caps = |backend: &'static str, os: &str, entries: Vec<CodecCapability>| {
let mut c = Capabilities::new(backend, os);
c.codecs = entries;
c
};
let hw = || entry(CodecDirection::Encode, Implementation::Hardware);
let sw = || entry(CodecDirection::Encode, Implementation::Software);
let dec = || entry(CodecDirection::Decode, Implementation::Hardware);
let mac = "macOS Version 14.2 (Build 23C64)";
assert_eq!(
h264_encode_detection(&caps("apple", mac, vec![hw(), sw(), dec()])),
H264Detection::Usable
);
assert_eq!(
h264_encode_detection(&caps("apple", mac, vec![sw(), dec()])),
H264Detection::NotDetected
);
assert_eq!(
h264_encode_detection(&caps(
"apple",
"iOS Version 17.2 (Build 21C62)",
vec![dec()]
)),
H264Detection::Unconfirmed
);
assert_eq!(
h264_encode_detection(&caps(
"apple",
"iOS Version 17.5 (Build 21F79)",
vec![dec()]
)),
H264Detection::NotDetected
);
for b in NO_H264_BACKENDS {
assert_eq!(
h264_encode_detection(&caps(b, "x", vec![hw()])),
H264Detection::NoPlatformCheck,
"{b}"
);
}
assert_eq!(
h264_encode_detection(&caps("fixture", "fixture", vec![hw()])),
H264Detection::Usable
);
assert_eq!(
H264Detection::from_session(Implementation::Hardware),
H264Detection::Usable
);
assert_eq!(
H264Detection::from_session(Implementation::Software),
H264Detection::NotDetected
);
assert_eq!(
H264Detection::from_session(Implementation::Unknown),
H264Detection::Unconfirmed
);
}
}