diff --git a/mpegtsmod.py b/mpegtsmod.py index b3fffb7..72fa8db 100644 --- a/mpegtsmod.py +++ b/mpegtsmod.py @@ -109,7 +109,7 @@ class DynamTSTransfer(pb.Viewable): #log.msg("DynamTSTransfer: pmt: %s, pids: %s" % (pmt, pids)) self.pmt = pmt self.pids = pids - self.didpmt = False + self.didpat = False def resumeProducing(self): if not self.request: @@ -125,22 +125,39 @@ class DynamTSTransfer(pb.Viewable): continue frst = dataarray[i + 1] pid = (frst & 0x1f) << 8 | dataarray[i + 2] + if not frst & 0x40: continue - elif not self.didpmt and pid == self.pmt: - startpmt = 4 + elif not self.didpat and pid == 0: + startpmt = i + 4 if ((dataarray[i + 3] >> 4) & 0x3) == 0x3: # Adaptation - startpmt += dataarray[i + startpmt] + 1 - startpmt += dataarray[i + startpmt] + 1 - assert data[i + startpmt] =='\x02', (startpmt, - data[i:i + 20]) + startpmt += dataarray[startpmt] + 1 + startpmt += dataarray[startpmt] + 1 + assert data[startpmt] =='\x00', (startpmt, + data[i:startpmt + 4]) + arraysize = ((dataarray[startpmt + 1] & + 0xf) << 8) | dataarray[startpmt + 2] + startpmt += 3 + arraysize -= 4 # CRC + # Remaining fields before array + startpmt += 5 + arraysize -= 5 + for startpmt in xrange(startpmt, + min(i + 188 - 3, startpmt + arraysize), 4): + prognum, ppid = struct.unpack('>2H', + data[startpmt:startpmt + 4]) + ppid = ppid & 0x1fff + if ppid == self.pmt: + break + else: + raise KeyError, 'unable to find pmt(%d) in pkt: %s' % (pmt, `data[i:i + 188]`) + self.pats = itertools.cycle(tssel.genpats( - self.pmt, struct.unpack('>H', data[i + - startpmt + 3:i + startpmt + 5])[0])) - self.didpmt = True + self.pmt, prognum)) + self.didpat = True - if pid == 0 and self.didpmt: + if pid == 0 and self.didpat: assert data[i + 4] =='\x00' and \ data[i + 5] == '\x00', 'error: %s' % `data[i:i + 10]` repcnt += 1