scuffle-flv 0.2.2

A pure Rust FLV demuxer.
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
//! Enhanced audio tag body
//!
//! Types and functions defined by the enhanced RTMP spec, page 19, ExAudioTagBody.

use std::io::{self, Read};

use byteorder::{BigEndian, ReadBytesExt};
use bytes::{Buf, Bytes};
use nutype_enum::nutype_enum;
use scuffle_bytes_util::BytesCursorExt;

use crate::audio::header::enhanced::{AudioFourCc, AudioPacketType, ExAudioTagHeader, ExAudioTagHeaderContent};

nutype_enum! {
    /// Audio channel order
    ///
    /// Defined by:
    /// - Enhanced RTMP spec, page 22-23, ExAudioTagBody
    pub enum AudioChannelOrder(u8) {
        /// Only the channel count is specified, without any further information about the channel order.
        Unspecified = 0,
        /// The native channel order (i.e., the channels are in the same order in which as defined in the [`AudioChannel`] enum).
        Native = 1,
        /// The channel order does not correspond to any predefined order and is stored as an explicit map.
        Custom = 2,
    }
}

nutype_enum! {
    /// Channel mappings enum
    ///
    /// See <https://en.wikipedia.org/wiki/Surround_sound#Standard_speaker_channels> and
    /// <https://en.wikipedia.org/wiki/22.2_surround_sound> for more information.
    pub enum AudioChannel(u8) {
        // Commonly used speaker configurations:

        /// Front left
        FrontLeft = 0,
        /// Front right
        FrontRight = 1,
        /// Front center
        FrontCenter = 2,
        /// Low frequency
        LowFrequency1 = 3,
        /// Back left
        BackLeft = 4,
        /// Back right
        BackRight = 5,
        /// Front left of center
        FrontLeftCenter = 6,
        /// Front right of center
        FrontRightCenter = 7,
        /// Back center
        BackCenter = 8,
        /// Side left
        SideLeft = 9,
        /// Side right
        SideRight = 10,
        /// Top center
        TopCenter = 11,
        /// Front left height
        TopFrontLeft = 12,
        /// Front center height
        TopFrontCenter = 13,
        /// Front right height
        TopFrontRight = 14,
        /// Rear left height
        TopBackLeft = 15,
        /// Rear center height
        TopBackCenter = 16,
        /// Rear right height
        TopBackRight = 17,

        // Mappings to complete 22.2 multichannel audio, as standardized in SMPTE ST2036-2-2008:

        /// Low frequency 2
        LowFrequency2 = 18,
        /// Top side left
        TopSideLeft = 19,
        /// Top side right
        TopSideRight = 20,
        /// Bottom front center
        BottomFrontCenter = 21,
        /// Bottom front left
        BottomFrontLeft = 22,
        /// Bottom front right
        BottomFrontRight = 23,
        /// Channel is empty and can be safely skipped.
        Unused = 0xfe,
        /// Channel contains data, but its speaker configuration is unknown.
        Unknown = 0xff,
    }
}

/// Mask used to indicate which channels are present in the stream.
///
/// See <https://en.wikipedia.org/wiki/Surround_sound#Standard_speaker_channels> and
/// <https://en.wikipedia.org/wiki/22.2_surround_sound> for more information.
#[bitmask_enum::bitmask(u32)]
pub enum AudioChannelMask {
    // Masks for commonly used speaker configurations:
    /// Front left
    FrontLeft = 0x000001,
    /// Front right
    FrontRight = 0x000002,
    /// Front center
    FrontCenter = 0x000004,
    /// Low frequency
    LowFrequency1 = 0x000008,
    /// Back left
    BackLeft = 0x000010,
    /// Back right
    BackRight = 0x000020,
    /// Front left of center
    FrontLeftCenter = 0x000040,
    /// Front right of center
    FrontRightCenter = 0x000080,
    /// Back center
    BackCenter = 0x000100,
    /// Side left
    SideLeft = 0x000200,
    /// Side right
    SideRight = 0x000400,
    /// Top center
    TopCenter = 0x000800,
    /// Front left height
    TopFrontLeft = 0x001000,
    /// Front center height
    TopFrontCenter = 0x002000,
    /// Front right height
    TopFrontRight = 0x004000,
    /// Rear left height
    TopBackLeft = 0x008000,
    /// Rear center height
    TopBackCenter = 0x010000,
    /// Rear right height
    TopBackRight = 0x020000,

