rtc 0.9.0

Sans-I/O WebRTC implementation in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
use std::fmt;

use crate::peer_connection::configuration::UNSPECIFIED_STR;
use crate::peer_connection::sdp::sdp_type::RTCSdpType;
use shared::error::{Error, Result};

#[derive(Default, Debug, Copy, Clone, PartialEq)]
pub(crate) enum StateChangeOp {
    #[default]
    SetLocal,
    SetRemote,
}

impl fmt::Display for StateChangeOp {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {
            StateChangeOp::SetLocal => write!(f, "SetLocal"),
            StateChangeOp::SetRemote => write!(f, "SetRemote"),
            //_ => write!(f, UNSPECIFIED_STR),
        }
    }
}

/// Indicates the state of the SDP offer/answer negotiation process.
///
/// `RTCSignalingState` describes the current state of the SDP (Session Description
/// Protocol) signaling exchange between local and remote peers. The signaling state
/// tracks progress through the offer/answer model defined in RFC 3264.
///
/// # Signaling Flow
///
/// The typical offer/answer exchange follows this pattern:
///
/// **Initiating Peer (Offerer):**
/// ```text
/// Stable → (setLocalDescription with offer) → HaveLocalOffer
///       → (setRemoteDescription with answer) → Stable
/// ```
///
/// **Responding Peer (Answerer):**
/// ```text
/// Stable → (setRemoteDescription with offer) → HaveRemoteOffer
///       → (setLocalDescription with answer) → Stable
/// ```
///
/// Provisional answers add intermediate states:
/// ```text
/// HaveLocalOffer → (setRemoteDescription with pranswer) → HaveRemotePranswer
///                → (setRemoteDescription with answer) → Stable
/// ```
///
/// # State Machine Rules
///
/// The signaling state machine enforces valid transitions according to the
/// WebRTC specification. Invalid transitions (e.g., setting an answer when
/// in Stable state) will result in errors.
///
/// # Examples
///
/// ## Monitoring Signaling State (Offerer)
///
/// ```no_run
/// use rtc::peer_connection::state::RTCSignalingState;
/// use rtc::peer_connection::event::RTCPeerConnectionEvent;
///
/// # fn handle_event_offerer(event: RTCPeerConnectionEvent) {
/// match event {
///     RTCPeerConnectionEvent::OnSignalingStateChangeEvent(state) => {
///         match state {
///             RTCSignalingState::Stable => {
///                 println!("Ready to create new offer");
///             }
///             RTCSignalingState::HaveLocalOffer => {
///                 println!("Offer sent, waiting for answer from remote peer");
///             }
///             RTCSignalingState::HaveRemotePranswer => {
///                 println!("Received provisional answer, waiting for final answer");
///             }
///             _ => {}
///         }
///     }
///     _ => {}
/// }
/// # }
/// ```
///
/// ## Monitoring Signaling State (Answerer)
///
/// ```no_run
/// use rtc::peer_connection::state::RTCSignalingState;
/// use rtc::peer_connection::event::RTCPeerConnectionEvent;
///
/// # fn handle_event_answerer(event: RTCPeerConnectionEvent) {
/// match event {
///     RTCPeerConnectionEvent::OnSignalingStateChangeEvent(state) => {
///         match state {
///             RTCSignalingState::HaveRemoteOffer => {
///                 println!("Received offer from remote peer");
///                 println!("Need to create and set answer");
///             }
///             RTCSignalingState::Stable => {
///                 println!("Answer sent and accepted - negotiation complete");
///             }
///             _ => {}
///         }
///     }
///     _ => {}
/// }
/// # }
/// ```
///
/// ## Checking if Negotiation is in Progress
///
/// ```
/// use rtc::peer_connection::state::RTCSignalingState;
///
/// fn is_negotiating(state: RTCSignalingState) -> bool {
///     !matches!(state, RTCSignalingState::Stable | RTCSignalingState::Closed)
/// }
///
/// fn can_create_offer(state: RTCSignalingState) -> bool {
///     matches!(state, RTCSignalingState::Stable)
/// }
///
/// assert!(!is_negotiating(RTCSignalingState::Stable));
/// assert!(is_negotiating(RTCSignalingState::HaveLocalOffer));
/// assert!(can_create_offer(RTCSignalingState::Stable));
/// assert!(!can_create_offer(RTCSignalingState::HaveRemoteOffer));
/// ```
///
/// ## String Conversion
///
/// ```
/// use rtc::peer_connection::state::RTCSignalingState;
///
/// // Convert to string
/// let state = RTCSignalingState::HaveLocalOffer;
/// assert_eq!(state.to_string(), "have-local-offer");
///
/// // Parse from string
/// let parsed: RTCSignalingState = "have-remote-offer".into();
/// assert_eq!(parsed, RTCSignalingState::HaveRemoteOffer);
/// ```
///
/// ## Handling State Transitions
///
/// ```no_run
/// use rtc::peer_connection::state::RTCSignalingState;
/// use rtc::peer_connection::event::RTCPeerConnectionEvent;
///
/// # fn handle_transitions(event: RTCPeerConnectionEvent, is_offerer: bool) {
/// match event {
///     RTCPeerConnectionEvent::OnSignalingStateChangeEvent(state) => {
///         match (is_offerer, state) {
///             (true, RTCSignalingState::Stable) => {
///                 println!("Ready to renegotiate if needed");
///             }
///             (true, RTCSignalingState::HaveLocalOffer) => {
///                 // Offer created and set locally
///                 println!("Send offer SDP to remote peer via signaling channel");
///             }
///             (false, RTCSignalingState::HaveRemoteOffer) => {
///                 // Offer received from remote
///                 println!("Create and send answer");
///             }
///             (false, RTCSignalingState::Stable) => {
///                 println!("Negotiation complete");
///             }
///             _ => {}
///         }
///     }
///     _ => {}
/// }
/// # }
/// ```
///
/// # Specifications
///
/// - [W3C RTCPeerConnection.signalingState]
/// - [MDN RTCPeerConnection.signalingState]
/// - [RFC 3264] - Offer/Answer Model
///
/// [W3C RTCPeerConnection.signalingState]: https://w3c.github.io/webrtc-pc/#dom-peerconnection-signaling-state
/// [MDN RTCPeerConnection.signalingState]: https://developer.mozilla.org/en-US/docs/Web/API/RTCPeerConnection/signalingState
/// [RFC 3264]: https://datatracker.ietf.org/doc/html/rfc3264
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
pub enum RTCSignalingState {
    /// State not specified. This should not occur in normal operation.
    Unspecified = 0,

