2 * Copyright (C) 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
27 #include "core/html/HTMLVideoElement.h"
29 #include "CSSPropertyNames.h"
30 #include "HTMLNames.h"
31 #include "bindings/v8/ExceptionState.h"
32 #include "core/dom/Attribute.h"
33 #include "core/dom/Document.h"
34 #include "core/dom/ExceptionCode.h"
35 #include "core/html/HTMLImageLoader.h"
36 #include "core/html/canvas/CanvasRenderingContext.h"
37 #include "core/html/parser/HTMLParserIdioms.h"
38 #include "core/frame/Settings.h"
39 #include "core/rendering/RenderImage.h"
40 #include "core/rendering/RenderVideo.h"
41 #include "platform/UserGestureIndicator.h"
45 using namespace HTMLNames;
47 inline HTMLVideoElement::HTMLVideoElement(Document& document)
48 : HTMLMediaElement(videoTag, document)
50 ScriptWrappable::init(this);
51 if (document.settings())
52 m_defaultPosterURL = AtomicString(document.settings()->defaultVideoPosterURL());
55 PassRefPtrWillBeRawPtr<HTMLVideoElement> HTMLVideoElement::create(Document& document)
57 RefPtrWillBeRawPtr<HTMLVideoElement> videoElement(adoptRefWillBeRefCountedGarbageCollected(new HTMLVideoElement(document)));
58 videoElement->suspendIfNeeded();
59 return videoElement.release();
62 bool HTMLVideoElement::rendererIsNeeded(const RenderStyle& style)
64 return HTMLElement::rendererIsNeeded(style);
67 RenderObject* HTMLVideoElement::createRenderer(RenderStyle*)
69 return new RenderVideo(this);
72 void HTMLVideoElement::attach(const AttachContext& context)
74 HTMLMediaElement::attach(context);
77 if (shouldDisplayPosterImage()) {
79 m_imageLoader = adoptPtr(new HTMLImageLoader(this));
80 m_imageLoader->updateFromElement();
82 toRenderImage(renderer())->imageResource()->setImageResource(m_imageLoader->image());
86 void HTMLVideoElement::collectStyleForPresentationAttribute(const QualifiedName& name, const AtomicString& value, MutableStylePropertySet* style)
88 if (name == widthAttr)
89 addHTMLLengthToStyle(style, CSSPropertyWidth, value);
90 else if (name == heightAttr)
91 addHTMLLengthToStyle(style, CSSPropertyHeight, value);
93 HTMLMediaElement::collectStyleForPresentationAttribute(name, value, style);
96 bool HTMLVideoElement::isPresentationAttribute(const QualifiedName& name) const
98 if (name == widthAttr || name == heightAttr)
100 return HTMLMediaElement::isPresentationAttribute(name);
103 void HTMLVideoElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
105 if (name == posterAttr) {
106 // Force a poster recalc by setting m_displayMode to Unknown directly before calling updateDisplayState.
107 HTMLMediaElement::setDisplayMode(Unknown);
108 updateDisplayState();
109 if (shouldDisplayPosterImage()) {
111 m_imageLoader = adoptPtr(new HTMLImageLoader(this));
112 m_imageLoader->updateFromElementIgnoringPreviousError();
115 toRenderImage(renderer())->imageResource()->setImageResource(0);
117 // Notify the player when the poster image URL changes.
119 player()->setPoster(posterImageURL());
121 HTMLMediaElement::parseAttribute(name, value);
124 bool HTMLVideoElement::supportsFullscreen() const
126 if (!document().page())
135 unsigned HTMLVideoElement::videoWidth() const
139 return player()->naturalSize().width();
142 unsigned HTMLVideoElement::videoHeight() const
146 return player()->naturalSize().height();
149 bool HTMLVideoElement::isURLAttribute(const Attribute& attribute) const
151 return attribute.name() == posterAttr || HTMLMediaElement::isURLAttribute(attribute);
154 const AtomicString HTMLVideoElement::imageSourceURL() const
156 const AtomicString& url = getAttribute(posterAttr);
157 if (!stripLeadingAndTrailingHTMLSpaces(url).isEmpty())
159 return m_defaultPosterURL;
162 void HTMLVideoElement::setDisplayMode(DisplayMode mode)
164 DisplayMode oldMode = displayMode();
165 KURL poster = posterImageURL();
167 if (!poster.isEmpty()) {
168 // We have a poster path, but only show it until the user triggers display by playing or seeking and the
169 // media engine has something to display.
170 // Don't show the poster if there is a seek operation or
171 // the video has restarted because of loop attribute
172 if (mode == Video && oldMode == Poster && !hasAvailableVideoFrame())
173 mode = PosterWaitingForVideo;
176 HTMLMediaElement::setDisplayMode(mode);
178 if (renderer() && displayMode() != oldMode)
179 renderer()->updateFromElement();
182 void HTMLVideoElement::updateDisplayState()
184 if (posterImageURL().isEmpty())
185 setDisplayMode(Video);
186 else if (displayMode() < Poster)
187 setDisplayMode(Poster);
190 void HTMLVideoElement::paintCurrentFrameInContext(GraphicsContext* context, const IntRect& destRect) const
192 MediaPlayer* player = HTMLMediaElement::player();
195 player->paint(context, destRect);
198 bool HTMLVideoElement::copyVideoTextureToPlatformTexture(blink::WebGraphicsContext3D* context, Platform3DObject texture, GLint level, GLenum type, GLenum internalFormat, bool premultiplyAlpha, bool flipY)
202 return player()->copyVideoTextureToPlatformTexture(context, texture, level, type, internalFormat, premultiplyAlpha, flipY);
205 bool HTMLVideoElement::hasAvailableVideoFrame() const
210 return player()->hasVideo() && player()->readyState() >= MediaPlayer::HaveCurrentData;
213 void HTMLVideoElement::webkitEnterFullscreen(ExceptionState& exceptionState)
218 if (!supportsFullscreen()) {
219 exceptionState.throwDOMException(InvalidStateError, "This element does not support fullscreen mode.");
226 void HTMLVideoElement::webkitExitFullscreen()
232 bool HTMLVideoElement::webkitSupportsFullscreen()
234 return supportsFullscreen();
237 bool HTMLVideoElement::webkitDisplayingFullscreen()
239 return isFullscreen();
242 void HTMLVideoElement::didMoveToNewDocument(Document& oldDocument)
245 m_imageLoader->elementDidMoveToNewDocument();
246 HTMLMediaElement::didMoveToNewDocument(oldDocument);
249 unsigned HTMLVideoElement::webkitDecodedFrameCount() const
254 return player()->decodedFrameCount();
257 unsigned HTMLVideoElement::webkitDroppedFrameCount() const
262 return player()->droppedFrameCount();
265 KURL HTMLVideoElement::posterImageURL() const
267 String url = stripLeadingAndTrailingHTMLSpaces(imageSourceURL());
270 return document().completeURL(url);
273 KURL HTMLVideoElement::mediaPlayerPosterURL()
275 return posterImageURL();
278 PassRefPtr<Image> HTMLVideoElement::getSourceImageForCanvas(SourceImageMode mode, SourceImageStatus* status) const
280 if (!hasAvailableVideoFrame()) {
281 *status = InvalidSourceImageStatus;
285 IntSize intrinsicSize(videoWidth(), videoHeight());
286 OwnPtr<ImageBuffer> imageBuffer = ImageBuffer::create(intrinsicSize);
288 *status = InvalidSourceImageStatus;
292 paintCurrentFrameInContext(imageBuffer->context(), IntRect(IntPoint(0, 0), intrinsicSize));
294 *status = NormalSourceImageStatus;
295 return imageBuffer->copyImage(mode == CopySourceImageIfVolatile ? CopyBackingStore : DontCopyBackingStore, Unscaled);
298 bool HTMLVideoElement::wouldTaintOrigin(SecurityOrigin* destinationSecurityOrigin) const
300 return !hasSingleSecurityOrigin() || (!(player() && player()->didPassCORSAccessCheck()) && destinationSecurityOrigin->taintsCanvas(currentSrc()));
303 FloatSize HTMLVideoElement::sourceSize() const
305 return FloatSize(videoWidth(), videoHeight());