Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

track_local: Add write_rtp_with_attributes #612

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading