rx-player
Version:
Canal+ HTML5 Video Player
70 lines (69 loc) • 2.85 kB
JavaScript
/**
* Copyright 2015 CANAL+ Group
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import isNonEmptyString from "../../../utils/is_non_empty_string";
/**
* Parse C nodes to build index timeline.
* @param {Object} nodes
*/
export default function parseCNodes(nodes) {
return nodes.reduce((timeline, node, i) => {
var _a, _b, _c, _d;
if (typeof node === "string") {
return timeline;
}
const dAttr = (_a = node.attributes.d) !== null && _a !== void 0 ? _a : null;
const tAttr = (_b = node.attributes.t) !== null && _b !== void 0 ? _b : null;
const rAttr = (_c = node.attributes.r) !== null && _c !== void 0 ? _c : null;
const repeatCount = rAttr !== null ? +rAttr - 1 : 0;
let start = tAttr !== null ? +tAttr : undefined;
let duration = dAttr !== null ? +dAttr : undefined;
if (i === 0) {
// first node
start = start === undefined || isNaN(start) ? 0 : start;
}
else {
// from second node to the end
const prev = timeline[i - 1];
if (start === undefined || isNaN(start)) {
if (prev.duration === undefined || isNaN(prev.duration)) {
throw new Error("Smooth: Invalid CNodes. Missing timestamp.");
}
start = prev.start + prev.duration * (prev.repeatCount + 1);
}
}
if (duration === undefined || isNaN(duration)) {
let nextNodeIdx = i;
let nextNodeElt;
do {
nextNodeIdx++;
nextNodeElt = nodes[nextNodeIdx];
} while (nextNodeElt !== undefined && typeof nextNodeElt === "string");
if (nextNodeElt !== undefined) {
const nextTAttr = (_d = nextNodeElt.attributes.t) !== null && _d !== void 0 ? _d : null;
const nextStart = isNonEmptyString(nextTAttr) ? +nextTAttr : null;
if (nextStart === null) {
throw new Error("Can't build index timeline from Smooth Manifest.");
}
duration = nextStart - start;
}
else {
return timeline;
}
}
timeline.push({ duration, start, repeatCount });
return timeline;
}, []);
}