Skip to content

Commit

Permalink
Fix unload/load bug in Player.
Browse files Browse the repository at this point in the history
Based on PR #613

There was a bug where calling unload() right before calling load()
would cause a race that would sometimes cause a failure.  This is
because the fields are reset before the unload is complete so the
second call to unload() (from inside load) will complete immediately.

So now we store the unload Promise chain while it is in progress so
we can wait on it from load().

Closes #612

Change-Id: I6c0cdd931827d709fc41322edd51fe10e4aa87ae
  • Loading branch information
TheModMaker authored and joeyparrish committed Jan 6, 2017
1 parent 1adaaec commit 18ed1af
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 8 deletions.
27 changes: 20 additions & 7 deletions lib/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,9 @@ shaka.Player = function(video, opt_dependencyInjector) {
/** @private {shaka.util.CancelableChain} */
this.loadChain_ = null;

/** @private {Promise} */
this.unloadChain_ = null;

/**
* @private {!Object.<string, {
* stream: shakaExtern.Stream,
Expand Down Expand Up @@ -177,6 +180,10 @@ shaka.Player.prototype.destroy = function() {

return cancelation.then(function() {
var p = Promise.all([
// We need to destroy the current fields as well as waiting for an
// existing unload to complete. It is fine to call destroyStreaming_ if
// there is an unload since it resets the fields immediately.
this.unloadChain_,
this.destroyStreaming_(),
this.eventManager_ ? this.eventManager_.destroy() : null,
this.networkingEngine_ ? this.networkingEngine_.destroy() : null
Expand Down Expand Up @@ -763,18 +770,24 @@ shaka.Player.prototype.unload = function() {
if (this.destroyed_) return Promise.resolve();
this.dispatchEvent(new shaka.util.FakeEvent('unloading'));

var p = Promise.resolve();
if (this.loadChain_) {
// A load is in progress. Cancel it, then reset the streaming system.
// A load is in progress, cancel it.
var interrupt = new shaka.util.Error(
shaka.util.Error.Category.PLAYER,
shaka.util.Error.Code.LOAD_INTERRUPTED);
return this.loadChain_.cancel(interrupt)
.then(this.resetStreaming_.bind(this));
} else {
// No loads or unloads are in progress.
// Just reset the streaming system if needed.
return this.resetStreaming_();
p = this.loadChain_.cancel(interrupt);
}

return p.then(function() {
// If there is an existing unload operation, use that.
if (!this.unloadChain_) {
this.unloadChain_ = this.resetStreaming_().then(function() {
this.unloadChain_ = null;
}.bind(this));
}
return this.unloadChain_;
}.bind(this));
};


Expand Down
2 changes: 1 addition & 1 deletion test/player_unit.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ describe('Player', function() {
});
});

xit('won\'t start loading until unloading is done', function(done) {
it('won\'t start loading until unloading is done', function(done) {
// There was a bug when calling unload before calling load would cause
// the load to continue before the (first) unload was complete.
// https://github.com/google/shaka-player/issues/612
Expand Down

0 comments on commit 18ed1af

Please sign in to comment.