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.
29 #include "HTMLMediaElement.h"
31 #include "Attribute.h"
33 #include "ChromeClient.h"
34 #include "ClientRect.h"
35 #include "ClientRectList.h"
36 #include "ContentType.h"
37 #include "CSSPropertyNames.h"
38 #include "CSSValueKeywords.h"
40 #include "EventNames.h"
41 #include "ExceptionCode.h"
43 #include "FrameLoader.h"
44 #include "FrameLoaderClient.h"
45 #include "FrameView.h"
46 #include "HTMLDocument.h"
47 #include "HTMLNames.h"
48 #include "HTMLSourceElement.h"
49 #include "HTMLVideoElement.h"
51 #include "MediaDocument.h"
52 #include "MediaError.h"
53 #include "MediaList.h"
54 #include "MediaPlayer.h"
55 #include "MediaQueryEvaluator.h"
56 #include "MIMETypeRegistry.h"
58 #include "RenderVideo.h"
59 #include "RenderView.h"
60 #include "ScriptEventListener.h"
62 #include "TimeRanges.h"
64 #include <wtf/CurrentTime.h>
65 #include <wtf/MathExtras.h>
66 #include <wtf/text/CString.h>
68 #if USE(ACCELERATED_COMPOSITING)
69 #include "RenderView.h"
70 #include "RenderLayerCompositor.h"
73 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
74 #include "RenderEmbeddedObject.h"
83 static String urlForLogging(const String& url)
85 static const unsigned maximumURLLengthForLogging = 128;
87 if (url.length() < maximumURLLengthForLogging)
89 return url.substring(0, maximumURLLengthForLogging) + "...";
92 static const char *boolString(bool val)
94 return val ? "true" : "false";
98 #ifndef LOG_MEDIA_EVENTS
99 // Default to not logging events because so many are generated they can overwhelm the rest of
101 #define LOG_MEDIA_EVENTS 0
104 #ifndef LOG_CACHED_TIME_WARNINGS
105 // Default to not logging warnings about excessive drift in the cached media time because it adds a
106 // fair amount of overhead and logging.
107 #define LOG_CACHED_TIME_WARNINGS 0
110 static const float invalidMediaTime = -1;
112 using namespace HTMLNames;
114 HTMLMediaElement::HTMLMediaElement(const QualifiedName& tagName, Document* document)
115 : HTMLElement(tagName, document)
116 , ActiveDOMObject(document, this)
117 , m_loadTimer(this, &HTMLMediaElement::loadTimerFired)
118 , m_asyncEventTimer(this, &HTMLMediaElement::asyncEventTimerFired)
119 , m_progressEventTimer(this, &HTMLMediaElement::progressEventTimerFired)
120 , m_playbackProgressTimer(this, &HTMLMediaElement::playbackProgressTimerFired)
121 , m_playedTimeRanges()
122 , m_playbackRate(1.0f)
123 , m_defaultPlaybackRate(1.0f)
124 , m_webkitPreservesPitch(true)
125 , m_networkState(NETWORK_EMPTY)
126 , m_readyState(HAVE_NOTHING)
127 , m_readyStateMaximum(HAVE_NOTHING)
130 , m_previousProgress(0)
131 , m_previousProgressTime(numeric_limits<double>::max())
132 , m_lastTimeUpdateEventWallTime(0)
133 , m_lastTimeUpdateEventMovieTime(numeric_limits<float>::max())
134 , m_loadState(WaitingForSource)
135 , m_currentSourceNode(0)
136 , m_nextChildNodeToConsider(0)
138 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
141 , m_restrictions(NoRestrictions)
142 , m_preload(MediaPlayer::Auto)
143 , m_displayMode(Unknown)
144 , m_processingMediaPlayerCallback(0)
145 , m_cachedTime(invalidMediaTime)
146 , m_cachedTimeWallClockUpdateTime(0)
147 , m_minimumWallClockTimeToCacheMediaTime(0)
149 , m_isWaitingUntilMediaCanStart(false)
150 , m_shouldDelayLoadEvent(false)
151 , m_haveFiredLoadedData(false)
152 , m_inActiveDocument(true)
153 , m_autoplaying(true)
157 , m_sentStalledEvent(false)
158 , m_sentEndEvent(false)
159 , m_pausedInternal(false)
160 , m_sendProgressEvents(true)
161 , m_isFullscreen(false)
162 , m_closedCaptionsVisible(false)
163 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
164 , m_needWidgetUpdate(false)
166 , m_dispatchingCanPlayEvent(false)
167 , m_loadInitiatedByUserGesture(false)
168 , m_completelyLoaded(false)
170 LOG(Media, "HTMLMediaElement::HTMLMediaElement");
171 document->registerForDocumentActivationCallbacks(this);
172 document->registerForMediaVolumeCallbacks(this);
175 HTMLMediaElement::~HTMLMediaElement()
177 LOG(Media, "HTMLMediaElement::~HTMLMediaElement");
178 if (m_isWaitingUntilMediaCanStart)
179 document()->removeMediaCanStartListener(this);
180 setShouldDelayLoadEvent(false);
181 document()->unregisterForDocumentActivationCallbacks(this);
182 document()->unregisterForMediaVolumeCallbacks(this);
185 void HTMLMediaElement::willMoveToNewOwnerDocument()
187 if (m_isWaitingUntilMediaCanStart)
188 document()->removeMediaCanStartListener(this);
189 setShouldDelayLoadEvent(false);
190 document()->unregisterForDocumentActivationCallbacks(this);
191 document()->unregisterForMediaVolumeCallbacks(this);
192 HTMLElement::willMoveToNewOwnerDocument();
195 void HTMLMediaElement::didMoveToNewOwnerDocument()
197 if (m_isWaitingUntilMediaCanStart)
198 document()->addMediaCanStartListener(this);
199 if (m_readyState < HAVE_CURRENT_DATA)
200 setShouldDelayLoadEvent(true);
201 document()->registerForDocumentActivationCallbacks(this);
202 document()->registerForMediaVolumeCallbacks(this);
203 HTMLElement::didMoveToNewOwnerDocument();
206 void HTMLMediaElement::attributeChanged(Attribute* attr, bool preserveDecls)
208 HTMLElement::attributeChanged(attr, preserveDecls);
210 const QualifiedName& attrName = attr->name();
211 if (attrName == srcAttr) {
212 // Trigger a reload, as long as the 'src' attribute is present.
213 if (!getAttribute(srcAttr).isEmpty())
216 else if (attrName == controlsAttr) {
217 #if !ENABLE(PLUGIN_PROXY_FOR_VIDEO)
218 if (!isVideo() && attached() && (controls() != (renderer() != 0))) {
223 renderer()->updateFromElement();
226 m_player->setControls(controls());
231 void HTMLMediaElement::parseMappedAttribute(Attribute* attr)
233 const QualifiedName& attrName = attr->name();
235 if (attrName == preloadAttr) {
236 String value = attr->value();
238 if (equalIgnoringCase(value, "none"))
239 m_preload = MediaPlayer::None;
240 else if (equalIgnoringCase(value, "metadata"))
241 m_preload = MediaPlayer::MetaData;
243 // The spec does not define an "invalid value default" but "auto" is suggested as the
244 // "missing value default", so use it for everything except "none" and "metadata"
245 m_preload = MediaPlayer::Auto;
248 // The attribute must be ignored if the autoplay attribute is present
249 if (!autoplay() && m_player)
250 m_player->setPreload(m_preload);
252 } else if (attrName == onabortAttr)
253 setAttributeEventListener(eventNames().abortEvent, createAttributeEventListener(this, attr));
254 else if (attrName == onbeforeloadAttr)
255 setAttributeEventListener(eventNames().beforeloadEvent, createAttributeEventListener(this, attr));
256 else if (attrName == oncanplayAttr)
257 setAttributeEventListener(eventNames().canplayEvent, createAttributeEventListener(this, attr));
258 else if (attrName == oncanplaythroughAttr)
259 setAttributeEventListener(eventNames().canplaythroughEvent, createAttributeEventListener(this, attr));
260 else if (attrName == ondurationchangeAttr)
261 setAttributeEventListener(eventNames().durationchangeEvent, createAttributeEventListener(this, attr));
262 else if (attrName == onemptiedAttr)
263 setAttributeEventListener(eventNames().emptiedEvent, createAttributeEventListener(this, attr));
264 else if (attrName == onendedAttr)
265 setAttributeEventListener(eventNames().endedEvent, createAttributeEventListener(this, attr));
266 else if (attrName == onerrorAttr)
267 setAttributeEventListener(eventNames().errorEvent, createAttributeEventListener(this, attr));
268 else if (attrName == onloadeddataAttr)
269 setAttributeEventListener(eventNames().loadeddataEvent, createAttributeEventListener(this, attr));
270 else if (attrName == onloadedmetadataAttr)
271 setAttributeEventListener(eventNames().loadedmetadataEvent, createAttributeEventListener(this, attr));
272 else if (attrName == onloadstartAttr)
273 setAttributeEventListener(eventNames().loadstartEvent, createAttributeEventListener(this, attr));
274 else if (attrName == onpauseAttr)
275 setAttributeEventListener(eventNames().pauseEvent, createAttributeEventListener(this, attr));
276 else if (attrName == onplayAttr)
277 setAttributeEventListener(eventNames().playEvent, createAttributeEventListener(this, attr));
278 else if (attrName == onplayingAttr)
279 setAttributeEventListener(eventNames().playingEvent, createAttributeEventListener(this, attr));
280 else if (attrName == onprogressAttr)
281 setAttributeEventListener(eventNames().progressEvent, createAttributeEventListener(this, attr));
282 else if (attrName == onratechangeAttr)
283 setAttributeEventListener(eventNames().ratechangeEvent, createAttributeEventListener(this, attr));
284 else if (attrName == onseekedAttr)
285 setAttributeEventListener(eventNames().seekedEvent, createAttributeEventListener(this, attr));
286 else if (attrName == onseekingAttr)
287 setAttributeEventListener(eventNames().seekingEvent, createAttributeEventListener(this, attr));
288 else if (attrName == onstalledAttr)
289 setAttributeEventListener(eventNames().stalledEvent, createAttributeEventListener(this, attr));
290 else if (attrName == onsuspendAttr)
291 setAttributeEventListener(eventNames().suspendEvent, createAttributeEventListener(this, attr));
292 else if (attrName == ontimeupdateAttr)
293 setAttributeEventListener(eventNames().timeupdateEvent, createAttributeEventListener(this, attr));
294 else if (attrName == onvolumechangeAttr)
295 setAttributeEventListener(eventNames().volumechangeEvent, createAttributeEventListener(this, attr));
296 else if (attrName == onwaitingAttr)
297 setAttributeEventListener(eventNames().waitingEvent, createAttributeEventListener(this, attr));
298 else if (attrName == onwebkitbeginfullscreenAttr)
299 setAttributeEventListener(eventNames().webkitbeginfullscreenEvent, createAttributeEventListener(this, attr));
300 else if (attrName == onwebkitendfullscreenAttr)
301 setAttributeEventListener(eventNames().webkitendfullscreenEvent, createAttributeEventListener(this, attr));
303 HTMLElement::parseMappedAttribute(attr);
306 bool HTMLMediaElement::rendererIsNeeded(RenderStyle* style)
308 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
310 Frame* frame = document()->frame();
316 return controls() ? HTMLElement::rendererIsNeeded(style) : false;
320 RenderObject* HTMLMediaElement::createRenderer(RenderArena* arena, RenderStyle*)
322 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
323 // Setup the renderer if we already have a proxy widget.
324 RenderEmbeddedObject* mediaRenderer = new (arena) RenderEmbeddedObject(this);
326 mediaRenderer->setWidget(m_proxyWidget);
328 Frame* frame = document()->frame();
329 FrameLoader* loader = frame ? frame->loader() : 0;
331 loader->showMediaPlayerProxyPlugin(m_proxyWidget.get());
333 return mediaRenderer;
335 return new (arena) RenderMedia(this);
339 void HTMLMediaElement::insertedIntoDocument()
341 LOG(Media, "HTMLMediaElement::removedFromDocument");
342 HTMLElement::insertedIntoDocument();
343 if (!getAttribute(srcAttr).isEmpty() && m_networkState == NETWORK_EMPTY)
347 void HTMLMediaElement::removedFromDocument()
349 LOG(Media, "HTMLMediaElement::removedFromDocument");
350 if (m_networkState > NETWORK_EMPTY)
351 pause(processingUserGesture());
354 HTMLElement::removedFromDocument();
357 void HTMLMediaElement::attach()
361 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
362 m_needWidgetUpdate = true;
365 HTMLElement::attach();
368 renderer()->updateFromElement();
369 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
370 else if (m_proxyWidget) {
371 Frame* frame = document()->frame();
372 FrameLoader* loader = frame ? frame->loader() : 0;
374 loader->hideMediaPlayerProxyPlugin(m_proxyWidget.get());
379 void HTMLMediaElement::recalcStyle(StyleChange change)
381 HTMLElement::recalcStyle(change);
384 renderer()->updateFromElement();
387 void HTMLMediaElement::scheduleLoad()
389 LOG(Media, "HTMLMediaElement::scheduleLoad");
390 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
391 createMediaPlayerProxy();
394 if (m_loadTimer.isActive())
397 m_loadTimer.startOneShot(0);
400 void HTMLMediaElement::scheduleNextSourceChild()
402 // Schedule the timer to try the next <source> element WITHOUT resetting state ala prepareForLoad.
403 m_loadTimer.startOneShot(0);
406 void HTMLMediaElement::scheduleEvent(const AtomicString& eventName)
409 LOG(Media, "HTMLMediaElement::scheduleEvent - scheduling '%s'", eventName.string().ascii().data());
411 m_pendingEvents.append(Event::create(eventName, false, true));
412 if (!m_asyncEventTimer.isActive())
413 m_asyncEventTimer.startOneShot(0);
416 void HTMLMediaElement::asyncEventTimerFired(Timer<HTMLMediaElement>*)
418 Vector<RefPtr<Event> > pendingEvents;
419 ExceptionCode ec = 0;
421 m_pendingEvents.swap(pendingEvents);
422 unsigned count = pendingEvents.size();
423 for (unsigned ndx = 0; ndx < count; ++ndx) {
425 LOG(Media, "HTMLMediaElement::asyncEventTimerFired - dispatching '%s'", pendingEvents[ndx]->type().string().ascii().data());
427 if (pendingEvents[ndx]->type() == eventNames().canplayEvent) {
428 m_dispatchingCanPlayEvent = true;
429 dispatchEvent(pendingEvents[ndx].release(), ec);
430 m_dispatchingCanPlayEvent = false;
432 dispatchEvent(pendingEvents[ndx].release(), ec);
436 void HTMLMediaElement::loadTimerFired(Timer<HTMLMediaElement>*)
438 if (m_loadState == LoadingFromSourceElement)
439 loadNextSourceChild();
444 PassRefPtr<MediaError> HTMLMediaElement::error() const
449 void HTMLMediaElement::setSrc(const String& url)
451 setAttribute(srcAttr, url);
454 String HTMLMediaElement::currentSrc() const
459 HTMLMediaElement::NetworkState HTMLMediaElement::networkState() const
461 return m_networkState;
464 String HTMLMediaElement::canPlayType(const String& mimeType) const
466 MediaPlayer::SupportsType support = MediaPlayer::supportsType(ContentType(mimeType));
472 case MediaPlayer::IsNotSupported:
475 case MediaPlayer::MayBeSupported:
478 case MediaPlayer::IsSupported:
479 canPlay = "probably";
483 LOG(Media, "HTMLMediaElement::canPlayType(%s) -> %s", mimeType.utf8().data(), canPlay.utf8().data());
488 void HTMLMediaElement::load(bool isUserGesture, ExceptionCode& ec)
490 LOG(Media, "HTMLMediaElement::load(isUserGesture : %s)", boolString(isUserGesture));
492 if (m_restrictions & RequireUserGestureForLoadRestriction && !isUserGesture)
493 ec = INVALID_STATE_ERR;
495 m_loadInitiatedByUserGesture = isUserGesture;
501 void HTMLMediaElement::prepareForLoad()
503 LOG(Media, "HTMLMediaElement::prepareForLoad");
505 // Perform the cleanup required for the resource load algorithm to run.
506 stopPeriodicTimers();
508 m_sentStalledEvent = false;
509 m_haveFiredLoadedData = false;
510 m_completelyLoaded = false;
511 m_displayMode = Unknown;
513 // 1 - Abort any already-running instance of the resource selection algorithm for this element.
514 m_loadState = WaitingForSource;
515 m_currentSourceNode = 0;
517 // 2 - If there are any tasks from the media element's media element event task source in
518 // one of the task queues, then remove those tasks.
519 cancelPendingEventsAndCallbacks();
521 // 3 - If the media element's networkState is set to NETWORK_LOADING or NETWORK_IDLE, queue
522 // a task to fire a simple event named abort at the media element.
523 if (m_networkState == NETWORK_LOADING || m_networkState == NETWORK_IDLE)
524 scheduleEvent(eventNames().abortEvent);
526 #if !ENABLE(PLUGIN_PROXY_FOR_VIDEO)
527 m_player = MediaPlayer::create(this);
530 m_player->cancelLoad();
532 createMediaPlayerProxy();
535 // 4 - If the media element's networkState is not set to NETWORK_EMPTY, then run these substeps
536 if (m_networkState != NETWORK_EMPTY) {
537 m_networkState = NETWORK_EMPTY;
538 m_readyState = HAVE_NOTHING;
539 m_readyStateMaximum = HAVE_NOTHING;
543 scheduleEvent(eventNames().emptiedEvent);
546 // 5 - Set the playbackRate attribute to the value of the defaultPlaybackRate attribute.
547 setPlaybackRate(defaultPlaybackRate());
549 // 6 - Set the error attribute to null and the autoplaying flag to true.
551 m_autoplaying = true;
553 // 7 - Invoke the media element's resource selection algorithm.
555 // 8 - Note: Playback of any previously playing media resource for this element stops.
557 // The resource selection algorithm
558 // 1 - Set the networkState to NETWORK_NO_SOURCE
559 m_networkState = NETWORK_NO_SOURCE;
561 // 2 - Asynchronously await a stable state.
563 m_playedTimeRanges = TimeRanges::create();
565 m_closedCaptionsVisible = false;
567 // The spec doesn't say to block the load event until we actually run the asynchronous section
568 // algorithm, but do it now because we won't start that until after the timer fires and the
569 // event may have already fired by then.
570 setShouldDelayLoadEvent(true);
573 void HTMLMediaElement::loadInternal()
575 // If we can't start a load right away, start it later.
576 Page* page = document()->page();
577 if (page && !page->canStartMedia()) {
578 if (m_isWaitingUntilMediaCanStart)
580 document()->addMediaCanStartListener(this);
581 m_isWaitingUntilMediaCanStart = true;
585 selectMediaResource();
588 void HTMLMediaElement::selectMediaResource()
590 LOG(Media, "HTMLMediaElement::selectMediaResource");
592 enum Mode { attribute, children };
593 Mode mode = attribute;
595 // 3 - ... the media element has neither a src attribute ...
596 if (!hasAttribute(srcAttr)) {
597 // ... nor a source element child: ...
599 for (node = firstChild(); node; node = node->nextSibling()) {
600 if (node->hasTagName(sourceTag))
605 m_loadState = WaitingForSource;
606 setShouldDelayLoadEvent(false);
608 // ... set the networkState to NETWORK_EMPTY, and abort these steps
609 m_networkState = NETWORK_EMPTY;
611 LOG(Media, "HTMLMediaElement::selectMediaResource, nothing to load");
618 // 4 - Set the media element's delaying-the-load-event flag to true (this delays the load event),
619 // and set its networkState to NETWORK_LOADING.
620 setShouldDelayLoadEvent(true);
621 m_networkState = NETWORK_LOADING;
624 scheduleEvent(eventNames().loadstartEvent);
626 // 6 - If mode is attribute, then run these substeps
627 if (mode == attribute) {
628 // If the src attribute's value is the empty string ... jump down to the failed step below
629 KURL mediaURL = getNonEmptyURLAttribute(srcAttr);
630 if (mediaURL.isEmpty()) {
632 LOG(Media, "HTMLMediaElement::selectMediaResource, empty 'src'");
636 if (isSafeToLoadURL(mediaURL, Complain) && dispatchBeforeLoadEvent(mediaURL.string())) {
637 ContentType contentType("");
638 m_loadState = LoadingFromSrcAttr;
639 loadResource(mediaURL, contentType);
643 LOG(Media, "HTMLMediaElement::selectMediaResource, 'src' not used");
647 // Otherwise, the source elements will be used
648 m_currentSourceNode = 0;
649 loadNextSourceChild();
652 void HTMLMediaElement::loadNextSourceChild()
654 ContentType contentType("");
655 KURL mediaURL = selectNextSourceChild(&contentType, Complain);
656 if (!mediaURL.isValid()) {
657 waitForSourceChange();
661 #if !ENABLE(PLUGIN_PROXY_FOR_VIDEO)
662 // Recreate the media player for the new url
663 m_player = MediaPlayer::create(this);
666 m_loadState = LoadingFromSourceElement;
667 loadResource(mediaURL, contentType);
670 void HTMLMediaElement::loadResource(const KURL& initialURL, ContentType& contentType)
672 ASSERT(isSafeToLoadURL(initialURL, Complain));
674 LOG(Media, "HTMLMediaElement::loadResource(%s, %s)", urlForLogging(initialURL.string()).utf8().data(), contentType.raw().utf8().data());
676 Frame* frame = document()->frame();
679 FrameLoader* loader = frame->loader();
683 KURL url(initialURL);
684 if (!loader->willLoadMediaElementURL(url))
687 // The resource fetch algorithm
688 m_networkState = NETWORK_LOADING;
692 LOG(Media, "HTMLMediaElement::loadResource - m_currentSrc -> %s", urlForLogging(m_currentSrc).utf8().data());
694 if (m_sendProgressEvents)
695 startProgressEventTimer();
698 m_player->setPreload(m_preload);
699 m_player->setPreservesPitch(m_webkitPreservesPitch);
702 m_player->load(m_currentSrc, contentType);
704 // If there is no poster to display, allow the media engine to render video frames as soon as
705 // they are available.
706 updateDisplayState();
709 renderer()->updateFromElement();
712 bool HTMLMediaElement::isSafeToLoadURL(const KURL& url, InvalidSourceAction actionIfInvalid)
714 if (!url.isValid()) {
715 LOG(Media, "HTMLMediaElement::isSafeToLoadURL(%s) -> FALSE because url is invalid", urlForLogging(url.string()).utf8().data());
719 Frame* frame = document()->frame();
720 if (!frame || !document()->securityOrigin()->canDisplay(url)) {
721 if (actionIfInvalid == Complain)
722 FrameLoader::reportLocalLoadFailed(frame, url.string());
723 LOG(Media, "HTMLMediaElement::isSafeToLoadURL(%s) -> FALSE rejected by SecurityOrigin", urlForLogging(url.string()).utf8().data());
730 void HTMLMediaElement::startProgressEventTimer()
732 if (m_progressEventTimer.isActive())
735 m_previousProgressTime = WTF::currentTime();
736 m_previousProgress = 0;
737 // 350ms is not magic, it is in the spec!
738 m_progressEventTimer.startRepeating(0.350);
741 void HTMLMediaElement::waitForSourceChange()
743 LOG(Media, "HTMLMediaElement::waitForSourceChange");
745 stopPeriodicTimers();
746 m_loadState = WaitingForSource;
748 // 6.17 - Waiting: Set the element's networkState attribute to the NETWORK_NO_SOURCE value
749 m_networkState = NETWORK_NO_SOURCE;
751 // 6.18 - Set the element's delaying-the-load-event flag to false. This stops delaying the load event.
752 setShouldDelayLoadEvent(false);
755 void HTMLMediaElement::noneSupported()
757 LOG(Media, "HTMLMediaElement::noneSupported");
759 stopPeriodicTimers();
760 m_loadState = WaitingForSource;
761 m_currentSourceNode = 0;
763 // 5 - Reaching this step indicates that either the URL failed to resolve, or the media
764 // resource failed to load. Set the error attribute to a new MediaError object whose
765 // code attribute is set to MEDIA_ERR_SRC_NOT_SUPPORTED.
766 m_error = MediaError::create(MediaError::MEDIA_ERR_SRC_NOT_SUPPORTED);
768 // 6 - Set the element's networkState attribute to the NETWORK_NO_SOURCE value.
769 m_networkState = NETWORK_NO_SOURCE;
771 // 7 - Queue a task to fire a progress event called error at the media element, in
772 // the context of the fetching process that was used to try to obtain the media
773 // resource in the resource fetch algorithm.
774 scheduleEvent(eventNames().errorEvent);
776 // 8 - Set the element's delaying-the-load-event flag to false. This stops delaying the load event.
777 setShouldDelayLoadEvent(false);
779 // 9 -Abort these steps. Until the load() method is invoked, the element won't attempt to load another resource.
781 updateDisplayState();
784 renderer()->updateFromElement();
787 void HTMLMediaElement::mediaEngineError(PassRefPtr<MediaError> err)
789 LOG(Media, "HTMLMediaElement::mediaEngineError(%d)", static_cast<int>(err->code()));
791 // 1 - The user agent should cancel the fetching process.
792 stopPeriodicTimers();
793 m_loadState = WaitingForSource;
795 // 2 - Set the error attribute to a new MediaError object whose code attribute is
796 // set to MEDIA_ERR_NETWORK/MEDIA_ERR_DECODE.
799 // 3 - Queue a task to fire a simple event named error at the media element.
800 scheduleEvent(eventNames().errorEvent);
802 // 4 - Set the element's networkState attribute to the NETWORK_EMPTY value and queue a
803 // task to fire a simple event called emptied at the element.
804 m_networkState = NETWORK_EMPTY;
805 scheduleEvent(eventNames().emptiedEvent);
807 // 5 - Set the element's delaying-the-load-event flag to false. This stops delaying the load event.
808 setShouldDelayLoadEvent(false);
810 // 6 - Abort the overall resource selection algorithm.
811 m_currentSourceNode = 0;
814 void HTMLMediaElement::cancelPendingEventsAndCallbacks()
816 LOG(Media, "HTMLMediaElement::cancelPendingEventsAndCallbacks");
818 m_pendingEvents.clear();
820 for (Node* node = firstChild(); node; node = node->nextSibling()) {
821 if (node->hasTagName(sourceTag))
822 static_cast<HTMLSourceElement*>(node)->cancelPendingErrorEvent();
826 Document* HTMLMediaElement::mediaPlayerOwningDocument()
828 Document* d = document();
836 void HTMLMediaElement::mediaPlayerNetworkStateChanged(MediaPlayer*)
838 beginProcessingMediaPlayerCallback();
839 setNetworkState(m_player->networkState());
840 endProcessingMediaPlayerCallback();
843 void HTMLMediaElement::setNetworkState(MediaPlayer::NetworkState state)
845 LOG(Media, "HTMLMediaElement::setNetworkState(%d) - current state is %d", static_cast<int>(state), static_cast<int>(m_networkState));
847 if (state == MediaPlayer::Empty) {
848 // Just update the cached state and leave, we can't do anything.
849 m_networkState = NETWORK_EMPTY;
853 if (state == MediaPlayer::FormatError || state == MediaPlayer::NetworkError || state == MediaPlayer::DecodeError) {
854 stopPeriodicTimers();
856 // If we failed while trying to load a <source> element, the movie was never parsed, and there are more
857 // <source> children, schedule the next one
858 if (m_readyState < HAVE_METADATA && m_loadState == LoadingFromSourceElement) {
860 if (m_currentSourceNode)
861 m_currentSourceNode->scheduleErrorEvent();
863 LOG(Media, "HTMLMediaElement::setNetworkState - error event not sent, <source> was removed");
865 if (havePotentialSourceChild()) {
866 LOG(Media, "HTMLMediaElement::setNetworkState - scheduling next <source>");
867 scheduleNextSourceChild();
869 LOG(Media, "HTMLMediaElement::setNetworkState - no more <source> elements, waiting");
870 waitForSourceChange();
876 if (state == MediaPlayer::NetworkError)
877 mediaEngineError(MediaError::create(MediaError::MEDIA_ERR_NETWORK));
878 else if (state == MediaPlayer::DecodeError)
879 mediaEngineError(MediaError::create(MediaError::MEDIA_ERR_DECODE));
880 else if (state == MediaPlayer::FormatError && m_loadState == LoadingFromSrcAttr)
883 updateDisplayState();
887 if (state == MediaPlayer::Idle) {
888 if (m_networkState > NETWORK_IDLE) {
889 m_progressEventTimer.stop();
890 scheduleEvent(eventNames().suspendEvent);
892 m_networkState = NETWORK_IDLE;
895 if (state == MediaPlayer::Loading) {
896 if (m_networkState < NETWORK_LOADING || m_networkState == NETWORK_NO_SOURCE)
897 startProgressEventTimer();
898 m_networkState = NETWORK_LOADING;
901 if (state == MediaPlayer::Loaded) {
902 if (m_networkState != NETWORK_IDLE) {
903 m_progressEventTimer.stop();
905 // Schedule one last progress event so we guarantee that at least one is fired
906 // for files that load very quickly.
907 scheduleEvent(eventNames().progressEvent);
909 m_networkState = NETWORK_IDLE;
910 m_completelyLoaded = true;
914 void HTMLMediaElement::mediaPlayerReadyStateChanged(MediaPlayer*)
916 beginProcessingMediaPlayerCallback();
918 setReadyState(m_player->readyState());
920 endProcessingMediaPlayerCallback();
923 void HTMLMediaElement::setReadyState(MediaPlayer::ReadyState state)
925 LOG(Media, "HTMLMediaElement::setReadyState(%d) - current state is %d,", static_cast<int>(state), static_cast<int>(m_readyState));
927 // Set "wasPotentiallyPlaying" BEFORE updating m_readyState, potentiallyPlaying() uses it
928 bool wasPotentiallyPlaying = potentiallyPlaying();
930 ReadyState oldState = m_readyState;
931 m_readyState = static_cast<ReadyState>(state);
933 if (m_readyState == oldState)
936 if (oldState > m_readyStateMaximum)
937 m_readyStateMaximum = oldState;
939 if (m_networkState == NETWORK_EMPTY)
944 if (wasPotentiallyPlaying && m_readyState < HAVE_FUTURE_DATA)
945 scheduleEvent(eventNames().waitingEvent);
947 // 4.8.10.10 step 14 & 15.
948 if (m_readyState >= HAVE_CURRENT_DATA)
951 if (wasPotentiallyPlaying && m_readyState < HAVE_FUTURE_DATA) {
953 scheduleTimeupdateEvent(false);
954 scheduleEvent(eventNames().waitingEvent);
958 if (m_readyState >= HAVE_METADATA && oldState < HAVE_METADATA) {
959 scheduleEvent(eventNames().durationchangeEvent);
960 scheduleEvent(eventNames().loadedmetadataEvent);
962 renderer()->updateFromElement();
966 bool shouldUpdateDisplayState = false;
968 if (m_readyState >= HAVE_CURRENT_DATA && oldState < HAVE_CURRENT_DATA && !m_haveFiredLoadedData) {
969 m_haveFiredLoadedData = true;
970 shouldUpdateDisplayState = true;
971 scheduleEvent(eventNames().loadeddataEvent);
972 setShouldDelayLoadEvent(false);
975 bool isPotentiallyPlaying = potentiallyPlaying();
976 if (m_readyState == HAVE_FUTURE_DATA && oldState <= HAVE_CURRENT_DATA) {
977 scheduleEvent(eventNames().canplayEvent);
978 if (isPotentiallyPlaying)
979 scheduleEvent(eventNames().playingEvent);
980 shouldUpdateDisplayState = true;
983 if (m_readyState == HAVE_ENOUGH_DATA && oldState < HAVE_ENOUGH_DATA) {
984 if (oldState <= HAVE_CURRENT_DATA)
985 scheduleEvent(eventNames().canplayEvent);
987 scheduleEvent(eventNames().canplaythroughEvent);
989 if (isPotentiallyPlaying && oldState <= HAVE_CURRENT_DATA)
990 scheduleEvent(eventNames().playingEvent);
992 if (m_autoplaying && m_paused && autoplay()) {
994 scheduleEvent(eventNames().playEvent);
995 scheduleEvent(eventNames().playingEvent);
998 shouldUpdateDisplayState = true;
1001 if (shouldUpdateDisplayState)
1002 updateDisplayState();
1007 void HTMLMediaElement::progressEventTimerFired(Timer<HTMLMediaElement>*)
1010 if (m_networkState != NETWORK_LOADING)
1013 unsigned progress = m_player->bytesLoaded();
1014 double time = WTF::currentTime();
1015 double timedelta = time - m_previousProgressTime;
1017 if (progress == m_previousProgress) {
1018 if (timedelta > 3.0 && !m_sentStalledEvent) {
1019 scheduleEvent(eventNames().stalledEvent);
1020 m_sentStalledEvent = true;
1023 scheduleEvent(eventNames().progressEvent);
1024 m_previousProgress = progress;
1025 m_previousProgressTime = time;
1026 m_sentStalledEvent = false;
1028 renderer()->updateFromElement();
1032 void HTMLMediaElement::rewind(float timeDelta)
1034 LOG(Media, "HTMLMediaElement::rewind(%f)", timeDelta);
1037 setCurrentTime(max(currentTime() - timeDelta, minTimeSeekable()), e);
1040 void HTMLMediaElement::returnToRealtime()
1042 LOG(Media, "HTMLMediaElement::returnToRealtime");
1044 setCurrentTime(maxTimeSeekable(), e);
1047 void HTMLMediaElement::addPlayedRange(float start, float end)
1049 LOG(Media, "HTMLMediaElement::addPlayedRange(%f, %f)", start, end);
1050 if (!m_playedTimeRanges)
1051 m_playedTimeRanges = TimeRanges::create();
1052 m_playedTimeRanges->add(start, end);
1055 bool HTMLMediaElement::supportsSave() const
1057 return m_player ? m_player->supportsSave() : false;
1060 void HTMLMediaElement::seek(float time, ExceptionCode& ec)
1062 LOG(Media, "HTMLMediaElement::seek(%f)", time);
1066 // 1 - If the media element's readyState is HAVE_NOTHING, then raise an INVALID_STATE_ERR exception.
1067 if (m_readyState == HAVE_NOTHING || !m_player) {
1068 ec = INVALID_STATE_ERR;
1072 // Get the current time before setting m_seeking, m_lastSeekTime is returned once it is set.
1073 refreshCachedTime();
1074 float now = currentTime();
1076 // 2 - If the element's seeking IDL attribute is true, then another instance of this algorithm is
1077 // already running. Abort that other instance of the algorithm without waiting for the step that
1078 // it is running to complete.
1079 // Nothing specific to be done here.
1081 // 3 - Set the seeking IDL attribute to true.
1082 // The flag will be cleared when the engine tells us the time has actually changed.
1085 // 5 - If the new playback position is later than the end of the media resource, then let it be the end
1086 // of the media resource instead.
1087 time = min(time, duration());
1089 // 6 - If the new playback position is less than the earliest possible position, let it be that position instead.
1090 float earliestTime = m_player->startTime();
1091 time = max(time, earliestTime);
1093 // Ask the media engine for the time value in the movie's time scale before comparing with current time. This
1094 // is necessary because if the seek time is not equal to currentTime but the delta is less than the movie's
1095 // time scale, we will ask the media engine to "seek" to the current movie time, which may be a noop and
1096 // not generate a timechanged callback. This means m_seeking will never be cleared and we will never
1097 // fire a 'seeked' event.
1099 float mediaTime = m_player->mediaTimeForTimeValue(time);
1100 if (time != mediaTime)
1101 LOG(Media, "HTMLMediaElement::seek(%f) - media timeline equivalent is %f", time, mediaTime);
1103 time = m_player->mediaTimeForTimeValue(time);
1105 // 7 - If the (possibly now changed) new playback position is not in one of the ranges given in the
1106 // seekable attribute, then let it be the position in one of the ranges given in the seekable attribute
1107 // that is the nearest to the new playback position. ... If there are no ranges given in the seekable
1108 // attribute then set the seeking IDL attribute to false and abort these steps.
1109 RefPtr<TimeRanges> seekableRanges = seekable();
1111 // Short circuit seeking to the current time by just firing the events if no seek is required.
1112 // Don't skip calling the media engine if we are in poster mode because a seek should always
1113 // cancel poster display.
1114 bool noSeekRequired = !seekableRanges->length() || (time == now && displayMode() != Poster);
1115 if (noSeekRequired) {
1117 scheduleEvent(eventNames().seekingEvent);
1118 scheduleTimeupdateEvent(false);
1119 scheduleEvent(eventNames().seekedEvent);
1124 time = seekableRanges->nearest(time);
1127 if (m_lastSeekTime < now)
1128 addPlayedRange(m_lastSeekTime, now);
1130 m_lastSeekTime = time;
1131 m_sentEndEvent = false;
1133 // 8 - Set the current playback position to the given new playback position
1134 m_player->seek(time);
1136 // 9 - Queue a task to fire a simple event named seeking at the element.
1137 scheduleEvent(eventNames().seekingEvent);
1139 // 10 - Queue a task to fire a simple event named timeupdate at the element.
1140 scheduleTimeupdateEvent(false);
1142 // 11-15 are handled, if necessary, when the engine signals a readystate change.
1145 void HTMLMediaElement::finishSeek()
1147 LOG(Media, "HTMLMediaElement::finishSeek");
1149 // 4.8.10.9 Seeking step 14
1152 // 4.8.10.9 Seeking step 15
1153 scheduleEvent(eventNames().seekedEvent);
1155 setDisplayMode(Video);
1158 HTMLMediaElement::ReadyState HTMLMediaElement::readyState() const
1160 return m_readyState;
1163 MediaPlayer::MovieLoadType HTMLMediaElement::movieLoadType() const
1165 return m_player ? m_player->movieLoadType() : MediaPlayer::Unknown;
1168 bool HTMLMediaElement::hasAudio() const
1170 return m_player ? m_player->hasAudio() : false;
1173 bool HTMLMediaElement::seeking() const
1178 void HTMLMediaElement::refreshCachedTime() const
1180 m_cachedTime = m_player->currentTime();
1181 m_cachedTimeWallClockUpdateTime = WTF::currentTime();
1184 void HTMLMediaElement::invalidateCachedTime()
1186 LOG(Media, "HTMLMediaElement::invalidateCachedTime");
1188 // Don't try to cache movie time when playback first starts as the time reported by the engine
1189 // sometimes fluctuates for a short amount of time, so the cached time will be off if we take it
1191 static const double minimumTimePlayingBeforeCacheSnapshot = 0.5;
1193 m_minimumWallClockTimeToCacheMediaTime = WTF::currentTime() + minimumTimePlayingBeforeCacheSnapshot;
1194 m_cachedTime = invalidMediaTime;
1198 float HTMLMediaElement::currentTime() const
1200 #if LOG_CACHED_TIME_WARNINGS
1201 static const double minCachedDeltaForWarning = 0.01;
1208 LOG(Media, "HTMLMediaElement::currentTime - seeking, returning %f", m_lastSeekTime);
1209 return m_lastSeekTime;
1212 if (m_cachedTime != invalidMediaTime && m_paused) {
1213 #if LOG_CACHED_TIME_WARNINGS
1214 float delta = m_cachedTime - m_player->currentTime();
1215 if (delta > minCachedDeltaForWarning)
1216 LOG(Media, "HTMLMediaElement::currentTime - WARNING, cached time is %f seconds off of media time when paused", delta);
1218 return m_cachedTime;
1221 // Is it too soon use a cached time?
1222 double now = WTF::currentTime();
1223 double maximumDurationToCacheMediaTime = m_player->maximumDurationToCacheMediaTime();
1225 if (maximumDurationToCacheMediaTime && m_cachedTime != invalidMediaTime && !m_paused && now > m_minimumWallClockTimeToCacheMediaTime) {
1226 double wallClockDelta = now - m_cachedTimeWallClockUpdateTime;
1228 // Not too soon, use the cached time only if it hasn't expired.
1229 if (wallClockDelta < maximumDurationToCacheMediaTime) {
1230 float adjustedCacheTime = static_cast<float>(m_cachedTime + (m_playbackRate * wallClockDelta));
1232 #if LOG_CACHED_TIME_WARNINGS
1233 float delta = adjustedCacheTime - m_player->currentTime();
1234 if (delta > minCachedDeltaForWarning)
1235 LOG(Media, "HTMLMediaElement::currentTime - WARNING, cached time is %f seconds off of media time when playing", delta);
1237 return adjustedCacheTime;
1241 #if LOG_CACHED_TIME_WARNINGS
1242 if (maximumDurationToCacheMediaTime && now > m_minimumWallClockTimeToCacheMediaTime && m_cachedTime != invalidMediaTime) {
1243 double wallClockDelta = now - m_cachedTimeWallClockUpdateTime;
1244 float delta = m_cachedTime + (m_playbackRate * wallClockDelta) - m_player->currentTime();
1245 LOG(Media, "HTMLMediaElement::currentTime - cached time was %f seconds off of media time when it expired", delta);
1249 refreshCachedTime();
1251 return m_cachedTime;
1254 void HTMLMediaElement::setCurrentTime(float time, ExceptionCode& ec)
1259 float HTMLMediaElement::startTime() const
1263 return m_player->startTime();
1266 float HTMLMediaElement::duration() const
1268 if (m_player && m_readyState >= HAVE_METADATA)
1269 return m_player->duration();
1271 return numeric_limits<float>::quiet_NaN();
1274 bool HTMLMediaElement::paused() const
1279 float HTMLMediaElement::defaultPlaybackRate() const
1281 return m_defaultPlaybackRate;
1284 void HTMLMediaElement::setDefaultPlaybackRate(float rate)
1286 if (m_defaultPlaybackRate != rate) {
1287 m_defaultPlaybackRate = rate;
1288 scheduleEvent(eventNames().ratechangeEvent);
1292 float HTMLMediaElement::playbackRate() const
1294 return m_player ? m_player->rate() : 0;
1297 void HTMLMediaElement::setPlaybackRate(float rate)
1299 LOG(Media, "HTMLMediaElement::setPlaybackRate(%f)", rate);
1301 if (m_playbackRate != rate) {
1302 m_playbackRate = rate;
1303 scheduleEvent(eventNames().ratechangeEvent);
1305 if (m_player && potentiallyPlaying() && m_player->rate() != rate)
1306 m_player->setRate(rate);
1309 bool HTMLMediaElement::webkitPreservesPitch() const
1311 return m_webkitPreservesPitch;
1314 void HTMLMediaElement::setWebkitPreservesPitch(bool preservesPitch)
1316 LOG(Media, "HTMLMediaElement::setWebkitPreservesPitch(%s)", boolString(preservesPitch));
1318 m_webkitPreservesPitch = preservesPitch;
1323 m_player->setPreservesPitch(preservesPitch);
1326 bool HTMLMediaElement::ended() const
1328 // 4.8.10.8 Playing the media resource
1329 // The ended attribute must return true if the media element has ended
1330 // playback and the direction of playback is forwards, and false otherwise.
1331 return endedPlayback() && m_playbackRate > 0;
1334 bool HTMLMediaElement::autoplay() const
1336 return hasAttribute(autoplayAttr);
1339 void HTMLMediaElement::setAutoplay(bool b)
1341 LOG(Media, "HTMLMediaElement::setAutoplay(%s)", boolString(b));
1342 setBooleanAttribute(autoplayAttr, b);
1345 String HTMLMediaElement::preload() const
1347 switch (m_preload) {
1348 case MediaPlayer::None:
1351 case MediaPlayer::MetaData:
1354 case MediaPlayer::Auto:
1359 ASSERT_NOT_REACHED();
1363 void HTMLMediaElement::setPreload(const String& preload)
1365 LOG(Media, "HTMLMediaElement::setPreload(%s)", preload.utf8().data());
1366 setAttribute(preloadAttr, preload);
1369 void HTMLMediaElement::play(bool isUserGesture)
1371 LOG(Media, "HTMLMediaElement::play(isUserGesture : %s)", boolString(isUserGesture));
1373 if (m_restrictions & RequireUserGestureForRateChangeRestriction && !isUserGesture)
1376 Document* doc = document();
1377 Settings* settings = doc->settings();
1378 if (settings && settings->needsSiteSpecificQuirks() && m_dispatchingCanPlayEvent && !m_loadInitiatedByUserGesture) {
1379 // It should be impossible to be processing the canplay event while handling a user gesture
1380 // since it is dispatched asynchronously.
1381 ASSERT(!isUserGesture);
1382 String host = doc->baseURL().host();
1383 if (host.endsWith(".npr.org", false) || equalIgnoringCase(host, "npr.org"))
1390 void HTMLMediaElement::playInternal()
1392 LOG(Media, "HTMLMediaElement::playInternal");
1394 // 4.8.10.9. Playing the media resource
1395 if (!m_player || m_networkState == NETWORK_EMPTY)
1398 if (endedPlayback()) {
1399 ExceptionCode unused;
1403 setPlaybackRate(defaultPlaybackRate());
1407 scheduleEvent(eventNames().playEvent);
1409 if (m_readyState <= HAVE_CURRENT_DATA)
1410 scheduleEvent(eventNames().waitingEvent);
1411 else if (m_readyState >= HAVE_FUTURE_DATA)
1412 scheduleEvent(eventNames().playingEvent);
1414 m_autoplaying = false;
1419 void HTMLMediaElement::pause(bool isUserGesture)
1421 LOG(Media, "HTMLMediaElement::pause(isUserGesture : %s)", boolString(isUserGesture));
1423 if (m_restrictions & RequireUserGestureForRateChangeRestriction && !isUserGesture)
1430 void HTMLMediaElement::pauseInternal()
1432 LOG(Media, "HTMLMediaElement::pauseInternal");
1434 // 4.8.10.9. Playing the media resource
1435 if (!m_player || m_networkState == NETWORK_EMPTY)
1438 m_autoplaying = false;
1442 scheduleTimeupdateEvent(false);
1443 scheduleEvent(eventNames().pauseEvent);
1449 bool HTMLMediaElement::loop() const
1451 return hasAttribute(loopAttr);
1454 void HTMLMediaElement::setLoop(bool b)
1456 LOG(Media, "HTMLMediaElement::setLoop(%s)", boolString(b));
1457 setBooleanAttribute(loopAttr, b);
1460 bool HTMLMediaElement::controls() const
1462 Frame* frame = document()->frame();
1464 // always show controls when scripting is disabled
1465 if (frame && !frame->script()->canExecuteScripts(NotAboutToExecuteScript))
1468 return hasAttribute(controlsAttr);
1471 void HTMLMediaElement::setControls(bool b)
1473 LOG(Media, "HTMLMediaElement::setControls(%s)", boolString(b));
1474 setBooleanAttribute(controlsAttr, b);
1477 float HTMLMediaElement::volume() const
1482 void HTMLMediaElement::setVolume(float vol, ExceptionCode& ec)
1484 LOG(Media, "HTMLMediaElement::setVolume(%f)", vol);
1486 if (vol < 0.0f || vol > 1.0f) {
1487 ec = INDEX_SIZE_ERR;
1491 if (m_volume != vol) {
1494 scheduleEvent(eventNames().volumechangeEvent);
1498 bool HTMLMediaElement::muted() const
1503 void HTMLMediaElement::setMuted(bool muted)
1505 LOG(Media, "HTMLMediaElement::setMuted(%s)", boolString(muted));
1507 if (m_muted != muted) {
1509 // Avoid recursion when the player reports volume changes.
1510 if (!processingMediaPlayerCallback()) {
1512 m_player->setMuted(m_muted);
1514 renderer()->updateFromElement();
1518 scheduleEvent(eventNames().volumechangeEvent);
1522 void HTMLMediaElement::togglePlayState()
1524 LOG(Media, "HTMLMediaElement::togglePlayState - canPlay() is %s", boolString(canPlay()));
1526 // We can safely call the internal play/pause methods, which don't check restrictions, because
1527 // this method is only called from the built-in media controller
1534 void HTMLMediaElement::beginScrubbing()
1536 LOG(Media, "HTMLMediaElement::beginScrubbing - paused() is %s", boolString(paused()));
1540 // Because a media element stays in non-paused state when it reaches end, playback resumes
1541 // when the slider is dragged from the end to another position unless we pause first. Do
1542 // a "hard pause" so an event is generated, since we want to stay paused after scrubbing finishes.
1543 pause(processingUserGesture());
1545 // Not at the end but we still want to pause playback so the media engine doesn't try to
1546 // continue playing during scrubbing. Pause without generating an event as we will
1547 // unpause after scrubbing finishes.
1548 setPausedInternal(true);
1553 void HTMLMediaElement::endScrubbing()
1555 LOG(Media, "HTMLMediaElement::endScrubbing - m_pausedInternal is %s", boolString(m_pausedInternal));
1557 if (m_pausedInternal)
1558 setPausedInternal(false);
1561 // The spec says to fire periodic timeupdate events (those sent while playing) every
1562 // "15 to 250ms", we choose the slowest frequency
1563 static const double maxTimeupdateEventFrequency = 0.25;
1565 void HTMLMediaElement::startPlaybackProgressTimer()
1567 if (m_playbackProgressTimer.isActive())
1570 m_previousProgressTime = WTF::currentTime();
1571 m_previousProgress = 0;
1572 m_playbackProgressTimer.startRepeating(maxTimeupdateEventFrequency);
1575 void HTMLMediaElement::playbackProgressTimerFired(Timer<HTMLMediaElement>*)
1578 if (!m_playbackRate)
1581 scheduleTimeupdateEvent(true);
1583 // FIXME: deal with cue ranges here
1586 void HTMLMediaElement::scheduleTimeupdateEvent(bool periodicEvent)
1588 double now = WTF::currentTime();
1589 double timedelta = now - m_lastTimeUpdateEventWallTime;
1591 // throttle the periodic events
1592 if (periodicEvent && timedelta < maxTimeupdateEventFrequency)
1595 // Some media engines make multiple "time changed" callbacks at the same time, but we only want one
1596 // event at a given time so filter here
1597 float movieTime = currentTime();
1598 if (movieTime != m_lastTimeUpdateEventMovieTime) {
1599 scheduleEvent(eventNames().timeupdateEvent);
1600 m_lastTimeUpdateEventWallTime = now;
1601 m_lastTimeUpdateEventMovieTime = movieTime;
1605 bool HTMLMediaElement::canPlay() const
1607 return paused() || ended() || m_readyState < HAVE_METADATA;
1610 float HTMLMediaElement::percentLoaded() const
1614 float duration = m_player->duration();
1616 if (!duration || isinf(duration))
1620 RefPtr<TimeRanges> timeRanges = m_player->buffered();
1621 for (unsigned i = 0; i < timeRanges->length(); ++i) {
1622 ExceptionCode ignoredException;
1623 float start = timeRanges->start(i, ignoredException);
1624 float end = timeRanges->end(i, ignoredException);
1625 buffered += end - start;
1627 return buffered / duration;
1630 bool HTMLMediaElement::havePotentialSourceChild()
1632 // Stash the current <source> node and next nodes so we can restore them after checking
1633 // to see there is another potential.
1634 HTMLSourceElement* currentSourceNode = m_currentSourceNode;
1635 Node* nextNode = m_nextChildNodeToConsider;
1637 KURL nextURL = selectNextSourceChild(0, DoNothing);
1639 m_currentSourceNode = currentSourceNode;
1640 m_nextChildNodeToConsider = nextNode;
1642 return nextURL.isValid();
1645 KURL HTMLMediaElement::selectNextSourceChild(ContentType *contentType, InvalidSourceAction actionIfInvalid)
1648 // Don't log if this was just called to find out if there are any valid <source> elements.
1649 bool shouldLog = actionIfInvalid != DoNothing;
1651 LOG(Media, "HTMLMediaElement::selectNextSourceChild(contentType : \"%s\")", contentType ? contentType->raw().utf8().data() : "");
1654 if (m_nextChildNodeToConsider == sourceChildEndOfListValue()) {
1657 LOG(Media, "HTMLMediaElement::selectNextSourceChild -> 0x0000, \"\"");
1664 HTMLSourceElement* source = 0;
1665 bool lookingForStartNode = m_nextChildNodeToConsider;
1666 bool canUse = false;
1668 for (node = firstChild(); !canUse && node; node = node->nextSibling()) {
1669 if (lookingForStartNode && m_nextChildNodeToConsider != node)
1671 lookingForStartNode = false;
1673 if (!node->hasTagName(sourceTag))
1676 source = static_cast<HTMLSourceElement*>(node);
1678 // If candidate does not have a src attribute, or if its src attribute's value is the empty string ... jump down to the failed step below
1679 mediaURL = source->getNonEmptyURLAttribute(srcAttr);
1682 LOG(Media, "HTMLMediaElement::selectNextSourceChild - 'src' is %s", urlForLogging(mediaURL).utf8().data());
1684 if (mediaURL.isEmpty())
1687 if (source->hasAttribute(mediaAttr)) {
1688 MediaQueryEvaluator screenEval("screen", document()->frame(), renderer() ? renderer()->style() : 0);
1689 RefPtr<MediaList> media = MediaList::createAllowingDescriptionSyntax(source->media());
1692 LOG(Media, "HTMLMediaElement::selectNextSourceChild - 'media' is %s", source->media().utf8().data());
1694 if (!screenEval.eval(media.get()))
1698 if (source->hasAttribute(typeAttr)) {
1701 LOG(Media, "HTMLMediaElement::selectNextSourceChild - 'type' is %s", source->type().utf8().data());
1703 if (!MediaPlayer::supportsType(ContentType(source->type())))
1707 // Is it safe to load this url?
1708 if (!isSafeToLoadURL(mediaURL, actionIfInvalid) || !dispatchBeforeLoadEvent(mediaURL.string()))
1711 // Making it this far means the <source> looks reasonable.
1715 if (!canUse && actionIfInvalid == Complain)
1716 source->scheduleErrorEvent();
1721 *contentType = ContentType(source->type());
1722 m_currentSourceNode = source;
1723 m_nextChildNodeToConsider = source->nextSibling();
1724 if (!m_nextChildNodeToConsider)
1725 m_nextChildNodeToConsider = sourceChildEndOfListValue();
1727 m_currentSourceNode = 0;
1728 m_nextChildNodeToConsider = sourceChildEndOfListValue();
1733 LOG(Media, "HTMLMediaElement::selectNextSourceChild -> %p, %s", m_currentSourceNode, canUse ? urlForLogging(mediaURL.string()).utf8().data() : "");
1735 return canUse ? mediaURL : KURL();
1738 void HTMLMediaElement::sourceWasAdded(HTMLSourceElement* source)
1740 LOG(Media, "HTMLMediaElement::sourceWasAdded(%p)", source);
1743 if (source->hasTagName(sourceTag)) {
1744 KURL url = source->getNonEmptyURLAttribute(srcAttr);
1745 LOG(Media, "HTMLMediaElement::sourceWasAdded - 'src' is %s", urlForLogging(url).utf8().data());
1749 // We should only consider a <source> element when there is not src attribute at all.
1750 if (hasAttribute(srcAttr))
1753 // 4.8.8 - If a source element is inserted as a child of a media element that has no src
1754 // attribute and whose networkState has the value NETWORK_EMPTY, the user agent must invoke
1755 // the media element's resource selection algorithm.
1756 if (networkState() == HTMLMediaElement::NETWORK_EMPTY) {
1761 if (m_currentSourceNode && source == m_currentSourceNode->nextSibling()) {
1762 LOG(Media, "HTMLMediaElement::sourceWasAdded - <source> inserted immediately after current source");
1763 m_nextChildNodeToConsider = source;
1767 if (m_nextChildNodeToConsider != sourceChildEndOfListValue())
1770 // 4.8.9.5, resource selection algorithm, source elements section:
1771 // 20 - Wait until the node after pointer is a node other than the end of the list. (This step might wait forever.)
1772 // 21 - Asynchronously await a stable state...
1773 // 22 - Set the element's delaying-the-load-event flag back to true (this delays the load event again, in case
1774 // it hasn't been fired yet).
1775 setShouldDelayLoadEvent(true);
1777 // 23 - Set the networkState back to NETWORK_LOADING.
1778 m_networkState = NETWORK_LOADING;
1780 // 24 - Jump back to the find next candidate step above.
1781 m_nextChildNodeToConsider = source;
1782 scheduleNextSourceChild();
1785 void HTMLMediaElement::sourceWillBeRemoved(HTMLSourceElement* source)
1787 LOG(Media, "HTMLMediaElement::sourceWillBeRemoved(%p)", source);
1790 if (source->hasTagName(sourceTag)) {
1791 KURL url = source->getNonEmptyURLAttribute(srcAttr);
1792 LOG(Media, "HTMLMediaElement::sourceWillBeRemoved - 'src' is %s", urlForLogging(url).utf8().data());
1796 if (source != m_currentSourceNode && source != m_nextChildNodeToConsider)
1799 if (source == m_nextChildNodeToConsider) {
1800 m_nextChildNodeToConsider = m_nextChildNodeToConsider->nextSibling();
1801 if (!m_nextChildNodeToConsider)
1802 m_nextChildNodeToConsider = sourceChildEndOfListValue();
1803 LOG(Media, "HTMLMediaElement::sourceRemoved - m_nextChildNodeToConsider set to %p", m_nextChildNodeToConsider);
1804 } else if (source == m_currentSourceNode) {
1805 // Clear the current source node pointer, but don't change the movie as the spec says:
1806 // 4.8.8 - Dynamically modifying a source element and its attribute when the element is already
1807 // inserted in a video or audio element will have no effect.
1808 m_currentSourceNode = 0;
1809 LOG(Media, "HTMLMediaElement::sourceRemoved - m_currentSourceNode set to 0");
1813 void HTMLMediaElement::mediaPlayerTimeChanged(MediaPlayer*)
1815 LOG(Media, "HTMLMediaElement::mediaPlayerTimeChanged");
1817 beginProcessingMediaPlayerCallback();
1819 invalidateCachedTime();
1821 // 4.8.10.9 step 14 & 15. Needed if no ReadyState change is associated with the seek.
1822 if (m_seeking && m_readyState >= HAVE_CURRENT_DATA)
1825 // Always call scheduleTimeupdateEvent when the media engine reports a time discontinuity,
1826 // it will only queue a 'timeupdate' event if we haven't already posted one at the current
1828 scheduleTimeupdateEvent(false);
1830 float now = currentTime();
1831 float dur = duration();
1832 if (!isnan(dur) && dur && now >= dur) {
1834 ExceptionCode ignoredException;
1835 m_sentEndEvent = false;
1836 seek(0, ignoredException);
1838 if (!m_sentEndEvent) {
1839 m_sentEndEvent = true;
1840 scheduleEvent(eventNames().endedEvent);
1845 m_sentEndEvent = false;
1848 endProcessingMediaPlayerCallback();
1851 void HTMLMediaElement::mediaPlayerVolumeChanged(MediaPlayer*)
1853 LOG(Media, "HTMLMediaElement::mediaPlayerVolumeChanged");
1855 beginProcessingMediaPlayerCallback();
1857 m_volume = m_player->volume();
1859 endProcessingMediaPlayerCallback();
1862 void HTMLMediaElement::mediaPlayerMuteChanged(MediaPlayer*)
1864 LOG(Media, "HTMLMediaElement::mediaPlayerMuteChanged");
1866 beginProcessingMediaPlayerCallback();
1868 setMuted(m_player->muted());
1869 endProcessingMediaPlayerCallback();
1872 void HTMLMediaElement::mediaPlayerDurationChanged(MediaPlayer*)
1874 LOG(Media, "HTMLMediaElement::mediaPlayerDurationChanged");
1876 beginProcessingMediaPlayerCallback();
1877 scheduleEvent(eventNames().durationchangeEvent);
1879 renderer()->updateFromElement();
1880 endProcessingMediaPlayerCallback();
1883 void HTMLMediaElement::mediaPlayerRateChanged(MediaPlayer*)
1885 LOG(Media, "HTMLMediaElement::mediaPlayerRateChanged");
1887 beginProcessingMediaPlayerCallback();
1889 invalidateCachedTime();
1891 // Stash the rate in case the one we tried to set isn't what the engine is
1892 // using (eg. it can't handle the rate we set)
1893 m_playbackRate = m_player->rate();
1894 endProcessingMediaPlayerCallback();
1897 void HTMLMediaElement::mediaPlayerPlaybackStateChanged(MediaPlayer*)
1899 LOG(Media, "HTMLMediaElement::mediaPlayerPlaybackStateChanged");
1904 beginProcessingMediaPlayerCallback();
1905 if (m_player->paused())
1909 endProcessingMediaPlayerCallback();
1912 void HTMLMediaElement::mediaPlayerSawUnsupportedTracks(MediaPlayer*)
1914 LOG(Media, "HTMLMediaElement::mediaPlayerSawUnsupportedTracks");
1916 // The MediaPlayer came across content it cannot completely handle.
1917 // This is normally acceptable except when we are in a standalone
1918 // MediaDocument. If so, tell the document what has happened.
1919 if (ownerDocument()->isMediaDocument()) {
1920 MediaDocument* mediaDocument = static_cast<MediaDocument*>(ownerDocument());
1921 mediaDocument->mediaElementSawUnsupportedTracks();
1925 // MediaPlayerPresentation methods
1926 void HTMLMediaElement::mediaPlayerRepaint(MediaPlayer*)
1928 beginProcessingMediaPlayerCallback();
1929 updateDisplayState();
1931 renderer()->repaint();
1932 endProcessingMediaPlayerCallback();
1935 void HTMLMediaElement::mediaPlayerSizeChanged(MediaPlayer*)
1937 LOG(Media, "HTMLMediaElement::mediaPlayerSizeChanged");
1939 beginProcessingMediaPlayerCallback();
1941 renderer()->updateFromElement();
1942 endProcessingMediaPlayerCallback();
1945 #if USE(ACCELERATED_COMPOSITING)
1946 bool HTMLMediaElement::mediaPlayerRenderingCanBeAccelerated(MediaPlayer*)
1948 if (renderer() && renderer()->isVideo()) {
1949 ASSERT(renderer()->view());
1950 return renderer()->view()->compositor()->canAccelerateVideoRendering(toRenderVideo(renderer()));
1955 void HTMLMediaElement::mediaPlayerRenderingModeChanged(MediaPlayer*)
1957 LOG(Media, "HTMLMediaElement::mediaPlayerRenderingModeChanged");
1959 // Kick off a fake recalcStyle that will update the compositing tree.
1960 setNeedsStyleRecalc(SyntheticStyleChange);
1964 void HTMLMediaElement::mediaPlayerEngineUpdated(MediaPlayer*)
1966 beginProcessingMediaPlayerCallback();
1967 LOG(Media, "HTMLMediaElement::mediaPlayerEngineUpdated");
1969 renderer()->updateFromElement();
1970 endProcessingMediaPlayerCallback();
1973 PassRefPtr<TimeRanges> HTMLMediaElement::buffered() const
1976 return TimeRanges::create();
1977 return m_player->buffered();
1980 PassRefPtr<TimeRanges> HTMLMediaElement::played()
1983 float time = currentTime();
1984 if (time > m_lastSeekTime)
1985 addPlayedRange(m_lastSeekTime, time);
1988 if (!m_playedTimeRanges)
1989 m_playedTimeRanges = TimeRanges::create();
1991 return m_playedTimeRanges->copy();
1994 PassRefPtr<TimeRanges> HTMLMediaElement::seekable() const
1996 // FIXME real ranges support
1997 if (!maxTimeSeekable())
1998 return TimeRanges::create();
1999 return TimeRanges::create(minTimeSeekable(), maxTimeSeekable());
2002 bool HTMLMediaElement::potentiallyPlaying() const
2004 // "pausedToBuffer" means the media engine's rate is 0, but only because it had to stop playing
2005 // when it ran out of buffered data. A movie is this state is "potentially playing", modulo the
2006 // checks in couldPlayIfEnoughData().
2007 bool pausedToBuffer = m_readyStateMaximum >= HAVE_FUTURE_DATA && m_readyState < HAVE_FUTURE_DATA;
2008 return (pausedToBuffer || m_readyState >= HAVE_FUTURE_DATA) && couldPlayIfEnoughData();
2011 bool HTMLMediaElement::couldPlayIfEnoughData() const
2013 return !paused() && !endedPlayback() && !stoppedDueToErrors() && !pausedForUserInteraction();
2016 bool HTMLMediaElement::endedPlayback() const
2018 float dur = duration();
2019 if (!m_player || isnan(dur))
2022 // 4.8.10.8 Playing the media resource
2024 // A media element is said to have ended playback when the element's
2025 // readyState attribute is HAVE_METADATA or greater,
2026 if (m_readyState < HAVE_METADATA)
2029 // and the current playback position is the end of the media resource and the direction
2030 // of playback is forwards and the media element does not have a loop attribute specified,
2031 float now = currentTime();
2032 if (m_playbackRate > 0)
2033 return dur > 0 && now >= dur && !loop();
2035 // or the current playback position is the earliest possible position and the direction
2036 // of playback is backwards
2037 if (m_playbackRate < 0)
2043 bool HTMLMediaElement::stoppedDueToErrors() const
2045 if (m_readyState >= HAVE_METADATA && m_error) {
2046 RefPtr<TimeRanges> seekableRanges = seekable();
2047 if (!seekableRanges->contain(currentTime()))
2054 bool HTMLMediaElement::pausedForUserInteraction() const
2056 // return !paused() && m_readyState >= HAVE_FUTURE_DATA && [UA requires a decitions from the user]
2060 float HTMLMediaElement::minTimeSeekable() const
2065 float HTMLMediaElement::maxTimeSeekable() const
2067 return m_player ? m_player->maxTimeSeekable() : 0;
2070 void HTMLMediaElement::updateVolume()
2075 // Avoid recursion when the player reports volume changes.
2076 if (!processingMediaPlayerCallback()) {
2077 Page* page = document()->page();
2078 float volumeMultiplier = page ? page->mediaVolume() : 1;
2080 m_player->setMuted(m_muted);
2081 m_player->setVolume(m_volume * volumeMultiplier);
2085 renderer()->updateFromElement();
2088 void HTMLMediaElement::updatePlayState()
2093 if (m_pausedInternal) {
2094 if (!m_player->paused())
2096 refreshCachedTime();
2097 m_playbackProgressTimer.stop();
2101 bool shouldBePlaying = potentiallyPlaying();
2102 bool playerPaused = m_player->paused();
2104 LOG(Media, "HTMLMediaElement::updatePlayState - shouldBePlaying = %s, playerPaused = %s",
2105 boolString(shouldBePlaying), boolString(playerPaused));
2107 if (shouldBePlaying) {
2108 setDisplayMode(Video);
2109 invalidateCachedTime();
2112 if (!m_isFullscreen && isVideo() && document() && document()->page() && document()->page()->chrome()->requiresFullscreenForVideoPlayback())
2115 // Set rate before calling play in case the rate was set before the media engine was setup.
2116 // The media engine should just stash the rate since it isn't already playing.
2117 m_player->setRate(m_playbackRate);
2121 startPlaybackProgressTimer();
2124 } else { // Should not be playing right now
2127 refreshCachedTime();
2129 m_playbackProgressTimer.stop();
2131 float time = currentTime();
2132 if (time > m_lastSeekTime)
2133 addPlayedRange(m_lastSeekTime, time);
2135 if (couldPlayIfEnoughData())
2136 m_player->prepareToPlay();
2140 renderer()->updateFromElement();
2143 void HTMLMediaElement::setPausedInternal(bool b)
2145 m_pausedInternal = b;
2149 void HTMLMediaElement::stopPeriodicTimers()
2151 m_progressEventTimer.stop();
2152 m_playbackProgressTimer.stop();
2155 void HTMLMediaElement::userCancelledLoad()
2157 LOG(Media, "HTMLMediaElement::userCancelledLoad");
2159 if (m_networkState == NETWORK_EMPTY || m_completelyLoaded)
2162 // If the media data fetching process is aborted by the user:
2164 // 1 - The user agent should cancel the fetching process.
2165 #if !ENABLE(PLUGIN_PROXY_FOR_VIDEO)
2168 stopPeriodicTimers();
2169 m_loadState = WaitingForSource;
2171 // 2 - Set the error attribute to a new MediaError object whose code attribute is set to MEDIA_ERR_ABORTED.
2172 m_error = MediaError::create(MediaError::MEDIA_ERR_ABORTED);
2174 // 3 - Queue a task to fire a simple event named error at the media element.
2175 scheduleEvent(eventNames().abortEvent);
2177 // 4 - If the media element's readyState attribute has a value equal to HAVE_NOTHING, set the
2178 // element's networkState attribute to the NETWORK_EMPTY value and queue a task to fire a
2179 // simple event named emptied at the element. Otherwise, set the element's networkState
2180 // attribute to the NETWORK_IDLE value.
2181 if (m_readyState == HAVE_NOTHING) {
2182 m_networkState = NETWORK_EMPTY;
2183 scheduleEvent(eventNames().emptiedEvent);
2186 m_networkState = NETWORK_IDLE;
2188 // 5 - Set the element's delaying-the-load-event flag to false. This stops delaying the load event.
2189 setShouldDelayLoadEvent(false);
2191 // 6 - Abort the overall resource selection algorithm.
2192 m_currentSourceNode = 0;
2194 // Reset m_readyState since m_player is gone.
2195 m_readyState = HAVE_NOTHING;
2198 bool HTMLMediaElement::canSuspend() const
2203 void HTMLMediaElement::stop()
2205 LOG(Media, "HTMLMediaElement::stop");
2209 m_inActiveDocument = false;
2210 userCancelledLoad();
2212 // Stop the playback without generating events
2213 setPausedInternal(true);
2216 renderer()->updateFromElement();
2218 stopPeriodicTimers();
2219 cancelPendingEventsAndCallbacks();
2222 void HTMLMediaElement::suspend(ReasonForSuspension why)
2224 LOG(Media, "HTMLMediaElement::suspend");
2228 case DocumentWillBecomeInactive:
2231 case JavaScriptDebuggerPaused:
2232 case WillShowDialog:
2233 // Do nothing, we don't pause media playback in these cases.
2238 void HTMLMediaElement::resume()
2240 LOG(Media, "HTMLMediaElement::resume");
2242 m_inActiveDocument = true;
2243 setPausedInternal(false);
2245 if (m_error && m_error->code() == MediaError::MEDIA_ERR_ABORTED) {
2246 // Restart the load if it was aborted in the middle by moving the document to the page cache.
2247 // m_error is only left at MEDIA_ERR_ABORTED when the document becomes inactive (it is set to
2248 // MEDIA_ERR_ABORTED while the abortEvent is being sent, but cleared immediately afterwards).
2249 // This behavior is not specified but it seems like a sensible thing to do.
2251 load(processingUserGesture(), ec);
2255 renderer()->updateFromElement();
2258 bool HTMLMediaElement::hasPendingActivity() const
2260 // Return true when we have pending events so we can't fire events after the JS
2261 // object gets collected.
2262 bool pending = m_pendingEvents.size();
2263 LOG(Media, "HTMLMediaElement::hasPendingActivity -> %s", boolString(pending));
2267 void HTMLMediaElement::mediaVolumeDidChange()
2269 LOG(Media, "HTMLMediaElement::mediaVolumeDidChange");
2273 void HTMLMediaElement::defaultEventHandler(Event* event)
2275 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
2276 RenderObject* r = renderer();
2277 if (!r || !r->isWidget())
2280 Widget* widget = toRenderWidget(r)->widget();
2282 widget->handleEvent(event);
2284 if (renderer() && renderer()->isMedia())
2285 toRenderMedia(renderer())->forwardEvent(event);
2286 if (event->defaultHandled())
2288 HTMLElement::defaultEventHandler(event);
2292 bool HTMLMediaElement::processingUserGesture() const
2294 Frame* frame = document()->frame();
2295 FrameLoader* loader = frame ? frame->loader() : 0;
2297 // return 'true' for safety if we don't know the answer
2298 return loader ? loader->isProcessingUserGesture() : true;
2301 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
2303 void HTMLMediaElement::ensureMediaPlayer()
2306 m_player = MediaPlayer::create(this);
2309 void HTMLMediaElement::deliverNotification(MediaPlayerProxyNotificationType notification)
2311 if (notification == MediaPlayerNotificationPlayPauseButtonPressed) {
2317 m_player->deliverNotification(notification);
2320 void HTMLMediaElement::setMediaPlayerProxy(WebMediaPlayerProxy* proxy)
2322 ensureMediaPlayer();
2323 m_player->setMediaPlayerProxy(proxy);
2326 void HTMLMediaElement::getPluginProxyParams(KURL& url, Vector<String>& names, Vector<String>& values)
2328 Frame* frame = document()->frame();
2329 FrameLoader* loader = frame ? frame->loader() : 0;
2332 KURL posterURL = getNonEmptyURLAttribute(posterAttr);
2333 if (!posterURL.isEmpty() && loader && loader->willLoadMediaElementURL(posterURL)) {
2334 names.append("_media_element_poster_");
2335 values.append(posterURL.string());
2340 names.append("_media_element_controls_");
2341 values.append("true");
2345 if (!isSafeToLoadURL(url, Complain))
2346 url = selectNextSourceChild(0, DoNothing);
2348 m_currentSrc = url.string();
2349 if (url.isValid() && loader && loader->willLoadMediaElementURL(url)) {
2350 names.append("_media_element_src_");
2351 values.append(m_currentSrc);
2355 void HTMLMediaElement::finishParsingChildren()
2357 HTMLElement::finishParsingChildren();
2358 document()->updateStyleIfNeeded();
2359 createMediaPlayerProxy();
2362 void HTMLMediaElement::createMediaPlayerProxy()
2364 ensureMediaPlayer();
2366 if (m_proxyWidget || (inDocument() && !m_needWidgetUpdate))
2369 Frame* frame = document()->frame();
2370 FrameLoader* loader = frame ? frame->loader() : 0;
2374 LOG(Media, "HTMLMediaElement::createMediaPlayerProxy");
2377 Vector<String> paramNames;
2378 Vector<String> paramValues;
2380 getPluginProxyParams(url, paramNames, paramValues);
2382 // Hang onto the proxy widget so it won't be destroyed if the plug-in is set to
2384 m_proxyWidget = loader->subframeLoader()->loadMediaPlayerProxyPlugin(this, url, paramNames, paramValues);
2386 m_needWidgetUpdate = false;
2389 void HTMLMediaElement::updateWidget(bool)
2391 mediaElement->setNeedWidgetUpdate(false);
2393 Vector<String> paramNames;
2394 Vector<String> paramValues;
2397 mediaElement->getPluginProxyParams(kurl, paramNames, paramValues);
2398 SubframeLoader* loader = document()->frame()->loader()->subframeLoader();
2399 loader->loadMediaPlayerProxyPlugin(mediaElement, kurl, paramNames, paramValues);
2402 #endif // ENABLE(PLUGIN_PROXY_FOR_VIDEO)
2404 void HTMLMediaElement::enterFullscreen()
2406 LOG(Media, "HTMLMediaElement::enterFullscreen");
2408 ASSERT(!m_isFullscreen);
2409 m_isFullscreen = true;
2410 if (document() && document()->page()) {
2411 document()->page()->chrome()->client()->enterFullscreenForNode(this);
2412 scheduleEvent(eventNames().webkitbeginfullscreenEvent);
2416 void HTMLMediaElement::exitFullscreen()
2418 LOG(Media, "HTMLMediaElement::exitFullscreen");
2420 ASSERT(m_isFullscreen);
2421 m_isFullscreen = false;
2422 if (document() && document()->page()) {
2423 if (document()->page()->chrome()->requiresFullscreenForVideoPlayback())
2425 document()->page()->chrome()->client()->exitFullscreenForNode(this);
2426 scheduleEvent(eventNames().webkitendfullscreenEvent);
2430 PlatformMedia HTMLMediaElement::platformMedia() const
2432 return m_player ? m_player->platformMedia() : NoPlatformMedia;
2435 #if USE(ACCELERATED_COMPOSITING)
2436 PlatformLayer* HTMLMediaElement::platformLayer() const
2438 return m_player ? m_player->platformLayer() : 0;
2442 bool HTMLMediaElement::hasClosedCaptions() const
2444 return m_player && m_player->hasClosedCaptions();
2447 bool HTMLMediaElement::closedCaptionsVisible() const
2449 return m_closedCaptionsVisible;
2452 void HTMLMediaElement::setClosedCaptionsVisible(bool closedCaptionVisible)
2454 LOG(Media, "HTMLMediaElement::setClosedCaptionsVisible(%s)", boolString(closedCaptionVisible));
2456 if (!m_player ||!hasClosedCaptions())
2459 m_closedCaptionsVisible = closedCaptionVisible;
2460 m_player->setClosedCaptionsVisible(closedCaptionVisible);
2462 renderer()->updateFromElement();
2465 void HTMLMediaElement::setWebkitClosedCaptionsVisible(bool visible)
2467 setClosedCaptionsVisible(visible);
2470 bool HTMLMediaElement::webkitClosedCaptionsVisible() const
2472 return closedCaptionsVisible();
2476 bool HTMLMediaElement::webkitHasClosedCaptions() const
2478 return hasClosedCaptions();
2481 void HTMLMediaElement::mediaCanStart()
2483 LOG(Media, "HTMLMediaElement::mediaCanStart");
2485 ASSERT(m_isWaitingUntilMediaCanStart);
2486 m_isWaitingUntilMediaCanStart = false;
2490 bool HTMLMediaElement::isURLAttribute(Attribute* attribute) const
2492 return attribute->name() == srcAttr;
2495 void HTMLMediaElement::setShouldDelayLoadEvent(bool shouldDelay)
2497 if (m_shouldDelayLoadEvent == shouldDelay)
2500 LOG(Media, "HTMLMediaElement::setShouldDelayLoadEvent(%s)", boolString(shouldDelay));
2502 m_shouldDelayLoadEvent = shouldDelay;
2504 document()->incrementLoadEventDelayCount();
2506 document()->decrementLoadEventDelayCount();