diff --git a/cmd/mjpeg-player/hlsjs/mts-demuxer.js b/cmd/mjpeg-player/hlsjs/mts-demuxer.js index 0eecfdc0..11c1f5d6 100644 --- a/cmd/mjpeg-player/hlsjs/mts-demuxer.js +++ b/cmd/mjpeg-player/hlsjs/mts-demuxer.js @@ -36,6 +36,7 @@ class MTSDemuxer{ init() { this.pmtParsed = false; this._pmtId = -1; + this._videoTrack = MTSDemuxer.createTrack('video'); this._audioTrack = MTSDemuxer.createTrack('audio'); this._id3Track = MTSDemuxer.createTrack('id3'); @@ -80,34 +81,29 @@ class MTSDemuxer{ } append(data) { - let videoTrack = this._videoTrack; - let videoData = videoTrack.pesData; - let videoId = videoTrack.pid; + let start, len = data.length, pusi, pid, afc, offset, pes, + unknownPIDs = false; + let pmtParsed = this.pmtParsed, + videoTrack = this._videoTrack, + audioTrack = this._audioTrack, + id3Track = this._id3Track, + videoId = videoTrack.pid, + audioId = audioTrack.pid, + id3Id = id3Track.pid, + pmtId = this._pmtId, + videoData = videoTrack.pesData, + audioData = audioTrack.pesData, + id3Data = id3Track.pesData, + parsePAT = this._parsePAT, + parsePMT = this._parsePMT, + parsePES = this._parsePES; - let audioTrack = this._audioTrack; - let audioData = audioTrack.pesData; - let audioId = audioTrack.pid; - - let id3Track = this._id3Track; - let id3Data = id3Track.pesData; - let id3Id = id3Track.pid; - - let pmtId = this._pmtId; - let pmtParsed = this.pmtParsed; - let parsePAT = this._parsePAT; - let parsePMT = this._parsePMT; - let parsePES = this._parsePES; - - let len = data.length; - let unknownPIDs = false; - const syncOffset = MTSDemuxer._syncOffset(data); // Don't parse last TS packet if incomplete. len -= (len + syncOffset) % 188; // Loop through TS packets. - let start, offset, pusi, pid, afc, pes; for (start = syncOffset; start < len; start += 188) { if (data[start] === 0x47) { pusi = !!(data[start + 1] & 0x40); @@ -242,12 +238,12 @@ class MTSDemuxer{ } _parsePMT (data, offset) { - let pid; - let result = { audio: -1, video: -1, id3: -1}; - let sectionLength = (data[offset + 1] & 0x0f) << 8 | data[offset + 2]; - let tableEnd = offset + 3 + sectionLength - 4; - // To determine where the table is, we have to figure out how long the program info descriptors are. - let programInfoLength = (data[offset + 10] & 0x0f) << 8 | data[offset + 11]; + let programInfoLength, pid, result = { audio: -1, video: -1, id3: -1}, + sectionLength = (data[offset + 1] & 0x0f) << 8 | data[offset + 2], + tableEnd = offset + 3 + sectionLength - 4; + // To determine where the table is, we have to figure out how + // long the program info descriptors are. + programInfoLength = (data[offset + 10] & 0x0f) << 8 | data[offset + 11]; // Advance the offset to the first entry in the mapping table. offset += 12 + programInfoLength; while (offset < tableEnd) { @@ -259,27 +255,27 @@ class MTSDemuxer{ if (result.video === -1) { result.video = pid; } - break; - case 0xcf: // SAMPLE-AES AAC. - case 0x0f: // ISO/IEC 13818-7 ADTS AAC (MPEG-2 lower bit-rate audio). - case 0xd2: // ADPCM audio. - case 0x03: // ISO/IEC 11172-3 (MPEG-1 audio). - case 0x24: - // console.warn('HEVC stream type found, not supported for now'); - case 0x04: // or ISO/IEC 13818-3 (MPEG-2 halved sample rate audio). - if (result.audio === -1) { - result.audio = pid; - } - break; - case 0x15: // Packetized metadata (ID3) - // console.log('ID3 PID:' + pid); - if (result.id3 === -1) { - result.id3 = pid; - } - break; - default: - // console.log('unknown stream type:' + data[offset]); - break; + break; + case 0xcf: // SAMPLE-AES AAC. + case 0x0f: // ISO/IEC 13818-7 ADTS AAC (MPEG-2 lower bit-rate audio). + case 0xd2: // ADPCM audio. + case 0x03: // ISO/IEC 11172-3 (MPEG-1 audio). + case 0x24: + // console.warn('HEVC stream type found, not supported for now'); + case 0x04: // or ISO/IEC 13818-3 (MPEG-2 halved sample rate audio). + if (result.audio === -1) { + result.audio = pid; + } + break; + case 0x15: // Packetized metadata (ID3) + // console.log('ID3 PID:' + pid); + if (result.id3 === -1) { + result.id3 = pid; + } + break; + default: + // console.log('unknown stream type:' + data[offset]); + break; } // Move to the next table entry, skip past the elementary stream descriptors, if present. offset += ((data[offset + 3] & 0x0F) << 8 | data[offset + 4]) + 5;