-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmuxer.go
52 lines (42 loc) · 880 Bytes
/
muxer.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
package flv
import (
"encoding/binary"
"io"
)
type Muxer interface {
WriteHeader(*HeaderCompo) (err error)
WriteTag(*TagCompo) (err error)
Close() error
}
func NewMuxer(w io.WriteCloser) (Muxer, error) {
return &muxer{
w: w,
}, nil
}
type muxer struct {
w io.WriteCloser
offset uint32
}
func (m *muxer) WriteHeader(c *HeaderCompo) (err error) {
_, err = m.w.Write(c.Raw[:])
return
}
func (m *muxer) WriteTag(c *TagCompo) (err error) {
if m.offset == 0 {
m.offset = c.GetTimestamp()
}
fixedTimstamp := c.GetTimestamp() - m.offset
modified := make([]byte, 4)
binary.BigEndian.PutUint32(modified, fixedTimstamp)
c.TagHeaderRaw[7] = modified[0]
copy(c.TagHeaderRaw[4:7], modified[1:4])
_, err = m.w.Write(c.TagHeaderRaw[:])
if err != nil {
return
}
_, err = m.w.Write(c.TagBodyRaw)
return
}
func (m *muxer) Close() error {
return m.w.Close()
}