Skip to main content

flowly_flv/tag/
audio.rs

1use bytes::{Buf, Bytes};
2
3/// The tag data part of `audio` FLV tag, including `tag data header` and `tag data body`.
4#[derive(Clone, Debug, PartialEq)]
5pub struct AudioTag {
6    /// The header part of `audio` FLV tag.
7    pub header: AudioTagHeader, // 8 bits.
8
9    /// The body part of `audio` FLV tag.
10    pub body: AudioTagBody,
11}
12
13/// The `tag data header` part of `audio` FLV tag data.
14#[derive(Copy, Clone, Debug, Eq, PartialEq)]
15pub struct AudioTagHeader {
16    /// The format of sound, 4 bits.
17    pub sound_format: SoundFormat,
18
19    /// The rate of sound, 2 bits.
20    pub sound_rate: SoundRate,
21
22    /// The sample size of sound, 1 bit.
23    pub sound_size: SoundSize,
24
25    /// The type of sound, 1 bit.
26    pub sound_type: SoundType,
27}
28
29/// The `tag data body` part of `audio` FLV tag data.
30#[derive(Clone, Debug, PartialEq)]
31pub struct AudioTagBody {
32    /// The actual `tag data body` of `audio` FLV tag data.
33    pub data: Bytes,
34}
35
36/// The audio format.
37#[allow(clippy::upper_case_acronyms)]
38#[derive(Copy, Clone, Debug, Eq, PartialEq)]
39pub enum SoundFormat {
40    /// 0, PcmPlatformEndian
41    PcmPlatformEndian,
42
43    /// 1, ADPCM
44    ADPCM,
45
46    /// 2, MP3
47    MP3,
48
49    /// 3, PcmLittleEndian
50    PcmLittleEndian,
51
52    /// 4, Nellymoser16kHzMono
53    Nellymoser16kHzMono,
54
55    /// 5, Nellymoser8kHzMono
56    Nellymoser8kHzMono,
57
58    /// 6, Nellymoser
59    Nellymoser,
60
61    /// 7, PcmALaw
62    PcmALaw,
63
64    /// 8, PcmMuLaw
65    PcmMuLaw,
66
67    /// 9, Reserved
68    ExHeader,
69
70    /// 10, MPEG-4 Part3 AAC
71    AAC,
72
73    /// 11, Speex
74    Speex,
75
76    /// 14, MP3_8kHz
77    MP3_8kHz,
78
79    /// 15, DeviceSpecific
80    DeviceSpecific,
81}
82
83impl From<u8> for SoundFormat {
84    fn from(value: u8) -> Self {
85        match value {
86            0 => SoundFormat::PcmPlatformEndian,
87            1 => SoundFormat::ADPCM,
88            2 => SoundFormat::MP3,
89            3 => SoundFormat::PcmLittleEndian,
90            4 => SoundFormat::Nellymoser16kHzMono,
91            5 => SoundFormat::Nellymoser8kHzMono,
92            6 => SoundFormat::Nellymoser,
93            7 => SoundFormat::PcmALaw,
94            8 => SoundFormat::PcmMuLaw,
95            9 => SoundFormat::ExHeader,
96            10 => SoundFormat::AAC,
97            11 => SoundFormat::Speex,
98            14 => SoundFormat::MP3_8kHz,
99            15 => SoundFormat::DeviceSpecific,
100            _ => unreachable!(),
101        }
102    }
103}
104
105/// The audio sampling rate.
106#[derive(Copy, Clone, Debug, Eq, PartialEq)]
107pub enum SoundRate {
108    /// 0, 5.5 KHz.
109    _5_5KHZ,
110
111    /// 1, 11 KHz.
112    _11KHZ,
113
114    /// 2, 22 KHz.
115    _22KHZ,
116
117    /// 3, 44 KHz.
118    _44KHZ,
119}
120
121impl From<u8> for SoundRate {
122    fn from(value: u8) -> Self {
123        match value {
124            0 => SoundRate::_5_5KHZ,
125            1 => SoundRate::_11KHZ,
126            2 => SoundRate::_22KHZ,
127            3 => SoundRate::_44KHZ,
128            _ => unreachable!(),
129        }
130    }
131}
132
133/// The size of each audio sample.
134#[derive(Copy, Clone, Debug, Eq, PartialEq)]
135pub enum SoundSize {
136    /// 0, 8 bit.
137    _8Bit,
138
139    /// 1, 16 bit.
140    _16Bit,
141}
142
143impl From<u8> for SoundSize {
144    fn from(value: u8) -> Self {
145        match value {
146            0 => SoundSize::_8Bit,
147            1 => SoundSize::_16Bit,
148            _ => unreachable!(),
149        }
150    }
151}
152
153/// The type of audio, including mono and stereo.
154#[derive(Copy, Clone, Debug, Eq, PartialEq)]
155pub enum SoundType {
156    /// 0, Mono sound.
157    Mono,
158
159    /// 1, Stereo sound.
160    Stereo,
161}
162
163impl From<u8> for SoundType {
164    fn from(value: u8) -> Self {
165        match value {
166            0 => SoundType::Mono,
167            1 => SoundType::Stereo,
168            _ => unreachable!(),
169        }
170    }
171}
172
173/// The `tag data body` part of `audio` FLV tag data whose `SoundFormat` is 10 -- AAC.
174#[derive(Clone, Debug, PartialEq)]
175pub struct AACAudioPacket {
176    /// Only useful when sound format is 10 -- AAC, 1 byte.
177    pub packet_type: AACPacketType,
178
179    /// The actual AAC data.
180    pub aac_data: Bytes,
181}
182
183/// The type of AAC packet.
184#[derive(Copy, Clone, Debug, Eq, PartialEq)]
185pub enum AACPacketType {
186    /// 0, SequenceHeader.
187    SequenceHeader,
188
189    /// 1, Raw.
190    Raw,
191}
192
193impl From<u8> for AACPacketType {
194    fn from(value: u8) -> Self {
195        match value {
196            0 => AACPacketType::SequenceHeader,
197            _ => AACPacketType::Raw,
198        }
199    }
200}
201
202/// Parse AAC audio packet.
203pub fn aac_audio_packet(mut reader: Bytes) -> AACAudioPacket {
204    let packet_type = reader.get_u8();
205
206    AACAudioPacket {
207        packet_type: AACPacketType::from(packet_type),
208        aac_data: reader,
209    }
210}