oximedia-net 0.1.2

Network streaming for OxiMedia
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
//! SRT connection monitoring and quality metrics.
//!
//! Provides utilities for monitoring connection quality and bandwidth.

use std::collections::VecDeque;
use std::time::{Duration, Instant};

/// Bandwidth estimator using sliding window.
#[derive(Debug)]
pub struct BandwidthEstimator {
    /// Sliding window of samples.
    samples: VecDeque<BandwidthSample>,
    /// Window duration.
    window_duration: Duration,
    /// Total bytes in window.
    total_bytes: u64,
}

/// A single bandwidth sample.
#[derive(Debug, Clone)]
struct BandwidthSample {
    /// Timestamp of the sample.
    timestamp: Instant,
    /// Bytes transferred.
    bytes: u64,
}

impl BandwidthEstimator {
    /// Creates a new bandwidth estimator.
    #[must_use]
    pub fn new(window_duration: Duration) -> Self {
        Self {
            samples: VecDeque::new(),
            window_duration,
            total_bytes: 0,
        }
    }

    /// Records a new sample.
    pub fn record(&mut self, bytes: u64) {
        let now = Instant::now();

        // Remove old samples outside the window
        while let Some(sample) = self.samples.front() {
            if now.duration_since(sample.timestamp) > self.window_duration {
                let old = self
                    .samples
                    .pop_front()
                    .expect("invariant: front exists (just checked with while let Some)");
                self.total_bytes = self.total_bytes.saturating_sub(old.bytes);
            } else {
                break;
            }
        }

        // Add new sample
        self.samples.push_back(BandwidthSample {
            timestamp: now,
            bytes,
        });
        self.total_bytes += bytes;
    }

    /// Returns current bandwidth estimate in bytes per second.
    #[must_use]
    pub fn estimate(&self) -> f64 {
        if self.samples.is_empty() {
            return 0.0;
        }

        let oldest = self
            .samples
            .front()
            .expect("invariant: samples non-empty (checked above)")
            .timestamp;
        let duration = Instant::now().duration_since(oldest).as_secs_f64();

        if duration > 0.0 {
            self.total_bytes as f64 / duration
        } else {
            0.0
        }
    }

    /// Returns bandwidth in megabits per second.
    #[must_use]
    pub fn mbps(&self) -> f64 {
        (self.estimate() * 8.0) / 1_000_000.0
    }

    /// Clears all samples.
    pub fn reset(&mut self) {
        self.samples.clear();
        self.total_bytes = 0;
    }
}

/// Connection quality metrics.
#[derive(Debug, Clone, Default)]
pub struct QualityMetrics {
    /// Current RTT in microseconds.
    pub rtt: u32,
    /// RTT variance in microseconds.
    pub rtt_var: u32,
    /// Packet loss rate (0.0 to 1.0).
    pub loss_rate: f64,
    /// Available bandwidth estimate (bytes/sec).
    pub bandwidth: f64,
    /// Send buffer utilization (0.0 to 1.0).
    pub send_buffer_util: f64,
    /// Receive buffer utilization (0.0 to 1.0).
    pub recv_buffer_util: f64,
    /// Number of retransmissions.
    pub retransmit_count: u64,
    /// Jitter in microseconds.
    pub jitter: u32,
}

impl QualityMetrics {
    /// Creates new quality metrics.
    #[must_use]
    pub const fn new() -> Self {
        Self {
            rtt: 0,
            rtt_var: 0,
            loss_rate: 0.0,
            bandwidth: 0.0,
            send_buffer_util: 0.0,
            recv_buffer_util: 0.0,
            retransmit_count: 0,
            jitter: 0,
        }
    }

    /// Returns true if quality is good (low loss, low RTT).
    #[must_use]
    pub const fn is_good(&self) -> bool {
        self.loss_rate < 0.01 && self.rtt < 100_000
    }

    /// Returns true if quality is degraded.
    #[must_use]
    pub const fn is_degraded(&self) -> bool {
        self.loss_rate > 0.05 || self.rtt > 500_000
    }

