forked from mozilla/popcorn-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopcorn.HTMLJWPlayerVideoElement.js
590 lines (505 loc) · 14.1 KB
/
popcorn.HTMLJWPlayerVideoElement.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
(function( Popcorn, window, document ) {
var
EMPTY_STRING = "",
jwReady = false,
jwLoaded = false,
jwCallbacks = [];
function onJWPlayerAPIReady() {
jwReady = true;
var i = jwCallbacks.length;
while( i-- ) {
jwCallbacks[ i ]();
delete jwCallbacks[ i ];
}
};
function jwplayerReadyCheck() {
if ( window.jwplayer ) {
onJWPlayerAPIReady();
} else {
setTimeout( jwplayerReadyCheck, 100 );
}
}
function isJWPlayerReady() {
// If the jwplayer API isn't injected, do it now.
if ( !jwLoaded ) {
if ( !window.jwplayer ) {
var tag = document.createElement( "script" );
tag.src = "https://jwpsrv.com/library/zaIF4JI9EeK2FSIACpYGxA.js";
var firstScriptTag = document.getElementsByTagName( "script" )[ 0 ];
firstScriptTag.parentNode.insertBefore( tag, firstScriptTag );
}
jwLoaded = true;
jwplayerReadyCheck();
}
return jwReady;
}
function addJWPlayerCallback( callback ) {
jwCallbacks.unshift( callback );
}
function HTMLJWPlayerVideoElement( id ) {
if ( !window.postMessage ) {
throw "ERROR: HTMLJWPlayerVideoElement requires window.postMessage";
}
var self = new Popcorn._MediaElementProto(),
parent = typeof id === "string" ? document.querySelector( id ) : id,
impl = {
src: EMPTY_STRING,
networkState: self.NETWORK_EMPTY,
readyState: self.HAVE_NOTHING,
seeking: false,
autoplay: EMPTY_STRING,
preload: EMPTY_STRING,
controls: false,
loop: false,
poster: EMPTY_STRING,
volume: 1,
muted: false,
currentTime: 0,
duration: NaN,
ended: false,
paused: true,
error: null
},
playerReady = false,
catchRoguePauseEvent = false,
catchRoguePlayEvent = false,
mediaReady = false,
loopedPlay = false,
player,
playerPaused = true,
mediaReadyCallbacks = [],
playerState = -1,
lastLoadedFraction = 0,
firstPlay = true,
firstPause = false;
// Namespace all events we'll produce
self._eventNamespace = Popcorn.guid( "HTMLJWPlayerVideoElement::" );
self.parentNode = parent;
// Mark this as JWPlayer
self._util.type = "JWPlayer";
function addMediaReadyCallback( callback ) {
mediaReadyCallbacks.unshift( callback );
}
function waitForMetaData(){
var duration = player.getDuration();
//JWPlayer sets the duration only after the video has started playing
//Hence, we assume that when duration is available all
//other metadata is also ready
if(duration == -1 || duration == undefined){
setTimeout(waitForMetaData, 0);
} else {
impl.duration = duration
self.dispatchEvent( "durationchange" );
playerReady = true;
impl.readyState = self.HAVE_METADATA;
self.dispatchEvent( "loadedmetadata" );
self.dispatchEvent( "loadeddata" );
impl.readyState = self.HAVE_FUTURE_DATA;
self.dispatchEvent( "canplay" );
mediaReady = true;
var i = 0;
while( mediaReadyCallbacks.length ) {
mediaReadyCallbacks[ i ]();
mediaReadyCallbacks.shift();
}
// We can't easily determine canplaythrough, but will send anyway.
impl.readyState = self.HAVE_ENOUGH_DATA;
self.dispatchEvent( "canplaythrough" );
}
}
function onReady() {
// JWPlayer needs a play/pause to force ready state.
waitForMetaData();
}
// TODO: (maybe)
// JWPlayer events cannot be removed, so we use functions inside the event.
// This way we can change these functions to "remove" events.
function onPauseEvent() {
if ( catchRoguePauseEvent ) {
catchRoguePauseEvent = false;
} else if ( firstPause ) {
firstPause = false;
onReady();
} else {
onPause();
}
}
function onPlayEvent() {
if ( firstPlay ) {
// fake ready event
firstPlay = false;
// Set initial paused state
if ( impl.autoplay || !impl.paused ) {
impl.paused = false;
addMediaReadyCallback( onPlay );
onReady();
} else {
firstPause = true;
catchRoguePlayEvent = true;
player.pause( true );
}
} else if ( catchRoguePlayEvent ) {
catchRoguePlayEvent = false;
catchRoguePauseEvent = true;
// Repause without triggering any events.
player.pause( true );
} else {
onPlay();
}
}
function onSeekEvent() {
if ( impl.seeking ) {
onSeeked();
}
}
function onPlayerReady() {
player.onPause( onPauseEvent );
player.onTime(function() {
if ( !impl.ended && !impl.seeking ) {
impl.currentTime = player.getPosition();
self.dispatchEvent( "timeupdate" );
}
});
player.onSeek( onSeekEvent );
player.onPlay(function() {
if ( !impl.ended ) {
onPlayEvent();
}
});
player.onBufferChange( onProgress );
player.onComplete( onEnded );
player.play( true );
}
function getDuration() {
return player.getDuration();
}
function onPlayerError( e ) {
var err = { name: "MediaError" };
err.message = e.message;
err.code = e.code || 5;
impl.error = err;
self.dispatchEvent( "error" );
}
function destroyPlayer() {
if ( !( playerReady && player ) ) {
return;
}
player.destroy();
}
function changeSrc( aSrc ) {
if ( !self._canPlaySrc( aSrc ) ) {
impl.error = {
name: "MediaError",
message: "Media Source Not Supported",
code: MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED
};
self.dispatchEvent( "error" );
return;
}
// Use any player vars passed on the URL
var playerVars = self._util.parseUri( aSrc ).queryKey;
// Show/hide controls. Sync with impl.controls and prefer URL value.
impl.controls = playerVars.controls = playerVars.controls || impl.controls;
impl.src = aSrc;
// Make sure JWPlayer is ready, and if not, register a callback
if ( !isJWPlayerReady() ) {
addJWPlayerCallback( function() { changeSrc( aSrc ); } );
return;
}
if ( playerReady ) {
destroyPlayer();
}
var params = {
width: "100%",
height: "100%",
autostart: impl.autoplay,
controls: impl.controls
};
// Source can either be a single file or multiple files that represent
// different quality
if(typeof aSrc == "string"){
params["file"] = aSrc;
} else {
params["sources"] = aSrc;
}
jwplayer( parent.id ).setup(params);
player = jwplayer( parent.id );
player.onReady( onPlayerReady );
player.onError( onPlayerError );
jwplayer.utils.log = function( msg, obj ) {
if ( typeof console !== "undefined" && typeof console.log !== "undefined" ) {
if ( obj ) {
console.log( msg, obj );
} else {
console.log( msg );
}
}
if ( msg === "No suitable players found and fallback enabled" ) {
onPlayerError({
message: msg,
code: 4
});
}
};
impl.networkState = self.NETWORK_LOADING;
self.dispatchEvent( "loadstart" );
self.dispatchEvent( "progress" );
}
function getCurrentTime() {
return impl.currentTime;
}
function changeCurrentTime( aTime ) {
impl.currentTime = aTime;
if ( !mediaReady ) {
addMediaReadyCallback( function() {
onSeeking();
player.seek( aTime );
});
return;
}
onSeeking();
player.seek( aTime );
}
function onSeeking() {
impl.seeking = true;
// jwplayer plays right after a seek, we do not want this.
if ( impl.paused ) {
catchRoguePlayEvent = true;
}
self.dispatchEvent( "seeking" );
}
function onSeeked() {
impl.ended = false;
impl.seeking = false;
self.dispatchEvent( "timeupdate" );
self.dispatchEvent( "seeked" );
self.dispatchEvent( "canplay" );
self.dispatchEvent( "canplaythrough" );
}
function onPlay() {
impl.paused = false;
if ( playerReady && playerPaused ) {
playerPaused = false;
// Only 1 play when video.loop=true
if ( ( impl.loop && !loopedPlay ) || !impl.loop ) {
loopedPlay = true;
self.dispatchEvent( "play" );
}
self.dispatchEvent( "playing" );
}
}
function onProgress() {
self.dispatchEvent( "progress" );
}
self.play = function() {
self.dispatchEvent( "play" );
impl.paused = false;
if ( !mediaReady ) {
addMediaReadyCallback( function() { self.play(); } );
return;
}
if ( impl.ended ) {
changeCurrentTime( 0 );
impl.ended = false;
}
player.play( true );
};
function onPause() {
impl.paused = true;
if ( !playerPaused ) {
playerPaused = true;
self.dispatchEvent( "pause" );
}
}
self.pause = function() {
impl.paused = true;
if ( !mediaReady ) {
addMediaReadyCallback( function() { self.pause(); } );
return;
}
player.pause( true );
};
function onEnded() {
if ( impl.loop ) {
changeCurrentTime( 0 );
} else {
impl.ended = true;
onPause();
self.dispatchEvent( "timeupdate" );
self.dispatchEvent( "ended" );
}
}
function setVolume( aValue ) {
impl.volume = aValue;
if ( !mediaReady ) {
addMediaReadyCallback( function() {
setVolume( impl.volume );
});
return;
}
player.setVolume( impl.volume * 100 );
self.dispatchEvent( "volumechange" );
}
function setMuted( aValue ) {
impl.muted = aValue;
if ( !mediaReady ) {
addMediaReadyCallback( function() { setMuted( impl.muted ); } );
return;
}
player.setMute( aValue );
self.dispatchEvent( "volumechange" );
}
function getMuted() {
return impl.muted;
}
Object.defineProperties( self, {
src: {
get: function() {
return impl.src;
},
set: function( aSrc ) {
if ( aSrc && aSrc !== impl.src ) {
changeSrc( aSrc );
}
}
},
autoplay: {
get: function() {
return impl.autoplay;
},
set: function( aValue ) {
impl.autoplay = self._util.isAttributeSet( aValue );
}
},
loop: {
get: function() {
return impl.loop;
},
set: function( aValue ) {
impl.loop = self._util.isAttributeSet( aValue );
}
},
width: {
get: function() {
return self.parentNode.offsetWidth;
}
},
height: {
get: function() {
return self.parentNode.offsetHeight;
}
},
currentTime: {
get: function() {
return getCurrentTime();
},
set: function( aValue ) {
changeCurrentTime( aValue );
}
},
duration: {
get: function() {
return getDuration();
}
},
ended: {
get: function() {
return impl.ended;
}
},
paused: {
get: function() {
return impl.paused;
}
},
seeking: {
get: function() {
return impl.seeking;
}
},
readyState: {
get: function() {
return impl.readyState;
}
},
networkState: {
get: function() {
return impl.networkState;
}
},
volume: {
get: function() {
return impl.volume;
},
set: function( aValue ) {
if ( aValue < 0 || aValue > 1 ) {
throw "Volume value must be between 0.0 and 1.0";
}
setVolume( aValue );
}
},
muted: {
get: function() {
return impl.muted;
},
set: function( aValue ) {
setMuted( self._util.isAttributeSet( aValue ) );
}
},
error: {
get: function() {
return impl.error;
}
},
buffered: {
get: function () {
var timeRanges = {
start: function( index ) {
if ( index === 0 ) {
return 0;
}
//throw fake DOMException/INDEX_SIZE_ERR
throw "INDEX_SIZE_ERR: DOM Exception 1";
},
end: function( index ) {
var duration;
if ( index === 0 ) {
duration = getDuration();
if ( !duration ) {
return 0;
}
return duration * ( player.getBuffer() / 100 );
}
//throw fake DOMException/INDEX_SIZE_ERR
throw "INDEX_SIZE_ERR: DOM Exception 1";
},
length: 1
};
return timeRanges;
}
}
});
self._canPlaySrc = Popcorn.HTMLJWPlayerVideoElement._canPlaySrc;
self.canPlayType = Popcorn.HTMLJWPlayerVideoElement.canPlayType;
return self;
}
Popcorn.HTMLJWPlayerVideoElement = function( id ) {
return new HTMLJWPlayerVideoElement( id );
};
// Helper for identifying URLs we know how to play.
Popcorn.HTMLJWPlayerVideoElement._canPlaySrc = function( source ) {
// Because of the nature of JWPlayer playing all media types,
// it can potentially play all url formats.
if(typeof source == "string"){
if(/.+\.+/g.exec(source)){
return "probably";
}
} else {
return "probably"
}
};
// This could potentially support everything. It is a bit of a catch all player.
Popcorn.HTMLJWPlayerVideoElement.canPlayType = function( type ) {
return "probably";
};
}( Popcorn, window, document ));