MediaStreamTrack

Struct MediaStreamTrack 

Source
pub struct MediaStreamTrack { /* private fields */ }
Expand description

Represents a single media track within a media stream.

A MediaStreamTrack represents an audio or video track from a media source. Each track has a unique identifier, a label (human-readable name), a kind (audio or video), and various state attributes.

The track can be enabled/disabled, muted/unmuted, and has a lifecycle state that progresses from “live” to “ended”. Once ended, a track cannot be restarted.

§Specification

See MediaStreamTrack in the W3C Media Capture and Streams specification.

§Examples

use rtc::media_stream::MediaStreamTrack;
use rtc::rtp_transceiver::rtp_sender::{RTCRtpCodec, RtpCodecKind};
use rtc::rtp_transceiver::rtp_sender::{RTCRtpEncodingParameters, RTCRtpCodingParameters};

let track = MediaStreamTrack::new(
    "stream-id".to_string(),
    "track-id".to_string(),
    "Microphone".to_string(),
    RtpCodecKind::Audio,
    vec![RTCRtpEncodingParameters {
        rtp_coding_parameters: RTCRtpCodingParameters {
            ssrc: Some(12345),
            ..Default::default()
        },
        codec: RTCRtpCodec::default(),
        ..Default::default()
    }],
);

assert_eq!(track.kind(), RtpCodecKind::Audio);
assert_eq!(track.label(), "Microphone");
assert!(track.enabled());
assert!(!track.muted());

Implementations§

Source§

impl MediaStreamTrack

Source

pub fn new( stream_id: MediaStreamId, track_id: MediaStreamTrackId, label: String, kind: RtpCodecKind, codings: Vec<RTCRtpEncodingParameters>, ) -> Self

Creates a new media stream track.

§Parameters
  • stream_id - The ID of the MediaStream this track belongs to
  • track_id - A unique identifier for this track (typically a UUID)
  • label - A human-readable label for the track (e.g., “Built-in Microphone”)
  • kind - The kind of media: Audio or Video
  • codings - Vector of RTP encoding parameters, each containing SSRC, codec, and optional RID. For simulcast, provide multiple entries with different SSRCs and RIDs.
§Returns

A new MediaStreamTrack initialized with the provided parameters. The track starts in the “live” state, enabled, and unmuted.

§Specification

See MediaStreamTrack.

§Examples
use rtc::media_stream::MediaStreamTrack;
use rtc::rtp_transceiver::rtp_sender::{RTCRtpCodec, RtpCodecKind};
use rtc::rtp_transceiver::rtp_sender::{RTCRtpEncodingParameters, RTCRtpCodingParameters};
use rtc::peer_connection::configuration::media_engine::MIME_TYPE_OPUS;

let codec = RTCRtpCodec {
    mime_type: MIME_TYPE_OPUS.to_string(),
    clock_rate: 48000,
    channels: 2,
    sdp_fmtp_line: String::new(),
    rtcp_feedback: vec![],
};

let audio_track = MediaStreamTrack::new(
    "my-stream".to_string(),
    "audio-1".to_string(),
    "Built-in Microphone".to_string(),
    RtpCodecKind::Audio,
    vec![RTCRtpEncodingParameters {
        rtp_coding_parameters: RTCRtpCodingParameters {
            ssrc: Some(123456),
            ..Default::default()
        },
        codec,
        ..Default::default()
    }],
);

assert_eq!(audio_track.track_id(), "audio-1");
assert_eq!(audio_track.label(), "Built-in Microphone");
Source

pub fn stream_id(&self) -> &MediaStreamId

Returns the stream ID this track belongs to.

§Specification

Related to MediaStream.id.

Source

pub fn track_id(&self) -> &MediaStreamTrackId

Returns the unique identifier of this track.

The identifier is a 36-character Universally Unique Identifier (UUID) assigned when the track is created and remains constant throughout the track’s lifetime.

§Specification

See MediaStreamTrack.id.

Source

pub fn label(&self) -> &String

Returns the human-readable label of this track.

The label is a user-agent assigned string that identifies the track source. For example, “Internal Microphone” or “FaceTime HD Camera”.

§Specification

See MediaStreamTrack.label.

§Examples
println!("Track label: {}", track.label());
Source

pub fn kind(&self) -> RtpCodecKind

Returns the kind of media this track represents.

Returns either RtpCodecKind::Audio for audio tracks or RtpCodecKind::Video for video tracks.

