rtp-parse 0.2.0

RTP packet library
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
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
use parsely_rs::*;

use super::{rtcp_fb_header::RtcpFbHeader, rtcp_header::RtcpHeader};

const U1_ZERO: u1 = u1::new(0);
const U1_ONE: u1 = u1::new(1);

const U2_ZERO: u2 = u2::new(0);
const U2_ONE: u2 = u2::new(1);
const U2_TWO: u2 = u2::new(2);

/// https://datatracker.ietf.org/doc/html/draft-holmer-rmcat-transport-wide-cc-extensions-01#section-3.1
///  0                   1                   2                   3
///  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// |V=2|P|  FMT=15 |    PT=205     |           length              |
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// |                     SSRC of packet sender                     |
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// |                      SSRC of media source                     |
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// |      base sequence number     |      packet status count      |
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// |                 reference time                | fb pkt. count |
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// |          packet chunk         |         packet chunk          |
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// .                                                               .
/// .                                                               .
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// |         packet chunk          |  recv delta   |  recv delta   |
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// .                                                               .
/// .                                                               .
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
/// |           recv delta          |  recv delta   | zero padding  |
/// +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
///
/// packet status count:  16 bits The number of packets this feedback
///  contains status for, starting with the packet identified
///  by the base sequence number.
///
/// feedback packet count:  8 bits A counter incremented by one for each
///  feedback packet sent.  Used to detect feedback packet
///  losses.
#[derive(Debug)]
pub struct RtcpFbTccPacket {
    pub header: RtcpHeader,
    pub fb_header: RtcpFbHeader,
    pub packet_reports: Vec<PacketReport>,
    pub reference_time: u24,
    pub feedback_packet_count: u8,
}

impl RtcpFbTccPacket {
    pub const FMT: u5 = u5::new(15);

    pub fn payload_length_bytes(&self) -> usize {
        todo!()
    }
}

impl<B: BitBuf> ParselyRead<B> for RtcpFbTccPacket {
    type Ctx = (RtcpHeader, RtcpFbHeader);

    fn read<T: ByteOrder>(buf: &mut B, (header, fb_header): Self::Ctx) -> ParselyResult<Self> {
        let bytes_remaining_start = buf.remaining_bytes();
        let base_seq_num = buf.get_u16::<T>().context("Reading field 'base_seq_num'")?;
        let packet_status_count = buf
            .get_u16::<T>()
            .context("Reading field 'packet_status_count'")?;
        let reference_time = buf
            .get_u24::<T>()
            .context("Reading field 'reference_time'")?;
        let feedback_packet_count = buf
            .get_u8()
            .context("Reading field 'feedback_packet_count'")?;

        let mut num_status_remaining = packet_status_count;
        let mut chunks: Vec<SomePacketStatusChunk> = vec![];
        while num_status_remaining > 0 {
            let chunk = SomePacketStatusChunk::read::<T>(buf, (num_status_remaining as usize,))
                .context("packet status chunk")?;
            num_status_remaining -= chunk.num_symbols();
            chunks.push(chunk);
        }
        let mut packet_reports: Vec<PacketReport> =
            Vec::with_capacity(packet_status_count as usize);
        let mut curr_seq_num = base_seq_num;
        for chunk in &chunks {
            for status_symbol in chunk.iter() {
                match status_symbol.delta_size_bytes() {
                    0 => packet_reports.push(PacketReport::UnreceivedPacket {
                        seq_num: curr_seq_num,
                    }),
                    1 => {
                        let delta_ticks = buf
                            .get_u8()
                            .with_context(|| format!("delta ticks for packet {curr_seq_num}"))?;
                        packet_reports.push(PacketReport::ReceivedPacketSmallDelta {
                            seq_num: curr_seq_num,
                            delta_ticks,
                        })
                    }
                    2 => {
                        let delta_ticks = buf
                            .get_u16::<T>()
                            .with_context(|| format!("delta ticks for packet {curr_seq_num}"))?
                            as i16;
                        packet_reports.push(PacketReport::ReceivedPacketLargeOrNegativeDelta {
                            seq_num: curr_seq_num,
                            delta_ticks,
                        })
                    }
                    delta_size_bytes => bail!("Invalid delta size: {delta_size_bytes} bytes"),
                }
                curr_seq_num = curr_seq_num.wrapping_add(1);
            }
        }
        while (bytes_remaining_start - buf.remaining_bytes()) % 4 != 0 {
            let _ = buf.get_u8().context("padding")?;
        }
        Ok(RtcpFbTccPacket {
            header,
            fb_header,
            packet_reports,
            reference_time,
            feedback_packet_count,
        })
    }
}

