From 566b4eea2900202d72ca1fc3d3f9720d32f88d54 Mon Sep 17 00:00:00 2001 From: Romain Leroux Date: Sat, 23 Sep 2023 20:33:11 +0200 Subject: [PATCH] Add IceState --- src/peerconnection.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/peerconnection.rs b/src/peerconnection.rs index 4a2cb36..9752a25 100644 --- a/src/peerconnection.rs +++ b/src/peerconnection.rs @@ -79,6 +79,32 @@ impl SignalingState { } } +#[derive(Debug, PartialEq, Eq)] +pub enum IceState { + New, + Checking, + Connected, + Completed, + Failed, + Disconnected, + Closed, +} + +impl IceState { + fn from_raw(state: sys::rtcIceState) -> Self { + match state { + sys::rtcIceState_RTC_ICE_NEW => Self::New, + sys::rtcIceState_RTC_ICE_CHECKING => Self::Checking, + sys::rtcIceState_RTC_ICE_CONNECTED => Self::Connected, + sys::rtcIceState_RTC_ICE_COMPLETED => Self::Completed, + sys::rtcIceState_RTC_ICE_FAILED => Self::Failed, + sys::rtcIceState_RTC_ICE_DISCONNECTED => Self::Disconnected, + sys::rtcIceState_RTC_ICE_CLOSED => Self::Closed, + _ => panic!("Unknown rtcIceState: {}", state), + } + } +} + #[derive(Debug, PartialEq, Eq, PartialOrd, Hash)] pub struct CandidatePair { pub local: String, @@ -174,6 +200,7 @@ pub trait PeerConnectionHandler { fn on_connection_state_change(&mut self, state: ConnectionState) {} fn on_gathering_state_change(&mut self, state: GatheringState) {} fn on_signaling_state_change(&mut self, state: SignalingState) {} + fn on_ice_state_change(&mut self, state: IceState) {} fn on_data_channel(&mut self, data_channel: Box>) {} } @@ -231,6 +258,11 @@ where Some(RtcPeerConnection::

::signaling_state_cb), ))?; + check(sys::rtcSetIceStateChangeCallback( + id, + Some(RtcPeerConnection::

::ice_state_cb), + ))?; + check(sys::rtcSetDataChannelCallback( id, Some(RtcPeerConnection::

::data_channel_cb), @@ -317,6 +349,15 @@ where rtc_pc.pc_handler.on_signaling_state_change(state); } + unsafe extern "C" fn ice_state_cb(_: i32, state: sys::rtcIceState, ptr: *mut c_void) { + let rtc_pc = &mut *(ptr as *mut RtcPeerConnection

); + + let state = IceState::from_raw(state); + + let _guard = rtc_pc.lock.lock(); + rtc_pc.pc_handler.on_ice_state_change(state); + } + unsafe extern "C" fn data_channel_cb(_: i32, id: i32, ptr: *mut c_void) { let rtc_pc = &mut *(ptr as *mut RtcPeerConnection

);