@@ -9,7 +9,7 @@ pub struct Mp4aBox {
9
9
pub channelcount : u16 ,
10
10
pub samplesize : u16 ,
11
11
pub samplerate : FixedPointU16 ,
12
- pub esds : EsdsBox ,
12
+ pub esds : Option < EsdsBox > ,
13
13
}
14
14
15
15
impl Default for Mp4aBox {
@@ -19,7 +19,7 @@ impl Default for Mp4aBox {
19
19
channelcount : 2 ,
20
20
samplesize : 16 ,
21
21
samplerate : FixedPointU16 :: new ( 48000 ) ,
22
- esds : EsdsBox :: default ( ) ,
22
+ esds : Some ( EsdsBox :: default ( ) ) ,
23
23
}
24
24
}
25
25
}
@@ -31,7 +31,7 @@ impl Mp4aBox {
31
31
channelcount : config. chan_conf as u16 ,
32
32
samplesize : 16 ,
33
33
samplerate : FixedPointU16 :: new ( config. freq_index . freq ( ) as u16 ) ,
34
- esds : EsdsBox :: new ( config) ,
34
+ esds : Some ( EsdsBox :: new ( config) ) ,
35
35
}
36
36
}
37
37
}
@@ -42,7 +42,11 @@ impl Mp4Box for Mp4aBox {
42
42
}
43
43
44
44
fn box_size ( & self ) -> u64 {
45
- HEADER_SIZE + 8 + 20 + self . esds . box_size ( )
45
+ let mut size = HEADER_SIZE + 8 + 20 ;
46
+ if let Some ( ref esds) = self . esds {
47
+ size += esds. box_size ( ) ;
48
+ }
49
+ size
46
50
}
47
51
}
48
52
@@ -62,21 +66,20 @@ impl<R: Read + Seek> ReadBox<&mut R> for Mp4aBox {
62
66
63
67
let header = BoxHeader :: read ( reader) ?;
64
68
let BoxHeader { name, size : s } = header;
69
+
70
+ let mut esds = None ;
65
71
if name == BoxType :: EsdsBox {
66
- let esds = EsdsBox :: read_box ( reader, s) ?;
67
-
68
- skip_bytes_to ( reader, start + size) ?;
69
-
70
- Ok ( Mp4aBox {
71
- data_reference_index,
72
- channelcount,
73
- samplesize,
74
- samplerate,
75
- esds,
76
- } )
77
- } else {
78
- Err ( Error :: InvalidData ( "esds not found" ) )
72
+ esds = Some ( EsdsBox :: read_box ( reader, s) ?) ;
79
73
}
74
+ skip_bytes_to ( reader, start + size) ?;
75
+
76
+ Ok ( Mp4aBox {
77
+ data_reference_index,
78
+ channelcount,
79
+ samplesize,
80
+ samplerate,
81
+ esds,
82
+ } )
80
83
}
81
84
}
82
85
@@ -95,7 +98,9 @@ impl<W: Write> WriteBox<&mut W> for Mp4aBox {
95
98
writer. write_u32 :: < BigEndian > ( 0 ) ?; // reserved
96
99
writer. write_u32 :: < BigEndian > ( self . samplerate . raw_value ( ) ) ?;
97
100
98
- self . esds . write_box ( writer) ?;
101
+ if let Some ( ref esds) = self . esds {
102
+ esds. write_box ( writer) ?;
103
+ }
99
104
100
105
Ok ( size)
101
106
}
@@ -524,7 +529,7 @@ mod tests {
524
529
channelcount : 2 ,
525
530
samplesize : 16 ,
526
531
samplerate : FixedPointU16 :: new ( 48000 ) ,
527
- esds : EsdsBox {
532
+ esds : Some ( EsdsBox {
528
533
version : 0 ,
529
534
flags : 0 ,
530
535
es_desc : ESDescriptor {
@@ -544,7 +549,29 @@ mod tests {
544
549
} ,
545
550
sl_config : SLConfigDescriptor :: default ( ) ,
546
551
} ,
547
- } ,
552
+ } ) ,
553
+ } ;
554
+ let mut buf = Vec :: new ( ) ;
555
+ src_box. write_box ( & mut buf) . unwrap ( ) ;
556
+ assert_eq ! ( buf. len( ) , src_box. box_size( ) as usize ) ;
557
+
558
+ let mut reader = Cursor :: new ( & buf) ;
559
+ let header = BoxHeader :: read ( & mut reader) . unwrap ( ) ;
560
+ assert_eq ! ( header. name, BoxType :: Mp4aBox ) ;
561
+ assert_eq ! ( src_box. box_size( ) , header. size) ;
562
+
563
+ let dst_box = Mp4aBox :: read_box ( & mut reader, header. size ) . unwrap ( ) ;
564
+ assert_eq ! ( src_box, dst_box) ;
565
+ }
566
+
567
+ #[ test]
568
+ fn test_mp4a_no_esds ( ) {
569
+ let src_box = Mp4aBox {
570
+ data_reference_index : 1 ,
571
+ channelcount : 2 ,
572
+ samplesize : 16 ,
573
+ samplerate : FixedPointU16 :: new ( 48000 ) ,
574
+ esds : None ,
548
575
} ;
549
576
let mut buf = Vec :: new ( ) ;
550
577
src_box. write_box ( & mut buf) . unwrap ( ) ;
0 commit comments