-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpacket.go
291 lines (265 loc) · 11 KB
/
packet.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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
package mpeg2ts
import (
"fmt"
"sync"
)
const (
ScrambleControl_NotScrambled = 0
ScrambleControl_Userdefined1 = 1
ScrambleControl_Userdefined2 = 2
ScrambleControl_Userdefined3 = 3
AdaptationField_Reserved = 0
AdaptationField_PayloadOnly = 1
AdaptationField_AdaptationFieldOnly = 2
AdaptationField_AdaptationFieldFollowed = 3
// ETSI EN 300 468 V1.17.1 p.24
TableID_ProgramAssociationSection = 0x00
TableID_ConditionalAccessSection = 0x01
TableID_ProgramMapSection = 0x02
TableID_TransportStreamDescriptionSection = 0x03
TableID_NetworkInformationSection_ActualNetwork = 0x40
TableID_NetworkInformationSection_OtherNetwork = 0x41
TableID_ServiceDescriptionSection_ActualDVBTransportStream = 0x42
TableID_ServiceDescriptionSection_OtherDVBTransportStream = 0x46
TableID_BouquetAssociationSection = 0x4A
TableID_UpdateNotificationTableSection = 0x4B
TableID_IP_MACNotificationSection = 0x4C
TableID_EventInformationSection_ActualDVBTransportStreamPresentFollowing = 0x4E
TableID_EventInformationSection_OtherDVBTransportStreamPresentFollowing = 0x4F
TableID_EventInformationSection_ActualDVBTransportStreamScheduleMin = 0x50
TableID_EventInformationSection_ActualDVBTransportStreamScheduleMax = 0x5F
TableID_EventInformationSection_OtherDVBTransportStreamScheduleMin = 0x60
TableID_EventInformationSection_OtherDVBTransportStreamScheduleMax = 0x6F
TableID_TimeDateSection = 0x70
TableID_RunningStatusSection = 0x71
TableID_StuffingSection = 0x72
TableID_TimeOffsetSection = 0x73
TableID_ApplicationInformationSection = 0x74
TableID_ContainerSection = 0x75
TableID_RelatedContentSection = 0x76
TableID_ContentIdentifierSection = 0x77
TableID_MPE_FECSection = 0x78
TableID_ResolutionProviderNotificationSection = 0x79
TableID_MPE_IFECSection = 0x7A
TableID_ProtectionMessageSection = 0x7B
TableID_DownloadableFontInfoSection = 0x7C
TableID_DiscontinuityInformationSection = 0x7E
TableID_SelectionInformationSection = 0x7F
TableID_UserDefinedMin = 0x80
TableID_UserDefinedMax = 0xFE
// TableID_Reserved = 0x04
// ...
// TableID_Reserved = 0x3F
// TableID_ReservedForFutureUse = 0x43
// TableID_ReservedForFutureUse = 0x44
// TableID_ReservedForFutureUse = 0x45
// TableID_ReservedForFutureUse = 0x47
// TableID_ReservedForFutureUse = 0x48
// TableID_ReservedForFutureUse = 0x49
// TableID_ReservedForFutureUse = 0x4D
// TableID_ReservedForFutureUse = 0x7D
// TableID_Reserved = 0xFF
)
func NewPacketList(chunkSize int) (PacketList, error) {
pl := PacketList{}
pl.mutex = &sync.Mutex{}
pl.chunkSize = chunkSize
pl.packets = make([]Packet, 0, 1024)
return pl, nil
}
func (ps *PacketList) DequeuePacket() (Packet, error) {
ps.mutex.Lock()
defer ps.mutex.Unlock()
if len(ps.packets) == 0 {
return Packet{}, fmt.Errorf("PacketList is empty")
}
packet := ps.packets[0]
ps.packets = append(ps.packets[:0], ps.packets[1:]...)
return packet, nil
}
func (ps *PacketList) All() []Packet {
ps.mutex.Lock()
defer ps.mutex.Unlock()
p := make([]Packet, len(ps.packets))
copy(p, ps.packets)
return p
}
func (ps *PacketList) AddPacket(p Packet) {
ps.mutex.Lock()
defer ps.mutex.Unlock()
ps.packets = append(ps.packets, p)
}
func (ps *PacketList) AddBytes(packetBytes []byte, packetSize int) error {
ps.mutex.Lock()
defer ps.mutex.Unlock()
if len(packetBytes) != packetSize {
return fmt.Errorf("packetBytes length and packetSize is not match. len(packetBytes) is %d", len(packetBytes))
}
index := len(ps.packets)
p := Packet{}
p.Data = make([]byte, PacketSizeDefault)
copy(p.Data, packetBytes)
p.Index = index
// fmt.Printf("index: %d\n", index)
err := p.parseHeader()
if err != nil {
return err
}
ps.packets = append(ps.packets, p)
// if index == 0 {
// fmt.Printf("%#v\n", p)
// fmt.Printf("%#v\n", *ps)
// fmt.Println("---------------------------------")
// }
return nil
}
func (p *Packet) GetHeader() ([]byte, error) {
if p.Data == nil || len(p.Data) != PacketSizeDefault {
return nil, fmt.Errorf("invalid header")
}
return p.Data[:4], nil
}
func (p *Packet) GetPayload() ([]byte, error) {
if !p.isHeaderParsed {
return nil, fmt.Errorf("execute parseHeader() first")
}
if len(p.Data) != PacketSizeDefault {
return nil, fmt.Errorf("invalid data size")
}
if p.HasAdaptationField() {
return p.Data[4+p.AdaptationField.Length+1:], nil
}
return p.Data[4:], nil
}
func (p *Packet) HasAdaptationField() bool {
c := p.AdaptationFieldControl
if c == AdaptationField_AdaptationFieldOnly || c == AdaptationField_AdaptationFieldFollowed {
return true
}
return false
}
func (p *Packet) parseHeader() error {
if p.Data[0] != 0x47 {
return fmt.Errorf("invalid magic number %02X", p.Data[0])
}
p.SyncByte = p.Data[0]
p.TransportErrorIndicator = ((p.Data[1] >> 7) & 0x01) == 1
p.PayloadUnitStartIndicator = ((p.Data[1] >> 6) & 0x01) == 1
p.TransportPriorityIndicator = ((p.Data[1] >> 5) & 0x01) == 1
p.PID = PID((uint16(p.Data[1])&0x1F)<<8 | uint16(p.Data[2]))
p.TransportScrambleControl = (p.Data[3] >> 6) & 0x03
p.AdaptationFieldControl = (p.Data[3] >> 4) & 0x03
p.ContinuityCheckIndex = (p.Data[3] & 0x0F)
// adaptation field
if p.HasAdaptationField() {
af := AdaptationField{}
af.Length = p.Data[4]
if p.AdaptationFieldControl == AdaptationField_AdaptationFieldFollowed {
if af.Length > 183 {
return fmt.Errorf("AdaptationField.Length should not exceed 182bytes")
}
} else if p.AdaptationFieldControl == AdaptationField_AdaptationFieldOnly {
if af.Length != 183 {
return fmt.Errorf("AdaptationField.Length must be 182bytes")
}
}
if p.AdaptationField.Length == 0 {
p.AdaptationField = af
p.isHeaderParsed = true
return nil
}
af.DiscontinuityIndicator = ((p.Data[5] >> 7) & 0x01) == 1
af.RandomAccessIndicator = ((p.Data[5] >> 6) & 0x01) == 1
af.ESPriorityIndicator = ((p.Data[5] >> 5) & 0x01) == 1
af.PCRFlag = ((p.Data[5] >> 4) & 0x01) == 1
af.OPCRFlag = ((p.Data[5] >> 3) & 0x01) == 1
af.SplicingPointFlag = ((p.Data[5] >> 2) & 0x01) == 1
af.TransportPrivateDataFlag = ((p.Data[5] >> 1) & 0x01) == 1
af.ExtensionFlag = (p.Data[5] & 0x01) == 1
// fmt.Printf("af: %#v\n", af)
// fmt.Printf("bytes: %#v\n", p.Data)
fieldIndex := 6
if af.PCRFlag {
// program_clock_reference_base 33 uimsbf
af.ProgramClockReference.Base = uint64(p.Data[fieldIndex])<<25 | uint64(p.Data[fieldIndex+1])<<17 | uint64(p.Data[fieldIndex+2])<<9 | uint64(p.Data[fieldIndex+3])<<1 | uint64(p.Data[fieldIndex+4])>>7&0x01
// reserved 6 bslbf
// program_clock_reference_extension 9 uimsbf
af.ProgramClockReference.Extension = uint16(p.Data[fieldIndex+4]&0x01)<<8 | uint16(p.Data[fieldIndex+5])
fieldIndex += 6
// fmt.Printf("PCR(%d/%d): %x %x\n", fieldIndex-5, af.Size, af.ProgramClockReference.Base, af.ProgramClockReference.Extension)
}
if af.OPCRFlag {
// original_program_clock_reference_base 33 uimsbf
af.OriginalProgramClockReference.Base = uint64(p.Data[fieldIndex])<<25 | uint64(p.Data[fieldIndex+1])<<17 | uint64(p.Data[fieldIndex+2])<<9 | uint64(p.Data[fieldIndex+3])<<1 | uint64(p.Data[fieldIndex+4])>>7&0x01
// reserved 6 bslbf
// original_program_clock_reference_extension 9 uimsbf
af.OriginalProgramClockReference.Extension = uint16(p.Data[fieldIndex+4]&0x01)<<8 | uint16(p.Data[fieldIndex+5])
fieldIndex += 6
// fmt.Println("[BUG] OPCR parsing is not implemented")
}
if af.SplicingPointFlag {
// splice_countdown 8 tcimsbf
af.SpliceCountdown = p.Data[fieldIndex]
fieldIndex++
}
if af.TransportPrivateDataFlag {
// transport_private_data_length 8 uimsbf
// for (i = 0; i < transport_private_data_length; i++) {
// private_data_byte 8 bslbf
// }
af.TransportPrivateData.Length = p.Data[fieldIndex]
af.TransportPrivateData.Data = p.Data[fieldIndex+1 : fieldIndex+1+int(af.TransportPrivateData.Length)]
fieldIndex += 1 + int(af.TransportPrivateData.Length)
}
if af.ExtensionFlag {
fmt.Println("[BUG] AdaptationFieldExtension parsing is not implemented")
fmt.Printf("af: %#v\n", af)
// adaptation_field_extension_length 8 uimsbf
// ltw_flag 1 bslbf
// piecewise_rate_flag 1 bslbf
// seamless_splice_flag 1 bslbf
// af_descriptor_not_present_flag 1 bslbf
// reserved 4 bslbf
// if (ltw_flag = = '1') {
// ltw_valid_flag 1 bslbf
// ltw_offset 15 uimsbf
// }
// if (piecewise_rate_flag = = '1') {
// reserved 2 bslbf
// piecewise_rate 22 uimsbf
// }
// if (seamless_splice_flag = = '1') {
// Splice_type 4 bslbf
// DTS_next_AU[32..30] 3 bslbf
// marker_bit 1 bslbf
// DTS_next_AU[29..15] 15 bslbf
// marker_bit 1 bslbf
// DTS_next_AU[14..0] 15 bslbf
// marker_bit 1 bslbf
// }
// if (af_descriptor_not_present_flag = = '0') {
// for (i = 0; i N1; i++) {
// af_descriptor()
// }
// }
// else {
// for (i = 0; i < N2; i++) {
// reserved 8 bslbf
// }
// }
}
// TODO: nokori
readedAFBytes := fieldIndex - 6 + 2
if int(af.Length) > 0 && readedAFBytes < int(af.Length) {
af.Stuffing = p.Data[6+readedAFBytes : 5+int(af.Length)]
for i, v := range af.Stuffing {
if v != 0xff {
return fmt.Errorf("[BUG] stuffing bytes contains non-0xff byte. data:0x%02x index:%d", v, i)
}
}
}
p.AdaptationField = af
}
p.isHeaderParsed = true
return nil
}