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: Use ManagedMediaSource when available #5683

Merged
merged 7 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
78 changes: 78 additions & 0 deletions externs/managedmediasource.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*! @license
* Shaka Player
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/

/**
* @fileoverview Externs for ManagedMediaSource which were missing in the
* Closure compiler.
*
* @externs
*/

/**
* @constructor
* @implements {EventTarget}
avelad marked this conversation as resolved.
Show resolved Hide resolved
*/
function ManagedMediaSource() {}

/** @override */
ManagedMediaSource.prototype.addEventListener =
function(type, listener, optPptions) {};

/** @override */
ManagedMediaSource.prototype.removeEventListener =
function(type, listener, optPptions) {};

/** @override */
ManagedMediaSource.prototype.dispatchEvent = function(evt) {};

/** @type {Array<SourceBuffer>} */
ManagedMediaSource.prototype.sourceBuffers;

/** @type {Array<SourceBuffer>} */
ManagedMediaSource.prototype.activeSourceBuffers;

/** @type {number} */
ManagedMediaSource.prototype.duration;

/**
* @param {string} type
* @return {SourceBuffer}
*/
ManagedMediaSource.prototype.addSourceBuffer = function(type) {};

/**
* @param {SourceBuffer} sourceBuffer
* @return {undefined}
*/
ManagedMediaSource.prototype.removeSourceBuffer = function(sourceBuffer) {};

/**
* Updates the live seekable range.
* @param {number} start
* @param {number} end
*/
ManagedMediaSource.prototype.setLiveSeekableRange = function(start, end) {};

/**
* Clears the live seekable range.
* @return {void}
*/
ManagedMediaSource.prototype.clearLiveSeekableRange = function() {};

/** @type {string} */
ManagedMediaSource.prototype.readyState;

/**
* @param {string=} optError
* @return {undefined}
*/
ManagedMediaSource.prototype.endOfStream = function(optError) {};

/**
* @param {string} type
* @return {boolean}
*/
ManagedMediaSource.isTypeSupported = function(type) {};
13 changes: 10 additions & 3 deletions lib/media/media_source_capabilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,16 @@ shaka.media.Capabilities = class {
if (supportMap.has(type)) {
return supportMap.get(type);
}
const currentSupport = MediaSource.isTypeSupported(type);
supportMap.set(type, currentSupport);
return currentSupport;
if (window.ManagedMediaSource) {
const currentSupport = ManagedMediaSource.isTypeSupported(type);
supportMap.set(type, currentSupport);
return currentSupport;
} else if (window.MediaSource) {
const currentSupport = MediaSource.isTypeSupported(type);
supportMap.set(type, currentSupport);
return currentSupport;
}
return false;
}

/**
Expand Down
44 changes: 32 additions & 12 deletions lib/media/media_source_engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ shaka.media.MediaSourceEngine = class {
/** @private {string} */
this.url_ = '';

/** @private {MediaSource} */
/** @private {(MediaSource|ManagedMediaSource)} */
this.mediaSource_ = this.createMediaSource(this.mediaSourceOpen_);

/** @private {boolean} */
Expand Down Expand Up @@ -150,25 +150,43 @@ shaka.media.MediaSourceEngine = class {
* Replaced by unit tests.
*
* @param {!shaka.util.PublicPromise} p
* @return {!MediaSource}
* @return {!(MediaSource|ManagedMediaSource)}
*/
createMediaSource(p) {
const mediaSource = new MediaSource();
if (window.ManagedMediaSource) {
this.video_.disableRemotePlayback = true;

// Set up MediaSource on the video element.
this.eventManager_.listenOnce(
mediaSource, 'sourceopen', () => this.onSourceOpen_(p));
const mediaSource = new ManagedMediaSource();

// Store the object URL for releasing it later.
this.url_ = shaka.media.MediaSourceEngine.createObjectURL(mediaSource);
this.eventManager_.listenOnce(
mediaSource, 'startstreaming', () => p.resolve());

this.video_.src = this.url_;
this.eventManager_.listenOnce(
mediaSource, 'sourceopen', () => this.onSourceOpen_());

return mediaSource;
this.url_ = shaka.media.MediaSourceEngine.createObjectURL(mediaSource);

this.video_.src = this.url_;

return mediaSource;
} else {
const mediaSource = new MediaSource();

// Set up MediaSource on the video element.
this.eventManager_.listenOnce(
mediaSource, 'sourceopen', () => this.onSourceOpen_(p));

// Store the object URL for releasing it later.
this.url_ = shaka.media.MediaSourceEngine.createObjectURL(mediaSource);

this.video_.src = this.url_;

return mediaSource;
}
}

/**
* @param {!shaka.util.PublicPromise} p
* @param {shaka.util.PublicPromise=} p
* @private
*/
onSourceOpen_(p) {
Expand All @@ -182,7 +200,9 @@ shaka.media.MediaSourceEngine = class {
// strong reference to the MediaSource object, and allow it to be
// garbage-collected later.
URL.revokeObjectURL(this.url_);
p.resolve();
if (p) {
p.resolve();
}
}

/**
Expand Down
5 changes: 3 additions & 2 deletions lib/util/platform.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,16 @@ shaka.util.Platform = class {
* @return {boolean}
*/
static supportsMediaSource() {
const mediaSource = window.ManagedMediaSource || window.MediaSource;
// Browsers that lack a media source implementation will have no reference
// to |window.MediaSource|. Platforms that we see having problematic media
// source implementations will have this reference removed via a polyfill.
if (!window.MediaSource) {
if (!mediaSource) {
return false;
}

// Some very old MediaSource implementations didn't have isTypeSupported.
if (!MediaSource.isTypeSupported) {
if (!mediaSource.isTypeSupported) {
return false;
}

Expand Down