github.com/puellanivis/breton@v0.2.16/lib/mpeg/ts/descriptor/descriptor.go (about) 1 package descriptor 2 3 import ( 4 "github.com/pkg/errors" 5 ) 6 7 var ( 8 descriptorRegistry = make(map[uint8]func() Descriptor) 9 ) 10 11 // Register registers a new tag to a function that will return an appropriate Descriptor for that tag. 12 func Register(tag uint8, fn func() Descriptor) { 13 descriptorRegistry[tag] = fn 14 } 15 16 // Descriptor defines an MPEG-TS Descriptor. 17 type Descriptor interface { 18 Tag() uint8 19 Len() int 20 21 Marshal() ([]byte, error) 22 Unmarshal([]byte) error 23 } 24 25 func defaultDescriptor() Descriptor { 26 return new(raw) 27 } 28 29 // Unmarshal decodes an arbitrary Descriptor from a byte slice. 30 func Unmarshal(b []byte) (d Descriptor, err error) { 31 if len(b) < 1 { 32 return nil, errors.New("empty buffer") 33 } 34 35 fn := descriptorRegistry[uint8(b[0])] 36 if fn == nil { 37 fn = defaultDescriptor 38 } 39 40 d = fn() 41 42 return d, d.Unmarshal(b) 43 }