Skip to content

Commit

Permalink
fix ice state disconnected bug
Browse files Browse the repository at this point in the history
  • Loading branch information
yngrtc committed Jan 21, 2024
1 parent f8862ef commit 2728215
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 26 deletions.
2 changes: 1 addition & 1 deletion examples/signal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ fn handle_offer_message(
);

let offer_sdp = serde_json::from_str::<RTCSessionDescription>(&offer_str)?;
let answer = server_states.accept_offer(session_id, endpoint_id, offer_sdp)?;
let answer = server_states.accept_offer(session_id, endpoint_id, None, offer_sdp)?;
let answer_str = serde_json::to_string(&answer)?;
info!("generate answer sdp: {}", answer_str);
Ok(Bytes::from(answer_str))
Expand Down
10 changes: 6 additions & 4 deletions src/handlers/gateway/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,12 @@ impl GatewayInbound {
};

let response_sdp = match request_sdp.sdp_type {
RTCSdpType::Offer => {
self.server_states
.accept_offer(session_id, endpoint_id, request_sdp)?
}
RTCSdpType::Offer => self.server_states.accept_offer(
session_id,
endpoint_id,
Some((&transport_context).into()),
request_sdp,
)?,
RTCSdpType::Answer => self.handle_answer_sdp(request_sdp)?,
_ => {
return Err(Error::Other(format!(
Expand Down
30 changes: 16 additions & 14 deletions src/server/session/description/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,25 +140,27 @@ pub enum RTCIceGatheringState {
Complete,
}

fn append_candidate_if_new(
c: &SocketAddr,
component: u16,
m: MediaDescription,
) -> MediaDescription {
let marshaled = format!("1 {} UDP 1 {} {} typ host", component, c.ip(), c.port());
for a in &m.attributes {
if let Some(value) = &a.value {
if &marshaled == value {
return m;
}
}
}
m.with_value_attribute("candidate".to_owned(), marshaled)
}

pub(crate) fn add_candidate_to_media_descriptions(
candidate: &SocketAddr,
mut m: MediaDescription,
ice_gathering_state: RTCIceGatheringState,
) -> Result<MediaDescription> {
let append_candidate_if_new =
|c: &SocketAddr, component: u16, m: MediaDescription| -> MediaDescription {
let marshaled = format!("1 {} UDP 1 {} {} typ host", component, c.ip(), c.port());
for a in &m.attributes {
if let Some(value) = &a.value {
if &marshaled == value {
return m;
}
}
}

m.with_value_attribute("candidate".to_owned(), marshaled)
};

m = append_candidate_if_new(candidate, 1, m); // 1: RTP

//TODO: m = append_candidate_if_new(candidate, 2, m); // 2: RTCP
Expand Down
24 changes: 17 additions & 7 deletions src/server/states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,23 +58,33 @@ impl ServerStates {
&self,
session_id: SessionId,
endpoint_id: EndpointId,
four_tuple: Option<FourTuple>,
mut offer: RTCSessionDescription,
) -> Result<RTCSessionDescription> {
let parsed = offer.unmarshal()?;
let remote_conn_cred = ConnectionCredentials::from_sdp(&parsed)?;
offer.parsed = Some(parsed);

let local_conn_cred = ConnectionCredentials::new(
&self.server_config.certificates,
remote_conn_cred.dtls_params.role,
);

let session = self.create_or_get_session(session_id);
let endpoint = session.get_endpoint(&endpoint_id);

if let Some(endpoint) = endpoint.as_ref() {
let local_conn_cred = if let Some(endpoint) = endpoint.as_ref() {
session.set_remote_description(endpoint, &offer)?;
}
let four_tuple = four_tuple.ok_or(Error::Other("missing FourTuple".to_string()))?;
let transport = endpoint
.get_transport(&four_tuple)
.ok_or(Error::Other(format!(
"can't find transport for endpoint id {} with {:?}",
endpoint_id, four_tuple
)))?;
transport.candidate().local_connection_credentials().clone()
} else {
ConnectionCredentials::new(
&self.server_config.certificates,
remote_conn_cred.dtls_params.role,
)
};

let answer = session.create_answer(&endpoint, &offer, &local_conn_cred.ice_params)?;

if endpoint.is_none() {
Expand Down

0 comments on commit 2728215

Please sign in to comment.