rtci-interceptor 0.5.0

Sans-I/O RTP/RTCP interceptor pipeline for WebRTC: RTCP reports, NACK, TWCC, congestion control, pacing, FEC
Documentation
//! The packets a stream sent, as they left: what an FEC encoder protects.
//!
//! FEC protects the source packets as they are on the wire: the receiver XORs the packets that
//! arrived, so the sender has to XOR the same bytes (RFC 8627 section 4.1, RFC 5109 section 8).
//! An encoder is a generator and sits above the pacer, where a media packet is not final yet: the
//! TWCC sender below it writes the transport-wide sequence number into the packet, adding the
//! header extension when the packet has none. An encoder that reads the packet at its own slot
//! protects an image the receiver never sees.
//!
//! [`WireImageTap`] sits below everything that edits a departing packet
//! ([`Slot::FecWireImage`](crate::Slot::FecWireImage)) and copies the packets of the streams an
//! encoder asked for. The encoder, still at [`Slot::FecEncoder`](crate::Slot::FecEncoder), takes
//! them from its [`WireImages`] handle the next time the chain polls it, and its repair packets
//! travel the whole chain: paced, numbered and recorded like any other packet.

use crate::Interceptor;
use crate::stream_info::StreamInfo;
use crate::{Packet, TaggedPacket};
use bytes::Bytes;
use sansio::Protocol;
use shared::error::Error;
use shared::marshal::Marshal;
use std::collections::{HashMap, HashSet, VecDeque};
use std::sync::{Arc, Mutex, MutexGuard};
use std::time::Instant;

/// The most wire images held for an encoder that is not taking them.
///
/// An encoder takes them every time the chain is polled, so more than a few is already unusual;
/// the bound is there so a chain whose encoder was dropped does not grow without limit. The
/// oldest go first: FEC for packets that left long ago is worth least.
pub const MAX_HELD_WIRE_IMAGES: usize = 1024;

/// The most wire images of one watched stream kept for its FlexFEC retransmissions
/// ([`WireImages::retransmit_on`]): as many packets as the NACK responder's send buffer holds
/// by default, so an image is kept for every packet a NACK can still be answered for.
pub const MAX_RETRANSMISSION_IMAGES: usize = 1024;

#[derive(Default)]
struct Shared {
    watched: HashSet<u32>,
    images: VecDeque<TaggedPacket>,
    dropped: u64,
    /// Repair streams whose FlexFEC retransmissions (R=1) get the wire image of the packet
    /// they carry.
    retransmission_streams: HashSet<u32>,
    /// The latest wire images of each watched stream, by sequence number, for those
    /// retransmissions (kept only while a repair stream is registered).
    kept: HashMap<u32, (HashMap<u16, Bytes>, VecDeque<u16>)>,
}

impl Shared {
    fn keep(&mut self, packet: &rtp::Packet) {
        let Ok(image) = packet.marshal() else {
            return;
        };
        let (images, order) = self.kept.entry(packet.header.ssrc).or_default();
        let sequence_number = packet.header.sequence_number;
        if images.insert(sequence_number, image.freeze()).is_none() {
            order.push_back(sequence_number);
        }
        while order.len() > MAX_RETRANSMISSION_IMAGES {
            if let Some(oldest) = order.pop_front() {
                images.remove(&oldest);
            }
        }
    }

    /// The wire image a FlexFEC retransmission on a registered repair stream is to carry:
    /// that of the source packet its payload names (RFC 8627 section 4.2.2.3: the FEC header
    /// of a retransmission is the source packet's RTP header, R=1 F=0, followed by the rest of
    /// the packet).
    fn retransmitted_image(&self, packet: &rtp::Packet) -> Option<Bytes> {
        if !self.retransmission_streams.contains(&packet.header.ssrc) {
            return None;
        }
        let payload = &packet.payload;
        if payload.len() < 12 || payload[0] & 0xC0 != 0x80 {
            return None;
        }
        let ssrc = u32::from_be_bytes([payload[8], payload[9], payload[10], payload[11]]);
        let sequence_number = u16::from_be_bytes([payload[2], payload[3]]);
        self.kept.get(&ssrc)?.0.get(&sequence_number).cloned()
    }
}

fn lock(shared: &Mutex<Shared>) -> MutexGuard<'_, Shared> {
    // Nothing is left half-done under this lock, so a panic elsewhere does not spoil the state.
    shared
        .lock()
        .unwrap_or_else(|poisoned| poisoned.into_inner())
}

/// A tap and the handle that reads it.
///
/// Register the tap at [`Slot::FecWireImage`](crate::Slot::FecWireImage); give the handle to the
/// encoder. The FEC send interceptors build the pair themselves
/// ([`FlexFec03SendInterceptor::wire_tap`](crate::FlexFec03SendInterceptor::wire_tap)); this is
/// for an encoder of your own.
pub fn wire_image_tap() -> (WireImageTap, WireImages) {
    let shared = Arc::new(Mutex::new(Shared::default()));
    (
        WireImageTap {
            shared: shared.clone(),
            read_queue: VecDeque::new(),
            write_queue: VecDeque::new(),
        },
        WireImages { shared },
    )
}

