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
impl MediaStreamTrack
Sourcepub fn new(
stream_id: MediaStreamId,
track_id: MediaStreamTrackId,
label: String,
kind: RtpCodecKind,
codings: Vec<RTCRtpEncodingParameters>,
) -> Self
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 theMediaStreamthis track belongs totrack_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:AudioorVideocodings- 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");Sourcepub fn stream_id(&self) -> &MediaStreamId
pub fn stream_id(&self) -> &MediaStreamId
Sourcepub fn track_id(&self) -> &MediaStreamTrackId
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.
Sourcepub fn label(&self) -> &String
pub fn label(&self) -> &String
Sourcepub fn kind(&self) -> RtpCodecKind
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
§Examples
match track.kind() {
RtpCodecKind::Audio => println!("This is an audio track"),
RtpCodecKind::Video => println!("This is a video track"),
_ => {},
}Sourcepub fn rid(&self, ssrc: SSRC) -> Option<&RtpStreamId>
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);
}Sourcepub fn codec(&self, ssrc: SSRC) -> Option<&RTCRtpCodec>
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.
Sourcepub fn ssrcs(&self) -> impl Iterator<Item = SSRC>
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);Sourcepub fn enabled(&self) -> bool
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
§Examples
if track.enabled() {
println!("Track is producing media");
} else {
println!("Track is disabled");
}Sourcepub fn set_enabled(&mut self, enabled: bool)
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-trueto enable the track,falseto disable it
§Specification
§Examples
// Temporarily disable the track
track.set_enabled(false);
// Later, re-enable it
track.set_enabled(true);Sourcepub fn muted(&self) -> bool
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
§Examples
if track.muted() {
println!("Track is muted by the system");
}Sourcepub fn stop(&mut self)
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
§Examples
// Stop the track when done
track.stop();
// Track is now permanently endedSourcepub fn get_capabilities(&self) -> &MediaTrackCapabilities
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 capabilitiesSourcepub fn get_constraints(&self) -> &MediaTrackConstraints
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 constraintsSourcepub fn get_settings(&self) -> &MediaTrackSettings
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 settingsSourcepub fn apply_constraints(&mut self, constraints: Option<MediaTrackConstraints>)
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. PassNoneto 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
impl Clone for MediaStreamTrack
Source§fn clone(&self) -> MediaStreamTrack
fn clone(&self) -> MediaStreamTrack
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more