rtci-media 0.1.0

Media samples and container readers and writers (IVF, Ogg Opus, H.264 / H.265 Annex B) for WebRTC
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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
#[cfg(test)]
mod sample_builder_frame_start_test;
#[cfg(test)]
mod sample_builder_gap_wait_test;
#[cfg(test)]
mod sample_builder_h26x_test;
#[cfg(test)]
mod sample_builder_test;
#[cfg(test)]
mod sample_sequence_location_test;

/// Tracks where a sample sits within the RTP sequence-number space.
pub mod sample_sequence_location;

use self::sample_sequence_location::{Comparison, SampleSequenceLocation};
use crate::Sample;
use bytes::Bytes;
use rtp::Packet;
use rtp::packetizer::Depacketizer;
use std::time::{Duration, Instant};

/// SampleBuilder buffers packets until media frames are complete.
pub struct SampleBuilder<T: Depacketizer> {
    /// how many packets to wait until we get a valid Sample
    max_late: u16,
    /// max timestamp between old and new timestamps before dropping packets
    max_late_timestamp: u32,
    buffer: Vec<Option<Packet>>,
    prepared_samples: Vec<Option<Sample>>,
    last_sample_timestamp: Option<u32>,

    /// Interface that allows us to take RTP packets to samples
    depacketizer: T,

    /// sample_rate allows us to compute duration of media.SamplecA
    sample_rate: u32,

    /// filled contains the head/tail of the packets inserted into the buffer
    filled: SampleSequenceLocation,

    /// active contains the active head/tail of the timestamp being actively processed
    active: SampleSequenceLocation,

    /// prepared contains the samples that have been processed to date
    prepared: SampleSequenceLocation,

    /// number of packets forced to be dropped
    dropped_packets: u16,

    /// number of padding packets detected and dropped. This number will be a subset of
    /// `dropped_packets`
    padding_packets: u16,

    /// Whether any packet has been consumed (built into a sample or dropped). Until then the
    /// segment being built starts at the earliest packet that has arrived, not at the first
    /// one to arrive: packets of the first access unit may come out of order.
    consumed_any: bool,

    /// How long a gap may stay missing before it is given up ([`Self::with_max_gap_wait`]);
    /// `None`: no bound in time.
    max_gap_wait: Option<Duration>,

    /// When each buffered packet arrived, by sequence number: allocated only with a gap bound,
    /// for when a gap became known (the arrival of the packet after it).
    arrivals: Vec<Option<Instant>>,

    /// Whether a gap was given up since the last [`Self::take_key_frame_request`].
    key_frame_requested: bool,

    /// How many gaps the time bound gave up.
    skipped_gaps: u64,
}

impl<T: Depacketizer> SampleBuilder<T> {
    /// Constructs a new SampleBuilder.
    /// `max_late` is how long to wait until we can construct a completed [`Sample`].
    /// `max_late` is measured in RTP packet sequence numbers.
    /// A large max_late will result in less packet loss but higher latency.
    /// The depacketizer extracts media samples from RTP packets.
    pub fn new(max_late: u16, depacketizer: T, sample_rate: u32) -> Self {
        Self {
            max_late,
            max_late_timestamp: 0,
            buffer: vec![None; u16::MAX as usize + 1],
            prepared_samples: (0..=u16::MAX as usize).map(|_| None).collect(),
            last_sample_timestamp: None,
            depacketizer,
            sample_rate,
            filled: SampleSequenceLocation::new(),
            active: SampleSequenceLocation::new(),
            prepared: SampleSequenceLocation::new(),
            dropped_packets: 0,
            padding_packets: 0,
            consumed_any: false,
            max_gap_wait: None,
            arrivals: Vec::new(),
            key_frame_requested: false,
            skipped_gaps: 0,
        }
    }