    // Completes 22.2 multichannel audio, as
    // standardized in SMPTE ST2036-2-2008:
    /// Low frequency 2
    LowFrequency2 = 0x040000,
    /// Top side left
    TopSideLeft = 0x080000,
    /// Top side right
    TopSideRight = 0x100000,
    /// Bottom front center
    BottomFrontCenter = 0x200000,
    /// Bottom front left
    BottomFrontLeft = 0x400000,
    /// Bottom front right
    BottomFrontRight = 0x800000,
}

/// Multichannel configuration
///
/// Describes the configuration of the audio channels in a multichannel audio stream.
///
/// Contained in an [`AudioPacket::MultichannelConfig`].
#[derive(Debug, Clone, PartialEq)]
pub enum MultichannelConfigOrder {
    /// Custom channel order
    ///
    /// The channels have a custom order that is explicitly defined by this packet.
    Custom(Vec<AudioChannel>),
    /// Native channel order
    ///
    /// Only the channels flagged in this packet are present in the stream
    /// in the order they are defined by the [`AudioChannelMask`].
    ///
    /// > You can perform a Bitwise AND
    /// > (i.e., audioChannelFlags & AudioChannelMask.xxx) to see if a
    /// > specific audio channel is present.
    Native(AudioChannelMask),
    /// The channel order is unspecified, only the channel count is known.
    Unspecified,
    /// An unknown channel order.
    ///
    /// Neither [`Unspecified`](AudioChannelOrder::Unspecified), [`Native`](AudioChannelOrder::Native),
    /// nor [`Custom`](AudioChannelOrder::Custom).
    Unknown(AudioChannelOrder),
}

/// Audio packet
///
/// Appears as part of the [`ExAudioTagBody`].
///
/// Defined by:
/// - Enhanced RTMP spec, page 23-25, ExAudioTagBody
#[derive(Debug, Clone, PartialEq)]
pub enum AudioPacket {
    /// Multichannel configuration
    ///
    /// > Specify a speaker for a channel as it appears in the bitstream.
    /// > This is needed if the codec is not self-describing for channel mapping.
    MultichannelConfig {
        /// The number of channels in the audio stream.
        channel_count: u8,
        /// The multichannel configuration.
        ///
        /// Specifies the order of the channels in the audio stream.
        multichannel_config: MultichannelConfigOrder,
    },
    /// Indicates the end of a sequence of audio packets.
    SequenceEnd,
    /// Indicates the start of a sequence of audio packets.
    SequenceStart {
        /// The header data for the sequence.
        header_data: Bytes,
    },
    /// Coded audio frames.
    CodedFrames {
        /// The audio data.
        data: Bytes,
    },
    /// An unknown [`AudioPacketType`].
    Unknown {
        /// The unknown packet type.
        audio_packet_type: AudioPacketType,
        /// The data.
        data: Bytes,
    },
}

