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 "bindings/core/v8/ExceptionState.h"
30 #include "core/CSSPropertyNames.h"
31 #include "core/HTMLNames.h"
32 #include "core/dom/Attribute.h"
33 #include "core/dom/Document.h"
34 #include "core/dom/ExceptionCode.h"
35 #include "core/dom/shadow/ShadowRoot.h"
36 #include "core/frame/Settings.h"
37 #include "core/html/HTMLImageLoader.h"
38 #include "core/html/canvas/CanvasRenderingContext.h"
39 #include "core/html/parser/HTMLParserIdioms.h"
40 #include "core/rendering/RenderImage.h"
41 #include "core/rendering/RenderVideo.h"
42 #include "platform/UserGestureIndicator.h"
43 #include "platform/graphics/GraphicsContext.h"
44 #include "platform/graphics/ImageBuffer.h"
45 #include "platform/graphics/gpu/Extensions3DUtil.h"
46 #include "public/platform/WebCanvas.h"
47 #include "public/platform/WebGraphicsContext3D.h"
51 using namespace HTMLNames;
53 inline HTMLVideoElement::HTMLVideoElement(Document& document)
54 : HTMLMediaElement(videoTag, document)
56 if (document.settings())
57 m_defaultPosterURL = AtomicString(document.settings()->defaultVideoPosterURL());
60 PassRefPtrWillBeRawPtr<HTMLVideoElement> HTMLVideoElement::create(Document& document)
62 RefPtrWillBeRawPtr<HTMLVideoElement> video = adoptRefWillBeNoop(new HTMLVideoElement(document));
63 video->ensureUserAgentShadowRoot();
64 video->suspendIfNeeded();
65 return video.release();
68 void HTMLVideoElement::trace(Visitor* visitor)
70 visitor->trace(m_imageLoader);
71 HTMLMediaElement::trace(visitor);
74 bool HTMLVideoElement::rendererIsNeeded(const RenderStyle& style)
76 return HTMLElement::rendererIsNeeded(style);
79 RenderObject* HTMLVideoElement::createRenderer(RenderStyle*)
81 return new RenderVideo(this);
84 void HTMLVideoElement::attach(const AttachContext& context)
86 HTMLMediaElement::attach(context);
89 if (shouldDisplayPosterImage()) {
91 m_imageLoader = HTMLImageLoader::create(this);
92 m_imageLoader->updateFromElement();
94 toRenderImage(renderer())->imageResource()->setImageResource(m_imageLoader->image());
98 void HTMLVideoElement::collectStyleForPresentationAttribute(const QualifiedName& name, const AtomicString& value, MutableStylePropertySet* style)
100 if (name == widthAttr)
101 addHTMLLengthToStyle(style, CSSPropertyWidth, value);
102 else if (name == heightAttr)
103 addHTMLLengthToStyle(style, CSSPropertyHeight, value);
105 HTMLMediaElement::collectStyleForPresentationAttribute(name, value, style);
108 bool HTMLVideoElement::isPresentationAttribute(const QualifiedName& name) const
110 if (name == widthAttr || name == heightAttr)
112 return HTMLMediaElement::isPresentationAttribute(name);
115 void HTMLVideoElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
117 if (name == posterAttr) {
118 // Force a poster recalc by setting m_displayMode to Unknown directly before calling updateDisplayState.
119 HTMLMediaElement::setDisplayMode(Unknown);
120 updateDisplayState();
121 if (shouldDisplayPosterImage()) {
123 m_imageLoader = HTMLImageLoader::create(this);
124 m_imageLoader->updateFromElement(ImageLoader::UpdateIgnorePreviousError);
127 toRenderImage(renderer())->imageResource()->setImageResource(0);
129 // Notify the player when the poster image URL changes.
130 if (webMediaPlayer())
131 webMediaPlayer()->setPoster(posterImageURL());
133 HTMLMediaElement::parseAttribute(name, value);
137 bool HTMLVideoElement::supportsFullscreen() const
139 if (!document().page())
142 if (!webMediaPlayer())
148 unsigned HTMLVideoElement::videoWidth() const
150 if (!webMediaPlayer())
152 return webMediaPlayer()->naturalSize().width;
155 unsigned HTMLVideoElement::videoHeight() const
157 if (!webMediaPlayer())
159 return webMediaPlayer()->naturalSize().height;
162 bool HTMLVideoElement::isURLAttribute(const Attribute& attribute) const
164 return attribute.name() == posterAttr || HTMLMediaElement::isURLAttribute(attribute);
167 const AtomicString HTMLVideoElement::imageSourceURL() const
169 const AtomicString& url = getAttribute(posterAttr);
170 if (!stripLeadingAndTrailingHTMLSpaces(url).isEmpty())
172 return m_defaultPosterURL;
175 void HTMLVideoElement::setDisplayMode(DisplayMode mode)
177 DisplayMode oldMode = displayMode();
178 KURL poster = posterImageURL();
180 if (!poster.isEmpty()) {
181 // We have a poster path, but only show it until the user triggers display by playing or seeking and the
182 // media engine has something to display.
183 // Don't show the poster if there is a seek operation or
184 // the video has restarted because of loop attribute
185 if (mode == Video && oldMode == Poster && !hasAvailableVideoFrame())
186 mode = PosterWaitingForVideo;
189 HTMLMediaElement::setDisplayMode(mode);
191 if (renderer() && displayMode() != oldMode)
192 renderer()->updateFromElement();
195 void HTMLVideoElement::updateDisplayState()
197 if (posterImageURL().isEmpty())
198 setDisplayMode(Video);
199 else if (displayMode() < Poster)
200 setDisplayMode(Poster);
203 void HTMLVideoElement::paintCurrentFrameInContext(GraphicsContext* context, const IntRect& destRect) const
205 if (!webMediaPlayer())
208 WebCanvas* canvas = context->canvas();
209 SkXfermode::Mode mode = WebCoreCompositeToSkiaComposite(context->compositeOperation(), context->blendModeOperation());
210 webMediaPlayer()->paint(canvas, destRect, context->getNormalizedAlpha(), mode);
213 bool HTMLVideoElement::copyVideoTextureToPlatformTexture(WebGraphicsContext3D* context, Platform3DObject texture, GLint level, GLenum internalFormat, GLenum type, bool premultiplyAlpha, bool flipY)
215 if (!webMediaPlayer())
218 if (!Extensions3DUtil::canUseCopyTextureCHROMIUM(internalFormat, type, level))
221 return webMediaPlayer()->copyVideoTextureToPlatformTexture(context, texture, level, internalFormat, type, premultiplyAlpha, flipY);
224 bool HTMLVideoElement::hasAvailableVideoFrame() const
226 if (!webMediaPlayer())
229 return webMediaPlayer()->hasVideo() && webMediaPlayer()->readyState() >= blink::WebMediaPlayer::ReadyStateHaveCurrentData;
232 void HTMLVideoElement::webkitEnterFullscreen(ExceptionState& exceptionState)
237 if (!supportsFullscreen()) {
238 exceptionState.throwDOMException(InvalidStateError, "This element does not support fullscreen mode.");
245 void HTMLVideoElement::webkitExitFullscreen()
251 bool HTMLVideoElement::webkitSupportsFullscreen()
253 return supportsFullscreen();
256 bool HTMLVideoElement::webkitDisplayingFullscreen()
258 return isFullscreen();
261 void HTMLVideoElement::didMoveToNewDocument(Document& oldDocument)
264 m_imageLoader->elementDidMoveToNewDocument();
265 HTMLMediaElement::didMoveToNewDocument(oldDocument);
268 unsigned HTMLVideoElement::webkitDecodedFrameCount() const
270 if (!webMediaPlayer())
273 return webMediaPlayer()->decodedFrameCount();
276 unsigned HTMLVideoElement::webkitDroppedFrameCount() const
278 if (!webMediaPlayer())
281 return webMediaPlayer()->droppedFrameCount();
284 KURL HTMLVideoElement::posterImageURL() const
286 String url = stripLeadingAndTrailingHTMLSpaces(imageSourceURL());
289 return document().completeURL(url);
292 KURL HTMLVideoElement::mediaPlayerPosterURL()
294 return posterImageURL();
297 PassRefPtr<Image> HTMLVideoElement::getSourceImageForCanvas(SourceImageMode mode, SourceImageStatus* status) const
299 if (!hasAvailableVideoFrame()) {
300 *status = InvalidSourceImageStatus;
304 IntSize intrinsicSize(videoWidth(), videoHeight());
305 OwnPtr<ImageBuffer> imageBuffer = ImageBuffer::create(intrinsicSize);
307 *status = InvalidSourceImageStatus;
311 paintCurrentFrameInContext(imageBuffer->context(), IntRect(IntPoint(0, 0), intrinsicSize));
313 *status = NormalSourceImageStatus;
314 return imageBuffer->copyImage(mode == CopySourceImageIfVolatile ? CopyBackingStore : DontCopyBackingStore, Unscaled);
317 bool HTMLVideoElement::wouldTaintOrigin(SecurityOrigin* destinationSecurityOrigin) const
319 return !hasSingleSecurityOrigin() || (!(webMediaPlayer() && webMediaPlayer()->didPassCORSAccessCheck()) && destinationSecurityOrigin->taintsCanvas(currentSrc()));
322 FloatSize HTMLVideoElement::sourceSize() const
324 return FloatSize(videoWidth(), videoHeight());