Skip to main content

rtc/media_stream/
track_state.rs

1//! MediaStreamTrack State
2//!
3//! This module defines the lifecycle state of a [`MediaStreamTrack`](super::MediaStreamTrack).
4//!
5//! # Specification
6//!
7//! See [MediaStreamTrack.readyState](https://www.w3.org/TR/mediacapture-streams/#dom-mediastreamtrack-readystate).
8
9use crate::peer_connection::configuration::UNSPECIFIED_STR;
10use std::fmt;
11
12/// Represents the lifecycle state of a media stream track.
13///
14/// A track progresses through these states during its lifetime. Once a track
15/// reaches the "ended" state, it cannot return to "live" state.
16///
17/// # Specification
18///
19/// See [MediaStreamTrackState](https://www.w3.org/TR/mediacapture-streams/#idl-def-mediastreamtrackstate)
20/// in the W3C Media Capture and Streams specification.
21///
22/// # Examples
23///
24/// ```
25/// use rtc::media_stream::MediaStreamTrackState;
26///
27/// let state = MediaStreamTrackState::Live;
28/// assert_eq!(state.to_string(), "live");
29///
30/// let state = MediaStreamTrackState::Ended;
31/// assert_eq!(state.to_string(), "ended");
32/// ```
33#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
34pub enum MediaStreamTrackState {
35    /// Unspecified or unknown state.
36    Unspecified,
37
38    /// The track is active and providing media data.
39    ///
40    /// A live track can produce media frames or samples. This is the initial
41    /// state of a track when it's created.
42    ///
43    /// # Specification
44    ///
45    /// See [live state](https://www.w3.org/TR/mediacapture-streams/#idl-def-mediastreamtrackstate-live).
46    #[default]
47    Live,
48
49    /// The track has permanently ended.
50    ///
51    /// An ended track no longer provides media data and cannot be restarted.
52    /// This state is reached when:
53    /// - [`stop()`](super::track::MediaStreamTrack::stop) is called
54    /// - The media source is disconnected or removed
55    /// - The user agent decides to end the track
56    ///
57    /// # Specification
58    ///
59    /// See [ended state](https://www.w3.org/TR/mediacapture-streams/#idl-def-mediastreamtrackstate-ended).
60    Ended,
61}
62
63const MEDIA_STREAM_TRACK_STATE_LIVE_STR: &str = "live";
64const MEDIA_STREAM_TRACK_STATE_ENDED_STR: &str = "ended";
65
66impl From<&str> for MediaStreamTrackState {
67    fn from(raw: &str) -> Self {
68        match raw {
69            MEDIA_STREAM_TRACK_STATE_LIVE_STR => MediaStreamTrackState::Live,
70            MEDIA_STREAM_TRACK_STATE_ENDED_STR => MediaStreamTrackState::Ended,
71            _ => MediaStreamTrackState::Unspecified,
72        }
73    }
74}
75
76impl fmt::Display for MediaStreamTrackState {
77    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
78        let s = match *self {
79            MediaStreamTrackState::Live => MEDIA_STREAM_TRACK_STATE_LIVE_STR,
80            MediaStreamTrackState::Ended => MEDIA_STREAM_TRACK_STATE_ENDED_STR,
81            MediaStreamTrackState::Unspecified => UNSPECIFIED_STR,
82        };
83        write!(f, "{s}")
84    }
85}