    /// Returns a quality score (0.0 to 100.0).
    #[must_use]
    pub fn quality_score(&self) -> f64 {
        let mut score = 100.0;

        // Penalize for packet loss
        score -= self.loss_rate * 1000.0;

        // Penalize for high RTT
        let rtt_ms = self.rtt as f64 / 1000.0;
        if rtt_ms > 50.0 {
            score -= (rtt_ms - 50.0) * 0.5;
        }

        // Penalize for high jitter
        let jitter_ms = self.jitter as f64 / 1000.0;
        if jitter_ms > 10.0 {
            score -= (jitter_ms - 10.0) * 0.3;
        }

        score.clamp(0.0, 100.0)
    }
}

/// Jitter calculator using RFC 3550 algorithm.
#[derive(Debug)]
pub struct JitterCalculator {
    /// Last arrival time.
    last_arrival: Option<Instant>,
    /// Last RTP timestamp.
    last_timestamp: u32,
    /// Current jitter estimate (microseconds).
    jitter: f64,
}

impl JitterCalculator {
    /// Creates a new jitter calculator.
    #[must_use]
    pub const fn new() -> Self {
        Self {
            last_arrival: None,
            last_timestamp: 0,
            jitter: 0.0,
        }
    }

    /// Updates jitter estimate with a new packet.
    pub fn update(&mut self, arrival: Instant, timestamp: u32) {
        if let Some(last_arrival) = self.last_arrival {
            let arrival_delta = arrival.duration_since(last_arrival).as_micros() as i64;
            let timestamp_delta = timestamp.wrapping_sub(self.last_timestamp) as i64;

            let delta = arrival_delta - timestamp_delta;
            let abs_delta = delta.unsigned_abs() as f64;

            // J = J + (|D| - J) / 16
            self.jitter += (abs_delta - self.jitter) / 16.0;
        }

        self.last_arrival = Some(arrival);
        self.last_timestamp = timestamp;
    }

    /// Returns current jitter estimate in microseconds.
    #[must_use]
    pub const fn jitter(&self) -> u32 {
        self.jitter as u32
    }

    /// Resets the jitter calculator.
    pub fn reset(&mut self) {
        self.last_arrival = None;
        self.last_timestamp = 0;
        self.jitter = 0.0;
    }
}

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

/// Loss rate estimator.
#[derive(Debug)]
pub struct LossRateEstimator {
    /// Total packets expected.
    total_expected: u64,
    /// Total packets lost.
    total_lost: u64,
    /// Recent window of samples.
    recent_samples: VecDeque<(u64, u64)>, // (expected, lost)
    /// Window size.
    window_size: usize,
}

impl LossRateEstimator {
    /// Creates a new loss rate estimator.
    #[must_use]
    pub fn new(window_size: usize) -> Self {
        Self {
            total_expected: 0,
            total_lost: 0,
            recent_samples: VecDeque::with_capacity(window_size),
            window_size,
        }
    }

    /// Records packet statistics.
    pub fn record(&mut self, expected: u64, lost: u64) {
        self.total_expected += expected;
        self.total_lost += lost;

        self.recent_samples.push_back((expected, lost));

        if self.recent_samples.len() > self.window_size {
            self.recent_samples.pop_front();
        }
    }

    /// Returns overall loss rate.
    #[must_use]
    pub fn overall_loss_rate(&self) -> f64 {
        if self.total_expected == 0 {
            0.0
        } else {
            self.total_lost as f64 / self.total_expected as f64
        }
    }

    /// Returns recent loss rate (within window).
    #[must_use]
    pub fn recent_loss_rate(&self) -> f64 {
        let (total_exp, total_lost): (u64, u64) = self
            .recent_samples
            .iter()
            .fold((0, 0), |(exp, lost), &(e, l)| (exp + e, lost + l));

        if total_exp == 0 {
            0.0
        } else {
            total_lost as f64 / total_exp as f64
        }
    }

    /// Resets statistics.
    pub fn reset(&mut self) {
        self.total_expected = 0;
        self.total_lost = 0;
        self.recent_samples.clear();
    }
}

