Skip to content

Commit

Permalink
fix(FEC-12089): Tizen playback breaks after ad finishes
Browse files Browse the repository at this point in the history
Make sure mediaKeys are removed before calling load, otherwise playing encrypted media can fail.
This can happen if reset was not called before load or if shaka failed to remove the mediaKeys on reset, which is something that can happen specifically when using the same video element for ads like with ima on smart tv.
  • Loading branch information
SivanA-Kaltura authored Jun 20, 2022
1 parent 6525241 commit f30030a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/dash-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -786,8 +786,9 @@ export default class DashAdapter extends BaseMediaSourceAdapter {
* @function load
* @override
*/
load(startTime: ?number): Promise<Object> {
async load(startTime: ?number): Promise<Object> {
if (!this._loadPromise) {
await this._removeMediaKeys();
this._shaka.attach(this._videoElement);
this._loadPromise = new Promise((resolve, reject) => {
if (this._sourceObj && this._sourceObj.url) {
Expand Down Expand Up @@ -878,6 +879,30 @@ export default class DashAdapter extends BaseMediaSourceAdapter {
return Promise.resolve();
}

/**
* Remove mediaKeys from the video element.
* mediaKeys are set if an encrypted media was previously played, and must be removed before a new encrypted media can be played.
* If mediaKeys are not null it means that shaka reset wasn't called or that it failed to remove them.
* @returns {Promise<void>} Promise that resolves when the operation finishes.
*/
async _removeMediaKeys(): Promise<void> {
if (this._videoElement && this._videoElement.mediaKeys) {
try {
DashAdapter._logger.debug('Removing mediaKeys from the video element');
await this._videoElement.setMediaKeys(null);
DashAdapter._logger.debug('mediaKeys removed');
} catch (e) {
// non encrypted playback should still work, so we don't reject
this._logger.warn('mediaKeys not cleared');
} finally {
// eslint-disable-next-line no-unsafe-finally
return Promise.resolve();
}
} else {
return Promise.resolve();
}
}

/**
* Get the original video tracks
* @function _getVideoTracks
Expand Down
9 changes: 9 additions & 0 deletions test/src/dash-adapter.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,15 @@ describe('DashAdapter: load', () => {
done();
});
});

it('should try to remove media keys on load', done => {
dashInstance = DashAdapter.createAdapter(video, vodSource, config);
const removeMediaKeys = global.sinon.spy(dashInstance, '_removeMediaKeys');
dashInstance.load();
removeMediaKeys.should.have.callCount(1);
removeMediaKeys.restore();
done();
});
});

describe('DashAdapter: targetBuffer', () => {
Expand Down

0 comments on commit f30030a

Please sign in to comment.