Skip to content

Commit 3ca6f26

Browse files
authored
fix: support check persistent-license to remove the mediakeysession (#7050)
Authored-by: Huan Pu <[email protected]>
1 parent 915e6c5 commit 3ca6f26

File tree

3 files changed

+77
-3
lines changed

3 files changed

+77
-3
lines changed

src/controller/eme-controller.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import { Logger } from '../utils/logger';
1212
import {
1313
getKeySystemsForConfig,
1414
getSupportedMediaKeySystemConfigurations,
15+
isPersistentSessionType,
1516
keySystemFormatToKeySystemDomain,
1617
keySystemIdToKeySystemDomain,
1718
KeySystems,
@@ -1409,8 +1410,17 @@ class EMEController extends Logger implements ComponentAPI {
14091410
if (index > -1) {
14101411
this.mediaKeySessions.splice(index, 1);
14111412
}
1412-
return mediaKeysSession
1413-
.remove()
1413+
const { drmSystemOptions } = this.config;
1414+
const removePromise = isPersistentSessionType(drmSystemOptions)
1415+
? new Promise((resolve, reject) => {
1416+
self.setTimeout(
1417+
() => reject(new Error(`MediaKeySession.remove() timeout`)),
1418+
8000,
1419+
);
1420+
mediaKeysSession.remove().then(resolve);
1421+
})
1422+
: Promise.resolve();
1423+
return removePromise
14141424
.catch((error) => {
14151425
this.log(`Could not remove session: ${error}`);
14161426
this.hls?.trigger(Events.ERROR, {

src/utils/mediakeys-helper.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,17 @@ function createMediaKeySystemConfigurations(
166166
return [baseConfig];
167167
}
168168

169+
export function isPersistentSessionType(
170+
drmSystemOptions: DRMSystemOptions,
171+
): boolean {
172+
return (
173+
drmSystemOptions.sessionType === 'persistent-license' ||
174+
!!drmSystemOptions.sessionTypes?.some(
175+
(type) => type === 'persistent-license',
176+
)
177+
);
178+
}
179+
169180
export function parsePlayReadyWRM(keyBytes: Uint8Array) {
170181
const keyBytesUtf16 = new Uint16Array(
171182
keyBytes.buffer,

tests/unit/controller/eme-controller.ts

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -691,10 +691,63 @@ describe('EMEController', function () {
691691
return;
692692
}
693693
return EMEController.CDMCleanupPromise.then(() => {
694-
expect(keySessionRemoveSpy).callCount(1);
695694
expect(keySessionCloseSpy).callCount(1);
696695
expect(emeController.mediaKeySessions.length).to.equal(0);
697696
expect(media.setMediaKeys).calledWith(null);
698697
});
699698
});
699+
700+
it('should remove all media key sessions and remove all media key sessions when call destroy with persistent-license session type', function () {
701+
const reqMediaKsAccessSpy = sinon.spy(function () {
702+
return Promise.resolve({
703+
// Media-keys mock
704+
keySystem: 'com.apple.fps',
705+
createMediaKeys: sinon.spy(() =>
706+
Promise.resolve({
707+
setServerCertificate: () => Promise.resolve(),
708+
createSession: () => ({
709+
addEventListener: () => {},
710+
removeEventListener: () => {},
711+
generateRequest: () => Promise.resolve(),
712+
remove: () => Promise.resolve(),
713+
update: () => Promise.resolve(),
714+
keyStatuses: new Map(),
715+
}),
716+
}),
717+
),
718+
});
719+
});
720+
const keySessionRemoveSpy = sinon.spy(() => Promise.resolve());
721+
const keySessionCloseSpy = sinon.spy(() => Promise.resolve());
722+
723+
setupEach({
724+
emeEnabled: true,
725+
requestMediaKeySystemAccessFunc: reqMediaKsAccessSpy,
726+
drmSystemOptions: {
727+
sessionType: 'persistent-license',
728+
},
729+
});
730+
731+
emeController.onMediaAttached(Events.MEDIA_ATTACHED, {
732+
media: media as any as HTMLMediaElement,
733+
});
734+
emeController.mediaKeySessions = [
735+
{
736+
mediaKeysSession: {
737+
remove: keySessionRemoveSpy,
738+
close: keySessionCloseSpy,
739+
},
740+
} as any,
741+
];
742+
emeController.destroy();
743+
744+
expect(EMEController.CDMCleanupPromise).to.be.a('Promise');
745+
if (!EMEController.CDMCleanupPromise) {
746+
return;
747+
}
748+
return EMEController.CDMCleanupPromise.then(() => {
749+
expect(keySessionCloseSpy).callCount(1);
750+
expect(emeController.mediaKeySessions.length).to.equal(0);
751+
});
752+
});
700753
});

0 commit comments

Comments
 (0)