forked from godeep/mp4
-
Notifications
You must be signed in to change notification settings - Fork 6
/
mdia.go
77 lines (70 loc) · 1.15 KB
/
mdia.go
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
package mp4
import "io"
// Media Box (mdia - mandatory)
//
// Contained in : Track Box (trak)
//
// Status: decoded
//
// Contains all information about the media data.
type MdiaBox struct {
Mdhd *MdhdBox
Hdlr *HdlrBox
Minf *MinfBox
}
func DecodeMdia(r io.Reader) (Box, error) {
l, err := DecodeContainer(r)
if err != nil {
return nil, err
}
m := &MdiaBox{}
for _, b := range l {
switch b.Type() {
case "mdhd":
m.Mdhd = b.(*MdhdBox)
case "hdlr":
m.Hdlr = b.(*HdlrBox)
case "minf":
m.Minf = b.(*MinfBox)
default:
return nil, ErrBadFormat
}
}
return m, nil
}
func (b *MdiaBox) Type() string {
return "mdia"
}
func (b *MdiaBox) Size() int {
sz := b.Mdhd.Size()
if b.Hdlr != nil {
sz += b.Hdlr.Size()
}
if b.Minf != nil {
sz += b.Minf.Size()
}
return sz + BoxHeaderSize
}
func (b *MdiaBox) Dump() {
b.Mdhd.Dump()
if b.Minf != nil {
b.Minf.Dump()
}
}
func (b *MdiaBox) Encode(w io.Writer) error {
err := EncodeHeader(b, w)
if err != nil {
return err
}
err = b.Mdhd.Encode(w)
if err != nil {
return err
}
if b.Hdlr != nil {
err = b.Hdlr.Encode(w)
if err != nil {
return err
}
}
return b.Minf.Encode(w)
}