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

Close media key sessions and clear media keys when media detached #2664

Merged
merged 4 commits into from
May 20, 2020
Merged
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
23 changes: 20 additions & 3 deletions src/controller/eme-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -522,10 +522,27 @@ class EMEController extends EventHandler {
}

onMediaDetached () {
if (this._media) {
this._media.removeEventListener('encrypted', this._onMediaEncrypted);
this._media = null; // release reference
const media = this._media;
const mediaKeysList = this._mediaKeysList;
if (!media) {
return;
}
media.removeEventListener('encrypted', this._onMediaEncrypted);
this._media = null;
this._mediaKeysList = [];
// Close all sessions and remove media keys from the video element.
Promise.all(mediaKeysList.map((mediaKeysListItem) => {
if (mediaKeysListItem.mediaKeysSession) {
return mediaKeysListItem.mediaKeysSession.close().catch(() => {
// Ignore errors when closing the sessions. Closing a session that
// generated no key requests will throw an error.
});
}
})).then(() => {
return media.setMediaKeys(null);
}).catch(() => {
// Ignore any failures while removing media keys from the video element.
});
}

// TODO: Use manifest types here when they are defined
Expand Down
30 changes: 30 additions & 0 deletions tests/unit/controller/eme-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const MediaMock = function () {
let media = new EventEmitter();
media.setMediaKeys = sinon.spy();
media.addEventListener = media.addListener.bind(media);
media.removeEventListener = media.removeListener.bind(media);
return media;
};

Expand Down Expand Up @@ -104,4 +105,33 @@ describe('EMEController', function () {
done();
}, 0);
});

it('should close all media key sessions and remove media keys when media is detached', function (done) {
let reqMediaKsAccessSpy = sinon.spy(function () {
return Promise.resolve({
// Media-keys mock
});
});
let keySessionCloseSpy = sinon.spy(() => Promise.resolve());

setupEach({
emeEnabled: true,
requestMediaKeySystemAccessFunc: reqMediaKsAccessSpy
});

emeController.onMediaAttached({ media });
emeController._mediaKeysList = [{
mediaKeysSession: {
close: keySessionCloseSpy
}
}];
emeController.onMediaDetached();

setTimeout(function () {
expect(keySessionCloseSpy.callCount).to.equal(1);
expect(emeController._mediaKeysList.length).to.equal(0);
expect(media.setMediaKeys.calledWith(null)).to.be.true;
done();
}, 0);
});
});