1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 | 'use strict';
|
12 |
|
13 | Object.defineProperty(exports, '__esModule', {
|
14 | value: true
|
15 | });
|
16 |
|
17 | var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
|
18 |
|
19 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; }
|
20 |
|
21 | function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
|
22 |
|
23 | var _globalWindow = require('global/window');
|
24 |
|
25 | var _globalWindow2 = _interopRequireDefault(_globalWindow);
|
26 |
|
27 | var _ranges = require('./ranges');
|
28 |
|
29 | var _ranges2 = _interopRequireDefault(_ranges);
|
30 |
|
31 | var _videoJs = require('video.js');
|
32 |
|
33 | var _videoJs2 = _interopRequireDefault(_videoJs);
|
34 |
|
35 |
|
36 | var timerCancelEvents = ['seeking', 'seeked', 'pause', 'playing', 'error'];
|
37 |
|
38 |
|
39 |
|
40 |
|
41 |
|
42 | var PlaybackWatcher = (function () {
|
43 | |
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 | function PlaybackWatcher(options) {
|
50 | var _this = this;
|
51 |
|
52 | _classCallCheck(this, PlaybackWatcher);
|
53 |
|
54 | this.tech_ = options.tech;
|
55 | this.seekable = options.seekable;
|
56 |
|
57 | this.consecutiveUpdates = 0;
|
58 | this.lastRecordedTime = null;
|
59 | this.timer_ = null;
|
60 | this.checkCurrentTimeTimeout_ = null;
|
61 |
|
62 | if (options.debug) {
|
63 | this.logger_ = _videoJs2['default'].log.bind(_videoJs2['default'], 'playback-watcher ->');
|
64 | }
|
65 | this.logger_('initialize');
|
66 |
|
67 | var waitingHandler = function waitingHandler() {
|
68 | return _this.techWaiting_();
|
69 | };
|
70 | var cancelTimerHandler = function cancelTimerHandler() {
|
71 | return _this.cancelTimer_();
|
72 | };
|
73 | var fixesBadSeeksHandler = function fixesBadSeeksHandler() {
|
74 | return _this.fixesBadSeeks_();
|
75 | };
|
76 |
|
77 | this.tech_.on('seekablechanged', fixesBadSeeksHandler);
|
78 | this.tech_.on('waiting', waitingHandler);
|
79 | this.tech_.on(timerCancelEvents, cancelTimerHandler);
|
80 | this.monitorCurrentTime_();
|
81 |
|
82 |
|
83 | this.dispose = function () {
|
84 | _this.logger_('dispose');
|
85 | _this.tech_.off('seekablechanged', fixesBadSeeksHandler);
|
86 | _this.tech_.off('waiting', waitingHandler);
|
87 | _this.tech_.off(timerCancelEvents, cancelTimerHandler);
|
88 | if (_this.checkCurrentTimeTimeout_) {
|
89 | _globalWindow2['default'].clearTimeout(_this.checkCurrentTimeTimeout_);
|
90 | }
|
91 | _this.cancelTimer_();
|
92 | };
|
93 | }
|
94 |
|
95 | |
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | _createClass(PlaybackWatcher, [{
|
102 | key: 'monitorCurrentTime_',
|
103 | value: function monitorCurrentTime_() {
|
104 | this.checkCurrentTime_();
|
105 |
|
106 | if (this.checkCurrentTimeTimeout_) {
|
107 | _globalWindow2['default'].clearTimeout(this.checkCurrentTimeTimeout_);
|
108 | }
|
109 |
|
110 |
|
111 | this.checkCurrentTimeTimeout_ = _globalWindow2['default'].setTimeout(this.monitorCurrentTime_.bind(this), 250);
|
112 | }
|
113 |
|
114 | |
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 | }, {
|
122 | key: 'checkCurrentTime_',
|
123 | value: function checkCurrentTime_() {
|
124 | if (this.tech_.seeking() && this.fixesBadSeeks_()) {
|
125 | this.consecutiveUpdates = 0;
|
126 | this.lastRecordedTime = this.tech_.currentTime();
|
127 | return;
|
128 | }
|
129 |
|
130 | if (this.tech_.paused() || this.tech_.seeking()) {
|
131 | return;
|
132 | }
|
133 |
|
134 | var currentTime = this.tech_.currentTime();
|
135 | var buffered = this.tech_.buffered();
|
136 |
|
137 | if (this.lastRecordedTime === currentTime && (!buffered.length || currentTime + 0.1 >= buffered.end(buffered.length - 1))) {
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 | return this.techWaiting_();
|
146 | }
|
147 |
|
148 | if (this.consecutiveUpdates >= 5 && currentTime === this.lastRecordedTime) {
|
149 | this.consecutiveUpdates++;
|
150 | this.waiting_();
|
151 | } else if (currentTime === this.lastRecordedTime) {
|
152 | this.consecutiveUpdates++;
|
153 | } else {
|
154 | this.consecutiveUpdates = 0;
|
155 | this.lastRecordedTime = currentTime;
|
156 | }
|
157 | }
|
158 |
|
159 | |
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 | }, {
|
166 | key: 'cancelTimer_',
|
167 | value: function cancelTimer_() {
|
168 | this.consecutiveUpdates = 0;
|
169 |
|
170 | if (this.timer_) {
|
171 | this.logger_('cancelTimer_');
|
172 | clearTimeout(this.timer_);
|
173 | }
|
174 |
|
175 | this.timer_ = null;
|
176 | }
|
177 |
|
178 | |
179 |
|
180 |
|
181 |
|
182 |
|
183 |
|
184 | }, {
|
185 | key: 'fixesBadSeeks_',
|
186 | value: function fixesBadSeeks_() {
|
187 | var seekable = this.seekable();
|
188 | var currentTime = this.tech_.currentTime();
|
189 |
|
190 | if (this.tech_.seeking() && this.outsideOfSeekableWindow_(seekable, currentTime)) {
|
191 | var seekableEnd = seekable.end(seekable.length - 1);
|
192 |
|
193 |
|
194 | this.logger_('Trying to seek outside of seekable at time ' + currentTime + ' with ' + ('seekable range ' + _ranges2['default'].printableRange(seekable) + '. Seeking to ') + (seekableEnd + '.'));
|
195 | this.tech_.setCurrentTime(seekableEnd);
|
196 | return true;
|
197 | }
|
198 |
|
199 | return false;
|
200 | }
|
201 |
|
202 | |
203 |
|
204 |
|
205 |
|
206 |
|
207 | }, {
|
208 | key: 'waiting_',
|
209 | value: function waiting_() {
|
210 | if (this.techWaiting_()) {
|
211 | return;
|
212 | }
|
213 |
|
214 |
|
215 | var currentTime = this.tech_.currentTime();
|
216 | var buffered = this.tech_.buffered();
|
217 | var currentRange = _ranges2['default'].findRange(buffered, currentTime);
|
218 |
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 | if (currentRange.length && currentTime + 3 <= currentRange.end(0)) {
|
228 | this.cancelTimer_();
|
229 | this.tech_.setCurrentTime(currentTime);
|
230 |
|
231 | this.logger_('Stopped at ' + currentTime + ' while inside a buffered region ' + ('[' + currentRange.start(0) + ' -> ' + currentRange.end(0) + ']. Attempting to resume ') + 'playback by seeking to the current time.');
|
232 |
|
233 |
|
234 | this.tech_.trigger({ type: 'usage', name: 'hls-unknown-waiting' });
|
235 | return;
|
236 | }
|
237 | }
|
238 |
|
239 | |
240 |
|
241 |
|
242 |
|
243 |
|
244 |
|
245 |
|
246 |
|
247 | }, {
|
248 | key: 'techWaiting_',
|
249 | value: function techWaiting_() {
|
250 | var seekable = this.seekable();
|
251 | var currentTime = this.tech_.currentTime();
|
252 |
|
253 | if (this.tech_.seeking() && this.fixesBadSeeks_()) {
|
254 |
|
255 | return true;
|
256 | }
|
257 |
|
258 | if (this.tech_.seeking() || this.timer_ !== null) {
|
259 |
|
260 | return true;
|
261 | }
|
262 |
|
263 | if (this.fellOutOfLiveWindow_(seekable, currentTime)) {
|
264 | var livePoint = seekable.end(seekable.length - 1);
|
265 |
|
266 | this.logger_('Fell out of live window at time ' + currentTime + '. Seeking to ' + ('live point (seekable end) ' + livePoint));
|
267 | this.cancelTimer_();
|
268 | this.tech_.setCurrentTime(livePoint);
|
269 |
|
270 |
|
271 | this.tech_.trigger({ type: 'usage', name: 'hls-live-resync' });
|
272 | return true;
|
273 | }
|
274 |
|
275 | var buffered = this.tech_.buffered();
|
276 | var nextRange = _ranges2['default'].findNextRange(buffered, currentTime);
|
277 |
|
278 | if (this.videoUnderflow_(nextRange, buffered, currentTime)) {
|
279 |
|
280 |
|
281 |
|
282 |
|
283 | this.cancelTimer_();
|
284 | this.tech_.setCurrentTime(currentTime);
|
285 |
|
286 |
|
287 | this.tech_.trigger({ type: 'usage', name: 'hls-video-underflow' });
|
288 | return true;
|
289 | }
|
290 |
|
291 |
|
292 | if (nextRange.length > 0) {
|
293 | var difference = nextRange.start(0) - currentTime;
|
294 |
|
295 | this.logger_('Stopped at ' + currentTime + ', setting timer for ' + difference + ', seeking ' + ('to ' + nextRange.start(0)));
|
296 |
|
297 | this.timer_ = setTimeout(this.skipTheGap_.bind(this), difference * 1000, currentTime);
|
298 | return true;
|
299 | }
|
300 |
|
301 |
|
302 | return false;
|
303 | }
|
304 | }, {
|
305 | key: 'outsideOfSeekableWindow_',
|
306 | value: function outsideOfSeekableWindow_(seekable, currentTime) {
|
307 | if (!seekable.length) {
|
308 |
|
309 | return false;
|
310 | }
|
311 |
|
312 |
|
313 | if (currentTime < seekable.start(0) - 0.1 || currentTime > seekable.end(seekable.length - 1) + 0.1) {
|
314 | return true;
|
315 | }
|
316 |
|
317 | return false;
|
318 | }
|
319 | }, {
|
320 | key: 'fellOutOfLiveWindow_',
|
321 | value: function fellOutOfLiveWindow_(seekable, currentTime) {
|
322 | if (seekable.length &&
|
323 |
|
324 | seekable.start(0) > 0 && currentTime < seekable.start(0)) {
|
325 | return true;
|
326 | }
|
327 |
|
328 | return false;
|
329 | }
|
330 | }, {
|
331 | key: 'videoUnderflow_',
|
332 | value: function videoUnderflow_(nextRange, buffered, currentTime) {
|
333 | if (nextRange.length === 0) {
|
334 |
|
335 |
|
336 | var gap = this.gapFromVideoUnderflow_(buffered, currentTime);
|
337 |
|
338 | if (gap) {
|
339 | this.logger_('Encountered a gap in video from ' + gap.start + ' to ' + gap.end + '. ' + ('Seeking to current time ' + currentTime));
|
340 |
|
341 | return true;
|
342 | }
|
343 | }
|
344 |
|
345 | return false;
|
346 | }
|
347 |
|
348 | |
349 |
|
350 |
|
351 |
|
352 |
|
353 |
|
354 | }, {
|
355 | key: 'skipTheGap_',
|
356 | value: function skipTheGap_(scheduledCurrentTime) {
|
357 | var buffered = this.tech_.buffered();
|
358 | var currentTime = this.tech_.currentTime();
|
359 | var nextRange = _ranges2['default'].findNextRange(buffered, currentTime);
|
360 |
|
361 | this.cancelTimer_();
|
362 |
|
363 | if (nextRange.length === 0 || currentTime !== scheduledCurrentTime) {
|
364 | return;
|
365 | }
|
366 |
|
367 | this.logger_('skipTheGap_:', 'currentTime:', currentTime, 'scheduled currentTime:', scheduledCurrentTime, 'nextRange start:', nextRange.start(0));
|
368 |
|
369 |
|
370 | this.tech_.setCurrentTime(nextRange.start(0) + _ranges2['default'].TIME_FUDGE_FACTOR);
|
371 |
|
372 | this.tech_.trigger({ type: 'usage', name: 'hls-gap-skip' });
|
373 | }
|
374 | }, {
|
375 | key: 'gapFromVideoUnderflow_',
|
376 | value: function gapFromVideoUnderflow_(buffered, currentTime) {
|
377 |
|
378 |
|
379 |
|
380 |
|
381 |
|
382 |
|
383 |
|
384 |
|
385 |
|
386 |
|
387 |
|
388 |
|
389 |
|
390 |
|
391 |
|
392 |
|
393 |
|
394 |
|
395 |
|
396 |
|
397 |
|
398 |
|
399 | var gaps = _ranges2['default'].findGaps(buffered);
|
400 |
|
401 | for (var i = 0; i < gaps.length; i++) {
|
402 | var start = gaps.start(i);
|
403 | var end = gaps.end(i);
|
404 |
|
405 |
|
406 | if (currentTime - start < 4 && currentTime - start > 2) {
|
407 | return {
|
408 | start: start,
|
409 | end: end
|
410 | };
|
411 | }
|
412 | }
|
413 |
|
414 | return null;
|
415 | }
|
416 |
|
417 | |
418 |
|
419 |
|
420 |
|
421 |
|
422 |
|
423 | }, {
|
424 | key: 'logger_',
|
425 | value: function logger_() {}
|
426 | }]);
|
427 |
|
428 | return PlaybackWatcher;
|
429 | })();
|
430 |
|
431 | exports['default'] = PlaybackWatcher;
|
432 | module.exports = exports['default']; |
\ | No newline at end of file |