Skip to content

Commit

Permalink
track_local: Add write_rtp_with_attributes (#612)
Browse files Browse the repository at this point in the history
This makes it easier to inject Attributes to the interceptor chain just
using TrackLocalStaticRTP
  • Loading branch information
haaspors authored Oct 1, 2024
1 parent 8d95518 commit c7cfe3c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 14 deletions.
33 changes: 23 additions & 10 deletions webrtc/src/track/track_local/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,26 @@ use crate::rtp_transceiver::*;
/// TrackLocalWriter is the Writer for outbound RTP Packets
#[async_trait]
pub trait TrackLocalWriter: fmt::Debug {
/// write_rtp_with_attributes encrypts a RTP packet and writes to the connection.
/// attributes are delivered to the interceptor chain
async fn write_rtp_with_attributes(
&self,
pkt: &rtp::packet::Packet,
attr: &Attributes,
) -> Result<usize>;

/// write_rtp encrypts a RTP packet and writes to the connection
async fn write_rtp(&self, p: &rtp::packet::Packet) -> Result<usize>;
async fn write_rtp(&self, pkt: &rtp::packet::Packet) -> Result<usize> {
let attr = Attributes::new();
self.write_rtp_with_attributes(pkt, &attr).await
}

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

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

#[async_trait]
impl TrackLocalWriter for InterceptorToTrackLocalWriter {
async fn write_rtp(&self, pkt: &rtp::packet::Packet) -> Result<usize> {
async fn write_rtp_with_attributes(
&self,
pkt: &rtp::packet::Packet,
attr: &Attributes,
) -> Result<usize> {
if self.is_sender_paused() {
return Ok(0);
}

let interceptor_rtp_writer = self.interceptor_rtp_writer.lock().await;
if let Some(writer) = &*interceptor_rtp_writer {
let a = Attributes::new();
Ok(writer.write(pkt, &a).await?)
Ok(writer.write(pkt, attr).await?)
} else {
Ok(0)
}
}

async fn write(&self, mut b: &[u8]) -> Result<usize> {
let pkt = rtp::packet::Packet::unmarshal(&mut b)?;
self.write_rtp(&pkt).await
}
}
24 changes: 20 additions & 4 deletions webrtc/src/track/track_local/track_local_static_rtp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,17 @@ impl TrackLocalStaticRTP {
&self,
p: &rtp::packet::Packet,
extensions: &[rtp::extension::HeaderExtension],
) -> Result<usize> {
let attr = Attributes::new();
self.write_rtp_with_extensions_attributes(p, extensions, &attr)
.await
}

pub async fn write_rtp_with_extensions_attributes(
&self,
p: &rtp::packet::Packet,
extensions: &[rtp::extension::HeaderExtension],
attr: &Attributes,
) -> Result<usize> {
let mut n = 0;
let mut write_errs = vec![];
Expand Down Expand Up @@ -140,7 +151,7 @@ impl TrackLocalStaticRTP {
}

if let Some(write_stream) = &b.write_stream {
match write_stream.write_rtp(&pkt).await {
match write_stream.write_rtp_with_attributes(&pkt, attr).await {
Ok(m) => {
n += m;
}
Expand Down Expand Up @@ -270,7 +281,7 @@ impl TrackLocal for TrackLocalStaticRTP {

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

/// write writes a RTP Packet as a buffer to the TrackLocalStaticRTP
Expand Down

0 comments on commit c7cfe3c

Please sign in to comment.