threefive3 3.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- scte35/__init__.py +27 -0
- scte35/base.py +189 -0
- scte35/bitn.py +193 -0
- scte35/commands.py +326 -0
- scte35/crc.py +49 -0
- scte35/cue.py +456 -0
- scte35/descriptors.py +549 -0
- scte35/hls.py +1171 -0
- scte35/packetdata.py +39 -0
- scte35/section.py +194 -0
- scte35/segment.py +128 -0
- scte35/segmentation.py +71 -0
- scte35/sixfix.py +194 -0
- scte35/spare.py +12 -0
- scte35/stream.py +686 -0
- scte35/streamtypes.py +127 -0
- scte35/sxp.py +350 -0
- scte35/upids.py +403 -0
- scte35/version.py +6 -0
- scte35/xml.py +355 -0
- threefive3-3.0.1.data/scripts/scte35 +365 -0
- threefive3-3.0.1.dist-info/LICENSE +24 -0
- threefive3-3.0.1.dist-info/METADATA +261 -0
- threefive3-3.0.1.dist-info/RECORD +26 -0
- threefive3-3.0.1.dist-info/WHEEL +5 -0
- threefive3-3.0.1.dist-info/top_level.txt +1 -0
scte35/stream.py
ADDED
|
@@ -0,0 +1,686 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Mpeg-TS Stream parsing class Stream
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
import sys
|
|
6
|
+
|
|
7
|
+
from functools import partial
|
|
8
|
+
from new_reader import reader
|
|
9
|
+
from .spare import print2
|
|
10
|
+
from .cue import Cue
|
|
11
|
+
from .packetdata import PacketData
|
|
12
|
+
from .streamtypes import streamtype_map
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def no_op(cue):
|
|
16
|
+
"""
|
|
17
|
+
no_op is just a dummy func to pass to Stream.decode()
|
|
18
|
+
to suppress output.
|
|
19
|
+
"""
|
|
20
|
+
return cue
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def show_cue(cue):
|
|
24
|
+
"""
|
|
25
|
+
default function call for Stream.decode
|
|
26
|
+
when a SCTE-35 packet is found.
|
|
27
|
+
"""
|
|
28
|
+
cue.show()
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def show_cue_stderr(cue):
|
|
32
|
+
"""
|
|
33
|
+
print2 cue data to sys.stderr
|
|
34
|
+
for Stream.decode_proxy
|
|
35
|
+
"""
|
|
36
|
+
cue.to_stderr()
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class ProgramInfo:
|
|
40
|
+
"""
|
|
41
|
+
ProgramInfo is a class to
|
|
42
|
+
hold Program information
|
|
43
|
+
for use with Stream.show()
|
|
44
|
+
"""
|
|
45
|
+
|
|
46
|
+
def __init__(self, pid=None, pcr_pid=None):
|
|
47
|
+
self.pid = pid
|
|
48
|
+
self.pcr_pid = pcr_pid
|
|
49
|
+
self.provider = b""
|
|
50
|
+
self.service = b""
|
|
51
|
+
self.streams = {} # pid to stream_type mapping
|
|
52
|
+
|
|
53
|
+
def show(self):
|
|
54
|
+
"""
|
|
55
|
+
show print2 the Program Infomation
|
|
56
|
+
in a familiar format.
|
|
57
|
+
"""
|
|
58
|
+
serv = self.service.decode(errors="ignore")
|
|
59
|
+
prov = self.provider.decode(errors="ignore")
|
|
60
|
+
print2("")
|
|
61
|
+
print2(f"\tService: {serv}")
|
|
62
|
+
print2(f"\tProvider: {prov}")
|
|
63
|
+
print2(f"\tPid: {self.pid}")
|
|
64
|
+
print2(f"\tPcr Pid: {self.pcr_pid}")
|
|
65
|
+
print2("\tStreams:")
|
|
66
|
+
# sorted_dict = {k:my_dict[k] for k in sorted(my_dict)})
|
|
67
|
+
keys = sorted(self.streams)
|
|
68
|
+
print2("\t Pid\t\tType")
|
|
69
|
+
for k in keys:
|
|
70
|
+
vee = int(self.streams[k], base=16)
|
|
71
|
+
if vee in streamtype_map:
|
|
72
|
+
vee = f"{hex(vee)}\t{streamtype_map[vee]}"
|
|
73
|
+
else:
|
|
74
|
+
vee = f"{vee} Unknown"
|
|
75
|
+
print2(f"\t {k} [{hex(k)}]\t{vee}")
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
class Pids:
|
|
79
|
+
"""
|
|
80
|
+
Pids holds sets of pids for pat,pcr,pmt, and scte35
|
|
81
|
+
"""
|
|
82
|
+
|
|
83
|
+
SDT_PID = 0x11
|
|
84
|
+
PAT_PID = 0x00
|
|
85
|
+
|
|
86
|
+
def __init__(self):
|
|
87
|
+
self.pcr = set()
|
|
88
|
+
self.pmt = set()
|
|
89
|
+
self.scte35 = set()
|
|
90
|
+
self.maybe_scte35 = set()
|
|
91
|
+
self.tables = set()
|
|
92
|
+
self.tables.add(self.PAT_PID)
|
|
93
|
+
self.tables.add(self.SDT_PID)
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
class Maps:
|
|
97
|
+
"""
|
|
98
|
+
Maps holds mappings
|
|
99
|
+
pids mapped to continuity_counters,
|
|
100
|
+
programs, partial tables and last payload.
|
|
101
|
+
|
|
102
|
+
programs mapped to pcr and pts
|
|
103
|
+
|
|
104
|
+
"""
|
|
105
|
+
|
|
106
|
+
def __init__(self):
|
|
107
|
+
self.pid_cc = {}
|
|
108
|
+
self.pid_prgm = {}
|
|
109
|
+
self.prgm_pcr = {}
|
|
110
|
+
self.prgm_pts = {}
|
|
111
|
+
self.prgm = {}
|
|
112
|
+
self.partial = {}
|
|
113
|
+
self.last = {}
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
class Stream:
|
|
117
|
+
"""
|
|
118
|
+
Stream class for parsing MPEG-TS data.
|
|
119
|
+
"""
|
|
120
|
+
|
|
121
|
+
# the _CONST are deprecated
|
|
122
|
+
# please switch to CONST
|
|
123
|
+
|
|
124
|
+
_PACKET_SIZE = PACKET_SIZE = 188
|
|
125
|
+
_SYNC_BYTE = SYNC_BYTE = 0x47
|
|
126
|
+
|
|
127
|
+
# tids
|
|
128
|
+
|
|
129
|
+
_PMT_TID = PMT_TID = b"\x02"
|
|
130
|
+
_SCTE35_TID = SCTE35_TID = b"\xfc"
|
|
131
|
+
_SDT_TID = SDT_TID = b"\x42"
|
|
132
|
+
ROLLOVER = 8589934591 # 95443.717678
|
|
133
|
+
ROLLOVER9K = 95443.717678
|
|
134
|
+
SCTE35_PES_START = b"\x00\x00\x01\xfc"
|
|
135
|
+
|
|
136
|
+
def __init__(self, tsdata, show_null=True):
|
|
137
|
+
"""
|
|
138
|
+
tsdata is an file or http/https url
|
|
139
|
+
set show_null=False to exclude Splice Nulls
|
|
140
|
+
|
|
141
|
+
Use like...
|
|
142
|
+
|
|
143
|
+
from scte35 import Stream
|
|
144
|
+
strm = Stream("vid.ts",show_null=False)
|
|
145
|
+
strm.decode()
|
|
146
|
+
|
|
147
|
+
"""
|
|
148
|
+
if isinstance(tsdata, str):
|
|
149
|
+
self._tsdata = reader(tsdata)
|
|
150
|
+
else:
|
|
151
|
+
self._tsdata = tsdata
|
|
152
|
+
self.show_null = show_null
|
|
153
|
+
self.start = {}
|
|
154
|
+
self.info = False
|
|
155
|
+
self.the_program = None
|
|
156
|
+
self.the_scte35_pids = []
|
|
157
|
+
self.pids = Pids()
|
|
158
|
+
self.maps = Maps()
|
|
159
|
+
|
|
160
|
+
def __repr__(self):
|
|
161
|
+
return str(self.__dict__)
|
|
162
|
+
|
|
163
|
+
@staticmethod
|
|
164
|
+
def as_90k(ticks):
|
|
165
|
+
"""
|
|
166
|
+
as_90k returns ticks as 90k clock time
|
|
167
|
+
"""
|
|
168
|
+
return round((ticks / 90000.0), 6)
|
|
169
|
+
|
|
170
|
+
@staticmethod
|
|
171
|
+
def _pusi_flag(pkt):
|
|
172
|
+
return pkt[1] & 0x40
|
|
173
|
+
|
|
174
|
+
@staticmethod
|
|
175
|
+
def _afc_flag(pkt3):
|
|
176
|
+
return pkt3 & 0x20
|
|
177
|
+
|
|
178
|
+
@staticmethod
|
|
179
|
+
def _pcr_flag(pkt):
|
|
180
|
+
return pkt[5] & 0x10
|
|
181
|
+
|
|
182
|
+
@staticmethod
|
|
183
|
+
def _spi_flag(pkt):
|
|
184
|
+
return pkt[5] & 0x20
|
|
185
|
+
|
|
186
|
+
@staticmethod
|
|
187
|
+
def _pts_flag(pay):
|
|
188
|
+
# uses pay not pkt
|
|
189
|
+
return pay[7] & 0x80
|
|
190
|
+
|
|
191
|
+
@staticmethod
|
|
192
|
+
def _parse_length(byte1, byte2):
|
|
193
|
+
"""
|
|
194
|
+
parse a 12 bit length value
|
|
195
|
+
"""
|
|
196
|
+
return (byte1 & 0xF) << 8 | byte2
|
|
197
|
+
|
|
198
|
+
@staticmethod
|
|
199
|
+
def _parse_pid(byte1, byte2):
|
|
200
|
+
"""
|
|
201
|
+
parse a 13 bit pid value
|
|
202
|
+
"""
|
|
203
|
+
pid = (byte1 & 0x1F) << 8 | byte2
|
|
204
|
+
return pid
|
|
205
|
+
|
|
206
|
+
@staticmethod
|
|
207
|
+
def _parse_program(byte1, byte2):
|
|
208
|
+
"""
|
|
209
|
+
parse a 16 bit program number value
|
|
210
|
+
"""
|
|
211
|
+
return (byte1 << 8) | byte2
|
|
212
|
+
|
|
213
|
+
@staticmethod
|
|
214
|
+
def _split_by_idx(pay, marker):
|
|
215
|
+
try:
|
|
216
|
+
return pay[pay.index(marker) :]
|
|
217
|
+
except:
|
|
218
|
+
return False
|
|
219
|
+
|
|
220
|
+
def _find_start(self):
|
|
221
|
+
"""
|
|
222
|
+
_find_start locates the first SYNC_BYTE
|
|
223
|
+
parses 1 packet and returns True
|
|
224
|
+
if SYNC_BYTE is found.
|
|
225
|
+
"""
|
|
226
|
+
while self._tsdata:
|
|
227
|
+
one = self._tsdata.read(1)
|
|
228
|
+
if one[0] == self.SYNC_BYTE:
|
|
229
|
+
tail = self._tsdata.read(self.PACKET_SIZE - 1)
|
|
230
|
+
self._parse(one + tail)
|
|
231
|
+
return True
|
|
232
|
+
print2("No Stream Found\n")
|
|
233
|
+
return False
|
|
234
|
+
|
|
235
|
+
def iter_pkts(self, num_pkts=1):
|
|
236
|
+
"""
|
|
237
|
+
iter_pkts iterates a mpegts stream into packets
|
|
238
|
+
"""
|
|
239
|
+
if self._find_start():
|
|
240
|
+
return iter(partial(self._tsdata.read, self.PACKET_SIZE * num_pkts), b"")
|
|
241
|
+
return False
|
|
242
|
+
|
|
243
|
+
def _mk_pkts(self, chunk):
|
|
244
|
+
return [
|
|
245
|
+
self._parse(chunk[i : i + self.PACKET_SIZE])
|
|
246
|
+
for i in range(0, len(chunk), self.PACKET_SIZE)
|
|
247
|
+
]
|
|
248
|
+
|
|
249
|
+
def decode(self, func=show_cue):
|
|
250
|
+
"""
|
|
251
|
+
Stream.decode reads self.tsdata to find SCTE35 packets.
|
|
252
|
+
func can be set to a custom function that accepts
|
|
253
|
+
a scte35.Cue instance as it's only argument.
|
|
254
|
+
"""
|
|
255
|
+
if not self._find_start():
|
|
256
|
+
return
|
|
257
|
+
num_pkts = 7
|
|
258
|
+
for chunk in self.iter_pkts(num_pkts=num_pkts):
|
|
259
|
+
_ = [func(cue) for cue in self._mk_pkts(chunk) if cue]
|
|
260
|
+
del _
|
|
261
|
+
return False
|
|
262
|
+
|
|
263
|
+
def decode_next(self):
|
|
264
|
+
"""
|
|
265
|
+
Stream.decode_next returns the next
|
|
266
|
+
SCTE35 cue as a scte35.Cue instance.
|
|
267
|
+
"""
|
|
268
|
+
for pkt in self.iter_pkts():
|
|
269
|
+
cue = self._parse(pkt)
|
|
270
|
+
if cue:
|
|
271
|
+
return cue
|
|
272
|
+
return False
|
|
273
|
+
|
|
274
|
+
def decode_program(self, the_program, func=show_cue):
|
|
275
|
+
"""
|
|
276
|
+
Stream.decode_program limits SCTE35 parsing
|
|
277
|
+
to a specific MPEGTS program.
|
|
278
|
+
"""
|
|
279
|
+
self.the_program = the_program
|
|
280
|
+
return self.decode(func)
|
|
281
|
+
|
|
282
|
+
def decode_pids(self, scte35_pids=[], func=show_cue):
|
|
283
|
+
"""
|
|
284
|
+
Stream.decode_pids takes a list of SCTE-35 Pids parse
|
|
285
|
+
and an optional call back function to run when a Cue is found.
|
|
286
|
+
if scte35_pids is not set, all scte35 pids will be parsed.
|
|
287
|
+
"""
|
|
288
|
+
self.the_scte35_pids = scte35_pids
|
|
289
|
+
return self.decode(func)
|
|
290
|
+
|
|
291
|
+
def decode_start_time(self):
|
|
292
|
+
"""
|
|
293
|
+
decode_start_time
|
|
294
|
+
"""
|
|
295
|
+
self.decode(func=no_op)
|
|
296
|
+
if len(self.start.values()) > 0:
|
|
297
|
+
return self.start.popitem()[1]
|
|
298
|
+
return False
|
|
299
|
+
|
|
300
|
+
def proxy(self, func=show_cue_stderr):
|
|
301
|
+
"""
|
|
302
|
+
Stream.decode_proxy writes all ts packets are written to stdout
|
|
303
|
+
for piping into another program like mplayer.
|
|
304
|
+
SCTE-35 cues are print2`ed to stderr.
|
|
305
|
+
"""
|
|
306
|
+
for pkt in self.iter_pkts():
|
|
307
|
+
cue = self._parse(pkt)
|
|
308
|
+
if cue:
|
|
309
|
+
func(cue)
|
|
310
|
+
sys.stdout.buffer.write(pkt)
|
|
311
|
+
return False
|
|
312
|
+
|
|
313
|
+
def show(self):
|
|
314
|
+
"""
|
|
315
|
+
displays streams that will be
|
|
316
|
+
parsed for SCTE-35.
|
|
317
|
+
"""
|
|
318
|
+
self.info = True
|
|
319
|
+
for pkt in self.iter_pkts():
|
|
320
|
+
self._parse_info(pkt)
|
|
321
|
+
if self.maps.prgm.keys():
|
|
322
|
+
sopro = sorted(self.maps.prgm.items())
|
|
323
|
+
for k, vee in sopro:
|
|
324
|
+
# if len(vee.streams.items()) > 0:
|
|
325
|
+
print2(f"\nProgram: {k}")
|
|
326
|
+
vee.show()
|
|
327
|
+
return True
|
|
328
|
+
|
|
329
|
+
def show_pts(self):
|
|
330
|
+
"""
|
|
331
|
+
show_pts displays current pts by pid.
|
|
332
|
+
"""
|
|
333
|
+
print("\tPrgm\tPTS")
|
|
334
|
+
for pkt in self.iter_pkts():
|
|
335
|
+
pid = self._parse_info(pkt)
|
|
336
|
+
# if pid in self.pids.pcr:
|
|
337
|
+
if self._pusi_flag(pkt) and pid != 0:
|
|
338
|
+
self._parse_pts(pkt, pid)
|
|
339
|
+
print(f"\t{self.pid2prgm(pid)}\t{self.pid2pts(pid)}")
|
|
340
|
+
|
|
341
|
+
def pts(self):
|
|
342
|
+
"""
|
|
343
|
+
pts returns a dict of program:pts
|
|
344
|
+
"""
|
|
345
|
+
return self.maps.prgm_pts
|
|
346
|
+
|
|
347
|
+
def pid2prgm(self, pid):
|
|
348
|
+
"""
|
|
349
|
+
pid2prgm takes a pid,
|
|
350
|
+
returns the program
|
|
351
|
+
"""
|
|
352
|
+
prgm = 1
|
|
353
|
+
if pid in self.maps.pid_prgm:
|
|
354
|
+
return self.maps.pid_prgm[pid]
|
|
355
|
+
return False
|
|
356
|
+
|
|
357
|
+
def pid2pts(self, pid):
|
|
358
|
+
"""
|
|
359
|
+
pid2pts takes a pid
|
|
360
|
+
returns the current pts
|
|
361
|
+
"""
|
|
362
|
+
prgm = self.pid2prgm(pid)
|
|
363
|
+
if prgm in self.maps.prgm_pts:
|
|
364
|
+
return self.as_90k(self.maps.prgm_pts[prgm])
|
|
365
|
+
return False
|
|
366
|
+
|
|
367
|
+
def pid2pcr(self, pid):
|
|
368
|
+
"""
|
|
369
|
+
pid2pcr takes a pid
|
|
370
|
+
returns the current pcr
|
|
371
|
+
"""
|
|
372
|
+
prgm = self.pid2prgm(pid)
|
|
373
|
+
if prgm in self.maps.prgm_pcr:
|
|
374
|
+
return self.as_90k(self.maps.prgm_pcr[prgm])
|
|
375
|
+
return False
|
|
376
|
+
|
|
377
|
+
def _unpad_afc(self, pkt):
|
|
378
|
+
if self._afc_flag(pkt[3]):
|
|
379
|
+
pkt = pkt[:4] + self._unpad(pkt[4:])
|
|
380
|
+
return pkt
|
|
381
|
+
|
|
382
|
+
def _unpad(self, bites):
|
|
383
|
+
pad = 255
|
|
384
|
+
one = 1
|
|
385
|
+
if not bites:
|
|
386
|
+
return b""
|
|
387
|
+
if bites[0] in [pad]:
|
|
388
|
+
self._unpad(bites[one:])
|
|
389
|
+
return bites
|
|
390
|
+
|
|
391
|
+
def _mk_packet_data(self, pid):
|
|
392
|
+
prgm = self.maps.pid_prgm[pid]
|
|
393
|
+
pdata = PacketData(pid, prgm)
|
|
394
|
+
pdata.mk_pcr(self.maps.prgm_pcr)
|
|
395
|
+
pdata.mk_pts(self.maps.prgm_pts)
|
|
396
|
+
return pdata
|
|
397
|
+
|
|
398
|
+
def _parse_cc(self, pkt, pid):
|
|
399
|
+
last_cc = None
|
|
400
|
+
c_c = pkt[3] & 0xF
|
|
401
|
+
if pid in self.maps.pid_cc:
|
|
402
|
+
last_cc = self.maps.pid_cc[pid]
|
|
403
|
+
good = (last_cc, ((last_cc + 1) % 16))
|
|
404
|
+
if c_c not in good:
|
|
405
|
+
print2(f" # BAD --> pid:\t{hex(pid)}\tlast cc:\t{last_cc}\tcc:\t{c_c}")
|
|
406
|
+
self.maps.pid_cc[pid] = c_c
|
|
407
|
+
|
|
408
|
+
def _parse_pts(self, pkt, pid):
|
|
409
|
+
"""
|
|
410
|
+
parse pts and store by program key
|
|
411
|
+
in the dict Stream._pid_pts
|
|
412
|
+
"""
|
|
413
|
+
payload = self._parse_payload(pkt)
|
|
414
|
+
if len(payload) > 13:
|
|
415
|
+
if self._pts_flag(payload):
|
|
416
|
+
pts = (payload[9] & 14) << 29
|
|
417
|
+
pts |= payload[10] << 22
|
|
418
|
+
pts |= (payload[11] >> 1) << 15
|
|
419
|
+
pts |= payload[12] << 7
|
|
420
|
+
pts |= payload[13] >> 1
|
|
421
|
+
prgm = self.pid2prgm(pid)
|
|
422
|
+
self.maps.prgm_pts[prgm] = pts
|
|
423
|
+
if prgm not in self.start:
|
|
424
|
+
self.start[prgm] = pts
|
|
425
|
+
|
|
426
|
+
def _parse_pcr(self, pkt, pid):
|
|
427
|
+
if self._afc_flag(pkt[3]):
|
|
428
|
+
pcr = pkt[6] << 25
|
|
429
|
+
pcr |= pkt[7] << 17
|
|
430
|
+
pcr |= pkt[8] << 9
|
|
431
|
+
pcr |= pkt[9] << 1
|
|
432
|
+
pcr |= pkt[10] >> 7
|
|
433
|
+
prgm = self.pid2prgm(pid)
|
|
434
|
+
self.maps.prgm_pcr[prgm] = pcr
|
|
435
|
+
|
|
436
|
+
def _parse_payload(self, pkt):
|
|
437
|
+
"""
|
|
438
|
+
_parse_payload returns the packet payload
|
|
439
|
+
"""
|
|
440
|
+
head_size = 4
|
|
441
|
+
if self._afc_flag(pkt[3]):
|
|
442
|
+
pkt = self._unpad_afc(pkt)
|
|
443
|
+
afl = pkt[4]
|
|
444
|
+
head_size += afl + 1 # +1 for afl byte
|
|
445
|
+
return pkt[head_size:]
|
|
446
|
+
|
|
447
|
+
def _parse_tables(self, pkt, pid):
|
|
448
|
+
"""
|
|
449
|
+
_parse_tables parse for
|
|
450
|
+
PAT, PMT, and SDT tables
|
|
451
|
+
based on pid of the pkt
|
|
452
|
+
"""
|
|
453
|
+
pay = self._parse_payload(pkt)
|
|
454
|
+
if self._same_as_last(pay, pid):
|
|
455
|
+
return False
|
|
456
|
+
if pid in self.pids.pmt:
|
|
457
|
+
return self._parse_pmt(pay, pid)
|
|
458
|
+
if pid == self.pids.PAT_PID:
|
|
459
|
+
return self._parse_pat(pay)
|
|
460
|
+
if pid == self.pids.SDT_PID and self.info:
|
|
461
|
+
return self._parse_sdt(pay)
|
|
462
|
+
return False
|
|
463
|
+
|
|
464
|
+
def _parse_info(self, pkt):
|
|
465
|
+
"""
|
|
466
|
+
_parse_info parses the packet for tables
|
|
467
|
+
and returns the pid
|
|
468
|
+
"""
|
|
469
|
+
pid = self._parse_pid(pkt[1], pkt[2])
|
|
470
|
+
if pid in self.pids.tables:
|
|
471
|
+
self._parse_tables(pkt, pid)
|
|
472
|
+
return pid
|
|
473
|
+
|
|
474
|
+
def _parse(self, pkt):
|
|
475
|
+
cue = False
|
|
476
|
+
pid = self._parse_info(pkt)
|
|
477
|
+
# self._parse_cc(pkt, pid)
|
|
478
|
+
if self._pcr_flag(pkt):
|
|
479
|
+
self._parse_pcr(pkt, pid)
|
|
480
|
+
if self._pusi_flag(pkt):
|
|
481
|
+
self._parse_pts(pkt, pid)
|
|
482
|
+
if self._pid_has_scte35(pid):
|
|
483
|
+
cue = self._parse_scte35(pkt, pid)
|
|
484
|
+
return cue
|
|
485
|
+
|
|
486
|
+
def _pid_has_scte35(self, pid):
|
|
487
|
+
if pid in self.pids.scte35 or pid in self.pids.maybe_scte35:
|
|
488
|
+
return True
|
|
489
|
+
return False
|
|
490
|
+
|
|
491
|
+
def _chk_partial(self, pay, pid, sep):
|
|
492
|
+
if pid in self.maps.partial:
|
|
493
|
+
pay = self.maps.partial.pop(pid) + pay
|
|
494
|
+
return self._split_by_idx(pay, sep)
|
|
495
|
+
|
|
496
|
+
def _same_as_last(self, pay, pid):
|
|
497
|
+
same = False
|
|
498
|
+
if pid in self.maps.last:
|
|
499
|
+
same = (False, True)[pay == self.maps.last[pid]]
|
|
500
|
+
if not same:
|
|
501
|
+
self.maps.last[pid] = pay
|
|
502
|
+
return same
|
|
503
|
+
|
|
504
|
+
def _section_incomplete(self, pay, pid, seclen):
|
|
505
|
+
# + 3 for the bytes before section starts
|
|
506
|
+
if (seclen + 3) > len(pay):
|
|
507
|
+
self.maps.partial[pid] = pay
|
|
508
|
+
return True
|
|
509
|
+
return False
|
|
510
|
+
|
|
511
|
+
def _parse_cue(self, pay, pid):
|
|
512
|
+
packet_data = None
|
|
513
|
+
packet_data = self._mk_packet_data(pid)
|
|
514
|
+
cue = Cue(pay, packet_data)
|
|
515
|
+
if cue.decode():
|
|
516
|
+
return cue
|
|
517
|
+
return False
|
|
518
|
+
|
|
519
|
+
def _strip_scte35_pes(self, pkt):
|
|
520
|
+
pay = self._parse_payload(pkt)
|
|
521
|
+
if self.SCTE35_PES_START in pay:
|
|
522
|
+
# print2(f"# Stripping PES Header from SCTE35 @ {self.pid2pts(pid)}")
|
|
523
|
+
pay = pay.split(self.SCTE35_PES_START, 1)[-1]
|
|
524
|
+
peslen = pay[4] + 5 # PES header length
|
|
525
|
+
pay = pay[peslen:]
|
|
526
|
+
return pay
|
|
527
|
+
|
|
528
|
+
def _chk_maybe_pid(self, pay, pid):
|
|
529
|
+
pay = self._chk_partial(pay, pid, self.SCTE35_TID)
|
|
530
|
+
if not pay:
|
|
531
|
+
if pid in self.pids.maybe_scte35:
|
|
532
|
+
self.pids.maybe_scte35.remove(pid)
|
|
533
|
+
return False
|
|
534
|
+
if pay[13] == self.show_null:
|
|
535
|
+
return False
|
|
536
|
+
return pay
|
|
537
|
+
|
|
538
|
+
def _mk_scte35_payload(self, pkt, pid):
|
|
539
|
+
pay = self._strip_scte35_pes(pkt)
|
|
540
|
+
if not pay:
|
|
541
|
+
return False
|
|
542
|
+
return self._chk_maybe_pid(pay, pid)
|
|
543
|
+
|
|
544
|
+
def _parse_scte35(self, pkt, pid):
|
|
545
|
+
"""
|
|
546
|
+
parse a scte35 cue from one or more packets
|
|
547
|
+
"""
|
|
548
|
+
pay = self._mk_scte35_payload(pkt, pid)
|
|
549
|
+
if not pay:
|
|
550
|
+
return False
|
|
551
|
+
seclen = self._parse_length(pay[1], pay[2])
|
|
552
|
+
if self._section_incomplete(pay, pid, seclen):
|
|
553
|
+
return False
|
|
554
|
+
pay = pay[: seclen + 3]
|
|
555
|
+
cue = self._parse_cue(pay, pid)
|
|
556
|
+
return cue
|
|
557
|
+
|
|
558
|
+
def _parse_sdt(self, pay):
|
|
559
|
+
"""
|
|
560
|
+
_parse_sdt parses the SDT for program metadata
|
|
561
|
+
"""
|
|
562
|
+
pay = self._chk_partial(pay, self.pids.SDT_PID, self.SDT_TID)
|
|
563
|
+
if not pay:
|
|
564
|
+
return False
|
|
565
|
+
seclen = self._parse_length(pay[1], pay[2])
|
|
566
|
+
if self._section_incomplete(pay, self.pids.SDT_PID, seclen):
|
|
567
|
+
return False
|
|
568
|
+
idx = 11
|
|
569
|
+
while idx < seclen + 3:
|
|
570
|
+
service_id = self._parse_program(pay[idx], pay[idx + 1])
|
|
571
|
+
idx += 3
|
|
572
|
+
dloop_len = self._parse_length(pay[idx], pay[idx + 1])
|
|
573
|
+
idx += 2
|
|
574
|
+
i = 0
|
|
575
|
+
while i < dloop_len:
|
|
576
|
+
if pay[idx] == 0x48:
|
|
577
|
+
i += 3
|
|
578
|
+
spnl = pay[idx + i]
|
|
579
|
+
i += 1
|
|
580
|
+
service_provider_name = pay[idx + i : idx + i + spnl]
|
|
581
|
+
i += spnl
|
|
582
|
+
snl = pay[idx + i]
|
|
583
|
+
i += 1
|
|
584
|
+
service_name = pay[idx + i : idx + i + snl]
|
|
585
|
+
i += snl
|
|
586
|
+
if service_id not in self.maps.prgm:
|
|
587
|
+
self.maps.prgm[service_id] = ProgramInfo()
|
|
588
|
+
pinfo = self.maps.prgm[service_id]
|
|
589
|
+
pinfo.provider = service_provider_name
|
|
590
|
+
pinfo.service = service_name
|
|
591
|
+
i = dloop_len
|
|
592
|
+
idx += i
|
|
593
|
+
return True
|
|
594
|
+
return False
|
|
595
|
+
|
|
596
|
+
def _parse_pat(self, pay):
|
|
597
|
+
"""
|
|
598
|
+
parse program association table
|
|
599
|
+
for program to pmt_pid mappings.
|
|
600
|
+
"""
|
|
601
|
+
pay = self._chk_partial(pay, self.pids.PAT_PID, b"")
|
|
602
|
+
seclen = self._parse_length(pay[2], pay[3])
|
|
603
|
+
if self._section_incomplete(pay, self.pids.PAT_PID, seclen):
|
|
604
|
+
return False
|
|
605
|
+
seclen -= 5 # pay bytes 4,5,6,7,8
|
|
606
|
+
idx = 9
|
|
607
|
+
chunk_size = 4
|
|
608
|
+
while seclen > 4: # 4 bytes for crc
|
|
609
|
+
program_number = self._parse_program(pay[idx], pay[idx + 1])
|
|
610
|
+
if program_number > 0:
|
|
611
|
+
pmt_pid = self._parse_pid(pay[idx + 2], pay[idx + 3])
|
|
612
|
+
self.pids.pmt.add(pmt_pid)
|
|
613
|
+
self.pids.tables.add(pmt_pid)
|
|
614
|
+
seclen -= chunk_size
|
|
615
|
+
idx += chunk_size
|
|
616
|
+
return True
|
|
617
|
+
|
|
618
|
+
def _mk_pmt_payload(self, pay, pid):
|
|
619
|
+
seclen = False
|
|
620
|
+
program_number = False
|
|
621
|
+
pay = self._chk_partial(pay, pid, self.PMT_TID)
|
|
622
|
+
if not pay:
|
|
623
|
+
return False
|
|
624
|
+
seclen = self._parse_length(pay[1], pay[2])
|
|
625
|
+
if self._section_incomplete(pay, pid, seclen):
|
|
626
|
+
return False
|
|
627
|
+
program_number = self._parse_program(pay[3], pay[4])
|
|
628
|
+
if self.the_program and (program_number != self.the_program):
|
|
629
|
+
return False
|
|
630
|
+
return pay, seclen, program_number
|
|
631
|
+
|
|
632
|
+
def _parse_pmt(self, pay, pid):
|
|
633
|
+
"""
|
|
634
|
+
parse program maps for streams
|
|
635
|
+
"""
|
|
636
|
+
pay, seclen, program_number = self._mk_pmt_payload(pay, pid)
|
|
637
|
+
if not program_number:
|
|
638
|
+
return False
|
|
639
|
+
pcr_pid = self._parse_pid(pay[8], pay[9])
|
|
640
|
+
if program_number not in self.maps.prgm:
|
|
641
|
+
self.maps.prgm[program_number] = ProgramInfo()
|
|
642
|
+
pinfo = self.maps.prgm[program_number]
|
|
643
|
+
pinfo.pid = pid
|
|
644
|
+
pinfo.pcr_pid = pcr_pid
|
|
645
|
+
self.pids.pcr.add(pcr_pid)
|
|
646
|
+
self.maps.pid_prgm[pcr_pid] = program_number
|
|
647
|
+
proginfolen = self._parse_length(pay[10], pay[11])
|
|
648
|
+
idx = 12 + proginfolen
|
|
649
|
+
si_len = seclen - (9 + proginfolen)
|
|
650
|
+
self._parse_program_streams(si_len, pay, idx, program_number)
|
|
651
|
+
return True
|
|
652
|
+
|
|
653
|
+
def _parse_program_streams(self, si_len, pay, idx, program_number):
|
|
654
|
+
"""
|
|
655
|
+
parse the elementary streams
|
|
656
|
+
from a program
|
|
657
|
+
"""
|
|
658
|
+
# 5 bytes for stream_type info
|
|
659
|
+
chunk_size = 5
|
|
660
|
+
end_idx = (idx + si_len) - chunk_size
|
|
661
|
+
while idx < end_idx:
|
|
662
|
+
stream_type, pid, ei_len = self._parse_stream_type(pay, idx)
|
|
663
|
+
pinfo = self.maps.prgm[program_number]
|
|
664
|
+
pinfo.streams[pid] = stream_type
|
|
665
|
+
idx += chunk_size + ei_len
|
|
666
|
+
self.maps.pid_prgm[pid] = program_number
|
|
667
|
+
|
|
668
|
+
def _parse_stream_type(self, pay, idx):
|
|
669
|
+
"""
|
|
670
|
+
extract stream pid and type
|
|
671
|
+
"""
|
|
672
|
+
stream_type = hex(pay[idx])
|
|
673
|
+
el_pid = self._parse_pid(pay[idx + 1], pay[idx + 2])
|
|
674
|
+
ei_len = self._parse_length(pay[idx + 3], pay[idx + 4])
|
|
675
|
+
self._set_scte35_pids(el_pid, stream_type)
|
|
676
|
+
return stream_type, el_pid, ei_len
|
|
677
|
+
|
|
678
|
+
def _set_scte35_pids(self, pid, stream_type):
|
|
679
|
+
"""
|
|
680
|
+
if stream_type is 0x06 or 0x86
|
|
681
|
+
add it to self._scte35_pids.
|
|
682
|
+
"""
|
|
683
|
+
if stream_type in ["0x86"]:
|
|
684
|
+
self.pids.scte35.add(pid)
|
|
685
|
+
if stream_type in ["0x06", "0x6"]:
|
|
686
|
+
self.pids.maybe_scte35.add(pid)
|