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

feat: Add support for ManagedMediaSource 'startstreaming' and 'endstream' event handling #1542

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
48 changes: 48 additions & 0 deletions src/playlist-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@
this.playlistExclusionDuration = playlistExclusionDuration;
this.maxPlaylistRetries = maxPlaylistRetries;
this.enableLowInitialPlaylist = enableLowInitialPlaylist;
this.hasManagedMediaSource_ = false;
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: To save a few lines of code, you could change this to:

this.hasManagedMediaSource_ = experimentalUseMMS && window.ManagedMediaSource;

Copy link
Contributor Author

@alex-barstow alex-barstow Oct 3, 2024

Choose a reason for hiding this comment

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

The intent of the flag was to signal that the ManagedMediaSource has been created and currently exists. If we set it here we should rename the flag to useManagedMediaSource_ or createManagedMediaSource_, as it hasn't been created yet.


if (this.useCueTags_) {
this.cueTagsTrack_ = this.tech_.addTextTrack(
Expand All @@ -215,6 +216,8 @@
// Airplay source not yet implemented. Remote playback must be disabled.
Copy link
Contributor

Choose a reason for hiding this comment

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

You can change the above if statement to:

if (this.hasManagedMediaSource_) {

this.tech_.el_.disableRemotePlayback = true;
this.mediaSource = new window.ManagedMediaSource();
this.hasManagedMediaSource_ = true;

videojs.log('Using ManagedMediaSource');
} else if (window.MediaSource) {
this.mediaSource = new window.MediaSource();
Expand All @@ -232,6 +235,14 @@
// we don't have to handle sourceclose since dispose will handle termination of
// everything, and the MediaSource should not be detached without a proper disposal

if (this.hasManagedMediaSource_) {
this.handleStartStreaming_ = this.handleStartStreaming_.bind(this);
Copy link
Contributor

Choose a reason for hiding this comment

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

You can add the content of this if statement to the above pre-existing if statement. All nits, but saves a few lines

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You mean to the if (experimentalUseMMS && window.ManagedMediaSource) {...} block? It is more concise but I opted against it because to my eye it made the code slightly less organized by putting the new addEventListener calls inside the block where the MediaSource is set rather than in the subsequent step/section where the other MediaSource event handlers are added. Minor issue, I'm happy to change it

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, all my comments were basically to save some lines of code by moving this code to where you set the hasManagedMediaSource flag and making the flag unnecessary, but fine with me if it keeps it cleaner in your eyes. 👍

this.handleEndStreaming_ = this.handleEndStreaming_.bind(this);

this.mediaSource.addEventListener('startstreaming', this.handleStartStreaming_);
this.mediaSource.addEventListener('endstreaming', this.handleEndStreaming_);
}

this.seekable_ = createTimeRanges();
this.hasPlayed_ = false;

Expand Down Expand Up @@ -1056,14 +1067,31 @@
*/
load() {
this.mainSegmentLoader_.load();

if (this.mediaTypes_.AUDIO.activePlaylistLoader) {
this.audioSegmentLoader_.load();
}

if (this.mediaTypes_.SUBTITLES.activePlaylistLoader) {
this.subtitleSegmentLoader_.load();
}
}

/**
* Call pause on our SegmentLoaders
*/
pause() {
this.mainSegmentLoader_.pause();

if (this.mediaTypes_.AUDIO.activePlaylistLoader) {
this.audioSegmentLoader_.pause();

Check warning on line 1087 in src/playlist-controller.js

View check run for this annotation

Codecov / codecov/patch

src/playlist-controller.js#L1087

Added line #L1087 was not covered by tests
}

if (this.mediaTypes_.SUBTITLES.activePlaylistLoader) {
this.subtitleSegmentLoader_.pause();

Check warning on line 1091 in src/playlist-controller.js

View check run for this annotation

Codecov / codecov/patch

src/playlist-controller.js#L1091

Added line #L1091 was not covered by tests
}
}

/**
* Re-tune playback quality level for the current player
* conditions. This method will perform destructive actions like removing
Expand Down Expand Up @@ -1223,6 +1251,26 @@
Number.MAX_VALUE : duration;
}

/**
* Handle the startstreaming event on the ManagedMediaSource.
* This event indicates that segment loading should be started/resumed.
*
* @private
*/
handleStartStreaming_() {
this.load();
}

/**
* Handle the endstreaming event on the ManagedMediaSource.
* This event indicates that segment loading should be paused.
*
* @private
*/
handleEndStreaming_() {
this.pause();
}

/**
* handle the durationchange event on the MediaSource
*
Expand Down
27 changes: 27 additions & 0 deletions test/playlist-controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7693,3 +7693,30 @@ QUnit.test('uses ManagedMediaSource only when opted in', function(assert) {
mmsSpy.restore();
mms.restore();
});

QUnit.test('ManagedMediaSource startstreaming and endstreaming events start and pause segment loading respectively', function(assert) {
const mms = useFakeManagedMediaSource();
const options = {
src: 'test.m3u8',
tech: this.player.tech_,
player_: this.player,
experimentalUseMMS: true
};

const controller = new PlaylistController(options);
const loadSpy = sinon.spy(controller, 'load');
const pauseSpy = sinon.spy(controller, 'pause');

assert.ok(loadSpy.notCalled, 'Segment loading not started yet');
assert.ok(pauseSpy.notCalled, 'Segment loading has not been paused');

controller.mediaSource.trigger('startstreaming');

assert.ok(loadSpy.calledOnce, 'Segment loading started on startstreaming event');

controller.mediaSource.trigger('endstreaming');

assert.ok(pauseSpy.calledOnce, 'Segment loading paused on endstreaming event');

mms.restore();
});
Loading