Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MPEG-TS probe improvement #5186

Merged
merged 1 commit into from
Feb 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 30 additions & 11 deletions src/demux/tsdemuxer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,24 @@ class TSDemuxer implements Demuxer {
return syncOffset !== -1;
}

static syncOffset(data: Uint8Array) {
static syncOffset(data: Uint8Array): number {
const scanwindow =
Math.min(PACKET_LENGTH * 5, data.length - PACKET_LENGTH * 2) + 1;
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
if (data[i] === 0x47 && data[i + PACKET_LENGTH] === 0x47) {
return i;
let foundPat = false;
for (let j = 0; j < scanwindow; j += PACKET_LENGTH) {
if (data[j] === 0x47) {
if (!foundPat && parsePID(data, j) === 0) {
foundPat = true;
}
if (foundPat && j + PACKET_LENGTH > scanwindow) {
return i;
}
} else {
break;
}
}
i++;
}
Expand Down Expand Up @@ -245,8 +255,7 @@ class TSDemuxer implements Demuxer {
for (let start = syncOffset; start < len; start += PACKET_LENGTH) {
if (data[start] === 0x47) {
const stt = !!(data[start + 1] & 0x40);
// pid is a 13-bit field starting at the last bit of TS[1]
const pid = ((data[start + 1] & 0x1f) << 8) + data[start + 2];
const pid = parsePID(data, start);
const atf = (data[start + 3] & 0x30) >> 4;

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

pmtId = this._pmtId = parsePAT(data, offset);
// logger.log('PMT PID:' + this._pmtId);
break;
case pmtId: {
if (stt) {
Expand Down Expand Up @@ -355,7 +365,7 @@ class TSDemuxer implements Demuxer {
pmtParsed = this.pmtParsed = true;
break;
}
case 17:
case 0x11:
case 0x1fff:
break;
default:
Expand Down Expand Up @@ -988,13 +998,22 @@ function createAVCSample(
};
}

function parsePAT(data, offset) {
function parsePID(data: Uint8Array, offset: number): number {
// pid is a 13-bit field starting at the last bit of TS[1]
return ((data[offset + 1] & 0x1f) << 8) + data[offset + 2];
}

function parsePAT(data: Uint8Array, offset: number): number {
// skip the PSI header and parse the first PMT entry
return ((data[offset + 10] & 0x1f) << 8) | data[offset + 11];
// logger.log('PMT PID:' + this._pmtId);
}

function parsePMT(data, offset, typeSupported, isSampleAes) {
function parsePMT(
data: Uint8Array,
offset: number,
typeSupported: TypeSupported,
isSampleAes: boolean
) {
const result = { audio: -1, avc: -1, id3: -1, segmentCodec: 'aac' };
const sectionLength = ((data[offset + 1] & 0x0f) << 8) | data[offset + 2];
const tableEnd = offset + 3 + sectionLength - 4;
Expand All @@ -1005,7 +1024,7 @@ function parsePMT(data, offset, typeSupported, isSampleAes) {
// advance the offset to the first entry in the mapping table
offset += 12 + programInfoLength;
while (offset < tableEnd) {
const pid = ((data[offset + 1] & 0x1f) << 8) | data[offset + 2];
const pid = parsePID(data, offset);
switch (data[offset]) {
case 0xcf: // SAMPLE-AES AAC
if (!isSampleAes) {
Expand Down