§Specification

See MediaStreamTrack.kind.

§Examples
match track.kind() {
    RtpCodecKind::Audio => println!("This is an audio track"),
    RtpCodecKind::Video => println!("This is a video track"),
    _ => {},
}
Source

pub fn rid(&self, ssrc: SSRC) -> Option<&RtpStreamId>

Returns the RTP stream ID (rid) for a given SSRC if this track is part of a simulcast or SVC configuration.

The rid (restriction identifier) is used to identify different encodings of the same media source in simulcast scenarios. This method finds the rid associated with a specific SSRC.

§Parameters
  • ssrc - The Synchronization Source identifier to look up
§Returns

Returns Some(&RtpStreamId) if a rid is found for the given SSRC, or None if:

  • No encoding with the given SSRC exists
  • The encoding has no rid configured
  • This is a non-simulcast track
§Specification

See RFC 8851 - RTP Payload Format Restrictions.

§Examples
use rtc::media_stream::MediaStreamTrack;
use rtc::rtp_transceiver::rtp_sender::{RTCRtpCodec, RtpCodecKind};
use rtc::rtp_transceiver::rtp_sender::{RTCRtpEncodingParameters, RTCRtpCodingParameters};

let track = MediaStreamTrack::new(
    "stream-id".to_string(),
    "track-id".to_string(),
    "Video Track".to_string(),
    RtpCodecKind::Video,
    vec![RTCRtpEncodingParameters {
        rtp_coding_parameters: RTCRtpCodingParameters {
            rid: "q".to_string(), // quarter resolution
            ssrc: Some(12345),
            ..Default::default()
        },
        codec: RTCRtpCodec::default(),
        ..Default::default()
    }],
);

// Get the rid for a specific SSRC
if let Some(rid) = track.rid(12345) {
    println!("RID for SSRC 12345: {}", rid);
}
Source

pub fn codec(&self, ssrc: SSRC) -> Option<&RTCRtpCodec>

Returns the RTP codec configuration for a specific SSRC.

For simulcast tracks with multiple encodings, each encoding may have its own codec configuration. This method retrieves the codec for a specific SSRC.

§Parameters
  • ssrc - The Synchronization Source identifier to look up
§Returns

Returns Some(&RTCRtpCodec) if a codec is found for the given SSRC, or None if:

  • No encoding with the given SSRC exists
  • The encoding has no codec configured (empty MIME type)
§Examples
use rtc::media_stream::MediaStreamTrack;
use rtc::rtp_transceiver::rtp_sender::{RTCRtpCodec, RtpCodecKind};
use rtc::rtp_transceiver::rtp_sender::{RTCRtpEncodingParameters, RTCRtpCodingParameters};
use rtc::peer_connection::configuration::media_engine::MIME_TYPE_VP8;

let codec = RTCRtpCodec {
    mime_type: MIME_TYPE_VP8.to_string(),
    clock_rate: 90000,
    channels: 0,
    sdp_fmtp_line: String::new(),
    rtcp_feedback: vec![],
};

let track = MediaStreamTrack::new(
    "stream-id".to_string(),
    "track-id".to_string(),
    "Video Track".to_string(),
    RtpCodecKind::Video,
    vec![RTCRtpEncodingParameters {
        rtp_coding_parameters: RTCRtpCodingParameters {
            ssrc: Some(12345),
            ..Default::default()
        },
        codec,
        ..Default::default()
    }],
);

// Get the codec for a specific SSRC
if let Some(codec) = track.codec(12345) {
    println!("Codec for SSRC 12345: {}", codec.mime_type);
}
§Specification

See RTCRtpCodec.

Source

pub fn ssrcs(&self) -> impl Iterator<Item = SSRC>

Returns an iterator over the Synchronization Source (SSRC) identifiers for this track.

The SSRC is a 32-bit identifier used in RTP to distinguish different media sources within a session. For simulcast tracks, this returns multiple SSRCs.

§Returns

An iterator that yields SSRC values (u32).

§Specification

See RFC 3550 - RTP: A Transport Protocol for Real-Time Applications.

§Examples
use rtc::media_stream::MediaStreamTrack;
use rtc::rtp_transceiver::rtp_sender::{RTCRtpCodec, RtpCodecKind};
use rtc::rtp_transceiver::rtp_sender::{RTCRtpEncodingParameters, RTCRtpCodingParameters};

