forked from rsms/html5-video
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.hvideo.js
419 lines (395 loc) · 11.7 KB
/
jquery.hvideo.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
/*
* HTML5 video player.
*
* @author Rasmus Andersson <http://hunch.se/>
* @license MIT (see file LICENSE)
*/
if (window.console === undefined)window.console={log:function(){}};
jQuery.fn.hvideo = function(options){
var hvideo = {
/** if <this> should be resized to fit the actual video size */
autoresize: false
};
if (typeof options === 'object') {
for (var k in options)
hvideo[k] = options[k];
}
var rootq = this;
var vq = rootq.find('video');
var ctrlq = rootq.find('controls');
var exctrlq = ctrlq.find('extended');
var posbarq = ctrlq.find('bar.position');
var totbarq = ctrlq.find('bar.total');
var bufbarq = ctrlq.find('bar.buffered');
var unbbarq = ctrlq.find('bar.unbuffered');
posbarq.width(0);
bufbarq.width(0);
// hvideo functions
hvideo.video = vq.get(0);
hvideo.barRect = totbarq.rect();
hvideo.zoomed = false;
hvideo.animatedZoom = true;
hvideo.layout = function() {
var vr = vq.rect();
var margin = 10;
// set control rect to video rect
ctrlq.rect(vr);
// extended controls
exctrlq.width(vr.width - parseInt(exctrlq.css('left')) - margin);
// update progress bar
var prevBarRect = hvideo.barRect;
hvideo.barRect = totbarq.rect();
unbbarq.width(hvideo.barRect.width);
bufbarq.width((bufbarq.width()/prevBarRect.width)*hvideo.barRect.width);
posbarq.width((posbarq.width()/prevBarRect.width)*hvideo.barRect.width);
}
hvideo.togglePlay = function() {
if (hvideo.video.paused)
hvideo.video.play();
else
hvideo.video.pause();
}
hvideo.toggleZoomed = function(ev){
if (hvideo.video.webkitEnterFullScreen !== undefined) {
return hvideo.video.webkitEnterFullScreen();
}
if (!hvideo.zoomed) {
// todo: in zoomed mode, hide mouse after N time of
// inactivity (track movement when playing).
hvideo.zoomed = true;
hvideo.zoomOriginalRect = vq.rect();
hvideo.zoomOriginalZIndex = vq.css('z-index');
hvideo.zoomOriginalPosition = vq.css('position');
rootq.addClass('zoomed');
var z = 1000;
vq.css('z-index', z);
ctrlq.css('z-index', z+1);
unbbarq.css('z-index', z+2);
bufbarq.css('z-index', z+3);
totbarq.css('z-index', z+4);
posbarq.css('z-index', z+5);
hvideo.updateSizeInZoomMode(null, true);
$(window).bind('resize', hvideo.updateSizeInZoomMode);
}
else {
hvideo.zoomed = false;
rootq.removeClass('zoomed');
$(window).unbind('resize', hvideo.updateSizeInZoomMode);
var z = parseInt(hvideo.zoomOriginalZIndex);
vq.css('z-index', hvideo.zoomOriginalZIndex);
ctrlq.css('z-index', z+1);
unbbarq.css('z-index', z+2);
bufbarq.css('z-index', z+3);
totbarq.css('z-index', z+4);
posbarq.css('z-index', z+5);
/* CURRENTLY BROKEN
if (animate === true && hvideo.animatedZoom) {
vq.animate({
left: hvideo.zoomOriginalRect.x+'px',
top: hvideo.zoomOriginalRect.y+'px',
width: hvideo.zoomOriginalRect.width+'px',
height: hvideo.zoomOriginalRect.height+'px'
}, 120, 'swing', function(){
vq.trigger('resize');
});
}
else {*/
vq.rect(hvideo.zoomOriginalRect);
vq.trigger('resize');
//}
}
}
hvideo.formatTime = function(seconds) {
var m = parseInt(seconds/60);
var s = parseInt(seconds - m);
return ''+m+':'+(s > 9 ? s : '0'+s);
}
hvideo.seek = function(pos, dry) {
var tr = hvideo.video.seekable;
if (dry === undefined) dry = false;
if (tr.length === 0) {
// not seekable
return false;
}
else if (tr.length === 1 && tr.start(0)+hvideo.video.duration === tr.end(0)) {
// can seek anywhere
if (!dry)
hvideo.video.currentTime = hvideo.video.duration * pos;
return hvideo.video.duration * pos;
}
else {
// can only seek to specific ranges -- find closest match
var requestedTime = hvideo.video.duration * pos;
var closestTime = null;
for (var i=0; i<tr.length; i++) {
var start = tr.start(i);
var end = tr.end(i);
if (closestTime === null)
closestTime = start;
if (start >= requestedTime && end <= requestedTime) {
closestTime = requestedTime;
break;
}
var d1 = max(start,requestedTime)-min(start,requestedTime);
var d2 = max(start,closestTime)-min(start,closestTime);
if (d1 < d2)
closestTime = start;
// todo: better matching by also looking at distance to end
}
if (closestTime !== null) {
if (!dry)
hvideo.video.currentTime = closestTime;
return closestTime;
}
}
return false;
}
hvideo.isScrubbing = false;
hvideo.updatePosition = function(currentTime) {
if (currentTime === undefined)
currentTime = hvideo.video.currentTime;
posbarq.width(hvideo.barRect.width * (currentTime / hvideo.video.duration));
posbarq.find('p.meta').text(hvideo.formatTime(currentTime));
}
hvideo.updateSizeInZoomMode = function(ev, animate) {
var wq = $(window);
var r = {x:0, y:0, width:wq.width(), height:wq.height()};
if (animate === true && hvideo.animatedZoom) {
var vr = vq.rect();
vq.css({
position: 'absolute',
left: vr.x+'px',
top: vr.y+'px'
});
vq.animate({
left: r.x+'px',
top: r.y+'px',
width: r.width+'px',
height: r.height+'px'
}, 120, 'swing', function(){
vq.trigger('resize');
});
}
else {
vq.rect(r);
vq.trigger('resize');
}
}
hvideo.ev2pos = function(ev) {
var pos = (ev.clientX - hvideo.barRect.x) / hvideo.barRect.width;
var threshold = 0.01;
if (pos <= threshold)
pos = 0.0;
else if (1.0-pos <= threshold)
pos = 1.0;
return pos;
}
hvideo.inRect = function(x,y, r) {
return (x >= r.x && x <= r.x+r.width && y >= r.y && y <= r.y+r.height);
}
hvideo.scheduleCtrlFadeOut = function() {
clearInterval(ctrlq.data('hvideo_fadeout_timer'));
ctrlq.data('hvideo_fadeout_timer', setTimeout(function(){
clearInterval(ctrlq.data('hvideo_fadeout_timer'));
ctrlq.fadeOut(100);
}, 200));
}
hvideo.setCursor = function(name) { // xxx wip
rootq.css('cursor', name);
ctrlq.css('cursor', name);
}
// save ref to hvideo struct in root element
rootq.data('hvideo', hvideo);
// controls events
ctrlq.mouseover(function(ev){
clearInterval(ctrlq.data('hvideo_fadeout_timer'));
}).mouseout(function(ev){
if (!hvideo.video.paused)
hvideo.scheduleCtrlFadeOut();
});
// play/pause button
ctrlq.find('button.play-pause').click(function(ev){
hvideo.togglePlay();
});
// mute audio button
ctrlq.find('button.mute-audio').click(function(ev){
hvideo.video.muted = !hvideo.video.muted;
});
// zoom control
ctrlq.bind('dblclick', function(ev) {
// skip if double-clicked in controls rect
var r = exctrlq.rect();
var r2 = ctrlq.rect();
r.x = r2.x;
r.width = r2.width;
if (!hvideo.inRect(ev.clientX, ev.clientY, r))
hvideo.toggleZoomed();
});
ctrlq.find('button.zoom').click(hvideo.toggleZoomed);
// progress seek
var seekfunc = function(ev){
hvideo.seek(hvideo.ev2pos(ev));
//console.log(ev, ev.clientX, ev.clientY, pos);
}
posbarq.click(seekfunc);
totbarq.click(seekfunc);
// progress scrubbing
var scrubSeekDelay = 20;
var scrubSeekDelayTimer = null;
var scrubVideoWasPaused = hvideo.video.paused;
var scrubfunc = function(ev){
var pos = hvideo.ev2pos(ev);
// only do coninuous seek if we have it buffered
var seektime = hvideo.video.duration * pos;
var tr = hvideo.video.buffered;
if (tr.length && seektime >= tr.start(0) && seektime <= tr.end(0)) {
// todo: support multiple TimeRange's
clearInterval(scrubSeekDelayTimer);
scrubSeekDelayTimer = setTimeout(function(){ hvideo.seek(pos) }, scrubSeekDelay);
}
hvideo.updatePosition(hvideo.video.duration * pos);
}
var stopscrubfunc = function(ev) {
hvideo.isScrubbing = false;
totbarq.unbind('mousemove', scrubfunc);
posbarq.unbind('mousemove', scrubfunc);
hvideo.seek(hvideo.ev2pos(ev));
if (!scrubVideoWasPaused)
hvideo.video.play();
}
var startscrubfunc = function(ev) {
scrubVideoWasPaused = hvideo.video.paused;
if (!scrubVideoWasPaused)
hvideo.video.pause();
hvideo.isScrubbing = true;
totbarq.mousemove(scrubfunc);
posbarq.mousemove(scrubfunc);
}
totbarq.mousedown(startscrubfunc);
posbarq.mousedown(startscrubfunc);
totbarq.mouseup(stopscrubfunc);
posbarq.mouseup(stopscrubfunc);
// todo: better solution
// keypress is not emmitted on the ctrl, video or any other element.
$(window).keypress(function(ev){
if (ev.which === 32) {
hvideo.togglePlay();
if (!hvideo.video.paused)
hvideo.scheduleCtrlFadeOut();
}
});
$(document).keydown(function(ev){
if (ev.which === 27 && hvideo.zoomed) {
// Ugly because we need to work around some layouting bugs
// Still quite buggy, for instance when video is paused and
// ctrl is not visible, they will never again be visible.
var op = ctrlq.css('opacity');
ctrlq.css('opacity', 0.001);
ctrlq.show();
hvideo.toggleZoomed();
ctrlq.fadeOut(100, function(){
ctrlq.css('opacity', 1.0);
});
}
});
// video events
vq.mouseover(function(ev){
if (!this.paused)
ctrlq.fadeIn(100);
}).bind('resize', function(ev){
hvideo.layout();
if (hvideo.zoomed)
ctrlq.find('button.zoom').addClass('zoomed');
else
ctrlq.find('button.zoom').removeClass('zoomed');
}).bind('canplaythrough', function(ev){
unbbarq.hide();
bufbarq.width(hvideo.barRect.width);
}).bind('durationchange', function(ev){
totbarq.find('p.meta').text(hvideo.formatTime(this.duration));
// todo: update/redraw all bars
}).bind('ended', function(ev){
this.pause();
}).bind('error', function(ev){
console.log('error -- failed to load video', ev);
}).bind('loadedmetadata', function(ev){
if (hvideo.autoresize) {
vq.width(hvideo.video.videoWidth).height(hvideo.video.videoHeight);
vq.trigger('resize');
}
}).bind('volumechange', function(ev){
if (hvideo.video.muted)
ctrlq.find('button.mute-audio').addClass('muted');
else
ctrlq.find('button.mute-audio').removeClass('muted');
}).bind('play', function(ev){
// will play
if (this.ended)
hvideo.seek(0);
}).bind('playing', function(ev){
ctrlq.find('button.play-pause').addClass('playing').removeClass('paused');
}).bind('pause', function(ev){
ctrlq.find('button.play-pause').addClass('paused').removeClass('playing');
clearInterval(ctrlq.data('hvideo_fadeout_timer'));
ctrlq.fadeIn(100);
}).bind('progress', function(ev){
var tr = this.buffered;
if (tr.length) {
// todo: support multiple TimeRange's
bufbarq.width(hvideo.barRect.width *
((this.buffered.end(0)-this.buffered.start(0)) / this.duration));
}
}).bind('timeupdate', function(ev){
if (!hvideo.isScrubbing)
hvideo.updatePosition();
})/* xxx wip:
.bind('waiting', function(ev){
ctrlq.find('button.play-pause').css('cursor', 'progress');
}).bind('playing', function(ev){
ctrlq.find('button.play-pause').css('cursor', 'auto');
})*/;
hvideo.layout();
return this;
}
// geometry: global frame
if(jQuery.fn.rect===undefined)jQuery.fn.rect = function(setrect){
if (setrect !== undefined) {
return this.css({
left: parseInt(setrect.x)+'px',
top: parseInt(setrect.y)+'px',
width: parseInt(setrect.width)+'px',
height: parseInt(setrect.height)+'px',
position: 'absolute'
});
}
else {
var p = this.position();
return {x:p.left, y:p.top, width:this.width(), height:this.height()};
}
}
// geometry: size
if(jQuery.fn.dimensions===undefined)jQuery.fn.dimensions = function(setsize){
if (setsize !== undefined) {
return this.css({
width: parseInt(setsize.width)+'px',
height: parseInt(setsize.height)+'px'
});
}
else {
return {width:this.width(), height:this.height()};
}
}
// geometry: position
if(jQuery.fn.origin===undefined)jQuery.fn.origin = function(setpoint){
if (setpoint !== undefined) {
return this.css({
left: parseInt(setpoint.x)+'px',
top: parseInt(setpoint.y)+'px',
position: 'absolute'
});
}
else {
var p = this.position();
return {x:p.left, y:p.top};
}
}