rtc/peer_connection/event/track_event.rs
1//! Track event types.
2//!
3//! Events related to incoming media tracks from the remote peer.
4
5use crate::media_stream::MediaStreamId;
6use crate::media_stream::track::MediaStreamTrackId;
7use crate::rtp_transceiver::{RTCRtpReceiverId, RtpStreamId};
8
9/// Initialization data for a track event.
10///
11/// Contains IDs needed to access the track, receiver, transceiver, and
12/// associated media streams when a track is opened.
13///
14/// # Fields
15///
16/// - `receiver_id` - ID of the RTP receiver handling this track
17/// - `track_id` - ID of the media stream track
18/// - `stream_ids` - IDs of media streams this track belongs to
19/// - `rid` - RTP Stream ID for simulcast/SVC (if applicable)
20///
21/// # Examples
22///
23/// ## Accessing track components
24///
25/// ```ignore
26/// // Note: Accessing receiver/transceiver requires &mut RTCPeerConnection
27/// use rtc::peer_connection::RTCPeerConnection;
28/// use rtc::peer_connection::event::{RTCPeerConnectionEvent, RTCTrackEvent};
29///
30/// fn handle_event(mut peer_connection: RTCPeerConnection, event: RTCPeerConnectionEvent) {
31/// match event {
32/// RTCPeerConnectionEvent::OnTrack(RTCTrackEvent::OnOpen(init)) => {
33/// // Access the receiver
34/// if let Some(receiver) = peer_connection.rtp_receiver(init.receiver_id) {
35/// // Get receiver parameters
36/// let params = receiver.get_parameters();
37/// println!("Codecs: {:?}", params.codecs);
38/// }
39///
40/// // Print associated stream IDs
41/// println!("Stream IDs: {:?}", init.stream_ids);
42///
43/// // Check if this is a simulcast stream
44/// if let Some(rid) = &init.rid {
45/// println!("Simulcast RID: {}", rid);
46/// }
47/// }
48/// _ => {}
49/// }
50/// }
51/// ```
52#[allow(clippy::enum_variant_names)]
53#[derive(Default, Debug, Clone)]
54pub struct RTCTrackEventInit {
55 /// ID of the RTP receiver handling this track.
56 ///
57 /// Use this with `peer_connection.rtp_receiver()` to access the receiver.
58 pub receiver_id: RTCRtpReceiverId,
59
60 /// ID of the media stream track.
61 ///
62 /// This uniquely identifies the track within the peer connection.
63 pub track_id: MediaStreamTrackId,
64
65 /// IDs of media streams this track belongs to.
66 ///
67 /// A track can be associated with multiple media streams.
68 /// These correspond to the msid attribute in the SDP.
69 pub stream_ids: Vec<MediaStreamId>,
70
71 /// SSRC of the first RTP packet that triggered this OnOpen event.
72 ///
73 /// This is always populated when `OnOpen` fires (since it fires on receipt of the
74 /// first RTP packet for this stream). Use this value when sending RTCP feedback
75 /// such as PLI or NACK targeted at this specific stream.
76 pub ssrc: u32,
77
78 /// RTP Stream ID (RID) for simulcast/SVC streams.
79 ///
80 /// In simulcast scenarios, this identifies which spatial/temporal layer this stream represents.
81 /// The value comes from the RTP header extension (urn:ietf:params:rtp-hdrext:sdes:rtp-stream-id)
82 /// in the first received RTP packet.
83 ///
84 /// - `None` - Non-simulcast track, or simulcast track before first RTP packet received
85 /// - `Some(rid)` - Simulcast stream identifier (e.g., "q", "h", "f" for quality levels)
86 pub rid: Option<RtpStreamId>,
87}
88
89/// Events related to incoming media tracks.
90///
91/// These events track the lifecycle of media tracks received from the remote peer.
92/// Applications should handle these events to access incoming audio/video streams
93/// and read RTP/RTCP packets.
94///
95/// # Lifecycle
96///
97/// 1. `OnOpen` - Track is ready, contains initialization data
98/// 2. `OnError` - An error occurred with the track
99/// 3. `OnClosing` - Track is starting to close
100/// 4. `OnClose` - Track is fully closed
101///
102/// # Examples
103///
104/// ## Handling track lifecycle
105///
106/// ```
107/// use rtc::peer_connection::event::{RTCPeerConnectionEvent, RTCTrackEvent};
108///
109/// # fn handle_event(event: RTCPeerConnectionEvent) {
110/// match event {
111/// RTCPeerConnectionEvent::OnTrack(track_event) => {
112/// match track_event {
113/// RTCTrackEvent::OnOpen(init) => {
114/// println!("Track opened: {:?}", init.track_id);
115/// // Start reading RTP packets
116/// }
117/// RTCTrackEvent::OnError(track_id) => {
118/// eprintln!("Track error: {:?}", track_id);
119/// }
120/// RTCTrackEvent::OnClose(track_id) => {
121/// println!("Track closed: {:?}", track_id);
122/// // Clean up resources
123/// }
124/// _ => {}
125/// }
126/// }
127/// _ => {}
128/// }
129/// # }
130/// ```
131///
132/// ## Reading media from track (conceptual)
133///
134/// ```ignore
135/// // Note: poll_read() is part of sans-I/O design
136/// use rtc::peer_connection::RTCPeerConnection;
137/// use rtc::peer_connection::event::{RTCPeerConnectionEvent, RTCTrackEvent};
138/// use rtc::peer_connection::message::RTCMessage;
139///
140/// fn handle_events(mut peer_connection: RTCPeerConnection) {
141/// // Poll events
142/// while let Some(event) = peer_connection.poll_event() {
143/// if let RTCPeerConnectionEvent::OnTrack(RTCTrackEvent::OnOpen(init)) = event {
144/// println!("Track opened, ready to receive media");
145/// }
146/// }
147///
148/// // Poll incoming media
149/// while let Some(message) = peer_connection.poll_read() {
150/// match message {
151/// RTCMessage::RtpPacket(track_id, rtp) => {
152/// println!("RTP packet from track {:?}: {} bytes", track_id, rtp.payload.len());
153/// }
154/// RTCMessage::RtcpPacket(receiver_id, rtcp) => {
155/// println!("RTCP packet: {:?}", rtcp);
156/// }
157/// _ => {}
158/// }
159/// }
160/// }
161/// ```
162///
163/// # Specification
164///
165/// See [RTCTrackEvent](https://www.w3.org/TR/webrtc/#rtctrackevent)
166#[allow(clippy::enum_variant_names)]
167#[derive(Debug, Clone)]
168pub enum RTCTrackEvent {
169 /// Track has opened and is ready to receive media.
170 ///
171 /// This variant contains initialization data with IDs to access
172 /// the track, receiver, transceiver, and associated streams.
173 OnOpen(RTCTrackEventInit),
174
175 /// An error occurred with the track.
176 ///
177 /// The track may still be usable depending on the error type.
178 OnError(MediaStreamTrackId),
179
180 /// Track is starting to close.
181 ///
182 /// The track is transitioning to the closing state.
183 OnClosing(MediaStreamTrackId),
184
185 /// Track has closed.
186 ///
187 /// The track is no longer usable and resources should be cleaned up.
188 OnClose(MediaStreamTrackId),
189}
190
191impl Default for RTCTrackEvent {
192 fn default() -> Self {
193 Self::OnOpen(Default::default())
194 }
195}