    /// No offer/answer exchange is in progress.
    ///
    /// This is the initial state and also the state after a successful
    /// offer/answer exchange completes. In this state:
    ///
    /// - Both local and remote descriptions may be null (initial state)
    /// - Or both descriptions are set (after successful negotiation)
    ///
    /// A new negotiation can only be started from this state. This is the
    /// default state.
    #[default]
    Stable,

    /// Local offer has been set, waiting for remote answer.
    ///
    /// A local description of type "offer" has been successfully applied via
    /// `setLocalDescription()`. The local peer is now waiting for the remote
    /// peer to respond with an answer or provisional answer.
    ///
    /// Transition: Stable → (setLocalDescription with offer) → HaveLocalOffer
    HaveLocalOffer,

    /// Remote offer has been set, need to create local answer.
    ///
    /// A remote description of type "offer" has been successfully applied via
    /// `setRemoteDescription()`. The local peer should now create and set a
    /// local answer (or provisional answer) to complete the negotiation.
    ///
    /// Transition: Stable → (setRemoteDescription with offer) → HaveRemoteOffer
    HaveRemoteOffer,

    /// Remote offer received, local provisional answer set.
    ///
    /// A remote description of type "offer" was applied, followed by a local
    /// description of type "pranswer" (provisional answer). The local peer
    /// will later send a final answer to complete negotiation.
    ///
    /// Provisional answers allow early media to flow before final codec/
    /// transport selection is complete.
    ///
    /// Transition: HaveRemoteOffer → (setLocalDescription with pranswer) → HaveLocalPranswer
    HaveLocalPranswer,

