1 | /**
|
2 | * @file playlist.js
|
3 | *
|
4 | * Playlist related utilities.
|
5 | */
|
6 | import {createTimeRange} from 'video.js';
|
7 | import window from 'global/window';
|
8 |
|
9 | /**
|
10 | * walk backward until we find a duration we can use
|
11 | * or return a failure
|
12 | *
|
13 | * @param {Playlist} playlist the playlist to walk through
|
14 | * @param {Number} endSequence the mediaSequence to stop walking on
|
15 | */
|
16 |
|
17 | const backwardDuration = function(playlist, endSequence) {
|
18 | let result = 0;
|
19 | let i = endSequence - playlist.mediaSequence;
|
20 | // if a start time is available for segment immediately following
|
21 | // the interval, use it
|
22 | let segment = playlist.segments[i];
|
23 |
|
24 | // Walk backward until we find the latest segment with timeline
|
25 | // information that is earlier than endSequence
|
26 | if (segment) {
|
27 | if (typeof segment.start !== 'undefined') {
|
28 | return { result: segment.start, precise: true };
|
29 | }
|
30 | if (typeof segment.end !== 'undefined') {
|
31 | return {
|
32 | result: segment.end - segment.duration,
|
33 | precise: true
|
34 | };
|
35 | }
|
36 | }
|
37 | while (i--) {
|
38 | segment = playlist.segments[i];
|
39 | if (typeof segment.end !== 'undefined') {
|
40 | return { result: result + segment.end, precise: true };
|
41 | }
|
42 |
|
43 | result += segment.duration;
|
44 |
|
45 | if (typeof segment.start !== 'undefined') {
|
46 | return { result: result + segment.start, precise: true };
|
47 | }
|
48 | }
|
49 | return { result, precise: false };
|
50 | };
|
51 |
|
52 | /**
|
53 | * walk forward until we find a duration we can use
|
54 | * or return a failure
|
55 | *
|
56 | * @param {Playlist} playlist the playlist to walk through
|
57 | * @param {Number} endSequence the mediaSequence to stop walking on
|
58 | */
|
59 | const forwardDuration = function(playlist, endSequence) {
|
60 | let result = 0;
|
61 | let segment;
|
62 | let i = endSequence - playlist.mediaSequence;
|
63 | // Walk forward until we find the earliest segment with timeline
|
64 | // information
|
65 |
|
66 | for (; i < playlist.segments.length; i++) {
|
67 | segment = playlist.segments[i];
|
68 | if (typeof segment.start !== 'undefined') {
|
69 | return {
|
70 | result: segment.start - result,
|
71 | precise: true
|
72 | };
|
73 | }
|
74 |
|
75 | result += segment.duration;
|
76 |
|
77 | if (typeof segment.end !== 'undefined') {
|
78 | return {
|
79 | result: segment.end - result,
|
80 | precise: true
|
81 | };
|
82 | }
|
83 |
|
84 | }
|
85 | // indicate we didn't find a useful duration estimate
|
86 | return { result: -1, precise: false };
|
87 | };
|
88 |
|
89 | /**
|
90 | * Calculate the media duration from the segments associated with a
|
91 | * playlist. The duration of a subinterval of the available segments
|
92 | * may be calculated by specifying an end index.
|
93 | *
|
94 | * @param {Object} playlist a media playlist object
|
95 | * @param {Number=} endSequence an exclusive upper boundary
|
96 | * for the playlist. Defaults to playlist length.
|
97 | * @param {Number} expired the amount of time that has dropped
|
98 | * off the front of the playlist in a live scenario
|
99 | * @return {Number} the duration between the first available segment
|
100 | * and end index.
|
101 | */
|
102 | const intervalDuration = function(playlist, endSequence, expired) {
|
103 | let backward;
|
104 | let forward;
|
105 |
|
106 | if (typeof endSequence === 'undefined') {
|
107 | endSequence = playlist.mediaSequence + playlist.segments.length;
|
108 | }
|
109 |
|
110 | if (endSequence < playlist.mediaSequence) {
|
111 | return 0;
|
112 | }
|
113 |
|
114 | // do a backward walk to estimate the duration
|
115 | backward = backwardDuration(playlist, endSequence);
|
116 | if (backward.precise) {
|
117 | // if we were able to base our duration estimate on timing
|
118 | // information provided directly from the Media Source, return
|
119 | // it
|
120 | return backward.result;
|
121 | }
|
122 |
|
123 | // walk forward to see if a precise duration estimate can be made
|
124 | // that way
|
125 | forward = forwardDuration(playlist, endSequence);
|
126 | if (forward.precise) {
|
127 | // we found a segment that has been buffered and so it's
|
128 | // position is known precisely
|
129 | return forward.result;
|
130 | }
|
131 |
|
132 | // return the less-precise, playlist-based duration estimate
|
133 | return backward.result + expired;
|
134 | };
|
135 |
|
136 | /**
|
137 | * Calculates the duration of a playlist. If a start and end index
|
138 | * are specified, the duration will be for the subset of the media
|
139 | * timeline between those two indices. The total duration for live
|
140 | * playlists is always Infinity.
|
141 | *
|
142 | * @param {Object} playlist a media playlist object
|
143 | * @param {Number=} endSequence an exclusive upper
|
144 | * boundary for the playlist. Defaults to the playlist media
|
145 | * sequence number plus its length.
|
146 | * @param {Number=} expired the amount of time that has
|
147 | * dropped off the front of the playlist in a live scenario
|
148 | * @return {Number} the duration between the start index and end
|
149 | * index.
|
150 | */
|
151 | export const duration = function(playlist, endSequence, expired) {
|
152 | if (!playlist) {
|
153 | return 0;
|
154 | }
|
155 |
|
156 | if (typeof expired !== 'number') {
|
157 | expired = 0;
|
158 | }
|
159 |
|
160 | // if a slice of the total duration is not requested, use
|
161 | // playlist-level duration indicators when they're present
|
162 | if (typeof endSequence === 'undefined') {
|
163 | // if present, use the duration specified in the playlist
|
164 | if (playlist.totalDuration) {
|
165 | return playlist.totalDuration;
|
166 | }
|
167 |
|
168 | // duration should be Infinity for live playlists
|
169 | if (!playlist.endList) {
|
170 | return window.Infinity;
|
171 | }
|
172 | }
|
173 |
|
174 | // calculate the total duration based on the segment durations
|
175 | return intervalDuration(playlist,
|
176 | endSequence,
|
177 | expired);
|
178 | };
|
179 |
|
180 | /**
|
181 | * Calculate the time between two indexes in the current playlist
|
182 | * neight the start- nor the end-index need to be within the current
|
183 | * playlist in which case, the targetDuration of the playlist is used
|
184 | * to approximate the durations of the segments
|
185 | *
|
186 | * @param {Object} playlist a media playlist object
|
187 | * @param {Number} startIndex
|
188 | * @param {Number} endIndex
|
189 | * @return {Number} the number of seconds between startIndex and endIndex
|
190 | */
|
191 | export const sumDurations = function(playlist, startIndex, endIndex) {
|
192 | let durations = 0;
|
193 |
|
194 | if (startIndex > endIndex) {
|
195 | [startIndex, endIndex] = [endIndex, startIndex];
|
196 | }
|
197 |
|
198 | if (startIndex < 0) {
|
199 | for (let i = startIndex; i < Math.min(0, endIndex); i++) {
|
200 | durations += playlist.targetDuration;
|
201 | }
|
202 | startIndex = 0;
|
203 | }
|
204 |
|
205 | for (let i = startIndex; i < endIndex; i++) {
|
206 | durations += playlist.segments[i].duration;
|
207 | }
|
208 |
|
209 | return durations;
|
210 | };
|
211 |
|
212 | /**
|
213 | * Determines the media index of the segment corresponding to the safe edge of the live
|
214 | * window which is the duration of the last segment plus 2 target durations from the end
|
215 | * of the playlist.
|
216 | *
|
217 | * @param {Object} playlist
|
218 | * a media playlist object
|
219 | * @return {Number}
|
220 | * The media index of the segment at the safe live point. 0 if there is no "safe"
|
221 | * point.
|
222 | * @function safeLiveIndex
|
223 | */
|
224 | export const safeLiveIndex = function(playlist) {
|
225 | if (!playlist.segments.length) {
|
226 | return 0;
|
227 | }
|
228 |
|
229 | let i = playlist.segments.length - 1;
|
230 | let distanceFromEnd = playlist.segments[i].duration || playlist.targetDuration;
|
231 | const safeDistance = distanceFromEnd + playlist.targetDuration * 2;
|
232 |
|
233 | while (i--) {
|
234 | distanceFromEnd += playlist.segments[i].duration;
|
235 |
|
236 | if (distanceFromEnd >= safeDistance) {
|
237 | break;
|
238 | }
|
239 | }
|
240 |
|
241 | return Math.max(0, i);
|
242 | };
|
243 |
|
244 | /**
|
245 | * Calculates the playlist end time
|
246 | *
|
247 | * @param {Object} playlist a media playlist object
|
248 | * @param {Number=} expired the amount of time that has
|
249 | * dropped off the front of the playlist in a live scenario
|
250 | * @param {Boolean|false} useSafeLiveEnd a boolean value indicating whether or not the
|
251 | * playlist end calculation should consider the safe live end
|
252 | * (truncate the playlist end by three segments). This is normally
|
253 | * used for calculating the end of the playlist's seekable range.
|
254 | * @returns {Number} the end time of playlist
|
255 | * @function playlistEnd
|
256 | */
|
257 | export const playlistEnd = function(playlist, expired, useSafeLiveEnd) {
|
258 | if (!playlist || !playlist.segments) {
|
259 | return null;
|
260 | }
|
261 | if (playlist.endList) {
|
262 | return duration(playlist);
|
263 | }
|
264 |
|
265 | if (expired === null) {
|
266 | return null;
|
267 | }
|
268 |
|
269 | expired = expired || 0;
|
270 |
|
271 | const endSequence = useSafeLiveEnd ? safeLiveIndex(playlist) : playlist.segments.length;
|
272 |
|
273 | return intervalDuration(playlist,
|
274 | playlist.mediaSequence + endSequence,
|
275 | expired);
|
276 | };
|
277 |
|
278 | /**
|
279 | * Calculates the interval of time that is currently seekable in a
|
280 | * playlist. The returned time ranges are relative to the earliest
|
281 | * moment in the specified playlist that is still available. A full
|
282 | * seekable implementation for live streams would need to offset
|
283 | * these values by the duration of content that has expired from the
|
284 | * stream.
|
285 | *
|
286 | * @param {Object} playlist a media playlist object
|
287 | * dropped off the front of the playlist in a live scenario
|
288 | * @param {Number=} expired the amount of time that has
|
289 | * dropped off the front of the playlist in a live scenario
|
290 | * @return {TimeRanges} the periods of time that are valid targets
|
291 | * for seeking
|
292 | */
|
293 | export const seekable = function(playlist, expired) {
|
294 | let useSafeLiveEnd = true;
|
295 | let seekableStart = expired || 0;
|
296 | let seekableEnd = playlistEnd(playlist, expired, useSafeLiveEnd);
|
297 |
|
298 | if (seekableEnd === null) {
|
299 | return createTimeRange();
|
300 | }
|
301 | return createTimeRange(seekableStart, seekableEnd);
|
302 | };
|
303 |
|
304 | const isWholeNumber = function(num) {
|
305 | return (num - Math.floor(num)) === 0;
|
306 | };
|
307 |
|
308 | const roundSignificantDigit = function(increment, num) {
|
309 | // If we have a whole number, just add 1 to it
|
310 | if (isWholeNumber(num)) {
|
311 | return num + (increment * 0.1);
|
312 | }
|
313 |
|
314 | let numDecimalDigits = num.toString().split('.')[1].length;
|
315 |
|
316 | for (let i = 1; i <= numDecimalDigits; i++) {
|
317 | let scale = Math.pow(10, i);
|
318 | let temp = num * scale;
|
319 |
|
320 | if (isWholeNumber(temp) ||
|
321 | i === numDecimalDigits) {
|
322 | return (temp + increment) / scale;
|
323 | }
|
324 | }
|
325 | };
|
326 |
|
327 | const ceilLeastSignificantDigit = roundSignificantDigit.bind(null, 1);
|
328 | const floorLeastSignificantDigit = roundSignificantDigit.bind(null, -1);
|
329 |
|
330 | /**
|
331 | * Determine the index and estimated starting time of the segment that
|
332 | * contains a specified playback position in a media playlist.
|
333 | *
|
334 | * @param {Object} playlist the media playlist to query
|
335 | * @param {Number} currentTime The number of seconds since the earliest
|
336 | * possible position to determine the containing segment for
|
337 | * @param {Number} startIndex
|
338 | * @param {Number} startTime
|
339 | * @return {Object}
|
340 | */
|
341 | export const getMediaInfoForTime = function(playlist,
|
342 | currentTime,
|
343 | startIndex,
|
344 | startTime) {
|
345 | let i;
|
346 | let segment;
|
347 | let numSegments = playlist.segments.length;
|
348 |
|
349 | let time = currentTime - startTime;
|
350 |
|
351 | if (time < 0) {
|
352 | // Walk backward from startIndex in the playlist, adding durations
|
353 | // until we find a segment that contains `time` and return it
|
354 | if (startIndex > 0) {
|
355 | for (i = startIndex - 1; i >= 0; i--) {
|
356 | segment = playlist.segments[i];
|
357 | time += floorLeastSignificantDigit(segment.duration);
|
358 | if (time > 0) {
|
359 | return {
|
360 | mediaIndex: i,
|
361 | startTime: startTime - sumDurations(playlist, startIndex, i)
|
362 | };
|
363 | }
|
364 | }
|
365 | }
|
366 | // We were unable to find a good segment within the playlist
|
367 | // so select the first segment
|
368 | return {
|
369 | mediaIndex: 0,
|
370 | startTime: currentTime
|
371 | };
|
372 | }
|
373 |
|
374 | // When startIndex is negative, we first walk forward to first segment
|
375 | // adding target durations. If we "run out of time" before getting to
|
376 | // the first segment, return the first segment
|
377 | if (startIndex < 0) {
|
378 | for (i = startIndex; i < 0; i++) {
|
379 | time -= playlist.targetDuration;
|
380 | if (time < 0) {
|
381 | return {
|
382 | mediaIndex: 0,
|
383 | startTime: currentTime
|
384 | };
|
385 | }
|
386 | }
|
387 | startIndex = 0;
|
388 | }
|
389 |
|
390 | // Walk forward from startIndex in the playlist, subtracting durations
|
391 | // until we find a segment that contains `time` and return it
|
392 | for (i = startIndex; i < numSegments; i++) {
|
393 | segment = playlist.segments[i];
|
394 | time -= ceilLeastSignificantDigit(segment.duration);
|
395 | if (time < 0) {
|
396 | return {
|
397 | mediaIndex: i,
|
398 | startTime: startTime + sumDurations(playlist, startIndex, i)
|
399 | };
|
400 | }
|
401 | }
|
402 |
|
403 | // We are out of possible candidates so load the last one...
|
404 | return {
|
405 | mediaIndex: numSegments - 1,
|
406 | startTime: currentTime
|
407 | };
|
408 | };
|
409 |
|
410 | /**
|
411 | * Check whether the playlist is blacklisted or not.
|
412 | *
|
413 | * @param {Object} playlist the media playlist object
|
414 | * @return {boolean} whether the playlist is blacklisted or not
|
415 | * @function isBlacklisted
|
416 | */
|
417 | export const isBlacklisted = function(playlist) {
|
418 | return playlist.excludeUntil && playlist.excludeUntil > Date.now();
|
419 | };
|
420 |
|
421 | /**
|
422 | * Check whether the playlist is compatible with current playback configuration or has
|
423 | * been blacklisted permanently for being incompatible.
|
424 | *
|
425 | * @param {Object} playlist the media playlist object
|
426 | * @return {boolean} whether the playlist is incompatible or not
|
427 | * @function isIncompatible
|
428 | */
|
429 | export const isIncompatible = function(playlist) {
|
430 | return playlist.excludeUntil && playlist.excludeUntil === Infinity;
|
431 | };
|
432 |
|
433 | /**
|
434 | * Check whether the playlist is enabled or not.
|
435 | *
|
436 | * @param {Object} playlist the media playlist object
|
437 | * @return {boolean} whether the playlist is enabled or not
|
438 | * @function isEnabled
|
439 | */
|
440 | export const isEnabled = function(playlist) {
|
441 | const blacklisted = isBlacklisted(playlist);
|
442 |
|
443 | return (!playlist.disabled && !blacklisted);
|
444 | };
|
445 |
|
446 | /**
|
447 | * Check whether the playlist has been manually disabled through the representations api.
|
448 | *
|
449 | * @param {Object} playlist the media playlist object
|
450 | * @return {boolean} whether the playlist is disabled manually or not
|
451 | * @function isDisabled
|
452 | */
|
453 | export const isDisabled = function(playlist) {
|
454 | return playlist.disabled;
|
455 | };
|
456 |
|
457 | /**
|
458 | * Returns whether the current playlist is an AES encrypted HLS stream
|
459 | *
|
460 | * @return {Boolean} true if it's an AES encrypted HLS stream
|
461 | */
|
462 | export const isAes = function(media) {
|
463 | for (let i = 0; i < media.segments.length; i++) {
|
464 | if (media.segments[i].key) {
|
465 | return true;
|
466 | }
|
467 | }
|
468 | return false;
|
469 | };
|
470 |
|
471 | /**
|
472 | * Returns whether the current playlist contains fMP4
|
473 | *
|
474 | * @return {Boolean} true if the playlist contains fMP4
|
475 | */
|
476 | export const isFmp4 = function(media) {
|
477 | for (let i = 0; i < media.segments.length; i++) {
|
478 | if (media.segments[i].map) {
|
479 | return true;
|
480 | }
|
481 | }
|
482 | return false;
|
483 | };
|
484 |
|
485 | /**
|
486 | * Checks if the playlist has a value for the specified attribute
|
487 | *
|
488 | * @param {String} attr
|
489 | * Attribute to check for
|
490 | * @param {Object} playlist
|
491 | * The media playlist object
|
492 | * @return {Boolean}
|
493 | * Whether the playlist contains a value for the attribute or not
|
494 | * @function hasAttribute
|
495 | */
|
496 | export const hasAttribute = function(attr, playlist) {
|
497 | return playlist.attributes && playlist.attributes[attr];
|
498 | };
|
499 |
|
500 | /**
|
501 | * Estimates the time required to complete a segment download from the specified playlist
|
502 | *
|
503 | * @param {Number} segmentDuration
|
504 | * Duration of requested segment
|
505 | * @param {Number} bandwidth
|
506 | * Current measured bandwidth of the player
|
507 | * @param {Object} playlist
|
508 | * The media playlist object
|
509 | * @param {Number=} bytesReceived
|
510 | * Number of bytes already received for the request. Defaults to 0
|
511 | * @return {Number|NaN}
|
512 | * The estimated time to request the segment. NaN if bandwidth information for
|
513 | * the given playlist is unavailable
|
514 | * @function estimateSegmentRequestTime
|
515 | */
|
516 | export const estimateSegmentRequestTime = function(segmentDuration,
|
517 | bandwidth,
|
518 | playlist,
|
519 | bytesReceived = 0) {
|
520 | if (!hasAttribute('BANDWIDTH', playlist)) {
|
521 | return NaN;
|
522 | }
|
523 |
|
524 | const size = segmentDuration * playlist.attributes.BANDWIDTH;
|
525 |
|
526 | return (size - (bytesReceived * 8)) / bandwidth;
|
527 | };
|
528 |
|
529 | // exports
|
530 | export default {
|
531 | duration,
|
532 | seekable,
|
533 | safeLiveIndex,
|
534 | getMediaInfoForTime,
|
535 | isEnabled,
|
536 | isDisabled,
|
537 | isBlacklisted,
|
538 | isIncompatible,
|
539 | playlistEnd,
|
540 | isAes,
|
541 | isFmp4,
|
542 | hasAttribute,
|
543 | estimateSegmentRequestTime
|
544 | };
|