Files
shaka-player/lib/device/device_factory.js
T
Wojciech Tyczyński 970d7756ea feat: Add Device API (#8210)
The goal is to simplify and abstract feature logic detection. Currently
lots of places depend on various calls to `shaka.util.Platform` and
mainteinance of this is hard & not easy to read.

By introducing device API ideally rest of the player logic would look
into device features instead of directly checking platform. Additionally
we can more easily cache needed values, so we won't have to parse user
agent several times anymore.

---------

Co-authored-by: Álvaro Velad Galván <ladvan91@hotmail.com>
2025-06-02 13:46:40 +02:00

65 lines
1.8 KiB
JavaScript

/*! @license
* Shaka Player
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
goog.provide('shaka.device.DeviceFactory');
goog.require('goog.asserts');
goog.require('shaka.log');
goog.require('shaka.util.Lazy');
goog.requireType('shaka.device.IDevice');
shaka.device.DeviceFactory = class {
/**
* @param {?function(): shaka.device.IDevice} deviceFactory
*/
static registerDeviceFactory(deviceFactory) {
goog.asserts.assert(!shaka.device.DeviceFactory.factory_,
'Device Factory should NOT be defined');
shaka.device.DeviceFactory.factory_ = deviceFactory;
}
/**
* @param {?function(): shaka.device.IDevice} deviceFactory
*/
static registerDefaultDeviceFactory(deviceFactory) {
goog.asserts.assert(!shaka.device.DeviceFactory.factory_,
'Default device Factory should NOT be defined');
shaka.device.DeviceFactory.defaultFactory_ = deviceFactory;
}
/**
* @return {shaka.device.IDevice}
*/
static getDevice() {
goog.asserts.assert(shaka.device.DeviceFactory.factory_ ||
shaka.device.DeviceFactory.defaultFactory_,
'Device Factory should be defined');
return shaka.device.DeviceFactory.device_.value();
}
};
/** @private {?function(): shaka.device.IDevice} */
shaka.device.DeviceFactory.factory_ = null;
/** @private {?function(): shaka.device.IDevice} */
shaka.device.DeviceFactory.defaultFactory_ = null;
/** @private {!shaka.util.Lazy<shaka.device.IDevice>} */
shaka.device.DeviceFactory.device_ = new shaka.util.Lazy(() => {
let device = undefined;
if (shaka.device.DeviceFactory.factory_) {
device = shaka.device.DeviceFactory.factory_();
}
if (!device && shaka.device.DeviceFactory.defaultFactory_) {
device = shaka.device.DeviceFactory.defaultFactory_();
}
if (device) {
shaka.log.info(device.toString());
}
return device;
});