Reviewed by Antti.
[WebKit-https.git] / WebCore / rendering / RenderMedia.cpp
1 /*
2  * Copyright (C) 2007, 2008 Apple Inc.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
24  */
25
26 #include "config.h"
27
28 #if ENABLE(VIDEO)
29 #include "RenderMedia.h"
30
31 #include "CSSStyleSelector.h"
32 #include "Event.h"
33 #include "EventNames.h"
34 #include "FloatConversion.h"
35 #include "FrameView.h"
36 #include "GraphicsContext.h"
37 #include "HTMLMediaElement.h"
38 #include "MediaControlElements.h"
39 #include "MouseEvent.h"
40 #include "MediaPlayer.h"
41 #include "RenderSlider.h"
42 #include "SystemTime.h"
43 #include <wtf/MathExtras.h>
44
45 using namespace std;
46
47 namespace WebCore {
48
49 using namespace EventNames;
50
51 static const double cTimeUpdateRepeatDelay = 0.2;
52 static const double cOpacityAnimationRepeatDelay = 0.05;
53 // FIXME get this from style
54 static const double cOpacityAnimationDuration = 0.1;
55
56 RenderMedia::RenderMedia(HTMLMediaElement* video)
57     : RenderReplaced(video)
58     , m_timeUpdateTimer(this, &RenderMedia::timeUpdateTimerFired)
59     , m_opacityAnimationTimer(this, &RenderMedia::opacityAnimationTimerFired)
60     , m_mouseOver(false)
61     , m_opacityAnimationStartTime(0)
62     , m_opacityAnimationFrom(0)
63     , m_opacityAnimationTo(1.0f)
64 {
65 }
66
67 RenderMedia::RenderMedia(HTMLMediaElement* video, const IntSize& intrinsicSize)
68     : RenderReplaced(video, intrinsicSize)
69     , m_timeUpdateTimer(this, &RenderMedia::timeUpdateTimerFired)
70     , m_opacityAnimationTimer(this, &RenderMedia::opacityAnimationTimerFired)
71     , m_mouseOver(false)
72     , m_opacityAnimationStartTime(0)
73     , m_opacityAnimationFrom(0)
74     , m_opacityAnimationTo(1.0f)
75 {
76 }
77
78 RenderMedia::~RenderMedia()
79 {
80     if (m_controlsShadowRoot && m_controlsShadowRoot->renderer()) {
81         static_cast<RenderMediaControlShadowRoot*>(m_controlsShadowRoot->renderer())->setParent(0);
82         m_controlsShadowRoot->detach();
83     }
84 }
85  
86 HTMLMediaElement* RenderMedia::mediaElement() const
87
88     return static_cast<HTMLMediaElement*>(node()); 
89 }
90
91 MediaPlayer* RenderMedia::player() const
92 {
93     return mediaElement()->player();
94 }
95
96 void RenderMedia::layout()
97 {
98     IntSize oldSize = contentBox().size();
99
100     RenderReplaced::layout();
101
102     RenderObject* controlsRenderer = m_controlsShadowRoot ? m_controlsShadowRoot->renderer() : 0;
103     if (!controlsRenderer)
104         return;
105     IntSize newSize = contentBox().size();
106     if (newSize != oldSize || controlsRenderer->needsLayout()) {
107         controlsRenderer->setPos(borderLeft() + paddingLeft(), borderTop() + paddingTop());
108         controlsRenderer->style()->setHeight(Length(newSize.height(), Fixed));
109         controlsRenderer->style()->setWidth(Length(newSize.width(), Fixed));
110         controlsRenderer->setNeedsLayout(true, false);
111         controlsRenderer->layout();
112         setChildNeedsLayout(false);
113     }
114 }
115
116 RenderObject* RenderMedia::firstChild() const 
117
118     return m_controlsShadowRoot ? m_controlsShadowRoot->renderer() : 0; 
119 }
120
121 RenderObject* RenderMedia::lastChild() const 
122
123     return m_controlsShadowRoot ? m_controlsShadowRoot->renderer() : 0;
124 }
125     
126 void RenderMedia::removeChild(RenderObject* child)
127 {
128     ASSERT(m_controlsShadowRoot);
129     ASSERT(child == m_controlsShadowRoot->renderer());
130     child->removeLayers(enclosingLayer());
131 }
132     
133 void RenderMedia::createControlsShadowRoot()
134 {
135     ASSERT(!m_controlsShadowRoot);
136     m_controlsShadowRoot = new MediaControlShadowRootElement(document(), mediaElement());
137 }
138
139 void RenderMedia::createPanel()
140 {
141     ASSERT(!m_panel);
142     RenderStyle* style = getPseudoStyle(RenderStyle::MEDIA_CONTROLS_PANEL);
143     m_panel = new HTMLDivElement(document());
144     RenderObject* renderer = m_panel->createRenderer(renderArena(), style);
145     if (renderer) {
146         m_panel->setRenderer(renderer);
147         renderer->setStyle(style);
148         m_panel->setAttached();
149         m_panel->setInDocument(true);
150         m_controlsShadowRoot->addChild(m_panel);
151         m_controlsShadowRoot->renderer()->addChild(renderer);
152     }
153 }
154
155 void RenderMedia::createMuteButton()
156 {
157     ASSERT(!m_muteButton);
158     m_muteButton = new MediaControlMuteButtonElement(document(), mediaElement());
159     m_muteButton->attachToParent(m_panel.get());
160 }
161
162 void RenderMedia::createPlayButton()
163 {
164     ASSERT(!m_playButton);
165     m_playButton = new MediaControlPlayButtonElement(document(), mediaElement());
166     m_playButton->attachToParent(m_panel.get());
167 }
168
169 void RenderMedia::createSeekBackButton()
170 {
171     ASSERT(!m_seekBackButton);
172     m_seekBackButton = new MediaControlSeekButtonElement(document(), mediaElement(), false);
173     m_seekBackButton->attachToParent(m_panel.get());
174 }
175
176 void RenderMedia::createSeekForwardButton()
177 {
178     ASSERT(!m_seekForwardButton);
179     m_seekForwardButton = new MediaControlSeekButtonElement(document(), mediaElement(), true);
180     m_seekForwardButton->attachToParent(m_panel.get());
181 }
182
183 void RenderMedia::createTimeline()
184 {
185     ASSERT(!m_timeline);
186     m_timeline = new MediaControlTimelineElement(document(), mediaElement());
187     m_timeline->attachToParent(m_panel.get());
188 }
189   
190 void RenderMedia::createTimeDisplay()
191 {
192     ASSERT(!m_timeDisplay);
193     RenderStyle* style = getPseudoStyle(RenderStyle::MEDIA_CONTROLS_TIME_DISPLAY);
194     m_timeDisplay = new HTMLDivElement(document());
195     RenderObject* renderer = m_timeDisplay->createRenderer(renderArena(), style);
196     if (renderer) {
197         m_timeDisplay->setRenderer(renderer);
198         renderer->setStyle(style);
199         m_timeDisplay->setAttached();
200         m_timeDisplay->setInDocument(true);
201         m_panel->addChild(m_timeDisplay);
202         m_panel->renderer()->addChild(renderer);
203     }
204 }
205
206 void RenderMedia::createFullscreenButton()
207 {
208     ASSERT(!m_fullscreenButton);
209     m_fullscreenButton = new MediaControlFullscreenButtonElement(document(), mediaElement());
210     m_fullscreenButton->attachToParent(m_panel.get());
211 }
212     
213 void RenderMedia::updateFromElement()
214 {
215     updateControls();
216 }
217             
218 void RenderMedia::updateControls()
219 {
220     HTMLMediaElement* media = mediaElement();
221     if (!media->controls()) {
222         if (m_controlsShadowRoot) {
223             m_controlsShadowRoot->detach();
224             m_panel = 0;
225             m_muteButton = 0;
226             m_playButton = 0;
227             m_timeline = 0;
228             m_seekBackButton = 0;
229             m_seekForwardButton = 0;
230             m_timeDisplay = 0;
231             m_fullscreenButton = 0;
232             m_controlsShadowRoot = 0;
233         }
234         return;
235     }
236     
237     if (!m_controlsShadowRoot) {
238         createControlsShadowRoot();
239         createPanel();
240         createMuteButton();
241         createPlayButton();
242         createTimeline();
243         createSeekBackButton();
244         createSeekForwardButton();
245         createTimeDisplay();
246         createFullscreenButton();
247     }
248     
249     if (media->paused() || media->ended() || media->networkState() < HTMLMediaElement::LOADED_METADATA)
250         m_timeUpdateTimer.stop();
251     else
252         m_timeUpdateTimer.startRepeating(cTimeUpdateRepeatDelay);
253     
254     if (m_muteButton)
255         m_muteButton->update();
256     if (m_playButton)
257         m_playButton->update();
258     if (m_timeline)
259         m_timeline->update();
260     if (m_seekBackButton)
261         m_seekBackButton->update();
262     if (m_seekForwardButton)
263         m_seekForwardButton->update();
264     if (m_fullscreenButton)
265         m_fullscreenButton->update();
266     updateTimeDisplay();
267     updateControlVisibility();
268 }
269
270 void RenderMedia::timeUpdateTimerFired(Timer<RenderMedia>*)
271 {
272     if (m_timeline)
273         m_timeline->update(false);
274     updateTimeDisplay();
275 }
276     
277 String RenderMedia::formatTime(float time)
278 {
279     if (!isfinite(time))
280         time = 0;
281     int seconds = (int)time; 
282     int hours = seconds / (60 * 60);
283     int minutes = (seconds / 60) % 60;
284     seconds %= 60;
285     return String::format("%02d:%02d:%02d", hours, minutes, seconds);
286 }
287
288 void RenderMedia::updateTimeDisplay()
289 {
290     if (!m_timeDisplay)
291         return;
292     String timeString = formatTime(mediaElement()->currentTime());
293     ExceptionCode ec;
294     m_timeDisplay->setInnerText(timeString, ec);
295 }
296     
297 void RenderMedia::updateControlVisibility() 
298 {
299     if (!m_panel || !m_panel->renderer())
300         return;
301     // do fading manually, css animations don't work well with shadow trees
302     HTMLMediaElement* media = mediaElement();
303     // Don't fade for audio controls.
304     if (!media->isVideo())
305         return;
306     bool visible = m_mouseOver || media->paused() || media->ended() || media->networkState() < HTMLMediaElement::LOADED_METADATA;
307     if (visible == (m_opacityAnimationTo > 0))
308         return;
309     if (visible) {
310         m_opacityAnimationFrom = m_panel->renderer()->style()->opacity();
311         m_opacityAnimationTo = 1.0f;
312     } else {
313         m_opacityAnimationFrom = m_panel->renderer()->style()->opacity();
314         m_opacityAnimationTo = 0;
315     }
316     m_opacityAnimationStartTime = currentTime();
317     m_opacityAnimationTimer.startRepeating(cOpacityAnimationRepeatDelay);
318 }
319     
320 void RenderMedia::changeOpacity(HTMLElement* e, float opacity) 
321 {
322     if (!e || !e->renderer() || !e->renderer()->style())
323         return;
324     RenderStyle* s = new (renderArena()) RenderStyle(*e->renderer()->style());
325     s->setOpacity(opacity);
326     // z-index can't be auto if opacity is used
327     s->setZIndex(0);
328     e->renderer()->setStyle(s);
329 }
330     
331 void RenderMedia::opacityAnimationTimerFired(Timer<RenderMedia>*)
332 {
333     double time = currentTime() - m_opacityAnimationStartTime;
334     if (time >= cOpacityAnimationDuration) {
335         time = cOpacityAnimationDuration;
336         m_opacityAnimationTimer.stop();
337     }
338     float opacity = narrowPrecisionToFloat(m_opacityAnimationFrom + (m_opacityAnimationTo - m_opacityAnimationFrom) * time / cOpacityAnimationDuration);
339     changeOpacity(m_panel.get(), opacity);
340 }
341     
342 void RenderMedia::forwardEvent(Event* event)
343 {
344     if (event->isMouseEvent() && m_controlsShadowRoot) {
345         MouseEvent* mouseEvent = static_cast<MouseEvent*>(event);
346         IntPoint point(mouseEvent->pageX(), mouseEvent->pageY());
347         if (m_muteButton && m_muteButton->renderer() && m_muteButton->renderer()->absoluteBoundingBoxRect().contains(point))
348             m_muteButton->defaultEventHandler(event);
349         if (m_playButton && m_playButton->renderer() && m_playButton->renderer()->absoluteBoundingBoxRect().contains(point))
350             m_playButton->defaultEventHandler(event);
351         if (m_seekBackButton && m_seekBackButton->renderer() && m_seekBackButton->renderer()->absoluteBoundingBoxRect().contains(point))
352             m_seekBackButton->defaultEventHandler(event);
353         if (m_seekForwardButton && m_seekForwardButton->renderer() && m_seekForwardButton->renderer()->absoluteBoundingBoxRect().contains(point))
354             m_seekForwardButton->defaultEventHandler(event);
355         if (m_timeline && m_timeline->renderer() && m_timeline->renderer()->absoluteBoundingBoxRect().contains(point))
356             m_timeline->defaultEventHandler(event);
357         if (m_fullscreenButton && m_fullscreenButton->renderer() && m_fullscreenButton->renderer()->absoluteBoundingBoxRect().contains(point))
358             m_fullscreenButton->defaultEventHandler(event);
359         
360         if (event->type() == mouseoverEvent) {
361             m_mouseOver = true;
362             updateControlVisibility();
363         }
364         if (event->type() == mouseoutEvent) {
365             // FIXME: moving over scrollbar thumb generates mouseout for the ancestor media element for some reason
366             m_mouseOver = absoluteBoundingBoxRect().contains(point);
367             updateControlVisibility();
368         }
369     }
370 }
371
372 } // namespace WebCore
373
374 #endif