impl<B: BitBufMut> ParselyWrite<B> for RtcpFbTccPacket {
    type Ctx = ();

    fn write<T: ByteOrder>(&self, buf: &mut B, _ctx: Self::Ctx) -> ParselyResult<()> {
        self.header.write::<T>(buf, ()).context("header")?;
        self.fb_header.write::<T>(buf, ()).context("fb header")?;

        if self.packet_reports.is_empty() {
            return Ok(());
        }
        let base_seq_num = self.packet_reports[0].seq_num();
        let packet_status_count = self.packet_reports.len() as u16;
        buf.put_u16::<T>(base_seq_num).context("base_seq_num")?;
        buf.put_u16::<T>(packet_status_count)
            .context("packet_status_count")?;
        buf.put_u24::<T>(self.reference_time)
            .context("reference_time")?;
        buf.put_u8(self.feedback_packet_count)
            .context("feedback_packet_count")?;
        todo!()
    }
}

impl StateSync for RtcpFbTccPacket {
    type SyncCtx = ();

    fn sync(&mut self, _sync_ctx: Self::SyncCtx) -> ParselyResult<()> {
        self.header
            .sync((self.payload_length_bytes() as u16, Self::FMT))?;

        self.packet_reports.sort_by_key(|pr| pr.seq_num());

        Ok(())
    }
}

// #[derive(Debug)]
// enum SomeRecvDelta {
//     Small(u8),
//     LargeOrNegative(i16),
// }

// fn write_some_recv_delta<B: BitBufMut>(buf: &mut B, delta: SomeRecvDelta) -> Result<()> {
//     match delta {
//         SomeRecvDelta::Small(d) => Ok(buf.write_u8(d)?),
//         // TODO: need support for writing a signed int here
//         SomeRecvDelta::LargeOrNegative(d) => Ok(buf.write_u16::<NetworkOrder>(d as u16)?),
//     }
// }
//
// fn write_rtcp_fb_tcc<B: BitBufMut>(buf: &mut B, fb_tcc: &RtcpFbTccPacket) -> Result<()> {
//     write_rtcp_header(buf, &fb_tcc.header).context("rtcp header")?;
//     write_rtcp_fb_header(buf, &fb_tcc.fb_header).context("fb header")?;
//
//     write_rtcp_fb_tcc_data(buf, &fb_tcc.packet_reports, fb_tcc.reference_time)
//         .context("fb tcc data")?;
//
//     Ok(())
// }
//
// /// Write the FB TCC packet data.  Note that `packet_reports` should be a _continuous_ set of
// /// reports: all NotReceived values should have already been inserted.
// fn write_rtcp_fb_tcc_data<B: BitBufMut>(
//     buf: &mut B,
//     packet_reports: &[PacketReport],
//     reference_time: u24,
// ) -> Result<()> {
//     let base_seq_num = packet_reports[0].seq_num();
//     buf.write_u16::<NetworkOrder>(base_seq_num)
//         .context("base seq num")?;
//     buf.write_u16::<NetworkOrder>(packet_reports.len() as u16)
//         .context("packet status count")?;
//     buf.write_u24::<NetworkOrder>(reference_time)
//         .context("reference time")?;
//
//     let (feedback_packet_count, chunks, deltas) = prepare_packet_reports(&packet_reports);
//     buf.write_u8(feedback_packet_count)
//         .context("feedback packet count")?;
//
//     for chunk in chunks {
//         write_some_packet_status_chunk(chunk, buf).context("packet status chunk")?;
//     }
//
//     for delta in deltas {
//         write_some_recv_delta(buf, delta).context("delta")?;
//     }
//
//     Ok(())
// }
//
// fn prepare_packet_reports(
//     packet_reports: &[PacketReport],
// ) -> (u8, Vec<SomePacketStatusChunk>, Vec<SomeRecvDelta>) {
//     let mut expected_seq_num = packet_reports[0].seq_num();
//     let mut chunks: Vec<SomePacketStatusChunk> = vec![];
//     let mut deltas: Vec<SomeRecvDelta> = vec![];
//     let mut curr_chunk = Chunk::default();
//     let mut seq_num_count = 0u8;
//     for packet_report in packet_reports {
//         while expected_seq_num != packet_report.seq_num() {
//             if !curr_chunk.can_add(PacketStatusSymbol::NotReceived) {
//                 chunks.push(curr_chunk.emit());
//             }
//             curr_chunk.add(PacketStatusSymbol::NotReceived);
//             expected_seq_num = expected_seq_num.wrapping_add(1);
//             seq_num_count = seq_num_count.wrapping_add(1);
//         }
//
//         if !curr_chunk.can_add(packet_report.symbol()) {
//             chunks.push(curr_chunk.emit());
//         }
//         match packet_report {
//             PacketReport::UnreceivedPacket { .. } => (),
//             PacketReport::ReceivedPacketSmallDelta { delta_ticks, .. } => {
//                 deltas.push(SomeRecvDelta::Small(*delta_ticks))
//             }
//             PacketReport::ReceivedPacketLargeOrNegativeDelta { delta_ticks, .. } => {
//                 deltas.push(SomeRecvDelta::LargeOrNegative(*delta_ticks))
//             }
//         }
//
//         expected_seq_num = expected_seq_num.wrapping_add(1);
//         seq_num_count = seq_num_count.wrapping_add(1);
//     }
//     chunks.push(curr_chunk.emit());
//
//     (seq_num_count, chunks, deltas)
// }