impl AudioPacket {
    /// Demux an [`AudioPacket`] from the given reader.
    ///
    /// This is implemented as per spec, Enhanced RTMP page 23-25, ExAudioTagBody.
    pub fn demux(header: &ExAudioTagHeader, reader: &mut io::Cursor<Bytes>) -> io::Result<Self> {
        let has_multiple_tracks = !matches!(
            header.content,
            ExAudioTagHeaderContent::NoMultiTrack(_) | ExAudioTagHeaderContent::OneTrack(_)
        );

        let size_of_audio_track = if has_multiple_tracks {
            Some(reader.read_u24::<BigEndian>()? as usize)
        } else {
            None
        };

        match header.audio_packet_type {
            AudioPacketType::MultichannelConfig => {
                let audio_channel_order = AudioChannelOrder::from(reader.read_u8()?);
                let channel_count = reader.read_u8()?;

                let multichannel_config = match audio_channel_order {
                    AudioChannelOrder::Custom => {
                        let channels = reader.extract_bytes(channel_count as usize)?;

                        MultichannelConfigOrder::Custom(channels.into_iter().map(AudioChannel::from).collect())
                    }
                    AudioChannelOrder::Native => {
                        let audio_channel_flags = AudioChannelMask::from(reader.read_u32::<BigEndian>()?);

                        MultichannelConfigOrder::Native(audio_channel_flags)
                    }
                    AudioChannelOrder::Unspecified => MultichannelConfigOrder::Unspecified,
                    _ => MultichannelConfigOrder::Unknown(audio_channel_order),
                };

                Ok(Self::MultichannelConfig {
                    channel_count,
                    multichannel_config,
                })
            }
            AudioPacketType::SequenceEnd => Ok(Self::SequenceEnd),
            AudioPacketType::SequenceStart => {
                let header_data = reader.extract_bytes(size_of_audio_track.unwrap_or(reader.remaining()))?;

                Ok(Self::SequenceStart { header_data })
            }
            AudioPacketType::CodedFrames => {
                let data = reader.extract_bytes(size_of_audio_track.unwrap_or(reader.remaining()))?;

                Ok(Self::CodedFrames { data })
            }
            _ => {
                let data = reader.extract_bytes(size_of_audio_track.unwrap_or(reader.remaining()))?;

                Ok(Self::Unknown {
                    audio_packet_type: header.audio_packet_type,
                    data,
                })
            }
        }
    }
}

/// One audio track contained in a multitrack audio.
#[derive(Debug, Clone, PartialEq)]
pub struct AudioTrack {
    /// The audio FOURCC of this track.
    pub audio_four_cc: AudioFourCc,
    /// The audio track ID.
    ///
    /// > For identifying the highest priority (a.k.a., default track)
    /// > or highest quality track, it is RECOMMENDED to use trackId
    /// > set to zero. For tracks of lesser priority or quality, use
    /// > multiple instances of trackId with ascending numerical values.
    /// > The concept of priority or quality can have multiple
    /// > interpretations, including but not limited to bitrate,
    /// > resolution, default angle, and language. This recommendation
    /// > serves as a guideline intended to standardize track numbering
    /// > across various applications.
    pub audio_track_id: u8,
    /// The audio packet contained in this track.
    pub packet: AudioPacket,
}

/// `ExAudioTagBody`
///
/// Defined by:
/// - Enhanced RTMP spec, page 22-25, ExAudioTagBody
#[derive(Debug, Clone, PartialEq)]
pub enum ExAudioTagBody {
    /// The body is not a multitrack body.
    NoMultitrack {
        /// The audio FOURCC of this body.
        audio_four_cc: AudioFourCc,
        /// The audio packet contained in this body.
        packet: AudioPacket,
    },
    /// The body is a multitrack body.
    ///
    /// This variant contains multiple audio tracks.
    /// See [`AudioTrack`] for more information.
    ManyTracks(Vec<AudioTrack>),
}

impl ExAudioTagBody {
    /// Demux an [`ExAudioTagBody`] from the given reader.
    ///
    /// This is implemented as per Enhanced RTMP spec, page 22-25, ExAudioTagBody.
    pub fn demux(header: &ExAudioTagHeader, reader: &mut io::Cursor<Bytes>) -> io::Result<Self> {
        let mut tracks = Vec::new();

        loop {
            let audio_four_cc = match header.content {
                ExAudioTagHeaderContent::ManyTracksManyCodecs => {
                    let mut audio_four_cc = [0; 4];
                    reader.read_exact(&mut audio_four_cc)?;
                    AudioFourCc::from(audio_four_cc)
                }
                ExAudioTagHeaderContent::OneTrack(audio_four_cc) => audio_four_cc,
                ExAudioTagHeaderContent::ManyTracks(audio_four_cc) => audio_four_cc,
                ExAudioTagHeaderContent::NoMultiTrack(audio_four_cc) => audio_four_cc,
                ExAudioTagHeaderContent::Unknown { audio_four_cc, .. } => audio_four_cc,
            };

            // if isAudioMultitrack
            let audio_track_id = if !matches!(header.content, ExAudioTagHeaderContent::NoMultiTrack(_)) {
                Some(reader.read_u8()?)
            } else {
                None
            };

            let packet = AudioPacket::demux(header, reader)?;

            if let Some(audio_track_id) = audio_track_id {
                // audio_track_id is only set if this is a multitrack audio, in other words, if `isAudioMultitrack` is true
                tracks.push(AudioTrack {
                    audio_four_cc,
                    audio_track_id,
                    packet,
                });

                // the loop only continues if there is still data to read and this is a audio with multiple tracks
                if !matches!(header.content, ExAudioTagHeaderContent::OneTrack(_)) && reader.has_remaining() {
                    continue;
                }

                break;
            } else {
                // exit early if this is a single track audio only completing one loop iteration
                return Ok(Self::NoMultitrack { audio_four_cc, packet });
            }
        }

        // at this point we know this is a multitrack audio because a single track audio would have exited early
        Ok(Self::ManyTracks(tracks))
    }
}

