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 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
/**
* @fileoverview MediaControls class implements media playback controls
* that exist outside of the audio/video HTML element.
*/
/**
* Model of a volume slider and a mute switch and its user interaction.
* @constructor
* @struct
*/
function VolumeModel() {
/**
* @type {boolean}
*/
this.isMuted_ = false;
/**
* The volume level in [0..1].
* @type {number}
*/
this.volume_ = 0.5;
};
/**
* After unmuting, the volume should be non-zero value to avoid that the mute
* button gives no response to user.
*/
VolumeModel.MIN_VOLUME_AFTER_UNMUTE = 0.01;
/**
* @return {number} the value to be set as the volume level of a media element.
*/
VolumeModel.prototype.getMediaVolume = function() {
return this.isMuted_ ? 0 : this.volume_;
};
/**
* Handles operation to the volume level slider.
* @param {number} value new position of the slider in [0..1].
*/
VolumeModel.prototype.onVolumeChanged = function(value) {
if (value == 0) {
this.isMuted_ = true;
} else {
this.isMuted_ = false;
this.volume_ = value;
}
};
/**
* Toggles the mute state.
*/
VolumeModel.prototype.toggleMute = function() {
this.isMuted_ = !this.isMuted_;
if (!this.isMuted_) {
this.volume_ = Math.max(VolumeModel.MIN_VOLUME_AFTER_UNMUTE, this.volume_);
}
};
/**
* Sets the status of the model.
* @param {number} volume the volume level in [0..1].
* @param {boolean} mute whether to mute the sound.
*/
VolumeModel.prototype.set = function(volume, mute) {
this.volume_ = volume;
this.isMuted_ = mute;
};
/**
* @param {!HTMLElement} containerElement The container for the controls.
* @param {function(Event)} onMediaError Function to display an error message.
* @constructor
* @struct
*/
function MediaControls(containerElement, onMediaError) {
this.container_ = containerElement;
this.document_ = this.container_.ownerDocument;
this.media_ = null;
this.onMediaPlayBound_ = this.onMediaPlay_.bind(this, true);
this.onMediaPauseBound_ = this.onMediaPlay_.bind(this, false);
this.onMediaDurationBound_ = this.onMediaDuration_.bind(this);
this.onMediaProgressBound_ = this.onMediaProgress_.bind(this);
this.onMediaError_ = onMediaError || function() {};
/**
* @type {VolumeModel}
* @private
*/
this.volumeModel_ = new VolumeModel();
/**
* @type {HTMLElement}
* @private
*/
this.playButton_ = null;
/**
* @type {PaperSliderElement}
* @private
*/
this.progressSlider_ = null;
/**
* @type {PaperSliderElement}
* @private
*/
this.volume_ = null;
/**
* @type {HTMLElement}
* @private
*/
this.textBanner_ = null;
/**
* @type {HTMLElement}
* @private
*/
this.soundButton_ = null;
/**
* @type {HTMLElement}
* @private
*/
this.subtitlesButton_ = null;
/**
* @private {TextTrack}
*/
this.subtitlesTrack_ = null;
/**
* @type {boolean}
* @private
*/
this.resumeAfterDrag_ = false;
/**
* @type {HTMLElement}
* @private
*/
this.currentTime_ = null;
/**
* @type {HTMLElement}
* @private
*/
this.currentTimeSpacer_ = null;
/**
* @private {boolean}
*/
this.seeking_ = false;
/**
* @private {boolean}
*/
this.showRemainingTime_ = false;
}
/**
* Button's state types. Values are used as CSS class names.
* @enum {string}
*/
MediaControls.ButtonStateType = {
DEFAULT: 'default',
PLAYING: 'playing',
ENDED: 'ended'
};
/**
* @return {HTMLAudioElement|HTMLVideoElement} The media element.
*/
MediaControls.prototype.getMedia = function() { return this.media_ };
/**
* Format the time in hh:mm:ss format (omitting redundant leading zeros)
* adding '-' sign if given value is negative.
* @param {number} timeInSec Time in seconds.
* @return {string} Formatted time string.
* @private
*/
MediaControls.formatTime_ = function(timeInSec) {
var result = '';
if (timeInSec < 0) {
timeInSec *= -1;
result += '-';
}
var seconds = Math.floor(timeInSec % 60);
var minutes = Math.floor((timeInSec / 60) % 60);
var hours = Math.floor(timeInSec / 60 / 60);
if (hours) result += hours + ':';
if (hours && (minutes < 10)) result += '0';
result += minutes + ':';
if (seconds < 10) result += '0';
result += seconds;
return result;
};
/**
* Create a custom control.
*
* @param {string} className Class name.
* @param {HTMLElement=} opt_parent Parent element or container if undefined.
* @param {string=} opt_tagName Tag name of the control. 'div' if undefined.
* @return {!HTMLElement} The new control element.
*/
MediaControls.prototype.createControl =
function(className, opt_parent, opt_tagName) {
var parent = opt_parent || this.container_;
var control = /** @type {!HTMLElement} */
(this.document_.createElement(opt_tagName || 'div'));
control.className = className;
parent.appendChild(control);
return control;
};
/**
* Create a custom button.
*
* @param {string} className Class name.
* @param {function(Event)=} opt_handler Click handler.
* @param {HTMLElement=} opt_parent Parent element or container if undefined.
* @param {number=} opt_numStates Number of states, default: 1.
* @return {!HTMLElement} The new button element.
*/
MediaControls.prototype.createButton = function(
className, opt_handler, opt_parent, opt_numStates) {
opt_numStates = opt_numStates || 1;
var button = this.createControl(className, opt_parent, 'files-icon-button');
button.classList.add('media-button');
button.setAttribute('state', MediaControls.ButtonStateType.DEFAULT);
if (opt_handler)
button.addEventListener('click', opt_handler);
return button;
};
/**
* Enable/disable controls.
*
* @param {boolean} on True if enable, false if disable.
* @private
*/
MediaControls.prototype.enableControls_ = function(on) {
var controls = this.container_.querySelectorAll('.media-control');
for (var i = 0; i != controls.length; i++) {
var classList = controls[i].classList;
if (on)
classList.remove('disabled');
else
classList.add('disabled');
}
this.progressSlider_.disabled = !on;
this.volume_.disabled = !on;
};
/*
* Playback control.
*/
/**
* Play the media.
*/
MediaControls.prototype.play = function() {
if (!this.media_)
return; // Media is detached.
this.media_.play();
};
/**
* Pause the media.
*/
MediaControls.prototype.pause = function() {
if (!this.media_)
return; // Media is detached.
this.media_.pause();
};
/**
* @return {boolean} True if the media is currently playing.
*/
MediaControls.prototype.isPlaying = function() {
return !!this.media_ && !this.media_.paused && !this.media_.ended;
};
/**
* Toggle play/pause.
*/
MediaControls.prototype.togglePlayState = function() {
if (this.isPlaying())
this.pause();
else
this.play();
};
/**
* Toggles play/pause state on a mouse click on the play/pause button.
*
* @param {Event} event Mouse click event.
*/
MediaControls.prototype.onPlayButtonClicked = function(event) {
this.togglePlayState();
};
/**
* @param {HTMLElement=} opt_parent Parent container.
*/
MediaControls.prototype.initPlayButton = function(opt_parent) {
this.playButton_ = this.createButton('play media-control',
this.onPlayButtonClicked.bind(this), opt_parent, 3 /* States. */);
this.playButton_.setAttribute('aria-label',
str('MEDIA_PLAYER_PLAY_BUTTON_LABEL'));
};
/*
* Time controls
*/
/**
* The default range of 100 is too coarse for the media progress slider.
*/
MediaControls.PROGRESS_RANGE = 5000;
/**
* 5 seconds should be skipped when left/right key is pressed.
*/
MediaControls.PROGRESS_MAX_SECONDS_TO_SMALL_SKIP = 5;
/**
* 10 seconds should be skipped when J/L key is pressed.
*/
MediaControls.PROGRESS_MAX_SECONDS_TO_BIG_SKIP = 10;
/**
* 10% of duration should be skipped when the video is too short to skip 5
* seconds.
*/
MediaControls.PROGRESS_MAX_RATIO_TO_SMALL_SKIP = 0.1;
/**
* 20% of duration should be skipped when the video is too short to skip 10
* seconds.
*/
MediaControls.PROGRESS_MAX_RATIO_TO_BIG_SKIP = 0.2;
/**
* @param {HTMLElement=} opt_parent Parent container.
*/
MediaControls.prototype.initTimeControls = function(opt_parent) {
var timeControls = this.createControl('time-controls', opt_parent);
var timeBox = this.createControl('time media-control', timeControls);
this.currentTimeSpacer_ = this.createControl('spacer', timeBox);
this.currentTime_ = this.createControl('current', timeBox);
this.currentTime_.addEventListener('click',
this.onTimeLabelClick_.bind(this));
// Set the initial width to the minimum to reduce the flicker.
this.updateTimeLabel_(0, 0);
this.progressSlider_ = /** @type {!PaperSliderElement} */ (
document.createElement('paper-slider'));
this.progressSlider_.classList.add('progress', 'media-control');
this.progressSlider_.max = MediaControls.PROGRESS_RANGE;
this.progressSlider_.setAttribute('aria-label',
str('MEDIA_PLAYER_SEEK_SLIDER_LABEL'));
this.progressSlider_.addEventListener('change', function(event) {
this.onProgressChange_(this.progressSlider_.ratio);
}.bind(this));
this.progressSlider_.addEventListener(
'immediate-value-change',
function(event) {
this.onProgressDrag_();
}.bind(this));
timeControls.appendChild(this.progressSlider_);
};
/**
* @param {number} current Current time is seconds.
* @param {number} duration Duration in seconds.
* @private
*/
MediaControls.prototype.displayProgress_ = function(current, duration) {
var ratio = current / duration;
this.progressSlider_.value = ratio * this.progressSlider_.max;
this.updateTimeLabel_(current);
};
/**
* @param {number} value Progress [0..1].
* @private
*/
MediaControls.prototype.onProgressChange_ = function(value) {
if (!this.media_)
return; // Media is detached.
if (!this.media_.seekable || !this.media_.duration) {
console.error('Inconsistent media state');
return;
}
this.setSeeking_(false);
// Re-start playing the video when the seek bar is moved from ending point.
if (this.media_.ended)
this.play();
var current = this.media_.duration * value;
this.media_.currentTime = current;
this.updateTimeLabel_(current);
};
/**
* @private
*/
MediaControls.prototype.onProgressDrag_ = function() {
if (!this.media_)
return; // Media is detached.
this.setSeeking_(true);
// Show seeking position instead of playing position while dragging.
if (this.media_.duration && this.progressSlider_.max > 0) {
var immediateRatio =
this.progressSlider_.immediateValue / this.progressSlider_.max;
var current = this.media_.duration * immediateRatio;
this.updateTimeLabel_(current);
}
};
/**
* Skips forward/backword.
* @param {number} sec Seconds to skip. Set negative value to skip backword.
* @private
*/
MediaControls.prototype.skip_ = function(sec) {
if (this.media_ && this.media_.duration > 0) {
var stepsToSkip = MediaControls.PROGRESS_RANGE *
(sec / this.media_.duration);
this.progressSlider_.value = Math.max(Math.min(
this.progressSlider_.value + stepsToSkip,
this.progressSlider_.max), 0);
this.onProgressChange_(this.progressSlider_.ratio);
}
};
/**
* Invokes small skip.
* @param {boolean} forward Whether to skip forward or backword.
*/
MediaControls.prototype.smallSkip = function(forward) {
var secondsToSkip = Math.min(
MediaControls.PROGRESS_MAX_SECONDS_TO_SMALL_SKIP,
this.media_.duration * MediaControls.PROGRESS_MAX_RATIO_TO_SMALL_SKIP);
if (!forward)
secondsToSkip *= -1;
this.skip_(secondsToSkip);
};
/**
* Invokes big skip.
* @param {boolean} forward Whether to skip forward or backword.
*/
MediaControls.prototype.bigSkip = function(forward) {
var secondsToSkip = Math.min(
MediaControls.PROGRESS_MAX_SECONDS_TO_BIG_SKIP,
this.media_.duration * MediaControls.PROGRESS_MAX_RATIO_TO_BIG_SKIP);
if (!forward)
secondsToSkip *= -1;
this.skip_(secondsToSkip);
};
/**
* Handles 'seeking' state, which starts by dragging slider knob and finishes by
* releasing it. While seeking, we pause the video when seeking starts and
* resume the last play state when seeking ends.
* @private
*/
MediaControls.prototype.setSeeking_ = function(seeking) {
if (seeking === this.seeking_)
return;
this.seeking_ = seeking;
if (seeking) {
this.resumeAfterDrag_ = this.isPlaying();
this.media_.pause(true /* seeking */);
} else {
if (this.resumeAfterDrag_) {
if (this.media_.ended)
this.onMediaPlay_(false);
else
this.media_.play(true /* seeking */);
}
this.resumeAfterDrag_ = false;
}
this.updatePlayButtonState_(this.isPlaying());
};
/**
* Click handler for the time label.
* @private
*/
MediaControls.prototype.onTimeLabelClick_ = function(event) {
this.showRemainingTime_ = !this.showRemainingTime_;
this.updateTimeLabel_(this.media_.currentTime, this.media_.duration);
}
/**
* Update the label for current playing position and video duration.
* The label should be like "0:06 / 0:32" or "-0:26 / 0:32".
* @param {number} current Current playing position.
* @param {number=} opt_duration Video's duration.
* @private
*/
MediaControls.prototype.updateTimeLabel_ = function(current, opt_duration) {
var duration = opt_duration;
if (duration === undefined)
duration = this.media_ ? this.media_.duration : 0;
// media's duration and currentTime can be NaN. Default to 0.
if (isNaN(duration))
duration = 0;
if (isNaN(current))
current = 0;
if (isFinite(duration)) {
this.currentTime_.textContent =
(this.showRemainingTime_ ? MediaControls.formatTime_(current - duration)
: MediaControls.formatTime_(current)) + ' / ' +
MediaControls.formatTime_(duration);
// Keep the maximum space to prevent time label from moving while playing.
this.currentTimeSpacer_.textContent =
(this.showRemainingTime_ ? '-' : '') +
MediaControls.formatTime_(duration) + ' / ' +
MediaControls.formatTime_(duration);
} else {
// Media's duration can be positive infinity value when the media source is
// not known to be bounded yet. In such cases, we should hide duration.
this.currentTime_.textContent = MediaControls.formatTime_(current);
this.currentTimeSpacer_.textContent = MediaControls.formatTime_(current);
}
};
/*
* Volume controls
*/
MediaControls.STORAGE_PREFIX = 'videoplayer-';
MediaControls.KEY_NORMALIZED_VOLUME =
MediaControls.STORAGE_PREFIX + 'normalized-volume';
MediaControls.KEY_MUTED =
MediaControls.STORAGE_PREFIX + 'muted';
/**
* @param {HTMLElement=} opt_parent Parent element for the controls.
*/
MediaControls.prototype.initVolumeControls = function(opt_parent) {
var volumeControls = this.createControl('volume-controls', opt_parent);
this.soundButton_ = this.createButton('sound media-control',
this.onSoundButtonClick_.bind(this), volumeControls);
this.soundButton_.setAttribute('level', 3); // max level.
this.soundButton_.setAttribute('aria-label',
str('MEDIA_PLAYER_MUTE_BUTTON_LABEL'));
this.volume_ = /** @type {!PaperSliderElement} */ (
document.createElement('paper-slider'));
this.volume_.classList.add('volume', 'media-control');
this.volume_.setAttribute('aria-label',
str('MEDIA_PLAYER_VOLUME_SLIDER_LABEL'));
this.volume_.addEventListener('change', function(event) {
this.onVolumeChange_(this.volume_.ratio);
}.bind(this));
this.volume_.addEventListener('immediate-value-change', function(event) {
this.onVolumeDrag_();
}.bind(this));
this.loadVolumeControlState();
volumeControls.appendChild(this.volume_);
};
MediaControls.prototype.loadVolumeControlState = function() {
chrome.storage.local.get([MediaControls.KEY_NORMALIZED_VOLUME,
MediaControls.KEY_MUTED],
function(retrieved) {
var normalizedVolume = (MediaControls.KEY_NORMALIZED_VOLUME
in retrieved)
? retrieved[MediaControls.KEY_NORMALIZED_VOLUME] : 1;
var isMuted = (MediaControls.KEY_MUTED in retrieved)
? retrieved[MediaControls.KEY_MUTED] : false;
this.volumeModel_.set(normalizedVolume, isMuted);
this.reflectVolumeToUi_();
}.bind(this));
};
MediaControls.prototype.saveVolumeControlState = function() {
var valuesToStore = {};
valuesToStore[MediaControls.KEY_NORMALIZED_VOLUME] =
this.volumeModel_.volume_;
valuesToStore[MediaControls.KEY_MUTED] = this.volumeModel_.isMuted_;
chrome.storage.local.set(valuesToStore);
};
/**
* Click handler for the sound level button.
* @private
*/
MediaControls.prototype.onSoundButtonClick_ = function() {
this.volumeModel_.toggleMute();
this.saveVolumeControlState();
this.reflectVolumeToUi_();
};
/**
* @param {number} value Volume [0..1].
* @return {number} The rough level [0..3] used to pick an icon.
* @private
*/
MediaControls.getVolumeLevel_ = function(value) {
if (value == 0) return 0;
if (value <= 1 / 3) return 1;
if (value <= 2 / 3) return 2;
return 3;
};
/**
* Reflects volume model to the UI elements.
* @private
*/
MediaControls.prototype.reflectVolumeToUi_ = function() {
this.soundButton_.setAttribute('level',
MediaControls.getVolumeLevel_(this.volumeModel_.getMediaVolume()));
this.soundButton_.setAttribute('aria-label', this.volumeModel_.isMuted_
? str('MEDIA_PLAYER_UNMUTE_BUTTON_LABEL')
: str('MEDIA_PLAYER_MUTE_BUTTON_LABEL'));
this.volume_.value = this.volumeModel_.getMediaVolume() * this.volume_.max
if (this.media_) {
this.media_.volume = this.volumeModel_.getMediaVolume();
}
}
/**
* Handles change event of the volume slider.
* @param {number} value Volume [0..1].
* @private
*/
MediaControls.prototype.onVolumeChange_ = function(value) {
if (!this.media_)
return; // Media is detached.
this.volumeModel_.onVolumeChanged(value);
this.saveVolumeControlState();
this.reflectVolumeToUi_();
};
/**
* @private
*/
MediaControls.prototype.onVolumeDrag_ = function() {
if (this.media_.volume !== 0) {
this.volumeModel_.onVolumeChanged(this.media_.volume);;
}
};
/**
* Initializes subtitles button.
*/
MediaControls.prototype.initSubtitlesButton = function() {
this.subtitlesTrack_ = null;
this.subtitlesButton_ =
this.createButton('subtitles', this.onSubtitlesButtonClicked_.bind(this));
};
/**
* @param {Event} event Mouse click event.
* @private
*/
MediaControls.prototype.onSubtitlesButtonClicked_ = function(event) {
if (!this.subtitlesTrack_) {
return;
}
this.toggleSubtitlesMode_(this.subtitlesTrack_.mode === 'hidden');
};
/**
* @param {boolean} on Whether enabled or not.
* @private
*/
MediaControls.prototype.toggleSubtitlesMode_ = function(on) {
if (!this.subtitlesTrack_) {
return;
}
if (on) {
this.subtitlesTrack_.mode = 'showing';
this.subtitlesButton_.setAttribute('showing', '');
this.subtitlesButton_.setAttribute('aria-label',
str('VIDEO_PLAYER_DISABLE_SUBTITLES_BUTTON_LABEL'));
} else {
this.subtitlesTrack_.mode = 'hidden';
this.subtitlesButton_.removeAttribute('showing');
this.subtitlesButton_.setAttribute('aria-label',
str('VIDEO_PLAYER_ENABLE_SUBTITLES_BUTTON_LABEL'));
}
};
/**
* @param {TextTrack} track Subtitles track
* @private
*/
MediaControls.prototype.attachTextTrack_ = function(track) {
this.subtitlesTrack_ = track;
if (this.subtitlesTrack_) {
this.toggleSubtitlesMode_(true);
this.subtitlesButton_.removeAttribute('unavailable');
} else {
this.subtitlesButton_.setAttribute('unavailable', '');
}
};
/**
* @private
*/
MediaControls.prototype.detachTextTrack_ = function() {
this.subtitlesTrack_ = null;
};
/*
* Media event handlers.
*/
/**
* Attach a media element.
*
* @param {!HTMLMediaElement} mediaElement The media element to control.
*/
MediaControls.prototype.attachMedia = function(mediaElement) {
this.media_ = mediaElement;
this.media_.addEventListener('play', this.onMediaPlayBound_);
this.media_.addEventListener('pause', this.onMediaPauseBound_);
this.media_.addEventListener('durationchange', this.onMediaDurationBound_);
this.media_.addEventListener('timeupdate', this.onMediaProgressBound_);
this.media_.addEventListener('error', this.onMediaError_);
// If the text banner is being displayed, hide it immediately, since it is
// related to the previous media.
this.textBanner_.removeAttribute('visible');
// Reflect the media state in the UI.
this.onMediaDuration_();
this.onMediaPlay_(this.isPlaying());
this.onMediaProgress_();
// Reflect the user specified volume to the media.
this.media_.volume = this.volumeModel_.getMediaVolume();
if (this.media_.textTracks && this.media_.textTracks.length > 0) {
this.attachTextTrack_(this.media_.textTracks[0]);
} else {
this.attachTextTrack_(null);
}
};
/**
* Detach media event handlers.
*/
MediaControls.prototype.detachMedia = function() {
if (!this.media_)
return;
this.media_.removeEventListener('play', this.onMediaPlayBound_);
this.media_.removeEventListener('pause', this.onMediaPauseBound_);
this.media_.removeEventListener('durationchange', this.onMediaDurationBound_);
this.media_.removeEventListener('timeupdate', this.onMediaProgressBound_);
this.media_.removeEventListener('error', this.onMediaError_);
this.media_ = null;
this.detachTextTrack_();
};
/**
* Force-empty the media pipeline. This is a workaround for crbug.com/149957.
* The document is not going to be GC-ed until the last Files app window closes,
* but we want the media pipeline to deinitialize ASAP to minimize leakage.
*/
MediaControls.prototype.cleanup = function() {
if (!this.media_)
return;
this.media_.src = '';
this.media_.load();
this.detachMedia();
};
/**
* 'play' and 'pause' event handler.
* @param {boolean} playing True if playing.
* @private
*/
MediaControls.prototype.onMediaPlay_ = function(playing) {
if (this.progressSlider_.dragging)
return;
this.updatePlayButtonState_(playing);
this.onPlayStateChanged();
};
/**
* 'durationchange' event handler.
* @private
*/
MediaControls.prototype.onMediaDuration_ = function() {
if (!this.media_ || !this.media_.duration) {
this.enableControls_(false);
return;
}
this.enableControls_(true);
if (this.media_.seekable)
this.progressSlider_.classList.remove('readonly');
else
this.progressSlider_.classList.add('readonly');
this.updateTimeLabel_(this.media_.currentTime, this.media_.duration);
if (this.media_.seekable)
this.restorePlayState();
};
/**
* 'timeupdate' event handler.
* @private
*/
MediaControls.prototype.onMediaProgress_ = function() {
if (!this.media_ || !this.media_.duration) {
this.displayProgress_(0, 1);
return;
}
var current = this.media_.currentTime;
var duration = this.media_.duration;
if (this.progressSlider_.dragging)
return;
this.displayProgress_(current, duration);
if (current == duration) {
this.onMediaComplete();
}
this.onPlayStateChanged();
};
/**
* Called when the media playback is complete.
*/
MediaControls.prototype.onMediaComplete = function() {};
/**
* Called when play/pause state is changed or on playback progress.
* This is the right moment to save the play state.
*/
MediaControls.prototype.onPlayStateChanged = function() {};
/**
* Updates the play button state.
* @param {boolean} playing If the video is playing.
* @private
*/
MediaControls.prototype.updatePlayButtonState_ = function(playing) {
if (this.media_.ended &&
this.progressSlider_.value === this.progressSlider_.max) {
this.playButton_.setAttribute('state',
MediaControls.ButtonStateType.ENDED);
this.playButton_.setAttribute('aria-label',
str('MEDIA_PLAYER_PLAY_BUTTON_LABEL'));
} else if (playing) {
this.playButton_.setAttribute('state',
MediaControls.ButtonStateType.PLAYING);
this.playButton_.setAttribute('aria-label',
str('MEDIA_PLAYER_PAUSE_BUTTON_LABEL'));
} else {
this.playButton_.setAttribute('state',
MediaControls.ButtonStateType.DEFAULT);
this.playButton_.setAttribute('aria-label',
str('MEDIA_PLAYER_PLAY_BUTTON_LABEL'));
}
};
/**
* Restore play state. Base implementation is empty.
*/
MediaControls.prototype.restorePlayState = function() {};
/**
* Encode current state into the page URL or the app state.
*/
MediaControls.prototype.encodeState = function() {
if (!this.media_ || !this.media_.duration)
return;
if (window.appState) {
window.appState.time = this.media_.currentTime;
util.saveAppState();
}
return;
};
/**
* Decode current state from the page URL or the app state.
* @return {boolean} True if decode succeeded.
*/
MediaControls.prototype.decodeState = function() {
if (!this.media_ || !window.appState || !('time' in window.appState))
return false;
// There is no page reload for apps v2, only app restart.
// Always restart in paused state.
this.media_.currentTime = window.appState.time;
this.pause();
return true;
};
/**
* Remove current state from the page URL or the app state.
*/
MediaControls.prototype.clearState = function() {
if (!window.appState)
return;
if ('time' in window.appState)
delete window.appState.time;
util.saveAppState();
return;
};
/**
* Create video controls.
*
* @param {!HTMLElement} containerElement The container for the controls.
* @param {function(Event)} onMediaError Function to display an error message.
* @param {function(Event)=} opt_fullScreenToggle Function to toggle fullscreen
* mode.
* @param {HTMLElement=} opt_stateIconParent The parent for the icon that
* gives visual feedback when the playback state changes.
* @constructor
* @struct
* @extends {MediaControls}
*/
function VideoControls(
containerElement, onMediaError, opt_fullScreenToggle, opt_stateIconParent) {
MediaControls.call(this, containerElement, onMediaError);
this.container_.classList.add('video-controls');
this.initPlayButton();
this.initTimeControls();
this.initVolumeControls();
this.initSubtitlesButton();
// Create the cast menu button.
// We need to use <button> since cr.ui.MenuButton.decorate modifies prototype
// chain, by which <files-icon-button> will not work correctly.
// TODO(fukino): Find a way to use files-icon-button consistently.
this.castButton_ = this.createControl(
'cast media-button', undefined, 'button');
this.castButton_.setAttribute('menu', '#cast-menu');
this.castButton_.setAttribute('aria-label', str('VIDEO_PLAYER_PLAY_ON'));
this.castButton_.setAttribute('state', MediaControls.ButtonStateType.DEFAULT);
this.castButton_.appendChild(document.createElement('files-ripple'));
cr.ui.decorate(this.castButton_, cr.ui.MenuButton);
// Create the cast button, which is a normal button and is used when we cast
// videos usign Media Router.
this.createButton('cast-button');
if (opt_fullScreenToggle) {
this.fullscreenButton_ =
this.createButton('fullscreen', opt_fullScreenToggle);
this.fullscreenButton_.setAttribute('aria-label',
str('VIDEO_PLAYER_FULL_SCREEN_BUTTON_LABEL'));
}
if (opt_stateIconParent) {
this.stateIcon_ = this.createControl(
'playback-state-icon', opt_stateIconParent);
this.textBanner_ = this.createControl('text-banner', opt_stateIconParent);
}
// Disables all controls at first.
this.enableControls_(false);
var videoControls = this;
chrome.mediaPlayerPrivate.onTogglePlayState.addListener(
function() { videoControls.togglePlayStateWithFeedback(); });
}
/**
* No resume if we are within this margin from the start or the end.
*/
VideoControls.RESUME_MARGIN = 0.03;
/**
* No resume for videos shorter than this.
*/
VideoControls.RESUME_THRESHOLD = 5 * 60; // 5 min.
/**
* When resuming rewind back this much.
*/
VideoControls.RESUME_REWIND = 5; // seconds.
VideoControls.prototype = { __proto__: MediaControls.prototype };
/**
* Shows icon feedback for the current state of the video player.
* @private
*/
VideoControls.prototype.showIconFeedback_ = function() {
var stateIcon = this.stateIcon_;
stateIcon.removeAttribute('state');
setTimeout(function() {
var newState = this.isPlaying() ? 'play' : 'pause';
var onAnimationEnd = function(state, event) {
if (stateIcon.getAttribute('state') === state)
stateIcon.removeAttribute('state');
stateIcon.removeEventListener('webkitAnimationEnd', onAnimationEnd);
}.bind(null, newState);
stateIcon.addEventListener('webkitAnimationEnd', onAnimationEnd);
// Shows the icon with animation.
stateIcon.setAttribute('state', newState);
}.bind(this), 0);
};
/**
* Shows a text banner.
*
* @param {string} identifier String identifier.
* @private
*/
VideoControls.prototype.showTextBanner_ = function(identifier) {
this.textBanner_.removeAttribute('visible');
this.textBanner_.textContent = str(identifier);
setTimeout(function() {
var onAnimationEnd = function(event) {
this.textBanner_.removeEventListener(
'webkitAnimationEnd', onAnimationEnd);
this.textBanner_.removeAttribute('visible');
}.bind(this);
this.textBanner_.addEventListener('webkitAnimationEnd', onAnimationEnd);
this.textBanner_.setAttribute('visible', 'true');
}.bind(this), 0);
};
/**
* @override
*/
VideoControls.prototype.onPlayButtonClicked = function(event) {
if (event.ctrlKey) {
this.toggleLoopedModeWithFeedback(true);
if (!this.isPlaying())
this.togglePlayState();
} else {
this.togglePlayState();
}
};
/**
* Media completion handler.
*/
VideoControls.prototype.onMediaComplete = function() {
this.onMediaPlay_(false); // Just update the UI.
this.savePosition(); // This will effectively forget the position.
};
/**
* Toggles the looped mode with feedback.
* @param {boolean} on Whether enabled or not.
*/
VideoControls.prototype.toggleLoopedModeWithFeedback = function(on) {
if (!this.getMedia().duration)
return;
this.toggleLoopedMode(on);
if (on) {
// TODO(mtomasz): Simplify, crbug.com/254318.
this.showTextBanner_('VIDEO_PLAYER_LOOPED_MODE');
}
};
/**
* Toggles the looped mode.
* @param {boolean} on Whether enabled or not.
*/
VideoControls.prototype.toggleLoopedMode = function(on) {
this.getMedia().loop = on;
};
/**
* Toggles play/pause state and flash an icon over the video.
*/
VideoControls.prototype.togglePlayStateWithFeedback = function() {
if (!this.getMedia().duration)
return;
this.togglePlayState();
this.showIconFeedback_();
};
/**
* Toggles play/pause state.
*/
VideoControls.prototype.togglePlayState = function() {
if (this.isPlaying()) {
// User gave the Pause command. Save the state and reset the loop mode.
this.toggleLoopedMode(false);
this.savePosition();
}
MediaControls.prototype.togglePlayState.apply(this, arguments);
};
/**
* Saves the playback position to the persistent storage.
* @param {boolean=} opt_sync True if the position must be saved synchronously
* (required when closing app windows).
*/
VideoControls.prototype.savePosition = function(opt_sync) {
if (!this.media_ ||
!this.media_.duration ||
this.media_.duration < VideoControls.RESUME_THRESHOLD) {
return;
}
var ratio = this.media_.currentTime / this.media_.duration;
var position;
if (ratio < VideoControls.RESUME_MARGIN ||
ratio > (1 - VideoControls.RESUME_MARGIN)) {
// We are too close to the beginning or the end.
// Remove the resume position so that next time we start from the beginning.
position = null;
} else {
position = Math.floor(
Math.max(0, this.media_.currentTime - VideoControls.RESUME_REWIND));
}
if (opt_sync) {
// Packaged apps cannot save synchronously.
// Pass the data to the background page.
if (!window.saveOnExit)
window.saveOnExit = [];
window.saveOnExit.push({ key: this.media_.src, value: position });
} else {
util.AppCache.update(this.media_.src, position);
}
};
/**
* Resumes the playback position saved in the persistent storage.
*/
VideoControls.prototype.restorePlayState = function() {
if (this.media_ && this.media_.duration >= VideoControls.RESUME_THRESHOLD) {
util.AppCache.getValue(this.media_.src, function(position) {
if (position)
this.media_.currentTime = position;
}.bind(this));
}
};
/**
* Updates video control when the window is fullscreened or restored.
* @param {boolean} fullscreen True if the window gets fullscreened.
*/
VideoControls.prototype.onFullScreenChanged = function(fullscreen) {
if (fullscreen) {
this.container_.setAttribute('fullscreen', '');
} else {
this.container_.removeAttribute('fullscreen');
}
if (this.fullscreenButton_) {
this.fullscreenButton_.setAttribute('aria-label',
fullscreen ? str('VIDEO_PLAYER_EXIT_FULL_SCREEN_BUTTON_LABEL')
: str('VIDEO_PLAYER_FULL_SCREEN_BUTTON_LABEL'));;
// If the fullscreen button has focus on entering fullscreen mode, reset the
// focus to make the spacebar toggle play/pause state. This is the
// consistent behavior with Youtube Web UI.
if (fullscreen)
this.fullscreenButton_.blur();
}
};
|