#[derive(Debug, PartialEq)]
pub enum PacketReport {
    UnreceivedPacket { seq_num: u16 },
    ReceivedPacketSmallDelta { seq_num: u16, delta_ticks: u8 },
    ReceivedPacketLargeOrNegativeDelta { seq_num: u16, delta_ticks: i16 },
}

impl PacketReport {
    pub fn seq_num(&self) -> u16 {
        match self {
            Self::UnreceivedPacket { seq_num } => *seq_num,
            Self::ReceivedPacketSmallDelta { seq_num, .. } => *seq_num,
            Self::ReceivedPacketLargeOrNegativeDelta { seq_num, .. } => *seq_num,
        }
    }

    pub fn symbol(&self) -> PacketStatusSymbol {
        match self {
            PacketReport::UnreceivedPacket { .. } => PacketStatusSymbol::NotReceived,
            PacketReport::ReceivedPacketSmallDelta { .. } => PacketStatusSymbol::ReceivedSmallDelta,
            PacketReport::ReceivedPacketLargeOrNegativeDelta { .. } => {
                PacketStatusSymbol::ReceivedLargeOrNegativeDelta
            }
        }
    }
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum PacketStatusSymbol {
    NotReceived = 0,
    ReceivedSmallDelta = 1,
    ReceivedLargeOrNegativeDelta = 2,
}

impl PacketStatusSymbol {
    // pub(crate) fn from_delta_size(delta_size: u8) -> Self {
    //     match delta_size {
    //         0 => PacketStatusSymbol::NotReceived,
    //         1 => PacketStatusSymbol::ReceivedSmallDelta,
    //         2 => PacketStatusSymbol::ReceivedLargeOrNegativeDelta,
    //         _ => todo!("invalid"),
    //     }
    // }

    pub(crate) fn delta_size_bytes(&self) -> u8 {
        match self {
            PacketStatusSymbol::NotReceived => 0,
            PacketStatusSymbol::ReceivedSmallDelta => 1,
            PacketStatusSymbol::ReceivedLargeOrNegativeDelta => 2,
        }
    }
}

impl From<u1> for PacketStatusSymbol {
    fn from(value: u1) -> Self {
        match value {
            U1_ZERO => PacketStatusSymbol::NotReceived,
            U1_ONE => PacketStatusSymbol::ReceivedSmallDelta,
            _ => unreachable!(),
        }
    }
}

impl TryInto<u1> for &PacketStatusSymbol {
    type Error = anyhow::Error;

