Skip to content

Commit

Permalink
refactor: rename warn event to log, change console logs to log events (
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonocasey committed Jul 9, 2021
1 parent abce11f commit 4995603
Show file tree
Hide file tree
Showing 11 changed files with 187 additions and 35 deletions.
5 changes: 4 additions & 1 deletion lib/codecs/adts.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,10 @@ AdtsStream = function(handlePartialSegments) {
AdtsStream.prototype.init.call(this);

this.skipWarn_ = function(start, end) {
this.trigger('warn', {message: `adts skiping bytes ${start} to ${end} in frame ${frameNum} outside syncword`});
this.trigger('log', {
level: 'warn',
message: `adts skiping bytes ${start} to ${end} in frame ${frameNum} outside syncword`
});
};

this.push = function(packet) {
Expand Down
10 changes: 6 additions & 4 deletions lib/m2ts/caption-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -1369,18 +1369,20 @@ Cea608Stream.prototype = new Stream();
Cea608Stream.prototype.flushDisplayed = function(pts) {
var content = this.displayed_
// remove spaces from the start and end of the string
.map(function(row) {
.map(function(row, index) {
try {
return row.trim();
} catch (e) {
// Ordinarily, this shouldn't happen. However, caption
// parsing errors should not throw exceptions and
// break playback.
// eslint-disable-next-line no-console
console.error('Skipping malformed caption.');
this.trigger('log', {
level: 'warn',
message: 'Skipping a malformed 608 caption at index ' + index + '.'
});
return '';
}
})
}, this)
// combine all text rows to display in one cue
.join('\n')
// and remove blank rows from the start and end, but not the middle
Expand Down
17 changes: 9 additions & 8 deletions lib/m2ts/metadata-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,6 @@ var
MetadataStream = function(options) {
var
settings = {
debug: !!(options && options.debug),

// the bytes of the program-level descriptor field in MP2T
// see ISO/IEC 13818-1:2013 (E), section 2.6 "Program and
// program element descriptors"
Expand Down Expand Up @@ -137,10 +135,10 @@ MetadataStream = function(options) {
chunk.data[0] !== 'I'.charCodeAt(0) ||
chunk.data[1] !== 'D'.charCodeAt(0) ||
chunk.data[2] !== '3'.charCodeAt(0))) {
if (settings.debug) {
// eslint-disable-next-line no-console
console.log('Skipping unrecognized metadata packet');
}
this.trigger('log', {
level: 'warn',
message: 'Skipping unrecognized metadata packet'
});
return;
}

Expand Down Expand Up @@ -198,8 +196,11 @@ MetadataStream = function(options) {
// determine the number of bytes in this frame
frameSize = parseSyncSafeInteger(tag.data.subarray(frameStart + 4, frameStart + 8));
if (frameSize < 1) {
// eslint-disable-next-line no-console
return console.log('Malformed ID3 frame encountered. Skipping metadata parsing.');
this.trigger('log', {
level: 'warn',
message: 'Malformed ID3 frame encountered. Skipping metadata parsing.'
});
return;
}
frameHeader = String.fromCharCode(tag.data[frameStart],
tag.data[frameStart + 1],
Expand Down
47 changes: 35 additions & 12 deletions lib/mp4/caption-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ var mapToSample = function(offset, samples) {
var findSeiNals = function(avcStream, samples, trackId) {
var
avcView = new DataView(avcStream.buffer, avcStream.byteOffset, avcStream.byteLength),
result = [],
result = {
logs: [],
seiNals: []
},
seiNal,
i,
length,
Expand Down Expand Up @@ -97,12 +100,14 @@ var findSeiNals = function(avcStream, samples, trackId) {
seiNal.pts = lastMatchedSample.pts;
seiNal.dts = lastMatchedSample.dts;
} else {
// eslint-disable-next-line no-console
console.log("We've encountered a nal unit without data. See mux.js#233.");
result.logs.push({
level: 'warn',
message: 'We\'ve encountered a nal unit without data at ' + i + ' for trackId ' + trackId + '. See mux.js#223.'
});
break;
}

result.push(seiNal);
result.seiNals.push(seiNal);
break;
default:
break;
Expand Down Expand Up @@ -199,19 +204,20 @@ var parseCaptionNals = function(segment, videoTrackId) {
var baseMediaDecodeTime = (tfdt.length > 0) ? parseTfdt(tfdt[0]).baseMediaDecodeTime : 0;
var truns = findBox(traf, ['trun']);
var samples;
var seiNals;
var result;

// Only parse video data for the chosen video track
if (videoTrackId === trackId && truns.length > 0) {
samples = parseSamples(truns, baseMediaDecodeTime, headerInfo);

seiNals = findSeiNals(mdat, samples, trackId);
result = findSeiNals(mdat, samples, trackId);

if (!captionNals[trackId]) {
captionNals[trackId] = [];
captionNals[trackId] = {seiNals: [], logs: []};
}

captionNals[trackId] = captionNals[trackId].concat(seiNals);
captionNals[trackId].seiNals = captionNals[trackId].seiNals.concat(result.seiNals);
captionNals[trackId].logs = captionNals[trackId].logs.concat(result.logs);
}
});

Expand All @@ -235,17 +241,20 @@ var parseCaptionNals = function(segment, videoTrackId) {
* @return {String} parsedCaptions[].text - The visible content of the caption
**/
var parseEmbeddedCaptions = function(segment, trackId, timescale) {
var seiNals;
var captionNals;

// the ISO-BMFF spec says that trackId can't be zero, but there's some broken content out there
if (trackId === null) {
return null;
}

seiNals = parseCaptionNals(segment, trackId);
captionNals = parseCaptionNals(segment, trackId);

var trackNals = captionNals[trackId] || {};

return {
seiNals: seiNals[trackId],
seiNals: trackNals.seiNals,
logs: trackNals.logs,
timescale: timescale
};
};
Expand Down Expand Up @@ -294,6 +303,10 @@ var CaptionParser = function() {
parsedCaptions.captions.push(event);
parsedCaptions.captionStreams[event.stream] = true;
});

captionStream.on('log', function(log) {
parsedCaptions.logs.push(log);
});
};

/**
Expand Down Expand Up @@ -355,7 +368,15 @@ var CaptionParser = function() {

parsedData = parseEmbeddedCaptions(segment, trackId, timescale);

if (parsedData && parsedData.logs) {
parsedCaptions.logs = parsedCaptions.logs.concat(parsedData.logs);
}

if (parsedData === null || !parsedData.seiNals) {
if (parsedCaptions.logs.length) {
return {logs: parsedCaptions.logs, captions: [], captionStreams: []};
}

return null;
}

Expand Down Expand Up @@ -404,6 +425,7 @@ var CaptionParser = function() {
this.clearParsedCaptions = function() {
parsedCaptions.captions = [];
parsedCaptions.captionStreams = {};
parsedCaptions.logs = [];
};

/**
Expand Down Expand Up @@ -440,7 +462,8 @@ var CaptionParser = function() {
parsedCaptions = {
captions: [],
// CC1, CC2, CC3, CC4
captionStreams: {}
captionStreams: {},
logs: []
};
} else {
this.clearParsedCaptions();
Expand Down
28 changes: 27 additions & 1 deletion lib/mp4/transmuxer.js
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,8 @@ Transmuxer = function(options) {
pipeline.coalesceStream.numberOfTracks++;
pipeline.audioSegmentStream = new AudioSegmentStream(audioTrack, options);

pipeline.audioSegmentStream.on('log', self.getLogTrigger_('audioSegmentStream'));

pipeline.audioSegmentStream.on('timingInfo',
self.trigger.bind(self, 'audioTimingInfo'));

Expand Down Expand Up @@ -1029,6 +1031,7 @@ Transmuxer = function(options) {
if (videoTrack && !pipeline.videoSegmentStream) {
pipeline.coalesceStream.numberOfTracks++;
pipeline.videoSegmentStream = new VideoSegmentStream(videoTrack, options);
pipeline.videoSegmentStream.on('log', self.getLogTrigger_('videoSegmentStream'));

pipeline.videoSegmentStream.on('timelineStartInfo', function(timelineStartInfo) {
// When video emits timelineStartInfo data after a flush, we forward that
Expand Down Expand Up @@ -1069,6 +1072,7 @@ Transmuxer = function(options) {
// hook up the audio segment stream to the first track with aac data
pipeline.coalesceStream.numberOfTracks++;
pipeline.audioSegmentStream = new AudioSegmentStream(audioTrack, options);
pipeline.audioSegmentStream.on('log', self.getLogTrigger_('audioSegmentStream'));

pipeline.audioSegmentStream.on('timingInfo',
self.trigger.bind(self, 'audioTimingInfo'));
Expand Down Expand Up @@ -1099,7 +1103,6 @@ Transmuxer = function(options) {
pipeline.coalesceStream.on('caption', this.trigger.bind(this, 'caption'));
// Let the consumer know we have finished flushing the entire pipeline
pipeline.coalesceStream.on('done', this.trigger.bind(this, 'done'));
pipeline.adtsStream.on('warn', this.trigger.bind(this, 'warn'));
};

// hook up the segment streams once track metadata is delivered
Expand Down Expand Up @@ -1155,6 +1158,14 @@ Transmuxer = function(options) {
}
};

this.getLogTrigger_ = function(key) {
var self = this;

return function(event) {
event.stream = key;
self.trigger('log', event);
};
};
// feed incoming data to the front of the parsing pipeline
this.push = function(data) {
if (hasFlushed) {
Expand All @@ -1165,6 +1176,21 @@ Transmuxer = function(options) {
} else if (!isAac && this.transmuxPipeline_.type !== 'ts') {
this.setupTsPipeline();
}

if (this.transmuxPipeline_) {
var keys = Object.keys(this.transmuxPipeline_);

for (var i = 0; i < keys.length; i++) {
var key = keys[i];

// skip non-stream keys and headOfPipeline
// which is just a duplicate
if (key === 'headOfPipeline' || !this.transmuxPipeline_[key].on) {
continue;
}
this.transmuxPipeline_[key].on('log', this.getLogTrigger_(key));
}
}
hasFlushed = false;
}
this.transmuxPipeline_.headOfPipeline.push(data);
Expand Down
18 changes: 18 additions & 0 deletions test/caption-parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ var dashInit = segments['dash-608-captions-init.mp4']();
// This file includes 2 segments data to force a flush
// of the first caption. The second caption is at 200s
var dashSegment = segments['dash-608-captions-seg.m4s']();
var malformedSei = segments['malformed-sei.m4s']();
var malformedSeiInit = segments['malformed-sei-init.mp4']();

var mp4Helpers = require('./utils/mp4-helpers');
var box = mp4Helpers.box;
Expand Down Expand Up @@ -120,6 +122,22 @@ QUnit.test('parseTrackId for version 0 and version 1 boxes', function(assert) {
'stream is not CC1');
});

QUnit.test('returns log on invalid sei nal parse', function(assert) {
var trackIds;
var timescales;
var result;
var logs = [];

trackIds = probe.videoTrackIds(malformedSeiInit);
timescales = probe.timescale(malformedSeiInit);

result = captionParser.parse(malformedSei, trackIds, timescales);

assert.deepEqual(result.logs, [
{level: 'warn', message: 'We\'ve encountered a nal unit without data at 189975 for trackId 1. See mux.js#223.'}
], 'logged invalid sei nal');
});

// ---------
// Test Data
// ---------
Expand Down
15 changes: 6 additions & 9 deletions test/caption-stream.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2648,14 +2648,13 @@ QUnit.test('mix of all modes (extract from CNN)', function(assert) {

});

QUnit.test('Cea608Stream will log errors, not throw an exception', function(assert) {
QUnit.test('Cea608Stream will trigger log on malformed captions', function(assert) {
var result;
var originalConsole = console.error;
var logs = [];

console.error = function(msg) {
logs.push(msg);
};
cea608Stream.on('log', function(log) {
logs.push(log);
})

// this will force an exception to happen in flushDisplayed
cea608Stream.displayed_[0] = undefined;
Expand All @@ -2674,12 +2673,10 @@ QUnit.test('Cea608Stream will log errors, not throw an exception', function(asse
assert.deepEqual(
logs,
[
'Skipping malformed caption.'
{level: 'warn', message: 'Skipping a malformed 608 caption at index 0.'}
],
'warnings were logged to the console'
'logs were triggered'
);

console.error = originalConsole;
});

var cea708Stream;
Expand Down
33 changes: 33 additions & 0 deletions test/metadata-stream.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,39 @@ QUnit.test('can construct a MetadataStream', function(assert) {
assert.ok(metadataStream, 'does not return null');
});

QUnit.test('triggers log for non-id3/invalid data', function(assert) {
var logs = [];

metadataStream.on('log', function(log) {
logs.push(log);
});

// id3 not long enough
metadataStream.push({type: 'timed-metadata', data: new Uint8Array()});
// invalid data
metadataStream.push({type: 'timed-metadata', data: new Uint8Array([
0x01, 0x01, 0x01, 0x01, 0x01,
0x01, 0x01, 0x01, 0x01, 0x01
])});
const zeroFrames = new Uint8Array(stringToInts('ID3').concat([
0x03, 0x00, // version 3.0 of ID3v2 (aka ID3v.2.3.0)
0x40, // flags. include an extended header
0x00, 0x00, 0x00, 0x00, // size. set later

// extended header
0x00, 0x00, 0x00, 0x06, // extended header size. no CRC
0x00, 0x00, // extended flags
0x00, 0x00, 0x00, 0x02 // size of padding
]));

metadataStream.push({type: 'timed-metadata', data: zeroFrames});

assert.deepEqual(logs, [
{level: 'warn', message: 'Skipping unrecognized metadata packet'},
{level: 'warn', message: 'Skipping unrecognized metadata packet'},
{level: 'warn', message: 'Malformed ID3 frame encountered. Skipping metadata parsing.'}
], 'logs as expected.');
});

QUnit.test('parses simple ID3 metadata out of PES packets', function(assert) {
var
Expand Down
Binary file added test/segments/malformed-sei-init.mp4
Binary file not shown.
Binary file added test/segments/malformed-sei.m4s
Binary file not shown.
Loading

0 comments on commit 4995603

Please sign in to comment.