-
Notifications
You must be signed in to change notification settings - Fork 1
/
demuxer.go
70 lines (55 loc) · 1.49 KB
/
demuxer.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
package flv
import (
"bytes"
"encoding/binary"
"fmt"
"io"
)
type Demuxer interface {
ReadHeader(*HeaderCompo) error
ReadTag(*TagCompo) error
Close() error
}
func NewDemuxer(r io.ReadCloser) (Demuxer, error) {
return &demuxer{
r: r,
}, nil
}
type demuxer struct {
r io.ReadCloser
}
func (d *demuxer) ReadHeader(c *HeaderCompo) (err error) {
if _, err = io.ReadFull(d.r, c.Raw[:]); err != nil {
return
}
reader := bytes.NewReader(c.Raw[:])
if err = binary.Read(reader, binary.BigEndian, &c.Header); err != nil {
return
}
if err = binary.Read(reader, binary.BigEndian, &c.PreviousTagSize); err != nil {
return
}
return nil
}
func (d *demuxer) ReadTag(c *TagCompo) (err error) {
if _, err = io.ReadFull(d.r, c.TagHeaderRaw[:]); err != nil {
return
}
c.TagHeader.TagType = c.TagHeaderRaw[0]
c.TagHeader.DataSize = c.TagHeaderRaw[1:4]
c.TagHeader.Timestamp = c.TagHeaderRaw[4:7]
c.TagHeader.TimestampExtended = c.TagHeaderRaw[7]
c.TagHeader.StreamID = c.TagHeaderRaw[8:11]
c.TagBodyRaw = GetBytes(int(c.GetDataSize() + 4))
if _, err = io.ReadAtLeast(io.LimitReader(d.r, int64(c.GetDataSize()+4)), c.TagBodyRaw, int(c.GetDataSize()+4)); err != nil {
return
}
c.PreviousTagSize = binary.BigEndian.Uint32(c.TagBodyRaw[c.GetDataSize() : c.GetDataSize()+4])
if c.GetDataSize()+11 != c.PreviousTagSize {
return fmt.Errorf("Data size incorrect, got: %d, want: %d.", c.PreviousTagSize, c.GetDataSize()+11)
}
return nil
}
func (d *demuxer) Close() error {
return d.r.Close()
}