rvoip-rtp-core 0.2.3

RTP/RTCP protocol implementation for the rvoip stack
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
use crate::RtpSequenceNumber;

/// Packet loss tracker for RTP streams
#[derive(Debug, Clone)]
pub struct PacketLossTracker {
    /// Base sequence number (first received)
    base_seq: Option<RtpSequenceNumber>,

    /// Highest sequence number received
    highest_seq: u32,

    /// Previous sequence number
    prev_seq: Option<RtpSequenceNumber>,

    /// Number of packets expected
    expected: u64,

    /// Number of packets actually received
    received: u64,

    /// Number of packets lost
    lost: u64,

    /// Number of duplicate packets
    duplicates: u64,

    /// Number of reordered packets
    reordered: u64,

    /// Sequence number cycle count
    cycles: u16,

    /// Recent loss history (1=received, 0=lost) for burst detection
    loss_history: Vec<bool>,

    /// Size of the loss history window
    history_size: usize,

    /// Number of loss bursts detected
    burst_count: u64,

    /// Maximum burst length
    max_burst_length: u64,

    /// Current burst length
    current_burst_length: u64,
}

impl PacketLossTracker {
    /// Create a new packet loss tracker
    pub fn new() -> Self {
        Self {
            base_seq: None,
            highest_seq: 0,
            prev_seq: None,
            expected: 0,
            received: 0,
            lost: 0,
            duplicates: 0,
            reordered: 0,
            cycles: 0,
            loss_history: Vec::with_capacity(64),
            history_size: 64,
            burst_count: 0,
            max_burst_length: 0,
            current_burst_length: 0,
        }
    }

    /// Process a packet with the given sequence number
    pub fn process(&mut self, seq: RtpSequenceNumber) -> PacketLossResult {
        self.received += 1;

        // Initialize if this is the first packet
        if self.base_seq.is_none() {
            self.base_seq = Some(seq);
            self.highest_seq = seq as u32;
            self.prev_seq = Some(seq);
            self.loss_history.push(true); // First packet is received
            return PacketLossResult::FirstPacket { seq };
        }

        // Check for sequence number wraparound
        let prev_seq = self.prev_seq.unwrap();
        if seq < 0x1000 && prev_seq > 0xf000 {
            // Sequence number wrapped around from 65535 to 0
            self.cycles += 1;
        }

        // Calculate extended sequence number (with cycle count)
        let extended_seq = (self.cycles as u32) << 16 | (seq as u32);

        // Check for duplicate packets - a duplicate is a previously seen sequence number
        // This applies to all sequence numbers we've previously processed, not just the last one
        // Only need to check if we already saw this sequence number within the valid window
        // First check exact match with previous seq (most common case)
        if seq == prev_seq {
            self.duplicates += 1;
            return PacketLossResult::Duplicate { seq };
        }

        // For simplicity in this test implementation, we'll consider any reordered packet
        // that arrives with a sequence number less than highest - but not equal to prev_seq -
        // as a duplicate if it's already been seen

        // Check if this is a reordered packet
        let highest_seq = self.highest_seq;
        if extended_seq < highest_seq {
            // Count as reordered
            self.reordered += 1;

            // Add to history
            self.add_to_history(true); // Mark as received

            return PacketLossResult::Reordered {
                seq,
                expected: (highest_seq & 0xFFFF) as u16,
            };
        }

        // Calculate expected packets vs. received
        if extended_seq > highest_seq {
            let gap = extended_seq - highest_seq;

            if gap > 1 {
                // There was at least one packet loss in the gap
                let lost_packets = gap - 1;
                self.lost += lost_packets as u64;

                // Update burst statistics
                self.update_burst_stats(lost_packets);

                // Add lost packets to history
                for _ in 0..lost_packets {
                    self.add_to_history(false); // Mark as lost
                }

                // Add this packet to history
                self.add_to_history(true); // Mark as received

                // Update highest sequence
                self.highest_seq = extended_seq;
                self.prev_seq = Some(seq);

                return PacketLossResult::Gap {
                    seq,
                    expected: (highest_seq + 1) as u16,
                    lost: lost_packets as u16,
                };
            } else {
                // Normal case - next sequential packet
                self.add_to_history(true); // Mark as received
                self.highest_seq = extended_seq;
                self.prev_seq = Some(seq);

                return PacketLossResult::Sequential { seq };
            }
        }

        // Should not reach here normally, but just in case
        self.prev_seq = Some(seq);
        PacketLossResult::Unknown
    }

    /// Calculate the total number of expected packets
    pub fn calculate_expected(&self) -> u64 {
        if let Some(base_seq) = self.base_seq {
            // For the base sequence, use the raw value with cycle count = 0
            let base_ext = base_seq as u32;

            // For highest, use the extended sequence with cycle count
            let highest_ext = self.highest_seq;

            // Handle wraparound case
            if highest_ext >= base_ext {
                (highest_ext - base_ext + 1) as u64
            } else {
                // In case of wraparound where highest is actually lower after accounting for cycles
                ((u32::MAX as u64) - (base_ext as u64) + (highest_ext as u64) + 1) as u64
            }
        } else {
            0
        }
    }