    /// Sets how long a missing packet is waited for, in wall-clock time, before the builder
    /// gives up on it. Off by default: without it a gap is held until `max_late` packets (or
    /// [`Self::with_max_time_delay`] of media time) have arrived behind it.
    ///
    /// A receiver without a time bound freezes for the length of its packet window on a gap
    /// that never fills: the window is a time only at one bit rate, and 256 packets were 8.6 s
    /// at 100 kbit/s in `tests/capacity_cap_video_rtc2rtc.rs`. Set this to about the repair
    /// window (a few round-trip times, no more than the playout delay accepted); see
    /// `docs/receiver-gap-policy.md`.
    ///
    /// The wait for the oldest missing packet runs from when the builder first knew of the gap:
    /// the arrival of the first packet after it. Once it has lasted the bound, the gap is given
    /// up as `max_late` gives it up: the samples complete before it are built, the incomplete
    /// one and the missing packets are dropped, and building resumes at the next packet that
    /// starts a sample ([`Depacketizer::is_partition_head`]). The [`Depacketizer`] cannot tell
    /// a key frame from another, so that sample may be one that refers to what was lost: the
    /// builder raises a key-frame request ([`Self::take_key_frame_request`]) for the
    /// application to send (PLI, RFC 4585 section 6.3.1). The next sample counts the packets
    /// skipped in [`Sample::prev_dropped_packets`]. A gap filled before the bound changes
    /// nothing.
    ///
    /// The bound is checked when [`Self::push`] and [`Self::pop`] are called, with the `now`
    /// they are given: nothing here reads a clock. While a gap waits, [`Self::poll_timeout`]
    /// names the instant to call [`Self::pop`] at if no packet arrives first.
    pub fn with_max_gap_wait(mut self, max_gap_wait: Duration) -> Self {
        self.max_gap_wait = Some(max_gap_wait);
        if self.arrivals.is_empty() {
            self.arrivals = vec![None; u16::MAX as usize + 1];
        }
        self
    }

    /// Whether a gap was given up for the time bound ([`Self::with_max_gap_wait`]) since the
    /// last call: the application should ask the sender for a key frame. Clears the request.
    pub fn take_key_frame_request(&mut self) -> bool {
        std::mem::take(&mut self.key_frame_requested)
    }

    /// How many gaps the time bound ([`Self::with_max_gap_wait`]) gave up.
    pub fn skipped_gaps(&self) -> u64 {
        self.skipped_gaps
    }

    /// When the oldest missing packet will have been waited for as long as
    /// [`Self::with_max_gap_wait`] allows: the instant to call [`Self::pop`] at if nothing
    /// arrives first. `None` without a bound or with no gap.
    pub fn poll_timeout(&self) -> Option<Instant> {
        let bound = self.max_gap_wait?;
        self.oldest_gap().map(|(_, known)| known + bound)
    }

    /// The oldest missing packet among those buffered, and when the builder learned of it
    /// (the arrival of the first packet after it). Only tracked with a gap bound.
    fn oldest_gap(&self) -> Option<(u16, Instant)> {
        if self.arrivals.is_empty() || !self.filled.has_data() {
            return None;
        }
        let mut i = self.filled.head;
        while i != self.filled.tail && self.buffer[i as usize].is_some() {
            i = i.wrapping_add(1);
        }
        if i == self.filled.tail {
            return None;
        }
        let missing = i;
        while i != self.filled.tail && self.buffer[i as usize].is_none() {
            i = i.wrapping_add(1);
        }
        // `filled.tail - 1` always holds the newest packet, so there is one after the gap.
        let known = self.arrivals.get(i as usize).copied().flatten()?;
        Some((missing, known))
    }

    /// Gives up every gap that has been missing for the bound at `now`, as `max_late` does.
    fn give_up_gaps(&mut self, now: Instant) {
        let Some(bound) = self.max_gap_wait else {
            return;
        };
        while let Some((missing, known)) = self.oldest_gap() {
            if now < known + bound {
                break;
            }
            while self.filled.has_data() && self.filled.compare(missing) == Comparison::Inside {
                self.drop_head(now);
            }
            self.skipped_gaps += 1;
            self.key_frame_requested = true;
        }
    }

    /// Sets how long to wait for a missing packet before giving up on the sample it belongs to.
    ///
    /// Bounds head-of-line blocking: without it a single lost packet would stall reassembly
    /// indefinitely. A bound in packets (`max_late`) is a bound in time only at one bit rate (256
    /// packets are 0.3 s at 2.5 Mbit/s and 8.6 s at 100 kbit/s): a receiver should bound the wait
    /// in time, about the NACK repair window, and ask for a key frame when a sample after a gap
    /// reports [`Sample::prev_dropped_packets`]. See `docs/receiver-gap-policy.md`.
    pub fn with_max_time_delay(mut self, max_late_duration: Duration) -> Self {
        self.max_late_timestamp =
            (self.sample_rate as u128 * max_late_duration.as_millis() / 1000) as u32;
        self
    }

