Skip to content

Commit

Permalink
Add IceState
Browse files Browse the repository at this point in the history
  • Loading branch information
lerouxrgd committed Sep 23, 2023
1 parent 0678e0a commit 566b4ee
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/peerconnection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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<RtcDataChannel<Self::DCH>>) {}
}

Expand Down Expand Up @@ -231,6 +258,11 @@ where
Some(RtcPeerConnection::<P>::signaling_state_cb),
))?;

check(sys::rtcSetIceStateChangeCallback(
id,
Some(RtcPeerConnection::<P>::ice_state_cb),
))?;

check(sys::rtcSetDataChannelCallback(
id,
Some(RtcPeerConnection::<P>::data_channel_cb),
Expand Down Expand Up @@ -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<P>);

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<P>);

Expand Down

0 comments on commit 566b4ee

Please sign in to comment.