    /// Local offer sent, remote provisional answer received.
    ///
    /// A local description of type "offer" was applied, followed by a remote
    /// description of type "pranswer" (provisional answer). The remote peer
    /// will later send a final answer to complete negotiation.
    ///
    /// Transition: HaveLocalOffer → (setRemoteDescription with pranswer) → HaveRemotePranswer
    HaveRemotePranswer,

    /// The peer connection has been closed.
    ///
    /// No further signaling operations are possible. The connection must be
    /// recreated to establish communication again.
    Closed,
}

const SIGNALING_STATE_STABLE_STR: &str = "stable";
const SIGNALING_STATE_HAVE_LOCAL_OFFER_STR: &str = "have-local-offer";
const SIGNALING_STATE_HAVE_REMOTE_OFFER_STR: &str = "have-remote-offer";
const SIGNALING_STATE_HAVE_LOCAL_PRANSWER_STR: &str = "have-local-pranswer";
const SIGNALING_STATE_HAVE_REMOTE_PRANSWER_STR: &str = "have-remote-pranswer";
const SIGNALING_STATE_CLOSED_STR: &str = "closed";

impl From<&str> for RTCSignalingState {
    fn from(raw: &str) -> Self {
        match raw {
            SIGNALING_STATE_STABLE_STR => RTCSignalingState::Stable,
            SIGNALING_STATE_HAVE_LOCAL_OFFER_STR => RTCSignalingState::HaveLocalOffer,
            SIGNALING_STATE_HAVE_REMOTE_OFFER_STR => RTCSignalingState::HaveRemoteOffer,
            SIGNALING_STATE_HAVE_LOCAL_PRANSWER_STR => RTCSignalingState::HaveLocalPranswer,
            SIGNALING_STATE_HAVE_REMOTE_PRANSWER_STR => RTCSignalingState::HaveRemotePranswer,
            SIGNALING_STATE_CLOSED_STR => RTCSignalingState::Closed,
            _ => RTCSignalingState::Unspecified,
        }
    }
}

impl fmt::Display for RTCSignalingState {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match *self {
            RTCSignalingState::Stable => write!(f, "{SIGNALING_STATE_STABLE_STR}"),
            RTCSignalingState::HaveLocalOffer => {
                write!(f, "{SIGNALING_STATE_HAVE_LOCAL_OFFER_STR}")
            }
            RTCSignalingState::HaveRemoteOffer => {
                write!(f, "{SIGNALING_STATE_HAVE_REMOTE_OFFER_STR}")
            }
            RTCSignalingState::HaveLocalPranswer => {
                write!(f, "{SIGNALING_STATE_HAVE_LOCAL_PRANSWER_STR}")
            }
            RTCSignalingState::HaveRemotePranswer => {
                write!(f, "{SIGNALING_STATE_HAVE_REMOTE_PRANSWER_STR}")
            }
            RTCSignalingState::Closed => write!(f, "{SIGNALING_STATE_CLOSED_STR}"),
            _ => write!(f, "{}", UNSPECIFIED_STR),
        }
    }
}

impl From<u8> for RTCSignalingState {
    fn from(v: u8) -> Self {
        match v {
            1 => RTCSignalingState::Stable,
            2 => RTCSignalingState::HaveLocalOffer,
            3 => RTCSignalingState::HaveRemoteOffer,
            4 => RTCSignalingState::HaveLocalPranswer,
            5 => RTCSignalingState::HaveRemotePranswer,
            6 => RTCSignalingState::Closed,
            _ => RTCSignalingState::Unspecified,
        }
    }
}