/// The reading end of a [`WireImageTap`].
#[derive(Clone)]
pub struct WireImages {
    shared: Arc<Mutex<Shared>>,
}

impl WireImages {
    /// Start copying the packets `ssrc` sends.
    pub fn watch(&self, ssrc: u32) {
        lock(&self.shared).watched.insert(ssrc);
    }

    /// Stop copying the packets `ssrc` sends. Images already held stay until taken.
    pub fn unwatch(&self, ssrc: u32) {
        let mut shared = lock(&self.shared);
        shared.watched.remove(&ssrc);
        shared.kept.remove(&ssrc);
    }

    /// Give every FlexFEC retransmission (R=1, RFC 8627 section 4.2.2.3) that leaves on the
    /// repair stream `repair_ssrc` the wire image of the source packet it carries: the packet
    /// as it left, header extensions written below the generator included, which is what the
    /// repair packets protect and what the receiver XORs them with. A retransmission is made
    /// above the extension writers (the NACK responder buffers the packet before the TWCC
    /// sender numbers it); without this its image differs from the one that left, and a
    /// receiver that rebuilds a neighbour with it rebuilds garbage.
    pub fn retransmit_on(&self, repair_ssrc: u32) {
        lock(&self.shared)
            .retransmission_streams
            .insert(repair_ssrc);
    }

    /// Stop rewriting the retransmissions of `repair_ssrc`.
    pub fn stop_retransmit_on(&self, repair_ssrc: u32) {
        let mut shared = lock(&self.shared);
        shared.retransmission_streams.remove(&repair_ssrc);
        if shared.retransmission_streams.is_empty() {
            shared.kept.clear();
        }
    }

    /// The packets that left since the last call, oldest first, each with the instant it was
    /// released to the network.
    pub fn take(&self) -> Vec<TaggedPacket> {
        lock(&self.shared).images.drain(..).collect()
    }

    /// How many images were given up because nothing took them.
    pub fn dropped(&self) -> u64 {
        lock(&self.shared).dropped
    }
}

/// Copies departing RTP packets of the watched streams, as they leave. Changes nothing: every
/// packet passes through as it came.
pub struct WireImageTap {
    shared: Arc<Mutex<Shared>>,
    read_queue: VecDeque<TaggedPacket>,
    write_queue: VecDeque<TaggedPacket>,
}

impl Protocol<TaggedPacket, TaggedPacket, ()> for WireImageTap {
    type Rout = TaggedPacket;
    type Wout = TaggedPacket;
    type Eout = ();
    type Error = Error;
    type Time = Instant;

    fn handle_read(&mut self, msg: TaggedPacket) -> Result<(), Self::Error> {
        self.read_queue.push_back(msg);
        Ok(())
    }

    fn poll_read(&mut self) -> Option<Self::Rout> {
        self.read_queue.pop_front()
    }

    fn handle_write(&mut self, mut msg: TaggedPacket) -> Result<(), Self::Error> {
        let retransmission = msg.message.has(&crate::Attribute::Retransmission);
        if let Packet::Rtp(rtp_packet) = &mut msg.message.packet {
            let mut shared = lock(&self.shared);
            if let Some(image) = shared.retransmitted_image(rtp_packet) {
                rtp_packet.payload = image;
            }
            if shared.watched.contains(&rtp_packet.header.ssrc)
                && !shared.retransmission_streams.is_empty()
                && !retransmission
            {
                shared.keep(rtp_packet);
            }
        }
        if let Packet::Rtp(rtp_packet) = &msg.message.packet {
            let mut shared = lock(&self.shared);
            if shared.watched.contains(&rtp_packet.header.ssrc) {
                if shared.images.len() >= MAX_HELD_WIRE_IMAGES {
                    shared.images.pop_front();
                    shared.dropped += 1;
                }
                shared.images.push_back(TaggedPacket {
                    now: msg.now,
                    transport: msg.transport,
                    message: msg.message.clone(),
                });
            }
        }
        self.write_queue.push_back(msg);
        Ok(())
    }

    fn poll_write(&mut self) -> Option<Self::Wout> {
        self.write_queue.pop_front()
    }

    fn handle_timeout(&mut self, _now: Instant) -> Result<(), Self::Error> {
        Ok(())
    }

    fn poll_timeout(&mut self) -> Option<Self::Time> {
        None
    }
}

impl Interceptor for WireImageTap {
    fn bind_local_stream(&mut self, _info: &StreamInfo) {}
    fn unbind_local_stream(&mut self, _info: &StreamInfo) {}
    fn bind_remote_stream(&mut self, _info: &StreamInfo) {}
    fn unbind_remote_stream(&mut self, _info: &StreamInfo) {}
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::AttributedPacket;
    use shared::TransportContext;

