async_mp4/mp4box/
stsd.rs

1use crate::full_box;
2use crate::mp4box::box_unknown::UnknownBox;
3use crate::types::array::Mp4Array;
4use async_trait::async_trait;
5use crate::bytes_read::{Mp4Readable, ReadMp4};
6use crate::bytes_write::{Mp4Writable, WriteMp4};
7use crate::error::MP4Error;
8use crate::header::BoxHeader;
9use crate::mp4box::avc1::{Avc1Box};
10use crate::mp4box::box_trait::{BoxRead, BoxWrite, IBox};
11use crate::mp4box::opus::OpusBox;
12
13#[derive(Debug, Clone, Eq, PartialEq, Hash)]
14pub enum StsdSampleEntry {
15    Avc1(Avc1Box),
16    Opus(OpusBox),
17    Unknown(UnknownBox),
18}
19
20#[async_trait]
21impl Mp4Readable for StsdSampleEntry {
22    async fn read<R: ReadMp4>(reader: &mut R) -> Result<Self, MP4Error> {
23        let header: BoxHeader = reader.read().await?;
24        Ok(match header.id {
25            Avc1Box::ID => Self::Avc1(<Avc1Box as BoxRead>::read(header, reader).await?),
26            OpusBox::ID => Self::Opus(<OpusBox as BoxRead>::read(header, reader).await?),
27            _ => Self::Unknown(<UnknownBox as BoxRead>::read(header, reader).await?)
28        })
29    }
30}
31
32impl Mp4Writable for StsdSampleEntry {
33    fn byte_size(&self) -> usize {
34        match self {
35            StsdSampleEntry::Avc1(it) => it.byte_size(),
36            StsdSampleEntry::Opus(it) => it.byte_size(),
37            StsdSampleEntry::Unknown(it) => it.byte_size()
38        }
39    }
40
41    fn write<W: WriteMp4>(&self, writer: &mut W) -> Result<usize, MP4Error> {
42        match self {
43            StsdSampleEntry::Avc1(it) => it.write(writer),
44            StsdSampleEntry::Opus(it) => it.write(writer),
45            StsdSampleEntry::Unknown(it) => it.write(writer)
46        }
47    }
48}
49
50full_box! {
51    box (b"stsd", Stsd, StsdBox, u32) data {
52        entries: Mp4Array<u32, StsdSampleEntry>
53    }
54}