rx-player
Version:
Canal+ HTML5 Video Player
73 lines (72 loc) • 3 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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = parseCNodes;
var is_non_empty_string_1 = require("../../../utils/is_non_empty_string");
/**
* Parse C nodes to build index timeline.
* @param {Object} nodes
*/
function parseCNodes(nodes) {
return nodes.reduce(function (timeline, node, i) {
var _a, _b, _c, _d;
if (typeof node === "string") {
return timeline;
}
var dAttr = (_a = node.attributes.d) !== null && _a !== void 0 ? _a : null;
var tAttr = (_b = node.attributes.t) !== null && _b !== void 0 ? _b : null;
var rAttr = (_c = node.attributes.r) !== null && _c !== void 0 ? _c : null;
var repeatCount = rAttr !== null ? +rAttr - 1 : 0;
var start = tAttr !== null ? +tAttr : undefined;
var duration = dAttr !== null ? +dAttr : undefined;
if (i === 0) {
// first node
start = start === undefined || isNaN(start) ? 0 : start;
}
else {
// from second node to the end
var 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)) {
var nextNodeIdx = i;
var nextNodeElt = void 0;
do {
nextNodeIdx++;
nextNodeElt = nodes[nextNodeIdx];
} while (nextNodeElt !== undefined && typeof nextNodeElt === "string");
if (nextNodeElt !== undefined) {
var nextTAttr = (_d = nextNodeElt.attributes.t) !== null && _d !== void 0 ? _d : null;
var nextStart = (0, is_non_empty_string_1.default)(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: duration, start: start, repeatCount: repeatCount });
return timeline;
}, []);
}