saorsa-webrtc 0.1.2

WebRTC implementation over ant-quic transport with DHT-based signaling
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
//! WebRTC types and data structures

use crate::identity::PeerIdentity;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use uuid::Uuid;

/// Unique identifier for a call
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct CallId(pub Uuid);

impl CallId {
    /// Create a new random call ID
    pub fn new() -> Self {
        Self(Uuid::new_v4())
    }
}

impl Default for CallId {
    fn default() -> Self {
        Self::new()
    }
}

impl std::fmt::Display for CallId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}

/// Media constraints for a call
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MediaConstraints {
    /// Enable audio
    pub audio: bool,
    /// Enable video
    pub video: bool,
    /// Enable screen sharing
    pub screen_share: bool,
}

impl MediaConstraints {
    /// Audio-only call
    pub fn audio_only() -> Self {
        Self {
            audio: true,
            video: false,
            screen_share: false,
        }
    }

    /// Video call with audio
    pub fn video_call() -> Self {
        Self {
            audio: true,
            video: true,
            screen_share: false,
        }
    }

    /// Screen share with audio
    pub fn screen_share() -> Self {
        Self {
            audio: true,
            video: false,
            screen_share: true,
        }
    }

    /// Check if audio is enabled
    pub fn has_audio(&self) -> bool {
        self.audio
    }

    /// Check if video is enabled
    pub fn has_video(&self) -> bool {
        self.video
    }

    /// Check if screen share is enabled
    pub fn has_screen_share(&self) -> bool {
        self.screen_share
    }

    /// Convert to media types
    pub fn to_media_types(&self) -> Vec<MediaType> {
        let mut types = Vec::new();
        if self.audio {
            types.push(MediaType::Audio);
        }
        if self.video {
            types.push(MediaType::Video);
        }
        if self.screen_share {
            types.push(MediaType::ScreenShare);
        }
        types
    }
}

/// Types of media in a call
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum MediaType {
    /// Audio stream
    Audio,
    /// Video stream
    Video,
    /// Screen share stream
    ScreenShare,
    /// Data channel
    DataChannel,
}

/// Call offer message
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(bound = "I: PeerIdentity")]
pub struct CallOffer<I: PeerIdentity> {
    /// Unique call identifier
    pub call_id: CallId,
    /// Identity of the caller
    pub caller: I,
    /// Identity of the callee
    pub callee: I,
    /// SDP offer string
    pub sdp: String,
    /// Media types in this call
    pub media_types: Vec<MediaType>,
    /// Timestamp when offer was created
    pub timestamp: DateTime<Utc>,
}

/// Call answer message
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CallAnswer {
    /// Call identifier
    pub call_id: CallId,
    /// SDP answer string
    pub sdp: String,
    /// Whether the call was accepted
    pub accepted: bool,
    /// Timestamp when answer was created
    pub timestamp: DateTime<Utc>,
}

/// ICE candidate for WebRTC connection
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IceCandidate {
    /// Call identifier
    pub call_id: CallId,
    /// ICE candidate string
    pub candidate: String,
    /// SDP media ID
    pub sdp_mid: Option<String>,
    /// SDP media line index
    pub sdp_mline_index: Option<u32>,
}

/// Call state enumeration
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum CallState {
    /// No active call
    Idle,
    /// Initiating call
    Calling,
    /// Establishing connection
    Connecting,
    /// Call is active
    Connected,
    /// Call is ending
    Ending,
    /// Call failed
    Failed,
}

/// Call quality metrics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CallQualityMetrics {
    /// Round-trip time in milliseconds
    pub rtt_ms: u32,
    /// Packet loss percentage
    pub packet_loss_percent: f32,
    /// Jitter in milliseconds
    pub jitter_ms: u32,
    /// Bandwidth in kilobits per second
    pub bandwidth_kbps: u32,
    /// Timestamp when metrics were collected
    pub timestamp: DateTime<Utc>,
}

impl CallQualityMetrics {
    /// Check if quality is good
    pub fn is_good_quality(&self) -> bool {
        self.rtt_ms < 100
            && self.packet_loss_percent < 1.0
            && self.jitter_ms < 20
            && self.bandwidth_kbps > 500
    }

    /// Check if network adaptation is needed
    pub fn needs_adaptation(&self) -> bool {
        self.rtt_ms > 200
            || self.packet_loss_percent > 3.0
            || self.jitter_ms > 40
            || self.bandwidth_kbps < 300
    }
}

