Skip to content

Commit

Permalink
Have Player Use Destroyer
Browse files Browse the repository at this point in the history
Changed the player to use shaka.util.Destroyer so that player
can be protected against multiple calls to destroy.

Change-Id: Ib255e465966e700eb8b8418281657ecb8b852eba
  • Loading branch information
vaage committed Jul 19, 2018
1 parent 40c3f39 commit 3dbe49b
Showing 1 changed file with 41 additions and 25 deletions.
66 changes: 41 additions & 25 deletions lib/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,6 @@ goog.require('shaka.util.StreamUtils');
shaka.Player = function(video, dependencyInjector) {
shaka.util.FakeEventTarget.call(this);

/** @private {boolean} */
this.destroyed_ = false;

/** @private {HTMLMediaElement} */
this.video_ = null;

Expand Down Expand Up @@ -184,7 +181,25 @@ shaka.Player = function(video, dependencyInjector) {
if (video) {
this.attach(video, true /* initializeMediaSource */);
}

/** @private {!shaka.util.Destroyer} */
this.destroyer_ = new shaka.util.Destroyer(async () => {
// Then, destroy other components and clear fields.
let p = Promise.all([
this.eventManager_ ? this.eventManager_.destroy() : null,
this.networkingEngine_ ? this.networkingEngine_.destroy() : null,
]);

this.textVisibility_ = false;
this.eventManager_ = null;
this.abrManager_ = null;
this.networkingEngine_ = null;
this.config_ = null;

await p;
});
};

goog.inherits(shaka.Player, shaka.util.FakeEventTarget);


Expand Down Expand Up @@ -219,23 +234,12 @@ shaka.Player.prototype.cancelLoad_ = function() {
*/
shaka.Player.prototype.destroy = async function() {
// First, detach from the media element. This implies unloading content
// and canceling pending loads.
// and canceling pending loads. This must be called before the destroyer
// as it will indirectly check if the player has already been destroyed and
// won't execute as expected. Calling detach multiple times is safe, so it
// is okay to be outside the protection of the destroyer.
await this.detach();
// Then, destroy other components and clear fields.

this.destroyed_ = true;

let p = Promise.all([
this.eventManager_ ? this.eventManager_.destroy() : null,
this.networkingEngine_ ? this.networkingEngine_.destroy() : null,
]);

this.textVisibility_ = false;
this.eventManager_ = null;
this.abrManager_ = null;
this.networkingEngine_ = null;
this.config_ = null;
await p;
await this.destroyer_.destroy();
};


Expand Down Expand Up @@ -1344,7 +1348,7 @@ shaka.Player.prototype.isBuffering = function() {
* @export
*/
shaka.Player.prototype.unload = async function(reinitializeMediaSource) {
if (this.destroyed_) {
if (this.destroyer_.destroyed()) {
return;
}

Expand Down Expand Up @@ -2021,7 +2025,9 @@ shaka.Player.prototype.addTextTrack = function(
// Don't create the media state until subtitles are actually enabled
return this.streamingEngine_.loadNewTextStream(
stream, this.textVisibility_).then(function() {
if (this.destroyed_) return;
if (this.destroyer_.destroyed()) {
return;
}

let curPeriodIdx = this.manifest_.periods.indexOf(period);
let activeText = this.streamingEngine_.getActiveText();
Expand Down Expand Up @@ -2727,7 +2733,9 @@ shaka.Player.prototype.onChangePeriod_ = function() {
* @private
*/
shaka.Player.prototype.updateState_ = function() {
if (this.destroyed_) return;
if (this.destroyer_.destroyed()) {
return;
}

let newState;
if (this.buffering_) {
Expand Down Expand Up @@ -3033,7 +3041,10 @@ shaka.Player.prototype.onAdaptation_ = function() {
// This gives StreamingEngine time to absorb the changes before the user
// tries to query them.
Promise.resolve().then(function() {
if (this.destroyed_) return;
if (this.destroyer_.destroyed()) {
return;
}

let event = new shaka.util.FakeEvent('adaptation');
this.dispatchEvent(event);
}.bind(this));
Expand All @@ -3049,7 +3060,10 @@ shaka.Player.prototype.onTracksChanged_ = function() {
// This gives StreamingEngine time to absorb the changes before the user
// tries to query them.
Promise.resolve().then(function() {
if (this.destroyed_) return;
if (this.destroyer_.destroyed()) {
return;
}

let event = new shaka.util.FakeEvent('trackschanged');
this.dispatchEvent(event);
}.bind(this));
Expand All @@ -3069,7 +3083,9 @@ shaka.Player.prototype.onTextTrackVisibility_ = function() {
*/
shaka.Player.prototype.onError_ = function(error) {
// Errors dispatched after destroy is called are irrelevant.
if (this.destroyed_) return;
if (this.destroyer_.destroyed()) {
return;
}

goog.asserts.assert(error instanceof shaka.util.Error, 'Wrong error type!');

Expand Down

0 comments on commit 3dbe49b

Please sign in to comment.