Skip to content

Commit

Permalink
feat: usage and logging on rendition change with reasons (#1088)
Browse files Browse the repository at this point in the history
  • Loading branch information
brandonocasey authored Mar 30, 2021
1 parent de0b55b commit 1b990f1
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
24 changes: 18 additions & 6 deletions src/master-playlist-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,22 @@ export class MasterPlaylistController extends videojs.EventTarget {
const nextPlaylist = this.selectPlaylist();

if (this.shouldSwitchToMedia_(nextPlaylist)) {
this.masterPlaylistLoader_.media(nextPlaylist);
this.switchMedia_(nextPlaylist, 'abr');
}
}

switchMedia_(playlist, cause, delay) {
const oldMedia = this.media();
const oldId = oldMedia && (oldMedia.id || oldMedia.uri);
const newId = playlist.id || playlist.uri;

if (oldId && oldId !== newId) {
this.logger_(`switch media ${oldId} -> ${newId} from ${cause}`);
this.tech_.trigger({type: 'usage', name: `vhs-rendition-change-${cause}`});
}
this.masterPlaylistLoader_.media(playlist, delay);
}

/**
* Start a timer that periodically calls checkABR_
*
Expand Down Expand Up @@ -416,7 +428,7 @@ export class MasterPlaylistController extends videojs.EventTarget {

this.initialMedia_ = selectedMedia;

this.masterPlaylistLoader_.media(this.initialMedia_);
this.switchMedia_(this.initialMedia_, 'initial');

// Under the standard case where a source URL is provided, loadedplaylist will
// fire again since the playlist will be requested. In the case of vhs-json
Expand Down Expand Up @@ -617,7 +629,7 @@ export class MasterPlaylistController extends videojs.EventTarget {
const nextPlaylist = this.selectPlaylist();

if (this.shouldSwitchToMedia_(nextPlaylist)) {
this.masterPlaylistLoader_.media(nextPlaylist);
this.switchMedia_(nextPlaylist, 'bandwidthupdate');
}

this.tech_.trigger('bandwidthupdate');
Expand Down Expand Up @@ -743,7 +755,7 @@ export class MasterPlaylistController extends videojs.EventTarget {
return;
}

this.masterPlaylistLoader_.media(media);
this.switchMedia_(media, 'smooth-quality');

this.mainSegmentLoader_.resetLoader();
// don't need to reset audio as it is reset when media changes
Expand All @@ -763,7 +775,7 @@ export class MasterPlaylistController extends videojs.EventTarget {
return;
}

this.masterPlaylistLoader_.media(media);
this.switchMedia_(media, 'fast-quality');

// Delete all buffered data to allow an immediate quality switch, then seek to give
// the browser a kick to remove any cached frames from the previous rendtion (.04 seconds
Expand Down Expand Up @@ -1118,7 +1130,7 @@ export class MasterPlaylistController extends videojs.EventTarget {
(Date.now() - nextPlaylist.lastRequest) <= delayDuration;

// delay if it's a final rendition or if the last refresh is sooner than half targetDuration
return this.masterPlaylistLoader_.media(nextPlaylist, isFinalRendition || shouldDelay);
return this.switchMedia_(nextPlaylist, 'exclude', isFinalRendition || shouldDelay);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion test/master-playlist-controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1948,7 +1948,7 @@ QUnit.test('does not get stuck in a loop due to inconsistent network/caching', f
const origMedia = playlistLoader.media.bind(playlistLoader);
const mediaChanges = [];

mpc.masterPlaylistLoader_.media = (media) => {
mpc.switchMedia_ = (media) => {
if (media) {
mediaChanges.push(media);
}
Expand Down
8 changes: 6 additions & 2 deletions test/playback-watcher.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1205,6 +1205,7 @@ loaderTypes.forEach(function(type) {

expectedUsage['vhs-rendition-blacklisted'] = 1;
expectedUsage['hls-rendition-blacklisted'] = 1;
// expectedUsage['vhs-rendition-change-exclude'] = 1;

assert.deepEqual(this.usageEvents, expectedUsage, 'usage as expected');

Expand All @@ -1224,7 +1225,7 @@ loaderTypes.forEach(function(type) {

const loader = this.mpc[`${type}SegmentLoader_`];
const playlists = this.mpc.master().playlists;
const excludeAndVerify = () => {
const excludeAndVerify = (last) => {
let oldPlaylist;
// this test only needs 9 appends, since we do an intial append

Expand All @@ -1243,6 +1244,9 @@ loaderTypes.forEach(function(type) {
expectedUsage[`vhs-${type}-download-exclusion`] = 1;
expectedUsage['vhs-rendition-blacklisted'] = 1;
expectedUsage['hls-rendition-blacklisted'] = 1;
if (!last) {
expectedUsage['vhs-rendition-change-exclude'] = 1;
}

assert.deepEqual(this.usageEvents, expectedUsage, 'usage as expected');
this.usageEvents = {};
Expand Down Expand Up @@ -1283,7 +1287,7 @@ loaderTypes.forEach(function(type) {

// exclude all playlists and verify
while (i--) {
excludeAndVerify();
excludeAndVerify((i === 0));
}

});
Expand Down
8 changes: 6 additions & 2 deletions test/videojs-http-streaming.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1940,7 +1940,9 @@ QUnit.test('blacklists fmp4 playlists by browser support', function(assert) {
assert.strictEqual(typeof playlists[2].excludeUntil, 'undefined', 'did not blacklist second playlist');
assert.deepEqual(debugLogs, [
`Internal problem encountered with playlist ${playlists[0].id}. browser does not support codec(s): "hvc1". Switching to playlist ${playlists[1].id}.`,
`Internal problem encountered with playlist ${playlists[1].id}. browser does not support codec(s): "ac-3". Switching to playlist ${playlists[2].id}.`
`switch media ${playlists[0].id} -> ${playlists[1].id} from exclude`,
`Internal problem encountered with playlist ${playlists[1].id}. browser does not support codec(s): "ac-3". Switching to playlist ${playlists[2].id}.`,
`switch media ${playlists[1].id} -> ${playlists[2].id} from exclude`
], 'debug log as expected');

window.MediaSource.isTypeSupported = oldIsTypeSupported;
Expand Down Expand Up @@ -2011,7 +2013,9 @@ QUnit.test('blacklists ts playlists by muxer support', function(assert) {
assert.strictEqual(typeof playlists[2].excludeUntil, 'undefined', 'did not blacklist third playlist');
assert.deepEqual(debugLogs, [
`Internal problem encountered with playlist ${playlists[0].id}. muxer does not support codec(s): "hvc1". Switching to playlist ${playlists[1].id}.`,
`Internal problem encountered with playlist ${playlists[1].id}. muxer does not support codec(s): "ac-3". Switching to playlist ${playlists[2].id}.`
`switch media ${playlists[0].id} -> ${playlists[1].id} from exclude`,
`Internal problem encountered with playlist ${playlists[1].id}. muxer does not support codec(s): "ac-3". Switching to playlist ${playlists[2].id}.`,
`switch media ${playlists[1].id} -> ${playlists[2].id} from exclude`
], 'debug log as expected');

window.MediaSource.isTypeSupported = oldIsTypeSupported;
Expand Down

0 comments on commit 1b990f1

Please sign in to comment.