Files
shaka-player/lib/util/functional.js
T
Andy(김규회) d98169bc25 feat(EME): Add retryLicensing() and failureCallback for manual license retry (#9638)
So basically, when a license request fails (eg. network Error, server
down whatever), apps can now retry from scratch by calling
`player.retryLicensing()`. This was tricky to implement because of EME
spec limitations: `generateRequest()` can only be called once per
session. So if it fails, it would be stuck.

So I close the old session and create a brand new one with the same
`initData`

> Will Video element throw an error during this process?

we were worried that closing the session would leave the video without
keys for a brief moment, potentially triggering errors. But in practice,
the transition is fast enough( I added a 0.1s delay for CDM clean up)
and the video element handles it gracefully

> Will new encrypted event fire? If not, will it limit this feature?

The encrypted event only fires when the browser first encounters
encrypted content. When we close and recreate a session, the content is
already loaded, so no new event

Solutions: In `CreateSession()` metadata store `initData` and
`initDataType` in the session metadata when the session is first
created. So when `retryLicensing()`is called, we just grab the stored
data and pass it to `generateRequest()` on the new session. No need to
wait for an `encrypted` event at all.

---------

Co-authored-by: Álvaro Velad Galván <ladvan91@hotmail.com>
Co-authored-by: Wojciech Tyczyński <tykus160@gmail.com>
2026-02-05 14:12:30 +01:00

110 lines
2.6 KiB
JavaScript

/*! @license
* Shaka Player
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
goog.provide('shaka.util.Functional');
goog.require('shaka.util.Timer');
/**
* @summary A set of functional utility functions.
*/
shaka.util.Functional = class {
/**
* Creates a promise chain that calls the given callback for each element in
* the array in a catch of a promise.
*
* e.g.:
* Promise.reject().catch(callback(array[0])).catch(callback(array[1]));
*
* @param {!Array<ELEM>} array
* @param {function(ELEM): !Promise<RESULT>} callback
* @return {!Promise<RESULT>}
* @template ELEM,RESULT
*/
static createFallbackPromiseChain(array, callback) {
return array.reduce((promise, elem) => {
return promise.catch(() => callback(elem));
}, Promise.reject());
}
/**
* Returns the first array concatenated to the second; used to collapse an
* array of arrays into a single array.
*
* @param {!Array<T>} all
* @param {!Array<T>} part
* @return {!Array<T>}
* @template T
*/
static collapseArrays(all, part) {
return all.concat(part);
}
/**
* A no-op function that ignores its arguments. This is used to suppress
* unused variable errors.
* @param {...*} args
*/
static ignored(...args) {}
/**
* A no-op function. Useful in promise chains.
*/
static noop() {}
/**
* Returns if the given value is not null; useful for filtering out null
* values.
*
* @param {T} value
* @return {boolean}
* @template T
*/
static isNotNull(value) {
return value != null;
}
/**
* Returns a Promise which is resolved only if |asyncProcess| is resolved, and
* only if it is resolved in less than |seconds| seconds.
*
* If the returned Promise is resolved, it returns the same value as
* |asyncProcess|.
*
* If |asyncProcess| fails, the returned Promise is rejected.
* If |asyncProcess| takes too long, the returned Promise is rejected, but
* |asyncProcess| is still allowed to complete.
*
* @param {number} seconds
* @param {!Promise<T>} asyncProcess
* @return {!Promise<T>}
* @template T
*/
static promiseWithTimeout(seconds, asyncProcess) {
return Promise.race([
asyncProcess,
new Promise(((_, reject) => {
new shaka.util.Timer(reject).tickAfter(seconds);
})),
]);
}
/**
* Returns a Promise which is resolved after the given number of seconds.
*
* @param {number} seconds
* @return {!Promise}
*/
static delay(seconds) {
return new Promise((resolve) => {
new shaka.util.Timer(resolve).tickAfter(seconds);
});
}
};