Skip to main content

flowly_flv/parser/tag/
audio.rs

1use crate::{
2    error::Error,
3    parser::{FlvParser, Parser},
4    reader::FlvReader,
5    tag::audio::{
6        AudioTag, AudioTagBody, AudioTagHeader, SoundFormat, SoundRate, SoundSize, SoundType,
7    },
8};
9
10impl<E> Parser<E, AudioTagHeader> for FlvParser {
11    type Error = Error<E>;
12
13    /// Parse audio tag data header.
14    fn parse(&mut self, reader: &mut impl FlvReader) -> Result<AudioTagHeader, Self::Error> {
15        let header = reader.read_u8()?;
16
17        Ok(AudioTagHeader {
18            sound_format: SoundFormat::from(header >> 4),
19            sound_rate: SoundRate::from((header >> 2) & 0b11),
20            sound_size: SoundSize::from((header >> 1) & 1),
21            sound_type: SoundType::from(header & 1),
22        })
23    }
24}
25
26impl<E> Parser<E, AudioTagBody> for FlvParser {
27    type Error = Error<E>;
28
29    /// Parse audio tag data body.
30    fn parse(&mut self, reader: &mut impl FlvReader) -> Result<AudioTagBody, Self::Error> {
31        Ok(AudioTagBody {
32            data: reader.read_to_end()?,
33        })
34    }
35}
36
37impl<E> Parser<E, AudioTag> for FlvParser {
38    type Error = Error<E>;
39    /// Parse audio tag data.
40    fn parse(&mut self, reader: &mut impl FlvReader) -> Result<AudioTag, Self::Error> {
41        let header: AudioTagHeader = self.parse(reader)?;
42        let body = self.parse(reader)?;
43
44        Ok(AudioTag { header, body })
45    }
46}