OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 'use strict'; | |
6 | |
7 /** | |
8 * Display error message. | |
9 * @param {string} message Message id. | |
10 */ | |
11 function showErrorMessage(message) { | |
12 var errorBanner = document.querySelector('#error'); | |
13 errorBanner.textContent = | |
14 loadTimeData.getString(message); | |
15 errorBanner.setAttribute('visible', 'true'); | |
16 | |
17 // The window is hidden if the video has not loaded yet. | |
18 chrome.app.window.current().show(); | |
19 } | |
20 | |
21 /** | |
22 * Handles playback (decoder) errors. | |
23 */ | |
24 function onPlaybackError() { | |
25 showErrorMessage('GALLERY_VIDEO_DECODING_ERROR'); | |
26 decodeErrorOccured = true; | |
27 | |
28 // Disable inactivity watcher, and disable the ui, by hiding tools manually. | |
29 controls.inactivityWatcher.disabled = true; | |
30 document.querySelector('#video-player').setAttribute('disabled', 'true'); | |
31 | |
32 // Detach the video element, since it may be unreliable and reset stored | |
33 // current playback time. | |
34 controls.cleanup(); | |
35 controls.clearState(); | |
36 | |
37 // Avoid reusing a video element. | |
38 video.parentNode.removeChild(video); | |
39 video = null; | |
40 } | |
41 | |
42 /** | |
43 * @param {Element} playerContainer Main container. | |
44 * @param {Element} videoContainer Container for the video element. | |
45 * @param {Element} controlsContainer Container for video controls. | |
46 * @constructor | |
47 */ | |
48 function FullWindowVideoControls( | |
49 playerContainer, videoContainer, controlsContainer) { | |
50 VideoControls.call(this, | |
51 controlsContainer, | |
52 onPlaybackError, | |
53 loadTimeData.getString.bind(loadTimeData), | |
54 this.toggleFullScreen_.bind(this), | |
55 videoContainer); | |
56 | |
57 this.playerContainer_ = playerContainer; | |
58 | |
59 this.updateStyle(); | |
60 window.addEventListener('resize', this.updateStyle.bind(this)); | |
61 | |
62 document.addEventListener('keydown', function(e) { | |
63 if (e.keyIdentifier == 'U+0020') { // Space | |
64 this.togglePlayStateWithFeedback(); | |
65 e.preventDefault(); | |
66 } | |
67 if (e.keyIdentifier == 'U+001B') { // Escape | |
68 util.toggleFullScreen( | |
69 chrome.app.window.current(), | |
70 false); // Leave the full screen mode. | |
71 e.preventDefault(); | |
72 } | |
73 }.bind(this)); | |
74 | |
75 // TODO(mtomasz): Simplify. crbug.com/254318. | |
76 videoContainer.addEventListener('click', function(e) { | |
77 if (e.ctrlKey) { | |
78 this.toggleLoopedModeWithFeedback(true); | |
79 if (!this.isPlaying()) | |
80 this.togglePlayStateWithFeedback(); | |
81 } else { | |
82 this.togglePlayStateWithFeedback(); | |
83 } | |
84 }.bind(this)); | |
85 | |
86 this.inactivityWatcher_ = new MouseInactivityWatcher(playerContainer); | |
87 this.__defineGetter__('inactivityWatcher', function() { | |
88 return this.inactivityWatcher_; | |
89 }); | |
90 | |
91 this.inactivityWatcher_.check(); | |
92 } | |
93 | |
94 FullWindowVideoControls.prototype = { __proto__: VideoControls.prototype }; | |
95 | |
96 /** | |
97 * Save the current state so that it survives page/app reload. | |
98 */ | |
99 FullWindowVideoControls.prototype.onPlayStateChanged = function() { | |
100 this.encodeState(); | |
101 }; | |
102 | |
103 /** | |
104 * Restore the state after the video is loaded. | |
105 */ | |
106 FullWindowVideoControls.prototype.restorePlayState = function() { | |
107 if (!this.decodeState()) { | |
108 VideoControls.prototype.restorePlayState.apply(this, arguments); | |
109 this.play(); | |
110 } | |
111 }; | |
112 | |
113 /** | |
114 * Toggles the full screen mode. | |
115 * @private | |
116 */ | |
117 FullWindowVideoControls.prototype.toggleFullScreen_ = function() { | |
118 var appWindow = chrome.app.window.current(); | |
119 util.toggleFullScreen(appWindow, !util.isFullScreen(appWindow)); | |
120 }; | |
121 | |
122 // TODO(mtomasz): Convert it to class members: crbug.com/171191. | |
123 var decodeErrorOccured; | |
124 var video; | |
125 var controls; | |
126 var metadataCache; | |
127 var volumeManager; | |
128 var selectedEntry; | |
129 | |
130 /** | |
131 * Initialize the video player window. | |
132 */ | |
133 function loadVideoPlayer() { | |
134 document.ondragstart = function(e) { e.preventDefault() }; | |
135 | |
136 chrome.fileBrowserPrivate.getStrings(function(strings) { | |
137 loadTimeData.data = strings; | |
138 | |
139 controls = new FullWindowVideoControls( | |
140 document.querySelector('#video-player'), | |
141 document.querySelector('#video-container'), | |
142 document.querySelector('#controls')); | |
143 | |
144 volumeManager = new VolumeManagerWrapper( | |
145 VolumeManagerWrapper.DriveEnabledStatus.DRIVE_ENABLED); | |
146 volumeManager.addEventListener('externally-unmounted', | |
147 onExternallyUnmounted); | |
148 metadataCache = MetadataCache.createFull(volumeManager); | |
149 | |
150 // If the video player is starting before the first instance of the File | |
151 // Manager then it does not have access to filesystem URLs. | |
152 // Request it now. | |
153 volumeManager.ensureInitialized(reload); | |
154 var reloadVideo = function(e) { | |
155 if (decodeErrorOccured && | |
156 // Ignore shortcut keys | |
157 !e.ctrlKey && !e.altKey && !e.shiftKey && !e.metaKey) { | |
158 reload(); | |
159 e.preventDefault(); | |
160 } | |
161 }; | |
162 | |
163 document.addEventListener('keydown', reloadVideo, true); | |
164 document.addEventListener('click', reloadVideo, true); | |
165 }); | |
166 } | |
167 | |
168 /** | |
169 * Closes video player when a volume containing the played item is unmounted. | |
170 * @param {Event} event The unmount event. | |
171 */ | |
172 function onExternallyUnmounted(event) { | |
173 if (!selectedEntry) | |
174 return; | |
175 | |
176 if (volumeManager.getVolumeInfo(selectedEntry) === event.volumeInfo) | |
177 window.close(); | |
178 } | |
179 | |
180 /** | |
181 * Unload the player. | |
182 */ | |
183 function unload() { | |
184 if (volumeManager) | |
185 volumeManager.dispose(); | |
186 | |
187 if (!controls.getMedia()) | |
188 return; | |
189 | |
190 controls.savePosition(true /* exiting */); | |
191 controls.cleanup(); | |
192 } | |
193 | |
194 /** | |
195 * Reload the player. | |
196 */ | |
197 function reload() { | |
198 // Re-enable ui and hide error message if already displayed. | |
199 document.querySelector('#video-player').removeAttribute('disabled'); | |
200 document.querySelector('#error').removeAttribute('visible'); | |
201 controls.inactivityWatcher.disabled = false; | |
202 decodeErrorOccured = false; | |
203 | |
204 var url; | |
205 if (window.appState) { | |
206 util.saveAppState(); | |
207 url = window.appState.url; | |
208 } else { | |
209 url = document.location.search.substr(1); | |
210 } | |
211 | |
212 document.title = decodeURIComponent(url.split('/').pop()); | |
213 var queue = new AsyncUtil.Queue(); | |
214 | |
215 queue.run(function(callback) { | |
216 webkitResolveLocalFileSystemURL(url, | |
217 function(entry) { | |
218 selectedEntry = entry; | |
219 callback(); | |
220 }, function() { | |
221 console.warn('Failed to resolve entry for: ' + url); | |
222 callback(); | |
223 }); | |
224 }); | |
225 | |
226 | |
227 queue.run(function(callback) { | |
228 if (!selectedEntry) { | |
229 showErrorMessage('GALLERY_VIDEO_ERROR'); | |
230 return; | |
231 } | |
232 metadataCache.get(selectedEntry, 'streaming', function(streaming) { | |
233 if (streaming && !navigator.onLine) { | |
234 showErrorMessage('GALLERY_VIDEO_OFFLINE'); | |
235 return; | |
236 } | |
237 | |
238 // Detach the previous video element, if exists. | |
239 if (video) | |
240 video.parentNode.removeChild(video); | |
241 | |
242 video = document.createElement('video'); | |
243 document.querySelector('#video-container').appendChild(video); | |
244 controls.attachMedia(video); | |
245 | |
246 video.src = selectedEntry.toURL(); | |
247 video.load(); | |
248 video.addEventListener('loadedmetadata', function() { | |
249 // TODO: chrome.app.window soon will be able to resize the content area. | |
250 // Until then use approximate title bar height. | |
251 var TITLE_HEIGHT = 33; | |
252 | |
253 var aspect = video.videoWidth / video.videoHeight; | |
254 var newWidth = video.videoWidth; | |
255 var newHeight = video.videoHeight + TITLE_HEIGHT; | |
256 | |
257 var shrinkX = newWidth / window.screen.availWidth; | |
258 var shrinkY = newHeight / window.screen.availHeight; | |
259 if (shrinkX > 1 || shrinkY > 1) { | |
260 if (shrinkY > shrinkX) { | |
261 newHeight = newHeight / shrinkY; | |
262 newWidth = (newHeight - TITLE_HEIGHT) * aspect; | |
263 } else { | |
264 newWidth = newWidth / shrinkX; | |
265 newHeight = newWidth / aspect + TITLE_HEIGHT; | |
266 } | |
267 } | |
268 | |
269 var oldLeft = window.screenX; | |
270 var oldTop = window.screenY; | |
271 var oldWidth = window.outerWidth; | |
272 var oldHeight = window.outerHeight; | |
273 | |
274 if (!oldWidth && !oldHeight) { | |
275 oldLeft = window.screen.availWidth / 2; | |
276 oldTop = window.screen.availHeight / 2; | |
277 } | |
278 | |
279 var appWindow = chrome.app.window.current(); | |
280 appWindow.resizeTo(newWidth, newHeight); | |
281 appWindow.moveTo(oldLeft - (newWidth - oldWidth) / 2, | |
282 oldTop - (newHeight - oldHeight) / 2); | |
283 appWindow.show(); | |
284 }); | |
285 }); | |
286 }); | |
287 } | |
288 | |
289 util.addPageLoadHandler(loadVideoPlayer); | |
OLD | NEW |