2 * Copyright (C) 2007-2014 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 #include "HTMLMediaElement.h"
30 #include "ApplicationCacheHost.h"
31 #include "ApplicationCacheResource.h"
32 #include "Attribute.h"
33 #include "CSSPropertyNames.h"
34 #include "CSSValueKeywords.h"
35 #include "ChromeClient.h"
36 #include "ClientRect.h"
37 #include "ClientRectList.h"
38 #include "ContentSecurityPolicy.h"
39 #include "ContentType.h"
40 #include "CookieJar.h"
41 #include "DiagnosticLoggingClient.h"
42 #include "DiagnosticLoggingKeys.h"
43 #include "DisplaySleepDisabler.h"
44 #include "DocumentLoader.h"
45 #include "ElementIterator.h"
46 #include "EventNames.h"
47 #include "ExceptionCodePlaceholder.h"
48 #include "FrameLoader.h"
49 #include "FrameLoaderClient.h"
50 #include "FrameView.h"
51 #include "HTMLSourceElement.h"
52 #include "HTMLVideoElement.h"
53 #include "JSHTMLMediaElement.h"
56 #include "MIMETypeRegistry.h"
57 #include "MainFrame.h"
58 #include "MediaController.h"
59 #include "MediaControls.h"
60 #include "MediaDocument.h"
61 #include "MediaError.h"
62 #include "MediaFragmentURIParser.h"
63 #include "MediaKeyEvent.h"
64 #include "MediaList.h"
65 #include "MediaPlayer.h"
66 #include "MediaQueryEvaluator.h"
67 #include "MediaResourceLoader.h"
68 #include "NetworkingContext.h"
69 #include "PageGroup.h"
70 #include "PageThrottler.h"
71 #include "PlatformMediaSessionManager.h"
72 #include "ProgressTracker.h"
73 #include "RenderLayerCompositor.h"
74 #include "RenderVideo.h"
75 #include "RenderView.h"
76 #include "ResourceLoadInfo.h"
77 #include "ScriptController.h"
78 #include "ScriptSourceCode.h"
79 #include "SecurityPolicy.h"
80 #include "SessionID.h"
82 #include "ShadowRoot.h"
83 #include "TimeRanges.h"
84 #include "UserContentController.h"
86 #include <runtime/Uint8Array.h>
87 #include <wtf/CurrentTime.h>
88 #include <wtf/MathExtras.h>
90 #include <wtf/text/CString.h>
92 #if ENABLE(VIDEO_TRACK)
93 #include "AudioTrackList.h"
94 #include "HTMLTrackElement.h"
95 #include "InbandGenericTextTrack.h"
96 #include "InbandTextTrackPrivate.h"
97 #include "InbandWebVTTTextTrack.h"
98 #include "RuntimeEnabledFeatures.h"
99 #include "TextTrackCueList.h"
100 #include "TextTrackList.h"
101 #include "VideoTrackList.h"
104 #if ENABLE(WEB_AUDIO)
105 #include "AudioSourceProvider.h"
106 #include "MediaElementAudioSourceNode.h"
110 #include "RuntimeApplicationChecksIOS.h"
113 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
114 #include "WebKitPlaybackTargetAvailabilityEvent.h"
117 #if ENABLE(MEDIA_SESSION)
118 #include "MediaSession.h"
121 #if ENABLE(MEDIA_SOURCE)
122 #include "DOMWindow.h"
123 #include "MediaSource.h"
124 #include "Performance.h"
125 #include "VideoPlaybackQuality.h"
128 #if ENABLE(MEDIA_STREAM)
129 #include "MediaStream.h"
130 #include "MediaStreamRegistry.h"
133 #if ENABLE(ENCRYPTED_MEDIA_V2)
134 #include "MediaKeyNeededEvent.h"
135 #include "MediaKeys.h"
138 #if USE(PLATFORM_TEXT_TRACK_MENU)
139 #include "PlatformTextTrack.h"
142 #if ENABLE(MEDIA_CONTROLS_SCRIPT)
143 #include "JSMediaControlsHost.h"
144 #include "MediaControlsHost.h"
145 #include "ScriptGlobalObject.h"
146 #include <bindings/ScriptObject.h>
151 static const double SeekRepeatDelay = 0.1;
152 static const double SeekTime = 0.2;
153 static const double ScanRepeatDelay = 1.5;
154 static const double ScanMaximumRate = 8;
156 static void setFlags(unsigned& value, unsigned flags)
161 static void clearFlags(unsigned& value, unsigned flags)
167 static String urlForLoggingMedia(const URL& url)
169 static const unsigned maximumURLLengthForLogging = 128;
171 if (url.string().length() < maximumURLLengthForLogging)
173 return url.string().substring(0, maximumURLLengthForLogging) + "...";
176 static const char* boolString(bool val)
178 return val ? "true" : "false";
182 #ifndef LOG_MEDIA_EVENTS
183 // Default to not logging events because so many are generated they can overwhelm the rest of
185 #define LOG_MEDIA_EVENTS 0
188 #ifndef LOG_CACHED_TIME_WARNINGS
189 // Default to not logging warnings about excessive drift in the cached media time because it adds a
190 // fair amount of overhead and logging.
191 #define LOG_CACHED_TIME_WARNINGS 0
194 #if ENABLE(MEDIA_SOURCE)
195 // URL protocol used to signal that the media source API is being used.
196 static const char* mediaSourceBlobProtocol = "blob";
199 using namespace HTMLNames;
201 typedef HashMap<Document*, HashSet<HTMLMediaElement*>> DocumentElementSetMap;
202 static DocumentElementSetMap& documentToElementSetMap()
204 DEPRECATED_DEFINE_STATIC_LOCAL(DocumentElementSetMap, map, ());
208 static void addElementToDocumentMap(HTMLMediaElement& element, Document& document)
210 DocumentElementSetMap& map = documentToElementSetMap();
211 HashSet<HTMLMediaElement*> set = map.take(&document);
213 map.add(&document, set);
216 static void removeElementFromDocumentMap(HTMLMediaElement& element, Document& document)
218 DocumentElementSetMap& map = documentToElementSetMap();
219 HashSet<HTMLMediaElement*> set = map.take(&document);
220 set.remove(&element);
222 map.add(&document, set);
225 #if ENABLE(ENCRYPTED_MEDIA)
226 static ExceptionCode exceptionCodeForMediaKeyException(MediaPlayer::MediaKeyException exception)
229 case MediaPlayer::NoError:
231 case MediaPlayer::InvalidPlayerState:
232 return INVALID_STATE_ERR;
233 case MediaPlayer::KeySystemNotSupported:
234 return NOT_SUPPORTED_ERR;
237 ASSERT_NOT_REACHED();
238 return INVALID_STATE_ERR;
242 #if ENABLE(VIDEO_TRACK)
243 class TrackDisplayUpdateScope {
245 TrackDisplayUpdateScope(HTMLMediaElement* mediaElement)
247 m_mediaElement = mediaElement;
248 m_mediaElement->beginIgnoringTrackDisplayUpdateRequests();
250 ~TrackDisplayUpdateScope()
252 ASSERT(m_mediaElement);
253 m_mediaElement->endIgnoringTrackDisplayUpdateRequests();
257 HTMLMediaElement* m_mediaElement;
261 struct HTMLMediaElement::TrackGroup {
262 enum GroupKind { CaptionsAndSubtitles, Description, Chapter, Metadata, Other };
264 TrackGroup(GroupKind kind)
272 Vector<RefPtr<TextTrack>> tracks;
273 RefPtr<TextTrack> visibleTrack;
274 RefPtr<TextTrack> defaultTrack;
279 HTMLMediaElement::HTMLMediaElement(const QualifiedName& tagName, Document& document, bool createdByParser)
280 : HTMLElement(tagName, document)
281 , ActiveDOMObject(&document)
282 , m_pendingActionTimer(*this, &HTMLMediaElement::pendingActionTimerFired)
283 , m_progressEventTimer(*this, &HTMLMediaElement::progressEventTimerFired)
284 , m_playbackProgressTimer(*this, &HTMLMediaElement::playbackProgressTimerFired)
285 , m_scanTimer(*this, &HTMLMediaElement::scanTimerFired)
286 , m_seekTaskQueue(document)
287 , m_resizeTaskQueue(document)
288 , m_shadowDOMTaskQueue(document)
289 , m_playedTimeRanges()
290 , m_asyncEventQueue(*this)
291 , m_requestedPlaybackRate(1)
292 , m_reportedPlaybackRate(1)
293 , m_defaultPlaybackRate(1)
294 , m_webkitPreservesPitch(true)
295 , m_networkState(NETWORK_EMPTY)
296 , m_readyState(HAVE_NOTHING)
297 , m_readyStateMaximum(HAVE_NOTHING)
299 , m_volumeInitialized(false)
300 , m_previousProgressTime(std::numeric_limits<double>::max())
301 , m_clockTimeAtLastUpdateEvent(0)
302 , m_lastTimeUpdateEventMovieTime(MediaTime::positiveInfiniteTime())
303 , m_loadState(WaitingForSource)
304 , m_videoFullscreenMode(VideoFullscreenModeNone)
306 , m_videoFullscreenGravity(MediaPlayer::VideoGravityResizeAspect)
308 , m_preload(MediaPlayer::Auto)
309 , m_displayMode(Unknown)
310 , m_processingMediaPlayerCallback(0)
311 #if ENABLE(MEDIA_SOURCE)
312 , m_droppedVideoFrames(0)
314 , m_clockTimeAtLastCachedTimeUpdate(0)
315 , m_minimumClockTimeToUpdateCachedTime(0)
316 , m_pendingActionFlags(0)
317 , m_actionAfterScan(Nothing)
319 , m_scanDirection(Forward)
320 , m_firstTimePlaying(true)
322 , m_isWaitingUntilMediaCanStart(false)
323 , m_shouldDelayLoadEvent(false)
324 , m_haveFiredLoadedData(false)
325 , m_inActiveDocument(true)
326 , m_autoplaying(true)
328 , m_explicitlyMuted(false)
331 , m_sentStalledEvent(false)
332 , m_sentEndEvent(false)
333 , m_pausedInternal(false)
334 , m_sendProgressEvents(true)
335 , m_closedCaptionsVisible(false)
336 , m_webkitLegacyClosedCaptionOverride(false)
337 , m_completelyLoaded(false)
338 , m_havePreparedToPlay(false)
339 , m_parsingInProgress(createdByParser)
340 , m_elementIsHidden(document.hidden())
341 #if ENABLE(MEDIA_CONTROLS_SCRIPT)
342 , m_mediaControlsDependOnPageScaleFactor(false)
344 #if ENABLE(VIDEO_TRACK)
345 , m_tracksAreReady(true)
346 , m_haveVisibleTextTrack(false)
347 , m_processingPreferenceChange(false)
348 , m_lastTextTrackUpdateTime(MediaTime(-1, 1))
349 , m_captionDisplayMode(CaptionUserPreferences::Automatic)
353 , m_ignoreTrackDisplayUpdate(0)
355 #if ENABLE(WEB_AUDIO)
356 , m_audioSourceNode(0)
358 , m_mediaSession(std::make_unique<MediaElementSession>(*this))
359 , m_reportedExtraMemoryCost(0)
360 #if ENABLE(MEDIA_STREAM)
361 , m_mediaStreamSrcObject(nullptr)
364 LOG(Media, "HTMLMediaElement::HTMLMediaElement(%p)", this);
365 setHasCustomStyleResolveCallbacks();
367 m_mediaSession->addBehaviorRestriction(MediaElementSession::RequireUserGestureForFullscreen);
368 m_mediaSession->addBehaviorRestriction(MediaElementSession::RequirePageConsentToLoadMedia);
369 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
370 m_mediaSession->addBehaviorRestriction(MediaElementSession::RequireUserGestureToAutoplayToExternalDevice);
373 // FIXME: We should clean up and look to better merge the iOS and non-iOS code below.
374 Settings* settings = document.settings();
376 if (settings && settings->requiresUserGestureForMediaPlayback()) {
377 m_mediaSession->addBehaviorRestriction(MediaElementSession::RequireUserGestureForRateChange);
378 m_mediaSession->addBehaviorRestriction(MediaElementSession::RequireUserGestureForLoad);
381 m_sendProgressEvents = false;
382 if (!settings || settings->requiresUserGestureForMediaPlayback()) {
383 // Allow autoplay in a MediaDocument that is not in an iframe.
384 if (document.ownerElement() || !document.isMediaDocument())
385 m_mediaSession->addBehaviorRestriction(MediaElementSession::RequireUserGestureForRateChange);
386 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
387 m_mediaSession->addBehaviorRestriction(MediaElementSession::RequireUserGestureToShowPlaybackTargetPicker);
390 // Relax RequireUserGestureForFullscreen when requiresUserGestureForMediaPlayback is not set:
391 m_mediaSession->removeBehaviorRestriction(MediaElementSession::RequireUserGestureForFullscreen);
393 #endif // !PLATFORM(IOS)
395 if (settings && settings->audioPlaybackRequiresUserGesture())
396 m_mediaSession->addBehaviorRestriction(MediaElementSession::RequireUserGestureForAudioRateChange);
398 #if ENABLE(VIDEO_TRACK)
400 m_captionDisplayMode = document.page()->group().captionPreferences()->captionDisplayMode();
403 registerWithDocument(document);
406 HTMLMediaElement::~HTMLMediaElement()
408 LOG(Media, "HTMLMediaElement::~HTMLMediaElement(%p)", this);
410 m_asyncEventQueue.close();
412 setShouldDelayLoadEvent(false);
413 unregisterWithDocument(document());
415 #if ENABLE(VIDEO_TRACK)
417 m_audioTracks->clearElement();
418 for (unsigned i = 0; i < m_audioTracks->length(); ++i)
419 m_audioTracks->item(i)->clearClient();
422 m_textTracks->clearElement();
424 for (unsigned i = 0; i < m_textTracks->length(); ++i)
425 m_textTracks->item(i)->clearClient();
428 m_videoTracks->clearElement();
429 for (unsigned i = 0; i < m_videoTracks->length(); ++i)
430 m_videoTracks->item(i)->clearClient();
434 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
435 if (hasEventListeners(eventNames().webkitplaybacktargetavailabilitychangedEvent)) {
436 m_hasPlaybackTargetAvailabilityListeners = false;
437 m_mediaSession->setHasPlaybackTargetAvailabilityListeners(*this, false);
442 if (m_mediaController) {
443 m_mediaController->removeMediaElement(this);
444 m_mediaController = 0;
447 #if ENABLE(MEDIA_SOURCE)
451 #if ENABLE(ENCRYPTED_MEDIA_V2)
455 #if ENABLE(MEDIA_CONTROLS_SCRIPT)
457 m_isolatedWorld->clearWrappers();
460 #if ENABLE(MEDIA_SESSION)
462 m_session->removeMediaElement(*this);
467 m_seekTaskQueue.close();
469 m_completelyLoaded = true;
472 void HTMLMediaElement::registerWithDocument(Document& document)
474 m_mediaSession->registerWithDocument(document);
476 if (m_isWaitingUntilMediaCanStart)
477 document.addMediaCanStartListener(this);
480 document.registerForMediaVolumeCallbacks(this);
481 document.registerForPrivateBrowsingStateChangedCallbacks(this);
484 document.registerForVisibilityStateChangedCallbacks(this);
486 #if ENABLE(VIDEO_TRACK)
487 if (m_requireCaptionPreferencesChangedCallbacks)
488 document.registerForCaptionPreferencesChangedCallbacks(this);
491 #if ENABLE(MEDIA_CONTROLS_SCRIPT)
492 if (m_mediaControlsDependOnPageScaleFactor)
493 document.registerForPageScaleFactorChangedCallbacks(this);
496 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
497 document.registerForPageCacheSuspensionCallbacks(this);
500 document.addAudioProducer(this);
501 addElementToDocumentMap(*this, document);
504 void HTMLMediaElement::unregisterWithDocument(Document& document)
506 m_mediaSession->unregisterWithDocument(document);
508 if (m_isWaitingUntilMediaCanStart)
509 document.removeMediaCanStartListener(this);
512 document.unregisterForMediaVolumeCallbacks(this);
513 document.unregisterForPrivateBrowsingStateChangedCallbacks(this);
516 document.unregisterForVisibilityStateChangedCallbacks(this);
518 #if ENABLE(VIDEO_TRACK)
519 if (m_requireCaptionPreferencesChangedCallbacks)
520 document.unregisterForCaptionPreferencesChangedCallbacks(this);
523 #if ENABLE(MEDIA_CONTROLS_SCRIPT)
524 if (m_mediaControlsDependOnPageScaleFactor)
525 document.unregisterForPageScaleFactorChangedCallbacks(this);
528 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
529 document.unregisterForPageCacheSuspensionCallbacks(this);
532 document.removeAudioProducer(this);
533 removeElementFromDocumentMap(*this, document);
536 void HTMLMediaElement::didMoveToNewDocument(Document* oldDocument)
538 if (m_shouldDelayLoadEvent) {
540 oldDocument->decrementLoadEventDelayCount();
541 document().incrementLoadEventDelayCount();
545 unregisterWithDocument(*oldDocument);
547 registerWithDocument(document());
549 HTMLElement::didMoveToNewDocument(oldDocument);
552 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
553 void HTMLMediaElement::documentWillSuspendForPageCache()
555 m_mediaSession->unregisterWithDocument(document());
558 void HTMLMediaElement::documentDidResumeFromPageCache()
560 m_mediaSession->registerWithDocument(document());
564 bool HTMLMediaElement::hasCustomFocusLogic() const
569 bool HTMLMediaElement::supportsFocus() const
571 if (document().isMediaDocument())
574 // If no controls specified, we should still be able to focus the element if it has tabIndex.
575 return controls() || HTMLElement::supportsFocus();
578 bool HTMLMediaElement::isMouseFocusable() const
583 void HTMLMediaElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
585 if (name == srcAttr) {
587 // Note, unless the restriction on requiring user action has been removed,
588 // do not begin downloading data on iOS.
589 if (!value.isNull() && m_mediaSession->dataLoadingPermitted(*this))
591 // Trigger a reload, as long as the 'src' attribute is present.
595 clearMediaPlayer(LoadMediaResource);
596 scheduleDelayedAction(LoadMediaResource);
598 } else if (name == controlsAttr)
599 configureMediaControls();
600 else if (name == loopAttr)
601 updateSleepDisabling();
602 else if (name == preloadAttr) {
603 if (equalIgnoringCase(value, "none"))
604 m_preload = MediaPlayer::None;
605 else if (equalIgnoringCase(value, "metadata"))
606 m_preload = MediaPlayer::MetaData;
608 // The spec does not define an "invalid value default" but "auto" is suggested as the
609 // "missing value default", so use it for everything except "none" and "metadata"
610 m_preload = MediaPlayer::Auto;
613 // The attribute must be ignored if the autoplay attribute is present
614 if (!autoplay() && m_player)
615 m_player->setPreload(m_mediaSession->effectivePreloadForElement(*this));
617 } else if (name == mediagroupAttr)
618 setMediaGroup(value);
620 HTMLElement::parseAttribute(name, value);
623 void HTMLMediaElement::finishParsingChildren()
625 HTMLElement::finishParsingChildren();
626 m_parsingInProgress = false;
628 #if ENABLE(VIDEO_TRACK)
629 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
632 if (descendantsOfType<HTMLTrackElement>(*this).first())
633 scheduleDelayedAction(ConfigureTextTracks);
637 bool HTMLMediaElement::rendererIsNeeded(const RenderStyle& style)
639 return controls() && HTMLElement::rendererIsNeeded(style);
642 RenderPtr<RenderElement> HTMLMediaElement::createElementRenderer(Ref<RenderStyle>&& style, const RenderTreePosition&)
644 return createRenderer<RenderMedia>(*this, WTF::move(style));
647 bool HTMLMediaElement::childShouldCreateRenderer(const Node& child) const
649 #if ENABLE(MEDIA_CONTROLS_SCRIPT)
650 return hasShadowRootParent(child) && HTMLElement::childShouldCreateRenderer(child);
652 if (!hasMediaControls())
654 // <media> doesn't allow its content, including shadow subtree, to
655 // be rendered. So this should return false for most of the children.
656 // One exception is a shadow tree built for rendering controls which should be visible.
657 // So we let them go here by comparing its subtree root with one of the controls.
658 return &mediaControls()->treeScope() == &child.treeScope()
659 && hasShadowRootParent(child)
660 && HTMLElement::childShouldCreateRenderer(child);
664 Node::InsertionNotificationRequest HTMLMediaElement::insertedInto(ContainerNode& insertionPoint)
666 LOG(Media, "HTMLMediaElement::insertedInto(%p)", this);
668 HTMLElement::insertedInto(insertionPoint);
669 if (insertionPoint.inDocument()) {
670 m_inActiveDocument = true;
673 if (m_networkState == NETWORK_EMPTY && !fastGetAttribute(srcAttr).isEmpty() && m_mediaSession->dataLoadingPermitted(*this))
675 if (m_networkState == NETWORK_EMPTY && !fastGetAttribute(srcAttr).isEmpty())
677 scheduleDelayedAction(LoadMediaResource);
680 if (!m_explicitlyMuted) {
681 m_explicitlyMuted = true;
682 m_muted = fastHasAttribute(mutedAttr);
685 configureMediaControls();
686 return InsertionDone;
689 void HTMLMediaElement::removedFrom(ContainerNode& insertionPoint)
691 LOG(Media, "HTMLMediaElement::removedFrom(%p)", this);
693 m_inActiveDocument = false;
694 if (insertionPoint.inDocument()) {
695 if (hasMediaControls())
696 mediaControls()->hide();
697 if (m_networkState > NETWORK_EMPTY)
699 if (m_videoFullscreenMode != VideoFullscreenModeNone)
703 size_t extraMemoryCost = m_player->extraMemoryCost();
704 if (extraMemoryCost > m_reportedExtraMemoryCost) {
705 JSC::VM& vm = JSDOMWindowBase::commonVM();
706 JSC::JSLockHolder lock(vm);
708 size_t extraMemoryCostDelta = extraMemoryCost - m_reportedExtraMemoryCost;
709 m_reportedExtraMemoryCost = extraMemoryCost;
710 // FIXME: Adopt reportExtraMemoryVisited, and switch to reportExtraMemoryAllocated.
711 // https://bugs.webkit.org/show_bug.cgi?id=142595
712 vm.heap.deprecatedReportExtraMemory(extraMemoryCostDelta);
717 HTMLElement::removedFrom(insertionPoint);
720 void HTMLMediaElement::willAttachRenderers()
725 void HTMLMediaElement::didAttachRenderers()
728 renderer()->updateFromElement();
731 void HTMLMediaElement::didRecalcStyle(Style::Change)
734 renderer()->updateFromElement();
737 void HTMLMediaElement::scheduleDelayedAction(DelayedActionType actionType)
739 LOG(Media, "HTMLMediaElement::scheduleLoad(%p)", this);
741 if ((actionType & LoadMediaResource) && !(m_pendingActionFlags & LoadMediaResource)) {
743 setFlags(m_pendingActionFlags, LoadMediaResource);
746 #if ENABLE(VIDEO_TRACK)
747 if (RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled() && (actionType & ConfigureTextTracks))
748 setFlags(m_pendingActionFlags, ConfigureTextTracks);
751 #if USE(PLATFORM_TEXT_TRACK_MENU)
752 if (actionType & TextTrackChangesNotification)
753 setFlags(m_pendingActionFlags, TextTrackChangesNotification);
756 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
757 if (actionType & CheckPlaybackTargetCompatablity)
758 setFlags(m_pendingActionFlags, CheckPlaybackTargetCompatablity);
761 m_pendingActionTimer.startOneShot(0);
764 void HTMLMediaElement::scheduleNextSourceChild()
766 // Schedule the timer to try the next <source> element WITHOUT resetting state ala prepareForLoad.
767 setFlags(m_pendingActionFlags, LoadMediaResource);
768 m_pendingActionTimer.startOneShot(0);
771 void HTMLMediaElement::scheduleEvent(const AtomicString& eventName)
774 LOG(Media, "HTMLMediaElement::scheduleEvent(%p) - scheduling '%s'", this, eventName.string().ascii().data());
776 RefPtr<Event> event = Event::create(eventName, false, true);
778 // Don't set the event target, the event queue will set it in GenericEventQueue::timerFired and setting it here
779 // will trigger an ASSERT if this element has been marked for deletion.
781 m_asyncEventQueue.enqueueEvent(event.release());
784 void HTMLMediaElement::pendingActionTimerFired()
786 Ref<HTMLMediaElement> protect(*this); // loadNextSourceChild may fire 'beforeload', which can make arbitrary DOM mutations.
788 #if ENABLE(VIDEO_TRACK)
789 if (RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled() && (m_pendingActionFlags & ConfigureTextTracks))
790 configureTextTracks();
793 if (m_pendingActionFlags & LoadMediaResource) {
794 if (m_loadState == LoadingFromSourceElement)
795 loadNextSourceChild();
800 #if USE(PLATFORM_TEXT_TRACK_MENU)
801 if (RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled() && (m_pendingActionFlags & TextTrackChangesNotification))
802 notifyMediaPlayerOfTextTrackChanges();
805 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
806 if (m_pendingActionFlags & CheckPlaybackTargetCompatablity && m_player && m_player->isCurrentPlaybackTargetWireless() && !m_player->canPlayToWirelessPlaybackTarget()) {
807 LOG(Media, "HTMLMediaElement::pendingActionTimerFired(%p) - calling setShouldPlayToPlaybackTarget(false)", this);
808 m_player->setShouldPlayToPlaybackTarget(false);
812 m_pendingActionFlags = 0;
815 PassRefPtr<MediaError> HTMLMediaElement::error() const
820 void HTMLMediaElement::setSrc(const String& url)
822 setAttribute(srcAttr, url);
825 #if ENABLE(MEDIA_STREAM)
826 void HTMLMediaElement::setSrcObject(MediaStream* mediaStream)
828 // FIXME: Setting the srcObject attribute may cause other changes to the media element's internal state:
829 // Specifically, if srcObject is specified, the UA must use it as the source of media, even if the src
830 // attribute is also set or children are present. If the value of srcObject is replaced or set to null
831 // the UA must re-run the media element load algorithm.
833 // https://bugs.webkit.org/show_bug.cgi?id=124896
835 m_mediaStreamSrcObject = mediaStream;
839 HTMLMediaElement::NetworkState HTMLMediaElement::networkState() const
841 return m_networkState;
844 String HTMLMediaElement::canPlayType(const String& mimeType, const String& keySystem, const URL& url) const
846 MediaEngineSupportParameters parameters;
847 ContentType contentType(mimeType);
848 parameters.type = contentType.type().lower();
849 parameters.codecs = contentType.parameter(ASCIILiteral("codecs"));
850 parameters.url = url;
851 #if ENABLE(ENCRYPTED_MEDIA)
852 parameters.keySystem = keySystem;
854 UNUSED_PARAM(keySystem);
856 MediaPlayer::SupportsType support = MediaPlayer::supportsType(parameters, this);
862 case MediaPlayer::IsNotSupported:
863 canPlay = emptyString();
865 case MediaPlayer::MayBeSupported:
866 canPlay = ASCIILiteral("maybe");
868 case MediaPlayer::IsSupported:
869 canPlay = ASCIILiteral("probably");
873 LOG(Media, "HTMLMediaElement::canPlayType(%p) - [%s, %s, %s] -> %s", this, mimeType.utf8().data(), keySystem.utf8().data(), url.stringCenterEllipsizedToLength().utf8().data(), canPlay.utf8().data());
878 void HTMLMediaElement::load()
880 Ref<HTMLMediaElement> protect(*this); // loadInternal may result in a 'beforeload' event, which can make arbitrary DOM mutations.
882 LOG(Media, "HTMLMediaElement::load(%p)", this);
884 if (!m_mediaSession->dataLoadingPermitted(*this))
886 if (ScriptController::processingUserGesture())
887 removeBehaviorsRestrictionsAfterFirstUserGesture();
894 void HTMLMediaElement::prepareForLoad()
896 LOG(Media, "HTMLMediaElement::prepareForLoad(%p)", this);
898 // Perform the cleanup required for the resource load algorithm to run.
899 stopPeriodicTimers();
900 m_pendingActionTimer.stop();
901 // FIXME: Figure out appropriate place to reset LoadTextTrackResource if necessary and set m_pendingActionFlags to 0 here.
902 m_pendingActionFlags &= ~LoadMediaResource;
903 m_sentEndEvent = false;
904 m_sentStalledEvent = false;
905 m_haveFiredLoadedData = false;
906 m_completelyLoaded = false;
907 m_havePreparedToPlay = false;
908 m_displayMode = Unknown;
909 m_currentSrc = URL();
911 // 1 - Abort any already-running instance of the resource selection algorithm for this element.
912 m_loadState = WaitingForSource;
913 m_currentSourceNode = 0;
915 // 2 - If there are any tasks from the media element's media element event task source in
916 // one of the task queues, then remove those tasks.
917 cancelPendingEventsAndCallbacks();
919 // 3 - If the media element's networkState is set to NETWORK_LOADING or NETWORK_IDLE, queue
920 // a task to fire a simple event named abort at the media element.
921 if (m_networkState == NETWORK_LOADING || m_networkState == NETWORK_IDLE)
922 scheduleEvent(eventNames().abortEvent);
924 #if ENABLE(MEDIA_SOURCE)
930 // 4 - If the media element's networkState is not set to NETWORK_EMPTY, then run these substeps
931 if (m_networkState != NETWORK_EMPTY) {
932 // 4.1 - Queue a task to fire a simple event named emptied at the media element.
933 scheduleEvent(eventNames().emptiedEvent);
935 // 4.2 - If a fetching process is in progress for the media element, the user agent should stop it.
936 m_networkState = NETWORK_EMPTY;
938 // 4.3 - Forget the media element's media-resource-specific tracks.
939 forgetResourceSpecificTracks();
941 // 4.4 - If readyState is not set to HAVE_NOTHING, then set it to that state.
942 m_readyState = HAVE_NOTHING;
943 m_readyStateMaximum = HAVE_NOTHING;
945 // 4.5 - If the paused attribute is false, then set it to true.
948 // 4.6 - If seeking is true, set it to false.
951 // 4.7 - Set the current playback position to 0.
952 // Set the official playback position to 0.
953 // If this changed the official playback position, then queue a task to fire a simple event named timeupdate at the media element.
954 // FIXME: Add support for firing this event. e.g., scheduleEvent(eventNames().timeUpdateEvent);
956 // 4.8 - Set the initial playback position to 0.
957 // FIXME: Make this less subtle. The position only becomes 0 because of the createMediaPlayer() call
961 invalidateCachedTime();
963 // 4.9 - Set the timeline offset to Not-a-Number (NaN).
964 // 4.10 - Update the duration attribute to Not-a-Number (NaN).
966 updateMediaController();
967 #if ENABLE(VIDEO_TRACK)
968 if (RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
969 updateActiveTextTrackCues(MediaTime::zeroTime());
973 // 5 - Set the playbackRate attribute to the value of the defaultPlaybackRate attribute.
974 setPlaybackRate(defaultPlaybackRate());
976 // 6 - Set the error attribute to null and the autoplaying flag to true.
978 m_autoplaying = true;
980 // 7 - Invoke the media element's resource selection algorithm.
982 // 8 - Note: Playback of any previously playing media resource for this element stops.
984 // The resource selection algorithm
985 // 1 - Set the networkState to NETWORK_NO_SOURCE
986 m_networkState = NETWORK_NO_SOURCE;
988 // 2 - Asynchronously await a stable state.
990 m_playedTimeRanges = TimeRanges::create();
992 // FIXME: Investigate whether these can be moved into m_networkState != NETWORK_EMPTY block above
993 // so they are closer to the relevant spec steps.
994 m_lastSeekTime = MediaTime::zeroTime();
996 // The spec doesn't say to block the load event until we actually run the asynchronous section
997 // algorithm, but do it now because we won't start that until after the timer fires and the
998 // event may have already fired by then.
999 MediaPlayer::Preload effectivePreload = m_mediaSession->effectivePreloadForElement(*this);
1000 if (effectivePreload != MediaPlayer::None)
1001 setShouldDelayLoadEvent(true);
1004 Settings* settings = document().settings();
1005 if (effectivePreload != MediaPlayer::None && settings && settings->mediaDataLoadsAutomatically())
1009 configureMediaControls();
1012 void HTMLMediaElement::loadInternal()
1014 // Some of the code paths below this function dispatch the BeforeLoad event. This ASSERT helps
1015 // us catch those bugs more quickly without needing all the branches to align to actually
1016 // trigger the event.
1017 ASSERT(!NoEventDispatchAssertion::isEventDispatchForbidden());
1019 // If we can't start a load right away, start it later.
1020 if (!m_mediaSession->pageAllowsDataLoading(*this)) {
1021 setShouldDelayLoadEvent(false);
1022 if (m_isWaitingUntilMediaCanStart)
1024 document().addMediaCanStartListener(this);
1025 m_isWaitingUntilMediaCanStart = true;
1029 clearFlags(m_pendingActionFlags, LoadMediaResource);
1031 // Once the page has allowed an element to load media, it is free to load at will. This allows a
1032 // playlist that starts in a foreground tab to continue automatically if the tab is subsequently
1033 // put into the background.
1034 m_mediaSession->removeBehaviorRestriction(MediaElementSession::RequirePageConsentToLoadMedia);
1036 #if ENABLE(VIDEO_TRACK)
1037 if (hasMediaControls())
1038 mediaControls()->changedClosedCaptionsVisibility();
1040 // HTMLMediaElement::textTracksAreReady will need "... the text tracks whose mode was not in the
1041 // disabled state when the element's resource selection algorithm last started".
1042 if (RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled()) {
1043 m_textTracksWhenResourceSelectionBegan.clear();
1045 for (unsigned i = 0; i < m_textTracks->length(); ++i) {
1046 TextTrack* track = m_textTracks->item(i);
1047 if (track->mode() != TextTrack::disabledKeyword())
1048 m_textTracksWhenResourceSelectionBegan.append(track);
1054 selectMediaResource();
1057 void HTMLMediaElement::selectMediaResource()
1059 LOG(Media, "HTMLMediaElement::selectMediaResource(%p)", this);
1061 enum Mode { attribute, children };
1063 // 3 - If the media element has a src attribute, then let mode be attribute.
1064 Mode mode = attribute;
1065 if (!fastHasAttribute(srcAttr)) {
1066 // Otherwise, if the media element does not have a src attribute but has a source
1067 // element child, then let mode be children and let candidate be the first such
1068 // source element child in tree order.
1069 if (auto firstSource = childrenOfType<HTMLSourceElement>(*this).first()) {
1071 m_nextChildNodeToConsider = firstSource;
1072 m_currentSourceNode = 0;
1074 // Otherwise the media element has neither a src attribute nor a source element
1075 // child: set the networkState to NETWORK_EMPTY, and abort these steps; the
1076 // synchronous section ends.
1077 m_loadState = WaitingForSource;
1078 setShouldDelayLoadEvent(false);
1079 m_networkState = NETWORK_EMPTY;
1081 LOG(Media, "HTMLMediaElement::selectMediaResource(%p) - nothing to load", this);
1086 // 4 - Set the media element's delaying-the-load-event flag to true (this delays the load event),
1087 // and set its networkState to NETWORK_LOADING.
1088 setShouldDelayLoadEvent(true);
1089 m_networkState = NETWORK_LOADING;
1091 // 5 - Queue a task to fire a simple event named loadstart at the media element.
1092 scheduleEvent(eventNames().loadstartEvent);
1094 // 6 - If mode is attribute, then run these substeps
1095 if (mode == attribute) {
1096 m_loadState = LoadingFromSrcAttr;
1098 // If the src attribute's value is the empty string ... jump down to the failed step below
1099 URL mediaURL = getNonEmptyURLAttribute(srcAttr);
1100 if (mediaURL.isEmpty()) {
1101 mediaLoadingFailed(MediaPlayer::FormatError);
1102 LOG(Media, "HTMLMediaElement::selectMediaResource(%p) - empty 'src'", this);
1106 if (!isSafeToLoadURL(mediaURL, Complain) || !dispatchBeforeLoadEvent(mediaURL.string())) {
1107 mediaLoadingFailed(MediaPlayer::FormatError);
1111 // No type or key system information is available when the url comes
1112 // from the 'src' attribute so MediaPlayer
1113 // will have to pick a media engine based on the file extension.
1114 ContentType contentType((String()));
1115 loadResource(mediaURL, contentType, String());
1116 LOG(Media, "HTMLMediaElement::selectMediaResource(%p) - using 'src' attribute url", this);
1120 // Otherwise, the source elements will be used
1121 loadNextSourceChild();
1124 void HTMLMediaElement::loadNextSourceChild()
1126 ContentType contentType((String()));
1128 URL mediaURL = selectNextSourceChild(&contentType, &keySystem, Complain);
1129 if (!mediaURL.isValid()) {
1130 waitForSourceChange();
1134 // Recreate the media player for the new url
1135 createMediaPlayer();
1137 m_loadState = LoadingFromSourceElement;
1138 loadResource(mediaURL, contentType, keySystem);
1141 void HTMLMediaElement::loadResource(const URL& initialURL, ContentType& contentType, const String& keySystem)
1143 ASSERT(isSafeToLoadURL(initialURL, Complain));
1145 LOG(Media, "HTMLMediaElement::loadResource(%p) - %s, %s, %s", this, urlForLoggingMedia(initialURL).utf8().data(), contentType.raw().utf8().data(), keySystem.utf8().data());
1147 Frame* frame = document().frame();
1149 mediaLoadingFailed(MediaPlayer::FormatError);
1153 Page* page = frame->page();
1155 mediaLoadingFailed(MediaPlayer::FormatError);
1159 URL url = initialURL;
1160 if (!frame->loader().willLoadMediaElementURL(url)) {
1161 mediaLoadingFailed(MediaPlayer::FormatError);
1165 #if ENABLE(CONTENT_EXTENSIONS)
1166 ResourceRequest request(url);
1167 DocumentLoader* documentLoader = frame->loader().documentLoader();
1169 if (page->userContentController() && documentLoader)
1170 page->userContentController()->processContentExtensionRulesForLoad(request, ResourceType::Media, *documentLoader);
1172 if (request.isNull()) {
1173 mediaLoadingFailed(MediaPlayer::FormatError);
1178 // The resource fetch algorithm
1179 m_networkState = NETWORK_LOADING;
1181 // If the url should be loaded from the application cache, pass the url of the cached file
1182 // to the media engine.
1183 ApplicationCacheHost* cacheHost = frame->loader().documentLoader()->applicationCacheHost();
1184 ApplicationCacheResource* resource = 0;
1185 if (cacheHost && cacheHost->shouldLoadResourceFromApplicationCache(ResourceRequest(url), resource)) {
1186 // Resources that are not present in the manifest will always fail to load (at least, after the
1187 // cache has been primed the first time), making the testing of offline applications simpler.
1188 if (!resource || resource->path().isEmpty()) {
1189 mediaLoadingFailed(MediaPlayer::NetworkError);
1194 // Log that we started loading a media element.
1195 if (Frame* frame = document().frame())
1196 frame->mainFrame().diagnosticLoggingClient().logDiagnosticMessageWithValue(DiagnosticLoggingKeys::mediaKey(), isVideo() ? DiagnosticLoggingKeys::videoKey() : DiagnosticLoggingKeys::audioKey(), DiagnosticLoggingKeys::loadingKey(), ShouldSample::No);
1198 m_firstTimePlaying = true;
1200 // Set m_currentSrc *before* changing to the cache url, the fact that we are loading from the app
1201 // cache is an internal detail not exposed through the media element API.
1205 url = ApplicationCacheHost::createFileURL(resource->path());
1206 LOG(Media, "HTMLMediaElement::loadResource(%p) - will load from app cache -> %s", this, urlForLoggingMedia(url).utf8().data());
1209 LOG(Media, "HTMLMediaElement::loadResource(%p) - m_currentSrc -> %s", this, urlForLoggingMedia(m_currentSrc).utf8().data());
1211 #if ENABLE(MEDIA_STREAM)
1212 if (MediaStreamRegistry::registry().lookup(url.string()))
1213 m_mediaSession->removeBehaviorRestriction(MediaElementSession::RequireUserGestureForRateChange);
1216 if (m_sendProgressEvents)
1217 startProgressEventTimer();
1219 bool privateMode = document().page() && document().page()->usesEphemeralSession();
1220 m_player->setPrivateBrowsingMode(privateMode);
1222 // Reset display mode to force a recalculation of what to show because we are resetting the player.
1223 setDisplayMode(Unknown);
1226 m_player->setPreload(m_mediaSession->effectivePreloadForElement(*this));
1227 m_player->setPreservesPitch(m_webkitPreservesPitch);
1229 if (!m_explicitlyMuted) {
1230 m_explicitlyMuted = true;
1231 m_muted = fastHasAttribute(mutedAttr);
1236 #if ENABLE(MEDIA_SOURCE)
1237 ASSERT(!m_mediaSource);
1239 if (url.protocolIs(mediaSourceBlobProtocol))
1240 m_mediaSource = MediaSource::lookup(url.string());
1242 if (m_mediaSource) {
1243 if (m_mediaSource->attachToElement(this))
1244 m_player->load(url, contentType, m_mediaSource.get());
1246 // Forget our reference to the MediaSource, so we leave it alone
1247 // while processing remainder of load failure.
1249 mediaLoadingFailed(MediaPlayer::FormatError);
1253 #if ENABLE(MEDIA_STREAM)
1254 if (m_mediaStreamSrcObject)
1255 m_player->load(m_mediaStreamSrcObject->privateStream());
1258 if (!m_player->load(url, contentType, keySystem))
1259 mediaLoadingFailed(MediaPlayer::FormatError);
1261 // If there is no poster to display, allow the media engine to render video frames as soon as
1262 // they are available.
1263 updateDisplayState();
1266 renderer()->updateFromElement();
1269 #if ENABLE(VIDEO_TRACK)
1270 static bool trackIndexCompare(TextTrack* a,
1273 return a->trackIndex() - b->trackIndex() < 0;
1276 static bool eventTimeCueCompare(const std::pair<MediaTime, TextTrackCue*>& a, const std::pair<MediaTime, TextTrackCue*>& b)
1278 // 12 - Sort the tasks in events in ascending time order (tasks with earlier
1280 if (a.first != b.first)
1281 return a.first - b.first < MediaTime::zeroTime();
1283 // If the cues belong to different text tracks, it doesn't make sense to
1284 // compare the two tracks by the relative cue order, so return the relative
1286 if (a.second->track() != b.second->track())
1287 return trackIndexCompare(a.second->track(), b.second->track());
1289 // 12 - Further sort tasks in events that have the same time by the
1290 // relative text track cue order of the text track cues associated
1291 // with these tasks.
1292 return a.second->cueIndex() - b.second->cueIndex() < 0;
1295 static bool compareCueInterval(const CueInterval& one, const CueInterval& two)
1297 return one.data()->isOrderedBefore(two.data());
1301 void HTMLMediaElement::updateActiveTextTrackCues(const MediaTime& movieTime)
1303 // 4.8.10.8 Playing the media resource
1305 // If the current playback position changes while the steps are running,
1306 // then the user agent must wait for the steps to complete, and then must
1307 // immediately rerun the steps.
1308 if (ignoreTrackDisplayUpdateRequests())
1311 LOG(Media, "HTMLMediaElement::updateActiveTextTracks(%p)", this);
1313 // 1 - Let current cues be a list of cues, initialized to contain all the
1314 // cues of all the hidden, showing, or showing by default text tracks of the
1315 // media element (not the disabled ones) whose start times are less than or
1316 // equal to the current playback position and whose end times are greater
1317 // than the current playback position.
1318 CueList currentCues;
1320 // The user agent must synchronously unset [the text track cue active] flag
1321 // whenever ... the media element's readyState is changed back to HAVE_NOTHING.
1322 if (m_readyState != HAVE_NOTHING && m_player) {
1323 currentCues = m_cueTree.allOverlaps(m_cueTree.createInterval(movieTime, movieTime));
1324 if (currentCues.size() > 1)
1325 std::sort(currentCues.begin(), currentCues.end(), &compareCueInterval);
1328 CueList previousCues;
1331 // 2 - Let other cues be a list of cues, initialized to contain all the cues
1332 // of hidden, showing, and showing by default text tracks of the media
1333 // element that are not present in current cues.
1334 previousCues = m_currentlyActiveCues;
1336 // 3 - Let last time be the current playback position at the time this
1337 // algorithm was last run for this media element, if this is not the first
1339 MediaTime lastTime = m_lastTextTrackUpdateTime;
1341 // 4 - If the current playback position has, since the last time this
1342 // algorithm was run, only changed through its usual monotonic increase
1343 // during normal playback, then let missed cues be the list of cues in other
1344 // cues whose start times are greater than or equal to last time and whose
1345 // end times are less than or equal to the current playback position.
1346 // Otherwise, let missed cues be an empty list.
1347 if (lastTime >= MediaTime::zeroTime() && m_lastSeekTime < movieTime) {
1348 CueList potentiallySkippedCues =
1349 m_cueTree.allOverlaps(m_cueTree.createInterval(lastTime, movieTime));
1351 for (size_t i = 0; i < potentiallySkippedCues.size(); ++i) {
1352 MediaTime cueStartTime = potentiallySkippedCues[i].low();
1353 MediaTime cueEndTime = potentiallySkippedCues[i].high();
1355 // Consider cues that may have been missed since the last seek time.
1356 if (cueStartTime > std::max(m_lastSeekTime, lastTime) && cueEndTime < movieTime)
1357 missedCues.append(potentiallySkippedCues[i]);
1361 m_lastTextTrackUpdateTime = movieTime;
1363 // 5 - If the time was reached through the usual monotonic increase of the
1364 // current playback position during normal playback, and if the user agent
1365 // has not fired a timeupdate event at the element in the past 15 to 250ms
1366 // and is not still running event handlers for such an event, then the user
1367 // agent must queue a task to fire a simple event named timeupdate at the
1368 // element. (In the other cases, such as explicit seeks, relevant events get
1369 // fired as part of the overall process of changing the current playback
1371 if (!m_paused && m_lastSeekTime <= lastTime)
1372 scheduleTimeupdateEvent(false);
1374 // Explicitly cache vector sizes, as their content is constant from here.
1375 size_t currentCuesSize = currentCues.size();
1376 size_t missedCuesSize = missedCues.size();
1377 size_t previousCuesSize = previousCues.size();
1379 // 6 - If all of the cues in current cues have their text track cue active
1380 // flag set, none of the cues in other cues have their text track cue active
1381 // flag set, and missed cues is empty, then abort these steps.
1382 bool activeSetChanged = missedCuesSize;
1384 for (size_t i = 0; !activeSetChanged && i < previousCuesSize; ++i)
1385 if (!currentCues.contains(previousCues[i]) && previousCues[i].data()->isActive())
1386 activeSetChanged = true;
1388 for (size_t i = 0; i < currentCuesSize; ++i) {
1389 TextTrackCue* cue = currentCues[i].data();
1391 if (cue->isRenderable())
1392 toVTTCue(cue)->updateDisplayTree(movieTime);
1394 if (!cue->isActive())
1395 activeSetChanged = true;
1398 if (!activeSetChanged)
1401 // 7 - If the time was reached through the usual monotonic increase of the
1402 // current playback position during normal playback, and there are cues in
1403 // other cues that have their text track cue pause-on-exi flag set and that
1404 // either have their text track cue active flag set or are also in missed
1405 // cues, then immediately pause the media element.
1406 for (size_t i = 0; !m_paused && i < previousCuesSize; ++i) {
1407 if (previousCues[i].data()->pauseOnExit()
1408 && previousCues[i].data()->isActive()
1409 && !currentCues.contains(previousCues[i]))
1413 for (size_t i = 0; !m_paused && i < missedCuesSize; ++i) {
1414 if (missedCues[i].data()->pauseOnExit())
1418 // 8 - Let events be a list of tasks, initially empty. Each task in this
1419 // list will be associated with a text track, a text track cue, and a time,
1420 // which are used to sort the list before the tasks are queued.
1421 Vector<std::pair<MediaTime, TextTrackCue*>> eventTasks;
1423 // 8 - Let affected tracks be a list of text tracks, initially empty.
1424 Vector<TextTrack*> affectedTracks;
1426 for (size_t i = 0; i < missedCuesSize; ++i) {
1427 // 9 - For each text track cue in missed cues, prepare an event named enter
1428 // for the TextTrackCue object with the text track cue start time.
1429 eventTasks.append(std::make_pair(missedCues[i].data()->startMediaTime(),
1430 missedCues[i].data()));
1432 // 10 - For each text track [...] in missed cues, prepare an event
1433 // named exit for the TextTrackCue object with the with the later of
1434 // the text track cue end time and the text track cue start time.
1436 // Note: An explicit task is added only if the cue is NOT a zero or
1437 // negative length cue. Otherwise, the need for an exit event is
1438 // checked when these tasks are actually queued below. This doesn't
1439 // affect sorting events before dispatch either, because the exit
1440 // event has the same time as the enter event.
1441 if (missedCues[i].data()->startMediaTime() < missedCues[i].data()->endMediaTime())
1442 eventTasks.append(std::make_pair(missedCues[i].data()->endMediaTime(), missedCues[i].data()));
1445 for (size_t i = 0; i < previousCuesSize; ++i) {
1446 // 10 - For each text track cue in other cues that has its text
1447 // track cue active flag set prepare an event named exit for the
1448 // TextTrackCue object with the text track cue end time.
1449 if (!currentCues.contains(previousCues[i]))
1450 eventTasks.append(std::make_pair(previousCues[i].data()->endMediaTime(),
1451 previousCues[i].data()));
1454 for (size_t i = 0; i < currentCuesSize; ++i) {
1455 // 11 - For each text track cue in current cues that does not have its
1456 // text track cue active flag set, prepare an event named enter for the
1457 // TextTrackCue object with the text track cue start time.
1458 if (!previousCues.contains(currentCues[i]))
1459 eventTasks.append(std::make_pair(currentCues[i].data()->startMediaTime(),
1460 currentCues[i].data()));
1463 // 12 - Sort the tasks in events in ascending time order (tasks with earlier
1465 std::sort(eventTasks.begin(), eventTasks.end(), eventTimeCueCompare);
1467 for (size_t i = 0; i < eventTasks.size(); ++i) {
1468 if (!affectedTracks.contains(eventTasks[i].second->track()))
1469 affectedTracks.append(eventTasks[i].second->track());
1471 // 13 - Queue each task in events, in list order.
1472 RefPtr<Event> event;
1474 // Each event in eventTasks may be either an enterEvent or an exitEvent,
1475 // depending on the time that is associated with the event. This
1476 // correctly identifies the type of the event, if the startTime is
1477 // less than the endTime in the cue.
1478 if (eventTasks[i].second->startTime() >= eventTasks[i].second->endTime()) {
1479 event = Event::create(eventNames().enterEvent, false, false);
1480 event->setTarget(eventTasks[i].second);
1481 m_asyncEventQueue.enqueueEvent(event.release());
1483 event = Event::create(eventNames().exitEvent, false, false);
1484 event->setTarget(eventTasks[i].second);
1485 m_asyncEventQueue.enqueueEvent(event.release());
1487 if (eventTasks[i].first == eventTasks[i].second->startMediaTime())
1488 event = Event::create(eventNames().enterEvent, false, false);
1490 event = Event::create(eventNames().exitEvent, false, false);
1492 event->setTarget(eventTasks[i].second);
1493 m_asyncEventQueue.enqueueEvent(event.release());
1497 // 14 - Sort affected tracks in the same order as the text tracks appear in
1498 // the media element's list of text tracks, and remove duplicates.
1499 std::sort(affectedTracks.begin(), affectedTracks.end(), trackIndexCompare);
1501 // 15 - For each text track in affected tracks, in the list order, queue a
1502 // task to fire a simple event named cuechange at the TextTrack object, and, ...
1503 for (size_t i = 0; i < affectedTracks.size(); ++i) {
1504 RefPtr<Event> event = Event::create(eventNames().cuechangeEvent, false, false);
1505 event->setTarget(affectedTracks[i]);
1507 m_asyncEventQueue.enqueueEvent(event.release());
1509 // ... if the text track has a corresponding track element, to then fire a
1510 // simple event named cuechange at the track element as well.
1511 if (is<LoadableTextTrack>(*affectedTracks[i])) {
1512 RefPtr<Event> event = Event::create(eventNames().cuechangeEvent, false, false);
1513 HTMLTrackElement* trackElement = downcast<LoadableTextTrack>(*affectedTracks[i]).trackElement();
1514 ASSERT(trackElement);
1515 event->setTarget(trackElement);
1517 m_asyncEventQueue.enqueueEvent(event.release());
1521 // 16 - Set the text track cue active flag of all the cues in the current
1522 // cues, and unset the text track cue active flag of all the cues in the
1524 for (size_t i = 0; i < currentCuesSize; ++i)
1525 currentCues[i].data()->setIsActive(true);
1527 for (size_t i = 0; i < previousCuesSize; ++i)
1528 if (!currentCues.contains(previousCues[i]))
1529 previousCues[i].data()->setIsActive(false);
1531 // Update the current active cues.
1532 m_currentlyActiveCues = currentCues;
1534 if (activeSetChanged)
1535 updateTextTrackDisplay();
1538 bool HTMLMediaElement::textTracksAreReady() const
1540 // 4.8.10.12.1 Text track model
1542 // The text tracks of a media element are ready if all the text tracks whose mode was not
1543 // in the disabled state when the element's resource selection algorithm last started now
1544 // have a text track readiness state of loaded or failed to load.
1545 for (unsigned i = 0; i < m_textTracksWhenResourceSelectionBegan.size(); ++i) {
1546 if (m_textTracksWhenResourceSelectionBegan[i]->readinessState() == TextTrack::Loading
1547 || m_textTracksWhenResourceSelectionBegan[i]->readinessState() == TextTrack::NotLoaded)
1554 void HTMLMediaElement::textTrackReadyStateChanged(TextTrack* track)
1556 if (m_player && m_textTracksWhenResourceSelectionBegan.contains(track)) {
1557 if (track->readinessState() != TextTrack::Loading)
1558 setReadyState(m_player->readyState());
1560 // The track readiness state might have changed as a result of the user
1561 // clicking the captions button. In this case, a check whether all the
1562 // resources have failed loading should be done in order to hide the CC button.
1563 if (hasMediaControls() && track->readinessState() == TextTrack::FailedToLoad)
1564 mediaControls()->refreshClosedCaptionsButtonVisibility();
1568 void HTMLMediaElement::audioTrackEnabledChanged(AudioTrack* track)
1570 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
1572 if (m_audioTracks && m_audioTracks->contains(track))
1573 m_audioTracks->scheduleChangeEvent();
1576 void HTMLMediaElement::textTrackModeChanged(TextTrack* track)
1578 bool trackIsLoaded = true;
1579 if (track->trackType() == TextTrack::TrackElement) {
1580 trackIsLoaded = false;
1581 for (auto& trackElement : childrenOfType<HTMLTrackElement>(*this)) {
1582 if (trackElement.track() == track) {
1583 if (trackElement.readyState() == HTMLTrackElement::LOADING || trackElement.readyState() == HTMLTrackElement::LOADED)
1584 trackIsLoaded = true;
1590 // If this is the first added track, create the list of text tracks.
1592 m_textTracks = TextTrackList::create(this, ActiveDOMObject::scriptExecutionContext());
1594 // Mark this track as "configured" so configureTextTracks won't change the mode again.
1595 track->setHasBeenConfigured(true);
1597 if (track->mode() != TextTrack::disabledKeyword() && trackIsLoaded)
1598 textTrackAddCues(track, track->cues());
1600 #if USE(PLATFORM_TEXT_TRACK_MENU)
1601 if (platformTextTrackMenu())
1602 platformTextTrackMenu()->trackWasSelected(track->platformTextTrack());
1605 configureTextTrackDisplay(AssumeTextTrackVisibilityChanged);
1607 if (m_textTracks && m_textTracks->contains(track))
1608 m_textTracks->scheduleChangeEvent();
1610 #if ENABLE(AVF_CAPTIONS)
1611 if (track->trackType() == TextTrack::TrackElement && m_player)
1612 m_player->notifyTrackModeChanged();
1616 void HTMLMediaElement::videoTrackSelectedChanged(VideoTrack* track)
1618 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
1620 if (m_videoTracks && m_videoTracks->contains(track))
1621 m_videoTracks->scheduleChangeEvent();
1624 void HTMLMediaElement::textTrackKindChanged(TextTrack* track)
1626 if (track->kind() != TextTrack::captionsKeyword() && track->kind() != TextTrack::subtitlesKeyword() && track->mode() == TextTrack::showingKeyword())
1627 track->setMode(TextTrack::hiddenKeyword());
1630 void HTMLMediaElement::beginIgnoringTrackDisplayUpdateRequests()
1632 ++m_ignoreTrackDisplayUpdate;
1635 void HTMLMediaElement::endIgnoringTrackDisplayUpdateRequests()
1637 ASSERT(m_ignoreTrackDisplayUpdate);
1638 --m_ignoreTrackDisplayUpdate;
1639 if (!m_ignoreTrackDisplayUpdate && m_inActiveDocument)
1640 updateActiveTextTrackCues(currentMediaTime());
1643 void HTMLMediaElement::textTrackAddCues(TextTrack* track, const TextTrackCueList* cues)
1645 if (track->mode() == TextTrack::disabledKeyword())
1648 TrackDisplayUpdateScope scope(this);
1649 for (size_t i = 0; i < cues->length(); ++i)
1650 textTrackAddCue(track, cues->item(i));
1653 void HTMLMediaElement::textTrackRemoveCues(TextTrack*, const TextTrackCueList* cues)
1655 TrackDisplayUpdateScope scope(this);
1656 for (size_t i = 0; i < cues->length(); ++i)
1657 textTrackRemoveCue(cues->item(i)->track(), cues->item(i));
1660 void HTMLMediaElement::textTrackAddCue(TextTrack* track, PassRefPtr<TextTrackCue> prpCue)
1662 if (track->mode() == TextTrack::disabledKeyword())
1665 RefPtr<TextTrackCue> cue = prpCue;
1667 // Negative duration cues need be treated in the interval tree as
1668 // zero-length cues.
1669 MediaTime endTime = std::max(cue->startMediaTime(), cue->endMediaTime());
1671 CueInterval interval = m_cueTree.createInterval(cue->startMediaTime(), endTime, cue.get());
1672 if (!m_cueTree.contains(interval))
1673 m_cueTree.add(interval);
1674 updateActiveTextTrackCues(currentMediaTime());
1677 void HTMLMediaElement::textTrackRemoveCue(TextTrack*, PassRefPtr<TextTrackCue> prpCue)
1679 RefPtr<TextTrackCue> cue = prpCue;
1680 // Negative duration cues need to be treated in the interval tree as
1681 // zero-length cues.
1682 MediaTime endTime = std::max(cue->startMediaTime(), cue->endMediaTime());
1684 CueInterval interval = m_cueTree.createInterval(cue->startMediaTime(), endTime, cue.get());
1685 m_cueTree.remove(interval);
1687 #if ENABLE(WEBVTT_REGIONS)
1688 // Since the cue will be removed from the media element and likely the
1689 // TextTrack might also be destructed, notifying the region of the cue
1690 // removal shouldn't be done.
1691 if (cue->isRenderable())
1692 toVTTCue(cue.get())->notifyRegionWhenRemovingDisplayTree(false);
1695 size_t index = m_currentlyActiveCues.find(interval);
1696 if (index != notFound) {
1697 cue->setIsActive(false);
1698 m_currentlyActiveCues.remove(index);
1701 if (cue->isRenderable())
1702 toVTTCue(cue.get())->removeDisplayTree();
1703 updateActiveTextTrackCues(currentMediaTime());
1705 #if ENABLE(WEBVTT_REGIONS)
1706 if (cue->isRenderable())
1707 toVTTCue(cue.get())->notifyRegionWhenRemovingDisplayTree(true);
1713 bool HTMLMediaElement::isSafeToLoadURL(const URL& url, InvalidURLAction actionIfInvalid)
1715 if (!url.isValid()) {
1716 LOG(Media, "HTMLMediaElement::isSafeToLoadURL(%p) - %s -> FALSE because url is invalid", this, urlForLoggingMedia(url).utf8().data());
1720 Frame* frame = document().frame();
1721 if (!frame || !document().securityOrigin()->canDisplay(url)) {
1722 if (actionIfInvalid == Complain)
1723 FrameLoader::reportLocalLoadFailed(frame, url.stringCenterEllipsizedToLength());
1724 LOG(Media, "HTMLMediaElement::isSafeToLoadURL(%p) - %s -> FALSE rejected by SecurityOrigin", this, urlForLoggingMedia(url).utf8().data());
1728 if (!document().contentSecurityPolicy()->allowMediaFromSource(url)) {
1729 LOG(Media, "HTMLMediaElement::isSafeToLoadURL(%p) - %s -> rejected by Content Security Policy", this, urlForLoggingMedia(url).utf8().data());
1736 void HTMLMediaElement::startProgressEventTimer()
1738 if (m_progressEventTimer.isActive())
1741 m_previousProgressTime = monotonicallyIncreasingTime();
1742 // 350ms is not magic, it is in the spec!
1743 m_progressEventTimer.startRepeating(0.350);
1746 void HTMLMediaElement::waitForSourceChange()
1748 LOG(Media, "HTMLMediaElement::waitForSourceChange(%p)", this);
1750 stopPeriodicTimers();
1751 m_loadState = WaitingForSource;
1753 // 6.17 - Waiting: Set the element's networkState attribute to the NETWORK_NO_SOURCE value
1754 m_networkState = NETWORK_NO_SOURCE;
1756 // 6.18 - Set the element's delaying-the-load-event flag to false. This stops delaying the load event.
1757 setShouldDelayLoadEvent(false);
1759 updateDisplayState();
1762 renderer()->updateFromElement();
1765 void HTMLMediaElement::noneSupported()
1767 LOG(Media, "HTMLMediaElement::noneSupported(%p)", this);
1769 stopPeriodicTimers();
1770 m_loadState = WaitingForSource;
1771 m_currentSourceNode = 0;
1774 // 6 - Reaching this step indicates that the media resource failed to load or that the given
1775 // URL could not be resolved. In one atomic operation, run the following steps:
1777 // 6.1 - Set the error attribute to a new MediaError object whose code attribute is set to
1778 // MEDIA_ERR_SRC_NOT_SUPPORTED.
1779 m_error = MediaError::create(MediaError::MEDIA_ERR_SRC_NOT_SUPPORTED);
1781 // 6.2 - Forget the media element's media-resource-specific text tracks.
1782 forgetResourceSpecificTracks();
1784 // 6.3 - Set the element's networkState attribute to the NETWORK_NO_SOURCE value.
1785 m_networkState = NETWORK_NO_SOURCE;
1787 // 7 - Queue a task to fire a simple event named error at the media element.
1788 scheduleEvent(eventNames().errorEvent);
1790 #if ENABLE(MEDIA_SOURCE)
1794 // 8 - Set the element's delaying-the-load-event flag to false. This stops delaying the load event.
1795 setShouldDelayLoadEvent(false);
1797 // 9 - Abort these steps. Until the load() method is invoked or the src attribute is changed,
1798 // the element won't attempt to load another resource.
1800 updateDisplayState();
1803 renderer()->updateFromElement();
1806 void HTMLMediaElement::mediaLoadingFailedFatally(MediaPlayer::NetworkState error)
1808 LOG(Media, "HTMLMediaElement::mediaLoadingFailedFatally(%p) - error = %d", this, static_cast<int>(error));
1810 // 1 - The user agent should cancel the fetching process.
1811 stopPeriodicTimers();
1812 m_loadState = WaitingForSource;
1814 // 2 - Set the error attribute to a new MediaError object whose code attribute is
1815 // set to MEDIA_ERR_NETWORK/MEDIA_ERR_DECODE.
1816 if (error == MediaPlayer::NetworkError)
1817 m_error = MediaError::create(MediaError::MEDIA_ERR_NETWORK);
1818 else if (error == MediaPlayer::DecodeError)
1819 m_error = MediaError::create(MediaError::MEDIA_ERR_DECODE);
1821 ASSERT_NOT_REACHED();
1823 // 3 - Queue a task to fire a simple event named error at the media element.
1824 scheduleEvent(eventNames().errorEvent);
1826 #if ENABLE(MEDIA_SOURCE)
1830 // 4 - Set the element's networkState attribute to the NETWORK_EMPTY value and queue a
1831 // task to fire a simple event called emptied at the element.
1832 m_networkState = NETWORK_EMPTY;
1833 scheduleEvent(eventNames().emptiedEvent);
1835 // 5 - Set the element's delaying-the-load-event flag to false. This stops delaying the load event.
1836 setShouldDelayLoadEvent(false);
1838 // 6 - Abort the overall resource selection algorithm.
1839 m_currentSourceNode = nullptr;
1842 if (is<MediaDocument>(document()))
1843 downcast<MediaDocument>(document()).mediaElementSawUnsupportedTracks();
1847 void HTMLMediaElement::cancelPendingEventsAndCallbacks()
1849 LOG(Media, "HTMLMediaElement::cancelPendingEventsAndCallbacks(%p)", this);
1850 m_asyncEventQueue.cancelAllEvents();
1852 for (auto& source : childrenOfType<HTMLSourceElement>(*this))
1853 source.cancelPendingErrorEvent();
1856 void HTMLMediaElement::mediaPlayerNetworkStateChanged(MediaPlayer*)
1858 beginProcessingMediaPlayerCallback();
1859 setNetworkState(m_player->networkState());
1860 endProcessingMediaPlayerCallback();
1863 static void logMediaLoadRequest(Page* page, const String& mediaEngine, const String& errorMessage, bool succeeded)
1868 DiagnosticLoggingClient& diagnosticLoggingClient = page->mainFrame().diagnosticLoggingClient();
1870 diagnosticLoggingClient.logDiagnosticMessageWithResult(DiagnosticLoggingKeys::mediaLoadingFailedKey(), errorMessage, DiagnosticLoggingResultFail, ShouldSample::No);
1874 diagnosticLoggingClient.logDiagnosticMessage(DiagnosticLoggingKeys::mediaLoadedKey(), mediaEngine, ShouldSample::No);
1876 if (!page->hasSeenAnyMediaEngine())
1877 diagnosticLoggingClient.logDiagnosticMessage(DiagnosticLoggingKeys::pageContainsAtLeastOneMediaEngineKey(), emptyString(), ShouldSample::No);
1879 if (!page->hasSeenMediaEngine(mediaEngine))
1880 diagnosticLoggingClient.logDiagnosticMessage(DiagnosticLoggingKeys::pageContainsMediaEngineKey(), mediaEngine, ShouldSample::No);
1882 page->sawMediaEngine(mediaEngine);
1885 static String stringForNetworkState(MediaPlayer::NetworkState state)
1888 case MediaPlayer::Empty: return ASCIILiteral("Empty");
1889 case MediaPlayer::Idle: return ASCIILiteral("Idle");
1890 case MediaPlayer::Loading: return ASCIILiteral("Loading");
1891 case MediaPlayer::Loaded: return ASCIILiteral("Loaded");
1892 case MediaPlayer::FormatError: return ASCIILiteral("FormatError");
1893 case MediaPlayer::NetworkError: return ASCIILiteral("NetworkError");
1894 case MediaPlayer::DecodeError: return ASCIILiteral("DecodeError");
1895 default: return emptyString();
1899 void HTMLMediaElement::mediaLoadingFailed(MediaPlayer::NetworkState error)
1901 stopPeriodicTimers();
1903 // If we failed while trying to load a <source> element, the movie was never parsed, and there are more
1904 // <source> children, schedule the next one
1905 if (m_readyState < HAVE_METADATA && m_loadState == LoadingFromSourceElement) {
1907 // resource selection algorithm
1908 // 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.
1909 if (m_currentSourceNode)
1910 m_currentSourceNode->scheduleErrorEvent();
1912 LOG(Media, "HTMLMediaElement::setNetworkState(%p) - error event not sent, <source> was removed", this);
1914 // 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.
1916 // 9.Otherwise.11 - Forget the media element's media-resource-specific tracks.
1917 forgetResourceSpecificTracks();
1919 if (havePotentialSourceChild()) {
1920 LOG(Media, "HTMLMediaElement::setNetworkState(%p) - scheduling next <source>", this);
1921 scheduleNextSourceChild();
1923 LOG(Media, "HTMLMediaElement::setNetworkState(%p) - no more <source> elements, waiting", this);
1924 waitForSourceChange();
1930 if ((error == MediaPlayer::NetworkError && m_readyState >= HAVE_METADATA) || error == MediaPlayer::DecodeError)
1931 mediaLoadingFailedFatally(error);
1932 else if ((error == MediaPlayer::FormatError || error == MediaPlayer::NetworkError) && m_loadState == LoadingFromSrcAttr)
1935 updateDisplayState();
1936 if (hasMediaControls()) {
1937 mediaControls()->reset();
1938 mediaControls()->reportedError();
1941 logMediaLoadRequest(document().page(), String(), stringForNetworkState(error), false);
1944 void HTMLMediaElement::setNetworkState(MediaPlayer::NetworkState state)
1946 LOG(Media, "HTMLMediaElement::setNetworkState(%p) - new state = %d, current state = %d", this, static_cast<int>(state), static_cast<int>(m_networkState));
1948 if (state == MediaPlayer::Empty) {
1949 // Just update the cached state and leave, we can't do anything.
1950 m_networkState = NETWORK_EMPTY;
1954 if (state == MediaPlayer::FormatError || state == MediaPlayer::NetworkError || state == MediaPlayer::DecodeError) {
1955 mediaLoadingFailed(state);
1959 if (state == MediaPlayer::Idle) {
1960 if (m_networkState > NETWORK_IDLE) {
1961 changeNetworkStateFromLoadingToIdle();
1962 setShouldDelayLoadEvent(false);
1964 m_networkState = NETWORK_IDLE;
1968 if (state == MediaPlayer::Loading) {
1969 if (m_networkState < NETWORK_LOADING || m_networkState == NETWORK_NO_SOURCE)
1970 startProgressEventTimer();
1971 m_networkState = NETWORK_LOADING;
1974 if (state == MediaPlayer::Loaded) {
1975 if (m_networkState != NETWORK_IDLE)
1976 changeNetworkStateFromLoadingToIdle();
1977 m_completelyLoaded = true;
1980 if (hasMediaControls())
1981 mediaControls()->updateStatusDisplay();
1984 void HTMLMediaElement::changeNetworkStateFromLoadingToIdle()
1986 m_progressEventTimer.stop();
1987 if (hasMediaControls() && m_player->didLoadingProgress())
1988 mediaControls()->bufferingProgressed();
1990 // Schedule one last progress event so we guarantee that at least one is fired
1991 // for files that load very quickly.
1992 scheduleEvent(eventNames().progressEvent);
1993 scheduleEvent(eventNames().suspendEvent);
1994 m_networkState = NETWORK_IDLE;
1997 void HTMLMediaElement::mediaPlayerReadyStateChanged(MediaPlayer*)
1999 beginProcessingMediaPlayerCallback();
2001 setReadyState(m_player->readyState());
2003 endProcessingMediaPlayerCallback();
2006 void HTMLMediaElement::setReadyState(MediaPlayer::ReadyState state)
2008 LOG(Media, "HTMLMediaElement::setReadyState(%p) - new state = %d, current state = %d,", this, static_cast<int>(state), static_cast<int>(m_readyState));
2010 // Set "wasPotentiallyPlaying" BEFORE updating m_readyState, potentiallyPlaying() uses it
2011 bool wasPotentiallyPlaying = potentiallyPlaying();
2013 ReadyState oldState = m_readyState;
2014 ReadyState newState = static_cast<ReadyState>(state);
2016 #if ENABLE(VIDEO_TRACK)
2017 bool tracksAreReady = !RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled() || textTracksAreReady();
2019 if (newState == oldState && m_tracksAreReady == tracksAreReady)
2022 m_tracksAreReady = tracksAreReady;
2024 if (newState == oldState)
2026 bool tracksAreReady = true;
2030 m_readyState = newState;
2032 // If a media file has text tracks the readyState may not progress beyond HAVE_FUTURE_DATA until
2033 // the text tracks are ready, regardless of the state of the media file.
2034 if (newState <= HAVE_METADATA)
2035 m_readyState = newState;
2037 m_readyState = HAVE_CURRENT_DATA;
2040 if (oldState > m_readyStateMaximum)
2041 m_readyStateMaximum = oldState;
2043 if (m_networkState == NETWORK_EMPTY)
2047 // 4.8.10.9, step 11
2048 if (wasPotentiallyPlaying && m_readyState < HAVE_FUTURE_DATA)
2049 scheduleEvent(eventNames().waitingEvent);
2051 // 4.8.10.10 step 14 & 15.
2052 if (!m_player->seeking() && m_readyState >= HAVE_CURRENT_DATA)
2055 if (wasPotentiallyPlaying && m_readyState < HAVE_FUTURE_DATA) {
2057 invalidateCachedTime();
2058 scheduleTimeupdateEvent(false);
2059 scheduleEvent(eventNames().waitingEvent);
2063 if (m_readyState >= HAVE_METADATA && oldState < HAVE_METADATA) {
2064 prepareMediaFragmentURI();
2065 scheduleEvent(eventNames().durationchangeEvent);
2066 scheduleEvent(eventNames().loadedmetadataEvent);
2067 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
2068 if (hasEventListeners(eventNames().webkitplaybacktargetavailabilitychangedEvent))
2069 enqueuePlaybackTargetAvailabilityChangedEvent();
2072 if (hasMediaControls())
2073 mediaControls()->loadedMetadata();
2075 renderer()->updateFromElement();
2077 logMediaLoadRequest(document().page(), m_player->engineDescription(), String(), true);
2080 bool shouldUpdateDisplayState = false;
2082 if (m_readyState >= HAVE_CURRENT_DATA && oldState < HAVE_CURRENT_DATA && !m_haveFiredLoadedData) {
2083 m_haveFiredLoadedData = true;
2084 shouldUpdateDisplayState = true;
2085 scheduleEvent(eventNames().loadeddataEvent);
2086 setShouldDelayLoadEvent(false);
2087 applyMediaFragmentURI();
2090 bool isPotentiallyPlaying = potentiallyPlaying();
2091 if (m_readyState == HAVE_FUTURE_DATA && oldState <= HAVE_CURRENT_DATA && tracksAreReady) {
2092 scheduleEvent(eventNames().canplayEvent);
2093 if (isPotentiallyPlaying)
2094 scheduleEvent(eventNames().playingEvent);
2095 shouldUpdateDisplayState = true;
2098 if (m_readyState == HAVE_ENOUGH_DATA && oldState < HAVE_ENOUGH_DATA && tracksAreReady) {
2099 if (oldState <= HAVE_CURRENT_DATA)
2100 scheduleEvent(eventNames().canplayEvent);
2102 scheduleEvent(eventNames().canplaythroughEvent);
2104 if (isPotentiallyPlaying && oldState <= HAVE_CURRENT_DATA)
2105 scheduleEvent(eventNames().playingEvent);
2107 if (m_autoplaying && m_paused && autoplay() && !document().isSandboxed(SandboxAutomaticFeatures) && m_mediaSession->playbackPermitted(*this)) {
2109 invalidateCachedTime();
2110 scheduleEvent(eventNames().playEvent);
2111 scheduleEvent(eventNames().playingEvent);
2114 shouldUpdateDisplayState = true;
2117 if (shouldUpdateDisplayState) {
2118 updateDisplayState();
2119 if (hasMediaControls()) {
2120 mediaControls()->refreshClosedCaptionsButtonVisibility();
2121 mediaControls()->updateStatusDisplay();
2126 updateMediaController();
2127 #if ENABLE(VIDEO_TRACK)
2128 if (RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
2129 updateActiveTextTrackCues(currentMediaTime());
2133 #if ENABLE(ENCRYPTED_MEDIA)
2134 void HTMLMediaElement::mediaPlayerKeyAdded(MediaPlayer*, const String& keySystem, const String& sessionId)
2136 MediaKeyEventInit initializer;
2137 initializer.keySystem = keySystem;
2138 initializer.sessionId = sessionId;
2139 initializer.bubbles = false;
2140 initializer.cancelable = false;
2142 RefPtr<Event> event = MediaKeyEvent::create(eventNames().webkitkeyaddedEvent, initializer);
2143 event->setTarget(this);
2144 m_asyncEventQueue.enqueueEvent(event.release());
2147 void HTMLMediaElement::mediaPlayerKeyError(MediaPlayer*, const String& keySystem, const String& sessionId, MediaPlayerClient::MediaKeyErrorCode errorCode, unsigned short systemCode)
2149 MediaKeyError::Code mediaKeyErrorCode = MediaKeyError::MEDIA_KEYERR_UNKNOWN;
2150 switch (errorCode) {
2151 case MediaPlayerClient::UnknownError:
2152 mediaKeyErrorCode = MediaKeyError::MEDIA_KEYERR_UNKNOWN;
2154 case MediaPlayerClient::ClientError:
2155 mediaKeyErrorCode = MediaKeyError::MEDIA_KEYERR_CLIENT;
2157 case MediaPlayerClient::ServiceError:
2158 mediaKeyErrorCode = MediaKeyError::MEDIA_KEYERR_SERVICE;
2160 case MediaPlayerClient::OutputError:
2161 mediaKeyErrorCode = MediaKeyError::MEDIA_KEYERR_OUTPUT;
2163 case MediaPlayerClient::HardwareChangeError:
2164 mediaKeyErrorCode = MediaKeyError::MEDIA_KEYERR_HARDWARECHANGE;
2166 case MediaPlayerClient::DomainError:
2167 mediaKeyErrorCode = MediaKeyError::MEDIA_KEYERR_DOMAIN;
2171 MediaKeyEventInit initializer;
2172 initializer.keySystem = keySystem;
2173 initializer.sessionId = sessionId;
2174 initializer.errorCode = MediaKeyError::create(mediaKeyErrorCode);
2175 initializer.systemCode = systemCode;
2176 initializer.bubbles = false;
2177 initializer.cancelable = false;
2179 RefPtr<Event> event = MediaKeyEvent::create(eventNames().webkitkeyerrorEvent, initializer);
2180 event->setTarget(this);
2181 m_asyncEventQueue.enqueueEvent(event.release());
2184 void HTMLMediaElement::mediaPlayerKeyMessage(MediaPlayer*, const String& keySystem, const String& sessionId, const unsigned char* message, unsigned messageLength, const URL& defaultURL)
2186 MediaKeyEventInit initializer;
2187 initializer.keySystem = keySystem;
2188 initializer.sessionId = sessionId;
2189 initializer.message = Uint8Array::create(message, messageLength);
2190 initializer.defaultURL = defaultURL;
2191 initializer.bubbles = false;
2192 initializer.cancelable = false;
2194 RefPtr<Event> event = MediaKeyEvent::create(eventNames().webkitkeymessageEvent, initializer);
2195 event->setTarget(this);
2196 m_asyncEventQueue.enqueueEvent(event.release());
2199 bool HTMLMediaElement::mediaPlayerKeyNeeded(MediaPlayer*, const String& keySystem, const String& sessionId, const unsigned char* initData, unsigned initDataLength)
2201 if (!hasEventListeners(eventNames().webkitneedkeyEvent)) {
2202 m_error = MediaError::create(MediaError::MEDIA_ERR_ENCRYPTED);
2203 scheduleEvent(eventNames().errorEvent);
2207 MediaKeyEventInit initializer;
2208 initializer.keySystem = keySystem;
2209 initializer.sessionId = sessionId;
2210 initializer.initData = Uint8Array::create(initData, initDataLength);
2211 initializer.bubbles = false;
2212 initializer.cancelable = false;
2214 RefPtr<Event> event = MediaKeyEvent::create(eventNames().webkitneedkeyEvent, initializer);
2215 event->setTarget(this);
2216 m_asyncEventQueue.enqueueEvent(event.release());
2221 #if ENABLE(ENCRYPTED_MEDIA_V2)
2222 RefPtr<ArrayBuffer> HTMLMediaElement::mediaPlayerCachedKeyForKeyId(const String& keyId) const
2224 return m_mediaKeys ? m_mediaKeys->cachedKeyForKeyId(keyId) : nullptr;
2227 bool HTMLMediaElement::mediaPlayerKeyNeeded(MediaPlayer*, Uint8Array* initData)
2229 if (!hasEventListeners("webkitneedkey")) {
2230 m_error = MediaError::create(MediaError::MEDIA_ERR_ENCRYPTED);
2231 scheduleEvent(eventNames().errorEvent);
2235 MediaKeyNeededEventInit initializer;
2236 initializer.initData = initData;
2237 initializer.bubbles = false;
2238 initializer.cancelable = false;
2240 RefPtr<Event> event = MediaKeyNeededEvent::create(eventNames().webkitneedkeyEvent, initializer);
2241 event->setTarget(this);
2242 m_asyncEventQueue.enqueueEvent(event.release());
2247 String HTMLMediaElement::mediaPlayerMediaKeysStorageDirectory() const
2249 Settings* settings = document().settings();
2251 return emptyString();
2253 String storageDirectory = settings->mediaKeysStorageDirectory();
2254 if (storageDirectory.isEmpty())
2255 return emptyString();
2257 SecurityOrigin* origin = document().securityOrigin();
2259 return emptyString();
2261 return pathByAppendingComponent(storageDirectory, origin->databaseIdentifier());
2264 void HTMLMediaElement::setMediaKeys(MediaKeys* mediaKeys)
2266 if (m_mediaKeys == mediaKeys)
2270 m_mediaKeys->setMediaElement(0);
2271 m_mediaKeys = mediaKeys;
2273 m_mediaKeys->setMediaElement(this);
2276 void HTMLMediaElement::keyAdded()
2279 m_player->keyAdded();
2283 void HTMLMediaElement::progressEventTimerFired()
2286 if (m_networkState != NETWORK_LOADING)
2289 double time = monotonicallyIncreasingTime();
2290 double timedelta = time - m_previousProgressTime;
2292 if (m_player->didLoadingProgress()) {
2293 scheduleEvent(eventNames().progressEvent);
2294 m_previousProgressTime = time;
2295 m_sentStalledEvent = false;
2297 renderer()->updateFromElement();
2298 if (hasMediaControls())
2299 mediaControls()->bufferingProgressed();
2300 } else if (timedelta > 3.0 && !m_sentStalledEvent) {
2301 scheduleEvent(eventNames().stalledEvent);
2302 m_sentStalledEvent = true;
2303 setShouldDelayLoadEvent(false);
2307 void HTMLMediaElement::rewind(double timeDelta)
2309 LOG(Media, "HTMLMediaElement::rewind(%p) - %f", this, timeDelta);
2310 setCurrentTime(std::max(currentMediaTime() - MediaTime::createWithDouble(timeDelta), minTimeSeekable()));
2313 void HTMLMediaElement::returnToRealtime()
2315 LOG(Media, "HTMLMediaElement::returnToRealtime(%p)", this);
2316 setCurrentTime(maxTimeSeekable());
2319 void HTMLMediaElement::addPlayedRange(const MediaTime& start, const MediaTime& end)
2321 LOG(Media, "HTMLMediaElement::addPlayedRange(%p) - [%s, %s]", this, toString(start).utf8().data(), toString(end).utf8().data());
2322 if (!m_playedTimeRanges)
2323 m_playedTimeRanges = TimeRanges::create();
2324 m_playedTimeRanges->ranges().add(start, end);
2327 bool HTMLMediaElement::supportsScanning() const
2329 return m_player ? m_player->supportsScanning() : false;
2332 void HTMLMediaElement::prepareToPlay()
2334 LOG(Media, "HTMLMediaElement::prepareToPlay(%p)", this);
2335 if (m_havePreparedToPlay)
2337 m_havePreparedToPlay = true;
2338 m_player->prepareToPlay();
2341 void HTMLMediaElement::fastSeek(double time)
2343 fastSeek(MediaTime::createWithDouble(time));
2346 void HTMLMediaElement::fastSeek(const MediaTime& time)
2348 LOG(Media, "HTMLMediaElement::fastSeek(%p) - %s", this, toString(time).utf8().data());
2350 // 9. If the approximate-for-speed flag is set, adjust the new playback position to a value that will
2351 // allow for playback to resume promptly. If new playback position before this step is before current
2352 // playback position, then the adjusted new playback position must also be before the current playback
2353 // position. Similarly, if the new playback position before this step is after current playback position,
2354 // then the adjusted new playback position must also be after the current playback position.
2355 refreshCachedTime();
2356 MediaTime delta = time - currentMediaTime();
2357 MediaTime negativeTolerance = delta >= MediaTime::zeroTime() ? delta : MediaTime::positiveInfiniteTime();
2358 MediaTime positiveTolerance = delta < MediaTime::zeroTime() ? -delta : MediaTime::positiveInfiniteTime();
2360 seekWithTolerance(time, negativeTolerance, positiveTolerance, true);
2363 void HTMLMediaElement::seek(const MediaTime& time)
2365 LOG(Media, "HTMLMediaElement::seek(%p) - %s", this, toString(time).utf8().data());
2366 seekWithTolerance(time, MediaTime::zeroTime(), MediaTime::zeroTime(), true);
2369 void HTMLMediaElement::seekInternal(const MediaTime& time)
2371 LOG(Media, "HTMLMediaElement::seekInternal(%p) - %s", this, toString(time).utf8().data());
2372 seekWithTolerance(time, MediaTime::zeroTime(), MediaTime::zeroTime(), false);
2375 void HTMLMediaElement::seekWithTolerance(const MediaTime& inTime, const MediaTime& negativeTolerance, const MediaTime& positiveTolerance, bool fromDOM)
2378 MediaTime time = inTime;
2380 // 1 - Set the media element's show poster flag to false.
2381 setDisplayMode(Video);
2383 // 2 - If the media element's readyState is HAVE_NOTHING, abort these steps.
2384 if (m_readyState == HAVE_NOTHING || !m_player)
2387 // If the media engine has been told to postpone loading data, let it go ahead now.
2388 if (m_preload < MediaPlayer::Auto && m_readyState < HAVE_FUTURE_DATA)
2391 // Get the current time before setting m_seeking, m_lastSeekTime is returned once it is set.
2392 refreshCachedTime();
2393 MediaTime now = currentMediaTime();
2395 // 3 - If the element's seeking IDL attribute is true, then another instance of this algorithm is
2396 // already running. Abort that other instance of the algorithm without waiting for the step that
2397 // it is running to complete.
2398 if (m_seekTaskQueue.hasPendingTasks()) {
2399 m_seekTaskQueue.cancelAllTasks();
2400 m_pendingSeek = nullptr;
2403 // 4 - Set the seeking IDL attribute to true.
2404 // The flag will be cleared when the engine tells us the time has actually changed.
2407 if (m_lastSeekTime < now)
2408 addPlayedRange(m_lastSeekTime, now);
2410 m_lastSeekTime = time;
2412 // 5 - If the seek was in response to a DOM method call or setting of an IDL attribute, then continue
2413 // the script. The remainder of these steps must be run asynchronously.
2414 m_pendingSeek = std::make_unique<PendingSeek>(now, time, negativeTolerance, positiveTolerance);
2416 m_seekTaskQueue.enqueueTask(std::bind(&HTMLMediaElement::seekTask, this));
2421 void HTMLMediaElement::seekTask()
2428 ASSERT(m_pendingSeek);
2429 MediaTime now = m_pendingSeek->now;
2430 MediaTime time = m_pendingSeek->targetTime;
2431 MediaTime negativeTolerance = m_pendingSeek->negativeTolerance;
2432 MediaTime positiveTolerance = m_pendingSeek->positiveTolerance;
2433 m_pendingSeek = nullptr;
2435 // 6 - If the new playback position is later than the end of the media resource, then let it be the end
2436 // of the media resource instead.
2437 time = std::min(time, durationMediaTime());
2439 // 7 - If the new playback position is less than the earliest possible position, let it be that position instead.
2440 MediaTime earliestTime = m_player->startTime();
2441 time = std::max(time, earliestTime);
2443 // Ask the media engine for the time value in the movie's time scale before comparing with current time. This
2444 // is necessary because if the seek time is not equal to currentTime but the delta is less than the movie's
2445 // time scale, we will ask the media engine to "seek" to the current movie time, which may be a noop and
2446 // not generate a timechanged callback. This means m_seeking will never be cleared and we will never
2447 // fire a 'seeked' event.
2449 MediaTime mediaTime = m_player->mediaTimeForTimeValue(time);
2450 if (time != mediaTime)
2451 LOG(Media, "HTMLMediaElement::seekTimerFired(%p) - %s - media timeline equivalent is %s", this, toString(time).utf8().data(), toString(mediaTime).utf8().data());
2453 time = m_player->mediaTimeForTimeValue(time);
2455 // 8 - If the (possibly now changed) new playback position is not in one of the ranges given in the
2456 // seekable attribute, then let it be the position in one of the ranges given in the seekable attribute
2457 // that is the nearest to the new playback position. ... If there are no ranges given in the seekable
2458 // attribute then set the seeking IDL attribute to false and abort these steps.
2459 RefPtr<TimeRanges> seekableRanges = seekable();
2461 // Short circuit seeking to the current time by just firing the events if no seek is required.
2462 // Don't skip calling the media engine if we are in poster mode because a seek should always
2463 // cancel poster display.
2464 bool noSeekRequired = !seekableRanges->length() || (time == now && displayMode() != Poster);
2466 #if ENABLE(MEDIA_SOURCE)
2467 // Always notify the media engine of a seek if the source is not closed. This ensures that the source is
2468 // always in a flushed state when the 'seeking' event fires.
2469 if (m_mediaSource && !m_mediaSource->isClosed())
2470 noSeekRequired = false;
2473 if (noSeekRequired) {
2475 scheduleEvent(eventNames().seekingEvent);
2476 scheduleTimeupdateEvent(false);
2477 scheduleEvent(eventNames().seekedEvent);
2482 time = seekableRanges->ranges().nearest(time);
2484 m_sentEndEvent = false;
2485 m_lastSeekTime = time;
2487 // 10 - Queue a task to fire a simple event named seeking at the element.
2488 scheduleEvent(eventNames().seekingEvent);
2490 // 11 - Set the current playback position to the given new playback position
2491 m_player->seekWithTolerance(time, negativeTolerance, positiveTolerance);
2493 // 12 - Wait until the user agent has established whether or not the media data for the new playback
2494 // position is available, and, if it is, until it has decoded enough data to play back that position.
2495 // 13 - Await a stable state. The synchronous section consists of all the remaining steps of this algorithm.
2498 void HTMLMediaElement::finishSeek()
2500 LOG(Media, "HTMLMediaElement::finishSeek(%p)", this);
2503 // 14 - Set the seeking IDL attribute to false.
2506 // 15 - Run the time maches on steps.
2507 // Handled by mediaPlayerTimeChanged().
2509 // 16 - Queue a task to fire a simple event named timeupdate at the element.
2510 scheduleEvent(eventNames().timeupdateEvent);
2512 // 17 - Queue a task to fire a simple event named seeked at the element.
2513 scheduleEvent(eventNames().seekedEvent);
2515 #if ENABLE(MEDIA_SOURCE)
2517 m_mediaSource->monitorSourceBuffers();
2521 HTMLMediaElement::ReadyState HTMLMediaElement::readyState() const
2523 return m_readyState;
2526 MediaPlayer::MovieLoadType HTMLMediaElement::movieLoadType() const
2528 return m_player ? m_player->movieLoadType() : MediaPlayer::Unknown;
2531 bool HTMLMediaElement::hasAudio() const
2533 return m_player ? m_player->hasAudio() : false;
2536 bool HTMLMediaElement::seeking() const
2541 void HTMLMediaElement::refreshCachedTime() const
2546 m_cachedTime = m_player->currentTime();
2547 if (!m_cachedTime) {
2548 // Do not use m_cachedTime until the media engine returns a non-zero value because we can't
2549 // estimate current time until playback actually begins.
2550 invalidateCachedTime();
2554 m_clockTimeAtLastCachedTimeUpdate = monotonicallyIncreasingTime();
2557 void HTMLMediaElement::invalidateCachedTime() const
2559 if (!m_player || !m_player->maximumDurationToCacheMediaTime())
2563 if (m_cachedTime.isValid())
2564 LOG(Media, "HTMLMediaElement::invalidateCachedTime(%p)", this);
2567 // Don't try to cache movie time when playback first starts as the time reported by the engine
2568 // sometimes fluctuates for a short amount of time, so the cached time will be off if we take it
2570 static const double minimumTimePlayingBeforeCacheSnapshot = 0.5;
2572 m_minimumClockTimeToUpdateCachedTime = monotonicallyIncreasingTime() + minimumTimePlayingBeforeCacheSnapshot;
2573 m_cachedTime = MediaTime::invalidTime();
2577 double HTMLMediaElement::currentTime() const
2579 return currentMediaTime().toDouble();
2582 MediaTime HTMLMediaElement::currentMediaTime() const
2584 #if LOG_CACHED_TIME_WARNINGS
2585 static const MediaTime minCachedDeltaForWarning = MediaTime::create(1, 100);
2589 return MediaTime::zeroTime();
2592 LOG(Media, "HTMLMediaElement::currentTime(%p) - seeking, returning %s", this, toString(m_lastSeekTime).utf8().data());
2593 return m_lastSeekTime;
2596 if (m_cachedTime.isValid() && m_paused) {
2597 #if LOG_CACHED_TIME_WARNINGS
2598 MediaTime delta = m_cachedTime - m_player->currentTime();
2599 if (delta > minCachedDeltaForWarning)
2600 LOG(Media, "HTMLMediaElement::currentTime(%p) - WARNING, cached time is %s seconds off of media time when paused", this, toString(delta).utf8().data());
2602 return m_cachedTime;
2605 // Is it too soon use a cached time?
2606 double now = monotonicallyIncreasingTime();
2607 double maximumDurationToCacheMediaTime = m_player->maximumDurationToCacheMediaTime();
2609 if (maximumDurationToCacheMediaTime && m_cachedTime.isValid() && !m_paused && now > m_minimumClockTimeToUpdateCachedTime) {
2610 double clockDelta = now - m_clockTimeAtLastCachedTimeUpdate;
2612 // Not too soon, use the cached time only if it hasn't expired.
2613 if (clockDelta < maximumDurationToCacheMediaTime) {
2614 MediaTime adjustedCacheTime = m_cachedTime + MediaTime::createWithDouble(effectivePlaybackRate() * clockDelta);
2616 #if LOG_CACHED_TIME_WARNINGS
2617 MediaTime delta = adjustedCacheTime - m_player->currentTime();
2618 if (delta > minCachedDeltaForWarning)
2619 LOG(Media, "HTMLMediaElement::currentTime(%p) - WARNING, cached time is %f seconds off of media time when playing", this, delta);
2621 return adjustedCacheTime;
2625 #if LOG_CACHED_TIME_WARNINGS
2626 if (maximumDurationToCacheMediaTime && now > m_minimumClockTimeToUpdateCachedTime && m_cachedTime != MediaPlayer::invalidTime()) {
2627 double clockDelta = now - m_clockTimeAtLastCachedTimeUpdate;
2628 MediaTime delta = m_cachedTime + MediaTime::createWithDouble(effectivePlaybackRate() * clockDelta) - m_player->currentTime();
2629 LOG(Media, "HTMLMediaElement::currentTime(%p) - cached time was %s seconds off of media time when it expired", this, toString(delta).utf8().data());
2633 refreshCachedTime();
2635 if (m_cachedTime.isInvalid())
2636 return MediaTime::zeroTime();
2638 return m_cachedTime;
2641 void HTMLMediaElement::setCurrentTime(double time)
2643 setCurrentTime(MediaTime::createWithDouble(time));
2646 void HTMLMediaElement::setCurrentTime(const MediaTime& time)
2648 if (m_mediaController)
2654 void HTMLMediaElement::setCurrentTime(double time, ExceptionCode& ec)
2656 // On setting, if the media element has a current media controller, then the user agent must
2657 // throw an InvalidStateError exception
2658 if (m_mediaController) {
2659 ec = INVALID_STATE_ERR;
2663 seek(MediaTime::createWithDouble(time));
2666 double HTMLMediaElement::duration() const
2668 return durationMediaTime().toDouble();
2671 MediaTime HTMLMediaElement::durationMediaTime() const
2673 if (m_player && m_readyState >= HAVE_METADATA)
2674 return m_player->duration();
2676 return MediaTime::invalidTime();
2679 bool HTMLMediaElement::paused() const
2681 // As of this writing, JavaScript garbage collection calls this function directly. In the past
2682 // we had problems where this was called on an object after a bad cast. The assertion below
2683 // made our regression test detect the problem, so we should keep it because of that. But note
2684 // that the value of the assertion relies on the compiler not being smart enough to know that
2685 // isHTMLUnknownElement is guaranteed to return false for an HTMLMediaElement.
2686 ASSERT(!isHTMLUnknownElement());
2691 double HTMLMediaElement::defaultPlaybackRate() const
2693 return m_defaultPlaybackRate;
2696 void HTMLMediaElement::setDefaultPlaybackRate(double rate)
2698 if (m_defaultPlaybackRate != rate) {
2699 LOG(Media, "HTMLMediaElement::setDefaultPlaybackRate(%p) - %f", this, rate);
2700 m_defaultPlaybackRate = rate;
2701 scheduleEvent(eventNames().ratechangeEvent);
2705 double HTMLMediaElement::effectivePlaybackRate() const
2707 return m_mediaController ? m_mediaController->playbackRate() : m_reportedPlaybackRate;
2710 double HTMLMediaElement::requestedPlaybackRate() const
2712 return m_mediaController ? m_mediaController->playbackRate() : m_requestedPlaybackRate;
2715 double HTMLMediaElement::playbackRate() const
2717 return m_requestedPlaybackRate;
2720 void HTMLMediaElement::setPlaybackRate(double rate)
2722 LOG(Media, "HTMLMediaElement::setPlaybackRate(%p) - %f", this, rate);
2724 if (m_player && potentiallyPlaying() && m_player->rate() != rate && !m_mediaController)
2725 m_player->setRate(rate);
2727 if (m_requestedPlaybackRate != rate) {
2728 m_reportedPlaybackRate = m_requestedPlaybackRate = rate;
2729 invalidateCachedTime();
2730 scheduleEvent(eventNames().ratechangeEvent);
2734 void HTMLMediaElement::updatePlaybackRate()
2736 double requestedRate = requestedPlaybackRate();
2737 if (m_player && potentiallyPlaying() && m_player->rate() != requestedRate)
2738 m_player->setRate(requestedRate);
2741 bool HTMLMediaElement::webkitPreservesPitch() const
2743 return m_webkitPreservesPitch;
2746 void HTMLMediaElement::setWebkitPreservesPitch(bool preservesPitch)
2748 LOG(Media, "HTMLMediaElement::setWebkitPreservesPitch(%p) - %s", this, boolString(preservesPitch));
2750 m_webkitPreservesPitch = preservesPitch;
2755 m_player->setPreservesPitch(preservesPitch);
2758 bool HTMLMediaElement::ended() const
2760 // 4.8.10.8 Playing the media resource
2761 // The ended attribute must return true if the media element has ended
2762 // playback and the direction of playback is forwards, and false otherwise.
2763 return endedPlayback() && requestedPlaybackRate() > 0;
2766 bool HTMLMediaElement::autoplay() const
2768 return fastHasAttribute(autoplayAttr);
2771 String HTMLMediaElement::preload() const
2773 switch (m_preload) {
2774 case MediaPlayer::None:
2775 return ASCIILiteral("none");
2776 case MediaPlayer::MetaData:
2777 return ASCIILiteral("metadata");
2778 case MediaPlayer::Auto:
2779 return ASCIILiteral("auto");
2782 ASSERT_NOT_REACHED();
2786 void HTMLMediaElement::setPreload(const String& preload)
2788 LOG(Media, "HTMLMediaElement::setPreload(%p) - %s", this, preload.utf8().data());
2789 setAttribute(preloadAttr, preload);
2792 void HTMLMediaElement::play()
2794 LOG(Media, "HTMLMediaElement::play(%p)", this);
2796 if (!m_mediaSession->playbackPermitted(*this))
2798 if (ScriptController::processingUserGesture())
2799 removeBehaviorsRestrictionsAfterFirstUserGesture();
2804 void HTMLMediaElement::playInternal()
2806 LOG(Media, "HTMLMediaElement::playInternal(%p)", this);
2808 if (!m_mediaSession->clientWillBeginPlayback()) {
2809 LOG(Media, " returning because of interruption");
2813 // 4.8.10.9. Playing the media resource
2814 if (!m_player || m_networkState == NETWORK_EMPTY)
2815 scheduleDelayedAction(LoadMediaResource);
2817 if (endedPlayback())
2818 seekInternal(MediaTime::zeroTime());
2820 if (m_mediaController)
2821 m_mediaController->bringElementUpToSpeed(this);
2825 invalidateCachedTime();
2826 scheduleEvent(eventNames().playEvent);
2828 if (m_readyState <= HAVE_CURRENT_DATA)
2829 scheduleEvent(eventNames().waitingEvent);
2830 else if (m_readyState >= HAVE_FUTURE_DATA)
2831 scheduleEvent(eventNames().playingEvent);
2833 #if ENABLE(MEDIA_SESSION)
2834 // 6.3 Activating a media session from a media element
2835 // When the play() method is invoked, the paused attribute is true, and the readyState attribute has the value
2836 // HAVE_FUTURE_DATA or HAVE_ENOUGH_DATA, then
2837 // 1. Let media session be the value of the current media session.
2838 // 2. If we are not currently in media session's list of active participating media elements then append
2839 // ourselves to this list.
2840 if (m_readyState == HAVE_ENOUGH_DATA || m_readyState == HAVE_FUTURE_DATA) {
2842 m_session->addActiveMediaElement(*this);
2846 m_autoplaying = false;
2848 updateMediaController();
2851 void HTMLMediaElement::pause()
2853 LOG(Media, "HTMLMediaElement::pause(%p)", this);
2855 if (!m_mediaSession->playbackPermitted(*this))
2862 void HTMLMediaElement::pauseInternal()
2864 LOG(Media, "HTMLMediaElement::pauseInternal(%p)", this);
2866 if (!m_mediaSession->clientWillPausePlayback()) {
2867 LOG(Media, " returning because of interruption");
2871 // 4.8.10.9. Playing the media resource
2872 if (!m_player || m_networkState == NETWORK_EMPTY) {
2873 // Unless the restriction on media requiring user action has been lifted
2874 // don't trigger loading if a script calls pause().
2875 if (!m_mediaSession->playbackPermitted(*this))
2877 scheduleDelayedAction(LoadMediaResource);
2880 m_autoplaying = false;
2884 scheduleTimeupdateEvent(false);
2885 scheduleEvent(eventNames().pauseEvent);
2891 #if ENABLE(MEDIA_SOURCE)
2892 void HTMLMediaElement::closeMediaSource()
2897 m_mediaSource->close();
2902 #if ENABLE(ENCRYPTED_MEDIA)
2903 void HTMLMediaElement::webkitGenerateKeyRequest(const String& keySystem, PassRefPtr<Uint8Array> initData, ExceptionCode& ec)
2905 #if ENABLE(ENCRYPTED_MEDIA_V2)
2906 static bool firstTime = true;
2907 if (firstTime && scriptExecutionContext()) {
2908 scriptExecutionContext()->addConsoleMessage(MessageSource::JS, MessageLevel::Warning, ASCIILiteral("'HTMLMediaElement.webkitGenerateKeyRequest()' is deprecated. Use 'MediaKeys.createSession()' instead."));
2913 if (keySystem.isEmpty()) {
2919 ec = INVALID_STATE_ERR;
2923 const unsigned char* initDataPointer = 0;
2924 unsigned initDataLength = 0;
2926 initDataPointer = initData->data();
2927 initDataLength = initData->length();
2930 MediaPlayer::MediaKeyException result = m_player->generateKeyRequest(keySystem, initDataPointer, initDataLength);
2931 ec = exceptionCodeForMediaKeyException(result);
2934 void HTMLMediaElement::webkitGenerateKeyRequest(const String& keySystem, ExceptionCode& ec)
2936 webkitGenerateKeyRequest(keySystem, Uint8Array::create(0), ec);
2939 void HTMLMediaElement::webkitAddKey(const String& keySystem, PassRefPtr<Uint8Array> key, PassRefPtr<Uint8Array> initData, const String& sessionId, ExceptionCode& ec)
2941 #if ENABLE(ENCRYPTED_MEDIA_V2)
2942 static bool firstTime = true;
2943 if (firstTime && scriptExecutionContext()) {
2944 scriptExecutionContext()->addConsoleMessage(MessageSource::JS, MessageLevel::Warning, ASCIILiteral("'HTMLMediaElement.webkitAddKey()' is deprecated. Use 'MediaKeySession.update()' instead."));
2949 if (keySystem.isEmpty()) {
2959 if (!key->length()) {
2960 ec = TYPE_MISMATCH_ERR;
2965 ec = INVALID_STATE_ERR;
2969 const unsigned char* initDataPointer = 0;
2970 unsigned initDataLength = 0;
2972 initDataPointer = initData->data();
2973 initDataLength = initData->length();
2976 MediaPlayer::MediaKeyException result = m_player->addKey(keySystem, key->data(), key->length(), initDataPointer, initDataLength, sessionId);
2977 ec = exceptionCodeForMediaKeyException(result);
2980 void HTMLMediaElement::webkitAddKey(const String& keySystem, PassRefPtr<Uint8Array> key, ExceptionCode& ec)
2982 webkitAddKey(keySystem, key, Uint8Array::create(0), String(), ec);
2985 void HTMLMediaElement::webkitCancelKeyRequest(const String& keySystem, const String& sessionId, ExceptionCode& ec)
2987 if (keySystem.isEmpty()) {
2993 ec = INVALID_STATE_ERR;
2997 MediaPlayer::MediaKeyException result = m_player->cancelKeyRequest(keySystem, sessionId);
2998 ec = exceptionCodeForMediaKeyException(result);
3003 bool HTMLMediaElement::loop() const
3005 return fastHasAttribute(loopAttr);
3008 void HTMLMediaElement::setLoop(bool b)
3010 LOG(Media, "HTMLMediaElement::setLoop(%p) - %s", this, boolString(b));
3011 setBooleanAttribute(loopAttr, b);
3014 bool HTMLMediaElement::controls() const
3016 Frame* frame = document().frame();
3018 // always show controls when scripting is disabled
3019 if (frame && !frame->script().canExecuteScripts(NotAboutToExecuteScript))
3022 return fastHasAttribute(controlsAttr);
3025 void HTMLMediaElement::setControls(bool b)
3027 LOG(Media, "HTMLMediaElement::setControls(%p) - %s", this, boolString(b));
3028 setBooleanAttribute(controlsAttr, b);
3031 double HTMLMediaElement::volume() const
3036 void HTMLMediaElement::setVolume(double vol, ExceptionCode& ec)
3038 LOG(Media, "HTMLMediaElement::setVolume(%p) - %f", this, vol);
3040 if (vol < 0.0f || vol > 1.0f) {
3041 ec = INDEX_SIZE_ERR;
3046 if (m_volume != vol) {
3048 m_volumeInitialized = true;
3050 scheduleEvent(eventNames().volumechangeEvent);
3055 bool HTMLMediaElement::muted() const
3057 return m_explicitlyMuted ? m_muted : fastHasAttribute(mutedAttr);
3060 void HTMLMediaElement::setMuted(bool muted)
3062 LOG(Media, "HTMLMediaElement::setMuted(%p) - %s", this, boolString(muted));
3065 UNUSED_PARAM(muted);
3067 if (m_muted != muted || !m_explicitlyMuted) {
3069 m_explicitlyMuted = true;
3070 // Avoid recursion when the player reports volume changes.
3071 if (!processingMediaPlayerCallback()) {
3073 m_player->setMuted(effectiveMuted());
3074 if (hasMediaControls())
3075 mediaControls()->changedMute();
3078 scheduleEvent(eventNames().volumechangeEvent);
3079 document().updateIsPlayingMedia();
3081 #if ENABLE(WIRELESS_PLAYBACK_TARGET)
3088 void HTMLMediaElement::togglePlayState()
3090 LOG(Media, "HTMLMediaElement::togglePlayState(%p) - canPlay() is %s", this, boolString(canPlay()));
3092 // We can safely call the internal play/pause methods, which don't check restrictions, because
3093 // this method is only called from the built-in media controller
3095 updatePlaybackRate();
3101 void HTMLMediaElement::beginScrubbing()
3103 LOG(Media, "HTMLMediaElement::beginScrubbing(%p) - paused() is %s", this, boolString(paused()));
3107 // Because a media element stays in non-paused state when it reaches end, playback resumes
3108 // when the slider is dragged from the end to another position unless we pause first. Do
3109 // a "hard pause" so an event is generated, since we want to stay paused after scrubbing finishes.
3112 // Not at the end but we still want to pause playback so the media engine doesn't try to
3113 // continue playing during scrubbing. Pause without generating an event as we will
3114 // unpause after scrubbing finishes.
3115 setPausedInternal(true);
3120 void HTMLMediaElement::endScrubbing()
3122 LOG(Media, "HTMLMediaElement::endScrubbing(%p) - m_pausedInternal is %s", this, boolString(m_pausedInternal));
3124 if (m_pausedInternal)
3125 setPausedInternal(false);
3128 void HTMLMediaElement::beginScanning(ScanDirection direction)
3130 m_scanType = supportsScanning() ? Scan : Seek;
3131 m_scanDirection = direction;
3133 if (m_scanType == Seek) {
3134 // Scanning by seeking requires the video to be paused during scanning.
3135 m_actionAfterScan = paused() ? Nothing : Play;
3138 // Scanning by scanning requires the video to be playing during scanninging.
3139 m_actionAfterScan = paused() ? Pause : Nothing;
3141 setPlaybackRate(nextScanRate());
3144 m_scanTimer.start(0, m_scanType == Seek ? SeekRepeatDelay : ScanRepeatDelay);
3147 void HTMLMediaElement::endScanning()
3149 if (m_scanType == Scan)
3150 setPlaybackRate(defaultPlaybackRate());
3152 if (m_actionAfterScan == Play)
3154 else if (m_actionAfterScan == Pause)
3157 if (m_scanTimer.isActive())
3161 double HTMLMediaElement::nextScanRate()
3163 double rate = std::min(ScanMaximumRate, fabs(playbackRate() * 2));
3164 if (m_scanDirection == Backward)
3167 rate = std::min(std::max(rate, minFastReverseRate()), maxFastForwardRate());
3172 void HTMLMediaElement::scanTimerFired()
3174 if (m_scanType == Seek) {
3175 double seekTime = m_scanDirection == Forward ? SeekTime : -SeekTime;
3176 setCurrentTime(currentTime() + seekTime);
3178 setPlaybackRate(nextScanRate());
3181 // The spec says to fire periodic timeupdate events (those sent while playing) every
3182 // "15 to 250ms", we choose the slowest frequency
3183 static const double maxTimeupdateEventFrequency = 0.25;
3185 void HTMLMediaElement::startPlaybackProgressTimer()
3187 if (m_playbackProgressTimer.isActive())
3190 m_previousProgressTime = monotonicallyIncreasingTime();
3191 m_playbackProgressTimer.startRepeating(maxTimeupdateEventFrequency);
3194 void HTMLMediaElement::playbackProgressTimerFired()
3198 if (m_fragmentEndTime.isValid() && currentMediaTime() >= m_fragmentEndTime && requestedPlaybackRate() > 0) {
3199 m_fragmentEndTime = MediaTime::invalidTime();
3200 if (!m_mediaController && !m_paused) {
3201 // changes paused to true and fires a simple event named pause at the media element.
3206 scheduleTimeupdateEvent(true);
3208 if (!requestedPlaybackRate())
3211 if (!m_paused && hasMediaControls())
3212 mediaControls()->playbackProgressed();
3214 #if ENABLE(VIDEO_TRACK)
3215 if (RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
3216 updateActiveTextTrackCues(currentMediaTime());
3219 #if ENABLE(MEDIA_SOURCE)
3221 m_mediaSource->monitorSourceBuffers();
3225 void HTMLMediaElement::scheduleTimeupdateEvent(bool periodicEvent)
3227 double now = monotonicallyIncreasingTime();
3228 double timedelta = now - m_clockTimeAtLastUpdateEvent;
3230 // throttle the periodic events
3231 if (periodicEvent && timedelta < maxTimeupdateEventFrequency)
3234 // Some media engines make multiple "time changed" callbacks at the same time, but we only want one
3235 // event at a given time so filter here
3236 MediaTime movieTime = currentMediaTime();
3237 if (movieTime != m_lastTimeUpdateEventMovieTime) {
3238 scheduleEvent(eventNames().timeupdateEvent);
3239 m_clockTimeAtLastUpdateEvent = now;
3240 m_lastTimeUpdateEventMovieTime = movieTime;
3244 bool HTMLMediaElement::canPlay() const
3246 return paused() || ended() || m_readyState < HAVE_METADATA;
3249 double HTMLMediaElement::percentLoaded() const
3253 MediaTime duration = m_player->duration();
3255 if (!duration || duration.isPositiveInfinite() || duration.isNegativeInfinite())
3258 MediaTime buffered = MediaTime::zeroTime();
3260 std::unique_ptr<PlatformTimeRanges> timeRanges = m_player->buffered();
3261 for (unsigned i = 0; i < timeRanges->length(); ++i) {
3262 MediaTime start = timeRanges->start(i, ignored);
3263 MediaTime end = timeRanges->end(i, ignored);
3264 buffered += end - start;
3266 return buffered.toDouble() / duration.toDouble();
3269 #if ENABLE(VIDEO_TRACK)
3271 void HTMLMediaElement::mediaPlayerDidAddAudioTrack(PassRefPtr<AudioTrackPrivate> prpTrack)
3273 if (isPlaying() && !m_mediaSession->playbackPermitted(*this))
3276 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
3279 addAudioTrack(AudioTrack::create(this, prpTrack));
3282 void HTMLMediaElement::mediaPlayerDidAddTextTrack(PassRefPtr<InbandTextTrackPrivate> prpTrack)
3284 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
3287 // 4.8.10.12.2 Sourcing in-band text tracks
3288 // 1. Associate the relevant data with a new text track and its corresponding new TextTrack object.
3289 RefPtr<InbandTextTrack> textTrack = InbandTextTrack::create(ActiveDOMObject::scriptExecutionContext(), this, prpTrack);
3290 textTrack->setMediaElement(this);
3292 // 2. Set the new text track's kind, label, and language based on the semantics of the relevant data,
3293 // as defined by the relevant specification. If there is no label in that data, then the label must
3294 // be set to the empty string.
3295 // 3. Associate the text track list of cues with the rules for updating the text track rendering appropriate
3296 // for the format in question.
3297 // 4. If the new text track's kind is metadata, then set the text track in-band metadata track dispatch type
3298 // as follows, based on the type of the media resource:
3299 // 5. Populate the new text track's list of cues with the cues parsed so far, folllowing the guidelines for exposing
3300 // cues, and begin updating it dynamically as necessary.
3301 // - Thess are all done by the media engine.
3303 // 6. Set the new text track's readiness state to loaded.
3304 textTrack->setReadinessState(TextTrack::Loaded);
3306 // 7. Set the new text track's mode to the mode consistent with the user's preferences and the requirements of
3307 // the relevant specification for the data.
3308 // - This will happen in configureTextTracks()
3309 scheduleDelayedAction(ConfigureTextTracks);
3311 // 8. Add the new text track to the media element's list of text tracks.
3312 // 9. Fire an event with the name addtrack, that does not bubble and is not cancelable, and that uses the TrackEvent
3313 // interface, with the track attribute initialized to the text track's TextTrack object, at the media element's
3314 // textTracks attribute's TextTrackList object.
3315 addTextTrack(textTrack.release());
3318 void HTMLMediaElement::mediaPlayerDidAddVideoTrack(PassRefPtr<VideoTrackPrivate> prpTrack)
3320 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
3323 addVideoTrack(VideoTrack::create(this, prpTrack));
3326 void HTMLMediaElement::mediaPlayerDidRemoveAudioTrack(PassRefPtr<AudioTrackPrivate> prpTrack)
3328 prpTrack->willBeRemoved();
3331 void HTMLMediaElement::mediaPlayerDidRemoveTextTrack(PassRefPtr<InbandTextTrackPrivate> prpTrack)
3333 prpTrack->willBeRemoved();
3336 void HTMLMediaElement::mediaPlayerDidRemoveVideoTrack(PassRefPtr<VideoTrackPrivate> prpTrack)
3338 prpTrack->willBeRemoved();
3341 #if USE(PLATFORM_TEXT_TRACK_MENU)
3342 void HTMLMediaElement::setSelectedTextTrack(PassRefPtr<PlatformTextTrack> platformTrack)
3347 TrackDisplayUpdateScope scope(this);
3349 if (!platformTrack) {
3350 setSelectedTextTrack(TextTrack::captionMenuOffItem());
3354 TextTrack* textTrack;
3355 if (platformTrack == PlatformTextTrack::captionMenuOffItem())
3356 textTrack = TextTrack::captionMenuOffItem();
3357 else if (platformTrack == PlatformTextTrack::captionMenuAutomaticItem())
3358 textTrack = TextTrack::captionMenuAutomaticItem();
3361 for (i = 0; i < m_textTracks->length(); ++i) {
3362 textTrack = m_textTracks->item(i);
3364 if (textTrack->platformTextTrack() == platformTrack)
3367 if (i == m_textTracks->length())
3371 setSelectedTextTrack(textTrack);
3374 Vector<RefPtr<PlatformTextTrack>> HTMLMediaElement::platformTextTracks()
3376 if (!m_textTracks || !m_textTracks->length())
3377 return Vector<RefPtr<PlatformTextTrack>>();
3379 Vector<RefPtr<PlatformTextTrack>> platformTracks;
3380 for (size_t i = 0; i < m_textTracks->length(); ++i)
3381 platformTracks.append(m_textTracks->item(i)->platformTextTrack());
3383 return platformTracks;
3386 void HTMLMediaElement::notifyMediaPlayerOfTextTrackChanges()
3388 if (!m_textTracks || !m_textTracks->length() || !platformTextTrackMenu())
3391 m_platformMenu->tracksDidChange();
3394 PlatformTextTrackMenuInterface* HTMLMediaElement::platformTextTrackMenu()
3397 return m_platformMenu.get();
3399 if (!m_player || !m_player->implementsTextTrackControls())
3402 m_platformMenu = m_player->textTrackMenu();
3403 if (!m_platformMenu)
3406 m_platformMenu->setClient(this);
3408 return m_platformMenu.get();
3410 #endif // #if USE(PLATFORM_TEXT_TRACK_MENU)
3412 void HTMLMediaElement::closeCaptionTracksChanged()
3414 if (hasMediaControls())
3415 mediaControls()->closedCaptionTracksChanged();
3417 #if USE(PLATFORM_TEXT_TRACK_MENU)
3418 if (m_player && m_player->implementsTextTrackControls())
3419 scheduleDelayedAction(TextTrackChangesNotification);
3423 void HTMLMediaElement::addAudioTrack(PassRefPtr<AudioTrack> track)
3425 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
3428 audioTracks()->append(track);
3431 void HTMLMediaElement::addTextTrack(PassRefPtr<TextTrack> track)
3433 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
3436 if (!m_requireCaptionPreferencesChangedCallbacks) {
3437 m_requireCaptionPreferencesChangedCallbacks = true;
3438 document().registerForCaptionPreferencesChangedCallbacks(this);
3441 textTracks()->append(track);
3443 closeCaptionTracksChanged();
3446 void HTMLMediaElement::addVideoTrack(PassRefPtr<VideoTrack> track)
3448 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
3451 videoTracks()->append(track);
3454 void HTMLMediaElement::removeAudioTrack(AudioTrack* track)
3456 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
3459 m_audioTracks->remove(track);
3462 void HTMLMediaElement::removeTextTrack(TextTrack* track, bool scheduleEvent)
3464 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
3467 TrackDisplayUpdateScope scope(this);
3468 TextTrackCueList* cues = track->cues();
3470 textTrackRemoveCues(track, cues);
3471 track->clearClient();
3472 m_textTracks->remove(track, scheduleEvent);
3474 closeCaptionTracksChanged();
3477 void HTMLMediaElement::removeVideoTrack(VideoTrack* track)
3479 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
3482 m_videoTracks->remove(track);
3485 void HTMLMediaElement::forgetResourceSpecificTracks()
3487 while (m_audioTracks && m_audioTracks->length())
3488 removeAudioTrack(m_audioTracks->lastItem());
3491 TrackDisplayUpdateScope scope(this);
3492 for (int i = m_textTracks->length() - 1; i >= 0; --i) {
3493 TextTrack* track = m_textTracks->item(i);
3495 if (track->trackType() == TextTrack::InBand)
3496 removeTextTrack(track, false);
3500 while (m_videoTracks && m_videoTracks->length())
3501 removeVideoTrack(m_videoTracks->lastItem());
3504 PassRefPtr<TextTrack> HTMLMediaElement::addTextTrack(const String& kind, const String& label, const String& language, ExceptionCode& ec)
3506 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
3509 // 4.8.10.12.4 Text track API
3510 // The addTextTrack(kind, label, language) method of media elements, when invoked, must run the following steps:
3512 // 1. If kind is not one of the following strings, then throw a SyntaxError exception and abort these steps
3513 if (!TextTrack::isValidKindKeyword(kind)) {
3518 // 2. If the label argument was omitted, let label be the empty string.