    fn too_old(&self, location: &SampleSequenceLocation) -> bool {
        if self.max_late_timestamp == 0 {
            return false;
        }

        let mut found_head: Option<u32> = None;
        let mut found_tail: Option<u32> = None;

        let mut i = location.head;
        while i != location.tail {
            if let Some(ref packet) = self.buffer[i as usize] {
                found_head = Some(packet.header.timestamp);
                break;
            }
            i = i.wrapping_add(1);
        }

        if found_head.is_none() {
            return false;
        }

        let mut i = location.tail.wrapping_sub(1);
        while i != location.head {
            if let Some(ref packet) = self.buffer[i as usize] {
                found_tail = Some(packet.header.timestamp);
                break;
            }
            i = i.wrapping_sub(1);
        }

        if found_tail.is_none() {
            return false;
        }

        found_tail.unwrap().wrapping_sub(found_head.unwrap()) > self.max_late_timestamp
    }

    /// Returns the timestamp associated with a given sample location
    fn fetch_timestamp(&self, location: &SampleSequenceLocation) -> Option<u32> {
        if location.empty() {
            None
        } else {
            Some(
                (self.buffer[location.head as usize])
                    .as_ref()?
                    .header
                    .timestamp,
            )
        }
    }

    fn release_packet(&mut self, i: u16) {
        self.buffer[i as usize] = None;
        if let Some(arrival) = self.arrivals.get_mut(i as usize) {
            *arrival = None;
        }
    }

    /// Clears all buffers that have already been consumed by
    /// popping.
    fn purge_consumed_buffers(&mut self) {
        let active = self.active;
        self.purge_consumed_location(&active, false);
    }

    /// Clears all buffers that have already been consumed
    /// during a sample building method.
    fn purge_consumed_location(&mut self, consume: &SampleSequenceLocation, force_consume: bool) {
        if !self.filled.has_data() {
            return;
        }
        match consume.compare(self.filled.head) {
            Comparison::Inside if force_consume => {
                self.release_packet(self.filled.head);
                self.filled.head = self.filled.head.wrapping_add(1);
            }
            Comparison::Before => {
                self.release_packet(self.filled.head);
                self.filled.head = self.filled.head.wrapping_add(1);
            }
            _ => {}
        }
    }

    /// Flushes all buffers that are already consumed or those buffers
    /// that are too late to consume.
    fn purge_buffers(&mut self, now: Instant) {
        self.purge_consumed_buffers();

        while (self.too_old(&self.filled) || (self.filled.count() > self.max_late))
            && self.filled.has_data()
        {
            self.drop_head(now);
        }
    }

    /// Consumes the oldest buffered position whatever is pending: builds the sample it starts
    /// if that can be built now, otherwise drops it (counted in `dropped_packets`).
    fn drop_head(&mut self, now: Instant) {
        if self.active.empty() {
            // refill the active based on the filled packets
            self.active = self.filled;
        }

        if self.active.has_data() && (self.active.head == self.filled.head) {
            // attempt to force the active packet to be consumed even though
            // outstanding data may be pending arrival
            let err = match self.build_sample(now, true) {
                Ok(_) => return,
                Err(e) => e,
            };

            if !matches!(err, BuildError::InvalidPartition(_)) {
                // In the InvalidPartition case `build_sample` will have already adjusted `dropped_packets`.
                self.dropped_packets = self.dropped_packets.saturating_add(1);
            }

            // could not build the sample so drop it
            self.consumed_any = true;
            self.active.head = self.active.head.wrapping_add(1);
        }

        self.release_packet(self.filled.head);
        self.filled.head = self.filled.head.wrapping_add(1);
    }

