Skip to content

Commit

Permalink
fix: only parse PES packets as PES packets (#378)
Browse files Browse the repository at this point in the history
  • Loading branch information
realeyes-ben committed Mar 5, 2021
1 parent 989bffd commit bb984db
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
10 changes: 9 additions & 1 deletion lib/m2ts/m2ts.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,15 @@ ElementaryStream = function() {
programMapTable,
parsePes = function(payload, pes) {
var ptsDtsFlags;

const startPrefix = payload[0] << 16 | payload[1] << 8 | payload[2]
// default to an empty array
pes.data = new Uint8Array()
// In certain live streams, the start of a TS fragment has ts packets
// that are frame data that is continuing from the previous fragment. This
// is to check that the pes data is the start of a new pes payload
if (startPrefix !== 1) {
return;
}
// get the packet length, this will be 0 for video
pes.packetLength = 6 + ((payload[4] << 8) | payload[5]);

Expand Down
34 changes: 34 additions & 0 deletions test/transmuxer.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,40 @@ QUnit.test('aggregates program stream packets from the transport stream', functi
assert.equal(events[0].data.byteLength, packetData.length, 'concatenated transport packets');
});

QUnit.test('aggregates program stream packets from the transport stream with no header data', function(assert) {
var events = []

elementaryStream.on('data', function(event) {
events.push(event);
});

elementaryStream.push({
type: 'pes',
streamType: H264_STREAM_TYPE,
data: new Uint8Array([0x1, 0x2, 0x3])
});

assert.equal(events.length, 0, 'buffers partial packets');

elementaryStream.push({
type: 'pes',
streamType: H264_STREAM_TYPE,
payloadUnitStartIndicator: true,
data: new Uint8Array([0x4, 0x5, 0x6])
});

elementaryStream.push({
type: 'pes',
streamType: H264_STREAM_TYPE,
data: new Uint8Array([0x7, 0x8, 0x9])
});
elementaryStream.flush();

assert.equal(events.length, 1, 'built one packet');
assert.equal(events[0].type, 'video', 'identified video data');
assert.equal(events[0].data.byteLength, 0, 'empty packet');
});

QUnit.test('parses an elementary stream packet with just a pts', function(assert) {
var packet;
elementaryStream.on('data', function(data) {
Expand Down

0 comments on commit bb984db

Please sign in to comment.