#[cfg(test)]
#[cfg_attr(all(test, coverage_nightly), coverage(off))]
mod tests {
    use bytes::Bytes;

    use super::AudioPacket;
    use crate::audio::body::enhanced::{
        AudioChannel, AudioChannelMask, AudioChannelOrder, AudioTrack, ExAudioTagBody, MultichannelConfigOrder,
    };
    use crate::audio::header::enhanced::{AudioFourCc, AudioPacketType, ExAudioTagHeader, ExAudioTagHeaderContent};
    use crate::common::AvMultitrackType;

    #[test]
    fn simple_audio_packets_demux() {
        let data = &[42, 42, 42, 42];

        let packet = AudioPacket::demux(
            &ExAudioTagHeader {
                audio_packet_mod_exs: vec![],
                audio_packet_type: AudioPacketType::SequenceStart,
                content: ExAudioTagHeaderContent::NoMultiTrack(AudioFourCc::Aac),
            },
            &mut std::io::Cursor::new(Bytes::from_static(data)),
        )
        .unwrap();
        assert_eq!(
            packet,
            AudioPacket::SequenceStart {
                header_data: Bytes::from_static(data)
            }
        );

        let packet = AudioPacket::demux(
            &ExAudioTagHeader {
                audio_packet_mod_exs: vec![],
                audio_packet_type: AudioPacketType::CodedFrames,
                content: ExAudioTagHeaderContent::NoMultiTrack(AudioFourCc::Aac),
            },
            &mut std::io::Cursor::new(Bytes::from_static(data)),
        )
        .unwrap();
        assert_eq!(
            packet,
            AudioPacket::CodedFrames {
                data: Bytes::from_static(data)
            }
        );

        let packet = AudioPacket::demux(
            &ExAudioTagHeader {
                audio_packet_mod_exs: vec![],
                audio_packet_type: AudioPacketType::SequenceEnd,
                content: ExAudioTagHeaderContent::NoMultiTrack(AudioFourCc::Aac),
            },
            &mut std::io::Cursor::new(Bytes::from_static(data)),
        )
        .unwrap();
        assert_eq!(packet, AudioPacket::SequenceEnd,);

        let packet = AudioPacket::demux(
            &ExAudioTagHeader {
                audio_packet_mod_exs: vec![],
                audio_packet_type: AudioPacketType(8),
                content: ExAudioTagHeaderContent::NoMultiTrack(AudioFourCc::Aac),
            },
            &mut std::io::Cursor::new(Bytes::from_static(data)),
        )
        .unwrap();
        assert_eq!(
            packet,
            AudioPacket::Unknown {
                audio_packet_type: AudioPacketType(8),
                data: Bytes::from_static(data),
            },
        );
    }

    #[test]
    fn audio_packet_with_size_demux() {
        let data = &[
            0, 0, 2, // size
            42, 42, // data
            13, 37, // should be ignored
        ];

        let header = ExAudioTagHeader {
            audio_packet_mod_exs: vec![],
            audio_packet_type: AudioPacketType::CodedFrames,
            content: ExAudioTagHeaderContent::ManyTracks(AudioFourCc::Aac),
        };

        let packet = AudioPacket::demux(&header, &mut std::io::Cursor::new(Bytes::from_static(data))).unwrap();

        assert_eq!(
            packet,
            AudioPacket::CodedFrames {
                data: Bytes::from_static(&[42, 42])
            },
        );
    }

