-
Notifications
You must be signed in to change notification settings - Fork 50
/
packet_test.go
267 lines (238 loc) · 9.29 KB
/
packet_test.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
package astits
import (
"bytes"
"fmt"
"testing"
"github.com/asticode/go-astikit"
"github.com/stretchr/testify/assert"
)
func packet(h PacketHeader, a PacketAdaptationField, i []byte, packet192bytes bool) ([]byte, *Packet) {
buf := &bytes.Buffer{}
w := astikit.NewBitsWriter(astikit.BitsWriterOptions{Writer: buf})
w.Write(uint8(syncByte)) // Sync byte
if packet192bytes {
w.Write([]byte("test")) // Sometimes packets are 192 bytes
}
w.Write(packetHeaderBytes(h, "11")) // Header
w.Write(packetAdaptationFieldBytes(a)) // Adaptation field
var payload = append(i, bytes.Repeat([]byte{0}, 147-len(i))...) // Payload
w.Write(payload)
return buf.Bytes(), &Packet{
AdaptationField: packetAdaptationField,
Header: packetHeader,
Payload: payload,
}
}
func packetShort(h PacketHeader, payload []byte) ([]byte, *Packet) {
buf := &bytes.Buffer{}
w := astikit.NewBitsWriter(astikit.BitsWriterOptions{Writer: buf})
w.Write(uint8(syncByte)) // Sync byte
w.Write(packetHeaderBytes(h, "01")) // Header
p := append(payload, bytes.Repeat([]byte{0}, MpegTsPacketSize-buf.Len())...)
w.Write(p)
return buf.Bytes(), &Packet{
Header: h,
Payload: payload,
}
}
func TestParsePacket(t *testing.T) {
// Packet not starting with a sync
buf := &bytes.Buffer{}
w := astikit.NewBitsWriter(astikit.BitsWriterOptions{Writer: buf})
w.Write(uint16(1)) // Invalid sync byte
_, err := parsePacket(astikit.NewBytesIterator(buf.Bytes()), nil)
assert.EqualError(t, err, ErrPacketMustStartWithASyncByte.Error())
// Valid
b, ep := packet(packetHeader, *packetAdaptationField, []byte("payload"), true)
p, err := parsePacket(astikit.NewBytesIterator(b), nil)
assert.NoError(t, err)
assert.Equal(t, p, ep)
// Skip
_, err = parsePacket(astikit.NewBytesIterator(b), func(p *Packet) bool { return true })
assert.EqualError(t, err, errSkippedPacket.Error())
}
func TestPayloadOffset(t *testing.T) {
assert.Equal(t, 3, payloadOffset(0, PacketHeader{}, nil))
assert.Equal(t, 7, payloadOffset(1, PacketHeader{HasAdaptationField: true}, &PacketAdaptationField{Length: 2}))
}
func TestWritePacket(t *testing.T) {
eb, ep := packet(packetHeader, *packetAdaptationField, []byte("payload"), false)
buf := &bytes.Buffer{}
w := astikit.NewBitsWriter(astikit.BitsWriterOptions{Writer: buf})
n, err := writePacket(w, ep, MpegTsPacketSize)
assert.NoError(t, err)
assert.Equal(t, MpegTsPacketSize, n)
assert.Equal(t, n, buf.Len())
assert.Equal(t, len(eb), buf.Len())
assert.Equal(t, eb, buf.Bytes())
}
func TestWritePacket_HeaderOnly(t *testing.T) {
shortPacketHeader := packetHeader
shortPacketHeader.HasPayload = false
shortPacketHeader.HasAdaptationField = false
_, ep := packetShort(shortPacketHeader, nil)
buf := &bytes.Buffer{}
w := astikit.NewBitsWriter(astikit.BitsWriterOptions{Writer: buf})
n, err := writePacket(w, ep, MpegTsPacketSize)
assert.NoError(t, err)
assert.Equal(t, MpegTsPacketSize, n)
assert.Equal(t, n, buf.Len())
// we can't just compare bytes returned by packetShort since they're not completely correct,
// so we just cross-check writePacket with parsePacket
i := astikit.NewBytesIterator(buf.Bytes())
p, err := parsePacket(i, nil)
assert.NoError(t, err)
assert.Equal(t, ep, p)
}
var packetHeader = PacketHeader{
ContinuityCounter: 10,
HasAdaptationField: true,
HasPayload: true,
PayloadUnitStartIndicator: true,
PID: 5461,
TransportErrorIndicator: true,
TransportPriority: true,
TransportScramblingControl: ScramblingControlScrambledWithEvenKey,
}
func packetHeaderBytes(h PacketHeader, afControl string) []byte {
buf := &bytes.Buffer{}
w := astikit.NewBitsWriter(astikit.BitsWriterOptions{Writer: buf})
w.Write(h.TransportErrorIndicator) // Transport error indicator
w.Write(h.PayloadUnitStartIndicator) // Payload unit start indicator
w.Write("1") // Transport priority
w.Write(fmt.Sprintf("%.13b", h.PID)) // PID
w.Write("10") // Scrambling control
w.Write(afControl) // Adaptation field control
w.Write(fmt.Sprintf("%.4b", h.ContinuityCounter)) // Continuity counter
return buf.Bytes()
}
func TestParsePacketHeader(t *testing.T) {
v, err := parsePacketHeader(astikit.NewBytesIterator(packetHeaderBytes(packetHeader, "11")))
assert.Equal(t, packetHeader, v)
assert.NoError(t, err)
}
func TestWritePacketHeader(t *testing.T) {
buf := &bytes.Buffer{}
w := astikit.NewBitsWriter(astikit.BitsWriterOptions{Writer: buf})
bytesWritten, err := writePacketHeader(w, packetHeader)
assert.NoError(t, err)
assert.Equal(t, bytesWritten, 3)
assert.Equal(t, bytesWritten, buf.Len())
assert.Equal(t, packetHeaderBytes(packetHeader, "11"), buf.Bytes())
}
var packetAdaptationField = &PacketAdaptationField{
AdaptationExtensionField: &PacketAdaptationExtensionField{
DTSNextAccessUnit: dtsClockReference,
HasLegalTimeWindow: true,
HasPiecewiseRate: true,
HasSeamlessSplice: true,
LegalTimeWindowIsValid: true,
LegalTimeWindowOffset: 10922,
Length: 11,
PiecewiseRate: 2796202,
SpliceType: 2,
},
DiscontinuityIndicator: true,
ElementaryStreamPriorityIndicator: true,
HasAdaptationExtensionField: true,
HasOPCR: true,
HasPCR: true,
HasTransportPrivateData: true,
HasSplicingCountdown: true,
Length: 36,
OPCR: pcr,
PCR: pcr,
RandomAccessIndicator: true,
SpliceCountdown: 2,
TransportPrivateDataLength: 4,
TransportPrivateData: []byte("test"),
StuffingLength: 5,
}
func packetAdaptationFieldBytes(a PacketAdaptationField) []byte {
buf := &bytes.Buffer{}
w := astikit.NewBitsWriter(astikit.BitsWriterOptions{Writer: buf})
w.Write(uint8(36)) // Length
w.Write(a.DiscontinuityIndicator) // Discontinuity indicator
w.Write("1") // Random access indicator
w.Write("1") // Elementary stream priority indicator
w.Write("1") // PCR flag
w.Write("1") // OPCR flag
w.Write("1") // Splicing point flag
w.Write("1") // Transport data flag
w.Write("1") // Adaptation field extension flag
w.Write(pcrBytes()) // PCR
w.Write(pcrBytes()) // OPCR
w.Write(uint8(2)) // Splice countdown
w.Write(uint8(4)) // Transport private data length
w.Write([]byte("test")) // Transport private data
w.Write(uint8(11)) // Adaptation extension length
w.Write("1") // LTW flag
w.Write("1") // Piecewise rate flag
w.Write("1") // Seamless splice flag
w.Write("11111") // Reserved
w.Write("1") // LTW valid flag
w.Write("010101010101010") // LTW offset
w.Write("11") // Piecewise rate reserved
w.Write("1010101010101010101010") // Piecewise rate
w.Write(dtsBytes("0010")) // Splice type + DTS next access unit
w.WriteN(^uint64(0), 40) // Stuffing bytes
return buf.Bytes()
}
func TestParsePacketAdaptationField(t *testing.T) {
v, err := parsePacketAdaptationField(astikit.NewBytesIterator(packetAdaptationFieldBytes(*packetAdaptationField)))
assert.Equal(t, packetAdaptationField, v)
assert.NoError(t, err)
}
func TestWritePacketAdaptationField(t *testing.T) {
buf := &bytes.Buffer{}
w := astikit.NewBitsWriter(astikit.BitsWriterOptions{Writer: buf})
eb := packetAdaptationFieldBytes(*packetAdaptationField)
bytesWritten, err := writePacketAdaptationField(w, packetAdaptationField)
assert.NoError(t, err)
assert.Equal(t, bytesWritten, buf.Len())
assert.Equal(t, len(eb), buf.Len())
assert.Equal(t, eb, buf.Bytes())
}
var pcr = &ClockReference{
Base: 5726623061,
Extension: 341,
}
func pcrBytes() []byte {
buf := &bytes.Buffer{}
w := astikit.NewBitsWriter(astikit.BitsWriterOptions{Writer: buf})
w.Write("101010101010101010101010101010101") // Base
w.Write("111111") // Reserved
w.Write("101010101") // Extension
return buf.Bytes()
}
func TestParsePCR(t *testing.T) {
v, err := parsePCR(astikit.NewBytesIterator(pcrBytes()))
assert.Equal(t, pcr, v)
assert.NoError(t, err)
}
func TestWritePCR(t *testing.T) {
buf := &bytes.Buffer{}
w := astikit.NewBitsWriter(astikit.BitsWriterOptions{Writer: buf})
bytesWritten, err := writePCR(w, pcr)
assert.NoError(t, err)
assert.Equal(t, bytesWritten, 6)
assert.Equal(t, bytesWritten, buf.Len())
assert.Equal(t, pcrBytes(), buf.Bytes())
}
func BenchmarkWritePCR(b *testing.B) {
buf := &bytes.Buffer{}
buf.Grow(6)
w := astikit.NewBitsWriter(astikit.BitsWriterOptions{Writer: buf})
b.ReportAllocs()
for i := 0; i < b.N; i++ {
buf.Reset()
writePCR(w, pcr)
}
}
func BenchmarkParsePacket(b *testing.B) {
bs, _ := packet(packetHeader, *packetAdaptationField, []byte("payload"), true)
for i := 0; i < b.N; i++ {
b.ReportAllocs()
parsePacket(astikit.NewBytesIterator(bs), nil)
}
}