Skip to main content

rtc_rtp/codec/smpte336m/
mod.rs

1//! SMPTE ST 336 (KLV) RTP payload format ([RFC 6597]).
2//!
3//! The format defines no header of its own: a KLV unit that fits the MTU is sent as-is,
4//! and a larger one is simply split into consecutive chunks. The marker bit (set by the
5//! payloader on the last chunk) is the only signal that a unit is complete.
6//!
7//! [RFC 6597]: https://datatracker.ietf.org/doc/html/rfc6597
8#[cfg(test)]
9mod smpte336m_test;
10
11use crate::packetizer::{Depacketizer, Payloader};
12use shared::error::Result;
13
14use bytes::Bytes;
15
16/// Smpte336mPayloader payloads SMPTE ST 336 (KLV) metadata units
17#[derive(Default, Debug, Copy, Clone)]
18pub struct Smpte336mPayloader;
19
20impl Payloader for Smpte336mPayloader {
21    fn payload(&mut self, mtu: usize, payload: &Bytes) -> Result<Vec<Bytes>> {
22        if payload.is_empty() || mtu == 0 {
23            return Ok(vec![]);
24        }
25
26        let mut remaining = payload.len();
27        let mut index = 0;
28        let mut payloads = Vec::with_capacity(remaining.div_ceil(mtu));
29        while remaining > 0 {
30            let chunk_size = std::cmp::min(mtu, remaining);
31            payloads.push(payload.slice(index..index + chunk_size));
32            remaining -= chunk_size;
33            index += chunk_size;
34        }
35
36        Ok(payloads)
37    }
38
39    fn clone_to(&self) -> Box<dyn Payloader> {
40        Box::new(*self)
41    }
42}
43
44/// Smpte336mDepacketizer depacketizes SMPTE ST 336 (KLV) metadata units
45#[derive(Default, Debug, Copy, Clone)]
46pub struct Smpte336mDepacketizer;
47
48impl Depacketizer for Smpte336mDepacketizer {
49    fn depacketize(&mut self, packet: &Bytes) -> Result<Bytes> {
50        Ok(packet.clone())
51    }
52
53    fn is_partition_head(&self, _payload: &Bytes) -> bool {
54        true
55    }
56
57    fn is_partition_tail(&self, marker: bool, _payload: &Bytes) -> bool {
58        marker
59    }
60}