forked from Eyevinn/mp4ff
-
Notifications
You must be signed in to change notification settings - Fork 1
/
moov.go
85 lines (74 loc) · 1.79 KB
/
moov.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
81
82
83
84
85
package mp4
import (
"io"
)
// MoovBox - Movie Box (moov - mandatory)
//
// Contains all meta-data. To be able to stream a file, the moov box should be placed before the mdat box.
type MoovBox struct {
Mvhd *MvhdBox
Trak *TrakBox // The first trak box
Traks []*TrakBox
Mvex *MvexBox
Children []Box
}
// NewMoovBox - Generate a new empty moov box
func NewMoovBox() *MoovBox {
return &MoovBox{}
}
// AddChild - Add a child box
func (m *MoovBox) AddChild(box Box) {
switch box.Type() {
case "mvhd":
m.Mvhd = box.(*MvhdBox)
case "trak":
trak := box.(*TrakBox)
if m.Trak == nil {
m.Trak = trak
}
m.Traks = append(m.Traks, trak)
if m.Mvex != nil { // Need to insert before mvex box
for i, child := range m.Children {
if child == m.Mvex {
m.Children = append(m.Children[:i+1], m.Children[i:]...)
m.Children[i] = trak
return
}
}
}
case "mvex":
m.Mvex = box.(*MvexBox)
}
m.Children = append(m.Children, box)
}
// DecodeMoov - box-specific decode
func DecodeMoov(hdr *boxHeader, startPos uint64, r io.Reader) (Box, error) {
l, err := DecodeContainerChildren(hdr, startPos+8, startPos+hdr.size, r)
if err != nil {
return nil, err
}
m := NewMoovBox()
for _, b := range l {
m.AddChild(b)
}
return m, err
}
// Type - box type
func (m *MoovBox) Type() string {
return "moov"
}
// Size - calculated size of box
func (m *MoovBox) Size() uint64 {
return containerSize(m.Children)
}
// GetChildren - list of child boxes
func (m *MoovBox) GetChildren() []Box {
return m.Children
}
// Encode - write moov container to w
func (m *MoovBox) Encode(w io.Writer) error {
return EncodeContainer(m, w)
}
func (m *MoovBox) Info(w io.Writer, specificBoxLevels, indent, indentStep string) error {
return ContainerInfo(m, w, specificBoxLevels, indent, indentStep)
}