    fn try_into(self) -> std::prelude::v1::Result<u1, Self::Error> {
        match self {
            PacketStatusSymbol::NotReceived => Ok(U1_ZERO),
            PacketStatusSymbol::ReceivedSmallDelta => Ok(U1_ONE),
            PacketStatusSymbol::ReceivedLargeOrNegativeDelta => Err(anyhow!(
                "PacketStatusSymbol::ReceivedLargeOrNegativeDelta can't be encoded into a u1"
            )),
        }
    }
}

impl TryInto<u1> for PacketStatusSymbol {
    type Error = anyhow::Error;

    fn try_into(self) -> std::prelude::v1::Result<u1, Self::Error> {
        (&self).try_into()
    }
}

impl TryFrom<u2> for PacketStatusSymbol {
    type Error = anyhow::Error;

    fn try_from(value: u2) -> std::prelude::v1::Result<Self, Self::Error> {
        match value {
            U2_ZERO => Ok(PacketStatusSymbol::NotReceived),
            U2_ONE => Ok(PacketStatusSymbol::ReceivedSmallDelta),
            U2_TWO => Ok(PacketStatusSymbol::ReceivedLargeOrNegativeDelta),
            pss => Err(anyhow!("Invalid 2 bit packet status symbol: {pss}")),
        }
    }
}

impl From<&PacketStatusSymbol> for u2 {
    fn from(val: &PacketStatusSymbol) -> Self {
        match val {
            PacketStatusSymbol::NotReceived => u2::new(0),
            PacketStatusSymbol::ReceivedSmallDelta => u2::new(1),
            PacketStatusSymbol::ReceivedLargeOrNegativeDelta => u2::new(2),
        }
    }
}

impl From<PacketStatusSymbol> for u2 {
    fn from(val: PacketStatusSymbol) -> Self {
        (&val).into()
    }
}

/// A status vector chunk starts with a 1 bit to identify it as a vector
/// chunk, followed by a symbol size bit and then 7 or 14 symbols,
/// depending on the size bit.
///
/// ```text
///      0                   1
///      0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
///     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
///     |T|S|       symbol list         |
///     +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
///
/// chunk type (T):  1 bit A one identifies this as a status vector
///             chunk.
///
/// symbol size (S):  1 bit A zero means this vector contains only
///             "packet received" (0) and "packet not received" (1)
///             symbols.  This means we can compress each symbol to just
///             one bit, 14 in total.  A one means this vector contains
///             the normal 2-bit symbols, 7 in total.
///
/// symbol list:  14 bits A list of packet status symbols, 7 or 14 in
///             total.
/// ```
#[derive(Debug, Clone, PartialEq)]
pub struct StatusVectorChunk(pub(crate) Vec<PacketStatusSymbol>);

impl StatusVectorChunk {
    fn has_two_bit_symbols(&self) -> bool {
        self.0
            .iter()
            .any(|ss| matches!(ss, PacketStatusSymbol::ReceivedLargeOrNegativeDelta))
    }

    fn iter(&self) -> std::slice::Iter<'_, PacketStatusSymbol> {
        self.0.iter()
    }
}

/// This method assumes buf's position is at the symbol-size bit.
impl<B: BitBuf> ParselyRead<B> for StatusVectorChunk {
    type Ctx = (usize,);

    fn read<T: ByteOrder>(buf: &mut B, (max_symbol_count,): Self::Ctx) -> ParselyResult<Self> {
        let symbol_size = buf.get_u1().context("symbol size")?;
        let mut packet_status_symbols = match symbol_size {
            s if s == 0 => {
                // 1 bit symbols
                let mut symbols = Vec::with_capacity(14);
                for i in 0..14 {
                    let symbol: PacketStatusSymbol = buf
                        .get_u1()
                        .with_context(|| format!("packet status symbol {i}"))
                        .map(|v| v.into())?;
                    symbols.push(symbol);
                }
                symbols
            }
            s if s == 1 => {
                // 2 bit symbols
                let mut symbols = Vec::with_capacity(7);
                for i in 0..7 {
                    let symbol: PacketStatusSymbol = buf
                        .get_u2()
                        .with_context(|| format!("packet status symbol {i}"))?
                        .try_into()
                        .context("converting u2 to packet status symbol")?;
                    symbols.push(symbol);
                }
                symbols
            }
            _ => unreachable!("u1 can only be 1 or 0"),
        };

        packet_status_symbols.truncate(max_symbol_count);

        Ok(StatusVectorChunk(packet_status_symbols))
    }
}