    #[test]
    fn audio_packet_custom_multichannel_config_demux() {
        let data = &[
            2, // channel order custom
            2, // channel count
            0, 1, // channels
        ];

        let header = ExAudioTagHeader {
            audio_packet_mod_exs: vec![],
            audio_packet_type: AudioPacketType::MultichannelConfig,
            content: ExAudioTagHeaderContent::NoMultiTrack(AudioFourCc::Aac),
        };

        let packet = AudioPacket::demux(&header, &mut std::io::Cursor::new(Bytes::from_static(data))).unwrap();

        assert_eq!(
            packet,
            AudioPacket::MultichannelConfig {
                channel_count: 2,
                multichannel_config: MultichannelConfigOrder::Custom(vec![
                    AudioChannel::FrontLeft,
                    AudioChannel::FrontRight
                ])
            },
        );
    }

    #[test]
    fn audio_packet_native_multichannel_config_demux() {
        let data = &[
            1, // channel order native
            2, // channel count
            0, 0, 0, 3, // channels
        ];

        let header = ExAudioTagHeader {
            audio_packet_mod_exs: vec![],
            audio_packet_type: AudioPacketType::MultichannelConfig,
            content: ExAudioTagHeaderContent::NoMultiTrack(AudioFourCc::Aac),
        };

        let packet = AudioPacket::demux(&header, &mut std::io::Cursor::new(Bytes::from_static(data))).unwrap();

        assert_eq!(
            packet,
            AudioPacket::MultichannelConfig {
                channel_count: 2,
                multichannel_config: MultichannelConfigOrder::Native(
                    AudioChannelMask::FrontLeft | AudioChannelMask::FrontRight
                )
            },
        );
    }

    #[test]
    fn audio_packet_other_multichannel_config_demux() {
        let data = &[
            0, // channel order unspecified
            2, // channel count
        ];

        let header = ExAudioTagHeader {
            audio_packet_mod_exs: vec![],
            audio_packet_type: AudioPacketType::MultichannelConfig,
            content: ExAudioTagHeaderContent::NoMultiTrack(AudioFourCc::Aac),
        };

        let packet = AudioPacket::demux(&header, &mut std::io::Cursor::new(Bytes::from_static(data))).unwrap();

        assert_eq!(
            packet,
            AudioPacket::MultichannelConfig {
                channel_count: 2,
                multichannel_config: MultichannelConfigOrder::Unspecified,
            },
        );

        let data = &[
            4, // channel order unknown
            2, // channel count
        ];

        let header = ExAudioTagHeader {
            audio_packet_mod_exs: vec![],
            audio_packet_type: AudioPacketType::MultichannelConfig,
            content: ExAudioTagHeaderContent::NoMultiTrack(AudioFourCc::Aac),
        };

        let packet = AudioPacket::demux(&header, &mut std::io::Cursor::new(Bytes::from_static(data))).unwrap();

        assert_eq!(
            packet,
            AudioPacket::MultichannelConfig {
                channel_count: 2,
                multichannel_config: MultichannelConfigOrder::Unknown(AudioChannelOrder(4)),
            },
        );
    }

    #[test]
    fn simple_body_demux() {
        let data = &[
            42, 42, // data
        ];

        let header = ExAudioTagHeader {
            audio_packet_mod_exs: vec![],
            audio_packet_type: AudioPacketType::CodedFrames,
            content: ExAudioTagHeaderContent::NoMultiTrack(AudioFourCc::Aac),
        };

        let packet = ExAudioTagBody::demux(&header, &mut std::io::Cursor::new(Bytes::from_static(data))).unwrap();

        assert_eq!(
            packet,
            ExAudioTagBody::NoMultitrack {
                audio_four_cc: AudioFourCc::Aac,
                packet: AudioPacket::CodedFrames {
                    data: Bytes::from_static(data)
                },
            },
        );
    }

