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