2 * Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012, 2013 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 "DiagnosticLoggingKeys.h"
41 #include "DocumentLoader.h"
42 #include "ElementIterator.h"
43 #include "EventNames.h"
44 #include "ExceptionCodePlaceholder.h"
45 #include "FrameLoader.h"
46 #include "FrameLoaderClient.h"
47 #include "FrameView.h"
48 #include "HTMLSourceElement.h"
49 #include "HTMLVideoElement.h"
50 #include "JSHTMLMediaElement.h"
53 #include "MIMETypeRegistry.h"
54 #include "MainFrame.h"
55 #include "MediaController.h"
56 #include "MediaControls.h"
57 #include "MediaDocument.h"
58 #include "MediaError.h"
59 #include "MediaFragmentURIParser.h"
60 #include "MediaKeyEvent.h"
61 #include "MediaList.h"
62 #include "MediaQueryEvaluator.h"
63 #include "MediaSessionManager.h"
64 #include "PageActivityAssertionToken.h"
65 #include "PageGroup.h"
66 #include "ProgressTracker.h"
67 #include "RenderVideo.h"
68 #include "RenderView.h"
69 #include "ScriptController.h"
70 #include "ScriptSourceCode.h"
71 #include "SecurityPolicy.h"
73 #include "ShadowRoot.h"
74 #include "TimeRanges.h"
76 #include <runtime/Uint8Array.h>
77 #include <wtf/CurrentTime.h>
78 #include <wtf/MathExtras.h>
80 #include <wtf/text/CString.h>
82 #if USE(ACCELERATED_COMPOSITING)
83 #include "RenderLayerCompositor.h"
86 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
87 #include "RenderEmbeddedObject.h"
88 #include "SubframeLoader.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(IOS_AIRPLAY)
114 #include "WebKitPlaybackTargetAvailabilityEvent.h"
118 #include "DisplaySleepDisabler.h"
121 #if ENABLE(MEDIA_SOURCE)
122 #include "DOMWindow.h"
123 #include "HTMLMediaSource.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 "UserAgentScripts.h"
147 #include <bindings/ScriptObject.h>
152 static void setFlags(unsigned& value, unsigned flags)
157 static void clearFlags(unsigned& value, unsigned flags)
163 static String urlForLoggingMedia(const URL& url)
165 static const unsigned maximumURLLengthForLogging = 128;
167 if (url.string().length() < maximumURLLengthForLogging)
169 return url.string().substring(0, maximumURLLengthForLogging) + "...";
172 static const char* boolString(bool val)
174 return val ? "true" : "false";
178 #ifndef LOG_MEDIA_EVENTS
179 // Default to not logging events because so many are generated they can overwhelm the rest of
181 #define LOG_MEDIA_EVENTS 0
184 #ifndef LOG_CACHED_TIME_WARNINGS
185 // Default to not logging warnings about excessive drift in the cached media time because it adds a
186 // fair amount of overhead and logging.
187 #define LOG_CACHED_TIME_WARNINGS 0
190 #if ENABLE(MEDIA_SOURCE)
191 // URL protocol used to signal that the media source API is being used.
192 static const char* mediaSourceBlobProtocol = "blob";
195 using namespace HTMLNames;
197 typedef HashMap<Document*, HashSet<HTMLMediaElement*>> DocumentElementSetMap;
198 static DocumentElementSetMap& documentToElementSetMap()
200 DEFINE_STATIC_LOCAL(DocumentElementSetMap, map, ());
204 static void addElementToDocumentMap(HTMLMediaElement& element, Document& document)
206 DocumentElementSetMap& map = documentToElementSetMap();
207 HashSet<HTMLMediaElement*> set = map.take(&document);
209 map.add(&document, set);
212 static void removeElementFromDocumentMap(HTMLMediaElement& element, Document& document)
214 DocumentElementSetMap& map = documentToElementSetMap();
215 HashSet<HTMLMediaElement*> set = map.take(&document);
216 set.remove(&element);
218 map.add(&document, set);
221 #if ENABLE(ENCRYPTED_MEDIA)
222 static ExceptionCode exceptionCodeForMediaKeyException(MediaPlayer::MediaKeyException exception)
225 case MediaPlayer::NoError:
227 case MediaPlayer::InvalidPlayerState:
228 return INVALID_STATE_ERR;
229 case MediaPlayer::KeySystemNotSupported:
230 return NOT_SUPPORTED_ERR;
233 ASSERT_NOT_REACHED();
234 return INVALID_STATE_ERR;
238 #if ENABLE(VIDEO_TRACK)
239 class TrackDisplayUpdateScope {
241 TrackDisplayUpdateScope(HTMLMediaElement* mediaElement)
243 m_mediaElement = mediaElement;
244 m_mediaElement->beginIgnoringTrackDisplayUpdateRequests();
246 ~TrackDisplayUpdateScope()
248 ASSERT(m_mediaElement);
249 m_mediaElement->endIgnoringTrackDisplayUpdateRequests();
253 HTMLMediaElement* m_mediaElement;
257 HTMLMediaElement::HTMLMediaElement(const QualifiedName& tagName, Document& document, bool createdByParser)
258 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
259 : HTMLFrameOwnerElement(tagName, document)
261 : HTMLElement(tagName, document)
263 , ActiveDOMObject(&document)
264 , m_loadTimer(this, &HTMLMediaElement::loadTimerFired)
265 , m_progressEventTimer(this, &HTMLMediaElement::progressEventTimerFired)
266 , m_playbackProgressTimer(this, &HTMLMediaElement::playbackProgressTimerFired)
267 , m_playedTimeRanges()
268 , m_asyncEventQueue(*this)
269 , m_playbackRate(1.0f)
270 , m_defaultPlaybackRate(1.0f)
271 , m_webkitPreservesPitch(true)
272 , m_networkState(NETWORK_EMPTY)
273 , m_readyState(HAVE_NOTHING)
274 , m_readyStateMaximum(HAVE_NOTHING)
276 , m_volumeInitialized(false)
278 , m_previousProgressTime(std::numeric_limits<double>::max())
279 , m_clockTimeAtLastUpdateEvent(0)
280 , m_lastTimeUpdateEventMovieTime(std::numeric_limits<double>::max())
281 , m_loadState(WaitingForSource)
282 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
285 , m_preload(MediaPlayer::Auto)
286 , m_displayMode(Unknown)
287 , m_processingMediaPlayerCallback(0)
288 #if ENABLE(MEDIA_SOURCE)
289 , m_droppedVideoFrames(0)
291 , m_cachedTime(MediaPlayer::invalidTime())
292 , m_clockTimeAtLastCachedTimeUpdate(0)
293 , m_minimumClockTimeToUpdateCachedTime(0)
294 , m_fragmentStartTime(MediaPlayer::invalidTime())
295 , m_fragmentEndTime(MediaPlayer::invalidTime())
296 , m_pendingActionFlags(0)
298 , m_isWaitingUntilMediaCanStart(false)
299 , m_shouldDelayLoadEvent(false)
300 , m_haveFiredLoadedData(false)
301 , m_inActiveDocument(true)
302 , m_autoplaying(true)
306 , m_sentStalledEvent(false)
307 , m_sentEndEvent(false)
308 , m_pausedInternal(false)
309 , m_sendProgressEvents(true)
310 , m_isFullscreen(false)
311 , m_closedCaptionsVisible(false)
312 , m_webkitLegacyClosedCaptionOverride(false)
313 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
314 , m_needWidgetUpdate(false)
316 , m_completelyLoaded(false)
317 , m_havePreparedToPlay(false)
318 , m_parsingInProgress(createdByParser)
319 #if ENABLE(PAGE_VISIBILITY_API)
320 , m_isDisplaySleepDisablingSuspended(document.hidden())
323 , m_requestingPlay(false)
325 #if ENABLE(VIDEO_TRACK)
326 , m_tracksAreReady(true)
327 , m_haveVisibleTextTrack(false)
328 , m_processingPreferenceChange(false)
329 , m_lastTextTrackUpdateTime(-1)
330 , m_captionDisplayMode(CaptionUserPreferences::Automatic)
334 , m_ignoreTrackDisplayUpdate(0)
336 #if ENABLE(WEB_AUDIO)
337 , m_audioSourceNode(0)
339 , m_mediaSession(HTMLMediaSession::create(*this))
340 , m_reportedExtraMemoryCost(0)
341 #if ENABLE(MEDIA_STREAM)
342 , m_mediaStreamSrcObject(nullptr)
345 LOG(Media, "HTMLMediaElement::HTMLMediaElement");
346 setHasCustomStyleResolveCallbacks();
348 m_mediaSession->addBehaviorRestriction(HTMLMediaSession::RequireUserGestureForFullscreen);
349 m_mediaSession->addBehaviorRestriction(HTMLMediaSession::RequirePageConsentToLoadMedia);
351 // FIXME: We should clean up and look to better merge the iOS and non-iOS code below.
352 Settings* settings = document.settings();
354 document.registerForMediaVolumeCallbacks(this);
355 document.registerForPrivateBrowsingStateChangedCallbacks(this);
357 #if ENABLE(PAGE_VISIBILITY_API)
358 document.registerForVisibilityStateChangedCallbacks(this);
361 if (settings && settings->mediaPlaybackRequiresUserGesture()) {
362 m_mediaSession->addBehaviorRestriction(HTMLMediaSession::RequireUserGestureForRateChange);
363 m_mediaSession->addBehaviorRestriction(HTMLMediaSession::RequireUserGestureForLoad);
366 m_sendProgressEvents = false;
367 if (!settings || settings->mediaPlaybackRequiresUserGesture()) {
368 m_mediaSession->addBehaviorRestriction(HTMLMediaSession::RequireUserGestureForRateChange);
369 #if ENABLE(IOS_AIRPLAY)
370 m_mediaSession->addBehaviorRestriction(HTMLMediaSession::RequireUserGestureToShowPlaybackTargetPicker);
373 #endif // !PLATFORM(IOS)
375 addElementToDocumentMap(*this, document);
377 #if ENABLE(VIDEO_TRACK)
378 document.registerForCaptionPreferencesChangedCallbacks(this);
380 m_captionDisplayMode = document.page()->group().captionPreferences()->captionDisplayMode();
384 HTMLMediaElement::~HTMLMediaElement()
386 LOG(Media, "HTMLMediaElement::~HTMLMediaElement");
388 m_asyncEventQueue.close();
390 if (m_isWaitingUntilMediaCanStart)
391 document().removeMediaCanStartListener(this);
392 setShouldDelayLoadEvent(false);
393 document().unregisterForMediaVolumeCallbacks(this);
394 document().unregisterForPrivateBrowsingStateChangedCallbacks(this);
396 #if ENABLE(PAGE_VISIBILITY_API)
397 document().unregisterForVisibilityStateChangedCallbacks(this);
400 #if ENABLE(VIDEO_TRACK)
401 document().unregisterForCaptionPreferencesChangedCallbacks(this);
403 m_audioTracks->clearElement();
404 for (unsigned i = 0; i < m_audioTracks->length(); ++i)
405 m_audioTracks->item(i)->clearClient();
408 m_textTracks->clearElement();
410 for (unsigned i = 0; i < m_textTracks->length(); ++i)
411 m_textTracks->item(i)->clearClient();
414 m_videoTracks->clearElement();
415 for (unsigned i = 0; i < m_videoTracks->length(); ++i)
416 m_videoTracks->item(i)->clearClient();
420 #if ENABLE(IOS_AIRPLAY)
421 if (m_player && !hasEventListeners(eventNames().webkitplaybacktargetavailabilitychangedEvent))
422 m_player->setHasPlaybackTargetAvailabilityListeners(false);
425 if (m_mediaController) {
426 m_mediaController->removeMediaElement(this);
427 m_mediaController = 0;
430 #if ENABLE(MEDIA_SOURCE)
434 #if ENABLE(ENCRYPTED_MEDIA_V2)
438 removeElementFromDocumentMap(*this, document());
440 m_completelyLoaded = true;
442 m_player->clearMediaPlayerClient();
445 void HTMLMediaElement::didMoveToNewDocument(Document* oldDocument)
447 if (m_isWaitingUntilMediaCanStart) {
449 oldDocument->removeMediaCanStartListener(this);
450 document().addMediaCanStartListener(this);
453 if (m_shouldDelayLoadEvent) {
455 oldDocument->decrementLoadEventDelayCount();
456 document().incrementLoadEventDelayCount();
460 oldDocument->unregisterForMediaVolumeCallbacks(this);
461 removeElementFromDocumentMap(*this, *oldDocument);
464 document().registerForMediaVolumeCallbacks(this);
465 addElementToDocumentMap(*this, document());
467 HTMLElement::didMoveToNewDocument(oldDocument);
470 bool HTMLMediaElement::hasCustomFocusLogic() const
475 bool HTMLMediaElement::supportsFocus() const
477 if (document().isMediaDocument())
480 // If no controls specified, we should still be able to focus the element if it has tabIndex.
481 return controls() || HTMLElement::supportsFocus();
484 bool HTMLMediaElement::isMouseFocusable() const
490 bool HTMLMediaElement::parseMediaPlayerAttribute(const QualifiedName& name, const AtomicString& value)
493 if (name == data_youtube_idAttr) {
494 m_player->attributeChanged(name.toString(), value);
497 if (name == titleAttr) {
498 m_player->attributeChanged(name.toString(), value);
502 if (Settings* settings = document().settings()) {
503 #if ENABLE(IOS_AIRPLAY)
504 if (name == webkitairplayAttr && settings->mediaPlaybackAllowsAirPlay()) {
505 m_player->attributeChanged(name.toString(), value);
509 if (name == webkit_playsinlineAttr && settings->mediaPlaybackAllowsInline()) {
510 m_player->attributeChanged(name.toString(), ASCIILiteral(value.isNull() ? "false" : "true"));
518 void HTMLMediaElement::parseAttribute(const QualifiedName& name, const AtomicString& value)
520 if (name == srcAttr) {
522 // Note, unless the restriction on requiring user action has been removed,
523 // do not begin downloading data on iOS.
524 if (!value.isNull() && m_mediaSession->dataLoadingPermitted(*this)) {
526 // Trigger a reload, as long as the 'src' attribute is present.
527 if (!value.isNull()) {
529 clearMediaPlayer(LoadMediaResource);
530 scheduleDelayedAction(LoadMediaResource);
532 } else if (name == controlsAttr)
533 configureMediaControls();
534 else if (name == loopAttr)
535 updateSleepDisabling();
536 else if (name == preloadAttr) {
537 if (equalIgnoringCase(value, "none"))
538 m_preload = MediaPlayer::None;
539 else if (equalIgnoringCase(value, "metadata"))
540 m_preload = MediaPlayer::MetaData;
542 // The spec does not define an "invalid value default" but "auto" is suggested as the
543 // "missing value default", so use it for everything except "none" and "metadata"
544 m_preload = MediaPlayer::Auto;
547 // The attribute must be ignored if the autoplay attribute is present
548 if (!autoplay() && m_player)
549 m_player->setPreload(m_mediaSession->effectivePreloadForElement(*this));
551 } else if (name == mediagroupAttr)
552 setMediaGroup(value);
553 else if (name == onabortAttr)
554 setAttributeEventListener(eventNames().abortEvent, name, value);
555 else if (name == onbeforeloadAttr)
556 setAttributeEventListener(eventNames().beforeloadEvent, name, value);
557 else if (name == oncanplayAttr)
558 setAttributeEventListener(eventNames().canplayEvent, name, value);
559 else if (name == oncanplaythroughAttr)
560 setAttributeEventListener(eventNames().canplaythroughEvent, name, value);
561 else if (name == ondurationchangeAttr)
562 setAttributeEventListener(eventNames().durationchangeEvent, name, value);
563 else if (name == onemptiedAttr)
564 setAttributeEventListener(eventNames().emptiedEvent, name, value);
565 else if (name == onendedAttr)
566 setAttributeEventListener(eventNames().endedEvent, name, value);
567 else if (name == onerrorAttr)
568 setAttributeEventListener(eventNames().errorEvent, name, value);
569 else if (name == onloadeddataAttr)
570 setAttributeEventListener(eventNames().loadeddataEvent, name, value);
571 else if (name == onloadedmetadataAttr)
572 setAttributeEventListener(eventNames().loadedmetadataEvent, name, value);
573 else if (name == onloadstartAttr)
574 setAttributeEventListener(eventNames().loadstartEvent, name, value);
575 else if (name == onpauseAttr)
576 setAttributeEventListener(eventNames().pauseEvent, name, value);
577 else if (name == onplayAttr)
578 setAttributeEventListener(eventNames().playEvent, name, value);
579 else if (name == onplayingAttr)
580 setAttributeEventListener(eventNames().playingEvent, name, value);
581 else if (name == onprogressAttr)
582 setAttributeEventListener(eventNames().progressEvent, name, value);
583 else if (name == onratechangeAttr)
584 setAttributeEventListener(eventNames().ratechangeEvent, name, value);
585 else if (name == onseekedAttr)
586 setAttributeEventListener(eventNames().seekedEvent, name, value);
587 else if (name == onseekingAttr)
588 setAttributeEventListener(eventNames().seekingEvent, name, value);
589 else if (name == onstalledAttr)
590 setAttributeEventListener(eventNames().stalledEvent, name, value);
591 else if (name == onsuspendAttr)
592 setAttributeEventListener(eventNames().suspendEvent, name, value);
593 else if (name == ontimeupdateAttr)
594 setAttributeEventListener(eventNames().timeupdateEvent, name, value);
595 else if (name == onvolumechangeAttr)
596 setAttributeEventListener(eventNames().volumechangeEvent, name, value);
597 else if (name == onwaitingAttr)
598 setAttributeEventListener(eventNames().waitingEvent, name, value);
599 else if (name == onwebkitbeginfullscreenAttr)
600 setAttributeEventListener(eventNames().webkitbeginfullscreenEvent, name, value);
601 else if (name == onwebkitendfullscreenAttr)
602 setAttributeEventListener(eventNames().webkitendfullscreenEvent, name, value);
603 #if ENABLE(IOS_AIRPLAY)
604 else if (name == onwebkitcurrentplaybacktargetiswirelesschangedAttr)
605 setAttributeEventListener(eventNames().webkitcurrentplaybacktargetiswirelesschangedEvent, name, value);
606 else if (name == onwebkitplaybacktargetavailabilitychangedAttr)
607 setAttributeEventListener(eventNames().webkitplaybacktargetavailabilitychangedEvent, name, value);
610 else if (m_player && parseMediaPlayerAttribute(name, value))
614 HTMLElement::parseAttribute(name, value);
617 void HTMLMediaElement::finishParsingChildren()
619 HTMLElement::finishParsingChildren();
620 m_parsingInProgress = false;
622 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
623 if (shouldUseVideoPluginProxy()) {
624 document().updateStyleIfNeeded();
625 createMediaPlayerProxy();
629 #if ENABLE(VIDEO_TRACK)
630 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
633 if (descendantsOfType<HTMLTrackElement>(*this).first())
634 scheduleDelayedAction(ConfigureTextTracks);
638 bool HTMLMediaElement::rendererIsNeeded(const RenderStyle& style)
640 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
641 if (shouldUseVideoPluginProxy())
644 return controls() && HTMLElement::rendererIsNeeded(style);
647 RenderPtr<RenderElement> HTMLMediaElement::createElementRenderer(PassRef<RenderStyle> style)
649 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
650 if (shouldUseVideoPluginProxy()) {
651 // Setup the renderer if we already have a proxy widget.
652 auto mediaRenderer = createRenderer<RenderEmbeddedObject>(*this, std::move(style));
654 mediaRenderer->setWidget(m_proxyWidget);
656 if (Frame* frame = document().frame())
657 frame->loader().client().showMediaPlayerProxyPlugin(m_proxyWidget.get());
659 return std::move(mediaRenderer);
662 return createRenderer<RenderMedia>(*this, std::move(style));
665 bool HTMLMediaElement::childShouldCreateRenderer(const Node& child) const
667 #if ENABLE(MEDIA_CONTROLS_SCRIPT)
668 return hasShadowRootParent(child) && HTMLElement::childShouldCreateRenderer(child);
670 if (!hasMediaControls())
672 // <media> doesn't allow its content, including shadow subtree, to
673 // be rendered. So this should return false for most of the children.
674 // One exception is a shadow tree built for rendering controls which should be visible.
675 // So we let them go here by comparing its subtree root with one of the controls.
676 return &mediaControls()->treeScope() == &child.treeScope()
677 && hasShadowRootParent(child)
678 && HTMLElement::childShouldCreateRenderer(child);
682 Node::InsertionNotificationRequest HTMLMediaElement::insertedInto(ContainerNode& insertionPoint)
684 LOG(Media, "HTMLMediaElement::insertedInto");
686 HTMLElement::insertedInto(insertionPoint);
687 if (insertionPoint.inDocument()) {
688 m_inActiveDocument = true;
691 if (m_networkState == NETWORK_EMPTY && !fastGetAttribute(srcAttr).isEmpty() && m_mediaSession->dataLoadingPermitted(*this))
693 if (m_networkState == NETWORK_EMPTY && !fastGetAttribute(srcAttr).isEmpty())
695 scheduleDelayedAction(LoadMediaResource);
698 configureMediaControls();
699 return InsertionDone;
702 void HTMLMediaElement::removedFrom(ContainerNode& insertionPoint)
704 LOG(Media, "HTMLMediaElement::removedFrom");
706 m_inActiveDocument = false;
707 if (insertionPoint.inDocument()) {
708 configureMediaControls();
709 if (m_networkState > NETWORK_EMPTY)
715 JSC::VM* vm = JSDOMWindowBase::commonVM();
716 JSC::JSLockHolder lock(vm);
718 size_t extraMemoryCost = m_player->extraMemoryCost();
719 size_t extraMemoryCostDelta = extraMemoryCost - m_reportedExtraMemoryCost;
720 m_reportedExtraMemoryCost = extraMemoryCost;
722 if (extraMemoryCostDelta > 0)
723 vm->heap.reportExtraMemoryCost(extraMemoryCostDelta);
727 HTMLElement::removedFrom(insertionPoint);
730 void HTMLMediaElement::willAttachRenderers()
734 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
735 if (shouldUseVideoPluginProxy())
736 m_needWidgetUpdate = true;
740 void HTMLMediaElement::didAttachRenderers()
743 renderer()->updateFromElement();
744 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
745 else if (m_proxyWidget) {
746 if (Frame* frame = document().frame())
747 frame->loader().client().hideMediaPlayerProxyPlugin(m_proxyWidget.get());
752 void HTMLMediaElement::didRecalcStyle(Style::Change)
755 renderer()->updateFromElement();
758 void HTMLMediaElement::scheduleDelayedAction(DelayedActionType actionType)
760 LOG(Media, "HTMLMediaElement::scheduleLoad");
762 if ((actionType & LoadMediaResource) && !(m_pendingActionFlags & LoadMediaResource)) {
763 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
764 if (shouldUseVideoPluginProxy())
765 createMediaPlayerProxy();
769 setFlags(m_pendingActionFlags, LoadMediaResource);
772 #if ENABLE(VIDEO_TRACK)
773 if (RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled() && (actionType & ConfigureTextTracks))
774 setFlags(m_pendingActionFlags, ConfigureTextTracks);
777 #if USE(PLATFORM_TEXT_TRACK_MENU)
778 if (actionType & TextTrackChangesNotification)
779 setFlags(m_pendingActionFlags, TextTrackChangesNotification);
782 m_loadTimer.startOneShot(0);
785 void HTMLMediaElement::scheduleNextSourceChild()
787 // Schedule the timer to try the next <source> element WITHOUT resetting state ala prepareForLoad.
788 setFlags(m_pendingActionFlags, LoadMediaResource);
789 m_loadTimer.startOneShot(0);
792 void HTMLMediaElement::scheduleEvent(const AtomicString& eventName)
795 LOG(Media, "HTMLMediaElement::scheduleEvent - scheduling '%s'", eventName.string().ascii().data());
797 RefPtr<Event> event = Event::create(eventName, false, true);
799 // Don't set the event target, the event queue will set it in GenericEventQueue::timerFired and setting it here
800 // will trigger an ASSERT if this element has been marked for deletion.
802 m_asyncEventQueue.enqueueEvent(event.release());
805 void HTMLMediaElement::loadTimerFired(Timer<HTMLMediaElement>&)
807 Ref<HTMLMediaElement> protect(*this); // loadNextSourceChild may fire 'beforeload', which can make arbitrary DOM mutations.
809 #if ENABLE(VIDEO_TRACK)
810 if (RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled() && (m_pendingActionFlags & ConfigureTextTracks))
811 configureTextTracks();
814 if (m_pendingActionFlags & LoadMediaResource) {
815 if (m_loadState == LoadingFromSourceElement)
816 loadNextSourceChild();
821 #if USE(PLATFORM_TEXT_TRACK_MENU)
822 if (RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled() && (m_pendingActionFlags & TextTrackChangesNotification))
823 notifyMediaPlayerOfTextTrackChanges();
826 m_pendingActionFlags = 0;
829 PassRefPtr<MediaError> HTMLMediaElement::error() const
834 void HTMLMediaElement::setSrc(const String& url)
836 setAttribute(srcAttr, url);
839 #if ENABLE(MEDIA_STREAM)
840 void HTMLMediaElement::setSrcObject(MediaStream* mediaStream)
842 // FIXME: Setting the srcObject attribute may cause other changes to the media element's internal state:
843 // Specifically, if srcObject is specified, the UA must use it as the source of media, even if the src
844 // attribute is also set or children are present. If the value of srcObject is replaced or set to null
845 // the UA must re-run the media element load algorithm.
847 // https://bugs.webkit.org/show_bug.cgi?id=124896
849 m_mediaStreamSrcObject = mediaStream;
853 HTMLMediaElement::NetworkState HTMLMediaElement::networkState() const
855 return m_networkState;
858 String HTMLMediaElement::canPlayType(const String& mimeType, const String& keySystem, const URL& url) const
860 MediaEngineSupportParameters parameters;
861 ContentType contentType(mimeType);
862 parameters.type = contentType.type().lower();
863 parameters.codecs = contentType.parameter(ASCIILiteral("codecs"));
864 parameters.url = url;
865 #if ENABLE(ENCRYPTED_MEDIA)
866 parameters.keySystem = keySystem;
868 UNUSED_PARAM(keySystem);
870 MediaPlayer::SupportsType support = MediaPlayer::supportsType(parameters, this);
876 case MediaPlayer::IsNotSupported:
877 canPlay = emptyString();
879 case MediaPlayer::MayBeSupported:
880 canPlay = ASCIILiteral("maybe");
882 case MediaPlayer::IsSupported:
883 canPlay = ASCIILiteral("probably");
887 LOG(Media, "HTMLMediaElement::canPlayType(%s, %s, %s) -> %s", mimeType.utf8().data(), keySystem.utf8().data(), url.stringCenterEllipsizedToLength().utf8().data(), canPlay.utf8().data());
892 void HTMLMediaElement::load()
894 Ref<HTMLMediaElement> protect(*this); // loadInternal may result in a 'beforeload' event, which can make arbitrary DOM mutations.
896 LOG(Media, "HTMLMediaElement::load()");
898 if (!m_mediaSession->dataLoadingPermitted(*this))
900 if (ScriptController::processingUserGesture())
901 removeBehaviorsRestrictionsAfterFirstUserGesture();
908 void HTMLMediaElement::prepareForLoad()
910 LOG(Media, "HTMLMediaElement::prepareForLoad");
912 // Perform the cleanup required for the resource load algorithm to run.
913 stopPeriodicTimers();
915 m_sentEndEvent = false;
916 m_sentStalledEvent = false;
917 m_haveFiredLoadedData = false;
918 m_completelyLoaded = false;
919 m_havePreparedToPlay = false;
920 m_displayMode = Unknown;
921 m_currentSrc = URL();
923 // 1 - Abort any already-running instance of the resource selection algorithm for this element.
924 m_loadState = WaitingForSource;
925 m_currentSourceNode = 0;
927 // 2 - If there are any tasks from the media element's media element event task source in
928 // one of the task queues, then remove those tasks.
929 cancelPendingEventsAndCallbacks();
931 // 3 - If the media element's networkState is set to NETWORK_LOADING or NETWORK_IDLE, queue
932 // a task to fire a simple event named abort at the media element.
933 if (m_networkState == NETWORK_LOADING || m_networkState == NETWORK_IDLE)
934 scheduleEvent(eventNames().abortEvent);
936 #if ENABLE(MEDIA_SOURCE)
940 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
941 if (shouldUseVideoPluginProxy()) {
943 m_player->cancelLoad();
945 createMediaPlayerProxy();
950 // 4 - If the media element's networkState is not set to NETWORK_EMPTY, then run these substeps
951 if (m_networkState != NETWORK_EMPTY) {
952 m_networkState = NETWORK_EMPTY;
953 m_readyState = HAVE_NOTHING;
954 m_readyStateMaximum = HAVE_NOTHING;
958 invalidateCachedTime();
959 scheduleEvent(eventNames().emptiedEvent);
960 updateMediaController();
961 #if ENABLE(VIDEO_TRACK)
962 if (RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
963 updateActiveTextTrackCues(0);
967 // 5 - Set the playbackRate attribute to the value of the defaultPlaybackRate attribute.
968 setPlaybackRate(defaultPlaybackRate());
970 // 6 - Set the error attribute to null and the autoplaying flag to true.
972 m_autoplaying = true;
974 // 7 - Invoke the media element's resource selection algorithm.
976 // 8 - Note: Playback of any previously playing media resource for this element stops.
978 // The resource selection algorithm
979 // 1 - Set the networkState to NETWORK_NO_SOURCE
980 m_networkState = NETWORK_NO_SOURCE;
982 // 2 - Asynchronously await a stable state.
984 m_playedTimeRanges = TimeRanges::create();
987 // The spec doesn't say to block the load event until we actually run the asynchronous section
988 // algorithm, but do it now because we won't start that until after the timer fires and the
989 // event may have already fired by then.
990 MediaPlayer::Preload effectivePreload = m_mediaSession->effectivePreloadForElement(*this);
991 if (effectivePreload != MediaPlayer::None)
992 setShouldDelayLoadEvent(true);
995 Settings* settings = document().settings();
996 if (effectivePreload != MediaPlayer::None && settings && settings->mediaDataLoadsAutomatically())
1000 configureMediaControls();
1003 void HTMLMediaElement::loadInternal()
1005 // Some of the code paths below this function dispatch the BeforeLoad event. This ASSERT helps
1006 // us catch those bugs more quickly without needing all the branches to align to actually
1007 // trigger the event.
1008 ASSERT(!NoEventDispatchAssertion::isEventDispatchForbidden());
1010 // If we can't start a load right away, start it later.
1011 if (!m_mediaSession->pageAllowsDataLoading(*this)) {
1012 setShouldDelayLoadEvent(false);
1013 if (m_isWaitingUntilMediaCanStart)
1015 document().addMediaCanStartListener(this);
1016 m_isWaitingUntilMediaCanStart = true;
1020 clearFlags(m_pendingActionFlags, LoadMediaResource);
1022 // Once the page has allowed an element to load media, it is free to load at will. This allows a
1023 // playlist that starts in a foreground tab to continue automatically if the tab is subsequently
1024 // put into the background.
1025 m_mediaSession->removeBehaviorRestriction(HTMLMediaSession::RequirePageConsentToLoadMedia);
1027 #if ENABLE(VIDEO_TRACK)
1028 if (hasMediaControls())
1029 mediaControls()->changedClosedCaptionsVisibility();
1031 // HTMLMediaElement::textTracksAreReady will need "... the text tracks whose mode was not in the
1032 // disabled state when the element's resource selection algorithm last started".
1033 if (RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled()) {
1034 m_textTracksWhenResourceSelectionBegan.clear();
1036 for (unsigned i = 0; i < m_textTracks->length(); ++i) {
1037 TextTrack* track = m_textTracks->item(i);
1038 if (track->mode() != TextTrack::disabledKeyword())
1039 m_textTracksWhenResourceSelectionBegan.append(track);
1045 selectMediaResource();
1048 void HTMLMediaElement::selectMediaResource()
1050 LOG(Media, "HTMLMediaElement::selectMediaResource");
1052 enum Mode { attribute, children };
1054 // 3 - If the media element has a src attribute, then let mode be attribute.
1055 Mode mode = attribute;
1056 if (!fastHasAttribute(srcAttr)) {
1057 // Otherwise, if the media element does not have a src attribute but has a source
1058 // element child, then let mode be children and let candidate be the first such
1059 // source element child in tree order.
1060 if (auto firstSource = childrenOfType<HTMLSourceElement>(*this).first()) {
1062 m_nextChildNodeToConsider = firstSource;
1063 m_currentSourceNode = 0;
1065 // Otherwise the media element has neither a src attribute nor a source element
1066 // child: set the networkState to NETWORK_EMPTY, and abort these steps; the
1067 // synchronous section ends.
1068 m_loadState = WaitingForSource;
1069 setShouldDelayLoadEvent(false);
1070 m_networkState = NETWORK_EMPTY;
1072 LOG(Media, "HTMLMediaElement::selectMediaResource, nothing to load");
1077 // 4 - Set the media element's delaying-the-load-event flag to true (this delays the load event),
1078 // and set its networkState to NETWORK_LOADING.
1079 setShouldDelayLoadEvent(true);
1080 m_networkState = NETWORK_LOADING;
1082 // 5 - Queue a task to fire a simple event named loadstart at the media element.
1083 scheduleEvent(eventNames().loadstartEvent);
1085 // 6 - If mode is attribute, then run these substeps
1086 if (mode == attribute) {
1087 m_loadState = LoadingFromSrcAttr;
1089 // If the src attribute's value is the empty string ... jump down to the failed step below
1090 URL mediaURL = getNonEmptyURLAttribute(srcAttr);
1091 if (mediaURL.isEmpty()) {
1092 mediaLoadingFailed(MediaPlayer::FormatError);
1093 LOG(Media, "HTMLMediaElement::selectMediaResource, empty 'src'");
1097 if (!isSafeToLoadURL(mediaURL, Complain) || !dispatchBeforeLoadEvent(mediaURL.string())) {
1098 mediaLoadingFailed(MediaPlayer::FormatError);
1102 // No type or key system information is available when the url comes
1103 // from the 'src' attribute so MediaPlayer
1104 // will have to pick a media engine based on the file extension.
1105 ContentType contentType((String()));
1106 loadResource(mediaURL, contentType, String());
1107 LOG(Media, "HTMLMediaElement::selectMediaResource, using 'src' attribute url");
1111 // Otherwise, the source elements will be used
1112 loadNextSourceChild();
1115 void HTMLMediaElement::loadNextSourceChild()
1117 ContentType contentType((String()));
1119 URL mediaURL = selectNextSourceChild(&contentType, &keySystem, Complain);
1120 if (!mediaURL.isValid()) {
1121 waitForSourceChange();
1125 #if ENABLE(PLUGIN_PROXY_FOR_VIDEO)
1126 if (!shouldUseVideoPluginProxy())
1128 // Recreate the media player for the new url
1129 createMediaPlayer();
1131 m_loadState = LoadingFromSourceElement;
1132 loadResource(mediaURL, contentType, keySystem);
1135 static URL createFileURLForApplicationCacheResource(const String& path)
1137 // URL should have a function to create a url from a path, but it does not. This function
1138 // is not suitable because URL::setPath uses encodeWithURLEscapeSequences, which it notes
1139 // does not correctly escape '#' and '?'. This function works for our purposes because
1140 // app cache media files are always created with encodeForFileName(createCanonicalUUIDString()).
1142 #if USE(CF) && PLATFORM(WIN)
1143 RetainPtr<CFURLRef> cfURL = adoptCF(CFURLCreateWithFileSystemPath(0, path.createCFString().get(), kCFURLWindowsPathStyle, false));
1144 URL url(cfURL.get());
1148 url.setProtocol(ASCIILiteral("file"));
1154 void HTMLMediaElement::loadResource(const URL& initialURL, ContentType& contentType, const String& keySystem)
1156 ASSERT(isSafeToLoadURL(initialURL, Complain));
1158 LOG(Media, "HTMLMediaElement::loadResource(%s, %s, %s)", urlForLoggingMedia(initialURL).utf8().data(), contentType.raw().utf8().data(), keySystem.utf8().data());
1160 Frame* frame = document().frame();
1162 mediaLoadingFailed(MediaPlayer::FormatError);
1166 URL url = initialURL;
1167 if (!frame->loader().willLoadMediaElementURL(url)) {
1168 mediaLoadingFailed(MediaPlayer::FormatError);
1172 // The resource fetch algorithm
1173 m_networkState = NETWORK_LOADING;
1175 // If the url should be loaded from the application cache, pass the url of the cached file
1176 // to the media engine.
1177 ApplicationCacheHost* cacheHost = frame->loader().documentLoader()->applicationCacheHost();
1178 ApplicationCacheResource* resource = 0;
1179 if (cacheHost && cacheHost->shouldLoadResourceFromApplicationCache(ResourceRequest(url), resource)) {
1180 // Resources that are not present in the manifest will always fail to load (at least, after the
1181 // cache has been primed the first time), making the testing of offline applications simpler.
1182 if (!resource || resource->path().isEmpty()) {
1183 mediaLoadingFailed(MediaPlayer::NetworkError);
1188 // Set m_currentSrc *before* changing to the cache url, the fact that we are loading from the app
1189 // cache is an internal detail not exposed through the media element API.
1193 url = createFileURLForApplicationCacheResource(resource->path());
1194 LOG(Media, "HTMLMediaElement::loadResource - will load from app cache -> %s", urlForLoggingMedia(url).utf8().data());
1197 LOG(Media, "HTMLMediaElement::loadResource - m_currentSrc -> %s", urlForLoggingMedia(m_currentSrc).utf8().data());
1199 #if ENABLE(MEDIA_STREAM)
1200 if (MediaStreamRegistry::registry().lookup(url.string()))
1201 m_mediaSession->removeBehaviorRestriction(HTMLMediaSession::RequireUserGestureForRateChange);
1204 if (m_sendProgressEvents)
1205 startProgressEventTimer();
1207 Settings* settings = document().settings();
1208 bool privateMode = !settings || settings->privateBrowsingEnabled();
1209 m_player->setPrivateBrowsingMode(privateMode);
1211 // Reset display mode to force a recalculation of what to show because we are resetting the player.
1212 setDisplayMode(Unknown);
1215 m_player->setPreload(m_mediaSession->effectivePreloadForElement(*this));
1216 m_player->setPreservesPitch(m_webkitPreservesPitch);
1218 if (fastHasAttribute(mutedAttr))
1222 #if ENABLE(MEDIA_SOURCE)
1223 ASSERT(!m_mediaSource);
1225 if (url.protocolIs(mediaSourceBlobProtocol))
1226 m_mediaSource = HTMLMediaSource::lookup(url.string());
1228 if (m_mediaSource) {
1229 if (m_mediaSource->attachToElement(this))
1230 m_player->load(url, contentType, m_mediaSource);
1232 // Forget our reference to the MediaSource, so we leave it alone
1233 // while processing remainder of load failure.
1235 mediaLoadingFailed(MediaPlayer::FormatError);
1239 if (!m_player->load(url, contentType, keySystem))
1240 mediaLoadingFailed(MediaPlayer::FormatError);
1242 // If there is no poster to display, allow the media engine to render video frames as soon as
1243 // they are available.
1244 updateDisplayState();
1247 renderer()->updateFromElement();
1250 #if ENABLE(VIDEO_TRACK)
1251 static bool trackIndexCompare(TextTrack* a,
1254 return a->trackIndex() - b->trackIndex() < 0;
1257 static bool eventTimeCueCompare(const std::pair<double, TextTrackCue*>& a,
1258 const std::pair<double, TextTrackCue*>& b)
1260 // 12 - Sort the tasks in events in ascending time order (tasks with earlier
1262 if (a.first != b.first)
1263 return a.first - b.first < 0;
1265 // If the cues belong to different text tracks, it doesn't make sense to
1266 // compare the two tracks by the relative cue order, so return the relative
1268 if (a.second->track() != b.second->track())
1269 return trackIndexCompare(a.second->track(), b.second->track());
1271 // 12 - Further sort tasks in events that have the same time by the
1272 // relative text track cue order of the text track cues associated
1273 // with these tasks.
1274 return a.second->cueIndex() - b.second->cueIndex() < 0;
1277 static bool compareCueInterval(const CueInterval& one, const CueInterval& two)
1279 return one.data()->isOrderedBefore(two.data());
1283 void HTMLMediaElement::updateActiveTextTrackCues(double movieTime)
1285 // 4.8.10.8 Playing the media resource
1287 // If the current playback position changes while the steps are running,
1288 // then the user agent must wait for the steps to complete, and then must
1289 // immediately rerun the steps.
1290 if (ignoreTrackDisplayUpdateRequests())
1293 LOG(Media, "HTMLMediaElement::updateActiveTextTracks");
1295 // 1 - Let current cues be a list of cues, initialized to contain all the
1296 // cues of all the hidden, showing, or showing by default text tracks of the
1297 // media element (not the disabled ones) whose start times are less than or
1298 // equal to the current playback position and whose end times are greater
1299 // than the current playback position.
1300 CueList currentCues;
1302 // The user agent must synchronously unset [the text track cue active] flag
1303 // whenever ... the media element's readyState is changed back to HAVE_NOTHING.
1304 if (m_readyState != HAVE_NOTHING && m_player) {
1305 currentCues = m_cueTree.allOverlaps(m_cueTree.createInterval(movieTime, movieTime));
1306 std::sort(currentCues.begin(), currentCues.end(), &compareCueInterval);
1309 CueList previousCues;
1312 // 2 - Let other cues be a list of cues, initialized to contain all the cues
1313 // of hidden, showing, and showing by default text tracks of the media
1314 // element that are not present in current cues.
1315 previousCues = m_currentlyActiveCues;
1317 // 3 - Let last time be the current playback position at the time this
1318 // algorithm was last run for this media element, if this is not the first
1320 double lastTime = m_lastTextTrackUpdateTime;
1322 // 4 - If the current playback position has, since the last time this
1323 // algorithm was run, only changed through its usual monotonic increase
1324 // during normal playback, then let missed cues be the list of cues in other
1325 // cues whose start times are greater than or equal to last time and whose
1326 // end times are less than or equal to the current playback position.
1327 // Otherwise, let missed cues be an empty list.
1328 if (lastTime >= 0 && m_lastSeekTime < movieTime) {
1329 CueList potentiallySkippedCues =
1330 m_cueTree.allOverlaps(m_cueTree.createInterval(lastTime, movieTime));
1332 for (size_t i = 0; i < potentiallySkippedCues.size(); ++i) {
1333 double cueStartTime = potentiallySkippedCues[i].low();
1334 double cueEndTime = potentiallySkippedCues[i].high();
1336 // Consider cues that may have been missed since the last seek time.
1337 if (cueStartTime > std::max(m_lastSeekTime, lastTime) && cueEndTime < movieTime)
1338 missedCues.append(potentiallySkippedCues[i]);
1342 m_lastTextTrackUpdateTime = movieTime;
1344 // 5 - If the time was reached through the usual monotonic increase of the
1345 // current playback position during normal playback, and if the user agent
1346 // has not fired a timeupdate event at the element in the past 15 to 250ms
1347 // and is not still running event handlers for such an event, then the user
1348 // agent must queue a task to fire a simple event named timeupdate at the
1349 // element. (In the other cases, such as explicit seeks, relevant events get
1350 // fired as part of the overall process of changing the current playback
1352 if (m_lastSeekTime <= lastTime)
1353 scheduleTimeupdateEvent(false);
1355 // Explicitly cache vector sizes, as their content is constant from here.
1356 size_t currentCuesSize = currentCues.size();
1357 size_t missedCuesSize = missedCues.size();
1358 size_t previousCuesSize = previousCues.size();
1360 // 6 - If all of the cues in current cues have their text track cue active
1361 // flag set, none of the cues in other cues have their text track cue active
1362 // flag set, and missed cues is empty, then abort these steps.
1363 bool activeSetChanged = missedCuesSize;
1365 for (size_t i = 0; !activeSetChanged && i < previousCuesSize; ++i)
1366 if (!currentCues.contains(previousCues[i]) && previousCues[i].data()->isActive())
1367 activeSetChanged = true;
1369 for (size_t i = 0; i < currentCuesSize; ++i) {
1370 currentCues[i].data()->updateDisplayTree(movieTime);
1372 if (!currentCues[i].data()->isActive())
1373 activeSetChanged = true;
1376 if (!activeSetChanged)
1379 // 7 - If the time was reached through the usual monotonic increase of the
1380 // current playback position during normal playback, and there are cues in
1381 // other cues that have their text track cue pause-on-exi flag set and that
1382 // either have their text track cue active flag set or are also in missed
1383 // cues, then immediately pause the media element.
1384 for (size_t i = 0; !m_paused && i < previousCuesSize; ++i) {
1385 if (previousCues[i].data()->pauseOnExit()
1386 && previousCues[i].data()->isActive()
1387 && !currentCues.contains(previousCues[i]))
1391 for (size_t i = 0; !m_paused && i < missedCuesSize; ++i) {
1392 if (missedCues[i].data()->pauseOnExit())
1396 // 8 - Let events be a list of tasks, initially empty. Each task in this
1397 // list will be associated with a text track, a text track cue, and a time,
1398 // which are used to sort the list before the tasks are queued.
1399 Vector<std::pair<double, TextTrackCue*>> eventTasks;
1401 // 8 - Let affected tracks be a list of text tracks, initially empty.
1402 Vector<TextTrack*> affectedTracks;
1404 for (size_t i = 0; i < missedCuesSize; ++i) {
1405 // 9 - For each text track cue in missed cues, prepare an event named enter
1406 // for the TextTrackCue object with the text track cue start time.
1407 eventTasks.append(std::make_pair(missedCues[i].data()->startTime(),
1408 missedCues[i].data()));
1410 // 10 - For each text track [...] in missed cues, prepare an event
1411 // named exit for the TextTrackCue object with the with the later of
1412 // the text track cue end time and the text track cue start time.
1414 // Note: An explicit task is added only if the cue is NOT a zero or
1415 // negative length cue. Otherwise, the need for an exit event is
1416 // checked when these tasks are actually queued below. This doesn't
1417 // affect sorting events before dispatch either, because the exit
1418 // event has the same time as the enter event.
1419 if (missedCues[i].data()->startTime() < missedCues[i].data()->endTime())
1420 eventTasks.append(std::make_pair(missedCues[i].data()->endTime(),
1421 missedCues[i].data()));
1424 for (size_t i = 0; i < previousCuesSize; ++i) {
1425 // 10 - For each text track cue in other cues that has its text
1426 // track cue active flag set prepare an event named exit for the
1427 // TextTrackCue object with the text track cue end time.
1428 if (!currentCues.contains(previousCues[i]))
1429 eventTasks.append(std::make_pair(previousCues[i].data()->endTime(),
1430 previousCues[i].data()));
1433 for (size_t i = 0; i < currentCuesSize; ++i) {
1434 // 11 - For each text track cue in current cues that does not have its
1435 // text track cue active flag set, prepare an event named enter for the
1436 // TextTrackCue object with the text track cue start time.
1437 if (!previousCues.contains(currentCues[i]))
1438 eventTasks.append(std::make_pair(currentCues[i].data()->startTime(),
1439 currentCues[i].data()));
1442 // 12 - Sort the tasks in events in ascending time order (tasks with earlier
1444 std::sort(eventTasks.begin(), eventTasks.end(), eventTimeCueCompare);
1446 for (size_t i = 0; i < eventTasks.size(); ++i) {
1447 if (!affectedTracks.contains(eventTasks[i].second->track()))
1448 affectedTracks.append(eventTasks[i].second->track());
1450 // 13 - Queue each task in events, in list order.
1451 RefPtr<Event> event;
1453 // Each event in eventTasks may be either an enterEvent or an exitEvent,
1454 // depending on the time that is associated with the event. This
1455 // correctly identifies the type of the event, if the startTime is
1456 // less than the endTime in the cue.
1457 if (eventTasks[i].second->startTime() >= eventTasks[i].second->endTime()) {
1458 event = Event::create(eventNames().enterEvent, false, false);
1459 event->setTarget(eventTasks[i].second);
1460 m_asyncEventQueue.enqueueEvent(event.release());
1462 event = Event::create(eventNames().exitEvent, false, false);
1463 event->setTarget(eventTasks[i].second);
1464 m_asyncEventQueue.enqueueEvent(event.release());
1466 if (eventTasks[i].first == eventTasks[i].second->startTime())
1467 event = Event::create(eventNames().enterEvent, false, false);
1469 event = Event::create(eventNames().exitEvent, false, false);
1471 event->setTarget(eventTasks[i].second);
1472 m_asyncEventQueue.enqueueEvent(event.release());
1476 // 14 - Sort affected tracks in the same order as the text tracks appear in
1477 // the media element's list of text tracks, and remove duplicates.
1478 std::sort(affectedTracks.begin(), affectedTracks.end(), trackIndexCompare);
1480 // 15 - For each text track in affected tracks, in the list order, queue a
1481 // task to fire a simple event named cuechange at the TextTrack object, and, ...
1482 for (size_t i = 0; i < affectedTracks.size(); ++i) {
1483 RefPtr<Event> event = Event::create(eventNames().cuechangeEvent, false, false);
1484 event->setTarget(affectedTracks[i]);
1486 m_asyncEventQueue.enqueueEvent(event.release());
1488 // ... if the text track has a corresponding track element, to then fire a
1489 // simple event named cuechange at the track element as well.
1490 if (affectedTracks[i]->trackType() == TextTrack::TrackElement) {
1491 RefPtr<Event> event = Event::create(eventNames().cuechangeEvent, false, false);
1492 HTMLTrackElement* trackElement = static_cast<LoadableTextTrack*>(affectedTracks[i])->trackElement();
1493 ASSERT(trackElement);
1494 event->setTarget(trackElement);
1496 m_asyncEventQueue.enqueueEvent(event.release());
1500 // 16 - Set the text track cue active flag of all the cues in the current
1501 // cues, and unset the text track cue active flag of all the cues in the
1503 for (size_t i = 0; i < currentCuesSize; ++i)
1504 currentCues[i].data()->setIsActive(true);
1506 for (size_t i = 0; i < previousCuesSize; ++i)
1507 if (!currentCues.contains(previousCues[i]))
1508 previousCues[i].data()->setIsActive(false);
1510 // Update the current active cues.
1511 m_currentlyActiveCues = currentCues;
1513 if (activeSetChanged)
1514 updateTextTrackDisplay();
1517 bool HTMLMediaElement::textTracksAreReady() const
1519 // 4.8.10.12.1 Text track model
1521 // The text tracks of a media element are ready if all the text tracks whose mode was not
1522 // in the disabled state when the element's resource selection algorithm last started now
1523 // have a text track readiness state of loaded or failed to load.
1524 for (unsigned i = 0; i < m_textTracksWhenResourceSelectionBegan.size(); ++i) {
1525 if (m_textTracksWhenResourceSelectionBegan[i]->readinessState() == TextTrack::Loading
1526 || m_textTracksWhenResourceSelectionBegan[i]->readinessState() == TextTrack::NotLoaded)
1533 void HTMLMediaElement::textTrackReadyStateChanged(TextTrack* track)
1535 if (m_player && m_textTracksWhenResourceSelectionBegan.contains(track)) {
1536 if (track->readinessState() != TextTrack::Loading)
1537 setReadyState(m_player->readyState());
1539 // The track readiness state might have changed as a result of the user
1540 // clicking the captions button. In this case, a check whether all the
1541 // resources have failed loading should be done in order to hide the CC button.
1542 if (hasMediaControls() && track->readinessState() == TextTrack::FailedToLoad)
1543 mediaControls()->refreshClosedCaptionsButtonVisibility();
1547 void HTMLMediaElement::audioTrackEnabledChanged(AudioTrack* track)
1549 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
1551 if (m_audioTracks && m_audioTracks->contains(track))
1552 m_audioTracks->scheduleChangeEvent();
1555 void HTMLMediaElement::textTrackModeChanged(TextTrack* track)
1557 bool trackIsLoaded = true;
1558 if (track->trackType() == TextTrack::TrackElement) {
1559 trackIsLoaded = false;
1560 for (auto& trackElement : childrenOfType<HTMLTrackElement>(*this)) {
1561 if (trackElement.track() == track) {
1562 if (trackElement.readyState() == HTMLTrackElement::LOADING || trackElement.readyState() == HTMLTrackElement::LOADED)
1563 trackIsLoaded = true;
1569 // If this is the first added track, create the list of text tracks.
1571 m_textTracks = TextTrackList::create(this, ActiveDOMObject::scriptExecutionContext());
1573 // Mark this track as "configured" so configureTextTracks won't change the mode again.
1574 track->setHasBeenConfigured(true);
1576 if (track->mode() != TextTrack::disabledKeyword() && trackIsLoaded)
1577 textTrackAddCues(track, track->cues());
1579 #if USE(PLATFORM_TEXT_TRACK_MENU)
1580 if (platformTextTrackMenu())
1581 platformTextTrackMenu()->trackWasSelected(track->platformTextTrack());
1584 configureTextTrackDisplay(AssumeTextTrackVisibilityChanged);
1586 if (m_textTracks && m_textTracks->contains(track))
1587 m_textTracks->scheduleChangeEvent();
1590 void HTMLMediaElement::videoTrackSelectedChanged(VideoTrack* track)
1592 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
1594 if (m_videoTracks && m_videoTracks->contains(track))
1595 m_videoTracks->scheduleChangeEvent();
1598 void HTMLMediaElement::textTrackKindChanged(TextTrack* track)
1600 if (track->kind() != TextTrack::captionsKeyword() && track->kind() != TextTrack::subtitlesKeyword() && track->mode() == TextTrack::showingKeyword())
1601 track->setMode(TextTrack::hiddenKeyword());
1604 void HTMLMediaElement::beginIgnoringTrackDisplayUpdateRequests()
1606 ++m_ignoreTrackDisplayUpdate;
1609 void HTMLMediaElement::endIgnoringTrackDisplayUpdateRequests()
1611 ASSERT(m_ignoreTrackDisplayUpdate);
1612 --m_ignoreTrackDisplayUpdate;
1613 if (!m_ignoreTrackDisplayUpdate && m_inActiveDocument)
1614 updateActiveTextTrackCues(currentTime());
1617 void HTMLMediaElement::textTrackAddCues(TextTrack* track, const TextTrackCueList* cues)
1619 if (track->mode() == TextTrack::disabledKeyword())
1622 TrackDisplayUpdateScope scope(this);
1623 for (size_t i = 0; i < cues->length(); ++i)
1624 textTrackAddCue(track, cues->item(i));
1627 void HTMLMediaElement::textTrackRemoveCues(TextTrack*, const TextTrackCueList* cues)
1629 TrackDisplayUpdateScope scope(this);
1630 for (size_t i = 0; i < cues->length(); ++i)
1631 textTrackRemoveCue(cues->item(i)->track(), cues->item(i));
1634 void HTMLMediaElement::textTrackAddCue(TextTrack* track, PassRefPtr<TextTrackCue> cue)
1636 if (track->mode() == TextTrack::disabledKeyword())
1639 // Negative duration cues need be treated in the interval tree as
1640 // zero-length cues.
1641 double endTime = std::max(cue->startTime(), cue->endTime());
1643 CueInterval interval = m_cueTree.createInterval(cue->startTime(), endTime, cue.get());
1644 if (!m_cueTree.contains(interval))
1645 m_cueTree.add(interval);
1646 updateActiveTextTrackCues(currentTime());
1649 void HTMLMediaElement::textTrackRemoveCue(TextTrack*, PassRefPtr<TextTrackCue> cue)
1651 // Negative duration cues need to be treated in the interval tree as
1652 // zero-length cues.
1653 double endTime = std::max(cue->startTime(), cue->endTime());
1655 CueInterval interval = m_cueTree.createInterval(cue->startTime(), endTime, cue.get());
1656 m_cueTree.remove(interval);
1658 size_t index = m_currentlyActiveCues.find(interval);
1659 if (index != notFound) {
1660 cue->setIsActive(false);
1661 m_currentlyActiveCues.remove(index);
1664 cue->removeDisplayTree();
1665 updateActiveTextTrackCues(currentTime());
1670 bool HTMLMediaElement::isSafeToLoadURL(const URL& url, InvalidURLAction actionIfInvalid)
1672 if (!url.isValid()) {
1673 LOG(Media, "HTMLMediaElement::isSafeToLoadURL(%s) -> FALSE because url is invalid", urlForLoggingMedia(url).utf8().data());
1677 Frame* frame = document().frame();
1678 if (!frame || !document().securityOrigin()->canDisplay(url)) {
1679 if (actionIfInvalid == Complain)
1680 FrameLoader::reportLocalLoadFailed(frame, url.stringCenterEllipsizedToLength());
1681 LOG(Media, "HTMLMediaElement::isSafeToLoadURL(%s) -> FALSE rejected by SecurityOrigin", urlForLoggingMedia(url).utf8().data());
1685 if (!document().contentSecurityPolicy()->allowMediaFromSource(url)) {
1686 LOG(Media, "HTMLMediaElement::isSafeToLoadURL(%s) -> rejected by Content Security Policy", urlForLoggingMedia(url).utf8().data());
1693 void HTMLMediaElement::startProgressEventTimer()
1695 if (m_progressEventTimer.isActive())
1698 m_previousProgressTime = monotonicallyIncreasingTime();
1699 // 350ms is not magic, it is in the spec!
1700 m_progressEventTimer.startRepeating(0.350);
1703 void HTMLMediaElement::waitForSourceChange()
1705 LOG(Media, "HTMLMediaElement::waitForSourceChange");
1707 stopPeriodicTimers();
1708 m_loadState = WaitingForSource;
1710 // 6.17 - Waiting: Set the element's networkState attribute to the NETWORK_NO_SOURCE value
1711 m_networkState = NETWORK_NO_SOURCE;
1713 // 6.18 - Set the element's delaying-the-load-event flag to false. This stops delaying the load event.
1714 setShouldDelayLoadEvent(false);
1716 updateDisplayState();
1719 renderer()->updateFromElement();
1722 void HTMLMediaElement::noneSupported()
1724 LOG(Media, "HTMLMediaElement::noneSupported");
1726 stopPeriodicTimers();
1727 m_loadState = WaitingForSource;
1728 m_currentSourceNode = 0;
1731 // 6 - Reaching this step indicates that the media resource failed to load or that the given
1732 // URL could not be resolved. In one atomic operation, run the following steps:
1734 // 6.1 - Set the error attribute to a new MediaError object whose code attribute is set to
1735 // MEDIA_ERR_SRC_NOT_SUPPORTED.
1736 m_error = MediaError::create(MediaError::MEDIA_ERR_SRC_NOT_SUPPORTED);
1738 // 6.2 - Forget the media element's media-resource-specific text tracks.
1740 // 6.3 - Set the element's networkState attribute to the NETWORK_NO_SOURCE value.
1741 m_networkState = NETWORK_NO_SOURCE;
1743 // 7 - Queue a task to fire a simple event named error at the media element.
1744 scheduleEvent(eventNames().errorEvent);
1746 #if ENABLE(MEDIA_SOURCE)
1750 // 8 - Set the element's delaying-the-load-event flag to false. This stops delaying the load event.
1751 setShouldDelayLoadEvent(false);
1753 // 9 - Abort these steps. Until the load() method is invoked or the src attribute is changed,
1754 // the element won't attempt to load another resource.
1756 updateDisplayState();
1759 renderer()->updateFromElement();
1762 void HTMLMediaElement::mediaLoadingFailedFatally(MediaPlayer::NetworkState error)
1764 LOG(Media, "HTMLMediaElement::mediaLoadingFailedFatally(%d)", static_cast<int>(error));
1766 // 1 - The user agent should cancel the fetching process.
1767 stopPeriodicTimers();
1768 m_loadState = WaitingForSource;
1770 // 2 - Set the error attribute to a new MediaError object whose code attribute is
1771 // set to MEDIA_ERR_NETWORK/MEDIA_ERR_DECODE.
1772 if (error == MediaPlayer::NetworkError)
1773 m_error = MediaError::create(MediaError::MEDIA_ERR_NETWORK);
1774 else if (error == MediaPlayer::DecodeError)
1775 m_error = MediaError::create(MediaError::MEDIA_ERR_DECODE);
1777 ASSERT_NOT_REACHED();
1779 // 3 - Queue a task to fire a simple event named error at the media element.
1780 scheduleEvent(eventNames().errorEvent);
1782 #if ENABLE(MEDIA_SOURCE)
1786 // 4 - Set the element's networkState attribute to the NETWORK_EMPTY value and queue a
1787 // task to fire a simple event called emptied at the element.
1788 m_networkState = NETWORK_EMPTY;
1789 scheduleEvent(eventNames().emptiedEvent);
1791 // 5 - Set the element's delaying-the-load-event flag to false. This stops delaying the load event.
1792 setShouldDelayLoadEvent(false);
1794 // 6 - Abort the overall resource selection algorithm.
1795 m_currentSourceNode = 0;
1798 void HTMLMediaElement::cancelPendingEventsAndCallbacks()
1800 LOG(Media, "HTMLMediaElement::cancelPendingEventsAndCallbacks");
1801 m_asyncEventQueue.cancelAllEvents();
1803 for (auto& source : childrenOfType<HTMLSourceElement>(*this))
1804 source.cancelPendingErrorEvent();
1807 Document* HTMLMediaElement::mediaPlayerOwningDocument()
1812 void HTMLMediaElement::mediaPlayerNetworkStateChanged(MediaPlayer*)
1814 beginProcessingMediaPlayerCallback();
1815 setNetworkState(m_player->networkState());
1816 endProcessingMediaPlayerCallback();
1819 static void logMediaLoadRequest(Page* page, const String& mediaEngine, const String& errorMessage, bool succeeded)
1821 if (!page || !page->settings().diagnosticLoggingEnabled())
1824 ChromeClient& chromeClient = page->chrome().client();
1827 chromeClient.logDiagnosticMessage(DiagnosticLoggingKeys::mediaLoadingFailedKey(), errorMessage, DiagnosticLoggingKeys::failKey());
1831 chromeClient.logDiagnosticMessage(DiagnosticLoggingKeys::mediaLoadedKey(), mediaEngine, DiagnosticLoggingKeys::noopKey());
1833 if (!page->hasSeenAnyMediaEngine())
1834 chromeClient.logDiagnosticMessage(DiagnosticLoggingKeys::pageContainsAtLeastOneMediaEngineKey(), emptyString(), DiagnosticLoggingKeys::noopKey());
1836 if (!page->hasSeenMediaEngine(mediaEngine))
1837 chromeClient.logDiagnosticMessage(DiagnosticLoggingKeys::pageContainsMediaEngineKey(), mediaEngine, DiagnosticLoggingKeys::noopKey());
1839 page->sawMediaEngine(mediaEngine);
1842 static String stringForNetworkState(MediaPlayer::NetworkState state)
1845 case MediaPlayer::Empty: return ASCIILiteral("Empty");
1846 case MediaPlayer::Idle: return ASCIILiteral("Idle");
1847 case MediaPlayer::Loading: return ASCIILiteral("Loading");
1848 case MediaPlayer::Loaded: return ASCIILiteral("Loaded");
1849 case MediaPlayer::FormatError: return ASCIILiteral("FormatError");
1850 case MediaPlayer::NetworkError: return ASCIILiteral("NetworkError");
1851 case MediaPlayer::DecodeError: return ASCIILiteral("DecodeError");
1852 default: return emptyString();
1856 void HTMLMediaElement::mediaLoadingFailed(MediaPlayer::NetworkState error)
1858 stopPeriodicTimers();
1860 // If we failed while trying to load a <source> element, the movie was never parsed, and there are more
1861 // <source> children, schedule the next one
1862 if (m_readyState < HAVE_METADATA && m_loadState == LoadingFromSourceElement) {
1864 if (m_currentSourceNode)
1865 m_currentSourceNode->scheduleErrorEvent();
1867 LOG(Media, "HTMLMediaElement::setNetworkState - error event not sent, <source> was removed");
1869 if (havePotentialSourceChild()) {
1870 LOG(Media, "HTMLMediaElement::setNetworkState - scheduling next <source>");
1871 scheduleNextSourceChild();
1873 LOG(Media, "HTMLMediaElement::setNetworkState - no more <source> elements, waiting");
1874 waitForSourceChange();
1880 if ((error == MediaPlayer::NetworkError && m_readyState >= HAVE_METADATA) || error == MediaPlayer::DecodeError)
1881 mediaLoadingFailedFatally(error);
1882 else if ((error == MediaPlayer::FormatError || error == MediaPlayer::NetworkError) && m_loadState == LoadingFromSrcAttr)
1885 updateDisplayState();
1886 if (hasMediaControls()) {
1887 mediaControls()->reset();
1888 mediaControls()->reportedError();
1891 logMediaLoadRequest(document().page(), String(), stringForNetworkState(error), false);
1894 void HTMLMediaElement::setNetworkState(MediaPlayer::NetworkState state)
1896 LOG(Media, "HTMLMediaElement::setNetworkState(%d) - current state is %d", static_cast<int>(state), static_cast<int>(m_networkState));
1898 if (state == MediaPlayer::Empty) {
1899 // Just update the cached state and leave, we can't do anything.
1900 m_networkState = NETWORK_EMPTY;
1904 if (state == MediaPlayer::FormatError || state == MediaPlayer::NetworkError || state == MediaPlayer::DecodeError) {
1905 mediaLoadingFailed(state);
1909 if (state == MediaPlayer::Idle) {
1910 if (m_networkState > NETWORK_IDLE) {
1911 changeNetworkStateFromLoadingToIdle();
1912 setShouldDelayLoadEvent(false);
1914 m_networkState = NETWORK_IDLE;
1918 if (state == MediaPlayer::Loading) {
1919 if (m_networkState < NETWORK_LOADING || m_networkState == NETWORK_NO_SOURCE)
1920 startProgressEventTimer();
1921 m_networkState = NETWORK_LOADING;
1924 if (state == MediaPlayer::Loaded) {
1925 if (m_networkState != NETWORK_IDLE)
1926 changeNetworkStateFromLoadingToIdle();
1927 m_completelyLoaded = true;
1930 if (hasMediaControls())
1931 mediaControls()->updateStatusDisplay();
1934 void HTMLMediaElement::changeNetworkStateFromLoadingToIdle()
1936 m_progressEventTimer.stop();
1937 if (hasMediaControls() && m_player->didLoadingProgress())
1938 mediaControls()->bufferingProgressed();
1940 // Schedule one last progress event so we guarantee that at least one is fired
1941 // for files that load very quickly.
1942 scheduleEvent(eventNames().progressEvent);
1943 scheduleEvent(eventNames().suspendEvent);
1944 m_networkState = NETWORK_IDLE;
1947 void HTMLMediaElement::mediaPlayerReadyStateChanged(MediaPlayer*)
1949 beginProcessingMediaPlayerCallback();
1951 setReadyState(m_player->readyState());
1953 endProcessingMediaPlayerCallback();
1956 void HTMLMediaElement::setReadyState(MediaPlayer::ReadyState state)
1958 LOG(Media, "HTMLMediaElement::setReadyState(%d) - current state is %d,", static_cast<int>(state), static_cast<int>(m_readyState));
1960 // Set "wasPotentiallyPlaying" BEFORE updating m_readyState, potentiallyPlaying() uses it
1961 bool wasPotentiallyPlaying = potentiallyPlaying();
1963 ReadyState oldState = m_readyState;
1964 ReadyState newState = static_cast<ReadyState>(state);
1966 #if ENABLE(VIDEO_TRACK)
1967 bool tracksAreReady = !RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled() || textTracksAreReady();
1969 if (newState == oldState && m_tracksAreReady == tracksAreReady)
1972 m_tracksAreReady = tracksAreReady;
1974 if (newState == oldState)
1976 bool tracksAreReady = true;
1980 m_readyState = newState;
1982 // If a media file has text tracks the readyState may not progress beyond HAVE_FUTURE_DATA until
1983 // the text tracks are ready, regardless of the state of the media file.
1984 if (newState <= HAVE_METADATA)
1985 m_readyState = newState;
1987 m_readyState = HAVE_CURRENT_DATA;
1990 if (oldState > m_readyStateMaximum)
1991 m_readyStateMaximum = oldState;
1993 if (m_networkState == NETWORK_EMPTY)
1997 // 4.8.10.9, step 11
1998 if (wasPotentiallyPlaying && m_readyState < HAVE_FUTURE_DATA)
1999 scheduleEvent(eventNames().waitingEvent);
2001 // 4.8.10.10 step 14 & 15.
2002 if (m_readyState >= HAVE_CURRENT_DATA)
2005 if (wasPotentiallyPlaying && m_readyState < HAVE_FUTURE_DATA) {
2007 invalidateCachedTime();
2008 scheduleTimeupdateEvent(false);
2009 scheduleEvent(eventNames().waitingEvent);
2013 if (m_readyState >= HAVE_METADATA && oldState < HAVE_METADATA) {
2014 prepareMediaFragmentURI();
2015 scheduleEvent(eventNames().durationchangeEvent);
2016 scheduleEvent(eventNames().loadedmetadataEvent);
2017 if (hasMediaControls())
2018 mediaControls()->loadedMetadata();
2020 renderer()->updateFromElement();
2022 logMediaLoadRequest(document().page(), m_player->engineDescription(), String(), true);
2025 bool shouldUpdateDisplayState = false;
2027 if (m_readyState >= HAVE_CURRENT_DATA && oldState < HAVE_CURRENT_DATA && !m_haveFiredLoadedData) {
2028 m_haveFiredLoadedData = true;
2029 shouldUpdateDisplayState = true;
2030 scheduleEvent(eventNames().loadeddataEvent);
2031 setShouldDelayLoadEvent(false);
2032 applyMediaFragmentURI();
2035 bool isPotentiallyPlaying = potentiallyPlaying();
2036 if (m_readyState == HAVE_FUTURE_DATA && oldState <= HAVE_CURRENT_DATA && tracksAreReady) {
2037 scheduleEvent(eventNames().canplayEvent);
2038 if (isPotentiallyPlaying)
2039 scheduleEvent(eventNames().playingEvent);
2040 shouldUpdateDisplayState = true;
2043 if (m_readyState == HAVE_ENOUGH_DATA && oldState < HAVE_ENOUGH_DATA && tracksAreReady) {
2044 if (oldState <= HAVE_CURRENT_DATA)
2045 scheduleEvent(eventNames().canplayEvent);
2047 scheduleEvent(eventNames().canplaythroughEvent);
2049 if (isPotentiallyPlaying && oldState <= HAVE_CURRENT_DATA)
2050 scheduleEvent(eventNames().playingEvent);
2052 if (m_autoplaying && m_paused && autoplay() && !document().isSandboxed(SandboxAutomaticFeatures) && m_mediaSession->playbackPermitted(*this)) {
2054 invalidateCachedTime();
2055 scheduleEvent(eventNames().playEvent);
2056 scheduleEvent(eventNames().playingEvent);
2059 shouldUpdateDisplayState = true;
2062 if (shouldUpdateDisplayState) {
2063 updateDisplayState();
2064 if (hasMediaControls()) {
2065 mediaControls()->refreshClosedCaptionsButtonVisibility();
2066 mediaControls()->updateStatusDisplay();
2071 updateMediaController();
2072 #if ENABLE(VIDEO_TRACK)
2073 if (RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
2074 updateActiveTextTrackCues(currentTime());
2078 #if ENABLE(ENCRYPTED_MEDIA)
2079 void HTMLMediaElement::mediaPlayerKeyAdded(MediaPlayer*, const String& keySystem, const String& sessionId)
2081 MediaKeyEventInit initializer;
2082 initializer.keySystem = keySystem;
2083 initializer.sessionId = sessionId;
2084 initializer.bubbles = false;
2085 initializer.cancelable = false;
2087 RefPtr<Event> event = MediaKeyEvent::create(eventNames().webkitkeyaddedEvent, initializer);
2088 event->setTarget(this);
2089 m_asyncEventQueue.enqueueEvent(event.release());
2092 void HTMLMediaElement::mediaPlayerKeyError(MediaPlayer*, const String& keySystem, const String& sessionId, MediaPlayerClient::MediaKeyErrorCode errorCode, unsigned short systemCode)
2094 MediaKeyError::Code mediaKeyErrorCode = MediaKeyError::MEDIA_KEYERR_UNKNOWN;
2095 switch (errorCode) {
2096 case MediaPlayerClient::UnknownError:
2097 mediaKeyErrorCode = MediaKeyError::MEDIA_KEYERR_UNKNOWN;
2099 case MediaPlayerClient::ClientError:
2100 mediaKeyErrorCode = MediaKeyError::MEDIA_KEYERR_CLIENT;
2102 case MediaPlayerClient::ServiceError:
2103 mediaKeyErrorCode = MediaKeyError::MEDIA_KEYERR_SERVICE;
2105 case MediaPlayerClient::OutputError:
2106 mediaKeyErrorCode = MediaKeyError::MEDIA_KEYERR_OUTPUT;
2108 case MediaPlayerClient::HardwareChangeError:
2109 mediaKeyErrorCode = MediaKeyError::MEDIA_KEYERR_HARDWARECHANGE;
2111 case MediaPlayerClient::DomainError:
2112 mediaKeyErrorCode = MediaKeyError::MEDIA_KEYERR_DOMAIN;
2116 MediaKeyEventInit initializer;
2117 initializer.keySystem = keySystem;
2118 initializer.sessionId = sessionId;
2119 initializer.errorCode = MediaKeyError::create(mediaKeyErrorCode);
2120 initializer.systemCode = systemCode;
2121 initializer.bubbles = false;
2122 initializer.cancelable = false;
2124 RefPtr<Event> event = MediaKeyEvent::create(eventNames().webkitkeyerrorEvent, initializer);
2125 event->setTarget(this);
2126 m_asyncEventQueue.enqueueEvent(event.release());
2129 void HTMLMediaElement::mediaPlayerKeyMessage(MediaPlayer*, const String& keySystem, const String& sessionId, const unsigned char* message, unsigned messageLength, const URL& defaultURL)
2131 MediaKeyEventInit initializer;
2132 initializer.keySystem = keySystem;
2133 initializer.sessionId = sessionId;
2134 initializer.message = Uint8Array::create(message, messageLength);
2135 initializer.defaultURL = defaultURL;
2136 initializer.bubbles = false;
2137 initializer.cancelable = false;
2139 RefPtr<Event> event = MediaKeyEvent::create(eventNames().webkitkeymessageEvent, initializer);
2140 event->setTarget(this);
2141 m_asyncEventQueue.enqueueEvent(event.release());
2144 bool HTMLMediaElement::mediaPlayerKeyNeeded(MediaPlayer*, const String& keySystem, const String& sessionId, const unsigned char* initData, unsigned initDataLength)
2146 if (!hasEventListeners(eventNames().webkitneedkeyEvent)) {
2147 m_error = MediaError::create(MediaError::MEDIA_ERR_ENCRYPTED);
2148 scheduleEvent(eventNames().errorEvent);
2152 MediaKeyEventInit initializer;
2153 initializer.keySystem = keySystem;
2154 initializer.sessionId = sessionId;
2155 initializer.initData = Uint8Array::create(initData, initDataLength);
2156 initializer.bubbles = false;
2157 initializer.cancelable = false;
2159 RefPtr<Event> event = MediaKeyEvent::create(eventNames().webkitneedkeyEvent, initializer);
2160 event->setTarget(this);
2161 m_asyncEventQueue.enqueueEvent(event.release());
2166 #if ENABLE(ENCRYPTED_MEDIA_V2)
2167 bool HTMLMediaElement::mediaPlayerKeyNeeded(MediaPlayer*, Uint8Array* initData)
2169 if (!hasEventListeners("webkitneedkey")) {
2170 m_error = MediaError::create(MediaError::MEDIA_ERR_ENCRYPTED);
2171 scheduleEvent(eventNames().errorEvent);
2175 MediaKeyNeededEventInit initializer;
2176 initializer.initData = initData;
2177 initializer.bubbles = false;
2178 initializer.cancelable = false;
2180 RefPtr<Event> event = MediaKeyNeededEvent::create(eventNames().webkitneedkeyEvent, initializer);
2181 event->setTarget(this);
2182 m_asyncEventQueue.enqueueEvent(event.release());
2187 void HTMLMediaElement::setMediaKeys(MediaKeys* mediaKeys)
2189 if (m_mediaKeys == mediaKeys)
2193 m_mediaKeys->setMediaElement(0);
2194 m_mediaKeys = mediaKeys;
2196 m_mediaKeys->setMediaElement(this);
2200 void HTMLMediaElement::progressEventTimerFired(Timer<HTMLMediaElement>&)
2203 if (m_networkState != NETWORK_LOADING)
2206 double time = monotonicallyIncreasingTime();
2207 double timedelta = time - m_previousProgressTime;
2209 if (m_player->didLoadingProgress()) {
2210 scheduleEvent(eventNames().progressEvent);
2211 m_previousProgressTime = time;
2212 m_sentStalledEvent = false;
2214 renderer()->updateFromElement();
2215 if (hasMediaControls())
2216 mediaControls()->bufferingProgressed();
2217 } else if (timedelta > 3.0 && !m_sentStalledEvent) {
2218 scheduleEvent(eventNames().stalledEvent);
2219 m_sentStalledEvent = true;
2220 setShouldDelayLoadEvent(false);
2224 void HTMLMediaElement::rewind(double timeDelta)
2226 LOG(Media, "HTMLMediaElement::rewind(%f)", timeDelta);
2227 setCurrentTime(std::max(currentTime() - timeDelta, minTimeSeekable()));
2230 void HTMLMediaElement::returnToRealtime()
2232 LOG(Media, "HTMLMediaElement::returnToRealtime");
2233 setCurrentTime(maxTimeSeekable());
2236 void HTMLMediaElement::addPlayedRange(double start, double end)
2238 LOG(Media, "HTMLMediaElement::addPlayedRange(%f, %f)", start, end);
2239 if (!m_playedTimeRanges)
2240 m_playedTimeRanges = TimeRanges::create();
2241 m_playedTimeRanges->add(start, end);
2244 bool HTMLMediaElement::supportsSave() const
2246 return m_player ? m_player->supportsSave() : false;
2249 bool HTMLMediaElement::supportsScanning() const
2251 return m_player ? m_player->supportsScanning() : false;
2254 void HTMLMediaElement::prepareToPlay()
2256 LOG(Media, "HTMLMediaElement::prepareToPlay(%p)", this);
2257 if (m_havePreparedToPlay)
2259 m_havePreparedToPlay = true;
2260 m_player->prepareToPlay();
2263 void HTMLMediaElement::fastSeek(double time)
2265 LOG(Media, "HTMLMediaElement::fastSeek(%f)", time);
2267 // 9. If the approximate-for-speed flag is set, adjust the new playback position to a value that will
2268 // allow for playback to resume promptly. If new playback position before this step is before current
2269 // playback position, then the adjusted new playback position must also be before the current playback
2270 // position. Similarly, if the new playback position before this step is after current playback position,
2271 // then the adjusted new playback position must also be after the current playback position.
2272 refreshCachedTime();
2273 double delta = time - currentTime();
2274 double negativeTolerance = delta >= 0 ? delta : std::numeric_limits<double>::infinity();
2275 double positiveTolerance = delta < 0 ? -delta : std::numeric_limits<double>::infinity();
2277 seekWithTolerance(time, negativeTolerance, positiveTolerance);
2280 void HTMLMediaElement::seek(double time)
2282 LOG(Media, "HTMLMediaElement::seek(%f)", time);
2283 seekWithTolerance(time, 0, 0);
2286 void HTMLMediaElement::seekWithTolerance(double time, double negativeTolerance, double positiveTolerance)
2290 // 1 - Set the media element's show poster flag to false.
2291 setDisplayMode(Video);
2293 // 2 - If the media element's readyState is HAVE_NOTHING, abort these steps.
2294 if (m_readyState == HAVE_NOTHING || !m_player)
2297 // If the media engine has been told to postpone loading data, let it go ahead now.
2298 if (m_preload < MediaPlayer::Auto && m_readyState < HAVE_FUTURE_DATA)
2301 // Get the current time before setting m_seeking, m_lastSeekTime is returned once it is set.
2302 refreshCachedTime();
2303 double now = currentTime();
2305 // 3 - If the element's seeking IDL attribute is true, then another instance of this algorithm is
2306 // already running. Abort that other instance of the algorithm without waiting for the step that
2307 // it is running to complete.
2308 // Nothing specific to be done here.
2310 // 4 - Set the seeking IDL attribute to true.
2311 // The flag will be cleared when the engine tells us the time has actually changed.
2314 // 5 - If the seek was in response to a DOM method call or setting of an IDL attribute, then continue
2315 // the script. The remainder of these steps must be run asynchronously.
2316 // Nothing to be done here.
2318 // 6 - If the new playback position is later than the end of the media resource, then let it be the end
2319 // of the media resource instead.
2320 time = std::min(time, duration());
2322 // 7 - If the new playback position is less than the earliest possible position, let it be that position instead.
2323 double earliestTime = m_player->startTime();
2324 time = std::max(time, earliestTime);
2326 // Ask the media engine for the time value in the movie's time scale before comparing with current time. This
2327 // is necessary because if the seek time is not equal to currentTime but the delta is less than the movie's
2328 // time scale, we will ask the media engine to "seek" to the current movie time, which may be a noop and
2329 // not generate a timechanged callback. This means m_seeking will never be cleared and we will never
2330 // fire a 'seeked' event.
2332 double mediaTime = m_player->mediaTimeForTimeValue(time);
2333 if (time != mediaTime)
2334 LOG(Media, "HTMLMediaElement::seek(%f) - media timeline equivalent is %f", time, mediaTime);
2336 time = m_player->mediaTimeForTimeValue(time);
2338 // 8 - If the (possibly now changed) new playback position is not in one of the ranges given in the
2339 // seekable attribute, then let it be the position in one of the ranges given in the seekable attribute
2340 // that is the nearest to the new playback position. ... If there are no ranges given in the seekable
2341 // attribute then set the seeking IDL attribute to false and abort these steps.
2342 RefPtr<TimeRanges> seekableRanges = seekable();
2344 // Short circuit seeking to the current time by just firing the events if no seek is required.
2345 // Don't skip calling the media engine if we are in poster mode because a seek should always
2346 // cancel poster display.
2347 bool noSeekRequired = !seekableRanges->length() || (time == now && displayMode() != Poster);
2349 #if ENABLE(MEDIA_SOURCE)
2350 // Always notify the media engine of a seek if the source is not closed. This ensures that the source is
2351 // always in a flushed state when the 'seeking' event fires.
2352 if (m_mediaSource && m_mediaSource->isClosed())
2353 noSeekRequired = false;
2356 if (noSeekRequired) {
2358 scheduleEvent(eventNames().seekingEvent);
2359 scheduleTimeupdateEvent(false);
2360 scheduleEvent(eventNames().seekedEvent);
2365 time = seekableRanges->nearest(time);
2368 if (m_lastSeekTime < now)
2369 addPlayedRange(m_lastSeekTime, now);
2371 m_lastSeekTime = time;
2372 m_sentEndEvent = false;
2374 // 10 - Queue a task to fire a simple event named seeking at the element.
2375 scheduleEvent(eventNames().seekingEvent);
2377 // 11 - Set the current playback position to the given new playback position
2378 m_player->seekWithTolerance(time, negativeTolerance, positiveTolerance);
2380 // 12 - Wait until the user agent has established whether or not the media data for the new playback
2381 // position is available, and, if it is, until it has decoded enough data to play back that position.
2382 // 13 - Await a stable state. The synchronous section consists of all the remaining steps of this algorithm.
2385 void HTMLMediaElement::finishSeek()
2387 LOG(Media, "HTMLMediaElement::finishSeek");
2389 #if ENABLE(MEDIA_SOURCE)
2391 m_mediaSource->monitorSourceBuffers();
2395 // 14 - Set the seeking IDL attribute to false.
2398 // 15 - Run the time maches on steps.
2399 // Handled by mediaPlayerTimeChanged().
2401 // 16 - Queue a task to fire a simple event named timeupdate at the element.
2402 scheduleEvent(eventNames().timeupdateEvent);
2404 // 17 - Queue a task to fire a simple event named seeked at the element.
2405 scheduleEvent(eventNames().seekedEvent);
2408 HTMLMediaElement::ReadyState HTMLMediaElement::readyState() const
2410 return m_readyState;
2413 MediaPlayer::MovieLoadType HTMLMediaElement::movieLoadType() const
2415 return m_player ? m_player->movieLoadType() : MediaPlayer::Unknown;
2418 bool HTMLMediaElement::hasAudio() const
2420 return m_player ? m_player->hasAudio() : false;
2423 bool HTMLMediaElement::seeking() const
2428 void HTMLMediaElement::refreshCachedTime() const
2430 m_cachedTime = m_player->currentTime();
2431 m_clockTimeAtLastCachedTimeUpdate = monotonicallyIncreasingTime();
2434 void HTMLMediaElement::invalidateCachedTime()
2436 LOG(Media, "HTMLMediaElement::invalidateCachedTime");
2438 // Don't try to cache movie time when playback first starts as the time reported by the engine
2439 // sometimes fluctuates for a short amount of time, so the cached time will be off if we take it
2441 static const double minimumTimePlayingBeforeCacheSnapshot = 0.5;
2443 m_minimumClockTimeToUpdateCachedTime = monotonicallyIncreasingTime() + minimumTimePlayingBeforeCacheSnapshot;
2444 m_cachedTime = MediaPlayer::invalidTime();
2448 double HTMLMediaElement::currentTime() const
2450 #if LOG_CACHED_TIME_WARNINGS
2451 static const double minCachedDeltaForWarning = 0.01;
2458 LOG(Media, "HTMLMediaElement::currentTime - seeking, returning %f", m_lastSeekTime);
2459 return m_lastSeekTime;
2462 if (m_cachedTime != MediaPlayer::invalidTime() && m_paused) {
2463 #if LOG_CACHED_TIME_WARNINGS
2464 double delta = m_cachedTime - m_player->currentTime();
2465 if (delta > minCachedDeltaForWarning)
2466 LOG(Media, "HTMLMediaElement::currentTime - WARNING, cached time is %f seconds off of media time when paused", delta);
2468 return m_cachedTime;
2471 // Is it too soon use a cached time?
2472 double now = monotonicallyIncreasingTime();
2473 double maximumDurationToCacheMediaTime = m_player->maximumDurationToCacheMediaTime();
2475 if (maximumDurationToCacheMediaTime && m_cachedTime != MediaPlayer::invalidTime() && !m_paused && now > m_minimumClockTimeToUpdateCachedTime) {
2476 double clockDelta = now - m_clockTimeAtLastCachedTimeUpdate;
2478 // Not too soon, use the cached time only if it hasn't expired.
2479 if (clockDelta < maximumDurationToCacheMediaTime) {
2480 double adjustedCacheTime = m_cachedTime + (m_playbackRate * clockDelta);
2482 #if LOG_CACHED_TIME_WARNINGS
2483 double delta = adjustedCacheTime - m_player->currentTime();
2484 if (delta > minCachedDeltaForWarning)
2485 LOG(Media, "HTMLMediaElement::currentTime - WARNING, cached time is %f seconds off of media time when playing", delta);
2487 return adjustedCacheTime;
2491 #if LOG_CACHED_TIME_WARNINGS
2492 if (maximumDurationToCacheMediaTime && now > m_minimumClockTimeToUpdateCachedTime && m_cachedTime != MediaPlayer::invalidTime()) {
2493 double clockDelta = now - m_clockTimeAtLastCachedTimeUpdate;
2494 double delta = m_cachedTime + (m_playbackRate * clockDelta) - m_player->currentTime();
2495 LOG(Media, "HTMLMediaElement::currentTime - cached time was %f seconds off of media time when it expired", delta);
2499 refreshCachedTime();
2501 return m_cachedTime;
2504 void HTMLMediaElement::setCurrentTime(double time)
2506 if (m_mediaController)
2512 double HTMLMediaElement::duration() const
2514 if (m_player && m_readyState >= HAVE_METADATA)
2515 return m_player->duration();
2517 return std::numeric_limits<double>::quiet_NaN();
2520 bool HTMLMediaElement::paused() const
2522 // As of this writing, JavaScript garbage collection calls this function directly. In the past
2523 // we had problems where this was called on an object after a bad cast. The assertion below
2524 // made our regression test detect the problem, so we should keep it because of that. But note
2525 // that the value of the assertion relies on the compiler not being smart enough to know that
2526 // isHTMLUnknownElement is guaranteed to return false for an HTMLMediaElement.
2527 ASSERT(!isHTMLUnknownElement());
2532 double HTMLMediaElement::defaultPlaybackRate() const
2534 return m_defaultPlaybackRate;
2537 void HTMLMediaElement::setDefaultPlaybackRate(double rate)
2539 if (m_defaultPlaybackRate != rate) {
2540 m_defaultPlaybackRate = rate;
2541 scheduleEvent(eventNames().ratechangeEvent);
2545 double HTMLMediaElement::playbackRate() const
2547 return m_playbackRate;
2550 void HTMLMediaElement::setPlaybackRate(double rate)
2552 LOG(Media, "HTMLMediaElement::setPlaybackRate(%f)", rate);
2554 if (m_player && potentiallyPlaying() && m_player->rate() != rate && !m_mediaController)
2555 m_player->setRate(rate);
2557 if (m_playbackRate != rate) {
2558 m_playbackRate = rate;
2559 invalidateCachedTime();
2560 scheduleEvent(eventNames().ratechangeEvent);
2564 void HTMLMediaElement::updatePlaybackRate()
2566 double effectiveRate = m_mediaController ? m_mediaController->playbackRate() : m_playbackRate;
2567 if (m_player && potentiallyPlaying() && m_player->rate() != effectiveRate)
2568 m_player->setRate(effectiveRate);
2571 bool HTMLMediaElement::webkitPreservesPitch() const
2573 return m_webkitPreservesPitch;
2576 void HTMLMediaElement::setWebkitPreservesPitch(bool preservesPitch)
2578 LOG(Media, "HTMLMediaElement::setWebkitPreservesPitch(%s)", boolString(preservesPitch));
2580 m_webkitPreservesPitch = preservesPitch;
2585 m_player->setPreservesPitch(preservesPitch);
2588 bool HTMLMediaElement::ended() const
2590 // 4.8.10.8 Playing the media resource
2591 // The ended attribute must return true if the media element has ended
2592 // playback and the direction of playback is forwards, and false otherwise.
2593 return endedPlayback() && m_playbackRate > 0;
2596 bool HTMLMediaElement::autoplay() const
2599 // Unless the restriction on requiring user actions has been lifted, we do not
2600 // allow playback to start just because the page has "autoplay". They are either
2601 // lifted through Settings, or once the user explictly calls load() or play()
2602 // because they have OK'ed us loading data. This allows playback to continue if
2603 // the URL is changed while the movie is playing.
2604 if (!m_mediaSession->playbackPermitted(*this) || !m_mediaSession->dataLoadingPermitted(*this))
2608 return fastHasAttribute(autoplayAttr);
2611 String HTMLMediaElement::preload() const
2613 switch (m_preload) {
2614 case MediaPlayer::None:
2615 return ASCIILiteral("none");
2617 case MediaPlayer::MetaData:
2618 return ASCIILiteral("metadata");
2620 case MediaPlayer::Auto:
2621 return ASCIILiteral("auto");
2625 ASSERT_NOT_REACHED();
2629 void HTMLMediaElement::setPreload(const String& preload)
2631 LOG(Media, "HTMLMediaElement::setPreload(%s)", preload.utf8().data());
2632 setAttribute(preloadAttr, preload);
2635 void HTMLMediaElement::play()
2637 LOG(Media, "HTMLMediaElement::play()");
2639 if (!m_mediaSession->playbackPermitted(*this))
2641 if (ScriptController::processingUserGesture())
2642 removeBehaviorsRestrictionsAfterFirstUserGesture();
2644 if (m_mediaSession->state() == MediaSession::Interrupted) {
2645 m_resumePlaybackAfterInterruption = true;
2652 void HTMLMediaElement::playInternal()
2654 LOG(Media, "HTMLMediaElement::playInternal");
2656 // 4.8.10.9. Playing the media resource
2657 if (!m_player || m_networkState == NETWORK_EMPTY)
2658 scheduleDelayedAction(LoadMediaResource);
2660 if (endedPlayback())
2663 if (m_mediaController)
2664 m_mediaController->bringElementUpToSpeed(this);
2668 invalidateCachedTime();
2669 scheduleEvent(eventNames().playEvent);
2671 if (m_readyState <= HAVE_CURRENT_DATA)
2672 scheduleEvent(eventNames().waitingEvent);
2673 else if (m_readyState >= HAVE_FUTURE_DATA)
2674 scheduleEvent(eventNames().playingEvent);
2676 m_autoplaying = false;
2678 m_requestingPlay = true;
2681 updateMediaController();
2684 void HTMLMediaElement::pause()
2686 LOG(Media, "HTMLMediaElement::pause()");
2688 if (!m_mediaSession->playbackPermitted(*this))
2691 if (m_mediaSession->state() == MediaSession::Interrupted) {
2692 m_resumePlaybackAfterInterruption = false;
2700 void HTMLMediaElement::pauseInternal()
2702 LOG(Media, "HTMLMediaElement::pauseInternal");
2704 // 4.8.10.9. Playing the media resource
2705 if (!m_player || m_networkState == NETWORK_EMPTY) {
2707 // Unless the restriction on media requiring user action has been lifted
2708 // don't trigger loading if a script calls pause().
2709 if (!m_mediaSession->playbackPermitted(*this))
2712 scheduleDelayedAction(LoadMediaResource);
2715 m_autoplaying = false;
2719 scheduleTimeupdateEvent(false);
2720 scheduleEvent(eventNames().pauseEvent);
2726 #if ENABLE(MEDIA_SOURCE)
2727 void HTMLMediaElement::closeMediaSource()
2732 m_mediaSource->close();
2737 #if ENABLE(ENCRYPTED_MEDIA)
2738 void HTMLMediaElement::webkitGenerateKeyRequest(const String& keySystem, PassRefPtr<Uint8Array> initData, ExceptionCode& ec)
2740 #if ENABLE(ENCRYPTED_MEDIA_V2)
2741 static bool firstTime = true;
2742 if (firstTime && scriptExecutionContext()) {
2743 scriptExecutionContext()->addConsoleMessage(JSMessageSource, WarningMessageLevel, "'HTMLMediaElement.webkitGenerateKeyRequest()' is deprecated. Use 'MediaKeys.createSession()' instead.");
2748 if (keySystem.isEmpty()) {
2754 ec = INVALID_STATE_ERR;
2758 const unsigned char* initDataPointer = 0;
2759 unsigned initDataLength = 0;
2761 initDataPointer = initData->data();
2762 initDataLength = initData->length();
2765 MediaPlayer::MediaKeyException result = m_player->generateKeyRequest(keySystem, initDataPointer, initDataLength);
2766 ec = exceptionCodeForMediaKeyException(result);
2769 void HTMLMediaElement::webkitGenerateKeyRequest(const String& keySystem, ExceptionCode& ec)
2771 webkitGenerateKeyRequest(keySystem, Uint8Array::create(0), ec);
2774 void HTMLMediaElement::webkitAddKey(const String& keySystem, PassRefPtr<Uint8Array> key, PassRefPtr<Uint8Array> initData, const String& sessionId, ExceptionCode& ec)
2776 #if ENABLE(ENCRYPTED_MEDIA_V2)
2777 static bool firstTime = true;
2778 if (firstTime && scriptExecutionContext()) {
2779 scriptExecutionContext()->addConsoleMessage(JSMessageSource, WarningMessageLevel, "'HTMLMediaElement.webkitAddKey()' is deprecated. Use 'MediaKeySession.update()' instead.");
2784 if (keySystem.isEmpty()) {
2794 if (!key->length()) {
2795 ec = TYPE_MISMATCH_ERR;
2800 ec = INVALID_STATE_ERR;
2804 const unsigned char* initDataPointer = 0;
2805 unsigned initDataLength = 0;
2807 initDataPointer = initData->data();
2808 initDataLength = initData->length();
2811 MediaPlayer::MediaKeyException result = m_player->addKey(keySystem, key->data(), key->length(), initDataPointer, initDataLength, sessionId);
2812 ec = exceptionCodeForMediaKeyException(result);
2815 void HTMLMediaElement::webkitAddKey(const String& keySystem, PassRefPtr<Uint8Array> key, ExceptionCode& ec)
2817 webkitAddKey(keySystem, key, Uint8Array::create(0), String(), ec);
2820 void HTMLMediaElement::webkitCancelKeyRequest(const String& keySystem, const String& sessionId, ExceptionCode& ec)
2822 if (keySystem.isEmpty()) {
2828 ec = INVALID_STATE_ERR;
2832 MediaPlayer::MediaKeyException result = m_player->cancelKeyRequest(keySystem, sessionId);
2833 ec = exceptionCodeForMediaKeyException(result);
2838 bool HTMLMediaElement::loop() const
2840 return fastHasAttribute(loopAttr);
2843 void HTMLMediaElement::setLoop(bool b)
2845 LOG(Media, "HTMLMediaElement::setLoop(%s)", boolString(b));
2846 setBooleanAttribute(loopAttr, b);
2849 bool HTMLMediaElement::controls() const
2851 Frame* frame = document().frame();
2853 // always show controls when scripting is disabled
2854 if (frame && !frame->script().canExecuteScripts(NotAboutToExecuteScript))
2857 // always show controls for video when fullscreen playback is required.
2858 if (isVideo() && document().page() && document().page()->chrome().requiresFullscreenForVideoPlayback())
2861 // Always show controls when in full screen mode.
2865 return fastHasAttribute(controlsAttr);
2868 void HTMLMediaElement::setControls(bool b)
2870 LOG(Media, "HTMLMediaElement::setControls(%s)", boolString(b));
2871 setBooleanAttribute(controlsAttr, b);
2874 double HTMLMediaElement::volume() const
2879 void HTMLMediaElement::setVolume(double vol, ExceptionCode& ec)
2881 LOG(Media, "HTMLMediaElement::setVolume(%f)", vol);
2883 if (vol < 0.0f || vol > 1.0f) {
2884 ec = INDEX_SIZE_ERR;
2889 if (m_volume != vol) {
2891 m_volumeInitialized = true;
2893 scheduleEvent(eventNames().volumechangeEvent);
2898 bool HTMLMediaElement::muted() const
2903 void HTMLMediaElement::setMuted(bool muted)
2905 LOG(Media, "HTMLMediaElement::setMuted(%s)", boolString(muted));
2908 UNUSED_PARAM(muted);
2910 if (m_muted != muted) {
2912 // Avoid recursion when the player reports volume changes.
2913 if (!processingMediaPlayerCallback()) {
2915 m_player->setMuted(m_muted);
2916 if (hasMediaControls())
2917 mediaControls()->changedMute();
2920 scheduleEvent(eventNames().volumechangeEvent);
2925 void HTMLMediaElement::togglePlayState()
2927 LOG(Media, "HTMLMediaElement::togglePlayState - canPlay() is %s", boolString(canPlay()));
2929 // We can safely call the internal play/pause methods, which don't check restrictions, because
2930 // this method is only called from the built-in media controller
2932 updatePlaybackRate();
2938 void HTMLMediaElement::beginScrubbing()
2940 LOG(Media, "HTMLMediaElement::beginScrubbing - paused() is %s", boolString(paused()));
2944 // Because a media element stays in non-paused state when it reaches end, playback resumes
2945 // when the slider is dragged from the end to another position unless we pause first. Do
2946 // a "hard pause" so an event is generated, since we want to stay paused after scrubbing finishes.
2949 // Not at the end but we still want to pause playback so the media engine doesn't try to
2950 // continue playing during scrubbing. Pause without generating an event as we will
2951 // unpause after scrubbing finishes.
2952 setPausedInternal(true);
2957 void HTMLMediaElement::endScrubbing()
2959 LOG(Media, "HTMLMediaElement::endScrubbing - m_pausedInternal is %s", boolString(m_pausedInternal));
2961 if (m_pausedInternal)
2962 setPausedInternal(false);
2965 // The spec says to fire periodic timeupdate events (those sent while playing) every
2966 // "15 to 250ms", we choose the slowest frequency
2967 static const double maxTimeupdateEventFrequency = 0.25;
2969 void HTMLMediaElement::startPlaybackProgressTimer()
2971 if (m_playbackProgressTimer.isActive())
2974 m_previousProgressTime = monotonicallyIncreasingTime();
2975 m_playbackProgressTimer.startRepeating(maxTimeupdateEventFrequency);
2978 void HTMLMediaElement::playbackProgressTimerFired(Timer<HTMLMediaElement>&)
2982 if (m_fragmentEndTime != MediaPlayer::invalidTime() && currentTime() >= m_fragmentEndTime && m_playbackRate > 0) {
2983 m_fragmentEndTime = MediaPlayer::invalidTime();
2984 if (!m_mediaController && !m_paused) {
2985 // changes paused to true and fires a simple event named pause at the media element.
2990 scheduleTimeupdateEvent(true);
2992 if (!m_playbackRate)
2995 if (!m_paused && hasMediaControls())
2996 mediaControls()->playbackProgressed();
2998 #if ENABLE(VIDEO_TRACK)
2999 if (RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
3000 updateActiveTextTrackCues(currentTime());
3003 #if ENABLE(MEDIA_SOURCE)
3005 m_mediaSource->monitorSourceBuffers();
3009 void HTMLMediaElement::scheduleTimeupdateEvent(bool periodicEvent)
3011 double now = monotonicallyIncreasingTime();
3012 double timedelta = now - m_clockTimeAtLastUpdateEvent;
3014 // throttle the periodic events
3015 if (periodicEvent && timedelta < maxTimeupdateEventFrequency)
3018 // Some media engines make multiple "time changed" callbacks at the same time, but we only want one
3019 // event at a given time so filter here
3020 double movieTime = currentTime();
3021 if (movieTime != m_lastTimeUpdateEventMovieTime) {
3022 scheduleEvent(eventNames().timeupdateEvent);
3023 m_clockTimeAtLastUpdateEvent = now;
3024 m_lastTimeUpdateEventMovieTime = movieTime;
3028 bool HTMLMediaElement::canPlay() const
3030 return paused() || ended() || m_readyState < HAVE_METADATA;
3033 double HTMLMediaElement::percentLoaded() const
3037 double duration = m_player->duration();
3039 if (!duration || std::isinf(duration))
3042 double buffered = 0;
3043 RefPtr<TimeRanges> timeRanges = m_player->buffered();
3044 for (unsigned i = 0; i < timeRanges->length(); ++i) {
3045 double start = timeRanges->start(i, IGNORE_EXCEPTION);
3046 double end = timeRanges->end(i, IGNORE_EXCEPTION);
3047 buffered += end - start;
3049 return buffered / duration;
3052 #if ENABLE(VIDEO_TRACK)
3054 void HTMLMediaElement::mediaPlayerDidAddAudioTrack(PassRefPtr<AudioTrackPrivate> prpTrack)
3056 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
3059 addAudioTrack(AudioTrack::create(this, prpTrack));
3062 void HTMLMediaElement::mediaPlayerDidAddTextTrack(PassRefPtr<InbandTextTrackPrivate> prpTrack)
3064 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
3067 // 4.8.10.12.2 Sourcing in-band text tracks
3068 // 1. Associate the relevant data with a new text track and its corresponding new TextTrack object.
3069 RefPtr<InbandTextTrack> textTrack = InbandTextTrack::create(ActiveDOMObject::scriptExecutionContext(), this, prpTrack);
3070 textTrack->setMediaElement(this);
3072 // 2. Set the new text track's kind, label, and language based on the semantics of the relevant data,
3073 // as defined by the relevant specification. If there is no label in that data, then the label must
3074 // be set to the empty string.
3075 // 3. Associate the text track list of cues with the rules for updating the text track rendering appropriate
3076 // for the format in question.
3077 // 4. If the new text track's kind is metadata, then set the text track in-band metadata track dispatch type
3078 // as follows, based on the type of the media resource:
3079 // 5. Populate the new text track's list of cues with the cues parsed so far, folllowing the guidelines for exposing
3080 // cues, and begin updating it dynamically as necessary.
3081 // - Thess are all done by the media engine.
3083 // 6. Set the new text track's readiness state to loaded.
3084 textTrack->setReadinessState(TextTrack::Loaded);
3086 // 7. Set the new text track's mode to the mode consistent with the user's preferences and the requirements of
3087 // the relevant specification for the data.
3088 // - This will happen in configureTextTracks()
3089 scheduleDelayedAction(ConfigureTextTracks);
3091 // 8. Add the new text track to the media element's list of text tracks.
3092 // 9. Fire an event with the name addtrack, that does not bubble and is not cancelable, and that uses the TrackEvent
3093 // interface, with the track attribute initialized to the text track's TextTrack object, at the media element's
3094 // textTracks attribute's TextTrackList object.
3095 addTextTrack(textTrack.release());
3098 void HTMLMediaElement::mediaPlayerDidAddVideoTrack(PassRefPtr<VideoTrackPrivate> prpTrack)
3100 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
3103 addVideoTrack(VideoTrack::create(this, prpTrack));
3106 void HTMLMediaElement::mediaPlayerDidRemoveAudioTrack(PassRefPtr<AudioTrackPrivate> prpTrack)
3108 prpTrack->willBeRemoved();
3111 void HTMLMediaElement::mediaPlayerDidRemoveTextTrack(PassRefPtr<InbandTextTrackPrivate> prpTrack)
3113 prpTrack->willBeRemoved();
3116 void HTMLMediaElement::mediaPlayerDidRemoveVideoTrack(PassRefPtr<VideoTrackPrivate> prpTrack)
3118 prpTrack->willBeRemoved();
3121 #if USE(PLATFORM_TEXT_TRACK_MENU)
3122 void HTMLMediaElement::setSelectedTextTrack(PassRefPtr<PlatformTextTrack> platformTrack)
3127 TrackDisplayUpdateScope scope(this);
3129 if (!platformTrack) {
3130 setSelectedTextTrack(TextTrack::captionMenuOffItem());
3134 TextTrack* textTrack;
3135 if (platformTrack == PlatformTextTrack::captionMenuOffItem())
3136 textTrack = TextTrack::captionMenuOffItem();
3137 else if (platformTrack == PlatformTextTrack::captionMenuAutomaticItem())
3138 textTrack = TextTrack::captionMenuAutomaticItem();
3141 for (i = 0; i < m_textTracks->length(); ++i) {
3142 textTrack = m_textTracks->item(i);
3144 if (textTrack->platformTextTrack() == platformTrack)
3147 if (i == m_textTracks->length())
3151 setSelectedTextTrack(textTrack);
3154 Vector<RefPtr<PlatformTextTrack>> HTMLMediaElement::platformTextTracks()
3156 if (!m_textTracks || !m_textTracks->length())
3157 return Vector<RefPtr<PlatformTextTrack>>();
3159 Vector<RefPtr<PlatformTextTrack>> platformTracks;
3160 for (size_t i = 0; i < m_textTracks->length(); ++i)
3161 platformTracks.append(m_textTracks->item(i)->platformTextTrack());
3163 return platformTracks;
3166 void HTMLMediaElement::notifyMediaPlayerOfTextTrackChanges()
3168 if (!m_textTracks || !m_textTracks->length() || !platformTextTrackMenu())
3171 m_platformMenu->tracksDidChange();
3174 PlatformTextTrackMenuInterface* HTMLMediaElement::platformTextTrackMenu()
3177 return m_platformMenu.get();
3179 if (!m_player || !m_player->implementsTextTrackControls())
3182 m_platformMenu = m_player->textTrackMenu();
3183 if (!m_platformMenu)
3186 m_platformMenu->setClient(this);
3188 return m_platformMenu.get();
3190 #endif // #if USE(PLATFORM_TEXT_TRACK_MENU)
3192 void HTMLMediaElement::closeCaptionTracksChanged()
3194 if (hasMediaControls())
3195 mediaControls()->closedCaptionTracksChanged();
3197 #if USE(PLATFORM_TEXT_TRACK_MENU)
3198 if (m_player && m_player->implementsTextTrackControls())
3199 scheduleDelayedAction(TextTrackChangesNotification);
3203 void HTMLMediaElement::addAudioTrack(PassRefPtr<AudioTrack> track)
3205 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
3208 audioTracks()->append(track);
3211 void HTMLMediaElement::addTextTrack(PassRefPtr<TextTrack> track)
3213 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
3216 textTracks()->append(track);
3218 closeCaptionTracksChanged();
3221 void HTMLMediaElement::addVideoTrack(PassRefPtr<VideoTrack> track)
3223 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
3226 videoTracks()->append(track);
3229 void HTMLMediaElement::removeAudioTrack(AudioTrack* track)
3231 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
3234 m_audioTracks->remove(track);
3237 void HTMLMediaElement::removeTextTrack(TextTrack* track)
3239 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
3242 TrackDisplayUpdateScope scope(this);
3243 TextTrackCueList* cues = track->cues();
3245 textTrackRemoveCues(track, cues);
3246 track->clearClient();
3247 m_textTracks->remove(track);
3249 closeCaptionTracksChanged();
3252 void HTMLMediaElement::removeVideoTrack(VideoTrack* track)
3254 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
3257 m_videoTracks->remove(track);
3260 void HTMLMediaElement::removeAllInbandTracks()
3262 while (m_audioTracks && m_audioTracks->length())
3263 removeAudioTrack(m_audioTracks->lastItem());
3266 TrackDisplayUpdateScope scope(this);
3267 for (int i = m_textTracks->length() - 1; i >= 0; --i) {
3268 TextTrack* track = m_textTracks->item(i);
3270 if (track->trackType() == TextTrack::InBand)
3271 removeTextTrack(track);
3275 while (m_videoTracks && m_videoTracks->length())
3276 removeVideoTrack(m_videoTracks->lastItem());
3279 PassRefPtr<TextTrack> HTMLMediaElement::addTextTrack(const String& kind, const String& label, const String& language, ExceptionCode& ec)
3281 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
3284 // 4.8.10.12.4 Text track API
3285 // The addTextTrack(kind, label, language) method of media elements, when invoked, must run the following steps:
3287 // 1. If kind is not one of the following strings, then throw a SyntaxError exception and abort these steps
3288 if (!TextTrack::isValidKindKeyword(kind)) {
3293 // 2. If the label argument was omitted, let label be the empty string.
3294 // 3. If the language argument was omitted, let language be the empty string.
3295 // 4. Create a new TextTrack object.
3297 // 5. Create a new text track corresponding to the new object, and set its text track kind to kind, its text
3298 // track label to label, its text track language to language...
3299 RefPtr<TextTrack> textTrack = TextTrack::create(ActiveDOMObject::scriptExecutionContext(), this, kind, emptyString(), label, language);
3301 // Note, due to side effects when changing track parameters, we have to
3302 // first append the track to the text track list.
3304 // 6. Add the new text track to the media element's list of text tracks.
3305 addTextTrack(textTrack);
3307 // ... its text track readiness state to the text track loaded state ...
3308 textTrack->setReadinessState(TextTrack::Loaded);
3310 // ... its text track mode to the text track hidden mode, and its text track list of cues to an empty list ...
3311 textTrack->setMode(TextTrack::hiddenKeyword());
3313 return textTrack.release();
3316 AudioTrackList* HTMLMediaElement::audioTracks()
3318 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
3322 m_audioTracks = AudioTrackList::create(this, ActiveDOMObject::scriptExecutionContext());
3324 return m_audioTracks.get();
3327 TextTrackList* HTMLMediaElement::textTracks()
3329 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
3333 m_textTracks = TextTrackList::create(this, ActiveDOMObject::scriptExecutionContext());
3335 return m_textTracks.get();
3338 VideoTrackList* HTMLMediaElement::videoTracks()
3340 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
3344 m_videoTracks = VideoTrackList::create(this, ActiveDOMObject::scriptExecutionContext());
3346 return m_videoTracks.get();
3349 void HTMLMediaElement::didAddTextTrack(HTMLTrackElement* trackElement)
3351 ASSERT(trackElement->hasTagName(trackTag));
3353 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
3356 // 4.8.10.12.3 Sourcing out-of-band text tracks
3357 // When a track element's parent element changes and the new parent is a media element,
3358 // then the user agent must add the track element's corresponding text track to the
3359 // media element's list of text tracks ... [continues in TextTrackList::append]
3360 RefPtr<TextTrack> textTrack = trackElement->track();
3364 addTextTrack(textTrack.release());
3366 // Do not schedule the track loading until parsing finishes so we don't start before all tracks
3367 // in the markup have been added.
3368 if (!m_parsingInProgress)
3369 scheduleDelayedAction(ConfigureTextTracks);
3371 if (hasMediaControls())
3372 mediaControls()->closedCaptionTracksChanged();
3375 void HTMLMediaElement::didRemoveTextTrack(HTMLTrackElement* trackElement)
3377 ASSERT(trackElement->hasTagName(trackTag));
3379 if (!RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
3383 if (trackElement->hasTagName(trackTag)) {
3384 URL url = trackElement->getNonEmptyURLAttribute(srcAttr);
3385 LOG(Media, "HTMLMediaElement::didRemoveTrack - 'src' is %s", urlForLoggingMedia(url).utf8().data());
3389 RefPtr<TextTrack> textTrack = trackElement->track();
3393 textTrack->setHasBeenConfigured(false);
3398 // 4.8.10.12.3 Sourcing out-of-band text tracks
3399 // When a track element's parent element changes and the old parent was a media element,
3400 // then the user agent must remove the track element's corresponding text track from the
3401 // media element's list of text tracks.
3402 removeTextTrack(textTrack.get());
3404 size_t index = m_textTracksWhenResourceSelectionBegan.find(textTrack.get());
3405 if (index != notFound)
3406 m_textTracksWhenResourceSelectionBegan.remove(index);
3409 void HTMLMediaElement::configureTextTrackGroup(const TrackGroup& group)
3411 ASSERT(group.tracks.size());
3413 LOG(Media, "HTMLMediaElement::configureTextTrackGroup");
3415 Page* page = document().page();
3416 CaptionUserPreferences* captionPreferences = page? page->group().captionPreferences() : 0;
3417 CaptionUserPreferences::CaptionDisplayMode displayMode = captionPreferences ? captionPreferences->captionDisplayMode() : CaptionUserPreferences::Automatic;
3419 // First, find the track in the group that should be enabled (if any).
3420 Vector<RefPtr<TextTrack>> currentlyEnabledTracks;
3421 RefPtr<TextTrack> trackToEnable;
3422 RefPtr<TextTrack> defaultTrack;
3423 RefPtr<TextTrack> fallbackTrack;
3424 RefPtr<TextTrack> forcedSubitleTrack;
3425 int highestTrackScore = 0;
3426 int highestForcedScore = 0;
3427 for (size_t i = 0; i < group.tracks.size(); ++i) {
3428 RefPtr<TextTrack> textTrack = group.tracks[i];
3430 if (m_processingPreferenceChange && textTrack->mode() == TextTrack::showingKeyword())
3431 currentlyEnabledTracks.append(textTrack);
3433 int trackScore = captionPreferences ? captionPreferences->textTrackSelectionScore(textTrack.get(), this) : 0;
3434 LOG(Media, "HTMLMediaElement::configureTextTrackGroup - '%s' track with language '%s' has score %i", textTrack->kind().string().utf8().data(), textTrack->language().string().utf8().data(), trackScore);
3438 // * If the text track kind is { [subtitles or captions] [descriptions] } and the user has indicated an interest in having a
3439 // track with this text track kind, text track language, and text track label enabled, and there is no
3440 // other text track in the media element's list of text tracks with a text track kind of either subtitles
3441 // or captions whose text track mode is showing
3443 // * If the text track kind is chapters and the text track language is one that the user agent has reason
3444 // to believe is appropriate for the user, and there is no other text track in the media element's list of
3445 // text tracks with a text track kind of chapters whose text track mode is showing
3446 // Let the text track mode be showing.
3447 if (trackScore > highestTrackScore) {
3448 highestTrackScore = trackScore;
3449 trackToEnable = textTrack;
3452 if (!defaultTrack && textTrack->isDefault())
3453 defaultTrack = textTrack;
3454 if (!defaultTrack && !fallbackTrack)
3455 fallbackTrack = textTrack;
3456 if (textTrack->containsOnlyForcedSubtitles() && trackScore > highestForcedScore) {
3457 forcedSubitleTrack = textTrack;
3458 highestForcedScore = trackScore;
3460 } else if (!group.visibleTrack && !defaultTrack && textTrack->isDefault()) {
3461 // * If the track element has a default attribute specified, and there is no other text track in the media
3462 // element's list of text tracks whose text track mode is showing or showing by default
3463 // Let the text track mode be showing by default.
3464 if (group.kind != TrackGroup::CaptionsAndSubtitles || displayMode != CaptionUserPreferences::ForcedOnly)
3465 defaultTrack = textTrack;
3469 if (!trackToEnable && defaultTrack)
3470 trackToEnable = defaultTrack;
3472 // If no track matches the user's preferred language, none was marked as 'default', and there is a forced subtitle track
3473 // in the same language as the language of the primary audio track, enable it.
3474 if (!trackToEnable && forcedSubitleTrack)
3475 trackToEnable = forcedSubitleTrack;
3477 // If no track matches, don't disable an already visible track unless preferences say they all should be off.
3478 if (group.kind != TrackGroup::CaptionsAndSubtitles || displayMode != CaptionUserPreferences::ForcedOnly) {
3479 if (!trackToEnable && !defaultTrack && group.visibleTrack)
3480 trackToEnable = group.visibleTrack;
3483 // If no track matches the user's preferred language and non was marked 'default', enable the first track
3484 // because the user has explicitly stated a preference for this kind of track.
3485 if (!trackToEnable && fallbackTrack)
3486 trackToEnable = fallbackTrack;
3489 m_subtitleTrackLanguage = trackToEnable->language();
3491 m_subtitleTrackLanguage = emptyString();
3493 if (currentlyEnabledTracks.size()) {
3494 for (size_t i = 0; i < currentlyEnabledTracks.size(); ++i) {
3495 RefPtr<TextTrack> textTrack = currentlyEnabledTracks[i];
3496 if (textTrack != trackToEnable)
3497 textTrack->setMode(TextTrack::disabledKeyword());
3502 trackToEnable->setMode(TextTrack::showingKeyword());
3504 m_processingPreferenceChange = false;
3507 void HTMLMediaElement::setSelectedTextTrack(TextTrack* trackToSelect)
3509 TextTrackList* trackList = textTracks();
3510 if (!trackList || !trackList->length())
3513 if (trackToSelect != TextTrack::captionMenuOffItem() && trackToSelect != TextTrack::captionMenuAutomaticItem()) {
3514 if (!trackList->contains(trackToSelect))
3517 for (int i = 0, length = trackList->length(); i < length; ++i) {
3518 TextTrack* track = trackList->item(i);
3519 if (!trackToSelect || track != trackToSelect)
3520 track->setMode(TextTrack::disabledKeyword());
3522 track->setMode(TextTrack::showingKeyword());
3526 CaptionUserPreferences* captionPreferences = document().page() ? document().page()->group().captionPreferences() : 0;
3527 if (!captionPreferences)
3530 CaptionUserPreferences::CaptionDisplayMode displayMode = captionPreferences->captionDisplayMode();
3531 if (trackToSelect == TextTrack::captionMenuOffItem())
3532 displayMode = CaptionUserPreferences::ForcedOnly;
3533 else if (trackToSelect == TextTrack::captionMenuAutomaticItem())
3534 displayMode = CaptionUserPreferences::Automatic;
3536 displayMode = CaptionUserPreferences::AlwaysOn;
3537 if (trackToSelect->language().length())
3538 captionPreferences->setPreferredLanguage(trackToSelect->language());
3540 // Set m_captionDisplayMode here so we don't reconfigure again when the preference changed notification comes through.
3541 m_captionDisplayMode = displayMode;
3544 captionPreferences->setCaptionDisplayMode(displayMode);
3547 void HTMLMediaElement::configureTextTracks()
3549 TrackGroup captionAndSubtitleTracks(TrackGroup::CaptionsAndSubtitles);
3550 TrackGroup descriptionTracks(TrackGroup::Description);
3551 TrackGroup chapterTracks(TrackGroup::Chapter);
3552 TrackGroup metadataTracks(TrackGroup::Metadata);
3553 TrackGroup otherTracks(TrackGroup::Other);
3558 for (size_t i = 0; i < m_textTracks->length(); ++i) {
3559 RefPtr<TextTrack> textTrack = m_textTracks->item(i);
3563 String kind = textTrack->kind();
3564 TrackGroup* currentGroup;
3565 if (kind == TextTrack::subtitlesKeyword() || kind == TextTrack::captionsKeyword() || kind == TextTrack::forcedKeyword())
3566 currentGroup = &captionAndSubtitleTracks;
3567 else if (kind == TextTrack::descriptionsKeyword())
3568 currentGroup = &descriptionTracks;
3569 else if (kind == TextTrack::chaptersKeyword())
3570 currentGroup = &chapterTracks;
3571 else if (kind == TextTrack::metadataKeyword())
3572 currentGroup = &metadataTracks;
3574 currentGroup = &otherTracks;
3576 if (!currentGroup->visibleTrack && textTrack->mode() == TextTrack::showingKeyword())
3577 currentGroup->visibleTrack = textTrack;
3578 if (!currentGroup->defaultTrack && textTrack->isDefault())
3579 currentGroup->defaultTrack = textTrack;
3581 // Do not add this track to the group if it has already been automatically configured
3582 // as we only want to call configureTextTrack once per track so that adding another
3583 // track after the initial configuration doesn't reconfigure every track - only those
3584 // that should be changed by the new addition. For example all metadata tracks are
3585 // disabled by default, and we don't want a track that has been enabled by script
3586 // to be disabled automatically when a new metadata track is added later.
3587 if (textTrack->hasBeenConfigured())
3590 if (textTrack->language().length())
3591 currentGroup->hasSrcLang = true;
3592 currentGroup->tracks.append(textTrack);
3595 if (captionAndSubtitleTracks.tracks.size())
3596 configureTextTrackGroup(captionAndSubtitleTracks);
3597 if (descriptionTracks.tracks.size())
3598 configureTextTrackGroup(descriptionTracks);
3599 if (chapterTracks.tracks.size())
3600 configureTextTrackGroup(chapterTracks);
3601 if (metadataTracks.tracks.size())
3602 configureTextTrackGroup(metadataTracks);
3603 if (otherTracks.tracks.size())
3604 configureTextTrackGroup(otherTracks);
3606 configureTextTrackDisplay();
3607 if (hasMediaControls())
3608 mediaControls()->closedCaptionTracksChanged();
3612 bool HTMLMediaElement::havePotentialSourceChild()
3614 // Stash the current <source> node and next nodes so we can restore them after checking
3615 // to see there is another potential.
3616 RefPtr<HTMLSourceElement> currentSourceNode = m_currentSourceNode;
3617 RefPtr<Node> nextNode = m_nextChildNodeToConsider;
3619 URL nextURL = selectNextSourceChild(0, 0, DoNothing);
3621 m_currentSourceNode = currentSourceNode;
3622 m_nextChildNodeToConsider = nextNode;
3624 return nextURL.isValid();
3627 URL HTMLMediaElement::selectNextSourceChild(ContentType* contentType, String* keySystem, InvalidURLAction actionIfInvalid)
3630 // Don't log if this was just called to find out if there are any valid <source> elements.
3631 bool shouldLog = actionIfInvalid != DoNothing;
3633 LOG(Media, "HTMLMediaElement::selectNextSourceChild");
3636 if (!m_nextChildNodeToConsider) {
3639 LOG(Media, "HTMLMediaElement::selectNextSourceChild -> 0x0000, \"\"");
3645 HTMLSourceElement* source = 0;
3648 bool lookingForStartNode = m_nextChildNodeToConsider;
3649 bool canUseSourceElement = false;
3650 bool okToLoadSourceURL;
3652 NodeVector potentialSourceNodes;
3653 getChildNodes(*this, potentialSourceNodes);
3655 for (unsigned i = 0; !canUseSourceElement && i < potentialSourceNodes.size(); ++i) {
3656 Node& node = potentialSourceNodes[i].get();
3657 if (lookingForStartNode && m_nextChildNodeToConsider != &node)
3659 lookingForStartNode = false;
3661 if (!node.hasTagName(sourceTag))
3663 if (node.parentNode() != this)
3666 source = toHTMLSourceElement(&node);
3668 // If candidate does not have a src attribute, or if its src attribute's value is the empty string ... jump down to the failed step below
3669 mediaURL = source->getNonEmptyURLAttribute(srcAttr);
3672 LOG(Media, "HTMLMediaElement::selectNextSourceChild - 'src' is %s", urlForLoggingMedia(mediaURL).utf8().data());
3674 if (mediaURL.isEmpty())
3677 if (source->fastHasAttribute(mediaAttr)) {
3678 MediaQueryEvaluator screenEval("screen", document().frame(), renderer() ? &renderer()->style() : nullptr);
3679 RefPtr<MediaQuerySet> media = MediaQuerySet::createAllowingDescriptionSyntax(source->media());
3682 LOG(Media, "HTMLMediaElement::selectNextSourceChild - 'media' is %s", source->media().utf8().data());
3684 if (!screenEval.eval(media.get()))
3688 type = source->type();
3689 // FIXME(82965): Add support for keySystem in <source> and set system from source.
3690 if (type.isEmpty() && mediaURL.protocolIsData())
3691 type = mimeTypeFromDataURL(mediaURL);
3692 if (!type.isEmpty() || !system.isEmpty()) {
3695 LOG(Media, "HTMLMediaElement::selectNextSourceChild - 'type' is '%s' - key system is '%s'", type.utf8().data(), system.utf8().data());
3697 MediaEngineSupportParameters parameters;
3698 ContentType contentType(type);
3699 parameters.type = contentType.type().lower();
3700 parameters.codecs = contentType.parameter(ASCIILiteral("codecs"));
3701 parameters.url = mediaURL;
3702 #if ENABLE(ENCRYPTED_MEDIA)
3703 parameters.keySystem = system;
3705 #if ENABLE(MEDIA_SOURCE)
3706 parameters.isMediaSource = mediaURL.protocolIs(mediaSourceBlobProtocol);
3708 if (!MediaPlayer::supportsType(parameters, this))
3712 // Is it safe to load this url?
3713 okToLoadSourceURL = isSafeToLoadURL(mediaURL, actionIfInvalid) && dispatchBeforeLoadEvent(mediaURL.string());
3715 // A 'beforeload' event handler can mutate the DOM, so check to see if the source element is still a child node.
3716 if (node.parentNode() != this) {
3717 LOG(Media, "HTMLMediaElement::selectNextSourceChild : 'beforeload' removed current element");
3722 if (!okToLoadSourceURL)
3725 // Making it this far means the <source> looks reasonable.
3726 canUseSourceElement = true;
3729 if (!canUseSourceElement && actionIfInvalid == Complain && source)
3730 source->scheduleErrorEvent();
3733 if (canUseSourceElement) {
3735 *contentType = ContentType(type);
3737 *keySystem = system;
3738 m_currentSourceNode = source;
3739 m_nextChildNodeToConsider = source->nextSibling();
3741 m_currentSourceNode = 0;
3742 m_nextChildNodeToConsider = 0;
3747 LOG(Media, "HTMLMediaElement::selectNextSourceChild -> %p, %s", m_currentSourceNode.get(), canUseSourceElement ? urlForLoggingMedia(mediaURL).utf8().data() : "");
3749 return canUseSourceElement ? mediaURL : URL();
3752 void HTMLMediaElement::sourceWasAdded(HTMLSourceElement* source)
3754 LOG(Media, "HTMLMediaElement::sourceWasAdded(%p)", source);
3757 if (source->hasTagName(sourceTag)) {
3758 URL url = source->getNonEmptyURLAttribute(srcAttr);
3759 LOG(Media, "HTMLMediaElement::sourceWasAdded - 'src' is %s", urlForLoggingMedia(url).utf8().data());
3763 // We should only consider a <source> element when there is not src attribute at all.
3764 if (fastHasAttribute(srcAttr))
3767 // 4.8.8 - If a source element is inserted as a child of a media element that has no src
3768 // attribute and whose networkState has the value NETWORK_EMPTY, the user agent must invoke
3769 // the media element's resource selection algorithm.
3770 if (networkState() == HTMLMediaElement::NETWORK_EMPTY) {
3771 scheduleDelayedAction(LoadMediaResource);
3772 m_nextChildNodeToConsider = source;
3776 if (m_currentSourceNode && source == m_currentSourceNode->nextSibling()) {
3777 LOG(Media, "HTMLMediaElement::sourceWasAdded - <source> inserted immediately after current source");
3778 m_nextChildNodeToConsider = source;
3782 if (m_nextChildNodeToConsider)
3785 // 4.8.9.5, resource selection algorithm, source elements section:
3786 // 21. Wait until the node after pointer is a node other than the end of the list. (This step might wait forever.)
3787 // 22. Asynchronously await a stable state...
3788 // 23. Set the element's delaying-the-load-event flag back to true (this delays the load event again, in case
3789 // it hasn't been fired yet).
3790 setShouldDelayLoadEvent(true);
3792 // 24. Set the networkState back to NETWORK_LOADING.
3793 m_networkState = NETWORK_LOADING;
3795 // 25. Jump back to the find next candidate step above.
3796 m_nextChildNodeToConsider = source;
3797 scheduleNextSourceChild();
3800 void HTMLMediaElement::sourceWasRemoved(HTMLSourceElement* source)
3802 LOG(Media, "HTMLMediaElement::sourceWasRemoved(%p)", source);
3805 if (source->hasTagName(sourceTag)) {
3806 URL url = source->getNonEmptyURLAttribute(srcAttr);
3807 LOG(Media, "HTMLMediaElement::sourceWasRemoved - 'src' is %s", urlForLoggingMedia(url).utf8().data());
3811 if (source != m_currentSourceNode && source != m_nextChildNodeToConsider)
3814 if (source == m_nextChildNodeToConsider) {
3815 if (m_currentSourceNode)
3816 m_nextChildNodeToConsider = m_currentSourceNode->nextSibling();
3817 LOG(Media, "HTMLMediaElement::sourceRemoved - m_nextChildNodeToConsider set to %p", m_nextChildNodeToConsider.get());
3818 } else if (source == m_currentSourceNode) {
3819 // Clear the current source node pointer, but don't change the movie as the spec says:
3820 // 4.8.8 - Dynamically modifying a source element and its attribute when the element is already
3821 // inserted in a video or audio element will have no effect.
3822 m_currentSourceNode = 0;
3823 LOG(Media, "HTMLMediaElement::sourceRemoved - m_currentSourceNode set to 0");
3827 void HTMLMediaElement::mediaPlayerTimeChanged(MediaPlayer*)
3829 LOG(Media, "HTMLMediaElement::mediaPlayerTimeChanged");
3831 #if ENABLE(VIDEO_TRACK)
3832 if (RuntimeEnabledFeatures::sharedFeatures().webkitVideoTrackEnabled())
3833 updateActiveTextTrackCues(currentTime());
3836 beginProcessingMediaPlayerCallback();
3838 invalidateCachedTime();
3840 // 4.8.10.9 step 14 & 15. Needed if no ReadyState change is associated with the seek.
3841 if (m_seeking && m_readyState >= HAVE_CURRENT_DATA && !m_player->seeking())
3844 // Always call scheduleTimeupdateEvent when the media engine reports a time discontinuity,
3845 // it will only queue a 'timeupdate' event if we haven't already posted one at the current
3847 scheduleTimeupdateEvent(false);
3849 double now = currentTime();
3850 double dur = duration();
3852 // When the current playback position reaches the end of the media resource then the user agent must follow these steps:
3853 if (!std::isnan(dur) && dur) {
3854 // If the media element has a loop attribute specified and does not have a current media controller,
3855 if (loop() && !m_mediaController && m_playbackRate > 0) {
3856 m_sentEndEvent = false;
3857 // then seek to the earliest possible position of the media resource and abort these steps when the direction of
3858 // playback is forwards,
3861 } else if ((now <= 0 && m_playbackRate < 0) || (now >= dur && m_playbackRate > 0)) {
3862 // If the media element does not have a current media controller, and the media element
3863 // has still ended playback and paused is false,
3864 if (!m_mediaController && !m_paused) {
3865 // changes paused to true and fires a simple event named pause at the media element.
3867 scheduleEvent(eventNames().pauseEvent);
3869 // Queue a task to fire a simple event named ended at the media element.
3870 if (!m_sentEndEvent) {
3871 m_sentEndEvent = true;
3872 scheduleEvent(eventNames().endedEvent);
3874 // If the media element has a current media controller, then report the controller state
3875 // for the media element's current media controller.
3876 updateMediaController();
3878 m_sentEndEvent = false;
3881 // The controller changes movie time directly instead of calling through here so we need
3882 // to post timeupdate events in response to time changes.
3883 scheduleTimeupdateEvent(false);
3885 m_sentEndEvent = false;
3889 endProcessingMediaPlayerCallback();
3892 void HTMLMediaElement::mediaPlayerVolumeChanged(MediaPlayer*)
3894 LOG(Media, "HTMLMediaElement::mediaPlayerVolumeChanged");
3896 beginProcessingMediaPlayerCallback();
3898 double vol = m_player->volume();
3899 if (vol != m_volume) {
3902 scheduleEvent(eventNames().volumechangeEvent);
3905 endProcessingMediaPlayerCallback();
3908 void HTMLMediaElement::mediaPlayerMuteChanged(MediaPlayer*)
3910 LOG(Media, "HTMLMediaElement::mediaPlayerMuteChanged");
3912 beginProcessingMediaPlayerCallback();
3914 setMuted(m_player->muted());
3915 endProcessingMediaPlayerCallback();
3918 void HTMLMediaElement::mediaPlayerDurationChanged(MediaPlayer* player)
3920 LOG(Media, "HTMLMediaElement::mediaPlayerDurationChanged");
3922 beginProcessingMediaPlayerCallback();
3924 scheduleEvent(eventNames().durationchangeEvent);
3925 mediaPlayerCharacteristicChanged(player);
3927 double now = currentTime();
3928 double dur = duration();
3932 endProcessingMediaPlayerCallback();
3935 void HTMLMediaElement::mediaPlayerRateChanged(MediaPlayer*)
3937 LOG(Media, "HTMLMediaElement::mediaPlayerRateChanged");
3939 beginProcessingMediaPlayerCallback();
3941 // Stash the rate in case the one we tried to set isn't what the engine is
3942 // using (eg. it can't handle the rate we set)
3943 m_playbackRate = m_player->rate();
3945 invalidateCachedTime();
3947 updateSleepDisabling();
3949 endProcessingMediaPlayerCallback();
3952 void HTMLMediaElement::mediaPlayerPlaybackStateChanged(MediaPlayer*)
3954 LOG(Media, "HTMLMediaElement::mediaPlayerPlaybackStateChanged");
3956 if (!m_player || m_pausedInternal)
3959 beginProcessingMediaPlayerCallback();
3960 if (m_player->paused())
3964 endProcessingMediaPlayerCallback();
3967 void HTMLMediaElement::mediaPlayerSawUnsupportedTracks(MediaPlayer*)
3969 LOG(Media, "HTMLMediaElement::mediaPlayerSawUnsupportedTracks");
3971 // The MediaPlayer came across content it cannot completely handle.
3972 // This is normally acceptable except when we are in a standalone
3973 // MediaDocument. If so, tell the document what has happened.
3974 if (document().isMediaDocument())
3975 toMediaDocument(document()).mediaElementSawUnsupportedTracks();
3978 void HTMLMediaElement::mediaPlayerResourceNotSupported(MediaPlayer*)
3980 LOG(Media, "HTMLMediaElement::mediaPlayerResourceNotSupported");
3982 // The MediaPlayer came across content which no installed engine supports.
3983 mediaLoadingFailed(MediaPlayer::FormatError);
3986 // MediaPlayerPresentation methods
3987 void HTMLMediaElement::mediaPlayerRepaint(MediaPlayer*)
3989 beginProcessingMediaPlayerCallback();
3990 updateDisplayState();
3992 renderer()->repaint();
3993 endProcessingMediaPlayerCallback();
3996 void HTMLMediaElement::mediaPlayerSizeChanged(MediaPlayer*)
3998 LOG(Media, "HTMLMediaElement::mediaPlayerSizeChanged");
4000 beginProcessingMediaPlayerCallback();
4002 renderer()->updateFromElement();
4003 endProcessingMediaPlayerCallback();
4006 #if USE(ACCELERATED_COMPOSITING)
4007 bool HTMLMediaElement::mediaPlayerRenderingCanBeAccelerated(MediaPlayer*)
4009 if (renderer() && renderer()->isVideo())
4010 return renderer()->view().compositor().canAccelerateVideoRendering(toRenderVideo(*renderer()));
4014 void HTMLMediaElement::mediaPlayerRenderingModeChanged(MediaPlayer*)
4016 LOG(Media, "HTMLMediaElement::mediaPlayerRenderingModeChanged");
4018 // Kick off a fake recalcStyle that will update the compositing tree.
4019 setNeedsStyleRecalc(SyntheticStyleChange);
4023 #if PLATFORM(WIN) && USE(AVFOUNDATION)
4024 GraphicsDeviceAdapter* HTMLMediaElement::mediaPlayerGraphicsDeviceAdapter(const MediaPlayer*) const
4026 if (!document().page())
4029 return document().page()->chrome().client().graphicsDeviceAdapter();
4033 void HTMLMediaElement::mediaPlayerEngineUpdated(MediaPlayer*)
4035 LOG(Media, "HTMLMediaElement::mediaPlayerEngineUpdated");
4036 beginProcessingMediaPlayerCallback();
4038 renderer()->updateFromElement();
4039 endProcessingMediaPlayerCallback();
4041 #if ENABLE(MEDIA_SOURCE)
4042 m_droppedVideoFrames = 0;
4046 void HTMLMediaElement::mediaPlayerFirstVideoFrameAvailable(MediaPlayer*)
4048 LOG(Media, "HTMLMediaElement::mediaPlayerFirstVideoFrameAvailable");
4049 beginProcessingMediaPlayerCallback();
4050 if (displayMode() == PosterWaitingForVideo) {
4051 setDisplayMode(Video);
4052 #if USE(ACCELERATED_COMPOSITING)
4053 mediaPlayerRenderingModeChanged(m_player.get());
4056 endProcessingMediaPlayerCallback();
4059 void HTMLMediaElement::mediaPlayerCharacteristicChanged(MediaPlayer*)
4061 LOG(Media, "HTMLMediaElement::mediaPlayerCharacteristicChanged");
4063 beginProcessingMediaPlayerCallback();
4065 #if ENABLE(VIDEO_TRACK)
4066 if (m_captionDisplayMode == CaptionUserPreferences::Automatic && m_subtitleTrackLanguage != m_player->languageOfPrimaryAudioTrack())
4067 markCaptionAndSubtitleTracksAsUnconfigured(AfterDelay);
4070 if (hasMediaControls())
4071 mediaControls()->reset();
4073 renderer()->updateFromElement();
4074 endProcessingMediaPlayerCallback();
4077 PassRefPtr<TimeRanges> HTMLMediaElement::buffered() const
4080 return TimeRanges::create();
4082 #if ENABLE(MEDIA_SOURCE)
4084 return m_mediaSource->buffered();
4087 return m_player->buffered();
4090 PassRefPtr<TimeRanges> HTMLMediaElement::played()
4093 double time = currentTime();
4094 if (time > m_lastSeekTime)
4095 addPlayedRange(m_lastSeekTime, time);
4098 if (!m_playedTimeRanges)
4099 m_playedTimeRanges = TimeRanges::create();