rx-player
Version:
Canal+ HTML5 Video Player
107 lines (106 loc) • 4.89 kB
JavaScript
import BROWSER_GLOBALS from "../../compat/browser_compatibility_types";
import { formatError } from "../../errors";
import { createRepresentationFilterFromFnString } from "../../manifest";
import isNullOrUndefined from "../../utils/is_null_or_undefined";
/**
* Format an error, which may be of any form, into an Object than can easily be
* serialized - e.g. to be communicated through a postMessage-like API.
* @param {*} error - The error to serialized
* @returns {Object} - A serialization-safe error format
*/
export function formatErrorForSender(error) {
const formattedError = formatError(error, {
defaultCode: "NONE",
defaultReason: "An unknown error stopped content playback.",
});
return formattedError.serialize();
}
/**
* Synchronize SegmentSinks with what has been buffered.
* @param {Object} observation - The just-received playback observation,
* including what has been buffered on lower-level buffers
* @param {Object} segmentSinksStore - Interface allowing to interact
* with `SegmentSink`s, so their inventory can be updated accordingly.
*/
export function synchronizeSegmentSinksOnObservation(observation, segmentSinksStore) {
// Synchronize SegmentSinks with what has been buffered.
["video", "audio", "text"].forEach((tType) => {
var _a;
const segmentSinkStatus = segmentSinksStore.getStatus(tType);
if (segmentSinkStatus.type === "initialized") {
segmentSinkStatus.value.synchronizeInventory((_a = observation.buffered[tType]) !== null && _a !== void 0 ? _a : []);
}
});
}
/**
* Set Representation.isCodecSupportedInWebWorker to true or false
* If the codec is supported in the current context.
* If MSE in worker is not available, the attribute is not set.
*/
export function updateCodecSupportInWorkerMode(manifestToUpdate) {
var _a, _b;
if (isNullOrUndefined(BROWSER_GLOBALS.MediaSource_)) {
return;
}
const codecsMap = new Map();
for (const period of manifestToUpdate.periods) {
const checkedAdaptations = [
...((_a = period.adaptations.video) !== null && _a !== void 0 ? _a : []),
...((_b = period.adaptations.audio) !== null && _b !== void 0 ? _b : []),
];
for (const adaptation of checkedAdaptations) {
for (const representation of adaptation.representations) {
const codec = representation.getMimeTypeString();
if (codecsMap.has(codec)) {
representation.isCodecSupportedInWebWorker = codecsMap.get(codec);
}
else {
const supported = BROWSER_GLOBALS.MediaSource_.isTypeSupported(codec);
representation.isCodecSupportedInWebWorker = supported;
codecsMap.set(codec, supported);
}
}
}
}
}
/**
* Some functions may be defined by the API, we call those "plugins".
* This function parses and extract the actual function from the different
* ways an application can provide it to us.
* @param {Object} input - The API input
* @param {Object} corePlugins - Context on what identified functions are
* defined right now.
* @returns {Function}
*/
export function extractExternalPlugins(input, corePlugins) {
var _a, _b, _c, _d, _e, _f, _g;
let manifestLoader;
let segmentLoader;
let representationFilter;
if (typeof ((_a = input.representationFilter) === null || _a === void 0 ? void 0 : _a.fn) === "function") {
representationFilter = input.representationFilter.fn;
}
else if (typeof ((_b = input.representationFilter) === null || _b === void 0 ? void 0 : _b.eval) === "string") {
representationFilter = createRepresentationFilterFromFnString(input.representationFilter.eval);
}
else if (typeof ((_c = input.representationFilter) === null || _c === void 0 ? void 0 : _c.workerId) === "string") {
representationFilter = corePlugins.representationFilters.get(input.representationFilter.workerId);
}
if (typeof ((_d = input.manifestLoader) === null || _d === void 0 ? void 0 : _d.fn) === "function") {
manifestLoader = input.manifestLoader.fn;
}
else if (typeof ((_e = input.manifestLoader) === null || _e === void 0 ? void 0 : _e.workerId) === "string") {
manifestLoader = corePlugins.manifestLoaders.get(input.manifestLoader.workerId);
}
if (typeof ((_f = input.segmentLoader) === null || _f === void 0 ? void 0 : _f.fn) === "function") {
segmentLoader = input.segmentLoader.fn;
}
else if (typeof ((_g = input.segmentLoader) === null || _g === void 0 ? void 0 : _g.workerId) === "string") {
segmentLoader = corePlugins.segmentLoaders.get(input.segmentLoader.workerId);
}
return {
manifestLoader,
segmentLoader,
representationFilter,
};
}