OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 'use strict'; | 5 'use strict'; |
6 | 6 |
7 /** | 7 /** |
8 * @param {Element} playerContainer Main container. | 8 * @param {Element} playerContainer Main container. |
9 * @param {Element} videoContainer Container for the video element. | 9 * @param {Element} videoContainer Container for the video element. |
10 * @param {Element} controlsContainer Container for video controls. | 10 * @param {Element} controlsContainer Container for video controls. |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
53 return this.inactivityWatcher_; | 53 return this.inactivityWatcher_; |
54 }.wrap(this)); | 54 }.wrap(this)); |
55 | 55 |
56 this.inactivityWatcher_.check(); | 56 this.inactivityWatcher_.check(); |
57 } | 57 } |
58 | 58 |
59 FullWindowVideoControls.prototype = { __proto__: VideoControls.prototype }; | 59 FullWindowVideoControls.prototype = { __proto__: VideoControls.prototype }; |
60 | 60 |
61 /** | 61 /** |
62 * Displays error message. | 62 * Displays error message. |
| 63 * |
63 * @param {string} message Message id. | 64 * @param {string} message Message id. |
64 * @private | 65 * @private |
65 */ | 66 */ |
66 FullWindowVideoControls.prototype.showErrorMessage_ = function(message) { | 67 FullWindowVideoControls.prototype.showErrorMessage_ = function(message) { |
67 var errorBanner = document.querySelector('#error'); | 68 var errorBanner = document.querySelector('#error'); |
68 errorBanner.textContent = | 69 errorBanner.textContent = |
69 loadTimeData.getString(message); | 70 loadTimeData.getString(message); |
70 errorBanner.setAttribute('visible', 'true'); | 71 errorBanner.setAttribute('visible', 'true'); |
71 | 72 |
72 // The window is hidden if the video has not loaded yet. | 73 // The window is hidden if the video has not loaded yet. |
(...skipping 30 matching lines...) Expand all Loading... |
103 util.toggleFullScreen(appWindow, !util.isFullScreen(appWindow)); | 104 util.toggleFullScreen(appWindow, !util.isFullScreen(appWindow)); |
104 }; | 105 }; |
105 | 106 |
106 /** | 107 /** |
107 * @constructor | 108 * @constructor |
108 */ | 109 */ |
109 function VideoPlayer() { | 110 function VideoPlayer() { |
110 this.controls_ = null; | 111 this.controls_ = null; |
111 this.videoElement_ = null; | 112 this.videoElement_ = null; |
112 this.videos_ = null; | 113 this.videos_ = null; |
| 114 this.currentPos_ = 0; |
113 | 115 |
114 Object.seal(this); | 116 Object.seal(this); |
115 } | 117 } |
116 | 118 |
117 VideoPlayer.prototype = { | 119 VideoPlayer.prototype = { |
118 get controls() { | 120 get controls() { |
119 return this.controls_; | 121 return this.controls_; |
120 } | 122 } |
121 }; | 123 }; |
122 | 124 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
160 | 162 |
161 this.controls_ = new FullWindowVideoControls( | 163 this.controls_ = new FullWindowVideoControls( |
162 document.querySelector('#video-player'), | 164 document.querySelector('#video-player'), |
163 document.querySelector('#video-container'), | 165 document.querySelector('#video-container'), |
164 document.querySelector('#controls')); | 166 document.querySelector('#controls')); |
165 | 167 |
166 var reloadVideo = function(e) { | 168 var reloadVideo = function(e) { |
167 if (this.controls_.decodeErrorOccured && | 169 if (this.controls_.decodeErrorOccured && |
168 // Ignore shortcut keys | 170 // Ignore shortcut keys |
169 !e.ctrlKey && !e.altKey && !e.shiftKey && !e.metaKey) { | 171 !e.ctrlKey && !e.altKey && !e.shiftKey && !e.metaKey) { |
170 this.playVideo(); | 172 this.reloadCurrentVideo_(function() { |
| 173 this.videoElement_.play(); |
| 174 }.wrap(this)); |
171 e.preventDefault(); | 175 e.preventDefault(); |
172 } | 176 } |
173 }.wrap(this); | 177 }.wrap(this); |
174 | 178 |
| 179 var arrowRight = document.querySelector('.arrow-box .arrow.right'); |
| 180 arrowRight.addEventListener('click', this.advance_.wrap(this, 1)); |
| 181 var arrowLeft = document.querySelector('.arrow-box .arrow.left'); |
| 182 arrowLeft.addEventListener('click', this.advance_.wrap(this, 0)); |
| 183 |
| 184 var videoPlayerElement = document.querySelector('#video-player'); |
| 185 if (videos.length > 1) |
| 186 videoPlayerElement.setAttribute('multiple', true); |
| 187 else |
| 188 videoPlayerElement.removeAttribute('multiple'); |
| 189 |
175 document.addEventListener('keydown', reloadVideo, true); | 190 document.addEventListener('keydown', reloadVideo, true); |
176 document.addEventListener('click', reloadVideo, true); | 191 document.addEventListener('click', reloadVideo, true); |
177 }; | 192 }; |
178 | 193 |
179 /** | 194 /** |
180 * Unloads the player. | 195 * Unloads the player. |
181 */ | 196 */ |
182 function unload() { | 197 function unload() { |
183 if (!player.controls || !player.controls.getMedia()) | 198 if (!player.controls || !player.controls.getMedia()) |
184 return; | 199 return; |
185 | 200 |
186 player.controls.savePosition(true /* exiting */); | 201 player.controls.savePosition(true /* exiting */); |
187 player.controls.cleanup(); | 202 player.controls.cleanup(); |
188 } | 203 } |
189 | 204 |
190 /** | 205 /** |
191 * Loads the video file. | 206 * Loads the video file. |
192 * @param {string} url URL of the video file. | 207 * @param {string} url URL of the video file. |
193 * @param {string} title Title of the video file. | 208 * @param {string} title Title of the video file. |
194 * @param {function(number, number)=} opt_callback Completion callback. | 209 * @param {function()=} opt_callback Completion callback. |
195 * @private | 210 * @private |
196 */ | 211 */ |
197 VideoPlayer.prototype.loadVideo_ = function(url, title, opt_callback) { | 212 VideoPlayer.prototype.loadVideo_ = function(url, title, opt_callback) { |
198 this.unloadVideo(); | 213 this.unloadVideo(); |
199 | 214 |
200 document.title = title; | 215 document.title = title; |
201 | 216 |
202 document.querySelector('#title').innerText = title; | 217 document.querySelector('#title').innerText = title; |
203 | 218 |
| 219 var videoPlayerElement = document.querySelector('#video-player'); |
| 220 if (this.currentPos_ === (this.videos_.length - 1)) |
| 221 videoPlayerElement.setAttribute('last-video', true); |
| 222 else |
| 223 videoPlayerElement.removeAttribute('last-video'); |
| 224 |
| 225 if (this.currentPos_ === 0) |
| 226 videoPlayerElement.setAttribute('first-video', true); |
| 227 else |
| 228 videoPlayerElement.removeAttribute('first-video'); |
| 229 |
204 // Re-enables ui and hides error message if already displayed. | 230 // Re-enables ui and hides error message if already displayed. |
205 document.querySelector('#video-player').removeAttribute('disabled'); | 231 document.querySelector('#video-player').removeAttribute('disabled'); |
206 document.querySelector('#error').removeAttribute('visible'); | 232 document.querySelector('#error').removeAttribute('visible'); |
207 this.controls.inactivityWatcher.disabled = false; | 233 this.controls.inactivityWatcher.disabled = false; |
208 this.controls.decodeErrorOccured = false; | 234 this.controls.decodeErrorOccured = false; |
209 | 235 |
210 this.videoElement_ = document.createElement('video'); | 236 this.videoElement_ = document.createElement('video'); |
211 document.querySelector('#video-container').appendChild(this.videoElement_); | 237 document.querySelector('#video-container').appendChild(this.videoElement_); |
212 this.controls.attachMedia(this.videoElement_); | 238 this.controls.attachMedia(this.videoElement_); |
213 | 239 |
214 this.videoElement_.src = url; | 240 this.videoElement_.src = url; |
215 this.videoElement_.load(); | 241 this.videoElement_.load(); |
216 if (opt_callback) | 242 |
217 this.videoElement_.addEventListener('loadedmetadata', opt_callback); | 243 if (opt_callback) { |
| 244 var handler = function(currentPos, event) { |
| 245 console.log('loaded: ', currentPos, this.currentPos_); |
| 246 if (currentPos === this.currentPos_) |
| 247 opt_callback(); |
| 248 this.videoElement_.removeEventListener('loadedmetadata', handler); |
| 249 }.wrap(this, this.currentPos_); |
| 250 |
| 251 this.videoElement_.addEventListener('loadedmetadata', handler); |
| 252 } |
218 }; | 253 }; |
219 | 254 |
220 /** | 255 /** |
221 * Plays the video. | 256 * Plays the first video. |
222 */ | 257 */ |
223 VideoPlayer.prototype.playVideo = function() { | 258 VideoPlayer.prototype.playFirstVideo = function() { |
224 var currentVideo = this.videos_[0]; | 259 this.currentPos_ = 0; |
225 this.loadVideo_(currentVideo.fileUrl, | 260 this.reloadCurrentVideo_(this.onFirstVideoReady_.wrap(this)); |
226 currentVideo.entry.name, | |
227 this.onVideoReady_.wrap(this)); | |
228 }; | 261 }; |
229 | 262 |
230 /** | 263 /** |
231 * Unloads the current video. | 264 * Unloads the current video. |
232 */ | 265 */ |
233 VideoPlayer.prototype.unloadVideo = function() { | 266 VideoPlayer.prototype.unloadVideo = function() { |
234 // Detach the previous video element, if exists. | 267 // Detach the previous video element, if exists. |
235 if (this.videoElement_) | 268 if (this.videoElement_) |
236 this.videoElement_.parentNode.removeChild(this.videoElement_); | 269 this.videoElement_.parentNode.removeChild(this.videoElement_); |
237 this.videoElement_ = null; | 270 this.videoElement_ = null; |
238 }; | 271 }; |
239 | 272 |
240 /** | 273 /** |
241 * Called when the video is ready after starting to load. | 274 * Called when the first video is ready after starting to load. |
242 * @private | 275 * @private |
243 */ | 276 */ |
244 VideoPlayer.prototype.onVideoReady_ = function() { | 277 VideoPlayer.prototype.onFirstVideoReady_ = function() { |
245 // TODO: chrome.app.window soon will be able to resize the content area. | 278 // TODO: chrome.app.window soon will be able to resize the content area. |
246 // Until then use approximate title bar height. | 279 // Until then use approximate title bar height. |
247 var TITLE_HEIGHT = 33; | 280 var TITLE_HEIGHT = 33; |
248 | 281 |
249 var videoWidth = this.videoElement_.videoWidth; | 282 var videoWidth = this.videoElement_.videoWidth; |
250 var videoHeight = this.videoElement_.videoHeight; | 283 var videoHeight = this.videoElement_.videoHeight; |
251 | 284 |
252 var aspect = videoWidth / videoHeight; | 285 var aspect = videoWidth / videoHeight; |
253 var newWidth = videoWidth; | 286 var newWidth = videoWidth; |
254 var newHeight = videoHeight + TITLE_HEIGHT; | 287 var newHeight = videoHeight + TITLE_HEIGHT; |
(...skipping 23 matching lines...) Expand all Loading... |
278 var appWindow = chrome.app.window.current(); | 311 var appWindow = chrome.app.window.current(); |
279 appWindow.resizeTo(newWidth, newHeight); | 312 appWindow.resizeTo(newWidth, newHeight); |
280 appWindow.moveTo(oldLeft - (newWidth - oldWidth) / 2, | 313 appWindow.moveTo(oldLeft - (newWidth - oldWidth) / 2, |
281 oldTop - (newHeight - oldHeight) / 2); | 314 oldTop - (newHeight - oldHeight) / 2); |
282 appWindow.show(); | 315 appWindow.show(); |
283 | 316 |
284 this.videoElement_.play(); | 317 this.videoElement_.play(); |
285 }; | 318 }; |
286 | 319 |
287 /** | 320 /** |
| 321 * Advances to the next (or previous) track. |
| 322 * |
| 323 * @param {boolean} direction True to the next, false to the previous. |
| 324 * @private |
| 325 */ |
| 326 VideoPlayer.prototype.advance_ = function(direction) { |
| 327 var newPos = this.currentPos_ + (direction ? 1 : -1); |
| 328 if (0 <= newPos && newPos < this.videos_.length) { |
| 329 this.currentPos_ = newPos; |
| 330 this.reloadCurrentVideo_(function() { |
| 331 this.videoElement_.play(); |
| 332 }.wrap(this)); |
| 333 } |
| 334 }; |
| 335 |
| 336 /** |
| 337 * Reloads the current video. |
| 338 * |
| 339 * @param {function()=} opt_callback Completion callback. |
| 340 * @private |
| 341 */ |
| 342 VideoPlayer.prototype.reloadCurrentVideo_ = function(opt_callback) { |
| 343 var currentVideo = this.videos_[this.currentPos_]; |
| 344 this.loadVideo_(currentVideo.fileUrl, currentVideo.entry.name, opt_callback); |
| 345 }; |
| 346 |
| 347 /** |
288 * Initialize the list of videos. | 348 * Initialize the list of videos. |
289 * @param {function(Array.<Object>)} callback Called with the video list when | 349 * @param {function(Array.<Object>)} callback Called with the video list when |
290 * it is ready. | 350 * it is ready. |
291 **/ | 351 */ |
292 function initVideos(callback) { | 352 function initVideos(callback) { |
293 if (window.videos) { | 353 if (window.videos) { |
294 var videos = window.videos; | 354 var videos = window.videos; |
295 window.videos = null; | 355 window.videos = null; |
296 callback(videos); | 356 callback(videos); |
297 return; | 357 return; |
298 } | 358 } |
299 | 359 |
300 chrome.runtime.onMessage.addListener( | 360 chrome.runtime.onMessage.addListener( |
301 function(request, sender, sendResponse) { | 361 function(request, sender, sendResponse) { |
302 var videos = window.videos; | 362 var videos = window.videos; |
303 window.videos = null; | 363 window.videos = null; |
304 callback(videos); | 364 callback(videos); |
305 }.wrap(null)); | 365 }.wrap(null)); |
306 } | 366 } |
307 | 367 |
308 var player = new VideoPlayer(); | 368 var player = new VideoPlayer(); |
309 | 369 |
310 /** | 370 /** |
311 * Initializes the strings. | 371 * Initializes the strings. |
312 * @param {function()} callback Called when the sting data is ready. | 372 * @param {function()} callback Called when the sting data is ready. |
313 **/ | 373 */ |
314 function initStrings(callback) { | 374 function initStrings(callback) { |
315 chrome.fileBrowserPrivate.getStrings(function(strings) { | 375 chrome.fileBrowserPrivate.getStrings(function(strings) { |
316 loadTimeData.data = strings; | 376 loadTimeData.data = strings; |
317 callback(); | 377 callback(); |
318 }.wrap(null)); | 378 }.wrap(null)); |
319 } | 379 } |
320 | 380 |
321 var initPromise = Promise.all( | 381 var initPromise = Promise.all( |
322 [new Promise(initVideos.wrap(null)), | 382 [new Promise(initVideos.wrap(null)), |
323 new Promise(initStrings.wrap(null)), | 383 new Promise(initStrings.wrap(null)), |
324 new Promise(util.addPageLoadHandler.wrap(null))]); | 384 new Promise(util.addPageLoadHandler.wrap(null))]); |
325 | 385 |
326 initPromise.then(function(results) { | 386 initPromise.then(function(results) { |
327 var videos = results[0]; | 387 var videos = results[0]; |
328 player.prepare(videos); | 388 player.prepare(videos); |
329 return new Promise(player.playVideo.wrap(player)); | 389 return new Promise(player.playFirstVideo.wrap(player)); |
330 }.wrap(null)); | 390 }.wrap(null)); |
OLD | NEW |