forked from mstoykov/mp4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mdia.go
80 lines (73 loc) · 1.26 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
78
79
80
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, size uint64) (Box, error) {
l, err := DecodeContainer(r, size)
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, &BadFormatErr{
enclosingBox: "mdia",
unexpectedBox: b.Type(),
}
}
}
return m, nil
}
func (b *MdiaBox) Type() string {
return "mdia"
}
func (b *MdiaBox) Size() uint64 {
sz := AddHeaderSize(b.Mdhd.Size())
if b.Hdlr != nil {
sz += AddHeaderSize(b.Hdlr.Size())
}
if b.Minf != nil {
sz += AddHeaderSize(b.Minf.Size())
}
return sz
}
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)
}