1use futures::AsyncReadExt;
2use crate::bytes_read::ReadMp4;
3use crate::bytes_write::{Mp4Writable, WriteMp4};
4use crate::error::MP4Error;
5use crate::header::BoxHeader;
6use crate::id::BoxId;
7use crate::mp4box::box_trait::{BoxRead, BoxWrite, IBox};
8use crate::r#type::BoxType;
9use crate::size::BoxSize;
10use async_trait::async_trait;
11
12#[derive(Debug, Clone, Eq, PartialEq, Hash)]
13pub struct MdatBox(pub Vec<u8>);
14
15impl MdatBox {
16
17 fn inner_byte_size(&self) -> usize {
18 self.0.byte_size()
19 }
20
21 pub fn header(&self) -> BoxHeader {
22 BoxHeader::from_id_and_inner_size(Self::ID, self.inner_byte_size())
23 }
24
25}
26
27impl IBox for MdatBox {
28 fn byte_size(&self) -> usize {
29 self.header().byte_size() + self.inner_byte_size()
30 }
31
32 const ID: BoxType = BoxType::Id(BoxId(*b"mdat"));
33}
34
35#[async_trait]
36impl BoxRead for MdatBox {
37 async fn read<R: ReadMp4>(header: BoxHeader, reader: &mut R) -> Result<Self, MP4Error> {
38 let data = match header.size_minus_self() {
39 BoxSize::Known(size) => {
40 let mut vec = vec![0u8; size];
41 reader.read_exact(vec.as_mut_slice()).await?;
42 vec
43 }
44 BoxSize::Unknown => {
45 let mut vec = vec![];
46 reader.read_to_end(&mut vec).await?;
47 vec
48 }
49 };
50 Ok(Self(data))
51 }
52}
53
54
55impl BoxWrite for MdatBox {
56 fn write<W: WriteMp4>(&self, writer: &mut W) -> Result<usize, MP4Error> {
57 let mut count = 0;
58 count += self.header().write(writer)?;
59 count += writer.write(&self.0)?;
60 Ok(count)
61 }
62}