forked from asticode/go-astits
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpacket_test.go
More file actions
184 lines (167 loc) · 8.1 KB
/
packet_test.go
File metadata and controls
184 lines (167 loc) · 8.1 KB
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
package astits
import (
"bytes"
"fmt"
"testing"
"github.com/asticode/go-astikit"
"github.com/stretchr/testify/assert"
)
func packet(h PacketHeader, a PacketAdaptationField, i []byte) ([]byte, *Packet) {
buf := &bytes.Buffer{}
w := astikit.NewBitsWriter(astikit.BitsWriterOptions{Writer: buf})
w.Write(uint8(syncByte)) // Sync byte
w.Write([]byte("test")) // Sometimes packets are 192 bytes
w.Write(packetHeaderBytes(h)) // Header
w.Write(packetAdaptationFieldBytes(a)) // Adaptation field
var payload = append(i, make([]byte, 147-len(i))...) // Payload
w.Write(payload)
return buf.Bytes(), &Packet{
AdaptationField: packetAdaptationField,
Header: packetHeader,
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()))
assert.EqualError(t, err, ErrPacketMustStartWithASyncByte.Error())
// Valid
b, ep := packet(packetHeader, *packetAdaptationField, []byte("payload"))
p, err := parsePacket(astikit.NewBytesIterator(b))
assert.NoError(t, err)
assert.Equal(t, p, ep)
}
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}))
}
var packetHeader = PacketHeader{
ContinuityCounter: 10,
HasAdaptationField: true,
HasPayload: true,
PayloadUnitStartIndicator: true,
PID: 5461,
TransportErrorIndicator: true,
TransportPriority: true,
TransportScramblingControl: ScramblingControlScrambledWithEvenKey,
}
func packetHeaderBytes(h PacketHeader) []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("11") // 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)))
assert.Equal(t, packetHeader, v)
assert.NoError(t, err)
}
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"),
}
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("1") // AF descriptor not present flag
w.Write("1111") // 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()) // Splice type + DTS next access unit
w.Write([]byte{0xFF, 0xFF, 0xFF, 0xFF, 0xFF}) // 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 TestParseSerialisedPacketAdaptationField(t *testing.T) {
b := packetAdaptationFieldBytes(*packetAdaptationField)
v, err := parsePacketAdaptationField(astikit.NewBytesIterator(b))
assert.Equal(t, packetAdaptationField, v)
assert.NoError(t, err)
buf := make([]byte, len(b))
err = v.Serialise(buf)
assert.NoError(t, err)
assert.Equal(t, b, buf)
}
func TestParseSerialisePacketWithAdaptationField(t *testing.T) {
b := []byte{0x47, 0x40, 0x64, 0x37, 0x91, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x02, 0xb0, 0x22, 0x00, 0x01, 0xc1, 0x00, 0x00, 0xe0, 0x65, 0xf0, 0x00, 0x1b, 0xe0, 0x65, 0xf0, 0x06, 0x28, 0x04, 0x64, 0x00, 0x20, 0x00, 0x03, 0xe0, 0x66, 0xf0, 0x00, 0x03, 0xe0, 0xc8, 0xf0, 0x00, 0x63, 0x97, 0x62, 0xbf}
fmt.Println(len(b))
p, err := parsePacket(astikit.NewBytesIterator(b))
assert.NoError(t, err)
buf := make([]byte, 188)
_, err = p.Serialise(buf)
assert.NoError(t, err)
assert.Equal(t, b, buf)
}
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)
}