    #[test]
    fn multitrack_many_codecs_body_demux() {
        let data = &[
            b'm', b'p', b'4', b'a', // audio four cc
            1,    // audio track id
            0, 0, 2, // size
            42, 42, // data
            b'O', b'p', b'u', b's', // audio four cc
            2,    // audio track id
            0, 0, 2, // size
            13, 37, // data
        ];

        let header = ExAudioTagHeader {
            audio_packet_mod_exs: vec![],
            audio_packet_type: AudioPacketType::CodedFrames,
            content: ExAudioTagHeaderContent::ManyTracksManyCodecs,
        };

        let packet = ExAudioTagBody::demux(&header, &mut std::io::Cursor::new(Bytes::from_static(data))).unwrap();

        assert_eq!(
            packet,
            ExAudioTagBody::ManyTracks(vec![
                AudioTrack {
                    audio_four_cc: AudioFourCc::Aac,
                    audio_track_id: 1,
                    packet: AudioPacket::CodedFrames {
                        data: Bytes::from_static(&[42, 42])
                    },
                },
                AudioTrack {
                    audio_four_cc: AudioFourCc::Opus,
                    audio_track_id: 2,
                    packet: AudioPacket::CodedFrames {
                        data: Bytes::from_static(&[13, 37])
                    },
                }
            ]),
        );
    }

    #[test]
    fn multitrack_body_demux() {
        let data = &[
            1, // audio track id
            0, 0, 2, // size
            42, 42, // data
            2,  // audio track id
            0, 0, 2, // size
            13, 37, // data
        ];

        let header = ExAudioTagHeader {
            audio_packet_mod_exs: vec![],
            audio_packet_type: AudioPacketType::CodedFrames,
            content: ExAudioTagHeaderContent::ManyTracks(AudioFourCc::Aac),
        };

        let packet = ExAudioTagBody::demux(&header, &mut std::io::Cursor::new(Bytes::from_static(data))).unwrap();

        assert_eq!(
            packet,
            ExAudioTagBody::ManyTracks(vec![
                AudioTrack {
                    audio_four_cc: AudioFourCc::Aac,
                    audio_track_id: 1,
                    packet: AudioPacket::CodedFrames {
                        data: Bytes::from_static(&[42, 42])
                    },
                },
                AudioTrack {
                    audio_four_cc: AudioFourCc::Aac,
                    audio_track_id: 2,
                    packet: AudioPacket::CodedFrames {
                        data: Bytes::from_static(&[13, 37])
                    },
                }
            ]),
        );
    }

    #[test]
    fn multitrack_one_track_body_demux() {
        let data = &[
            1, // audio track id
            42, 42, // data
        ];

        let header = ExAudioTagHeader {
            audio_packet_mod_exs: vec![],
            audio_packet_type: AudioPacketType::CodedFrames,
            content: ExAudioTagHeaderContent::OneTrack(AudioFourCc::Aac),
        };

        let packet = ExAudioTagBody::demux(&header, &mut std::io::Cursor::new(Bytes::from_static(data))).unwrap();

        assert_eq!(
            packet,
            ExAudioTagBody::ManyTracks(vec![AudioTrack {
                audio_four_cc: AudioFourCc::Aac,
                audio_track_id: 1,
                packet: AudioPacket::CodedFrames {
                    data: Bytes::from_static(&[42, 42])
                },
            }]),
        );
    }

    #[test]
    fn multitrack_unknown_body_demux() {
        let data = &[
            1, // audio track id
            0, 0, 2, // size
            42, 42, // data
        ];

        let header = ExAudioTagHeader {
            audio_packet_mod_exs: vec![],
            audio_packet_type: AudioPacketType::CodedFrames,
            content: ExAudioTagHeaderContent::Unknown {
                audio_four_cc: AudioFourCc::Aac,
                audio_multitrack_type: AvMultitrackType(4),
            },
        };

        let packet = ExAudioTagBody::demux(&header, &mut std::io::Cursor::new(Bytes::from_static(data))).unwrap();

        assert_eq!(
            packet,
            ExAudioTagBody::ManyTracks(vec![AudioTrack {
                audio_track_id: 1,
                audio_four_cc: AudioFourCc::Aac,
                packet: AudioPacket::CodedFrames {
                    data: Bytes::from_static(&[42, 42])
                }
            }]),
        );
    }
}