Skip to content

Commit bb984db

Browse files
authored
fix: only parse PES packets as PES packets (#378)
1 parent 989bffd commit bb984db

File tree

2 files changed

+43
-1
lines changed

2 files changed

+43
-1
lines changed

lib/m2ts/m2ts.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,15 @@ ElementaryStream = function() {
310310
programMapTable,
311311
parsePes = function(payload, pes) {
312312
var ptsDtsFlags;
313-
313+
const startPrefix = payload[0] << 16 | payload[1] << 8 | payload[2]
314+
// default to an empty array
315+
pes.data = new Uint8Array()
316+
// In certain live streams, the start of a TS fragment has ts packets
317+
// that are frame data that is continuing from the previous fragment. This
318+
// is to check that the pes data is the start of a new pes payload
319+
if (startPrefix !== 1) {
320+
return;
321+
}
314322
// get the packet length, this will be 0 for video
315323
pes.packetLength = 6 + ((payload[4] << 8) | payload[5]);
316324

test/transmuxer.test.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,40 @@ QUnit.test('aggregates program stream packets from the transport stream', functi
419419
assert.equal(events[0].data.byteLength, packetData.length, 'concatenated transport packets');
420420
});
421421

422+
QUnit.test('aggregates program stream packets from the transport stream with no header data', function(assert) {
423+
var events = []
424+
425+
elementaryStream.on('data', function(event) {
426+
events.push(event);
427+
});
428+
429+
elementaryStream.push({
430+
type: 'pes',
431+
streamType: H264_STREAM_TYPE,
432+
data: new Uint8Array([0x1, 0x2, 0x3])
433+
});
434+
435+
assert.equal(events.length, 0, 'buffers partial packets');
436+
437+
elementaryStream.push({
438+
type: 'pes',
439+
streamType: H264_STREAM_TYPE,
440+
payloadUnitStartIndicator: true,
441+
data: new Uint8Array([0x4, 0x5, 0x6])
442+
});
443+
444+
elementaryStream.push({
445+
type: 'pes',
446+
streamType: H264_STREAM_TYPE,
447+
data: new Uint8Array([0x7, 0x8, 0x9])
448+
});
449+
elementaryStream.flush();
450+
451+
assert.equal(events.length, 1, 'built one packet');
452+
assert.equal(events[0].type, 'video', 'identified video data');
453+
assert.equal(events[0].data.byteLength, 0, 'empty packet');
454+
});
455+
422456
QUnit.test('parses an elementary stream packet with just a pts', function(assert) {
423457
var packet;
424458
elementaryStream.on('data', function(data) {

0 commit comments

Comments
 (0)