Skip to content

Commit

Permalink
factorize code
Browse files Browse the repository at this point in the history
  • Loading branch information
Florent-Bouisset authored and peaBerberian committed Aug 5, 2024
1 parent 42e2dab commit 9c2c23f
Showing 1 changed file with 30 additions and 44 deletions.
74 changes: 30 additions & 44 deletions src/compat/eme/eme-api-implementation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,29 +144,8 @@ function getEmeApiImplementation(
let createCustomMediaKeys: (keyType: string) => ICustomMediaKeys;

if (preferredApiType === "webkit" && WebKitMediaKeysConstructor !== undefined) {
const compatibleEventListener = createCompatibleEventListener(
["needkey"],
undefined /* prefixes */,
);
onEncrypted = (
target: IEventTargetLike,
listener: (event?: Event) => void,
cancelSignal: CancellationSignal,
) => {
compatibleEventListener(
target,
(event?: Event) => {
const patchedEvent = object_assign(
{ forceSessionRecreation: true },
event as MediaEncryptedEvent,
);
listener(patchedEvent);
},
cancelSignal,
);
};

const callbacks = getWebKitMediaKeysCallbacks();
onEncrypted = createOnEncryptedForWebkit();
isTypeSupported = callbacks.isTypeSupported;
createCustomMediaKeys = callbacks.createCustomMediaKeys;
setMediaKeys = callbacks.setMediaKeys;
Expand All @@ -182,28 +161,7 @@ function getEmeApiImplementation(
implementation = "older-webkit";
// This is for WebKit with prefixed EME api
} else if (WebKitMediaKeysConstructor !== undefined) {
const compatibleEventListener = createCompatibleEventListener(
["needkey"],
undefined /* prefixes */,
);
onEncrypted = (
target: IEventTargetLike,
listener: (event?: Event) => void,
cancelSignal: CancellationSignal,
) => {
compatibleEventListener(
target,
(event?: Event) => {
const patchedEvent = object_assign(
{ forceSessionRecreation: true },
event as MediaEncryptedEvent,
);
listener(patchedEvent);
},
cancelSignal,
);
};

onEncrypted = createOnEncryptedForWebkit();
const callbacks = getWebKitMediaKeysCallbacks();
isTypeSupported = callbacks.isTypeSupported;
createCustomMediaKeys = callbacks.createCustomMediaKeys;
Expand Down Expand Up @@ -317,6 +275,34 @@ function getEmeApiImplementation(
implementation,
};
}
/**
* Create an event listener for the "webkitneedkey" event
* @returns
*/
function createOnEncryptedForWebkit(): IEmeApiImplementation["onEncrypted"] {
const compatibleEventListener = createCompatibleEventListener(
["needkey"],
undefined /* prefixes */,
);
const onEncrypted = (
target: IEventTargetLike,
listener: (event?: Event) => void,
cancelSignal: CancellationSignal,
) => {
compatibleEventListener(
target,
(event?: Event) => {
const patchedEvent = object_assign(
{ forceSessionRecreation: true },
event as MediaEncryptedEvent,
);
listener(patchedEvent);
},
cancelSignal,
);
};
return onEncrypted;
}

/**
* Set the given MediaKeys on the given HTMLMediaElement.
Expand Down

0 comments on commit 9c2c23f

Please sign in to comment.