rtc/media_stream/track.rs
1//! MediaStreamTrack API
2//!
3//! This module implements the `MediaStreamTrack` interface as defined in the
4//! [W3C Media Capture and Streams specification](https://www.w3.org/TR/mediacapture-streams/#mediastreamtrack).
5//!
6//! A [`MediaStreamTrack`] represents a single media track within a media stream, such as
7//! an audio track from a microphone or a video track from a camera. Each track has properties
8//! like enabled/disabled state, mute status, and lifecycle state (live or ended).
9//!
10//! # Examples
11//!
12//! ## Creating a video track
13//!
14//! ```
15//! use rtc::media_stream::MediaStreamTrack;
16//! use rtc::rtp_transceiver::rtp_sender::{RTCRtpCodec, RtpCodecKind};
17//! use rtc::rtp_transceiver::rtp_sender::{RTCRtpEncodingParameters, RTCRtpCodingParameters};
18//! use rtc::peer_connection::configuration::media_engine::MIME_TYPE_VP8;
19//!
20//! # fn example() -> Result<(), Box<dyn std::error::Error>> {
21//! let codec = RTCRtpCodec {
22//! mime_type: MIME_TYPE_VP8.to_string(),
23//! clock_rate: 90000,
24//! channels: 0,
25//! sdp_fmtp_line: String::new(),
26//! rtcp_feedback: vec![],
27//! };
28//!
29//! let video_track = MediaStreamTrack::new(
30//! "stream-123".to_string(),
31//! "track-456".to_string(),
32//! "Front Camera".to_string(),
33//! RtpCodecKind::Video,
34//! vec![RTCRtpEncodingParameters {
35//! rtp_coding_parameters: RTCRtpCodingParameters {
36//! ssrc: Some(789012),
37//! ..Default::default()
38//! },
39//! codec,
40//! ..Default::default()
41//! }],
42//! );
43//!
44//! assert_eq!(video_track.kind(), RtpCodecKind::Video);
45//! assert_eq!(video_track.label(), "Front Camera");
46//! assert!(video_track.enabled());
47//! # Ok(())
48//! # }
49//! ```
50//!
51//! ## Controlling track state
52//!
53//! ```
54//! # use rtc::media_stream::MediaStreamTrack;
55//! # use rtc::rtp_transceiver::rtp_sender::{RTCRtpCodec, RtpCodecKind};
56//! # fn example(mut track: MediaStreamTrack) {
57//! // Disable the track temporarily
58//! track.set_enabled(false);
59//! assert!(!track.enabled());
60//!
61//! // Re-enable the track
62//! track.set_enabled(true);
63//! assert!(track.enabled());
64//!
65//! // Stop the track permanently
66//! track.stop();
67//! # }
68//! ```
69//!
70//! # Specifications
71//!
72//! - [W3C MediaStreamTrack](https://www.w3.org/TR/mediacapture-streams/#mediastreamtrack)
73//! - [MDN MediaStreamTrack](https://developer.mozilla.org/en-US/docs/Web/API/MediaStreamTrack)
74
75use crate::media_stream::MediaStreamId;
76use crate::media_stream::track_capabilities::MediaTrackCapabilities;
77use crate::media_stream::track_constraints::MediaTrackConstraints;
78use crate::media_stream::track_settings::MediaTrackSettings;
79use crate::media_stream::track_state::MediaStreamTrackState;
80use crate::rtp_transceiver::rtp_sender::{
81 RTCRtpCodec, RTCRtpCodingParameters, RTCRtpEncodingParameters, RtpCodecKind,
82};
83use crate::rtp_transceiver::{RtpStreamId, SSRC};
84
85/// Unique identifier for a media stream track.
86///
87/// As defined in [MediaStreamTrack.id](https://www.w3.org/TR/mediacapture-streams/#dom-mediastreamtrack-id).
88pub type MediaStreamTrackId = String;
89
90/// Represents a single media track within a media stream.
91///
92/// A `MediaStreamTrack` represents an audio or video track from a media source.
93/// Each track has a unique identifier, a label (human-readable name), a kind
94/// (audio or video), and various state attributes.
95///
96/// The track can be enabled/disabled, muted/unmuted, and has a lifecycle state
97/// that progresses from "live" to "ended". Once ended, a track cannot be restarted.
98///
99/// # Specification
100///
101/// See [MediaStreamTrack](https://www.w3.org/TR/mediacapture-streams/#mediastreamtrack)
102/// in the W3C Media Capture and Streams specification.
103///
104/// # Examples
105///
106/// ```
107/// use rtc::media_stream::MediaStreamTrack;
108/// use rtc::rtp_transceiver::rtp_sender::{RTCRtpCodec, RtpCodecKind};
109/// use rtc::rtp_transceiver::rtp_sender::{RTCRtpEncodingParameters, RTCRtpCodingParameters};
110///
111/// # fn example() -> Result<(), Box<dyn std::error::Error>> {
112/// let track = MediaStreamTrack::new(
113/// "stream-id".to_string(),
114/// "track-id".to_string(),
115/// "Microphone".to_string(),
116/// RtpCodecKind::Audio,
117/// vec![RTCRtpEncodingParameters {
118/// rtp_coding_parameters: RTCRtpCodingParameters {
119/// ssrc: Some(12345),
120/// ..Default::default()
121/// },
122/// codec: RTCRtpCodec::default(),
123/// ..Default::default()
124/// }],
125/// );
126///
127/// assert_eq!(track.kind(), RtpCodecKind::Audio);
128/// assert_eq!(track.label(), "Microphone");
129/// assert!(track.enabled());
130/// assert!(!track.muted());
131/// # Ok(())
132/// # }
133/// ```
134#[derive(Default, Debug, Clone)]
135pub struct MediaStreamTrack {
136 stream_id: MediaStreamId,
137 track_id: MediaStreamTrackId,
138 label: String,
139 kind: RtpCodecKind,
140 codings: Vec<RTCRtpEncodingParameters>,
141
142 muted: bool,
143 enabled: bool,
144 ready_state: MediaStreamTrackState,
145 restrictable: bool,
146 capabilities: MediaTrackCapabilities,
147 constraints: MediaTrackConstraints,
148 settings: MediaTrackSettings,
149}
150
151impl MediaStreamTrack {
152 /// Creates a new media stream track.
153 ///
154 /// # Parameters
155 ///
156 /// * `stream_id` - The ID of the [`MediaStream`](crate::media_stream::MediaStream) this track belongs to
157 /// * `track_id` - A unique identifier for this track (typically a UUID)
158 /// * `label` - A human-readable label for the track (e.g., "Built-in Microphone")
159 /// * `kind` - The kind of media: [`Audio`](RtpCodecKind::Audio) or [`Video`](RtpCodecKind::Video)
160 /// * `codings` - Vector of RTP encoding parameters, each containing SSRC, codec, and optional RID.
161 /// For simulcast, provide multiple entries with different SSRCs and RIDs.
162 ///
163 /// # Returns
164 ///
165 /// A new `MediaStreamTrack` initialized with the provided parameters. The track
166 /// starts in the "live" state, enabled, and unmuted.
167 ///
168 /// # Specification
169 ///
170 /// See [MediaStreamTrack](https://www.w3.org/TR/mediacapture-streams/#mediastreamtrack).
171 ///
172 /// # Examples
173 ///
174 /// ```
175 /// use rtc::media_stream::MediaStreamTrack;
176 /// use rtc::rtp_transceiver::rtp_sender::{RTCRtpCodec, RtpCodecKind};
177 /// use rtc::rtp_transceiver::rtp_sender::{RTCRtpEncodingParameters, RTCRtpCodingParameters};
178 /// use rtc::peer_connection::configuration::media_engine::MIME_TYPE_OPUS;
179 ///
180 /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
181 /// let codec = RTCRtpCodec {
182 /// mime_type: MIME_TYPE_OPUS.to_string(),
183 /// clock_rate: 48000,
184 /// channels: 2,
185 /// sdp_fmtp_line: String::new(),
186 /// rtcp_feedback: vec![],
187 /// };
188 ///
189 /// let audio_track = MediaStreamTrack::new(
190 /// "my-stream".to_string(),
191 /// "audio-1".to_string(),
192 /// "Built-in Microphone".to_string(),
193 /// RtpCodecKind::Audio,
194 /// vec![RTCRtpEncodingParameters {
195 /// rtp_coding_parameters: RTCRtpCodingParameters {
196 /// ssrc: Some(123456),
197 /// ..Default::default()
198 /// },
199 /// codec,
200 /// ..Default::default()
201 /// }],
202 /// );
203 ///
204 /// assert_eq!(audio_track.track_id(), "audio-1");
205 /// assert_eq!(audio_track.label(), "Built-in Microphone");
206 /// # Ok(())
207 /// # }
208 /// ```
209 pub fn new(
210 stream_id: MediaStreamId,
211 track_id: MediaStreamTrackId,
212 label: String,
213 kind: RtpCodecKind,
214 codings: Vec<RTCRtpEncodingParameters>,
215 ) -> Self {
216 Self {
217 stream_id,
218 track_id,
219 label,
220 kind,
221 codings,
222
223 muted: false,
224 enabled: true,
225 ready_state: MediaStreamTrackState::Live,
226 restrictable: false,
227
228 ..Default::default()
229 }
230 }
231
232 /// Returns the stream ID this track belongs to.
233 ///
234 /// # Specification
235 ///
236 /// Related to [MediaStream.id](https://www.w3.org/TR/mediacapture-streams/#dom-mediastream-id).
237 pub fn stream_id(&self) -> &MediaStreamId {
238 &self.stream_id
239 }
240
241 /// Returns the unique identifier of this track.
242 ///
243 /// The identifier is a 36-character Universally Unique Identifier (UUID)
244 /// assigned when the track is created and remains constant throughout
245 /// the track's lifetime.
246 ///
247 /// # Specification
248 ///
249 /// See [MediaStreamTrack.id](https://www.w3.org/TR/mediacapture-streams/#dom-mediastreamtrack-id).
250 pub fn track_id(&self) -> &MediaStreamTrackId {
251 &self.track_id
252 }
253
254 /// Returns the human-readable label of this track.
255 ///
256 /// The label is a user-agent assigned string that identifies the track source.
257 /// For example, "Internal Microphone" or "FaceTime HD Camera".
258 ///
259 /// # Specification
260 ///
261 /// See [MediaStreamTrack.label](https://www.w3.org/TR/mediacapture-streams/#dom-mediastreamtrack-label).
262 ///
263 /// # Examples
264 ///
265 /// ```
266 /// # use rtc::media_stream::MediaStreamTrack;
267 /// # use rtc::rtp_transceiver::rtp_sender::{RTCRtpCodec, RtpCodecKind};
268 /// # fn example(track: MediaStreamTrack) {
269 /// println!("Track label: {}", track.label());
270 /// # }
271 /// ```
272 pub fn label(&self) -> &String {
273 &self.label
274 }
275
276 /// Returns the kind of media this track represents.
277 ///
278 /// Returns either [`RtpCodecKind::Audio`] for audio tracks or
279 /// [`RtpCodecKind::Video`] for video tracks.
280 ///
281 /// # Specification
282 ///
283 /// See [MediaStreamTrack.kind](https://www.w3.org/TR/mediacapture-streams/#dom-mediastreamtrack-kind).
284 ///
285 /// # Examples
286 ///
287 /// ```
288 /// # use rtc::media_stream::MediaStreamTrack;
289 /// # use rtc::rtp_transceiver::rtp_sender::{RTCRtpCodec, RtpCodecKind};
290 /// # fn example(track: MediaStreamTrack) {
291 /// match track.kind() {
292 /// RtpCodecKind::Audio => println!("This is an audio track"),
293 /// RtpCodecKind::Video => println!("This is a video track"),
294 /// _ => {},
295 /// }
296 /// # }
297 /// ```
298 pub fn kind(&self) -> RtpCodecKind {
299 self.kind
300 }
301
302 /// Returns the RTP stream ID (rid) for a given SSRC if this track is part of a simulcast or SVC configuration.
303 ///
304 /// The rid (restriction identifier) is used to identify different encodings of the
305 /// same media source in simulcast scenarios. This method finds the rid associated with
306 /// a specific SSRC.
307 ///
308 /// # Parameters
309 ///
310 /// * `ssrc` - The Synchronization Source identifier to look up
311 ///
312 /// # Returns
313 ///
314 /// Returns `Some(&RtpStreamId)` if a rid is found for the given SSRC, or `None` if:
315 /// - No encoding with the given SSRC exists
316 /// - The encoding has no rid configured
317 /// - This is a non-simulcast track
318 ///
319 /// # Specification
320 ///
321 /// See [RFC 8851 - RTP Payload Format Restrictions](https://www.rfc-editor.org/rfc/rfc8851.html).
322 ///
323 /// # Examples
324 ///
325 /// ```
326 /// use rtc::media_stream::MediaStreamTrack;
327 /// use rtc::rtp_transceiver::rtp_sender::{RTCRtpCodec, RtpCodecKind};
328 /// use rtc::rtp_transceiver::rtp_sender::{RTCRtpEncodingParameters, RTCRtpCodingParameters};
329 ///
330 /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
331 /// let track = MediaStreamTrack::new(
332 /// "stream-id".to_string(),
333 /// "track-id".to_string(),
334 /// "Video Track".to_string(),
335 /// RtpCodecKind::Video,
336 /// vec![RTCRtpEncodingParameters {
337 /// rtp_coding_parameters: RTCRtpCodingParameters {
338 /// rid: "q".to_string(), // quarter resolution
339 /// ssrc: Some(12345),
340 /// ..Default::default()
341 /// },
342 /// codec: RTCRtpCodec::default(),
343 /// ..Default::default()
344 /// }],
345 /// );
346 ///
347 /// // Get the rid for a specific SSRC
348 /// if let Some(rid) = track.rid(12345) {
349 /// println!("RID for SSRC 12345: {}", rid);
350 /// }
351 /// # Ok(())
352 /// # }
353 /// ```
354 pub fn rid(&self, ssrc: SSRC) -> Option<&RtpStreamId> {
355 self.codings
356 .iter()
357 .find(|coding| {
358 !coding.rtp_coding_parameters.rid.is_empty()
359 && coding
360 .rtp_coding_parameters
361 .ssrc
362 .is_some_and(|t_ssrc| t_ssrc == ssrc)
363 })
364 .map(|coding| &coding.rtp_coding_parameters.rid)
365 }
366
367 /// Returns the RTP codec configuration for a specific SSRC.
368 ///
369 /// For simulcast tracks with multiple encodings, each encoding may have its own
370 /// codec configuration. This method retrieves the codec for a specific SSRC.
371 ///
372 /// # Parameters
373 ///
374 /// * `ssrc` - The Synchronization Source identifier to look up
375 ///
376 /// # Returns
377 ///
378 /// Returns `Some(&RTCRtpCodec)` if a codec is found for the given SSRC, or `None` if:
379 /// - No encoding with the given SSRC exists
380 /// - The encoding has no codec configured (empty MIME type)
381 ///
382 /// # Examples
383 ///
384 /// ```
385 /// use rtc::media_stream::MediaStreamTrack;
386 /// use rtc::rtp_transceiver::rtp_sender::{RTCRtpCodec, RtpCodecKind};
387 /// use rtc::rtp_transceiver::rtp_sender::{RTCRtpEncodingParameters, RTCRtpCodingParameters};
388 /// use rtc::peer_connection::configuration::media_engine::MIME_TYPE_VP8;
389 ///
390 /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
391 /// let codec = RTCRtpCodec {
392 /// mime_type: MIME_TYPE_VP8.to_string(),
393 /// clock_rate: 90000,
394 /// channels: 0,
395 /// sdp_fmtp_line: String::new(),
396 /// rtcp_feedback: vec![],
397 /// };
398 ///
399 /// let track = MediaStreamTrack::new(
400 /// "stream-id".to_string(),
401 /// "track-id".to_string(),
402 /// "Video Track".to_string(),
403 /// RtpCodecKind::Video,
404 /// vec![RTCRtpEncodingParameters {
405 /// rtp_coding_parameters: RTCRtpCodingParameters {
406 /// ssrc: Some(12345),
407 /// ..Default::default()
408 /// },
409 /// codec,
410 /// ..Default::default()
411 /// }],
412 /// );
413 ///
414 /// // Get the codec for a specific SSRC
415 /// if let Some(codec) = track.codec(12345) {
416 /// println!("Codec for SSRC 12345: {}", codec.mime_type);
417 /// }
418 /// # Ok(())
419 /// # }
420 /// ```
421 ///
422 /// # Specification
423 ///
424 /// See [RTCRtpCodec](https://www.w3.org/TR/webrtc/#dom-rtcrtpcodecparameters).
425 pub fn codec(&self, ssrc: SSRC) -> Option<&RTCRtpCodec> {
426 self.codings
427 .iter()
428 .find(|coding| {
429 !coding.codec.mime_type.is_empty()
430 && coding
431 .rtp_coding_parameters
432 .ssrc
433 .is_some_and(|track_ssrc| track_ssrc == ssrc)
434 })
435 .map(|coding| &coding.codec)
436 }
437
438 /// Returns an iterator over the Synchronization Source (SSRC) identifiers for this track.
439 ///
440 /// The SSRC is a 32-bit identifier used in RTP to distinguish different
441 /// media sources within a session. For simulcast tracks, this returns multiple SSRCs.
442 ///
443 /// # Returns
444 ///
445 /// An iterator that yields SSRC values (u32).
446 ///
447 /// # Specification
448 ///
449 /// See [RFC 3550 - RTP: A Transport Protocol for Real-Time Applications](https://www.rfc-editor.org/rfc/rfc3550.html#section-5.1).
450 ///
451 /// # Examples
452 ///
453 /// ```
454 /// use rtc::media_stream::MediaStreamTrack;
455 /// use rtc::rtp_transceiver::rtp_sender::{RTCRtpCodec, RtpCodecKind};
456 /// use rtc::rtp_transceiver::rtp_sender::{RTCRtpEncodingParameters, RTCRtpCodingParameters};
457 ///
458 /// # fn example() -> Result<(), Box<dyn std::error::Error>> {
459 /// let track = MediaStreamTrack::new(
460 /// "stream-id".to_string(),
461 /// "track-id".to_string(),
462 /// "Video Track".to_string(),
463 /// RtpCodecKind::Video,
464 /// vec![RTCRtpEncodingParameters {
465 /// rtp_coding_parameters: RTCRtpCodingParameters {
466 /// ssrc: Some(12345),
467 /// ..Default::default()
468 /// },
469 /// codec: RTCRtpCodec::default(),
470 /// ..Default::default()
471 /// }],
472 /// );
473 ///
474 /// // Get all SSRCs
475 /// let ssrcs: Vec<u32> = track.ssrcs().collect();
476 /// println!("Track SSRCs: {:?}", ssrcs);
477 /// # Ok(())
478 /// # }
479 /// ```
480 pub fn ssrcs(&self) -> impl Iterator<Item = SSRC> {
481 self.codings
482 .iter()
483 .filter(|coding| coding.rtp_coding_parameters.ssrc.is_some())
484 .map(|coding| coding.rtp_coding_parameters.ssrc.unwrap_or_default())
485 }
486
487 /// Returns whether this track is enabled.
488 ///
489 /// When a track is disabled, it produces silence (for audio) or black frames (for video),
490 /// but remains live and connected. This is useful for temporarily stopping media flow
491 /// without tearing down the connection.
492 ///
493 /// # Specification
494 ///
495 /// See [MediaStreamTrack.enabled](https://www.w3.org/TR/mediacapture-streams/#dom-mediastreamtrack-enabled).
496 ///
497 /// # Examples
498 ///
499 /// ```
500 /// # use rtc::media_stream::MediaStreamTrack;
501 /// # use rtc::rtp_transceiver::rtp_sender::{RTCRtpCodec, RtpCodecKind};
502 /// # fn example(track: MediaStreamTrack) {
503 /// if track.enabled() {
504 /// println!("Track is producing media");
505 /// } else {
506 /// println!("Track is disabled");
507 /// }
508 /// # }
509 /// ```
510 pub fn enabled(&self) -> bool {
511 self.enabled
512 }
513
514 /// Sets whether this track is enabled.
515 ///
516 /// Disabling a track causes it to produce silence (audio) or black frames (video).
517 /// This is reversible - enabling the track again resumes normal media flow.
518 ///
519 /// # Parameters
520 ///
521 /// * `enabled` - `true` to enable the track, `false` to disable it
522 ///
523 /// # Specification
524 ///
525 /// See [MediaStreamTrack.enabled](https://www.w3.org/TR/mediacapture-streams/#dom-mediastreamtrack-enabled).
526 ///
527 /// # Examples
528 ///
529 /// ```
530 /// # use rtc::media_stream::MediaStreamTrack;
531 /// # use rtc::rtp_transceiver::rtp_sender::{RTCRtpCodec, RtpCodecKind};
532 /// # fn example(mut track: MediaStreamTrack) {
533 /// // Temporarily disable the track
534 /// track.set_enabled(false);
535 ///
536 /// // Later, re-enable it
537 /// track.set_enabled(true);
538 /// # }
539 /// ```
540 pub fn set_enabled(&mut self, enabled: bool) {
541 self.enabled = enabled;
542 }
543
544 /// Returns whether this track is muted.
545 ///
546 /// A muted track is unable to provide media data due to technical limitations,
547 /// such as hardware issues or permission denials. Unlike [`enabled`](Self::enabled),
548 /// which is under application control, muting is controlled by the user agent or system.
549 ///
550 /// # Specification
551 ///
552 /// See [MediaStreamTrack.muted](https://www.w3.org/TR/mediacapture-streams/#dom-mediastreamtrack-muted).
553 ///
554 /// # Examples
555 ///
556 /// ```
557 /// # use rtc::media_stream::MediaStreamTrack;
558 /// # use rtc::rtp_transceiver::rtp_sender::{RTCRtpCodec, RtpCodecKind};
559 /// # fn example(track: MediaStreamTrack) {
560 /// if track.muted() {
561 /// println!("Track is muted by the system");
562 /// }
563 /// # }
564 /// ```
565 pub fn muted(&self) -> bool {
566 self.muted
567 }
568
569 pub fn set_muted(&mut self, muted: bool) {
570 self.muted = muted;
571 }
572
573 /// Returns MediaStreamTrackState
574 pub fn ready_state(&self) -> MediaStreamTrackState {
575 self.ready_state
576 }
577
578 /// Permanently stops this track.
579 ///
580 /// Stopping a track ends its media source and transitions the track to the "ended" state.
581 /// Once stopped, a track cannot be restarted. The track will no longer produce media,
582 /// and resources associated with it are released.
583 ///
584 /// # Specification
585 ///
586 /// See [MediaStreamTrack.stop()](https://www.w3.org/TR/mediacapture-streams/#dom-mediastreamtrack-stop).
587 ///
588 /// # Examples
589 ///
590 /// ```
591 /// # use rtc::media_stream::MediaStreamTrack;
592 /// # use rtc::rtp_transceiver::rtp_sender::{RTCRtpCodec, RtpCodecKind};
593 /// # fn example(mut track: MediaStreamTrack) {
594 /// // Stop the track when done
595 /// track.stop();
596 ///
597 /// // Track is now permanently ended
598 /// # }
599 /// ```
600 pub fn stop(&mut self) {
601 self.ready_state = MediaStreamTrackState::Ended;
602 }
603
604 /// Returns the capabilities of this track.
605 ///
606 /// Capabilities represent the inherent properties of the track's source, such as
607 /// supported resolutions, frame rates, sample rates, etc. These are determined by
608 /// the underlying hardware or media source.
609 ///
610 /// # Specification
611 ///
612 /// See [MediaStreamTrack.getCapabilities()](https://www.w3.org/TR/mediacapture-streams/#dom-mediastreamtrack-getcapabilities).
613 ///
614 /// # Examples
615 ///
616 /// ```
617 /// # use rtc::media_stream::MediaStreamTrack;
618 /// # use rtc::rtp_transceiver::rtp_sender::{RTCRtpCodec, RtpCodecKind};
619 /// # fn example(track: MediaStreamTrack) {
620 /// let capabilities = track.get_capabilities();
621 /// // Inspect supported capabilities
622 /// # }
623 /// ```
624 pub fn get_capabilities(&self) -> &MediaTrackCapabilities {
625 &self.capabilities
626 }
627
628 /// Returns the currently applied constraints for this track.
629 ///
630 /// Constraints define the allowed ranges or exact values for various track properties.
631 /// They are applied using [`apply_constraints()`](Self::apply_constraints).
632 ///
633 /// # Specification
634 ///
635 /// See [MediaStreamTrack.getConstraints()](https://www.w3.org/TR/mediacapture-streams/#dom-mediastreamtrack-getconstraints).
636 ///
637 /// # Examples
638 ///
639 /// ```
640 /// # use rtc::media_stream::MediaStreamTrack;
641 /// # use rtc::rtp_transceiver::rtp_sender::{RTCRtpCodec, RtpCodecKind};
642 /// # fn example(track: MediaStreamTrack) {
643 /// let constraints = track.get_constraints();
644 /// // Examine current constraints
645 /// # }
646 /// ```
647 pub fn get_constraints(&self) -> &MediaTrackConstraints {
648 &self.constraints
649 }
650
651 /// Returns the actual settings currently in effect for this track.
652 ///
653 /// Settings represent the current values of track properties like resolution,
654 /// frame rate, sample rate, etc. These may differ from requested constraints
655 /// based on hardware limitations or system conditions.
656 ///
657 /// # Specification
658 ///
659 /// See [MediaStreamTrack.getSettings()](https://www.w3.org/TR/mediacapture-streams/#dom-mediastreamtrack-getsettings).
660 ///
661 /// # Examples
662 ///
663 /// ```
664 /// # use rtc::media_stream::MediaStreamTrack;
665 /// # use rtc::rtp_transceiver::rtp_sender::{RTCRtpCodec, RtpCodecKind};
666 /// # fn example(track: MediaStreamTrack) {
667 /// let settings = track.get_settings();
668 /// // Check actual track settings
669 /// # }
670 /// ```
671 pub fn get_settings(&self) -> &MediaTrackSettings {
672 &self.settings
673 }
674
675 /// Applies constraints to this track.
676 ///
677 /// Constraints allow the application to specify desired values or ranges for
678 /// track properties like resolution, frame rate, etc. The user agent will
679 /// attempt to satisfy these constraints, but may not be able to meet all of them
680 /// depending on hardware capabilities and system conditions.
681 ///
682 /// If the track is in the "ended" state, this method has no effect.
683 ///
684 /// # Parameters
685 ///
686 /// * `constraints` - Optional constraints to apply. Pass `None` to remove all constraints.
687 ///
688 /// # Specification
689 ///
690 /// See [MediaStreamTrack.applyConstraints()](https://www.w3.org/TR/mediacapture-streams/#dom-mediastreamtrack-applyconstraints).
691 ///
692 /// # Examples
693 ///
694 /// ```
695 /// # use rtc::media_stream::MediaStreamTrack;
696 /// # use rtc::media_stream::MediaTrackConstraints;
697 /// # use rtc::rtp_transceiver::rtp_sender::{RTCRtpCodec, RtpCodecKind};
698 /// # fn example(mut track: MediaStreamTrack) {
699 /// // Apply new constraints
700 /// let constraints = MediaTrackConstraints::default();
701 /// track.apply_constraints(Some(constraints));
702 ///
703 /// // Remove all constraints
704 /// track.apply_constraints(None);
705 /// # }
706 /// ```
707 pub fn apply_constraints(&mut self, constraints: Option<MediaTrackConstraints>) {
708 if self.ready_state == MediaStreamTrackState::Ended {
709 return;
710 }
711
712 if let Some(constraints) = constraints {
713 //TODO: https://www.w3.org/TR/mediacapture-streams/#dom-mediastreamtrack-applyconstraints
714 self.constraints = constraints;
715 }
716 }
717
718 /// Sets the codec for this track by ssrc (internal use only).
719 ///
720 /// # Parameters
721 ///
722 /// * `codec` - The new RTP codec configuration
723 ///
724 /// * `ssrc` - The new RTP SSRC
725 ///
726 /// # Return
727 ///
728 /// true: insert new entry if not found
729 ///
730 /// false: update entry when found
731 pub(crate) fn set_codec_by_ssrc(&mut self, codec: RTCRtpCodec, ssrc: SSRC) -> bool {
732 if let Some(coding) = self.codings.iter_mut().find(|coding| {
733 coding
734 .rtp_coding_parameters
735 .ssrc
736 .is_some_and(|t_ssrc| t_ssrc == ssrc)
737 }) {
738 coding.codec = codec;
739 false
740 } else {
741 self.codings.push(RTCRtpEncodingParameters {
742 rtp_coding_parameters: RTCRtpCodingParameters {
743 rid: "".to_string(),
744 ssrc: Some(ssrc),
745 rtx: None,
746 fec: None,
747 },
748 codec,
749 ..Default::default()
750 });
751 true
752 }
753 }
754
755 pub(crate) fn set_codec_ssrc_by_rid(
756 &mut self,
757 codec: RTCRtpCodec,
758 ssrc: SSRC,
759 rid: &RtpStreamId,
760 ) -> bool {
761 if let Some(coding) = self
762 .codings
763 .iter_mut()
764 .find(|coding| &coding.rtp_coding_parameters.rid == rid)
765 {
766 coding.codec = codec;
767 coding.rtp_coding_parameters.ssrc = Some(ssrc);
768 false
769 } else {
770 self.codings.push(RTCRtpEncodingParameters {
771 rtp_coding_parameters: RTCRtpCodingParameters {
772 rid: rid.to_string(),
773 ssrc: Some(ssrc),
774 rtx: None,
775 fec: None,
776 },
777 codec,
778 ..Default::default()
779 });
780 true
781 }
782 }
783
784 pub(crate) fn get_codec_by_ssrc(&self, ssrc: SSRC) -> Option<&RTCRtpCodec> {
785 self.codings
786 .iter()
787 .find(|coding| {
788 coding
789 .rtp_coding_parameters
790 .ssrc
791 .is_some_and(|t_ssrc| t_ssrc == ssrc)
792 })
793 .map(|coding| &coding.codec)
794 }
795
796 pub fn codings(&self) -> &[RTCRtpEncodingParameters] {
797 &self.codings
798 }
799
800 pub fn add_coding(&mut self, coding: RTCRtpEncodingParameters) {
801 self.codings.push(coding);
802 }
803}