1 |
|
2 |
|
3 |
|
4 |
|
5 | import {sumDurations, getPartsAndSegments} from './playlist';
|
6 | import videojs from 'video.js';
|
7 | import logger from './util/logger';
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | const MAX_MEDIA_SEQUENCE_DIFF_FOR_SYNC = 86400;
|
15 |
|
16 | export const syncPointStrategies = [
|
17 |
|
18 |
|
19 | {
|
20 | name: 'VOD',
|
21 | run: (syncController, playlist, duration, currentTimeline, currentTime) => {
|
22 | if (duration !== Infinity) {
|
23 | const syncPoint = {
|
24 | time: 0,
|
25 | segmentIndex: 0,
|
26 | partIndex: null
|
27 | };
|
28 |
|
29 | return syncPoint;
|
30 | }
|
31 | return null;
|
32 | }
|
33 | },
|
34 |
|
35 | {
|
36 | name: 'ProgramDateTime',
|
37 | run: (syncController, playlist, duration, currentTimeline, currentTime) => {
|
38 | if (!Object.keys(syncController.timelineToDatetimeMappings).length) {
|
39 | return null;
|
40 | }
|
41 |
|
42 | let syncPoint = null;
|
43 | let lastDistance = null;
|
44 | const partsAndSegments = getPartsAndSegments(playlist);
|
45 |
|
46 | currentTime = currentTime || 0;
|
47 | for (let i = 0; i < partsAndSegments.length; i++) {
|
48 |
|
49 |
|
50 | const index = (playlist.endList || currentTime === 0) ? i : partsAndSegments.length - (i + 1);
|
51 | const partAndSegment = partsAndSegments[index];
|
52 | const segment = partAndSegment.segment;
|
53 | const datetimeMapping =
|
54 | syncController.timelineToDatetimeMappings[segment.timeline];
|
55 |
|
56 | if (!datetimeMapping) {
|
57 | continue;
|
58 | }
|
59 |
|
60 | if (segment.dateTimeObject) {
|
61 | const segmentTime = segment.dateTimeObject.getTime() / 1000;
|
62 | let start = segmentTime + datetimeMapping;
|
63 |
|
64 |
|
65 | if (segment.parts && typeof partAndSegment.partIndex === 'number') {
|
66 | for (let z = 0; z < partAndSegment.partIndex; z++) {
|
67 | start += segment.parts[z].duration;
|
68 | }
|
69 | }
|
70 | const distance = Math.abs(currentTime - start);
|
71 |
|
72 |
|
73 |
|
74 | if (lastDistance !== null && (distance === 0 || lastDistance < distance)) {
|
75 | break;
|
76 | }
|
77 |
|
78 | lastDistance = distance;
|
79 | syncPoint = {
|
80 | time: start,
|
81 | segmentIndex: partAndSegment.segmentIndex,
|
82 | partIndex: partAndSegment.partIndex
|
83 | };
|
84 | }
|
85 | }
|
86 | return syncPoint;
|
87 | }
|
88 | },
|
89 |
|
90 |
|
91 | {
|
92 | name: 'Segment',
|
93 | run: (syncController, playlist, duration, currentTimeline, currentTime) => {
|
94 | let syncPoint = null;
|
95 | let lastDistance = null;
|
96 |
|
97 | currentTime = currentTime || 0;
|
98 | const partsAndSegments = getPartsAndSegments(playlist);
|
99 |
|
100 | for (let i = 0; i < partsAndSegments.length; i++) {
|
101 |
|
102 |
|
103 | const index = (playlist.endList || currentTime === 0) ? i : partsAndSegments.length - (i + 1);
|
104 | const partAndSegment = partsAndSegments[index];
|
105 | const segment = partAndSegment.segment;
|
106 | const start = partAndSegment.part && partAndSegment.part.start || segment && segment.start;
|
107 |
|
108 | if (segment.timeline === currentTimeline && typeof start !== 'undefined') {
|
109 | const distance = Math.abs(currentTime - start);
|
110 |
|
111 |
|
112 |
|
113 | if (lastDistance !== null && lastDistance < distance) {
|
114 | break;
|
115 | }
|
116 |
|
117 | if (!syncPoint || lastDistance === null || lastDistance >= distance) {
|
118 | lastDistance = distance;
|
119 | syncPoint = {
|
120 | time: start,
|
121 | segmentIndex: partAndSegment.segmentIndex,
|
122 | partIndex: partAndSegment.partIndex
|
123 | };
|
124 | }
|
125 |
|
126 | }
|
127 | }
|
128 | return syncPoint;
|
129 | }
|
130 | },
|
131 |
|
132 |
|
133 | {
|
134 | name: 'Discontinuity',
|
135 | run: (syncController, playlist, duration, currentTimeline, currentTime) => {
|
136 | let syncPoint = null;
|
137 |
|
138 | currentTime = currentTime || 0;
|
139 |
|
140 | if (playlist.discontinuityStarts && playlist.discontinuityStarts.length) {
|
141 | let lastDistance = null;
|
142 |
|
143 | for (let i = 0; i < playlist.discontinuityStarts.length; i++) {
|
144 | const segmentIndex = playlist.discontinuityStarts[i];
|
145 | const discontinuity = playlist.discontinuitySequence + i + 1;
|
146 | const discontinuitySync = syncController.discontinuities[discontinuity];
|
147 |
|
148 | if (discontinuitySync) {
|
149 | const distance = Math.abs(currentTime - discontinuitySync.time);
|
150 |
|
151 |
|
152 |
|
153 | if (lastDistance !== null && lastDistance < distance) {
|
154 | break;
|
155 | }
|
156 |
|
157 | if (!syncPoint || lastDistance === null || lastDistance >= distance) {
|
158 | lastDistance = distance;
|
159 | syncPoint = {
|
160 | time: discontinuitySync.time,
|
161 | segmentIndex,
|
162 | partIndex: null
|
163 | };
|
164 | }
|
165 | }
|
166 | }
|
167 | }
|
168 | return syncPoint;
|
169 | }
|
170 | },
|
171 |
|
172 |
|
173 | {
|
174 | name: 'Playlist',
|
175 | run: (syncController, playlist, duration, currentTimeline, currentTime) => {
|
176 | if (playlist.syncInfo) {
|
177 | const syncPoint = {
|
178 | time: playlist.syncInfo.time,
|
179 | segmentIndex: playlist.syncInfo.mediaSequence - playlist.mediaSequence,
|
180 | partIndex: null
|
181 | };
|
182 |
|
183 | return syncPoint;
|
184 | }
|
185 | return null;
|
186 | }
|
187 | }
|
188 | ];
|
189 |
|
190 | export default class SyncController extends videojs.EventTarget {
|
191 | constructor(options = {}) {
|
192 | super();
|
193 |
|
194 | this.timelines = [];
|
195 | this.discontinuities = [];
|
196 | this.timelineToDatetimeMappings = {};
|
197 |
|
198 | this.logger_ = logger('SyncController');
|
199 | }
|
200 |
|
201 | |
202 |
|
203 |
|
204 |
|
205 |
|
206 |
|
207 |
|
208 |
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 | getSyncPoint(playlist, duration, currentTimeline, currentTime) {
|
217 | const syncPoints = this.runStrategies_(
|
218 | playlist,
|
219 | duration,
|
220 | currentTimeline,
|
221 | currentTime
|
222 | );
|
223 |
|
224 | if (!syncPoints.length) {
|
225 |
|
226 |
|
227 |
|
228 | return null;
|
229 | }
|
230 |
|
231 |
|
232 |
|
233 |
|
234 | return this.selectSyncPoint_(syncPoints, { key: 'time', value: currentTime });
|
235 | }
|
236 |
|
237 | |
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 |
|
248 | getExpiredTime(playlist, duration) {
|
249 | if (!playlist || !playlist.segments) {
|
250 | return null;
|
251 | }
|
252 |
|
253 | const syncPoints = this.runStrategies_(
|
254 | playlist,
|
255 | duration,
|
256 | playlist.discontinuitySequence,
|
257 | 0
|
258 | );
|
259 |
|
260 |
|
261 | if (!syncPoints.length) {
|
262 | return null;
|
263 | }
|
264 |
|
265 | const syncPoint = this.selectSyncPoint_(syncPoints, {
|
266 | key: 'segmentIndex',
|
267 | value: 0
|
268 | });
|
269 |
|
270 |
|
271 |
|
272 | if (syncPoint.segmentIndex > 0) {
|
273 | syncPoint.time *= -1;
|
274 | }
|
275 |
|
276 | return Math.abs(syncPoint.time + sumDurations({
|
277 | defaultDuration: playlist.targetDuration,
|
278 | durationList: playlist.segments,
|
279 | startIndex: syncPoint.segmentIndex,
|
280 | endIndex: 0
|
281 | }));
|
282 | }
|
283 |
|
284 | |
285 |
|
286 |
|
287 |
|
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 | runStrategies_(playlist, duration, currentTimeline, currentTime) {
|
299 | const syncPoints = [];
|
300 |
|
301 |
|
302 | for (let i = 0; i < syncPointStrategies.length; i++) {
|
303 | const strategy = syncPointStrategies[i];
|
304 | const syncPoint = strategy.run(
|
305 | this,
|
306 | playlist,
|
307 | duration,
|
308 | currentTimeline,
|
309 | currentTime
|
310 | );
|
311 |
|
312 | if (syncPoint) {
|
313 | syncPoint.strategy = strategy.name;
|
314 | syncPoints.push({
|
315 | strategy: strategy.name,
|
316 | syncPoint
|
317 | });
|
318 | }
|
319 | }
|
320 |
|
321 | return syncPoints;
|
322 | }
|
323 |
|
324 | |
325 |
|
326 |
|
327 |
|
328 |
|
329 |
|
330 |
|
331 |
|
332 |
|
333 |
|
334 |
|
335 |
|
336 |
|
337 |
|
338 |
|
339 | selectSyncPoint_(syncPoints, target) {
|
340 | let bestSyncPoint = syncPoints[0].syncPoint;
|
341 | let bestDistance = Math.abs(syncPoints[0].syncPoint[target.key] - target.value);
|
342 | let bestStrategy = syncPoints[0].strategy;
|
343 |
|
344 | for (let i = 1; i < syncPoints.length; i++) {
|
345 | const newDistance = Math.abs(syncPoints[i].syncPoint[target.key] - target.value);
|
346 |
|
347 | if (newDistance < bestDistance) {
|
348 | bestDistance = newDistance;
|
349 | bestSyncPoint = syncPoints[i].syncPoint;
|
350 | bestStrategy = syncPoints[i].strategy;
|
351 | }
|
352 | }
|
353 |
|
354 | this.logger_(`syncPoint for [${target.key}: ${target.value}] chosen with strategy` +
|
355 | ` [${bestStrategy}]: [time:${bestSyncPoint.time},` +
|
356 | ` segmentIndex:${bestSyncPoint.segmentIndex}` +
|
357 | (typeof bestSyncPoint.partIndex === 'number' ? `,partIndex:${bestSyncPoint.partIndex}` : '') +
|
358 | ']');
|
359 |
|
360 | return bestSyncPoint;
|
361 | }
|
362 |
|
363 | |
364 |
|
365 |
|
366 |
|
367 |
|
368 |
|
369 |
|
370 |
|
371 | saveExpiredSegmentInfo(oldPlaylist, newPlaylist) {
|
372 | const mediaSequenceDiff = newPlaylist.mediaSequence - oldPlaylist.mediaSequence;
|
373 |
|
374 |
|
375 | if (mediaSequenceDiff > MAX_MEDIA_SEQUENCE_DIFF_FOR_SYNC) {
|
376 | videojs.log.warn(`Not saving expired segment info. Media sequence gap ${mediaSequenceDiff} is too large.`);
|
377 | return;
|
378 | }
|
379 |
|
380 |
|
381 |
|
382 | for (let i = mediaSequenceDiff - 1; i >= 0; i--) {
|
383 | const lastRemovedSegment = oldPlaylist.segments[i];
|
384 |
|
385 | if (lastRemovedSegment && typeof lastRemovedSegment.start !== 'undefined') {
|
386 | newPlaylist.syncInfo = {
|
387 | mediaSequence: oldPlaylist.mediaSequence + i,
|
388 | time: lastRemovedSegment.start
|
389 | };
|
390 | this.logger_(`playlist refresh sync: [time:${newPlaylist.syncInfo.time},` +
|
391 | ` mediaSequence: ${newPlaylist.syncInfo.mediaSequence}]`);
|
392 | this.trigger('syncinfoupdate');
|
393 | break;
|
394 | }
|
395 | }
|
396 | }
|
397 |
|
398 | |
399 |
|
400 |
|
401 |
|
402 |
|
403 |
|
404 | setDateTimeMappingForStart(playlist) {
|
405 |
|
406 |
|
407 |
|
408 |
|
409 | this.timelineToDatetimeMappings = {};
|
410 |
|
411 | if (playlist.segments &&
|
412 | playlist.segments.length &&
|
413 | playlist.segments[0].dateTimeObject) {
|
414 | const firstSegment = playlist.segments[0];
|
415 | const playlistTimestamp = firstSegment.dateTimeObject.getTime() / 1000;
|
416 |
|
417 | this.timelineToDatetimeMappings[firstSegment.timeline] = -playlistTimestamp;
|
418 | }
|
419 | }
|
420 |
|
421 | |
422 |
|
423 |
|
424 |
|
425 |
|
426 |
|
427 |
|
428 |
|
429 |
|
430 |
|
431 |
|
432 |
|
433 | saveSegmentTimingInfo({ segmentInfo, shouldSaveTimelineMapping }) {
|
434 | const didCalculateSegmentTimeMapping = this.calculateSegmentTimeMapping_(
|
435 | segmentInfo,
|
436 | segmentInfo.timingInfo,
|
437 | shouldSaveTimelineMapping
|
438 | );
|
439 | const segment = segmentInfo.segment;
|
440 |
|
441 | if (didCalculateSegmentTimeMapping) {
|
442 | this.saveDiscontinuitySyncInfo_(segmentInfo);
|
443 |
|
444 |
|
445 |
|
446 | if (!segmentInfo.playlist.syncInfo) {
|
447 | segmentInfo.playlist.syncInfo = {
|
448 | mediaSequence: segmentInfo.playlist.mediaSequence + segmentInfo.mediaIndex,
|
449 | time: segment.start
|
450 | };
|
451 | }
|
452 | }
|
453 |
|
454 | const dateTime = segment.dateTimeObject;
|
455 |
|
456 | if (segment.discontinuity && shouldSaveTimelineMapping && dateTime) {
|
457 | this.timelineToDatetimeMappings[segment.timeline] = -(dateTime.getTime() / 1000);
|
458 | }
|
459 | }
|
460 |
|
461 | timestampOffsetForTimeline(timeline) {
|
462 | if (typeof this.timelines[timeline] === 'undefined') {
|
463 | return null;
|
464 | }
|
465 | return this.timelines[timeline].time;
|
466 | }
|
467 |
|
468 | mappingForTimeline(timeline) {
|
469 | if (typeof this.timelines[timeline] === 'undefined') {
|
470 | return null;
|
471 | }
|
472 | return this.timelines[timeline].mapping;
|
473 | }
|
474 |
|
475 | |
476 |
|
477 |
|
478 |
|
479 |
|
480 |
|
481 |
|
482 |
|
483 |
|
484 |
|
485 |
|
486 |
|
487 |
|
488 |
|
489 |
|
490 | calculateSegmentTimeMapping_(segmentInfo, timingInfo, shouldSaveTimelineMapping) {
|
491 |
|
492 | const segment = segmentInfo.segment;
|
493 | const part = segmentInfo.part;
|
494 | let mappingObj = this.timelines[segmentInfo.timeline];
|
495 | let start;
|
496 | let end;
|
497 |
|
498 | if (typeof segmentInfo.timestampOffset === 'number') {
|
499 | mappingObj = {
|
500 | time: segmentInfo.startOfSegment,
|
501 | mapping: segmentInfo.startOfSegment - timingInfo.start
|
502 | };
|
503 | if (shouldSaveTimelineMapping) {
|
504 | this.timelines[segmentInfo.timeline] = mappingObj;
|
505 | this.trigger('timestampoffset');
|
506 |
|
507 | this.logger_(`time mapping for timeline ${segmentInfo.timeline}: ` +
|
508 | `[time: ${mappingObj.time}] [mapping: ${mappingObj.mapping}]`);
|
509 | }
|
510 |
|
511 | start = segmentInfo.startOfSegment;
|
512 | end = timingInfo.end + mappingObj.mapping;
|
513 |
|
514 | } else if (mappingObj) {
|
515 | start = timingInfo.start + mappingObj.mapping;
|
516 | end = timingInfo.end + mappingObj.mapping;
|
517 | } else {
|
518 | return false;
|
519 | }
|
520 |
|
521 | if (part) {
|
522 | part.start = start;
|
523 | part.end = end;
|
524 | }
|
525 |
|
526 |
|
527 |
|
528 |
|
529 |
|
530 |
|
531 | if (!segment.start || start < segment.start) {
|
532 | segment.start = start;
|
533 | }
|
534 | segment.end = end;
|
535 |
|
536 | return true;
|
537 | }
|
538 |
|
539 | |
540 |
|
541 |
|
542 |
|
543 |
|
544 |
|
545 |
|
546 |
|
547 | saveDiscontinuitySyncInfo_(segmentInfo) {
|
548 | const playlist = segmentInfo.playlist;
|
549 | const segment = segmentInfo.segment;
|
550 |
|
551 |
|
552 |
|
553 |
|
554 | if (segment.discontinuity) {
|
555 | this.discontinuities[segment.timeline] = {
|
556 | time: segment.start,
|
557 | accuracy: 0
|
558 | };
|
559 | } else if (playlist.discontinuityStarts && playlist.discontinuityStarts.length) {
|
560 |
|
561 |
|
562 | for (let i = 0; i < playlist.discontinuityStarts.length; i++) {
|
563 | const segmentIndex = playlist.discontinuityStarts[i];
|
564 | const discontinuity = playlist.discontinuitySequence + i + 1;
|
565 | const mediaIndexDiff = segmentIndex - segmentInfo.mediaIndex;
|
566 | const accuracy = Math.abs(mediaIndexDiff);
|
567 |
|
568 | if (!this.discontinuities[discontinuity] ||
|
569 | this.discontinuities[discontinuity].accuracy > accuracy) {
|
570 | let time;
|
571 |
|
572 | if (mediaIndexDiff < 0) {
|
573 | time = segment.start - sumDurations({
|
574 | defaultDuration: playlist.targetDuration,
|
575 | durationList: playlist.segments,
|
576 | startIndex: segmentInfo.mediaIndex,
|
577 | endIndex: segmentIndex
|
578 | });
|
579 | } else {
|
580 | time = segment.end + sumDurations({
|
581 | defaultDuration: playlist.targetDuration,
|
582 | durationList: playlist.segments,
|
583 | startIndex: segmentInfo.mediaIndex + 1,
|
584 | endIndex: segmentIndex
|
585 | });
|
586 | }
|
587 |
|
588 | this.discontinuities[discontinuity] = {
|
589 | time,
|
590 | accuracy
|
591 | };
|
592 | }
|
593 | }
|
594 | }
|
595 | }
|
596 |
|
597 | dispose() {
|
598 | this.trigger('dispose');
|
599 | this.off();
|
600 | }
|
601 | }
|