    /// Adds an RTP Packet to self's buffer.
    ///
    /// Push does not copy the input. If you wish to reuse
    /// this memory make sure to copy before calling push
    pub fn push(&mut self, now: Instant, p: Packet) {
        let sequence_number = p.header.sequence_number;
        self.buffer[sequence_number as usize] = Some(p);
        if let Some(arrival) = self.arrivals.get_mut(sequence_number as usize) {
            *arrival = Some(now);
        }
        match self.filled.compare(sequence_number) {
            Comparison::Void => {
                self.filled.head = sequence_number;
                self.filled.tail = sequence_number.wrapping_add(1);
            }
            Comparison::Before => {
                self.filled.head = sequence_number;
                // RFC 6184 5.6, 5.8 and RFC 7798 6: NAL units are passed on in RTP
                // sequence-number order. Before anything was consumed there is no earlier
                // sample to be late for: a packet that arrives after a later one of the
                // first sample still belongs in it, so the segment being built starts there.
                if !self.consumed_any && self.active.has_data() {
                    self.active.head = sequence_number;
                }
            }
            Comparison::After => {
                self.filled.tail = sequence_number.wrapping_add(1);
            }
            _ => {}
        }
        self.purge_buffers(now);
        self.give_up_gaps(now);
    }

    /// Creates a sample from a valid collection of RTP Packets by
    /// walking forwards building a sample if everything looks good clear and
    /// update buffer+values
    fn build_sample(
        &mut self,
        now: Instant,
        purging_buffers: bool,
    ) -> Result<SampleSequenceLocation, BuildError> {
        if self.active.empty() {
            self.active = self.filled;
        }

        if self.active.empty() {
            return Err(BuildError::NoActiveSegment);
        }

        if self.filled.compare(self.active.tail) == Comparison::Inside {
            self.active.tail = self.filled.tail;
        }

        let mut consume = SampleSequenceLocation::new();

        let mut i = self.active.head;
        // `self.active` isn't modified in the loop, fetch the timestamp once and cache it.
        let head_timestamp = self.fetch_timestamp(&self.active);
        while let Some(ref packet) = self.buffer[i as usize] {
            if self.active.compare(i) == Comparison::After {
                break;
            }
            let is_same_timestamp = head_timestamp.map(|t| packet.header.timestamp == t);
            let is_different_timestamp = is_same_timestamp.map(std::ops::Not::not);
            let is_partition_tail = self
                .depacketizer
                .is_partition_tail(packet.header.marker, &packet.payload);

            // If the timestamp is not the same it might be because the next packet is both a start
            // and end of the next partition in which case a sample should be generated now. This
            // can happen when padding packets are used .e.g:
            //
            // p1(t=1), p2(t=1), p3(t=1), p4(t=2, marker=true, start=true)
            //
            // In thic case the generated sample should be p1 through p3, but excluding p4 which is
            // its own sample.
            if is_partition_tail && is_same_timestamp.unwrap_or(true) {
                consume.head = self.active.head;
                consume.tail = i.wrapping_add(1);
                break;
            }

            if is_different_timestamp.unwrap_or(false) {
                consume.head = self.active.head;
                consume.tail = i;
                break;
            }
            i = i.wrapping_add(1);
        }

        if consume.empty() {
            return Err(BuildError::NothingToConsume);
        }

        if !purging_buffers && self.buffer[consume.tail as usize].is_none() {
            // wait for the next packet after this set of packets to arrive
            // to ensure at least one post sample timestamp is known
            // (unless we have to release right now)
            return Err(BuildError::PendingTimestampPacket);
        }

        let sample_timestamp = self.fetch_timestamp(&self.active).unwrap_or(0);
        let mut after_timestamp = sample_timestamp;

        // scan for any packet after the current and use that time stamp as the diff point
        for i in consume.tail..self.active.tail {
            if let Some(ref packet) = self.buffer[i as usize] {
                after_timestamp = packet.header.timestamp;
                break;
            }
        }

        // prior to decoding all the packets, check if this packet
        // would end being disposed anyway
        let head_payload = self.buffer[consume.head as usize]
            .as_ref()
            .map(|p| &p.payload)
            .ok_or(BuildError::GapInSegment)?;
        if !self.depacketizer.is_partition_head(head_payload) {
            // libWebRTC will sometimes send several empty padding packets to smooth out send
            // rate. These packets don't carry any media payloads.
            let is_padding = consume.range(&self.buffer).all(|p| {
                p.map(|p| {
                    self.last_sample_timestamp == Some(p.header.timestamp) && p.payload.is_empty()
                })
                .unwrap_or(false)
            });

            self.dropped_packets = self.dropped_packets.saturating_add(consume.count());
            if is_padding {
                self.padding_packets = self.padding_packets.saturating_add(consume.count());
            }
            self.purge_consumed_location(&consume, true);
            self.purge_consumed_buffers();

            self.consumed_any = true;
            self.active.head = consume.tail;
            return Err(BuildError::InvalidPartition(consume));
        }

        // the head set of packets is now fully consumed
        self.consumed_any = true;
        self.active.head = consume.tail;

        // Assemble the sample payload from the consumed packets. For the common
        // single-packet sample (all audio, and any frame that fits one RTP
        // packet) hand back the depacketized `Bytes` directly — it is
        // refcounted, so no copy is needed. Only multi-packet samples require
        // concatenation, and even then `Bytes::from(data)` reuses the `Vec`'s
        // buffer instead of copying it a second time.
        let sample_data: Bytes = if consume.count() == 1 {
            let payload = self.buffer[consume.head as usize]
                .as_ref()
                .map(|p| &p.payload)
                .ok_or(BuildError::GapInSegment)?;
            self.depacketizer
                .depacketize(payload)
                .map_err(|_| BuildError::DepacketizerFailed)?
        } else {
            let mut data: Vec<u8> = Vec::new();
            let mut i = consume.head;
            while i != consume.tail {
                let payload = self.buffer[i as usize]
                    .as_ref()
                    .map(|p| &p.payload)
                    .ok_or(BuildError::GapInSegment)?;

                let p = self
                    .depacketizer
                    .depacketize(payload)
                    .map_err(|_| BuildError::DepacketizerFailed)?;

                data.extend_from_slice(&p);
                i = i.wrapping_add(1);
            }
            Bytes::from(data)
        };
        let samples = after_timestamp.wrapping_sub(sample_timestamp);

        let sample = Sample {
            data: sample_data,
            timestamp: now,
            duration: Duration::from_secs_f64((samples as f64) / (self.sample_rate as f64)),
            packet_timestamp: sample_timestamp,
            prev_dropped_packets: self.dropped_packets,
            prev_padding_packets: self.padding_packets,
        };

        self.dropped_packets = 0;
        self.padding_packets = 0;
        self.last_sample_timestamp = Some(sample_timestamp);

        self.prepared_samples[self.prepared.tail as usize] = Some(sample);
        self.prepared.tail = self.prepared.tail.wrapping_add(1);

        self.purge_consumed_location(&consume, true);
        self.purge_consumed_buffers();

        Ok(consume)
    }

