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 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 | 128 |
129 /** | 129 /** |
130 * @constructor | 130 * @constructor |
131 */ | 131 */ |
132 function VideoPlayer() { | 132 function VideoPlayer() { |
133 this.controls_ = null; | 133 this.controls_ = null; |
134 this.videoElement_ = null; | 134 this.videoElement_ = null; |
135 this.videos_ = null; | 135 this.videos_ = null; |
136 this.currentPos_ = 0; | 136 this.currentPos_ = 0; |
137 | 137 |
| 138 this.currentCast_ = null; |
| 139 |
138 Object.seal(this); | 140 Object.seal(this); |
139 } | 141 } |
140 | 142 |
141 VideoPlayer.prototype = { | 143 VideoPlayer.prototype = { |
142 get controls() { | 144 get controls() { |
143 return this.controls_; | 145 return this.controls_; |
144 } | 146 } |
145 }; | 147 }; |
146 | 148 |
147 /** | 149 /** |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
265 videoPlayerElement.setAttribute('first-video', true); | 267 videoPlayerElement.setAttribute('first-video', true); |
266 else | 268 else |
267 videoPlayerElement.removeAttribute('first-video'); | 269 videoPlayerElement.removeAttribute('first-video'); |
268 | 270 |
269 // Re-enables ui and hides error message if already displayed. | 271 // Re-enables ui and hides error message if already displayed. |
270 document.querySelector('#video-player').removeAttribute('disabled'); | 272 document.querySelector('#video-player').removeAttribute('disabled'); |
271 document.querySelector('#error').removeAttribute('visible'); | 273 document.querySelector('#error').removeAttribute('visible'); |
272 this.controls.inactivityWatcher.disabled = false; | 274 this.controls.inactivityWatcher.disabled = false; |
273 this.controls.decodeErrorOccured = false; | 275 this.controls.decodeErrorOccured = false; |
274 | 276 |
275 this.videoElement_ = document.createElement('video'); | 277 if (this.currentCast_) { |
276 document.querySelector('#video-container').appendChild(this.videoElement_); | 278 videoPlayerElement.setAttribute('casting', true); |
277 this.controls.attachMedia(this.videoElement_); | 279 this.videoElement_ = new CastVideoElement(); |
| 280 this.controls.attachMedia(this.videoElement_); |
278 | 281 |
279 this.videoElement_.src = url; | 282 document.querySelector('#cast-name-label').textContent = |
| 283 loadTimeData.getString('VIDEO_PLAYER_PLAYING_ON');; |
| 284 document.querySelector('#cast-name').textContent = this.currentCast_.name; |
| 285 } else { |
| 286 videoPlayerElement.removeAttribute('casting'); |
| 287 |
| 288 this.videoElement_ = document.createElement('video'); |
| 289 document.querySelector('#video-container').appendChild(this.videoElement_); |
| 290 |
| 291 this.controls.attachMedia(this.videoElement_); |
| 292 this.videoElement_.src = url; |
| 293 } |
| 294 |
280 this.videoElement_.load(); | 295 this.videoElement_.load(); |
281 | 296 |
282 if (opt_callback) { | 297 if (opt_callback) { |
283 var handler = function(currentPos, event) { | 298 var handler = function(currentPos, event) { |
284 console.log('loaded: ', currentPos, this.currentPos_); | 299 console.log('loaded: ', currentPos, this.currentPos_); |
285 if (currentPos === this.currentPos_) | 300 if (currentPos === this.currentPos_) |
286 opt_callback(); | 301 opt_callback(); |
287 this.videoElement_.removeEventListener('loadedmetadata', handler); | 302 this.videoElement_.removeEventListener('loadedmetadata', handler); |
288 }.wrap(this, this.currentPos_); | 303 }.wrap(this, this.currentPos_); |
289 | 304 |
290 this.videoElement_.addEventListener('loadedmetadata', handler); | 305 this.videoElement_.addEventListener('loadedmetadata', handler); |
291 } | 306 } |
292 }; | 307 }; |
293 | 308 |
294 /** | 309 /** |
295 * Plays the first video. | 310 * Plays the first video. |
296 */ | 311 */ |
297 VideoPlayer.prototype.playFirstVideo = function() { | 312 VideoPlayer.prototype.playFirstVideo = function() { |
298 this.currentPos_ = 0; | 313 this.currentPos_ = 0; |
299 this.reloadCurrentVideo_(this.onFirstVideoReady_.wrap(this)); | 314 this.reloadCurrentVideo_(this.onFirstVideoReady_.wrap(this)); |
300 }; | 315 }; |
301 | 316 |
302 /** | 317 /** |
303 * Unloads the current video. | 318 * Unloads the current video. |
304 */ | 319 */ |
305 VideoPlayer.prototype.unloadVideo = function() { | 320 VideoPlayer.prototype.unloadVideo = function() { |
306 // Detach the previous video element, if exists. | 321 // Detach the previous video element, if exists. |
307 if (this.videoElement_) | 322 if (this.videoElement_ && this.videoElement_.parentNode) |
308 this.videoElement_.parentNode.removeChild(this.videoElement_); | 323 this.videoElement_.parentNode.removeChild(this.videoElement_); |
309 this.videoElement_ = null; | 324 this.videoElement_ = null; |
310 }; | 325 }; |
311 | 326 |
312 /** | 327 /** |
313 * Called when the first video is ready after starting to load. | 328 * Called when the first video is ready after starting to load. |
314 * @private | 329 * @private |
315 */ | 330 */ |
316 VideoPlayer.prototype.onFirstVideoReady_ = function() { | 331 VideoPlayer.prototype.onFirstVideoReady_ = function() { |
317 var videoWidth = this.videoElement_.videoWidth; | 332 var videoWidth = this.videoElement_.videoWidth; |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
373 * | 388 * |
374 * @param {function()=} opt_callback Completion callback. | 389 * @param {function()=} opt_callback Completion callback. |
375 * @private | 390 * @private |
376 */ | 391 */ |
377 VideoPlayer.prototype.reloadCurrentVideo_ = function(opt_callback) { | 392 VideoPlayer.prototype.reloadCurrentVideo_ = function(opt_callback) { |
378 var currentVideo = this.videos_[this.currentPos_]; | 393 var currentVideo = this.videos_[this.currentPos_]; |
379 this.loadVideo_(currentVideo.fileUrl, currentVideo.entry.name, opt_callback); | 394 this.loadVideo_(currentVideo.fileUrl, currentVideo.entry.name, opt_callback); |
380 }; | 395 }; |
381 | 396 |
382 /** | 397 /** |
| 398 * Invokes when a menuitem in the cast menu is selected. |
| 399 * @param {Object} cast Selected element in the list of casts. |
| 400 */ |
| 401 VideoPlayer.prototype.onCastSelected_ = function(cast) { |
| 402 // If the selected item is same as the current item, do nothing. |
| 403 if ((this.currentCast_ && this.currentCast_.name) === (cast && cast.name)) |
| 404 return; |
| 405 |
| 406 this.currentCast_ = cast || null; |
| 407 this.reloadCurrentVideo_(); |
| 408 }; |
| 409 |
| 410 /** |
383 * Set the list of casts. | 411 * Set the list of casts. |
384 * @param {Array.<Object>} casts List of casts. | 412 * @param {Array.<Object>} casts List of casts. |
385 */ | 413 */ |
386 VideoPlayer.prototype.setCastList = function(casts) { | 414 VideoPlayer.prototype.setCastList = function(casts) { |
387 var button = document.querySelector('.cast-button'); | 415 var button = document.querySelector('.cast-button'); |
388 var menu = document.querySelector('#cast-menu'); | 416 var menu = document.querySelector('#cast-menu'); |
389 menu.innerHTML = ''; | 417 menu.innerHTML = ''; |
390 | 418 |
| 419 // TODO(yoshiki): Handle the case that the current cast disapears. |
| 420 |
391 if (casts.length === 0) { | 421 if (casts.length === 0) { |
392 button.classList.add('hidden'); | 422 button.classList.add('hidden'); |
| 423 if (!this.currentCast_) { |
| 424 this.currentCast_ = null; |
| 425 this.reloadCurrentVideo_(); |
| 426 } |
393 return; | 427 return; |
394 } | 428 } |
395 | 429 |
| 430 var item = new cr.ui.MenuItem(); |
| 431 item.label = loadTimeData.getString('VIDEO_PLAYER_PLAY_THIS_COMPUTER'); |
| 432 item.addEventListener('activate', this.onCastSelected_.wrap(this, null)); |
| 433 menu.appendChild(item); |
| 434 |
396 for (var i = 0; i < casts.length; i++) { | 435 for (var i = 0; i < casts.length; i++) { |
397 var item = new cr.ui.MenuItem(); | 436 var item = new cr.ui.MenuItem(); |
398 item.textContent = casts[i].name; | 437 item.label = casts[i].name; |
| 438 item.addEventListener('activate', |
| 439 this.onCastSelected_.wrap(this, casts[i])); |
399 menu.appendChild(item); | 440 menu.appendChild(item); |
400 } | 441 } |
401 button.classList.remove('hidden'); | 442 button.classList.remove('hidden'); |
402 }; | 443 }; |
403 | 444 |
404 /** | 445 /** |
405 * Initialize the list of videos. | 446 * Initialize the list of videos. |
406 * @param {function(Array.<Object>)} callback Called with the video list when | 447 * @param {function(Array.<Object>)} callback Called with the video list when |
407 * it is ready. | 448 * it is ready. |
408 */ | 449 */ |
(...skipping 29 matching lines...) Expand all Loading... |
438 var initPromise = Promise.all( | 479 var initPromise = Promise.all( |
439 [new Promise(initVideos.wrap(null)), | 480 [new Promise(initVideos.wrap(null)), |
440 new Promise(initStrings.wrap(null)), | 481 new Promise(initStrings.wrap(null)), |
441 new Promise(util.addPageLoadHandler.wrap(null))]); | 482 new Promise(util.addPageLoadHandler.wrap(null))]); |
442 | 483 |
443 initPromise.then(function(results) { | 484 initPromise.then(function(results) { |
444 var videos = results[0]; | 485 var videos = results[0]; |
445 player.prepare(videos); | 486 player.prepare(videos); |
446 return new Promise(player.playFirstVideo.wrap(player)); | 487 return new Promise(player.playFirstVideo.wrap(player)); |
447 }.wrap(null)); | 488 }.wrap(null)); |
OLD | NEW |