Skip to content
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
5 changes: 5 additions & 0 deletions .changeset/session_description_signaling_thread.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
webrtc-sys: patch
---

Copy peer connection session descriptions on the WebRTC signaling thread to prevent crashes when a description is replaced concurrently.
9 changes: 8 additions & 1 deletion libwebrtc/src/peer_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ mod tests {
use log::trace;
use tokio::sync::mpsc;

use crate::{peer_connection::*, peer_connection_factory::*};
use crate::{peer_connection::*, peer_connection_factory::*, session_description::SdpType};

#[tokio::test]
async fn create_pc() {
Expand All @@ -350,6 +350,8 @@ mod tests {

let bob = factory.create_peer_connection(config.clone()).unwrap();
let alice = factory.create_peer_connection(config.clone()).unwrap();
assert!(bob.current_local_description().is_none());
assert!(bob.current_remote_description().is_none());

let (bob_ice_tx, mut bob_ice_rx) = mpsc::unbounded_channel::<IceCandidate>();
let (alice_ice_tx, mut alice_ice_rx) = mpsc::unbounded_channel::<IceCandidate>();
Expand Down Expand Up @@ -378,6 +380,8 @@ mod tests {
trace!("Alice answer: {:?}", answer);
alice.set_local_description(answer.clone()).await.unwrap();
bob.set_remote_description(answer).await.unwrap();
let local_description = bob.current_local_description().unwrap();
let remote_description = bob.current_remote_description().unwrap();

let bob_ice = bob_ice_rx.recv().await.unwrap();
let alice_ice = alice_ice_rx.recv().await.unwrap();
Expand All @@ -396,5 +400,8 @@ mod tests {

alice.close();
bob.close();
drop(bob);
assert_eq!(local_description.sdp_type(), SdpType::Offer);
assert_eq!(remote_description.sdp_type(), SdpType::Answer);
}
}
68 changes: 38 additions & 30 deletions webrtc-sys/src/peer_connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -287,56 +287,64 @@ rust::Vec<RtpTransceiverPtr> PeerConnection::get_transceivers() const {
return vec;
}

namespace {

using DescriptionGetter = const webrtc::SessionDescriptionInterface* (
webrtc::PeerConnectionInterface::*)() const;

std::unique_ptr<SessionDescription> clone_session_description(
webrtc::Thread* signaling_thread,
const webrtc::PeerConnectionInterface* peer_connection,
DescriptionGetter getter) {
// WebRTC may replace the borrowed description on the signaling thread.
return signaling_thread->BlockingCall([&] {
const auto* description = (peer_connection->*getter)();
return description
? std::make_unique<SessionDescription>(description->Clone())
: nullptr;
});
}

} // namespace

std::unique_ptr<SessionDescription> PeerConnection::current_local_description()
const {
auto local_description = peer_connection_->current_local_description();
if (local_description)
return std::make_unique<SessionDescription>(local_description->Clone());

return nullptr;
return clone_session_description(
rtc_runtime_->signaling_thread(), peer_connection_.get(),
&webrtc::PeerConnectionInterface::current_local_description);
}

std::unique_ptr<SessionDescription> PeerConnection::current_remote_description()
const {
auto remote_description = peer_connection_->current_remote_description();
if (remote_description)
return std::make_unique<SessionDescription>(remote_description->Clone());

return nullptr;
return clone_session_description(
rtc_runtime_->signaling_thread(), peer_connection_.get(),
&webrtc::PeerConnectionInterface::current_remote_description);
}

std::unique_ptr<SessionDescription> PeerConnection::pending_local_description()
const {
auto local_description = peer_connection_->pending_local_description();
if (local_description)
return std::make_unique<SessionDescription>(local_description->Clone());

return nullptr;
return clone_session_description(
rtc_runtime_->signaling_thread(), peer_connection_.get(),
&webrtc::PeerConnectionInterface::pending_local_description);
}

std::unique_ptr<SessionDescription> PeerConnection::pending_remote_description()
const {
auto remote_description = peer_connection_->pending_remote_description();
if (remote_description)
return std::make_unique<SessionDescription>(remote_description->Clone());

return nullptr;
return clone_session_description(
rtc_runtime_->signaling_thread(), peer_connection_.get(),
&webrtc::PeerConnectionInterface::pending_remote_description);
}

std::unique_ptr<SessionDescription> PeerConnection::local_description() const {
auto local_description = peer_connection_->local_description();
if (local_description)
return std::make_unique<SessionDescription>(local_description->Clone());

return nullptr;
return clone_session_description(
rtc_runtime_->signaling_thread(), peer_connection_.get(),
&webrtc::PeerConnectionInterface::local_description);
}

std::unique_ptr<SessionDescription> PeerConnection::remote_description() const {
auto remote_description = peer_connection_->remote_description();
if (remote_description)
return std::make_unique<SessionDescription>(remote_description->Clone());

return nullptr;
return clone_session_description(
rtc_runtime_->signaling_thread(), peer_connection_.get(),
&webrtc::PeerConnectionInterface::remote_description);
}

PeerConnectionState PeerConnection::connection_state() const {
Expand Down
Loading