impl<B: BitBufMut> ParselyWrite<B> for StatusVectorChunk {
    type Ctx = ();

    fn write<T: ByteOrder>(&self, buf: &mut B, _ctx: Self::Ctx) -> ParselyResult<()> {
        buf.put_u1(u1::new(1)).context("SV chunk type")?;
        if self.has_two_bit_symbols() {
            buf.put_u1(u1::new(1)).context("SV chunk symbol size")?;
            for (i, symbol) in self.iter().enumerate() {
                buf.put_u2(symbol.into())
                    .with_context(|| format!("2 bit sv chunk symbol {i}"))?;
            }
        } else {
            buf.put_u1(u1::new(0)).context("SV chunk symbol size")?;
            for (i, symbol) in self.iter().enumerate() {
                buf.put_u1(
                    symbol
                        .try_into()
                        .context("Trying to convert status symbol to u1")?,
                )
                .with_context(|| format!("2 bit sv chunk symbol {i}"))?;
            }
        }
        Ok(())
    }
}

impl_stateless_sync!(StatusVectorChunk);

/// A run length chunk starts with 0 bit, followed by a packet status
/// symbol and the run length of that symbol.
/// ```text
///     0                   1
///     0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5
///    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
///    |T| S |       Run Length        |
///    +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
///
/// chunk type (T):  1 bit A zero identifies this as a run length chunk.
///
/// packet status symbol (S):  2 bits The symbol repeated in this run.
///             See above.
///
/// run length (L):  13 bits An unsigned integer denoting the run length.
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RunLengthEncodingChunk {
    pub symbol: PacketStatusSymbol,
    pub run_length: u13,
}

/// Assumes the buf's position is at the packet status symbol bit
impl<B: BitBuf> ParselyRead<B> for RunLengthEncodingChunk {
    type Ctx = ();

    fn read<T: ByteOrder>(buf: &mut B, _ctx: Self::Ctx) -> ParselyResult<Self> {
        let symbol = buf
            .get_u2()
            .context("Reading run length encoding symbol")?
            .try_into()
            .context("Converting u2 to packet status symbol")?;

        let run_length = buf.get_u13::<T>().context("Reading run length")?;

        Ok(RunLengthEncodingChunk { symbol, run_length })
    }
}

impl<B: BitBufMut> ParselyWrite<B> for RunLengthEncodingChunk {
    type Ctx = ();

    fn write<T: ByteOrder>(&self, buf: &mut B, _ctx: Self::Ctx) -> ParselyResult<()> {
        buf.put_u1(u1::new(0)).context("rle chunk type")?;
        buf.put_u2(self.symbol.into()).context("rle chunk symbol")?;
        buf.put_u13::<T>(self.run_length)
            .context("rle chunk run length")?;

        Ok(())
    }
}

impl_stateless_sync!(RunLengthEncodingChunk);

#[derive(Debug, Clone)]
pub(crate) enum SomePacketStatusChunk {
    StatusVectorChunk(StatusVectorChunk),
    RunLengthEncodingChunk(RunLengthEncodingChunk),
}

pub(crate) enum SomePacketStatusChunkIterator<'a> {
    StatusVector(std::slice::Iter<'a, PacketStatusSymbol>),
    RunLength(std::iter::Repeat<PacketStatusSymbol>, usize), // (iter, remaining count)
}

impl Iterator for SomePacketStatusChunkIterator<'_> {
    type Item = PacketStatusSymbol;

    fn next(&mut self) -> Option<Self::Item> {
        match self {
            SomePacketStatusChunkIterator::StatusVector(iter) => iter.next().copied(),
            SomePacketStatusChunkIterator::RunLength(iter, remaining) => {
                if *remaining == 0 {
                    None
                } else {
                    *remaining -= 1;
                    iter.next()
                }
            }
        }
    }
}

impl SomePacketStatusChunk {
    pub(crate) fn num_symbols(&self) -> u16 {
        match self {
            SomePacketStatusChunk::StatusVectorChunk(svc) => svc.0.len() as u16,
            SomePacketStatusChunk::RunLengthEncodingChunk(rlec) => rlec.run_length.into(),
        }
    }

