async_mp4/mp4box/
box_full.rs1use std::ops::{Deref, DerefMut};
2use crate::error::MP4Error;
3use crate::header::BoxHeader;
4use async_trait::async_trait;
5use crate::bytes_read::{Mp4Readable, ReadMp4};
6use crate::bytes_write::{FlagTrait, Mp4Writable, WriteMp4};
7use crate::mp4box::box_root::MP4Box;
8use crate::mp4box::box_trait::{PartialBox, PartialBoxRead, PartialBoxWrite};
9use crate::r#type::BoxType;
10
11#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
12pub struct FullBoxData<F: FlagTrait> {
13 pub version: u8,
14 pub flags: F
15}
16
17#[async_trait]
18impl<F: FlagTrait> Mp4Readable for FullBoxData<F> {
19 async fn read<R: ReadMp4>(reader: &mut R) -> Result<Self, MP4Error> {
20 let version = reader.read().await?;
21 let flags = reader.read_u24().await?.into();
22 Ok(Self{version, flags})
23 }
24}
25
26impl<F: FlagTrait> Mp4Writable for FullBoxData<F> {
27
28 fn byte_size(&self) -> usize {
29 4
30 }
31
32 fn write<W: WriteMp4>(&self, writer: &mut W) -> Result<usize, MP4Error> {
33 let mut count = 0;
34 count += self.version.write(writer)?;
35 count += writer.write_u24(self.flags.into())?;
36 Ok(count)
37 }
38}
39
40pub trait FullBoxInfo {
41 type Flag: FlagTrait;
42 fn version(&self) -> u8 {0}
43 fn flags(&self) -> Self::Flag {Self::Flag::default()}
44}
45
46#[derive(Debug, Clone, Default, Eq, PartialEq, Hash)]
47pub struct FullBox<P, F>
48 where
49 P: PartialBox<ParentData=FullBoxData<F>> + FullBoxInfo,
50 F: FlagTrait
51{
52 pub inner: P,
53}
54
55impl<P, F> From<P> for MP4Box<FullBox<P, F>>
56 where
57 P: PartialBox<ParentData=FullBoxData<F>> + FullBoxInfo,
58 F: FlagTrait
59{
60 fn from(inner: P) -> Self {
61 Self{inner: FullBox {inner}}
62 }
63}
64
65impl<P, F> PartialBox for FullBox<P, F> where
66 P: PartialBox<ParentData=FullBoxData<F>> + FullBoxInfo,
67 F: FlagTrait {
68 type ParentData = ();
69 type ThisData = FullBoxData<F>;
70
71 fn byte_size(&self) -> usize {
72 let version = 0;
73 let flags = F::default();
74 FullBoxData { version, flags }.byte_size() + self.inner.byte_size()
75 }
76
77 const ID: BoxType = P::ID;
78}
79
80#[async_trait]
81impl<P, F> PartialBoxRead for FullBox<P, F> where
82 P: PartialBox<ParentData=FullBoxData<F>> + PartialBoxRead + FullBoxInfo + Send + Sync,
83 F: FlagTrait,{
84 async fn read_data<R: ReadMp4>(_: Self::ParentData, reader: &mut R) -> Result<Self, MP4Error> {
85 let data = FullBoxData::read(reader).await?;
86 let inner = P::read_data(data, reader).await?;
87 Ok(Self { inner })
88 }
89
90 async fn read_child<R: ReadMp4>(&mut self, header: BoxHeader, reader: &mut R) -> Result<(), MP4Error> {
91 self.inner.read_child(header, reader).await
92 }
93}
94
95impl<P, F> PartialBoxWrite for FullBox<P, F> where
96 P: PartialBox<ParentData=FullBoxData<F>> + PartialBoxWrite + FullBoxInfo<Flag=F> + Send + Sync,
97 F: FlagTrait, {
98
99 fn write_data<W: WriteMp4>(&self, writer: &mut W) -> Result<usize, MP4Error> {
100 let mut count = 0;
101 let version = self.inner.version();
102 let flags = self.inner.flags();
103 count += FullBoxData::<F>{ version, flags }.write(writer)?;
104 count += self.inner.write_data(writer)?;
105 Ok(count)
106 }
107
108 fn write_children<W: WriteMp4>(&self, writer: &mut W) -> Result<usize, MP4Error> {
109 self.inner.write_children(writer)
110 }
111}
112
113impl<P, F> Deref for FullBox<P, F>
114 where
115 P: PartialBox<ParentData=FullBoxData<F>> + FullBoxInfo,
116 F: FlagTrait,
117{
118 type Target = P;
119
120 fn deref(&self) -> &Self::Target {
121 &self.inner
122 }
123}
124
125impl<P, F> DerefMut for FullBox<P, F>
126 where
127 P: PartialBox<ParentData=FullBoxData<F>> + FullBoxInfo,
128 F: FlagTrait,
129{
130 fn deref_mut(&mut self) -> &mut Self::Target {
131 &mut self.inner
132 }
133}
134
135