use std::collections::HashMap;
use std::time::Duration;
use std::time::Instant;
use bytes::Bytes;
use rtci::rtp::packetizer::Depacketizer;
use crate::encoded_frames::FrameAssembler;
use crate::error::Error;
use crate::peer::Core;
use crate::peer::JsEvent;
pub const SILENCE: Duration = Duration::from_millis(2000);
#[derive(Debug)]
pub struct SinkFrame {
pub data: Vec<u8>,
pub key_frame: bool,
pub mime_type: String,
pub rtp_timestamp: u32,
pub width_height: Option<(u32, u32)>,
}
#[derive(Debug, Default)]
pub struct SinkOutput {
pub resize: Option<(u32, u32)>,
pub frame: Option<SinkFrame>,
}
#[derive(Default)]
pub struct MediaSinks {
assemblers: HashMap<u32, Option<(u8, FrameAssembler)>>,
codecs: HashMap<u32, (u8, String)>,
last_packet: HashMap<u32, Instant>,
sizes: HashMap<u32, (u32, u32)>,
}
impl MediaSinks {
pub fn silence_deadline(&self) -> Option<Instant> {
self.last_packet.values().min().map(|t| *t + SILENCE)
}
}
impl Core {
pub(crate) fn receiver_read_frames(
&mut self,
key: u32,
on: bool,
) -> Result<(), Error> {
self.index_of(key)?;
if on {
self.sinks.assemblers.entry(key).or_insert(None);
} else {
self.sinks.assemblers.remove(&key);
}
Ok(())
}
pub(crate) fn sink_needs_key_frame(&self, key: u32) -> bool {
self
.sinks
.assemblers
.get(&key)
.and_then(Option::as_ref)
.is_some_and(|(_, a)| a.needs_key_frame())
}
pub(crate) fn receiver_muted(&mut self, key: u32, generation: u32) {
self.media.unmute_generation.insert(key, generation);
self.rearm_unmute(key);
}
fn rearm_unmute(&mut self, key: u32) {
self.media.flowing.remove(&key);
self.sinks.last_packet.remove(&key);
}
pub(crate) fn receives_now(&mut self, key: u32) -> bool {
let Ok(index) = self.index_of(key) else {
return false;
};
self
.pc
.rtp_transceiver(index)
.and_then(|t| t.current_direction())
.is_some_and(|d| d.has_recv())
}
pub(crate) fn check_silence(&mut self, now: Instant) {
let silent: Vec<u32> = self
.sinks
.last_packet
.iter()
.filter(|(_, t)| now.saturating_duration_since(**t) >= SILENCE)
.map(|(k, _)| *k)
.collect();
for key in silent {
self.rearm_unmute(key);
self
.events
.push_back(JsEvent::TrackMute { transceiver: key });
}
}
pub(crate) fn sink_packet(
&mut self,
key: u32,
packet: &rtci::rtp::Packet,
now: Instant,
) -> SinkOutput {
let mut out = SinkOutput::default();
self.sinks.last_packet.insert(key, now);
let pt = packet.header.payload_type;
let known = self.sinks.codecs.get(&key).is_some_and(|(p, _)| *p == pt);
if !known {
let Some(mime) = self.receiver_mime_type(key, pt) else {
return out;
};
self.sinks.codecs.insert(key, (pt, mime));
}
let Some(mime) = self.sinks.codecs.get(&key).map(|(_, m)| m.clone()) else {
return out;
};
if let Some(size) = packet_coded_size(&mime, &packet.payload)
&& self.sinks.sizes.get(&key) != Some(&size)
{
self.sinks.sizes.insert(key, size);
out.resize = Some(size);
}
let Some(slot) = self.sinks.assemblers.get_mut(&key) else {
return out;
};
if slot.as_ref().is_none_or(|(p, _)| *p != pt) {
*slot = Some((pt, FrameAssembler::new(pt, &mime)));
}
let Some((_, assembler)) = slot.as_mut() else {
return out;
};
let Some(frame) = assembler.push(&packet.header, &packet.payload) else {
return out;
};
if frame.key_frame
&& let Some(size) = frame_coded_size(&mime, &frame.data)
&& self.sinks.sizes.get(&key) != Some(&size)
{
self.sinks.sizes.insert(key, size);
out.resize = Some(size);
}
out.frame = Some(SinkFrame {
key_frame: frame.key_frame,
mime_type: mime,
rtp_timestamp: frame.info.rtp_timestamp,
width_height: self.sinks.sizes.get(&key).copied(),
data: frame.data,
});
out
}
fn receiver_mime_type(&mut self, key: u32, pt: u8) -> Option<String> {
let index = self.index_of(key).ok()?;
let mut receiver = self.pc.rtp_receiver(index.into())?;
receiver
.get_parameters()
.rtp_parameters
.codecs
.iter()
.find(|c| c.payload_type == pt)
.map(|c| c.rtp_codec.mime_type.clone())
}
}
pub fn packet_coded_size(
mime_type: &str,
payload: &Bytes,
) -> Option<(u32, u32)> {
use rtci::rtp::codec;
if payload.is_empty() {
return None;
}
match mime_type.to_ascii_lowercase().as_str() {
"video/vp8" => {
let mut p = codec::vp8::Vp8Packet::default();
let frame = p.depacketize(payload).ok()?;
if p.s == 0 || p.pid != 0 {
return None;
}
crate::media::frame_size("video/vp8", &frame)
.map(|(w, h)| (w as u32, h as u32))
}
"video/vp9" => {
let mut p = codec::vp9::Vp9Packet::default();
let frame = p.depacketize(payload).ok()?;
if p.b
&& let Some(size) = vp9_frame_size(&frame)
{
return Some(size);
}
if p.v && p.y {
let w = *p.width.last()?;
let h = *p.height.last()?;
return Some((w as u32, h as u32));
}
None
}
"video/h264" => {
let first = *payload.first()?;
match first & 0x1f {
7 => h264_sps_size(payload),
24 => aggregated_nal_units(&payload[1..])
.find(|n| n.first().is_some_and(|b| b & 0x1f == 7))
.and_then(h264_sps_size),
_ => None,
}
}
"video/h265" => {
let first = *payload.first()?;
match (first >> 1) & 0x3f {
33 => h265_sps_size(payload),
48 => aggregated_nal_units(payload.get(2..)?)
.find(|n| n.first().is_some_and(|b| (b >> 1) & 0x3f == 33))
.and_then(h265_sps_size),
_ => None,
}
}
_ => None,
}
}
pub fn frame_coded_size(mime_type: &str, data: &[u8]) -> Option<(u32, u32)> {
match mime_type.to_ascii_lowercase().as_str() {
"video/vp8" => crate::media::frame_size("video/vp8", data)
.map(|(w, h)| (w as u32, h as u32)),
"video/vp9" => vp9_frame_size(data),
"video/h264" => annex_b_nal_units(data)
.find(|n| n.first().is_some_and(|b| b & 0x1f == 7))
.and_then(h264_sps_size),
"video/h265" => annex_b_nal_units(data)
.find(|n| n.first().is_some_and(|b| (b >> 1) & 0x3f == 33))
.and_then(h265_sps_size),
_ => None,
}
}
fn aggregated_nal_units(mut rest: &[u8]) -> impl Iterator<Item = &[u8]> {
std::iter::from_fn(move || {
if rest.len() < 2 {
return None;
}
let size = u16::from_be_bytes([rest[0], rest[1]]) as usize;
if rest.len() < 2 + size {
return None;
}
let nal = &rest[2..2 + size];
rest = &rest[2 + size..];
Some(nal)
})
}
fn annex_b_nal_units(data: &[u8]) -> impl Iterator<Item = &[u8]> {
let mut starts = Vec::new();
let mut i = 0;
while i + 3 <= data.len() {
if data[i] == 0 && data[i + 1] == 0 && data[i + 2] == 1 {
starts.push(i + 3);
i += 3;
} else {
i += 1;
}
}
let mut ends: Vec<usize> = starts.iter().skip(1).map(|s| s - 3).collect();
ends.push(data.len());
starts.into_iter().zip(ends).map(move |(a, b)| {
let mut end = b.max(a);
while end > a && data[end - 1] == 0 {
end -= 1;
}
&data[a..end]
})
}
fn rbsp(nal: &[u8]) -> Vec<u8> {
let mut out = Vec::with_capacity(nal.len());
let mut zeros = 0;
for &b in nal {
if zeros >= 2 && b == 3 {
zeros = 0;
continue;
}
zeros = if b == 0 { zeros + 1 } else { 0 };
out.push(b);
}
out
}
struct Bits<'a> {
data: &'a [u8],
pos: usize,
}
impl<'a> Bits<'a> {
fn new(data: &'a [u8]) -> Self {
Self { data, pos: 0 }
}
fn bit(&mut self) -> Option<u32> {
let byte = *self.data.get(self.pos / 8)?;
let bit = (byte >> (7 - (self.pos % 8))) & 1;
self.pos += 1;
Some(bit as u32)
}
fn bits(&mut self, n: u32) -> Option<u32> {
let mut v = 0u32;
for _ in 0..n {
v = (v << 1) | self.bit()?;
}
Some(v)
}
fn skip(&mut self, n: usize) -> Option<()> {
if self.pos + n > self.data.len() * 8 {
return None;
}
self.pos += n;
Some(())
}
fn ue(&mut self) -> Option<u32> {
let mut zeros = 0u32;
while self.bit()? == 0 {
zeros += 1;
if zeros > 31 {
return None;
}
}
if zeros == 0 {
return Some(0);
}
let rest = self.bits(zeros)? as u64;
u32::try_from((1u64 << zeros) - 1 + rest).ok()
}
fn se(&mut self) -> Option<i64> {
let k = self.ue()? as i64;
Some(if k % 2 == 1 { (k + 1) / 2 } else { -(k / 2) })
}
}
pub fn h264_sps_size(nal: &[u8]) -> Option<(u32, u32)> {
if nal.first()? & 0x1f != 7 {
return None;
}
let data = rbsp(nal.get(1..)?);
let mut r = Bits::new(&data);
let profile_idc = r.bits(8)?;
r.skip(16)?; r.ue()?; let mut chroma_format_idc = 1;
let mut separate_colour_plane = false;
if matches!(
profile_idc,
100 | 110 | 122 | 244 | 44 | 83 | 86 | 118 | 128 | 138 | 139 | 134 | 135
) {
chroma_format_idc = r.ue()?;
if chroma_format_idc == 3 {
separate_colour_plane = r.bit()? == 1;
}
r.ue()?; r.ue()?; r.bit()?; if r.bit()? == 1 {
let lists = if chroma_format_idc == 3 { 12 } else { 8 };
for i in 0..lists {
if r.bit()? == 1 {
let size = if i < 6 { 16 } else { 64 };
let (mut last, mut next) = (8i64, 8i64);
for _ in 0..size {
if next != 0 {
let delta = r.se()?;
next = (last + delta + 256).rem_euclid(256);
}
if next != 0 {
last = next;
}
}
}
}
}
}
r.ue()?; match r.ue()? {
0 => {
r.ue()?; }
1 => {
r.bit()?; r.se()?; r.se()?; let cycle = r.ue()?;
if cycle > 255 {
return None;
}
for _ in 0..cycle {
r.se()?; }
}
_ => {}
}
r.ue()?; r.bit()?; let width_mbs = r.ue()? as u64 + 1;
let height_map_units = r.ue()? as u64 + 1;
let frame_mbs_only = r.bit()? as u64;
if frame_mbs_only == 0 {
r.bit()?; }
r.bit()?; let (mut left, mut right, mut top, mut bottom) = (0u64, 0u64, 0u64, 0u64);
if r.bit()? == 1 {
left = r.ue()? as u64;
right = r.ue()? as u64;
top = r.ue()? as u64;
bottom = r.ue()? as u64;
}
let chroma_array_type = if separate_colour_plane {
0
} else {
chroma_format_idc
};
let (crop_x, crop_y) = if chroma_array_type == 0 {
(1, 2 - frame_mbs_only)
} else {
let sub_width = if chroma_format_idc == 3 { 1 } else { 2 };
let sub_height = if chroma_format_idc == 1 { 2 } else { 1 };
(sub_width, sub_height * (2 - frame_mbs_only))
};
let width = (width_mbs * 16).checked_sub(crop_x * (left + right))?;
let height = ((2 - frame_mbs_only) * height_map_units * 16)
.checked_sub(crop_y * (top + bottom))?;
Some((u32::try_from(width).ok()?, u32::try_from(height).ok()?))
}
pub fn h265_sps_size(nal: &[u8]) -> Option<(u32, u32)> {
if (nal.first()? >> 1) & 0x3f != 33 {
return None;
}
let data = rbsp(nal.get(2..)?);
let mut r = Bits::new(&data);
r.bits(4)?; let max_sub_layers_minus1 = r.bits(3)? as usize;
r.bit()?; r.skip(96)?;
let mut present = Vec::with_capacity(max_sub_layers_minus1);
for _ in 0..max_sub_layers_minus1 {
let profile = r.bit()? == 1;
let level = r.bit()? == 1;
present.push((profile, level));
}
if max_sub_layers_minus1 > 0 {
r.skip(2 * (8 - max_sub_layers_minus1))?; }
for (profile, level) in present {
if profile {
r.skip(88)?;
}
if level {
r.skip(8)?;
}
}
r.ue()?; let chroma_format_idc = r.ue()?;
let mut separate_colour_plane = false;
if chroma_format_idc == 3 {
separate_colour_plane = r.bit()? == 1;
}
let width = r.ue()? as u64;
let height = r.ue()? as u64;
let (mut left, mut right, mut top, mut bottom) = (0u64, 0u64, 0u64, 0u64);
if r.bit()? == 1 {
left = r.ue()? as u64;
right = r.ue()? as u64;
top = r.ue()? as u64;
bottom = r.ue()? as u64;
}
let chroma_array_type = if separate_colour_plane {
0
} else {
chroma_format_idc
};
let sub_width = if chroma_array_type == 1 || chroma_array_type == 2 {
2
} else {
1
};
let sub_height = if chroma_array_type == 1 { 2 } else { 1 };
let width = width.checked_sub(sub_width * (left + right))?;
let height = height.checked_sub(sub_height * (top + bottom))?;
if width == 0 || height == 0 {
return None;
}
Some((u32::try_from(width).ok()?, u32::try_from(height).ok()?))
}
pub fn vp9_frame_size(frame: &[u8]) -> Option<(u32, u32)> {
let mut r = Bits::new(frame);
if r.bits(2)? != 2 {
return None; }
let low = r.bit()?;
let high = r.bit()?;
let profile = (high << 1) | low;
if profile == 3 {
r.bit()?; }
if r.bit()? == 1 {
return None; }
if r.bit()? != 0 {
return None; }
r.bit()?; r.bit()?; if r.bits(24)? != 0x49_83_42 {
return None; }
if profile >= 2 {
r.bit()?; }
let color_space = r.bits(3)?;
if color_space != 7 {
r.bit()?; if profile == 1 || profile == 3 {
r.skip(3)?; }
} else if profile == 1 || profile == 3 {
r.bit()?; }
let width = r.bits(16)? + 1;
let height = r.bits(16)? + 1;
Some((width, height))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn exp_golomb() {
let data = [0b1010_0110, 0b0100_0000];
let mut r = Bits::new(&data);
assert_eq!(r.ue(), Some(0));
assert_eq!(r.ue(), Some(1));
assert_eq!(r.ue(), Some(2));
assert_eq!(r.ue(), Some(3));
let data = [0b0100_1100];
let mut r = Bits::new(&data);
assert_eq!(r.se(), Some(1));
assert_eq!(r.se(), Some(-1));
}
#[test]
fn emulation_prevention_is_removed() {
assert_eq!(rbsp(&[0, 0, 3, 1, 0, 0, 3]), vec![0, 0, 1, 0, 0]);
}
#[test]
fn the_fake_clips_are_sized() {
for mime in ["video/vp8", "video/vp9", "video/h264", "video/h265"] {
let (frames, _) = crate::fake_media::clip(mime, false).unwrap();
assert_eq!(
frame_coded_size(mime, frames[0]),
Some((640, 480)),
"{mime}"
);
}
for mime in ["video/vp8", "video/vp9"] {
let (frames, _) = crate::fake_media::clip(mime, false).unwrap();
assert_eq!(frame_coded_size(mime, frames[1]), None, "{mime}");
}
}
#[test]
fn sizes_from_packets() {
let vp8 = Bytes::from_static(&[
0x10, 0x50, 0x01, 0x00, 0x9d, 0x01, 0x2a, 0x80, 0x02, 0xe0, 0x01,
]);
assert_eq!(packet_coded_size("video/VP8", &vp8), Some((640, 480)));
let delta = Bytes::from_static(&[0x10, 0x51, 0x01, 0x00, 0x00, 0x00]);
assert_eq!(packet_coded_size("video/vp8", &delta), None);
let (frames, _) = crate::fake_media::clip("video/h264", false).unwrap();
let sps = annex_b_nal_units(frames[0])
.find(|n| n[0] & 0x1f == 7)
.unwrap()
.to_vec();
assert_eq!(
packet_coded_size("video/h264", &Bytes::from(sps.clone())),
Some((640, 480))
);
let mut stap = vec![24u8];
stap.extend_from_slice(&(sps.len() as u16).to_be_bytes());
stap.extend_from_slice(&sps);
assert_eq!(
packet_coded_size("video/h264", &Bytes::from(stap)),
Some((640, 480))
);
}
}