-
Notifications
You must be signed in to change notification settings - Fork 0
/
descriptor.go
43 lines (33 loc) · 820 Bytes
/
descriptor.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
package descriptor
import (
"github.com/pkg/errors"
)
var (
descriptorRegistry = make(map[uint8]func() Descriptor)
)
// Register registers a new tag to a function that will return an appropriate Descriptor for that tag.
func Register(tag uint8, fn func() Descriptor) {
descriptorRegistry[tag] = fn
}
// Descriptor defines an MPEG-TS Descriptor.
type Descriptor interface {
Tag() uint8
Len() int
Marshal() ([]byte, error)
Unmarshal([]byte) error
}
func defaultDescriptor() Descriptor {
return new(raw)
}
// Unmarshal decodes an arbitrary Descriptor from a byte slice.
func Unmarshal(b []byte) (d Descriptor, err error) {
if len(b) < 1 {
return nil, errors.New("empty buffer")
}
fn := descriptorRegistry[uint8(b[0])]
if fn == nil {
fn = defaultDescriptor
}
d = fn()
return d, d.Unmarshal(b)
}