/// Multi-party call information
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(bound = "I: PeerIdentity")]
pub struct MultiPartyCall<I: PeerIdentity> {
    /// Call identifier
    pub call_id: CallId,
    /// Participating peers
    pub participants: Vec<I>,
    /// Call architecture
    pub architecture: CallArchitecture,
    /// Creation timestamp
    pub created_at: DateTime<Utc>,
}

/// Call architecture for multi-party calls
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum CallArchitecture {
    /// Mesh - Direct P2P between all participants (2-4 people)
    Mesh,
    /// SFU - Selective Forwarding Unit (5+ people)
    SFU,
}

/// Recording consent management
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(bound = "I: PeerIdentity")]
pub struct RecordingConsent<I: PeerIdentity> {
    /// Call being recorded
    pub call_id: CallId,
    /// Who is requesting to record
    pub requester: I,
    /// Participants who must consent
    pub participants: Vec<I>,
}

/// Consent status
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ConsentStatus {
    /// Awaiting response
    Pending,
    /// Consent granted
    Granted,
    /// Consent denied
    Denied,
    /// Consent revoked
    Revoked,
}

/// Network adaptation settings
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AdaptationSettings {
    /// Video bitrate in kilobits per second
    pub video_bitrate_kbps: u32,
    /// Video resolution
    pub video_resolution: VideoResolution,
    /// Video frames per second
    pub video_fps: u32,
    /// Audio bitrate in kilobits per second
    pub audio_bitrate_kbps: u32,
    /// Enable discontinuous transmission
    pub enable_dtx: bool,
}

/// Video resolution options
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum VideoResolution {
    /// 320x240
    QVGA240,
    /// 640x480
    SD480,
    /// 1280x720
    HD720,
    /// 1920x1080
    HD1080,
}

impl VideoResolution {
    /// Get width in pixels
    pub fn width(&self) -> u32 {
        match self {
            Self::QVGA240 => 320,
            Self::SD480 => 640,
            Self::HD720 => 1280,
            Self::HD1080 => 1920,
        }
    }

    /// Get height in pixels
    pub fn height(&self) -> u32 {
        match self {
            Self::QVGA240 => 240,
            Self::SD480 => 480,
            Self::HD720 => 720,
            Self::HD1080 => 1080,
        }
    }
}

/// Native QUIC connectivity configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NativeQuicConfiguration {
    /// DHT-based peer discovery is enabled by default
    pub dht_discovery: bool,
    /// Coordinator-based hole punching configuration
    pub hole_punching: bool,
}

impl Default for NativeQuicConfiguration {
    fn default() -> Self {
        Self {
            dht_discovery: true,
            hole_punching: true,
        }
    }
}

/// WebRTC signaling message wrapper
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(bound = "I: PeerIdentity")]
pub enum SignalingMessage<I: PeerIdentity> {
    /// Call offer
    Offer(CallOffer<I>),
    /// Call answer
    Answer(CallAnswer),
    /// End call
    CallEnd {
        /// Call to end
        call_id: CallId,
    },
    /// Reject call
    CallReject {
        /// Call to reject
        call_id: CallId,
    },
}

/// Call event for notifications
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(bound = "I: PeerIdentity")]
pub enum CallEvent<I: PeerIdentity> {
    /// Incoming call received
    IncomingCall {
        /// The call offer
        offer: CallOffer<I>,
    },
    /// Call initiated
    CallInitiated {
        /// Call identifier
        call_id: CallId,
        /// Who is being called
        callee: I,
        /// Media constraints
        constraints: MediaConstraints,
    },
    /// Call accepted
    CallAccepted {
        /// Call identifier
        call_id: CallId,
        /// The answer
        answer: CallAnswer,
    },
    /// Call rejected
    CallRejected {
        /// Call identifier
        call_id: CallId,
    },
    /// Call ended
    CallEnded {
        /// Call identifier
        call_id: CallId,
    },
    /// Connection established
    ConnectionEstablished {
        /// Call identifier
        call_id: CallId,
    },
    /// Connection failed
    ConnectionFailed {
        /// Call identifier
        call_id: CallId,
        /// Error description
        error: String,
    },
    /// Quality changed
    QualityChanged {
        /// Call identifier
        call_id: CallId,
        /// Current metrics
        metrics: CallQualityMetrics,
    },
}

