2 * Copyright (C) 2007-2015 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 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 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.
28 #include "HTMLMediaElement.h"
30 #include "ApplicationCacheHost.h"
31 #include "ApplicationCacheResource.h"
32 #include "Attribute.h"
33 #include "CSSPropertyNames.h"
34 #include "CSSValueKeywords.h"
35 #include "ChromeClient.h"
36 #include "ClientRect.h"
37 #include "ClientRectList.h"
38 #include "ContentSecurityPolicy.h"
39 #include "ContentType.h"
40 #include "CookieJar.h"
41 #include "DiagnosticLoggingClient.h"
42 #include "DiagnosticLoggingKeys.h"
43 #include "DisplaySleepDisabler.h"
44 #include "DocumentLoader.h"
45 #include "ElementIterator.h"
46 #include "EventNames.h"
47 #include "ExceptionCodePlaceholder.h"
48 #include "FrameLoader.h"
49 #include "FrameLoaderClient.h"
50 #include "FrameView.h"
51 #include "HTMLSourceElement.h"
52 #include "HTMLVideoElement.h"
53 #include "JSHTMLMediaElement.h"
56 #include "MIMETypeRegistry.h"
57 #include "MainFrame.h"
58 #include "MediaController.h"
59 #include "MediaControls.h"
60 #include "MediaDocument.h"
61 #include "MediaError.h"
62 #include "MediaFragmentURIParser.h"
63 #include "MediaKeyEvent.h"
64 #include "MediaList.h"
65 #include "MediaPlayer.h"
66 #include "MediaQueryEvaluator.h"
67 #include "MediaResourceLoader.h"
68 #include "MemoryPressureHandler.h"
69 #include "NetworkingContext.h"
70 #include "PageGroup.h"
71 #include "PageThrottler.h"
72 #include "PlatformMediaSessionManager.h"
73 #include "ProgressTracker.h"
74 #include "RenderLayerCompositor.h"
75 #include "RenderVideo.h"
76 #include "RenderView.h"
77 #include "ResourceLoadInfo.h"
78 #include "ScriptController.h"
79 #include "ScriptSourceCode.h"
80 #include "SecurityPolicy.h"
81 #include "SessionID.h"
83 #include "ShadowRoot.h"
84 #include "TimeRanges.h"
85 #include "UserContentController.h"
87 #include <runtime/Uint8Array.h>
88 #include <wtf/CurrentTime.h>
89 #include <wtf/MathExtras.h>
91 #include <wtf/text/CString.h>
93 #if ENABLE(VIDEO_TRACK)
94 #include "AudioTrackList.h"
95 #include "HTMLTrackElement.h"
96 #include "InbandGenericTextTrack.h"
97 #include "InbandTextTrackPrivate.h"
98 #include "InbandWebVTTTextTrack.h"
99 #include "RuntimeEnabledFeatures.h"
100 #include "TextTrackCueList.h"
101 #include "TextTrackList.h"
102 #include "VideoTrackList.h"
105 #if ENABLE(WEB_AUDIO)
106 #include "AudioSourceProvider.h"
107 #include "MediaElementAudioSourceNode.h"
111 #include "RuntimeApplicationChecksIOS.h"
114 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
115 #include "WebKitPlaybackTargetAvailabilityEvent.h"
118 #if ENABLE(MEDIA_SESSION)
119 #include "MediaSession.h"
122 #if ENABLE(MEDIA_SOURCE)
123 #include "DOMWindow.h"
124 #include "MediaSource.h"
125 #include "Performance.h"
126 #include "VideoPlaybackQuality.h"
129 #if ENABLE(MEDIA_STREAM)
130 #include "MediaStream.h"
131 #include "MediaStreamRegistry.h"
134 #if ENABLE(ENCRYPTED_MEDIA_V2)
135 #include "MediaKeyNeededEvent.h"
136 #include "MediaKeys.h"
139 #if USE(PLATFORM_TEXT_TRACK_MENU)
140 #include "PlatformTextTrack.h"
143 #if ENABLE(MEDIA_CONTROLS_SCRIPT)
144 #include "JSMediaControlsHost.h"
145 #include "MediaControlsHost.h"
146 #include "ScriptGlobalObject.h"
147 #include <bindings/ScriptObject.h>
152 static const double SeekRepeatDelay = 0.1;
153 static const double SeekTime = 0.2;
154 static const double ScanRepeatDelay = 1.5;
155 static const double ScanMaximumRate = 8;
157 static void setFlags(unsigned& value, unsigned flags)
162 static void clearFlags(unsigned& value, unsigned flags)
168 static String urlForLoggingMedia(const URL& url)
170 static const unsigned maximumURLLengthForLogging = 128;
172 if (url.string().length() < maximumURLLengthForLogging)
174 return url.string().substring(0, maximumURLLengthForLogging) + "...";
177 static const char* boolString(bool val)
179 return val ? "true" : "false";
183 #ifndef LOG_MEDIA_EVENTS
184 // Default to not logging events because so many are generated they can overwhelm the rest of
186 #define LOG_MEDIA_EVENTS 0
189 #ifndef LOG_CACHED_TIME_WARNINGS
190 // Default to not logging warnings about excessive drift in the cached media time because it adds a
191 // fair amount of overhead and logging.
192 #define LOG_CACHED_TIME_WARNINGS 0
195 #if ENABLE(MEDIA_SOURCE)
196 // URL protocol used to signal that the media source API is being used.
197 static const char* mediaSourceBlobProtocol = "blob";
200 #if ENABLE(MEDIA_STREAM)
201 // URL protocol used to signal that the media stream API is being used.
202 static const char* mediaStreamBlobProtocol = "blob";
205 using namespace HTMLNames;
207 typedef HashMap<Document*, HashSet<HTMLMediaElement*>> DocumentElementSetMap;
208 static DocumentElementSetMap& documentToElementSetMap()
210 DEPRECATED_DEFINE_STATIC_LOCAL(DocumentElementSetMap, map, ());
214 static void addElementToDocumentMap(HTMLMediaElement& element, Document& document)
216 DocumentElementSetMap& map = documentToElementSetMap();
217 HashSet<HTMLMediaElement*> set = map.take(&document);
219 map.add(&document, set);
222 static void removeElementFromDocumentMap(HTMLMediaElement& element, Document& document)
224 DocumentElementSetMap& map = documentToElementSetMap();
225 HashSet<HTMLMediaElement*> set = map.take(&document);
226 set.remove(&element);
228 map.add(&document, set);
231 #if ENABLE(ENCRYPTED_MEDIA)
232 static ExceptionCode exceptionCodeForMediaKeyException(MediaPlayer::MediaKeyException exception)
235 case MediaPlayer::NoError:
237 case MediaPlayer::InvalidPlayerState:
238 return INVALID_STATE_ERR;
239 case MediaPlayer::KeySystemNotSupported:
240 return NOT_SUPPORTED_ERR;
243 ASSERT_NOT_REACHED();
244 return INVALID_STATE_ERR;
248 #if ENABLE(VIDEO_TRACK)
249 class TrackDisplayUpdateScope {
251 TrackDisplayUpdateScope(HTMLMediaElement* mediaElement)
253 m_mediaElement = mediaElement;
254 m_mediaElement->beginIgnoringTrackDisplayUpdateRequests();
256 ~TrackDisplayUpdateScope()
258 ASSERT(m_mediaElement);
259 m_mediaElement->endIgnoringTrackDisplayUpdateRequests();
263 HTMLMediaElement* m_mediaElement;
267 struct HTMLMediaElement::TrackGroup {
268 enum GroupKind { CaptionsAndSubtitles, Description, Chapter, Metadata, Other };
270 TrackGroup(GroupKind kind)
278 Vector<RefPtr<TextTrack>> tracks;
279 RefPtr<TextTrack> visibleTrack;
280 RefPtr<TextTrack> defaultTrack;
285 HashSet<HTMLMediaElement*>& HTMLMediaElement::allMediaElements()
287 static NeverDestroyed<HashSet<HTMLMediaElement*>> elements;
291 HTMLMediaElement::HTMLMediaElement(const QualifiedName& tagName, Document& document, bool createdByParser)
292 : HTMLElement(tagName, document)
293 , ActiveDOMObject(&document)
294 , m_pendingActionTimer(*this, &HTMLMediaElement::pendingActionTimerFired)
295 , m_progressEventTimer(*this, &HTMLMediaElement::progressEventTimerFired)
296 , m_playbackProgressTimer(*this, &HTMLMediaElement::playbackProgressTimerFired)
297 , m_scanTimer(*this, &HTMLMediaElement::scanTimerFired)
298 , m_seekTaskQueue(document)
299 , m_resizeTaskQueue(document)
300 , m_shadowDOMTaskQueue(document)
301 , m_playedTimeRanges()
302 , m_asyncEventQueue(*this)
303 , m_requestedPlaybackRate(1)
304 , m_reportedPlaybackRate(1)
305 , m_defaultPlaybackRate(1)
306 , m_webkitPreservesPitch(true)
307 , m_networkState(NETWORK_EMPTY)
308 , m_readyState(HAVE_NOTHING)
309 , m_readyStateMaximum(HAVE_NOTHING)
311 , m_volumeInitialized(false)
312 , m_previousProgressTime(std::numeric_limits<double>::max())
313 , m_clockTimeAtLastUpdateEvent(0)
314 , m_lastTimeUpdateEventMovieTime(MediaTime::positiveInfiniteTime())
315 , m_loadState(WaitingForSource)
316 , m_videoFullscreenMode(VideoFullscreenModeNone)
318 , m_videoFullscreenGravity(MediaPlayer::VideoGravityResizeAspect)
320 , m_preload(MediaPlayer::Auto)
321 , m_displayMode(Unknown)
322 , m_processingMediaPlayerCallback(0)
323 #if ENABLE(MEDIA_SOURCE)
324 , m_droppedVideoFrames(0)
326 , m_clockTimeAtLastCachedTimeUpdate(0)
327 , m_minimumClockTimeToUpdateCachedTime(0)
328 , m_pendingActionFlags(0)
329 , m_actionAfterScan(Nothing)
331 , m_scanDirection(Forward)
332 , m_firstTimePlaying(true)
334 , m_isWaitingUntilMediaCanStart(false)
335 , m_shouldDelayLoadEvent(false)
336 , m_haveFiredLoadedData(false)
337 , m_inActiveDocument(true)
338 , m_autoplaying(true)
340 , m_explicitlyMuted(false)
341 , m_initiallyMuted(false)
344 , m_sentStalledEvent(false)
345 , m_sentEndEvent(false)
346 , m_pausedInternal(false)
347 , m_sendProgressEvents(true)
348 , m_closedCaptionsVisible(false)
349 , m_webkitLegacyClosedCaptionOverride(false)
350 , m_completelyLoaded(false)
351 , m_havePreparedToPlay(false)
352 , m_parsingInProgress(createdByParser)
353 , m_elementIsHidden(document.hidden())
354 #if ENABLE(MEDIA_CONTROLS_SCRIPT)
355 , m_mediaControlsDependOnPageScaleFactor(false)
357 #if ENABLE(VIDEO_TRACK)
358 , m_tracksAreReady(true)
359 , m_haveVisibleTextTrack(false)
360 , m_processingPreferenceChange(false)
361 , m_lastTextTrackUpdateTime(MediaTime(-1, 1))
362 , m_captionDisplayMode(CaptionUserPreferences::Automatic)
366 , m_ignoreTrackDisplayUpdate(0)
368 #if ENABLE(WEB_AUDIO)
369 , m_audioSourceNode(0)
371 , m_mediaSession(std::make_unique<MediaElementSession>(*this))
372 , m_reportedExtraMemoryCost(0)
373 #if ENABLE(MEDIA_STREAM)
374 , m_mediaStreamSrcObject(nullptr)
377 allMediaElements().add(this);
379 LOG(Media, "HTMLMediaElement::HTMLMediaElement(%p)", this);
380 setHasCustomStyleResolveCallbacks();
382 m_mediaSession->addBehaviorRestriction(MediaElementSession::RequireUserGestureForFullscreen);
383 m_mediaSession->addBehaviorRestriction(MediaElementSession::RequirePageConsentToLoadMedia);
384 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
385 m_mediaSession->addBehaviorRestriction(MediaElementSession::RequireUserGestureToAutoplayToExternalDevice);
388 // FIXME: We should clean up and look to better merge the iOS and non-iOS code below.
389 Settings* settings = document.settings();
391 if (settings && settings->requiresUserGestureForMediaPlayback()) {
392 m_mediaSession->addBehaviorRestriction(MediaElementSession::RequireUserGestureForRateChange);
393 m_mediaSession->addBehaviorRestriction(MediaElementSession::RequireUserGestureForLoad);
396 m_sendProgressEvents = false;
397 if (!settings || settings->requiresUserGestureForMediaPlayback()) {
398 // Allow autoplay in a MediaDocument that is not in an iframe.
399 if (document.ownerElement() || !document.isMediaDocument())
400 m_mediaSession->addBehaviorRestriction(MediaElementSession::RequireUserGestureForRateChange);
401 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
402 m_mediaSession->addBehaviorRestriction(MediaElementSession::RequireUserGestureToShowPlaybackTargetPicker);
405 // Relax RequireUserGestureForFullscreen when requiresUserGestureForMediaPlayback is not set:
406 m_mediaSession->removeBehaviorRestriction(MediaElementSession::RequireUserGestureForFullscreen);
408 #endif // !PLATFORM(IOS)
410 if (settings && settings->audioPlaybackRequiresUserGesture() && settings->requiresUserGestureForMediaPlayback())
411 m_mediaSession->addBehaviorRestriction(MediaElementSession::RequireUserGestureForAudioRateChange);
413 #if ENABLE(VIDEO_TRACK)
415 m_captionDisplayMode = document.page()->group().captionPreferences()->captionDisplayMode();
418 #if ENABLE(MEDIA_SESSION)
419 setSessionInternal(document.defaultMediaSession());
422 registerWithDocument(document);
425 HTMLMediaElement::~HTMLMediaElement()
427 LOG(Media, "HTMLMediaElement::~HTMLMediaElement(%p)", this);
429 allMediaElements().remove(this);
431 m_asyncEventQueue.close();
433 setShouldDelayLoadEvent(false);
434 unregisterWithDocument(document());
436 #if ENABLE(VIDEO_TRACK)
438 m_audioTracks->clearElement();
439 for (unsigned i = 0; i < m_audioTracks->length(); ++i)
440 m_audioTracks->item(i)->clearClient();
443 m_textTracks->clearElement();
445 for (unsigned i = 0; i < m_textTracks->length(); ++i)
446 m_textTracks->item(i)->clearClient();
449 m_videoTracks->clearElement();
450 for (unsigned i = 0; i < m_videoTracks->length(); ++i)
451 m_videoTracks->item(i)->clearClient();
455 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
456 if (hasEventListeners(eventNames().webkitplaybacktargetavailabilitychangedEvent)) {
457 m_hasPlaybackTargetAvailabilityListeners = false;
458 m_mediaSession->setHasPlaybackTargetAvailabilityListeners(*this, false);
463 if (m_mediaController) {
464 m_mediaController->removeMediaElement(this);
465 m_mediaController = nullptr;
468 #if ENABLE(MEDIA_SOURCE)
472 #if ENABLE(ENCRYPTED_MEDIA_V2)
476 #if ENABLE(MEDIA_CONTROLS_SCRIPT)
478 m_isolatedWorld->clearWrappers();
481 #if ENABLE(MEDIA_SESSION)
483 m_session->removeMediaElement(*this);
488 m_seekTaskQueue.close();
490 m_completelyLoaded = true;
493 void HTMLMediaElement::registerWithDocument(Document& document)
495 m_mediaSession->registerWithDocument(document);
497 if (m_isWaitingUntilMediaCanStart)
498 document.addMediaCanStartListener(this);
501 document.registerForMediaVolumeCallbacks(this);
502 document.registerForPrivateBrowsingStateChangedCallbacks(this);
505 document.registerForVisibilityStateChangedCallbacks(this);
507 #if ENABLE(VIDEO_TRACK)
508 if (m_requireCaptionPreferencesChangedCallbacks)
509 document.registerForCaptionPreferencesChangedCallbacks(this);
512 #if ENABLE(MEDIA_CONTROLS_SCRIPT)
513 if (m_mediaControlsDependOnPageScaleFactor)
514 document.registerForPageScaleFactorChangedCallbacks(this);
517 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
518 document.registerForPageCacheSuspensionCallbacks(this);
521 document.registerForAllowsMediaDocumentInlinePlaybackChangedCallbacks(*this);
523 document.addAudioProducer(this);
524 addElementToDocumentMap(*this, document);
527 void HTMLMediaElement::unregisterWithDocument(Document& document)
529 m_mediaSession->unregisterWithDocument(document);
531 if (m_isWaitingUntilMediaCanStart)
532 document.removeMediaCanStartListener(this);
535 document.unregisterForMediaVolumeCallbacks(this);
536 document.unregisterForPrivateBrowsingStateChangedCallbacks(this);
539 document.unregisterForVisibilityStateChangedCallbacks(this);
541 #if ENABLE(VIDEO_TRACK)
542 if (m_requireCaptionPreferencesChangedCallbacks)
543 document.unregisterForCaptionPreferencesChangedCallbacks(this);
546 #if ENABLE(MEDIA_CONTROLS_SCRIPT)
547 if (m_mediaControlsDependOnPageScaleFactor)
548 document.unregisterForPageScaleFactorChangedCallbacks(this);
551 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
552 document.unregisterForPageCacheSuspensionCallbacks(this);
555 document.unregisterForAllowsMediaDocumentInlinePlaybackChangedCallbacks(*this);
557 document.removeAudioProducer(this);
558 removeElementFromDocumentMap(*this, document);
561 void HTMLMediaElement::didMoveToNewDocument(Document* oldDocument)
563 if (m_shouldDelayLoadEvent) {
565 oldDocument->decrementLoadEventDelayCount();
566 document().incrementLoadEventDelayCount();
570 unregisterWithDocument(*oldDocument);
572 registerWithDocument(document());
574 HTMLElement::didMoveToNewDocument(oldDocument);
577 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
578 void HTMLMediaElement::documentWillSuspendForPageCache()
580 m_mediaSession->unregisterWithDocument(document());
583 void HTMLMediaElement::documentDidResumeFromPageCache()
585 m_mediaSession->registerWithDocument(document());
589 bool HTMLMediaElement::hasCustomFocusLogic() const
594 bool HTMLMediaElement::supportsFocus() const
596 if (document().isMediaDocument())
599 // If no controls specified, we should still be able to focus the element if it has tabIndex.
600 return controls() || HTMLElement::supportsFocus();
603 bool HTMLMediaElement::isMouseFocusable() const
608 void HTMLMediaElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
610 if (name == srcAttr) {
612 // Note, unless the restriction on requiring user action has been removed,
613 // do not begin downloading data on iOS.
614 if (!value.isNull() && m_mediaSession->dataLoadingPermitted(*this))
616 // Trigger a reload, as long as the 'src' attribute is present.
620 clearMediaPlayer(LoadMediaResource);
621 scheduleDelayedAction(LoadMediaResource);
623 } else if (name == controlsAttr)
624 configureMediaControls();
625 else if (name == loopAttr)
626 updateSleepDisabling();
627 else if (name == preloadAttr) {
628 if (equalIgnoringCase(value, "none"))
629 m_preload = MediaPlayer::None;
630 else if (equalIgnoringCase(value, "metadata"))
631 m_preload = MediaPlayer::MetaData;
633 // The spec does not define an "invalid value default" but "auto" is suggested as the
634 // "missing value default", so use it for everything except "none" and "metadata"
635 m_preload = MediaPlayer::Auto;
638 // The attribute must be ignored if the autoplay attribute is present
639 if (!autoplay() && m_player)
640 m_player->setPreload(m_mediaSession->effectivePreloadForElement(*this));
642 } else if (name == mediagroupAttr)
643 setMediaGroup(value);
645 HTMLElement::parseAttribute(name, value);
648 void HTMLMediaElement::finishParsingChildren()
650 HTMLElement::finishParsingChildren();
651 m_parsingInProgress = false;
653 #if ENABLE(VIDEO_TRACK)
654 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
657 if (descendantsOfType<HTMLTrackElement>(*this).first())
658 scheduleDelayedAction(ConfigureTextTracks);
662 bool HTMLMediaElement::rendererIsNeeded(const RenderStyle& style)
664 return controls() && HTMLElement::rendererIsNeeded(style);
667 RenderPtr<RenderElement> HTMLMediaElement::createElementRenderer(Ref<RenderStyle>&& style, const RenderTreePosition&)
669 return createRenderer<RenderMedia>(*this, WTF::move(style));
672 bool HTMLMediaElement::childShouldCreateRenderer(const Node& child) const
674 #if ENABLE(MEDIA_CONTROLS_SCRIPT)
675 return hasShadowRootParent(child) && HTMLElement::childShouldCreateRenderer(child);
677 if (!hasMediaControls())
679 // <media> doesn't allow its content, including shadow subtree, to
680 // be rendered. So this should return false for most of the children.
681 // One exception is a shadow tree built for rendering controls which should be visible.
682 // So we let them go here by comparing its subtree root with one of the controls.
683 return &mediaControls()->treeScope() == &child.treeScope()
684 && hasShadowRootParent(child)
685 && HTMLElement::childShouldCreateRenderer(child);
689 Node::InsertionNotificationRequest HTMLMediaElement::insertedInto(ContainerNode& insertionPoint)
691 LOG(Media, "HTMLMediaElement::insertedInto(%p)", this);
693 HTMLElement::insertedInto(insertionPoint);
694 if (insertionPoint.inDocument()) {
695 m_inActiveDocument = true;
698 if (m_networkState == NETWORK_EMPTY && !fastGetAttribute(srcAttr).isEmpty() && m_mediaSession->dataLoadingPermitted(*this))
700 if (m_networkState == NETWORK_EMPTY && !fastGetAttribute(srcAttr).isEmpty())
702 scheduleDelayedAction(LoadMediaResource);
705 if (!m_explicitlyMuted) {
706 m_explicitlyMuted = true;
707 m_muted = fastHasAttribute(mutedAttr);
710 configureMediaControls();
711 return InsertionDone;
714 void HTMLMediaElement::removedFrom(ContainerNode& insertionPoint)
716 LOG(Media, "HTMLMediaElement::removedFrom(%p)", this);
718 m_inActiveDocument = false;
719 if (insertionPoint.inDocument()) {
720 if (hasMediaControls())
721 mediaControls()->hide();
722 if (m_networkState > NETWORK_EMPTY)
724 if (m_videoFullscreenMode != VideoFullscreenModeNone)
728 size_t extraMemoryCost = m_player->extraMemoryCost();
729 if (extraMemoryCost > m_reportedExtraMemoryCost) {
730 JSC::VM& vm = JSDOMWindowBase::commonVM();
731 JSC::JSLockHolder lock(vm);
733 size_t extraMemoryCostDelta = extraMemoryCost - m_reportedExtraMemoryCost;
734 m_reportedExtraMemoryCost = extraMemoryCost;
735 // FIXME: Adopt reportExtraMemoryVisited, and switch to reportExtraMemoryAllocated.
736 // https://bugs.webkit.org/show_bug.cgi?id=142595
737 vm.heap.deprecatedReportExtraMemory(extraMemoryCostDelta);
742 HTMLElement::removedFrom(insertionPoint);
745 void HTMLMediaElement::willAttachRenderers()
750 void HTMLMediaElement::didAttachRenderers()
753 renderer()->updateFromElement();
756 void HTMLMediaElement::didRecalcStyle(Style::Change)
759 renderer()->updateFromElement();
762 void HTMLMediaElement::scheduleDelayedAction(DelayedActionType actionType)
764 LOG(Media, "HTMLMediaElement::scheduleLoad(%p)", this);
766 if ((actionType & LoadMediaResource) && !(m_pendingActionFlags & LoadMediaResource)) {
768 setFlags(m_pendingActionFlags, LoadMediaResource);
771 #if ENABLE(VIDEO_TRACK)
772 if (RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled() && (actionType & ConfigureTextTracks))
773 setFlags(m_pendingActionFlags, ConfigureTextTracks);
776 #if USE(PLATFORM_TEXT_TRACK_MENU)
777 if (actionType & TextTrackChangesNotification)
778 setFlags(m_pendingActionFlags, TextTrackChangesNotification);
781 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
782 if (actionType & CheckPlaybackTargetCompatablity)
783 setFlags(m_pendingActionFlags, CheckPlaybackTargetCompatablity);
786 if (actionType & CheckMediaState)
787 setFlags(m_pendingActionFlags, CheckMediaState);
789 m_pendingActionTimer.startOneShot(0);
792 void HTMLMediaElement::scheduleNextSourceChild()
794 // Schedule the timer to try the next <source> element WITHOUT resetting state ala prepareForLoad.
795 setFlags(m_pendingActionFlags, LoadMediaResource);
796 m_pendingActionTimer.startOneShot(0);
799 void HTMLMediaElement::scheduleEvent(const AtomicString& eventName)
802 LOG(Media, "HTMLMediaElement::scheduleEvent(%p) - scheduling '%s'", this, eventName.string().ascii().data());
804 RefPtr<Event> event = Event::create(eventName, false, true);
806 // Don't set the event target, the event queue will set it in GenericEventQueue::timerFired and setting it here
807 // will trigger an ASSERT if this element has been marked for deletion.
809 m_asyncEventQueue.enqueueEvent(event.release());
812 void HTMLMediaElement::pendingActionTimerFired()
814 Ref<HTMLMediaElement> protect(*this); // loadNextSourceChild may fire 'beforeload', which can make arbitrary DOM mutations.
816 #if ENABLE(VIDEO_TRACK)
817 if (RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled() && (m_pendingActionFlags & ConfigureTextTracks))
818 configureTextTracks();
821 if (m_pendingActionFlags & LoadMediaResource) {
822 if (m_loadState == LoadingFromSourceElement)
823 loadNextSourceChild();
828 #if USE(PLATFORM_TEXT_TRACK_MENU)
829 if (RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled() && (m_pendingActionFlags & TextTrackChangesNotification))
830 notifyMediaPlayerOfTextTrackChanges();
833 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
834 if (m_pendingActionFlags & CheckPlaybackTargetCompatablity && m_player && m_player->isCurrentPlaybackTargetWireless() && !m_player->canPlayToWirelessPlaybackTarget()) {
835 LOG(Media, "HTMLMediaElement::pendingActionTimerFired(%p) - calling setShouldPlayToPlaybackTarget(false)", this);
836 m_failedToPlayToWirelessTarget = true;
837 m_player->setShouldPlayToPlaybackTarget(false);
840 if (m_pendingActionFlags & CheckMediaState)
844 m_pendingActionFlags = 0;
847 PassRefPtr<MediaError> HTMLMediaElement::error() const
852 void HTMLMediaElement::setSrc(const String& url)
854 setAttribute(srcAttr, url);
857 #if ENABLE(MEDIA_STREAM)
858 void HTMLMediaElement::setSrcObject(MediaStream* mediaStream)
860 // FIXME: Setting the srcObject attribute may cause other changes to the media element's internal state:
861 // Specifically, if srcObject is specified, the UA must use it as the source of media, even if the src
862 // attribute is also set or children are present. If the value of srcObject is replaced or set to null
863 // the UA must re-run the media element load algorithm.
865 // https://bugs.webkit.org/show_bug.cgi?id=124896
867 m_mediaStreamSrcObject = mediaStream;
871 HTMLMediaElement::NetworkState HTMLMediaElement::networkState() const
873 return m_networkState;
876 String HTMLMediaElement::canPlayType(const String& mimeType, const String& keySystem, const URL& url) const
878 MediaEngineSupportParameters parameters;
879 ContentType contentType(mimeType);
880 parameters.type = contentType.type().lower();
881 parameters.codecs = contentType.parameter(ASCIILiteral("codecs"));
882 parameters.url = url;
883 #if ENABLE(ENCRYPTED_MEDIA)
884 parameters.keySystem = keySystem;
886 UNUSED_PARAM(keySystem);
888 MediaPlayer::SupportsType support = MediaPlayer::supportsType(parameters, this);
894 case MediaPlayer::IsNotSupported:
895 canPlay = emptyString();
897 case MediaPlayer::MayBeSupported:
898 canPlay = ASCIILiteral("maybe");
900 case MediaPlayer::IsSupported:
901 canPlay = ASCIILiteral("probably");
905 LOG(Media, "HTMLMediaElement::canPlayType(%p) - [%s, %s, %s] -> %s", this, mimeType.utf8().data(), keySystem.utf8().data(), url.stringCenterEllipsizedToLength().utf8().data(), canPlay.utf8().data());
910 double HTMLMediaElement::getStartDate() const
912 return m_player->getStartDate().toDouble();
915 void HTMLMediaElement::load()
917 Ref<HTMLMediaElement> protect(*this); // loadInternal may result in a 'beforeload' event, which can make arbitrary DOM mutations.
919 LOG(Media, "HTMLMediaElement::load(%p)", this);
921 if (!m_mediaSession->dataLoadingPermitted(*this))
923 if (ScriptController::processingUserGesture())
924 removeBehaviorsRestrictionsAfterFirstUserGesture();
931 void HTMLMediaElement::prepareForLoad()
933 LOG(Media, "HTMLMediaElement::prepareForLoad(%p)", this);
935 // Perform the cleanup required for the resource load algorithm to run.
936 stopPeriodicTimers();
937 m_pendingActionTimer.stop();
938 // FIXME: Figure out appropriate place to reset LoadTextTrackResource if necessary and set m_pendingActionFlags to 0 here.
939 m_pendingActionFlags &= ~LoadMediaResource;
940 m_sentEndEvent = false;
941 m_sentStalledEvent = false;
942 m_haveFiredLoadedData = false;
943 m_completelyLoaded = false;
944 m_havePreparedToPlay = false;
945 m_displayMode = Unknown;
946 m_currentSrc = URL();
948 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
949 m_failedToPlayToWirelessTarget = false;
952 // 1 - Abort any already-running instance of the resource selection algorithm for this element.
953 m_loadState = WaitingForSource;
954 m_currentSourceNode = nullptr;
956 // 2 - If there are any tasks from the media element's media element event task source in
957 // one of the task queues, then remove those tasks.
958 cancelPendingEventsAndCallbacks();
960 // 3 - If the media element's networkState is set to NETWORK_LOADING or NETWORK_IDLE, queue
961 // a task to fire a simple event named abort at the media element.
962 if (m_networkState == NETWORK_LOADING || m_networkState == NETWORK_IDLE)
963 scheduleEvent(eventNames().abortEvent);
965 #if ENABLE(MEDIA_SOURCE)
971 // 4 - If the media element's networkState is not set to NETWORK_EMPTY, then run these substeps
972 if (m_networkState != NETWORK_EMPTY) {
973 // 4.1 - Queue a task to fire a simple event named emptied at the media element.
974 scheduleEvent(eventNames().emptiedEvent);
976 // 4.2 - If a fetching process is in progress for the media element, the user agent should stop it.
977 m_networkState = NETWORK_EMPTY;
979 // 4.3 - Forget the media element's media-resource-specific tracks.
980 forgetResourceSpecificTracks();
982 // 4.4 - If readyState is not set to HAVE_NOTHING, then set it to that state.
983 m_readyState = HAVE_NOTHING;
984 m_readyStateMaximum = HAVE_NOTHING;
986 // 4.5 - If the paused attribute is false, then set it to true.
989 // 4.6 - If seeking is true, set it to false.
992 // 4.7 - Set the current playback position to 0.
993 // Set the official playback position to 0.
994 // If this changed the official playback position, then queue a task to fire a simple event named timeupdate at the media element.
995 // FIXME: Add support for firing this event. e.g., scheduleEvent(eventNames().timeUpdateEvent);
997 // 4.8 - Set the initial playback position to 0.
998 // FIXME: Make this less subtle. The position only becomes 0 because of the createMediaPlayer() call
1000 refreshCachedTime();
1002 invalidateCachedTime();
1004 // 4.9 - Set the timeline offset to Not-a-Number (NaN).
1005 // 4.10 - Update the duration attribute to Not-a-Number (NaN).
1007 updateMediaController();
1008 #if ENABLE(VIDEO_TRACK)
1009 if (RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
1010 updateActiveTextTrackCues(MediaTime::zeroTime());
1014 // 5 - Set the playbackRate attribute to the value of the defaultPlaybackRate attribute.
1015 setPlaybackRate(defaultPlaybackRate());
1017 // 6 - Set the error attribute to null and the autoplaying flag to true.
1019 m_autoplaying = true;
1021 // 7 - Invoke the media element's resource selection algorithm.
1023 // 8 - Note: Playback of any previously playing media resource for this element stops.
1025 // The resource selection algorithm
1026 // 1 - Set the networkState to NETWORK_NO_SOURCE
1027 m_networkState = NETWORK_NO_SOURCE;
1029 // 2 - Asynchronously await a stable state.
1031 m_playedTimeRanges = TimeRanges::create();
1033 // FIXME: Investigate whether these can be moved into m_networkState != NETWORK_EMPTY block above
1034 // so they are closer to the relevant spec steps.
1035 m_lastSeekTime = MediaTime::zeroTime();
1037 // The spec doesn't say to block the load event until we actually run the asynchronous section
1038 // algorithm, but do it now because we won't start that until after the timer fires and the
1039 // event may have already fired by then.
1040 MediaPlayer::Preload effectivePreload = m_mediaSession->effectivePreloadForElement(*this);
1041 if (effectivePreload != MediaPlayer::None)
1042 setShouldDelayLoadEvent(true);
1045 if (effectivePreload != MediaPlayer::None && m_mediaSession->allowsAutomaticMediaDataLoading(*this))
1049 configureMediaControls();
1052 void HTMLMediaElement::loadInternal()
1054 // Some of the code paths below this function dispatch the BeforeLoad event. This ASSERT helps
1055 // us catch those bugs more quickly without needing all the branches to align to actually
1056 // trigger the event.
1057 ASSERT(!NoEventDispatchAssertion::isEventDispatchForbidden());
1059 // If we can't start a load right away, start it later.
1060 if (!m_mediaSession->pageAllowsDataLoading(*this)) {
1061 setShouldDelayLoadEvent(false);
1062 if (m_isWaitingUntilMediaCanStart)
1064 document().addMediaCanStartListener(this);
1065 m_isWaitingUntilMediaCanStart = true;
1069 clearFlags(m_pendingActionFlags, LoadMediaResource);
1071 // Once the page has allowed an element to load media, it is free to load at will. This allows a
1072 // playlist that starts in a foreground tab to continue automatically if the tab is subsequently
1073 // put into the background.
1074 m_mediaSession->removeBehaviorRestriction(MediaElementSession::RequirePageConsentToLoadMedia);
1076 #if ENABLE(VIDEO_TRACK)
1077 if (hasMediaControls())
1078 mediaControls()->changedClosedCaptionsVisibility();
1080 // HTMLMediaElement::textTracksAreReady will need "... the text tracks whose mode was not in the
1081 // disabled state when the element's resource selection algorithm last started".
1082 if (RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled()) {
1083 m_textTracksWhenResourceSelectionBegan.clear();
1085 for (unsigned i = 0; i < m_textTracks->length(); ++i) {
1086 TextTrack* track = m_textTracks->item(i);
1087 if (track->mode() != TextTrack::disabledKeyword())
1088 m_textTracksWhenResourceSelectionBegan.append(track);
1094 selectMediaResource();
1097 void HTMLMediaElement::selectMediaResource()
1099 LOG(Media, "HTMLMediaElement::selectMediaResource(%p)", this);
1101 enum Mode { attribute, children };
1103 // 3 - If the media element has a src attribute, then let mode be attribute.
1104 Mode mode = attribute;
1105 if (!fastHasAttribute(srcAttr)) {
1106 // Otherwise, if the media element does not have a src attribute but has a source
1107 // element child, then let mode be children and let candidate be the first such
1108 // source element child in tree order.
1109 if (auto firstSource = childrenOfType<HTMLSourceElement>(*this).first()) {
1111 m_nextChildNodeToConsider = firstSource;
1112 m_currentSourceNode = nullptr;
1114 // Otherwise the media element has neither a src attribute nor a source element
1115 // child: set the networkState to NETWORK_EMPTY, and abort these steps; the
1116 // synchronous section ends.
1117 m_loadState = WaitingForSource;
1118 setShouldDelayLoadEvent(false);
1119 m_networkState = NETWORK_EMPTY;
1121 LOG(Media, "HTMLMediaElement::selectMediaResource(%p) - nothing to load", this);
1126 // 4 - Set the media element's delaying-the-load-event flag to true (this delays the load event),
1127 // and set its networkState to NETWORK_LOADING.
1128 setShouldDelayLoadEvent(true);
1129 m_networkState = NETWORK_LOADING;
1131 // 5 - Queue a task to fire a simple event named loadstart at the media element.
1132 scheduleEvent(eventNames().loadstartEvent);
1134 // 6 - If mode is attribute, then run these substeps
1135 if (mode == attribute) {
1136 m_loadState = LoadingFromSrcAttr;
1138 // If the src attribute's value is the empty string ... jump down to the failed step below
1139 URL mediaURL = getNonEmptyURLAttribute(srcAttr);
1140 if (mediaURL.isEmpty()) {
1141 mediaLoadingFailed(MediaPlayer::FormatError);
1142 LOG(Media, "HTMLMediaElement::selectMediaResource(%p) - empty 'src'", this);
1146 if (!isSafeToLoadURL(mediaURL, Complain) || !dispatchBeforeLoadEvent(mediaURL.string())) {
1147 mediaLoadingFailed(MediaPlayer::FormatError);
1151 // No type or key system information is available when the url comes
1152 // from the 'src' attribute so MediaPlayer
1153 // will have to pick a media engine based on the file extension.
1154 ContentType contentType((String()));
1155 loadResource(mediaURL, contentType, String());
1156 LOG(Media, "HTMLMediaElement::selectMediaResource(%p) - using 'src' attribute url", this);
1160 // Otherwise, the source elements will be used
1161 loadNextSourceChild();
1164 void HTMLMediaElement::loadNextSourceChild()
1166 ContentType contentType((String()));
1168 URL mediaURL = selectNextSourceChild(&contentType, &keySystem, Complain);
1169 if (!mediaURL.isValid()) {
1170 waitForSourceChange();
1174 // Recreate the media player for the new url
1175 createMediaPlayer();
1177 m_loadState = LoadingFromSourceElement;
1178 loadResource(mediaURL, contentType, keySystem);
1181 void HTMLMediaElement::loadResource(const URL& initialURL, ContentType& contentType, const String& keySystem)
1183 ASSERT(isSafeToLoadURL(initialURL, Complain));
1185 LOG(Media, "HTMLMediaElement::loadResource(%p) - %s, %s, %s", this, urlForLoggingMedia(initialURL).utf8().data(), contentType.raw().utf8().data(), keySystem.utf8().data());
1187 Frame* frame = document().frame();
1189 mediaLoadingFailed(MediaPlayer::FormatError);
1193 Page* page = frame->page();
1195 mediaLoadingFailed(MediaPlayer::FormatError);
1199 URL url = initialURL;
1200 if (!frame->loader().willLoadMediaElementURL(url)) {
1201 mediaLoadingFailed(MediaPlayer::FormatError);
1205 #if ENABLE(CONTENT_EXTENSIONS)
1206 ResourceRequest request(url);
1207 DocumentLoader* documentLoader = frame->loader().documentLoader();
1209 if (page->userContentController() && documentLoader)
1210 page->userContentController()->processContentExtensionRulesForLoad(*page, request, ResourceType::Media, *documentLoader);
1212 if (request.isNull()) {
1213 mediaLoadingFailed(MediaPlayer::FormatError);
1218 // The resource fetch algorithm
1219 m_networkState = NETWORK_LOADING;
1221 // If the url should be loaded from the application cache, pass the url of the cached file
1222 // to the media engine.
1223 ApplicationCacheHost* cacheHost = frame->loader().documentLoader()->applicationCacheHost();
1224 ApplicationCacheResource* resource = 0;
1225 if (cacheHost && cacheHost->shouldLoadResourceFromApplicationCache(ResourceRequest(url), resource)) {
1226 // Resources that are not present in the manifest will always fail to load (at least, after the
1227 // cache has been primed the first time), making the testing of offline applications simpler.
1228 if (!resource || resource->path().isEmpty()) {
1229 mediaLoadingFailed(MediaPlayer::NetworkError);
1234 // Log that we started loading a media element.
1235 if (Frame* frame = document().frame())
1236 frame->mainFrame().diagnosticLoggingClient().logDiagnosticMessageWithValue(DiagnosticLoggingKeys::mediaKey(), isVideo() ? DiagnosticLoggingKeys::videoKey() : DiagnosticLoggingKeys::audioKey(), DiagnosticLoggingKeys::loadingKey(), ShouldSample::No);
1238 m_firstTimePlaying = true;
1240 // Set m_currentSrc *before* changing to the cache url, the fact that we are loading from the app
1241 // cache is an internal detail not exposed through the media element API.
1245 url = ApplicationCacheHost::createFileURL(resource->path());
1246 LOG(Media, "HTMLMediaElement::loadResource(%p) - will load from app cache -> %s", this, urlForLoggingMedia(url).utf8().data());
1249 LOG(Media, "HTMLMediaElement::loadResource(%p) - m_currentSrc -> %s", this, urlForLoggingMedia(m_currentSrc).utf8().data());
1251 #if ENABLE(MEDIA_STREAM)
1252 if (MediaStreamRegistry::registry().lookup(url.string()))
1253 m_mediaSession->removeBehaviorRestriction(MediaElementSession::RequireUserGestureForRateChange);
1256 if (m_sendProgressEvents)
1257 startProgressEventTimer();
1259 bool privateMode = document().page() && document().page()->usesEphemeralSession();
1260 m_player->setPrivateBrowsingMode(privateMode);
1262 // Reset display mode to force a recalculation of what to show because we are resetting the player.
1263 setDisplayMode(Unknown);
1266 m_player->setPreload(m_mediaSession->effectivePreloadForElement(*this));
1267 m_player->setPreservesPitch(m_webkitPreservesPitch);
1269 if (!m_explicitlyMuted) {
1270 m_explicitlyMuted = true;
1271 m_muted = fastHasAttribute(mutedAttr);
1276 #if ENABLE(MEDIA_SOURCE)
1277 ASSERT(!m_mediaSource);
1279 if (url.protocolIs(mediaSourceBlobProtocol))
1280 m_mediaSource = MediaSource::lookup(url.string());
1282 if (m_mediaSource) {
1283 if (m_mediaSource->attachToElement(this))
1284 m_player->load(url, contentType, m_mediaSource.get());
1286 // Forget our reference to the MediaSource, so we leave it alone
1287 // while processing remainder of load failure.
1288 m_mediaSource = nullptr;
1289 mediaLoadingFailed(MediaPlayer::FormatError);
1293 #if ENABLE(MEDIA_STREAM)
1294 if (m_mediaStreamSrcObject)
1295 m_player->load(m_mediaStreamSrcObject->privateStream());
1298 if (!m_player->load(url, contentType, keySystem))
1299 mediaLoadingFailed(MediaPlayer::FormatError);
1301 // If there is no poster to display, allow the media engine to render video frames as soon as
1302 // they are available.
1303 updateDisplayState();
1306 renderer()->updateFromElement();
1309 #if ENABLE(VIDEO_TRACK)
1310 static bool trackIndexCompare(TextTrack* a,
1313 return a->trackIndex() - b->trackIndex() < 0;
1316 static bool eventTimeCueCompare(const std::pair<MediaTime, TextTrackCue*>& a, const std::pair<MediaTime, TextTrackCue*>& b)
1318 // 12 - Sort the tasks in events in ascending time order (tasks with earlier
1320 if (a.first != b.first)
1321 return a.first - b.first < MediaTime::zeroTime();
1323 // If the cues belong to different text tracks, it doesn't make sense to
1324 // compare the two tracks by the relative cue order, so return the relative
1326 if (a.second->track() != b.second->track())
1327 return trackIndexCompare(a.second->track(), b.second->track());
1329 // 12 - Further sort tasks in events that have the same time by the
1330 // relative text track cue order of the text track cues associated
1331 // with these tasks.
1332 return a.second->cueIndex() - b.second->cueIndex() < 0;
1335 static bool compareCueInterval(const CueInterval& one, const CueInterval& two)
1337 return one.data()->isOrderedBefore(two.data());
1341 void HTMLMediaElement::updateActiveTextTrackCues(const MediaTime& movieTime)
1343 // 4.8.10.8 Playing the media resource
1345 // If the current playback position changes while the steps are running,
1346 // then the user agent must wait for the steps to complete, and then must
1347 // immediately rerun the steps.
1348 if (ignoreTrackDisplayUpdateRequests())
1351 LOG(Media, "HTMLMediaElement::updateActiveTextTracks(%p)", this);
1353 // 1 - Let current cues be a list of cues, initialized to contain all the
1354 // cues of all the hidden, showing, or showing by default text tracks of the
1355 // media element (not the disabled ones) whose start times are less than or
1356 // equal to the current playback position and whose end times are greater
1357 // than the current playback position.
1358 CueList currentCues;
1360 // The user agent must synchronously unset [the text track cue active] flag
1361 // whenever ... the media element's readyState is changed back to HAVE_NOTHING.
1362 if (m_readyState != HAVE_NOTHING && m_player) {
1363 currentCues = m_cueTree.allOverlaps(m_cueTree.createInterval(movieTime, movieTime));
1364 if (currentCues.size() > 1)
1365 std::sort(currentCues.begin(), currentCues.end(), &compareCueInterval);
1368 CueList previousCues;
1371 // 2 - Let other cues be a list of cues, initialized to contain all the cues
1372 // of hidden, showing, and showing by default text tracks of the media
1373 // element that are not present in current cues.
1374 previousCues = m_currentlyActiveCues;
1376 // 3 - Let last time be the current playback position at the time this
1377 // algorithm was last run for this media element, if this is not the first
1379 MediaTime lastTime = m_lastTextTrackUpdateTime;
1381 // 4 - If the current playback position has, since the last time this
1382 // algorithm was run, only changed through its usual monotonic increase
1383 // during normal playback, then let missed cues be the list of cues in other
1384 // cues whose start times are greater than or equal to last time and whose
1385 // end times are less than or equal to the current playback position.
1386 // Otherwise, let missed cues be an empty list.
1387 if (lastTime >= MediaTime::zeroTime() && m_lastSeekTime < movieTime) {
1388 CueList potentiallySkippedCues =
1389 m_cueTree.allOverlaps(m_cueTree.createInterval(lastTime, movieTime));
1391 for (size_t i = 0; i < potentiallySkippedCues.size(); ++i) {
1392 MediaTime cueStartTime = potentiallySkippedCues[i].low();
1393 MediaTime cueEndTime = potentiallySkippedCues[i].high();
1395 // Consider cues that may have been missed since the last seek time.
1396 if (cueStartTime > std::max(m_lastSeekTime, lastTime) && cueEndTime < movieTime)
1397 missedCues.append(potentiallySkippedCues[i]);
1401 m_lastTextTrackUpdateTime = movieTime;
1403 // 5 - If the time was reached through the usual monotonic increase of the
1404 // current playback position during normal playback, and if the user agent
1405 // has not fired a timeupdate event at the element in the past 15 to 250ms
1406 // and is not still running event handlers for such an event, then the user
1407 // agent must queue a task to fire a simple event named timeupdate at the
1408 // element. (In the other cases, such as explicit seeks, relevant events get
1409 // fired as part of the overall process of changing the current playback
1411 if (!m_paused && m_lastSeekTime <= lastTime)
1412 scheduleTimeupdateEvent(false);
1414 // Explicitly cache vector sizes, as their content is constant from here.
1415 size_t currentCuesSize = currentCues.size();
1416 size_t missedCuesSize = missedCues.size();
1417 size_t previousCuesSize = previousCues.size();
1419 // 6 - If all of the cues in current cues have their text track cue active
1420 // flag set, none of the cues in other cues have their text track cue active
1421 // flag set, and missed cues is empty, then abort these steps.
1422 bool activeSetChanged = missedCuesSize;
1424 for (size_t i = 0; !activeSetChanged && i < previousCuesSize; ++i)
1425 if (!currentCues.contains(previousCues[i]) && previousCues[i].data()->isActive())
1426 activeSetChanged = true;
1428 for (size_t i = 0; i < currentCuesSize; ++i) {
1429 TextTrackCue* cue = currentCues[i].data();
1431 if (cue->isRenderable())
1432 toVTTCue(cue)->updateDisplayTree(movieTime);
1434 if (!cue->isActive())
1435 activeSetChanged = true;
1438 if (!activeSetChanged)
1441 // 7 - If the time was reached through the usual monotonic increase of the
1442 // current playback position during normal playback, and there are cues in
1443 // other cues that have their text track cue pause-on-exi flag set and that
1444 // either have their text track cue active flag set or are also in missed
1445 // cues, then immediately pause the media element.
1446 for (size_t i = 0; !m_paused && i < previousCuesSize; ++i) {
1447 if (previousCues[i].data()->pauseOnExit()
1448 && previousCues[i].data()->isActive()
1449 && !currentCues.contains(previousCues[i]))
1453 for (size_t i = 0; !m_paused && i < missedCuesSize; ++i) {
1454 if (missedCues[i].data()->pauseOnExit())
1458 // 8 - Let events be a list of tasks, initially empty. Each task in this
1459 // list will be associated with a text track, a text track cue, and a time,
1460 // which are used to sort the list before the tasks are queued.
1461 Vector<std::pair<MediaTime, TextTrackCue*>> eventTasks;
1463 // 8 - Let affected tracks be a list of text tracks, initially empty.
1464 Vector<TextTrack*> affectedTracks;
1466 for (size_t i = 0; i < missedCuesSize; ++i) {
1467 // 9 - For each text track cue in missed cues, prepare an event named enter
1468 // for the TextTrackCue object with the text track cue start time.
1469 eventTasks.append(std::make_pair(missedCues[i].data()->startMediaTime(),
1470 missedCues[i].data()));
1472 // 10 - For each text track [...] in missed cues, prepare an event
1473 // named exit for the TextTrackCue object with the with the later of
1474 // the text track cue end time and the text track cue start time.
1476 // Note: An explicit task is added only if the cue is NOT a zero or
1477 // negative length cue. Otherwise, the need for an exit event is
1478 // checked when these tasks are actually queued below. This doesn't
1479 // affect sorting events before dispatch either, because the exit
1480 // event has the same time as the enter event.
1481 if (missedCues[i].data()->startMediaTime() < missedCues[i].data()->endMediaTime())
1482 eventTasks.append(std::make_pair(missedCues[i].data()->endMediaTime(), missedCues[i].data()));
1485 for (size_t i = 0; i < previousCuesSize; ++i) {
1486 // 10 - For each text track cue in other cues that has its text
1487 // track cue active flag set prepare an event named exit for the
1488 // TextTrackCue object with the text track cue end time.
1489 if (!currentCues.contains(previousCues[i]))
1490 eventTasks.append(std::make_pair(previousCues[i].data()->endMediaTime(),
1491 previousCues[i].data()));
1494 for (size_t i = 0; i < currentCuesSize; ++i) {
1495 // 11 - For each text track cue in current cues that does not have its
1496 // text track cue active flag set, prepare an event named enter for the
1497 // TextTrackCue object with the text track cue start time.
1498 if (!previousCues.contains(currentCues[i]))
1499 eventTasks.append(std::make_pair(currentCues[i].data()->startMediaTime(),
1500 currentCues[i].data()));
1503 // 12 - Sort the tasks in events in ascending time order (tasks with earlier
1505 std::sort(eventTasks.begin(), eventTasks.end(), eventTimeCueCompare);
1507 for (size_t i = 0; i < eventTasks.size(); ++i) {
1508 if (!affectedTracks.contains(eventTasks[i].second->track()))
1509 affectedTracks.append(eventTasks[i].second->track());
1511 // 13 - Queue each task in events, in list order.
1512 RefPtr<Event> event;
1514 // Each event in eventTasks may be either an enterEvent or an exitEvent,
1515 // depending on the time that is associated with the event. This
1516 // correctly identifies the type of the event, if the startTime is
1517 // less than the endTime in the cue.
1518 if (eventTasks[i].second->startTime() >= eventTasks[i].second->endTime()) {
1519 event = Event::create(eventNames().enterEvent, false, false);
1520 event->setTarget(eventTasks[i].second);
1521 m_asyncEventQueue.enqueueEvent(event.release());
1523 event = Event::create(eventNames().exitEvent, false, false);
1524 event->setTarget(eventTasks[i].second);
1525 m_asyncEventQueue.enqueueEvent(event.release());
1527 if (eventTasks[i].first == eventTasks[i].second->startMediaTime())
1528 event = Event::create(eventNames().enterEvent, false, false);
1530 event = Event::create(eventNames().exitEvent, false, false);
1532 event->setTarget(eventTasks[i].second);
1533 m_asyncEventQueue.enqueueEvent(event.release());
1537 // 14 - Sort affected tracks in the same order as the text tracks appear in
1538 // the media element's list of text tracks, and remove duplicates.
1539 std::sort(affectedTracks.begin(), affectedTracks.end(), trackIndexCompare);
1541 // 15 - For each text track in affected tracks, in the list order, queue a
1542 // task to fire a simple event named cuechange at the TextTrack object, and, ...
1543 for (size_t i = 0; i < affectedTracks.size(); ++i) {
1544 RefPtr<Event> event = Event::create(eventNames().cuechangeEvent, false, false);
1545 event->setTarget(affectedTracks[i]);
1547 m_asyncEventQueue.enqueueEvent(event.release());
1549 // ... if the text track has a corresponding track element, to then fire a
1550 // simple event named cuechange at the track element as well.
1551 if (is<LoadableTextTrack>(*affectedTracks[i])) {
1552 RefPtr<Event> event = Event::create(eventNames().cuechangeEvent, false, false);
1553 HTMLTrackElement* trackElement = downcast<LoadableTextTrack>(*affectedTracks[i]).trackElement();
1554 ASSERT(trackElement);
1555 event->setTarget(trackElement);
1557 m_asyncEventQueue.enqueueEvent(event.release());
1561 // 16 - Set the text track cue active flag of all the cues in the current
1562 // cues, and unset the text track cue active flag of all the cues in the
1564 for (size_t i = 0; i < currentCuesSize; ++i)
1565 currentCues[i].data()->setIsActive(true);
1567 for (size_t i = 0; i < previousCuesSize; ++i)
1568 if (!currentCues.contains(previousCues[i]))
1569 previousCues[i].data()->setIsActive(false);
1571 // Update the current active cues.
1572 m_currentlyActiveCues = currentCues;
1574 if (activeSetChanged)
1575 updateTextTrackDisplay();
1578 bool HTMLMediaElement::textTracksAreReady() const
1580 // 4.8.10.12.1 Text track model
1582 // The text tracks of a media element are ready if all the text tracks whose mode was not
1583 // in the disabled state when the element's resource selection algorithm last started now
1584 // have a text track readiness state of loaded or failed to load.
1585 for (unsigned i = 0; i < m_textTracksWhenResourceSelectionBegan.size(); ++i) {
1586 if (m_textTracksWhenResourceSelectionBegan[i]->readinessState() == TextTrack::Loading
1587 || m_textTracksWhenResourceSelectionBegan[i]->readinessState() == TextTrack::NotLoaded)
1594 void HTMLMediaElement::textTrackReadyStateChanged(TextTrack* track)
1596 if (m_player && m_textTracksWhenResourceSelectionBegan.contains(track)) {
1597 if (track->readinessState() != TextTrack::Loading)
1598 setReadyState(m_player->readyState());
1600 // The track readiness state might have changed as a result of the user
1601 // clicking the captions button. In this case, a check whether all the
1602 // resources have failed loading should be done in order to hide the CC button.
1603 if (hasMediaControls() && track->readinessState() == TextTrack::FailedToLoad)
1604 mediaControls()->refreshClosedCaptionsButtonVisibility();
1608 void HTMLMediaElement::audioTrackEnabledChanged(AudioTrack* track)
1610 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
1612 if (m_audioTracks && m_audioTracks->contains(track))
1613 m_audioTracks->scheduleChangeEvent();
1616 void HTMLMediaElement::textTrackModeChanged(TextTrack* track)
1618 bool trackIsLoaded = true;
1619 if (track->trackType() == TextTrack::TrackElement) {
1620 trackIsLoaded = false;
1621 for (auto& trackElement : childrenOfType<HTMLTrackElement>(*this)) {
1622 if (trackElement.track() == track) {
1623 if (trackElement.readyState() == HTMLTrackElement::LOADING || trackElement.readyState() == HTMLTrackElement::LOADED)
1624 trackIsLoaded = true;
1630 // If this is the first added track, create the list of text tracks.
1632 m_textTracks = TextTrackList::create(this, ActiveDOMObject::scriptExecutionContext());
1634 // Mark this track as "configured" so configureTextTracks won't change the mode again.
1635 track->setHasBeenConfigured(true);
1637 if (track->mode() != TextTrack::disabledKeyword() && trackIsLoaded)
1638 textTrackAddCues(track, track->cues());
1640 #if USE(PLATFORM_TEXT_TRACK_MENU)
1641 if (platformTextTrackMenu())
1642 platformTextTrackMenu()->trackWasSelected(track->platformTextTrack());
1645 configureTextTrackDisplay(AssumeTextTrackVisibilityChanged);
1647 if (m_textTracks && m_textTracks->contains(track))
1648 m_textTracks->scheduleChangeEvent();
1650 #if ENABLE(AVF_CAPTIONS)
1651 if (track->trackType() == TextTrack::TrackElement && m_player)
1652 m_player->notifyTrackModeChanged();
1656 void HTMLMediaElement::videoTrackSelectedChanged(VideoTrack* track)
1658 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
1660 if (m_videoTracks && m_videoTracks->contains(track))
1661 m_videoTracks->scheduleChangeEvent();
1664 void HTMLMediaElement::textTrackKindChanged(TextTrack* track)
1666 if (track->kind() != TextTrack::captionsKeyword() && track->kind() != TextTrack::subtitlesKeyword() && track->mode() == TextTrack::showingKeyword())
1667 track->setMode(TextTrack::hiddenKeyword());
1670 void HTMLMediaElement::beginIgnoringTrackDisplayUpdateRequests()
1672 ++m_ignoreTrackDisplayUpdate;
1675 void HTMLMediaElement::endIgnoringTrackDisplayUpdateRequests()
1677 ASSERT(m_ignoreTrackDisplayUpdate);
1678 --m_ignoreTrackDisplayUpdate;
1679 if (!m_ignoreTrackDisplayUpdate && m_inActiveDocument)
1680 updateActiveTextTrackCues(currentMediaTime());
1683 void HTMLMediaElement::textTrackAddCues(TextTrack* track, const TextTrackCueList* cues)
1685 if (track->mode() == TextTrack::disabledKeyword())
1688 TrackDisplayUpdateScope scope(this);
1689 for (size_t i = 0; i < cues->length(); ++i)
1690 textTrackAddCue(track, cues->item(i));
1693 void HTMLMediaElement::textTrackRemoveCues(TextTrack*, const TextTrackCueList* cues)
1695 TrackDisplayUpdateScope scope(this);
1696 for (size_t i = 0; i < cues->length(); ++i)
1697 textTrackRemoveCue(cues->item(i)->track(), cues->item(i));
1700 void HTMLMediaElement::textTrackAddCue(TextTrack* track, PassRefPtr<TextTrackCue> prpCue)
1702 if (track->mode() == TextTrack::disabledKeyword())
1705 RefPtr<TextTrackCue> cue = prpCue;
1707 // Negative duration cues need be treated in the interval tree as
1708 // zero-length cues.
1709 MediaTime endTime = std::max(cue->startMediaTime(), cue->endMediaTime());
1711 CueInterval interval = m_cueTree.createInterval(cue->startMediaTime(), endTime, cue.get());
1712 if (!m_cueTree.contains(interval))
1713 m_cueTree.add(interval);
1714 updateActiveTextTrackCues(currentMediaTime());
1717 void HTMLMediaElement::textTrackRemoveCue(TextTrack*, PassRefPtr<TextTrackCue> prpCue)
1719 RefPtr<TextTrackCue> cue = prpCue;
1720 // Negative duration cues need to be treated in the interval tree as
1721 // zero-length cues.
1722 MediaTime endTime = std::max(cue->startMediaTime(), cue->endMediaTime());
1724 CueInterval interval = m_cueTree.createInterval(cue->startMediaTime(), endTime, cue.get());
1725 m_cueTree.remove(interval);
1727 #if ENABLE(WEBVTT_REGIONS)
1728 // Since the cue will be removed from the media element and likely the
1729 // TextTrack might also be destructed, notifying the region of the cue
1730 // removal shouldn't be done.
1731 if (cue->isRenderable())
1732 toVTTCue(cue.get())->notifyRegionWhenRemovingDisplayTree(false);
1735 size_t index = m_currentlyActiveCues.find(interval);
1736 if (index != notFound) {
1737 cue->setIsActive(false);
1738 m_currentlyActiveCues.remove(index);
1741 if (cue->isRenderable())
1742 toVTTCue(cue.get())->removeDisplayTree();
1743 updateActiveTextTrackCues(currentMediaTime());
1745 #if ENABLE(WEBVTT_REGIONS)
1746 if (cue->isRenderable())
1747 toVTTCue(cue.get())->notifyRegionWhenRemovingDisplayTree(true);
1753 bool HTMLMediaElement::isSafeToLoadURL(const URL& url, InvalidURLAction actionIfInvalid)
1755 if (!url.isValid()) {
1756 LOG(Media, "HTMLMediaElement::isSafeToLoadURL(%p) - %s -> FALSE because url is invalid", this, urlForLoggingMedia(url).utf8().data());
1760 Frame* frame = document().frame();
1761 if (!frame || !document().securityOrigin()->canDisplay(url)) {
1762 if (actionIfInvalid == Complain)
1763 FrameLoader::reportLocalLoadFailed(frame, url.stringCenterEllipsizedToLength());
1764 LOG(Media, "HTMLMediaElement::isSafeToLoadURL(%p) - %s -> FALSE rejected by SecurityOrigin", this, urlForLoggingMedia(url).utf8().data());
1768 if (!document().contentSecurityPolicy()->allowMediaFromSource(url, isInUserAgentShadowTree())) {
1769 LOG(Media, "HTMLMediaElement::isSafeToLoadURL(%p) - %s -> rejected by Content Security Policy", this, urlForLoggingMedia(url).utf8().data());
1776 void HTMLMediaElement::startProgressEventTimer()
1778 if (m_progressEventTimer.isActive())
1781 m_previousProgressTime = monotonicallyIncreasingTime();
1782 // 350ms is not magic, it is in the spec!
1783 m_progressEventTimer.startRepeating(0.350);
1786 void HTMLMediaElement::waitForSourceChange()
1788 LOG(Media, "HTMLMediaElement::waitForSourceChange(%p)", this);
1790 stopPeriodicTimers();
1791 m_loadState = WaitingForSource;
1793 // 6.17 - Waiting: Set the element's networkState attribute to the NETWORK_NO_SOURCE value
1794 m_networkState = NETWORK_NO_SOURCE;
1796 // 6.18 - Set the element's delaying-the-load-event flag to false. This stops delaying the load event.
1797 setShouldDelayLoadEvent(false);
1799 updateDisplayState();
1802 renderer()->updateFromElement();
1805 void HTMLMediaElement::noneSupported()
1807 LOG(Media, "HTMLMediaElement::noneSupported(%p)", this);
1809 stopPeriodicTimers();
1810 m_loadState = WaitingForSource;
1811 m_currentSourceNode = nullptr;
1814 // 6 - Reaching this step indicates that the media resource failed to load or that the given
1815 // URL could not be resolved. In one atomic operation, run the following steps:
1817 // 6.1 - Set the error attribute to a new MediaError object whose code attribute is set to
1818 // MEDIA_ERR_SRC_NOT_SUPPORTED.
1819 m_error = MediaError::create(MediaError::MEDIA_ERR_SRC_NOT_SUPPORTED);
1821 // 6.2 - Forget the media element's media-resource-specific text tracks.
1822 forgetResourceSpecificTracks();
1824 // 6.3 - Set the element's networkState attribute to the NETWORK_NO_SOURCE value.
1825 m_networkState = NETWORK_NO_SOURCE;
1827 // 7 - Queue a task to fire a simple event named error at the media element.
1828 scheduleEvent(eventNames().errorEvent);
1830 #if ENABLE(MEDIA_SOURCE)
1834 // 8 - Set the element's delaying-the-load-event flag to false. This stops delaying the load event.
1835 setShouldDelayLoadEvent(false);
1837 // 9 - Abort these steps. Until the load() method is invoked or the src attribute is changed,
1838 // the element won't attempt to load another resource.
1840 updateDisplayState();
1843 renderer()->updateFromElement();
1846 void HTMLMediaElement::mediaLoadingFailedFatally(MediaPlayer::NetworkState error)
1848 LOG(Media, "HTMLMediaElement::mediaLoadingFailedFatally(%p) - error = %d", this, static_cast<int>(error));
1850 // 1 - The user agent should cancel the fetching process.
1851 stopPeriodicTimers();
1852 m_loadState = WaitingForSource;
1854 // 2 - Set the error attribute to a new MediaError object whose code attribute is
1855 // set to MEDIA_ERR_NETWORK/MEDIA_ERR_DECODE.
1856 if (error == MediaPlayer::NetworkError)
1857 m_error = MediaError::create(MediaError::MEDIA_ERR_NETWORK);
1858 else if (error == MediaPlayer::DecodeError)
1859 m_error = MediaError::create(MediaError::MEDIA_ERR_DECODE);
1861 ASSERT_NOT_REACHED();
1863 // 3 - Queue a task to fire a simple event named error at the media element.
1864 scheduleEvent(eventNames().errorEvent);
1866 #if ENABLE(MEDIA_SOURCE)
1870 // 4 - Set the element's networkState attribute to the NETWORK_EMPTY value and queue a
1871 // task to fire a simple event called emptied at the element.
1872 m_networkState = NETWORK_EMPTY;
1873 scheduleEvent(eventNames().emptiedEvent);
1875 // 5 - Set the element's delaying-the-load-event flag to false. This stops delaying the load event.
1876 setShouldDelayLoadEvent(false);
1878 // 6 - Abort the overall resource selection algorithm.
1879 m_currentSourceNode = nullptr;
1882 if (is<MediaDocument>(document()))
1883 downcast<MediaDocument>(document()).mediaElementSawUnsupportedTracks();
1887 void HTMLMediaElement::cancelPendingEventsAndCallbacks()
1889 LOG(Media, "HTMLMediaElement::cancelPendingEventsAndCallbacks(%p)", this);
1890 m_asyncEventQueue.cancelAllEvents();
1892 for (auto& source : childrenOfType<HTMLSourceElement>(*this))
1893 source.cancelPendingErrorEvent();
1896 void HTMLMediaElement::mediaPlayerNetworkStateChanged(MediaPlayer*)
1898 beginProcessingMediaPlayerCallback();
1899 setNetworkState(m_player->networkState());
1900 endProcessingMediaPlayerCallback();
1903 static void logMediaLoadRequest(Page* page, const String& mediaEngine, const String& errorMessage, bool succeeded)
1908 DiagnosticLoggingClient& diagnosticLoggingClient = page->mainFrame().diagnosticLoggingClient();
1910 diagnosticLoggingClient.logDiagnosticMessageWithResult(DiagnosticLoggingKeys::mediaLoadingFailedKey(), errorMessage, DiagnosticLoggingResultFail, ShouldSample::No);
1914 diagnosticLoggingClient.logDiagnosticMessage(DiagnosticLoggingKeys::mediaLoadedKey(), mediaEngine, ShouldSample::No);
1916 if (!page->hasSeenAnyMediaEngine())
1917 diagnosticLoggingClient.logDiagnosticMessage(DiagnosticLoggingKeys::pageContainsAtLeastOneMediaEngineKey(), emptyString(), ShouldSample::No);
1919 if (!page->hasSeenMediaEngine(mediaEngine))
1920 diagnosticLoggingClient.logDiagnosticMessage(DiagnosticLoggingKeys::pageContainsMediaEngineKey(), mediaEngine, ShouldSample::No);
1922 page->sawMediaEngine(mediaEngine);
1925 static String stringForNetworkState(MediaPlayer::NetworkState state)
1928 case MediaPlayer::Empty: return ASCIILiteral("Empty");
1929 case MediaPlayer::Idle: return ASCIILiteral("Idle");
1930 case MediaPlayer::Loading: return ASCIILiteral("Loading");
1931 case MediaPlayer::Loaded: return ASCIILiteral("Loaded");
1932 case MediaPlayer::FormatError: return ASCIILiteral("FormatError");
1933 case MediaPlayer::NetworkError: return ASCIILiteral("NetworkError");
1934 case MediaPlayer::DecodeError: return ASCIILiteral("DecodeError");
1935 default: return emptyString();
1939 void HTMLMediaElement::mediaLoadingFailed(MediaPlayer::NetworkState error)
1941 stopPeriodicTimers();
1943 // If we failed while trying to load a <source> element, the movie was never parsed, and there are more
1944 // <source> children, schedule the next one
1945 if (m_readyState < HAVE_METADATA && m_loadState == LoadingFromSourceElement) {
1947 // resource selection algorithm
1948 // 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.
1949 if (m_currentSourceNode)
1950 m_currentSourceNode->scheduleErrorEvent();
1952 LOG(Media, "HTMLMediaElement::setNetworkState(%p) - error event not sent, <source> was removed", this);
1954 // 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.
1956 // 9.Otherwise.11 - Forget the media element's media-resource-specific tracks.
1957 forgetResourceSpecificTracks();
1959 if (havePotentialSourceChild()) {
1960 LOG(Media, "HTMLMediaElement::setNetworkState(%p) - scheduling next <source>", this);
1961 scheduleNextSourceChild();
1963 LOG(Media, "HTMLMediaElement::setNetworkState(%p) - no more <source> elements, waiting", this);
1964 waitForSourceChange();
1970 if ((error == MediaPlayer::NetworkError && m_readyState >= HAVE_METADATA) || error == MediaPlayer::DecodeError)
1971 mediaLoadingFailedFatally(error);
1972 else if ((error == MediaPlayer::FormatError || error == MediaPlayer::NetworkError) && m_loadState == LoadingFromSrcAttr)
1975 updateDisplayState();
1976 if (hasMediaControls()) {
1977 mediaControls()->reset();
1978 mediaControls()->reportedError();
1981 logMediaLoadRequest(document().page(), String(), stringForNetworkState(error), false);
1984 void HTMLMediaElement::setNetworkState(MediaPlayer::NetworkState state)
1986 LOG(Media, "HTMLMediaElement::setNetworkState(%p) - new state = %d, current state = %d", this, static_cast<int>(state), static_cast<int>(m_networkState));
1988 if (state == MediaPlayer::Empty) {
1989 // Just update the cached state and leave, we can't do anything.
1990 m_networkState = NETWORK_EMPTY;
1994 if (state == MediaPlayer::FormatError || state == MediaPlayer::NetworkError || state == MediaPlayer::DecodeError) {
1995 mediaLoadingFailed(state);
1999 if (state == MediaPlayer::Idle) {
2000 if (m_networkState > NETWORK_IDLE) {
2001 changeNetworkStateFromLoadingToIdle();
2002 setShouldDelayLoadEvent(false);
2004 m_networkState = NETWORK_IDLE;
2008 if (state == MediaPlayer::Loading) {
2009 if (m_networkState < NETWORK_LOADING || m_networkState == NETWORK_NO_SOURCE)
2010 startProgressEventTimer();
2011 m_networkState = NETWORK_LOADING;
2014 if (state == MediaPlayer::Loaded) {
2015 if (m_networkState != NETWORK_IDLE)
2016 changeNetworkStateFromLoadingToIdle();
2017 m_completelyLoaded = true;
2020 if (hasMediaControls())
2021 mediaControls()->updateStatusDisplay();
2024 void HTMLMediaElement::changeNetworkStateFromLoadingToIdle()
2026 m_progressEventTimer.stop();
2027 if (hasMediaControls() && m_player->didLoadingProgress())
2028 mediaControls()->bufferingProgressed();
2030 // Schedule one last progress event so we guarantee that at least one is fired
2031 // for files that load very quickly.
2032 scheduleEvent(eventNames().progressEvent);
2033 scheduleEvent(eventNames().suspendEvent);
2034 m_networkState = NETWORK_IDLE;
2037 void HTMLMediaElement::mediaPlayerReadyStateChanged(MediaPlayer*)
2039 beginProcessingMediaPlayerCallback();
2041 setReadyState(m_player->readyState());
2043 endProcessingMediaPlayerCallback();
2046 void HTMLMediaElement::setReadyState(MediaPlayer::ReadyState state)
2048 LOG(Media, "HTMLMediaElement::setReadyState(%p) - new state = %d, current state = %d,", this, static_cast<int>(state), static_cast<int>(m_readyState));
2050 // Set "wasPotentiallyPlaying" BEFORE updating m_readyState, potentiallyPlaying() uses it
2051 bool wasPotentiallyPlaying = potentiallyPlaying();
2053 ReadyState oldState = m_readyState;
2054 ReadyState newState = static_cast<ReadyState>(state);
2056 #if ENABLE(VIDEO_TRACK)
2057 bool tracksAreReady = !RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled() || textTracksAreReady();
2059 if (newState == oldState && m_tracksAreReady == tracksAreReady)
2062 m_tracksAreReady = tracksAreReady;
2064 if (newState == oldState)
2066 bool tracksAreReady = true;
2070 m_readyState = newState;
2072 // If a media file has text tracks the readyState may not progress beyond HAVE_FUTURE_DATA until
2073 // the text tracks are ready, regardless of the state of the media file.
2074 if (newState <= HAVE_METADATA)
2075 m_readyState = newState;
2077 m_readyState = HAVE_CURRENT_DATA;
2080 if (oldState > m_readyStateMaximum)
2081 m_readyStateMaximum = oldState;
2083 if (m_networkState == NETWORK_EMPTY)
2087 // 4.8.10.9, step 11
2088 if (wasPotentiallyPlaying && m_readyState < HAVE_FUTURE_DATA)
2089 scheduleEvent(eventNames().waitingEvent);
2091 // 4.8.10.10 step 14 & 15.
2092 if (!m_player->seeking() && m_readyState >= HAVE_CURRENT_DATA)
2095 if (wasPotentiallyPlaying && m_readyState < HAVE_FUTURE_DATA) {
2097 invalidateCachedTime();
2098 scheduleTimeupdateEvent(false);
2099 scheduleEvent(eventNames().waitingEvent);
2103 if (m_readyState >= HAVE_METADATA && oldState < HAVE_METADATA) {
2104 prepareMediaFragmentURI();
2105 scheduleEvent(eventNames().durationchangeEvent);
2106 scheduleEvent(eventNames().loadedmetadataEvent);
2107 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
2108 if (hasEventListeners(eventNames().webkitplaybacktargetavailabilitychangedEvent))
2109 enqueuePlaybackTargetAvailabilityChangedEvent();
2111 m_initiallyMuted = m_volume < 0.05 || muted();
2113 if (hasMediaControls())
2114 mediaControls()->loadedMetadata();
2116 renderer()->updateFromElement();
2118 logMediaLoadRequest(document().page(), m_player->engineDescription(), String(), true);
2121 bool shouldUpdateDisplayState = false;
2123 if (m_readyState >= HAVE_CURRENT_DATA && oldState < HAVE_CURRENT_DATA && !m_haveFiredLoadedData) {
2124 m_haveFiredLoadedData = true;
2125 shouldUpdateDisplayState = true;
2126 scheduleEvent(eventNames().loadeddataEvent);
2127 setShouldDelayLoadEvent(false);
2128 applyMediaFragmentURI();
2131 bool isPotentiallyPlaying = potentiallyPlaying();
2132 if (m_readyState == HAVE_FUTURE_DATA && oldState <= HAVE_CURRENT_DATA && tracksAreReady) {
2133 scheduleEvent(eventNames().canplayEvent);
2134 if (isPotentiallyPlaying)
2135 scheduleEvent(eventNames().playingEvent);
2136 shouldUpdateDisplayState = true;
2139 if (m_readyState == HAVE_ENOUGH_DATA && oldState < HAVE_ENOUGH_DATA && tracksAreReady) {
2140 if (oldState <= HAVE_CURRENT_DATA)
2141 scheduleEvent(eventNames().canplayEvent);
2143 scheduleEvent(eventNames().canplaythroughEvent);
2145 if (isPotentiallyPlaying && oldState <= HAVE_CURRENT_DATA)
2146 scheduleEvent(eventNames().playingEvent);
2148 if (m_autoplaying && m_paused && autoplay() && !document().isSandboxed(SandboxAutomaticFeatures) && m_mediaSession->playbackPermitted(*this)) {
2150 invalidateCachedTime();
2151 scheduleEvent(eventNames().playEvent);
2152 scheduleEvent(eventNames().playingEvent);
2155 shouldUpdateDisplayState = true;
2158 if (shouldUpdateDisplayState) {
2159 updateDisplayState();
2160 if (hasMediaControls()) {
2161 mediaControls()->refreshClosedCaptionsButtonVisibility();
2162 mediaControls()->updateStatusDisplay();
2167 updateMediaController();
2168 #if ENABLE(VIDEO_TRACK)
2169 if (RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
2170 updateActiveTextTrackCues(currentMediaTime());
2174 #if ENABLE(ENCRYPTED_MEDIA)
2175 void HTMLMediaElement::mediaPlayerKeyAdded(MediaPlayer*, const String& keySystem, const String& sessionId)
2177 MediaKeyEventInit initializer;
2178 initializer.keySystem = keySystem;
2179 initializer.sessionId = sessionId;
2180 initializer.bubbles = false;
2181 initializer.cancelable = false;
2183 RefPtr<Event> event = MediaKeyEvent::create(eventNames().webkitkeyaddedEvent, initializer);
2184 event->setTarget(this);
2185 m_asyncEventQueue.enqueueEvent(event.release());
2188 void HTMLMediaElement::mediaPlayerKeyError(MediaPlayer*, const String& keySystem, const String& sessionId, MediaPlayerClient::MediaKeyErrorCode errorCode, unsigned short systemCode)
2190 MediaKeyError::Code mediaKeyErrorCode = MediaKeyError::MEDIA_KEYERR_UNKNOWN;
2191 switch (errorCode) {
2192 case MediaPlayerClient::UnknownError:
2193 mediaKeyErrorCode = MediaKeyError::MEDIA_KEYERR_UNKNOWN;
2195 case MediaPlayerClient::ClientError:
2196 mediaKeyErrorCode = MediaKeyError::MEDIA_KEYERR_CLIENT;
2198 case MediaPlayerClient::ServiceError:
2199 mediaKeyErrorCode = MediaKeyError::MEDIA_KEYERR_SERVICE;
2201 case MediaPlayerClient::OutputError:
2202 mediaKeyErrorCode = MediaKeyError::MEDIA_KEYERR_OUTPUT;
2204 case MediaPlayerClient::HardwareChangeError:
2205 mediaKeyErrorCode = MediaKeyError::MEDIA_KEYERR_HARDWARECHANGE;
2207 case MediaPlayerClient::DomainError:
2208 mediaKeyErrorCode = MediaKeyError::MEDIA_KEYERR_DOMAIN;
2212 MediaKeyEventInit initializer;
2213 initializer.keySystem = keySystem;
2214 initializer.sessionId = sessionId;
2215 initializer.errorCode = MediaKeyError::create(mediaKeyErrorCode);
2216 initializer.systemCode = systemCode;
2217 initializer.bubbles = false;
2218 initializer.cancelable = false;
2220 RefPtr<Event> event = MediaKeyEvent::create(eventNames().webkitkeyerrorEvent, initializer);
2221 event->setTarget(this);
2222 m_asyncEventQueue.enqueueEvent(event.release());
2225 void HTMLMediaElement::mediaPlayerKeyMessage(MediaPlayer*, const String& keySystem, const String& sessionId, const unsigned char* message, unsigned messageLength, const URL& defaultURL)
2227 MediaKeyEventInit initializer;
2228 initializer.keySystem = keySystem;
2229 initializer.sessionId = sessionId;
2230 initializer.message = Uint8Array::create(message, messageLength);
2231 initializer.defaultURL = defaultURL;
2232 initializer.bubbles = false;
2233 initializer.cancelable = false;
2235 RefPtr<Event> event = MediaKeyEvent::create(eventNames().webkitkeymessageEvent, initializer);
2236 event->setTarget(this);
2237 m_asyncEventQueue.enqueueEvent(event.release());
2240 bool HTMLMediaElement::mediaPlayerKeyNeeded(MediaPlayer*, const String& keySystem, const String& sessionId, const unsigned char* initData, unsigned initDataLength)
2242 if (!hasEventListeners(eventNames().webkitneedkeyEvent)) {
2243 m_error = MediaError::create(MediaError::MEDIA_ERR_ENCRYPTED);
2244 scheduleEvent(eventNames().errorEvent);
2248 MediaKeyEventInit initializer;
2249 initializer.keySystem = keySystem;
2250 initializer.sessionId = sessionId;
2251 initializer.initData = Uint8Array::create(initData, initDataLength);
2252 initializer.bubbles = false;
2253 initializer.cancelable = false;
2255 RefPtr<Event> event = MediaKeyEvent::create(eventNames().webkitneedkeyEvent, initializer);
2256 event->setTarget(this);
2257 m_asyncEventQueue.enqueueEvent(event.release());
2262 #if ENABLE(ENCRYPTED_MEDIA_V2)
2263 RefPtr<ArrayBuffer> HTMLMediaElement::mediaPlayerCachedKeyForKeyId(const String& keyId) const
2265 return m_mediaKeys ? m_mediaKeys->cachedKeyForKeyId(keyId) : nullptr;
2268 bool HTMLMediaElement::mediaPlayerKeyNeeded(MediaPlayer*, Uint8Array* initData)
2270 if (!hasEventListeners("webkitneedkey")) {
2271 m_error = MediaError::create(MediaError::MEDIA_ERR_ENCRYPTED);
2272 scheduleEvent(eventNames().errorEvent);
2276 MediaKeyNeededEventInit initializer;
2277 initializer.initData = initData;
2278 initializer.bubbles = false;
2279 initializer.cancelable = false;
2281 RefPtr<Event> event = MediaKeyNeededEvent::create(eventNames().webkitneedkeyEvent, initializer);
2282 event->setTarget(this);
2283 m_asyncEventQueue.enqueueEvent(event.release());
2288 String HTMLMediaElement::mediaPlayerMediaKeysStorageDirectory() const
2290 Settings* settings = document().settings();
2292 return emptyString();
2294 String storageDirectory = settings->mediaKeysStorageDirectory();
2295 if (storageDirectory.isEmpty())
2296 return emptyString();
2298 SecurityOrigin* origin = document().securityOrigin();
2300 return emptyString();
2302 return pathByAppendingComponent(storageDirectory, origin->databaseIdentifier());
2305 void HTMLMediaElement::setMediaKeys(MediaKeys* mediaKeys)
2307 if (m_mediaKeys == mediaKeys)
2311 m_mediaKeys->setMediaElement(0);
2312 m_mediaKeys = mediaKeys;
2314 m_mediaKeys->setMediaElement(this);
2317 void HTMLMediaElement::keyAdded()
2320 m_player->keyAdded();
2324 void HTMLMediaElement::progressEventTimerFired()
2327 if (m_networkState != NETWORK_LOADING)
2330 double time = monotonicallyIncreasingTime();
2331 double timedelta = time - m_previousProgressTime;
2333 if (m_player->didLoadingProgress()) {
2334 scheduleEvent(eventNames().progressEvent);
2335 m_previousProgressTime = time;
2336 m_sentStalledEvent = false;
2338 renderer()->updateFromElement();
2339 if (hasMediaControls())
2340 mediaControls()->bufferingProgressed();
2341 } else if (timedelta > 3.0 && !m_sentStalledEvent) {
2342 scheduleEvent(eventNames().stalledEvent);
2343 m_sentStalledEvent = true;
2344 setShouldDelayLoadEvent(false);
2348 void HTMLMediaElement::rewind(double timeDelta)
2350 LOG(Media, "HTMLMediaElement::rewind(%p) - %f", this, timeDelta);
2351 setCurrentTime(std::max(currentMediaTime() - MediaTime::createWithDouble(timeDelta), minTimeSeekable()));
2354 void HTMLMediaElement::returnToRealtime()
2356 LOG(Media, "HTMLMediaElement::returnToRealtime(%p)", this);
2357 setCurrentTime(maxTimeSeekable());
2360 void HTMLMediaElement::addPlayedRange(const MediaTime& start, const MediaTime& end)
2362 LOG(Media, "HTMLMediaElement::addPlayedRange(%p) - [%s, %s]", this, toString(start).utf8().data(), toString(end).utf8().data());
2363 if (!m_playedTimeRanges)
2364 m_playedTimeRanges = TimeRanges::create();
2365 m_playedTimeRanges->ranges().add(start, end);
2368 bool HTMLMediaElement::supportsScanning() const
2370 return m_player ? m_player->supportsScanning() : false;
2373 void HTMLMediaElement::prepareToPlay()
2375 LOG(Media, "HTMLMediaElement::prepareToPlay(%p)", this);
2376 if (m_havePreparedToPlay)
2378 m_havePreparedToPlay = true;
2379 m_player->prepareToPlay();
2382 void HTMLMediaElement::fastSeek(double time)
2384 fastSeek(MediaTime::createWithDouble(time));
2387 void HTMLMediaElement::fastSeek(const MediaTime& time)
2389 LOG(Media, "HTMLMediaElement::fastSeek(%p) - %s", this, toString(time).utf8().data());
2391 // 9. If the approximate-for-speed flag is set, adjust the new playback position to a value that will
2392 // allow for playback to resume promptly. If new playback position before this step is before current
2393 // playback position, then the adjusted new playback position must also be before the current playback
2394 // position. Similarly, if the new playback position before this step is after current playback position,
2395 // then the adjusted new playback position must also be after the current playback position.
2396 refreshCachedTime();
2397 MediaTime delta = time - currentMediaTime();
2398 MediaTime negativeTolerance = delta >= MediaTime::zeroTime() ? delta : MediaTime::positiveInfiniteTime();
2399 MediaTime positiveTolerance = delta < MediaTime::zeroTime() ? -delta : MediaTime::positiveInfiniteTime();
2401 seekWithTolerance(time, negativeTolerance, positiveTolerance, true);
2404 void HTMLMediaElement::seek(const MediaTime& time)
2406 LOG(Media, "HTMLMediaElement::seek(%p) - %s", this, toString(time).utf8().data());
2407 seekWithTolerance(time, MediaTime::zeroTime(), MediaTime::zeroTime(), true);
2410 void HTMLMediaElement::seekInternal(const MediaTime& time)
2412 LOG(Media, "HTMLMediaElement::seekInternal(%p) - %s", this, toString(time).utf8().data());
2413 seekWithTolerance(time, MediaTime::zeroTime(), MediaTime::zeroTime(), false);
2416 void HTMLMediaElement::seekWithTolerance(const MediaTime& inTime, const MediaTime& negativeTolerance, const MediaTime& positiveTolerance, bool fromDOM)
2419 MediaTime time = inTime;
2421 // 1 - Set the media element's show poster flag to false.
2422 setDisplayMode(Video);
2424 // 2 - If the media element's readyState is HAVE_NOTHING, abort these steps.
2425 if (m_readyState == HAVE_NOTHING || !m_player)
2428 // If the media engine has been told to postpone loading data, let it go ahead now.
2429 if (m_preload < MediaPlayer::Auto && m_readyState < HAVE_FUTURE_DATA)
2432 // Get the current time before setting m_seeking, m_lastSeekTime is returned once it is set.
2433 refreshCachedTime();
2434 MediaTime now = currentMediaTime();
2436 // 3 - If the element's seeking IDL attribute is true, then another instance of this algorithm is
2437 // already running. Abort that other instance of the algorithm without waiting for the step that
2438 // it is running to complete.
2439 if (m_seekTaskQueue.hasPendingTasks()) {
2440 m_seekTaskQueue.cancelAllTasks();
2441 m_pendingSeek = nullptr;
2444 // 4 - Set the seeking IDL attribute to true.
2445 // The flag will be cleared when the engine tells us the time has actually changed.
2448 if (m_lastSeekTime < now)
2449 addPlayedRange(m_lastSeekTime, now);
2451 m_lastSeekTime = time;
2453 // 5 - If the seek was in response to a DOM method call or setting of an IDL attribute, then continue
2454 // the script. The remainder of these steps must be run asynchronously.
2455 m_pendingSeek = std::make_unique<PendingSeek>(now, time, negativeTolerance, positiveTolerance);
2457 m_seekTaskQueue.enqueueTask(std::bind(&HTMLMediaElement::seekTask, this));
2462 void HTMLMediaElement::seekTask()
2469 ASSERT(m_pendingSeek);
2470 MediaTime now = m_pendingSeek->now;
2471 MediaTime time = m_pendingSeek->targetTime;
2472 MediaTime negativeTolerance = m_pendingSeek->negativeTolerance;
2473 MediaTime positiveTolerance = m_pendingSeek->positiveTolerance;
2474 m_pendingSeek = nullptr;
2476 // 6 - If the new playback position is later than the end of the media resource, then let it be the end
2477 // of the media resource instead.
2478 time = std::min(time, durationMediaTime());
2480 // 7 - If the new playback position is less than the earliest possible position, let it be that position instead.
2481 MediaTime earliestTime = m_player->startTime();
2482 time = std::max(time, earliestTime);
2484 // Ask the media engine for the time value in the movie's time scale before comparing with current time. This
2485 // is necessary because if the seek time is not equal to currentTime but the delta is less than the movie's
2486 // time scale, we will ask the media engine to "seek" to the current movie time, which may be a noop and
2487 // not generate a timechanged callback. This means m_seeking will never be cleared and we will never
2488 // fire a 'seeked' event.
2490 MediaTime mediaTime = m_player->mediaTimeForTimeValue(time);
2491 if (time != mediaTime)
2492 LOG(Media, "HTMLMediaElement::seekTimerFired(%p) - %s - media timeline equivalent is %s", this, toString(time).utf8().data(), toString(mediaTime).utf8().data());
2494 time = m_player->mediaTimeForTimeValue(time);
2496 // 8 - If the (possibly now changed) new playback position is not in one of the ranges given in the
2497 // seekable attribute, then let it be the position in one of the ranges given in the seekable attribute
2498 // that is the nearest to the new playback position. ... If there are no ranges given in the seekable
2499 // attribute then set the seeking IDL attribute to false and abort these steps.
2500 RefPtr<TimeRanges> seekableRanges = seekable();
2502 // Short circuit seeking to the current time by just firing the events if no seek is required.
2503 // Don't skip calling the media engine if we are in poster mode because a seek should always
2504 // cancel poster display.
2505 bool noSeekRequired = !seekableRanges->length() || (time == now && displayMode() != Poster);
2507 #if ENABLE(MEDIA_SOURCE)
2508 // Always notify the media engine of a seek if the source is not closed. This ensures that the source is
2509 // always in a flushed state when the 'seeking' event fires.
2510 if (m_mediaSource && !m_mediaSource->isClosed())
2511 noSeekRequired = false;
2514 if (noSeekRequired) {
2516 scheduleEvent(eventNames().seekingEvent);
2517 scheduleTimeupdateEvent(false);
2518 scheduleEvent(eventNames().seekedEvent);
2523 time = seekableRanges->ranges().nearest(time);
2525 m_sentEndEvent = false;
2526 m_lastSeekTime = time;
2528 // 10 - Queue a task to fire a simple event named seeking at the element.
2529 scheduleEvent(eventNames().seekingEvent);
2531 // 11 - Set the current playback position to the given new playback position
2532 m_player->seekWithTolerance(time, negativeTolerance, positiveTolerance);
2534 // 12 - Wait until the user agent has established whether or not the media data for the new playback
2535 // position is available, and, if it is, until it has decoded enough data to play back that position.
2536 // 13 - Await a stable state. The synchronous section consists of all the remaining steps of this algorithm.
2539 void HTMLMediaElement::finishSeek()
2541 LOG(Media, "HTMLMediaElement::finishSeek(%p)", this);
2544 // 14 - Set the seeking IDL attribute to false.
2547 // 15 - Run the time maches on steps.
2548 // Handled by mediaPlayerTimeChanged().
2550 // 16 - Queue a task to fire a simple event named timeupdate at the element.
2551 scheduleEvent(eventNames().timeupdateEvent);
2553 // 17 - Queue a task to fire a simple event named seeked at the element.
2554 scheduleEvent(eventNames().seekedEvent);
2556 #if ENABLE(MEDIA_SOURCE)
2558 m_mediaSource->monitorSourceBuffers();
2562 HTMLMediaElement::ReadyState HTMLMediaElement::readyState() const
2564 return m_readyState;
2567 MediaPlayer::MovieLoadType HTMLMediaElement::movieLoadType() const
2569 return m_player ? m_player->movieLoadType() : MediaPlayer::Unknown;
2572 bool HTMLMediaElement::hasAudio() const
2574 return m_player ? m_player->hasAudio() : false;
2577 bool HTMLMediaElement::seeking() const
2582 void HTMLMediaElement::refreshCachedTime() const
2587 m_cachedTime = m_player->currentTime();
2588 if (!m_cachedTime) {
2589 // Do not use m_cachedTime until the media engine returns a non-zero value because we can't
2590 // estimate current time until playback actually begins.
2591 invalidateCachedTime();
2595 m_clockTimeAtLastCachedTimeUpdate = monotonicallyIncreasingTime();
2598 void HTMLMediaElement::invalidateCachedTime() const
2600 if (!m_player || !m_player->maximumDurationToCacheMediaTime())
2604 if (m_cachedTime.isValid())
2605 LOG(Media, "HTMLMediaElement::invalidateCachedTime(%p)", this);
2608 // Don't try to cache movie time when playback first starts as the time reported by the engine
2609 // sometimes fluctuates for a short amount of time, so the cached time will be off if we take it
2611 static const double minimumTimePlayingBeforeCacheSnapshot = 0.5;
2613 m_minimumClockTimeToUpdateCachedTime = monotonicallyIncreasingTime() + minimumTimePlayingBeforeCacheSnapshot;
2614 m_cachedTime = MediaTime::invalidTime();
2618 double HTMLMediaElement::currentTime() const
2620 return currentMediaTime().toDouble();
2623 MediaTime HTMLMediaElement::currentMediaTime() const
2625 #if LOG_CACHED_TIME_WARNINGS
2626 static const MediaTime minCachedDeltaForWarning = MediaTime::create(1, 100);
2630 return MediaTime::zeroTime();
2633 LOG(Media, "HTMLMediaElement::currentTime(%p) - seeking, returning %s", this, toString(m_lastSeekTime).utf8().data());
2634 return m_lastSeekTime;
2637 if (m_cachedTime.isValid() && m_paused) {
2638 #if LOG_CACHED_TIME_WARNINGS
2639 MediaTime delta = m_cachedTime - m_player->currentTime();
2640 if (delta > minCachedDeltaForWarning)
2641 LOG(Media, "HTMLMediaElement::currentTime(%p) - WARNING, cached time is %s seconds off of media time when paused", this, toString(delta).utf8().data());
2643 return m_cachedTime;
2646 // Is it too soon use a cached time?
2647 double now = monotonicallyIncreasingTime();
2648 double maximumDurationToCacheMediaTime = m_player->maximumDurationToCacheMediaTime();
2650 if (maximumDurationToCacheMediaTime && m_cachedTime.isValid() && !m_paused && now > m_minimumClockTimeToUpdateCachedTime) {
2651 double clockDelta = now - m_clockTimeAtLastCachedTimeUpdate;
2653 // Not too soon, use the cached time only if it hasn't expired.
2654 if (clockDelta < maximumDurationToCacheMediaTime) {
2655 MediaTime adjustedCacheTime = m_cachedTime + MediaTime::createWithDouble(effectivePlaybackRate() * clockDelta);
2657 #if LOG_CACHED_TIME_WARNINGS
2658 MediaTime delta = adjustedCacheTime - m_player->currentTime();
2659 if (delta > minCachedDeltaForWarning)
2660 LOG(Media, "HTMLMediaElement::currentTime(%p) - WARNING, cached time is %f seconds off of media time when playing", this, delta);
2662 return adjustedCacheTime;
2666 #if LOG_CACHED_TIME_WARNINGS
2667 if (maximumDurationToCacheMediaTime && now > m_minimumClockTimeToUpdateCachedTime && m_cachedTime != MediaPlayer::invalidTime()) {
2668 double clockDelta = now - m_clockTimeAtLastCachedTimeUpdate;
2669 MediaTime delta = m_cachedTime + MediaTime::createWithDouble(effectivePlaybackRate() * clockDelta) - m_player->currentTime();
2670 LOG(Media, "HTMLMediaElement::currentTime(%p) - cached time was %s seconds off of media time when it expired", this, toString(delta).utf8().data());
2674 refreshCachedTime();
2676 if (m_cachedTime.isInvalid())
2677 return MediaTime::zeroTime();
2679 return m_cachedTime;
2682 void HTMLMediaElement::setCurrentTime(double time)
2684 setCurrentTime(MediaTime::createWithDouble(time));
2687 void HTMLMediaElement::setCurrentTime(const MediaTime& time)
2689 if (m_mediaController)
2695 void HTMLMediaElement::setCurrentTime(double time, ExceptionCode& ec)
2697 // On setting, if the media element has a current media controller, then the user agent must
2698 // throw an InvalidStateError exception
2699 if (m_mediaController) {
2700 ec = INVALID_STATE_ERR;
2704 seek(MediaTime::createWithDouble(time));
2707 double HTMLMediaElement::duration() const
2709 return durationMediaTime().toDouble();
2712 MediaTime HTMLMediaElement::durationMediaTime() const
2714 if (m_player && m_readyState >= HAVE_METADATA)
2715 return m_player->duration();
2717 return MediaTime::invalidTime();
2720 bool HTMLMediaElement::paused() const
2722 // As of this writing, JavaScript garbage collection calls this function directly. In the past
2723 // we had problems where this was called on an object after a bad cast. The assertion below
2724 // made our regression test detect the problem, so we should keep it because of that. But note
2725 // that the value of the assertion relies on the compiler not being smart enough to know that
2726 // isHTMLUnknownElement is guaranteed to return false for an HTMLMediaElement.
2727 ASSERT(!isHTMLUnknownElement());
2732 double HTMLMediaElement::defaultPlaybackRate() const
2734 return m_defaultPlaybackRate;
2737 void HTMLMediaElement::setDefaultPlaybackRate(double rate)
2739 if (m_defaultPlaybackRate != rate) {
2740 LOG(Media, "HTMLMediaElement::setDefaultPlaybackRate(%p) - %f", this, rate);
2741 m_defaultPlaybackRate = rate;
2742 scheduleEvent(eventNames().ratechangeEvent);
2746 double HTMLMediaElement::effectivePlaybackRate() const
2748 return m_mediaController ? m_mediaController->playbackRate() : m_reportedPlaybackRate;
2751 double HTMLMediaElement::requestedPlaybackRate() const
2753 return m_mediaController ? m_mediaController->playbackRate() : m_requestedPlaybackRate;
2756 double HTMLMediaElement::playbackRate() const
2758 return m_requestedPlaybackRate;
2761 void HTMLMediaElement::setPlaybackRate(double rate)
2763 LOG(Media, "HTMLMediaElement::setPlaybackRate(%p) - %f", this, rate);
2765 if (m_player && potentiallyPlaying() && m_player->rate() != rate && !m_mediaController)
2766 m_player->setRate(rate);
2768 if (m_requestedPlaybackRate != rate) {
2769 m_reportedPlaybackRate = m_requestedPlaybackRate = rate;
2770 invalidateCachedTime();
2771 scheduleEvent(eventNames().ratechangeEvent);
2775 void HTMLMediaElement::updatePlaybackRate()
2777 double requestedRate = requestedPlaybackRate();
2778 if (m_player && potentiallyPlaying() && m_player->rate() != requestedRate)
2779 m_player->setRate(requestedRate);
2782 bool HTMLMediaElement::webkitPreservesPitch() const
2784 return m_webkitPreservesPitch;
2787 void HTMLMediaElement::setWebkitPreservesPitch(bool preservesPitch)
2789 LOG(Media, "HTMLMediaElement::setWebkitPreservesPitch(%p) - %s", this, boolString(preservesPitch));
2791 m_webkitPreservesPitch = preservesPitch;
2796 m_player->setPreservesPitch(preservesPitch);
2799 bool HTMLMediaElement::ended() const
2801 // 4.8.10.8 Playing the media resource
2802 // The ended attribute must return true if the media element has ended
2803 // playback and the direction of playback is forwards, and false otherwise.
2804 return endedPlayback() && requestedPlaybackRate() > 0;
2807 bool HTMLMediaElement::autoplay() const
2809 return fastHasAttribute(autoplayAttr);
2812 String HTMLMediaElement::preload() const
2814 switch (m_preload) {
2815 case MediaPlayer::None:
2816 return ASCIILiteral("none");
2817 case MediaPlayer::MetaData:
2818 return ASCIILiteral("metadata");
2819 case MediaPlayer::Auto:
2820 return ASCIILiteral("auto");
2823 ASSERT_NOT_REACHED();
2827 void HTMLMediaElement::setPreload(const String& preload)
2829 LOG(Media, "HTMLMediaElement::setPreload(%p) - %s", this, preload.utf8().data());
2830 setAttribute(preloadAttr, preload);
2833 void HTMLMediaElement::play()
2835 LOG(Media, "HTMLMediaElement::play(%p)", this);
2837 if (!m_mediaSession->playbackPermitted(*this))
2839 if (ScriptController::processingUserGesture())
2840 removeBehaviorsRestrictionsAfterFirstUserGesture();
2845 void HTMLMediaElement::playInternal()
2847 LOG(Media, "HTMLMediaElement::playInternal(%p)", this);
2849 if (!m_mediaSession->clientWillBeginPlayback()) {
2850 LOG(Media, " returning because of interruption");
2854 // 4.8.10.9. Playing the media resource
2855 if (!m_player || m_networkState == NETWORK_EMPTY)
2856 scheduleDelayedAction(LoadMediaResource);
2858 if (endedPlayback())
2859 seekInternal(MediaTime::zeroTime());
2861 if (m_mediaController)
2862 m_mediaController->bringElementUpToSpeed(this);
2866 invalidateCachedTime();
2867 scheduleEvent(eventNames().playEvent);
2869 if (m_readyState <= HAVE_CURRENT_DATA)
2870 scheduleEvent(eventNames().waitingEvent);
2871 else if (m_readyState >= HAVE_FUTURE_DATA)
2872 scheduleEvent(eventNames().playingEvent);
2874 #if ENABLE(MEDIA_SESSION)
2875 // 6.3 Activating a media session from a media element
2876 // When the play() method is invoked, the paused attribute is true, and the readyState attribute has the value
2877 // HAVE_FUTURE_DATA or HAVE_ENOUGH_DATA, then
2878 // 1. Let media session be the value of the current media session.
2879 // 2. If we are not currently in media session's list of active participating media elements then append
2880 // ourselves to this list.
2881 // 3. Let activated be the result of running the media session invocation algorithm for media session.
2882 // 4. If activated is failure, pause ourselves.
2883 if (m_readyState == HAVE_ENOUGH_DATA || m_readyState == HAVE_FUTURE_DATA) {
2885 m_session->addActiveMediaElement(*this);
2887 if (!m_session->invoke()) {
2895 m_autoplaying = false;
2897 updateMediaController();
2900 void HTMLMediaElement::pause()
2902 LOG(Media, "HTMLMediaElement::pause(%p)", this);
2904 if (!m_mediaSession->playbackPermitted(*this))
2911 void HTMLMediaElement::pauseInternal()
2913 LOG(Media, "HTMLMediaElement::pauseInternal(%p)", this);
2915 if (!m_mediaSession->clientWillPausePlayback()) {
2916 LOG(Media, " returning because of interruption");
2920 // 4.8.10.9. Playing the media resource
2921 if (!m_player || m_networkState == NETWORK_EMPTY) {
2922 // Unless the restriction on media requiring user action has been lifted
2923 // don't trigger loading if a script calls pause().
2924 if (!m_mediaSession->playbackPermitted(*this))
2926 scheduleDelayedAction(LoadMediaResource);
2929 m_autoplaying = false;
2933 scheduleTimeupdateEvent(false);
2934 scheduleEvent(eventNames().pauseEvent);
2936 if (MemoryPressureHandler::singleton().isUnderMemoryPressure())
2937 purgeBufferedDataIfPossible();
2943 #if ENABLE(MEDIA_SOURCE)
2944 void HTMLMediaElement::closeMediaSource()
2949 m_mediaSource->close();
2950 m_mediaSource = nullptr;
2954 #if ENABLE(ENCRYPTED_MEDIA)
2955 void HTMLMediaElement::webkitGenerateKeyRequest(const String& keySystem, PassRefPtr<Uint8Array> initData, ExceptionCode& ec)
2957 #if ENABLE(ENCRYPTED_MEDIA_V2)
2958 static bool firstTime = true;
2959 if (firstTime && scriptExecutionContext()) {
2960 scriptExecutionContext()->addConsoleMessage(MessageSource::JS, MessageLevel::Warning, ASCIILiteral("'HTMLMediaElement.webkitGenerateKeyRequest()' is deprecated. Use 'MediaKeys.createSession()' instead."));
2965 if (keySystem.isEmpty()) {
2971 ec = INVALID_STATE_ERR;
2975 const unsigned char* initDataPointer = 0;
2976 unsigned initDataLength = 0;
2978 initDataPointer = initData->data();
2979 initDataLength = initData->length();
2982 MediaPlayer::MediaKeyException result = m_player->generateKeyRequest(keySystem, initDataPointer, initDataLength);
2983 ec = exceptionCodeForMediaKeyException(result);
2986 void HTMLMediaElement::webkitGenerateKeyRequest(const String& keySystem, ExceptionCode& ec)
2988 webkitGenerateKeyRequest(keySystem, Uint8Array::create(0), ec);
2991 void HTMLMediaElement::webkitAddKey(const String& keySystem, PassRefPtr<Uint8Array> key, PassRefPtr<Uint8Array> initData, const String& sessionId, ExceptionCode& ec)
2993 #if ENABLE(ENCRYPTED_MEDIA_V2)
2994 static bool firstTime = true;
2995 if (firstTime && scriptExecutionContext()) {
2996 scriptExecutionContext()->addConsoleMessage(MessageSource::JS, MessageLevel::Warning, ASCIILiteral("'HTMLMediaElement.webkitAddKey()' is deprecated. Use 'MediaKeySession.update()' instead."));
3001 if (keySystem.isEmpty()) {
3011 if (!key->length()) {
3012 ec = TYPE_MISMATCH_ERR;
3017 ec = INVALID_STATE_ERR;
3021 const unsigned char* initDataPointer = 0;
3022 unsigned initDataLength = 0;
3024 initDataPointer = initData->data();
3025 initDataLength = initData->length();
3028 MediaPlayer::MediaKeyException result = m_player->addKey(keySystem, key->data(), key->length(), initDataPointer, initDataLength, sessionId);
3029 ec = exceptionCodeForMediaKeyException(result);
3032 void HTMLMediaElement::webkitAddKey(const String& keySystem, PassRefPtr<Uint8Array> key, ExceptionCode& ec)
3034 webkitAddKey(keySystem, key, Uint8Array::create(0), String(), ec);
3037 void HTMLMediaElement::webkitCancelKeyRequest(const String& keySystem, const String& sessionId, ExceptionCode& ec)
3039 if (keySystem.isEmpty()) {
3045 ec = INVALID_STATE_ERR;
3049 MediaPlayer::MediaKeyException result = m_player->cancelKeyRequest(keySystem, sessionId);
3050 ec = exceptionCodeForMediaKeyException(result);
3055 bool HTMLMediaElement::loop() const
3057 return fastHasAttribute(loopAttr);
3060 void HTMLMediaElement::setLoop(bool b)
3062 LOG(Media, "HTMLMediaElement::setLoop(%p) - %s", this, boolString(b));
3063 setBooleanAttribute(loopAttr, b);
3066 bool HTMLMediaElement::controls() const
3068 Frame* frame = document().frame();
3070 // always show controls when scripting is disabled
3071 if (frame && !frame->script().canExecuteScripts(NotAboutToExecuteScript))
3074 return fastHasAttribute(controlsAttr);
3077 void HTMLMediaElement::setControls(bool b)
3079 LOG(Media, "HTMLMediaElement::setControls(%p) - %s", this, boolString(b));
3080 setBooleanAttribute(controlsAttr, b);
3083 double HTMLMediaElement::volume() const
3088 void HTMLMediaElement::setVolume(double vol, ExceptionCode& ec)
3090 LOG(Media, "HTMLMediaElement::setVolume(%p) - %f", this, vol);
3092 if (vol < 0.0f || vol > 1.0f) {
3093 ec = INDEX_SIZE_ERR;
3098 if (m_volume != vol) {
3100 m_volumeInitialized = true;
3102 scheduleEvent(eventNames().volumechangeEvent);
3107 bool HTMLMediaElement::muted() const
3109 return m_explicitlyMuted ? m_muted : fastHasAttribute(mutedAttr);
3112 void HTMLMediaElement::setMuted(bool muted)
3114 LOG(Media, "HTMLMediaElement::setMuted(%p) - %s", this, boolString(muted));
3117 UNUSED_PARAM(muted);
3119 if (m_muted != muted || !m_explicitlyMuted) {
3121 m_explicitlyMuted = true;
3122 // Avoid recursion when the player reports volume changes.
3123 if (!processingMediaPlayerCallback()) {
3125 m_player->setMuted(effectiveMuted());
3126 if (hasMediaControls())
3127 mediaControls()->changedMute();
3130 scheduleEvent(eventNames().volumechangeEvent);
3131 document().updateIsPlayingMedia();
3133 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
3134 updateMediaState(UpdateMediaState::Asynchronously);
3140 void HTMLMediaElement::togglePlayState()
3142 LOG(Media, "HTMLMediaElement::togglePlayState(%p) - canPlay() is %s", this, boolString(canPlay()));
3144 // We can safely call the internal play/pause methods, which don't check restrictions, because
3145 // this method is only called from the built-in media controller
3147 updatePlaybackRate();
3153 void HTMLMediaElement::beginScrubbing()
3155 LOG(Media, "HTMLMediaElement::beginScrubbing(%p) - paused() is %s", this, boolString(paused()));
3159 // Because a media element stays in non-paused state when it reaches end, playback resumes
3160 // when the slider is dragged from the end to another position unless we pause first. Do
3161 // a "hard pause" so an event is generated, since we want to stay paused after scrubbing finishes.
3164 // Not at the end but we still want to pause playback so the media engine doesn't try to
3165 // continue playing during scrubbing. Pause without generating an event as we will
3166 // unpause after scrubbing finishes.
3167 setPausedInternal(true);
3172 void HTMLMediaElement::endScrubbing()
3174 LOG(Media, "HTMLMediaElement::endScrubbing(%p) - m_pausedInternal is %s", this, boolString(m_pausedInternal));
3176 if (m_pausedInternal)
3177 setPausedInternal(false);
3180 void HTMLMediaElement::beginScanning(ScanDirection direction)
3182 m_scanType = supportsScanning() ? Scan : Seek;
3183 m_scanDirection = direction;
3185 if (m_scanType == Seek) {
3186 // Scanning by seeking requires the video to be paused during scanning.
3187 m_actionAfterScan = paused() ? Nothing : Play;
3190 // Scanning by scanning requires the video to be playing during scanninging.
3191 m_actionAfterScan = paused() ? Pause : Nothing;
3193 setPlaybackRate(nextScanRate());
3196 m_scanTimer.start(0, m_scanType == Seek ? SeekRepeatDelay : ScanRepeatDelay);
3199 void HTMLMediaElement::endScanning()
3201 if (m_scanType == Scan)
3202 setPlaybackRate(defaultPlaybackRate());
3204 if (m_actionAfterScan == Play)
3206 else if (m_actionAfterScan == Pause)
3209 if (m_scanTimer.isActive())
3213 double HTMLMediaElement::nextScanRate()
3215 double rate = std::min(ScanMaximumRate, fabs(playbackRate() * 2));
3216 if (m_scanDirection == Backward)
3219 rate = std::min(std::max(rate, minFastReverseRate()), maxFastForwardRate());
3224 void HTMLMediaElement::scanTimerFired()
3226 if (m_scanType == Seek) {
3227 double seekTime = m_scanDirection == Forward ? SeekTime : -SeekTime;
3228 setCurrentTime(currentTime() + seekTime);
3230 setPlaybackRate(nextScanRate());
3233 // The spec says to fire periodic timeupdate events (those sent while playing) every
3234 // "15 to 250ms", we choose the slowest frequency
3235 static const double maxTimeupdateEventFrequency = 0.25;
3237 void HTMLMediaElement::startPlaybackProgressTimer()
3239 if (m_playbackProgressTimer.isActive())
3242 m_previousProgressTime = monotonicallyIncreasingTime();
3243 m_playbackProgressTimer.startRepeating(maxTimeupdateEventFrequency);
3246 void HTMLMediaElement::playbackProgressTimerFired()
3250 if (m_fragmentEndTime.isValid() && currentMediaTime() >= m_fragmentEndTime && requestedPlaybackRate() > 0) {
3251 m_fragmentEndTime = MediaTime::invalidTime();
3252 if (!m_mediaController && !m_paused) {
3253 // changes paused to true and fires a simple event named pause at the media element.
3258 scheduleTimeupdateEvent(true);
3260 if (!requestedPlaybackRate())
3263 if (!m_paused && hasMediaControls())
3264 mediaControls()->playbackProgressed();
3266 #if ENABLE(VIDEO_TRACK)
3267 if (RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
3268 updateActiveTextTrackCues(currentMediaTime());
3271 #if ENABLE(MEDIA_SOURCE)
3273 m_mediaSource->monitorSourceBuffers();
3277 void HTMLMediaElement::scheduleTimeupdateEvent(bool periodicEvent)
3279 double now = monotonicallyIncreasingTime();
3280 double timedelta = now - m_clockTimeAtLastUpdateEvent;
3282 // throttle the periodic events
3283 if (periodicEvent && timedelta < maxTimeupdateEventFrequency)
3286 // Some media engines make multiple "time changed" callbacks at the same time, but we only want one
3287 // event at a given time so filter here
3288 MediaTime movieTime = currentMediaTime();
3289 if (movieTime != m_lastTimeUpdateEventMovieTime) {
3290 scheduleEvent(eventNames().timeupdateEvent);
3291 m_clockTimeAtLastUpdateEvent = now;
3292 m_lastTimeUpdateEventMovieTime = movieTime;
3296 bool HTMLMediaElement::canPlay() const
3298 return paused() || ended() || m_readyState < HAVE_METADATA;
3301 double HTMLMediaElement::percentLoaded() const
3305 MediaTime duration = m_player->duration();
3307 if (!duration || duration.isPositiveInfinite() || duration.isNegativeInfinite())
3310 MediaTime buffered = MediaTime::zeroTime();
3312 std::unique_ptr<PlatformTimeRanges> timeRanges = m_player->buffered();
3313 for (unsigned i = 0; i < timeRanges->length(); ++i) {
3314 MediaTime start = timeRanges->start(i, ignored);
3315 MediaTime end = timeRanges->end(i, ignored);
3316 buffered += end - start;
3318 return buffered.toDouble() / duration.toDouble();
3321 #if ENABLE(VIDEO_TRACK)
3323 void HTMLMediaElement::mediaPlayerDidAddAudioTrack(PassRefPtr<AudioTrackPrivate> prpTrack)
3325 if (isPlaying() && !m_mediaSession->playbackPermitted(*this))
3328 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
3331 addAudioTrack(AudioTrack::create(this, prpTrack));
3334 void HTMLMediaElement::mediaPlayerDidAddTextTrack(PassRefPtr<InbandTextTrackPrivate> prpTrack)
3336 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
3339 // 4.8.10.12.2 Sourcing in-band text tracks
3340 // 1. Associate the relevant data with a new text track and its corresponding new TextTrack object.
3341 RefPtr<InbandTextTrack> textTrack = InbandTextTrack::create(ActiveDOMObject::scriptExecutionContext(), this, prpTrack);
3342 textTrack->setMediaElement(this);
3344 // 2. Set the new text track's kind, label, and language based on the semantics of the relevant data,
3345 // as defined by the relevant specification. If there is no label in that data, then the label must
3346 // be set to the empty string.
3347 // 3. Associate the text track list of cues with the rules for updating the text track rendering appropriate
3348 // for the format in question.
3349 // 4. If the new text track's kind is metadata, then set the text track in-band metadata track dispatch type
3350 // as follows, based on the type of the media resource:
3351 // 5. Populate the new text track's list of cues with the cues parsed so far, folllowing the guidelines for exposing
3352 // cues, and begin updating it dynamically as necessary.
3353 // - Thess are all done by the media engine.
3355 // 6. Set the new text track's readiness state to loaded.
3356 textTrack->setReadinessState(TextTrack::Loaded);
3358 // 7. Set the new text track's mode to the mode consistent with the user's preferences and the requirements of
3359 // the relevant specification for the data.
3360 // - This will happen in configureTextTracks()
3361 scheduleDelayedAction(ConfigureTextTracks);
3363 // 8. Add the new text track to the media element's list of text tracks.
3364 // 9. Fire an event with the name addtrack, that does not bubble and is not cancelable, and that uses the TrackEvent
3365 // interface, with the track attribute initialized to the text track's TextTrack object, at the media element's
3366 // textTracks attribute's TextTrackList object.
3367 addTextTrack(textTrack.release());
3370 void HTMLMediaElement::mediaPlayerDidAddVideoTrack(PassRefPtr<VideoTrackPrivate> prpTrack)
3372 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
3375 addVideoTrack(VideoTrack::create(this, prpTrack));
3378 void HTMLMediaElement::mediaPlayerDidRemoveAudioTrack(PassRefPtr<AudioTrackPrivate> prpTrack)
3380 prpTrack->willBeRemoved();
3383 void HTMLMediaElement::mediaPlayerDidRemoveTextTrack(PassRefPtr<InbandTextTrackPrivate> prpTrack)
3385 prpTrack->willBeRemoved();
3388 void HTMLMediaElement::mediaPlayerDidRemoveVideoTrack(PassRefPtr<VideoTrackPrivate> prpTrack)
3390 prpTrack->willBeRemoved();
3393 #if USE(PLATFORM_TEXT_TRACK_MENU)
3394 void HTMLMediaElement::setSelectedTextTrack(PassRefPtr<PlatformTextTrack> platformTrack)
3399 TrackDisplayUpdateScope scope(this);
3401 if (!platformTrack) {
3402 setSelectedTextTrack(TextTrack::captionMenuOffItem());
3406 TextTrack* textTrack;
3407 if (platformTrack == PlatformTextTrack::captionMenuOffItem())
3408 textTrack = TextTrack::captionMenuOffItem();
3409 else if (platformTrack == PlatformTextTrack::captionMenuAutomaticItem())
3410 textTrack = TextTrack::captionMenuAutomaticItem();
3413 for (i = 0; i < m_textTracks->length(); ++i) {
3414 textTrack = m_textTracks->item(i);
3416 if (textTrack->platformTextTrack() == platformTrack)
3419 if (i == m_textTracks->length())
3423 setSelectedTextTrack(textTrack);
3426 Vector<RefPtr<PlatformTextTrack>> HTMLMediaElement::platformTextTracks()
3428 if (!m_textTracks || !m_textTracks->length())
3429 return Vector<RefPtr<PlatformTextTrack>>();
3431 Vector<RefPtr<PlatformTextTrack>> platformTracks;
3432 for (size_t i = 0; i < m_textTracks->length(); ++i)
3433 platformTracks.append(m_textTracks->item(i)->platformTextTrack());
3435 return platformTracks;
3438 void HTMLMediaElement::notifyMediaPlayerOfTextTrackChanges()
3440 if (!m_textTracks || !m_textTracks->length() || !platformTextTrackMenu())
3443 m_platformMenu->tracksDidChange();
3446 PlatformTextTrackMenuInterface* HTMLMediaElement::platformTextTrackMenu()
3449 return m_platformMenu.get();
3451 if (!m_player || !m_player->implementsTextTrackControls())
3454 m_platformMenu = m_player->textTrackMenu();
3455 if (!m_platformMenu)
3458 m_platformMenu->setClient(this);
3460 return m_platformMenu.get();
3462 #endif // #if USE(PLATFORM_TEXT_TRACK_MENU)
3464 void HTMLMediaElement::closeCaptionTracksChanged()
3466 if (hasMediaControls())
3467 mediaControls()->closedCaptionTracksChanged();
3469 #if USE(PLATFORM_TEXT_TRACK_MENU)
3470 if (m_player && m_player->implementsTextTrackControls())
3471 scheduleDelayedAction(TextTrackChangesNotification);
3475 void HTMLMediaElement::addAudioTrack(PassRefPtr<AudioTrack> track)
3477 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
3480 audioTracks()->append(track);
3483 void HTMLMediaElement::addTextTrack(PassRefPtr<TextTrack> track)
3485 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
3488 if (!m_requireCaptionPreferencesChangedCallbacks) {
3489 m_requireCaptionPreferencesChangedCallbacks = true;
3490 document().registerForCaptionPreferencesChangedCallbacks(this);
3493 textTracks()->append(track);
3495 closeCaptionTracksChanged();
3498 void HTMLMediaElement::addVideoTrack(PassRefPtr<VideoTrack> track)
3500 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
3503 videoTracks()->append(track);
3506 void HTMLMediaElement::removeAudioTrack(AudioTrack* track)
3508 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
3511 m_audioTracks->remove(track);
3514 void HTMLMediaElement::removeTextTrack(TextTrack* track, bool scheduleEvent)
3516 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
3519 TrackDisplayUpdateScope scope(this);
3520 if (TextTrackCueList* cues = track->cues())
3521 textTrackRemoveCues(track, cues);
3522 track->clearClient();
3524 m_textTracks->remove(track, scheduleEvent);
3526 closeCaptionTracksChanged();
3529 void HTMLMediaElement::removeVideoTrack(VideoTrack* track)
3531 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
3534 m_videoTracks->remove(track);
3537 void HTMLMediaElement::forgetResourceSpecificTracks()
3539 while (m_audioTracks && m_audioTracks->length())
3540 removeAudioTrack(m_audioTracks->lastItem());
3543 TrackDisplayUpdateScope scope(this);
3544 for (int i = m_textTracks->length() - 1; i >= 0; --i) {
3545 TextTrack* track = m_textTracks->item(i);
3547 if (track->trackType() == TextTrack::InBand)
3548 removeTextTrack(track, false);
3552 while (m_videoTracks && m_videoTracks->length())
3553 removeVideoTrack(m_videoTracks->lastItem());
3556 PassRefPtr<TextTrack> HTMLMediaElement::addTextTrack(const String& kind, const String& label, const String& language, ExceptionCode& ec)
3558 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
3561 // 4.8.10.12.4 Text track API
3562 // The addTextTrack(kind, label, language) method of media elements, when invoked, must run the following steps:
3564 // 1. If kind is not one of the following strings, then throw a SyntaxError exception and abort these steps
3565 if (!TextTrack::isValidKindKeyword(kind)) {
3570 // 2. If the label argument was omitted, let label be the empty string.
3571 // 3. If the language argument was omitted, let language be the empty string.
3572 // 4. Create a new TextTrack object.
3574 // 5. Create a new text track corresponding to the new object, and set its text track kind to kind, its text
3575 // track label to label, its text track language to language...
3576 RefPtr<TextTrack> textTrack = TextTrack::create(ActiveDOMObject::scriptExecutionContext(), this, kind, emptyString(), label, language);
3578 // Note, due to side effects when changing track parameters, we have to
3579 // first append the track to the text track list.
3581 // 6. Add the new text track to the media element's list of text tracks.
3582 addTextTrack(textTrack);
3584 // ... its text track readiness state to the text track loaded state ...
3585 textTrack->setReadinessState(TextTrack::Loaded);
3587 // ... its text track mode to the text track hidden mode, and its text track list of cues to an empty list ...
3588 textTrack->setMode(TextTrack::hiddenKeyword());
3590 return textTrack.release();
3593 AudioTrackList* HTMLMediaElement::audioTracks()
3595 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
3599 m_audioTracks = AudioTrackList::create(this, ActiveDOMObject::scriptExecutionContext());
3601 return m_audioTracks.get();
3604 TextTrackList* HTMLMediaElement::textTracks()
3606 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
3610 m_textTracks = TextTrackList::create(this, ActiveDOMObject::scriptExecutionContext());
3612 return m_textTracks.get();
3615 VideoTrackList* HTMLMediaElement::videoTracks()
3617 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
3621 m_videoTracks = VideoTrackList::create(this, ActiveDOMObject::scriptExecutionContext());
3623 return m_videoTracks.get();
3626 void HTMLMediaElement::didAddTextTrack(HTMLTrackElement* trackElement)
3628 ASSERT(trackElement->hasTagName(trackTag));
3630 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
3633 // 4.8.10.12.3 Sourcing out-of-band text tracks
3634 // When a track element's parent element changes and the new parent is a media element,
3635 // then the user agent must add the track element's corresponding text track to the
3636 // media element's list of text tracks ... [continues in TextTrackList::append]
3637 RefPtr<TextTrack> textTrack = trackElement->track();
3641 addTextTrack(textTrack.release());
3643 // Do not schedule the track loading until parsing finishes so we don't start before all tracks
3644 // in the markup have been added.
3645 if (!m_parsingInProgress)
3646 scheduleDelayedAction(ConfigureTextTracks);
3648 if (hasMediaControls())
3649 mediaControls()->closedCaptionTracksChanged();
3652 void HTMLMediaElement::didRemoveTextTrack(HTMLTrackElement* trackElement)
3654 ASSERT(trackElement->hasTagName(trackTag));
3656 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
3660 if (trackElement->hasTagName(trackTag)) {
3661 URL url = trackElement->getNonEmptyURLAttribute(srcAttr);
3662 LOG(Media, "HTMLMediaElement::didRemoveTrack(%p) - 'src' is %s", this, urlForLoggingMedia(url).utf8().data());
3666 RefPtr<TextTrack> textTrack = trackElement->track();
3670 textTrack->setHasBeenConfigured(false);
3675 // 4.8.10.12.3 Sourcing out-of-band text tracks
3676 // When a track element's parent element changes and the old parent was a media element,
3677 // then the user agent must remove the track element's corresponding text track from the
3678 // media element's list of text tracks.
3679 removeTextTrack(textTrack.get());
3681 size_t index = m_textTracksWhenResourceSelectionBegan.find(textTrack.get());
3682 if (index != notFound)
3683 m_textTracksWhenResourceSelectionBegan.remove(index);
3686 void HTMLMediaElement::configureTextTrackGroup(const TrackGroup& group)
3688 ASSERT(group.tracks.size());
3690 LOG(Media, "HTMLMediaElement::configureTextTrackGroup(%p)", this);
3692 Page* page = document().page();
3693 CaptionUserPreferences* captionPreferences = page ? page->group().captionPreferences() : 0;
3694 CaptionUserPreferences::CaptionDisplayMode displayMode = captionPreferences ? captionPreferences->captionDisplayMode() : CaptionUserPreferences::Automatic;
3696 // First, find the track in the group that should be enabled (if any).
3697 Vector<RefPtr<TextTrack>> currentlyEnabledTracks;
3698 RefPtr<TextTrack> trackToEnable;
3699 RefPtr<TextTrack> defaultTrack;
3700 RefPtr<TextTrack> fallbackTrack;
3701 RefPtr<TextTrack> forcedSubitleTrack;
3702 int highestTrackScore = 0;
3703 int highestForcedScore = 0;
3705 // If there is a visible track, it has already been configured so it won't be considered in the loop below. We don't want to choose another
3706 // track if it is less suitable, and we do want to disable it if another track is more suitable.
3707 int alreadyVisibleTrackScore = 0;
3708 if (group.visibleTrack && captionPreferences) {
3709 alreadyVisibleTrackScore = captionPreferences->textTrackSelectionScore(group.visibleTrack.get(), this);
3710 currentlyEnabledTracks.append(group.visibleTrack);
3713 for (size_t i = 0; i < group.tracks.size(); ++i) {
3714 RefPtr<TextTrack> textTrack = group.tracks[i];
3716 if (m_processingPreferenceChange && textTrack->mode() == TextTrack::showingKeyword())
3717 currentlyEnabledTracks.append(textTrack);
3719 int trackScore = captionPreferences ? captionPreferences->textTrackSelectionScore(textTrack.get(), this) : 0;
3720 LOG(Media, "HTMLMediaElement::configureTextTrackGroup(%p) - '%s' track with language '%s' has score %i", this, textTrack->kind().string().utf8().data(), textTrack->language().string().utf8().data(), trackScore);
3724 // * If the text track kind is { [subtitles or captions] [descriptions] } and the user has indicated an interest in having a
3725 // track with this text track kind, text track language, and text track label enabled, and there is no
3726 // other text track in the media element's list of text tracks with a text track kind of either subtitles
3727 // or captions whose text track mode is showing
3729 // * If the text track kind is chapters and the text track language is one that the user agent has reason
3730 // to believe is appropriate for the user, and there is no other text track in the media element's list of
3731 // text tracks with a text track kind of chapters whose text track mode is showing
3732 // Let the text track mode be showing.
3733 if (trackScore > highestTrackScore && trackScore > alreadyVisibleTrackScore) {
3734 highestTrackScore = trackScore;
3735 trackToEnable = textTrack;
3738 if (!defaultTrack && textTrack->isDefault())
3739 defaultTrack = textTrack;
3740 if (!defaultTrack && !fallbackTrack)
3741 fallbackTrack = textTrack;
3742 if (textTrack->containsOnlyForcedSubtitles() && trackScore > highestForcedScore) {
3743 forcedSubitleTrack = textTrack;
3744 highestForcedScore = trackScore;
3746 } else if (!group.visibleTrack && !defaultTrack && textTrack->isDefault()) {
3747 // * If the track element has a default attribute specified, and there is no other text track in the media
3748 // element's list of text tracks whose text track mode is showing or showing by default
3749 // Let the text track mode be showing by default.
3750 if (group.kind != TrackGroup::CaptionsAndSubtitles || displayMode != CaptionUserPreferences::ForcedOnly)
3751 defaultTrack = textTrack;
3755 if (!trackToEnable && defaultTrack)
3756 trackToEnable = defaultTrack;
3758 // If no track matches the user's preferred language, none was marked as 'default', and there is a forced subtitle track
3759 // in the same language as the language of the primary audio track, enable it.
3760 if (!trackToEnable && forcedSubitleTrack)
3761 trackToEnable = forcedSubitleTrack;
3763 // If no track matches, don't disable an already visible track unless preferences say they all should be off.
3764 if (group.kind != TrackGroup::CaptionsAndSubtitles || displayMode != CaptionUserPreferences::ForcedOnly) {
3765 if (!trackToEnable && !defaultTrack && group.visibleTrack)
3766 trackToEnable = group.visibleTrack;
3769 // If no track matches the user's preferred language and non was marked 'default', enable the first track
3770 // because the user has explicitly stated a preference for this kind of track.
3771 if (!trackToEnable && fallbackTrack)
3772 trackToEnable = fallbackTrack;
3775 m_subtitleTrackLanguage = trackToEnable->language();
3777 m_subtitleTrackLanguage = emptyString();
3779 if (currentlyEnabledTracks.size()) {
3780 for (size_t i = 0; i < currentlyEnabledTracks.size(); ++i) {
3781 RefPtr<TextTrack> textTrack = currentlyEnabledTracks[i];
3782 if (textTrack != trackToEnable)
3783 textTrack->setMode(TextTrack::disabledKeyword());
3787 if (trackToEnable) {
3788 trackToEnable->setMode(TextTrack::showingKeyword());
3790 // If user preferences indicate we should always display captions, make sure we reflect the
3791 // proper status via the webkitClosedCaptionsVisible API call:
3792 if (!webkitClosedCaptionsVisible() && closedCaptionsVisible() && displayMode == CaptionUserPreferences::AlwaysOn)
3793 m_webkitLegacyClosedCaptionOverride = true;
3796 updateCaptionContainer();
3798 m_processingPreferenceChange = false;
3801 static JSC::JSValue controllerJSValue(JSC::ExecState& exec, JSDOMGlobalObject& globalObject, HTMLMediaElement& media)
3803 auto mediaJSWrapper = toJS(&exec, &globalObject, &media);
3805 // Retrieve the controller through the JS object graph
3806 JSC::JSObject* mediaJSWrapperObject = JSC::jsDynamicCast<JSC::JSObject*>(mediaJSWrapper);
3807 if (!mediaJSWrapperObject)
3808 return JSC::jsNull();
3810 JSC::Identifier controlsHost = JSC::Identifier::fromString(&exec.vm(), "controlsHost");
3811 JSC::JSValue controlsHostJSWrapper = mediaJSWrapperObject->get(&exec, controlsHost);
3812 if (exec.hadException())
3813 return JSC::jsNull();
3815 JSC::JSObject* controlsHostJSWrapperObject = JSC::jsDynamicCast<JSC::JSObject*>(controlsHostJSWrapper);
3816 if (!controlsHostJSWrapperObject)
3817 return JSC::jsNull();
3819 JSC::Identifier controllerID = JSC::Identifier::fromString(&exec.vm(), "controller");
3820 JSC::JSValue controllerJSWrapper = controlsHostJSWrapperObject->get(&exec, controllerID);
3821 if (exec.hadException())
3822 return JSC::jsNull();
3824 return controllerJSWrapper;
3827 void HTMLMediaElement::updateCaptionContainer()
3829 LOG(Media, "HTMLMediaElement::updateCaptionContainer(%p)", this);
3830 #if ENABLE(MEDIA_CONTROLS_SCRIPT)
3831 Page* page = document().page();
3835 DOMWrapperWorld& world = ensureIsolatedWorld();
3837 if (!ensureMediaControlsInjectedScript())
3840 ensureUserAgentShadowRoot();
3842 if (!m_mediaControlsHost)
3843 m_mediaControlsHost = MediaControlsHost::create(this);
3845 ScriptController& scriptController = document().frame()->script();
3846 JSDOMGlobalObject* globalObject = JSC::jsCast<JSDOMGlobalObject*>(scriptController.globalObject(world));
3847 JSC::ExecState* exec = globalObject->globalExec();
3848 JSC::JSLockHolder lock(exec);
3850 JSC::JSValue controllerValue = controllerJSValue(*exec, *globalObject, *this);
3851 JSC::JSObject* controllerObject = JSC::jsDynamicCast<JSC::JSObject*>(controllerValue);
3852 if (!controllerObject)
3855 // The media controls script must provide a method on the Controller object with the following details.
3856 // Name: updateCaptionContainer
3861 JSC::JSValue methodValue = controllerObject->get(exec, JSC::Identifier::fromString(exec, "updateCaptionContainer"));
3862 JSC::JSObject* methodObject = JSC::jsDynamicCast<JSC::JSObject*>(methodValue);
3866 JSC::CallData callData;
3867 JSC::CallType callType = methodObject->methodTable()->getCallData(methodObject, callData);
3868 if (callType == JSC::CallTypeNone)
3871 JSC::MarkedArgumentBuffer noArguments;
3872 JSC::call(exec, methodObject, callType, callData, controllerObject, noArguments);
3873 exec->clearException();
3877 void HTMLMediaElement::layoutSizeChanged()
3879 #if ENABLE(MEDIA_CONTROLS_SCRIPT)
3880 RefPtr<HTMLMediaElement> strongThis = this;
3881 std::function<void()> task = [strongThis] {
3882 if (ShadowRoot* root = strongThis->userAgentShadowRoot())
3883 root->dispatchEvent(Event::create("resize", false, false));
3885 m_resizeTaskQueue.enqueueTask(task);
3889 void HTMLMediaElement::setSelectedTextTrack(TextTrack* trackToSelect)
3891 TextTrackList* trackList = textTracks();
3892 if (!trackList || !trackList->length())
3895 if (trackToSelect != TextTrack::captionMenuOffItem() && trackToSelect != TextTrack::captionMenuAutomaticItem()) {
3896 if (!trackList->contains(trackToSelect))
3899 for (int i = 0, length = trackList->length(); i < length; ++i) {
3900 TextTrack* track = trackList->item(i);
3901 if (!trackToSelect || track != trackToSelect)
3902 track->setMode(TextTrack::disabledKeyword());
3904 track->setMode(TextTrack::showingKeyword());
3906 } else if (trackToSelect == TextTrack::captionMenuOffItem()) {
3907 for (int i = 0, length = trackList->length(); i < length; ++i)
3908 trackList->item(i)->setMode(TextTrack::disabledKeyword());
3911 CaptionUserPreferences* captionPreferences = document().page() ? document().page()->group().captionPreferences() : 0;
3912 if (!captionPreferences)
3915 CaptionUserPreferences::CaptionDisplayMode displayMode;
3916 if (trackToSelect == TextTrack::captionMenuOffItem())
3917 displayMode = CaptionUserPreferences::ForcedOnly;
3918 else if (trackToSelect == TextTrack::captionMenuAutomaticItem())
3919 displayMode = CaptionUserPreferences::Automatic;
3921 displayMode = CaptionUserPreferences::AlwaysOn;
3922 if (trackToSelect->language().length())
3923 captionPreferences->setPreferredLanguage(trackToSelect->language());
3926 captionPreferences->setCaptionDisplayMode(displayMode);
3929 void HTMLMediaElement::configureTextTracks()
3931 TrackGroup captionAndSubtitleTracks(TrackGroup::CaptionsAndSubtitles);
3932 TrackGroup descriptionTracks(TrackGroup::Description);
3933 TrackGroup chapterTracks(TrackGroup::Chapter);
3934 TrackGroup metadataTracks(TrackGroup::Metadata);
3935 TrackGroup otherTracks(TrackGroup::Other);
3940 for (size_t i = 0; i < m_textTracks->length(); ++i) {
3941 RefPtr<TextTrack> textTrack = m_textTracks->item(i);
3945 String kind = textTrack->kind();
3946 TrackGroup* currentGroup;
3947 if (kind == TextTrack::subtitlesKeyword() || kind == TextTrack::captionsKeyword() || kind == TextTrack::forcedKeyword())
3948 currentGroup = &captionAndSubtitleTracks;
3949 else if (kind == TextTrack::descriptionsKeyword())
3950 currentGroup = &descriptionTracks;
3951 else if (kind == TextTrack::chaptersKeyword())
3952 currentGroup = &chapterTracks;
3953 else if (kind == TextTrack::metadataKeyword())
3954 currentGroup = &metadataTracks;
3956 currentGroup = &otherTracks;
3958 if (!currentGroup->visibleTrack && textTrack->mode() == TextTrack::showingKeyword())
3959 currentGroup->visibleTrack = textTrack;
3960 if (!currentGroup->defaultTrack && textTrack->isDefault())
3961 currentGroup->defaultTrack = textTrack;
3963 // Do not add this track to the group if it has already been automatically configured
3964 // as we only want to call configureTextTrack once per track so that adding another
3965 // track after the initial configuration doesn't reconfigure every track - only those
3966 // that should be changed by the new addition. For example all metadata tracks are
3967 // disabled by default, and we don't want a track that has been enabled by script
3968 // to be disabled automatically when a new metadata track is added later.
3969 if (textTrack->hasBeenConfigured())
3972 if (textTrack->language().length())
3973 currentGroup->hasSrcLang = true;
3974 currentGroup->tracks.append(textTrack);
3977 if (captionAndSubtitleTracks.tracks.size())
3978 configureTextTrackGroup(captionAndSubtitleTracks);
3979 if (descriptionTracks.tracks.size())
3980 configureTextTrackGroup(descriptionTracks);
3981 if (chapterTracks.tracks.size())
3982 configureTextTrackGroup(chapterTracks);
3983 if (metadataTracks.tracks.size())
3984 configureTextTrackGroup(metadataTracks);
3985 if (otherTracks.tracks.size())
3986 configureTextTrackGroup(otherTracks);
3988 configureTextTrackDisplay();
3989 if (hasMediaControls())
3990 mediaControls()->closedCaptionTracksChanged();
3994 bool HTMLMediaElement::havePotentialSourceChild()
3996 // Stash the current <source> node and next nodes so we can restore them after checking
3997 // to see there is another potential.
3998 RefPtr<HTMLSourceElement> currentSourceNode = m_currentSourceNode;
3999 RefPtr<Node> nextNode = m_nextChildNodeToConsider;
4001 URL nextURL = selectNextSourceChild(0, 0, DoNothing);
4003 m_currentSourceNode = currentSourceNode;
4004 m_nextChildNodeToConsider = nextNode;
4006 return nextURL.isValid();
4009 URL HTMLMediaElement::selectNextSourceChild(ContentType* contentType, String* keySystem, InvalidURLAction actionIfInvalid)
4012 // Don't log if this was just called to find out if there are any valid <source> elements.
4013 bool shouldLog = actionIfInvalid != DoNothing;
4015 LOG(Media, "HTMLMediaElement::selectNextSourceChild(%p)", this);
4018 if (!m_nextChildNodeToConsider) {
4021 LOG(Media, "HTMLMediaElement::selectNextSourceChild(%p) - end of list, stopping", this);
4027 HTMLSourceElement* source = nullptr;
4030 bool lookingForStartNode = m_nextChildNodeToConsider;
4031 bool canUseSourceElement = false;
4032 bool okToLoadSourceURL;
4034 NodeVector potentialSourceNodes;
4035 getChildNodes(*this, potentialSourceNodes);
4037 for (unsigned i = 0; !canUseSourceElement && i < potentialSourceNodes.size(); ++i) {
4038 Node& node = potentialSourceNodes[i].get();
4039 if (lookingForStartNode && m_nextChildNodeToConsider != &node)
4041 lookingForStartNode = false;
4043 if (!node.hasTagName(sourceTag))
4045 if (node.parentNode() != this)
4048 source = downcast<HTMLSourceElement>(&node);
4050 // 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
4051 mediaURL = source->getNonEmptyURLAttribute(srcAttr);
4054 LOG(Media, "HTMLMediaElement::selectNextSourceChild(%p) - 'src' is %s", this, urlForLoggingMedia(mediaURL).utf8().data());
4056 if (mediaURL.isEmpty())
4059 if (source->fastHasAttribute(mediaAttr)) {
4060 MediaQueryEvaluator screenEval("screen", document().frame(), renderer() ? &renderer()->style() : nullptr);
4061 RefPtr<MediaQuerySet> media = MediaQuerySet::createAllowingDescriptionSyntax(source->media());
4064 LOG(Media, "HTMLMediaElement::selectNextSourceChild(%p) - 'media' is %s", this, source->media().utf8().data());
4066 if (!screenEval.eval(media.get()))
4070 type = source->type();
4071 // FIXME(82965): Add support for keySystem in <source> and set system from source.
4072 if (type.isEmpty() && mediaURL.protocolIsData())
4073 type = mimeTypeFromDataURL(mediaURL);
4074 if (!type.isEmpty() || !system.isEmpty()) {
4077 LOG(Media, "HTMLMediaElement::selectNextSourceChild(%p) - 'type' is '%s' - key system is '%s'", this, type.utf8().data(), system.utf8().data());
4079 MediaEngineSupportParameters parameters;
4080 ContentType contentType(type);
4081 parameters.type = contentType.type().lower();
4082 parameters.codecs = contentType.parameter(ASCIILiteral("codecs"));
4083 parameters.url = mediaURL;
4084 #if ENABLE(ENCRYPTED_MEDIA)
4085 parameters.keySystem = system;
4087 #if ENABLE(MEDIA_SOURCE)
4088 parameters.isMediaSource = mediaURL.protocolIs(mediaSourceBlobProtocol);
4090 #if ENABLE(MEDIA_STREAM)
4091 parameters.isMediaStream = mediaURL.protocolIs(mediaStreamBlobProtocol);
4093 if (!MediaPlayer::supportsType(parameters, this))
4097 // Is it safe to load this url?
4098 okToLoadSourceURL = isSafeToLoadURL(mediaURL, actionIfInvalid) && dispatchBeforeLoadEvent(mediaURL.string());
4100 // A 'beforeload' event handler can mutate the DOM, so check to see if the source element is still a child node.