forked from Eyevinn/mp4ff
-
Notifications
You must be signed in to change notification settings - Fork 1
/
container.go
75 lines (69 loc) · 1.51 KB
/
container.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
package mp4
import (
"fmt"
"io"
)
// ContainerBox is interface for ContainerBoxes
type ContainerBox interface {
Type() string
Size() uint64
Encode(w io.Writer) error
GetChildren() []Box
Info(w io.Writer, specificBoxLevels, indent, indentStep string) error
}
func containerSize(boxes []Box) uint64 {
var contentSize uint64 = 0
for _, box := range boxes {
contentSize += box.Size()
}
return boxHeaderSize + contentSize
}
// DecodeContainerChildren decodes a container box
func DecodeContainerChildren(hdr *boxHeader, startPos, endPos uint64, r io.Reader) ([]Box, error) {
l := []Box{}
pos := startPos
for {
b, err := DecodeBox(pos, r)
if err == io.EOF {
return l, nil
}
if err != nil {
return l, err
}
l = append(l, b)
pos += b.Size()
if pos == endPos {
return l, nil
} else if pos > endPos {
return nil, fmt.Errorf("Non-matching children box sizes")
}
}
}
// EncodeContainer - marshal container c to w
func EncodeContainer(c ContainerBox, w io.Writer) error {
err := EncodeHeader(c, w)
if err != nil {
return err
}
for _, b := range c.GetChildren() {
err = b.Encode(w)
if err != nil {
return err
}
}
return nil
}
func ContainerInfo(c ContainerBox, w io.Writer, specificBoxLevels, indent, indentStep string) error {
bd := newInfoDumper(w, indent, c, -1, 0)
if bd.err != nil {
return bd.err
}
var err error
for _, child := range c.GetChildren() {
err := child.Info(w, specificBoxLevels, indent+indentStep, indentStep)
if err != nil {
return err
}
}
return err
}