Skip to content

Commit ae1ae70

Browse files
authored
feat: Add support for ManagedMediaSource 'startstreaming' and 'endstream' event handling (#1542)
1 parent bfc17b4 commit ae1ae70

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

src/playlist-controller.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ export class PlaylistController extends videojs.EventTarget {
215215
// Airplay source not yet implemented. Remote playback must be disabled.
216216
this.tech_.el_.disableRemotePlayback = true;
217217
this.mediaSource = new window.ManagedMediaSource();
218+
218219
videojs.log('Using ManagedMediaSource');
219220
} else if (window.MediaSource) {
220221
this.mediaSource = new window.MediaSource();
@@ -223,12 +224,16 @@ export class PlaylistController extends videojs.EventTarget {
223224
this.handleDurationChange_ = this.handleDurationChange_.bind(this);
224225
this.handleSourceOpen_ = this.handleSourceOpen_.bind(this);
225226
this.handleSourceEnded_ = this.handleSourceEnded_.bind(this);
227+
this.load = this.load.bind(this);
228+
this.pause = this.pause.bind(this);
226229

227230
this.mediaSource.addEventListener('durationchange', this.handleDurationChange_);
228231

229232
// load the media source into the player
230233
this.mediaSource.addEventListener('sourceopen', this.handleSourceOpen_);
231234
this.mediaSource.addEventListener('sourceended', this.handleSourceEnded_);
235+
this.mediaSource.addEventListener('startstreaming', this.load);
236+
this.mediaSource.addEventListener('endstreaming', this.pause);
232237
// we don't have to handle sourceclose since dispose will handle termination of
233238
// everything, and the MediaSource should not be detached without a proper disposal
234239

@@ -1056,14 +1061,31 @@ export class PlaylistController extends videojs.EventTarget {
10561061
*/
10571062
load() {
10581063
this.mainSegmentLoader_.load();
1064+
10591065
if (this.mediaTypes_.AUDIO.activePlaylistLoader) {
10601066
this.audioSegmentLoader_.load();
10611067
}
1068+
10621069
if (this.mediaTypes_.SUBTITLES.activePlaylistLoader) {
10631070
this.subtitleSegmentLoader_.load();
10641071
}
10651072
}
10661073

1074+
/**
1075+
* Call pause on our SegmentLoaders
1076+
*/
1077+
pause() {
1078+
this.mainSegmentLoader_.pause();
1079+
1080+
if (this.mediaTypes_.AUDIO.activePlaylistLoader) {
1081+
this.audioSegmentLoader_.pause();
1082+
}
1083+
1084+
if (this.mediaTypes_.SUBTITLES.activePlaylistLoader) {
1085+
this.subtitleSegmentLoader_.pause();
1086+
}
1087+
}
1088+
10671089
/**
10681090
* Re-tune playback quality level for the current player
10691091
* conditions. This method will perform destructive actions like removing

test/playlist-controller.test.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7693,3 +7693,39 @@ QUnit.test('uses ManagedMediaSource only when opted in', function(assert) {
76937693
mmsSpy.restore();
76947694
mms.restore();
76957695
});
7696+
7697+
QUnit.test('ManagedMediaSource startstreaming and endstreaming events start and pause segment loading respectively', function(assert) {
7698+
const mms = useFakeManagedMediaSource();
7699+
const options = {
7700+
src: 'test.m3u8',
7701+
tech: this.player.tech_,
7702+
player_: this.player,
7703+
experimentalUseMMS: true
7704+
};
7705+
7706+
const controller = new PlaylistController(options);
7707+
7708+
controller.mediaTypes_.AUDIO.activePlaylistLoader = {};
7709+
controller.mediaTypes_.SUBTITLES.activePlaylistLoader = {};
7710+
7711+
const mainLoadSpy = sinon.spy(controller.mainSegmentLoader_, 'load');
7712+
const audioLoadSpy = sinon.spy(controller.audioSegmentLoader_, 'load');
7713+
const subtitleLoadSpy = sinon.spy(controller.subtitleSegmentLoader_, 'load');
7714+
const mainPauseSpy = sinon.spy(controller.mainSegmentLoader_, 'pause');
7715+
const audioPauseSpy = sinon.spy(controller.audioSegmentLoader_, 'pause');
7716+
const subtitlePauseSpy = sinon.spy(controller.subtitleSegmentLoader_, 'pause');
7717+
7718+
controller.mediaSource.trigger('startstreaming');
7719+
7720+
assert.ok(mainLoadSpy.calledOnce, 'Segment loading started on startstreaming event');
7721+
assert.ok(audioLoadSpy.calledOnce, 'Audio segment loading started on startstreaming event');
7722+
assert.ok(subtitleLoadSpy.calledOnce, 'Subtitle segment loading started on startstreaming event');
7723+
7724+
controller.mediaSource.trigger('endstreaming');
7725+
7726+
assert.ok(mainPauseSpy.calledOnce, 'Main segment loading paused on endstreaming event');
7727+
assert.ok(audioPauseSpy.calledOnce, 'Audio segment loading paused on endstreaming event');
7728+
assert.ok(subtitlePauseSpy.calledOnce, 'Subtitle segment loading paused on endstreaming event');
7729+
7730+
mms.restore();
7731+
});

0 commit comments

Comments
 (0)