forked from Eyevinn/mp4ff
-
Notifications
You must be signed in to change notification settings - Fork 1
/
schm.go
84 lines (76 loc) · 1.86 KB
/
schm.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
package mp4
import (
"io"
"io/ioutil"
)
// SchmBox - Scheme Type Box
type SchmBox struct {
Version byte
Flags uint32
SchemeType string // 4CC represented as uint32
SchemeVersion uint32
SchemeUri string // Absolute null-terminated URL
}
// DecodeSchm - box-specific decode
func DecodeSchm(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 := &SchmBox{
Version: version,
Flags: versionAndFlags & flagsMask,
}
b.SchemeType = s.ReadFixedLengthString(4)
b.SchemeVersion = s.ReadUint32()
if b.Flags&0x01 != 0 {
b.SchemeUri, err = s.ReadZeroTerminatedString()
if err != nil {
return nil, err
}
}
return b, nil
}
// Type - return box type
func (b *SchmBox) Type() string {
return "schm"
}
// Size - return calculated size
func (b *SchmBox) Size() uint64 {
size := uint64(20)
if b.Flags&0x01 != 0 {
size += uint64(len(b.SchemeUri) + 1)
}
return size
}
// Encode - write box to w
func (b *SchmBox) 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)
sw.WriteString(b.SchemeType, false)
sw.WriteUint32(b.SchemeVersion)
if b.Flags&0x01 != 0 {
sw.WriteString(b.SchemeUri, true)
}
_, err = w.Write(buf)
return err
}
// Info - write box info to w
func (b *SchmBox) Info(w io.Writer, specificBoxLevels, indent, indentStep string) (err error) {
bd := newInfoDumper(w, indent, b, int(b.Version), b.Flags)
bd.write(" - schemeType: %s", b.SchemeType)
bd.write(" - schemeVersion: %d", b.SchemeVersion)
if b.Flags&0x01 != 0 {
bd.write(" - schemeURI: %q", b.SchemeUri)
}
return bd.err
}