Skip to main content

rtci_rtp/extension/
mod.rs

1use std::borrow::Cow;
2use std::fmt;
3
4use shared::{
5    error::Result,
6    marshal::{Marshal, MarshalSize},
7};
8
9/// Absolute capture time: when a frame was captured, on the capturer's NTP clock.
10pub mod abs_capture_time_extension;
11/// Absolute send time, for one-way-delay based bandwidth estimation.
12pub mod abs_send_time_extension;
13/// Per-packet audio loudness and voice activity ([RFC 6464]).
14pub mod audio_level_extension;
15/// Corruption detection samples (a libwebrtc experiment, W3C webrtc-stats
16/// `corruptionMeasurements`).
17pub mod corruption_detection_extension;
18/// The audio level of each contributing source of a mixed packet ([RFC 6465]).
19///
20/// [RFC 6465]: https://datatracker.ietf.org/doc/html/rfc6465
21pub mod csrc_audio_level_extension;
22/// In-band NTP-format timestamps for rapid synchronisation ([RFC 6051] section 3.3).
23///
24/// [RFC 6051]: https://datatracker.ietf.org/doc/html/rfc6051
25pub mod ntp_timestamp_extension;
26/// A requested playout-delay range, for latency/smoothness trade-offs.
27pub mod playout_delay_extension;
28/// SDES items in header extensions: MID, RtpStreamId, RepairedRtpStreamId ([RFC 7941],
29/// [RFC 8852]).
30///
31/// [RFC 7941]: https://datatracker.ietf.org/doc/html/rfc7941
32/// [RFC 8852]: https://datatracker.ietf.org/doc/html/rfc8852
33pub mod sdes_extension;
34/// Transmission time offset ([RFC 5450]).
35///
36/// [RFC 5450]: https://datatracker.ietf.org/doc/html/rfc5450
37pub mod transmission_offset_extension;
38/// The transport-wide sequence number that TWCC feedback refers to.
39pub mod transport_cc_extension;
40/// Camera direction and rotation (CVO), so a receiver can display video upright.
41pub mod video_orientation_extension;
42
43/// The URI of the absolute-send-time extension.
44pub const ABS_SEND_TIME_URI: &str = "http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time";
45/// The URI of the absolute-capture-time extension.
46pub const ABS_CAPTURE_TIME_URI: &str =
47    "http://www.webrtc.org/experiments/rtp-hdrext/abs-capture-time";
48/// The URI of the corruption detection extension (W3C webrtc-stats 8.5).
49pub const CORRUPTION_DETECTION_URI: &str =
50    "http://www.webrtc.org/experiments/rtp-hdrext/corruption-detection";
51/// The URI of the client-to-mixer audio level extension (RFC 6464).
52pub const AUDIO_LEVEL_URI: &str = "urn:ietf:params:rtp-hdrext:ssrc-audio-level";
53/// The URI of the mixer-to-client audio level extension (RFC 6465).
54pub const CSRC_AUDIO_LEVEL_URI: &str = "urn:ietf:params:rtp-hdrext:csrc-audio-level";
55/// The URI of the 64-bit NTP timestamp extension (RFC 6051 section 3.3).
56pub const NTP_64_URI: &str = ntp_timestamp_extension::NTP_64_URI;
57/// The URI of the 56-bit NTP timestamp extension (RFC 6051 section 3.3).
58pub const NTP_56_URI: &str = ntp_timestamp_extension::NTP_56_URI;
59/// The URI of the playout-delay extension.
60pub const PLAYOUT_DELAY_URI: &str = "http://www.webrtc.org/experiments/rtp-hdrext/playout-delay";
61/// The URI of the transport-wide sequence number extension.
62pub const TRANSPORT_CC_URI: &str =
63    "http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01";
64/// The URI of the video orientation extension (3GPP TS 26.114).
65pub const VIDEO_ORIENTATION_URI: &str = "urn:3gpp:video-orientation";
66/// The URI of the MID extension (RFC 8843).
67pub const SDES_MID_URI: &str = "urn:ietf:params:rtp-hdrext:sdes:mid";
68/// The URI of the RtpStreamId extension (RFC 8852).
69pub const SDES_RTP_STREAM_ID_URI: &str = "urn:ietf:params:rtp-hdrext:sdes:rtp-stream-id";
70/// The URI of the RepairedRtpStreamId extension (RFC 8852).
71pub const SDES_REPAIRED_RTP_STREAM_ID_URI: &str =
72    "urn:ietf:params:rtp-hdrext:sdes:repaired-rtp-stream-id";
73/// The URI of the transmission time offset extension (RFC 5450).
74pub const TRANSMISSION_OFFSET_URI: &str = "urn:ietf:params:rtp-hdrext:toffset";
75
76/// A generic RTP header extension.
77#[non_exhaustive]
78pub enum HeaderExtension {
79    /// The absolute-send-time extension.
80    AbsSendTime(abs_send_time_extension::AbsSendTimeExtension),
81    /// The audio-level extension.
82    AudioLevel(audio_level_extension::AudioLevelExtension),
83    /// The playout-delay extension.
84    PlayoutDelay(playout_delay_extension::PlayoutDelayExtension),
85    /// The transport-wide CC extension.
86    TransportCc(transport_cc_extension::TransportCcExtension),
87    /// The video-orientation extension.
88    VideoOrientation(video_orientation_extension::VideoOrientationExtension),
89    /// The absolute-capture-time extension.
90    AbsCaptureTime(abs_capture_time_extension::AbsCaptureTimeExtension),
91    /// The mixer-to-client audio level extension.
92    CsrcAudioLevel(csrc_audio_level_extension::CsrcAudioLevelExtension),
93    /// The MID extension.
94    Mid(sdes_extension::Mid),
95    /// The RtpStreamId extension.
96    RtpStreamId(sdes_extension::RtpStreamId),
97    /// The RepairedRtpStreamId extension.
98    RepairedRtpStreamId(sdes_extension::RepairedRtpStreamId),
99    /// The transmission time offset extension.
100    TransmissionOffset(transmission_offset_extension::TransmissionOffsetExtension),
101
102    /// A custom extension
103    Custom {
104        /// The extension's canonical URI, which is what SDP negotiates ids against.
105        uri: Cow<'static, str>,
106        /// The extension value, erased so extensions of different types can be held together.
107        extension: Box<dyn Marshal + 'static>,
108    },
109}
110
111impl HeaderExtension {
112    /// The extension's URI.
113    pub fn uri(&self) -> Cow<'static, str> {
114        use HeaderExtension::*;
115
116        match self {
117            AbsSendTime(_) => ABS_SEND_TIME_URI.into(),
118            AudioLevel(_) => AUDIO_LEVEL_URI.into(),
119            PlayoutDelay(_) => PLAYOUT_DELAY_URI.into(),
120            TransportCc(_) => TRANSPORT_CC_URI.into(),
121            VideoOrientation(_) => VIDEO_ORIENTATION_URI.into(),
122            AbsCaptureTime(_) => ABS_CAPTURE_TIME_URI.into(),
123            CsrcAudioLevel(_) => CSRC_AUDIO_LEVEL_URI.into(),
124            Mid(_) => SDES_MID_URI.into(),
125            RtpStreamId(_) => SDES_RTP_STREAM_ID_URI.into(),
126            RepairedRtpStreamId(_) => SDES_REPAIRED_RTP_STREAM_ID_URI.into(),
127            TransmissionOffset(_) => TRANSMISSION_OFFSET_URI.into(),
128            Custom { uri, .. } => uri.clone(),
129        }
130    }
131
132    /// Whether both refer to the same extension, comparing URIs rather than values.
133    pub fn is_same(&self, other: &Self) -> bool {
134        // One URI is one extension, whatever its value.
135        self.uri() == other.uri()
136    }
137}
138
139impl MarshalSize for HeaderExtension {
140    fn marshal_size(&self) -> usize {
141        use HeaderExtension::*;
142        match self {
143            AbsSendTime(ext) => ext.marshal_size(),
144            AudioLevel(ext) => ext.marshal_size(),
145            PlayoutDelay(ext) => ext.marshal_size(),
146            TransportCc(ext) => ext.marshal_size(),
147            VideoOrientation(ext) => ext.marshal_size(),
148            AbsCaptureTime(ext) => ext.marshal_size(),
149            CsrcAudioLevel(ext) => ext.marshal_size(),
150            Mid(ext) => ext.marshal_size(),
151            RtpStreamId(ext) => ext.marshal_size(),
152            RepairedRtpStreamId(ext) => ext.marshal_size(),
153            TransmissionOffset(ext) => ext.marshal_size(),
154            Custom { extension: ext, .. } => ext.marshal_size(),
155        }
156    }
157}
158
159impl Marshal for HeaderExtension {
160    fn marshal_to(&self, buf: &mut [u8]) -> Result<usize> {
161        use HeaderExtension::*;
162        match self {
163            AbsSendTime(ext) => ext.marshal_to(buf),
164            AudioLevel(ext) => ext.marshal_to(buf),
165            PlayoutDelay(ext) => ext.marshal_to(buf),
166            TransportCc(ext) => ext.marshal_to(buf),
167            VideoOrientation(ext) => ext.marshal_to(buf),
168            AbsCaptureTime(ext) => ext.marshal_to(buf),
169            CsrcAudioLevel(ext) => ext.marshal_to(buf),
170            Mid(ext) => ext.marshal_to(buf),
171            RtpStreamId(ext) => ext.marshal_to(buf),
172            RepairedRtpStreamId(ext) => ext.marshal_to(buf),
173            TransmissionOffset(ext) => ext.marshal_to(buf),
174            Custom { extension: ext, .. } => ext.marshal_to(buf),
175        }
176    }
177}
178
179impl fmt::Debug for HeaderExtension {
180    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
181        use HeaderExtension::*;
182
183        match self {
184            AbsSendTime(ext) => f.debug_tuple("AbsSendTime").field(ext).finish(),
185            AudioLevel(ext) => f.debug_tuple("AudioLevel").field(ext).finish(),
186            PlayoutDelay(ext) => f.debug_tuple("PlayoutDelay").field(ext).finish(),
187            TransportCc(ext) => f.debug_tuple("TransportCc").field(ext).finish(),
188            VideoOrientation(ext) => f.debug_tuple("VideoOrientation").field(ext).finish(),
189            AbsCaptureTime(ext) => f.debug_tuple("AbsCaptureTime").field(ext).finish(),
190            CsrcAudioLevel(ext) => f.debug_tuple("CsrcAudioLevel").field(ext).finish(),
191            Mid(ext) => f.debug_tuple("Mid").field(ext).finish(),
192            RtpStreamId(ext) => f.debug_tuple("RtpStreamId").field(ext).finish(),
193            RepairedRtpStreamId(ext) => f.debug_tuple("RepairedRtpStreamId").field(ext).finish(),
194            TransmissionOffset(ext) => f.debug_tuple("TransmissionOffset").field(ext).finish(),
195            Custom { uri, extension: _ } => f.debug_struct("Custom").field("uri", uri).finish(),
196        }
197    }
198}
199
200#[cfg(test)]
201mod extension_test;