Skip to content

Commit 2654b8e

Browse files
committed
Collects unexpected boxes during decoding
This change introduces the ability to collect and retain any unexpected boxes encountered during the decoding process. Previously, the decoder would give an Error::unexpectedBox and failing the decode process. Now, these boxes are stored in a `unexpected` Vec within their parent structures (Moov, Trak, Mdia, Minf, Stbl, etc.). This allows for more robust handling of malformed or extended MP4 files, as the application can now inspect and process unexpected boxes as needed.
1 parent f744e0a commit 2654b8e

File tree

23 files changed

+118
-17
lines changed

23 files changed

+118
-17
lines changed

src/atom.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@ macro_rules! nested {
175175
$( let mut [<$optional:lower>] = None;)*
176176
$( let mut [<$multiple:lower>] = Vec::new();)*
177177

178+
// aggregate unexpected boxes, if any
179+
let mut unexpected = Vec::new();
180+
178181
while let Some(atom) = Any::decode_maybe(buf)? {
179182
match atom {
180183
$(Any::$required(atom) => {
@@ -195,14 +198,18 @@ macro_rules! nested {
195198
Any::Unknown(kind, _) => {
196199
tracing::warn!("unknown box: {:?}", kind);
197200
},
198-
_ => return Err(Error::UnexpectedBox(atom.kind())),
201+
_ => {
202+
tracing::warn!("unexpected box: {:?}", atom.kind());
203+
unexpected.push(atom);
204+
}
199205
}
200206
}
201207

202208
Ok(Self {
203209
$([<$required:lower>]: [<$required:lower>].ok_or(Error::MissingBox($required::KIND))? ,)*
204210
$([<$optional:lower>],)*
205211
$([<$multiple:lower>],)*
212+
unexpected
206213
})
207214
}
208215

src/meta/iprp.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::*;
88
pub struct Iprp {
99
pub ipco: Ipco,
1010
pub ipma: Vec<Ipma>,
11+
pub unexpected: Vec<Any>,
1112
}
1213

1314
impl Atom for Iprp {

src/meta/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ mod tests {
158158
location: "".into(),
159159
}],
160160
},
161+
unexpected: vec![],
161162
});
162163
expected.push(Iloc {
163164
item_locations: vec![ItemLocation {
@@ -229,6 +230,7 @@ mod tests {
229230
},
230231
],
231232
}],
233+
unexpected: vec![],
232234
});
233235
expected.push(Iref {
234236
references: vec![Reference {

src/moof/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ pub use traf::*;
66

77
use crate::*;
88

9-
#[derive(Debug, Clone, PartialEq, Eq, Default)]
9+
#[derive(Debug, Clone, PartialEq, Default)]
1010
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
1111
pub struct Moof {
1212
pub mfhd: Mfhd,
1313
pub traf: Vec<Traf>,
14+
pub unexpected: Vec<Any>,
1415
}
1516

1617
impl Atom for Moof {

src/moof/traf/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ pub use trun::*;
88

99
use crate::*;
1010

11-
#[derive(Debug, Clone, PartialEq, Eq, Default)]
11+
#[derive(Debug, Clone, PartialEq, Default)]
1212
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
1313
pub struct Traf {
1414
pub tfhd: Tfhd,
@@ -21,6 +21,7 @@ pub struct Traf {
2121
pub saio: Vec<Saio>,
2222
pub meta: Option<Meta>,
2323
pub udta: Option<Udta>,
24+
pub unexpected: Vec<Any>,
2425
}
2526

2627
impl Atom for Traf {

src/moov/mod.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub struct Moov {
1818
pub mvex: Option<Mvex>,
1919
pub trak: Vec<Trak>,
2020
pub udta: Option<Udta>,
21+
pub unexpected: Vec<Any>,
2122
}
2223

2324
impl Atom for Moov {
@@ -137,7 +138,8 @@ mod test {
137138
default_sample_description_index: 1,
138139
default_sample_duration: 3000,
139140
..Default::default()
140-
}]
141+
}],
142+
unexpected: vec![],
141143
}),
142144
trak: vec![Trak {
143145
tkhd: Tkhd {
@@ -156,7 +158,8 @@ mod test {
156158
media_rate: 1,
157159
..Default::default()
158160
}]
159-
})
161+
}),
162+
unexpected: vec![],
160163
}),
161164
meta: None,
162165
mdia: Mdia {
@@ -184,7 +187,8 @@ mod test {
184187
dinf: Dinf {
185188
dref: Dref {
186189
urls: vec![Url::default()]
187-
}
190+
},
191+
unexpected: vec![],
188192
},
189193
stbl: Stbl {
190194
stsd: Stsd {
@@ -224,12 +228,16 @@ mod test {
224228
},
225229
stco: Some(Stco::default()),
226230
..Default::default()
227-
}
228-
}
231+
},
232+
unexpected: vec![],
233+
},
234+
unexpected: vec![],
229235
},
230-
udta: None
236+
udta: None,
237+
unexpected: vec![],
231238
}],
232239
udta: None,
240+
unexpected: vec![],
233241
}
234242
)
235243
}

src/moov/mvex/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ pub use trex::*;
66

77
use crate::*;
88

9-
#[derive(Debug, Clone, PartialEq, Eq, Default)]
9+
#[derive(Debug, Clone, PartialEq, Default)]
1010
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
1111
pub struct Mvex {
1212
pub mehd: Option<Mehd>,
1313
pub trex: Vec<Trex>,
14+
pub unexpected: Vec<Any>,
1415
}
1516

1617
impl Atom for Mvex {

src/moov/trak/edts/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ pub use elst::*;
33

44
use crate::*;
55

6-
#[derive(Debug, Clone, PartialEq, Eq, Default)]
6+
#[derive(Debug, Clone, PartialEq, Default)]
77
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
88
pub struct Edts {
99
pub elst: Option<Elst>,
10+
pub unexpected: Vec<Any>,
1011
}
1112

1213
impl Atom for Edts {

src/moov/trak/mdia/minf/dinf/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ pub use dref::*;
33

44
use crate::*;
55

6-
#[derive(Debug, Clone, PartialEq, Eq, Default)]
6+
#[derive(Debug, Clone, PartialEq, Default)]
77
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
88
pub struct Dinf {
99
pub dref: Dref,
10+
pub unexpected: Vec<Any>,
1011
}
1112

1213
impl Atom for Dinf {

src/moov/trak/mdia/minf/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ pub use vmhd::*;
1010

1111
use crate::*;
1212

13-
#[derive(Debug, Clone, PartialEq, Eq, Default)]
13+
#[derive(Debug, Clone, PartialEq, Default)]
1414
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
1515
pub struct Minf {
1616
pub vmhd: Option<Vmhd>,
1717
pub smhd: Option<Smhd>,
1818
pub dinf: Dinf,
1919
pub stbl: Stbl,
20+
pub unexpected: Vec<Any>,
2021
}
2122

2223
impl Atom for Minf {

0 commit comments

Comments
 (0)