xref: /webrtc/rtp/src/extension/mod.rs (revision 203529d4)
1 use std::borrow::Cow;
2 use std::fmt;
3 
4 use util::{Marshal, MarshalSize};
5 
6 pub mod abs_send_time_extension;
7 pub mod audio_level_extension;
8 pub mod transport_cc_extension;
9 pub mod video_orientation_extension;
10 
11 /// A generic RTP header extension.
12 pub enum HeaderExtension {
13     AbsSendTime(abs_send_time_extension::AbsSendTimeExtension),
14     AudioLevel(audio_level_extension::AudioLevelExtension),
15     TransportCc(transport_cc_extension::TransportCcExtension),
16     VideoOrientation(video_orientation_extension::VideoOrientationExtension),
17 
18     /// A custom extension
19     Custom {
20         uri: Cow<'static, str>,
21         extension: Box<dyn Marshal + Send + Sync + 'static>,
22     },
23 }
24 
25 impl HeaderExtension {
uri(&self) -> Cow<'static, str>26     pub fn uri(&self) -> Cow<'static, str> {
27         use HeaderExtension::*;
28 
29         match self {
30             AbsSendTime(_) => "http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time".into(),
31             AudioLevel(_) => "urn:ietf:params:rtp-hdrext:ssrc-audio-level".into(),
32             TransportCc(_) => {
33                 "http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01".into()
34             }
35             VideoOrientation(_) => "urn:3gpp:video-orientation".into(),
36             Custom { uri, .. } => uri.clone(),
37         }
38     }
39 
is_same(&self, other: &Self) -> bool40     pub fn is_same(&self, other: &Self) -> bool {
41         use HeaderExtension::*;
42         match (self, other) {
43             (AbsSendTime(_), AbsSendTime(_)) => true,
44             (AudioLevel(_), AudioLevel(_)) => true,
45             (TransportCc(_), TransportCc(_)) => true,
46             (VideoOrientation(_), VideoOrientation(_)) => true,
47             (Custom { uri, .. }, Custom { uri: other_uri, .. }) => uri == other_uri,
48             _ => false,
49         }
50     }
51 }
52 
53 impl MarshalSize for HeaderExtension {
marshal_size(&self) -> usize54     fn marshal_size(&self) -> usize {
55         use HeaderExtension::*;
56         match self {
57             AbsSendTime(ext) => ext.marshal_size(),
58             AudioLevel(ext) => ext.marshal_size(),
59             TransportCc(ext) => ext.marshal_size(),
60             VideoOrientation(ext) => ext.marshal_size(),
61             Custom { extension: ext, .. } => ext.marshal_size(),
62         }
63     }
64 }
65 
66 impl Marshal for HeaderExtension {
marshal_to(&self, buf: &mut [u8]) -> util::Result<usize>67     fn marshal_to(&self, buf: &mut [u8]) -> util::Result<usize> {
68         use HeaderExtension::*;
69         match self {
70             AbsSendTime(ext) => ext.marshal_to(buf),
71             AudioLevel(ext) => ext.marshal_to(buf),
72             TransportCc(ext) => ext.marshal_to(buf),
73             VideoOrientation(ext) => ext.marshal_to(buf),
74             Custom { extension: ext, .. } => ext.marshal_to(buf),
75         }
76     }
77 }
78 
79 impl fmt::Debug for HeaderExtension {
fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result80     fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
81         use HeaderExtension::*;
82 
83         match self {
84             AbsSendTime(ext) => f.debug_tuple("AbsSendTime").field(ext).finish(),
85             AudioLevel(ext) => f.debug_tuple("AudioLevel").field(ext).finish(),
86             TransportCc(ext) => f.debug_tuple("TransportCc").field(ext).finish(),
87             VideoOrientation(ext) => f.debug_tuple("VideoOrientation").field(ext).finish(),
88             Custom { uri, extension: _ } => f.debug_struct("Custom").field("uri", uri).finish(),
89         }
90     }
91 }
92