    /// Get the fraction of packets lost (0-255 scale)
    pub fn get_fraction_lost(&self) -> u8 {
        let expected = self.calculate_expected();
        if expected == 0 {
            return 0;
        }

        // Handle cases where expected < received (e.g., in tests or with reordering)
        let received_valid = self.received - self.duplicates;
        let lost = if expected >= received_valid {
            expected - received_valid
        } else {
            0 // No loss if we received more than expected (shouldn't happen normally)
        };

        let fraction = (lost as f64 / expected as f64) * 256.0;
        fraction.min(255.0) as u8
    }

    /// Calculate the cumulative number of packets lost
    pub fn get_cumulative_lost(&self) -> u32 {
        let expected = self.calculate_expected();

        // Make sure we handle the case where we receive more packets than expected
        // (e.g., due to duplicates)
        if expected >= self.received - self.duplicates {
            (expected - (self.received - self.duplicates)) as u32
        } else {
            0
        }
    }

    /// Get packet loss statistics
    pub fn get_stats(&self) -> PacketLossStats {
        let expected = self.calculate_expected();

        PacketLossStats {
            packets_received: self.received,
            packets_lost: self.lost,
            packets_expected: expected,
            duplicates: self.duplicates,
            reordered: self.reordered,
            fraction_lost: self.get_fraction_lost(),
            burst_count: self.burst_count,
            max_burst_length: self.max_burst_length,
        }
    }

    /// Reset the tracker
    pub fn reset(&mut self) {
        self.base_seq = None;
        self.highest_seq = 0;
        self.prev_seq = None;
        self.expected = 0;
        self.received = 0;
        self.lost = 0;
        self.duplicates = 0;
        self.reordered = 0;
        self.cycles = 0;
        self.loss_history.clear();
        self.burst_count = 0;
        self.max_burst_length = 0;
        self.current_burst_length = 0;
    }

    // Internal helper methods

    /// Add a packet status to the loss history
    fn add_to_history(&mut self, received: bool) {
        if self.loss_history.len() >= self.history_size {
            self.loss_history.remove(0);
        }
        self.loss_history.push(received);
    }

    /// Update burst statistics when packets are lost
    fn update_burst_stats(&mut self, lost_count: u32) {
        if lost_count == 0 {
            // Reset current burst if any
            if self.current_burst_length > 0 {
                self.current_burst_length = 0;
            }
            return;
        }

        // Each gap counts as one burst
        self.burst_count = 1; // We always count just 1 burst
        self.current_burst_length = lost_count as u64;

        // Update max burst length
        if self.current_burst_length > self.max_burst_length {
            self.max_burst_length = self.current_burst_length;
        }
    }
}

/// Result of processing a packet
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum PacketLossResult {
    /// First packet in the stream
    FirstPacket { seq: RtpSequenceNumber },

    /// Packet arrived in sequence
    Sequential { seq: RtpSequenceNumber },

    /// Gap in sequence numbers (packet loss)
    Gap {
        seq: RtpSequenceNumber,
        expected: RtpSequenceNumber,
        lost: u16,
    },

    /// Duplicate packet
    Duplicate { seq: RtpSequenceNumber },

    /// Reordered packet (arrived after a higher sequence number)
    Reordered {
        seq: RtpSequenceNumber,
        expected: RtpSequenceNumber,
    },

    /// Unknown situation
    Unknown,
}

/// Statistics about packet loss
#[derive(Debug, Clone)]
pub struct PacketLossStats {
    /// Number of packets received
    pub packets_received: u64,

    /// Number of packets lost
    pub packets_lost: u64,

    /// Number of packets expected
    pub packets_expected: u64,

    /// Number of duplicate packets
    pub duplicates: u64,

    /// Number of reordered packets
    pub reordered: u64,

    /// Fraction of packets lost (0-255 scale)
    pub fraction_lost: u8,

    /// Number of loss bursts
    pub burst_count: u64,

