forked from asticode/go-astits
-
Notifications
You must be signed in to change notification settings - Fork 0
/
packet_test.go
150 lines (135 loc) · 5.39 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
package astits
import (
"fmt"
"testing"
"github.com/asticode/go-astitools/binary"
"github.com/stretchr/testify/assert"
)
func packet(h PacketHeader, a PacketAdaptationField, i []byte) ([]byte, *Packet) {
w := astibinary.New()
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 w.Bytes(), &Packet{
AdaptationField: packetAdaptationField,
Bytes: w.Bytes(),
Header: packetHeader,
Payload: payload,
}
}
func TestParsePacket(t *testing.T) {
// Packet not starting with a sync
w := astibinary.New()
w.Write(uint16(1)) // Invalid sync byte
_, err := parsePacket(w.Bytes())
assert.EqualError(t, err, ErrPacketMustStartWithASyncByte.Error())
// Valid
b, ep := packet(*packetHeader, *packetAdaptationField, []byte("payload"))
p, err := parsePacket(b)
assert.NoError(t, err)
assert.Equal(t, p, ep)
}
func TestPayloadOffset(t *testing.T) {
assert.Equal(t, 3, payloadOffset(&PacketHeader{}, nil))
assert.Equal(t, 6, payloadOffset(&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 {
w := astibinary.New()
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 w.Bytes()
}
func TestParsePacketHeader(t *testing.T) {
assert.Equal(t, packetHeader, parsePacketHeader(packetHeaderBytes(*packetHeader)))
}
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 {
w := astibinary.New()
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()) // Splice type + DTS next access unit
w.Write([]byte("stuff")) // Stuffing bytes
return w.Bytes()
}
func TestParsePacketAdaptationField(t *testing.T) {
assert.Equal(t, packetAdaptationField, parsePacketAdaptationField(packetAdaptationFieldBytes(*packetAdaptationField)))
}
var pcr = &ClockReference{
Base: 5726623061,
Extension: 341,
}
func pcrBytes() []byte {
w := astibinary.New()
w.Write("101010101010101010101010101010101") // Base
w.Write("111111") // Reserved
w.Write("101010101") // Extension
return w.Bytes()
}
func TestParsePCR(t *testing.T) {
assert.Equal(t, pcr, parsePCR(pcrBytes()))
}