Skip to content

Commit

Permalink
chore: log transmuxer log events via segment loader (#1155)
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonocasey authored Jul 9, 2021
1 parent b7942ff commit 1e2f7a4
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 22 deletions.
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@
"global": "^4.4.0",
"m3u8-parser": "4.7.0",
"mpd-parser": "0.17.0",
"mux.js": "5.11.3",
"mux.js": "5.12.1",
"video.js": "^6 || ^7"
},
"peerDependencies": {
Expand All @@ -81,7 +81,7 @@
"lodash-compat": "^3.10.0",
"nomnoml": "^0.3.0",
"rollup": "^2.36.1",
"rollup-plugin-worker-factory": "0.5.5",
"rollup-plugin-worker-factory": "0.5.7",
"shelljs": "^0.8.4",
"sinon": "^8.1.1",
"url-toolkit": "^2.2.1",
Expand Down
34 changes: 24 additions & 10 deletions src/media-segment-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,8 @@ const transmuxAndNotify = ({
isEndOfTimeline,
endedTimelineFn,
dataFn,
doneFn
doneFn,
onTransmuxerLog
}) => {
const fmp4Tracks = segment.map && segment.map.tracks || {};
const isMuxed = Boolean(fmp4Tracks.audio && fmp4Tracks.video);
Expand Down Expand Up @@ -358,6 +359,7 @@ const transmuxAndNotify = ({
onEndedTimeline: () => {
endedTimelineFn();
},
onTransmuxerLog,
onDone: (result) => {
if (!doneFn) {
return;
Expand Down Expand Up @@ -415,7 +417,8 @@ const handleSegmentBytes = ({
isEndOfTimeline,
endedTimelineFn,
dataFn,
doneFn
doneFn,
onTransmuxerLog
}) => {
let bytesAsUint8Array = new Uint8Array(bytes);

Expand Down Expand Up @@ -510,6 +513,9 @@ const handleSegmentBytes = ({
// transfer bytes back to us
bytes = message.data.buffer;
segment.bytes = bytesAsUint8Array = message.data;
message.logs.forEach(function(log) {
onTransmuxerLog(videojs.mergeOptions(log, {stream: 'mp4CaptionParser'}));
});
finishLoading(message.captions);
}
});
Expand Down Expand Up @@ -547,7 +553,8 @@ const handleSegmentBytes = ({
isEndOfTimeline,
endedTimelineFn,
dataFn,
doneFn
doneFn,
onTransmuxerLog
});
};

Expand Down Expand Up @@ -623,7 +630,8 @@ const decryptSegment = ({
isEndOfTimeline,
endedTimelineFn,
dataFn,
doneFn
doneFn,
onTransmuxerLog
}) => {
decrypt({
id: segment.requestId,
Expand All @@ -645,7 +653,8 @@ const decryptSegment = ({
isEndOfTimeline,
endedTimelineFn,
dataFn,
doneFn
doneFn,
onTransmuxerLog
});
});
};
Expand Down Expand Up @@ -691,7 +700,8 @@ const waitForCompletion = ({
isEndOfTimeline,
endedTimelineFn,
dataFn,
doneFn
doneFn,
onTransmuxerLog
}) => {
let count = 0;
let didError = false;
Expand Down Expand Up @@ -737,7 +747,8 @@ const waitForCompletion = ({
isEndOfTimeline,
endedTimelineFn,
dataFn,
doneFn
doneFn,
onTransmuxerLog
});
}
// Otherwise, everything is ready just continue
Expand All @@ -753,7 +764,8 @@ const waitForCompletion = ({
isEndOfTimeline,
endedTimelineFn,
dataFn,
doneFn
doneFn,
onTransmuxerLog
});
};

Expand Down Expand Up @@ -942,7 +954,8 @@ export const mediaSegmentRequest = ({
isEndOfTimeline,
endedTimelineFn,
dataFn,
doneFn
doneFn,
onTransmuxerLog
}) => {
const activeXhrs = [];
const finishProcessingFn = waitForCompletion({
Expand All @@ -957,7 +970,8 @@ export const mediaSegmentRequest = ({
isEndOfTimeline,
endedTimelineFn,
dataFn,
doneFn
doneFn,
onTransmuxerLog
});

// optionally, request the decryption key
Expand Down
5 changes: 4 additions & 1 deletion src/segment-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -2427,7 +2427,10 @@ export default class SegmentLoader extends videojs.EventTarget {
id3Fn: this.handleId3_.bind(this),

dataFn: this.handleData_.bind(this),
doneFn: this.segmentRequestFinished_.bind(this)
doneFn: this.segmentRequestFinished_.bind(this),
onTransmuxerLog: ({message, level, stream}) => {
this.logger_(`${segmentInfoString(segmentInfo)} logged from transmuxer stream ${stream} as a ${level}: ${message}`);
}
});
}

Expand Down
4 changes: 4 additions & 0 deletions src/segment-transmuxer.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export const processTransmux = (options) => {
onCaptions,
onDone,
onEndedTimeline,
onTransmuxerLog,
isEndOfTimeline
} = options;
const transmuxedData = {
Expand Down Expand Up @@ -125,6 +126,9 @@ export const processTransmux = (options) => {
waitForEndedTimelineEvent = false;
onEndedTimeline();
}
if (event.data.action === 'log') {
onTransmuxerLog(event.data.log);
}

// wait for the transmuxed event since we may have audio and video
if (event.data.type !== 'transmuxed') {
Expand Down
5 changes: 5 additions & 0 deletions src/transmuxer-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,10 @@ const wireTransmuxerEvents = function(self, transmuxer) {
});
});

transmuxer.on('log', function(log) {
self.postMessage({action: 'log', log});
});

};

/**
Expand Down Expand Up @@ -198,6 +202,7 @@ class MessageHandlers {
this.self.postMessage({
action: 'mp4Captions',
captions: parsed && parsed.captions || [],
logs: parsed && parsed.logs || [],
data: segment.buffer
}, [segment.buffer]);
}
Expand Down
9 changes: 6 additions & 3 deletions test/media-segment-request.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ QUnit.module('Media Segment Request - make it to transmuxer', {
xhr: this.xhr,
xhrOptions: this.xhrOptions,
decryptionWorker: this.mockDecrypter,
segment: {}
segment: {},
onTransmuxerLog: () => {}
};

[
Expand Down Expand Up @@ -1335,7 +1336,8 @@ QUnit.test('non-TS segment will get parsed for captions', function(assert) {
data: {
action: 'mp4Captions',
data: event.data,
captions
captions,
logs: []
}
});
}
Expand Down Expand Up @@ -1475,7 +1477,8 @@ QUnit.test('non-TS segment will get parsed for captions on next segment request
data: {
action: 'mp4Captions',
data: event.data,
captions
captions,
logs: []
}
});
}
Expand Down
49 changes: 49 additions & 0 deletions test/segment-loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -902,6 +902,55 @@ QUnit.module('SegmentLoader', function(hooks) {
});
});

QUnit.test('uses the log event from the transmuxer', function(assert) {
const playlist = playlistWithDuration(10);
const ogPost = loader.transmuxer_.postMessage;
const messages = [];

loader.logger_ = (message) => {
messages.push(message);
};

loader.transmuxer_.postMessage = (message) => {
const retval = ogPost.call(loader.transmuxer_, message);

if (message.action === 'push') {
const log = newEvent('message');

log.data = {action: 'log', log: {message: 'debug foo', stream: 'something', level: 'warn'}};

loader.transmuxer_.dispatchEvent(log);
return;
}

return retval;
};

return setupMediaSource(loader.mediaSource_, loader.sourceUpdater_, {isVideoOnly: true}).then(() => {
return new Promise((resolve, reject) => {
loader.one('appended', resolve);
loader.one('error', reject);

loader.playlist(playlist);
loader.load();

this.clock.tick(100);
// segment
standardXHRResponse(this.requests.shift(), videoOneSecondSegment());
});
}).then(() => {
let messageFound = false;

messages.forEach(function(message) {
if ((/debug foo/).test(message) && (/warn/).test(message) && (/something/).test(message)) {
messageFound = true;
}
});

assert.ok(messageFound, 'message was logged');
});
});

QUnit.test('segmentKey will cache new encrypted keys with cacheEncryptionKeys true', function(assert) {
loader.cacheEncryptionKeys_ = true;

Expand Down
1 change: 1 addition & 0 deletions test/transmuxer-worker.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,7 @@ QUnit.test('can parse mp4 captions', function(assert) {

assert.equal(message.action, 'mp4Captions', 'returned mp4Captions event');
assert.deepEqual(message.captions.length, 2, 'two captions');
assert.deepEqual(message.logs.length, 0, 'no logs returned');
assert.deepEqual(
new Uint8Array(message.data),
data,
Expand Down

0 comments on commit 1e2f7a4

Please sign in to comment.