-
Notifications
You must be signed in to change notification settings - Fork 84
/
kind.go
87 lines (77 loc) · 2.06 KB
/
kind.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
package mp4
import (
"fmt"
"io"
"github.com/Eyevinn/mp4ff/bits"
)
// KindBox - Track Kind Box
type KindBox struct {
Version byte
Flags uint32
SchemeURI string
Value string
}
// DecodeKind - box-specific decode
func DecodeKind(hdr BoxHeader, startPos uint64, r io.Reader) (Box, error) {
data, err := readBoxBody(r, hdr)
if err != nil {
return nil, err
}
sr := bits.NewFixedSliceReader(data)
return DecodeKindSR(hdr, startPos, sr)
}
// DecodeKindSR - box-specific decode
func DecodeKindSR(hdr BoxHeader, startPos uint64, sr bits.SliceReader) (Box, error) {
versionAndFlags := sr.ReadUint32()
maxLen := hdr.payloadLen() - 4 - 1
schemeURI := sr.ReadZeroTerminatedString(maxLen)
maxLen = hdr.payloadLen() - 4 - len(schemeURI) - 1
value := sr.ReadZeroTerminatedString(maxLen)
if err := sr.AccError(); err != nil {
return nil, fmt.Errorf("decode kind: %w", err)
}
b := KindBox{
Version: byte(versionAndFlags >> 24),
Flags: versionAndFlags & flagsMask,
SchemeURI: schemeURI,
Value: value,
}
return &b, nil
}
// Type - box type
func (b *KindBox) Type() string {
return "kind"
}
// Size - calculated size of box
func (b *KindBox) Size() uint64 {
return uint64(boxHeaderSize + 4 + len(b.SchemeURI) + 1 + len(b.Value) + 1)
}
// Encode - write box to w
func (b *KindBox) Encode(w io.Writer) error {
sw := bits.NewFixedSliceWriter(int(b.Size()))
err := b.EncodeSW(sw)
if err != nil {
return err
}
_, err = w.Write(sw.Bytes())
return err
}
// Encode - write box to w
func (b *KindBox) EncodeSW(sw bits.SliceWriter) error {
err := EncodeHeaderSW(b, sw)
if err != nil {
return err
}
versionAndFlags := (uint32(b.Version) << 24) + b.Flags
sw.WriteUint32(versionAndFlags)
sw.WriteString(b.SchemeURI, true)
sw.WriteString(b.Value, true)
return sw.AccError()
}
// Info - write box-specific information
func (b *KindBox) Info(w io.Writer, specificBoxLevels, indent, indentStep string) error {
bd := newInfoDumper(w, indent, b, int(b.Version), b.Flags)
bd.write(" - schemeURI: %s", b.SchemeURI)
bd.write(" - value: %s", b.Value)
return bd.err
}