Skip to content

Commit c7cfe3c

Browse files
authored
track_local: Add write_rtp_with_attributes (webrtc-rs#612)
This makes it easier to inject Attributes to the interceptor chain just using TrackLocalStaticRTP
1 parent 8d95518 commit c7cfe3c

File tree

2 files changed

+43
-14
lines changed

2 files changed

+43
-14
lines changed

webrtc/src/track/track_local/mod.rs

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,26 @@ use crate::rtp_transceiver::*;
2323
/// TrackLocalWriter is the Writer for outbound RTP Packets
2424
#[async_trait]
2525
pub trait TrackLocalWriter: fmt::Debug {
26+
/// write_rtp_with_attributes encrypts a RTP packet and writes to the connection.
27+
/// attributes are delivered to the interceptor chain
28+
async fn write_rtp_with_attributes(
29+
&self,
30+
pkt: &rtp::packet::Packet,
31+
attr: &Attributes,
32+
) -> Result<usize>;
33+
2634
/// write_rtp encrypts a RTP packet and writes to the connection
27-
async fn write_rtp(&self, p: &rtp::packet::Packet) -> Result<usize>;
35+
async fn write_rtp(&self, pkt: &rtp::packet::Packet) -> Result<usize> {
36+
let attr = Attributes::new();
37+
self.write_rtp_with_attributes(pkt, &attr).await
38+
}
2839

2940
/// write encrypts and writes a full RTP packet
30-
async fn write(&self, b: &[u8]) -> Result<usize>;
41+
async fn write(&self, mut b: &[u8]) -> Result<usize> {
42+
let pkt = rtp::packet::Packet::unmarshal(&mut b)?;
43+
let attr = Attributes::new();
44+
self.write_rtp_with_attributes(&pkt, &attr).await
45+
}
3146
}
3247

3348
/// TrackLocalContext is the Context passed when a TrackLocal has been Binded/Unbinded from a PeerConnection, and used
@@ -149,22 +164,20 @@ impl std::fmt::Debug for InterceptorToTrackLocalWriter {
149164

150165
#[async_trait]
151166
impl TrackLocalWriter for InterceptorToTrackLocalWriter {
152-
async fn write_rtp(&self, pkt: &rtp::packet::Packet) -> Result<usize> {
167+
async fn write_rtp_with_attributes(
168+
&self,
169+
pkt: &rtp::packet::Packet,
170+
attr: &Attributes,
171+
) -> Result<usize> {
153172
if self.is_sender_paused() {
154173
return Ok(0);
155174
}
156175

157176
let interceptor_rtp_writer = self.interceptor_rtp_writer.lock().await;
158177
if let Some(writer) = &*interceptor_rtp_writer {
159-
let a = Attributes::new();
160-
Ok(writer.write(pkt, &a).await?)
178+
Ok(writer.write(pkt, attr).await?)
161179
} else {
162180
Ok(0)
163181
}
164182
}
165-
166-
async fn write(&self, mut b: &[u8]) -> Result<usize> {
167-
let pkt = rtp::packet::Packet::unmarshal(&mut b)?;
168-
self.write_rtp(&pkt).await
169-
}
170183
}

webrtc/src/track/track_local/track_local_static_rtp.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,17 @@ impl TrackLocalStaticRTP {
8181
&self,
8282
p: &rtp::packet::Packet,
8383
extensions: &[rtp::extension::HeaderExtension],
84+
) -> Result<usize> {
85+
let attr = Attributes::new();
86+
self.write_rtp_with_extensions_attributes(p, extensions, &attr)
87+
.await
88+
}
89+
90+
pub async fn write_rtp_with_extensions_attributes(
91+
&self,
92+
p: &rtp::packet::Packet,
93+
extensions: &[rtp::extension::HeaderExtension],
94+
attr: &Attributes,
8495
) -> Result<usize> {
8596
let mut n = 0;
8697
let mut write_errs = vec![];
@@ -140,7 +151,7 @@ impl TrackLocalStaticRTP {
140151
}
141152

142153
if let Some(write_stream) = &b.write_stream {
143-
match write_stream.write_rtp(&pkt).await {
154+
match write_stream.write_rtp_with_attributes(&pkt, attr).await {
144155
Ok(m) => {
145156
n += m;
146157
}
@@ -270,7 +281,7 @@ impl TrackLocal for TrackLocalStaticRTP {
270281

271282
#[async_trait]
272283
impl TrackLocalWriter for TrackLocalStaticRTP {
273-
/// write_rtp writes a RTP Packet to the TrackLocalStaticRTP
284+
/// `write_rtp_with_attributes` writes a RTP Packet to the TrackLocalStaticRTP
274285
/// If one PeerConnection fails the packets will still be sent to
275286
/// all PeerConnections. The error message will contain the ID of the failed
276287
/// PeerConnections so you can remove them
@@ -279,8 +290,13 @@ impl TrackLocalWriter for TrackLocalStaticRTP {
279290
/// function are blocked internally. Care must be taken to not increase the sequence number
280291
/// while the sender is paused. While the actual _sending_ is blocked, the receiver will
281292
/// miss out when the sequence number "rolls over", which in turn will break SRTP.
282-
async fn write_rtp(&self, p: &rtp::packet::Packet) -> Result<usize> {
283-
self.write_rtp_with_extensions(p, &[]).await
293+
async fn write_rtp_with_attributes(
294+
&self,
295+
pkt: &rtp::packet::Packet,
296+
attr: &Attributes,
297+
) -> Result<usize> {
298+
self.write_rtp_with_extensions_attributes(pkt, &[], attr)
299+
.await
284300
}
285301

286302
/// write writes a RTP Packet as a buffer to the TrackLocalStaticRTP

0 commit comments

Comments
 (0)