2 * Copyright (C) 2011, 2012 Apple Inc. All rights reserved.
3 * Copyright (C) 2011, 2012 Google Inc. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "MediaControls.h"
32 #include "ExceptionCodePlaceholder.h"
37 MediaControls::MediaControls(Document& document)
38 : HTMLDivElement(HTMLNames::divTag, document)
39 , m_mediaController(0)
41 #if ENABLE(VIDEO_TRACK)
42 , m_textDisplayContainer(0)
45 , m_currentTimeDisplay(0)
47 , m_panelMuteButton(0)
49 , m_toggleClosedCaptionsButton(0)
50 , m_fullScreenButton(0)
51 , m_hideFullscreenControlsTimer(this, &MediaControls::hideFullscreenControlsTimerFired)
52 , m_isFullscreen(false)
53 , m_isMouseOverControls(false)
57 void MediaControls::setMediaController(MediaControllerInterface* controller)
59 if (m_mediaController == controller)
61 m_mediaController = controller;
64 m_panel->setMediaController(controller);
65 #if ENABLE(VIDEO_TRACK)
66 if (m_textDisplayContainer)
67 m_textDisplayContainer->setMediaController(controller);
70 m_playButton->setMediaController(controller);
71 if (m_currentTimeDisplay)
72 m_currentTimeDisplay->setMediaController(controller);
74 m_timeline->setMediaController(controller);
75 if (m_panelMuteButton)
76 m_panelMuteButton->setMediaController(controller);
78 m_volumeSlider->setMediaController(controller);
79 if (m_toggleClosedCaptionsButton)
80 m_toggleClosedCaptionsButton->setMediaController(controller);
81 if (m_fullScreenButton)
82 m_fullScreenButton->setMediaController(controller);
85 void MediaControls::reset()
87 Page* page = document().page();
91 m_playButton->updateDisplayType();
93 updateCurrentTimeDisplay();
95 double duration = m_mediaController->duration();
96 if (std::isfinite(duration) || page->theme().hasOwnDisabledStateHandlingFor(MediaSliderPart)) {
97 m_timeline->setDuration(duration);
98 m_timeline->setPosition(m_mediaController->currentTime());
101 if (m_mediaController->hasAudio() || page->theme().hasOwnDisabledStateHandlingFor(MediaMuteButtonPart))
102 m_panelMuteButton->show();
104 m_panelMuteButton->hide();
106 if (m_volumeSlider) {
107 if (!m_mediaController->hasAudio())
108 m_volumeSlider->hide();
110 m_volumeSlider->show();
115 refreshClosedCaptionsButtonVisibility();
117 if (m_fullScreenButton) {
118 if (m_mediaController->supportsFullscreen() && m_mediaController->hasVideo())
119 m_fullScreenButton->show();
121 m_fullScreenButton->hide();
127 void MediaControls::reportedError()
129 Page* page = document().page();
133 if (!page->theme().hasOwnDisabledStateHandlingFor(MediaMuteButtonPart)) {
134 m_panelMuteButton->hide();
135 m_volumeSlider->hide();
138 if (m_toggleClosedCaptionsButton && !page->theme().hasOwnDisabledStateHandlingFor(MediaToggleClosedCaptionsButtonPart))
139 m_toggleClosedCaptionsButton->hide();
141 if (m_fullScreenButton && !page->theme().hasOwnDisabledStateHandlingFor(MediaEnterFullscreenButtonPart))
142 m_fullScreenButton->hide();
145 void MediaControls::loadedMetadata()
150 void MediaControls::show()
153 m_panel->setIsDisplayed(true);
157 void MediaControls::hide()
159 m_panel->setIsDisplayed(false);
163 void MediaControls::makeOpaque()
165 m_panel->makeOpaque();
168 void MediaControls::makeTransparent()
170 m_panel->makeTransparent();
173 bool MediaControls::shouldHideControls()
175 return !m_panel->hovered();
178 void MediaControls::bufferingProgressed()
180 // We only need to update buffering progress when paused, during normal
181 // playback playbackProgressed() will take care of it.
182 if (m_mediaController->paused())
183 m_timeline->setPosition(m_mediaController->currentTime());
186 void MediaControls::playbackStarted()
188 m_playButton->updateDisplayType();
189 m_timeline->setPosition(m_mediaController->currentTime());
190 updateCurrentTimeDisplay();
193 startHideFullscreenControlsTimer();
196 void MediaControls::playbackProgressed()
198 m_timeline->setPosition(m_mediaController->currentTime());
199 updateCurrentTimeDisplay();
201 if (!m_isMouseOverControls && m_mediaController->hasVideo())
205 void MediaControls::playbackStopped()
207 m_playButton->updateDisplayType();
208 m_timeline->setPosition(m_mediaController->currentTime());
209 updateCurrentTimeDisplay();
212 stopHideFullscreenControlsTimer();
215 void MediaControls::updateCurrentTimeDisplay()
217 double now = m_mediaController->currentTime();
219 Page* page = document().page();
223 m_currentTimeDisplay->setInnerText(page->theme().formatMediaControlsTime(now), IGNORE_EXCEPTION);
224 m_currentTimeDisplay->setCurrentValue(now);
227 void MediaControls::showVolumeSlider()
229 if (!m_mediaController->hasAudio())
232 m_volumeSlider->show();
235 void MediaControls::changedMute()
237 m_panelMuteButton->changedMute();
240 void MediaControls::changedVolume()
244 if (m_panelMuteButton && m_panelMuteButton->renderer())
245 m_panelMuteButton->renderer()->repaint();
248 void MediaControls::changedClosedCaptionsVisibility()
250 if (m_toggleClosedCaptionsButton)
251 m_toggleClosedCaptionsButton->updateDisplayType();
254 void MediaControls::refreshClosedCaptionsButtonVisibility()
256 if (!m_toggleClosedCaptionsButton)
259 if (m_mediaController->hasClosedCaptions())
260 m_toggleClosedCaptionsButton->show();
262 m_toggleClosedCaptionsButton->hide();
265 void MediaControls::closedCaptionTracksChanged()
267 refreshClosedCaptionsButtonVisibility();
270 void MediaControls::enteredFullscreen()
272 m_isFullscreen = true;
273 m_fullScreenButton->setIsFullscreen(true);
275 if (Page* page = document().page())
276 page->chrome().setCursorHiddenUntilMouseMoves(true);
278 startHideFullscreenControlsTimer();
279 #if ENABLE(VIDEO_TRACK)
280 if (m_textDisplayContainer)
281 m_textDisplayContainer->enteredFullscreen();
285 void MediaControls::exitedFullscreen()
287 m_isFullscreen = false;
288 m_fullScreenButton->setIsFullscreen(false);
289 stopHideFullscreenControlsTimer();
290 #if ENABLE(VIDEO_TRACK)
291 if (m_textDisplayContainer)
292 m_textDisplayContainer->exitedFullscreen();
296 void MediaControls::defaultEventHandler(Event* event)
298 HTMLDivElement::defaultEventHandler(event);
300 if (event->type() == eventNames().mouseoverEvent) {
301 if (!containsRelatedTarget(event)) {
302 m_isMouseOverControls = true;
303 if (!m_mediaController->canPlay()) {
305 if (shouldHideControls())
306 startHideFullscreenControlsTimer();
312 if (event->type() == eventNames().mouseoutEvent) {
313 if (!containsRelatedTarget(event)) {
314 m_isMouseOverControls = false;
315 stopHideFullscreenControlsTimer();
320 if (event->type() == eventNames().mousemoveEvent) {
321 if (m_isFullscreen) {
322 // When we get a mouse move in fullscreen mode, show the media controls, and start a timer
323 // that will hide the media controls after a 3 seconds without a mouse move.
325 if (shouldHideControls())
326 startHideFullscreenControlsTimer();
332 void MediaControls::hideFullscreenControlsTimerFired(Timer<MediaControls>&)
334 if (m_mediaController->paused())
340 if (!shouldHideControls())
343 if (Page* page = document().page())
344 page->chrome().setCursorHiddenUntilMouseMoves(true);
349 void MediaControls::startHideFullscreenControlsTimer()
354 Page* page = document().page();
358 m_hideFullscreenControlsTimer.startOneShot(page->settings().timeWithoutMouseMovementBeforeHidingControls());
361 void MediaControls::stopHideFullscreenControlsTimer()
363 m_hideFullscreenControlsTimer.stop();
366 const AtomicString& MediaControls::shadowPseudoId() const
368 DEPRECATED_DEFINE_STATIC_LOCAL(AtomicString, id, ("-webkit-media-controls"));
372 bool MediaControls::containsRelatedTarget(Event* event)
374 if (!event->isMouseEvent())
376 EventTarget* relatedTarget = toMouseEvent(event)->relatedTarget();
379 return contains(relatedTarget->toNode());
382 #if ENABLE(VIDEO_TRACK)
383 void MediaControls::createTextTrackDisplay()
385 if (m_textDisplayContainer)
388 RefPtr<MediaControlTextTrackContainerElement> textDisplayContainer = MediaControlTextTrackContainerElement::create(document());
389 m_textDisplayContainer = textDisplayContainer.get();
391 if (m_mediaController)
392 m_textDisplayContainer->setMediaController(m_mediaController);
394 // Insert it before the first controller element so it always displays behind the controls.
395 insertBefore(textDisplayContainer.release(), m_panel, IGNORE_EXCEPTION);
398 void MediaControls::showTextTrackDisplay()
400 if (!m_textDisplayContainer)
401 createTextTrackDisplay();
402 m_textDisplayContainer->show();
405 void MediaControls::hideTextTrackDisplay()
407 if (!m_textDisplayContainer)
408 createTextTrackDisplay();
409 m_textDisplayContainer->hide();
412 void MediaControls::updateTextTrackDisplay()
414 if (!m_textDisplayContainer)
415 createTextTrackDisplay();
417 m_textDisplayContainer->updateDisplay();
420 void MediaControls::textTrackPreferencesChanged()
422 closedCaptionTracksChanged();
423 if (m_textDisplayContainer)
424 m_textDisplayContainer->updateSizes(true);
428 void MediaControls::setSliderVolume()
430 m_volumeSlider->setVolume(m_mediaController->muted() ? 0.0 : m_mediaController->volume());