Skip to content

Commit 0ea63e8

Browse files
committedFeb 24, 2025
feat: sort key systems by priority
1 parent 49d1ffa commit 0ea63e8

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed
 

‎packages/playback/src/lib/eme/eme-manager.ts

+26-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import type { IEmeManager, IEmeManagerDependencies, IKeySessionMetadata } from '../types/eme-manager.declarations';
22
import type { INetworkManager } from '../types/network.declarations';
33
import type { ILogger } from '../types/logger.declarations';
4-
import type { IPlayerSource } from '../types/source.declarations';
4+
import type { IKeySystemConfig, IPlayerSource } from '../types/source.declarations';
55
import type { IEventEmitter } from '../types/event-emitter.declarations';
66
import type {
77
EventTypeToEventMap,
@@ -166,7 +166,7 @@ export class EmeManager implements IEmeManager {
166166
this.privateEventEmitter_.addEventListener(PlayerEventType.DashManifestParsed, this.handleParsedManifestEvent_);
167167
}
168168

169-
private getKeySystemConfig_(): Record<string, MediaKeySystemConfiguration> {
169+
private getMediaKeySystemConfig_(): Record<string, MediaKeySystemConfiguration> {
170170
// TODO: Write logic to get this info from manifests and segment data
171171
// We will probably need to pass in a list of key systems
172172

@@ -225,16 +225,35 @@ export class EmeManager implements IEmeManager {
225225
return mediaKeySystemAccess;
226226
}
227227

228-
// TODO: Sort by priority before this
228+
// Sort key systems by priority
229+
const keySystemsArray = Object.keys(keySystems).map((key) => [key, keySystems[key]]);
229230

230-
for (const keySystem in keySystems) {
231-
const keySystemConfig = this.getKeySystemConfig_();
231+
keySystemsArray.sort((a, b) => {
232+
const keySystemPriorityA = (a[1] as IKeySystemConfig).priority;
233+
const keySystemPriorityB = (b[1] as IKeySystemConfig).priority;
234+
235+
if (!keySystemPriorityA && !keySystemPriorityB) {
236+
return 0;
237+
} else if (!keySystemPriorityA) {
238+
return 1;
239+
} else if (!keySystemPriorityB) {
240+
return -1;
241+
}
242+
243+
return keySystemPriorityA - keySystemPriorityB;
244+
});
245+
246+
keySystemsArray.forEach(async (keySystemArr) => {
247+
const keySystem = keySystemArr[0] as string;
248+
// const keySystemConfig = keySystemArr[1] as IKeySystemConfig;
249+
// TODO: Pass in key system info
250+
const mediaKeySystemConfig = this.getMediaKeySystemConfig_();
232251

233252
try {
234253
this.eventEmitter_.emitEvent(new KeySystemAccessRequestedEvent(keySystem));
235254
this.logger_.debug('EME: Requesting media key system access.');
236255

237-
mediaKeySystemAccess = await navigator.requestMediaKeySystemAccess(keySystem, [keySystemConfig]);
256+
mediaKeySystemAccess = await navigator.requestMediaKeySystemAccess(keySystem, [mediaKeySystemConfig]);
238257

239258
return mediaKeySystemAccess;
240259
} catch (error) {
@@ -243,7 +262,7 @@ export class EmeManager implements IEmeManager {
243262
`EME: Media key system access request failed. Key System: ${keySystem} Error: ${error as Error}`
244263
);
245264
}
246-
}
265+
});
247266

248267
return mediaKeySystemAccess;
249268
}

‎packages/playback/src/lib/types/source.declarations.ts

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ export interface IKeySystemConfig {
1919
* Defaults to ''.
2020
*/
2121
individualizationServerUri?: string;
22+
/**
23+
* A custom function to find the content ID from the init data.
24+
*/
2225
getContentId?: (initData: ArrayBuffer) => string;
2326
/**
2427
* Rare cases when we want to leave it up to the user to get the license

0 commit comments

Comments
 (0)