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)
55 setPseudo(AtomicString("-webkit-media-controls", AtomicString::ConstructFromLiteral));
58 void MediaControls::setMediaController(MediaControllerInterface* controller)
60 if (m_mediaController == controller)
62 m_mediaController = controller;
65 m_panel->setMediaController(controller);
66 #if ENABLE(VIDEO_TRACK)
67 if (m_textDisplayContainer)
68 m_textDisplayContainer->setMediaController(controller);
71 m_playButton->setMediaController(controller);
72 if (m_currentTimeDisplay)
73 m_currentTimeDisplay->setMediaController(controller);
75 m_timeline->setMediaController(controller);
76 if (m_panelMuteButton)
77 m_panelMuteButton->setMediaController(controller);
79 m_volumeSlider->setMediaController(controller);
80 if (m_toggleClosedCaptionsButton)
81 m_toggleClosedCaptionsButton->setMediaController(controller);
82 if (m_fullScreenButton)
83 m_fullScreenButton->setMediaController(controller);
86 void MediaControls::reset()
88 Page* page = document().page();
92 m_playButton->updateDisplayType();
94 updateCurrentTimeDisplay();
96 double duration = m_mediaController->duration();
97 if (std::isfinite(duration) || page->theme().hasOwnDisabledStateHandlingFor(MediaSliderPart)) {
98 m_timeline->setDuration(duration);
99 m_timeline->setPosition(m_mediaController->currentTime());
102 if (m_mediaController->hasAudio() || page->theme().hasOwnDisabledStateHandlingFor(MediaMuteButtonPart))
103 m_panelMuteButton->show();
105 m_panelMuteButton->hide();
107 if (m_volumeSlider) {
108 if (!m_mediaController->hasAudio())
109 m_volumeSlider->hide();
111 m_volumeSlider->show();
116 refreshClosedCaptionsButtonVisibility();
118 if (m_fullScreenButton) {
119 if (m_mediaController->supportsFullscreen() && m_mediaController->hasVideo())
120 m_fullScreenButton->show();
122 m_fullScreenButton->hide();
128 void MediaControls::reportedError()
130 Page* page = document().page();
134 if (!page->theme().hasOwnDisabledStateHandlingFor(MediaMuteButtonPart)) {
135 m_panelMuteButton->hide();
136 m_volumeSlider->hide();
139 if (m_toggleClosedCaptionsButton && !page->theme().hasOwnDisabledStateHandlingFor(MediaToggleClosedCaptionsButtonPart))
140 m_toggleClosedCaptionsButton->hide();
142 if (m_fullScreenButton && !page->theme().hasOwnDisabledStateHandlingFor(MediaEnterFullscreenButtonPart))
143 m_fullScreenButton->hide();
146 void MediaControls::loadedMetadata()
151 void MediaControls::show()
154 m_panel->setIsDisplayed(true);
158 void MediaControls::hide()
160 m_panel->setIsDisplayed(false);
164 void MediaControls::makeOpaque()
166 m_panel->makeOpaque();
169 void MediaControls::makeTransparent()
171 m_panel->makeTransparent();
174 bool MediaControls::shouldHideControls()
176 return !m_panel->hovered();
179 void MediaControls::bufferingProgressed()
181 // We only need to update buffering progress when paused, during normal
182 // playback playbackProgressed() will take care of it.
183 if (m_mediaController->paused())
184 m_timeline->setPosition(m_mediaController->currentTime());
187 void MediaControls::playbackStarted()
189 m_playButton->updateDisplayType();
190 m_timeline->setPosition(m_mediaController->currentTime());
191 updateCurrentTimeDisplay();
194 startHideFullscreenControlsTimer();
197 void MediaControls::playbackProgressed()
199 m_timeline->setPosition(m_mediaController->currentTime());
200 updateCurrentTimeDisplay();
202 if (!m_isMouseOverControls && m_mediaController->hasVideo())
206 void MediaControls::playbackStopped()
208 m_playButton->updateDisplayType();
209 m_timeline->setPosition(m_mediaController->currentTime());
210 updateCurrentTimeDisplay();
213 stopHideFullscreenControlsTimer();
216 void MediaControls::updateCurrentTimeDisplay()
218 double now = m_mediaController->currentTime();
220 Page* page = document().page();
224 m_currentTimeDisplay->setInnerText(page->theme().formatMediaControlsTime(now), IGNORE_EXCEPTION);
225 m_currentTimeDisplay->setCurrentValue(now);
228 void MediaControls::showVolumeSlider()
230 if (!m_mediaController->hasAudio())
233 m_volumeSlider->show();
236 void MediaControls::changedMute()
238 m_panelMuteButton->changedMute();
241 void MediaControls::changedVolume()
245 if (m_panelMuteButton && m_panelMuteButton->renderer())
246 m_panelMuteButton->renderer()->repaint();
249 void MediaControls::changedClosedCaptionsVisibility()
251 if (m_toggleClosedCaptionsButton)
252 m_toggleClosedCaptionsButton->updateDisplayType();
255 void MediaControls::refreshClosedCaptionsButtonVisibility()
257 if (!m_toggleClosedCaptionsButton)
260 if (m_mediaController->hasClosedCaptions())
261 m_toggleClosedCaptionsButton->show();
263 m_toggleClosedCaptionsButton->hide();
266 void MediaControls::closedCaptionTracksChanged()
268 refreshClosedCaptionsButtonVisibility();
271 void MediaControls::enteredFullscreen()
273 m_isFullscreen = true;
274 m_fullScreenButton->setIsFullscreen(true);
276 if (Page* page = document().page())
277 page->chrome().setCursorHiddenUntilMouseMoves(true);
279 startHideFullscreenControlsTimer();
280 #if ENABLE(VIDEO_TRACK)
281 if (m_textDisplayContainer)
282 m_textDisplayContainer->enteredFullscreen();
286 void MediaControls::exitedFullscreen()
288 m_isFullscreen = false;
289 m_fullScreenButton->setIsFullscreen(false);
290 stopHideFullscreenControlsTimer();
291 #if ENABLE(VIDEO_TRACK)
292 if (m_textDisplayContainer)
293 m_textDisplayContainer->exitedFullscreen();
297 void MediaControls::defaultEventHandler(Event* event)
299 HTMLDivElement::defaultEventHandler(event);
301 if (event->type() == eventNames().mouseoverEvent) {
302 if (!containsRelatedTarget(event)) {
303 m_isMouseOverControls = true;
304 if (!m_mediaController->canPlay()) {
306 if (shouldHideControls())
307 startHideFullscreenControlsTimer();
313 if (event->type() == eventNames().mouseoutEvent) {
314 if (!containsRelatedTarget(event)) {
315 m_isMouseOverControls = false;
316 stopHideFullscreenControlsTimer();
321 if (event->type() == eventNames().mousemoveEvent) {
322 if (m_isFullscreen) {
323 // When we get a mouse move in fullscreen mode, show the media controls, and start a timer
324 // that will hide the media controls after a 3 seconds without a mouse move.
326 if (shouldHideControls())
327 startHideFullscreenControlsTimer();
333 void MediaControls::hideFullscreenControlsTimerFired(Timer<MediaControls>&)
335 if (m_mediaController->paused())
341 if (!shouldHideControls())
344 if (Page* page = document().page())
345 page->chrome().setCursorHiddenUntilMouseMoves(true);
350 void MediaControls::startHideFullscreenControlsTimer()
355 Page* page = document().page();
359 m_hideFullscreenControlsTimer.startOneShot(page->settings().timeWithoutMouseMovementBeforeHidingControls());
362 void MediaControls::stopHideFullscreenControlsTimer()
364 m_hideFullscreenControlsTimer.stop();
367 bool MediaControls::containsRelatedTarget(Event* event)
369 if (!event->isMouseEvent())
371 EventTarget* relatedTarget = toMouseEvent(event)->relatedTarget();
374 return contains(relatedTarget->toNode());
377 #if ENABLE(VIDEO_TRACK)
378 void MediaControls::createTextTrackDisplay()
380 if (m_textDisplayContainer)
383 RefPtr<MediaControlTextTrackContainerElement> textDisplayContainer = MediaControlTextTrackContainerElement::create(document());
384 m_textDisplayContainer = textDisplayContainer.get();
386 if (m_mediaController)
387 m_textDisplayContainer->setMediaController(m_mediaController);
389 // Insert it before the first controller element so it always displays behind the controls.
390 insertBefore(textDisplayContainer.release(), m_panel, IGNORE_EXCEPTION);
393 void MediaControls::showTextTrackDisplay()
395 if (!m_textDisplayContainer)
396 createTextTrackDisplay();
397 m_textDisplayContainer->show();
400 void MediaControls::hideTextTrackDisplay()
402 if (!m_textDisplayContainer)
403 createTextTrackDisplay();
404 m_textDisplayContainer->hide();
407 void MediaControls::updateTextTrackDisplay()
409 if (!m_textDisplayContainer)
410 createTextTrackDisplay();
412 m_textDisplayContainer->updateDisplay();
415 void MediaControls::textTrackPreferencesChanged()
417 closedCaptionTracksChanged();
418 if (m_textDisplayContainer)
419 m_textDisplayContainer->updateSizes(true);
423 void MediaControls::setSliderVolume()
425 m_volumeSlider->setVolume(m_mediaController->muted() ? 0.0 : m_mediaController->volume());