8000 MPEG-TS probe improvement (#5186) · video-dev/hls.js@7a10ef6 · GitHub
[go: up one dir, main page]

Skip to content

Commit 7a10ef6

Browse files
authored
MPEG-TS probe improvement (#5186)
Fixes #5183
1 parent 9e6e3ba commit 7a10ef6

File tree

1 file changed

+30
-11
lines changed

1 file changed

+30
-11
lines changed

src/demux/tsdemuxer.ts

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,24 @@ class TSDemuxer implements Demuxer {
9898
return syncOffset !== -1;
9999
}
100100

101-
static syncOffset(data: Uint8Array) {
101+
static syncOffset(data: Uint8Array): number {
102102
const scanwindow =
103-
Math.min(PACKET_LENGTH * 5, data.length - PACKET_LENGTH * 2) + 1;
103+
Math.min(PACKET_LENGTH * 5, data.length - PACKET_LENGTH) + 1;
104104
let i = 0;
105105
while (i < scanwindow) {
106106
// a TS init segment should contain at least 2 TS packets: PAT and PMT, each starting with 0x47
107-
if (data[i] === 0x47 && data[i + PACKET_LENGTH] === 0x47) {
108-
return i;
107+
let foundPat = false;
108+
for (let j = 0; j < scanwindow; j += PACKET_LENGTH) {
109+
if (data[j] === 0x47) {
110+
if (!foundPat && parsePID(data, j) === 0) {
111+
foundPat = true;
112+
}
113+
if (foundPat && j + PACKET_LENGTH > scanwindow) {
114+
return i;
115+
}
116+
} else {
117+
break;
118+
}
109119
}
110120
i++;
111121
}
@@ -245,8 +255,7 @@ class TSDemuxer implements Demuxer {
245255
for (let start = syncOffset; start < len; start += PACKET_LENGTH) {
246256
if (data[start] === 0x47) {
247257
const stt = !!(data[start + 1] & 0x40);
248-
// pid is a 13-bit field starting at the last bit of TS[1]
249-
const pid = ((data[start + 1] & 0x1f) << 8) + data[start + 2];
258+
const pid = parsePID(data, start);
250259
const atf = (data[start + 3] & 0x30) >> 4;
251260

252261
// if an adaption field is present, its length is specified by the fifth byte of the TS packet header.
@@ -312,6 +321,7 @@ class TSDemuxer implements Demuxer {
312321
}
313322

314323
pmtId = this._pmtId = parsePAT(data, offset);
324+
// logger.log('PMT PID:' + this._pmtId);
315325
break;
316326
case pmtId: {
317327
if (stt) {
@@ -355,7 +365,7 @@ class TSDemuxer implements Demuxer {
355365
pmtParsed = this.pmtParsed = true;
356366
break;
357367
}
358-
case 17:
368+
case 0x11:
359369
case 0x1fff:
360370
break;
361371
default:
@@ -988,13 +998,22 @@ function createAVCSample(
988998
};
989999
}
9901000

991-
function parsePAT(data, offset) {
1001+
function parsePID(data: Uint8Array, offset: number): number {
1002+
// pid is a 13-bit field starting at the last bit of TS[1]
1003+
return ((data[offset + 1] & 0x1f) << 8) + data[offset + 2];
1004+
}
1005+
1006+
function parsePAT(data: Uint8Array, offset: number): number {
9921007
// skip the PSI header and parse the first PMT entry
9931008
return ((data[offset + 10] & 0x1f) << 8) | data[offset + 11];
994-
// logger.log('PMT PID:' + this._pmtId);
9951009
}
9961010

997-
function parsePMT(data, offset, typeSupported, isSampleAes) {
1011+
function parsePMT(
1012+
data: Uint8Array,
1013+
offset: number,
1014+
typeSupported: TypeSupported,
1015+
isSampleAes: boolean
1016+
) {
9981017
const result = { audio: -1, avc: -1, id3: -1, segmentCodec: 'aac' };
9991018
const sectionLength = ((data[offset + 1] & 0x0f) << 8) | data[offset + 2];
10001019
const tableEnd = offset + 3 + sectionLength - 4;
@@ -1005,7 +1024,7 @@ function parsePMT(data, offset, typeSupported, isSampleAes) {
10051024
// advance the offset to the first entry in the mapping table
10061025
offset += 12 + programInfoLength;
10071026
while (offset < tableEnd) {
1008-
const pid = ((data[offset + 1] & 0x1f) << 8) | data[offset + 2];
1027+
const pid = parsePID(data, offset);
10091028
switch (data[offset]) {
10101029
case 0xcf: // SAMPLE-AES AAC
10111030
if (!isSampleAes) {

0 commit comments

Comments
 (0)
0