    /// Compiles pushed RTP packets into media samples and then
    /// returns the next valid sample (or None if no sample is compiled).
    pub fn pop(&mut self, now: Instant) -> Option<Sample> {
        self.give_up_gaps(now);
        let _ = self.build_sample(now, false);

        if self.prepared.empty() {
            return None;
        }
        let result = self.prepared_samples[self.prepared.head as usize].take();
        self.prepared.head = self.prepared.head.wrapping_add(1);
        result
    }

    /// Compiles pushed RTP packets into media samples and then
    /// returns the next valid sample with its associated RTP timestamp (or `None` if
    /// no sample is compiled).
    pub fn pop_with_timestamp(&mut self, now: Instant) -> Option<(Sample, u32)> {
        if let Some(sample) = self.pop(now) {
            let timestamp = sample.packet_timestamp;
            Some((sample, timestamp))
        } else {
            None
        }
    }
}

// Computes the distance between two sequence numbers
/*pub(crate) fn seqnum_distance(head: u16, tail: u16) -> u16 {
    if head > tail {
        head.wrapping_add(tail)
    } else {
        tail - head
    }
}*/

pub(crate) fn seqnum_distance(x: u16, y: u16) -> u16 {
    let diff = x.wrapping_sub(y);
    if diff > 0xFFFF / 2 {
        0xFFFF - diff + 1
    } else {
        diff
    }
}

#[derive(Debug)]
enum BuildError {
    /// There's no active segment of RTP packets to consider yet.
    NoActiveSegment,

    /// No sample partition could be found in the active segment.
    NothingToConsume,

    /// A segment to consume was identified, but a subsequent packet is needed to determine the
    /// duration of the sample.
    PendingTimestampPacket,

    /// The active segment's head was not aligned with a sample partition head. Some packets were
    /// dropped.
    InvalidPartition(SampleSequenceLocation),

    /// There was a gap in the active segment because of one or more missing RTP packets.
    GapInSegment,

    /// We failed to depacketize an RTP packet.
    DepacketizerFailed,
}