/
mozilla-video-tools.js
543 lines (457 loc) · 13.8 KB
/
mozilla-video-tools.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
/**
* Various tools for the HTML5 video element
*
* Meant to be used with CSS in /styles/tignish/video-player.css.
*
* This file contains Flash-detection routines adapted from SWFObject and
* originally licensed under the MIT license.
*
* See http://blog.deconcept.com/flashobject/
*
* This file can make use of the SMILE-based subtitling routines provided
* courtesy of Fabien Cazenave for INRIA under the BSD license. Those are
* stored in mozilla-video-tools-addsubtitles.js
*
*
* @copyright 2009-2011 Mozilla Corporation
* @author Michael Gauthier <mike@silverorange.com>
*/
// create namespace
if (typeof Mozilla == 'undefined') {
var Mozilla = {};
}
// {{{ Mozilla.VideoControl
/**
* Initializes video controls on this page after the document has been loaded
*/
$(document).ready(function() {
$('.mozilla-video-control').each(function() {
Mozilla.VideoControl.controls.push(
new Mozilla.VideoControl($(this))
)
});
});
/**
* Provides a click-to-play button for HTML5 video element content
*
* If the HTML5 video element is supported, the following markup will
* automatically get the click-to-play button when the page initializes:
* <code>
* <div class="mozilla-video-control">
* <video ... />
* </div>
* </code>
*
* @param jQuery|String container
*/
Mozilla.VideoControl = function(container)
{
if (typeof container == 'String') {
container = $('#' + container);
}
this.container = container;
// Retrieve jQuery object and the corresponding DOM element
var video = container.find('video:first');
// If there is not preload attribute set, set it to metadata
// because this library depends on preloading
var preload = video.attr('preload');
if(!preload || preload == 'none') {
video.attr('preload', 'metadata');
}
this.video = video;
this._video = this.video[0];
this.semaphore = false;
/*
* Check if HTMLMediaElement exists to filter out browsers that do not
* support the video element.
*/
if ( typeof HTMLMediaElement != 'undefined'
&& this._video instanceof HTMLMediaElement
) {
this.drawControl();
this._video._control = this;
}
}
Mozilla.VideoControl.controls = [];
Mozilla.VideoControl.prototype.drawControl = function()
{
var $overlayHiddenText = $(this._video).data('overlay-hidden-text');
if (!$overlayHiddenText) {
$overlayHiddenText = 'Play video';
}
var buttonTag = '<a href="#" role="button" class="mozilla-video-control-overlay" style="opacity: 0">' +
'<span class="hidden">' +
$overlayHiddenText +
'</span></a>';
this.control = $(buttonTag);
// Show the click-to-play button. In the future, this may be changed
// to show the click-to-play button based on media events like the
// hiding is done.
if (this._video.paused || this._video.ended) {
this.show();
}
var that = this;
// hide click-to-play button on these events
this.video.bind('play playing seeking waiting', function(event) {
that.hide();
});
this.control.mouseover(function(event) {
if (!that.semaphore) {
that.prelight();
}
});
this.control.mouseout(function(event) {
if (!that.semaphore) {
that.unprelight();
}
});
this.control.click(function(event) {
event.preventDefault();
if (that.semaphore || !that.videoCanPlay()) {
return;
}
that.semaphore = true;
// rewind the video
if (that._video.ended) {
that._video.currentTime = 0;
}
that._video.play();
});
this.container.append(this.control);
}
Mozilla.VideoControl.prototype.videoCanPlay = function()
{
// check if we're using an older draft version of the readyState spec
var current_data = (typeof HTMLMediaElement.CAN_PLAY == 'undefined') ?
HTMLMediaElement.HAVE_CURRENT_DATA : HTMLMediaElement.CAN_PLAY;
return (this._video.readyState >= current_data);
}
Mozilla.VideoControl.prototype.show = function()
{
var that = this;
this._video.controls = false;
// FIXME : Does not work on http://mozilla.local/en-US/firefox/video/
// this.control.show();
this.control.css('display', 'block');
this.control.stop(true).fadeTo('slow', 0.7, function() {
that.semaphore = false;
});
}
Mozilla.VideoControl.prototype.hide = function()
{
var that = this;
if (this.control.is(':visible')) {
this.semaphore = true;
this.control.stop(true).fadeTo('fast', 0, function() {
$(this).hide();
that._video.controls = true;
});
}
}
Mozilla.VideoControl.prototype.prelight = function()
{
if (this.control.is(':visible')) {
this.control.stop(true).fadeTo('fast', 1);
}
}
Mozilla.VideoControl.prototype.unprelight = function()
{
if (this.control.is(':visible')) {
this.control.stop(true).fadeTo('fast', 0.7);
}
}
// }}}
// {{{ Mozilla.VideoPlayer
/**
* Popup player using HTML5 video element with flash fallback
*
* @param String id
* @param Array sources
* @param String flv_url
* @param Booelan autoplay
*/
Mozilla.VideoPlayer = function(id, sources, flv_url, autoplay, extra_content)
{
this.id = id;
this.flv_url = flv_url;
this.sources = sources;
this.opened = false;
if (arguments.length > 3) {
this.autoplay = autoplay;
} else {
this.autoplay = true;
}
if (arguments.length > 4) {
this.extra_content = extra_content;
} else {
this.extra_content = '';
}
var that = this;
$(document).ready(function() {
that.init();
});
}
Mozilla.VideoPlayer.height = 385;
Mozilla.VideoPlayer.width = 640;
Mozilla.VideoPlayer.ie6 = /MSIE\s[1-6]\./.test(navigator.userAgent);
Mozilla.VideoPlayer.close_text = 'Close';
Mozilla.VideoPlayer.fallback_text =
'This video requires a browser with support for open video or the '
+ '<a href="http://get.adobe.com/flashplayer">Adobe Flash '
+ 'Player</a>. Alternatively, you may use the video download links '
+ 'provided.';
Mozilla.VideoPlayer.prototype.init = function()
{
var that = this;
// add overlay and preview image to document
this.overlay = $('<a class="mozilla-video-player-overlay" href="#"/>')
.hide()
.appendTo('body')
.click(function(e) { e.preventDefault(); that.close(); });
this.video_container = $('<div class="mozilla-video-player-window" />')
.hide()
.appendTo('body');
// set video link and video preview link event handler
$('#' + this.id + ', #' + this.id + '-preview').click(function(e) {
e.preventDefault();
that.open();
});
}
Mozilla.VideoPlayer.prototype.clearVideoPlayer = function()
{
// remove event handlers
this.video_container.unbind('click');
// workaround for FF Bug #533840, manually pause all videos
this.video_container.find('video').each(function() {
$(this)[0].pause();
});
// remove all elements
this.video_container.empty();
}
Mozilla.VideoPlayer.prototype.drawVideoPlayer = function()
{
var that = this;
this.clearVideoPlayer();
// get content for player
if (typeof HTMLMediaElement != 'undefined') {
var content = this.getVideoPlayerContent();
} else if (Mozilla.VideoPlayer.flash_verison.isValid([7, 0, 0])) {
var content = this.getFlashPlayerContent();
} else {
var content = this.getFallbackContent();
}
// add download links
content += '<div class="video-download-links">';
if (this.extra_content != '') {
content += this.extra_content;
}
content += '<ul>';
$.each(this.sources, function(index, source) {
content += '<li><a href="' + source.url + '">' + source.title + '</a></li>';
});
content += '</ul></div>';
this.video_container.append(
$('<div class="mozilla-video-player-close" />').append(
$('<a href="#" />').click(function(event) {
event.preventDefault();
that.close();
}).append(
$('<img src="/img/covehead/video/clothes-lol.png" height="32" width="32" alt="' + Mozilla.VideoPlayer.close_text + '" />')
)
)
).append(
$('<div class="mozilla-video-player-content" />').html(content)
);
}
Mozilla.VideoPlayer.prototype.getVideoPlayerContent = function()
{
var content =
'<video id="htmlPlayer" width="' + Mozilla.VideoPlayer.width + '" '
+ 'height="' + Mozilla.VideoPlayer.height + '" '
+ 'controls="controls"';
if (this.autoplay) {
content += ' autoplay="autoplay"';
}
content += '>';
$.each(this.sources, function(index, source) {
if (!source.type) return; // must have MIME type
content += '<source src="' + source.url + '" '
+ 'type="' + source.type + '"/>';
});
content += '</video>';
return content;
}
Mozilla.VideoPlayer.prototype.getFlashPlayerContent = function()
{
var url = '/includes/flash/playerWithControls.swf?flv=' + this.flv_url
+ '&autoplay=';
url+= (this.autoplay) ? 'true' : 'false';
var content =
'<object type="application/x-shockwave-flash" style="'
+ 'width: ' + Mozilla.VideoPlayer.width + 'px; '
+ 'height: ' + Mozilla.VideoPlayer.height + 'px;" '
+ 'wmode="transparent" data="' + url + '">'
+ '<param name="movie" value="' + url + '">'
+ '<param name="wmode" value="transparent">'
+ '</object>';
return content;
}
Mozilla.VideoPlayer.prototype.getFallbackContent = function()
{
var content =
'<div class="mozilla-video-player-no-flash">'
+ Mozilla.VideoPlayer.fallback_text
+ '</div>';
return content;
}
Mozilla.VideoPlayer.prototype.open = function()
{
// hide the language form because its select element won't render
// correctly in IE6
$('#lang_form').hide();
if (Mozilla.VideoPlayer.ie6) {
this.video_container.css('position', 'absolute');
this.overlay.css('position', 'absolute');
}
var that = this;
this.resizeHandler = function(e) {
that.resizeOverlay();
};
$(window).resize(this.resizeHandler);
if (Mozilla.VideoPlayer.ie6) {
$(window).scroll(this.resizeHandler);
}
this.drawVideoPlayer();
this.resizeOverlay();
this.overlay.fadeTo(400, 0.75);
this.video_container.fadeIn();
this.opened = true;
// getSubtitles() depends on mozilla-video-tools-addsubtitles.js
if (window.getSubtitles) {
getSubtitles(this.video_container.css('top'));
}
}
Mozilla.VideoPlayer.prototype.resizeOverlay = function()
{
if (Mozilla.VideoPlayer.ie6) {
var scrollTop = $(window).scrollTop();
var docHeight = $(document).height();
var winHeight = $(window).height();
var playerHeight = 430;
var bottom = scrollTop + (winHeight + playerHeight) / 2;
if (bottom > docHeight) {
// this prevents infinite scroll
this.video_container.css('top', docHeight - playerHeight - 10);
} else {
this.video_container.css('top', scrollTop + (winHeight - playerHeight) / 2);
}
this.overlay.height(docHeight);
} else {
this.video_container.css('top', ($(window).height() - 430) / 2);
this.overlay.height($(window).height());
}
}
Mozilla.VideoPlayer.prototype.close = function()
{
// hideSubtitles() depends on mozilla-video-tools-addsubtitles.js
if (window.hideSubtitles) {
hideSubtitles();
}
this.overlay.fadeOut();
this.video_container.fadeOut();
// clear the video content so IE will stop playing the audio
this.clearVideoPlayer();
// if language form was hidden, show it again
$('#lang_form').show();
$(window).unbind('resize', this.resizeHandler);
this.opened = false;
}
Mozilla.VideoPlayer.prototype.toggle = function()
{
if (this.opened)
this.close();
else
this.open();
}
Mozilla.VideoPlayer.getFlashVersion = function()
{
var version = new Mozilla.FlashVersion([0, 0, 0]);
if (navigator.plugins && navigator.mimeTypes.length) {
var x = navigator.plugins['Shockwave Flash'];
if (x && x.description) {
// strip text to get version number only
version = x.description.replace(/([a-zA-Z]|\s)+/, '');
// convert revisions and beta to dots
version = version.replace(/(\s+r|\s+b[0-9]+)/, '.');
// get version
version = new Mozilla.FlashVersion(version.split('.'));
}
} else {
if (navigator.userAgent && navigator.userAgent.indexOf('Windows CE') >= 0) {
var axo = true;
var flash_version = 3;
while (axo) {
// look for greatest installed version starting at 4
try {
major_version++;
axo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.' + major_version);
version = new Mozilla.FlashVersion([major_version, 0, 0]);
} catch (e) {
axo = null;
}
}
} else {
try {
var axo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.7');
} catch (e) {
try {
var axo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.6');
version = new Mozilla.FlashVersion([6, 0, 21]);
axo.AllowScriptAccess = 'always';
} catch (e) {
if (version.major == 6) {
return version;
}
}
try {
axo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash');
} catch (e) {}
}
if (axo != null) {
version = new Mozilla.FlashVersion(axo.GetVariable('$version').split(' ')[1].split(','));
}
}
}
return version;
};
Mozilla.FlashVersion = function(version)
{
this.major = version[0] != null ? parseInt(version[0]) : 0;
this.minor = version[1] != null ? parseInt(version[1]) : 0;
this.rev = version[2] != null ? parseInt(version[2]) : 0;
};
Mozilla.FlashVersion.prototype.isValid = function(version)
{
if (version instanceof Array) {
version = new Mozilla.FlashVersion(version);
}
if (this.major < version.major) {
return false;
}
if (this.major > version.major) {
return true;
}
if (this.minor < version.minor) {
return false;
}
if (this.minor > version.minor) {
return true;
}
if (this.rev < version.rev) {
return false;
}
return true;
};
// init flash version
Mozilla.VideoPlayer.flash_verison = Mozilla.VideoPlayer.getFlashVersion();
// }}}