2 * Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012, 2013 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "core/html/HTMLMediaElement.h"
30 #include "HTMLNames.h"
31 #include "RuntimeEnabledFeatures.h"
32 #include "bindings/v8/ExceptionState.h"
33 #include "bindings/v8/ExceptionStatePlaceholder.h"
34 #include "bindings/v8/ScriptController.h"
35 #include "bindings/v8/ScriptEventListener.h"
36 #include "core/css/MediaList.h"
37 #include "core/dom/Attribute.h"
38 #include "core/dom/ExceptionCode.h"
39 #include "core/dom/FullscreenElementStack.h"
40 #include "core/dom/shadow/ShadowRoot.h"
41 #include "core/events/Event.h"
42 #include "core/frame/LocalFrame.h"
43 #include "core/frame/Settings.h"
44 #include "core/frame/UseCounter.h"
45 #include "core/frame/csp/ContentSecurityPolicy.h"
46 #include "core/html/HTMLMediaSource.h"
47 #include "core/html/HTMLSourceElement.h"
48 #include "core/html/HTMLTrackElement.h"
49 #include "core/html/MediaController.h"
50 #include "core/html/MediaError.h"
51 #include "core/html/MediaFragmentURIParser.h"
52 #include "core/html/TimeRanges.h"
53 #include "core/html/shadow/MediaControls.h"
54 #include "core/html/track/InbandTextTrack.h"
55 #include "core/html/track/TextTrackCueList.h"
56 #include "core/html/track/TextTrackList.h"
57 #include "core/loader/FrameLoader.h"
58 #include "core/rendering/RenderVideo.h"
59 #include "core/rendering/RenderView.h"
60 #include "core/rendering/compositing/RenderLayerCompositor.h"
61 #include "platform/ContentType.h"
62 #include "platform/Language.h"
63 #include "platform/Logging.h"
64 #include "platform/MIMETypeFromURL.h"
65 #include "platform/MIMETypeRegistry.h"
66 #include "platform/NotImplemented.h"
67 #include "platform/UserGestureIndicator.h"
68 #include "platform/graphics/GraphicsLayer.h"
69 #include "platform/weborigin/SecurityOrigin.h"
70 #include "public/platform/Platform.h"
71 #include "public/platform/WebContentDecryptionModule.h"
72 #include "public/platform/WebInbandTextTrack.h"
73 #include "wtf/CurrentTime.h"
74 #include "wtf/MathExtras.h"
75 #include "wtf/NonCopyingSort.h"
76 #include "wtf/Uint8Array.h"
77 #include "wtf/text/CString.h"
80 #include "platform/audio/AudioSourceProvider.h"
81 #include "modules/webaudio/MediaElementAudioSourceNode.h"
85 using blink::WebInbandTextTrack;
86 using blink::WebMediaPlayer;
87 using blink::WebMimeRegistry;
92 static String urlForLoggingMedia(const KURL& url)
94 static const unsigned maximumURLLengthForLogging = 128;
96 if (url.string().length() < maximumURLLengthForLogging)
98 return url.string().substring(0, maximumURLLengthForLogging) + "...";
101 static const char* boolString(bool val)
103 return val ? "true" : "false";
107 #ifndef LOG_MEDIA_EVENTS
108 // Default to not logging events because so many are generated they can overwhelm the rest of
110 #define LOG_MEDIA_EVENTS 0
113 #ifndef LOG_CACHED_TIME_WARNINGS
114 // Default to not logging warnings about excessive drift in the cached media time because it adds a
115 // fair amount of overhead and logging.
116 #define LOG_CACHED_TIME_WARNINGS 0
119 // URL protocol used to signal that the media source API is being used.
120 static const char mediaSourceBlobProtocol[] = "blob";
122 using namespace HTMLNames;
125 typedef HashMap<Document*, HashSet<HTMLMediaElement*> > DocumentElementSetMap;
126 static DocumentElementSetMap& documentToElementSetMap()
128 DEFINE_STATIC_LOCAL(DocumentElementSetMap, map, ());
132 static void addElementToDocumentMap(HTMLMediaElement* element, Document* document)
134 DocumentElementSetMap& map = documentToElementSetMap();
135 HashSet<HTMLMediaElement*> set = map.take(document);
137 map.add(document, set);
140 static void removeElementFromDocumentMap(HTMLMediaElement* element, Document* document)
142 DocumentElementSetMap& map = documentToElementSetMap();
143 HashSet<HTMLMediaElement*> set = map.take(document);
146 map.add(document, set);
149 class TrackDisplayUpdateScope {
151 TrackDisplayUpdateScope(HTMLMediaElement* mediaElement)
153 m_mediaElement = mediaElement;
154 m_mediaElement->beginIgnoringTrackDisplayUpdateRequests();
156 ~TrackDisplayUpdateScope()
158 ASSERT(m_mediaElement);
159 m_mediaElement->endIgnoringTrackDisplayUpdateRequests();
163 HTMLMediaElement* m_mediaElement;
166 static bool canLoadURL(const KURL& url, const ContentType& contentType, const String& keySystem)
168 DEFINE_STATIC_LOCAL(const String, codecs, ("codecs"));
170 String contentMIMEType = contentType.type().lower();
171 String contentTypeCodecs = contentType.parameter(codecs);
173 // If the MIME type is missing or is not meaningful, try to figure it out from the URL.
174 if (contentMIMEType.isEmpty() || contentMIMEType == "application/octet-stream" || contentMIMEType == "text/plain") {
175 if (url.protocolIsData())
176 contentMIMEType = mimeTypeFromDataURL(url.string());
179 // If no MIME type is specified, always attempt to load.
180 if (contentMIMEType.isEmpty())
183 // 4.8.10.3 MIME types - In the absence of a specification to the contrary, the MIME type "application/octet-stream"
184 // when used with parameters, e.g. "application/octet-stream;codecs=theora", is a type that the user agent knows
186 if (contentMIMEType != "application/octet-stream" || contentTypeCodecs.isEmpty()) {
187 WebMimeRegistry::SupportsType supported = blink::Platform::current()->mimeRegistry()->supportsMediaMIMEType(contentMIMEType, contentTypeCodecs, keySystem.lower());
188 return supported > WebMimeRegistry::IsNotSupported;
194 WebMimeRegistry::SupportsType HTMLMediaElement::supportsType(const ContentType& contentType, const String& keySystem)
196 DEFINE_STATIC_LOCAL(const String, codecs, ("codecs"));
198 if (!RuntimeEnabledFeatures::mediaEnabled())
199 return WebMimeRegistry::IsNotSupported;
201 String type = contentType.type().lower();
202 // The codecs string is not lower-cased because MP4 values are case sensitive
203 // per http://tools.ietf.org/html/rfc4281#page-7.
204 String typeCodecs = contentType.parameter(codecs);
205 String system = keySystem.lower();
208 return WebMimeRegistry::IsNotSupported;
210 // 4.8.10.3 MIME types - The canPlayType(type) method must return the empty string if type is a type that the
211 // user agent knows it cannot render or is the type "application/octet-stream"
212 if (type == "application/octet-stream")
213 return WebMimeRegistry::IsNotSupported;
215 return blink::Platform::current()->mimeRegistry()->supportsMediaMIMEType(type, typeCodecs, system);
218 URLRegistry* HTMLMediaElement::s_mediaStreamRegistry = 0;
220 void HTMLMediaElement::setMediaStreamRegistry(URLRegistry* registry)
222 ASSERT(!s_mediaStreamRegistry);
223 s_mediaStreamRegistry = registry;
226 bool HTMLMediaElement::isMediaStreamURL(const String& url)
228 return s_mediaStreamRegistry ? s_mediaStreamRegistry->contains(url) : false;
231 HTMLMediaElement::HTMLMediaElement(const QualifiedName& tagName, Document& document)
232 : HTMLElement(tagName, document)
233 , ActiveDOMObject(&document)
234 , m_loadTimer(this, &HTMLMediaElement::loadTimerFired)
235 , m_progressEventTimer(this, &HTMLMediaElement::progressEventTimerFired)
236 , m_playbackProgressTimer(this, &HTMLMediaElement::playbackProgressTimerFired)
237 , m_playedTimeRanges()
238 , m_asyncEventQueue(GenericEventQueue::create(this))
239 , m_playbackRate(1.0f)
240 , m_defaultPlaybackRate(1.0f)
241 , m_networkState(NETWORK_EMPTY)
242 , m_readyState(HAVE_NOTHING)
243 , m_readyStateMaximum(HAVE_NOTHING)
246 , m_previousProgressTime(numeric_limits<double>::max())
247 , m_duration(numeric_limits<double>::quiet_NaN())
248 , m_lastTimeUpdateEventWallTime(0)
249 , m_lastTimeUpdateEventMovieTime(numeric_limits<double>::max())
250 , m_loadState(WaitingForSource)
253 , m_preload(MediaPlayer::Auto)
254 , m_displayMode(Unknown)
255 , m_cachedTime(MediaPlayer::invalidTime())
256 , m_cachedTimeWallClockUpdateTime(0)
257 , m_minimumWallClockTimeToCacheMediaTime(0)
258 , m_fragmentStartTime(MediaPlayer::invalidTime())
259 , m_fragmentEndTime(MediaPlayer::invalidTime())
260 , m_pendingActionFlags(0)
261 , m_userGestureRequiredForPlay(false)
263 , m_shouldDelayLoadEvent(false)
264 , m_haveFiredLoadedData(false)
266 , m_autoplaying(true)
270 , m_sentStalledEvent(false)
271 , m_sentEndEvent(false)
272 , m_pausedInternal(false)
273 , m_closedCaptionsVisible(false)
274 , m_completelyLoaded(false)
275 , m_havePreparedToPlay(false)
276 , m_delayingLoadForPreloadNone(false)
277 , m_tracksAreReady(true)
278 , m_haveVisibleTextTrack(false)
279 , m_processingPreferenceChange(false)
280 , m_lastTextTrackUpdateTime(-1)
281 , m_textTracks(nullptr)
282 , m_ignoreTrackDisplayUpdate(0)
283 #if ENABLE(WEB_AUDIO)
284 , m_audioSourceNode(0)
287 ASSERT(RuntimeEnabledFeatures::mediaEnabled());
289 WTF_LOG(Media, "HTMLMediaElement::HTMLMediaElement");
290 ScriptWrappable::init(this);
292 if (document.settings() && document.settings()->mediaPlaybackRequiresUserGesture())
293 m_userGestureRequiredForPlay = true;
295 // We must always have a ShadowRoot so children like <source> will not render
296 // as they never have an insertion point.
297 ensureUserAgentShadowRoot();
298 setHasCustomStyleCallbacks();
299 addElementToDocumentMap(this, &document);
302 HTMLMediaElement::~HTMLMediaElement()
304 WTF_LOG(Media, "HTMLMediaElement::~HTMLMediaElement");
306 m_asyncEventQueue->close();
308 setShouldDelayLoadEvent(false);
311 m_textTracks->clearOwner();
313 if (m_mediaController) {
314 m_mediaController->removeMediaElement(this);
315 m_mediaController = nullptr;
320 removeElementFromDocumentMap(this, &document());
322 // Destroying the player may cause a resource load to be canceled,
323 // which could result in userCancelledLoad() being called back.
324 // Setting m_completelyLoaded ensures that such a call will not cause
325 // us to dispatch an abort event, which would result in a crash.
326 // See http://crbug.com/233654 for more details.
327 m_completelyLoaded = true;
329 // Destroying the player may cause a resource load to be canceled,
330 // which could result in Document::dispatchWindowLoadEvent() being
331 // called via ResourceFetch::didLoadResource() then
332 // FrameLoader::loadDone(). To prevent load event dispatching during
333 // object destruction, we use Document::incrementLoadEventDelayCount().
334 // See http://crbug.com/275223 for more details.
335 document().incrementLoadEventDelayCount();
337 clearMediaPlayerAndAudioSourceProviderClient();
339 document().decrementLoadEventDelayCount();
342 void HTMLMediaElement::didMoveToNewDocument(Document& oldDocument)
344 WTF_LOG(Media, "HTMLMediaElement::didMoveToNewDocument");
346 if (m_shouldDelayLoadEvent) {
347 document().incrementLoadEventDelayCount();
348 // Note: Keeping the load event delay count increment on oldDocument that was added
349 // when m_shouldDelayLoadEvent was set so that destruction of m_player can not
350 // cause load event dispatching in oldDocument.
352 // Incrementing the load event delay count so that destruction of m_player can not
353 // cause load event dispatching in oldDocument.
354 oldDocument.incrementLoadEventDelayCount();
357 removeElementFromDocumentMap(this, &oldDocument);
358 addElementToDocumentMap(this, &document());
360 // FIXME: This is a temporary fix to prevent this object from causing the
361 // MediaPlayer to dereference LocalFrame and FrameLoader pointers from the
362 // previous document. A proper fix would provide a mechanism to allow this
363 // object to refresh the MediaPlayer's LocalFrame and FrameLoader references on
364 // document changes so that playback can be resumed properly.
367 // Decrement the load event delay count on oldDocument now that m_player has been destroyed
368 // and there is no risk of dispatching a load event from within the destructor.
369 oldDocument.decrementLoadEventDelayCount();
371 ActiveDOMObject::didMoveToNewExecutionContext(&document());
372 HTMLElement::didMoveToNewDocument(oldDocument);
375 bool HTMLMediaElement::hasCustomFocusLogic() const
380 bool HTMLMediaElement::supportsFocus() const
382 if (ownerDocument()->isMediaDocument())
385 // If no controls specified, we should still be able to focus the element if it has tabIndex.
386 return controls() || HTMLElement::supportsFocus();
389 bool HTMLMediaElement::isMouseFocusable() const
394 void HTMLMediaElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
396 if (name == srcAttr) {
397 // Trigger a reload, as long as the 'src' attribute is present.
398 if (!value.isNull()) {
399 clearMediaPlayer(LoadMediaResource);
400 scheduleDelayedAction(LoadMediaResource);
402 } else if (name == controlsAttr) {
403 configureMediaControls();
404 } else if (name == preloadAttr) {
405 if (equalIgnoringCase(value, "none"))
406 m_preload = MediaPlayer::None;
407 else if (equalIgnoringCase(value, "metadata"))
408 m_preload = MediaPlayer::MetaData;
410 // The spec does not define an "invalid value default" but "auto" is suggested as the
411 // "missing value default", so use it for everything except "none" and "metadata"
412 m_preload = MediaPlayer::Auto;
415 // The attribute must be ignored if the autoplay attribute is present
416 if (!autoplay() && m_player)
419 } else if (name == mediagroupAttr) {
420 setMediaGroup(value);
422 HTMLElement::parseAttribute(name, value);
426 void HTMLMediaElement::finishParsingChildren()
428 HTMLElement::finishParsingChildren();
430 if (!RuntimeEnabledFeatures::videoTrackEnabled())
433 if (Traversal<HTMLTrackElement>::firstChild(*this))
434 scheduleDelayedAction(LoadTextTrackResource);
437 bool HTMLMediaElement::rendererIsNeeded(const RenderStyle& style)
439 return controls() ? HTMLElement::rendererIsNeeded(style) : false;
442 RenderObject* HTMLMediaElement::createRenderer(RenderStyle*)
444 return new RenderMedia(this);
447 Node::InsertionNotificationRequest HTMLMediaElement::insertedInto(ContainerNode* insertionPoint)
449 WTF_LOG(Media, "HTMLMediaElement::insertedInto");
451 HTMLElement::insertedInto(insertionPoint);
452 if (insertionPoint->inDocument()) {
455 if (!getAttribute(srcAttr).isEmpty() && m_networkState == NETWORK_EMPTY)
456 scheduleDelayedAction(LoadMediaResource);
459 configureMediaControls();
460 return InsertionDone;
463 void HTMLMediaElement::removedFrom(ContainerNode* insertionPoint)
465 WTF_LOG(Media, "HTMLMediaElement::removedFrom");
468 if (insertionPoint->inDocument() && insertionPoint->document().isActive()) {
469 configureMediaControls();
470 if (m_networkState > NETWORK_EMPTY)
474 HTMLElement::removedFrom(insertionPoint);
477 void HTMLMediaElement::attach(const AttachContext& context)
479 HTMLElement::attach(context);
482 renderer()->updateFromElement();
485 void HTMLMediaElement::didRecalcStyle(StyleRecalcChange)
488 renderer()->updateFromElement();
491 void HTMLMediaElement::scheduleDelayedAction(DelayedActionType actionType)
493 WTF_LOG(Media, "HTMLMediaElement::scheduleDelayedAction");
495 if ((actionType & LoadMediaResource) && !(m_pendingActionFlags & LoadMediaResource)) {
497 m_pendingActionFlags |= LoadMediaResource;
500 if (RuntimeEnabledFeatures::videoTrackEnabled() && (actionType & LoadTextTrackResource))
501 m_pendingActionFlags |= LoadTextTrackResource;
503 if (!m_loadTimer.isActive())
504 m_loadTimer.startOneShot(0, FROM_HERE);
507 void HTMLMediaElement::scheduleNextSourceChild()
509 // Schedule the timer to try the next <source> element WITHOUT resetting state ala prepareForLoad.
510 m_pendingActionFlags |= LoadMediaResource;
511 m_loadTimer.startOneShot(0, FROM_HERE);
514 void HTMLMediaElement::scheduleEvent(const AtomicString& eventName)
516 scheduleEvent(Event::createCancelable(eventName));
519 void HTMLMediaElement::scheduleEvent(PassRefPtr<Event> event)
522 WTF_LOG(Media, "HTMLMediaElement::scheduleEvent - scheduling '%s'", event->type().ascii().data());
524 m_asyncEventQueue->enqueueEvent(event);
527 void HTMLMediaElement::loadTimerFired(Timer<HTMLMediaElement>*)
529 if (RuntimeEnabledFeatures::videoTrackEnabled() && (m_pendingActionFlags & LoadTextTrackResource))
530 configureTextTracks();
532 if (m_pendingActionFlags & LoadMediaResource) {
533 if (m_loadState == LoadingFromSourceElement)
534 loadNextSourceChild();
539 m_pendingActionFlags = 0;
542 PassRefPtr<MediaError> HTMLMediaElement::error() const
547 void HTMLMediaElement::setSrc(const AtomicString& url)
549 setAttribute(srcAttr, url);
552 HTMLMediaElement::NetworkState HTMLMediaElement::networkState() const
554 return m_networkState;
557 String HTMLMediaElement::canPlayType(const String& mimeType, const String& keySystem) const
559 if (!keySystem.isNull())
560 UseCounter::count(document(), UseCounter::CanPlayTypeKeySystem);
562 WebMimeRegistry::SupportsType support = supportsType(ContentType(mimeType), keySystem);
568 case WebMimeRegistry::IsNotSupported:
569 canPlay = emptyString();
571 case WebMimeRegistry::MayBeSupported:
574 case WebMimeRegistry::IsSupported:
575 canPlay = "probably";
579 WTF_LOG(Media, "HTMLMediaElement::canPlayType(%s, %s) -> %s", mimeType.utf8().data(), keySystem.utf8().data(), canPlay.utf8().data());
584 void HTMLMediaElement::load()
586 WTF_LOG(Media, "HTMLMediaElement::load()");
588 if (UserGestureIndicator::processingUserGesture())
589 m_userGestureRequiredForPlay = false;
596 void HTMLMediaElement::prepareForLoad()
598 WTF_LOG(Media, "HTMLMediaElement::prepareForLoad");
600 // Perform the cleanup required for the resource load algorithm to run.
601 stopPeriodicTimers();
603 // FIXME: Figure out appropriate place to reset LoadTextTrackResource if necessary and set m_pendingActionFlags to 0 here.
604 m_pendingActionFlags &= ~LoadMediaResource;
605 m_sentEndEvent = false;
606 m_sentStalledEvent = false;
607 m_haveFiredLoadedData = false;
608 m_completelyLoaded = false;
609 m_havePreparedToPlay = false;
610 m_displayMode = Unknown;
612 // 1 - Abort any already-running instance of the resource selection algorithm for this element.
613 m_loadState = WaitingForSource;
614 m_currentSourceNode = nullptr;
616 // 2 - If there are any tasks from the media element's media element event task source in
617 // one of the task queues, then remove those tasks.
618 cancelPendingEventsAndCallbacks();
620 // 3 - If the media element's networkState is set to NETWORK_LOADING or NETWORK_IDLE, queue
621 // a task to fire a simple event named abort at the media element.
622 if (m_networkState == NETWORK_LOADING || m_networkState == NETWORK_IDLE)
623 scheduleEvent(EventTypeNames::abort);
629 // 4 - If the media element's networkState is not set to NETWORK_EMPTY, then run these substeps
630 if (m_networkState != NETWORK_EMPTY) {
631 // 4.1 - Queue a task to fire a simple event named emptied at the media element.
632 scheduleEvent(EventTypeNames::emptied);
634 // 4.2 - If a fetching process is in progress for the media element, the user agent should stop it.
635 m_networkState = NETWORK_EMPTY;
637 // 4.3 - Forget the media element's media-resource-specific tracks.
638 forgetResourceSpecificTracks();
640 // 4.4 - If readyState is not set to HAVE_NOTHING, then set it to that state.
641 m_readyState = HAVE_NOTHING;
642 m_readyStateMaximum = HAVE_NOTHING;
644 // 4.5 - If the paused attribute is false, then set it to true.
647 // 4.6 - If seeking is true, set it to false.
650 // 4.7 - Set the current playback position to 0.
651 // Set the official playback position to 0.
652 // If this changed the official playback position, then queue a task to fire a simple event named timeupdate at the media element.
653 // FIXME: Add support for firing this event.
655 // 4.8 - Set the initial playback position to 0.
656 // FIXME: Make this less subtle. The position only becomes 0 because of the createMediaPlayer() call
659 invalidateCachedTime();
661 // 4.9 - Set the timeline offset to Not-a-Number (NaN).
662 // 4.10 - Update the duration attribute to Not-a-Number (NaN).
665 updateMediaController();
666 if (RuntimeEnabledFeatures::videoTrackEnabled())
667 updateActiveTextTrackCues(0);
670 // 5 - Set the playbackRate attribute to the value of the defaultPlaybackRate attribute.
671 setPlaybackRate(defaultPlaybackRate());
673 // 6 - Set the error attribute to null and the autoplaying flag to true.
675 m_autoplaying = true;
677 // 7 - Invoke the media element's resource selection algorithm.
679 // 8 - Note: Playback of any previously playing media resource for this element stops.
681 // The resource selection algorithm
682 // 1 - Set the networkState to NETWORK_NO_SOURCE
683 m_networkState = NETWORK_NO_SOURCE;
685 // 2 - Asynchronously await a stable state.
687 m_playedTimeRanges = TimeRanges::create();
689 // FIXME: Investigate whether these can be moved into m_networkState != NETWORK_EMPTY block above
690 // so they are closer to the relevant spec steps.
692 m_duration = numeric_limits<double>::quiet_NaN();
694 // The spec doesn't say to block the load event until we actually run the asynchronous section
695 // algorithm, but do it now because we won't start that until after the timer fires and the
696 // event may have already fired by then.
697 setShouldDelayLoadEvent(true);
699 configureMediaControls();
702 void HTMLMediaElement::loadInternal()
704 // HTMLMediaElement::textTracksAreReady will need "... the text tracks whose mode was not in the
705 // disabled state when the element's resource selection algorithm last started".
706 if (RuntimeEnabledFeatures::videoTrackEnabled()) {
707 m_textTracksWhenResourceSelectionBegan.clear();
709 for (unsigned i = 0; i < m_textTracks->length(); ++i) {
710 TextTrack* track = m_textTracks->item(i);
711 if (track->mode() != TextTrack::disabledKeyword())
712 m_textTracksWhenResourceSelectionBegan.append(track);
717 selectMediaResource();
720 void HTMLMediaElement::selectMediaResource()
722 WTF_LOG(Media, "HTMLMediaElement::selectMediaResource");
724 enum Mode { attribute, children };
726 // 3 - If the media element has a src attribute, then let mode be attribute.
727 Mode mode = attribute;
728 if (!fastHasAttribute(srcAttr)) {
729 // Otherwise, if the media element does not have a src attribute but has a source
730 // element child, then let mode be children and let candidate be the first such
731 // source element child in tree order.
732 if (HTMLSourceElement* element = Traversal<HTMLSourceElement>::firstChild(*this)) {
734 m_nextChildNodeToConsider = element;
735 m_currentSourceNode = nullptr;
737 // Otherwise the media element has neither a src attribute nor a source element
738 // child: set the networkState to NETWORK_EMPTY, and abort these steps; the
739 // synchronous section ends.
740 m_loadState = WaitingForSource;
741 setShouldDelayLoadEvent(false);
742 m_networkState = NETWORK_EMPTY;
744 WTF_LOG(Media, "HTMLMediaElement::selectMediaResource, nothing to load");
749 // 4 - Set the media element's delaying-the-load-event flag to true (this delays the load event),
750 // and set its networkState to NETWORK_LOADING.
751 setShouldDelayLoadEvent(true);
752 m_networkState = NETWORK_LOADING;
754 // 5 - Queue a task to fire a simple event named loadstart at the media element.
755 scheduleEvent(EventTypeNames::loadstart);
757 // 6 - If mode is attribute, then run these substeps
758 if (mode == attribute) {
759 m_loadState = LoadingFromSrcAttr;
761 // If the src attribute's value is the empty string ... jump down to the failed step below
762 KURL mediaURL = getNonEmptyURLAttribute(srcAttr);
763 if (mediaURL.isEmpty()) {
764 mediaLoadingFailed(MediaPlayer::FormatError);
765 WTF_LOG(Media, "HTMLMediaElement::selectMediaResource, empty 'src'");
769 if (!isSafeToLoadURL(mediaURL, Complain)) {
770 mediaLoadingFailed(MediaPlayer::FormatError);
774 // No type or key system information is available when the url comes
775 // from the 'src' attribute so MediaPlayer
776 // will have to pick a media engine based on the file extension.
777 ContentType contentType((String()));
778 loadResource(mediaURL, contentType, String());
779 WTF_LOG(Media, "HTMLMediaElement::selectMediaResource, using 'src' attribute url");
783 // Otherwise, the source elements will be used
784 loadNextSourceChild();
787 void HTMLMediaElement::loadNextSourceChild()
789 ContentType contentType((String()));
791 KURL mediaURL = selectNextSourceChild(&contentType, &keySystem, Complain);
792 if (!mediaURL.isValid()) {
793 waitForSourceChange();
797 // Recreate the media player for the new url
800 m_loadState = LoadingFromSourceElement;
801 loadResource(mediaURL, contentType, keySystem);
804 void HTMLMediaElement::loadResource(const KURL& url, ContentType& contentType, const String& keySystem)
806 ASSERT(isSafeToLoadURL(url, Complain));
808 WTF_LOG(Media, "HTMLMediaElement::loadResource(%s, %s, %s)", urlForLoggingMedia(url).utf8().data(), contentType.raw().utf8().data(), keySystem.utf8().data());
810 LocalFrame* frame = document().frame();
812 mediaLoadingFailed(MediaPlayer::FormatError);
816 // The resource fetch algorithm
817 m_networkState = NETWORK_LOADING;
819 // Set m_currentSrc *before* changing to the cache url, the fact that we are loading from the app
820 // cache is an internal detail not exposed through the media element API.
823 WTF_LOG(Media, "HTMLMediaElement::loadResource - m_currentSrc -> %s", urlForLoggingMedia(m_currentSrc).utf8().data());
825 startProgressEventTimer();
827 // Reset display mode to force a recalculation of what to show because we are resetting the player.
828 setDisplayMode(Unknown);
833 if (fastHasAttribute(mutedAttr))
837 ASSERT(!m_mediaSource);
839 bool attemptLoad = true;
841 if (url.protocolIs(mediaSourceBlobProtocol)) {
842 if (isMediaStreamURL(url.string())) {
843 m_userGestureRequiredForPlay = false;
845 m_mediaSource = HTMLMediaSource::lookup(url.string());
848 if (!m_mediaSource->attachToElement(this)) {
849 // Forget our reference to the MediaSource, so we leave it alone
850 // while processing remainder of load failure.
851 m_mediaSource = nullptr;
858 if (attemptLoad && canLoadURL(url, contentType, keySystem)) {
859 ASSERT(!webMediaPlayer());
861 if (m_preload == MediaPlayer::None) {
862 m_delayingLoadForPreloadNone = true;
864 m_player->load(loadType(), m_currentSrc, corsMode());
867 mediaLoadingFailed(MediaPlayer::FormatError);
870 // If there is no poster to display, allow the media engine to render video frames as soon as
871 // they are available.
872 updateDisplayState();
875 renderer()->updateFromElement();
878 void HTMLMediaElement::setPlayerPreload()
880 m_player->setPreload(m_preload);
882 if (m_delayingLoadForPreloadNone && m_preload != MediaPlayer::None)
886 void HTMLMediaElement::startDelayedLoad()
888 ASSERT(m_delayingLoadForPreloadNone);
890 m_delayingLoadForPreloadNone = false;
892 m_player->load(loadType(), m_currentSrc, corsMode());
895 WebMediaPlayer::LoadType HTMLMediaElement::loadType() const
898 return WebMediaPlayer::LoadTypeMediaSource;
900 if (isMediaStreamURL(m_currentSrc.string()))
901 return WebMediaPlayer::LoadTypeMediaStream;
903 return WebMediaPlayer::LoadTypeURL;
906 static bool trackIndexCompare(TextTrack* a,
909 return a->trackIndex() - b->trackIndex() < 0;
912 static bool eventTimeCueCompare(const std::pair<double, TextTrackCue*>& a,
913 const std::pair<double, TextTrackCue*>& b)
915 // 12 - Sort the tasks in events in ascending time order (tasks with earlier
917 if (a.first != b.first)
918 return a.first - b.first < 0;
920 // If the cues belong to different text tracks, it doesn't make sense to
921 // compare the two tracks by the relative cue order, so return the relative
923 if (a.second->track() != b.second->track())
924 return trackIndexCompare(a.second->track(), b.second->track());
926 // 12 - Further sort tasks in events that have the same time by the
927 // relative text track cue order of the text track cues associated
929 return a.second->cueIndex() - b.second->cueIndex() < 0;
933 void HTMLMediaElement::updateActiveTextTrackCues(double movieTime)
935 // 4.8.10.8 Playing the media resource
937 // If the current playback position changes while the steps are running,
938 // then the user agent must wait for the steps to complete, and then must
939 // immediately rerun the steps.
940 if (ignoreTrackDisplayUpdateRequests())
943 // 1 - Let current cues be a list of cues, initialized to contain all the
944 // cues of all the hidden, showing, or showing by default text tracks of the
945 // media element (not the disabled ones) whose start times are less than or
946 // equal to the current playback position and whose end times are greater
947 // than the current playback position.
950 // The user agent must synchronously unset [the text track cue active] flag
951 // whenever ... the media element's readyState is changed back to HAVE_NOTHING.
952 if (m_readyState != HAVE_NOTHING && m_player)
953 currentCues = m_cueTree.allOverlaps(m_cueTree.createInterval(movieTime, movieTime));
955 CueList previousCues;
958 // 2 - Let other cues be a list of cues, initialized to contain all the cues
959 // of hidden, showing, and showing by default text tracks of the media
960 // element that are not present in current cues.
961 previousCues = m_currentlyActiveCues;
963 // 3 - Let last time be the current playback position at the time this
964 // algorithm was last run for this media element, if this is not the first
966 double lastTime = m_lastTextTrackUpdateTime;
968 // 4 - If the current playback position has, since the last time this
969 // algorithm was run, only changed through its usual monotonic increase
970 // during normal playback, then let missed cues be the list of cues in other
971 // cues whose start times are greater than or equal to last time and whose
972 // end times are less than or equal to the current playback position.
973 // Otherwise, let missed cues be an empty list.
974 if (lastTime >= 0 && m_lastSeekTime < movieTime) {
975 CueList potentiallySkippedCues =
976 m_cueTree.allOverlaps(m_cueTree.createInterval(lastTime, movieTime));
978 for (size_t i = 0; i < potentiallySkippedCues.size(); ++i) {
979 double cueStartTime = potentiallySkippedCues[i].low();
980 double cueEndTime = potentiallySkippedCues[i].high();
982 // Consider cues that may have been missed since the last seek time.
983 if (cueStartTime > max(m_lastSeekTime, lastTime) && cueEndTime < movieTime)
984 missedCues.append(potentiallySkippedCues[i]);
988 m_lastTextTrackUpdateTime = movieTime;
990 // 5 - If the time was reached through the usual monotonic increase of the
991 // current playback position during normal playback, and if the user agent
992 // has not fired a timeupdate event at the element in the past 15 to 250ms
993 // and is not still running event handlers for such an event, then the user
994 // agent must queue a task to fire a simple event named timeupdate at the
995 // element. (In the other cases, such as explicit seeks, relevant events get
996 // fired as part of the overall process of changing the current playback
998 if (!m_seeking && m_lastSeekTime <= lastTime)
999 scheduleTimeupdateEvent(true);
1001 // Explicitly cache vector sizes, as their content is constant from here.
1002 size_t currentCuesSize = currentCues.size();
1003 size_t missedCuesSize = missedCues.size();
1004 size_t previousCuesSize = previousCues.size();
1006 // 6 - If all of the cues in current cues have their text track cue active
1007 // flag set, none of the cues in other cues have their text track cue active
1008 // flag set, and missed cues is empty, then abort these steps.
1009 bool activeSetChanged = missedCuesSize;
1011 for (size_t i = 0; !activeSetChanged && i < previousCuesSize; ++i)
1012 if (!currentCues.contains(previousCues[i]) && previousCues[i].data()->isActive())
1013 activeSetChanged = true;
1015 for (size_t i = 0; i < currentCuesSize; ++i) {
1016 currentCues[i].data()->updateDisplayTree(movieTime);
1018 if (!currentCues[i].data()->isActive())
1019 activeSetChanged = true;
1022 if (!activeSetChanged)
1025 // 7 - If the time was reached through the usual monotonic increase of the
1026 // current playback position during normal playback, and there are cues in
1027 // other cues that have their text track cue pause-on-exi flag set and that
1028 // either have their text track cue active flag set or are also in missed
1029 // cues, then immediately pause the media element.
1030 for (size_t i = 0; !m_paused && i < previousCuesSize; ++i) {
1031 if (previousCues[i].data()->pauseOnExit()
1032 && previousCues[i].data()->isActive()
1033 && !currentCues.contains(previousCues[i]))
1037 for (size_t i = 0; !m_paused && i < missedCuesSize; ++i) {
1038 if (missedCues[i].data()->pauseOnExit())
1042 // 8 - Let events be a list of tasks, initially empty. Each task in this
1043 // list will be associated with a text track, a text track cue, and a time,
1044 // which are used to sort the list before the tasks are queued.
1045 Vector<std::pair<double, TextTrackCue*> > eventTasks;
1047 // 8 - Let affected tracks be a list of text tracks, initially empty.
1048 Vector<TextTrack*> affectedTracks;
1050 for (size_t i = 0; i < missedCuesSize; ++i) {
1051 // 9 - For each text track cue in missed cues, prepare an event named enter
1052 // for the TextTrackCue object with the text track cue start time.
1053 eventTasks.append(std::make_pair(missedCues[i].data()->startTime(),
1054 missedCues[i].data()));
1056 // 10 - For each text track [...] in missed cues, prepare an event
1057 // named exit for the TextTrackCue object with the with the later of
1058 // the text track cue end time and the text track cue start time.
1060 // Note: An explicit task is added only if the cue is NOT a zero or
1061 // negative length cue. Otherwise, the need for an exit event is
1062 // checked when these tasks are actually queued below. This doesn't
1063 // affect sorting events before dispatch either, because the exit
1064 // event has the same time as the enter event.
1065 if (missedCues[i].data()->startTime() < missedCues[i].data()->endTime())
1066 eventTasks.append(std::make_pair(missedCues[i].data()->endTime(),
1067 missedCues[i].data()));
1070 for (size_t i = 0; i < previousCuesSize; ++i) {
1071 // 10 - For each text track cue in other cues that has its text
1072 // track cue active flag set prepare an event named exit for the
1073 // TextTrackCue object with the text track cue end time.
1074 if (!currentCues.contains(previousCues[i]))
1075 eventTasks.append(std::make_pair(previousCues[i].data()->endTime(),
1076 previousCues[i].data()));
1079 for (size_t i = 0; i < currentCuesSize; ++i) {
1080 // 11 - For each text track cue in current cues that does not have its
1081 // text track cue active flag set, prepare an event named enter for the
1082 // TextTrackCue object with the text track cue start time.
1083 if (!previousCues.contains(currentCues[i]))
1084 eventTasks.append(std::make_pair(currentCues[i].data()->startTime(),
1085 currentCues[i].data()));
1088 // 12 - Sort the tasks in events in ascending time order (tasks with earlier
1090 nonCopyingSort(eventTasks.begin(), eventTasks.end(), eventTimeCueCompare);
1092 for (size_t i = 0; i < eventTasks.size(); ++i) {
1093 if (!affectedTracks.contains(eventTasks[i].second->track()))
1094 affectedTracks.append(eventTasks[i].second->track());
1096 // 13 - Queue each task in events, in list order.
1097 RefPtr<Event> event;
1099 // Each event in eventTasks may be either an enterEvent or an exitEvent,
1100 // depending on the time that is associated with the event. This
1101 // correctly identifies the type of the event, if the startTime is
1102 // less than the endTime in the cue.
1103 if (eventTasks[i].second->startTime() >= eventTasks[i].second->endTime()) {
1104 event = Event::create(EventTypeNames::enter);
1105 event->setTarget(eventTasks[i].second);
1106 m_asyncEventQueue->enqueueEvent(event.release());
1108 event = Event::create(EventTypeNames::exit);
1109 event->setTarget(eventTasks[i].second);
1110 m_asyncEventQueue->enqueueEvent(event.release());
1112 if (eventTasks[i].first == eventTasks[i].second->startTime())
1113 event = Event::create(EventTypeNames::enter);
1115 event = Event::create(EventTypeNames::exit);
1117 event->setTarget(eventTasks[i].second);
1118 m_asyncEventQueue->enqueueEvent(event.release());
1122 // 14 - Sort affected tracks in the same order as the text tracks appear in
1123 // the media element's list of text tracks, and remove duplicates.
1124 nonCopyingSort(affectedTracks.begin(), affectedTracks.end(), trackIndexCompare);
1126 // 15 - For each text track in affected tracks, in the list order, queue a
1127 // task to fire a simple event named cuechange at the TextTrack object, and, ...
1128 for (size_t i = 0; i < affectedTracks.size(); ++i) {
1129 RefPtr<Event> event = Event::create(EventTypeNames::cuechange);
1130 event->setTarget(affectedTracks[i]);
1132 m_asyncEventQueue->enqueueEvent(event.release());
1134 // ... if the text track has a corresponding track element, to then fire a
1135 // simple event named cuechange at the track element as well.
1136 if (affectedTracks[i]->trackType() == TextTrack::TrackElement) {
1137 RefPtr<Event> event = Event::create(EventTypeNames::cuechange);
1138 HTMLTrackElement* trackElement = static_cast<LoadableTextTrack*>(affectedTracks[i])->trackElement();
1139 ASSERT(trackElement);
1140 event->setTarget(trackElement);
1142 m_asyncEventQueue->enqueueEvent(event.release());
1146 // 16 - Set the text track cue active flag of all the cues in the current
1147 // cues, and unset the text track cue active flag of all the cues in the
1149 for (size_t i = 0; i < currentCuesSize; ++i)
1150 currentCues[i].data()->setIsActive(true);
1152 for (size_t i = 0; i < previousCuesSize; ++i)
1153 if (!currentCues.contains(previousCues[i]))
1154 previousCues[i].data()->setIsActive(false);
1156 // Update the current active cues.
1157 m_currentlyActiveCues = currentCues;
1159 if (activeSetChanged)
1160 updateTextTrackDisplay();
1163 bool HTMLMediaElement::textTracksAreReady() const
1165 // 4.8.10.12.1 Text track model
1167 // The text tracks of a media element are ready if all the text tracks whose mode was not
1168 // in the disabled state when the element's resource selection algorithm last started now
1169 // have a text track readiness state of loaded or failed to load.
1170 for (unsigned i = 0; i < m_textTracksWhenResourceSelectionBegan.size(); ++i) {
1171 if (m_textTracksWhenResourceSelectionBegan[i]->readinessState() == TextTrack::Loading
1172 || m_textTracksWhenResourceSelectionBegan[i]->readinessState() == TextTrack::NotLoaded)
1179 void HTMLMediaElement::textTrackReadyStateChanged(TextTrack* track)
1181 if (m_player && m_textTracksWhenResourceSelectionBegan.contains(track)) {
1182 if (track->readinessState() != TextTrack::Loading)
1183 setReadyState(m_player->readyState());
1185 // The track readiness state might have changed as a result of the user
1186 // clicking the captions button. In this case, a check whether all the
1187 // resources have failed loading should be done in order to hide the CC button.
1188 if (hasMediaControls() && track->readinessState() == TextTrack::FailedToLoad)
1189 mediaControls()->refreshClosedCaptionsButtonVisibility();
1193 void HTMLMediaElement::textTrackModeChanged(TextTrack* track)
1195 if (track->trackType() == TextTrack::TrackElement) {
1196 // 4.8.10.12.3 Sourcing out-of-band text tracks
1197 // ... when a text track corresponding to a track element is created with text track
1198 // mode set to disabled and subsequently changes its text track mode to hidden, showing,
1199 // or showing by default for the first time, the user agent must immediately and synchronously
1200 // run the following algorithm ...
1202 for (HTMLTrackElement* trackElement = Traversal<HTMLTrackElement>::firstChild(*this); trackElement; trackElement = Traversal<HTMLTrackElement>::nextSibling(*trackElement)) {
1203 if (trackElement->track() != track)
1206 // Mark this track as "configured" so configureTextTracks won't change the mode again.
1207 track->setHasBeenConfigured(true);
1208 if (track->mode() != TextTrack::disabledKeyword()) {
1209 if (trackElement->readyState() == HTMLTrackElement::LOADED)
1210 textTrackAddCues(track, track->cues());
1212 // If this is the first added track, create the list of text tracks.
1214 m_textTracks = TextTrackList::create(this);
1218 } else if (track->trackType() == TextTrack::AddTrack && track->mode() != TextTrack::disabledKeyword()) {
1219 textTrackAddCues(track, track->cues());
1222 configureTextTrackDisplay(AssumeVisibleChange);
1224 ASSERT(textTracks()->contains(track));
1225 textTracks()->scheduleChangeEvent();
1228 void HTMLMediaElement::textTrackKindChanged(TextTrack* track)
1230 if (track->kind() != TextTrack::captionsKeyword() && track->kind() != TextTrack::subtitlesKeyword() && track->mode() == TextTrack::showingKeyword())
1231 track->setMode(TextTrack::hiddenKeyword());
1234 void HTMLMediaElement::beginIgnoringTrackDisplayUpdateRequests()
1236 ++m_ignoreTrackDisplayUpdate;
1239 void HTMLMediaElement::endIgnoringTrackDisplayUpdateRequests()
1241 ASSERT(m_ignoreTrackDisplayUpdate);
1242 --m_ignoreTrackDisplayUpdate;
1243 if (!m_ignoreTrackDisplayUpdate && m_active)
1244 updateActiveTextTrackCues(currentTime());
1247 void HTMLMediaElement::textTrackAddCues(TextTrack* track, const TextTrackCueList* cues)
1249 WTF_LOG(Media, "HTMLMediaElement::textTrackAddCues");
1250 if (track->mode() == TextTrack::disabledKeyword())
1253 TrackDisplayUpdateScope scope(this);
1254 for (size_t i = 0; i < cues->length(); ++i)
1255 textTrackAddCue(cues->item(i)->track(), cues->item(i));
1258 void HTMLMediaElement::textTrackRemoveCues(TextTrack*, const TextTrackCueList* cues)
1260 WTF_LOG(Media, "HTMLMediaElement::textTrackRemoveCues");
1262 TrackDisplayUpdateScope scope(this);
1263 for (size_t i = 0; i < cues->length(); ++i)
1264 textTrackRemoveCue(cues->item(i)->track(), cues->item(i));
1267 void HTMLMediaElement::textTrackAddCue(TextTrack* track, PassRefPtr<TextTrackCue> cue)
1269 if (track->mode() == TextTrack::disabledKeyword())
1272 // Negative duration cues need be treated in the interval tree as
1273 // zero-length cues.
1274 double endTime = max(cue->startTime(), cue->endTime());
1276 CueInterval interval = m_cueTree.createInterval(cue->startTime(), endTime, cue.get());
1277 if (!m_cueTree.contains(interval))
1278 m_cueTree.add(interval);
1279 updateActiveTextTrackCues(currentTime());
1282 void HTMLMediaElement::textTrackRemoveCue(TextTrack*, PassRefPtr<TextTrackCue> cue)
1284 // Negative duration cues need to be treated in the interval tree as
1285 // zero-length cues.
1286 double endTime = max(cue->startTime(), cue->endTime());
1288 CueInterval interval = m_cueTree.createInterval(cue->startTime(), endTime, cue.get());
1289 m_cueTree.remove(interval);
1291 // Since the cue will be removed from the media element and likely the
1292 // TextTrack might also be destructed, notifying the region of the cue
1293 // removal shouldn't be done.
1294 cue->notifyRegionWhenRemovingDisplayTree(false);
1296 size_t index = m_currentlyActiveCues.find(interval);
1297 if (index != kNotFound) {
1298 m_currentlyActiveCues.remove(index);
1299 cue->setIsActive(false);
1301 cue->removeDisplayTree();
1302 updateActiveTextTrackCues(currentTime());
1304 cue->notifyRegionWhenRemovingDisplayTree(true);
1308 bool HTMLMediaElement::isSafeToLoadURL(const KURL& url, InvalidURLAction actionIfInvalid)
1310 if (!url.isValid()) {
1311 WTF_LOG(Media, "HTMLMediaElement::isSafeToLoadURL(%s) -> FALSE because url is invalid", urlForLoggingMedia(url).utf8().data());
1315 LocalFrame* frame = document().frame();
1316 if (!frame || !document().securityOrigin()->canDisplay(url)) {
1317 if (actionIfInvalid == Complain)
1318 FrameLoader::reportLocalLoadFailed(frame, url.elidedString());
1319 WTF_LOG(Media, "HTMLMediaElement::isSafeToLoadURL(%s) -> FALSE rejected by SecurityOrigin", urlForLoggingMedia(url).utf8().data());
1323 if (!document().contentSecurityPolicy()->allowMediaFromSource(url)) {
1324 WTF_LOG(Media, "HTMLMediaElement::isSafeToLoadURL(%s) -> rejected by Content Security Policy", urlForLoggingMedia(url).utf8().data());
1331 void HTMLMediaElement::startProgressEventTimer()
1333 if (m_progressEventTimer.isActive())
1336 m_previousProgressTime = WTF::currentTime();
1337 // 350ms is not magic, it is in the spec!
1338 m_progressEventTimer.startRepeating(0.350, FROM_HERE);
1341 void HTMLMediaElement::waitForSourceChange()
1343 WTF_LOG(Media, "HTMLMediaElement::waitForSourceChange");
1345 stopPeriodicTimers();
1346 m_loadState = WaitingForSource;
1348 // 6.17 - Waiting: Set the element's networkState attribute to the NETWORK_NO_SOURCE value
1349 m_networkState = NETWORK_NO_SOURCE;
1351 // 6.18 - Set the element's delaying-the-load-event flag to false. This stops delaying the load event.
1352 setShouldDelayLoadEvent(false);
1354 updateDisplayState();
1357 renderer()->updateFromElement();
1360 void HTMLMediaElement::noneSupported()
1362 WTF_LOG(Media, "HTMLMediaElement::noneSupported");
1364 stopPeriodicTimers();
1365 m_loadState = WaitingForSource;
1366 m_currentSourceNode = nullptr;
1369 // 6 - Reaching this step indicates that the media resource failed to load or that the given
1370 // URL could not be resolved. In one atomic operation, run the following steps:
1372 // 6.1 - Set the error attribute to a new MediaError object whose code attribute is set to
1373 // MEDIA_ERR_SRC_NOT_SUPPORTED.
1374 m_error = MediaError::create(MediaError::MEDIA_ERR_SRC_NOT_SUPPORTED);
1376 // 6.2 - Forget the media element's media-resource-specific text tracks.
1377 forgetResourceSpecificTracks();
1379 // 6.3 - Set the element's networkState attribute to the NETWORK_NO_SOURCE value.
1380 m_networkState = NETWORK_NO_SOURCE;
1382 // 7 - Queue a task to fire a simple event named error at the media element.
1383 scheduleEvent(EventTypeNames::error);
1387 // 8 - Set the element's delaying-the-load-event flag to false. This stops delaying the load event.
1388 setShouldDelayLoadEvent(false);
1390 // 9 - Abort these steps. Until the load() method is invoked or the src attribute is changed,
1391 // the element won't attempt to load another resource.
1393 updateDisplayState();
1396 renderer()->updateFromElement();
1399 void HTMLMediaElement::mediaEngineError(PassRefPtr<MediaError> err)
1401 WTF_LOG(Media, "HTMLMediaElement::mediaEngineError(%d)", static_cast<int>(err->code()));
1403 // 1 - The user agent should cancel the fetching process.
1404 stopPeriodicTimers();
1405 m_loadState = WaitingForSource;
1407 // 2 - Set the error attribute to a new MediaError object whose code attribute is
1408 // set to MEDIA_ERR_NETWORK/MEDIA_ERR_DECODE.
1411 // 3 - Queue a task to fire a simple event named error at the media element.
1412 scheduleEvent(EventTypeNames::error);
1416 // 4 - Set the element's networkState attribute to the NETWORK_EMPTY value and queue a
1417 // task to fire a simple event called emptied at the element.
1418 m_networkState = NETWORK_EMPTY;
1419 scheduleEvent(EventTypeNames::emptied);
1421 // 5 - Set the element's delaying-the-load-event flag to false. This stops delaying the load event.
1422 setShouldDelayLoadEvent(false);
1424 // 6 - Abort the overall resource selection algorithm.
1425 m_currentSourceNode = nullptr;
1428 void HTMLMediaElement::cancelPendingEventsAndCallbacks()
1430 WTF_LOG(Media, "HTMLMediaElement::cancelPendingEventsAndCallbacks");
1431 m_asyncEventQueue->cancelAllEvents();
1433 for (HTMLSourceElement* source = Traversal<HTMLSourceElement>::firstChild(*this); source; source = Traversal<HTMLSourceElement>::nextSibling(*source))
1434 source->cancelPendingErrorEvent();
1437 void HTMLMediaElement::mediaPlayerNetworkStateChanged()
1439 setNetworkState(m_player->networkState());
1442 void HTMLMediaElement::mediaLoadingFailed(MediaPlayer::NetworkState error)
1444 stopPeriodicTimers();
1446 // If we failed while trying to load a <source> element, the movie was never parsed, and there are more
1447 // <source> children, schedule the next one
1448 if (m_readyState < HAVE_METADATA && m_loadState == LoadingFromSourceElement) {
1450 // resource selection algorithm
1451 // Step 9.Otherwise.9 - Failed with elements: Queue a task, using the DOM manipulation task source, to fire a simple event named error at the candidate element.
1452 if (m_currentSourceNode)
1453 m_currentSourceNode->scheduleErrorEvent();
1455 WTF_LOG(Media, "HTMLMediaElement::setNetworkState - error event not sent, <source> was removed");
1457 // 9.Otherwise.10 - Asynchronously await a stable state. The synchronous section consists of all the remaining steps of this algorithm until the algorithm says the synchronous section has ended.
1459 // 9.Otherwise.11 - Forget the media element's media-resource-specific tracks.
1460 forgetResourceSpecificTracks();
1462 if (havePotentialSourceChild()) {
1463 WTF_LOG(Media, "HTMLMediaElement::setNetworkState - scheduling next <source>");
1464 scheduleNextSourceChild();
1466 WTF_LOG(Media, "HTMLMediaElement::setNetworkState - no more <source> elements, waiting");
1467 waitForSourceChange();
1473 if (error == MediaPlayer::NetworkError && m_readyState >= HAVE_METADATA)
1474 mediaEngineError(MediaError::create(MediaError::MEDIA_ERR_NETWORK));
1475 else if (error == MediaPlayer::DecodeError)
1476 mediaEngineError(MediaError::create(MediaError::MEDIA_ERR_DECODE));
1477 else if ((error == MediaPlayer::FormatError || error == MediaPlayer::NetworkError) && m_loadState == LoadingFromSrcAttr)
1480 updateDisplayState();
1481 if (hasMediaControls())
1482 mediaControls()->reset();
1485 void HTMLMediaElement::setNetworkState(MediaPlayer::NetworkState state)
1487 WTF_LOG(Media, "HTMLMediaElement::setNetworkState(%d) - current state is %d", static_cast<int>(state), static_cast<int>(m_networkState));
1489 if (state == MediaPlayer::Empty) {
1490 // Just update the cached state and leave, we can't do anything.
1491 m_networkState = NETWORK_EMPTY;
1495 if (state == MediaPlayer::FormatError || state == MediaPlayer::NetworkError || state == MediaPlayer::DecodeError) {
1496 mediaLoadingFailed(state);
1500 if (state == MediaPlayer::Idle) {
1501 if (m_networkState > NETWORK_IDLE) {
1502 changeNetworkStateFromLoadingToIdle();
1503 setShouldDelayLoadEvent(false);
1505 m_networkState = NETWORK_IDLE;
1509 if (state == MediaPlayer::Loading) {
1510 if (m_networkState < NETWORK_LOADING || m_networkState == NETWORK_NO_SOURCE)
1511 startProgressEventTimer();
1512 m_networkState = NETWORK_LOADING;
1515 if (state == MediaPlayer::Loaded) {
1516 if (m_networkState != NETWORK_IDLE)
1517 changeNetworkStateFromLoadingToIdle();
1518 m_completelyLoaded = true;
1522 void HTMLMediaElement::changeNetworkStateFromLoadingToIdle()
1524 m_progressEventTimer.stop();
1526 // Schedule one last progress event so we guarantee that at least one is fired
1527 // for files that load very quickly.
1528 scheduleEvent(EventTypeNames::progress);
1529 scheduleEvent(EventTypeNames::suspend);
1530 m_networkState = NETWORK_IDLE;
1533 void HTMLMediaElement::mediaPlayerReadyStateChanged()
1535 setReadyState(m_player->readyState());
1538 void HTMLMediaElement::setReadyState(MediaPlayer::ReadyState state)
1540 WTF_LOG(Media, "HTMLMediaElement::setReadyState(%d) - current state is %d,", static_cast<int>(state), static_cast<int>(m_readyState));
1542 // Set "wasPotentiallyPlaying" BEFORE updating m_readyState, potentiallyPlaying() uses it
1543 bool wasPotentiallyPlaying = potentiallyPlaying();
1545 ReadyState oldState = m_readyState;
1546 ReadyState newState = static_cast<ReadyState>(state);
1548 bool tracksAreReady = !RuntimeEnabledFeatures::videoTrackEnabled() || textTracksAreReady();
1550 if (newState == oldState && m_tracksAreReady == tracksAreReady)
1553 m_tracksAreReady = tracksAreReady;
1556 m_readyState = newState;
1558 // If a media file has text tracks the readyState may not progress beyond HAVE_FUTURE_DATA until
1559 // the text tracks are ready, regardless of the state of the media file.
1560 if (newState <= HAVE_METADATA)
1561 m_readyState = newState;
1563 m_readyState = HAVE_CURRENT_DATA;
1566 if (oldState > m_readyStateMaximum)
1567 m_readyStateMaximum = oldState;
1569 if (m_networkState == NETWORK_EMPTY)
1573 // 4.8.10.9, step 9 note: If the media element was potentially playing immediately before
1574 // it started seeking, but seeking caused its readyState attribute to change to a value
1575 // lower than HAVE_FUTURE_DATA, then a waiting will be fired at the element.
1576 if (wasPotentiallyPlaying && m_readyState < HAVE_FUTURE_DATA)
1577 scheduleEvent(EventTypeNames::waiting);
1579 // 4.8.10.9 steps 12-14
1580 if (m_readyState >= HAVE_CURRENT_DATA)
1583 if (wasPotentiallyPlaying && m_readyState < HAVE_FUTURE_DATA) {
1585 scheduleTimeupdateEvent(false);
1586 scheduleEvent(EventTypeNames::waiting);
1590 if (m_readyState >= HAVE_METADATA && oldState < HAVE_METADATA) {
1591 prepareMediaFragmentURI();
1592 scheduleEvent(EventTypeNames::durationchange);
1593 if (isHTMLVideoElement(*this))
1594 scheduleEvent(EventTypeNames::resize);
1595 scheduleEvent(EventTypeNames::loadedmetadata);
1596 if (hasMediaControls())
1597 mediaControls()->reset();
1599 renderer()->updateFromElement();
1602 bool shouldUpdateDisplayState = false;
1604 if (m_readyState >= HAVE_CURRENT_DATA && oldState < HAVE_CURRENT_DATA && !m_haveFiredLoadedData) {
1605 m_haveFiredLoadedData = true;
1606 shouldUpdateDisplayState = true;
1607 scheduleEvent(EventTypeNames::loadeddata);
1608 setShouldDelayLoadEvent(false);
1609 applyMediaFragmentURI();
1612 bool isPotentiallyPlaying = potentiallyPlaying();
1613 if (m_readyState == HAVE_FUTURE_DATA && oldState <= HAVE_CURRENT_DATA && tracksAreReady) {
1614 scheduleEvent(EventTypeNames::canplay);
1615 if (isPotentiallyPlaying)
1616 scheduleEvent(EventTypeNames::playing);
1617 shouldUpdateDisplayState = true;
1620 if (m_readyState == HAVE_ENOUGH_DATA && oldState < HAVE_ENOUGH_DATA && tracksAreReady) {
1621 if (oldState <= HAVE_CURRENT_DATA) {
1622 scheduleEvent(EventTypeNames::canplay);
1623 if (isPotentiallyPlaying)
1624 scheduleEvent(EventTypeNames::playing);
1627 if (m_autoplaying && m_paused && autoplay() && !document().isSandboxed(SandboxAutomaticFeatures) && !m_userGestureRequiredForPlay) {
1629 invalidateCachedTime();
1630 scheduleEvent(EventTypeNames::play);
1631 scheduleEvent(EventTypeNames::playing);
1634 scheduleEvent(EventTypeNames::canplaythrough);
1636 shouldUpdateDisplayState = true;
1639 if (shouldUpdateDisplayState) {
1640 updateDisplayState();
1641 if (hasMediaControls())
1642 mediaControls()->refreshClosedCaptionsButtonVisibility();
1646 updateMediaController();
1647 if (RuntimeEnabledFeatures::videoTrackEnabled())
1648 updateActiveTextTrackCues(currentTime());
1651 void HTMLMediaElement::progressEventTimerFired(Timer<HTMLMediaElement>*)
1654 if (m_networkState != NETWORK_LOADING)
1657 double time = WTF::currentTime();
1658 double timedelta = time - m_previousProgressTime;
1660 if (m_player->didLoadingProgress()) {
1661 scheduleEvent(EventTypeNames::progress);
1662 m_previousProgressTime = time;
1663 m_sentStalledEvent = false;
1665 renderer()->updateFromElement();
1666 } else if (timedelta > 3.0 && !m_sentStalledEvent) {
1667 scheduleEvent(EventTypeNames::stalled);
1668 m_sentStalledEvent = true;
1669 setShouldDelayLoadEvent(false);
1673 void HTMLMediaElement::addPlayedRange(double start, double end)
1675 WTF_LOG(Media, "HTMLMediaElement::addPlayedRange(%f, %f)", start, end);
1676 if (!m_playedTimeRanges)
1677 m_playedTimeRanges = TimeRanges::create();
1678 m_playedTimeRanges->add(start, end);
1681 bool HTMLMediaElement::supportsSave() const
1683 return m_player ? m_player->supportsSave() : false;
1686 void HTMLMediaElement::prepareToPlay()
1688 WTF_LOG(Media, "HTMLMediaElement::prepareToPlay(%p)", this);
1689 if (m_havePreparedToPlay)
1691 m_havePreparedToPlay = true;
1693 if (m_delayingLoadForPreloadNone)
1697 void HTMLMediaElement::seek(double time, ExceptionState& exceptionState)
1699 WTF_LOG(Media, "HTMLMediaElement::seek(%f)", time);
1703 // 1 - If the media element's readyState is HAVE_NOTHING, then raise an InvalidStateError exception.
1704 if (m_readyState == HAVE_NOTHING || !m_player) {
1705 exceptionState.throwDOMException(InvalidStateError, "The element's readyState is HAVE_NOTHING.");
1709 // If the media engine has been told to postpone loading data, let it go ahead now.
1710 if (m_preload < MediaPlayer::Auto && m_readyState < HAVE_FUTURE_DATA)
1713 // Get the current time before setting m_seeking, m_lastSeekTime is returned once it is set.
1714 refreshCachedTime();
1715 double now = currentTime();
1717 // 2 - If the element's seeking IDL attribute is true, then another instance of this algorithm is
1718 // already running. Abort that other instance of the algorithm without waiting for the step that
1719 // it is running to complete.
1720 // Nothing specific to be done here.
1722 // 3 - Set the seeking IDL attribute to true.
1723 // The flag will be cleared when the engine tells us the time has actually changed.
1726 // 5 - If the new playback position is later than the end of the media resource, then let it be the end
1727 // of the media resource instead.
1728 time = min(time, duration());
1730 // 6 - If the new playback position is less than the earliest possible position, let it be that position instead.
1731 time = max(time, 0.0);
1733 // Ask the media engine for the time value in the movie's time scale before comparing with current time. This
1734 // is necessary because if the seek time is not equal to currentTime but the delta is less than the movie's
1735 // time scale, we will ask the media engine to "seek" to the current movie time, which may be a noop and
1736 // not generate a timechanged callback. This means m_seeking will never be cleared and we will never
1737 // fire a 'seeked' event.
1739 double mediaTime = m_player->mediaTimeForTimeValue(time);
1740 if (time != mediaTime)
1741 WTF_LOG(Media, "HTMLMediaElement::seek(%f) - media timeline equivalent is %f", time, mediaTime);
1743 time = m_player->mediaTimeForTimeValue(time);
1745 // 7 - If the (possibly now changed) new playback position is not in one of the ranges given in the
1746 // seekable attribute, then let it be the position in one of the ranges given in the seekable attribute
1747 // that is the nearest to the new playback position. ... If there are no ranges given in the seekable
1748 // attribute then set the seeking IDL attribute to false and abort these steps.
1749 RefPtr<TimeRanges> seekableRanges = seekable();
1751 // Short circuit seeking to the current time by just firing the events if no seek is required.
1752 // Don't skip calling the media engine if we are in poster mode because a seek should always
1753 // cancel poster display.
1754 bool noSeekRequired = !seekableRanges->length() || (time == now && displayMode() != Poster);
1756 // Always notify the media engine of a seek if the source is not closed. This ensures that the source is
1757 // always in a flushed state when the 'seeking' event fires.
1758 if (m_mediaSource && m_mediaSource->isClosed())
1759 noSeekRequired = false;
1761 if (noSeekRequired) {
1763 scheduleEvent(EventTypeNames::seeking);
1764 // FIXME: There must be a stable state before timeupdate+seeked are dispatched and seeking
1765 // is reset to false. See http://crbug.com/266631
1766 scheduleTimeupdateEvent(false);
1767 scheduleEvent(EventTypeNames::seeked);
1772 time = seekableRanges->nearest(time);
1775 if (m_lastSeekTime < now)
1776 addPlayedRange(m_lastSeekTime, now);
1778 m_lastSeekTime = time;
1779 m_sentEndEvent = false;
1781 // 8 - Queue a task to fire a simple event named seeking at the element.
1782 scheduleEvent(EventTypeNames::seeking);
1784 // 9 - Set the current playback position to the given new playback position
1785 m_player->seek(time);
1787 // 10-14 are handled, if necessary, when the engine signals a readystate change or otherwise
1788 // satisfies seek completion and signals a time change.
1791 void HTMLMediaElement::finishSeek()
1793 WTF_LOG(Media, "HTMLMediaElement::finishSeek");
1795 // 4.8.10.9 Seeking completion
1796 // 12 - Set the seeking IDL attribute to false.
1799 // 13 - Queue a task to fire a simple event named timeupdate at the element.
1800 scheduleTimeupdateEvent(false);
1802 // 14 - Queue a task to fire a simple event named seeked at the element.
1803 scheduleEvent(EventTypeNames::seeked);
1805 setDisplayMode(Video);
1808 HTMLMediaElement::ReadyState HTMLMediaElement::readyState() const
1810 return m_readyState;
1813 bool HTMLMediaElement::hasAudio() const
1815 return m_player ? m_player->hasAudio() : false;
1818 bool HTMLMediaElement::seeking() const
1823 void HTMLMediaElement::refreshCachedTime() const
1825 m_cachedTime = m_player->currentTime();
1826 m_cachedTimeWallClockUpdateTime = WTF::currentTime();
1829 void HTMLMediaElement::invalidateCachedTime()
1831 WTF_LOG(Media, "HTMLMediaElement::invalidateCachedTime");
1833 // Don't try to cache movie time when playback first starts as the time reported by the engine
1834 // sometimes fluctuates for a short amount of time, so the cached time will be off if we take it
1836 static const double minimumTimePlayingBeforeCacheSnapshot = 0.5;
1838 m_minimumWallClockTimeToCacheMediaTime = WTF::currentTime() + minimumTimePlayingBeforeCacheSnapshot;
1839 m_cachedTime = MediaPlayer::invalidTime();
1843 double HTMLMediaElement::currentTime() const
1845 #if LOG_CACHED_TIME_WARNINGS
1846 static const double minCachedDeltaForWarning = 0.01;
1853 WTF_LOG(Media, "HTMLMediaElement::currentTime - seeking, returning %f", m_lastSeekTime);
1854 return m_lastSeekTime;
1857 if (m_cachedTime != MediaPlayer::invalidTime() && m_paused) {
1858 #if LOG_CACHED_TIME_WARNINGS
1859 double delta = m_cachedTime - m_player->currentTime();
1860 if (delta > minCachedDeltaForWarning)
1861 WTF_LOG(Media, "HTMLMediaElement::currentTime - WARNING, cached time is %f seconds off of media time when paused", delta);
1863 return m_cachedTime;
1866 refreshCachedTime();
1868 return m_cachedTime;
1871 void HTMLMediaElement::setCurrentTime(double time, ExceptionState& exceptionState)
1873 if (m_mediaController) {
1874 exceptionState.throwDOMException(InvalidStateError, "The element is slaved to a MediaController.");
1877 seek(time, exceptionState);
1880 double HTMLMediaElement::duration() const
1882 if (!m_player || m_readyState < HAVE_METADATA)
1883 return numeric_limits<double>::quiet_NaN();
1885 // FIXME: Refactor so m_duration is kept current (in both MSE and
1886 // non-MSE cases) once we have transitioned from HAVE_NOTHING ->
1887 // HAVE_METADATA. Currently, m_duration may be out of date for at least MSE
1888 // case because MediaSourceBase and SourceBuffer do not notify the element
1889 // directly upon duration changes caused by endOfStream, remove, or append
1890 // operations; rather the notification is triggered by the WebMediaPlayer
1891 // implementation observing that the underlying engine has updated duration
1892 // and notifying the element to consult its MediaSource for current
1893 // duration. See http://crbug.com/266644
1896 return m_mediaSource->duration();
1898 return m_player->duration();
1901 bool HTMLMediaElement::paused() const
1906 double HTMLMediaElement::defaultPlaybackRate() const
1908 return m_defaultPlaybackRate;
1911 void HTMLMediaElement::setDefaultPlaybackRate(double rate)
1913 if (m_defaultPlaybackRate != rate) {
1914 m_defaultPlaybackRate = rate;
1915 scheduleEvent(EventTypeNames::ratechange);
1919 double HTMLMediaElement::playbackRate() const
1921 return m_playbackRate;
1924 void HTMLMediaElement::setPlaybackRate(double rate)
1926 WTF_LOG(Media, "HTMLMediaElement::setPlaybackRate(%f)", rate);
1928 if (m_playbackRate != rate) {
1929 m_playbackRate = rate;
1930 invalidateCachedTime();
1931 scheduleEvent(EventTypeNames::ratechange);
1934 if (m_player && potentiallyPlaying() && m_player->rate() != rate && !m_mediaController)
1935 m_player->setRate(rate);
1938 void HTMLMediaElement::updatePlaybackRate()
1940 double effectiveRate = m_mediaController ? m_mediaController->playbackRate() : m_playbackRate;
1941 if (m_player && potentiallyPlaying() && m_player->rate() != effectiveRate)
1942 m_player->setRate(effectiveRate);
1945 bool HTMLMediaElement::ended() const
1947 // 4.8.10.8 Playing the media resource
1948 // The ended attribute must return true if the media element has ended
1949 // playback and the direction of playback is forwards, and false otherwise.
1950 return endedPlayback() && m_playbackRate > 0;
1953 bool HTMLMediaElement::autoplay() const
1955 return fastHasAttribute(autoplayAttr);
1958 String HTMLMediaElement::preload() const
1960 switch (m_preload) {
1961 case MediaPlayer::None:
1964 case MediaPlayer::MetaData:
1967 case MediaPlayer::Auto:
1972 ASSERT_NOT_REACHED();
1976 void HTMLMediaElement::setPreload(const AtomicString& preload)
1978 WTF_LOG(Media, "HTMLMediaElement::setPreload(%s)", preload.utf8().data());
1979 setAttribute(preloadAttr, preload);
1982 void HTMLMediaElement::play()
1984 WTF_LOG(Media, "HTMLMediaElement::play()");
1986 if (m_userGestureRequiredForPlay && !UserGestureIndicator::processingUserGesture())
1988 if (UserGestureIndicator::processingUserGesture())
1989 m_userGestureRequiredForPlay = false;
1994 void HTMLMediaElement::playInternal()
1996 WTF_LOG(Media, "HTMLMediaElement::playInternal");
1998 // 4.8.10.9. Playing the media resource
1999 if (!m_player || m_networkState == NETWORK_EMPTY)
2000 scheduleDelayedAction(LoadMediaResource);
2002 if (endedPlayback())
2003 seek(0, IGNORE_EXCEPTION);
2005 if (m_mediaController)
2006 m_mediaController->bringElementUpToSpeed(this);
2010 invalidateCachedTime();
2011 scheduleEvent(EventTypeNames::play);
2013 if (m_readyState <= HAVE_CURRENT_DATA)
2014 scheduleEvent(EventTypeNames::waiting);
2015 else if (m_readyState >= HAVE_FUTURE_DATA)
2016 scheduleEvent(EventTypeNames::playing);
2018 m_autoplaying = false;
2021 updateMediaController();
2024 void HTMLMediaElement::pause()
2026 WTF_LOG(Media, "HTMLMediaElement::pause()");
2028 if (!m_player || m_networkState == NETWORK_EMPTY)
2029 scheduleDelayedAction(LoadMediaResource);
2031 m_autoplaying = false;
2035 scheduleTimeupdateEvent(false);
2036 scheduleEvent(EventTypeNames::pause);
2042 void HTMLMediaElement::closeMediaSource()
2047 m_mediaSource->close();
2048 m_mediaSource = nullptr;
2051 bool HTMLMediaElement::loop() const
2053 return fastHasAttribute(loopAttr);
2056 void HTMLMediaElement::setLoop(bool b)
2058 WTF_LOG(Media, "HTMLMediaElement::setLoop(%s)", boolString(b));
2059 setBooleanAttribute(loopAttr, b);
2062 bool HTMLMediaElement::controls() const
2064 LocalFrame* frame = document().frame();
2066 // always show controls when scripting is disabled
2067 if (frame && !frame->script().canExecuteScripts(NotAboutToExecuteScript))
2070 // Always show controls when in full screen mode.
2074 return fastHasAttribute(controlsAttr);
2077 void HTMLMediaElement::setControls(bool b)
2079 WTF_LOG(Media, "HTMLMediaElement::setControls(%s)", boolString(b));
2080 setBooleanAttribute(controlsAttr, b);
2083 double HTMLMediaElement::volume() const
2088 void HTMLMediaElement::setVolume(double vol, ExceptionState& exceptionState)
2090 WTF_LOG(Media, "HTMLMediaElement::setVolume(%f)", vol);
2092 if (vol < 0.0f || vol > 1.0f) {
2093 exceptionState.throwDOMException(IndexSizeError, ExceptionMessages::indexOutsideRange("volume", vol, 0.0, ExceptionMessages::InclusiveBound, 1.0, ExceptionMessages::InclusiveBound));
2097 if (m_volume != vol) {
2100 scheduleEvent(EventTypeNames::volumechange);
2104 bool HTMLMediaElement::muted() const
2109 void HTMLMediaElement::setMuted(bool muted)
2111 WTF_LOG(Media, "HTMLMediaElement::setMuted(%s)", boolString(muted));
2113 if (m_muted == muted)
2120 if (hasMediaControls())
2121 mediaControls()->changedMute();
2123 scheduleEvent(EventTypeNames::volumechange);
2126 // The spec says to fire periodic timeupdate events (those sent while playing) every
2127 // "15 to 250ms", we choose the slowest frequency
2128 static const double maxTimeupdateEventFrequency = 0.25;
2130 void HTMLMediaElement::startPlaybackProgressTimer()
2132 if (m_playbackProgressTimer.isActive())
2135 m_previousProgressTime = WTF::currentTime();
2136 m_playbackProgressTimer.startRepeating(maxTimeupdateEventFrequency, FROM_HERE);
2139 void HTMLMediaElement::playbackProgressTimerFired(Timer<HTMLMediaElement>*)
2143 if (m_fragmentEndTime != MediaPlayer::invalidTime() && currentTime() >= m_fragmentEndTime && m_playbackRate > 0) {
2144 m_fragmentEndTime = MediaPlayer::invalidTime();
2145 if (!m_mediaController && !m_paused) {
2146 UseCounter::count(document(), UseCounter::HTMLMediaElementPauseAtFragmentEnd);
2147 // changes paused to true and fires a simple event named pause at the media element.
2153 scheduleTimeupdateEvent(true);
2155 if (!m_playbackRate)
2158 if (!m_paused && hasMediaControls())
2159 mediaControls()->playbackProgressed();
2161 if (RuntimeEnabledFeatures::videoTrackEnabled())
2162 updateActiveTextTrackCues(currentTime());
2165 void HTMLMediaElement::scheduleTimeupdateEvent(bool periodicEvent)
2167 double now = WTF::currentTime();
2168 double timedelta = now - m_lastTimeUpdateEventWallTime;
2170 // throttle the periodic events
2171 if (periodicEvent && timedelta < maxTimeupdateEventFrequency)
2174 // Some media engines make multiple "time changed" callbacks at the same time, but we only want one
2175 // event at a given time so filter here
2176 double movieTime = currentTime();
2177 if (movieTime != m_lastTimeUpdateEventMovieTime) {
2178 scheduleEvent(EventTypeNames::timeupdate);
2179 m_lastTimeUpdateEventWallTime = now;
2180 m_lastTimeUpdateEventMovieTime = movieTime;
2184 bool HTMLMediaElement::togglePlayStateWillPlay() const
2186 if (m_mediaController)
2187 return m_mediaController->paused() || m_mediaController->isRestrained();
2191 void HTMLMediaElement::togglePlayState()
2193 // The activation behavior of a media element that is exposing a user interface to the user
2194 if (m_mediaController) {
2195 if (m_mediaController->isRestrained())
2196 m_mediaController->play();
2197 else if (m_mediaController->paused())
2198 m_mediaController->unpause();
2200 m_mediaController->pause();
2209 void HTMLMediaElement::mediaPlayerDidAddTextTrack(WebInbandTextTrack* webTrack)
2211 if (!RuntimeEnabledFeatures::videoTrackEnabled())
2214 // 4.8.10.12.2 Sourcing in-band text tracks
2215 // 1. Associate the relevant data with a new text track and its corresponding new TextTrack object.
2216 RefPtr<InbandTextTrack> textTrack = InbandTextTrack::create(document(), webTrack);
2218 // 2. Set the new text track's kind, label, and language based on the semantics of the relevant data,
2219 // as defined by the relevant specification. If there is no label in that data, then the label must
2220 // be set to the empty string.
2221 // 3. Associate the text track list of cues with the rules for updating the text track rendering appropriate
2222 // for the format in question.
2223 // 4. If the new text track's kind is metadata, then set the text track in-band metadata track dispatch type
2224 // as follows, based on the type of the media resource:
2225 // 5. Populate the new text track's list of cues with the cues parsed so far, folllowing the guidelines for exposing
2226 // cues, and begin updating it dynamically as necessary.
2227 // - Thess are all done by the media engine.
2229 // 6. Set the new text track's readiness state to loaded.
2230 textTrack->setReadinessState(TextTrack::Loaded);
2232 // 7. Set the new text track's mode to the mode consistent with the user's preferences and the requirements of
2233 // the relevant specification for the data.
2234 // - This will happen in configureTextTracks()
2235 scheduleDelayedAction(LoadTextTrackResource);
2237 // 8. Add the new text track to the media element's list of text tracks.
2238 // 9. Fire an event with the name addtrack, that does not bubble and is not cancelable, and that uses the TrackEvent
2239 // interface, with the track attribute initialized to the text track's TextTrack object, at the media element's
2240 // textTracks attribute's TextTrackList object.
2241 addTextTrack(textTrack.get());
2244 void HTMLMediaElement::mediaPlayerDidRemoveTextTrack(WebInbandTextTrack* webTrack)
2246 if (!RuntimeEnabledFeatures::videoTrackEnabled())
2252 // This cast is safe because we created the InbandTextTrack with the WebInbandTextTrack
2253 // passed to mediaPlayerDidAddTextTrack.
2254 RefPtr<InbandTextTrack> textTrack = static_cast<InbandTextTrack*>(webTrack->client());
2258 removeTextTrack(textTrack.get());
2261 void HTMLMediaElement::closeCaptionTracksChanged()
2263 if (hasMediaControls())
2264 mediaControls()->closedCaptionTracksChanged();
2267 void HTMLMediaElement::addTextTrack(TextTrack* track)
2269 textTracks()->append(track);
2271 closeCaptionTracksChanged();
2274 void HTMLMediaElement::removeTextTrack(TextTrack* track)
2276 TrackDisplayUpdateScope scope(this);
2277 m_textTracks->remove(track);
2279 closeCaptionTracksChanged();
2282 void HTMLMediaElement::forgetResourceSpecificTracks()
2285 TrackDisplayUpdateScope scope(this);
2286 m_textTracks->removeAllInbandTracks();
2287 closeCaptionTracksChanged();
2291 PassRefPtr<TextTrack> HTMLMediaElement::addTextTrack(const AtomicString& kind, const AtomicString& label, const AtomicString& language, ExceptionState& exceptionState)
2293 ASSERT(RuntimeEnabledFeatures::videoTrackEnabled());
2295 // 4.8.10.12.4 Text track API
2296 // The addTextTrack(kind, label, language) method of media elements, when invoked, must run the following steps:
2298 // 1. If kind is not one of the following strings, then throw a SyntaxError exception and abort these steps
2299 if (!TextTrack::isValidKindKeyword(kind)) {
2300 exceptionState.throwDOMException(SyntaxError, "The 'kind' provided ('" + kind + "') is invalid.");
2304 // 2. If the label argument was omitted, let label be the empty string.
2305 // 3. If the language argument was omitted, let language be the empty string.
2306 // 4. Create a new TextTrack object.
2308 // 5. Create a new text track corresponding to the new object, and set its text track kind to kind, its text
2309 // track label to label, its text track language to language...
2310 RefPtr<TextTrack> textTrack = TextTrack::create(document(), kind, label, language);
2312 // Note, due to side effects when changing track parameters, we have to
2313 // first append the track to the text track list.
2315 // 6. Add the new text track to the media element's list of text tracks.
2316 addTextTrack(textTrack.get());
2318 // ... its text track readiness state to the text track loaded state ...
2319 textTrack->setReadinessState(TextTrack::Loaded);
2321 // ... its text track mode to the text track hidden mode, and its text track list of cues to an empty list ...
2322 textTrack->setMode(TextTrack::hiddenKeyword());
2324 return textTrack.release();
2327 TextTrackList* HTMLMediaElement::textTracks()
2329 ASSERT(RuntimeEnabledFeatures::videoTrackEnabled());
2332 m_textTracks = TextTrackList::create(this);
2334 return m_textTracks.get();
2337 void HTMLMediaElement::didAddTrackElement(HTMLTrackElement* trackElement)
2339 if (!RuntimeEnabledFeatures::videoTrackEnabled())
2342 // 4.8.10.12.3 Sourcing out-of-band text tracks
2343 // When a track element's parent element changes and the new parent is a media element,
2344 // then the user agent must add the track element's corresponding text track to the
2345 // media element's list of text tracks ... [continues in TextTrackList::append]
2346 RefPtr<TextTrack> textTrack = trackElement->track();
2350 addTextTrack(textTrack.get());
2352 // Do not schedule the track loading until parsing finishes so we don't start before all tracks
2353 // in the markup have been added.
2354 if (isFinishedParsingChildren())
2355 scheduleDelayedAction(LoadTextTrackResource);
2357 if (hasMediaControls())
2358 mediaControls()->closedCaptionTracksChanged();
2361 void HTMLMediaElement::didRemoveTrackElement(HTMLTrackElement* trackElement)
2363 if (!RuntimeEnabledFeatures::videoTrackEnabled())
2367 KURL url = trackElement->getNonEmptyURLAttribute(srcAttr);
2368 WTF_LOG(Media, "HTMLMediaElement::didRemoveTrackElement - 'src' is %s", urlForLoggingMedia(url).utf8().data());
2371 RefPtr<TextTrack> textTrack = trackElement->track();
2375 textTrack->setHasBeenConfigured(false);
2380 // 4.8.10.12.3 Sourcing out-of-band text tracks
2381 // When a track element's parent element changes and the old parent was a media element,
2382 // then the user agent must remove the track element's corresponding text track from the
2383 // media element's list of text tracks.
2384 removeTextTrack(textTrack.get());
2386 size_t index = m_textTracksWhenResourceSelectionBegan.find(textTrack.get());
2387 if (index != kNotFound)
2388 m_textTracksWhenResourceSelectionBegan.remove(index);
2391 static int textTrackLanguageSelectionScore(const TextTrack& track)
2393 if (track.language().isEmpty())
2396 Vector<AtomicString> languages = userPreferredLanguages();
2397 size_t languageMatchIndex = indexOfBestMatchingLanguageInList(track.language(), languages);
2398 if (languageMatchIndex >= languages.size())
2401 // Matching a track language is more important than matching track type, so this multiplier must be
2402 // greater than the maximum value returned by textTrackSelectionScore.
2403 return (languages.size() - languageMatchIndex) * 10;
2406 static int textTrackSelectionScore(const TextTrack& track, Settings* settings)
2413 if (track.kind() != TextTrack::captionsKeyword() && track.kind() != TextTrack::subtitlesKeyword())
2416 if (track.kind() == TextTrack::subtitlesKeyword() && settings->shouldDisplaySubtitles())
2418 else if (track.kind() == TextTrack::captionsKeyword() && settings->shouldDisplayCaptions())
2421 return trackScore + textTrackLanguageSelectionScore(track);
2424 void HTMLMediaElement::configureTextTrackGroup(const TrackGroup& group)
2426 ASSERT(group.tracks.size());
2428 WTF_LOG(Media, "HTMLMediaElement::configureTextTrackGroup(%d)", group.kind);
2430 Settings* settings = document().settings();
2432 // First, find the track in the group that should be enabled (if any).
2433 Vector<RefPtr<TextTrack> > currentlyEnabledTracks;
2434 RefPtr<TextTrack> trackToEnable;
2435 RefPtr<TextTrack> defaultTrack;
2436 RefPtr<TextTrack> fallbackTrack;
2437 int highestTrackScore = 0;
2438 for (size_t i = 0; i < group.tracks.size(); ++i) {
2439 RefPtr<TextTrack> textTrack = group.tracks[i];
2441 if (m_processingPreferenceChange && textTrack->mode() == TextTrack::showingKeyword())
2442 currentlyEnabledTracks.append(textTrack);
2444 int trackScore = textTrackSelectionScore(*textTrack, settings);
2446 // * If the text track kind is { [subtitles or captions] [descriptions] } and the user has indicated an interest in having a
2447 // track with this text track kind, text track language, and text track label enabled, and there is no
2448 // other text track in the media element's list of text tracks with a text track kind of either subtitles
2449 // or captions whose text track mode is showing
2451 // * If the text track kind is chapters and the text track language is one that the user agent has reason
2452 // to believe is appropriate for the user, and there is no other text track in the media element's list of
2453 // text tracks with a text track kind of chapters whose text track mode is showing
2454 // Let the text track mode be showing.
2455 if (trackScore > highestTrackScore) {
2456 highestTrackScore = trackScore;
2457 trackToEnable = textTrack;
2460 if (!defaultTrack && textTrack->isDefault())
2461 defaultTrack = textTrack;
2462 if (!defaultTrack && !fallbackTrack)
2463 fallbackTrack = textTrack;
2464 } else if (!group.visibleTrack && !defaultTrack && textTrack->isDefault()) {
2465 // * If the track element has a default attribute specified, and there is no other text track in the media
2466 // element's list of text tracks whose text track mode is showing or showing by default
2467 // Let the text track mode be showing by default.
2468 defaultTrack = textTrack;
2472 if (!trackToEnable && defaultTrack)
2473 trackToEnable = defaultTrack;
2475 // If no track matches the user's preferred language and non was marked 'default', enable the first track
2476 // because the user has explicitly stated a preference for this kind of track.
2477 if (!fallbackTrack && m_closedCaptionsVisible && group.kind == TrackGroup::CaptionsAndSubtitles)
2478 fallbackTrack = group.tracks[0];
2480 if (!trackToEnable && fallbackTrack)
2481 trackToEnable = fallbackTrack;
2483 if (currentlyEnabledTracks.size()) {
2484 for (size_t i = 0; i < currentlyEnabledTracks.size(); ++i) {
2485 RefPtr<TextTrack> textTrack = currentlyEnabledTracks[i];
2486 if (textTrack != trackToEnable)
2487 textTrack->setMode(TextTrack::disabledKeyword());
2492 trackToEnable->setMode(TextTrack::showingKeyword());
2495 void HTMLMediaElement::configureTextTracks()
2497 TrackGroup captionAndSubtitleTracks(TrackGroup::CaptionsAndSubtitles);
2498 TrackGroup descriptionTracks(TrackGroup::Description);
2499 TrackGroup chapterTracks(TrackGroup::Chapter);
2500 TrackGroup metadataTracks(TrackGroup::Metadata);
2501 TrackGroup otherTracks(TrackGroup::Other);
2506 for (size_t i = 0; i < m_textTracks->length(); ++i) {
2507 RefPtr<TextTrack> textTrack = m_textTracks->item(i);
2511 String kind = textTrack->kind();
2512 TrackGroup* currentGroup;
2513 if (kind == TextTrack::subtitlesKeyword() || kind == TextTrack::captionsKeyword())
2514 currentGroup = &captionAndSubtitleTracks;
2515 else if (kind == TextTrack::descriptionsKeyword())
2516 currentGroup = &descriptionTracks;
2517 else if (kind == TextTrack::chaptersKeyword())
2518 currentGroup = &chapterTracks;
2519 else if (kind == TextTrack::metadataKeyword())
2520 currentGroup = &metadataTracks;
2522 currentGroup = &otherTracks;
2524 if (!currentGroup->visibleTrack && textTrack->mode() == TextTrack::showingKeyword())
2525 currentGroup->visibleTrack = textTrack;
2526 if (!currentGroup->defaultTrack && textTrack->isDefault())
2527 currentGroup->defaultTrack = textTrack;
2529 // Do not add this track to the group if it has already been automatically configured
2530 // as we only want to call configureTextTrack once per track so that adding another
2531 // track after the initial configuration doesn't reconfigure every track - only those
2532 // that should be changed by the new addition. For example all metadata tracks are
2533 // disabled by default, and we don't want a track that has been enabled by script
2534 // to be disabled automatically when a new metadata track is added later.
2535 if (textTrack->hasBeenConfigured())
2538 if (textTrack->language().length())
2539 currentGroup->hasSrcLang = true;
2540 currentGroup->tracks.append(textTrack);
2543 if (captionAndSubtitleTracks.tracks.size())
2544 configureTextTrackGroup(captionAndSubtitleTracks);
2545 if (descriptionTracks.tracks.size())
2546 configureTextTrackGroup(descriptionTracks);
2547 if (chapterTracks.tracks.size())
2548 configureTextTrackGroup(chapterTracks);
2549 if (metadataTracks.tracks.size())
2550 configureTextTrackGroup(metadataTracks);
2551 if (otherTracks.tracks.size())
2552 configureTextTrackGroup(otherTracks);
2554 if (hasMediaControls())
2555 mediaControls()->closedCaptionTracksChanged();
2558 bool HTMLMediaElement::havePotentialSourceChild()
2560 // Stash the current <source> node and next nodes so we can restore them after checking
2561 // to see there is another potential.
2562 RefPtr<HTMLSourceElement> currentSourceNode = m_currentSourceNode;
2563 RefPtr<Node> nextNode = m_nextChildNodeToConsider;
2565 KURL nextURL = selectNextSourceChild(0, 0, DoNothing);
2567 m_currentSourceNode = currentSourceNode;
2568 m_nextChildNodeToConsider = nextNode;
2570 return nextURL.isValid();
2573 KURL HTMLMediaElement::selectNextSourceChild(ContentType* contentType, String* keySystem, InvalidURLAction actionIfInvalid)
2576 // Don't log if this was just called to find out if there are any valid <source> elements.
2577 bool shouldLog = actionIfInvalid != DoNothing;
2579 WTF_LOG(Media, "HTMLMediaElement::selectNextSourceChild");
2582 if (!m_nextChildNodeToConsider) {
2585 WTF_LOG(Media, "HTMLMediaElement::selectNextSourceChild -> 0x0000, \"\"");
2592 HTMLSourceElement* source = 0;
2595 bool lookingForStartNode = m_nextChildNodeToConsider;
2596 bool canUseSourceElement = false;
2598 NodeVector potentialSourceNodes;
2599 getChildNodes(*this, potentialSourceNodes);
2601 for (unsigned i = 0; !canUseSourceElement && i < potentialSourceNodes.size(); ++i) {
2602 node = potentialSourceNodes[i].get();
2603 if (lookingForStartNode && m_nextChildNodeToConsider != node)
2605 lookingForStartNode = false;
2607 if (!isHTMLSourceElement(*node))
2609 if (node->parentNode() != this)
2612 source = toHTMLSourceElement(node);
2614 // 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
2615 mediaURL = source->getNonEmptyURLAttribute(srcAttr);
2618 WTF_LOG(Media, "HTMLMediaElement::selectNextSourceChild - 'src' is %s", urlForLoggingMedia(mediaURL).utf8().data());
2620 if (mediaURL.isEmpty())
2623 type = source->type();
2624 // FIXME(82965): Add support for keySystem in <source> and set system from source.
2625 if (type.isEmpty() && mediaURL.protocolIsData())
2626 type = mimeTypeFromDataURL(mediaURL);
2627 if (!type.isEmpty() || !system.isEmpty()) {
2630 WTF_LOG(Media, "HTMLMediaElement::selectNextSourceChild - 'type' is '%s' - key system is '%s'", type.utf8().data(), system.utf8().data());
2632 if (!supportsType(ContentType(type), system))
2636 // Is it safe to load this url?
2637 if (!isSafeToLoadURL(mediaURL, actionIfInvalid))
2640 // Making it this far means the <source> looks reasonable.
2641 canUseSourceElement = true;
2644 if (!canUseSourceElement && actionIfInvalid == Complain && source)
2645 source->scheduleErrorEvent();
2648 if (canUseSourceElement) {
2650 *contentType = ContentType(type);
2652 *keySystem = system;
2653 m_currentSourceNode = source;
2654 m_nextChildNodeToConsider = source->nextSibling();
2656 m_currentSourceNode = nullptr;
2657 m_nextChildNodeToConsider = nullptr;
2662 WTF_LOG(Media, "HTMLMediaElement::selectNextSourceChild -> %p, %s", m_currentSourceNode.get(), canUseSourceElement ? urlForLoggingMedia(mediaURL).utf8().data() : "");
2664 return canUseSourceElement ? mediaURL : KURL();
2667 void HTMLMediaElement::sourceWasAdded(HTMLSourceElement* source)
2669 WTF_LOG(Media, "HTMLMediaElement::sourceWasAdded(%p)", source);
2672 KURL url = source->getNonEmptyURLAttribute(srcAttr);
2673 WTF_LOG(Media, "HTMLMediaElement::sourceWasAdded - 'src' is %s", urlForLoggingMedia(url).utf8().data());
2676 // We should only consider a <source> element when there is not src attribute at all.
2677 if (fastHasAttribute(srcAttr))
2680 // 4.8.8 - If a source element is inserted as a child of a media element that has no src
2681 // attribute and whose networkState has the value NETWORK_EMPTY, the user agent must invoke
2682 // the media element's resource selection algorithm.
2683 if (networkState() == HTMLMediaElement::NETWORK_EMPTY) {
2684 scheduleDelayedAction(LoadMediaResource);
2685 m_nextChildNodeToConsider = source;
2689 if (m_currentSourceNode && source == m_currentSourceNode->nextSibling()) {
2690 WTF_LOG(Media, "HTMLMediaElement::sourceWasAdded - <source> inserted immediately after current source");
2691 m_nextChildNodeToConsider = source;
2695 if (m_nextChildNodeToConsider)
2698 // 4.8.9.5, resource selection algorithm, source elements section:
2699 // 21. Wait until the node after pointer is a node other than the end of the list. (This step might wait forever.)
2700 // 22. Asynchronously await a stable state...
2701 // 23. Set the element's delaying-the-load-event flag back to true (this delays the load event again, in case
2702 // it hasn't been fired yet).
2703 setShouldDelayLoadEvent(true);
2705 // 24. Set the networkState back to NETWORK_LOADING.
2706 m_networkState = NETWORK_LOADING;
2708 // 25. Jump back to the find next candidate step above.
2709 m_nextChildNodeToConsider = source;
2710 scheduleNextSourceChild();
2713 void HTMLMediaElement::sourceWasRemoved(HTMLSourceElement* source)
2715 WTF_LOG(Media, "HTMLMediaElement::sourceWasRemoved(%p)", source);
2718 KURL url = source->getNonEmptyURLAttribute(srcAttr);
2719 WTF_LOG(Media, "HTMLMediaElement::sourceWasRemoved - 'src' is %s", urlForLoggingMedia(url).utf8().data());
2722 if (source != m_currentSourceNode && source != m_nextChildNodeToConsider)
2725 if (source == m_nextChildNodeToConsider) {
2726 if (m_currentSourceNode)
2727 m_nextChildNodeToConsider = m_currentSourceNode->nextSibling();
2728 WTF_LOG(Media, "HTMLMediaElement::sourceRemoved - m_nextChildNodeToConsider set to %p", m_nextChildNodeToConsider.get());
2729 } else if (source == m_currentSourceNode) {
2730 // Clear the current source node pointer, but don't change the movie as the spec says:
2731 // 4.8.8 - Dynamically modifying a source element and its attribute when the element is already
2732 // inserted in a video or audio element will have no effect.
2733 m_currentSourceNode = nullptr;
2734 WTF_LOG(Media, "HTMLMediaElement::sourceRemoved - m_currentSourceNode set to 0");
2738 void HTMLMediaElement::mediaPlayerTimeChanged()
2740 WTF_LOG(Media, "HTMLMediaElement::mediaPlayerTimeChanged");
2742 if (RuntimeEnabledFeatures::videoTrackEnabled())
2743 updateActiveTextTrackCues(currentTime());
2745 invalidateCachedTime();
2747 // 4.8.10.9 steps 12-14. Needed if no ReadyState change is associated with the seek.
2748 if (m_seeking && m_readyState >= HAVE_CURRENT_DATA && !m_player->seeking())
2751 // Always call scheduleTimeupdateEvent when the media engine reports a time discontinuity,
2752 // it will only queue a 'timeupdate' event if we haven't already posted one at the current
2754 scheduleTimeupdateEvent(false);
2756 double now = currentTime();
2757 double dur = duration();
2759 // When the current playback position reaches the end of the media resource when the direction of
2760 // playback is forwards, then the user agent must follow these steps:
2761 if (!std::isnan(dur) && dur && now >= dur && m_playbackRate > 0) {
2762 // If the media element has a loop attribute specified and does not have a current media controller,
2763 if (loop() && !m_mediaController) {
2764 m_sentEndEvent = false;
2765 // then seek to the earliest possible position of the media resource and abort these steps.
2766 seek(0, IGNORE_EXCEPTION);
2768 // If the media element does not have a current media controller, and the media element
2769 // has still ended playback, and the direction of playback is still forwards, and paused
2771 if (!m_mediaController && !m_paused) {
2772 // changes paused to true and fires a simple event named pause at the media element.
2774 scheduleEvent(EventTypeNames::pause);
2776 // Queue a task to fire a simple event named ended at the media element.
2777 if (!m_sentEndEvent) {
2778 m_sentEndEvent = true;
2779 scheduleEvent(EventTypeNames::ended);
2781 // If the media element has a current media controller, then report the controller state
2782 // for the media element's current media controller.
2783 updateMediaController();
2787 m_sentEndEvent = false;
2792 void HTMLMediaElement::mediaPlayerDurationChanged()
2794 WTF_LOG(Media, "HTMLMediaElement::mediaPlayerDurationChanged");
2795 durationChanged(duration());
2798 void HTMLMediaElement::durationChanged(double duration)
2800 WTF_LOG(Media, "HTMLMediaElement::durationChanged(%f)", duration);
2802 // Abort if duration unchanged.
2803 if (m_duration == duration)
2806 m_duration = duration;
2807 scheduleEvent(EventTypeNames::durationchange);
2809 if (hasMediaControls())
2810 mediaControls()->reset();
2812 renderer()->updateFromElement();
2814 if (currentTime() > duration)
2815 seek(duration, IGNORE_EXCEPTION);
2818 void HTMLMediaElement::mediaPlayerPlaybackStateChanged()
2820 WTF_LOG(Media, "HTMLMediaElement::mediaPlayerPlaybackStateChanged");
2822 if (!m_player || m_pausedInternal)
2825 if (m_player->paused())
2831 void HTMLMediaElement::mediaPlayerRequestFullscreen()
2833 WTF_LOG(Media, "HTMLMediaElement::mediaPlayerRequestFullscreen");
2835 // The player is responsible for only invoking this callback in response to
2836 // user interaction or when it is technically required to play the video.
2837 UserGestureIndicator gestureIndicator(DefinitelyProcessingNewUserGesture);
2842 void HTMLMediaElement::mediaPlayerRequestSeek(double time)
2844 // The player is the source of this seek request.
2845 if (m_mediaController) {
2846 m_mediaController->setCurrentTime(time, IGNORE_EXCEPTION);
2849 setCurrentTime(time, IGNORE_EXCEPTION);
2852 // MediaPlayerPresentation methods
2853 void HTMLMediaElement::mediaPlayerRepaint()
2856 m_webLayer->invalidate();
2858 updateDisplayState();
2860 renderer()->repaint();
2863 void HTMLMediaElement::mediaPlayerSizeChanged()
2865 WTF_LOG(Media, "HTMLMediaElement::mediaPlayerSizeChanged");
2867 ASSERT(hasVideo()); // "resize" makes no sense absent video.
2868 if (m_readyState > HAVE_NOTHING && isHTMLVideoElement(*this))
2869 scheduleEvent(EventTypeNames::resize);
2872 renderer()->updateFromElement();
2875 PassRefPtr<TimeRanges> HTMLMediaElement::buffered() const
2878 return TimeRanges::create();
2881 return m_mediaSource->buffered();
2883 return m_player->buffered();
2886 PassRefPtr<TimeRanges> HTMLMediaElement::played()
2889 double time = currentTime();
2890 if (time > m_lastSeekTime)
2891 addPlayedRange(m_lastSeekTime, time);
2894 if (!m_playedTimeRanges)
2895 m_playedTimeRanges = TimeRanges::create();
2897 return m_playedTimeRanges->copy();
2900 PassRefPtr<TimeRanges> HTMLMediaElement::seekable() const
2903 double maxTimeSeekable = m_player->maxTimeSeekable();
2904 if (maxTimeSeekable)
2905 return TimeRanges::create(0, maxTimeSeekable);
2907 return TimeRanges::create();
2910 bool HTMLMediaElement::potentiallyPlaying() const
2912 // "pausedToBuffer" means the media engine's rate is 0, but only because it had to stop playing
2913 // when it ran out of buffered data. A movie is this state is "potentially playing", modulo the
2914 // checks in couldPlayIfEnoughData().
2915 bool pausedToBuffer = m_readyStateMaximum >= HAVE_FUTURE_DATA && m_readyState < HAVE_FUTURE_DATA;
2916 return (pausedToBuffer || m_readyState >= HAVE_FUTURE_DATA) && couldPlayIfEnoughData() && !isBlockedOnMediaController();
2919 bool HTMLMediaElement::couldPlayIfEnoughData() const
2921 return !paused() && !endedPlayback() && !stoppedDueToErrors() && !pausedForUserInteraction();
2924 bool HTMLMediaElement::endedPlayback() const
2926 double dur = duration();
2927 if (!m_player || std::isnan(dur))
2930 // 4.8.10.8 Playing the media resource
2932 // A media element is said to have ended playback when the element's
2933 // readyState attribute is HAVE_METADATA or greater,
2934 if (m_readyState < HAVE_METADATA)
2937 // and the current playback position is the end of the media resource and the direction
2938 // of playback is forwards, Either the media element does not have a loop attribute specified,
2939 // or the media element has a current media controller.
2940 double now = currentTime();
2941 if (m_playbackRate > 0)
2942 return dur > 0 && now >= dur && (!loop() || m_mediaController);
2944 // or the current playback position is the earliest possible position and the direction
2945 // of playback is backwards
2946 if (m_playbackRate < 0)
2952 bool HTMLMediaElement::stoppedDueToErrors() const
2954 if (m_readyState >= HAVE_METADATA && m_error) {
2955 RefPtr<TimeRanges> seekableRanges = seekable();
2956 if (!seekableRanges->contain(currentTime()))
2963 bool HTMLMediaElement::pausedForUserInteraction() const
2965 // return !paused() && m_readyState >= HAVE_FUTURE_DATA && [UA requires a decitions from the user]
2969 void HTMLMediaElement::updateVolume()
2971 if (webMediaPlayer())
2972 webMediaPlayer()->setVolume(playerVolume());
2974 if (hasMediaControls())
2975 mediaControls()->changedVolume();
2978 double HTMLMediaElement::playerVolume() const
2980 double volumeMultiplier = 1;
2981 bool shouldMute = m_muted;
2983 if (m_mediaController) {
2984 volumeMultiplier *= m_mediaController->volume();
2985 shouldMute = m_mediaController->muted();
2988 return shouldMute ? 0 : m_volume * volumeMultiplier;
2991 void HTMLMediaElement::updatePlayState()
2996 if (m_pausedInternal) {
2997 if (!m_player->paused())
2999 refreshCachedTime();
3000 m_playbackProgressTimer.stop();
3001 if (hasMediaControls())
3002 mediaControls()->playbackStopped();
3006 bool shouldBePlaying = potentiallyPlaying();
3007 bool playerPaused = m_player->paused();
3009 WTF_LOG(Media, "HTMLMediaElement::updatePlayState - shouldBePlaying = %s, playerPaused = %s",
3010 boolString(shouldBePlaying), boolString(playerPaused));
3012 if (shouldBePlaying) {
3013 setDisplayMode(Video);
3014 invalidateCachedTime();
3017 // Set rate, muted before calling play in case they were set before the media engine was setup.
3018 // The media engine should just stash the rate and muted values since it isn't already playing.
3019 m_player->setRate(m_playbackRate);
3025 if (hasMediaControls())
3026 mediaControls()->playbackStarted();
3027 startPlaybackProgressTimer();
3030 } else { // Should not be playing right now
3033 refreshCachedTime();
3035 m_playbackProgressTimer.stop();
3037 double time = currentTime();
3038 if (time > m_lastSeekTime)
3039 addPlayedRange(m_lastSeekTime, time);
3041 if (couldPlayIfEnoughData())
3044 if (hasMediaControls())
3045 mediaControls()->playbackStopped();
3048 updateMediaController();
3051 renderer()->updateFromElement();
3054 void HTMLMediaElement::setPausedInternal(bool b)
3056 m_pausedInternal = b;
3060 void HTMLMediaElement::stopPeriodicTimers()
3062 m_progressEventTimer.stop();
3063 m_playbackProgressTimer.stop();
3066 void HTMLMediaElement::userCancelledLoad()
3068 WTF_LOG(Media, "HTMLMediaElement::userCancelledLoad");
3070 // If the media data fetching process is aborted by the user:
3072 // 1 - The user agent should cancel the fetching process.
3073 clearMediaPlayer(-1);
3075 if (m_networkState == NETWORK_EMPTY || m_completelyLoaded)
3078 // 2 - Set the error attribute to a new MediaError object whose code attribute is set to MEDIA_ERR_ABORTED.
3079 m_error = MediaError::create(MediaError::MEDIA_ERR_ABORTED);
3081 // 3 - Queue a task to fire a simple event named error at the media element.
3082 scheduleEvent(EventTypeNames::abort);
3086 // 4 - If the media element's readyState attribute has a value equal to HAVE_NOTHING, set the
3087 // element's networkState attribute to the NETWORK_EMPTY value and queue a task to fire a
3088 // simple event named emptied at the element. Otherwise, set the element's networkState
3089 // attribute to the NETWORK_IDLE value.
3090 if (m_readyState == HAVE_NOTHING) {
3091 m_networkState = NETWORK_EMPTY;
3092 scheduleEvent(EventTypeNames::emptied);
3095 m_networkState = NETWORK_IDLE;
3097 // 5 - Set the element's delaying-the-load-event flag to false. This stops delaying the load event.
3098 setShouldDelayLoadEvent(false);
3100 // 6 - Abort the overall resource selection algorithm.
3101 m_currentSourceNode = nullptr;
3103 // Reset m_readyState since m_player is gone.
3104 m_readyState = HAVE_NOTHING;
3105 updateMediaController();
3106 if (RuntimeEnabledFeatures::videoTrackEnabled())
3107 updateActiveTextTrackCues(0);
3110 void HTMLMediaElement::clearMediaPlayerAndAudioSourceProviderClient()
3112 #if ENABLE(WEB_AUDIO)
3113 if (m_audioSourceNode)
3114 m_audioSourceNode->lock();
3116 if (audioSourceProvider())
3117 audioSourceProvider()->setClient(0);
3122 #if ENABLE(WEB_AUDIO)
3123 if (m_audioSourceNode)
3124 m_audioSourceNode->unlock();
3128 void HTMLMediaElement::clearMediaPlayer(int flags)
3130 forgetResourceSpecificTracks();
3134 m_delayingLoadForPreloadNone = false;
3136 clearMediaPlayerAndAudioSourceProviderClient();
3138 stopPeriodicTimers();
3141 m_pendingActionFlags &= ~flags;
3142 m_loadState = WaitingForSource;
3145 configureTextTrackDisplay(AssumeNoVisibleChange);
3148 void HTMLMediaElement::stop()
3150 WTF_LOG(Media, "HTMLMediaElement::stop");
3153 userCancelledLoad();
3155 // Stop the playback without generating events
3157 setPausedInternal(true);
3160 renderer()->updateFromElement();
3162 stopPeriodicTimers();
3163 cancelPendingEventsAndCallbacks();
3165 m_asyncEventQueue->close();
3168 bool HTMLMediaElement::hasPendingActivity() const
3170 return (hasAudio() && isPlaying()) || m_asyncEventQueue->hasPendingEvents();
3173 void HTMLMediaElement::contextDestroyed()
3175 if (m_mediaController)
3176 m_mediaController->clearExecutionContext();
3177 ActiveDOMObject::contextDestroyed();
3180 bool HTMLMediaElement::isFullscreen() const
3182 return FullscreenElementStack::isActiveFullScreenElement(this);
3185 void HTMLMediaElement::enterFullscreen()
3187 WTF_LOG(Media, "HTMLMediaElement::enterFullscreen");
3189 if (document().settings() && document().settings()->fullScreenEnabled())
3190 FullscreenElementStack::from(document()).requestFullScreenForElement(this, 0, FullscreenElementStack::ExemptIFrameAllowFullScreenRequirement);
3193 void HTMLMediaElement::exitFullscreen()
3195 WTF_LOG(Media, "HTMLMediaElement::exitFullscreen");
3197 if (document().settings() && document().settings()->fullScreenEnabled() && isFullscreen())
3198 FullscreenElementStack::from(document()).webkitCancelFullScreen();
3201 void HTMLMediaElement::didBecomeFullscreenElement()
3203 if (hasMediaControls())
3204 mediaControls()->enteredFullscreen();
3205 if (RuntimeEnabledFeatures::overlayFullscreenVideoEnabled() && isHTMLVideoElement(*this))
3206 document().renderView()->compositor()->setCompositingLayersNeedRebuild();
3209 void HTMLMediaElement::willStopBeingFullscreenElement()
3211 if (hasMediaControls())
3212 mediaControls()->exitedFullscreen();
3213 if (RuntimeEnabledFeatures::overlayFullscreenVideoEnabled() && isHTMLVideoElement(*this))
3214 document().renderView()->compositor()->setCompositingLayersNeedRebuild();
3217 blink::WebLayer* HTMLMediaElement::platformLayer() const
3222 bool HTMLMediaElement::hasClosedCaptions() const
3224 if (RuntimeEnabledFeatures::videoTrackEnabled() && m_textTracks) {
3225 for (unsigned i = 0; i < m_textTracks->length(); ++i) {
3226 if (m_textTracks->item(i)->readinessState() == TextTrack::FailedToLoad)
3229 if (m_textTracks->item(i)->kind() == TextTrack::captionsKeyword()
3230 || m_textTracks->item(i)->kind() == TextTrack::subtitlesKeyword())
3237 bool HTMLMediaElement::closedCaptionsVisible() const
3239 return m_closedCaptionsVisible;
3242 void HTMLMediaElement::updateTextTrackDisplay()
3244 WTF_LOG(Media, "HTMLMediaElement::updateTextTrackDisplay");
3246 if (!hasMediaControls() && !createMediaControls())
3249 mediaControls()->updateTextTrackDisplay();
3252 void HTMLMediaElement::setClosedCaptionsVisible(bool closedCaptionVisible)
3254 WTF_LOG(Media, "HTMLMediaElement::setClosedCaptionsVisible(%s)", boolString(closedCaptionVisible));
3256 if (!m_player || !hasClosedCaptions())
3259 m_closedCaptionsVisible = closedCaptionVisible;
3261 if (RuntimeEnabledFeatures::videoTrackEnabled()) {
3262 m_processingPreferenceChange = true;
3263 markCaptionAndSubtitleTracksAsUnconfigured();
3264 m_processingPreferenceChange = false;
3266 updateTextTrackDisplay();
3270 unsigned HTMLMediaElement::webkitAudioDecodedByteCount() const
3274 return m_player->audioDecodedByteCount();
3277 unsigned HTMLMediaElement::webkitVideoDecodedByteCount() const
3281 return m_player->videoDecodedByteCount();
3284 bool HTMLMediaElement::isURLAttribute(const Attribute& attribute) const
3286 return attribute.name() == srcAttr || HTMLElement::isURLAttribute(attribute);
3289 void HTMLMediaElement::setShouldDelayLoadEvent(bool shouldDelay)
3291 if (m_shouldDelayLoadEvent == shouldDelay)
3294 WTF_LOG(Media, "HTMLMediaElement::setShouldDelayLoadEvent(%s)", boolString(shouldDelay));
3296 m_shouldDelayLoadEvent = shouldDelay;
3298 document().incrementLoadEventDelayCount();
3300 document().decrementLoadEventDelayCount();
3304 MediaControls* HTMLMediaElement::mediaControls() const
3306 return toMediaControls(userAgentShadowRoot()->firstChild());
3309 bool HTMLMediaElement::hasMediaControls() const
3311 if (ShadowRoot* userAgent = userAgentShadowRoot()) {
3312 Node* node = userAgent->firstChild();
3313 ASSERT_WITH_SECURITY_IMPLICATION(!node || node->isMediaControls());
3320 bool HTMLMediaElement::createMediaControls()
3322 if (hasMediaControls())
3325 RefPtr<MediaControls> mediaControls = MediaControls::create(*this);
3329 mediaControls->reset();
3331 mediaControls->enteredFullscreen();
3333 ensureUserAgentShadowRoot().appendChild(mediaControls);
3335 if (!controls() || !inDocument())
3336 mediaControls->hide();
3341 void HTMLMediaElement::configureMediaControls()
3343 if (!controls() || !inDocument()) {
3344 if (hasMediaControls())
3345 mediaControls()->hide();
3349 if (!hasMediaControls() && !createMediaControls())
3352 mediaControls()->reset();
3353 mediaControls()->show();
3356 void HTMLMediaElement::configureTextTrackDisplay(VisibilityChangeAssumption assumption)
3358 ASSERT(m_textTracks);
3359 WTF_LOG(Media, "HTMLMediaElement::configureTextTrackDisplay");
3361 if (m_processingPreferenceChange)
3364 bool haveVisibleTextTrack = false;
3365 for (unsigned i = 0; i < m_textTracks->length(); ++i) {
3366 if (m_textTracks->item(i)->mode() == TextTrack::showingKeyword()) {
3367 haveVisibleTextTrack = true;
3372 if (assumption == AssumeNoVisibleChange
3373 && m_haveVisibleTextTrack == haveVisibleTextTrack) {
3374 updateActiveTextTrackCues(currentTime());
3377 m_haveVisibleTextTrack = haveVisibleTextTrack;
3378 m_closedCaptionsVisible = m_haveVisibleTextTrack;
3380 if (!m_haveVisibleTextTrack && !hasMediaControls())
3382 if (!hasMediaControls() && !createMediaControls())
3385 mediaControls()->changedClosedCaptionsVisibility();
3387 if (RuntimeEnabledFeatures::videoTrackEnabled()) {
3388 updateActiveTextTrackCues(currentTime());
3389 updateTextTrackDisplay();
3393 void HTMLMediaElement::markCaptionAndSubtitleTracksAsUnconfigured()
3398 // Mark all tracks as not "configured" so that configureTextTracks()
3399 // will reconsider which tracks to display in light of new user preferences
3400 // (e.g. default tracks should not be displayed if the user has turned off
3401 // captions and non-default tracks should be displayed based on language
3402 // preferences if the user has turned captions on).
3403 for (unsigned i = 0; i < m_textTracks->length(); ++i) {
3404 RefPtr<TextTrack> textTrack = m_textTracks->item(i);
3405 String kind = textTrack->kind();
3407 if (kind == TextTrack::subtitlesKeyword() || kind == TextTrack::captionsKeyword())
3408 textTrack->setHasBeenConfigured(false);
3410 configureTextTracks();
3413 bool HTMLMediaElement::willRespondToMouseClickEvents()
3418 void* HTMLMediaElement::preDispatchEventHandler(Event* event)
3420 if (event && event->type() == EventTypeNames::webkitfullscreenchange)
3421 configureMediaControls();
3426 void HTMLMediaElement::defaultEventHandler(Event* event)
3428 if (event->type() == EventTypeNames::click && willRespondToMouseClickEvents()) {
3430 event->setDefaultHandled();
3433 HTMLElement::defaultEventHandler(event);
3436 void HTMLMediaElement::createMediaPlayer()
3438 #if ENABLE(WEB_AUDIO)
3439 if (m_audioSourceNode)
3440 m_audioSourceNode->lock();
3446 m_player = MediaPlayer::create(this);
3448 #if ENABLE(WEB_AUDIO)
3449 if (m_audioSourceNode) {
3450 // When creating the player, make sure its AudioSourceProvider knows about the MediaElementAudioSourceNode.
3451 if (audioSourceProvider())
3452 audioSourceProvider()->setClient(m_audioSourceNode);
3454 m_audioSourceNode->unlock();
3459 #if ENABLE(WEB_AUDIO)
3460 void HTMLMediaElement::setAudioSourceNode(MediaElementAudioSourceNode* sourceNode)
3462 m_audioSourceNode = sourceNode;
3464 if (m_audioSourceNode)
3465 m_audioSourceNode->lock();
3467 if (audioSourceProvider())
3468 audioSourceProvider()->setClient(m_audioSourceNode);
3470 if (m_audioSourceNode)
3471 m_audioSourceNode->unlock();
3474 AudioSourceProvider* HTMLMediaElement::audioSourceProvider()
3477 return m_player->audioSourceProvider();
3483 const AtomicString& HTMLMediaElement::mediaGroup() const
3485 return fastGetAttribute(mediagroupAttr);
3488 void HTMLMediaElement::setMediaGroup(const AtomicString& group)
3490 // When a media element is created with a mediagroup attribute, and when a media element's mediagroup
3491 // attribute is set, changed, or removed, the user agent must run the following steps:
3492 // 1. Let m [this] be the media element in question.
3493 // 2. Let m have no current media controller, if it currently has one.
3494 setControllerInternal(nullptr);
3496 // 3. If m's mediagroup attribute is being removed, then abort these steps.
3497 if (group.isNull() || group.isEmpty())
3500 // 4. If there is another media element whose Document is the same as m's Document (even if one or both
3501 // of these elements are not actually in the Document),
3502 HashSet<HTMLMediaElement*> elements = documentToElementSetMap().get(&document());
3503 for (HashSet<HTMLMediaElement*>::iterator i = elements.begin(); i != elements.end(); ++i) {
3507 // and which also has a mediagroup attribute, and whose mediagroup attribute has the same value as
3508 // the new value of m's mediagroup attribute,
3509 if ((*i)->mediaGroup() == group) {
3510 // then let controller be that media element's current media controller.
3511 setControllerInternal((*i)->controller());
3516 // Otherwise, let controller be a newly created MediaController.
3517 setControllerInternal(MediaController::create(Node::executionContext()));
3520 MediaController* HTMLMediaElement::controller() const
3522 return m_mediaController.get();
3525 void HTMLMediaElement::setController(PassRefPtr<MediaController> controller)
3527 // 4.8.10.11.2 Media controllers: controller attribute.
3528 // On setting, it must first remove the element's mediagroup attribute, if any,
3529 removeAttribute(mediagroupAttr);
3530 // and then set the current media controller to the given value.
3531 setControllerInternal(controller);
3534 void HTMLMediaElement::setControllerInternal(PassRefPtr<MediaController> controller)
3536 if (m_mediaController)
3537 m_mediaController->removeMediaElement(this);
3539 m_mediaController = controller;
3541 if (m_mediaController) {
3542 UseCounter::count(document(), UseCounter::HTMLMediaElementControllerNotNull);
3543 m_mediaController->addMediaElement(this);
3547 void HTMLMediaElement::updateMediaController()
3549 if (m_mediaController)
3550 m_mediaController->reportControllerState();
3553 bool HTMLMediaElement::isBlocked() const
3555 // A media element is a blocked media element if its readyState attribute is in the
3556 // HAVE_NOTHING state, the HAVE_METADATA state, or the HAVE_CURRENT_DATA state,
3557 if (m_readyState <= HAVE_CURRENT_DATA)
3560 // or if the element has paused for user interaction.
3561 return pausedForUserInteraction();
3564 bool HTMLMediaElement::isBlockedOnMediaController() const
3566 if (!m_mediaController)
3569 // A media element is blocked on its media controller if the MediaController is a blocked
3570 // media controller,
3571 if (m_mediaController->isBlocked())
3574 // or if its media controller position is either before the media resource's earliest possible
3575 // position relative to the MediaController's timeline or after the end of the media resource
3576 // relative to the MediaController's timeline.
3577 double mediaControllerPosition = m_mediaController->currentTime();
3578 if (mediaControllerPosition < 0 || mediaControllerPosition > duration())
3584 void HTMLMediaElement::prepareMediaFragmentURI()
3586 MediaFragmentURIParser fragmentParser(m_currentSrc);
3587 double dur = duration();
3589 double start = fragmentParser.startTime();
3590 if (start != MediaFragmentURIParser::invalidTimeValue() && start > 0) {
3591 m_fragmentStartTime = start;
3592 if (m_fragmentStartTime > dur)
3593 m_fragmentStartTime = dur;
3595 m_fragmentStartTime = MediaPlayer::invalidTime();
3597 double end = fragmentParser.endTime();
3598 if (end != MediaFragmentURIParser::invalidTimeValue() && end > 0 && end > m_fragmentStartTime) {
3599 m_fragmentEndTime = end;
3600 if (m_fragmentEndTime > dur)
3601 m_fragmentEndTime = dur;
3603 m_fragmentEndTime = MediaPlayer::invalidTime();
3605 if (m_fragmentStartTime != MediaPlayer::invalidTime() && m_readyState < HAVE_FUTURE_DATA)
3609 void HTMLMediaElement::applyMediaFragmentURI()
3611 if (m_fragmentStartTime != MediaPlayer::invalidTime()) {
3612 m_sentEndEvent = false;
3613 UseCounter::count(document(), UseCounter::HTMLMediaElementSeekToFragmentStart);
3614 seek(m_fragmentStartTime, IGNORE_EXCEPTION);
3618 WebMediaPlayer::CORSMode HTMLMediaElement::corsMode() const
3620 const AtomicString& crossOriginMode = fastGetAttribute(crossoriginAttr);
3621 if (crossOriginMode.isNull())
3622 return WebMediaPlayer::CORSModeUnspecified;
3623 if (equalIgnoringCase(crossOriginMode, "use-credentials"))
3624 return WebMediaPlayer::CORSModeUseCredentials;
3625 return WebMediaPlayer::CORSModeAnonymous;
3628 void HTMLMediaElement::mediaPlayerSetWebLayer(blink::WebLayer* webLayer)
3630 if (webLayer == m_webLayer)
3633 // If either of the layers is null we need to enable or disable compositing. This is done by triggering a style recalc.
3634 if (!m_webLayer || !webLayer)
3635 scheduleLayerUpdate();
3638 GraphicsLayer::unregisterContentsLayer(m_webLayer);
3639 m_webLayer = webLayer;
3641 m_webLayer->setOpaque(m_opaque);
3642 GraphicsLayer::registerContentsLayer(m_webLayer);
3646 void HTMLMediaElement::mediaPlayerSetOpaque(bool opaque)
3650 m_webLayer->setOpaque(m_opaque);
3653 void HTMLMediaElement::mediaPlayerMediaSourceOpened(blink::WebMediaSource* webMediaSource)
3655 m_mediaSource->setWebMediaSourceAndOpen(adoptPtr(webMediaSource));
3658 bool HTMLMediaElement::isInteractiveContent() const
3660 return fastHasAttribute(controlsAttr);