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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
package mp4
import (
"errors"
"fmt"
"reflect"
"strings"
)
var ErrBoxInfoNotFound = errors.New("box info not found")
// BoxType is mpeg box type
type BoxType [4]byte
func StrToBoxType(code string) BoxType {
if len(code) != 4 {
panic(fmt.Errorf("invalid box type id length: [%s]", code))
}
return BoxType{code[0], code[1], code[2], code[3]}
}
func (boxType BoxType) String() string {
if isPrintable(boxType[0]) && isPrintable(boxType[1]) && isPrintable(boxType[2]) && isPrintable(boxType[3]) {
s := string([]byte{boxType[0], boxType[1], boxType[2], boxType[3]})
s = strings.ReplaceAll(s, string([]byte{0xa9}), "(c)")
return s
}
return fmt.Sprintf("0x%02x%02x%02x%02x", boxType[0], boxType[1], boxType[2], boxType[3])
}
func isASCII(c byte) bool {
return c >= 0x20 && c <= 0x7e
}
func isPrintable(c byte) bool {
return isASCII(c) || c == 0xa9
}
func (lhs BoxType) MatchWith(rhs BoxType) bool {
if lhs == boxTypeAny || rhs == boxTypeAny {
return true
}
return lhs == rhs
}
var boxTypeAny = BoxType{0x00, 0x00, 0x00, 0x00}
func BoxTypeAny() BoxType {
return boxTypeAny
}
type boxDef struct {
dataType reflect.Type
versions []uint8
isTarget func(Context) bool
fields []*field
}
var boxMap = make(map[BoxType][]boxDef, 64)
func AddBoxDef(payload IBox, versions ...uint8) {
boxMap[payload.GetType()] = append(boxMap[payload.GetType()], boxDef{
dataType: reflect.TypeOf(payload).Elem(),
versions: versions,
fields: buildFields(payload),
})
}
func AddBoxDefEx(payload IBox, isTarget func(Context) bool, versions ...uint8) {
boxMap[payload.GetType()] = append(boxMap[payload.GetType()], boxDef{
dataType: reflect.TypeOf(payload).Elem(),
versions: versions,
isTarget: isTarget,
fields: buildFields(payload),
})
}
func AddAnyTypeBoxDef(payload IAnyType, boxType BoxType, versions ...uint8) {
boxMap[boxType] = append(boxMap[boxType], boxDef{
dataType: reflect.TypeOf(payload).Elem(),
versions: versions,
fields: buildFields(payload),
})
}
func AddAnyTypeBoxDefEx(payload IAnyType, boxType BoxType, isTarget func(Context) bool, versions ...uint8) {
boxMap[boxType] = append(boxMap[boxType], boxDef{
dataType: reflect.TypeOf(payload).Elem(),
versions: versions,
isTarget: isTarget,
fields: buildFields(payload),
})
}
func (boxType BoxType) getBoxDef(ctx Context) *boxDef {
boxDefs := boxMap[boxType]
for i := len(boxDefs) - 1; i >= 0; i-- {
boxDef := &boxDefs[i]
if boxDef.isTarget == nil || boxDef.isTarget(ctx) {
return boxDef
}
}
return nil
}
func (boxType BoxType) IsSupported(ctx Context) bool {
return boxType.getBoxDef(ctx) != nil
}
func (boxType BoxType) New(ctx Context) (IBox, error) {
boxDef := boxType.getBoxDef(ctx)
if boxDef == nil {
return nil, ErrBoxInfoNotFound
}
box, ok := reflect.New(boxDef.dataType).Interface().(IBox)
if !ok {
return nil, fmt.Errorf("box type not implements IBox interface: %s", boxType.String())
}
anyTypeBox, ok := box.(IAnyType)
if ok {
anyTypeBox.SetType(boxType)
}
return box, nil
}
func (boxType BoxType) GetSupportedVersions(ctx Context) ([]uint8, error) {
boxDef := boxType.getBoxDef(ctx)
if boxDef == nil {
return nil, ErrBoxInfoNotFound
}
return boxDef.versions, nil
}
func (boxType BoxType) IsSupportedVersion(ver uint8, ctx Context) bool {
boxDef := boxType.getBoxDef(ctx)
if boxDef == nil {
return false
}
if len(boxDef.versions) == 0 {
return true
}
for _, sver := range boxDef.versions {
if ver == sver {
return true
}
}
return false
}
|