Skip to content

Commit

Permalink
Wait for close(), but timeout after 1s
Browse files Browse the repository at this point in the history
An improved workaround for https://crbug.com/690583 and #1093, on top
of PR #1168.

If we don't wait at all, we end up trying to use the same session ID
too quickly, which causes a playback failure.

Change-Id: I8c9d1a0f09432054b862e033b80b0c2f47739c74
  • Loading branch information
joeyparrish committed Dec 6, 2017
1 parent 5cca0e5 commit 8c80b6c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 9 deletions.
18 changes: 10 additions & 8 deletions lib/media/drm_engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,21 +170,23 @@ shaka.media.DrmEngine.prototype.destroy = function() {
var Functional = shaka.util.Functional;
this.destroyed_ = true;

// TODO: wait for sessions to close when destroying. Due to a bug in Chrome,
// sometimes the Promise returned by MediaKeySession#close never resolves.
// See #1093 and https://crbug.com/690583
var async = [];

// Wait for sessions to close when destroying.
this.activeSessions_.forEach(function(activeSession) {
// Ignore any errors when closing the sessions. One such error would be
// an invalid state error triggered by closing a session which has not
// generated any key requests.
// Chrome sometimes returns |undefined|: https://crbug.com/690664
var p = activeSession.session.close() || Promise.resolve();
p = p.catch(Functional.noop);
var close = activeSession.session.close().catch(Functional.noop);
// Due to a bug in Chrome, sometimes the Promise returned by close()
// never resolves. See #1093 and https://crbug.com/690583
var closeTimeout = new Promise(function(resolve) {
setTimeout(resolve, 1000);
});
async.push(Promise.race([close, closeTimeout]));
});
this.allSessionsLoaded_.reject();

var async = [];

if (this.eventManager_)
async.push(this.eventManager_.destroy());

Expand Down
10 changes: 9 additions & 1 deletion test/offline/offline_integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,15 @@ describe('Offline', /** @suppress {accessControls} */ function() {
expect(session).toBeFalsy();
return drmEngine.destroy();
})
.catch(fail)
.catch(function(error) {
fail(error);

// Make sure we clean up the extra DrmEngine even if the Promise
// chain and test fail.
if (drmEngine) {
return drmEngine.destroy();
}
})
.then(done);
});

Expand Down

0 comments on commit 8c80b6c

Please sign in to comment.