/// Call session information
#[derive(Debug, Clone)]
pub struct CallSession<I: PeerIdentity> {
    /// Call identifier
    pub call_id: CallId,
    /// Participating peers
    pub participants: Vec<I>,
    /// Current state
    pub state: CallState,
    /// Media constraints
    pub media_constraints: MediaConstraints,
    /// Creation timestamp
    pub created_at: DateTime<Utc>,
    /// Start time (when connected)
    pub start_time: Option<DateTime<Utc>>,
    /// End time
    pub end_time: Option<DateTime<Utc>>,
    /// Quality metrics history
    pub quality_metrics: Vec<CallQualityMetrics>,
}

impl<I: PeerIdentity> CallSession<I> {
    /// Create a new call session
    pub fn new(call_id: CallId, constraints: MediaConstraints) -> Self {
        Self {
            call_id,
            participants: Vec::new(),
            state: CallState::Idle,
            media_constraints: constraints,
            created_at: Utc::now(),
            start_time: None,
            end_time: None,
            quality_metrics: Vec::new(),
        }
    }

    /// Get call duration
    pub fn duration(&self) -> Option<chrono::Duration> {
        if let (Some(start), Some(end)) = (self.start_time, self.end_time) {
            Some(end - start)
        } else {
            self.start_time.map(|start| Utc::now() - start)
        }
    }

    /// Add a participant
    pub fn add_participant(&mut self, participant: I) {
        if !self
            .participants
            .iter()
            .any(|p| p.to_string_repr() == participant.to_string_repr())
        {
            self.participants.push(participant);
        }
    }

    /// Remove a participant
    pub fn remove_participant(&mut self, participant: &I) {
        self.participants
            .retain(|p| p.to_string_repr() != participant.to_string_repr());
    }

    /// Add quality metric
    pub fn add_quality_metric(&mut self, metric: CallQualityMetrics) {
        self.quality_metrics.push(metric);

        // Keep only last 100 metrics
        if self.quality_metrics.len() > 100 {
            self.quality_metrics.remove(0);
        }
    }

    /// Get latest quality metric
    pub fn latest_quality(&self) -> Option<&CallQualityMetrics> {
        self.quality_metrics.last()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::identity::PeerIdentityString;

    #[test]
    fn test_call_id() {
        let id1 = CallId::new();
        let id2 = CallId::new();
        assert_ne!(id1, id2);
    }

    #[test]
    fn test_media_constraints() {
        let audio = MediaConstraints::audio_only();
        assert!(audio.has_audio());
        assert!(!audio.has_video());
        assert!(!audio.has_screen_share());

        let video = MediaConstraints::video_call();
        assert!(video.has_audio());
        assert!(video.has_video());
        assert!(!video.has_screen_share());

        let screen = MediaConstraints::screen_share();
        assert!(screen.has_audio());
        assert!(!screen.has_video());
        assert!(screen.has_screen_share());
    }

    #[test]
    fn test_call_session() {
        let call_id = CallId::new();
        let constraints = MediaConstraints::video_call();
        let mut session: CallSession<PeerIdentityString> = CallSession::new(call_id, constraints);

        assert_eq!(session.call_id, call_id);
        assert_eq!(session.state, CallState::Idle);
        assert_eq!(session.participants.len(), 0);

        let peer = PeerIdentityString::new("alice");
        session.add_participant(peer.clone());
        assert_eq!(session.participants.len(), 1);

        session.add_participant(peer.clone()); // Should not add duplicate
        assert_eq!(session.participants.len(), 1);
    }

    #[test]
    fn test_quality_metrics() {
        let good = CallQualityMetrics {
            rtt_ms: 50,
            packet_loss_percent: 0.5,
            jitter_ms: 10,
            bandwidth_kbps: 1000,
            timestamp: Utc::now(),
        };
        assert!(good.is_good_quality());
        assert!(!good.needs_adaptation());

        let bad = CallQualityMetrics {
            rtt_ms: 300,
            packet_loss_percent: 5.0,
            jitter_ms: 50,
            bandwidth_kbps: 200,
            timestamp: Utc::now(),
        };
        assert!(!bad.is_good_quality());
        assert!(bad.needs_adaptation());
    }

    #[test]
    fn test_video_resolution() {
        let hd720 = VideoResolution::HD720;
        assert_eq!(hd720.width(), 1280);
        assert_eq!(hd720.height(), 720);

        let hd1080 = VideoResolution::HD1080;
        assert_eq!(hd1080.width(), 1920);
        assert_eq!(hd1080.height(), 1080);
    }
}