rtci 0.5.0

A sans-I/O WebRTC implementation: peer connections with ICE, DTLS-SRTP, SCTP data channels, RTP/RTCP and JSEP negotiation
Documentation
//! When the engine's own gathering may start, and what it gathers before a local description
//! represents the candidates' ICE generation (W3C webrtc-pc 5.6 `[[EarlyCandidates]]`).
//!
//! RFC 9429 section 4.1.1: pre-gathering "MUST only occur upon application request, and
//! therefore the default candidate pool size MUST be zero"; section 3.5.4 has gathering begin
//! with `set_local_description`. The engine keeps no candidate pool (`iceCandidatePoolSize` is
//! stored only), so it sends nothing to a STUN or TURN server before the first local
//! description, whatever host candidates the application adds before it, unless the
//! application asks with `SettingEngineBuilder::with_gathering_before_local_description`.
//!
//! W3C webrtc-pc 5.6: "If either connection.[[PendingLocalDescription]] or
//! connection.[[CurrentLocalDescription]] are not null, and represent the ICE generation for
//! which candidate was gathered, surface the candidate with candidate and connection, and abort
//! these steps. Otherwise, append candidate to connection.[[EarlyCandidates]]." A candidate the
//! engine gathers itself (server-reflexive, relayed, its ICE-TCP candidates) for a generation no
//! local description represents yet (before the first one; an ICE restart whose offer or answer
//! is not set yet) is held, and released when `set_local_description` applies a description of
//! its generation (4.4.1.4 "release early candidates"); one of a generation no transport is in
//! any more (an abandoned restart) is dropped. The application's own candidates are surfaced as
//! it adds them: it gathers and times them itself.

use super::RTCPeerConnection;
use super::event::RTCPeerConnectionEvent;

/// `[[EarlyCandidates]]` of the engine's gathering.
#[derive(Default)]
pub(crate) struct EarlyCandidates {
    /// The generation (ufrag) and the `icecandidate` event, in order.
    held: Vec<(String, RTCPeerConnectionEvent)>,
    /// Set while the engine adds a candidate it gathered itself
    /// (`add_engine_local_candidate`).
    pub(crate) engine_candidate: bool,
}

impl RTCPeerConnection {
    /// Holds `event`, the `icecandidate` event of a candidate of ICE generation `ufrag`, when
    /// the engine gathered it and no local description represents its generation yet; returns
    /// it otherwise.
    pub(crate) fn hold_early_candidate(
        &mut self,
        event: RTCPeerConnectionEvent,
        ufrag: &str,
    ) -> Option<RTCPeerConnectionEvent> {
        if self.early_candidates.engine_candidate
            && !self.local_ice_ufrags().iter().any(|u| u == ufrag)
        {
            self.early_candidates.held.push((ufrag.to_owned(), event));
            None
        } else {
            Some(event)
        }
    }

    /// W3C webrtc-pc 4.4.1.4 "release early candidates": after `set_local_description` (or a
    /// rollback), the held candidates whose generation the current or pending local description
    /// represents are surfaced, in order; those of a generation no transport is in any more are
    /// dropped; the others wait for their description.
    pub(crate) fn release_early_candidates(&mut self) {
        if self.early_candidates.held.is_empty() {
            return;
        }
        let described = self.local_ice_ufrags();
        let live: Vec<String> = self
            .pipeline_context
            .transports
            .ids()
            .into_iter()
            .map(|id| {
                self.ice_transport_of(id)
                    .agent
                    .get_local_credentials()
                    .ufrag
                    .clone()
            })
            .collect();
        let mut waiting = vec![];
        for (ufrag, event) in std::mem::take(&mut self.early_candidates.held) {
            if described.contains(&ufrag) {
                self.pipeline_context.event_outs.push_back(event);
            } else if live.contains(&ufrag) {
                waiting.push((ufrag, event));
            } else {
                log::debug!("a held candidate of an abandoned ICE generation is dropped");
            }
        }
        self.early_candidates.held = waiting;
    }

    /// RFC 9429 sections 4.1.1 and 3.5.4: whether the engine may send its gathering requests to
    /// STUN and TURN servers now: from the first local description on, before it only under
    /// `SettingEngineBuilder::with_gathering_before_local_description`.
    pub(crate) fn engine_gathering_allowed(&self) -> bool {
        self.local_description_was_set || self.setting_engine.gathering_before_local_description
    }
}