Skip to content

Commit

Permalink
Fix MPEG-TS probing when PAT and PMT are not present in the first thr…
Browse files Browse the repository at this point in the history
…ee TS packets (#5252)

Fixes regression in #5251
  • Loading branch information
robwalch committed Feb 28, 2023
1 parent bf49dd2 commit a056f08
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/controller/base-stream-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -506,8 +506,8 @@ export default class BaseStreamController
part ? ' part: ' + part.index : ''
} of ${this.logPrefix === '[stream-controller]' ? 'level' : 'track'} ${
frag.level
} (frag:[${(frag.startPTS || NaN).toFixed(3)}-${(
frag.endPTS || NaN
} (frag:[${(frag.startPTS ?? NaN).toFixed(3)}-${(
frag.endPTS ?? NaN
).toFixed(3)}] > buffer:${
media
? TimeRanges.toString(BufferHelper.getBuffered(media))
Expand Down
7 changes: 5 additions & 2 deletions src/demux/tsdemuxer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,14 @@ class TSDemuxer implements Demuxer {
}

static syncOffset(data: Uint8Array): number {
const length = data.length;
const scanwindow =
Math.min(PACKET_LENGTH * 5, data.length - PACKET_LENGTH) + 1;
let i = 0;
while (i < scanwindow) {
// a TS init segment should contain at least 2 TS packets: PAT and PMT, each starting with 0x47
let foundPat = false;
for (let j = 0; j < scanwindow; j += PACKET_LENGTH) {
for (let j = i; j < length; j += PACKET_LENGTH) {
if (data[j] === 0x47) {
if (!foundPat && parsePID(data, j) === 0) {
foundPat = true;
Expand Down Expand Up @@ -357,7 +358,9 @@ class TSDemuxer implements Demuxer {
}

if (unknownPID !== null && !pmtParsed) {
logger.log(`unknown PID '${unknownPID}' in TS found`);
logger.warn(
`MPEG-TS PMT found at ${start} after unknown PID '${unknownPID}'. Backtracking to sync byte @${syncOffset} to parse all TS packets.`
);
unknownPID = null;
// we set it to -188, the += 188 in the for loop will reset start to 0
start = syncOffset - 188;
Expand Down

0 comments on commit a056f08

Please sign in to comment.