-
Notifications
You must be signed in to change notification settings - Fork 22
/
trak.go
74 lines (67 loc) · 1.12 KB
/
trak.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
package mp4
import "io"
// Track Box (tkhd - mandatory)
//
// Contained in : Movie Box (moov)
//
// A media file can contain one or more tracks.
type TrakBox struct {
Tkhd *TkhdBox
Mdia *MdiaBox
Edts *EdtsBox
}
func DecodeTrak(r io.Reader) (Box, error) {
l, err := DecodeContainer(r)
if err != nil {
return nil, err
}
t := &TrakBox{}
for _, b := range l {
switch b.Type() {
case "tkhd":
t.Tkhd = b.(*TkhdBox)
case "mdia":
t.Mdia = b.(*MdiaBox)
case "edts":
t.Edts = b.(*EdtsBox)
default:
return nil, ErrBadFormat
}
}
return t, nil
}
func (b *TrakBox) Type() string {
return "trak"
}
func (b *TrakBox) Size() int {
sz := b.Tkhd.Size()
sz += b.Mdia.Size()
if b.Edts != nil {
sz += b.Edts.Size()
}
return sz + BoxHeaderSize
}
func (b *TrakBox) Dump() {
b.Tkhd.Dump()
if b.Edts != nil {
b.Edts.Dump()
}
b.Mdia.Dump()
}
func (b *TrakBox) Encode(w io.Writer) error {
err := EncodeHeader(b, w)
if err != nil {
return err
}
err = b.Tkhd.Encode(w)
if err != nil {
return err
}
if b.Edts != nil {
err = b.Edts.Encode(w)
if err != nil {
return err
}
}
return b.Mdia.Encode(w)
}