/
html5.js
764 lines (644 loc) · 23.6 KB
/
html5.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
define([
'utils/css',
'utils/helpers',
'utils/stretching',
'utils/underscore',
'events/events',
'events/states',
'providers/default',
'utils/backbone.events'
], function(cssUtils, utils, stretchUtils, _, events, states, DefaultProvider, Events) {
var clearTimeout = window.clearTimeout,
STALL_DELAY = 256,
_isIE = utils.isMSIE(),
_isMobile = utils.isMobile(),
_isSafari = utils.isSafari(),
_isAndroid = utils.isAndroidNative(),
_isIOS7 = utils.isIOS(7),
_name = 'html5';
function _setupListeners(eventsHash, videoTag) {
utils.foreach(eventsHash, function(evt, evtCallback) {
videoTag.addEventListener(evt, evtCallback, false);
});
}
function _removeListeners(eventsHash, videoTag) {
utils.foreach(eventsHash, function(evt, evtCallback) {
videoTag.removeEventListener(evt, evtCallback, false);
});
}
function _useAndroidHLS(source) {
if (source.type === 'hls') {
//when androidhls is not set to false, allow HLS playback on Android 4.1 and up
if (source.androidhls !== false) {
var isAndroidNative = utils.isAndroidNative;
if (isAndroidNative(2) || isAndroidNative(3) || isAndroidNative('4.0')) {
return false;
} else if (utils.isAndroid()) { //utils.isAndroidNative()) {
// skip canPlayType check
// canPlayType returns '' in native browser even though HLS will play
return true;
}
} else if (utils.isAndroid()) {
return false;
}
}
return null;
}
function VideoProvider(_playerId, _playerConfig) {
// Current media state
this.state = states.IDLE;
// Are we buffering due to seek, or due to playback?
this.seeking = false;
_.extend(this, Events);
// Overwrite the event dispatchers to block on certain occasions
this.trigger = function(type, args) {
if (!_attached) {
return;
}
return Events.trigger.call(this, type, args);
};
var _this = this,
_mediaEvents = {
//abort: _generalHandler,
click : _clickHandler,
durationchange: _durationChangeHandler,
//emptied: _generalHandler,
ended: _endedHandler,
error: _errorHandler,
//play: _onPlayHandler, // play is attempted, but hasn't necessarily started
//loadstart: _generalHandler,
//loadeddata: _onLoadedData, // we have duration
loadedmetadata: _loadedMetadataHandler, // we have video dimensions
canplay: _canPlayHandler,
playing: _playingHandler,
progress: _progressHandler,
//canplaythrough: _generalHandler,
//pause: _pauseHandler,
//ratechange: _generalHandler,
//readystatechange: _generalHandler,
seeked: _seekedHandler,
//seeking: _seekingHandler,
//stalled: _stalledHandler,
//suspend: _generalHandler,
timeupdate: _timeUpdateHandler,
volumechange: _volumeChangeHandler,
//waiting: _stalledHandler,
webkitbeginfullscreen: _fullscreenBeginHandler,
webkitendfullscreen: _fullscreenEndHandler
},
// DOM container
_container,
// Currently playing source
_source,
// Current duration
_duration,
// Current position
_position,
// Whether seeking is ready yet
_canSeek = false,
// Whether we have sent out the BUFFER_FULL event
_bufferFull,
// If we should seek on canplay
_delayedSeek = 0,
// Using setInterval to check buffered ranges
_playbackTimeout = -1,
// Last sent buffer amount
_buffered = -1,
// Whether or not we're listening to video tag events
_attached = true,
// Quality levels
_levels,
// Current quality level index
_currentQuality = -1,
// android hls doesn't update currentTime so we want to skip the stall check since it always fails
_isAndroidHLS = null,
// post roll support
_beforecompleted = false,
_fullscreenState = false;
// Find video tag, or create it if it doesn't exist. View may not be built yet.
var element = document.getElementById(_playerId);
var _videotag = (element) ? element.querySelector('video') : undefined;
_videotag = _videotag || document.createElement('video');
_videotag.className = 'jw-video jw-reset';
_setupListeners(_mediaEvents, _videotag);
// Workaround for a Safari bug where video disappears on switch to fullscreen
if (!_isIOS7) {
_videotag.controls = true;
_videotag.controls = false;
}
// Enable AirPlay
_videotag.setAttribute('x-webkit-airplay', 'allow');
_videotag.setAttribute('webkit-playsinline', '');
function _clickHandler(evt) {
_this.trigger('click', evt);
}
function _durationChangeHandler() {
if (!_attached) {
return;
}
_setBuffered(_getBuffer(), _position, _videotag.duration);
_setDuration(_videotag.duration);
}
function _progressHandler() {
if (!_attached) {
return;
}
_setBuffered(_getBuffer(), _position, _duration);
}
function _timeUpdateHandler() {
if (!_attached) {
return;
}
_canSeek = true;
clearTimeout(_playbackTimeout);
if (_this.state === states.STALLED) {
_this.setState(states.PLAYING);
} else if (_this.state === states.PLAYING) {
_playbackTimeout = setTimeout(_checkPlaybackStalled, STALL_DELAY);
}
_setDuration(_videotag.duration);
_setPosition(_videotag.currentTime);
// send time events when playing
if (_this.state === states.PLAYING) {
_this.trigger(events.JWPLAYER_MEDIA_TIME, {
position: _position,
duration: _duration
});
}
}
function _setBuffered(buffered, currentTime, duration) {
if (buffered !== _buffered || duration !== _duration) {
_buffered = buffered;
_this.trigger(events.JWPLAYER_MEDIA_BUFFER, {
bufferPercent: buffered * 100,
position: currentTime,
duration: duration
});
}
}
function _setPosition(currentTime) {
_position = currentTime;
}
function _setDuration(duration) {
_duration = duration;
if (_delayedSeek > 0 && _duration > _delayedSeek) {
_this.seek(_delayedSeek);
}
}
function _sendMetaEvent() {
_this.trigger(events.JWPLAYER_MEDIA_META, {
duration: _videotag.duration,
height: _videotag.videoHeight,
width: _videotag.videoWidth
});
_setDuration(_videotag.duration);
}
function _canPlayHandler() {
if (!_attached) {
return;
}
_canSeek = true;
_sendBufferFull();
}
function _loadedMetadataHandler() {
if (!_attached) {
return;
}
//fixes Chrome bug where it doesn't like being muted before video is loaded
if (_videotag.muted) {
_videotag.muted = false;
_videotag.muted = true;
}
_sendMetaEvent();
}
function _sendBufferFull() {
if (!_bufferFull) {
_bufferFull = true;
_this.trigger(events.JWPLAYER_MEDIA_BUFFER_FULL);
}
}
function _playingHandler() {
if (!_attached) {
return;
}
_this.setState(states.PLAYING);
_this.trigger(events.JWPLAYER_PROVIDER_FIRST_FRAME, {});
}
function _stalledHandler() {
if (!_attached) {
return;
}
// Android HLS doesnt update its times correctly so it always falls in here. Do not allow it to stall.
if (_isAndroidHLS) {
return;
}
if (_videotag.paused || _videotag.ended) {
return;
}
// A stall after loading/error, should just stay loading/error
if (_this.state === states.LOADING || _this.state === states.ERROR) {
return;
}
// During seek we stay in paused state
if (_this.seeking) {
return;
}
_this.setState(states.STALLED);
}
function _errorHandler() {
if (!_attached) {
return;
}
utils.log('Error playing media: %o %s', _videotag.error, _videotag.src || _source.file);
_this.trigger(events.JWPLAYER_MEDIA_ERROR, {
message: 'Error loading media: File could not be played'
});
}
function _getPublicLevels(levels) {
var publicLevels;
if (utils.typeOf(levels) === 'array' && levels.length > 0) {
publicLevels = _.map(levels, function(level, i) {
return {
label: level.label || i
};
});
}
return publicLevels;
}
function _setLevels(levels) {
_levels = levels;
_currentQuality = _pickInitialQuality(levels);
var publicLevels = _getPublicLevels(levels);
if (publicLevels) {
//_trigger?
_this.trigger(events.JWPLAYER_MEDIA_LEVELS, {
levels: publicLevels,
currentQuality: _currentQuality
});
}
}
function _pickInitialQuality(levels) {
var currentQuality = Math.max(0, _currentQuality);
var label = _playerConfig.qualityLabel;
if (levels) {
for (var i = 0; i < levels.length; i++) {
if (levels[i]['default']) {
currentQuality = i;
}
if (label && levels[i].label === label) {
return i;
}
}
}
return currentQuality;
}
function _forceVideoLoad() {
// These browsers will not replay videos without reloading them
return (_isMobile || _isSafari);
}
function _completeLoad(startTime, duration) {
_source = _levels[_currentQuality];
_delayedSeek = 0;
clearTimeout(_playbackTimeout);
var sourceChanged = (_videotag.src !== _source.file);
if (sourceChanged || _forceVideoLoad()) {
_duration = duration;
_setVideotagSource();
_videotag.load();
} else {
// Load event is from the same video as before
if (startTime === 0) {
// restart video without dispatching seek event
_delayedSeek = -1;
_this.seek(startTime);
}
// meta event is usually triggered by load, and is needed for googima to work on replay
_sendMetaEvent();
_videotag.play();
}
_position = _videotag.currentTime;
if (_isMobile) {
// results in html5.controller calling video.play()
_sendBufferFull();
}
//in ios and fullscreen, set controls true, then when it goes to normal screen the controls don't show'
if (utils.isIOS() && _this.getFullScreen()) {
_videotag.controls = true;
}
if (startTime > 0) {
_this.seek(startTime);
}
}
function _setVideotagSource() {
_canSeek = false;
_bufferFull = false;
_isAndroidHLS = _useAndroidHLS(_source);
_videotag.src = _source.file;
// set preload from the source, or default to metadata
_videotag.setAttribute('preload', _source.preload || 'metadata');
}
this.stop = function() {
if (!_attached) {
return;
}
clearTimeout(_playbackTimeout);
_videotag.removeAttribute('src');
if (!_isIE) {
_videotag.load();
}
_currentQuality = -1;
this.setState(states.IDLE);
};
this.destroy = function() {
_removeListeners(_mediaEvents, _videotag);
this.remove();
this.off();
};
this.init = function(item) {
if (!_attached) {
return;
}
_levels = item.sources;
_currentQuality = _pickInitialQuality(item.sources);
this.sendMediaType(item.sources);
_source = _levels[_currentQuality];
_position = item.starttime || 0;
_duration = item.duration || 0;
_setVideotagSource(item);
};
this.load = function(item) {
if (!_attached) {
return;
}
_setLevels(item.sources);
this.sendMediaType(item.sources);
if (!_isMobile) {
// don't change state on mobile because a touch event may be required to start playback
_this.setState(states.LOADING);
}
_completeLoad(item.starttime || 0, item.duration || 0);
};
this.play = function() {
if (_this.seeking) {
_this.setState(states.LOADING);
_this.once(events.JWPLAYER_MEDIA_SEEKED, _this.play);
return;
}
_videotag.play();
};
this.pause = function() {
clearTimeout(_playbackTimeout);
_videotag.pause();
this.setState(states.PAUSED);
};
this.seek = function(seekPos) {
if (!_attached) {
return;
}
if (_delayedSeek === 0) {
this.trigger(events.JWPLAYER_MEDIA_SEEK, {
position: _videotag.currentTime,
offset: seekPos
});
}
if (_canSeek) {
_delayedSeek = 0;
// handle readystate issue
var status = utils.tryCatch(function() {
_this.seeking = true;
_videotag.currentTime = seekPos;
});
if (status instanceof utils.Error) {
_delayedSeek = seekPos;
}
} else {
_delayedSeek = seekPos;
}
};
function _seekedHandler() {
_this.seeking = false;
_this.trigger(events.JWPLAYER_MEDIA_SEEKED);
}
this.volume = function(vol) {
// volume must be 0.0 - 1.0
vol = utils.between(vol/100, 0, 1);
_videotag.volume = vol;
};
function _volumeChangeHandler() {
_this.trigger('volume', {
volume: Math.round(_videotag.volume * 100)
});
_this.trigger('mute', {
mute: _videotag.muted
});
}
this.mute = function(state) {
_videotag.muted = !!state;
};
function _checkPlaybackStalled() {
if (!_attached) {
return;
}
// Browsers, including latest chrome, do not always report Stalled events in a timely fashion
if (_videotag.currentTime === _position) {
_stalledHandler();
}
}
function _getBuffer() {
var buffered = _videotag.buffered;
var duration = _videotag.duration;
if (!buffered || buffered.length === 0 || duration <= 0 || duration === Infinity) {
return 0;
}
return utils.between(buffered.end(buffered.length-1) / duration, 0, 1);
}
function _endedHandler() {
if (!_attached) {
return;
}
if (_this.state !== states.IDLE && _this.state !== states.COMPLETE) {
clearTimeout(_playbackTimeout);
_currentQuality = -1;
_beforecompleted = true;
_this.trigger(events.JWPLAYER_MEDIA_BEFORECOMPLETE);
// This event may trigger the detaching of the player
// In that case, playback isn't complete until the player is re-attached
if (!_attached) {
return;
}
_playbackComplete();
}
}
function _playbackComplete() {
clearTimeout(_playbackTimeout);
_this.setState(states.COMPLETE);
_beforecompleted = false;
_this.trigger(events.JWPLAYER_MEDIA_COMPLETE);
}
function _fullscreenBeginHandler(e) {
_fullscreenState = true;
_sendFullscreen(e);
// show controls on begin fullscreen so that they are disabled properly at end
if (utils.isIOS()) {
_videotag.controls = false;
}
}
function _fullscreenEndHandler(e) {
_fullscreenState = false;
_sendFullscreen(e);
if (utils.isIOS()) {
_videotag.controls = false;
// iOS may pause the video after exiting fullscreen
// when using the "Done" button
if (_videotag.paused) {
_this.setState(states.PAUSED);
}
}
}
function _sendFullscreen(e) {
_this.trigger('fullscreenchange', {
target: e.target,
jwstate: _fullscreenState
});
}
this.checkComplete = function() {
return _beforecompleted;
};
/**
* Return the video tag and stop listening to events
*/
this.detachMedia = function() {
clearTimeout(_playbackTimeout);
_attached = false;
return _videotag;
};
/**
* Begin listening to events again
*/
this.attachMedia = function(seekable) {
_attached = true;
if (!seekable) {
_canSeek = false;
}
// In case the video tag was modified while we shared it
_videotag.loop = false;
// This is after a postroll completes
if (_beforecompleted) {
_playbackComplete();
}
};
this.setContainer = function(element) {
_container = element;
element.appendChild(_videotag);
};
this.getContainer = function() {
return _container;
};
this.remove = function() {
// stop video silently
if (_videotag) {
_videotag.removeAttribute('src');
if (!_isIE) {
_videotag.load();
}
}
clearTimeout(_playbackTimeout);
_currentQuality = -1;
// remove
if (_container === _videotag.parentNode) {
_container.removeChild(_videotag);
}
};
this.setVisibility = function(state) {
state = !!state;
if (state || _isAndroid) {
// Changing visibility to hidden on Android < 4.2 causes
// the pause event to be fired. This causes audio files to
// become unplayable. Hence the video tag is always kept
// visible on Android devices.
cssUtils.style(_container, {
visibility: 'visible',
opacity: 1
});
} else {
cssUtils.style(_container, {
visibility: '',
opacity: 0
});
}
};
this.resize = function(width, height, stretching) {
return stretchUtils.stretch(stretching,
_videotag,
width, height,
_videotag.videoWidth, _videotag.videoHeight);
};
this.setFullscreen = function(state) {
state = !!state;
// This implementation is for iOS and Android WebKit only
// This won't get called if the player contain can go fullscreen
if (state) {
var status = utils.tryCatch(function() {
var enterFullscreen =
_videotag.webkitEnterFullscreen ||
_videotag.webkitEnterFullScreen;
if (enterFullscreen) {
enterFullscreen.apply(_videotag);
}
});
if (status instanceof utils.Error) {
//object can't go fullscreen
return false;
}
return _this.getFullScreen();
} else {
var exitFullscreen =
_videotag.webkitExitFullscreen ||
_videotag.webkitExitFullScreen;
if (exitFullscreen) {
exitFullscreen.apply(_videotag);
}
}
return state;
};
_this.getFullScreen = function() {
return _fullscreenState || !!_videotag.webkitDisplayingFullscreen;
};
this.setCurrentQuality = function(quality) {
if (_currentQuality === quality) {
return;
}
quality = parseInt(quality, 10);
if (quality >= 0) {
if (_levels && _levels.length > quality) {
_currentQuality = quality;
this.trigger(events.JWPLAYER_MEDIA_LEVEL_CHANGED, {
currentQuality: quality,
levels: _getPublicLevels(_levels)
});
var time = _videotag.currentTime || 0;
var duration = _videotag.duration;
if (duration <= 0) {
duration = _duration;
}
_this.setState(states.LOADING);
_completeLoad(time, duration || 0);
}
}
};
this.getCurrentQuality = function() {
return _currentQuality;
};
this.getQualityLevels = function() {
return _getPublicLevels(_levels);
};
this.getName = function() {
return { name : _name };
};
}
// Register provider
var F = function(){};
F.prototype = DefaultProvider;
VideoProvider.prototype = new F();
return VideoProvider;
});