Skip to main content

RTCPeerConnectionState

Enum RTCPeerConnectionState 

Source
pub enum RTCPeerConnectionState {
    Unspecified,
    New,
    Connecting,
    Connected,
    Disconnected,
    Failed,
    Closed,
}
Expand description

Indicates the overall state of the peer connection.

RTCPeerConnectionState is an aggregate state that combines the states of both the ICE transport layer and the DTLS transport layer. It provides a high-level view of whether the connection is ready for media to flow.

§State Determination

This state is derived from the combination of:

  • ICE transport states - Network connectivity
  • DTLS transport states - Encryption handshake

The peer connection is only fully “connected” when both ICE and DTLS are successfully established.

§State Transitions

Typical progression for a successful connection:

New → Connecting → Connected

If problems occur:

Connected → Disconnected → (may recover to Connected)
Connected → Failed (permanent failure)
Any state → Closed (connection closed)

§Examples

§Monitoring Overall Connection State

use rtc::peer_connection::state::RTCPeerConnectionState;
use rtc::peer_connection::event::RTCPeerConnectionEvent;

match event {
    RTCPeerConnectionEvent::OnConnectionStateChangeEvent(state) => {
        match state {
            RTCPeerConnectionState::New => {
                println!("Connection initialized");
            }
            RTCPeerConnectionState::Connecting => {
                println!("Establishing connection (ICE + DTLS)...");
            }
            RTCPeerConnectionState::Connected => {
                println!("✓ Connection established - media can flow!");
            }
            RTCPeerConnectionState::Disconnected => {
                println!("⚠ Connection lost - may reconnect automatically");
            }
            RTCPeerConnectionState::Failed => {
                println!("✗ Connection failed - requires ICE restart");
            }
            RTCPeerConnectionState::Closed => {
                println!("Connection closed");
            }
            _ => {}
        }
    }
    _ => {}
}

§Checking if Connection is Active

use rtc::peer_connection::state::RTCPeerConnectionState;

fn is_connected(state: RTCPeerConnectionState) -> bool {
    matches!(state, RTCPeerConnectionState::Connected)
}

fn can_send_media(state: RTCPeerConnectionState) -> bool {
    matches!(state, RTCPeerConnectionState::Connected)
}

assert!(is_connected(RTCPeerConnectionState::Connected));
assert!(!is_connected(RTCPeerConnectionState::Connecting));
assert!(can_send_media(RTCPeerConnectionState::Connected));

§String Conversion

use rtc::peer_connection::state::RTCPeerConnectionState;

// Convert to string
let state = RTCPeerConnectionState::Connected;
assert_eq!(state.to_string(), "connected");

// Parse from string
let parsed: RTCPeerConnectionState = "connecting".into();
assert_eq!(parsed, RTCPeerConnectionState::Connecting);

§Handling Disconnections

use rtc::peer_connection::state::RTCPeerConnectionState;
use rtc::peer_connection::event::RTCPeerConnectionEvent;

match event {
    RTCPeerConnectionEvent::OnConnectionStateChangeEvent(state) => {
        match state {
            RTCPeerConnectionState::Disconnected => {
                println!("Connection lost - ICE will try to reconnect");
                // Optionally: Show reconnecting UI
                // Wait for automatic recovery or timeout
            }
            RTCPeerConnectionState::Failed => {
                println!("Connection failed permanently");
                // Typically: Trigger ICE restart
                // Or: Close and recreate connection
            }
            RTCPeerConnectionState::Connected => {
                println!("Connection restored!");
            }
            _ => {}
        }
    }
    _ => {}
}

§Specifications

Variants§

§

Unspecified

State not specified. This should not occur in normal operation.

§

New

Connection is in initial state.

Any of the ICE or DTLS transports are in the “new” state and none are in “connecting”, “checking”, “failed”, or “disconnected” state, or all transports are in “closed” state, or there are no transports.

§

Connecting

Connection establishment is in progress.

At least one ICE or DTLS transport is in the “connecting” or “checking” state and none are in “failed” state. This indicates that ICE connectivity checks are running and/or DTLS handshake is in progress.

Media cannot flow yet in this state.

§

Connected

Connection is established and media can flow.

All ICE and DTLS transports are in “connected”, “completed”, or “closed” state, and at least one is in “connected” or “completed” state. Both ICE connectivity and DTLS encryption are successfully established.

This is the desired state for active media communication.

§

Disconnected

Connection was established but has been lost.

At least one ICE or DTLS transport is in “disconnected” state and none are in “failed”, “connecting”, or “checking” state. This may be a temporary condition due to network changes.

ICE will attempt to restore connectivity automatically. If it succeeds, the state will return to Connected. If it fails, the state will move to Failed.

§

Failed

Connection has permanently failed.

At least one ICE or DTLS transport is in “failed” state. The connection cannot be established or restored without intervention, typically requiring an ICE restart.

Common causes: NAT/firewall blocking, network unreachable, DTLS handshake failure.

§

Closed

Connection has been closed.

The peer connection’s isClosed member is true. All transports have been shut down. No further communication is possible.

Trait Implementations§

Source§

impl Clone for RTCPeerConnectionState

Source§

fn clone(&self) -> RTCPeerConnectionState

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for RTCPeerConnectionState

Source§

impl Debug for RTCPeerConnectionState

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for RTCPeerConnectionState

Source§

fn default() -> RTCPeerConnectionState

Returns the “default value” for a type. Read more
Source§

impl Display for RTCPeerConnectionState

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for RTCPeerConnectionState

Source§

impl From<&str> for RTCPeerConnectionState

Source§

fn from(raw: &str) -> Self

Converts to this type from the input type.
Source§

impl From<u8> for RTCPeerConnectionState

Source§

fn from(v: u8) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for RTCPeerConnectionState

Source§

fn eq(&self, other: &RTCPeerConnectionState) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for RTCPeerConnectionState

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> ArchivePointee for T

Source§

type ArchivedMetadata = ()

The archived version of the pointer metadata for this type.
Source§

fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata

Converts some archived metadata to the pointer metadata for itself.
Source§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

Source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

Source§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

Source§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

Source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

Source§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

Source§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

Source§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> LayoutRaw for T

Source§

fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>

Returns the layout of the type.
Source§

impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
where T: SharedNiching<N1, N2>, N1: Niching<T>, N2: Niching<T>,

Source§

unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool

Returns whether the given value has been niched. Read more
Source§

fn resolve_niched(out: Place<NichedOption<T, N1>>)

Writes data to out indicating that a T is niched.
Source§

impl<T> Pointee for T

Source§

type Metadata = ()

The metadata type for pointers and references to this type.
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V