let track = MediaStreamTrack::new(
    "stream-id".to_string(),
    "track-id".to_string(),
    "Video Track".to_string(),
    RtpCodecKind::Video,
    vec![RTCRtpEncodingParameters {
        rtp_coding_parameters: RTCRtpCodingParameters {
            ssrc: Some(12345),
            ..Default::default()
        },
        codec: RTCRtpCodec::default(),
        ..Default::default()
    }],
);

// Get all SSRCs
let ssrcs: Vec<u32> = track.ssrcs().collect();
println!("Track SSRCs: {:?}", ssrcs);
Source

pub fn enabled(&self) -> bool

Returns whether this track is enabled.

When a track is disabled, it produces silence (for audio) or black frames (for video), but remains live and connected. This is useful for temporarily stopping media flow without tearing down the connection.

§Specification

See MediaStreamTrack.enabled.

§Examples
if track.enabled() {
    println!("Track is producing media");
} else {
    println!("Track is disabled");
}
Source

pub fn set_enabled(&mut self, enabled: bool)

Sets whether this track is enabled.

Disabling a track causes it to produce silence (audio) or black frames (video). This is reversible - enabling the track again resumes normal media flow.

§Parameters
  • enabled - true to enable the track, false to disable it
§Specification

See MediaStreamTrack.enabled.

§Examples
// Temporarily disable the track
track.set_enabled(false);

// Later, re-enable it
track.set_enabled(true);
Source

pub fn muted(&self) -> bool

Returns whether this track is muted.

A muted track is unable to provide media data due to technical limitations, such as hardware issues or permission denials. Unlike enabled, which is under application control, muting is controlled by the user agent or system.

§Specification

See MediaStreamTrack.muted.

§Examples
if track.muted() {
    println!("Track is muted by the system");
}
Source

pub fn stop(&mut self)

Permanently stops this track.

Stopping a track ends its media source and transitions the track to the “ended” state. Once stopped, a track cannot be restarted. The track will no longer produce media, and resources associated with it are released.

§Specification

See MediaStreamTrack.stop().

§Examples
// Stop the track when done
track.stop();

// Track is now permanently ended
Source

pub fn get_capabilities(&self) -> &MediaTrackCapabilities

Returns the capabilities of this track.

Capabilities represent the inherent properties of the track’s source, such as supported resolutions, frame rates, sample rates, etc. These are determined by the underlying hardware or media source.

§Specification

See MediaStreamTrack.getCapabilities().

§Examples
let capabilities = track.get_capabilities();
// Inspect supported capabilities
Source

pub fn get_constraints(&self) -> &MediaTrackConstraints

Returns the currently applied constraints for this track.

Constraints define the allowed ranges or exact values for various track properties. They are applied using apply_constraints().

§Specification

See MediaStreamTrack.getConstraints().

§Examples
let constraints = track.get_constraints();
// Examine current constraints
Source

pub fn get_settings(&self) -> &MediaTrackSettings

Returns the actual settings currently in effect for this track.

Settings represent the current values of track properties like resolution, frame rate, sample rate, etc. These may differ from requested constraints based on hardware limitations or system conditions.

§Specification

See MediaStreamTrack.getSettings().

§Examples
let settings = track.get_settings();
// Check actual track settings
Source

pub fn apply_constraints(&mut self, constraints: Option<MediaTrackConstraints>)

Applies constraints to this track.

Constraints allow the application to specify desired values or ranges for track properties like resolution, frame rate, etc. The user agent will attempt to satisfy these constraints, but may not be able to meet all of them depending on hardware capabilities and system conditions.

If the track is in the “ended” state, this method has no effect.

§Parameters
  • constraints - Optional constraints to apply. Pass None to remove all constraints.
§Specification

See MediaStreamTrack.applyConstraints().

§Examples
// Apply new constraints
let constraints = MediaTrackConstraints::default();
track.apply_constraints(Some(constraints));

// Remove all constraints
track.apply_constraints(None);

Trait Implementations§

Source§

impl Clone for MediaStreamTrack

Source§

fn clone(&self) -> MediaStreamTrack

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for MediaStreamTrack

Source§

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

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

impl Default for MediaStreamTrack

Source§

fn default() -> MediaStreamTrack

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

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<'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<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<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<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> 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, 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

Source§

impl<T> ErasedDestructor for T
where T: 'static,