#[cfg(test)]
mod h264_test;
use super::*;
pub(crate) const DEFAULT_PROFILE_LEVEL_ID: &str = "42000a";
const DEFAULT_PACKETIZATION_MODE: &str = "0";
pub(crate) const PARAMETERS: &[ParameterUse] = &[
row("profile-level-id", ParameterRole::Configuration, true, true),
row(
"max-recv-level",
ParameterRole::ReceiverCapability,
true,
false,
),
row(
"packetization-mode",
ParameterRole::Configuration,
true,
true,
),
row(
"sprop-deint-buf-req",
ParameterRole::StreamProperty,
false,
true,
),
row(
"sprop-interleaving-depth",
ParameterRole::StreamProperty,
false,
true,
),
row(
"sprop-max-don-diff",
ParameterRole::StreamProperty,
false,
true,
),
row(
"sprop-init-buf-time",
ParameterRole::StreamProperty,
false,
true,
),
row("max-mbps", ParameterRole::ReceiverCapability, true, false),
row("max-smbps", ParameterRole::ReceiverCapability, true, false),
row("max-fs", ParameterRole::ReceiverCapability, true, false),
row("max-cpb", ParameterRole::ReceiverCapability, true, false),
row("max-dpb", ParameterRole::ReceiverCapability, true, false),
row("max-br", ParameterRole::ReceiverCapability, true, false),
row(
"redundant-pic-cap",
ParameterRole::ReceiverCapability,
true,
false,
),
row(
"deint-buf-cap",
ParameterRole::ReceiverCapability,
true,
false,
),
row(
"max-rcmd-nalu-size",
ParameterRole::ReceiverCapability,
true,
false,
),
row(
"sar-understood",
ParameterRole::ReceiverCapability,
true,
false,
),
row(
"sar-supported",
ParameterRole::ReceiverCapability,
true,
false,
),
row(
"in-band-parameter-sets",
ParameterRole::ReceiverCapability,
true,
false,
),
row(
"use-level-src-parameter-sets",
ParameterRole::ReceiverCapability,
true,
false,
),
row(
"level-asymmetry-allowed",
ParameterRole::OfferAnswer,
false,
false,
),
row(
"sprop-parameter-sets",
ParameterRole::ParameterSets,
false,
true,
),
row(
"sprop-level-parameter-sets",
ParameterRole::ParameterSets,
false,
true,
),
];
const fn row(
name: &'static str,
role: ParameterRole,
recvonly: bool,
sendonly: bool,
) -> ParameterUse {
ParameterUse {
name,
role,
recvonly,
sendonly,
}
}
const PROFILES: &[(&str, u8, u8, u8)] = &[
("CB", 0x42, 0b0100_1111, 0b0100_0000),
("CB", 0x4D, 0b1000_1111, 0b1000_0000),
("CB", 0x58, 0b1100_1111, 0b1100_0000),
("B", 0x42, 0b0100_1111, 0b0000_0000),
("B", 0x58, 0b1100_1111, 0b1000_0000),
("M", 0x4D, 0b1010_1111, 0b0000_0000),
("E", 0x58, 0b1100_1111, 0b0000_0000),
("H", 0x64, 0xFF, 0b0000_0000),
("H10", 0x6E, 0xFF, 0b0000_0000),
("H42", 0x7A, 0xFF, 0b0000_0000),
("H44", 0xF4, 0xFF, 0b0000_0000),
("H10I", 0x6E, 0xFF, 0b0001_0000),
("H42I", 0x7A, 0xFF, 0b0001_0000),
("H44I", 0xF4, 0xFF, 0b0001_0000),
("C44I", 0x2C, 0xFF, 0b0001_0000),
];
fn table_5_profile(profile_idc: u8, profile_iop: u8) -> Option<&'static str> {
PROFILES
.iter()
.find(|(_, idc, mask, value)| *idc == profile_idc && profile_iop & mask == *value)
.map(|(profile, ..)| *profile)
}
fn profile_level_id_matches(a: &str, b: &str) -> bool {
let decode = |value: &str| -> Option<(u8, u8)> {
let bytes = hex::decode(value.trim()).ok()?;
(bytes.len() == 3).then(|| (bytes[0], bytes[1]))
};
let (Some((a_idc, a_iop)), Some((b_idc, b_iop))) = (decode(a), decode(b)) else {
return false;
};
match (table_5_profile(a_idc, a_iop), table_5_profile(b_idc, b_iop)) {
(Some(a), Some(b)) => a == b,
_ => a_idc == b_idc && a_iop == b_iop,
}
}
#[derive(Debug, PartialEq)]
pub(crate) struct H264Fmtp {
pub(crate) parameters: HashMap<String, String>,
}
impl Fmtp for H264Fmtp {
fn mime_type(&self) -> &str {
"video/h264"
}
fn match_fmtp(&self, f: &dyn Fmtp) -> bool {
let Some(c) = f.as_any().downcast_ref::<H264Fmtp>() else {
return false;
};
let get = |fmtp: &H264Fmtp, key: &str, default: &'static str| -> String {
fmtp.parameters
.get(key)
.map_or_else(|| default.to_owned(), |v| v.trim().to_owned())
};
let mode = get(self, "packetization-mode", DEFAULT_PACKETIZATION_MODE);
["0", "1", "2"].contains(&mode.as_str())
&& mode == get(c, "packetization-mode", DEFAULT_PACKETIZATION_MODE)
&& profile_level_id_matches(
&get(self, "profile-level-id", DEFAULT_PROFILE_LEVEL_ID),
&get(c, "profile-level-id", DEFAULT_PROFILE_LEVEL_ID),
)
}
fn parameter(&self, key: &str) -> Option<&String> {
self.parameters.get(key)
}
fn equal(&self, other: &dyn Fmtp) -> bool {
other.as_any().downcast_ref::<H264Fmtp>() == Some(self)
}
fn as_any(&self) -> &dyn Any {
self
}
}