Repository navigation
Expand file tree
/
Copy pathview.js
More file actions
446 lines (385 loc) · 13.5 KB
/
Copy pathview.js
File metadata and controls
446 lines (385 loc) · 13.5 KB
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
'use strict';
/*
* This is a stripped down version of video.js that handles the view activity
* and displays streaming videos specified by url.
*
* Unfortunately, there is a fair bit of code duplication between this
* file and video.js, but we are too close to the v1 deadline to refactor
* the shared code into a single shared file. If the video player UI changes
* those changes will have to be made in both video.js and view.js
*/
navigator.mozSetMessageHandler('activity', function viewVideo(activity) {
var dom = {}; // document elements
var playing = false;
var endedTimer;
var controlShowing = false;
var controlFadeTimeout = null;
var dragging = false;
var data = activity.source.data;
var blob = data.blob;
var type = data.type;
var url = data.url;
var title = data.title || '';
var storage; // A device storage object used by the save button
var saved = false; // Did we save it?
var endedTimer; // The workaround of bug 783512.
var videoRotation = 0;
// touch start id is the identifier of touch event. we only need to process
// events related to this id.
var touchStartID = null;
var isPausedWhileDragging;
var sliderRect;
initUI();
// If blob exists, video should be launched by open activity
if (blob) {
// The title we display for this video may be explicitly specified,
// or it might be the specified filename to save to or it might be
// the filename of the blob.
title = data.title || baseName(data.filename) || baseName(blob.name);
url = URL.createObjectURL(blob);
// If the app that initiated this activity wants us to allow the
// user to save this blob as a file, and if device storage is available
// and if there is enough free space, then display a save button.
if (data.allowSave && data.filename && checkFilename()) {
getStorageIfAvailable('videos', blob.size, function(ds) {
storage = ds;
dom.menu.hidden = false;
});
}
// to hide player because it shows in the wrong rotation.
dom.player.classList.add('hidden');
// video rotation is not parsed, parse it.
getVideoRotation(blob, function(rotation) {
if (typeof rotation === 'string') {
console.error('get video rotation error: ' + rotation);
videoRotation = 0;
} else {
videoRotation = rotation;
}
// show player when player size and rotation are correct.
dom.player.classList.remove('hidden');
// start to play the video that showPlayer also calls fitContainer.
showPlayer(url, title);
});
} else {
// In the url case, we don't need to calculate the rotation.
showPlayer(url, title);
}
// Terminate video playback when visibility is changed.
window.addEventListener('visibilitychange',
function onVisibilityChanged() {
if (document.hidden) {
done();
}
});
function initUI() {
// Fullscreen mode and inline activities don't seem to play well together
// so we'll play the video without going into fullscreen mode.
// Get all the elements we use by their id
var ids = ['player', 'fullscreen-view', 'videoControls',
'close', 'play', 'playHead',
'elapsedTime', 'video-title', 'duration-text', 'elapsed-text',
'slider-wrapper', 'spinner-overlay',
'menu', 'save', 'banner', 'message'];
ids.forEach(function createElementRef(name) {
dom[toCamelCase(name)] = document.getElementById(name);
});
function toCamelCase(str) {
return str.replace(/\-(.)/g, function replacer(str, p1) {
return p1.toUpperCase();
});
}
dom.player.mozAudioChannelType = 'content';
// show|hide controls over the player
dom.videoControls.addEventListener('touchstart', handlePlayerTouchStart);
dom.videoControls.addEventListener('touchmove', handlePlayerTouchMove);
dom.videoControls.addEventListener('touchend', handlePlayerTouchEnd);
// Rescale when window size changes. This should get called when
// orientation changes.
window.addEventListener('resize', function() {
if (dom.player.readyState !== dom.player.HAVE_NOTHING) {
VideoUtils.fitContainer(dom.fullscreenView, dom.player,
videoRotation || 0);
}
});
dom.player.addEventListener('timeupdate', timeUpdated);
// showing + hiding the loading spinner
dom.player.addEventListener('waiting', showSpinner);
dom.player.addEventListener('playing', hideSpinner);
dom.player.addEventListener('play', hideSpinner);
dom.player.addEventListener('pause', hideSpinner);
dom.player.addEventListener('ended', playerEnded);
dom.player.addEventListener('canplaythrough', hideSpinner);
// Set the 'lang' and 'dir' attributes to <html> when the page is translated
window.addEventListener('localized', function showBody() {
document.documentElement.lang = navigator.mozL10n.language.code;
document.documentElement.dir = navigator.mozL10n.language.direction;
});
}
function checkFilename() {
var dotIdx = data.filename.lastIndexOf('.');
if (dotIdx > -1) {
var ext = data.filename.substr(dotIdx + 1);
return MimeMapper.guessTypeFromExtension(ext) === blob.type;
} else {
return false;
}
}
function setControlsVisibility(visible) {
dom.videoControls.classList[visible ? 'remove' : 'add']('hidden');
controlShowing = visible;
if (visible) {
// update elapsed time and slider while showing.
updateSlider();
}
}
function handlePlayerTouchStart(event) {
// If we have a touch start event, we don't need others.
if (null != touchStartID) {
return;
}
touchStartID = event.changedTouches[0].identifier;
// If we interact with the controls before they fade away,
// cancel the fade
if (controlFadeTimeout) {
clearTimeout(controlFadeTimeout);
controlFadeTimeout = null;
}
if (!controlShowing) {
setControlsVisibility(true);
// add preventDefault to prevent click dispatching.
event.preventDefault();
return;
}
if (event.target == dom.play) {
if (dom.play.classList.contains('paused'))
play();
else
pause();
} else if (event.target == dom.close) {
done();
event.preventDefault();
} else if (event.target == dom.save) {
save();
} else if (event.target == dom.sliderWrapper) {
dragSlider(event);
} else {
setControlsVisibility(false);
}
}
function done() {
pause();
// Release any video resources
dom.player.removeAttribute('src');
dom.player.load();
// End the activity
activity.postResult({saved: saved});
}
function save() {
// Hide the menu that holds the save button: we can only save once
dom.menu.hidden = true;
// XXX work around bug 870619
dom.videoTitle.textContent = dom.videoTitle.textContent;
getUnusedFilename(storage, data.filename, function(filename) {
var savereq = storage.addNamed(blob, filename);
savereq. {
// Remember that it has been saved so we can pass this back
// to the invoking app
saved = filename;
// And tell the user
showBanner(navigator.mozL10n.get('saved', { title: title }));
};
savereq. {
// XXX we don't report this to the user because it is hard to
// localize.
console.error('Error saving', filename, e);
};
});
}
// show video player
function showPlayer(url, title) {
dom.videoTitle.textContent = title || '';
dom.player.src = url;
dom.player. {
dom.durationText.textContent = MediaUtils.formatDuration(
dom.player.duration);
timeUpdated();
dom.play.classList.remove('paused');
VideoUtils.fitContainer(dom.fullscreenView, dom.player,
videoRotation || 0);
dom.player.currentTime = 0;
// Show the controls briefly then fade out
setControlsVisibility(true);
controlFadeTimeout = setTimeout(function() {
setControlsVisibility(false);
}, 2000);
play();
};
dom.player. { URL.revokeObjectURL(url); };
dom.player. {
var errorid = '';
switch (evt.target.error.code) {
case MediaError.MEDIA_ERR_ABORTED:
// This aborted error should be triggered by the user
// so we don't have to show any error messages
return;
case MediaError.MEDIA_ERR_NETWORK:
errorid = 'error-network';
break;
case MediaError.MEDIA_ERR_DECODE:
case MediaError.MEDIA_ERR_SRC_NOT_SUPPORTED:
// If users tap some video link in an offline page
// the error code will be MEDIA_ERR_SRC_NOT_SUPPORTED
// we also prompt the unsupported error message for it
errorid = 'error-unsupported';
break;
// Is it possible to be unknown errors?
default:
errorid = 'error-unknown';
break;
}
handleError(navigator.mozL10n.get(errorid));
};
}
function handleError(msg) {
alert(msg);
done();
}
function play() {
// Switch the button icon
dom.play.classList.remove('paused');
// Start playing
dom.player.play();
playing = true;
}
function pause() {
// Switch the button icon
dom.play.classList.add('paused');
// Stop playing the video
dom.player.pause();
playing = false;
}
// Update the progress bar and play head as the video plays
function timeUpdated() {
if (controlShowing) {
// We can't update a progress bar if we don't know how long
// the video is. It is kind of a bug that the <video> element
// can't figure this out for ogv videos.
if (dom.player.duration === Infinity || dom.player.duration === 0) {
return;
}
updateSlider();
}
// Since we don't always get reliable 'ended' events, see if
// we've reached the end this way.
// See: https://bugzilla.mozilla.org/show_bug.cgi?id=783512
// If we're within 1 second of the end of the video, register
// a timeout a half a second after we'd expect an ended event.
if (!endedTimer) {
if (!dragging && dom.player.currentTime >= dom.player.duration - 1) {
var timeUntilEnd = (dom.player.duration - dom.player.currentTime + .5);
endedTimer = setTimeout(playerEnded, timeUntilEnd * 1000);
}
} else if (dragging && dom.player.currentTime < dom.player.duration - 1) {
// If there is a timer set and we drag away from the end, cancel the timer
clearTimeout(endedTimer);
endedTimer = null;
}
}
function playerEnded() {
if (dragging) {
return;
}
if (endedTimer) {
clearTimeout(endedTimer);
endedTimer = null;
}
dom.player.currentTime = 0;
pause();
}
function updateSlider() {
var percent = (dom.player.currentTime / dom.player.duration) * 100;
if (isNaN(percent)) // this happens when we end the activity
return;
percent += '%';
dom.elapsedText.textContent = MediaUtils.formatDuration(
dom.player.currentTime);
dom.elapsedTime.style.width = percent;
// Don't move the play head if the user is dragging it.
if (!dragging)
dom.playHead.style.left = percent;
}
// handle drags on the time slider
function dragSlider(e) {
isPausedWhileDragging = dom.player.paused;
dragging = true;
// calculate the slider wrapper size for slider dragging.
sliderRect = dom.sliderWrapper.getBoundingClientRect();
// We can't do anything if we don't know our duration
if (dom.player.duration === Infinity)
return;
if (!isPausedWhileDragging) {
dom.player.pause();
}
handlePlayerTouchMove(e);
}
function handlePlayerTouchEnd(event) {
// We don't care the event not related to touchStartID
if (!event.changedTouches.identifiedTouch(touchStartID)) {
return;
}
touchStartID = null;
if (!dragging) {
// We don't need to do anything without dragging.
return;
}
dragging = false;
dom.playHead.classList.remove('active');
if (dom.player.currentTime === dom.player.duration) {
pause();
} else if (!isPausedWhileDragging) {
dom.player.play();
}
}
function handlePlayerTouchMove(event) {
if (!dragging) {
return;
}
var touch = event.changedTouches.identifiedTouch(touchStartID);
// We don't care the event not related to touchStartID
if (!touch) {
return;
}
var pos = (touch.clientX - sliderRect.left) / sliderRect.width;
pos = Math.max(pos, 0
50E0
);
pos = Math.min(pos, 1);
var percent = pos * 100 + '%';
dom.playHead.classList.add('active');
dom.playHead.style.left = percent;
dom.elapsedTime.style.width = percent;
dom.player.currentTime = dom.player.duration * pos;
dom.elapsedText.textContent = MediaUtils.formatDuration(
dom.player.currentTime);
}
function showBanner(msg) {
dom.message.textContent = msg;
dom.banner.hidden = false;
setTimeout(function() {
dom.banner.hidden = true;
}, 3000);
}
// Strip directories and just return the base filename
function baseName(filename) {
if (!filename)
return '';
return filename.substring(filename.lastIndexOf('/') + 1);
}
function showSpinner() {
if (!blob) {
dom.spinnerOverlay.classList.remove('hidden');
}
}
function hideSpinner() {
dom.spinnerOverlay.classList.add('hidden');
}
});