1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
use uuid::Uuid;
use crate::bytes_read::{Mp4Readable, ReadMp4};
use crate::bytes_write::{Mp4Writable, WriteMp4};
use crate::error::MP4Error;
use crate::id::BoxId;
use crate::r#type::BoxType;
use crate::size::BoxSize;
use crate::size::BoxSize::Known;
use crate::size::BoxSize::Unknown;
use async_trait::async_trait;
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub struct BoxHeader {
pub size: BoxSize,
pub id: BoxType
}
impl BoxHeader {
pub fn from_id_and_inner_size(id: BoxType, inner_size: usize) -> Self {
Self {
size: BoxSize::from_size_without_self(inner_size + id.byte_size()),
id
}
}
pub fn size_minus_self(&self) -> BoxSize {
match self.size {
Known(size) => Known(self.byte_size() - size),
Unknown => Unknown
}
}
}
#[async_trait]
impl Mp4Readable for BoxHeader {
async fn read<R: ReadMp4>(reader: &mut R) -> Result<Self, MP4Error> {
let size: u32 = reader.read().await?;
let id = BoxId::read(reader).await?;
let size = match size {
0 => Unknown,
1 => {
let size: u64 = reader.read().await?;
Known(size as _)
}
_ => Known(size as _)
};
let id = if id == b"uuid" {
BoxType::UUID(reader.read().await?)
} else {
BoxType::Id(id)
};
Ok(Self {
size, id
})
}
}
impl Mp4Writable for BoxHeader {
fn byte_size(&self) -> usize {
self.size.byte_size() + self.id.byte_size()
}
fn write<W: WriteMp4>(&self, writer: &mut W) -> Result<usize, MP4Error> {
let (id, uuid) = match self.id {
BoxType::Id(id) => (id, None),
BoxType::UUID(uuid) => (BoxId(*b"uuid"), Some(uuid))
};
let (size, big_size) = match self.size {
Known(size) => {
if cfg!(target_pointer_width = "64") {
if size > u32::MAX as usize {
(1, Some(size as u64))
} else {
(size as u32, None)
}
} else {
(size as u32, None)
}
}
Unknown => (0, None)
};
let mut count = 0;
count += size.write(writer)?;
count += id.write(writer)?;
count += big_size.write(writer)?;
count += uuid.write(writer)?;
Ok(count)
}
}
#[async_trait]
impl Mp4Readable for Uuid {
async fn read<R: ReadMp4>(reader: &mut R) -> Result<Self, MP4Error> {
Ok(Uuid::from_bytes(reader.read().await?))
}
}
impl Mp4Writable for Uuid {
fn byte_size(&self) -> usize {
self.as_bytes().byte_size()
}
fn write<W: WriteMp4>(&self, writer: &mut W) -> Result<usize, MP4Error> {
self.as_bytes().write(writer)
}
}