| 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 /** | 5 /** |
| 6 * @param {!HTMLElement} playerContainer Main container. | 6 * @param {!HTMLElement} playerContainer Main container. |
| 7 * @param {!HTMLElement} videoContainer Container for the video element. | 7 * @param {!HTMLElement} videoContainer Container for the video element. |
| 8 * @param {!HTMLElement} controlsContainer Container for video controls. | 8 * @param {!HTMLElement} controlsContainer Container for video controls. |
| 9 * @constructor | 9 * @constructor |
| 10 * @struct | 10 * @struct |
| (...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 369 this.controls.attachMedia(this.videoElement_); | 369 this.controls.attachMedia(this.videoElement_); |
| 370 }.bind(this)); | 370 }.bind(this)); |
| 371 }.bind(this)); | 371 }.bind(this)); |
| 372 } else { | 372 } else { |
| 373 metrics.recordPlayType(metrics.PLAY_TYPE.LOCAL); | 373 metrics.recordPlayType(metrics.PLAY_TYPE.LOCAL); |
| 374 videoPlayerElement.removeAttribute('casting'); | 374 videoPlayerElement.removeAttribute('casting'); |
| 375 | 375 |
| 376 this.videoElement_ = document.createElement('video'); | 376 this.videoElement_ = document.createElement('video'); |
| 377 getRequiredElement('video-container').appendChild(this.videoElement_); | 377 getRequiredElement('video-container').appendChild(this.videoElement_); |
| 378 | 378 |
| 379 var videoUrl = video.toURL(); |
| 379 this.controls.attachMedia(this.videoElement_); | 380 this.controls.attachMedia(this.videoElement_); |
| 380 this.videoElement_.src = video.toURL(); | 381 var source = document.createElement('source'); |
| 382 source.src = videoUrl; |
| 383 this.videoElement_.appendChild(source); |
| 381 | 384 |
| 382 media.isAvailableForCast().then(function(result) { | 385 media.isAvailableForCast().then(function(result) { |
| 383 if (result) | 386 if (result) |
| 384 videoPlayerElement.setAttribute('castable', true); | 387 videoPlayerElement.setAttribute('castable', true); |
| 385 else | 388 else |
| 386 videoPlayerElement.removeAttribute('castable'); | 389 videoPlayerElement.removeAttribute('castable'); |
| 387 }).catch(function() { | 390 }).catch(function() { |
| 388 videoPlayerElement.setAttribute('castable', true); | 391 videoPlayerElement.setAttribute('castable', true); |
| 389 }); | 392 }); |
| 390 | 393 |
| 391 videoElementInitializePromise = Promise.resolve(); | 394 videoElementInitializePromise = this.searchSubtitle_(videoUrl) |
| 395 .then(function(subltitleUrl) { |
| 396 if (subltitleUrl) { |
| 397 var track = document.createElement('track'); |
| 398 track.src = subltitleUrl; |
| 399 track.kind = 'subtitles'; |
| 400 track.default = true; |
| 401 this.videoElement_.appendChild(track); |
| 402 } |
| 403 }.bind(this)); |
| 392 } | 404 } |
| 393 | |
| 394 videoElementInitializePromise | 405 videoElementInitializePromise |
| 395 .then(function() { | 406 .then(function() { |
| 396 var handler = function(currentPos) { | 407 var handler = function(currentPos) { |
| 397 if (currentPos === this.currentPos_) { | 408 if (currentPos === this.currentPos_) { |
| 398 if (opt_callback) | 409 if (opt_callback) |
| 399 opt_callback(); | 410 opt_callback(); |
| 400 videoPlayerElement.removeAttribute('loading'); | 411 videoPlayerElement.removeAttribute('loading'); |
| 401 } | 412 } |
| 402 | 413 |
| 403 this.videoElement_.removeEventListener('loadedmetadata', handler); | 414 this.videoElement_.removeEventListener('loadedmetadata', handler); |
| 404 }.wrap(this, this.currentPos_); | 415 }.wrap(this, this.currentPos_); |
| 405 | 416 |
| 406 this.videoElement_.addEventListener('loadedmetadata', handler); | 417 this.videoElement_.addEventListener('loadedmetadata', handler); |
| 407 | 418 |
| 408 this.videoElement_.addEventListener('play', function() { | 419 this.videoElement_.addEventListener('play', function() { |
| 409 chrome.power.requestKeepAwake('display'); | 420 chrome.power.requestKeepAwake('display'); |
| 410 this.updateInactivityWatcherState_(); | 421 this.updateInactivityWatcherState_(); |
| 411 }.wrap(this)); | 422 }.wrap(this)); |
| 412 this.videoElement_.addEventListener('pause', function() { | 423 this.videoElement_.addEventListener('pause', function() { |
| 413 chrome.power.releaseKeepAwake(); | 424 chrome.power.releaseKeepAwake(); |
| 414 this.updateInactivityWatcherState_(); | 425 this.updateInactivityWatcherState_(); |
| 415 }.wrap(this)); | 426 }.wrap(this)); |
| 416 | 427 // TODO(ryoh): |
| 417 this.videoElement_.load(); | 428 // If you modify the video element that is already inserted, |
| 429 // you have to call load() method. |
| 430 // https://dev.w3.org/html5/spec-author-view/video.html |
| 431 // But we always create new video element (see above), |
| 432 // we don't have to call load(). |
| 433 // If you call load() method here, |
| 434 // you can't see subtitles. |
| 435 // (It might be a bug: https://crbug.com/594537) |
| 436 //this.videoElement_.load(); |
| 418 callback(); | 437 callback(); |
| 419 }.bind(this)) | 438 }.bind(this)) |
| 420 // In case of error. | 439 // In case of error. |
| 421 .catch(function(error) { | 440 .catch(function(error) { |
| 422 if (this.currentCast_) | 441 if (this.currentCast_) |
| 423 metrics.recordCastVideoErrorAction(); | 442 metrics.recordCastVideoErrorAction(); |
| 424 | 443 |
| 425 videoPlayerElement.removeAttribute('loading'); | 444 videoPlayerElement.removeAttribute('loading'); |
| 426 console.error('Failed to initialize the video element.', | 445 console.error('Failed to initialize the video element.', |
| 427 error.stack || error); | 446 error.stack || error); |
| 428 this.controls_.showErrorMessage( | 447 this.controls_.showErrorMessage( |
| 429 'VIDEO_PLAYER_VIDEO_FILE_UNSUPPORTED'); | 448 'VIDEO_PLAYER_VIDEO_FILE_UNSUPPORTED'); |
| 430 callback(); | 449 callback(); |
| 431 }.bind(this)); | 450 }.bind(this)); |
| 432 }.wrap(this)); | 451 }.wrap(this)); |
| 433 }; | 452 }; |
| 434 | 453 |
| 435 /** | 454 /** |
| 455 * Search subtile file corresponding to a video. |
| 456 * @param {string} url a url of a video. |
| 457 * @return {string} a url of subtitle file, or an empty string. |
| 458 */ |
| 459 VideoPlayer.prototype.searchSubtitle_ = function(url) { |
| 460 var baseUrl = util.splitExtension(url)[0]; |
| 461 var resolveLocalFileSystemWithExtension = function(extension) { |
| 462 return new Promise( |
| 463 window.webkitResolveLocalFileSystemURL.bind(null, baseUrl + extension)); |
| 464 }; |
| 465 return resolveLocalFileSystemWithExtension('.vtt').then(function(subtitle) { |
| 466 return subtitle.toURL(); |
| 467 }).catch(function() { |
| 468 return ''; |
| 469 }); |
| 470 }; |
| 471 |
| 472 /** |
| 436 * Plays the first video. | 473 * Plays the first video. |
| 437 */ | 474 */ |
| 438 VideoPlayer.prototype.playFirstVideo = function() { | 475 VideoPlayer.prototype.playFirstVideo = function() { |
| 439 this.currentPos_ = 0; | 476 this.currentPos_ = 0; |
| 440 this.reloadCurrentVideo(this.onFirstVideoReady_.wrap(this)); | 477 this.reloadCurrentVideo(this.onFirstVideoReady_.wrap(this)); |
| 441 }; | 478 }; |
| 442 | 479 |
| 443 /** | 480 /** |
| 444 * Unloads the current video. | 481 * Unloads the current video. |
| 445 * @param {boolean=} opt_keepSession If true, keep using the current session. | 482 * @param {boolean=} opt_keepSession If true, keep using the current session. |
| (...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 705 return new Promise(function(fulfill, reject) { | 742 return new Promise(function(fulfill, reject) { |
| 706 util.URLsToEntries(window.appState.items, function(entries) { | 743 util.URLsToEntries(window.appState.items, function(entries) { |
| 707 metrics.recordOpenVideoPlayerAction(); | 744 metrics.recordOpenVideoPlayerAction(); |
| 708 metrics.recordNumberOfOpenedFiles(entries.length); | 745 metrics.recordNumberOfOpenedFiles(entries.length); |
| 709 | 746 |
| 710 player.prepare(entries); | 747 player.prepare(entries); |
| 711 player.playFirstVideo(player, fulfill); | 748 player.playFirstVideo(player, fulfill); |
| 712 }.wrap()); | 749 }.wrap()); |
| 713 }.wrap()); | 750 }.wrap()); |
| 714 }.wrap()); | 751 }.wrap()); |
| OLD | NEW |