scuffle_flv/audio/body/legacy/
mod.rs

1//! Legacy audio tag body
2//!
3//! Types and functions defined by the legacy FLV spec, Annex E.4.2.1.
4
5use std::io;
6
7use byteorder::ReadBytesExt;
8use bytes::Bytes;
9use scuffle_bytes_util::BytesCursorExt;
10
11use crate::audio::header::legacy::{LegacyAudioTagHeader, SoundFormat};
12
13pub mod aac;
14
15/// The legacy FLV `AudioTagBody`.
16///
17/// Defined by:
18/// - Legacy FLV spec, Annex E.4.2.1
19#[derive(Debug, Clone, PartialEq)]
20pub enum LegacyAudioTagBody {
21    /// AAC Audio Packet
22    Aac(aac::AacAudioData),
23    /// Any other audio format
24    Other {
25        /// The sound data
26        sound_data: Bytes,
27    },
28}
29
30impl LegacyAudioTagBody {
31    /// Demux the audio tag body from the given reader.
32    ///
33    /// The reader will be consumed entirely.
34    pub fn demux(header: &LegacyAudioTagHeader, reader: &mut io::Cursor<Bytes>) -> io::Result<Self> {
35        match header.sound_format {
36            SoundFormat::Aac => {
37                let aac_packet_type = aac::AacPacketType::from(reader.read_u8()?);
38                Ok(Self::Aac(aac::AacAudioData::new(aac_packet_type, reader.extract_remaining())))
39            }
40            _ => Ok(Self::Other {
41                sound_data: reader.extract_remaining(),
42            }),
43        }
44    }
45}