-
Notifications
You must be signed in to change notification settings - Fork 0
/
demux.go
157 lines (135 loc) · 3.27 KB
/
demux.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
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
152
153
154
155
156
157
package psi
import (
"bytes"
"sync"
"github.com/mh-orange/ts"
)
type TableHandler interface {
Handle(table Table)
}
type TableHandlerFunc func(table Table)
func (thf TableHandlerFunc) Handle(table Table) {
thf(table)
}
type TableDemux interface {
Handle(pkt ts.Packet)
Clear(id uint8)
Select(id uint8, handler TableHandler)
}
type TableBuffer interface {
Append([]byte)
Flush()
HasNext() bool
Next() Table
}
type defaultTableBuffer struct {
buffer []byte
tables []Table
}
func sectionLength(data []byte) (length int) {
if len(data) > 3 && data[0] != 0xff {
length = 3 + int(ts.Uimsbf16(data[1:3], 16))
}
return length
}
func (tb *defaultTableBuffer) Next() (table Table) {
if len(tb.tables) > 0 {
table = tb.tables[0]
tb.tables = tb.tables[1:]
}
return
}
func (tb *defaultTableBuffer) HasNext() bool {
return len(tb.tables) > 0
}
func (tb *defaultTableBuffer) Flush() {
tb.buffer = nil
}
func (tb *defaultTableBuffer) Append(data []byte) {
tb.buffer = append(tb.buffer, data...)
// stopping condition is end of the buffer (do nothing) or
// table id of 0xff (flush remaining bytes)
for len(tb.buffer) > 0 && tb.buffer[0] != 0xff {
length := sectionLength(tb.buffer)
if length > 0 && length < len(tb.buffer) {
table := make(Table, length)
copy(table, tb.buffer[0:length])
tb.tables = append(tb.tables, table)
tb.buffer = tb.buffer[length:]
} else {
break
}
}
if len(tb.buffer) > 0 && tb.buffer[0] == 0xff {
tb.buffer = nil
}
}
type tableDemux struct {
buffer TableBuffer
mu sync.Mutex
handlers map[uint8]TableHandler
first bool
cc uint8
}
func NewTableDemux() TableDemux {
return &tableDemux{
buffer: &defaultTableBuffer{},
handlers: make(map[uint8]TableHandler),
first: true,
}
}
func (td *tableDemux) Select(tableID uint8, handler TableHandler) {
td.mu.Lock()
td.handlers[tableID] = handler
td.mu.Unlock()
}
func (td *tableDemux) Clear(tableID uint8) {
td.mu.Lock()
delete(td.handlers, tableID)
td.mu.Unlock()
}
func (td *tableDemux) Handle(pkt ts.Packet) {
var payload []byte
discontinuous := false
// check continuity counter
nextCc := pkt.ContinuityCounter()
if !td.first && td.cc+1 != nextCc {
discontinuous = true
// check for expected discontinuity
if field, err := pkt.AdaptationField(); err == nil {
discontinuous = !field.IsDiscontinuous()
}
}
td.first = false
td.cc = nextCc
if pkt.HasPayload() {
payload, _ = pkt.Payload()
}
// If the Payload Unit Start Indicator (PUSI) is set, then a new PSI table
// begins in the payload section of the packet. If the PUSI is not set,
// then the payload is a continuation of a previous packet's payload. This
// can simply be appended to the current buffer as long as there is no
// discontinuity
//
// When PUSI is set it doesn't matter if there is a discontinuity since we're
// starting with a new table
if pkt.PUSI() {
td.buffer.Append(payload[int(payload[0])+1:])
} else if !discontinuous {
td.buffer.Append(payload)
} else {
td.buffer.Flush()
}
for td.buffer.HasNext() {
table := td.buffer.Next()
crc := ts.ComputeCRC(table[0 : len(table)-4])
if !bytes.Equal(crc, table[len(table)-4:]) {
continue
}
td.mu.Lock()
if handler, ok := td.handlers[table.ID()]; ok {
handler.Handle(table)
}
td.mu.Unlock()
}
}