-
Notifications
You must be signed in to change notification settings - Fork 1
/
moof.go
89 lines (79 loc) · 1.82 KB
/
moof.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
86
87
88
89
package mp4
import (
"fmt"
"io"
)
// MoofBox - Movie Fragment Box (moof)
//
// Contains all meta-data. To be able to stream a file, the moov box should be placed before the mdat box.
type MoofBox struct {
Mfhd *MfhdBox
Traf *TrafBox // The first traf child box
Trafs []*TrafBox
Children []Box
StartPos uint64
}
// DecodeMoof - box-specific decode
func DecodeMoof(hdr *boxHeader, startPos uint64, r io.Reader) (Box, error) {
children, err := DecodeContainerChildren(hdr, startPos+8, startPos+hdr.size, r)
if err != nil {
return nil, err
}
m := &MoofBox{}
m.StartPos = startPos
for _, box := range children {
err := m.AddChild(box)
if err != nil {
return nil, err
}
}
return m, nil
}
// AddChild - add child box
func (m *MoofBox) AddChild(b Box) error {
switch b.Type() {
case "mfhd":
m.Mfhd = b.(*MfhdBox)
case "traf":
if m.Traf == nil {
m.Traf = b.(*TrafBox)
}
m.Trafs = append(m.Trafs, b.(*TrafBox))
}
m.Children = append(m.Children, b)
return nil
}
// Type - returns box type
func (m *MoofBox) Type() string {
return "moof"
}
// Size - returns calculated size
func (m *MoofBox) Size() uint64 {
return containerSize(m.Children)
}
// Encode - write moof after updating trun dataoffset
func (m *MoofBox) Encode(w io.Writer) error {
for _, trun := range m.Traf.Truns {
if trun.HasDataOffset() && trun.DataOffset == 0 {
return fmt.Errorf("Dataoffset in trun not set")
}
}
err := EncodeHeader(m, w)
if err != nil {
return err
}
for _, b := range m.Children {
err = b.Encode(w)
if err != nil {
return err
}
}
return nil
}
// GetChildren - list of child boxes
func (m *MoofBox) GetChildren() []Box {
return m.Children
}
func (m *MoofBox) Info(w io.Writer, specificBoxLevels, indent, indentStep string) error {
return ContainerInfo(m, w, specificBoxLevels, indent, indentStep)
}