Skip to content

Commit

Permalink
add create_ice_transport
Browse files Browse the repository at this point in the history
  • Loading branch information
yngrtc committed Mar 30, 2024
1 parent d4d56c9 commit 71431f7
Show file tree
Hide file tree
Showing 4 changed files with 166 additions and 262 deletions.
56 changes: 18 additions & 38 deletions rtc/src/handler/ice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::transport::ice_transport::{IceTransportEvent, RTCIceTransport};
use bytes::BytesMut;
use ice::Event;
use log::{debug, error, warn};
use shared::error::{Error, Result};
use shared::error::Result;
use shared::Transmit;
use std::time::Instant;

Expand All @@ -18,15 +18,7 @@ impl RTCHandler for RTCIceTransport {
message,
};

let try_read = || -> Result<()> {
let ice_agent = self
.gatherer
.agent
.as_mut()
.ok_or(Error::ErrICEAgentNotExist)?;

ice_agent.handle_read(stun_transmit)
};
let try_read = || -> Result<()> { self.gatherer.agent.handle_read(stun_transmit) };

if let Err(err) = try_read() {
warn!("try_read got error {}", err);
Expand Down Expand Up @@ -63,22 +55,18 @@ impl RTCHandler for RTCIceTransport {
}

fn poll_event(&mut self) -> Option<RTCEvent> {
if let Some(ice_agent) = self.gatherer.agent.as_mut() {
if let Some(event) = ice_agent.poll_event() {
match event {
Event::ConnectionStateChange(state) => Some(RTCEvent::IceTransportEvent(
IceTransportEvent::OnConnectionStateChange(state.into()),
)),
Event::SelectedCandidatePairChange(local, remote) => {
Some(RTCEvent::IceTransportEvent(
IceTransportEvent::OnSelectedCandidatePairChange(Box::new(
RTCIceCandidatePair::new((&*local).into(), (&*remote).into()),
)),
))
}
if let Some(event) = self.gatherer.agent.poll_event() {
match event {
Event::ConnectionStateChange(state) => Some(RTCEvent::IceTransportEvent(
IceTransportEvent::OnConnectionStateChange(state.into()),
)),
Event::SelectedCandidatePairChange(local, remote) => {
Some(RTCEvent::IceTransportEvent(
IceTransportEvent::OnSelectedCandidatePairChange(Box::new(
RTCIceCandidatePair::new((&*local).into(), (&*remote).into()),
)),
))
}
} else {
None
}
} else {
None
Expand All @@ -88,14 +76,8 @@ impl RTCHandler for RTCIceTransport {
/// Handles a timeout event
fn handle_timeout(&mut self, now: Instant) {
let mut try_timeout = || -> Result<()> {
let ice_agent = self
.gatherer
.agent
.as_mut()
.ok_or(Error::ErrICEAgentNotExist)?;

ice_agent.handle_timeout(now);
while let Some(transmit) = ice_agent.poll_transmit() {
self.gatherer.agent.handle_timeout(now);
while let Some(transmit) = self.gatherer.agent.poll_transmit() {
self.transmits.push_back(Transmit {
now: transmit.now,
transport: transmit.transport,
Expand All @@ -116,11 +98,9 @@ impl RTCHandler for RTCIceTransport {

/// Polls a timeout event
fn poll_timeout(&mut self, eto: &mut Instant) {
if let Some(ice_agent) = self.gatherer.agent.as_mut() {
if let Some(timeout) = ice_agent.poll_timeout() {
if timeout < *eto {
*eto = timeout;
}
if let Some(timeout) = self.gatherer.agent.poll_timeout() {
if timeout < *eto {
*eto = timeout;
}
}
}
Expand Down
116 changes: 85 additions & 31 deletions rtc/src/peer_connection/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ use crate::peer_connection::sdp::session_description::RTCSessionDescription;
use crate::peer_connection::signaling_state::{
/*check_next_signaling_state,*/ RTCSignalingState, //StateChangeOp,
};
//use crate::transport::dtls_transport::RTCDtlsTransport;
use crate::transport::ice_transport::ice_gatherer::{RTCIceGatherOptions, RTCIceGatherer};
use crate::transport::ice_transport::RTCIceTransport;
//use crate::transport::sctp_transport::RTCSctpTransport;
/*use crate::rtp_transceiver::rtp_codec::{RTCRtpHeaderExtensionCapability, RTPCodecType};
use crate::rtp_transceiver::rtp_receiver::RTCRtpReceiver;
use crate::rtp_transceiver::rtp_sender::RTCRtpSender;
Expand Down Expand Up @@ -177,8 +181,9 @@ pub struct RTCPeerConnection {
pub(super) pending_local_description: Option<RTCSessionDescription>,
pub(super) pending_remote_description: Option<RTCSessionDescription>,

pub(super) ice_agent: ice::Agent,

pub(super) ice_transport: RTCIceTransport,
//pub(super) dtls_transport: RTCDtlsTransport,
//pub(super) sctp_transport: RTCSctpTransport,
/// ops is an operations queue which will ensure the enqueued actions are
/// executed in order. It is used for asynchronously, but serially processing
/// remote and local descriptions
Expand Down Expand Up @@ -228,36 +233,15 @@ impl RTCPeerConnection {
pub(crate) fn new(api: &API, mut configuration: RTCConfiguration) -> Result<Self> {
RTCPeerConnection::init_configuration(&mut configuration)?;

let mut candidate_types = vec![];
if api.setting_engine.candidates.ice_lite {
candidate_types.push(ice::candidate::CandidateType::Host);
} else if configuration.ice_transport_policy == RTCIceTransportPolicy::Relay {
candidate_types.push(ice::candidate::CandidateType::Relay);
}

let mut validated_servers = vec![];
for server in configuration.get_ice_servers() {
let url = server.urls()?;
validated_servers.extend(url);
}
// Create the ICE transport
let ice_transport = Self::create_ice_transport(api, &configuration)?;

let ice_agent_config = ice::AgentConfig {
lite: api.setting_engine.candidates.ice_lite,
urls: validated_servers,
disconnected_timeout: api.setting_engine.timeout.ice_disconnected_timeout,
failed_timeout: api.setting_engine.timeout.ice_failed_timeout,
keepalive_interval: api.setting_engine.timeout.ice_keepalive_interval,
candidate_types,
host_acceptance_min_wait: api.setting_engine.timeout.ice_host_acceptance_min_wait,
srflx_acceptance_min_wait: api.setting_engine.timeout.ice_srflx_acceptance_min_wait,
prflx_acceptance_min_wait: api.setting_engine.timeout.ice_prflx_acceptance_min_wait,
relay_acceptance_min_wait: api.setting_engine.timeout.ice_relay_acceptance_min_wait,
local_ufrag: api.setting_engine.candidates.username_fragment.clone(),
local_pwd: api.setting_engine.candidates.password.clone(),
..Default::default()
};
// Create the DTLS transport
//let certificates = configuration.certificates.drain(..).collect();
//let dtls_transport = api.new_dtls_transport(Arc::clone(&pc.ice_transport), certificates)?);

let ice_agent = ice::Agent::new(Arc::new(ice_agent_config))?;
// Create the SCTP transport
//let sctp_transport = Arc::new(api.new_sctp_transport(Arc::clone(&pc.dtls_transport))?);

// <https://w3c.github.io/webrtc-pc/#constructor> (Step #2)
// Some variables defined explicitly despite their implicit zero values to
Expand Down Expand Up @@ -291,7 +275,9 @@ impl RTCPeerConnection {
setting_engine: api.setting_engine.clone(),
media_engine: api.media_engine.clone(),
is_negotiation_needed: false,
ice_agent,

ice_transport,

events: Default::default(),
})
}
Expand Down Expand Up @@ -690,6 +676,74 @@ impl RTCPeerConnection {
self.stats_id.as_str()
}

fn create_ice_gatherer(
ice_agent: ice::Agent,
opts: RTCIceGatherOptions,
setting_engine: &Arc<SettingEngine>,
) -> Result<RTCIceGatherer> {
let mut validated_servers = vec![];
if !opts.ice_servers.is_empty() {
for server in &opts.ice_servers {
let url = server.urls()?;
validated_servers.extend(url);
}
}

Ok(RTCIceGatherer::new(
ice_agent,
validated_servers,
opts.ice_gather_policy,
Arc::clone(setting_engine),
))
}

fn create_ice_transport(
api: &API,
configuration: &RTCConfiguration,
) -> Result<RTCIceTransport> {
let mut candidate_types = vec![];
if api.setting_engine.candidates.ice_lite {
candidate_types.push(ice::candidate::CandidateType::Host);
} else if configuration.ice_transport_policy == RTCIceTransportPolicy::Relay {
candidate_types.push(ice::candidate::CandidateType::Relay);
}

let mut validated_servers = vec![];
for server in configuration.get_ice_servers() {
let url = server.urls()?;
validated_servers.extend(url);
}

let ice_agent_config = ice::AgentConfig {
lite: api.setting_engine.candidates.ice_lite,
urls: validated_servers,
disconnected_timeout: api.setting_engine.timeout.ice_disconnected_timeout,
failed_timeout: api.setting_engine.timeout.ice_failed_timeout,
keepalive_interval: api.setting_engine.timeout.ice_keepalive_interval,
candidate_types,
host_acceptance_min_wait: api.setting_engine.timeout.ice_host_acceptance_min_wait,
srflx_acceptance_min_wait: api.setting_engine.timeout.ice_srflx_acceptance_min_wait,
prflx_acceptance_min_wait: api.setting_engine.timeout.ice_prflx_acceptance_min_wait,
relay_acceptance_min_wait: api.setting_engine.timeout.ice_relay_acceptance_min_wait,
local_ufrag: api.setting_engine.candidates.username_fragment.clone(),
local_pwd: api.setting_engine.candidates.password.clone(),
..Default::default()
};

// Create the ICE transport
let ice_agent = ice::Agent::new(Arc::new(ice_agent_config))?;
let ice_gatherer = Self::create_ice_gatherer(
ice_agent,
RTCIceGatherOptions {
ice_servers: configuration.get_ice_servers(),
ice_gather_policy: configuration.ice_transport_policy,
},
&api.setting_engine,
)?;

Ok(RTCIceTransport::new(ice_gatherer))
}

/*
/// create_offer starts the PeerConnection and generates the localDescription
/// <https://w3c.github.io/webrtc-pc/#dom-rtcpeerconnection-createoffer>
Expand Down
Loading

0 comments on commit 71431f7

Please sign in to comment.