-
Notifications
You must be signed in to change notification settings - Fork 1
/
mehd.go
78 lines (70 loc) · 1.64 KB
/
mehd.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
package mp4
import (
"io"
"io/ioutil"
)
// MehdBox - Movie Extends Header Box
// Optional, provides overall duration of a fragmented movie
type MehdBox struct {
Version byte
Flags uint32
FragmentDuration int64
}
// DecodeMehd - box-specific decode
func DecodeMehd(hdr *boxHeader, startPos uint64, r io.Reader) (Box, error) {
data, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
s := NewSliceReader(data)
versionAndFlags := s.ReadUint32()
version := byte(versionAndFlags >> 24)
b := &MehdBox{
Version: version,
Flags: versionAndFlags & flagsMask,
}
if version == 0 {
b.FragmentDuration = int64(s.ReadInt32())
} else {
b.FragmentDuration = s.ReadInt64()
}
return b, nil
}
// Type - return box type
func (b *MehdBox) Type() string {
return "mehd"
}
// Size - return calculated size
func (b *MehdBox) Size() uint64 {
size := uint64(boxHeaderSize) + 4
if b.Version == 0 {
size += 4
} else {
size += 8
}
return size
}
// Encode - write box to w
func (b *MehdBox) Encode(w io.Writer) error {
err := EncodeHeader(b, w)
if err != nil {
return err
}
buf := makebuf(b)
sw := NewSliceWriter(buf)
versionAndFlags := (uint32(b.Version) << 24) + b.Flags
sw.WriteUint32(versionAndFlags)
if b.Version == 0 {
sw.WriteUint32(uint32(b.FragmentDuration))
} else {
sw.WriteUint64(uint64(b.FragmentDuration))
}
_, err = w.Write(buf)
return err
}
// Dump - write MehBox details.
func (b *MehdBox) Info(w io.Writer, specificBoxLevels, indent, indentStep string) (err error) {
bd := newInfoDumper(w, indent, b, int(b.Version), b.Flags)
bd.write(" - fragmentDuration: %d", b.FragmentDuration)
return bd.err
}