/// Connection monitor that aggregates all metrics.
#[derive(Debug)]
pub struct ConnectionMonitor {
    /// Bandwidth estimator.
    bandwidth: BandwidthEstimator,
    /// Jitter calculator.
    jitter: JitterCalculator,
    /// Loss rate estimator.
    loss_rate: LossRateEstimator,
    /// Last update time.
    last_update: Instant,
}

impl ConnectionMonitor {
    /// Creates a new connection monitor.
    #[must_use]
    pub fn new() -> Self {
        Self {
            bandwidth: BandwidthEstimator::new(Duration::from_secs(5)),
            jitter: JitterCalculator::new(),
            loss_rate: LossRateEstimator::new(100),
            last_update: Instant::now(),
        }
    }

    /// Records sent data.
    pub fn record_send(&mut self, bytes: u64) {
        self.bandwidth.record(bytes);
        self.last_update = Instant::now();
    }

    /// Records received packet.
    pub fn record_receive(&mut self, bytes: u64, timestamp: u32) {
        self.bandwidth.record(bytes);
        self.jitter.update(Instant::now(), timestamp);
        self.last_update = Instant::now();
    }

    /// Records packet loss.
    pub fn record_loss(&mut self, expected: u64, lost: u64) {
        self.loss_rate.record(expected, lost);
    }

    /// Returns current quality metrics.
    #[must_use]
    pub fn metrics(&self, rtt: u32, rtt_var: u32, retransmit_count: u64) -> QualityMetrics {
        QualityMetrics {
            rtt,
            rtt_var,
            loss_rate: self.loss_rate.recent_loss_rate(),
            bandwidth: self.bandwidth.estimate(),
            send_buffer_util: 0.0, // Would be set from external stats
            recv_buffer_util: 0.0,
            retransmit_count,
            jitter: self.jitter.jitter(),
        }
    }

    /// Returns bandwidth in megabits per second.
    #[must_use]
    pub fn bandwidth_mbps(&self) -> f64 {
        self.bandwidth.mbps()
    }

    /// Resets all statistics.
    pub fn reset(&mut self) {
        self.bandwidth.reset();
        self.jitter.reset();
        self.loss_rate.reset();
        self.last_update = Instant::now();
    }

    /// Returns time since last update.
    #[must_use]
    pub fn time_since_update(&self) -> Duration {
        self.last_update.elapsed()
    }
}

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

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

    #[test]
    fn test_bandwidth_estimator() {
        let mut est = BandwidthEstimator::new(Duration::from_secs(1));
        est.record(1000);
        assert!(est.estimate() > 0.0);
    }

    #[test]
    fn test_quality_metrics_score() {
        let metrics = QualityMetrics {
            rtt: 50_000,
            loss_rate: 0.0,
            jitter: 5_000,
            ..Default::default()
        };

        let score = metrics.quality_score();
        assert!(score > 90.0);
        assert!(metrics.is_good());
    }

    #[test]
    fn test_quality_metrics_degraded() {
        let metrics = QualityMetrics {
            rtt: 600_000,
            loss_rate: 0.1,
            ..Default::default()
        };

        assert!(metrics.is_degraded());
        assert!(!metrics.is_good());
    }

    #[test]
    fn test_jitter_calculator() {
        let mut calc = JitterCalculator::new();
        let now = Instant::now();

        calc.update(now, 0);
        calc.update(now + Duration::from_millis(20), 20_000);

        assert_eq!(calc.jitter(), 0);
    }

    #[test]
    fn test_loss_rate_estimator() {
        let mut est = LossRateEstimator::new(10);
        est.record(100, 5);
        est.record(100, 3);

        let rate = est.overall_loss_rate();
        assert!((rate - 0.04).abs() < 0.01);
    }

    #[test]
    fn test_connection_monitor() {
        let mut monitor = ConnectionMonitor::new();
        monitor.record_send(1000);
        monitor.record_receive(500, 1000);

        let metrics = monitor.metrics(50_000, 10_000, 0);
        assert_eq!(metrics.rtt, 50_000);
    }
}