use crate::peer_connection::configuration::media_engine::{MIME_TYPE_H264, MIME_TYPE_VP8};
use crate::rtp_transceiver::rtp_sender::RTCRtpRidRestrictions;
use sdp::description::media::MediaDescription;
const MACROBLOCK_PIXELS: u64 = 256;
const MACROBLOCK_SIDE: u64 = 16;
const H264_MAX_BR_FACTOR: u64 = 1200;
fn tighter(current: Option<u64>, other: Option<u64>) -> Option<u64> {
match (current, other) {
(Some(a), Some(b)) => Some(a.min(b)),
(a, b) => a.or(b),
}
}
fn fmtp_number(fmtp: &str, name: &str) -> Option<u64> {
fmtp.split(';').find_map(|parameter| {
let (key, value) = parameter.trim().split_once('=')?;
key.trim()
.eq_ignore_ascii_case(name)
.then(|| value.trim().parse().ok())
.flatten()
})
}
pub(crate) fn section_bandwidth(media: &MediaDescription) -> Option<u64> {
let of = |kind: &str| {
media
.bandwidth
.iter()
.find(|b| !b.experimental && b.bandwidth_type.eq_ignore_ascii_case(kind))
.map(|b| b.bandwidth)
};
of("TIAS").or_else(|| of("AS").map(|kbps| kbps.saturating_mul(1000)))
}
pub(crate) fn combined_send_restrictions(
restrictions: &RTCRtpRidRestrictions,
mime_type: &str,
fmtp: &str,
bandwidth: Option<u64>,
) -> RTCRtpRidRestrictions {
let mut combined = restrictions.clone();
if mime_type.eq_ignore_ascii_case(MIME_TYPE_VP8) {
combined.max_fps = tighter(combined.max_fps, fmtp_number(fmtp, "max-fr"));
if let Some(max_fs) = fmtp_number(fmtp, "max-fs") {
combined.max_fs = tighter(
combined.max_fs,
Some(max_fs.saturating_mul(MACROBLOCK_PIXELS)),
);
let side = ((max_fs.saturating_mul(8)) as f64).sqrt() as u64 * MACROBLOCK_SIDE;
combined.max_width = tighter(combined.max_width, Some(side));
combined.max_height = tighter(combined.max_height, Some(side));
}
} else if mime_type.eq_ignore_ascii_case(MIME_TYPE_H264) {
combined.max_br = tighter(
combined.max_br,
fmtp_number(fmtp, "max-br").map(|br| br.saturating_mul(H264_MAX_BR_FACTOR)),
);
combined.max_fs = tighter(
combined.max_fs,
fmtp_number(fmtp, "max-fs").map(|fs| fs.saturating_mul(MACROBLOCK_PIXELS)),
);
combined.max_pps = tighter(
combined.max_pps,
fmtp_number(fmtp, "max-mbps").map(|mbps| mbps.saturating_mul(MACROBLOCK_PIXELS)),
);
}
combined.max_br = tighter(combined.max_br, bandwidth);
combined
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_smaller_of_each_rid_and_format_limit_applies() {
let rid = RTCRtpRidRestrictions {
max_fps: Some(30),
max_width: Some(1280),
max_br: Some(2_000_000),
..Default::default()
};
let vp8 = combined_send_restrictions(&rid, MIME_TYPE_VP8, "max-fr=15;max-fs=3600", None);
assert_eq!(vp8.max_fps, Some(15));
assert_eq!(vp8.max_fs, Some(921_600));
assert_eq!(vp8.max_width, Some(1280));
assert_eq!(vp8.max_height, Some(2704));
assert_eq!(vp8.max_br, Some(2_000_000));
let h264 = combined_send_restrictions(
&rid,
MIME_TYPE_H264,
"profile-level-id=42e01f; max-br=1000; max-mbps=40500",
Some(500_000),
);
assert_eq!(h264.max_br, Some(500_000), "b=TIAS is the tighter");
assert_eq!(h264.max_pps, Some(40_500 * 256));
assert_eq!(h264.max_fps, Some(30), "H.264 states no frame rate");
}
}