Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Keep media update timeout alive so live playlists can recover #1176

Merged
merged 8 commits into from
Aug 17, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/master-playlist-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const shouldSwitchToMedia = function({
// This is because in LIVE, the player plays 3 segments from the end of the
// playlist, and if `BUFFER_LOW_WATER_LINE` is greater than the duration availble
// in those segments, a viewer will never experience a rendition upswitch.
if (!currentPlaylist || !currentPlaylist.endList) {
if (!currentPlaylist) {
log(`${sharedLogLine} as current playlist ` + (!currentPlaylist ? 'is not set' : 'is live'));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per the comment above, will this mean that we should have a way of ignoring the low water for live in the rest of this function?

And we may need to update this line and the comment above

return true;
}
Expand Down
46 changes: 35 additions & 11 deletions src/playlist-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -505,13 +505,7 @@ export default class PlaylistLoader extends EventTarget {
this.trigger('playlistunchanged');
}

// refresh live playlists after a target duration passes
if (!this.media().endList) {
window.clearTimeout(this.mediaUpdateTimeout);
this.mediaUpdateTimeout = window.setTimeout(() => {
this.trigger('mediaupdatetimeout');
}, refreshDelay(this.media(), !!update));
}
this.updateMediaUpdateTimeout_(refreshDelay(this.media(), !!update));

this.trigger('loadedplaylist');
}
Expand Down Expand Up @@ -619,6 +613,8 @@ export default class PlaylistLoader extends EventTarget {
return;
}

this.updateMediaUpdateTimeout_(refreshDelay(playlist, true));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

May be worth a comment above here saying why we update the timeout even if it's switching to the active playlist

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added


// switching to the active playlist is a no-op
if (!mediaChange) {
return;
Expand Down Expand Up @@ -679,8 +675,12 @@ export default class PlaylistLoader extends EventTarget {
* pause loading of the playlist
*/
pause() {
if (this.mediaUpdateTimeout) {
window.clearTimeout(this.mediaUpdateTimeout);
this.mediaUpdateTimeout = null;
}

this.stopRequest();
window.clearTimeout(this.mediaUpdateTimeout);
if (this.state === 'HAVE_NOTHING') {
// If we pause the loader before any data has been retrieved, its as if we never
// started, so reset to an unstarted state.
Expand All @@ -705,14 +705,20 @@ export default class PlaylistLoader extends EventTarget {
* start loading of the playlist
*/
load(shouldDelay) {
window.clearTimeout(this.mediaUpdateTimeout);

if (this.mediaUpdateTimeout) {
window.clearTimeout(this.mediaUpdateTimeout);
this.mediaUpdateTimeout = null;
}
const media = this.media();

if (shouldDelay) {
const delay = media ? ((media.partTargetDuration || media.targetDuration) / 2) * 1000 : 5 * 1000;

this.mediaUpdateTimeout = window.setTimeout(() => this.load(), delay);
this.mediaUpdateTimeout = window.setTimeout(() => {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

imo this should be called something else, but I think we should save it for the playlist-loader base class refactor as dash-playlist-loader does the same thing.

this.mediaUpdateTimeout = null;
this.load();
gkatsev marked this conversation as resolved.
Show resolved Hide resolved
}, delay);

return;
}

Expand All @@ -728,6 +734,24 @@ export default class PlaylistLoader extends EventTarget {
}
}

updateMediaUpdateTimeout_(delay) {
if (this.mediaUpdateTimeout) {
window.clearTimeout(this.mediaUpdateTimeout);
this.mediaUpdateTimeout = null;
}

// we only have use mediaupdatetimeout for live playlists.
if (!this.media() || this.media().endList) {
return;
}

this.mediaUpdateTimeout = window.setTimeout(() => {
this.mediaUpdateTimeout = null;
this.trigger('mediaupdatetimeout');
this.updateMediaUpdateTimeout_(delay);
}, delay);
}

/**
* start loading of the playlist
*/
Expand Down
39 changes: 0 additions & 39 deletions test/master-playlist-controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2033,36 +2033,6 @@ QUnit.test(

mediaChanges.length = 0;

endList = false;
currentTime = 100;
currentPlaylistBandwidth = 1000;
nextPlaylistBandwidth = 1001;
buffered = [];
this.masterPlaylistController.mainSegmentLoader_.trigger('bandwidthupdate');
assert.equal(
mediaChanges.length,
1,
'changes live media when no buffer and higher bandwidth playlist'
);
buffered = [[0, 100], [100, 109]];
this.masterPlaylistController.mainSegmentLoader_.trigger('bandwidthupdate');
assert.equal(
mediaChanges.length,
2,
'changes live media when insufficient forward buffer and higher ' +
'bandwidth playlist'
);
buffered = [[0, 100], [100, 130]];
this.masterPlaylistController.mainSegmentLoader_.trigger('bandwidthupdate');
assert.equal(
mediaChanges.length,
3,
'changes live media when sufficient forward buffer and higher ' +
'bandwidth playlist'
);

mediaChanges.length = 0;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to remove these tests?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These only tested that live content always switches as far as I could tell. We might have to add one more back in depending on changes that I have to make in shouldSwitchToMedia.

endList = true;
currentTime = 9;
duration = 18;
Expand Down Expand Up @@ -5884,15 +5854,6 @@ QUnit.test('true if a no current playlist', function(assert) {
assert.ok(mpc.shouldSwitchToMedia_(nextPlaylist), 'should switch without currentPlaylist');
});

QUnit.test('true if current playlist is live', function(assert) {
const mpc = this.masterPlaylistController;

mpc.masterPlaylistLoader_.media = () => ({endList: false, id: 'bar'});
const nextPlaylist = {id: 'foo', endList: true};

assert.ok(mpc.shouldSwitchToMedia_(nextPlaylist), 'should switch with live currentPlaylist');
});

QUnit.test('true if duration < 30', function(assert) {
const mpc = this.masterPlaylistController;
const nextPlaylist = {id: 'foo', endList: true};
Expand Down
56 changes: 56 additions & 0 deletions test/playlist-loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2101,6 +2101,62 @@ QUnit.module('Playlist Loader', function(hooks) {
);
});

QUnit.test('mediaupdatetimeout works as expeted for live playlists', function(assert) {
const loader = new PlaylistLoader('master.m3u8', this.fakeVhs);
let media =
'#EXTM3U\n' +
'#EXT-X-MEDIA-SEQUENCE:0\n' +
'#EXTINF:5,\n' +
'low-0.ts\n' +
'#EXTINF:5,\n' +
'low-1.ts\n';

loader.load();

this.requests.shift().respond(
200, null,
'#EXTM3U\n' +
'#EXT-X-STREAM-INF:BANDWIDTH=1\n' +
'media.m3u8\n' +
'#EXT-X-STREAM-INF:BANDWIDTH=1\n' +
'media2.m3u8\n'
);

this.requests.shift().respond(200, null, media);

assert.ok(loader.mediaUpdateTimeout, 'has an initial media update timeout');

this.clock.tick(5000);

media += '#EXTINF:5\nlow-2.ts\n';

this.requests.shift().respond(200, null, media);

assert.ok(loader.mediaUpdateTimeout, 'media update timeout created another');

loader.pause();
assert.notOk(loader.mediaUpdateTimeout, 'media update timeout cleared');

loader.media(loader.master.playlists[0]);

assert.ok(loader.mediaUpdateTimeout, 'media update timeout created again');
assert.equal(this.requests.length, 0, 'no request');

loader.media(loader.master.playlists[1]);

assert.ok(loader.mediaUpdateTimeout, 'media update timeout created');
assert.equal(this.requests.length, 1, 'playlist requested');

this.requests.shift().respond(500, null, 'fail');

assert.ok(loader.mediaUpdateTimeout, 'media update timeout exists after request failure');

this.clock.tick(5000);

assert.ok(loader.mediaUpdateTimeout, 'media update timeout created again');
assert.equal(this.requests.length, 1, 'playlist re-requested');
});

QUnit.module('llhls', {
beforeEach() {
this.fakeVhs.options_ = {experimentalLLHLS: true};
Expand Down