Skip to content

Commit a2847e5

Browse files
Add generic rtp::extension::HeaderExtension
1 parent 7220f44 commit a2847e5

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

rtp/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
* Adds a new generic header extensions type `rtp::extension::HeaderExtension` which allows abstracting over all known extensions as well as custom extensions.
6+
57
## v0.6.7
68

79
* Bumped util dependecy to `0.6.0`.

rtp/src/extension/mod.rs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,79 @@
1+
use std::borrow::Cow;
2+
use std::fmt;
3+
4+
use util::{Marshal, MarshalSize};
5+
16
pub mod abs_send_time_extension;
27
pub mod audio_level_extension;
38
pub mod transport_cc_extension;
49
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 {
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+
}
40+
41+
impl MarshalSize for HeaderExtension {
42+
fn marshal_size(&self) -> usize {
43+
use HeaderExtension::*;
44+
match self {
45+
AbsSendTime(ext) => ext.marshal_size(),
46+
AudioLevel(ext) => ext.marshal_size(),
47+
TransportCc(ext) => ext.marshal_size(),
48+
VideoOrientation(ext) => ext.marshal_size(),
49+
Custom { extension: ext, .. } => ext.marshal_size(),
50+
}
51+
}
52+
}
53+
54+
impl Marshal for HeaderExtension {
55+
fn marshal_to(&self, buf: &mut [u8]) -> util::Result<usize> {
56+
use HeaderExtension::*;
57+
match self {
58+
AbsSendTime(ext) => ext.marshal_to(buf),
59+
AudioLevel(ext) => ext.marshal_to(buf),
60+
TransportCc(ext) => ext.marshal_to(buf),
61+
VideoOrientation(ext) => ext.marshal_to(buf),
62+
Custom { extension: ext, .. } => ext.marshal_to(buf),
63+
}
64+
}
65+
}
66+
67+
impl fmt::Debug for HeaderExtension {
68+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
69+
use HeaderExtension::*;
70+
71+
match self {
72+
AbsSendTime(ext) => f.debug_tuple("AbsSendTime").field(ext).finish(),
73+
AudioLevel(ext) => f.debug_tuple("AudioLevel").field(ext).finish(),
74+
TransportCc(ext) => f.debug_tuple("TransportCc").field(ext).finish(),
75+
VideoOrientation(ext) => f.debug_tuple("VideoOrientation").field(ext).finish(),
76+
Custom { uri, extension: _ } => f.debug_struct("Custom").field("uri", uri).finish(),
77+
}
78+
}
79+
}

0 commit comments

Comments
 (0)