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
|
/*
* Copyright (C) 2011, 2012 Apple Inc. All rights reserved.
* Copyright (C) 2011, 2012 Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "config.h"
#include "core/html/shadow/MediaControls.h"
#include "bindings/core/v8/ExceptionStatePlaceholder.h"
#include "core/dom/ClientRect.h"
#include "core/events/MouseEvent.h"
#include "core/frame/Settings.h"
#include "core/html/HTMLMediaElement.h"
#include "core/html/MediaController.h"
#include "core/rendering/RenderTheme.h"
namespace blink {
// If you change this value, then also update the corresponding value in
// LayoutTests/media/media-controls.js.
static const double timeWithoutMouseMovementBeforeHidingMediaControls = 3;
static bool fullscreenIsSupported(const Document& document)
{
return !document.settings() || document.settings()->fullscreenSupported();
}
MediaControls::MediaControls(HTMLMediaElement& mediaElement)
: HTMLDivElement(mediaElement.document())
, m_mediaElement(&mediaElement)
, m_panel(nullptr)
, m_textDisplayContainer(nullptr)
, m_overlayPlayButton(nullptr)
, m_overlayEnclosure(nullptr)
, m_playButton(nullptr)
, m_currentTimeDisplay(nullptr)
, m_timeline(nullptr)
, m_muteButton(nullptr)
, m_volumeSlider(nullptr)
, m_toggleClosedCaptionsButton(nullptr)
, m_fullScreenButton(nullptr)
, m_castButton(nullptr)
, m_overlayCastButton(nullptr)
, m_durationDisplay(nullptr)
, m_enclosure(nullptr)
, m_hideMediaControlsTimer(this, &MediaControls::hideMediaControlsTimerFired)
, m_isMouseOverControls(false)
, m_isPausedForScrubbing(false)
, m_wasLastEventTouch(false)
{
}
PassRefPtrWillBeRawPtr<MediaControls> MediaControls::create(HTMLMediaElement& mediaElement)
{
RefPtrWillBeRawPtr<MediaControls> controls = adoptRefWillBeNoop(new MediaControls(mediaElement));
if (controls->initializeControls())
return controls.release();
return nullptr;
}
// The media controls DOM structure looks like:
//
// MediaControls (-webkit-media-controls)
// +-MediaControlOverlayEnclosureElement (-webkit-media-controls-overlay-enclosure)
// | +-MediaControlTextTrackContainerElement (-webkit-media-text-track-container)
// | | {when text tracks are enabled}
// | +-MediaControlOverlayPlayButtonElement (-webkit-media-controls-overlay-play-button)
// | | {if mediaControlsOverlayPlayButtonEnabled}
// | \-MediaControlCastButtonElement (-internal-media-controls-overlay-cast-button)
// \-MediaControlPanelEnclosureElement (-webkit-media-controls-enclosure)
// \-MediaControlPanelElement (-webkit-media-controls-panel)
// +-MediaControlPlayButtonElement (-webkit-media-controls-play-button)
// +-MediaControlTimelineElement (-webkit-media-controls-timeline)
// +-MediaControlCurrentTimeDisplayElement (-webkit-media-controls-current-time-display)
// +-MediaControlTimeRemainingDisplayElement (-webkit-media-controls-time-remaining-display)
// +-MediaControlMuteButtonElement (-webkit-media-controls-mute-button)
// +-MediaControlVolumeSliderElement (-webkit-media-controls-volume-slider)
// +-MediaControlToggleClosedCaptionsButtonElement (-webkit-media-controls-toggle-closed-captions-button)
// +-MediaControlCastButtonElement (-internal-media-controls-cast-button)
// \-MediaControlFullscreenButtonElement (-webkit-media-controls-fullscreen-button)
//
// Most of the structure is built by MediaControls::initializeControls() - the
// exception being MediaControlTextTrackContainerElement which is added
// on-demand by MediaControls::createTextTrackDisplay().
bool MediaControls::initializeControls()
{
TrackExceptionState exceptionState;
RefPtrWillBeRawPtr<MediaControlOverlayEnclosureElement> overlayEnclosure = MediaControlOverlayEnclosureElement::create(*this);
if (document().settings() && document().settings()->mediaControlsOverlayPlayButtonEnabled()) {
RefPtrWillBeRawPtr<MediaControlOverlayPlayButtonElement> overlayPlayButton = MediaControlOverlayPlayButtonElement::create(*this);
m_overlayPlayButton = overlayPlayButton.get();
overlayEnclosure->appendChild(overlayPlayButton.release(), exceptionState);
if (exceptionState.hadException())
return false;
}
RefPtrWillBeRawPtr<MediaControlCastButtonElement> overlayCastButton = MediaControlCastButtonElement::create(*this, true);
m_overlayCastButton = overlayCastButton.get();
overlayEnclosure->appendChild(overlayCastButton.release(), exceptionState);
if (exceptionState.hadException())
return false;
m_overlayEnclosure = overlayEnclosure.get();
appendChild(overlayEnclosure.release(), exceptionState);
if (exceptionState.hadException())
return false;
// Create an enclosing element for the panel so we can visually offset the controls correctly.
RefPtrWillBeRawPtr<MediaControlPanelEnclosureElement> enclosure = MediaControlPanelEnclosureElement::create(*this);
RefPtrWillBeRawPtr<MediaControlPanelElement> panel = MediaControlPanelElement::create(*this);
RefPtrWillBeRawPtr<MediaControlPlayButtonElement> playButton = MediaControlPlayButtonElement::create(*this);
m_playButton = playButton.get();
panel->appendChild(playButton.release(), exceptionState);
if (exceptionState.hadException())
return false;
RefPtrWillBeRawPtr<MediaControlTimelineElement> timeline = MediaControlTimelineElement::create(*this);
m_timeline = timeline.get();
panel->appendChild(timeline.release(), exceptionState);
if (exceptionState.hadException())
return false;
RefPtrWillBeRawPtr<MediaControlCurrentTimeDisplayElement> currentTimeDisplay = MediaControlCurrentTimeDisplayElement::create(*this);
m_currentTimeDisplay = currentTimeDisplay.get();
m_currentTimeDisplay->hide();
panel->appendChild(currentTimeDisplay.release(), exceptionState);
if (exceptionState.hadException())
return false;
RefPtrWillBeRawPtr<MediaControlTimeRemainingDisplayElement> durationDisplay = MediaControlTimeRemainingDisplayElement::create(*this);
m_durationDisplay = durationDisplay.get();
panel->appendChild(durationDisplay.release(), exceptionState);
if (exceptionState.hadException())
return false;
RefPtrWillBeRawPtr<MediaControlMuteButtonElement> muteButton = MediaControlMuteButtonElement::create(*this);
m_muteButton = muteButton.get();
panel->appendChild(muteButton.release(), exceptionState);
if (exceptionState.hadException())
return false;
RefPtrWillBeRawPtr<MediaControlVolumeSliderElement> slider = MediaControlVolumeSliderElement::create(*this);
m_volumeSlider = slider.get();
panel->appendChild(slider.release(), exceptionState);
if (exceptionState.hadException())
return false;
RefPtrWillBeRawPtr<MediaControlToggleClosedCaptionsButtonElement> toggleClosedCaptionsButton = MediaControlToggleClosedCaptionsButtonElement::create(*this);
m_toggleClosedCaptionsButton = toggleClosedCaptionsButton.get();
panel->appendChild(toggleClosedCaptionsButton.release(), exceptionState);
if (exceptionState.hadException())
return false;
RefPtrWillBeRawPtr<MediaControlCastButtonElement> castButton = MediaControlCastButtonElement::create(*this, false);
m_castButton = castButton.get();
panel->appendChild(castButton.release(), exceptionState);
if (exceptionState.hadException())
return false;
RefPtrWillBeRawPtr<MediaControlFullscreenButtonElement> fullscreenButton = MediaControlFullscreenButtonElement::create(*this);
m_fullScreenButton = fullscreenButton.get();
panel->appendChild(fullscreenButton.release(), exceptionState);
if (exceptionState.hadException())
return false;
m_panel = panel.get();
enclosure->appendChild(panel.release(), exceptionState);
if (exceptionState.hadException())
return false;
m_enclosure = enclosure.get();
appendChild(enclosure.release(), exceptionState);
if (exceptionState.hadException())
return false;
return true;
}
void MediaControls::reset()
{
double duration = mediaElement().duration();
m_durationDisplay->setInnerText(RenderTheme::theme().formatMediaControlsTime(duration), ASSERT_NO_EXCEPTION);
m_durationDisplay->setCurrentValue(duration);
updatePlayState();
updateCurrentTimeDisplay();
m_timeline->setDuration(duration);
m_timeline->setPosition(mediaElement().currentTime());
if (!mediaElement().hasAudio())
m_volumeSlider->hide();
else
m_volumeSlider->show();
updateVolume();
refreshClosedCaptionsButtonVisibility();
if (mediaElement().hasVideo() && fullscreenIsSupported(document()))
m_fullScreenButton->show();
else
m_fullScreenButton->hide();
refreshCastButtonVisibility();
makeOpaque();
}
void MediaControls::show()
{
makeOpaque();
m_panel->setIsDisplayed(true);
m_panel->show();
if (m_overlayPlayButton)
m_overlayPlayButton->updateDisplayType();
}
void MediaControls::mediaElementFocused()
{
if (mediaElement().shouldShowControls()) {
show();
resetHideMediaControlsTimer();
}
}
void MediaControls::hide()
{
m_panel->setIsDisplayed(false);
m_panel->hide();
if (m_overlayPlayButton)
m_overlayPlayButton->hide();
}
void MediaControls::makeOpaque()
{
m_panel->makeOpaque();
}
void MediaControls::makeTransparent()
{
m_panel->makeTransparent();
}
bool MediaControls::shouldHideMediaControls(unsigned behaviorFlags) const
{
// Never hide for a media element without visual representation.
if (!mediaElement().hasVideo() || mediaElement().isPlayingRemotely())
return false;
// Don't hide if the mouse is over the controls.
const bool ignoreControlsHover = behaviorFlags & IgnoreControlsHover;
if (!ignoreControlsHover && m_panel->hovered())
return false;
// Don't hide if the mouse is over the video area.
const bool ignoreVideoHover = behaviorFlags & IgnoreVideoHover;
if (!ignoreVideoHover && m_isMouseOverControls)
return false;
// Don't hide if focus is on the HTMLMediaElement or within the
// controls/shadow tree. (Perform the checks separately to avoid going
// through all the potential ancestor hosts for the focused element.)
const bool ignoreFocus = behaviorFlags & IgnoreFocus;
if (!ignoreFocus && (mediaElement().focused() || contains(document().focusedElement())))
return false;
return true;
}
void MediaControls::playbackStarted()
{
m_currentTimeDisplay->show();
m_durationDisplay->hide();
updatePlayState();
m_timeline->setPosition(mediaElement().currentTime());
updateCurrentTimeDisplay();
startHideMediaControlsTimer();
}
void MediaControls::playbackProgressed()
{
m_timeline->setPosition(mediaElement().currentTime());
updateCurrentTimeDisplay();
if (shouldHideMediaControls())
makeTransparent();
}
void MediaControls::playbackStopped()
{
updatePlayState();
m_timeline->setPosition(mediaElement().currentTime());
updateCurrentTimeDisplay();
makeOpaque();
stopHideMediaControlsTimer();
}
void MediaControls::updatePlayState()
{
if (m_isPausedForScrubbing)
return;
if (m_overlayPlayButton)
m_overlayPlayButton->updateDisplayType();
m_playButton->updateDisplayType();
}
void MediaControls::beginScrubbing()
{
if (!mediaElement().togglePlayStateWillPlay()) {
m_isPausedForScrubbing = true;
mediaElement().togglePlayState();
}
}
void MediaControls::endScrubbing()
{
if (m_isPausedForScrubbing) {
m_isPausedForScrubbing = false;
if (mediaElement().togglePlayStateWillPlay())
mediaElement().togglePlayState();
}
}
void MediaControls::updateCurrentTimeDisplay()
{
double now = mediaElement().currentTime();
double duration = mediaElement().duration();
// After seek, hide duration display and show current time.
if (now > 0) {
m_currentTimeDisplay->show();
m_durationDisplay->hide();
}
// Allow the theme to format the time.
m_currentTimeDisplay->setInnerText(RenderTheme::theme().formatMediaControlsCurrentTime(now, duration), IGNORE_EXCEPTION);
m_currentTimeDisplay->setCurrentValue(now);
}
void MediaControls::updateVolume()
{
m_muteButton->updateDisplayType();
if (m_muteButton->renderer())
m_muteButton->renderer()->setShouldDoFullPaintInvalidation();
if (mediaElement().muted())
m_volumeSlider->setVolume(0);
else
m_volumeSlider->setVolume(mediaElement().volume());
}
void MediaControls::changedClosedCaptionsVisibility()
{
m_toggleClosedCaptionsButton->updateDisplayType();
}
void MediaControls::refreshClosedCaptionsButtonVisibility()
{
if (mediaElement().hasClosedCaptions())
m_toggleClosedCaptionsButton->show();
else
m_toggleClosedCaptionsButton->hide();
}
void MediaControls::textTracksChanged()
{
refreshClosedCaptionsButtonVisibility();
}
static Element* elementFromCenter(Element& element)
{
RefPtrWillBeRawPtr<ClientRect> clientRect = element.getBoundingClientRect();
int centerX = static_cast<int>((clientRect->left() + clientRect->right()) / 2);
int centerY = static_cast<int>((clientRect->top() + clientRect->bottom()) / 2);
return element.document().elementFromPoint(centerX , centerY);
}
void MediaControls::tryShowOverlayCastButton()
{
// The element needs to be shown to have its dimensions and position.
m_overlayCastButton->show();
if (elementFromCenter(*m_overlayCastButton) != &mediaElement())
m_overlayCastButton->hide();
}
void MediaControls::refreshCastButtonVisibility()
{
if (mediaElement().hasRemoteRoutes()) {
// The reason for the autoplay test is that some pages (e.g. vimeo.com) have an autoplay background video, which
// doesn't autoplay on Chrome for Android (we prevent it) so starts paused. In such cases we don't want to automatically
// show the cast button, since it looks strange and is unlikely to correspond with anything the user wants to do.
// If a user does want to cast a paused autoplay video then they can still do so by touching or clicking on the
// video, which will cause the cast button to appear.
if (!mediaElement().shouldShowControls() && !mediaElement().autoplay() && mediaElement().paused()) {
showOverlayCastButton();
} else if (mediaElement().shouldShowControls()) {
m_overlayCastButton->hide();
m_castButton->show();
// Check that the cast button actually fits on the bar.
if (m_fullScreenButton->getBoundingClientRect()->right() > m_panel->getBoundingClientRect()->right()) {
m_castButton->hide();
tryShowOverlayCastButton();
}
}
} else {
m_castButton->hide();
m_overlayCastButton->hide();
}
}
void MediaControls::showOverlayCastButton()
{
tryShowOverlayCastButton();
resetHideMediaControlsTimer();
}
void MediaControls::enteredFullscreen()
{
m_fullScreenButton->setIsFullscreen(true);
stopHideMediaControlsTimer();
startHideMediaControlsTimer();
}
void MediaControls::exitedFullscreen()
{
m_fullScreenButton->setIsFullscreen(false);
stopHideMediaControlsTimer();
startHideMediaControlsTimer();
}
void MediaControls::startedCasting()
{
m_castButton->setIsPlayingRemotely(true);
m_overlayCastButton->setIsPlayingRemotely(true);
}
void MediaControls::stoppedCasting()
{
m_castButton->setIsPlayingRemotely(false);
m_overlayCastButton->setIsPlayingRemotely(false);
}
void MediaControls::defaultEventHandler(Event* event)
{
HTMLDivElement::defaultEventHandler(event);
m_wasLastEventTouch = event->isTouchEvent() || event->isGestureEvent()
|| (event->isMouseEvent() && toMouseEvent(event)->fromTouch());
if (event->type() == EventTypeNames::mouseover) {
if (!containsRelatedTarget(event)) {
m_isMouseOverControls = true;
if (!mediaElement().togglePlayStateWillPlay()) {
makeOpaque();
if (shouldHideMediaControls())
startHideMediaControlsTimer();
}
}
return;
}
if (event->type() == EventTypeNames::mouseout) {
if (!containsRelatedTarget(event)) {
m_isMouseOverControls = false;
stopHideMediaControlsTimer();
}
return;
}
if (event->type() == EventTypeNames::mousemove) {
// When we get a mouse move, show the media controls, and start a timer
// that will hide the media controls after a 3 seconds without a mouse move.
makeOpaque();
refreshCastButtonVisibility();
if (shouldHideMediaControls(IgnoreVideoHover))
startHideMediaControlsTimer();
return;
}
}
void MediaControls::hideMediaControlsTimerFired(Timer<MediaControls>*)
{
if (mediaElement().togglePlayStateWillPlay())
return;
unsigned behaviorFlags = IgnoreFocus | IgnoreVideoHover;
if (m_wasLastEventTouch) {
behaviorFlags |= IgnoreControlsHover;
}
if (!shouldHideMediaControls(behaviorFlags))
return;
makeTransparent();
m_overlayCastButton->hide();
}
void MediaControls::startHideMediaControlsTimer()
{
m_hideMediaControlsTimer.startOneShot(timeWithoutMouseMovementBeforeHidingMediaControls, FROM_HERE);
}
void MediaControls::stopHideMediaControlsTimer()
{
m_hideMediaControlsTimer.stop();
}
void MediaControls::resetHideMediaControlsTimer()
{
stopHideMediaControlsTimer();
if (!mediaElement().paused())
startHideMediaControlsTimer();
}
const AtomicString& MediaControls::shadowPseudoId() const
{
DEFINE_STATIC_LOCAL(AtomicString, id, ("-webkit-media-controls", AtomicString::ConstructFromLiteral));
return id;
}
bool MediaControls::containsRelatedTarget(Event* event)
{
if (!event->isMouseEvent())
return false;
EventTarget* relatedTarget = toMouseEvent(event)->relatedTarget();
if (!relatedTarget)
return false;
return contains(relatedTarget->toNode());
}
void MediaControls::createTextTrackDisplay()
{
if (m_textDisplayContainer)
return;
RefPtrWillBeRawPtr<MediaControlTextTrackContainerElement> textDisplayContainer = MediaControlTextTrackContainerElement::create(*this);
m_textDisplayContainer = textDisplayContainer.get();
// Insert it before (behind) all other control elements.
m_overlayEnclosure->insertBefore(textDisplayContainer.release(), m_overlayEnclosure->firstChild());
}
void MediaControls::showTextTrackDisplay()
{
if (!m_textDisplayContainer)
createTextTrackDisplay();
m_textDisplayContainer->show();
}
void MediaControls::hideTextTrackDisplay()
{
if (!m_textDisplayContainer)
createTextTrackDisplay();
m_textDisplayContainer->hide();
}
void MediaControls::updateTextTrackDisplay()
{
if (!m_textDisplayContainer)
createTextTrackDisplay();
m_textDisplayContainer->updateDisplay();
}
void MediaControls::trace(Visitor* visitor)
{
visitor->trace(m_mediaElement);
visitor->trace(m_panel);
visitor->trace(m_textDisplayContainer);
visitor->trace(m_overlayPlayButton);
visitor->trace(m_overlayEnclosure);
visitor->trace(m_playButton);
visitor->trace(m_currentTimeDisplay);
visitor->trace(m_timeline);
visitor->trace(m_muteButton);
visitor->trace(m_volumeSlider);
visitor->trace(m_toggleClosedCaptionsButton);
visitor->trace(m_fullScreenButton);
visitor->trace(m_durationDisplay);
visitor->trace(m_enclosure);
visitor->trace(m_castButton);
visitor->trace(m_overlayCastButton);
HTMLDivElement::trace(visitor);
}
}
|