Repository navigation
Expand file tree
/
Copy pathmetadata.js
More file actions
338 lines (303 loc) · 12.1 KB
/
Copy pathmetadata.js
File metadata and controls
338 lines (303 loc) · 12.1 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
// This file is part of the Gaia Video app.
//
// It includes functions to obtain metadata for video files. The most
// important parts of this metadata are obtained by loading the video into
// an offscreen <video> element.
//
// The phones we want to run on have only a single hardware h.264 video
// decoder, and gecko is not yet smart enough to share the hardware among
// all the <video> elements that want to use it. So all apps (camera,
// gallery and video) that have permission to use the video hardware must be
// careful to only use it while they are in the foreground and to relinquish
// it when they go to the background.
//
// The video app wants to use <video> elements for metadata parsing and for
// playing videos for the user. It can't do both of these things at the same
// time. The MediaDB metadata parsing architecture assumes that it can,
// however. So the Video app does not pass a metadata parser function to
// MediaDB. MediaDB notifies the app about new video files but does not
// include any metadata with those notifications. Instead, we use the
// queueing and metadata parsing functions in this file to handle metadata
// parsing in an interruptible way.
//
// When we have a new video file that needs metadata, we pass it to
// addToMetadataQueue(). When we need to stop parsing metadata (because we
// want to play a video or because we went to the background) we call
// stopParsingMetadata(). And when we're able to parse metadata again we
// call startParsingMetadata(). When a queued video is processed and its
// metadata is ready, it is saved to MediaDB, and then addVideo() is invoked
// to display the video title and thumbnail.
//
var metadataQueue = [];
var processingQueue = false;
var stopParsingMetadataCallback = null;
// This function queues a fileinfo object with no metadata. When the app is
// able it will obtain metadata for the video and pass the updated fileinfo
// to addVideo() so that it becomes visible to the user.
function addToMetadataQueue(fileinfo) {
metadataQueue.push(fileinfo);
startParsingMetadata();
}
// Start or resume metadata parsing, if conditions are right
function startParsingMetadata() {
// If there is no work queued, or if we're already working, return right away
if (processingQueue || metadataQueue.length === 0)
return;
// Don't parse metadata if we are not the foreground app. When we're
// in the background we need to allow the foreground app to use the
// video hardware. Also, if the video player is showing then we
// can't parse metadata because we're already using the video hardware.
if (document.hidden || playerShowing) {
return;
}
// Start processing the queue
processingQueue = true;
showThrobber();
processFirstQueuedItem();
}
// Stop parsing metadata. If a callback is specified, call it when stopped.
function stopParsingMetadata(callback) {
// If we're not processing metadata, just call the callback right away
if (!processingQueue) {
if (callback)
callback();
return;
}
// Otherwise, request a stop.
// This variable acts as a flag to prevent processFirstQueuedItem from
// creating any more metadatas.
stopParsingMetadataCallback = callback || true;
}
// If we've been asked to stop processing, then run the callback, and stop.
// Otherwise, if there is nothing in the queue, do nothing. Otherwise, take
// the first item off the queue, obtain metadata for it, save the metadata
// to the database, and then call addVideo() to display the newly processed
// video to the user. Finally, use setTimeout() to schedule a new call to
// this same function.
//
// Are we guaranteed to always stop and to always call the callback?
//
function processFirstQueuedItem() {
// If the stop flag is set, call the callback and stop
if (stopParsingMetadataCallback) {
var callback = stopParsingMetadataCallback;
stopParsingMetadataCallback = null;
processingQueue = false;
hideThrobber();
if (callback !== true)
callback(); // Okay, we've stopped.
return;
}
// If there is no work queued, update dialog in case there are no playable
// videos; then return right away
if (metadataQueue.length === 0) {
processingQueue = false;
hideThrobber();
updateDialog();
return;
}
// Otherwise, take a fileinfo object from the front of the queue and
// process it with a <video> tag to obtain size, duration and a poster
// image. This happens asynchronously. When it is done, we call
// processFirstQueuedItem() again to re-check the stop flag and process
// the next item on the queue.
var fileinfo = metadataQueue.shift();
videodb.getFile(fileinfo.name, function(file) {
getMetadata(file, function(metadata) {
// Associate the metadata with this fileinfo object
fileinfo.metadata = metadata;
// Save it to the database
videodb.updateMetadata(fileinfo.name, metadata);
// Create and insert a thumbnail for the video
if (metadata.isVideo)
addVideo(fileinfo);
// And process the next video in the queue
setTimeout(processFirstQueuedItem);
});
}, function(err) {
console.error('getFile error: ', fileinfo.name, err);
processFirstQueuedItem();
});
}
// Given a video File object, asynchronously pass an object of metadata to
// the specified callback.
function getMetadata(videofile, callback) {
// This is the video element that will get the metadata for us.
// Because of an apparent bug in gecko, it needs to be here rather than
// something that is shared globally.
var offscreenVideo = document.createElement('video');
var metadata = {};
// If its a video type we don't know how to play, ignore it.
if (!offscreenVideo.canPlayType(videofile.type)) {
metadata.isVideo = false;
callback(metadata);
return;
}
// Create a blob: url for the file. It will be revoked in unload().
var url = URL.createObjectURL(videofile);
// Load the video into an offscreen <video> element.
offscreenVideo.preload = 'metadata';
offscreenVideo.src = url;
offscreenVideo. {
// Something went wrong. Maybe the file was corrupt?
console.error("Can't play video", videofile.name, e);
metadata.isVideo = false;
unload();
callback(metadata);
};
offscreenVideo. {
// If videoWidth is 0 then this is likely an audio file (ogg / mp4)
// with an ambiguous filename extension that makes it look like a video.
// This test only works correctly if we're using a new offscreen video
// element each time. If I try to make the element a global, then it
// retains the size of the last video when a non-video is loaded.
if (!offscreenVideo.videoWidth) {
metadata.isVideo = false;
unload();
callback(metadata);
return;
}
// Otherwise it is a video!
metadata.isVideo = true;
// read title from metadata or fallback to filename.
metadata.title = readFromMetadata('title') ||
fileNameToVideoName(videofile.name);
// The video element tells us the video duration and size.
metadata.duration = offscreenVideo.duration;
metadata.width = offscreenVideo.videoWidth;
metadata.height = offscreenVideo.videoHeight;
// If this is a .3gp video file, look for its rotation matrix and
// then create the thumbnail. Otherwise set rotation to 0 and
// create the thumbnail immediately. getVideoRotation is defined
// in shared/js/media/get_video_rotation.js
if (/.3gp$/.test(videofile.name)) {
getVideoRotation(videofile, function(rotation) {
if (typeof rotation === 'number')
metadata.rotation = rotation;
else if (typeof rotation === 'string')
console.warn('Video rotation:', rotation);
createThumbnail();
});
} else {
metadata.rotation = 0;
createThumbnail();
}
};
// The text case of key in metadata is not always lower or upper cases. That
// depends on the creation tools. This function helps to read keys in lower
// cases and returns the value of corresponding key.
function readFromMetadata(lowerCaseKey) {
var tags = offscreenVideo.mozGetMetadata();
for (var key in tags) {
// to lower case and match it.
if (key.toLowerCase() === lowerCaseKey) {
return tags[key];
}
}
// no matched key, return undefined.
return;
}
function createThumbnail() {
// Videos often begin with a black screen, so skip ahead 5 seconds
// or 1/10th of the video, whichever is shorter in the hope that we'll
// get a more interesting thumbnail that way.
// Because of bug 874692, corrupt video files may not be seekable,
// and may not fire an error event, so if we aren't able to seek
// after a certain amount of time, we'll abort and assume that the
// video is invalid.
offscreenVideo.currentTime = Math.min(5, offscreenVideo.duration / 10);
var failed = false; // Did seeking fail?
var timeout = setTimeout(fail, 10000); // Fail after 10 seconds
offscreenVideo. // Or if we get an error event
function fail() { // Seeking failure case
console.warn('Seek failed while creating thumbnail for', videofile.name,
'. Ignoring corrupt file.');
failed = true;
clearTimeout(timeout);
offscreenVideo.>
metadata.isVideo = false;
unload();
callback(metadata);
}
offscreenVideo. { // Seeking success case
if (failed) // Avoid race condition: if we already failed, stop now
return;
clearTimeout(timeout);
captureFrame(offscreenVideo, metadata, function(poster) {
if (poster === null) {
// If something goes wrong in captureFrame, it probably means that
// this is not a valid video. In any case, if we don't have a
// thumbnail image we shouldn't try to display it to the user.
// XXX: See bug 869289: maybe we should not fail here.
fail();
}
else {
metadata.poster = poster;
unload();
callback(metadata); // We've got all the metadata we need now.
}
});
};
}
// Free the resources being used by the offscreen video element
function unload() {
URL.revokeObjectURL(offscreenVideo.src);
offscreenVideo.removeAttribute('src');
offscreenVideo.load();
}
// Derive the video title from its filename.
function fileNameToVideoName(filename) {
filename = filename.split('/').pop()
.replace(/\.(webm|ogv|ogg|mp4|3gp)$/, '')
.replace(/[_\.]/g, ' ');
return filename.charAt(0).toUpperCase() + filename.slice(1);
}
}
function captureFrame(player, metadata, callback) {
try {
var canvas = document.createElement('canvas');
var ctx = canvas.getContext('2d');
canvas.width = THUMBNAIL_WIDTH;
canvas.height = THUMBNAIL_HEIGHT;
var vw = player.videoWidth, vh = player.videoHeight;
var tw, th;
// If a rotation is specified, rotate the canvas context
switch (metadata.rotation) {
case 90:
ctx.translate(THUMBNAIL_WIDTH, 0);
ctx.rotate(Math.PI / 2);
tw = THUMBNAIL_HEIGHT;
th = THUMBNAIL_WIDTH;
break;
case 180:
ctx.translate(THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT);
ctx.rotate(Math.PI);
tw = THUMBNAIL_WIDTH;
th = THUMBNAIL_HEIGHT;
break;
case 270:
ctx.translate(0, THUMBNAIL_HEIGHT);
ctx.rotate(-Math.PI / 2);
tw = THUMBNAIL_HEIGHT;
th = THUMBNAIL_WIDTH;
break;
default:
tw = THUMBNAIL_WIDTH;
th = THUMBNAIL_HEIGHT;
break;
}
// Figure out what portion of the video we want to draw into the thumbnail
var scale = Math.min(vw / tw, vh / th);
var w = tw * scale, h = th * scale;
var x = (vw - w) / 2, y = (vh - h) / 2;
// Draw the current video frame into the image
ctx.drawImage(player, x, y, w, h, 0, 0, tw, th);
// Convert it to an image file and pass to the callback.
canvas.toBlob(callback, 'image/jpeg');
}
catch (e) {
console.error('Exception in captureFrame:', e, e.stack);
callback(null);
}
}