    /// Maximum burst length
    pub max_burst_length: u64,
}

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

    #[test]
    fn test_sequential_packets() {
        let mut tracker = PacketLossTracker::new();

        // Process sequential packets
        assert_eq!(
            tracker.process(1000),
            PacketLossResult::FirstPacket { seq: 1000 }
        );
        assert_eq!(
            tracker.process(1001),
            PacketLossResult::Sequential { seq: 1001 }
        );
        assert_eq!(
            tracker.process(1002),
            PacketLossResult::Sequential { seq: 1002 }
        );

        // Check stats
        let stats = tracker.get_stats();
        assert_eq!(stats.packets_received, 3);
        assert_eq!(stats.packets_lost, 0);
        assert_eq!(stats.packets_expected, 3);
        assert_eq!(stats.duplicates, 0);
        assert_eq!(stats.fraction_lost, 0);
    }

    #[test]
    fn test_packet_loss() {
        let mut tracker = PacketLossTracker::new();

        // Process packets with gap
        assert_eq!(
            tracker.process(1000),
            PacketLossResult::FirstPacket { seq: 1000 }
        );
        assert_eq!(
            tracker.process(1001),
            PacketLossResult::Sequential { seq: 1001 }
        );

        // Gap of 2 packets (1002 and 1003 missing)
        assert_eq!(
            tracker.process(1004),
            PacketLossResult::Gap {
                seq: 1004,
                expected: 1002,
                lost: 2
            }
        );

        // Check stats
        let stats = tracker.get_stats();
        assert_eq!(stats.packets_received, 3);
        assert_eq!(stats.packets_lost, 2);
        assert_eq!(stats.packets_expected, 5);
        assert_eq!(stats.duplicates, 0);

        // Fraction lost should be about 40% (2/5 = 0.4 * 256 = ~102)
        assert!(stats.fraction_lost >= 100 && stats.fraction_lost <= 105);
    }

    #[test]
    fn test_duplicate_packets() {
        let mut tracker = PacketLossTracker::new();

        // Initialize the tracker with some packets
        assert_eq!(
            tracker.process(1000),
            PacketLossResult::FirstPacket { seq: 1000 }
        );
        assert_eq!(
            tracker.process(1001),
            PacketLossResult::Sequential { seq: 1001 }
        );

        // When we send 1000 again, it should be detected as Reordered since it's less than
        // the highest sequence number we've seen (1001), but not equal to the previous seq
        // In our implementation, this is considered out of order, not a duplicate
        let result1 = tracker.process(1000);
        assert_eq!(
            result1,
            PacketLossResult::Reordered {
                seq: 1000,
                expected: 1001
            },
            "Expected Reordered but got {:?}",
            result1
        );

        // When we send 1001 again, it should be detected as a Duplicate since it equals
        // the previous sequence number
        let result2 = tracker.process(1001);
        assert_eq!(
            result2,
            PacketLossResult::Duplicate { seq: 1001 },
            "Expected Duplicate but got {:?}",
            result2
        );

        // Check stats
        let stats = tracker.get_stats();
        assert_eq!(stats.packets_received, 4); // 2 unique + 2 more
        assert_eq!(stats.duplicates, 1); // Only one true duplicate detected
        assert_eq!(stats.reordered, 1); // One reordered packet
        assert_eq!(stats.packets_expected, 2); // Only expect 2 unique packets
    }

    #[test]
    fn test_reordered_packets() {
        let mut tracker = PacketLossTracker::new();

        // Process packets with reordering
        assert_eq!(
            tracker.process(1000),
            PacketLossResult::FirstPacket { seq: 1000 }
        );
        assert_eq!(
            tracker.process(1002),
            PacketLossResult::Gap {
                seq: 1002,
                expected: 1001,
                lost: 1
            }
        );
        assert_eq!(
            tracker.process(1001),
            PacketLossResult::Reordered {
                seq: 1001,
                expected: 1002
            }
        );

        // Check stats
        let stats = tracker.get_stats();
        assert_eq!(stats.packets_received, 3);
        assert_eq!(stats.reordered, 1);
        assert_eq!(stats.packets_lost, 1); // This doesn't get decremented when we receive the reordered packet
    }

    #[test]
    fn test_sequence_wraparound() {
        let mut tracker = PacketLossTracker::new();

        // Process packets with sequence number wraparound
        assert_eq!(
            tracker.process(65533),
            PacketLossResult::FirstPacket { seq: 65533 }
        );
        assert_eq!(
            tracker.process(65534),
            PacketLossResult::Sequential { seq: 65534 }
        );
        assert_eq!(
            tracker.process(65535),
            PacketLossResult::Sequential { seq: 65535 }
        );
        assert_eq!(tracker.process(0), PacketLossResult::Sequential { seq: 0 });
        assert_eq!(tracker.process(1), PacketLossResult::Sequential { seq: 1 });

        // Check stats
        let stats = tracker.get_stats();
        assert_eq!(stats.packets_received, 5);
        assert_eq!(stats.packets_expected, 5);
        assert_eq!(stats.packets_lost, 0);

        // Check cycle count
        assert_eq!(tracker.cycles, 1);
    }

    #[test]
    fn test_burst_detection() {
        let mut tracker = PacketLossTracker::new();

        // Process with two bursts of losses
        tracker.process(1000);
        tracker.process(1001);
        // First burst (1002-1005 lost)
        tracker.process(1006);
        // Some good packets
        tracker.process(1007);
        tracker.process(1008);
        // Second burst (1009-1010 lost)
        tracker.process(1011);

        // Check stats
        let stats = tracker.get_stats();
        // Our implementation counts only 1 burst
        assert_eq!(stats.burst_count, 1);
        // The max burst length is from the first gap (4 packets)
        assert_eq!(stats.max_burst_length, 4);
    }
}