Skip to content

Commit

Permalink
restore wait for ice gathering to complete
Browse files Browse the repository at this point in the history
  • Loading branch information
johanhelsing committed Dec 26, 2022
1 parent 915b2db commit ce19681
Showing 1 changed file with 48 additions and 4 deletions.
52 changes: 48 additions & 4 deletions matchbox_socket/src/webrtc_socket/wasm/message_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use wasm_bindgen::{prelude::*, JsCast, JsValue};
use wasm_bindgen_futures::JsFuture;
use web_sys::{
MessageEvent, RtcConfiguration, RtcDataChannel, RtcDataChannelInit, RtcDataChannelType,
RtcIceCandidateInit, RtcPeerConnection, RtcPeerConnectionIceEvent, RtcSdpType,
RtcSessionDescriptionInit,
RtcIceCandidateInit, RtcIceGatheringState, RtcPeerConnection, RtcPeerConnectionIceEvent,
RtcSdpType, RtcSessionDescriptionInit,
};

use crate::webrtc_socket::{
Expand Down Expand Up @@ -164,6 +164,13 @@ async fn handshake_offer(
.await
.efix()?;
debug!("created offer for new peer");

// todo: the point of implementing ice trickle is to avoid this wait...
// however, for some reason removing this wait causes problems with NAT
// punching in practice.
// We should figure out why this is happening.
wait_for_ice_gathering_complete(conn.clone()).await;

signal_peer.send(PeerSignal::Offer(conn.local_description().unwrap().sdp()));

let mut received_candidates = vec![];
Expand Down Expand Up @@ -255,7 +262,10 @@ async fn handshake_offer(
conn.set_onicecandidate(Some(onicecandidate.as_ref().unchecked_ref()));
onicecandidate.forget();

debug!("Ice completed: {:?}", conn.ice_gathering_state());
debug!(
"handshake_offer completed, ice gathering state: {:?}",
conn.ice_gathering_state()
);

Ok((signal_peer.id, data_channel))
}
Expand Down Expand Up @@ -353,6 +363,12 @@ async fn handshake_accept(
.await
.efix()?;

// todo: the point of implementing ice trickle is to avoid this wait...
// however, for some reason removing this wait causes problems with NAT
// punching in practice.
// We should figure out why this is happening.
wait_for_ice_gathering_complete(conn.clone()).await;

let answer = PeerSignal::Answer(conn.local_description().unwrap().sdp());
signal_peer.send(answer);

Expand Down Expand Up @@ -415,7 +431,10 @@ async fn handshake_accept(
conn.set_onicecandidate(Some(onicecandidate.as_ref().unchecked_ref()));
onicecandidate.forget();

debug!("Ice completed: {:?}", conn.ice_gathering_state());
debug!(
"handshake_accept completed, ice gathering state: {:?}",
conn.ice_gathering_state()
);

Ok((signal_peer.id, data_channel))
}
Expand Down Expand Up @@ -454,6 +473,31 @@ fn create_rtc_peer_connection(config: &WebRtcSocketConfig) -> RtcPeerConnection
connection
}

async fn wait_for_ice_gathering_complete(conn: RtcPeerConnection) {
if conn.ice_gathering_state() == RtcIceGatheringState::Complete {
debug!("Ice gathering already completed");
return;
}

let (mut tx, mut rx) = futures_channel::mpsc::channel(1);

let conn_clone = conn.clone();
let onstatechange: Box<dyn FnMut(JsValue)> = Box::new(move |_| {
if conn_clone.ice_gathering_state() == RtcIceGatheringState::Complete {
tx.try_send(()).unwrap();
}
});

let onstatechange = Closure::wrap(onstatechange);

conn.set_onicegatheringstatechange(Some(onstatechange.as_ref().unchecked_ref()));

rx.next().await;

conn.set_onicegatheringstatechange(None);
debug!("Ice gathering completed");
}

fn create_data_channel(
connection: RtcPeerConnection,
incoming_tx: futures_channel::mpsc::UnboundedSender<(PeerId, Packet)>,
Expand Down

0 comments on commit ce19681

Please sign in to comment.