    pub fn iter(&self) -> SomePacketStatusChunkIterator<'_> {
        match self {
            SomePacketStatusChunk::StatusVectorChunk(StatusVectorChunk(vec)) => {
                SomePacketStatusChunkIterator::StatusVector(vec.iter())
            }
            SomePacketStatusChunk::RunLengthEncodingChunk(chunk) => {
                SomePacketStatusChunkIterator::RunLength(
                    std::iter::repeat(chunk.symbol),
                    chunk.run_length.into(),
                )
            }
        }
    }
}

impl<B: BitBuf> ParselyRead<B> for SomePacketStatusChunk {
    type Ctx = (usize,);

    fn read<T: ByteOrder>(buf: &mut B, (max_symbol_count,): Self::Ctx) -> ParselyResult<Self> {
        let chunk_type = buf.get_u1().context("chunk type")?;
        match chunk_type {
            ct if ct == 0 => RunLengthEncodingChunk::read::<T>(buf, ())
                .map(SomePacketStatusChunk::RunLengthEncodingChunk)
                .context("run length encoding chunk"),
            ct if ct == 1 => StatusVectorChunk::read::<T>(buf, (max_symbol_count,))
                .map(SomePacketStatusChunk::StatusVectorChunk)
                .context("status vector chunk"),
            _ => unreachable!(),
        }
    }
}

impl<B: BitBufMut> ParselyWrite<B> for SomePacketStatusChunk {
    type Ctx = ();

    fn write<T: ByteOrder>(&self, buf: &mut B, _ctx: Self::Ctx) -> ParselyResult<()> {
        match self {
            SomePacketStatusChunk::RunLengthEncodingChunk(rle_chunk) => {
                rle_chunk.write::<T>(buf, ())?
            }
            SomePacketStatusChunk::StatusVectorChunk(sv_chunk) => sv_chunk.write::<T>(buf, ())?,
        }
        Ok(())
    }
}

impl_stateless_sync!(SomePacketStatusChunk);

#[cfg(test)]
mod test {
    use super::*;
    use bits_io::bits;
    // use bits_io::prelude::*;

    #[test]
    fn test_sv_chunk_1_bit_symbols() {
        let chunk_data = bits!(1, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1);
        let mut bits = Bits::copy_from_bit_slice(chunk_data);
        // Advance past the chunk type bit since we're calling StatusVectorChunk::read directly and
        // it assumes that bit has already been read
        bits.advance_bits(1);

        let sv_chunk = StatusVectorChunk::read::<NetworkOrder>(&mut bits, (14,)).unwrap();

        assert_eq!(sv_chunk.0.len(), 14);
        assert!(bits.is_empty());
        assert_eq!(
            sv_chunk.0,
            vec![
                PacketStatusSymbol::ReceivedSmallDelta,
                PacketStatusSymbol::ReceivedSmallDelta,
                PacketStatusSymbol::NotReceived,
                PacketStatusSymbol::NotReceived,
                PacketStatusSymbol::ReceivedSmallDelta,
                PacketStatusSymbol::ReceivedSmallDelta,
                PacketStatusSymbol::NotReceived,
                PacketStatusSymbol::NotReceived,
                PacketStatusSymbol::ReceivedSmallDelta,
                PacketStatusSymbol::ReceivedSmallDelta,
                PacketStatusSymbol::NotReceived,
                PacketStatusSymbol::NotReceived,
                PacketStatusSymbol::ReceivedSmallDelta,
                PacketStatusSymbol::ReceivedSmallDelta,
            ]
        );

        let mut bits_mut = BitsMut::new();
        sv_chunk.write::<NetworkOrder>(&mut bits_mut, ()).unwrap();
        assert_eq!(chunk_data, bits_mut.as_ref());
    }

    #[test]
    fn test_sv_chunk_1_bit_symbols_with_limit() {
        let mut chunk_data = bits!(0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 1, 1);

        let sv_chunk = StatusVectorChunk::read::<NetworkOrder>(&mut chunk_data, (3,)).unwrap();
        assert_eq!(sv_chunk.0.len(), 3);
        assert!(chunk_data.is_empty());
        assert_eq!(
            sv_chunk.0,
            vec![
                PacketStatusSymbol::ReceivedSmallDelta,
                PacketStatusSymbol::ReceivedSmallDelta,
                PacketStatusSymbol::NotReceived,
            ]
        );
    }