    fn rtp(ssrc: u32, sequence_number: u16) -> TaggedPacket {
        TaggedPacket {
            now: Instant::now(),
            transport: TransportContext::default(),
            message: AttributedPacket::new(Packet::Rtp(rtp::Packet {
                header: rtp::header::Header {
                    version: 2,
                    ssrc,
                    sequence_number,
                    ..Default::default()
                },
                payload: vec![1, 2, 3].into(),
            })),
        }
    }

    fn sequence_numbers(images: &[TaggedPacket]) -> Vec<u16> {
        images
            .iter()
            .filter_map(|image| match &image.message.packet {
                Packet::Rtp(rtp) => Some(rtp.header.sequence_number),
                Packet::Rtcp(_) => None,
            })
            .collect()
    }

    #[test]
    fn only_watched_streams_are_copied_and_every_packet_passes_through() {
        let (mut tap, images) = wire_image_tap();
        images.watch(1);

        tap.handle_write(rtp(1, 10)).unwrap();
        tap.handle_write(rtp(2, 20)).unwrap();
        tap.handle_write(rtp(1, 11)).unwrap();

        let mut passed = Vec::new();
        while let Some(out) = tap.poll_write() {
            passed.push(out);
        }
        assert_eq!(vec![10, 20, 11], sequence_numbers(&passed));
        assert_eq!(vec![10, 11], sequence_numbers(&images.take()));
        assert!(images.take().is_empty(), "taken once");
    }

    #[test]
    fn an_unwatched_stream_is_no_longer_copied() {
        let (mut tap, images) = wire_image_tap();
        images.watch(1);
        images.unwatch(1);

        tap.handle_write(rtp(1, 10)).unwrap();
        assert!(images.take().is_empty());
    }

    /// RFC 8627 section 4.2.2.3: a FlexFEC retransmission carries the source packet, and the
    /// receiver XORs repair packets with it as with any source packet it holds. The NACK
    /// responder makes it from its buffer, above the TWCC sender that writes the
    /// transport-wide sequence number into the packet; the tap, below it, puts the packet as it
    /// left into the retransmission. (Without this, an SFrame call's packet rebuilt beside a
    /// retransmitted one came out corrupt: diagnostic run 46341015789.) Parity packets on the
    /// repair stream, and packets of a repair stream not registered, pass unchanged.
    #[test]
    fn a_flexfec_retransmission_leaves_with_the_wire_image_of_its_packet() {
        let (mut tap, images) = wire_image_tap();
        images.watch(1);
        images.retransmit_on(9);
        // The source packet as it left: the TWCC sender wrote its extension.
        let mut left = rtp(1, 10);
        if let Packet::Rtp(packet) = &mut left.message.packet {
            packet.header.extension = true;
            packet.header.extension_profile = 0xBEDE;
            packet
                .header
                .set_extension(3, bytes::Bytes::from_static(&[0, 42]))
                .unwrap();
        }
        let image = match &left.message.packet {
            Packet::Rtp(packet) => packet.marshal().unwrap().freeze(),
            Packet::Rtcp(_) => unreachable!(),
        };
        tap.handle_write(left).unwrap();
        // The retransmission as the responder made it: the buffered packet, no extension.
        let buffered = match &rtp(1, 10).message.packet {
            Packet::Rtp(packet) => packet.marshal().unwrap().freeze(),
            Packet::Rtcp(_) => unreachable!(),
        };
        assert_ne!(buffered, image);
        let repair = |payload: bytes::Bytes, ssrc: u32| {
            let mut packet = rtp(ssrc, 500);
            if let Packet::Rtp(rtp) = &mut packet.message.packet {
                rtp.payload = payload;
            }
            packet
        };
        tap.handle_write(repair(buffered.clone(), 9)).unwrap();
        // A parity packet (R=0) and an unregistered repair stream are left alone.
        let mut parity = buffered.to_vec();
        parity[0] &= 0x3F;
        tap.handle_write(repair(parity.clone().into(), 9)).unwrap();
        tap.handle_write(repair(buffered.clone(), 8)).unwrap();

        let payloads: Vec<bytes::Bytes> = std::iter::from_fn(|| tap.poll_write())
            .skip(1)
            .map(|out| match out.message.packet {
                Packet::Rtp(packet) => packet.payload,
                Packet::Rtcp(_) => unreachable!(),
            })
            .collect();
        assert_eq!(payloads[0], image, "the packet as it left");
        assert_eq!(payloads[1], bytes::Bytes::from(parity), "parity unchanged");
        assert_eq!(payloads[2], buffered, "not registered: unchanged");
    }

    #[test]
    fn images_nothing_takes_are_bounded() {
        let (mut tap, images) = wire_image_tap();
        images.watch(1);

        for sequence_number in 0..(MAX_HELD_WIRE_IMAGES as u16 + 5) {
            tap.handle_write(rtp(1, sequence_number)).unwrap();
            while tap.poll_write().is_some() {}
        }

        let held = images.take();
        assert_eq!(MAX_HELD_WIRE_IMAGES, held.len());
        assert_eq!(5, sequence_numbers(&held)[0], "the oldest were given up");
        assert_eq!(5, images.dropped());
    }
}