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"
45 #include "DocumentLoader.h"
46 #include "ElementIterator.h"
47 #include "EventNames.h"
48 #include "ExceptionCodePlaceholder.h"
49 #include "FrameLoader.h"
50 #include "FrameLoaderClient.h"
51 #include "FrameView.h"
52 #include "HTMLSourceElement.h"
53 #include "HTMLVideoElement.h"
54 #include "JSHTMLMediaElement.h"
57 #include "MIMETypeRegistry.h"
58 #include "MainFrame.h"
59 #include "MediaController.h"
60 #include "MediaControls.h"
61 #include "MediaDocument.h"
62 #include "MediaError.h"
63 #include "MediaFragmentURIParser.h"
64 #include "MediaKeyEvent.h"
65 #include "MediaList.h"
66 #include "MediaPlayer.h"
67 #include "MediaQueryEvaluator.h"
68 #include "MediaResourceLoader.h"
69 #include "MemoryPressureHandler.h"
70 #include "NetworkingContext.h"
71 #include "NoEventDispatchAssertion.h"
72 #include "PageGroup.h"
73 #include "PageThrottler.h"
74 #include "PlatformMediaSessionManager.h"
75 #include "ProgressTracker.h"
76 #include "RenderLayerCompositor.h"
77 #include "RenderVideo.h"
78 #include "RenderView.h"
79 #include "ResourceLoadInfo.h"
80 #include "ScriptController.h"
81 #include "ScriptSourceCode.h"
82 #include "SecurityPolicy.h"
83 #include "SessionID.h"
85 #include "ShadowRoot.h"
86 #include "TimeRanges.h"
87 #include "UserContentController.h"
88 #include "UserGestureIndicator.h"
90 #include <runtime/Uint8Array.h>
91 #include <wtf/CurrentTime.h>
92 #include <wtf/MathExtras.h>
94 #include <wtf/text/CString.h>
96 #if ENABLE(VIDEO_TRACK)
97 #include "AudioTrackList.h"
98 #include "HTMLTrackElement.h"
99 #include "InbandGenericTextTrack.h"
100 #include "InbandTextTrackPrivate.h"
101 #include "InbandWebVTTTextTrack.h"
102 #include "RuntimeEnabledFeatures.h"
103 #include "TextTrackCueList.h"
104 #include "TextTrackList.h"
105 #include "VideoTrackList.h"
108 #if ENABLE(WEB_AUDIO)
109 #include "AudioSourceProvider.h"
110 #include "MediaElementAudioSourceNode.h"
113 #if PLATFORM(IOS) || (PLATFORM(MAC) && ENABLE(VIDEO_PRESENTATION_MODE))
114 #include "WebVideoFullscreenInterface.h"
118 #include "RuntimeApplicationChecks.h"
119 #include "WebVideoFullscreenInterfaceAVKit.h"
122 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
123 #include "WebKitPlaybackTargetAvailabilityEvent.h"
126 #if ENABLE(MEDIA_SESSION)
127 #include "MediaSession.h"
130 #if ENABLE(MEDIA_SOURCE)
131 #include "DOMWindow.h"
132 #include "MediaSource.h"
133 #include "Performance.h"
134 #include "VideoPlaybackQuality.h"
137 #if ENABLE(MEDIA_STREAM)
139 #include "MediaStream.h"
140 #include "MediaStreamRegistry.h"
143 #if ENABLE(ENCRYPTED_MEDIA_V2)
144 #include "MediaKeyNeededEvent.h"
145 #include "MediaKeys.h"
148 #if ENABLE(MEDIA_CONTROLS_SCRIPT)
149 #include "JSMediaControlsHost.h"
150 #include "MediaControlsHost.h"
151 #include <bindings/ScriptObject.h>
156 static const double SeekRepeatDelay = 0.1;
157 static const double SeekTime = 0.2;
158 static const double ScanRepeatDelay = 1.5;
159 static const double ScanMaximumRate = 8;
161 static void setFlags(unsigned& value, unsigned flags)
166 static void clearFlags(unsigned& value, unsigned flags)
172 static String urlForLoggingMedia(const URL& url)
174 static const unsigned maximumURLLengthForLogging = 128;
176 if (url.string().length() < maximumURLLengthForLogging)
178 return url.string().substring(0, maximumURLLengthForLogging) + "...";
181 static const char* boolString(bool val)
183 return val ? "true" : "false";
186 static String actionName(HTMLMediaElementEnums::DelayedActionType action)
188 StringBuilder actionBuilder;
190 #define ACTION(_actionType) \
191 if (action & (HTMLMediaElementEnums::_actionType)) { \
192 if (!actionBuilder.isEmpty()) \
193 actionBuilder.append(", "); \
194 actionBuilder.append(#_actionType); \
197 ACTION(LoadMediaResource);
198 ACTION(ConfigureTextTracks);
199 ACTION(TextTrackChangesNotification);
200 ACTION(ConfigureTextTrackDisplay);
201 ACTION(CheckPlaybackTargetCompatablity);
202 ACTION(CheckMediaState);
203 ACTION(MediaEngineUpdated);
205 return actionBuilder.toString();
212 #ifndef LOG_MEDIA_EVENTS
213 // Default to not logging events because so many are generated they can overwhelm the rest of
215 #define LOG_MEDIA_EVENTS 0
218 #ifndef LOG_CACHED_TIME_WARNINGS
219 // Default to not logging warnings about excessive drift in the cached media time because it adds a
220 // fair amount of overhead and logging.
221 #define LOG_CACHED_TIME_WARNINGS 0
224 #if ENABLE(MEDIA_SOURCE)
225 // URL protocol used to signal that the media source API is being used.
226 static const char* mediaSourceBlobProtocol = "blob";
229 #if ENABLE(MEDIA_STREAM)
230 // URL protocol used to signal that the media stream API is being used.
231 static const char* mediaStreamBlobProtocol = "blob";
234 using namespace HTMLNames;
236 typedef HashMap<Document*, HashSet<HTMLMediaElement*>> DocumentElementSetMap;
237 static DocumentElementSetMap& documentToElementSetMap()
239 static NeverDestroyed<DocumentElementSetMap> map;
243 static void addElementToDocumentMap(HTMLMediaElement& element, Document& document)
245 DocumentElementSetMap& map = documentToElementSetMap();
246 HashSet<HTMLMediaElement*> set = map.take(&document);
248 map.add(&document, set);
251 static void removeElementFromDocumentMap(HTMLMediaElement& element, Document& document)
253 DocumentElementSetMap& map = documentToElementSetMap();
254 HashSet<HTMLMediaElement*> set = map.take(&document);
255 set.remove(&element);
257 map.add(&document, set);
260 #if ENABLE(ENCRYPTED_MEDIA)
261 static ExceptionCode exceptionCodeForMediaKeyException(MediaPlayer::MediaKeyException exception)
264 case MediaPlayer::NoError:
266 case MediaPlayer::InvalidPlayerState:
267 return INVALID_STATE_ERR;
268 case MediaPlayer::KeySystemNotSupported:
269 return NOT_SUPPORTED_ERR;
272 ASSERT_NOT_REACHED();
273 return INVALID_STATE_ERR;
277 #if ENABLE(VIDEO_TRACK)
278 class TrackDisplayUpdateScope {
280 TrackDisplayUpdateScope(HTMLMediaElement* mediaElement)
282 m_mediaElement = mediaElement;
283 m_mediaElement->beginIgnoringTrackDisplayUpdateRequests();
285 ~TrackDisplayUpdateScope()
287 ASSERT(m_mediaElement);
288 m_mediaElement->endIgnoringTrackDisplayUpdateRequests();
292 HTMLMediaElement* m_mediaElement;
296 struct HTMLMediaElement::TrackGroup {
297 enum GroupKind { CaptionsAndSubtitles, Description, Chapter, Metadata, Other };
299 TrackGroup(GroupKind kind)
307 Vector<RefPtr<TextTrack>> tracks;
308 RefPtr<TextTrack> visibleTrack;
309 RefPtr<TextTrack> defaultTrack;
314 HashSet<HTMLMediaElement*>& HTMLMediaElement::allMediaElements()
316 static NeverDestroyed<HashSet<HTMLMediaElement*>> elements;
320 #if ENABLE(MEDIA_SESSION)
321 typedef HashMap<uint64_t, HTMLMediaElement*> IDToElementMap;
323 static IDToElementMap& elementIDsToElements()
325 static NeverDestroyed<IDToElementMap> map;
329 HTMLMediaElement* HTMLMediaElement::elementWithID(uint64_t id)
331 if (id == HTMLMediaElementInvalidID)
334 return elementIDsToElements().get(id);
337 static uint64_t nextElementID()
339 static uint64_t elementID = 0;
344 HTMLMediaElement::HTMLMediaElement(const QualifiedName& tagName, Document& document, bool createdByParser)
345 : HTMLElement(tagName, document)
346 , ActiveDOMObject(&document)
347 , m_pendingActionTimer(*this, &HTMLMediaElement::pendingActionTimerFired)
348 , m_progressEventTimer(*this, &HTMLMediaElement::progressEventTimerFired)
349 , m_playbackProgressTimer(*this, &HTMLMediaElement::playbackProgressTimerFired)
350 , m_scanTimer(*this, &HTMLMediaElement::scanTimerFired)
351 , m_seekTaskQueue(document)
352 , m_resizeTaskQueue(document)
353 , m_shadowDOMTaskQueue(document)
354 , m_playedTimeRanges()
355 , m_asyncEventQueue(*this)
356 , m_requestedPlaybackRate(1)
357 , m_reportedPlaybackRate(1)
358 , m_defaultPlaybackRate(1)
359 , m_webkitPreservesPitch(true)
360 , m_networkState(NETWORK_EMPTY)
361 , m_readyState(HAVE_NOTHING)
362 , m_readyStateMaximum(HAVE_NOTHING)
364 , m_volumeInitialized(false)
365 , m_previousProgressTime(std::numeric_limits<double>::max())
366 , m_clockTimeAtLastUpdateEvent(0)
367 , m_lastTimeUpdateEventMovieTime(MediaTime::positiveInfiniteTime())
368 , m_loadState(WaitingForSource)
369 , m_videoFullscreenMode(VideoFullscreenModeNone)
370 #if PLATFORM(IOS) || (PLATFORM(MAC) && ENABLE(VIDEO_PRESENTATION_MODE))
371 , m_videoFullscreenGravity(MediaPlayer::VideoGravityResizeAspect)
373 , m_preload(MediaPlayer::Auto)
374 , m_displayMode(Unknown)
375 , m_processingMediaPlayerCallback(0)
376 #if ENABLE(MEDIA_SOURCE)
377 , m_droppedVideoFrames(0)
379 , m_clockTimeAtLastCachedTimeUpdate(0)
380 , m_minimumClockTimeToUpdateCachedTime(0)
381 , m_pendingActionFlags(0)
382 , m_actionAfterScan(Nothing)
384 , m_scanDirection(Forward)
385 , m_firstTimePlaying(true)
387 , m_isWaitingUntilMediaCanStart(false)
388 , m_shouldDelayLoadEvent(false)
389 , m_haveFiredLoadedData(false)
390 , m_inActiveDocument(true)
391 , m_autoplaying(true)
393 , m_explicitlyMuted(false)
394 , m_initiallyMuted(false)
397 , m_sentStalledEvent(false)
398 , m_sentEndEvent(false)
399 , m_pausedInternal(false)
400 , m_sendProgressEvents(true)
401 , m_closedCaptionsVisible(false)
402 , m_webkitLegacyClosedCaptionOverride(false)
403 , m_completelyLoaded(false)
404 , m_havePreparedToPlay(false)
405 , m_parsingInProgress(createdByParser)
406 , m_elementIsHidden(document.hidden())
407 , m_creatingControls(false)
408 #if ENABLE(MEDIA_CONTROLS_SCRIPT)
409 , m_mediaControlsDependOnPageScaleFactor(false)
410 , m_haveSetUpCaptionContainer(false)
412 #if ENABLE(VIDEO_TRACK)
413 , m_tracksAreReady(true)
414 , m_haveVisibleTextTrack(false)
415 , m_processingPreferenceChange(false)
416 , m_lastTextTrackUpdateTime(MediaTime(-1, 1))
417 , m_captionDisplayMode(CaptionUserPreferences::Automatic)
421 , m_ignoreTrackDisplayUpdate(0)
423 #if ENABLE(WEB_AUDIO)
424 , m_audioSourceNode(0)
426 , m_mediaSession(std::make_unique<MediaElementSession>(*this))
427 , m_reportedExtraMemoryCost(0)
428 #if ENABLE(MEDIA_STREAM)
429 , m_mediaStreamSrcObject(nullptr)
432 allMediaElements().add(this);
434 LOG(Media, "HTMLMediaElement::HTMLMediaElement(%p)", this);
435 setHasCustomStyleResolveCallbacks();
437 m_mediaSession->addBehaviorRestriction(MediaElementSession::RequireUserGestureForFullscreen);
438 m_mediaSession->addBehaviorRestriction(MediaElementSession::RequirePageConsentToLoadMedia);
439 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
440 m_mediaSession->addBehaviorRestriction(MediaElementSession::RequireUserGestureToAutoplayToExternalDevice);
443 Settings* settings = document.settings();
445 m_sendProgressEvents = false;
447 if (!settings || settings->videoPlaybackRequiresUserGesture()) {
448 // Allow autoplay in a MediaDocument that is not in an iframe.
449 if (document.ownerElement() || !document.isMediaDocument()) {
450 m_mediaSession->addBehaviorRestriction(MediaElementSession::RequireUserGestureForVideoRateChange);
451 m_mediaSession->addBehaviorRestriction(MediaElementSession::RequireUserGestureForLoad);
453 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
454 m_mediaSession->addBehaviorRestriction(MediaElementSession::RequireUserGestureToShowPlaybackTargetPicker);
459 // Relax RequireUserGestureForFullscreen when videoPlaybackRequiresUserGesture is not set:
460 m_mediaSession->removeBehaviorRestriction(MediaElementSession::RequireUserGestureForFullscreen);
463 if (settings && settings->invisibleAutoplayNotPermitted())
464 m_mediaSession->addBehaviorRestriction(MediaElementSession::InvisibleAutoplayNotPermitted);
466 if (settings && settings->audioPlaybackRequiresUserGesture())
467 m_mediaSession->addBehaviorRestriction(MediaElementSession::RequireUserGestureForAudioRateChange);
469 if (!settings || !settings->mediaDataLoadsAutomatically())
470 m_mediaSession->addBehaviorRestriction(MediaElementSession::AutoPreloadingNotPermitted);
472 #if ENABLE(VIDEO_TRACK)
474 m_captionDisplayMode = document.page()->group().captionPreferences().captionDisplayMode();
477 if (settings && settings->mainContentUserGestureOverrideEnabled())
478 m_mediaSession->addBehaviorRestriction(MediaElementSession::OverrideUserGestureRequirementForMainContent);
480 #if ENABLE(MEDIA_SESSION)
481 m_elementID = nextElementID();
482 elementIDsToElements().add(m_elementID, this);
484 setSessionInternal(document.defaultMediaSession());
487 registerWithDocument(document);
490 HTMLMediaElement::~HTMLMediaElement()
492 LOG(Media, "HTMLMediaElement::~HTMLMediaElement(%p)", this);
494 allMediaElements().remove(this);
496 m_asyncEventQueue.close();
498 setShouldDelayLoadEvent(false);
499 unregisterWithDocument(document());
501 if (document().page())
502 document().page()->chrome().client().clearPlaybackControlsManager(*this);
504 #if ENABLE(VIDEO_TRACK)
506 m_audioTracks->clearElement();
507 for (unsigned i = 0; i < m_audioTracks->length(); ++i)
508 m_audioTracks->item(i)->clearClient();
511 m_textTracks->clearElement();
513 for (unsigned i = 0; i < m_textTracks->length(); ++i)
514 m_textTracks->item(i)->clearClient();
517 m_videoTracks->clearElement();
518 for (unsigned i = 0; i < m_videoTracks->length(); ++i)
519 m_videoTracks->item(i)->clearClient();
523 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
524 if (hasEventListeners(eventNames().webkitplaybacktargetavailabilitychangedEvent)) {
525 m_hasPlaybackTargetAvailabilityListeners = false;
526 m_mediaSession->setHasPlaybackTargetAvailabilityListeners(*this, false);
531 if (m_mediaController) {
532 m_mediaController->removeMediaElement(this);
533 m_mediaController = nullptr;
536 #if ENABLE(MEDIA_SOURCE)
540 #if ENABLE(ENCRYPTED_MEDIA_V2)
544 #if ENABLE(MEDIA_CONTROLS_SCRIPT)
546 m_isolatedWorld->clearWrappers();
549 #if ENABLE(MEDIA_SESSION)
551 m_session->removeMediaElement(*this);
555 elementIDsToElements().remove(m_elementID);
558 m_seekTaskQueue.close();
560 m_completelyLoaded = true;
563 void HTMLMediaElement::registerWithDocument(Document& document)
565 m_mediaSession->registerWithDocument(document);
567 if (m_isWaitingUntilMediaCanStart)
568 document.addMediaCanStartListener(this);
571 document.registerForMediaVolumeCallbacks(this);
572 document.registerForPrivateBrowsingStateChangedCallbacks(this);
575 document.registerForVisibilityStateChangedCallbacks(this);
577 #if ENABLE(VIDEO_TRACK)
578 if (m_requireCaptionPreferencesChangedCallbacks)
579 document.registerForCaptionPreferencesChangedCallbacks(this);
582 #if ENABLE(MEDIA_CONTROLS_SCRIPT)
583 if (m_mediaControlsDependOnPageScaleFactor)
584 document.registerForPageScaleFactorChangedCallbacks(this);
587 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
588 document.registerForDocumentSuspensionCallbacks(this);
591 document.registerForAllowsMediaDocumentInlinePlaybackChangedCallbacks(*this);
593 document.addAudioProducer(this);
594 addElementToDocumentMap(*this, document);
597 void HTMLMediaElement::unregisterWithDocument(Document& document)
599 m_mediaSession->unregisterWithDocument(document);
601 if (m_isWaitingUntilMediaCanStart)
602 document.removeMediaCanStartListener(this);
605 document.unregisterForMediaVolumeCallbacks(this);
606 document.unregisterForPrivateBrowsingStateChangedCallbacks(this);
609 document.unregisterForVisibilityStateChangedCallbacks(this);
611 #if ENABLE(VIDEO_TRACK)
612 if (m_requireCaptionPreferencesChangedCallbacks)
613 document.unregisterForCaptionPreferencesChangedCallbacks(this);
616 #if ENABLE(MEDIA_CONTROLS_SCRIPT)
617 if (m_mediaControlsDependOnPageScaleFactor)
618 document.unregisterForPageScaleFactorChangedCallbacks(this);
621 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
622 document.unregisterForDocumentSuspensionCallbacks(this);
625 document.unregisterForAllowsMediaDocumentInlinePlaybackChangedCallbacks(*this);
627 document.removeAudioProducer(this);
628 removeElementFromDocumentMap(*this, document);
631 void HTMLMediaElement::didMoveToNewDocument(Document* oldDocument)
633 if (m_shouldDelayLoadEvent) {
635 oldDocument->decrementLoadEventDelayCount();
636 document().incrementLoadEventDelayCount();
640 unregisterWithDocument(*oldDocument);
642 registerWithDocument(document());
644 HTMLElement::didMoveToNewDocument(oldDocument);
645 updateShouldAutoplay();
648 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
649 void HTMLMediaElement::prepareForDocumentSuspension()
651 m_mediaSession->unregisterWithDocument(document());
654 void HTMLMediaElement::resumeFromDocumentSuspension()
656 m_mediaSession->registerWithDocument(document());
657 updateShouldAutoplay();
661 bool HTMLMediaElement::hasCustomFocusLogic() const
666 bool HTMLMediaElement::supportsFocus() const
668 if (document().isMediaDocument())
671 // If no controls specified, we should still be able to focus the element if it has tabIndex.
672 return controls() || HTMLElement::supportsFocus();
675 bool HTMLMediaElement::isMouseFocusable() const
680 void HTMLMediaElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
682 if (name == srcAttr) {
684 // Note, unless the restriction on requiring user action has been removed,
685 // do not begin downloading data on iOS.
686 if (!value.isNull() && m_mediaSession->dataLoadingPermitted(*this))
688 // Trigger a reload, as long as the 'src' attribute is present.
692 clearMediaPlayer(LoadMediaResource);
693 scheduleDelayedAction(LoadMediaResource);
695 } else if (name == controlsAttr)
696 configureMediaControls();
697 else if (name == loopAttr)
698 updateSleepDisabling();
699 else if (name == preloadAttr) {
700 if (equalLettersIgnoringASCIICase(value, "none"))
701 m_preload = MediaPlayer::None;
702 else if (equalLettersIgnoringASCIICase(value, "metadata"))
703 m_preload = MediaPlayer::MetaData;
705 // The spec does not define an "invalid value default" but "auto" is suggested as the
706 // "missing value default", so use it for everything except "none" and "metadata"
707 m_preload = MediaPlayer::Auto;
710 // The attribute must be ignored if the autoplay attribute is present
711 if (!autoplay() && m_player)
712 m_player->setPreload(m_mediaSession->effectivePreloadForElement(*this));
714 } else if (name == mediagroupAttr)
715 setMediaGroup(value);
717 HTMLElement::parseAttribute(name, value);
720 void HTMLMediaElement::finishParsingChildren()
722 HTMLElement::finishParsingChildren();
723 m_parsingInProgress = false;
725 #if ENABLE(VIDEO_TRACK)
726 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
729 if (descendantsOfType<HTMLTrackElement>(*this).first())
730 scheduleDelayedAction(ConfigureTextTracks);
734 bool HTMLMediaElement::rendererIsNeeded(const RenderStyle& style)
736 return controls() && HTMLElement::rendererIsNeeded(style);
739 RenderPtr<RenderElement> HTMLMediaElement::createElementRenderer(RenderStyle&& style, const RenderTreePosition&)
741 return createRenderer<RenderMedia>(*this, WTFMove(style));
744 bool HTMLMediaElement::childShouldCreateRenderer(const Node& child) const
746 #if ENABLE(MEDIA_CONTROLS_SCRIPT)
747 return hasShadowRootParent(child) && HTMLElement::childShouldCreateRenderer(child);
749 if (!hasMediaControls())
751 // <media> doesn't allow its content, including shadow subtree, to
752 // be rendered. So this should return false for most of the children.
753 // One exception is a shadow tree built for rendering controls which should be visible.
754 // So we let them go here by comparing its subtree root with one of the controls.
755 return &mediaControls()->treeScope() == &child.treeScope()
756 && hasShadowRootParent(child)
757 && HTMLElement::childShouldCreateRenderer(child);
761 Node::InsertionNotificationRequest HTMLMediaElement::insertedInto(ContainerNode& insertionPoint)
763 LOG(Media, "HTMLMediaElement::insertedInto(%p)", this);
765 HTMLElement::insertedInto(insertionPoint);
766 if (insertionPoint.inDocument()) {
767 m_inActiveDocument = true;
770 if (m_networkState == NETWORK_EMPTY && !fastGetAttribute(srcAttr).isEmpty() && m_mediaSession->dataLoadingPermitted(*this))
772 if (m_networkState == NETWORK_EMPTY && !fastGetAttribute(srcAttr).isEmpty())
774 scheduleDelayedAction(LoadMediaResource);
777 if (!m_explicitlyMuted) {
778 m_explicitlyMuted = true;
779 m_muted = fastHasAttribute(mutedAttr);
782 configureMediaControls();
783 return InsertionDone;
786 void HTMLMediaElement::removedFrom(ContainerNode& insertionPoint)
788 LOG(Media, "HTMLMediaElement::removedFrom(%p)", this);
790 m_inActiveDocument = false;
791 if (insertionPoint.inDocument()) {
792 if (hasMediaControls())
793 mediaControls()->hide();
794 if (m_networkState > NETWORK_EMPTY)
796 if (m_videoFullscreenMode != VideoFullscreenModeNone)
800 size_t extraMemoryCost = m_player->extraMemoryCost();
801 if (extraMemoryCost > m_reportedExtraMemoryCost) {
802 JSC::VM& vm = JSDOMWindowBase::commonVM();
803 JSC::JSLockHolder lock(vm);
805 size_t extraMemoryCostDelta = extraMemoryCost - m_reportedExtraMemoryCost;
806 m_reportedExtraMemoryCost = extraMemoryCost;
807 // FIXME: Adopt reportExtraMemoryVisited, and switch to reportExtraMemoryAllocated.
808 // https://bugs.webkit.org/show_bug.cgi?id=142595
809 vm.heap.deprecatedReportExtraMemory(extraMemoryCostDelta);
814 HTMLElement::removedFrom(insertionPoint);
817 void HTMLMediaElement::willAttachRenderers()
822 void HTMLMediaElement::didAttachRenderers()
824 if (RenderElement* renderer = this->renderer()) {
825 renderer->updateFromElement();
826 if (m_mediaSession->hasBehaviorRestriction(MediaElementSession::InvisibleAutoplayNotPermitted)
827 || m_mediaSession->hasBehaviorRestriction(MediaElementSession::OverrideUserGestureRequirementForMainContent))
828 renderer->registerForVisibleInViewportCallback();
830 updateShouldAutoplay();
833 void HTMLMediaElement::willDetachRenderers()
836 renderer()->unregisterForVisibleInViewportCallback();
839 void HTMLMediaElement::didDetachRenderers()
841 updateShouldAutoplay();
844 void HTMLMediaElement::didRecalcStyle(Style::Change)
847 renderer()->updateFromElement();
850 void HTMLMediaElement::scheduleDelayedAction(DelayedActionType actionType)
852 LOG(Media, "HTMLMediaElement::scheduleDelayedAction(%p) - setting %s flag", this, actionName(actionType).utf8().data());
854 if ((actionType & LoadMediaResource) && !(m_pendingActionFlags & LoadMediaResource)) {
856 setFlags(m_pendingActionFlags, LoadMediaResource);
859 #if ENABLE(VIDEO_TRACK)
860 if (RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled() && (actionType & ConfigureTextTracks))
861 setFlags(m_pendingActionFlags, ConfigureTextTracks);
864 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
865 if (actionType & CheckPlaybackTargetCompatablity)
866 setFlags(m_pendingActionFlags, CheckPlaybackTargetCompatablity);
869 if (actionType & CheckMediaState)
870 setFlags(m_pendingActionFlags, CheckMediaState);
872 if (actionType & MediaEngineUpdated)
873 setFlags(m_pendingActionFlags, MediaEngineUpdated);
875 m_pendingActionTimer.startOneShot(0);
878 void HTMLMediaElement::scheduleNextSourceChild()
880 // Schedule the timer to try the next <source> element WITHOUT resetting state ala prepareForLoad.
881 LOG(Media, "HTMLMediaElement::scheduleNextSourceChild(%p) - setting %s flag", this, actionName(LoadMediaResource).utf8().data());
882 setFlags(m_pendingActionFlags, LoadMediaResource);
883 m_pendingActionTimer.startOneShot(0);
886 void HTMLMediaElement::scheduleEvent(const AtomicString& eventName)
889 LOG(Media, "HTMLMediaElement::scheduleEvent(%p) - scheduling '%s'", this, eventName.string().ascii().data());
891 RefPtr<Event> event = Event::create(eventName, false, true);
893 // Don't set the event target, the event queue will set it in GenericEventQueue::timerFired and setting it here
894 // will trigger an ASSERT if this element has been marked for deletion.
896 m_asyncEventQueue.enqueueEvent(event.release());
899 void HTMLMediaElement::pendingActionTimerFired()
901 Ref<HTMLMediaElement> protect(*this); // loadNextSourceChild may fire 'beforeload', which can make arbitrary DOM mutations.
902 PendingActionFlags pendingActions = m_pendingActionFlags;
903 m_pendingActionFlags = 0;
905 #if ENABLE(VIDEO_TRACK)
906 if (RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled() && (pendingActions & ConfigureTextTracks))
907 configureTextTracks();
910 if (pendingActions & LoadMediaResource) {
911 if (m_loadState == LoadingFromSourceElement)
912 loadNextSourceChild();
917 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
918 if (pendingActions & CheckPlaybackTargetCompatablity && m_isPlayingToWirelessTarget && !m_player->canPlayToWirelessPlaybackTarget()) {
919 LOG(Media, "HTMLMediaElement::pendingActionTimerFired(%p) - calling setShouldPlayToPlaybackTarget(false)", this);
920 m_failedToPlayToWirelessTarget = true;
921 m_player->setShouldPlayToPlaybackTarget(false);
924 if (pendingActions & CheckMediaState)
928 if (pendingActions & MediaEngineUpdated)
929 mediaEngineWasUpdated();
932 PassRefPtr<MediaError> HTMLMediaElement::error() const
937 void HTMLMediaElement::setSrc(const String& url)
939 setAttribute(srcAttr, url);
942 #if ENABLE(MEDIA_STREAM)
943 void HTMLMediaElement::setSrcObject(ScriptExecutionContext& context, MediaStream* mediaStream)
945 // FIXME: Setting the srcObject attribute may cause other changes to the media element's internal state:
946 // Specifically, if srcObject is specified, the UA must use it as the source of media, even if the src
947 // attribute is also set or children are present. If the value of srcObject is replaced or set to null
948 // the UA must re-run the media element load algorithm.
950 // https://bugs.webkit.org/show_bug.cgi?id=124896
952 m_mediaStreamSrcObject = mediaStream;
953 setSrc(DOMURL::createPublicURL(context, mediaStream));
957 HTMLMediaElement::NetworkState HTMLMediaElement::networkState() const
959 return m_networkState;
962 String HTMLMediaElement::canPlayType(const String& mimeType, const String& keySystem, const URL& url) const
964 MediaEngineSupportParameters parameters;
965 ContentType contentType(mimeType);
966 parameters.type = contentType.type().convertToASCIILowercase();
967 parameters.codecs = contentType.parameter(ASCIILiteral("codecs"));
968 parameters.url = url;
969 #if ENABLE(ENCRYPTED_MEDIA)
970 parameters.keySystem = keySystem;
972 UNUSED_PARAM(keySystem);
974 MediaPlayer::SupportsType support = MediaPlayer::supportsType(parameters, this);
980 case MediaPlayer::IsNotSupported:
981 canPlay = emptyString();
983 case MediaPlayer::MayBeSupported:
984 canPlay = ASCIILiteral("maybe");
986 case MediaPlayer::IsSupported:
987 canPlay = ASCIILiteral("probably");
991 LOG(Media, "HTMLMediaElement::canPlayType(%p) - [%s, %s, %s] -> %s", this, mimeType.utf8().data(), keySystem.utf8().data(), url.stringCenterEllipsizedToLength().utf8().data(), canPlay.utf8().data());
996 double HTMLMediaElement::getStartDate() const
998 return m_player->getStartDate().toDouble();
1001 void HTMLMediaElement::load()
1003 Ref<HTMLMediaElement> protect(*this); // loadInternal may result in a 'beforeload' event, which can make arbitrary DOM mutations.
1005 LOG(Media, "HTMLMediaElement::load(%p)", this);
1007 if (!m_mediaSession->dataLoadingPermitted(*this))
1009 if (ScriptController::processingUserGestureForMedia())
1010 removeBehaviorsRestrictionsAfterFirstUserGesture();
1017 void HTMLMediaElement::prepareForLoad()
1019 LOG(Media, "HTMLMediaElement::prepareForLoad(%p)", this);
1021 // Perform the cleanup required for the resource load algorithm to run.
1022 stopPeriodicTimers();
1023 m_pendingActionTimer.stop();
1024 // FIXME: Figure out appropriate place to reset LoadTextTrackResource if necessary and set m_pendingActionFlags to 0 here.
1025 m_pendingActionFlags &= ~LoadMediaResource;
1026 m_sentEndEvent = false;
1027 m_sentStalledEvent = false;
1028 m_haveFiredLoadedData = false;
1029 m_completelyLoaded = false;
1030 m_havePreparedToPlay = false;
1031 m_displayMode = Unknown;
1032 m_currentSrc = URL();
1034 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
1035 m_failedToPlayToWirelessTarget = false;
1038 // 1 - Abort any already-running instance of the resource selection algorithm for this element.
1039 m_loadState = WaitingForSource;
1040 m_currentSourceNode = nullptr;
1042 // 2 - If there are any tasks from the media element's media element event task source in
1043 // one of the task queues, then remove those tasks.
1044 cancelPendingEventsAndCallbacks();
1046 // 3 - If the media element's networkState is set to NETWORK_LOADING or NETWORK_IDLE, queue
1047 // a task to fire a simple event named abort at the media element.
1048 if (m_networkState == NETWORK_LOADING || m_networkState == NETWORK_IDLE)
1049 scheduleEvent(eventNames().abortEvent);
1051 #if ENABLE(MEDIA_SOURCE)
1055 createMediaPlayer();
1057 // 4 - If the media element's networkState is not set to NETWORK_EMPTY, then run these substeps
1058 if (m_networkState != NETWORK_EMPTY) {
1059 // 4.1 - Queue a task to fire a simple event named emptied at the media element.
1060 scheduleEvent(eventNames().emptiedEvent);
1062 // 4.2 - If a fetching process is in progress for the media element, the user agent should stop it.
1063 m_networkState = NETWORK_EMPTY;
1065 // 4.3 - Forget the media element's media-resource-specific tracks.
1066 forgetResourceSpecificTracks();
1068 // 4.4 - If readyState is not set to HAVE_NOTHING, then set it to that state.
1069 m_readyState = HAVE_NOTHING;
1070 m_readyStateMaximum = HAVE_NOTHING;
1072 // 4.5 - If the paused attribute is false, then set it to true.
1075 // 4.6 - If seeking is true, set it to false.
1078 // 4.7 - Set the current playback position to 0.
1079 // Set the official playback position to 0.
1080 // If this changed the official playback position, then queue a task to fire a simple event named timeupdate at the media element.
1081 // FIXME: Add support for firing this event. e.g., scheduleEvent(eventNames().timeUpdateEvent);
1083 // 4.8 - Set the initial playback position to 0.
1084 // FIXME: Make this less subtle. The position only becomes 0 because of the createMediaPlayer() call
1086 refreshCachedTime();
1088 invalidateCachedTime();
1090 // 4.9 - Set the timeline offset to Not-a-Number (NaN).
1091 // 4.10 - Update the duration attribute to Not-a-Number (NaN).
1093 updateMediaController();
1094 #if ENABLE(VIDEO_TRACK)
1095 if (RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
1096 updateActiveTextTrackCues(MediaTime::zeroTime());
1100 // 5 - Set the playbackRate attribute to the value of the defaultPlaybackRate attribute.
1101 setPlaybackRate(defaultPlaybackRate());
1103 // 6 - Set the error attribute to null and the autoplaying flag to true.
1105 m_autoplaying = true;
1106 mediaSession().clientWillBeginAutoplaying();
1108 // 7 - Invoke the media element's resource selection algorithm.
1110 // 8 - Note: Playback of any previously playing media resource for this element stops.
1112 // The resource selection algorithm
1113 // 1 - Set the networkState to NETWORK_NO_SOURCE
1114 m_networkState = NETWORK_NO_SOURCE;
1116 // 2 - Asynchronously await a stable state.
1118 m_playedTimeRanges = TimeRanges::create();
1120 // FIXME: Investigate whether these can be moved into m_networkState != NETWORK_EMPTY block above
1121 // so they are closer to the relevant spec steps.
1122 m_lastSeekTime = MediaTime::zeroTime();
1124 // The spec doesn't say to block the load event until we actually run the asynchronous section
1125 // algorithm, but do it now because we won't start that until after the timer fires and the
1126 // event may have already fired by then.
1127 MediaPlayer::Preload effectivePreload = m_mediaSession->effectivePreloadForElement(*this);
1128 if (effectivePreload != MediaPlayer::None)
1129 setShouldDelayLoadEvent(true);
1132 if (effectivePreload != MediaPlayer::None && m_mediaSession->allowsAutomaticMediaDataLoading(*this))
1136 configureMediaControls();
1139 void HTMLMediaElement::loadInternal()
1141 LOG(Media, "HTMLMediaElement::loadInternal(%p)", this);
1143 // Some of the code paths below this function dispatch the BeforeLoad event. This ASSERT helps
1144 // us catch those bugs more quickly without needing all the branches to align to actually
1145 // trigger the event.
1146 ASSERT(!NoEventDispatchAssertion::isEventDispatchForbidden());
1148 // If we can't start a load right away, start it later.
1149 if (!m_mediaSession->pageAllowsDataLoading(*this)) {
1150 LOG(Media, "HTMLMediaElement::loadInternal(%p) - not allowed to load in background, waiting", this);
1151 setShouldDelayLoadEvent(false);
1152 if (m_isWaitingUntilMediaCanStart)
1154 m_isWaitingUntilMediaCanStart = true;
1155 document().addMediaCanStartListener(this);
1159 clearFlags(m_pendingActionFlags, LoadMediaResource);
1161 // Once the page has allowed an element to load media, it is free to load at will. This allows a
1162 // playlist that starts in a foreground tab to continue automatically if the tab is subsequently
1163 // put into the background.
1164 m_mediaSession->removeBehaviorRestriction(MediaElementSession::RequirePageConsentToLoadMedia);
1166 #if ENABLE(VIDEO_TRACK)
1167 if (hasMediaControls())
1168 mediaControls()->changedClosedCaptionsVisibility();
1170 // HTMLMediaElement::textTracksAreReady will need "... the text tracks whose mode was not in the
1171 // disabled state when the element's resource selection algorithm last started".
1172 if (RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled()) {
1173 m_textTracksWhenResourceSelectionBegan.clear();
1175 for (unsigned i = 0; i < m_textTracks->length(); ++i) {
1176 TextTrack* track = m_textTracks->item(i);
1177 if (track->mode() != TextTrack::disabledKeyword())
1178 m_textTracksWhenResourceSelectionBegan.append(track);
1184 selectMediaResource();
1187 void HTMLMediaElement::selectMediaResource()
1189 LOG(Media, "HTMLMediaElement::selectMediaResource(%p)", this);
1195 enum Mode { attribute, children };
1197 // 3 - If the media element has a src attribute, then let mode be attribute.
1198 Mode mode = attribute;
1199 if (!fastHasAttribute(srcAttr)) {
1200 // Otherwise, if the media element does not have a src attribute but has a source
1201 // element child, then let mode be children and let candidate be the first such
1202 // source element child in tree order.
1203 if (auto firstSource = childrenOfType<HTMLSourceElement>(*this).first()) {
1205 m_nextChildNodeToConsider = firstSource;
1206 m_currentSourceNode = nullptr;
1208 // Otherwise the media element has neither a src attribute nor a source element
1209 // child: set the networkState to NETWORK_EMPTY, and abort these steps; the
1210 // synchronous section ends.
1211 m_loadState = WaitingForSource;
1212 setShouldDelayLoadEvent(false);
1213 m_networkState = NETWORK_EMPTY;
1215 LOG(Media, "HTMLMediaElement::selectMediaResource(%p) - nothing to load", this);
1220 // 4 - Set the media element's delaying-the-load-event flag to true (this delays the load event),
1221 // and set its networkState to NETWORK_LOADING.
1222 setShouldDelayLoadEvent(true);
1223 m_networkState = NETWORK_LOADING;
1225 // 5 - Queue a task to fire a simple event named loadstart at the media element.
1226 scheduleEvent(eventNames().loadstartEvent);
1228 // 6 - If mode is attribute, then run these substeps
1229 if (mode == attribute) {
1230 m_loadState = LoadingFromSrcAttr;
1232 // If the src attribute's value is the empty string ... jump down to the failed step below
1233 URL mediaURL = getNonEmptyURLAttribute(srcAttr);
1234 if (mediaURL.isEmpty()) {
1235 mediaLoadingFailed(MediaPlayer::FormatError);
1236 LOG(Media, "HTMLMediaElement::selectMediaResource(%p) - empty 'src'", this);
1240 if (!isSafeToLoadURL(mediaURL, Complain) || !dispatchBeforeLoadEvent(mediaURL.string())) {
1241 mediaLoadingFailed(MediaPlayer::FormatError);
1245 // No type or key system information is available when the url comes
1246 // from the 'src' attribute so MediaPlayer
1247 // will have to pick a media engine based on the file extension.
1248 ContentType contentType((String()));
1249 loadResource(mediaURL, contentType, String());
1250 LOG(Media, "HTMLMediaElement::selectMediaResource(%p) - using 'src' attribute url", this);
1254 // Otherwise, the source elements will be used
1255 loadNextSourceChild();
1258 void HTMLMediaElement::loadNextSourceChild()
1260 ContentType contentType((String()));
1262 URL mediaURL = selectNextSourceChild(&contentType, &keySystem, Complain);
1263 if (!mediaURL.isValid()) {
1264 waitForSourceChange();
1268 // Recreate the media player for the new url
1269 createMediaPlayer();
1271 m_loadState = LoadingFromSourceElement;
1272 loadResource(mediaURL, contentType, keySystem);
1275 void HTMLMediaElement::loadResource(const URL& initialURL, ContentType& contentType, const String& keySystem)
1277 ASSERT(isSafeToLoadURL(initialURL, Complain));
1279 LOG(Media, "HTMLMediaElement::loadResource(%p) - %s, %s, %s", this, urlForLoggingMedia(initialURL).utf8().data(), contentType.raw().utf8().data(), keySystem.utf8().data());
1281 Frame* frame = document().frame();
1283 mediaLoadingFailed(MediaPlayer::FormatError);
1287 Page* page = frame->page();
1289 mediaLoadingFailed(MediaPlayer::FormatError);
1293 URL url = initialURL;
1294 if (!frame->loader().willLoadMediaElementURL(url)) {
1295 mediaLoadingFailed(MediaPlayer::FormatError);
1299 #if ENABLE(CONTENT_EXTENSIONS)
1300 ResourceRequest request(url);
1301 DocumentLoader* documentLoader = frame->loader().documentLoader();
1303 if (documentLoader && page->userContentProvider().processContentExtensionRulesForLoad(request, ResourceType::Media, *documentLoader) == ContentExtensions::BlockedStatus::Blocked) {
1305 mediaLoadingFailed(MediaPlayer::FormatError);
1310 // The resource fetch algorithm
1311 m_networkState = NETWORK_LOADING;
1313 // If the url should be loaded from the application cache, pass the url of the cached file
1314 // to the media engine.
1315 ApplicationCacheHost* cacheHost = frame->loader().documentLoader()->applicationCacheHost();
1316 ApplicationCacheResource* resource = 0;
1317 if (cacheHost && cacheHost->shouldLoadResourceFromApplicationCache(ResourceRequest(url), resource)) {
1318 // Resources that are not present in the manifest will always fail to load (at least, after the
1319 // cache has been primed the first time), making the testing of offline applications simpler.
1320 if (!resource || resource->path().isEmpty()) {
1321 mediaLoadingFailed(MediaPlayer::NetworkError);
1326 // Log that we started loading a media element.
1327 page->diagnosticLoggingClient().logDiagnosticMessageWithValue(DiagnosticLoggingKeys::mediaKey(), isVideo() ? DiagnosticLoggingKeys::videoKey() : DiagnosticLoggingKeys::audioKey(), DiagnosticLoggingKeys::loadingKey(), ShouldSample::No);
1329 m_firstTimePlaying = true;
1331 // Set m_currentSrc *before* changing to the cache url, the fact that we are loading from the app
1332 // cache is an internal detail not exposed through the media element API.
1336 url = ApplicationCacheHost::createFileURL(resource->path());
1337 LOG(Media, "HTMLMediaElement::loadResource(%p) - will load from app cache -> %s", this, urlForLoggingMedia(url).utf8().data());
1340 LOG(Media, "HTMLMediaElement::loadResource(%p) - m_currentSrc -> %s", this, urlForLoggingMedia(m_currentSrc).utf8().data());
1342 if (m_sendProgressEvents)
1343 startProgressEventTimer();
1345 bool privateMode = document().page() && document().page()->usesEphemeralSession();
1346 m_player->setPrivateBrowsingMode(privateMode);
1348 // Reset display mode to force a recalculation of what to show because we are resetting the player.
1349 setDisplayMode(Unknown);
1352 m_player->setPreload(m_mediaSession->effectivePreloadForElement(*this));
1353 m_player->setPreservesPitch(m_webkitPreservesPitch);
1355 if (!m_explicitlyMuted) {
1356 m_explicitlyMuted = true;
1357 m_muted = fastHasAttribute(mutedAttr);
1362 bool loadAttempted = false;
1363 #if ENABLE(MEDIA_SOURCE)
1364 ASSERT(!m_mediaSource);
1366 if (url.protocolIs(mediaSourceBlobProtocol))
1367 m_mediaSource = MediaSource::lookup(url.string());
1369 if (m_mediaSource) {
1370 if (m_mediaSource->attachToElement(this))
1371 m_player->load(url, contentType, m_mediaSource.get());
1373 // Forget our reference to the MediaSource, so we leave it alone
1374 // while processing remainder of load failure.
1375 m_mediaSource = nullptr;
1376 mediaLoadingFailed(MediaPlayer::FormatError);
1378 loadAttempted = true;
1382 #if ENABLE(MEDIA_STREAM)
1383 if (!loadAttempted) {
1384 if (!m_mediaStreamSrcObject && url.protocolIs(mediaStreamBlobProtocol))
1385 m_mediaStreamSrcObject = MediaStreamRegistry::shared().lookUp(url);
1387 if (m_mediaStreamSrcObject) {
1388 loadAttempted = true;
1389 if (!m_player->load(m_mediaStreamSrcObject->privateStream()))
1390 mediaLoadingFailed(MediaPlayer::FormatError);
1395 if (!loadAttempted && !m_player->load(url, contentType, keySystem))
1396 mediaLoadingFailed(MediaPlayer::FormatError);
1398 // If there is no poster to display, allow the media engine to render video frames as soon as
1399 // they are available.
1400 updateDisplayState();
1403 renderer()->updateFromElement();
1406 #if ENABLE(VIDEO_TRACK)
1407 static bool trackIndexCompare(TextTrack* a,
1410 return a->trackIndex() - b->trackIndex() < 0;
1413 static bool eventTimeCueCompare(const std::pair<MediaTime, TextTrackCue*>& a, const std::pair<MediaTime, TextTrackCue*>& b)
1415 // 12 - Sort the tasks in events in ascending time order (tasks with earlier
1417 if (a.first != b.first)
1418 return a.first - b.first < MediaTime::zeroTime();
1420 // If the cues belong to different text tracks, it doesn't make sense to
1421 // compare the two tracks by the relative cue order, so return the relative
1423 if (a.second->track() != b.second->track())
1424 return trackIndexCompare(a.second->track(), b.second->track());
1426 // 12 - Further sort tasks in events that have the same time by the
1427 // relative text track cue order of the text track cues associated
1428 // with these tasks.
1429 return a.second->cueIndex() - b.second->cueIndex() < 0;
1432 static bool compareCueInterval(const CueInterval& one, const CueInterval& two)
1434 return one.data()->isOrderedBefore(two.data());
1438 void HTMLMediaElement::updateActiveTextTrackCues(const MediaTime& movieTime)
1440 // 4.8.10.8 Playing the media resource
1442 // If the current playback position changes while the steps are running,
1443 // then the user agent must wait for the steps to complete, and then must
1444 // immediately rerun the steps.
1445 if (ignoreTrackDisplayUpdateRequests())
1448 LOG(Media, "HTMLMediaElement::updateActiveTextTrackCues(%p)", this);
1450 // 1 - Let current cues be a list of cues, initialized to contain all the
1451 // cues of all the hidden, showing, or showing by default text tracks of the
1452 // media element (not the disabled ones) whose start times are less than or
1453 // equal to the current playback position and whose end times are greater
1454 // than the current playback position.
1455 CueList currentCues;
1457 // The user agent must synchronously unset [the text track cue active] flag
1458 // whenever ... the media element's readyState is changed back to HAVE_NOTHING.
1459 if (m_readyState != HAVE_NOTHING && m_player) {
1460 currentCues = m_cueTree.allOverlaps(m_cueTree.createInterval(movieTime, movieTime));
1461 if (currentCues.size() > 1)
1462 std::sort(currentCues.begin(), currentCues.end(), &compareCueInterval);
1465 CueList previousCues;
1468 // 2 - Let other cues be a list of cues, initialized to contain all the cues
1469 // of hidden, showing, and showing by default text tracks of the media
1470 // element that are not present in current cues.
1471 previousCues = m_currentlyActiveCues;
1473 // 3 - Let last time be the current playback position at the time this
1474 // algorithm was last run for this media element, if this is not the first
1476 MediaTime lastTime = m_lastTextTrackUpdateTime;
1478 // 4 - If the current playback position has, since the last time this
1479 // algorithm was run, only changed through its usual monotonic increase
1480 // during normal playback, then let missed cues be the list of cues in other
1481 // cues whose start times are greater than or equal to last time and whose
1482 // end times are less than or equal to the current playback position.
1483 // Otherwise, let missed cues be an empty list.
1484 if (lastTime >= MediaTime::zeroTime() && m_lastSeekTime < movieTime) {
1485 CueList potentiallySkippedCues =
1486 m_cueTree.allOverlaps(m_cueTree.createInterval(lastTime, movieTime));
1488 for (size_t i = 0; i < potentiallySkippedCues.size(); ++i) {
1489 MediaTime cueStartTime = potentiallySkippedCues[i].low();
1490 MediaTime cueEndTime = potentiallySkippedCues[i].high();
1492 // Consider cues that may have been missed since the last seek time.
1493 if (cueStartTime > std::max(m_lastSeekTime, lastTime) && cueEndTime < movieTime)
1494 missedCues.append(potentiallySkippedCues[i]);
1498 m_lastTextTrackUpdateTime = movieTime;
1500 // 5 - If the time was reached through the usual monotonic increase of the
1501 // current playback position during normal playback, and if the user agent
1502 // has not fired a timeupdate event at the element in the past 15 to 250ms
1503 // and is not still running event handlers for such an event, then the user
1504 // agent must queue a task to fire a simple event named timeupdate at the
1505 // element. (In the other cases, such as explicit seeks, relevant events get
1506 // fired as part of the overall process of changing the current playback
1508 if (!m_paused && m_lastSeekTime <= lastTime)
1509 scheduleTimeupdateEvent(false);
1511 // Explicitly cache vector sizes, as their content is constant from here.
1512 size_t currentCuesSize = currentCues.size();
1513 size_t missedCuesSize = missedCues.size();
1514 size_t previousCuesSize = previousCues.size();
1516 // 6 - If all of the cues in current cues have their text track cue active
1517 // flag set, none of the cues in other cues have their text track cue active
1518 // flag set, and missed cues is empty, then abort these steps.
1519 bool activeSetChanged = missedCuesSize;
1521 for (size_t i = 0; !activeSetChanged && i < previousCuesSize; ++i)
1522 if (!currentCues.contains(previousCues[i]) && previousCues[i].data()->isActive())
1523 activeSetChanged = true;
1525 for (size_t i = 0; i < currentCuesSize; ++i) {
1526 TextTrackCue* cue = currentCues[i].data();
1528 if (cue->isRenderable())
1529 toVTTCue(cue)->updateDisplayTree(movieTime);
1531 if (!cue->isActive())
1532 activeSetChanged = true;
1535 if (!activeSetChanged)
1538 // 7 - If the time was reached through the usual monotonic increase of the
1539 // current playback position during normal playback, and there are cues in
1540 // other cues that have their text track cue pause-on-exi flag set and that
1541 // either have their text track cue active flag set or are also in missed
1542 // cues, then immediately pause the media element.
1543 for (size_t i = 0; !m_paused && i < previousCuesSize; ++i) {
1544 if (previousCues[i].data()->pauseOnExit()
1545 && previousCues[i].data()->isActive()
1546 && !currentCues.contains(previousCues[i]))
1550 for (size_t i = 0; !m_paused && i < missedCuesSize; ++i) {
1551 if (missedCues[i].data()->pauseOnExit())
1555 // 8 - Let events be a list of tasks, initially empty. Each task in this
1556 // list will be associated with a text track, a text track cue, and a time,
1557 // which are used to sort the list before the tasks are queued.
1558 Vector<std::pair<MediaTime, TextTrackCue*>> eventTasks;
1560 // 8 - Let affected tracks be a list of text tracks, initially empty.
1561 Vector<TextTrack*> affectedTracks;
1563 for (size_t i = 0; i < missedCuesSize; ++i) {
1564 // 9 - For each text track cue in missed cues, prepare an event named enter
1565 // for the TextTrackCue object with the text track cue start time.
1566 eventTasks.append(std::make_pair(missedCues[i].data()->startMediaTime(),
1567 missedCues[i].data()));
1569 // 10 - For each text track [...] in missed cues, prepare an event
1570 // named exit for the TextTrackCue object with the with the later of
1571 // the text track cue end time and the text track cue start time.
1573 // Note: An explicit task is added only if the cue is NOT a zero or
1574 // negative length cue. Otherwise, the need for an exit event is
1575 // checked when these tasks are actually queued below. This doesn't
1576 // affect sorting events before dispatch either, because the exit
1577 // event has the same time as the enter event.
1578 if (missedCues[i].data()->startMediaTime() < missedCues[i].data()->endMediaTime())
1579 eventTasks.append(std::make_pair(missedCues[i].data()->endMediaTime(), missedCues[i].data()));
1582 for (size_t i = 0; i < previousCuesSize; ++i) {
1583 // 10 - For each text track cue in other cues that has its text
1584 // track cue active flag set prepare an event named exit for the
1585 // TextTrackCue object with the text track cue end time.
1586 if (!currentCues.contains(previousCues[i]))
1587 eventTasks.append(std::make_pair(previousCues[i].data()->endMediaTime(),
1588 previousCues[i].data()));
1591 for (size_t i = 0; i < currentCuesSize; ++i) {
1592 // 11 - For each text track cue in current cues that does not have its
1593 // text track cue active flag set, prepare an event named enter for the
1594 // TextTrackCue object with the text track cue start time.
1595 if (!previousCues.contains(currentCues[i]))
1596 eventTasks.append(std::make_pair(currentCues[i].data()->startMediaTime(),
1597 currentCues[i].data()));
1600 // 12 - Sort the tasks in events in ascending time order (tasks with earlier
1602 std::sort(eventTasks.begin(), eventTasks.end(), eventTimeCueCompare);
1604 for (size_t i = 0; i < eventTasks.size(); ++i) {
1605 if (!affectedTracks.contains(eventTasks[i].second->track()))
1606 affectedTracks.append(eventTasks[i].second->track());
1608 // 13 - Queue each task in events, in list order.
1609 RefPtr<Event> event;
1611 // Each event in eventTasks may be either an enterEvent or an exitEvent,
1612 // depending on the time that is associated with the event. This
1613 // correctly identifies the type of the event, if the startTime is
1614 // less than the endTime in the cue.
1615 if (eventTasks[i].second->startTime() >= eventTasks[i].second->endTime()) {
1616 event = Event::create(eventNames().enterEvent, false, false);
1617 event->setTarget(eventTasks[i].second);
1618 m_asyncEventQueue.enqueueEvent(event.release());
1620 event = Event::create(eventNames().exitEvent, false, false);
1621 event->setTarget(eventTasks[i].second);
1622 m_asyncEventQueue.enqueueEvent(event.release());
1624 if (eventTasks[i].first == eventTasks[i].second->startMediaTime())
1625 event = Event::create(eventNames().enterEvent, false, false);
1627 event = Event::create(eventNames().exitEvent, false, false);
1629 event->setTarget(eventTasks[i].second);
1630 m_asyncEventQueue.enqueueEvent(event.release());
1634 // 14 - Sort affected tracks in the same order as the text tracks appear in
1635 // the media element's list of text tracks, and remove duplicates.
1636 std::sort(affectedTracks.begin(), affectedTracks.end(), trackIndexCompare);
1638 // 15 - For each text track in affected tracks, in the list order, queue a
1639 // task to fire a simple event named cuechange at the TextTrack object, and, ...
1640 for (size_t i = 0; i < affectedTracks.size(); ++i) {
1641 RefPtr<Event> event = Event::create(eventNames().cuechangeEvent, false, false);
1642 event->setTarget(affectedTracks[i]);
1644 m_asyncEventQueue.enqueueEvent(event.release());
1646 // ... if the text track has a corresponding track element, to then fire a
1647 // simple event named cuechange at the track element as well.
1648 if (is<LoadableTextTrack>(*affectedTracks[i])) {
1649 RefPtr<Event> event = Event::create(eventNames().cuechangeEvent, false, false);
1650 HTMLTrackElement* trackElement = downcast<LoadableTextTrack>(*affectedTracks[i]).trackElement();
1651 ASSERT(trackElement);
1652 event->setTarget(trackElement);
1654 m_asyncEventQueue.enqueueEvent(event.release());
1658 // 16 - Set the text track cue active flag of all the cues in the current
1659 // cues, and unset the text track cue active flag of all the cues in the
1661 for (size_t i = 0; i < currentCuesSize; ++i)
1662 currentCues[i].data()->setIsActive(true);
1664 for (size_t i = 0; i < previousCuesSize; ++i)
1665 if (!currentCues.contains(previousCues[i]))
1666 previousCues[i].data()->setIsActive(false);
1668 // Update the current active cues.
1669 m_currentlyActiveCues = currentCues;
1671 if (activeSetChanged)
1672 updateTextTrackDisplay();
1675 bool HTMLMediaElement::textTracksAreReady() const
1677 // 4.8.10.12.1 Text track model
1679 // The text tracks of a media element are ready if all the text tracks whose mode was not
1680 // in the disabled state when the element's resource selection algorithm last started now
1681 // have a text track readiness state of loaded or failed to load.
1682 for (unsigned i = 0; i < m_textTracksWhenResourceSelectionBegan.size(); ++i) {
1683 if (m_textTracksWhenResourceSelectionBegan[i]->readinessState() == TextTrack::Loading
1684 || m_textTracksWhenResourceSelectionBegan[i]->readinessState() == TextTrack::NotLoaded)
1691 void HTMLMediaElement::textTrackReadyStateChanged(TextTrack* track)
1693 if (m_player && m_textTracksWhenResourceSelectionBegan.contains(track)) {
1694 if (track->readinessState() != TextTrack::Loading)
1695 setReadyState(m_player->readyState());
1697 // The track readiness state might have changed as a result of the user
1698 // clicking the captions button. In this case, a check whether all the
1699 // resources have failed loading should be done in order to hide the CC button.
1700 if (hasMediaControls() && track->readinessState() == TextTrack::FailedToLoad)
1701 mediaControls()->refreshClosedCaptionsButtonVisibility();
1705 void HTMLMediaElement::audioTrackEnabledChanged(AudioTrack* track)
1707 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
1709 if (m_audioTracks && m_audioTracks->contains(track))
1710 m_audioTracks->scheduleChangeEvent();
1713 void HTMLMediaElement::textTrackModeChanged(TextTrack* track)
1715 bool trackIsLoaded = true;
1716 if (track->trackType() == TextTrack::TrackElement) {
1717 trackIsLoaded = false;
1718 for (auto& trackElement : childrenOfType<HTMLTrackElement>(*this)) {
1719 if (trackElement.track() == track) {
1720 if (trackElement.readyState() == HTMLTrackElement::LOADING || trackElement.readyState() == HTMLTrackElement::LOADED)
1721 trackIsLoaded = true;
1727 // If this is the first added track, create the list of text tracks.
1729 m_textTracks = TextTrackList::create(this, ActiveDOMObject::scriptExecutionContext());
1731 // Mark this track as "configured" so configureTextTracks won't change the mode again.
1732 track->setHasBeenConfigured(true);
1734 if (track->mode() != TextTrack::disabledKeyword() && trackIsLoaded)
1735 textTrackAddCues(track, track->cues());
1737 configureTextTrackDisplay(AssumeTextTrackVisibilityChanged);
1739 if (m_textTracks && m_textTracks->contains(track))
1740 m_textTracks->scheduleChangeEvent();
1742 #if ENABLE(AVF_CAPTIONS)
1743 if (track->trackType() == TextTrack::TrackElement && m_player)
1744 m_player->notifyTrackModeChanged();
1748 void HTMLMediaElement::videoTrackSelectedChanged(VideoTrack* track)
1750 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
1752 if (m_videoTracks && m_videoTracks->contains(track))
1753 m_videoTracks->scheduleChangeEvent();
1756 void HTMLMediaElement::textTrackKindChanged(TextTrack* track)
1758 if (track->kind() != TextTrack::captionsKeyword() && track->kind() != TextTrack::subtitlesKeyword() && track->mode() == TextTrack::showingKeyword())
1759 track->setMode(TextTrack::hiddenKeyword());
1762 void HTMLMediaElement::beginIgnoringTrackDisplayUpdateRequests()
1764 ++m_ignoreTrackDisplayUpdate;
1767 void HTMLMediaElement::endIgnoringTrackDisplayUpdateRequests()
1769 ASSERT(m_ignoreTrackDisplayUpdate);
1770 --m_ignoreTrackDisplayUpdate;
1771 if (!m_ignoreTrackDisplayUpdate && m_inActiveDocument)
1772 updateActiveTextTrackCues(currentMediaTime());
1775 void HTMLMediaElement::textTrackAddCues(TextTrack* track, const TextTrackCueList* cues)
1777 if (track->mode() == TextTrack::disabledKeyword())
1780 TrackDisplayUpdateScope scope(this);
1781 for (size_t i = 0; i < cues->length(); ++i)
1782 textTrackAddCue(track, cues->item(i));
1785 void HTMLMediaElement::textTrackRemoveCues(TextTrack*, const TextTrackCueList* cues)
1787 TrackDisplayUpdateScope scope(this);
1788 for (size_t i = 0; i < cues->length(); ++i)
1789 textTrackRemoveCue(cues->item(i)->track(), cues->item(i));
1792 void HTMLMediaElement::textTrackAddCue(TextTrack* track, PassRefPtr<TextTrackCue> prpCue)
1794 if (track->mode() == TextTrack::disabledKeyword())
1797 RefPtr<TextTrackCue> cue = prpCue;
1799 // Negative duration cues need be treated in the interval tree as
1800 // zero-length cues.
1801 MediaTime endTime = std::max(cue->startMediaTime(), cue->endMediaTime());
1803 CueInterval interval = m_cueTree.createInterval(cue->startMediaTime(), endTime, cue.get());
1804 if (!m_cueTree.contains(interval))
1805 m_cueTree.add(interval);
1806 updateActiveTextTrackCues(currentMediaTime());
1809 void HTMLMediaElement::textTrackRemoveCue(TextTrack*, PassRefPtr<TextTrackCue> prpCue)
1811 RefPtr<TextTrackCue> cue = prpCue;
1812 // Negative duration cues need to be treated in the interval tree as
1813 // zero-length cues.
1814 MediaTime endTime = std::max(cue->startMediaTime(), cue->endMediaTime());
1816 CueInterval interval = m_cueTree.createInterval(cue->startMediaTime(), endTime, cue.get());
1817 m_cueTree.remove(interval);
1819 // Since the cue will be removed from the media element and likely the
1820 // TextTrack might also be destructed, notifying the region of the cue
1821 // removal shouldn't be done.
1822 if (cue->isRenderable())
1823 toVTTCue(cue.get())->notifyRegionWhenRemovingDisplayTree(false);
1825 size_t index = m_currentlyActiveCues.find(interval);
1826 if (index != notFound) {
1827 cue->setIsActive(false);
1828 m_currentlyActiveCues.remove(index);
1831 if (cue->isRenderable())
1832 toVTTCue(cue.get())->removeDisplayTree();
1833 updateActiveTextTrackCues(currentMediaTime());
1835 if (cue->isRenderable())
1836 toVTTCue(cue.get())->notifyRegionWhenRemovingDisplayTree(true);
1841 bool HTMLMediaElement::isSafeToLoadURL(const URL& url, InvalidURLAction actionIfInvalid)
1843 if (!url.isValid()) {
1844 LOG(Media, "HTMLMediaElement::isSafeToLoadURL(%p) - %s -> FALSE because url is invalid", this, urlForLoggingMedia(url).utf8().data());
1848 Frame* frame = document().frame();
1849 if (!frame || !document().securityOrigin()->canDisplay(url)) {
1850 if (actionIfInvalid == Complain)
1851 FrameLoader::reportLocalLoadFailed(frame, url.stringCenterEllipsizedToLength());
1852 LOG(Media, "HTMLMediaElement::isSafeToLoadURL(%p) - %s -> FALSE rejected by SecurityOrigin", this, urlForLoggingMedia(url).utf8().data());
1856 if (!document().contentSecurityPolicy()->allowMediaFromSource(url, isInUserAgentShadowTree())) {
1857 LOG(Media, "HTMLMediaElement::isSafeToLoadURL(%p) - %s -> rejected by Content Security Policy", this, urlForLoggingMedia(url).utf8().data());
1864 void HTMLMediaElement::startProgressEventTimer()
1866 if (m_progressEventTimer.isActive())
1869 m_previousProgressTime = monotonicallyIncreasingTime();
1870 // 350ms is not magic, it is in the spec!
1871 m_progressEventTimer.startRepeating(0.350);
1874 void HTMLMediaElement::waitForSourceChange()
1876 LOG(Media, "HTMLMediaElement::waitForSourceChange(%p)", this);
1878 stopPeriodicTimers();
1879 m_loadState = WaitingForSource;
1881 // 6.17 - Waiting: Set the element's networkState attribute to the NETWORK_NO_SOURCE value
1882 m_networkState = NETWORK_NO_SOURCE;
1884 // 6.18 - Set the element's delaying-the-load-event flag to false. This stops delaying the load event.
1885 setShouldDelayLoadEvent(false);
1887 updateDisplayState();
1890 renderer()->updateFromElement();
1893 void HTMLMediaElement::noneSupported()
1895 LOG(Media, "HTMLMediaElement::noneSupported(%p)", this);
1897 stopPeriodicTimers();
1898 m_loadState = WaitingForSource;
1899 m_currentSourceNode = nullptr;
1902 // 6 - Reaching this step indicates that the media resource failed to load or that the given
1903 // URL could not be resolved. In one atomic operation, run the following steps:
1905 // 6.1 - Set the error attribute to a new MediaError object whose code attribute is set to
1906 // MEDIA_ERR_SRC_NOT_SUPPORTED.
1907 m_error = MediaError::create(MediaError::MEDIA_ERR_SRC_NOT_SUPPORTED);
1909 // 6.2 - Forget the media element's media-resource-specific text tracks.
1910 forgetResourceSpecificTracks();
1912 // 6.3 - Set the element's networkState attribute to the NETWORK_NO_SOURCE value.
1913 m_networkState = NETWORK_NO_SOURCE;
1915 // 7 - Queue a task to fire a simple event named error at the media element.
1916 scheduleEvent(eventNames().errorEvent);
1918 #if ENABLE(MEDIA_SOURCE)
1922 // 8 - Set the element's delaying-the-load-event flag to false. This stops delaying the load event.
1923 setShouldDelayLoadEvent(false);
1925 // 9 - Abort these steps. Until the load() method is invoked or the src attribute is changed,
1926 // the element won't attempt to load another resource.
1928 updateDisplayState();
1931 renderer()->updateFromElement();
1934 void HTMLMediaElement::mediaLoadingFailedFatally(MediaPlayer::NetworkState error)
1936 LOG(Media, "HTMLMediaElement::mediaLoadingFailedFatally(%p) - error = %d", this, static_cast<int>(error));
1938 // 1 - The user agent should cancel the fetching process.
1939 stopPeriodicTimers();
1940 m_loadState = WaitingForSource;
1942 // 2 - Set the error attribute to a new MediaError object whose code attribute is
1943 // set to MEDIA_ERR_NETWORK/MEDIA_ERR_DECODE.
1944 if (error == MediaPlayer::NetworkError)
1945 m_error = MediaError::create(MediaError::MEDIA_ERR_NETWORK);
1946 else if (error == MediaPlayer::DecodeError)
1947 m_error = MediaError::create(MediaError::MEDIA_ERR_DECODE);
1949 ASSERT_NOT_REACHED();
1951 // 3 - Queue a task to fire a simple event named error at the media element.
1952 scheduleEvent(eventNames().errorEvent);
1954 #if ENABLE(MEDIA_SOURCE)
1958 // 4 - Set the element's networkState attribute to the NETWORK_EMPTY value and queue a
1959 // task to fire a simple event called emptied at the element.
1960 m_networkState = NETWORK_EMPTY;
1961 scheduleEvent(eventNames().emptiedEvent);
1963 // 5 - Set the element's delaying-the-load-event flag to false. This stops delaying the load event.
1964 setShouldDelayLoadEvent(false);
1966 // 6 - Abort the overall resource selection algorithm.
1967 m_currentSourceNode = nullptr;
1970 if (is<MediaDocument>(document()))
1971 downcast<MediaDocument>(document()).mediaElementSawUnsupportedTracks();
1975 void HTMLMediaElement::cancelPendingEventsAndCallbacks()
1977 LOG(Media, "HTMLMediaElement::cancelPendingEventsAndCallbacks(%p)", this);
1978 m_asyncEventQueue.cancelAllEvents();
1980 for (auto& source : childrenOfType<HTMLSourceElement>(*this))
1981 source.cancelPendingErrorEvent();
1984 void HTMLMediaElement::mediaPlayerNetworkStateChanged(MediaPlayer*)
1986 beginProcessingMediaPlayerCallback();
1987 setNetworkState(m_player->networkState());
1988 endProcessingMediaPlayerCallback();
1991 static void logMediaLoadRequest(Page* page, const String& mediaEngine, const String& errorMessage, bool succeeded)
1996 DiagnosticLoggingClient& diagnosticLoggingClient = page->diagnosticLoggingClient();
1998 diagnosticLoggingClient.logDiagnosticMessageWithResult(DiagnosticLoggingKeys::mediaLoadingFailedKey(), errorMessage, DiagnosticLoggingResultFail, ShouldSample::No);
2002 diagnosticLoggingClient.logDiagnosticMessage(DiagnosticLoggingKeys::mediaLoadedKey(), mediaEngine, ShouldSample::No);
2004 if (!page->hasSeenAnyMediaEngine())
2005 diagnosticLoggingClient.logDiagnosticMessage(DiagnosticLoggingKeys::pageContainsAtLeastOneMediaEngineKey(), emptyString(), ShouldSample::No);
2007 if (!page->hasSeenMediaEngine(mediaEngine))
2008 diagnosticLoggingClient.logDiagnosticMessage(DiagnosticLoggingKeys::pageContainsMediaEngineKey(), mediaEngine, ShouldSample::No);
2010 page->sawMediaEngine(mediaEngine);
2013 static String stringForNetworkState(MediaPlayer::NetworkState state)
2016 case MediaPlayer::Empty: return ASCIILiteral("Empty");
2017 case MediaPlayer::Idle: return ASCIILiteral("Idle");
2018 case MediaPlayer::Loading: return ASCIILiteral("Loading");
2019 case MediaPlayer::Loaded: return ASCIILiteral("Loaded");
2020 case MediaPlayer::FormatError: return ASCIILiteral("FormatError");
2021 case MediaPlayer::NetworkError: return ASCIILiteral("NetworkError");
2022 case MediaPlayer::DecodeError: return ASCIILiteral("DecodeError");
2023 default: return emptyString();
2027 void HTMLMediaElement::mediaLoadingFailed(MediaPlayer::NetworkState error)
2029 stopPeriodicTimers();
2031 // If we failed while trying to load a <source> element, the movie was never parsed, and there are more
2032 // <source> children, schedule the next one
2033 if (m_readyState < HAVE_METADATA && m_loadState == LoadingFromSourceElement) {
2035 // resource selection algorithm
2036 // 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.
2037 if (m_currentSourceNode)
2038 m_currentSourceNode->scheduleErrorEvent();
2040 LOG(Media, "HTMLMediaElement::setNetworkState(%p) - error event not sent, <source> was removed", this);
2042 // 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.
2044 // 9.Otherwise.11 - Forget the media element's media-resource-specific tracks.
2045 forgetResourceSpecificTracks();
2047 if (havePotentialSourceChild()) {
2048 LOG(Media, "HTMLMediaElement::setNetworkState(%p) - scheduling next <source>", this);
2049 scheduleNextSourceChild();
2051 LOG(Media, "HTMLMediaElement::setNetworkState(%p) - no more <source> elements, waiting", this);
2052 waitForSourceChange();
2058 if ((error == MediaPlayer::NetworkError && m_readyState >= HAVE_METADATA) || error == MediaPlayer::DecodeError)
2059 mediaLoadingFailedFatally(error);
2060 else if ((error == MediaPlayer::FormatError || error == MediaPlayer::NetworkError) && m_loadState == LoadingFromSrcAttr)
2063 updateDisplayState();
2064 if (hasMediaControls()) {
2065 mediaControls()->reset();
2066 mediaControls()->reportedError();
2069 logMediaLoadRequest(document().page(), String(), stringForNetworkState(error), false);
2071 m_mediaSession->clientCharacteristicsChanged();
2074 void HTMLMediaElement::setNetworkState(MediaPlayer::NetworkState state)
2076 LOG(Media, "HTMLMediaElement::setNetworkState(%p) - new state = %d, current state = %d", this, static_cast<int>(state), static_cast<int>(m_networkState));
2078 if (state == MediaPlayer::Empty) {
2079 // Just update the cached state and leave, we can't do anything.
2080 m_networkState = NETWORK_EMPTY;
2084 if (state == MediaPlayer::FormatError || state == MediaPlayer::NetworkError || state == MediaPlayer::DecodeError) {
2085 mediaLoadingFailed(state);
2089 if (state == MediaPlayer::Idle) {
2090 if (m_networkState > NETWORK_IDLE) {
2091 changeNetworkStateFromLoadingToIdle();
2092 setShouldDelayLoadEvent(false);
2094 m_networkState = NETWORK_IDLE;
2098 if (state == MediaPlayer::Loading) {
2099 if (m_networkState < NETWORK_LOADING || m_networkState == NETWORK_NO_SOURCE)
2100 startProgressEventTimer();
2101 m_networkState = NETWORK_LOADING;
2104 if (state == MediaPlayer::Loaded) {
2105 if (m_networkState != NETWORK_IDLE)
2106 changeNetworkStateFromLoadingToIdle();
2107 m_completelyLoaded = true;
2110 if (hasMediaControls())
2111 mediaControls()->updateStatusDisplay();
2114 void HTMLMediaElement::changeNetworkStateFromLoadingToIdle()
2116 m_progressEventTimer.stop();
2117 if (hasMediaControls() && m_player->didLoadingProgress())
2118 mediaControls()->bufferingProgressed();
2120 // Schedule one last progress event so we guarantee that at least one is fired
2121 // for files that load very quickly.
2122 scheduleEvent(eventNames().progressEvent);
2123 scheduleEvent(eventNames().suspendEvent);
2124 m_networkState = NETWORK_IDLE;
2127 void HTMLMediaElement::mediaPlayerReadyStateChanged(MediaPlayer*)
2129 beginProcessingMediaPlayerCallback();
2131 setReadyState(m_player->readyState());
2133 endProcessingMediaPlayerCallback();
2136 bool HTMLMediaElement::canTransitionFromAutoplayToPlay() const
2138 return isAutoplaying()
2141 && !pausedForUserInteraction()
2142 && !document().isSandboxed(SandboxAutomaticFeatures)
2143 && mediaSession().playbackPermitted(*this);
2146 void HTMLMediaElement::setReadyState(MediaPlayer::ReadyState state)
2148 LOG(Media, "HTMLMediaElement::setReadyState(%p) - new state = %d, current state = %d,", this, static_cast<int>(state), static_cast<int>(m_readyState));
2150 // Set "wasPotentiallyPlaying" BEFORE updating m_readyState, potentiallyPlaying() uses it
2151 bool wasPotentiallyPlaying = potentiallyPlaying();
2153 ReadyState oldState = m_readyState;
2154 ReadyState newState = static_cast<ReadyState>(state);
2156 #if ENABLE(VIDEO_TRACK)
2157 bool tracksAreReady = !RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled() || textTracksAreReady();
2159 if (newState == oldState && m_tracksAreReady == tracksAreReady)
2162 m_tracksAreReady = tracksAreReady;
2164 if (newState == oldState)
2166 bool tracksAreReady = true;
2170 m_readyState = newState;
2172 // If a media file has text tracks the readyState may not progress beyond HAVE_FUTURE_DATA until
2173 // the text tracks are ready, regardless of the state of the media file.
2174 if (newState <= HAVE_METADATA)
2175 m_readyState = newState;
2177 m_readyState = HAVE_CURRENT_DATA;
2180 if (oldState > m_readyStateMaximum)
2181 m_readyStateMaximum = oldState;
2183 if (m_networkState == NETWORK_EMPTY)
2187 // 4.8.10.9, step 11
2188 if (wasPotentiallyPlaying && m_readyState < HAVE_FUTURE_DATA)
2189 scheduleEvent(eventNames().waitingEvent);
2191 // 4.8.10.10 step 14 & 15.
2192 if (!m_player->seeking() && m_readyState >= HAVE_CURRENT_DATA)
2195 if (wasPotentiallyPlaying && m_readyState < HAVE_FUTURE_DATA) {
2197 invalidateCachedTime();
2198 scheduleTimeupdateEvent(false);
2199 scheduleEvent(eventNames().waitingEvent);
2203 if (m_readyState >= HAVE_METADATA && oldState < HAVE_METADATA) {
2204 prepareMediaFragmentURI();
2205 scheduleEvent(eventNames().durationchangeEvent);
2206 scheduleResizeEvent();
2207 scheduleEvent(eventNames().loadedmetadataEvent);
2208 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
2209 if (hasEventListeners(eventNames().webkitplaybacktargetavailabilitychangedEvent))
2210 enqueuePlaybackTargetAvailabilityChangedEvent();
2212 m_initiallyMuted = m_volume < 0.05 || muted();
2214 if (hasMediaControls())
2215 mediaControls()->loadedMetadata();
2217 renderer()->updateFromElement();
2219 if (is<MediaDocument>(document()))
2220 downcast<MediaDocument>(document()).mediaElementNaturalSizeChanged(expandedIntSize(m_player->naturalSize()));
2222 logMediaLoadRequest(document().page(), m_player->engineDescription(), String(), true);
2224 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
2225 updateMediaState(UpdateMediaState::Asynchronously);
2228 m_mediaSession->clientCharacteristicsChanged();
2231 bool shouldUpdateDisplayState = false;
2233 if (m_readyState >= HAVE_CURRENT_DATA && oldState < HAVE_CURRENT_DATA && !m_haveFiredLoadedData) {
2234 m_haveFiredLoadedData = true;
2235 shouldUpdateDisplayState = true;
2236 scheduleEvent(eventNames().loadeddataEvent);
2237 setShouldDelayLoadEvent(false);
2238 applyMediaFragmentURI();
2241 bool isPotentiallyPlaying = potentiallyPlaying();
2242 if (m_readyState == HAVE_FUTURE_DATA && oldState <= HAVE_CURRENT_DATA && tracksAreReady) {
2243 scheduleEvent(eventNames().canplayEvent);
2244 if (isPotentiallyPlaying)
2245 scheduleEvent(eventNames().playingEvent);
2246 shouldUpdateDisplayState = true;
2249 if (m_readyState == HAVE_ENOUGH_DATA && oldState < HAVE_ENOUGH_DATA && tracksAreReady) {
2250 if (oldState <= HAVE_CURRENT_DATA)
2251 scheduleEvent(eventNames().canplayEvent);
2253 scheduleEvent(eventNames().canplaythroughEvent);
2255 if (isPotentiallyPlaying && oldState <= HAVE_CURRENT_DATA)
2256 scheduleEvent(eventNames().playingEvent);
2258 if (canTransitionFromAutoplayToPlay()) {
2260 invalidateCachedTime();
2261 scheduleEvent(eventNames().playEvent);
2262 scheduleEvent(eventNames().playingEvent);
2265 shouldUpdateDisplayState = true;
2268 if (shouldUpdateDisplayState) {
2269 updateDisplayState();
2270 if (hasMediaControls()) {
2271 mediaControls()->refreshClosedCaptionsButtonVisibility();
2272 mediaControls()->updateStatusDisplay();
2277 updateMediaController();
2278 #if ENABLE(VIDEO_TRACK)
2279 if (RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
2280 updateActiveTextTrackCues(currentMediaTime());
2284 #if ENABLE(ENCRYPTED_MEDIA)
2285 void HTMLMediaElement::mediaPlayerKeyAdded(MediaPlayer*, const String& keySystem, const String& sessionId)
2287 Ref<Event> event = MediaKeyEvent::create(eventNames().webkitkeyaddedEvent, keySystem, sessionId, nullptr, nullptr, emptyString(), nullptr, 0);
2288 event->setTarget(this);
2289 m_asyncEventQueue.enqueueEvent(WTFMove(event));
2292 void HTMLMediaElement::mediaPlayerKeyError(MediaPlayer*, const String& keySystem, const String& sessionId, MediaPlayerClient::MediaKeyErrorCode errorCode, unsigned short systemCode)
2294 MediaKeyError::Code mediaKeyErrorCode = MediaKeyError::MEDIA_KEYERR_UNKNOWN;
2295 switch (errorCode) {
2296 case MediaPlayerClient::UnknownError:
2297 mediaKeyErrorCode = MediaKeyError::MEDIA_KEYERR_UNKNOWN;
2299 case MediaPlayerClient::ClientError:
2300 mediaKeyErrorCode = MediaKeyError::MEDIA_KEYERR_CLIENT;
2302 case MediaPlayerClient::ServiceError:
2303 mediaKeyErrorCode = MediaKeyError::MEDIA_KEYERR_SERVICE;
2305 case MediaPlayerClient::OutputError:
2306 mediaKeyErrorCode = MediaKeyError::MEDIA_KEYERR_OUTPUT;
2308 case MediaPlayerClient::HardwareChangeError:
2309 mediaKeyErrorCode = MediaKeyError::MEDIA_KEYERR_HARDWARECHANGE;
2311 case MediaPlayerClient::DomainError:
2312 mediaKeyErrorCode = MediaKeyError::MEDIA_KEYERR_DOMAIN;
2316 Ref<Event> event = MediaKeyEvent::create(eventNames().webkitkeyerrorEvent, keySystem, sessionId, nullptr, nullptr, emptyString(), MediaKeyError::create(mediaKeyErrorCode), systemCode);
2317 event->setTarget(this);
2318 m_asyncEventQueue.enqueueEvent(WTFMove(event));
2321 void HTMLMediaElement::mediaPlayerKeyMessage(MediaPlayer*, const String& keySystem, const String& sessionId, const unsigned char* message, unsigned messageLength, const URL& defaultURL)
2323 Ref<Event> event = MediaKeyEvent::create(eventNames().webkitkeymessageEvent, keySystem, sessionId, nullptr, Uint8Array::create(message, messageLength), defaultURL, nullptr, 0);
2324 event->setTarget(this);
2325 m_asyncEventQueue.enqueueEvent(WTFMove(event));
2328 bool HTMLMediaElement::mediaPlayerKeyNeeded(MediaPlayer*, const String& keySystem, const String& sessionId, const unsigned char* initData, unsigned initDataLength)
2330 if (!hasEventListeners(eventNames().webkitneedkeyEvent)) {
2331 m_error = MediaError::create(MediaError::MEDIA_ERR_ENCRYPTED);
2332 scheduleEvent(eventNames().errorEvent);
2336 Ref<Event> event = MediaKeyEvent::create(eventNames().webkitneedkeyEvent, keySystem, sessionId, Uint8Array::create(initData, initDataLength), nullptr, emptyString(), nullptr, 0);
2337 event->setTarget(this);
2338 m_asyncEventQueue.enqueueEvent(WTFMove(event));
2343 #if ENABLE(ENCRYPTED_MEDIA_V2)
2344 RefPtr<ArrayBuffer> HTMLMediaElement::mediaPlayerCachedKeyForKeyId(const String& keyId) const
2346 return m_mediaKeys ? m_mediaKeys->cachedKeyForKeyId(keyId) : nullptr;
2349 bool HTMLMediaElement::mediaPlayerKeyNeeded(MediaPlayer*, Uint8Array* initData)
2351 if (!hasEventListeners("webkitneedkey")) {
2352 m_error = MediaError::create(MediaError::MEDIA_ERR_ENCRYPTED);
2353 scheduleEvent(eventNames().errorEvent);
2357 RefPtr<Event> event = MediaKeyNeededEvent::create(eventNames().webkitneedkeyEvent, initData);
2358 event->setTarget(this);
2359 m_asyncEventQueue.enqueueEvent(event.release());
2364 String HTMLMediaElement::mediaPlayerMediaKeysStorageDirectory() const
2366 Settings* settings = document().settings();
2368 return emptyString();
2370 String storageDirectory = settings->mediaKeysStorageDirectory();
2371 if (storageDirectory.isEmpty())
2372 return emptyString();
2374 SecurityOrigin* origin = document().securityOrigin();
2376 return emptyString();
2378 return pathByAppendingComponent(storageDirectory, origin->databaseIdentifier());
2381 void HTMLMediaElement::setMediaKeys(MediaKeys* mediaKeys)
2383 if (m_mediaKeys == mediaKeys)
2387 m_mediaKeys->setMediaElement(0);
2388 m_mediaKeys = mediaKeys;
2390 m_mediaKeys->setMediaElement(this);
2393 void HTMLMediaElement::keyAdded()
2396 m_player->keyAdded();
2400 void HTMLMediaElement::progressEventTimerFired()
2403 if (m_networkState != NETWORK_LOADING)
2406 double time = monotonicallyIncreasingTime();
2407 double timedelta = time - m_previousProgressTime;
2409 if (m_player->didLoadingProgress()) {
2410 scheduleEvent(eventNames().progressEvent);
2411 m_previousProgressTime = time;
2412 m_sentStalledEvent = false;
2414 renderer()->updateFromElement();
2415 if (hasMediaControls())
2416 mediaControls()->bufferingProgressed();
2417 } else if (timedelta > 3.0 && !m_sentStalledEvent) {
2418 scheduleEvent(eventNames().stalledEvent);
2419 m_sentStalledEvent = true;
2420 setShouldDelayLoadEvent(false);
2424 void HTMLMediaElement::rewind(double timeDelta)
2426 LOG(Media, "HTMLMediaElement::rewind(%p) - %f", this, timeDelta);
2427 setCurrentTime(std::max(currentMediaTime() - MediaTime::createWithDouble(timeDelta), minTimeSeekable()));
2430 void HTMLMediaElement::returnToRealtime()
2432 LOG(Media, "HTMLMediaElement::returnToRealtime(%p)", this);
2433 setCurrentTime(maxTimeSeekable());
2436 void HTMLMediaElement::addPlayedRange(const MediaTime& start, const MediaTime& end)
2438 LOG(Media, "HTMLMediaElement::addPlayedRange(%p) - [%s, %s]", this, toString(start).utf8().data(), toString(end).utf8().data());
2439 if (!m_playedTimeRanges)
2440 m_playedTimeRanges = TimeRanges::create();
2441 m_playedTimeRanges->ranges().add(start, end);
2444 bool HTMLMediaElement::supportsScanning() const
2446 return m_player ? m_player->supportsScanning() : false;
2449 void HTMLMediaElement::prepareToPlay()
2451 LOG(Media, "HTMLMediaElement::prepareToPlay(%p)", this);
2452 if (m_havePreparedToPlay)
2454 m_havePreparedToPlay = true;
2455 m_player->prepareToPlay();
2458 void HTMLMediaElement::fastSeek(double time)
2460 fastSeek(MediaTime::createWithDouble(time));
2463 void HTMLMediaElement::fastSeek(const MediaTime& time)
2465 LOG(Media, "HTMLMediaElement::fastSeek(%p) - %s", this, toString(time).utf8().data());
2467 // 9. If the approximate-for-speed flag is set, adjust the new playback position to a value that will
2468 // allow for playback to resume promptly. If new playback position before this step is before current
2469 // playback position, then the adjusted new playback position must also be before the current playback
2470 // position. Similarly, if the new playback position before this step is after current playback position,
2471 // then the adjusted new playback position must also be after the current playback position.
2472 refreshCachedTime();
2473 MediaTime delta = time - currentMediaTime();
2474 MediaTime negativeTolerance = delta >= MediaTime::zeroTime() ? delta : MediaTime::positiveInfiniteTime();
2475 MediaTime positiveTolerance = delta < MediaTime::zeroTime() ? -delta : MediaTime::positiveInfiniteTime();
2477 seekWithTolerance(time, negativeTolerance, positiveTolerance, true);
2480 void HTMLMediaElement::seek(const MediaTime& time)
2482 LOG(Media, "HTMLMediaElement::seek(%p) - %s", this, toString(time).utf8().data());
2483 seekWithTolerance(time, MediaTime::zeroTime(), MediaTime::zeroTime(), true);
2486 void HTMLMediaElement::seekInternal(const MediaTime& time)
2488 LOG(Media, "HTMLMediaElement::seekInternal(%p) - %s", this, toString(time).utf8().data());
2489 seekWithTolerance(time, MediaTime::zeroTime(), MediaTime::zeroTime(), false);
2492 void HTMLMediaElement::seekWithTolerance(const MediaTime& inTime, const MediaTime& negativeTolerance, const MediaTime& positiveTolerance, bool fromDOM)
2495 MediaTime time = inTime;
2497 // 1 - Set the media element's show poster flag to false.
2498 setDisplayMode(Video);
2500 // 2 - If the media element's readyState is HAVE_NOTHING, abort these steps.
2501 if (m_readyState == HAVE_NOTHING || !m_player)
2504 // If the media engine has been told to postpone loading data, let it go ahead now.
2505 if (m_preload < MediaPlayer::Auto && m_readyState < HAVE_FUTURE_DATA)
2508 // Get the current time before setting m_seeking, m_lastSeekTime is returned once it is set.
2509 refreshCachedTime();
2510 MediaTime now = currentMediaTime();
2512 // 3 - If the element's seeking IDL attribute is true, then another instance of this algorithm is
2513 // already running. Abort that other instance of the algorithm without waiting for the step that
2514 // it is running to complete.
2515 if (m_seekTaskQueue.hasPendingTasks()) {
2516 LOG(Media, "HTMLMediaElement::seekWithTolerance(%p) - cancelling pending seeks", this);
2517 m_seekTaskQueue.cancelAllTasks();
2518 if (m_pendingSeek) {
2519 now = m_pendingSeek->now;
2520 m_pendingSeek = nullptr;
2522 m_pendingSeekType = NoSeek;
2525 // 4 - Set the seeking IDL attribute to true.
2526 // The flag will be cleared when the engine tells us the time has actually changed.
2529 if (m_lastSeekTime < now)
2530 addPlayedRange(m_lastSeekTime, now);
2532 m_lastSeekTime = time;
2534 // 5 - If the seek was in response to a DOM method call or setting of an IDL attribute, then continue
2535 // the script. The remainder of these steps must be run asynchronously.
2536 m_pendingSeek = std::make_unique<PendingSeek>(now, time, negativeTolerance, positiveTolerance);
2538 LOG(Media, "HTMLMediaElement::seekWithTolerance(%p) - enqueuing seek from %s to %s", this, toString(now).utf8().data(), toString(time).utf8().data());
2539 m_seekTaskQueue.enqueueTask(std::bind(&HTMLMediaElement::seekTask, this));
2544 void HTMLMediaElement::seekTask()
2546 LOG(Media, "HTMLMediaElement::seekTask(%p)", this);
2553 ASSERT(m_pendingSeek);
2554 MediaTime now = m_pendingSeek->now;
2555 MediaTime time = m_pendingSeek->targetTime;
2556 MediaTime negativeTolerance = m_pendingSeek->negativeTolerance;
2557 MediaTime positiveTolerance = m_pendingSeek->positiveTolerance;
2558 m_pendingSeek = nullptr;
2560 // 6 - If the new playback position is later than the end of the media resource, then let it be the end
2561 // of the media resource instead.
2562 time = std::min(time, durationMediaTime());
2564 // 7 - If the new playback position is less than the earliest possible position, let it be that position instead.
2565 MediaTime earliestTime = m_player->startTime();
2566 time = std::max(time, earliestTime);
2568 // Ask the media engine for the time value in the movie's time scale before comparing with current time. This
2569 // is necessary because if the seek time is not equal to currentTime but the delta is less than the movie's
2570 // time scale, we will ask the media engine to "seek" to the current movie time, which may be a noop and
2571 // not generate a timechanged callback. This means m_seeking will never be cleared and we will never
2572 // fire a 'seeked' event.
2574 MediaTime mediaTime = m_player->mediaTimeForTimeValue(time);
2575 if (time != mediaTime)
2576 LOG(Media, "HTMLMediaElement::seekTask(%p) - %s - media timeline equivalent is %s", this, toString(time).utf8().data(), toString(mediaTime).utf8().data());
2578 time = m_player->mediaTimeForTimeValue(time);
2580 // 8 - If the (possibly now changed) new playback position is not in one of the ranges given in the
2581 // seekable attribute, then let it be the position in one of the ranges given in the seekable attribute
2582 // that is the nearest to the new playback position. ... If there are no ranges given in the seekable
2583 // attribute then set the seeking IDL attribute to false and abort these steps.
2584 RefPtr<TimeRanges> seekableRanges = seekable();
2585 bool noSeekRequired = !seekableRanges->length();
2587 // Short circuit seeking to the current time by just firing the events if no seek is required.
2588 // Don't skip calling the media engine if 1) we are in poster mode (because a seek should always cancel
2589 // poster display), or 2) if there is a pending fast seek, or 3) if this seek is not an exact seek
2590 SeekType thisSeekType = (negativeTolerance == MediaTime::zeroTime() && positiveTolerance == MediaTime::zeroTime()) ? Precise : Fast;
2591 if (!noSeekRequired && time == now && thisSeekType == Precise && m_pendingSeekType != Fast && displayMode() != Poster)
2592 noSeekRequired = true;
2594 #if ENABLE(MEDIA_SOURCE)
2595 // Always notify the media engine of a seek if the source is not closed. This ensures that the source is
2596 // always in a flushed state when the 'seeking' event fires.
2597 if (m_mediaSource && !m_mediaSource->isClosed())
2598 noSeekRequired = false;
2601 if (noSeekRequired) {
2602 LOG(Media, "HTMLMediaElement::seekTask(%p) - seek to %s ignored", this, toString(time).utf8().data());
2604 scheduleEvent(eventNames().seekingEvent);
2605 scheduleTimeupdateEvent(false);
2606 scheduleEvent(eventNames().seekedEvent);
2611 time = seekableRanges->ranges().nearest(time);
2613 m_sentEndEvent = false;
2614 m_lastSeekTime = time;
2615 m_pendingSeekType = thisSeekType;
2618 // 10 - Queue a task to fire a simple event named seeking at the element.
2619 scheduleEvent(eventNames().seekingEvent);
2621 // 11 - Set the current playback position to the given new playback position
2622 m_player->seekWithTolerance(time, negativeTolerance, positiveTolerance);
2624 // 12 - Wait until the user agent has established whether or not the media data for the new playback
2625 // position is available, and, if it is, until it has decoded enough data to play back that position.
2626 // 13 - Await a stable state. The synchronous section consists of all the remaining steps of this algorithm.
2629 void HTMLMediaElement::clearSeeking()
2632 m_pendingSeekType = NoSeek;
2633 invalidateCachedTime();
2636 void HTMLMediaElement::finishSeek()
2639 // 14 - Set the seeking IDL attribute to false.
2642 LOG(Media, "HTMLMediaElement::finishSeek(%p) - current time = %s", this, toString(currentMediaTime()).utf8().data());
2644 // 15 - Run the time maches on steps.
2645 // Handled by mediaPlayerTimeChanged().
2647 // 16 - Queue a task to fire a simple event named timeupdate at the element.
2648 scheduleEvent(eventNames().timeupdateEvent);
2650 // 17 - Queue a task to fire a simple event named seeked at the element.
2651 scheduleEvent(eventNames().seekedEvent);
2653 #if ENABLE(MEDIA_SOURCE)
2655 m_mediaSource->monitorSourceBuffers();
2659 HTMLMediaElement::ReadyState HTMLMediaElement::readyState() const
2661 return m_readyState;
2664 MediaPlayer::MovieLoadType HTMLMediaElement::movieLoadType() const
2666 return m_player ? m_player->movieLoadType() : MediaPlayer::Unknown;
2669 bool HTMLMediaElement::hasAudio() const
2671 return m_player ? m_player->hasAudio() : false;
2674 bool HTMLMediaElement::seeking() const
2679 void HTMLMediaElement::refreshCachedTime() const
2684 m_cachedTime = m_player->currentTime();
2685 if (!m_cachedTime) {
2686 // Do not use m_cachedTime until the media engine returns a non-zero value because we can't
2687 // estimate current time until playback actually begins.
2688 invalidateCachedTime();
2692 m_clockTimeAtLastCachedTimeUpdate = monotonicallyIncreasingTime();
2695 void HTMLMediaElement::invalidateCachedTime() const
2697 m_cachedTime = MediaTime::invalidTime();
2698 if (!m_player || !m_player->maximumDurationToCacheMediaTime())
2702 if (m_cachedTime.isValid())
2703 LOG(Media, "HTMLMediaElement::invalidateCachedTime(%p)", this);
2706 // Don't try to cache movie time when playback first starts as the time reported by the engine
2707 // sometimes fluctuates for a short amount of time, so the cached time will be off if we take it
2709 static const double minimumTimePlayingBeforeCacheSnapshot = 0.5;
2711 m_minimumClockTimeToUpdateCachedTime = monotonicallyIncreasingTime() + minimumTimePlayingBeforeCacheSnapshot;
2715 double HTMLMediaElement::currentTime() const
2717 return currentMediaTime().toDouble();
2720 MediaTime HTMLMediaElement::currentMediaTime() const
2722 #if LOG_CACHED_TIME_WARNINGS
2723 static const MediaTime minCachedDeltaForWarning = MediaTime::create(1, 100);
2727 return MediaTime::zeroTime();
2730 LOG(Media, "HTMLMediaElement::currentTime(%p) - seeking, returning %s", this, toString(m_lastSeekTime).utf8().data());
2731 return m_lastSeekTime;
2734 if (m_cachedTime.isValid() && m_paused) {
2735 #if LOG_CACHED_TIME_WARNINGS
2736 MediaTime delta = m_cachedTime - m_player->currentTime();
2737 if (delta > minCachedDeltaForWarning)
2738 LOG(Media, "HTMLMediaElement::currentTime(%p) - WARNING, cached time is %s seconds off of media time when paused", this, toString(delta).utf8().data());
2740 return m_cachedTime;
2743 // Is it too soon use a cached time?
2744 double now = monotonicallyIncreasingTime();
2745 double maximumDurationToCacheMediaTime = m_player->maximumDurationToCacheMediaTime();
2747 if (maximumDurationToCacheMediaTime && m_cachedTime.isValid() && !m_paused && now > m_minimumClockTimeToUpdateCachedTime) {
2748 double clockDelta = now - m_clockTimeAtLastCachedTimeUpdate;
2750 // Not too soon, use the cached time only if it hasn't expired.
2751 if (clockDelta < maximumDurationToCacheMediaTime) {
2752 MediaTime adjustedCacheTime = m_cachedTime + MediaTime::createWithDouble(effectivePlaybackRate() * clockDelta);
2754 #if LOG_CACHED_TIME_WARNINGS
2755 MediaTime delta = adjustedCacheTime - m_player->currentTime();
2756 if (delta > minCachedDeltaForWarning)
2757 LOG(Media, "HTMLMediaElement::currentTime(%p) - WARNING, cached time is %f seconds off of media time when playing", this, delta);
2759 return adjustedCacheTime;
2763 #if LOG_CACHED_TIME_WARNINGS
2764 if (maximumDurationToCacheMediaTime && now > m_minimumClockTimeToUpdateCachedTime && m_cachedTime != MediaPlayer::invalidTime()) {
2765 double clockDelta = now - m_clockTimeAtLastCachedTimeUpdate;
2766 MediaTime delta = m_cachedTime + MediaTime::createWithDouble(effectivePlaybackRate() * clockDelta) - m_player->currentTime();
2767 LOG(Media, "HTMLMediaElement::currentTime(%p) - cached time was %s seconds off of media time when it expired", this, toString(delta).utf8().data());
2771 refreshCachedTime();
2773 if (m_cachedTime.isInvalid())
2774 return MediaTime::zeroTime();
2776 return m_cachedTime;
2779 void HTMLMediaElement::setCurrentTime(double time)
2781 setCurrentTime(MediaTime::createWithDouble(time));
2784 void HTMLMediaElement::setCurrentTime(const MediaTime& time)
2786 if (m_mediaController)
2792 void HTMLMediaElement::setCurrentTime(double time, ExceptionCode& ec)
2794 // On setting, if the media element has a current media controller, then the user agent must
2795 // throw an InvalidStateError exception
2796 if (m_mediaController) {
2797 ec = INVALID_STATE_ERR;
2801 seek(MediaTime::createWithDouble(time));
2804 double HTMLMediaElement::duration() const
2806 return durationMediaTime().toDouble();
2809 MediaTime HTMLMediaElement::durationMediaTime() const
2811 if (m_player && m_readyState >= HAVE_METADATA)
2812 return m_player->duration();
2814 return MediaTime::invalidTime();
2817 bool HTMLMediaElement::paused() const
2819 // As of this writing, JavaScript garbage collection calls this function directly. In the past
2820 // we had problems where this was called on an object after a bad cast. The assertion below
2821 // made our regression test detect the problem, so we should keep it because of that. But note
2822 // that the value of the assertion relies on the compiler not being smart enough to know that
2823 // isHTMLUnknownElement is guaranteed to return false for an HTMLMediaElement.
2824 ASSERT(!isHTMLUnknownElement());
2829 double HTMLMediaElement::defaultPlaybackRate() const
2831 #if ENABLE(MEDIA_STREAM)
2832 // http://w3c.github.io/mediacapture-main/#mediastreams-in-media-elements
2833 // "defaultPlaybackRate" - On setting: ignored. On getting: return 1.0
2834 // A MediaStream is not seekable. Therefore, this attribute must always have the
2835 // value 1.0 and any attempt to alter it must be ignored. Note that this also means
2836 // that the ratechange event will not fire.
2837 if (m_mediaStreamSrcObject)
2841 return m_defaultPlaybackRate;
2844 void HTMLMediaElement::setDefaultPlaybackRate(double rate)
2846 #if ENABLE(MEDIA_STREAM)
2847 // http://w3c.github.io/mediacapture-main/#mediastreams-in-media-elements
2848 // "defaultPlaybackRate" - On setting: ignored. On getting: return 1.0
2849 // A MediaStream is not seekable. Therefore, this attribute must always have the
2850 // value 1.0 and any attempt to alter it must be ignored. Note that this also means
2851 // that the ratechange event will not fire.
2852 if (m_mediaStreamSrcObject)
2856 if (m_defaultPlaybackRate != rate) {
2857 LOG(Media, "HTMLMediaElement::setDefaultPlaybackRate(%p) - %f", this, rate);
2858 m_defaultPlaybackRate = rate;
2859 scheduleEvent(eventNames().ratechangeEvent);
2863 double HTMLMediaElement::effectivePlaybackRate() const
2865 return m_mediaController ? m_mediaController->playbackRate() : m_reportedPlaybackRate;
2868 double HTMLMediaElement::requestedPlaybackRate() const
2870 return m_mediaController ? m_mediaController->playbackRate() : m_requestedPlaybackRate;
2873 double HTMLMediaElement::playbackRate() const
2875 #if ENABLE(MEDIA_STREAM)
2876 // http://w3c.github.io/mediacapture-main/#mediastreams-in-media-elements
2877 // "playbackRate" - A MediaStream is not seekable. Therefore, this attribute must always
2878 // have the value 1.0 and any attempt to alter it must be ignored. Note that this also
2879 // means that the ratechange event will not fire.
2880 if (m_mediaStreamSrcObject)
2884 return m_requestedPlaybackRate;
2887 void HTMLMediaElement::setPlaybackRate(double rate)
2889 LOG(Media, "HTMLMediaElement::setPlaybackRate(%p) - %f", this, rate);
2891 #if ENABLE(MEDIA_STREAM)
2892 // http://w3c.github.io/mediacapture-main/#mediastreams-in-media-elements
2893 // "playbackRate" - A MediaStream is not seekable. Therefore, this attribute must always
2894 // have the value 1.0 and any attempt to alter it must be ignored. Note that this also
2895 // means that the ratechange event will not fire.
2896 if (m_mediaStreamSrcObject)
2900 if (m_player && potentiallyPlaying() && m_player->rate() != rate && !m_mediaController)
2901 m_player->setRate(rate);
2903 if (m_requestedPlaybackRate != rate) {
2904 m_reportedPlaybackRate = m_requestedPlaybackRate = rate;
2905 invalidateCachedTime();
2906 scheduleEvent(eventNames().ratechangeEvent);
2910 void HTMLMediaElement::updatePlaybackRate()
2912 double requestedRate = requestedPlaybackRate();
2913 if (m_player && potentiallyPlaying() && m_player->rate() != requestedRate)
2914 m_player->setRate(requestedRate);
2917 bool HTMLMediaElement::webkitPreservesPitch() const
2919 return m_webkitPreservesPitch;
2922 void HTMLMediaElement::setWebkitPreservesPitch(bool preservesPitch)
2924 LOG(Media, "HTMLMediaElement::setWebkitPreservesPitch(%p) - %s", this, boolString(preservesPitch));
2926 m_webkitPreservesPitch = preservesPitch;
2931 m_player->setPreservesPitch(preservesPitch);
2934 bool HTMLMediaElement::ended() const
2936 #if ENABLE(MEDIA_STREAM)
2937 // http://w3c.github.io/mediacapture-main/#mediastreams-in-media-elements
2938 // When the MediaStream state moves from the active to the inactive state, the User Agent
2939 // must raise an ended event on the HTMLMediaElement and set its ended attribute to true.
2940 if (m_mediaStreamSrcObject && m_player && m_player->ended())
2944 // 4.8.10.8 Playing the media resource
2945 // The ended attribute must return true if the media element has ended
2946 // playback and the direction of playback is forwards, and false otherwise.
2947 return endedPlayback() && requestedPlaybackRate() > 0;
2950 bool HTMLMediaElement::autoplay() const
2952 return fastHasAttribute(autoplayAttr);
2955 String HTMLMediaElement::preload() const
2957 #if ENABLE(MEDIA_STREAM)
2958 // http://w3c.github.io/mediacapture-main/#mediastreams-in-media-elements
2959 // "preload" - On getting: none. On setting: ignored.
2960 if (m_mediaStreamSrcObject)
2961 return ASCIILiteral("none");
2964 switch (m_preload) {
2965 case MediaPlayer::None:
2966 return ASCIILiteral("none");
2967 case MediaPlayer::MetaData:
2968 return ASCIILiteral("metadata");
2969 case MediaPlayer::Auto:
2970 return ASCIILiteral("auto");
2973 ASSERT_NOT_REACHED();
2977 void HTMLMediaElement::setPreload(const String& preload)
2979 LOG(Media, "HTMLMediaElement::setPreload(%p) - %s", this, preload.utf8().data());
2980 #if ENABLE(MEDIA_STREAM)
2981 // http://w3c.github.io/mediacapture-main/#mediastreams-in-media-elements
2982 // "preload" - On getting: none. On setting: ignored.
2983 if (m_mediaStreamSrcObject)
2987 setAttribute(preloadAttr, preload);
2990 void HTMLMediaElement::play()
2992 LOG(Media, "HTMLMediaElement::play(%p)", this);
2994 if (!m_mediaSession->playbackPermitted(*this))
2996 if (ScriptController::processingUserGestureForMedia())
2997 removeBehaviorsRestrictionsAfterFirstUserGesture();
3002 void HTMLMediaElement::playInternal()
3004 LOG(Media, "HTMLMediaElement::playInternal(%p)", this);
3006 if (!m_mediaSession->clientWillBeginPlayback()) {
3007 LOG(Media, " returning because of interruption");
3011 // 4.8.10.9. Playing the media resource
3012 if (!m_player || m_networkState == NETWORK_EMPTY)
3013 scheduleDelayedAction(LoadMediaResource);
3015 if (endedPlayback())
3016 seekInternal(MediaTime::zeroTime());
3018 if (m_mediaController)
3019 m_mediaController->bringElementUpToSpeed(this);
3023 invalidateCachedTime();
3024 scheduleEvent(eventNames().playEvent);
3026 if (m_readyState <= HAVE_CURRENT_DATA)
3027 scheduleEvent(eventNames().waitingEvent);
3028 else if (m_readyState >= HAVE_FUTURE_DATA)
3029 scheduleEvent(eventNames().playingEvent);
3031 #if ENABLE(MEDIA_SESSION)
3032 // 6.3 Activating a media session from a media element
3033 // When the play() method is invoked, the paused attribute is true, and the readyState attribute has the value
3034 // HAVE_FUTURE_DATA or HAVE_ENOUGH_DATA, then
3035 // 1. Let media session be the value of the current media session.
3036 // 2. If we are not currently in media session's list of active participating media elements then append
3037 // ourselves to this list.
3038 // 3. Let activated be the result of running the media session invocation algorithm for media session.
3039 // 4. If activated is failure, pause ourselves.
3040 if (m_readyState == HAVE_ENOUGH_DATA || m_readyState == HAVE_FUTURE_DATA) {
3042 m_session->addActiveMediaElement(*this);
3044 if (m_session->kind() == MediaSessionKind::Content) {
3045 if (Page* page = document().page())
3046 page->chrome().client().focusedContentMediaElementDidChange(m_elementID);
3049 if (!m_session->invoke()) {
3057 m_autoplaying = false;
3059 updateMediaController();
3062 void HTMLMediaElement::pause()
3064 LOG(Media, "HTMLMediaElement::pause(%p)", this);
3066 if (!m_mediaSession->playbackPermitted(*this))
3073 void HTMLMediaElement::pauseInternal()
3075 LOG(Media, "HTMLMediaElement::pauseInternal(%p)", this);
3077 if (!m_mediaSession->clientWillPausePlayback()) {
3078 LOG(Media, " returning because of interruption");
3082 // 4.8.10.9. Playing the media resource
3083 if (!m_player || m_networkState == NETWORK_EMPTY) {
3084 // Unless the restriction on media requiring user action has been lifted
3085 // don't trigger loading if a script calls pause().
3086 if (!m_mediaSession->playbackPermitted(*this))
3088 scheduleDelayedAction(LoadMediaResource);
3091 m_autoplaying = false;
3095 scheduleTimeupdateEvent(false);
3096 scheduleEvent(eventNames().pauseEvent);
3098 if (MemoryPressureHandler::singleton().isUnderMemoryPressure())
3099 purgeBufferedDataIfPossible();
3105 #if ENABLE(MEDIA_SOURCE)
3106 void HTMLMediaElement::closeMediaSource()
3111 m_mediaSource->close();
3112 m_mediaSource = nullptr;
3116 #if ENABLE(ENCRYPTED_MEDIA)
3117 void HTMLMediaElement::webkitGenerateKeyRequest(const String& keySystem, const RefPtr<Uint8Array>& initData, ExceptionCode& ec)
3119 #if ENABLE(ENCRYPTED_MEDIA_V2)
3120 static bool firstTime = true;
3121 if (firstTime && scriptExecutionContext()) {
3122 scriptExecutionContext()->addConsoleMessage(MessageSource::JS, MessageLevel::Warning, ASCIILiteral("'HTMLMediaElement.webkitGenerateKeyRequest()' is deprecated. Use 'MediaKeys.createSession()' instead."));
3127 if (keySystem.isEmpty()) {
3133 ec = INVALID_STATE_ERR;
3137 const unsigned char* initDataPointer = nullptr;
3138 unsigned initDataLength = 0;
3140 initDataPointer = initData->data();
3141 initDataLength = initData->length();
3144 MediaPlayer::MediaKeyException result = m_player->generateKeyRequest(keySystem, initDataPointer, initDataLength);
3145 ec = exceptionCodeForMediaKeyException(result);
3148 void HTMLMediaElement::webkitAddKey(const String& keySystem, Uint8Array& key, const RefPtr<Uint8Array>& initData, const String& sessionId, ExceptionCode& ec)
3150 #if ENABLE(ENCRYPTED_MEDIA_V2)
3151 static bool firstTime = true;
3152 if (firstTime && scriptExecutionContext()) {
3153 scriptExecutionContext()->addConsoleMessage(MessageSource::JS, MessageLevel::Warning, ASCIILiteral("'HTMLMediaElement.webkitAddKey()' is deprecated. Use 'MediaKeySession.update()' instead."));
3158 if (keySystem.isEmpty()) {
3163 if (!key.length()) {
3164 ec = TYPE_MISMATCH_ERR;
3169 ec = INVALID_STATE_ERR;
3173 const unsigned char* initDataPointer = nullptr;
3174 unsigned initDataLength = 0;
3176 initDataPointer = initData->data();
3177 initDataLength = initData->length();
3180 MediaPlayer::MediaKeyException result = m_player->addKey(keySystem, key.data(), key.length(), initDataPointer, initDataLength, sessionId);
3181 ec = exceptionCodeForMediaKeyException(result);
3184 void HTMLMediaElement::webkitCancelKeyRequest(const String& keySystem, const String& sessionId, ExceptionCode& ec)
3186 if (keySystem.isEmpty()) {
3192 ec = INVALID_STATE_ERR;
3196 MediaPlayer::MediaKeyException result = m_player->cancelKeyRequest(keySystem, sessionId);
3197 ec = exceptionCodeForMediaKeyException(result);
3202 bool HTMLMediaElement::loop() const
3204 return fastHasAttribute(loopAttr);
3207 void HTMLMediaElement::setLoop(bool b)
3209 LOG(Media, "HTMLMediaElement::setLoop(%p) - %s", this, boolString(b));
3210 setBooleanAttribute(loopAttr, b);
3213 bool HTMLMediaElement::controls() const
3215 Frame* frame = document().frame();
3217 // always show controls when scripting is disabled
3218 if (frame && !frame->script().canExecuteScripts(NotAboutToExecuteScript))
3221 return fastHasAttribute(controlsAttr);
3224 void HTMLMediaElement::setControls(bool b)
3226 LOG(Media, "HTMLMediaElement::setControls(%p) - %s", this, boolString(b));
3227 setBooleanAttribute(controlsAttr, b);
3230 double HTMLMediaElement::volume() const
3235 void HTMLMediaElement::setVolume(double vol, ExceptionCode& ec)
3237 LOG(Media, "HTMLMediaElement::setVolume(%p) - %f", this, vol);
3239 if (vol < 0.0f || vol > 1.0f) {
3240 ec = INDEX_SIZE_ERR;
3245 if (m_volume != vol) {
3247 m_volumeInitialized = true;
3249 scheduleEvent(eventNames().volumechangeEvent);
3254 bool HTMLMediaElement::muted() const
3256 return m_explicitlyMuted ? m_muted : fastHasAttribute(mutedAttr);
3259 void HTMLMediaElement::setMuted(bool muted)
3261 LOG(Media, "HTMLMediaElement::setMuted(%p) - %s", this, boolString(muted));
3264 UNUSED_PARAM(muted);
3266 if (m_muted != muted || !m_explicitlyMuted) {
3268 m_explicitlyMuted = true;
3269 // Avoid recursion when the player reports volume changes.
3270 if (!processingMediaPlayerCallback()) {
3272 m_player->setMuted(effectiveMuted());
3273 if (hasMediaControls())
3274 mediaControls()->changedMute();
3277 scheduleEvent(eventNames().volumechangeEvent);
3279 #if ENABLE(MEDIA_SESSION)
3280 document().updateIsPlayingMedia(m_elementID);
3282 document().updateIsPlayingMedia();
3285 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
3286 updateMediaState(UpdateMediaState::Asynchronously);
3292 void HTMLMediaElement::togglePlayState()
3294 LOG(Media, "HTMLMediaElement::togglePlayState(%p) - canPlay() is %s", this, boolString(canPlay()));
3296 // We can safely call the internal play/pause methods, which don't check restrictions, because
3297 // this method is only called from the built-in media controller
3299 updatePlaybackRate();
3305 void HTMLMediaElement::beginScrubbing()
3307 LOG(Media, "HTMLMediaElement::beginScrubbing(%p) - paused() is %s", this, boolString(paused()));
3311 // Because a media element stays in non-paused state when it reaches end, playback resumes
3312 // when the slider is dragged from the end to another position unless we pause first. Do
3313 // a "hard pause" so an event is generated, since we want to stay paused after scrubbing finishes.
3316 // Not at the end but we still want to pause playback so the media engine doesn't try to
3317 // continue playing during scrubbing. Pause without generating an event as we will
3318 // unpause after scrubbing finishes.
3319 setPausedInternal(true);
3324 void HTMLMediaElement::endScrubbing()
3326 LOG(Media, "HTMLMediaElement::endScrubbing(%p) - m_pausedInternal is %s", this, boolString(m_pausedInternal));
3328 if (m_pausedInternal)
3329 setPausedInternal(false);
3332 void HTMLMediaElement::beginScanning(ScanDirection direction)
3334 m_scanType = supportsScanning() ? Scan : Seek;
3335 m_scanDirection = direction;
3337 if (m_scanType == Seek) {
3338 // Scanning by seeking requires the video to be paused during scanning.
3339 m_actionAfterScan = paused() ? Nothing : Play;
3342 // Scanning by scanning requires the video to be playing during scanninging.
3343 m_actionAfterScan = paused() ? Pause : Nothing;
3345 setPlaybackRate(nextScanRate());
3348 m_scanTimer.start(0, m_scanType == Seek ? SeekRepeatDelay : ScanRepeatDelay);
3351 void HTMLMediaElement::endScanning()
3353 if (m_scanType == Scan)
3354 setPlaybackRate(defaultPlaybackRate());
3356 if (m_actionAfterScan == Play)
3358 else if (m_actionAfterScan == Pause)
3361 if (m_scanTimer.isActive())
3365 double HTMLMediaElement::nextScanRate()
3367 double rate = std::min(ScanMaximumRate, fabs(playbackRate() * 2));
3368 if (m_scanDirection == Backward)
3371 rate = std::min(std::max(rate, minFastReverseRate()), maxFastForwardRate());
3376 void HTMLMediaElement::scanTimerFired()
3378 if (m_scanType == Seek) {
3379 double seekTime = m_scanDirection == Forward ? SeekTime : -SeekTime;
3380 setCurrentTime(currentTime() + seekTime);
3382 setPlaybackRate(nextScanRate());
3385 // The spec says to fire periodic timeupdate events (those sent while playing) every
3386 // "15 to 250ms", we choose the slowest frequency
3387 static const double maxTimeupdateEventFrequency = 0.25;
3389 void HTMLMediaElement::startPlaybackProgressTimer()
3391 if (m_playbackProgressTimer.isActive())
3394 m_previousProgressTime = monotonicallyIncreasingTime();
3395 m_playbackProgressTimer.startRepeating(maxTimeupdateEventFrequency);
3398 void HTMLMediaElement::playbackProgressTimerFired()
3402 if (m_fragmentEndTime.isValid() && currentMediaTime() >= m_fragmentEndTime && requestedPlaybackRate() > 0) {
3403 m_fragmentEndTime = MediaTime::invalidTime();
3404 if (!m_mediaController && !m_paused) {
3405 // changes paused to true and fires a simple event named pause at the media element.
3410 scheduleTimeupdateEvent(true);
3412 if (!requestedPlaybackRate())
3415 if (!m_paused && hasMediaControls())
3416 mediaControls()->playbackProgressed();
3418 #if ENABLE(VIDEO_TRACK)
3419 if (RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
3420 updateActiveTextTrackCues(currentMediaTime());
3423 #if ENABLE(MEDIA_SOURCE)
3425 m_mediaSource->monitorSourceBuffers();
3429 void HTMLMediaElement::scheduleTimeupdateEvent(bool periodicEvent)
3431 double now = monotonicallyIncreasingTime();
3432 double timedelta = now - m_clockTimeAtLastUpdateEvent;
3434 // throttle the periodic events
3435 if (periodicEvent && timedelta < maxTimeupdateEventFrequency)
3438 // Some media engines make multiple "time changed" callbacks at the same time, but we only want one
3439 // event at a given time so filter here
3440 MediaTime movieTime = currentMediaTime();
3441 if (movieTime != m_lastTimeUpdateEventMovieTime) {
3442 scheduleEvent(eventNames().timeupdateEvent);
3443 m_clockTimeAtLastUpdateEvent = now;
3444 m_lastTimeUpdateEventMovieTime = movieTime;
3448 bool HTMLMediaElement::canPlay() const
3450 return paused() || ended() || m_readyState < HAVE_METADATA;
3453 double HTMLMediaElement::percentLoaded() const
3457 MediaTime duration = m_player->duration();
3459 if (!duration || duration.isPositiveInfinite() || duration.isNegativeInfinite())
3462 MediaTime buffered = MediaTime::zeroTime();
3464 std::unique_ptr<PlatformTimeRanges> timeRanges = m_player->buffered();
3465 for (unsigned i = 0; i < timeRanges->length(); ++i) {
3466 MediaTime start = timeRanges->start(i, ignored);
3467 MediaTime end = timeRanges->end(i, ignored);
3468 buffered += end - start;
3470 return buffered.toDouble() / duration.toDouble();
3473 #if ENABLE(VIDEO_TRACK)
3475 void HTMLMediaElement::mediaPlayerDidAddAudioTrack(PassRefPtr<AudioTrackPrivate> prpTrack)
3477 if (isPlaying() && !m_mediaSession->playbackPermitted(*this))
3480 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
3483 addAudioTrack(AudioTrack::create(this, prpTrack));
3486 void HTMLMediaElement::mediaPlayerDidAddTextTrack(PassRefPtr<InbandTextTrackPrivate> prpTrack)
3488 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
3491 // 4.8.10.12.2 Sourcing in-band text tracks
3492 // 1. Associate the relevant data with a new text track and its corresponding new TextTrack object.
3493 RefPtr<InbandTextTrack> textTrack = InbandTextTrack::create(ActiveDOMObject::scriptExecutionContext(), this, prpTrack);
3494 textTrack->setMediaElement(this);
3496 // 2. Set the new text track's kind, label, and language based on the semantics of the relevant data,
3497 // as defined by the relevant specification. If there is no label in that data, then the label must
3498 // be set to the empty string.
3499 // 3. Associate the text track list of cues with the rules for updating the text track rendering appropriate
3500 // for the format in question.
3501 // 4. If the new text track's kind is metadata, then set the text track in-band metadata track dispatch type
3502 // as follows, based on the type of the media resource:
3503 // 5. Populate the new text track's list of cues with the cues parsed so far, folllowing the guidelines for exposing
3504 // cues, and begin updating it dynamically as necessary.
3505 // - Thess are all done by the media engine.
3507 // 6. Set the new text track's readiness state to loaded.