forked from Eyevinn/mp4ff
-
Notifications
You must be signed in to change notification settings - Fork 1
/
emsg.go
125 lines (116 loc) · 3.29 KB
/
emsg.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package mp4
import (
"fmt"
"io"
"io/ioutil"
)
// EmsgBox - DASHEventMessageBox as defined in ISO/IEC 23009-1
type EmsgBox struct {
Version byte
Flags uint32
TimeScale uint32
PresentationTimeDelta uint32
PresentationTime uint64
EventDuration uint32
Id uint32
SchemeIdURI string
Value string
}
// DecodeEmsg - box-specific decode
func DecodeEmsg(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 := &EmsgBox{
Version: version,
Flags: versionAndFlags & flagsMask,
}
if version == 1 {
b.TimeScale = s.ReadUint32()
b.PresentationTime = s.ReadUint64()
b.EventDuration = s.ReadUint32()
b.Id = s.ReadUint32()
b.SchemeIdURI, err = s.ReadZeroTerminatedString()
if err != nil {
return nil, fmt.Errorf("Read schemedIDUri error in emsg")
}
b.Value, err = s.ReadZeroTerminatedString()
if err != nil {
return nil, fmt.Errorf("Read schemedIDUri error in emsg")
}
} else if version == 0 {
b.SchemeIdURI, err = s.ReadZeroTerminatedString()
if err != nil {
return nil, fmt.Errorf("Read schemedIDUri error in emsg")
}
b.Value, err = s.ReadZeroTerminatedString()
if err != nil {
return nil, fmt.Errorf("Read schemedIDUri error in emsg")
}
b.TimeScale = s.ReadUint32()
b.PresentationTimeDelta = s.ReadUint32()
b.EventDuration = s.ReadUint32()
b.Id = s.ReadUint32()
} else {
return nil, fmt.Errorf("Unknown version for emsg")
}
return b, nil
}
// Type - box type
func (b *EmsgBox) Type() string {
return "emsg"
}
// Size - calculated size of box
func (b *EmsgBox) Size() uint64 {
if b.Version == 1 {
return uint64(boxHeaderSize + 4 + 4 + 8 + 4 + 4 + len(b.SchemeIdURI) + 1 + len(b.Value) + 1)
}
return uint64(boxHeaderSize + 4 + len(b.SchemeIdURI) + 1 + len(b.Value) + 1 + 4 + 4 + 4 + 4) // m.Version == 0
}
// Encode - write box to w
func (b *EmsgBox) Encode(w io.Writer) error {
err := EncodeHeader(b, w)
if err != nil {
return err
}
buf := make([]byte, b.Size()-boxHeaderSize)
sw := NewSliceWriter(buf)
versionAndFlags := (uint32(b.Version) << 24) + b.Flags
sw.WriteUint32(versionAndFlags)
if b.Version == 1 {
sw.WriteUint32(b.TimeScale)
sw.WriteUint64(b.PresentationTime)
sw.WriteUint32(b.EventDuration)
sw.WriteUint32(b.Id)
sw.WriteString(b.SchemeIdURI, true)
sw.WriteString(b.Value, true)
} else {
sw.WriteString(b.SchemeIdURI, true)
sw.WriteString(b.Value, true)
sw.WriteUint32(b.TimeScale)
sw.WriteUint32(b.PresentationTimeDelta)
sw.WriteUint32(b.EventDuration)
sw.WriteUint32(b.Id)
}
_, err = w.Write(buf)
return err
}
func (b *EmsgBox) Info(w io.Writer, specificBoxLevels, indent, indentStep string) error {
bd := newInfoDumper(w, indent, b, int(b.Version), b.Flags)
bd.write(" - timeScale: %d", b.TimeScale)
if b.Version > 0 {
bd.write(" - presentationTime: %d", b.PresentationTime)
}
bd.write(" - eventDuration: %d", b.EventDuration)
bd.write(" - id: %d", b.Id)
bd.write(" - schedIdURI: %s", b.SchemeIdURI)
bd.write(" - value: %s", b.Value)
if b.Version == 0 {
bd.write(" - presentationTimeDelta: %d", b.PresentationTimeDelta)
}
return bd.err
}