Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(553)

Side by Side Diff: third_party/WebKit/Source/core/html/HTMLMediaElement.cpp

Issue 2051253002: Start autoplay muted videos with autoplay attribute when they are visible. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: reduce Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/WebKit/Source/core/html/HTMLMediaElement.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012, 2013 Apple Inc. All rights reserved. 2 * Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012, 2013 Apple Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
(...skipping 17 matching lines...) Expand all
28 #include "bindings/core/v8/ExceptionState.h" 28 #include "bindings/core/v8/ExceptionState.h"
29 #include "bindings/core/v8/ExceptionStatePlaceholder.h" 29 #include "bindings/core/v8/ExceptionStatePlaceholder.h"
30 #include "bindings/core/v8/ScriptController.h" 30 #include "bindings/core/v8/ScriptController.h"
31 #include "bindings/core/v8/ScriptEventListener.h" 31 #include "bindings/core/v8/ScriptEventListener.h"
32 #include "bindings/core/v8/ScriptPromiseResolver.h" 32 #include "bindings/core/v8/ScriptPromiseResolver.h"
33 #include "core/HTMLNames.h" 33 #include "core/HTMLNames.h"
34 #include "core/css/MediaList.h" 34 #include "core/css/MediaList.h"
35 #include "core/dom/Attribute.h" 35 #include "core/dom/Attribute.h"
36 #include "core/dom/DOMException.h" 36 #include "core/dom/DOMException.h"
37 #include "core/dom/ElementTraversal.h" 37 #include "core/dom/ElementTraversal.h"
38 #include "core/dom/ElementVisibilityObserver.h"
38 #include "core/dom/Fullscreen.h" 39 #include "core/dom/Fullscreen.h"
39 #include "core/dom/shadow/ShadowRoot.h" 40 #include "core/dom/shadow/ShadowRoot.h"
40 #include "core/events/Event.h" 41 #include "core/events/Event.h"
41 #include "core/frame/FrameView.h" 42 #include "core/frame/FrameView.h"
42 #include "core/frame/LocalFrame.h" 43 #include "core/frame/LocalFrame.h"
43 #include "core/frame/Settings.h" 44 #include "core/frame/Settings.h"
44 #include "core/frame/UseCounter.h" 45 #include "core/frame/UseCounter.h"
45 #include "core/frame/csp/ContentSecurityPolicy.h" 46 #include "core/frame/csp/ContentSecurityPolicy.h"
46 #include "core/html/HTMLMediaSource.h" 47 #include "core/html/HTMLMediaSource.h"
47 #include "core/html/HTMLSourceElement.h" 48 #include "core/html/HTMLSourceElement.h"
(...skipping 299 matching lines...) Expand 10 before | Expand all | Expand 10 after
347 visitor->trace(m_element); 348 visitor->trace(m_element);
348 Client::trace(visitor); 349 Client::trace(visitor);
349 } 350 }
350 351
351 private: 352 private:
352 AutoplayHelperClientImpl(HTMLMediaElement* element) : m_element(element) {} 353 AutoplayHelperClientImpl(HTMLMediaElement* element) : m_element(element) {}
353 354
354 Member<HTMLMediaElement> m_element; 355 Member<HTMLMediaElement> m_element;
355 }; 356 };
356 357
357
358 void HTMLMediaElement::recordAutoplayMetric(AutoplayMetrics metric) 358 void HTMLMediaElement::recordAutoplayMetric(AutoplayMetrics metric)
359 { 359 {
360 DEFINE_STATIC_LOCAL(EnumerationHistogram, autoplayHistogram, ("Blink.MediaEl ement.Autoplay", NumberOfAutoplayMetrics)); 360 DEFINE_STATIC_LOCAL(EnumerationHistogram, autoplayHistogram, ("Blink.MediaEl ement.Autoplay", NumberOfAutoplayMetrics));
361 autoplayHistogram.count(metric); 361 autoplayHistogram.count(metric);
362 } 362 }
363 363
364 WebMimeRegistry::SupportsType HTMLMediaElement::supportsType(const ContentType& contentType) 364 WebMimeRegistry::SupportsType HTMLMediaElement::supportsType(const ContentType& contentType)
365 { 365 {
366 DEFINE_STATIC_LOCAL(const String, codecs, ("codecs")); 366 DEFINE_STATIC_LOCAL(const String, codecs, ("codecs"));
367 367
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
443 , m_inOverlayFullscreenVideo(false) 443 , m_inOverlayFullscreenVideo(false)
444 , m_audioTracks(AudioTrackList::create(*this)) 444 , m_audioTracks(AudioTrackList::create(*this))
445 , m_videoTracks(VideoTrackList::create(*this)) 445 , m_videoTracks(VideoTrackList::create(*this))
446 , m_textTracks(nullptr) 446 , m_textTracks(nullptr)
447 , m_playPromiseResolveTask(CancellableTaskFactory::create(this, &HTMLMediaEl ement::resolveScheduledPlayPromises)) 447 , m_playPromiseResolveTask(CancellableTaskFactory::create(this, &HTMLMediaEl ement::resolveScheduledPlayPromises))
448 , m_playPromiseRejectTask(CancellableTaskFactory::create(this, &HTMLMediaEle ment::rejectScheduledPlayPromises)) 448 , m_playPromiseRejectTask(CancellableTaskFactory::create(this, &HTMLMediaEle ment::rejectScheduledPlayPromises))
449 , m_audioSourceNode(nullptr) 449 , m_audioSourceNode(nullptr)
450 , m_autoplayHelperClient(AutoplayHelperClientImpl::create(this)) 450 , m_autoplayHelperClient(AutoplayHelperClientImpl::create(this))
451 , m_autoplayHelper(AutoplayExperimentHelper::create(m_autoplayHelperClient.g et())) 451 , m_autoplayHelper(AutoplayExperimentHelper::create(m_autoplayHelperClient.g et()))
452 , m_remotePlaybackClient(nullptr) 452 , m_remotePlaybackClient(nullptr)
453 , m_autoplayVisibilityObserver(nullptr)
453 { 454 {
454 ThreadState::current()->registerPreFinalizer(this); 455 ThreadState::current()->registerPreFinalizer(this);
455 456
456 DVLOG(MEDIA_LOG_LEVEL) << "HTMLMediaElement(" << (void*)this << ")"; 457 DVLOG(MEDIA_LOG_LEVEL) << "HTMLMediaElement(" << (void*)this << ")";
457 458
458 // If any experiment is enabled, then we want to enable a user gesture by 459 // If any experiment is enabled, then we want to enable a user gesture by
459 // default, otherwise the experiment does nothing. 460 // default, otherwise the experiment does nothing.
460 if ((document.settings() && document.settings()->mediaPlaybackRequiresUserGe sture()) 461 if ((document.settings() && document.settings()->mediaPlaybackRequiresUserGe sture())
461 || m_autoplayHelper->isExperimentEnabled()) { 462 || m_autoplayHelper->isExperimentEnabled()) {
462 m_lockedPendingUserGesture = true; 463 m_lockedPendingUserGesture = true;
(...skipping 1171 matching lines...) Expand 10 before | Expand all | Expand 10 after
1634 if (m_readyState == HAVE_ENOUGH_DATA && oldState < HAVE_ENOUGH_DATA && track sAreReady) { 1635 if (m_readyState == HAVE_ENOUGH_DATA && oldState < HAVE_ENOUGH_DATA && track sAreReady) {
1635 if (oldState <= HAVE_CURRENT_DATA) { 1636 if (oldState <= HAVE_CURRENT_DATA) {
1636 scheduleEvent(EventTypeNames::canplay); 1637 scheduleEvent(EventTypeNames::canplay);
1637 if (isPotentiallyPlaying) 1638 if (isPotentiallyPlaying)
1638 scheduleNotifyPlaying(); 1639 scheduleNotifyPlaying();
1639 } 1640 }
1640 1641
1641 // Check for autoplay, and record metrics about it if needed. 1642 // Check for autoplay, and record metrics about it if needed.
1642 if (shouldAutoplay(RecordMetricsBehavior::DoRecord)) { 1643 if (shouldAutoplay(RecordMetricsBehavior::DoRecord)) {
1643 recordAutoplaySourceMetric(AutoplaySourceAttribute); 1644 recordAutoplaySourceMetric(AutoplaySourceAttribute);
1645
1644 // If the autoplay experiment says that it's okay to play now, 1646 // If the autoplay experiment says that it's okay to play now,
1645 // then don't require a user gesture. 1647 // then don't require a user gesture.
1646 m_autoplayHelper->becameReadyToPlay(); 1648 m_autoplayHelper->becameReadyToPlay();
1647 1649
1648 if (!isGestureNeededForPlayback()) { 1650 if (!isGestureNeededForPlayback()) {
1649 m_paused = false; 1651 if (isHTMLVideoElement() && muted() && RuntimeEnabledFeatures::a utoplayMutedVideosEnabled()) {
1650 invalidateCachedTime(); 1652 m_autoplayVisibilityObserver = new ElementVisibilityObserver (this, WTF::bind<bool>(&HTMLMediaElement::onVisibilityChangedForAutoplay, this)) ;
1651 scheduleEvent(EventTypeNames::play); 1653 m_autoplayVisibilityObserver->start();
1652 scheduleNotifyPlaying(); 1654 } else {
1653 m_autoplaying = false; 1655 m_paused = false;
1656 invalidateCachedTime();
1657 scheduleEvent(EventTypeNames::play);
1658 scheduleNotifyPlaying();
1659 m_autoplaying = false;
1660 }
1654 } 1661 }
1655 } 1662 }
1656 1663
1657 scheduleEvent(EventTypeNames::canplaythrough); 1664 scheduleEvent(EventTypeNames::canplaythrough);
1658 1665
1659 shouldUpdateDisplayState = true; 1666 shouldUpdateDisplayState = true;
1660 } 1667 }
1661 1668
1662 if (shouldUpdateDisplayState) { 1669 if (shouldUpdateDisplayState) {
1663 updateDisplayState(); 1670 updateDisplayState();
(...skipping 1978 matching lines...) Expand 10 before | Expand all | Expand 10 after
3642 visitor->trace(m_cueTimeline); 3649 visitor->trace(m_cueTimeline);
3643 visitor->trace(m_textTracks); 3650 visitor->trace(m_textTracks);
3644 visitor->trace(m_textTracksWhenResourceSelectionBegan); 3651 visitor->trace(m_textTracksWhenResourceSelectionBegan);
3645 visitor->trace(m_playPromiseResolvers); 3652 visitor->trace(m_playPromiseResolvers);
3646 visitor->trace(m_playPromiseResolveList); 3653 visitor->trace(m_playPromiseResolveList);
3647 visitor->trace(m_playPromiseRejectList); 3654 visitor->trace(m_playPromiseRejectList);
3648 visitor->trace(m_audioSourceProvider); 3655 visitor->trace(m_audioSourceProvider);
3649 visitor->trace(m_autoplayHelperClient); 3656 visitor->trace(m_autoplayHelperClient);
3650 visitor->trace(m_autoplayHelper); 3657 visitor->trace(m_autoplayHelper);
3651 visitor->trace(m_srcObject); 3658 visitor->trace(m_srcObject);
3659 visitor->trace(m_autoplayVisibilityObserver);
3652 visitor->template registerWeakMembers<HTMLMediaElement, &HTMLMediaElement::c learWeakMembers>(this); 3660 visitor->template registerWeakMembers<HTMLMediaElement, &HTMLMediaElement::c learWeakMembers>(this);
3653 Supplementable<HTMLMediaElement>::trace(visitor); 3661 Supplementable<HTMLMediaElement>::trace(visitor);
3654 HTMLElement::trace(visitor); 3662 HTMLElement::trace(visitor);
3655 ActiveDOMObject::trace(visitor); 3663 ActiveDOMObject::trace(visitor);
3656 } 3664 }
3657 3665
3658 DEFINE_TRACE_WRAPPERS(HTMLMediaElement) 3666 DEFINE_TRACE_WRAPPERS(HTMLMediaElement)
3659 { 3667 {
3660 visitor->traceWrappers(m_videoTracks); 3668 visitor->traceWrappers(m_videoTracks);
3661 visitor->traceWrappers(m_audioTracks); 3669 visitor->traceWrappers(m_audioTracks);
(...skipping 210 matching lines...) Expand 10 before | Expand all | Expand 10 after
3872 3880
3873 if (isHTMLVideoElement()) { 3881 if (isHTMLVideoElement()) {
3874 videoHistogram.count(source); 3882 videoHistogram.count(source);
3875 if (muted()) 3883 if (muted())
3876 mutedVideoHistogram.count(source); 3884 mutedVideoHistogram.count(source);
3877 } else { 3885 } else {
3878 audioHistogram.count(source); 3886 audioHistogram.count(source);
3879 } 3887 }
3880 } 3888 }
3881 3889
3890 void HTMLMediaElement::onVisibilityChangedForAutoplay(bool isVisible)
3891 {
3892 if (!isVisible)
3893 return;
3894
3895 if (shouldAutoplay()) {
3896 m_paused = false;
3897 invalidateCachedTime();
3898 scheduleEvent(EventTypeNames::play);
3899 scheduleNotifyPlaying();
3900 m_autoplaying = false;
3901
3902 updatePlayState();
3903 }
3904
3905 m_autoplayVisibilityObserver->stop();
3906 m_autoplayVisibilityObserver = nullptr;
3907 }
3908
3882 void HTMLMediaElement::clearWeakMembers(Visitor* visitor) 3909 void HTMLMediaElement::clearWeakMembers(Visitor* visitor)
3883 { 3910 {
3884 if (!ThreadHeap::isHeapObjectAlive(m_audioSourceNode)) { 3911 if (!ThreadHeap::isHeapObjectAlive(m_audioSourceNode)) {
3885 getAudioSourceProvider().setClient(nullptr); 3912 getAudioSourceProvider().setClient(nullptr);
3886 m_audioSourceNode = nullptr; 3913 m_audioSourceNode = nullptr;
3887 } 3914 }
3888 } 3915 }
3889 3916
3890 void HTMLMediaElement::AudioSourceProviderImpl::wrap(WebAudioSourceProvider* pro vider) 3917 void HTMLMediaElement::AudioSourceProviderImpl::wrap(WebAudioSourceProvider* pro vider)
3891 { 3918 {
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
3985 4012
3986 IntRect HTMLMediaElement::AutoplayHelperClientImpl::absoluteBoundingBoxRect() co nst 4013 IntRect HTMLMediaElement::AutoplayHelperClientImpl::absoluteBoundingBoxRect() co nst
3987 { 4014 {
3988 IntRect result; 4015 IntRect result;
3989 if (LayoutObject* object = m_element->layoutObject()) 4016 if (LayoutObject* object = m_element->layoutObject())
3990 result = object->absoluteBoundingBoxRect(); 4017 result = object->absoluteBoundingBoxRect();
3991 return result; 4018 return result;
3992 } 4019 }
3993 4020
3994 } // namespace blink 4021 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/html/HTMLMediaElement.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698