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

unify add/remove_track arguments for peer connections #523

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,12 @@ async fn remove_video(
r: Request<Body>,
) -> Result<Response<Body>, hyper::Error> {
let senders = pc.get_senders().await;

if !senders.is_empty() {
if let Err(err) = pc.remove_track(&senders[0]).await {
panic!("{}", err);
if let Some(track) = senders[0].track().await {
if let Err(err) = pc.remove_track(track).await {
panic!("{}", err);
}
}
}

Expand Down
16 changes: 7 additions & 9 deletions webrtc/src/peer_connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1731,26 +1731,24 @@ impl RTCPeerConnection {
}

/// remove_track removes a Track from the PeerConnection
pub async fn remove_track(&self, sender: &Arc<RTCRtpSender>) -> Result<()> {
pub async fn remove_track(&self, track: Arc<dyn TrackLocal + Send + Sync>) -> Result<()> {
if self.internal.is_closed.load(Ordering::SeqCst) {
return Err(Error::ErrConnectionClosed);
}

let mut transceiver = None;
let mut signals = None;
{
let rtp_transceivers = self.internal.rtp_transceivers.lock().await;
for t in &*rtp_transceivers {
if t.sender().await.id == sender.id {
if sender.track().await.is_none() {
return Ok(());
}
transceiver = Some(t.clone());
let sender = t.sender().await;
if sender.id == track.id() {
signals = Some((t.clone(), sender.clone()));
break;
}
}
}

let t = transceiver.ok_or(Error::ErrSenderNotCreatedByConnection)?;
let (t, s) = signals.ok_or(Error::ErrSenderNotCreatedByConnection)?;

// This also happens in `set_sending_track` but we need to make sure we do this
// before we call sender.stop to avoid a race condition when removing tracks and
Expand All @@ -1760,7 +1758,7 @@ impl RTCPeerConnection {
t.direction().has_recv(),
));
// Stop the sender
let sender_result = sender.stop().await;
let sender_result = s.stop().await;
// This also updates direction
let sending_track_result = t.set_sending_track(None).await;

Expand Down