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 → ConnectedIf 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
impl Clone for RTCPeerConnectionState
Source§fn clone(&self) -> RTCPeerConnectionState
fn clone(&self) -> RTCPeerConnectionState
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreimpl Copy for RTCPeerConnectionState
Source§impl Debug for RTCPeerConnectionState
impl Debug for RTCPeerConnectionState
Source§impl Default for RTCPeerConnectionState
impl Default for RTCPeerConnectionState
Source§fn default() -> RTCPeerConnectionState
fn default() -> RTCPeerConnectionState
Source§impl Display for RTCPeerConnectionState
impl Display for RTCPeerConnectionState
impl Eq for RTCPeerConnectionState
Source§impl From<&str> for RTCPeerConnectionState
impl From<&str> for RTCPeerConnectionState
Source§impl From<u8> for RTCPeerConnectionState
impl From<u8> for RTCPeerConnectionState
Source§impl PartialEq for RTCPeerConnectionState
impl PartialEq for RTCPeerConnectionState
impl StructuralPartialEq for RTCPeerConnectionState
Auto Trait Implementations§
impl Freeze for RTCPeerConnectionState
impl RefUnwindSafe for RTCPeerConnectionState
impl Send for RTCPeerConnectionState
impl Sync for RTCPeerConnectionState
impl Unpin for RTCPeerConnectionState
impl UnsafeUnpin for RTCPeerConnectionState
impl UnwindSafe for RTCPeerConnectionState
Blanket Implementations§
Source§impl<T> ArchivePointee for T
impl<T> ArchivePointee for T
Source§type ArchivedMetadata = ()
type ArchivedMetadata = ()
Source§fn pointer_metadata(
_: &<T as ArchivePointee>::ArchivedMetadata,
) -> <T as Pointee>::Metadata
fn pointer_metadata( _: &<T as ArchivePointee>::ArchivedMetadata, ) -> <T as Pointee>::Metadata
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<T> LayoutRaw for T
impl<T> LayoutRaw for T
Source§fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
fn layout_raw(_: <T as Pointee>::Metadata) -> Result<Layout, LayoutError>
Source§impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
impl<T, N1, N2> Niching<NichedOption<T, N1>> for N2
Source§unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
unsafe fn is_niched(niched: *const NichedOption<T, N1>) -> bool
Source§fn resolve_niched(out: Place<NichedOption<T, N1>>)
fn resolve_niched(out: Place<NichedOption<T, N1>>)
out indicating that a T is niched.