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 "MediaControls.h"
52 #include "MediaDocument.h"
53 #include "MediaError.h"
54 #include "MediaList.h"
55 #include "MediaPlayer.h"
56 #include "MediaQueryEvaluator.h"
57 #include "MIMETypeRegistry.h"
59 #include "RenderVideo.h"
60 #include "RenderView.h"
61 #include "ScriptEventListener.h"
63 #include "TimeRanges.h"
65 #include <wtf/CurrentTime.h>
66 #include <wtf/MathExtras.h>
67 #include <wtf/text/CString.h>
69 #if USE(ACCELERATED_COMPOSITING)
70 #include "RenderView.h"
71 #include "RenderLayerCompositor.h"
74 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
75 #include "RenderEmbeddedObject.h"
84 static String urlForLogging(const String& url)
86 static const unsigned maximumURLLengthForLogging = 128;
88 if (url.length() < maximumURLLengthForLogging)
90 return url.substring(0, maximumURLLengthForLogging) + "...";
93 static const char *boolString(bool val)
95 return val ? "true" : "false";
99 #ifndef LOG_MEDIA_EVENTS
100 // Default to not logging events because so many are generated they can overwhelm the rest of
102 #define LOG_MEDIA_EVENTS 0
105 #ifndef LOG_CACHED_TIME_WARNINGS
106 // Default to not logging warnings about excessive drift in the cached media time because it adds a
107 // fair amount of overhead and logging.
108 #define LOG_CACHED_TIME_WARNINGS 0
111 static const float invalidMediaTime = -1;
113 using namespace HTMLNames;
115 HTMLMediaElement::HTMLMediaElement(const QualifiedName& tagName, Document* document)
116 : HTMLElement(tagName, document)
117 , ActiveDOMObject(document, this)
118 , m_loadTimer(this, &HTMLMediaElement::loadTimerFired)
119 , m_asyncEventTimer(this, &HTMLMediaElement::asyncEventTimerFired)
120 , m_progressEventTimer(this, &HTMLMediaElement::progressEventTimerFired)
121 , m_playbackProgressTimer(this, &HTMLMediaElement::playbackProgressTimerFired)
122 , m_playedTimeRanges()
123 , m_playbackRate(1.0f)
124 , m_defaultPlaybackRate(1.0f)
125 , m_webkitPreservesPitch(true)
126 , m_networkState(NETWORK_EMPTY)
127 , m_readyState(HAVE_NOTHING)
128 , m_readyStateMaximum(HAVE_NOTHING)
131 , m_previousProgress(0)
132 , m_previousProgressTime(numeric_limits<double>::max())
133 , m_lastTimeUpdateEventWallTime(0)
134 , m_lastTimeUpdateEventMovieTime(numeric_limits<float>::max())
135 , m_loadState(WaitingForSource)
136 , m_currentSourceNode(0)
137 , m_nextChildNodeToConsider(0)
139 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
142 , m_restrictions(NoRestrictions)
143 , m_preload(MediaPlayer::Auto)
144 , m_displayMode(Unknown)
145 , m_processingMediaPlayerCallback(0)
146 , m_cachedTime(invalidMediaTime)
147 , m_cachedTimeWallClockUpdateTime(0)
148 , m_minimumWallClockTimeToCacheMediaTime(0)
150 , m_isWaitingUntilMediaCanStart(false)
151 , m_shouldDelayLoadEvent(false)
152 , m_haveFiredLoadedData(false)
153 , m_inActiveDocument(true)
154 , m_autoplaying(true)
158 , m_sentStalledEvent(false)
159 , m_sentEndEvent(false)
160 , m_pausedInternal(false)
161 , m_sendProgressEvents(true)
162 , m_isFullscreen(false)
163 , m_closedCaptionsVisible(false)
164 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
165 , m_needWidgetUpdate(false)
167 , m_dispatchingCanPlayEvent(false)
168 , m_loadInitiatedByUserGesture(false)
169 , m_completelyLoaded(false)
171 LOG(Media, "HTMLMediaElement::HTMLMediaElement");
172 document->registerForDocumentActivationCallbacks(this);
173 document->registerForMediaVolumeCallbacks(this);
176 HTMLMediaElement::~HTMLMediaElement()
178 LOG(Media, "HTMLMediaElement::~HTMLMediaElement");
179 if (m_isWaitingUntilMediaCanStart)
180 document()->removeMediaCanStartListener(this);
181 setShouldDelayLoadEvent(false);
182 document()->unregisterForDocumentActivationCallbacks(this);
183 document()->unregisterForMediaVolumeCallbacks(this);
186 void HTMLMediaElement::willMoveToNewOwnerDocument()
188 if (m_isWaitingUntilMediaCanStart)
189 document()->removeMediaCanStartListener(this);
190 setShouldDelayLoadEvent(false);
191 document()->unregisterForDocumentActivationCallbacks(this);
192 document()->unregisterForMediaVolumeCallbacks(this);
193 HTMLElement::willMoveToNewOwnerDocument();
196 void HTMLMediaElement::didMoveToNewOwnerDocument()
198 if (m_isWaitingUntilMediaCanStart)
199 document()->addMediaCanStartListener(this);
200 if (m_readyState < HAVE_CURRENT_DATA)
201 setShouldDelayLoadEvent(true);
202 document()->registerForDocumentActivationCallbacks(this);
203 document()->registerForMediaVolumeCallbacks(this);
204 HTMLElement::didMoveToNewOwnerDocument();
207 void HTMLMediaElement::attributeChanged(Attribute* attr, bool preserveDecls)
209 HTMLElement::attributeChanged(attr, preserveDecls);
211 const QualifiedName& attrName = attr->name();
212 if (attrName == srcAttr) {
213 // Trigger a reload, as long as the 'src' attribute is present.
214 if (!getAttribute(srcAttr).isEmpty())
217 else if (attrName == controlsAttr) {
218 #if !ENABLE(PLUGIN_PROXY_FOR_VIDEO)
219 if (!isVideo() && attached() && (controls() != (renderer() != 0))) {
224 renderer()->updateFromElement();
227 m_player->setControls(controls());
232 void HTMLMediaElement::parseMappedAttribute(Attribute* attr)
234 const QualifiedName& attrName = attr->name();
236 if (attrName == preloadAttr) {
237 String value = attr->value();
239 if (equalIgnoringCase(value, "none"))
240 m_preload = MediaPlayer::None;
241 else if (equalIgnoringCase(value, "metadata"))
242 m_preload = MediaPlayer::MetaData;
244 // The spec does not define an "invalid value default" but "auto" is suggested as the
245 // "missing value default", so use it for everything except "none" and "metadata"
246 m_preload = MediaPlayer::Auto;
249 // The attribute must be ignored if the autoplay attribute is present
250 if (!autoplay() && m_player)
251 m_player->setPreload(m_preload);
253 } else if (attrName == onabortAttr)
254 setAttributeEventListener(eventNames().abortEvent, createAttributeEventListener(this, attr));
255 else if (attrName == onbeforeloadAttr)
256 setAttributeEventListener(eventNames().beforeloadEvent, createAttributeEventListener(this, attr));
257 else if (attrName == oncanplayAttr)
258 setAttributeEventListener(eventNames().canplayEvent, createAttributeEventListener(this, attr));
259 else if (attrName == oncanplaythroughAttr)
260 setAttributeEventListener(eventNames().canplaythroughEvent, createAttributeEventListener(this, attr));
261 else if (attrName == ondurationchangeAttr)
262 setAttributeEventListener(eventNames().durationchangeEvent, createAttributeEventListener(this, attr));
263 else if (attrName == onemptiedAttr)
264 setAttributeEventListener(eventNames().emptiedEvent, createAttributeEventListener(this, attr));
265 else if (attrName == onendedAttr)
266 setAttributeEventListener(eventNames().endedEvent, createAttributeEventListener(this, attr));
267 else if (attrName == onerrorAttr)
268 setAttributeEventListener(eventNames().errorEvent, createAttributeEventListener(this, attr));
269 else if (attrName == onloadeddataAttr)
270 setAttributeEventListener(eventNames().loadeddataEvent, createAttributeEventListener(this, attr));
271 else if (attrName == onloadedmetadataAttr)
272 setAttributeEventListener(eventNames().loadedmetadataEvent, createAttributeEventListener(this, attr));
273 else if (attrName == onloadstartAttr)
274 setAttributeEventListener(eventNames().loadstartEvent, createAttributeEventListener(this, attr));
275 else if (attrName == onpauseAttr)
276 setAttributeEventListener(eventNames().pauseEvent, createAttributeEventListener(this, attr));
277 else if (attrName == onplayAttr)
278 setAttributeEventListener(eventNames().playEvent, createAttributeEventListener(this, attr));
279 else if (attrName == onplayingAttr)
280 setAttributeEventListener(eventNames().playingEvent, createAttributeEventListener(this, attr));
281 else if (attrName == onprogressAttr)
282 setAttributeEventListener(eventNames().progressEvent, createAttributeEventListener(this, attr));
283 else if (attrName == onratechangeAttr)
284 setAttributeEventListener(eventNames().ratechangeEvent, createAttributeEventListener(this, attr));
285 else if (attrName == onseekedAttr)
286 setAttributeEventListener(eventNames().seekedEvent, createAttributeEventListener(this, attr));
287 else if (attrName == onseekingAttr)
288 setAttributeEventListener(eventNames().seekingEvent, createAttributeEventListener(this, attr));
289 else if (attrName == onstalledAttr)
290 setAttributeEventListener(eventNames().stalledEvent, createAttributeEventListener(this, attr));
291 else if (attrName == onsuspendAttr)
292 setAttributeEventListener(eventNames().suspendEvent, createAttributeEventListener(this, attr));
293 else if (attrName == ontimeupdateAttr)
294 setAttributeEventListener(eventNames().timeupdateEvent, createAttributeEventListener(this, attr));
295 else if (attrName == onvolumechangeAttr)
296 setAttributeEventListener(eventNames().volumechangeEvent, createAttributeEventListener(this, attr));
297 else if (attrName == onwaitingAttr)
298 setAttributeEventListener(eventNames().waitingEvent, createAttributeEventListener(this, attr));
299 else if (attrName == onwebkitbeginfullscreenAttr)
300 setAttributeEventListener(eventNames().webkitbeginfullscreenEvent, createAttributeEventListener(this, attr));
301 else if (attrName == onwebkitendfullscreenAttr)
302 setAttributeEventListener(eventNames().webkitendfullscreenEvent, createAttributeEventListener(this, attr));
304 HTMLElement::parseMappedAttribute(attr);
307 bool HTMLMediaElement::rendererIsNeeded(RenderStyle* style)
309 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
311 Frame* frame = document()->frame();
317 return controls() ? HTMLElement::rendererIsNeeded(style) : false;
321 RenderObject* HTMLMediaElement::createRenderer(RenderArena* arena, RenderStyle*)
323 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
324 // Setup the renderer if we already have a proxy widget.
325 RenderEmbeddedObject* mediaRenderer = new (arena) RenderEmbeddedObject(this);
327 mediaRenderer->setWidget(m_proxyWidget);
329 Frame* frame = document()->frame();
330 FrameLoader* loader = frame ? frame->loader() : 0;
332 loader->showMediaPlayerProxyPlugin(m_proxyWidget.get());
334 return mediaRenderer;
336 return new (arena) RenderMedia(this);
340 void HTMLMediaElement::insertedIntoDocument()
342 LOG(Media, "HTMLMediaElement::removedFromDocument");
343 HTMLElement::insertedIntoDocument();
344 if (!getAttribute(srcAttr).isEmpty() && m_networkState == NETWORK_EMPTY)
348 void HTMLMediaElement::removedFromDocument()
350 LOG(Media, "HTMLMediaElement::removedFromDocument");
351 if (m_networkState > NETWORK_EMPTY)
352 pause(processingUserGesture());
355 HTMLElement::removedFromDocument();
358 void HTMLMediaElement::attach()
362 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
363 m_needWidgetUpdate = true;
366 HTMLElement::attach();
369 renderer()->updateFromElement();
370 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
371 else if (m_proxyWidget) {
372 Frame* frame = document()->frame();
373 FrameLoader* loader = frame ? frame->loader() : 0;
375 loader->hideMediaPlayerProxyPlugin(m_proxyWidget.get());
380 void HTMLMediaElement::recalcStyle(StyleChange change)
382 HTMLElement::recalcStyle(change);
385 renderer()->updateFromElement();
388 void HTMLMediaElement::scheduleLoad()
390 LOG(Media, "HTMLMediaElement::scheduleLoad");
391 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
392 createMediaPlayerProxy();
395 if (m_loadTimer.isActive())
398 m_loadTimer.startOneShot(0);
401 void HTMLMediaElement::scheduleNextSourceChild()
403 // Schedule the timer to try the next <source> element WITHOUT resetting state ala prepareForLoad.
404 m_loadTimer.startOneShot(0);
407 void HTMLMediaElement::scheduleEvent(const AtomicString& eventName)
410 LOG(Media, "HTMLMediaElement::scheduleEvent - scheduling '%s'", eventName.string().ascii().data());
412 m_pendingEvents.append(Event::create(eventName, false, true));
413 if (!m_asyncEventTimer.isActive())
414 m_asyncEventTimer.startOneShot(0);
417 void HTMLMediaElement::asyncEventTimerFired(Timer<HTMLMediaElement>*)
419 Vector<RefPtr<Event> > pendingEvents;
420 ExceptionCode ec = 0;
422 m_pendingEvents.swap(pendingEvents);
423 unsigned count = pendingEvents.size();
424 for (unsigned ndx = 0; ndx < count; ++ndx) {
426 LOG(Media, "HTMLMediaElement::asyncEventTimerFired - dispatching '%s'", pendingEvents[ndx]->type().string().ascii().data());
428 if (pendingEvents[ndx]->type() == eventNames().canplayEvent) {
429 m_dispatchingCanPlayEvent = true;
430 dispatchEvent(pendingEvents[ndx].release(), ec);
431 m_dispatchingCanPlayEvent = false;
433 dispatchEvent(pendingEvents[ndx].release(), ec);
437 void HTMLMediaElement::loadTimerFired(Timer<HTMLMediaElement>*)
439 if (m_loadState == LoadingFromSourceElement)
440 loadNextSourceChild();
445 PassRefPtr<MediaError> HTMLMediaElement::error() const
450 void HTMLMediaElement::setSrc(const String& url)
452 setAttribute(srcAttr, url);
455 String HTMLMediaElement::currentSrc() const
460 HTMLMediaElement::NetworkState HTMLMediaElement::networkState() const
462 return m_networkState;
465 String HTMLMediaElement::canPlayType(const String& mimeType) const
467 MediaPlayer::SupportsType support = MediaPlayer::supportsType(ContentType(mimeType));
473 case MediaPlayer::IsNotSupported:
476 case MediaPlayer::MayBeSupported:
479 case MediaPlayer::IsSupported:
480 canPlay = "probably";
484 LOG(Media, "HTMLMediaElement::canPlayType(%s) -> %s", mimeType.utf8().data(), canPlay.utf8().data());
489 void HTMLMediaElement::load(bool isUserGesture, ExceptionCode& ec)
491 LOG(Media, "HTMLMediaElement::load(isUserGesture : %s)", boolString(isUserGesture));
493 if (m_restrictions & RequireUserGestureForLoadRestriction && !isUserGesture)
494 ec = INVALID_STATE_ERR;
496 m_loadInitiatedByUserGesture = isUserGesture;
502 void HTMLMediaElement::prepareForLoad()
504 LOG(Media, "HTMLMediaElement::prepareForLoad");
506 // Perform the cleanup required for the resource load algorithm to run.
507 stopPeriodicTimers();
509 m_sentStalledEvent = false;
510 m_haveFiredLoadedData = false;
511 m_completelyLoaded = false;
512 m_displayMode = Unknown;
514 // 1 - Abort any already-running instance of the resource selection algorithm for this element.
515 m_loadState = WaitingForSource;
516 m_currentSourceNode = 0;
518 // 2 - If there are any tasks from the media element's media element event task source in
519 // one of the task queues, then remove those tasks.
520 cancelPendingEventsAndCallbacks();
522 // 3 - If the media element's networkState is set to NETWORK_LOADING or NETWORK_IDLE, queue
523 // a task to fire a simple event named abort at the media element.
524 if (m_networkState == NETWORK_LOADING || m_networkState == NETWORK_IDLE)
525 scheduleEvent(eventNames().abortEvent);
527 #if !ENABLE(PLUGIN_PROXY_FOR_VIDEO)
528 m_player = MediaPlayer::create(this);
531 m_player->cancelLoad();
533 createMediaPlayerProxy();
536 // 4 - If the media element's networkState is not set to NETWORK_EMPTY, then run these substeps
537 if (m_networkState != NETWORK_EMPTY) {
538 m_networkState = NETWORK_EMPTY;
539 m_readyState = HAVE_NOTHING;
540 m_readyStateMaximum = HAVE_NOTHING;
544 invalidateCachedTime();
545 scheduleEvent(eventNames().emptiedEvent);
548 // 5 - Set the playbackRate attribute to the value of the defaultPlaybackRate attribute.
549 setPlaybackRate(defaultPlaybackRate());
551 // 6 - Set the error attribute to null and the autoplaying flag to true.
553 m_autoplaying = true;
555 // 7 - Invoke the media element's resource selection algorithm.
557 // 8 - Note: Playback of any previously playing media resource for this element stops.
559 // The resource selection algorithm
560 // 1 - Set the networkState to NETWORK_NO_SOURCE
561 m_networkState = NETWORK_NO_SOURCE;
563 // 2 - Asynchronously await a stable state.
565 m_playedTimeRanges = TimeRanges::create();
567 m_closedCaptionsVisible = false;
569 // The spec doesn't say to block the load event until we actually run the asynchronous section
570 // algorithm, but do it now because we won't start that until after the timer fires and the
571 // event may have already fired by then.
572 setShouldDelayLoadEvent(true);
575 void HTMLMediaElement::loadInternal()
577 // If we can't start a load right away, start it later.
578 Page* page = document()->page();
579 if (page && !page->canStartMedia()) {
580 if (m_isWaitingUntilMediaCanStart)
582 document()->addMediaCanStartListener(this);
583 m_isWaitingUntilMediaCanStart = true;
587 selectMediaResource();
590 void HTMLMediaElement::selectMediaResource()
592 LOG(Media, "HTMLMediaElement::selectMediaResource");
594 enum Mode { attribute, children };
595 Mode mode = attribute;
597 // 3 - ... the media element has neither a src attribute ...
598 if (!hasAttribute(srcAttr)) {
599 // ... nor a source element child: ...
601 for (node = firstChild(); node; node = node->nextSibling()) {
602 if (node->hasTagName(sourceTag))
607 m_loadState = WaitingForSource;
608 setShouldDelayLoadEvent(false);
610 // ... set the networkState to NETWORK_EMPTY, and abort these steps
611 m_networkState = NETWORK_EMPTY;
613 LOG(Media, "HTMLMediaElement::selectMediaResource, nothing to load");
620 // 4 - Set the media element's delaying-the-load-event flag to true (this delays the load event),
621 // and set its networkState to NETWORK_LOADING.
622 setShouldDelayLoadEvent(true);
623 m_networkState = NETWORK_LOADING;
626 scheduleEvent(eventNames().loadstartEvent);
628 // 6 - If mode is attribute, then run these substeps
629 if (mode == attribute) {
630 // If the src attribute's value is the empty string ... jump down to the failed step below
631 KURL mediaURL = getNonEmptyURLAttribute(srcAttr);
632 if (mediaURL.isEmpty()) {
634 LOG(Media, "HTMLMediaElement::selectMediaResource, empty 'src'");
638 if (isSafeToLoadURL(mediaURL, Complain) && dispatchBeforeLoadEvent(mediaURL.string())) {
639 ContentType contentType("");
640 m_loadState = LoadingFromSrcAttr;
641 loadResource(mediaURL, contentType);
645 LOG(Media, "HTMLMediaElement::selectMediaResource, 'src' not used");
649 // Otherwise, the source elements will be used
650 m_currentSourceNode = 0;
651 loadNextSourceChild();
654 void HTMLMediaElement::loadNextSourceChild()
656 ContentType contentType("");
657 KURL mediaURL = selectNextSourceChild(&contentType, Complain);
658 if (!mediaURL.isValid()) {
659 waitForSourceChange();
663 #if !ENABLE(PLUGIN_PROXY_FOR_VIDEO)
664 // Recreate the media player for the new url
665 m_player = MediaPlayer::create(this);
668 m_loadState = LoadingFromSourceElement;
669 loadResource(mediaURL, contentType);
672 void HTMLMediaElement::loadResource(const KURL& initialURL, ContentType& contentType)
674 ASSERT(isSafeToLoadURL(initialURL, Complain));
676 LOG(Media, "HTMLMediaElement::loadResource(%s, %s)", urlForLogging(initialURL.string()).utf8().data(), contentType.raw().utf8().data());
678 Frame* frame = document()->frame();
681 FrameLoader* loader = frame->loader();
685 KURL url(initialURL);
686 if (!loader->willLoadMediaElementURL(url))
689 // The resource fetch algorithm
690 m_networkState = NETWORK_LOADING;
694 LOG(Media, "HTMLMediaElement::loadResource - m_currentSrc -> %s", urlForLogging(m_currentSrc).utf8().data());
696 if (m_sendProgressEvents)
697 startProgressEventTimer();
700 m_player->setPreload(m_preload);
701 m_player->setPreservesPitch(m_webkitPreservesPitch);
704 m_player->load(m_currentSrc, contentType);
706 // If there is no poster to display, allow the media engine to render video frames as soon as
707 // they are available.
708 updateDisplayState();
711 renderer()->updateFromElement();
714 bool HTMLMediaElement::isSafeToLoadURL(const KURL& url, InvalidSourceAction actionIfInvalid)
716 if (!url.isValid()) {
717 LOG(Media, "HTMLMediaElement::isSafeToLoadURL(%s) -> FALSE because url is invalid", urlForLogging(url.string()).utf8().data());
721 Frame* frame = document()->frame();
722 if (!frame || !document()->securityOrigin()->canDisplay(url)) {
723 if (actionIfInvalid == Complain)
724 FrameLoader::reportLocalLoadFailed(frame, url.string());
725 LOG(Media, "HTMLMediaElement::isSafeToLoadURL(%s) -> FALSE rejected by SecurityOrigin", urlForLogging(url.string()).utf8().data());
732 void HTMLMediaElement::startProgressEventTimer()
734 if (m_progressEventTimer.isActive())
737 m_previousProgressTime = WTF::currentTime();
738 m_previousProgress = 0;
739 // 350ms is not magic, it is in the spec!
740 m_progressEventTimer.startRepeating(0.350);
743 void HTMLMediaElement::waitForSourceChange()
745 LOG(Media, "HTMLMediaElement::waitForSourceChange");
747 stopPeriodicTimers();
748 m_loadState = WaitingForSource;
750 // 6.17 - Waiting: Set the element's networkState attribute to the NETWORK_NO_SOURCE value
751 m_networkState = NETWORK_NO_SOURCE;
753 // 6.18 - Set the element's delaying-the-load-event flag to false. This stops delaying the load event.
754 setShouldDelayLoadEvent(false);
757 void HTMLMediaElement::noneSupported()
759 LOG(Media, "HTMLMediaElement::noneSupported");
761 stopPeriodicTimers();
762 m_loadState = WaitingForSource;
763 m_currentSourceNode = 0;
765 // 5 - Reaching this step indicates that either the URL failed to resolve, or the media
766 // resource failed to load. Set the error attribute to a new MediaError object whose
767 // code attribute is set to MEDIA_ERR_SRC_NOT_SUPPORTED.
768 m_error = MediaError::create(MediaError::MEDIA_ERR_SRC_NOT_SUPPORTED);
770 // 6 - Set the element's networkState attribute to the NETWORK_NO_SOURCE value.
771 m_networkState = NETWORK_NO_SOURCE;
773 // 7 - Queue a task to fire a progress event called error at the media element, in
774 // the context of the fetching process that was used to try to obtain the media
775 // resource in the resource fetch algorithm.
776 scheduleEvent(eventNames().errorEvent);
778 // 8 - Set the element's delaying-the-load-event flag to false. This stops delaying the load event.
779 setShouldDelayLoadEvent(false);
781 // 9 -Abort these steps. Until the load() method is invoked, the element won't attempt to load another resource.
783 updateDisplayState();
786 renderer()->updateFromElement();
789 void HTMLMediaElement::mediaEngineError(PassRefPtr<MediaError> err)
791 LOG(Media, "HTMLMediaElement::mediaEngineError(%d)", static_cast<int>(err->code()));
793 // 1 - The user agent should cancel the fetching process.
794 stopPeriodicTimers();
795 m_loadState = WaitingForSource;
797 // 2 - Set the error attribute to a new MediaError object whose code attribute is
798 // set to MEDIA_ERR_NETWORK/MEDIA_ERR_DECODE.
801 // 3 - Queue a task to fire a simple event named error at the media element.
802 scheduleEvent(eventNames().errorEvent);
804 // 4 - Set the element's networkState attribute to the NETWORK_EMPTY value and queue a
805 // task to fire a simple event called emptied at the element.
806 m_networkState = NETWORK_EMPTY;
807 scheduleEvent(eventNames().emptiedEvent);
809 // 5 - Set the element's delaying-the-load-event flag to false. This stops delaying the load event.
810 setShouldDelayLoadEvent(false);
812 // 6 - Abort the overall resource selection algorithm.
813 m_currentSourceNode = 0;
816 void HTMLMediaElement::cancelPendingEventsAndCallbacks()
818 LOG(Media, "HTMLMediaElement::cancelPendingEventsAndCallbacks");
820 m_pendingEvents.clear();
822 for (Node* node = firstChild(); node; node = node->nextSibling()) {
823 if (node->hasTagName(sourceTag))
824 static_cast<HTMLSourceElement*>(node)->cancelPendingErrorEvent();
828 Document* HTMLMediaElement::mediaPlayerOwningDocument()
830 Document* d = document();
838 void HTMLMediaElement::mediaPlayerNetworkStateChanged(MediaPlayer*)
840 beginProcessingMediaPlayerCallback();
841 setNetworkState(m_player->networkState());
842 endProcessingMediaPlayerCallback();
845 void HTMLMediaElement::setNetworkState(MediaPlayer::NetworkState state)
847 LOG(Media, "HTMLMediaElement::setNetworkState(%d) - current state is %d", static_cast<int>(state), static_cast<int>(m_networkState));
849 if (state == MediaPlayer::Empty) {
850 // Just update the cached state and leave, we can't do anything.
851 m_networkState = NETWORK_EMPTY;
855 if (state == MediaPlayer::FormatError || state == MediaPlayer::NetworkError || state == MediaPlayer::DecodeError) {
856 stopPeriodicTimers();
858 // If we failed while trying to load a <source> element, the movie was never parsed, and there are more
859 // <source> children, schedule the next one
860 if (m_readyState < HAVE_METADATA && m_loadState == LoadingFromSourceElement) {
862 if (m_currentSourceNode)
863 m_currentSourceNode->scheduleErrorEvent();
865 LOG(Media, "HTMLMediaElement::setNetworkState - error event not sent, <source> was removed");
867 if (havePotentialSourceChild()) {
868 LOG(Media, "HTMLMediaElement::setNetworkState - scheduling next <source>");
869 scheduleNextSourceChild();
871 LOG(Media, "HTMLMediaElement::setNetworkState - no more <source> elements, waiting");
872 waitForSourceChange();
878 if (state == MediaPlayer::NetworkError)
879 mediaEngineError(MediaError::create(MediaError::MEDIA_ERR_NETWORK));
880 else if (state == MediaPlayer::DecodeError)
881 mediaEngineError(MediaError::create(MediaError::MEDIA_ERR_DECODE));
882 else if (state == MediaPlayer::FormatError && m_loadState == LoadingFromSrcAttr)
885 updateDisplayState();
889 if (state == MediaPlayer::Idle) {
890 if (m_networkState > NETWORK_IDLE) {
891 m_progressEventTimer.stop();
892 scheduleEvent(eventNames().suspendEvent);
894 m_networkState = NETWORK_IDLE;
897 if (state == MediaPlayer::Loading) {
898 if (m_networkState < NETWORK_LOADING || m_networkState == NETWORK_NO_SOURCE)
899 startProgressEventTimer();
900 m_networkState = NETWORK_LOADING;
903 if (state == MediaPlayer::Loaded) {
904 if (m_networkState != NETWORK_IDLE) {
905 m_progressEventTimer.stop();
907 // Schedule one last progress event so we guarantee that at least one is fired
908 // for files that load very quickly.
909 scheduleEvent(eventNames().progressEvent);
911 m_networkState = NETWORK_IDLE;
912 m_completelyLoaded = true;
916 void HTMLMediaElement::mediaPlayerReadyStateChanged(MediaPlayer*)
918 beginProcessingMediaPlayerCallback();
920 setReadyState(m_player->readyState());
922 endProcessingMediaPlayerCallback();
925 void HTMLMediaElement::setReadyState(MediaPlayer::ReadyState state)
927 LOG(Media, "HTMLMediaElement::setReadyState(%d) - current state is %d,", static_cast<int>(state), static_cast<int>(m_readyState));
929 // Set "wasPotentiallyPlaying" BEFORE updating m_readyState, potentiallyPlaying() uses it
930 bool wasPotentiallyPlaying = potentiallyPlaying();
932 ReadyState oldState = m_readyState;
933 m_readyState = static_cast<ReadyState>(state);
935 if (m_readyState == oldState)
938 if (oldState > m_readyStateMaximum)
939 m_readyStateMaximum = oldState;
941 if (m_networkState == NETWORK_EMPTY)
946 if (wasPotentiallyPlaying && m_readyState < HAVE_FUTURE_DATA)
947 scheduleEvent(eventNames().waitingEvent);
949 // 4.8.10.10 step 14 & 15.
950 if (m_readyState >= HAVE_CURRENT_DATA)
953 if (wasPotentiallyPlaying && m_readyState < HAVE_FUTURE_DATA) {
955 scheduleTimeupdateEvent(false);
956 scheduleEvent(eventNames().waitingEvent);
960 if (m_readyState >= HAVE_METADATA && oldState < HAVE_METADATA) {
961 scheduleEvent(eventNames().durationchangeEvent);
962 scheduleEvent(eventNames().loadedmetadataEvent);
964 renderer()->updateFromElement();
968 bool shouldUpdateDisplayState = false;
970 if (m_readyState >= HAVE_CURRENT_DATA && oldState < HAVE_CURRENT_DATA && !m_haveFiredLoadedData) {
971 m_haveFiredLoadedData = true;
972 shouldUpdateDisplayState = true;
973 scheduleEvent(eventNames().loadeddataEvent);
974 setShouldDelayLoadEvent(false);
977 bool isPotentiallyPlaying = potentiallyPlaying();
978 if (m_readyState == HAVE_FUTURE_DATA && oldState <= HAVE_CURRENT_DATA) {
979 scheduleEvent(eventNames().canplayEvent);
980 if (isPotentiallyPlaying)
981 scheduleEvent(eventNames().playingEvent);
982 shouldUpdateDisplayState = true;
985 if (m_readyState == HAVE_ENOUGH_DATA && oldState < HAVE_ENOUGH_DATA) {
986 if (oldState <= HAVE_CURRENT_DATA)
987 scheduleEvent(eventNames().canplayEvent);
989 scheduleEvent(eventNames().canplaythroughEvent);
991 if (isPotentiallyPlaying && oldState <= HAVE_CURRENT_DATA)
992 scheduleEvent(eventNames().playingEvent);
994 if (m_autoplaying && m_paused && autoplay()) {
996 invalidateCachedTime();
997 scheduleEvent(eventNames().playEvent);
998 scheduleEvent(eventNames().playingEvent);
1001 shouldUpdateDisplayState = true;
1004 if (shouldUpdateDisplayState)
1005 updateDisplayState();
1010 void HTMLMediaElement::progressEventTimerFired(Timer<HTMLMediaElement>*)
1013 if (m_networkState != NETWORK_LOADING)
1016 unsigned progress = m_player->bytesLoaded();
1017 double time = WTF::currentTime();
1018 double timedelta = time - m_previousProgressTime;
1020 if (progress == m_previousProgress) {
1021 if (timedelta > 3.0 && !m_sentStalledEvent) {
1022 scheduleEvent(eventNames().stalledEvent);
1023 m_sentStalledEvent = true;
1026 scheduleEvent(eventNames().progressEvent);
1027 m_previousProgress = progress;
1028 m_previousProgressTime = time;
1029 m_sentStalledEvent = false;
1031 renderer()->updateFromElement();
1035 void HTMLMediaElement::rewind(float timeDelta)
1037 LOG(Media, "HTMLMediaElement::rewind(%f)", timeDelta);
1040 setCurrentTime(max(currentTime() - timeDelta, minTimeSeekable()), e);
1043 void HTMLMediaElement::returnToRealtime()
1045 LOG(Media, "HTMLMediaElement::returnToRealtime");
1047 setCurrentTime(maxTimeSeekable(), e);
1050 void HTMLMediaElement::addPlayedRange(float start, float end)
1052 LOG(Media, "HTMLMediaElement::addPlayedRange(%f, %f)", start, end);
1053 if (!m_playedTimeRanges)
1054 m_playedTimeRanges = TimeRanges::create();
1055 m_playedTimeRanges->add(start, end);
1058 bool HTMLMediaElement::supportsSave() const
1060 return m_player ? m_player->supportsSave() : false;
1063 void HTMLMediaElement::seek(float time, ExceptionCode& ec)
1065 LOG(Media, "HTMLMediaElement::seek(%f)", time);
1069 // 1 - If the media element's readyState is HAVE_NOTHING, then raise an INVALID_STATE_ERR exception.
1070 if (m_readyState == HAVE_NOTHING || !m_player) {
1071 ec = INVALID_STATE_ERR;
1075 // Get the current time before setting m_seeking, m_lastSeekTime is returned once it is set.
1076 refreshCachedTime();
1077 float now = currentTime();
1079 // 2 - If the element's seeking IDL attribute is true, then another instance of this algorithm is
1080 // already running. Abort that other instance of the algorithm without waiting for the step that
1081 // it is running to complete.
1082 // Nothing specific to be done here.
1084 // 3 - Set the seeking IDL attribute to true.
1085 // The flag will be cleared when the engine tells us the time has actually changed.
1088 // 5 - If the new playback position is later than the end of the media resource, then let it be the end
1089 // of the media resource instead.
1090 time = min(time, duration());
1092 // 6 - If the new playback position is less than the earliest possible position, let it be that position instead.
1093 float earliestTime = m_player->startTime();
1094 time = max(time, earliestTime);
1096 // Ask the media engine for the time value in the movie's time scale before comparing with current time. This
1097 // is necessary because if the seek time is not equal to currentTime but the delta is less than the movie's
1098 // time scale, we will ask the media engine to "seek" to the current movie time, which may be a noop and
1099 // not generate a timechanged callback. This means m_seeking will never be cleared and we will never
1100 // fire a 'seeked' event.
1102 float mediaTime = m_player->mediaTimeForTimeValue(time);
1103 if (time != mediaTime)
1104 LOG(Media, "HTMLMediaElement::seek(%f) - media timeline equivalent is %f", time, mediaTime);
1106 time = m_player->mediaTimeForTimeValue(time);
1108 // 7 - If the (possibly now changed) new playback position is not in one of the ranges given in the
1109 // seekable attribute, then let it be the position in one of the ranges given in the seekable attribute
1110 // that is the nearest to the new playback position. ... If there are no ranges given in the seekable
1111 // attribute then set the seeking IDL attribute to false and abort these steps.
1112 RefPtr<TimeRanges> seekableRanges = seekable();
1114 // Short circuit seeking to the current time by just firing the events if no seek is required.
1115 // Don't skip calling the media engine if we are in poster mode because a seek should always
1116 // cancel poster display.
1117 bool noSeekRequired = !seekableRanges->length() || (time == now && displayMode() != Poster);
1118 if (noSeekRequired) {
1120 scheduleEvent(eventNames().seekingEvent);
1121 scheduleTimeupdateEvent(false);
1122 scheduleEvent(eventNames().seekedEvent);
1127 time = seekableRanges->nearest(time);
1130 if (m_lastSeekTime < now)
1131 addPlayedRange(m_lastSeekTime, now);
1133 m_lastSeekTime = time;
1134 m_sentEndEvent = false;
1136 // 8 - Set the current playback position to the given new playback position
1137 m_player->seek(time);
1139 // 9 - Queue a task to fire a simple event named seeking at the element.
1140 scheduleEvent(eventNames().seekingEvent);
1142 // 10 - Queue a task to fire a simple event named timeupdate at the element.
1143 scheduleTimeupdateEvent(false);
1145 // 11-15 are handled, if necessary, when the engine signals a readystate change.
1148 void HTMLMediaElement::finishSeek()
1150 LOG(Media, "HTMLMediaElement::finishSeek");
1152 // 4.8.10.9 Seeking step 14
1155 // 4.8.10.9 Seeking step 15
1156 scheduleEvent(eventNames().seekedEvent);
1158 setDisplayMode(Video);
1161 HTMLMediaElement::ReadyState HTMLMediaElement::readyState() const
1163 return m_readyState;
1166 MediaPlayer::MovieLoadType HTMLMediaElement::movieLoadType() const
1168 return m_player ? m_player->movieLoadType() : MediaPlayer::Unknown;
1171 bool HTMLMediaElement::hasAudio() const
1173 return m_player ? m_player->hasAudio() : false;
1176 bool HTMLMediaElement::seeking() const
1181 void HTMLMediaElement::refreshCachedTime() const
1183 m_cachedTime = m_player->currentTime();
1184 m_cachedTimeWallClockUpdateTime = WTF::currentTime();
1187 void HTMLMediaElement::invalidateCachedTime()
1189 LOG(Media, "HTMLMediaElement::invalidateCachedTime");
1191 // Don't try to cache movie time when playback first starts as the time reported by the engine
1192 // sometimes fluctuates for a short amount of time, so the cached time will be off if we take it
1194 static const double minimumTimePlayingBeforeCacheSnapshot = 0.5;
1196 m_minimumWallClockTimeToCacheMediaTime = WTF::currentTime() + minimumTimePlayingBeforeCacheSnapshot;
1197 m_cachedTime = invalidMediaTime;
1201 float HTMLMediaElement::currentTime() const
1203 #if LOG_CACHED_TIME_WARNINGS
1204 static const double minCachedDeltaForWarning = 0.01;
1211 LOG(Media, "HTMLMediaElement::currentTime - seeking, returning %f", m_lastSeekTime);
1212 return m_lastSeekTime;
1215 if (m_cachedTime != invalidMediaTime && m_paused) {
1216 #if LOG_CACHED_TIME_WARNINGS
1217 float delta = m_cachedTime - m_player->currentTime();
1218 if (delta > minCachedDeltaForWarning)
1219 LOG(Media, "HTMLMediaElement::currentTime - WARNING, cached time is %f seconds off of media time when paused", delta);
1221 return m_cachedTime;
1224 // Is it too soon use a cached time?
1225 double now = WTF::currentTime();
1226 double maximumDurationToCacheMediaTime = m_player->maximumDurationToCacheMediaTime();
1228 if (maximumDurationToCacheMediaTime && m_cachedTime != invalidMediaTime && !m_paused && now > m_minimumWallClockTimeToCacheMediaTime) {
1229 double wallClockDelta = now - m_cachedTimeWallClockUpdateTime;
1231 // Not too soon, use the cached time only if it hasn't expired.
1232 if (wallClockDelta < maximumDurationToCacheMediaTime) {
1233 float adjustedCacheTime = static_cast<float>(m_cachedTime + (m_playbackRate * wallClockDelta));
1235 #if LOG_CACHED_TIME_WARNINGS
1236 float delta = adjustedCacheTime - m_player->currentTime();
1237 if (delta > minCachedDeltaForWarning)
1238 LOG(Media, "HTMLMediaElement::currentTime - WARNING, cached time is %f seconds off of media time when playing", delta);
1240 return adjustedCacheTime;
1244 #if LOG_CACHED_TIME_WARNINGS
1245 if (maximumDurationToCacheMediaTime && now > m_minimumWallClockTimeToCacheMediaTime && m_cachedTime != invalidMediaTime) {
1246 double wallClockDelta = now - m_cachedTimeWallClockUpdateTime;
1247 float delta = m_cachedTime + (m_playbackRate * wallClockDelta) - m_player->currentTime();
1248 LOG(Media, "HTMLMediaElement::currentTime - cached time was %f seconds off of media time when it expired", delta);
1252 refreshCachedTime();
1254 return m_cachedTime;
1257 void HTMLMediaElement::setCurrentTime(float time, ExceptionCode& ec)
1262 float HTMLMediaElement::startTime() const
1266 return m_player->startTime();
1269 float HTMLMediaElement::duration() const
1271 if (m_player && m_readyState >= HAVE_METADATA)
1272 return m_player->duration();
1274 return numeric_limits<float>::quiet_NaN();
1277 bool HTMLMediaElement::paused() const
1282 float HTMLMediaElement::defaultPlaybackRate() const
1284 return m_defaultPlaybackRate;
1287 void HTMLMediaElement::setDefaultPlaybackRate(float rate)
1289 if (m_defaultPlaybackRate != rate) {
1290 m_defaultPlaybackRate = rate;
1291 scheduleEvent(eventNames().ratechangeEvent);
1295 float HTMLMediaElement::playbackRate() const
1297 return m_player ? m_player->rate() : 0;
1300 void HTMLMediaElement::setPlaybackRate(float rate)
1302 LOG(Media, "HTMLMediaElement::setPlaybackRate(%f)", rate);
1304 if (m_playbackRate != rate) {
1305 m_playbackRate = rate;
1306 invalidateCachedTime();
1307 scheduleEvent(eventNames().ratechangeEvent);
1309 if (m_player && potentiallyPlaying() && m_player->rate() != rate)
1310 m_player->setRate(rate);
1313 bool HTMLMediaElement::webkitPreservesPitch() const
1315 return m_webkitPreservesPitch;
1318 void HTMLMediaElement::setWebkitPreservesPitch(bool preservesPitch)
1320 LOG(Media, "HTMLMediaElement::setWebkitPreservesPitch(%s)", boolString(preservesPitch));
1322 m_webkitPreservesPitch = preservesPitch;
1327 m_player->setPreservesPitch(preservesPitch);
1330 bool HTMLMediaElement::ended() const
1332 // 4.8.10.8 Playing the media resource
1333 // The ended attribute must return true if the media element has ended
1334 // playback and the direction of playback is forwards, and false otherwise.
1335 return endedPlayback() && m_playbackRate > 0;
1338 bool HTMLMediaElement::autoplay() const
1340 return hasAttribute(autoplayAttr);
1343 void HTMLMediaElement::setAutoplay(bool b)
1345 LOG(Media, "HTMLMediaElement::setAutoplay(%s)", boolString(b));
1346 setBooleanAttribute(autoplayAttr, b);
1349 String HTMLMediaElement::preload() const
1351 switch (m_preload) {
1352 case MediaPlayer::None:
1355 case MediaPlayer::MetaData:
1358 case MediaPlayer::Auto:
1363 ASSERT_NOT_REACHED();
1367 void HTMLMediaElement::setPreload(const String& preload)
1369 LOG(Media, "HTMLMediaElement::setPreload(%s)", preload.utf8().data());
1370 setAttribute(preloadAttr, preload);
1373 void HTMLMediaElement::play(bool isUserGesture)
1375 LOG(Media, "HTMLMediaElement::play(isUserGesture : %s)", boolString(isUserGesture));
1377 if (m_restrictions & RequireUserGestureForRateChangeRestriction && !isUserGesture)
1380 Document* doc = document();
1381 Settings* settings = doc->settings();
1382 if (settings && settings->needsSiteSpecificQuirks() && m_dispatchingCanPlayEvent && !m_loadInitiatedByUserGesture) {
1383 // It should be impossible to be processing the canplay event while handling a user gesture
1384 // since it is dispatched asynchronously.
1385 ASSERT(!isUserGesture);
1386 String host = doc->baseURL().host();
1387 if (host.endsWith(".npr.org", false) || equalIgnoringCase(host, "npr.org"))
1394 void HTMLMediaElement::playInternal()
1396 LOG(Media, "HTMLMediaElement::playInternal");
1398 // 4.8.10.9. Playing the media resource
1399 if (!m_player || m_networkState == NETWORK_EMPTY)
1402 if (endedPlayback()) {
1403 ExceptionCode unused;
1407 setPlaybackRate(defaultPlaybackRate());
1411 invalidateCachedTime();
1412 scheduleEvent(eventNames().playEvent);
1414 if (m_readyState <= HAVE_CURRENT_DATA)
1415 scheduleEvent(eventNames().waitingEvent);
1416 else if (m_readyState >= HAVE_FUTURE_DATA)
1417 scheduleEvent(eventNames().playingEvent);
1419 m_autoplaying = false;
1424 void HTMLMediaElement::pause(bool isUserGesture)
1426 LOG(Media, "HTMLMediaElement::pause(isUserGesture : %s)", boolString(isUserGesture));
1428 if (m_restrictions & RequireUserGestureForRateChangeRestriction && !isUserGesture)
1435 void HTMLMediaElement::pauseInternal()
1437 LOG(Media, "HTMLMediaElement::pauseInternal");
1439 // 4.8.10.9. Playing the media resource
1440 if (!m_player || m_networkState == NETWORK_EMPTY)
1443 m_autoplaying = false;
1447 scheduleTimeupdateEvent(false);
1448 scheduleEvent(eventNames().pauseEvent);
1454 bool HTMLMediaElement::loop() const
1456 return hasAttribute(loopAttr);
1459 void HTMLMediaElement::setLoop(bool b)
1461 LOG(Media, "HTMLMediaElement::setLoop(%s)", boolString(b));
1462 setBooleanAttribute(loopAttr, b);
1465 bool HTMLMediaElement::controls() const
1467 Frame* frame = document()->frame();
1469 // always show controls when scripting is disabled
1470 if (frame && !frame->script()->canExecuteScripts(NotAboutToExecuteScript))
1473 return hasAttribute(controlsAttr);
1476 void HTMLMediaElement::setControls(bool b)
1478 LOG(Media, "HTMLMediaElement::setControls(%s)", boolString(b));
1479 setBooleanAttribute(controlsAttr, b);
1482 float HTMLMediaElement::volume() const
1487 void HTMLMediaElement::setVolume(float vol, ExceptionCode& ec)
1489 LOG(Media, "HTMLMediaElement::setVolume(%f)", vol);
1491 if (vol < 0.0f || vol > 1.0f) {
1492 ec = INDEX_SIZE_ERR;
1496 if (m_volume != vol) {
1499 scheduleEvent(eventNames().volumechangeEvent);
1503 bool HTMLMediaElement::muted() const
1508 void HTMLMediaElement::setMuted(bool muted)
1510 LOG(Media, "HTMLMediaElement::setMuted(%s)", boolString(muted));
1512 if (m_muted != muted) {
1514 // Avoid recursion when the player reports volume changes.
1515 if (!processingMediaPlayerCallback()) {
1517 m_player->setMuted(m_muted);
1519 renderer()->updateFromElement();
1523 scheduleEvent(eventNames().volumechangeEvent);
1527 void HTMLMediaElement::togglePlayState()
1529 LOG(Media, "HTMLMediaElement::togglePlayState - canPlay() is %s", boolString(canPlay()));
1531 // We can safely call the internal play/pause methods, which don't check restrictions, because
1532 // this method is only called from the built-in media controller
1539 void HTMLMediaElement::beginScrubbing()
1541 LOG(Media, "HTMLMediaElement::beginScrubbing - paused() is %s", boolString(paused()));
1545 // Because a media element stays in non-paused state when it reaches end, playback resumes
1546 // when the slider is dragged from the end to another position unless we pause first. Do
1547 // a "hard pause" so an event is generated, since we want to stay paused after scrubbing finishes.
1548 pause(processingUserGesture());
1550 // Not at the end but we still want to pause playback so the media engine doesn't try to
1551 // continue playing during scrubbing. Pause without generating an event as we will
1552 // unpause after scrubbing finishes.
1553 setPausedInternal(true);
1558 void HTMLMediaElement::endScrubbing()
1560 LOG(Media, "HTMLMediaElement::endScrubbing - m_pausedInternal is %s", boolString(m_pausedInternal));
1562 if (m_pausedInternal)
1563 setPausedInternal(false);
1566 // The spec says to fire periodic timeupdate events (those sent while playing) every
1567 // "15 to 250ms", we choose the slowest frequency
1568 static const double maxTimeupdateEventFrequency = 0.25;
1570 void HTMLMediaElement::startPlaybackProgressTimer()
1572 if (m_playbackProgressTimer.isActive())
1575 m_previousProgressTime = WTF::currentTime();
1576 m_previousProgress = 0;
1577 m_playbackProgressTimer.startRepeating(maxTimeupdateEventFrequency);
1580 void HTMLMediaElement::playbackProgressTimerFired(Timer<HTMLMediaElement>*)
1583 if (!m_playbackRate)
1586 scheduleTimeupdateEvent(true);
1588 // FIXME: deal with cue ranges here
1591 void HTMLMediaElement::scheduleTimeupdateEvent(bool periodicEvent)
1593 double now = WTF::currentTime();
1594 double timedelta = now - m_lastTimeUpdateEventWallTime;
1596 // throttle the periodic events
1597 if (periodicEvent && timedelta < maxTimeupdateEventFrequency)
1600 // Some media engines make multiple "time changed" callbacks at the same time, but we only want one
1601 // event at a given time so filter here
1602 float movieTime = currentTime();
1603 if (movieTime != m_lastTimeUpdateEventMovieTime) {
1604 scheduleEvent(eventNames().timeupdateEvent);
1605 m_lastTimeUpdateEventWallTime = now;
1606 m_lastTimeUpdateEventMovieTime = movieTime;
1610 bool HTMLMediaElement::canPlay() const
1612 return paused() || ended() || m_readyState < HAVE_METADATA;
1615 float HTMLMediaElement::percentLoaded() const
1619 float duration = m_player->duration();
1621 if (!duration || isinf(duration))
1625 RefPtr<TimeRanges> timeRanges = m_player->buffered();
1626 for (unsigned i = 0; i < timeRanges->length(); ++i) {
1627 ExceptionCode ignoredException;
1628 float start = timeRanges->start(i, ignoredException);
1629 float end = timeRanges->end(i, ignoredException);
1630 buffered += end - start;
1632 return buffered / duration;
1635 bool HTMLMediaElement::havePotentialSourceChild()
1637 // Stash the current <source> node and next nodes so we can restore them after checking
1638 // to see there is another potential.
1639 HTMLSourceElement* currentSourceNode = m_currentSourceNode;
1640 Node* nextNode = m_nextChildNodeToConsider;
1642 KURL nextURL = selectNextSourceChild(0, DoNothing);
1644 m_currentSourceNode = currentSourceNode;
1645 m_nextChildNodeToConsider = nextNode;
1647 return nextURL.isValid();
1650 KURL HTMLMediaElement::selectNextSourceChild(ContentType *contentType, InvalidSourceAction actionIfInvalid)
1653 // Don't log if this was just called to find out if there are any valid <source> elements.
1654 bool shouldLog = actionIfInvalid != DoNothing;
1656 LOG(Media, "HTMLMediaElement::selectNextSourceChild(contentType : \"%s\")", contentType ? contentType->raw().utf8().data() : "");
1659 if (m_nextChildNodeToConsider == sourceChildEndOfListValue()) {
1662 LOG(Media, "HTMLMediaElement::selectNextSourceChild -> 0x0000, \"\"");
1669 HTMLSourceElement* source = 0;
1670 bool lookingForStartNode = m_nextChildNodeToConsider;
1671 bool canUse = false;
1673 for (node = firstChild(); !canUse && node; node = node->nextSibling()) {
1674 if (lookingForStartNode && m_nextChildNodeToConsider != node)
1676 lookingForStartNode = false;
1678 if (!node->hasTagName(sourceTag))
1681 source = static_cast<HTMLSourceElement*>(node);
1683 // 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
1684 mediaURL = source->getNonEmptyURLAttribute(srcAttr);
1687 LOG(Media, "HTMLMediaElement::selectNextSourceChild - 'src' is %s", urlForLogging(mediaURL).utf8().data());
1689 if (mediaURL.isEmpty())
1692 if (source->hasAttribute(mediaAttr)) {
1693 MediaQueryEvaluator screenEval("screen", document()->frame(), renderer() ? renderer()->style() : 0);
1694 RefPtr<MediaList> media = MediaList::createAllowingDescriptionSyntax(source->media());
1697 LOG(Media, "HTMLMediaElement::selectNextSourceChild - 'media' is %s", source->media().utf8().data());
1699 if (!screenEval.eval(media.get()))
1703 if (source->hasAttribute(typeAttr)) {
1706 LOG(Media, "HTMLMediaElement::selectNextSourceChild - 'type' is %s", source->type().utf8().data());
1708 if (!MediaPlayer::supportsType(ContentType(source->type())))
1712 // Is it safe to load this url?
1713 if (!isSafeToLoadURL(mediaURL, actionIfInvalid) || !dispatchBeforeLoadEvent(mediaURL.string()))
1716 // Making it this far means the <source> looks reasonable.
1720 if (!canUse && actionIfInvalid == Complain)
1721 source->scheduleErrorEvent();
1726 *contentType = ContentType(source->type());
1727 m_currentSourceNode = source;
1728 m_nextChildNodeToConsider = source->nextSibling();
1729 if (!m_nextChildNodeToConsider)
1730 m_nextChildNodeToConsider = sourceChildEndOfListValue();
1732 m_currentSourceNode = 0;
1733 m_nextChildNodeToConsider = sourceChildEndOfListValue();
1738 LOG(Media, "HTMLMediaElement::selectNextSourceChild -> %p, %s", m_currentSourceNode, canUse ? urlForLogging(mediaURL.string()).utf8().data() : "");
1740 return canUse ? mediaURL : KURL();
1743 void HTMLMediaElement::sourceWasAdded(HTMLSourceElement* source)
1745 LOG(Media, "HTMLMediaElement::sourceWasAdded(%p)", source);
1748 if (source->hasTagName(sourceTag)) {
1749 KURL url = source->getNonEmptyURLAttribute(srcAttr);
1750 LOG(Media, "HTMLMediaElement::sourceWasAdded - 'src' is %s", urlForLogging(url).utf8().data());
1754 // We should only consider a <source> element when there is not src attribute at all.
1755 if (hasAttribute(srcAttr))
1758 // 4.8.8 - If a source element is inserted as a child of a media element that has no src
1759 // attribute and whose networkState has the value NETWORK_EMPTY, the user agent must invoke
1760 // the media element's resource selection algorithm.
1761 if (networkState() == HTMLMediaElement::NETWORK_EMPTY) {
1766 if (m_currentSourceNode && source == m_currentSourceNode->nextSibling()) {
1767 LOG(Media, "HTMLMediaElement::sourceWasAdded - <source> inserted immediately after current source");
1768 m_nextChildNodeToConsider = source;
1772 if (m_nextChildNodeToConsider != sourceChildEndOfListValue())
1775 // 4.8.9.5, resource selection algorithm, source elements section:
1776 // 20 - Wait until the node after pointer is a node other than the end of the list. (This step might wait forever.)
1777 // 21 - Asynchronously await a stable state...
1778 // 22 - Set the element's delaying-the-load-event flag back to true (this delays the load event again, in case
1779 // it hasn't been fired yet).
1780 setShouldDelayLoadEvent(true);
1782 // 23 - Set the networkState back to NETWORK_LOADING.
1783 m_networkState = NETWORK_LOADING;
1785 // 24 - Jump back to the find next candidate step above.
1786 m_nextChildNodeToConsider = source;
1787 scheduleNextSourceChild();
1790 void HTMLMediaElement::sourceWillBeRemoved(HTMLSourceElement* source)
1792 LOG(Media, "HTMLMediaElement::sourceWillBeRemoved(%p)", source);
1795 if (source->hasTagName(sourceTag)) {
1796 KURL url = source->getNonEmptyURLAttribute(srcAttr);
1797 LOG(Media, "HTMLMediaElement::sourceWillBeRemoved - 'src' is %s", urlForLogging(url).utf8().data());
1801 if (source != m_currentSourceNode && source != m_nextChildNodeToConsider)
1804 if (source == m_nextChildNodeToConsider) {
1805 m_nextChildNodeToConsider = m_nextChildNodeToConsider->nextSibling();
1806 if (!m_nextChildNodeToConsider)
1807 m_nextChildNodeToConsider = sourceChildEndOfListValue();
1808 LOG(Media, "HTMLMediaElement::sourceRemoved - m_nextChildNodeToConsider set to %p", m_nextChildNodeToConsider);
1809 } else if (source == m_currentSourceNode) {
1810 // Clear the current source node pointer, but don't change the movie as the spec says:
1811 // 4.8.8 - Dynamically modifying a source element and its attribute when the element is already
1812 // inserted in a video or audio element will have no effect.
1813 m_currentSourceNode = 0;
1814 LOG(Media, "HTMLMediaElement::sourceRemoved - m_currentSourceNode set to 0");
1818 void HTMLMediaElement::mediaPlayerTimeChanged(MediaPlayer*)
1820 LOG(Media, "HTMLMediaElement::mediaPlayerTimeChanged");
1822 beginProcessingMediaPlayerCallback();
1824 invalidateCachedTime();
1826 // 4.8.10.9 step 14 & 15. Needed if no ReadyState change is associated with the seek.
1827 if (m_seeking && m_readyState >= HAVE_CURRENT_DATA)
1830 // Always call scheduleTimeupdateEvent when the media engine reports a time discontinuity,
1831 // it will only queue a 'timeupdate' event if we haven't already posted one at the current
1833 scheduleTimeupdateEvent(false);
1835 float now = currentTime();
1836 float dur = duration();
1837 if (!isnan(dur) && dur && now >= dur) {
1839 ExceptionCode ignoredException;
1840 m_sentEndEvent = false;
1841 seek(0, ignoredException);
1843 if (!m_sentEndEvent) {
1844 m_sentEndEvent = true;
1845 scheduleEvent(eventNames().endedEvent);
1850 m_sentEndEvent = false;
1853 endProcessingMediaPlayerCallback();
1856 void HTMLMediaElement::mediaPlayerVolumeChanged(MediaPlayer*)
1858 LOG(Media, "HTMLMediaElement::mediaPlayerVolumeChanged");
1860 beginProcessingMediaPlayerCallback();
1862 m_volume = m_player->volume();
1864 endProcessingMediaPlayerCallback();
1867 void HTMLMediaElement::mediaPlayerMuteChanged(MediaPlayer*)
1869 LOG(Media, "HTMLMediaElement::mediaPlayerMuteChanged");
1871 beginProcessingMediaPlayerCallback();
1873 setMuted(m_player->muted());
1874 endProcessingMediaPlayerCallback();
1877 void HTMLMediaElement::mediaPlayerDurationChanged(MediaPlayer*)
1879 LOG(Media, "HTMLMediaElement::mediaPlayerDurationChanged");
1881 beginProcessingMediaPlayerCallback();
1882 scheduleEvent(eventNames().durationchangeEvent);
1884 renderer()->updateFromElement();
1885 endProcessingMediaPlayerCallback();
1888 void HTMLMediaElement::mediaPlayerRateChanged(MediaPlayer*)
1890 LOG(Media, "HTMLMediaElement::mediaPlayerRateChanged");
1892 beginProcessingMediaPlayerCallback();
1894 invalidateCachedTime();
1896 // Stash the rate in case the one we tried to set isn't what the engine is
1897 // using (eg. it can't handle the rate we set)
1898 m_playbackRate = m_player->rate();
1899 invalidateCachedTime();
1900 endProcessingMediaPlayerCallback();
1903 void HTMLMediaElement::mediaPlayerPlaybackStateChanged(MediaPlayer*)
1905 LOG(Media, "HTMLMediaElement::mediaPlayerPlaybackStateChanged");
1910 beginProcessingMediaPlayerCallback();
1911 if (m_player->paused())
1915 endProcessingMediaPlayerCallback();
1918 void HTMLMediaElement::mediaPlayerSawUnsupportedTracks(MediaPlayer*)
1920 LOG(Media, "HTMLMediaElement::mediaPlayerSawUnsupportedTracks");
1922 // The MediaPlayer came across content it cannot completely handle.
1923 // This is normally acceptable except when we are in a standalone
1924 // MediaDocument. If so, tell the document what has happened.
1925 if (ownerDocument()->isMediaDocument()) {
1926 MediaDocument* mediaDocument = static_cast<MediaDocument*>(ownerDocument());
1927 mediaDocument->mediaElementSawUnsupportedTracks();
1931 // MediaPlayerPresentation methods
1932 void HTMLMediaElement::mediaPlayerRepaint(MediaPlayer*)
1934 beginProcessingMediaPlayerCallback();
1935 updateDisplayState();
1937 renderer()->repaint();
1938 endProcessingMediaPlayerCallback();
1941 void HTMLMediaElement::mediaPlayerSizeChanged(MediaPlayer*)
1943 LOG(Media, "HTMLMediaElement::mediaPlayerSizeChanged");
1945 beginProcessingMediaPlayerCallback();
1947 renderer()->updateFromElement();
1948 endProcessingMediaPlayerCallback();
1951 #if USE(ACCELERATED_COMPOSITING)
1952 bool HTMLMediaElement::mediaPlayerRenderingCanBeAccelerated(MediaPlayer*)
1954 if (renderer() && renderer()->isVideo()) {
1955 ASSERT(renderer()->view());
1956 return renderer()->view()->compositor()->canAccelerateVideoRendering(toRenderVideo(renderer()));
1961 void HTMLMediaElement::mediaPlayerRenderingModeChanged(MediaPlayer*)
1963 LOG(Media, "HTMLMediaElement::mediaPlayerRenderingModeChanged");
1965 // Kick off a fake recalcStyle that will update the compositing tree.
1966 setNeedsStyleRecalc(SyntheticStyleChange);
1970 void HTMLMediaElement::mediaPlayerEngineUpdated(MediaPlayer*)
1972 beginProcessingMediaPlayerCallback();
1973 LOG(Media, "HTMLMediaElement::mediaPlayerEngineUpdated");
1975 renderer()->updateFromElement();
1976 endProcessingMediaPlayerCallback();
1979 PassRefPtr<TimeRanges> HTMLMediaElement::buffered() const
1982 return TimeRanges::create();
1983 return m_player->buffered();
1986 PassRefPtr<TimeRanges> HTMLMediaElement::played()
1989 float time = currentTime();
1990 if (time > m_lastSeekTime)
1991 addPlayedRange(m_lastSeekTime, time);
1994 if (!m_playedTimeRanges)
1995 m_playedTimeRanges = TimeRanges::create();
1997 return m_playedTimeRanges->copy();
2000 PassRefPtr<TimeRanges> HTMLMediaElement::seekable() const
2002 // FIXME real ranges support
2003 if (!maxTimeSeekable())
2004 return TimeRanges::create();
2005 return TimeRanges::create(minTimeSeekable(), maxTimeSeekable());
2008 bool HTMLMediaElement::potentiallyPlaying() const
2010 // "pausedToBuffer" means the media engine's rate is 0, but only because it had to stop playing
2011 // when it ran out of buffered data. A movie is this state is "potentially playing", modulo the
2012 // checks in couldPlayIfEnoughData().
2013 bool pausedToBuffer = m_readyStateMaximum >= HAVE_FUTURE_DATA && m_readyState < HAVE_FUTURE_DATA;
2014 return (pausedToBuffer || m_readyState >= HAVE_FUTURE_DATA) && couldPlayIfEnoughData();
2017 bool HTMLMediaElement::couldPlayIfEnoughData() const
2019 return !paused() && !endedPlayback() && !stoppedDueToErrors() && !pausedForUserInteraction();
2022 bool HTMLMediaElement::endedPlayback() const
2024 float dur = duration();
2025 if (!m_player || isnan(dur))
2028 // 4.8.10.8 Playing the media resource
2030 // A media element is said to have ended playback when the element's
2031 // readyState attribute is HAVE_METADATA or greater,
2032 if (m_readyState < HAVE_METADATA)
2035 // and the current playback position is the end of the media resource and the direction
2036 // of playback is forwards and the media element does not have a loop attribute specified,
2037 float now = currentTime();
2038 if (m_playbackRate > 0)
2039 return dur > 0 && now >= dur && !loop();
2041 // or the current playback position is the earliest possible position and the direction
2042 // of playback is backwards
2043 if (m_playbackRate < 0)
2049 bool HTMLMediaElement::stoppedDueToErrors() const
2051 if (m_readyState >= HAVE_METADATA && m_error) {
2052 RefPtr<TimeRanges> seekableRanges = seekable();
2053 if (!seekableRanges->contain(currentTime()))
2060 bool HTMLMediaElement::pausedForUserInteraction() const
2062 // return !paused() && m_readyState >= HAVE_FUTURE_DATA && [UA requires a decitions from the user]
2066 float HTMLMediaElement::minTimeSeekable() const
2071 float HTMLMediaElement::maxTimeSeekable() const
2073 return m_player ? m_player->maxTimeSeekable() : 0;
2076 void HTMLMediaElement::updateVolume()
2081 // Avoid recursion when the player reports volume changes.
2082 if (!processingMediaPlayerCallback()) {
2083 Page* page = document()->page();
2084 float volumeMultiplier = page ? page->mediaVolume() : 1;
2086 m_player->setMuted(m_muted);
2087 m_player->setVolume(m_volume * volumeMultiplier);
2091 renderer()->updateFromElement();
2094 void HTMLMediaElement::updatePlayState()
2099 if (m_pausedInternal) {
2100 if (!m_player->paused())
2102 refreshCachedTime();
2103 m_playbackProgressTimer.stop();
2107 bool shouldBePlaying = potentiallyPlaying();
2108 bool playerPaused = m_player->paused();
2110 LOG(Media, "HTMLMediaElement::updatePlayState - shouldBePlaying = %s, playerPaused = %s",
2111 boolString(shouldBePlaying), boolString(playerPaused));
2113 if (shouldBePlaying) {
2114 setDisplayMode(Video);
2115 invalidateCachedTime();
2118 if (!m_isFullscreen && isVideo() && document() && document()->page() && document()->page()->chrome()->requiresFullscreenForVideoPlayback())
2121 // Set rate before calling play in case the rate was set before the media engine was setup.
2122 // The media engine should just stash the rate since it isn't already playing.
2123 m_player->setRate(m_playbackRate);
2127 startPlaybackProgressTimer();
2130 } else { // Should not be playing right now
2133 refreshCachedTime();
2135 m_playbackProgressTimer.stop();
2137 float time = currentTime();
2138 if (time > m_lastSeekTime)
2139 addPlayedRange(m_lastSeekTime, time);
2141 if (couldPlayIfEnoughData())
2142 m_player->prepareToPlay();
2146 renderer()->updateFromElement();
2149 void HTMLMediaElement::setPausedInternal(bool b)
2151 m_pausedInternal = b;
2155 void HTMLMediaElement::stopPeriodicTimers()
2157 m_progressEventTimer.stop();
2158 m_playbackProgressTimer.stop();
2161 void HTMLMediaElement::userCancelledLoad()
2163 LOG(Media, "HTMLMediaElement::userCancelledLoad");
2165 if (m_networkState == NETWORK_EMPTY || m_completelyLoaded)
2168 // If the media data fetching process is aborted by the user:
2170 // 1 - The user agent should cancel the fetching process.
2171 #if !ENABLE(PLUGIN_PROXY_FOR_VIDEO)
2174 stopPeriodicTimers();
2175 m_loadState = WaitingForSource;
2177 // 2 - Set the error attribute to a new MediaError object whose code attribute is set to MEDIA_ERR_ABORTED.
2178 m_error = MediaError::create(MediaError::MEDIA_ERR_ABORTED);
2180 // 3 - Queue a task to fire a simple event named error at the media element.
2181 scheduleEvent(eventNames().abortEvent);
2183 // 4 - If the media element's readyState attribute has a value equal to HAVE_NOTHING, set the
2184 // element's networkState attribute to the NETWORK_EMPTY value and queue a task to fire a
2185 // simple event named emptied at the element. Otherwise, set the element's networkState
2186 // attribute to the NETWORK_IDLE value.
2187 if (m_readyState == HAVE_NOTHING) {
2188 m_networkState = NETWORK_EMPTY;
2189 scheduleEvent(eventNames().emptiedEvent);
2192 m_networkState = NETWORK_IDLE;
2194 // 5 - Set the element's delaying-the-load-event flag to false. This stops delaying the load event.
2195 setShouldDelayLoadEvent(false);
2197 // 6 - Abort the overall resource selection algorithm.
2198 m_currentSourceNode = 0;
2200 // Reset m_readyState since m_player is gone.
2201 m_readyState = HAVE_NOTHING;
2204 bool HTMLMediaElement::canSuspend() const
2209 void HTMLMediaElement::stop()
2211 LOG(Media, "HTMLMediaElement::stop");
2215 m_inActiveDocument = false;
2216 userCancelledLoad();
2218 // Stop the playback without generating events
2219 setPausedInternal(true);
2222 renderer()->updateFromElement();
2224 stopPeriodicTimers();
2225 cancelPendingEventsAndCallbacks();
2228 void HTMLMediaElement::suspend(ReasonForSuspension why)
2230 LOG(Media, "HTMLMediaElement::suspend");
2234 case DocumentWillBecomeInactive:
2237 case JavaScriptDebuggerPaused:
2238 case WillShowDialog:
2239 // Do nothing, we don't pause media playback in these cases.
2244 void HTMLMediaElement::resume()
2246 LOG(Media, "HTMLMediaElement::resume");
2248 m_inActiveDocument = true;
2249 setPausedInternal(false);
2251 if (m_error && m_error->code() == MediaError::MEDIA_ERR_ABORTED) {
2252 // Restart the load if it was aborted in the middle by moving the document to the page cache.
2253 // m_error is only left at MEDIA_ERR_ABORTED when the document becomes inactive (it is set to
2254 // MEDIA_ERR_ABORTED while the abortEvent is being sent, but cleared immediately afterwards).
2255 // This behavior is not specified but it seems like a sensible thing to do.
2257 load(processingUserGesture(), ec);
2261 renderer()->updateFromElement();
2264 bool HTMLMediaElement::hasPendingActivity() const
2266 // Return true when we have pending events so we can't fire events after the JS
2267 // object gets collected.
2268 bool pending = m_pendingEvents.size();
2269 LOG(Media, "HTMLMediaElement::hasPendingActivity -> %s", boolString(pending));
2273 void HTMLMediaElement::mediaVolumeDidChange()
2275 LOG(Media, "HTMLMediaElement::mediaVolumeDidChange");
2279 void HTMLMediaElement::defaultEventHandler(Event* event)
2281 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
2282 RenderObject* r = renderer();
2283 if (!r || !r->isWidget())
2286 Widget* widget = toRenderWidget(r)->widget();
2288 widget->handleEvent(event);
2290 if (renderer() && renderer()->isMedia())
2291 toRenderMedia(renderer())->controls()->forwardEvent(event);
2292 if (event->defaultHandled())
2294 HTMLElement::defaultEventHandler(event);
2298 bool HTMLMediaElement::processingUserGesture() const
2300 Frame* frame = document()->frame();
2301 FrameLoader* loader = frame ? frame->loader() : 0;
2303 // return 'true' for safety if we don't know the answer
2304 return loader ? loader->isProcessingUserGesture() : true;
2307 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
2309 void HTMLMediaElement::ensureMediaPlayer()
2312 m_player = MediaPlayer::create(this);
2315 void HTMLMediaElement::deliverNotification(MediaPlayerProxyNotificationType notification)
2317 if (notification == MediaPlayerNotificationPlayPauseButtonPressed) {
2323 m_player->deliverNotification(notification);
2326 void HTMLMediaElement::setMediaPlayerProxy(WebMediaPlayerProxy* proxy)
2328 ensureMediaPlayer();
2329 m_player->setMediaPlayerProxy(proxy);
2332 void HTMLMediaElement::getPluginProxyParams(KURL& url, Vector<String>& names, Vector<String>& values)
2334 Frame* frame = document()->frame();
2335 FrameLoader* loader = frame ? frame->loader() : 0;
2338 KURL posterURL = getNonEmptyURLAttribute(posterAttr);
2339 if (!posterURL.isEmpty() && loader && loader->willLoadMediaElementURL(posterURL)) {
2340 names.append("_media_element_poster_");
2341 values.append(posterURL.string());
2346 names.append("_media_element_controls_");
2347 values.append("true");
2351 if (!isSafeToLoadURL(url, Complain))
2352 url = selectNextSourceChild(0, DoNothing);
2354 m_currentSrc = url.string();
2355 if (url.isValid() && loader && loader->willLoadMediaElementURL(url)) {
2356 names.append("_media_element_src_");
2357 values.append(m_currentSrc);
2361 void HTMLMediaElement::finishParsingChildren()
2363 HTMLElement::finishParsingChildren();
2364 document()->updateStyleIfNeeded();
2365 createMediaPlayerProxy();
2368 void HTMLMediaElement::createMediaPlayerProxy()
2370 ensureMediaPlayer();
2372 if (m_proxyWidget || (inDocument() && !m_needWidgetUpdate))
2375 Frame* frame = document()->frame();
2376 FrameLoader* loader = frame ? frame->loader() : 0;
2380 LOG(Media, "HTMLMediaElement::createMediaPlayerProxy");
2383 Vector<String> paramNames;
2384 Vector<String> paramValues;
2386 getPluginProxyParams(url, paramNames, paramValues);
2388 // Hang onto the proxy widget so it won't be destroyed if the plug-in is set to
2390 m_proxyWidget = loader->subframeLoader()->loadMediaPlayerProxyPlugin(this, url, paramNames, paramValues);
2392 m_needWidgetUpdate = false;
2395 void HTMLMediaElement::updateWidget(PluginCreationOption)
2397 mediaElement->setNeedWidgetUpdate(false);
2399 Vector<String> paramNames;
2400 Vector<String> paramValues;
2403 mediaElement->getPluginProxyParams(kurl, paramNames, paramValues);
2404 SubframeLoader* loader = document()->frame()->loader()->subframeLoader();
2405 loader->loadMediaPlayerProxyPlugin(mediaElement, kurl, paramNames, paramValues);
2408 #endif // ENABLE(PLUGIN_PROXY_FOR_VIDEO)
2410 void HTMLMediaElement::enterFullscreen()
2412 LOG(Media, "HTMLMediaElement::enterFullscreen");
2414 ASSERT(!m_isFullscreen);
2415 m_isFullscreen = true;
2416 if (document() && document()->page()) {
2417 document()->page()->chrome()->client()->enterFullscreenForNode(this);
2418 scheduleEvent(eventNames().webkitbeginfullscreenEvent);
2422 void HTMLMediaElement::exitFullscreen()
2424 LOG(Media, "HTMLMediaElement::exitFullscreen");
2426 ASSERT(m_isFullscreen);
2427 m_isFullscreen = false;
2428 if (document() && document()->page()) {
2429 if (document()->page()->chrome()->requiresFullscreenForVideoPlayback())
2431 document()->page()->chrome()->client()->exitFullscreenForNode(this);
2432 scheduleEvent(eventNames().webkitendfullscreenEvent);
2436 PlatformMedia HTMLMediaElement::platformMedia() const
2438 return m_player ? m_player->platformMedia() : NoPlatformMedia;
2441 #if USE(ACCELERATED_COMPOSITING)
2442 PlatformLayer* HTMLMediaElement::platformLayer() const
2444 return m_player ? m_player->platformLayer() : 0;
2448 bool HTMLMediaElement::hasClosedCaptions() const
2450 return m_player && m_player->hasClosedCaptions();
2453 bool HTMLMediaElement::closedCaptionsVisible() const
2455 return m_closedCaptionsVisible;
2458 void HTMLMediaElement::setClosedCaptionsVisible(bool closedCaptionVisible)
2460 LOG(Media, "HTMLMediaElement::setClosedCaptionsVisible(%s)", boolString(closedCaptionVisible));
2462 if (!m_player ||!hasClosedCaptions())
2465 m_closedCaptionsVisible = closedCaptionVisible;
2466 m_player->setClosedCaptionsVisible(closedCaptionVisible);
2468 renderer()->updateFromElement();
2471 void HTMLMediaElement::setWebkitClosedCaptionsVisible(bool visible)
2473 setClosedCaptionsVisible(visible);
2476 bool HTMLMediaElement::webkitClosedCaptionsVisible() const
2478 return closedCaptionsVisible();
2482 bool HTMLMediaElement::webkitHasClosedCaptions() const
2484 return hasClosedCaptions();
2487 void HTMLMediaElement::mediaCanStart()
2489 LOG(Media, "HTMLMediaElement::mediaCanStart");
2491 ASSERT(m_isWaitingUntilMediaCanStart);
2492 m_isWaitingUntilMediaCanStart = false;
2496 bool HTMLMediaElement::isURLAttribute(Attribute* attribute) const
2498 return attribute->name() == srcAttr;
2501 void HTMLMediaElement::setShouldDelayLoadEvent(bool shouldDelay)
2503 if (m_shouldDelayLoadEvent == shouldDelay)
2506 LOG(Media, "HTMLMediaElement::setShouldDelayLoadEvent(%s)", boolString(shouldDelay));
2508 m_shouldDelayLoadEvent = shouldDelay;
2510 document()->incrementLoadEventDelayCount();
2512 document()->decrementLoadEventDelayCount();