    #[test]
    fn test_sv_chunk_2_bit_symbols() {
        let chunk_data = bits!(1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0);
        let mut bits = Bits::copy_from_bit_slice(chunk_data);
        bits.advance_bits(1);

        let sv_chunk = StatusVectorChunk::read::<NetworkOrder>(&mut bits, (15,)).unwrap();
        assert_eq!(sv_chunk.0.len(), 7);
        assert!(bits.is_empty());
        assert_eq!(
            sv_chunk.0,
            vec![
                PacketStatusSymbol::NotReceived,
                PacketStatusSymbol::ReceivedSmallDelta,
                PacketStatusSymbol::ReceivedLargeOrNegativeDelta,
                PacketStatusSymbol::NotReceived,
                PacketStatusSymbol::ReceivedSmallDelta,
                PacketStatusSymbol::ReceivedLargeOrNegativeDelta,
                PacketStatusSymbol::NotReceived,
            ]
        );

        let mut bits_mut = BitsMut::new();
        sv_chunk.write::<NetworkOrder>(&mut bits_mut, ()).unwrap();
        assert_eq!(chunk_data, bits_mut.as_ref());
    }

    #[test]
    fn test_rle_chunk() {
        let chunk_data = bits!(0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1);
        let mut bits = Bits::copy_from_bit_slice(chunk_data);
        bits.advance_bits(1);

        let rle_chunk = RunLengthEncodingChunk::read::<NetworkOrder>(&mut bits, ()).unwrap();
        assert!(bits.is_empty());
        assert_eq!(rle_chunk.symbol, PacketStatusSymbol::ReceivedSmallDelta);
        assert_eq!(rle_chunk.run_length, 0b0000000010101);

        let mut bits_mut = BitsMut::new();
        rle_chunk.write::<NetworkOrder>(&mut bits_mut, ()).unwrap();
        assert_eq!(chunk_data, bits_mut.as_ref());
    }

    #[test]
    fn test_rtcp_fb_tcc_packet() {
        #[rustfmt::skip]
        let data_buf = [
            // base seq num = 385, packet status count = 8
            0x01, 0x81, 0x00, 0x08, 
            // reference time = 1683176, fk pkt count = 69
            0x19, 0xae, 0xe8, 0x45,
            
            0xd9, 0x55, 0x20, 0x01, 
            0xa8, 0xff, 0xfc, 0x04,
            0x00, 0x50, 0x04, 0x00, 
            0x00, 0x00, 0x00, 0x00
        ];
        let mut bits = Bits::copy_from_bytes(&data_buf[..]);
        let tcc_packet = RtcpFbTccPacket::read::<NetworkOrder>(
            &mut bits,
            (RtcpHeader::default(), RtcpFbHeader::default()),
        )
        .unwrap();
        assert_eq!(tcc_packet.reference_time, u24::new(1683176));
        assert_eq!(tcc_packet.feedback_packet_count, 69);
        assert_eq!(
            tcc_packet.packet_reports,
            [
                PacketReport::ReceivedPacketSmallDelta {
                    seq_num: 385,
                    delta_ticks: 168,
                },
                PacketReport::ReceivedPacketLargeOrNegativeDelta {
                    seq_num: 386,
                    delta_ticks: -4,
                },
                PacketReport::ReceivedPacketSmallDelta {
                    seq_num: 387,
                    delta_ticks: 4,
                },
                PacketReport::ReceivedPacketSmallDelta {
                    seq_num: 388,
                    delta_ticks: 0,
                },
                PacketReport::ReceivedPacketSmallDelta {
                    seq_num: 389,
                    delta_ticks: 80,
                },
                PacketReport::ReceivedPacketSmallDelta {
                    seq_num: 390,
                    delta_ticks: 4,
                },
                PacketReport::ReceivedPacketSmallDelta {
                    seq_num: 391,
                    delta_ticks: 0,
                },
                PacketReport::ReceivedPacketSmallDelta {
                    seq_num: 392,
                    delta_ticks: 0,
                },
            ]
        );
    }
}