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