pub(crate) fn check_next_signaling_state(
    cur: RTCSignalingState,
    next: RTCSignalingState,
    op: StateChangeOp,
    sdp_type: RTCSdpType,
) -> Result<RTCSignalingState> {
    // Special case for rollbacks
    if sdp_type == RTCSdpType::Rollback && cur == RTCSignalingState::Stable {
        return Err(Error::ErrSignalingStateCannotRollback);
    }

    // 4.3.1 valid state transitions
    match cur {
        RTCSignalingState::Stable => {
            match op {
                StateChangeOp::SetLocal => {
                    // stable->SetLocal(offer)->have-local-offer
                    if sdp_type == RTCSdpType::Offer && next == RTCSignalingState::HaveLocalOffer {
                        return Ok(next);
                    }
                }
                StateChangeOp::SetRemote => {
                    // stable->SetRemote(offer)->have-remote-offer
                    if sdp_type == RTCSdpType::Offer && next == RTCSignalingState::HaveRemoteOffer {
                        return Ok(next);
                    }
                }
            }
        }
        RTCSignalingState::HaveLocalOffer => {
            if op == StateChangeOp::SetRemote {
                match sdp_type {
                    // have-local-offer->SetRemote(answer)->stable
                    RTCSdpType::Answer => {
                        if next == RTCSignalingState::Stable {
                            return Ok(next);
                        }
                    }
                    // have-local-offer->SetRemote(pranswer)->have-remote-pranswer
                    RTCSdpType::Pranswer => {
                        if next == RTCSignalingState::HaveRemotePranswer {
                            return Ok(next);
                        }
                    }
                    _ => {}
                }
            } else if op == StateChangeOp::SetLocal {
                match sdp_type {
                    // have-local-offer->SetLocal(offer)->have-local-offer (reoffer)
                    RTCSdpType::Offer => {
                        if next == RTCSignalingState::HaveLocalOffer {
                            return Ok(next);
                        }
                    }
                    // have-local-offer->SetLocal(rollback)->stable
                    RTCSdpType::Rollback => {
                        if next == RTCSignalingState::Stable {
                            return Ok(next);
                        }
                    }
                    _ => {}
                }
            }
        }
        RTCSignalingState::HaveRemotePranswer => {
            if op == StateChangeOp::SetRemote && sdp_type == RTCSdpType::Answer {
                // have-remote-pranswer->SetRemote(answer)->stable
                if next == RTCSignalingState::Stable {
                    return Ok(next);
                }
            }
        }
        RTCSignalingState::HaveRemoteOffer => {
            if op == StateChangeOp::SetLocal {
                match sdp_type {
                    // have-remote-offer->SetLocal(answer)->stable
                    RTCSdpType::Answer => {
                        if next == RTCSignalingState::Stable {
                            return Ok(next);
                        }
                    }
                    // have-remote-offer->SetLocal(pranswer)->have-local-pranswer
                    RTCSdpType::Pranswer => {
                        if next == RTCSignalingState::HaveLocalPranswer {
                            return Ok(next);
                        }
                    }
                    _ => {}
                }
            } else if op == StateChangeOp::SetRemote
                && sdp_type == RTCSdpType::Rollback
                && next == RTCSignalingState::Stable
            {
                // have-remote-offer->SetRemote(rollback)->stable
                return Ok(next);
            }
        }
        RTCSignalingState::HaveLocalPranswer => {
            if op == StateChangeOp::SetLocal && sdp_type == RTCSdpType::Answer {
                // have-local-pranswer->SetLocal(answer)->stable
                if next == RTCSignalingState::Stable {
                    return Ok(next);
                }
            }
        }
        _ => {
            return Err(Error::ErrSignalingStateProposedTransitionInvalid(format!(
                "from {} applying {} {}",
                cur,
                sdp_type,
                op == StateChangeOp::SetLocal
            )));
        }
    };

    Err(Error::ErrSignalingStateProposedTransitionInvalid(format!(
        "from {} applying {} {}",
        cur,
        op == StateChangeOp::SetLocal,
        sdp_type
    )))
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_new_signaling_state() {
        let tests = vec![
            ("Unspecified", RTCSignalingState::Unspecified),
            ("stable", RTCSignalingState::Stable),
            ("have-local-offer", RTCSignalingState::HaveLocalOffer),
            ("have-remote-offer", RTCSignalingState::HaveRemoteOffer),
            ("have-local-pranswer", RTCSignalingState::HaveLocalPranswer),
            (
                "have-remote-pranswer",
                RTCSignalingState::HaveRemotePranswer,
            ),
            ("closed", RTCSignalingState::Closed),
        ];

        for (state_string, expected_state) in tests {
            assert_eq!(RTCSignalingState::from(state_string), expected_state);
        }
    }

    #[test]
    fn test_signaling_state_string() {
        let tests = vec![
            (RTCSignalingState::Unspecified, "Unspecified"),
            (RTCSignalingState::Stable, "stable"),
            (RTCSignalingState::HaveLocalOffer, "have-local-offer"),
            (RTCSignalingState::HaveRemoteOffer, "have-remote-offer"),
            (RTCSignalingState::HaveLocalPranswer, "have-local-pranswer"),
            (
                RTCSignalingState::HaveRemotePranswer,
                "have-remote-pranswer",
            ),
            (RTCSignalingState::Closed, "closed"),
        ];

        for (state, expected_string) in tests {
            assert_eq!(state.to_string(), expected_string);
        }
    }

    #[test]
    fn test_signaling_state_transitions() {
        let tests = vec![
            (
                "stable->SetLocal(offer)->have-local-offer",
                RTCSignalingState::Stable,
                RTCSignalingState::HaveLocalOffer,
                StateChangeOp::SetLocal,
                RTCSdpType::Offer,
                None,
            ),
            (
                "stable->SetRemote(offer)->have-remote-offer",
                RTCSignalingState::Stable,
                RTCSignalingState::HaveRemoteOffer,
                StateChangeOp::SetRemote,
                RTCSdpType::Offer,
                None,
            ),
            (
                "have-local-offer->SetRemote(answer)->stable",
                RTCSignalingState::HaveLocalOffer,
                RTCSignalingState::Stable,
                StateChangeOp::SetRemote,
                RTCSdpType::Answer,
                None,
            ),
            (
                "have-local-offer->SetRemote(pranswer)->have-remote-pranswer",
                RTCSignalingState::HaveLocalOffer,
                RTCSignalingState::HaveRemotePranswer,
                StateChangeOp::SetRemote,
                RTCSdpType::Pranswer,
                None,
            ),
            (
                "have-remote-pranswer->SetRemote(answer)->stable",
                RTCSignalingState::HaveRemotePranswer,
                RTCSignalingState::Stable,
                StateChangeOp::SetRemote,
                RTCSdpType::Answer,
                None,
            ),
            (
                "have-remote-offer->SetLocal(answer)->stable",
                RTCSignalingState::HaveRemoteOffer,
                RTCSignalingState::Stable,
                StateChangeOp::SetLocal,
                RTCSdpType::Answer,
                None,
            ),
            (
                "have-remote-offer->SetLocal(pranswer)->have-local-pranswer",
                RTCSignalingState::HaveRemoteOffer,
                RTCSignalingState::HaveLocalPranswer,
                StateChangeOp::SetLocal,
                RTCSdpType::Pranswer,
                None,
            ),
            (
                "have-local-pranswer->SetLocal(answer)->stable",
                RTCSignalingState::HaveLocalPranswer,
                RTCSignalingState::Stable,
                StateChangeOp::SetLocal,
                RTCSdpType::Answer,
                None,
            ),
            (
                "(invalid) stable->SetRemote(pranswer)->have-remote-pranswer",
                RTCSignalingState::Stable,
                RTCSignalingState::HaveRemotePranswer,
                StateChangeOp::SetRemote,
                RTCSdpType::Pranswer,
                Some(Error::ErrSignalingStateProposedTransitionInvalid(format!(
                    "from {} applying {} {}",
                    RTCSignalingState::Stable,
                    false,
                    RTCSdpType::Pranswer
                ))),
            ),
            (
                "(invalid) stable->SetRemote(rollback)->have-local-offer",
                RTCSignalingState::Stable,
                RTCSignalingState::HaveLocalOffer,
                StateChangeOp::SetRemote,
                RTCSdpType::Rollback,
                Some(Error::ErrSignalingStateCannotRollback),
            ),
        ];

        for (desc, cur, next, op, sdp_type, expected_err) in tests {
            let result = check_next_signaling_state(cur, next, op, sdp_type);
            match (&result, &expected_err) {
                (Ok(got), None) => {
                    assert_eq!(*got, next, "{desc} state mismatch");
                }
                (Err(got), Some(err)) => {
                    assert_eq!(got.to_string(), err.to_string(), "{desc} error mismatch");
                }
                _ => {
                    panic!("{desc}: expected {expected_err:?}, but got {result:?}");
                }
            };
        }
    }
}