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

Unified Diff: Source/core/html/HTMLMediaElement.cpp

Issue 1179223002: Implement autoplay gesture override experiment. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Refactored into AutoplayExperimentHelper. Created 5 years, 4 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 side-by-side diff with in-line comments
Download patch
Index: Source/core/html/HTMLMediaElement.cpp
diff --git a/Source/core/html/HTMLMediaElement.cpp b/Source/core/html/HTMLMediaElement.cpp
index 63eda2639e62a01ca9a5fd97b8bdfc5a2bd153f8..9360e955c7a83c5df89de8c3fcf6baf1d3586da3 100644
--- a/Source/core/html/HTMLMediaElement.cpp
+++ b/Source/core/html/HTMLMediaElement.cpp
@@ -251,25 +251,7 @@ static bool canLoadURL(const KURL& url, const ContentType& contentType, const St
return false;
}
-// These values are used for a histogram. Do not reorder.
-enum AutoplayMetrics {
- // Media element with autoplay seen.
- AutoplayMediaFound = 0,
- // Autoplay enabled and user stopped media play at any point.
- AutoplayStopped = 1,
- // Autoplay enabled but user bailed out on media play early.
- AutoplayBailout = 2,
- // Autoplay disabled but user manually started media.
- AutoplayManualStart = 3,
- // Autoplay was (re)enabled through a user-gesture triggered load()
- AutoplayEnabledThroughLoad = 4,
- // Autoplay disabled by sandbox flags.
- AutoplayDisabledBySandbox = 5,
- // This enum value must be last.
- NumberOfAutoplayMetrics,
-};
-
-static void recordAutoplayMetric(AutoplayMetrics metric)
+void HTMLMediaElement::recordAutoplayMetric(AutoplayMetrics metric)
{
Platform::current()->histogramEnumeration("Blink.MediaElement.Autoplay", metric, NumberOfAutoplayMetrics);
}
@@ -367,6 +349,7 @@ HTMLMediaElement::HTMLMediaElement(const QualifiedName& tagName, Document& docum
#if ENABLE(WEB_AUDIO)
, m_audioSourceNode(nullptr)
#endif
+ , m_autoplayHelper(*this)
{
#if ENABLE(OILPAN)
ThreadState::current()->registerPreFinalizer(this);
@@ -385,6 +368,7 @@ HTMLMediaElement::HTMLMediaElement(const QualifiedName& tagName, Document& docum
HTMLMediaElement::~HTMLMediaElement()
{
WTF_LOG(Media, "HTMLMediaElement::~HTMLMediaElement(%p)", this);
+
#if !ENABLE(OILPAN)
// HTMLMediaElement and m_asyncEventQueue always become unreachable
// together. So HTMLMediaElement and m_asyncEventQueue are destructed in
@@ -694,12 +678,37 @@ String HTMLMediaElement::canPlayType(const String& mimeType, const String& keySy
return canPlay;
}
+void HTMLMediaElement::recordMetricsIfStopping()
philipj_slow 2015/08/13 11:12:48 Since this AutoplayExperimentHelper is a friend cl
liberato (no reviews please) 2015/09/01 06:54:20 they could, but they were part of the old autoplay
+{
+ // If not playing, then nothing to record.
+ if (!m_playing)
+ return;
+
+ const bool bailout = isBailout();
+
+ // Record that play was stopped. We don't care if it was autoplay,
+ // play(), or the user manually started it.
+ recordAutoplayMetric(AnyPlaybackStopped);
+ if (bailout)
+ recordAutoplayMetric(AnyPlaybackBailout);
+
+ // If this was a gestureless play, then record that separately.
+ // These cover attr and play() gestureless starts.
+ if (m_initialPlayWithoutUserGestures) {
+ m_initialPlayWithoutUserGestures = false;
+
+ recordAutoplayMetric(AutoplayStopped);
+
+ if (bailout)
+ recordAutoplayMetric(AutoplayBailout);
+ }
+}
+
void HTMLMediaElement::load()
{
WTF_LOG(Media, "HTMLMediaElement::load(%p)", this);
- if (m_initialPlayWithoutUserGestures && m_playing)
- gesturelessInitialPlayHalted();
+ recordMetricsIfStopping();
if (UserGestureIndicator::processingUserGesture() && m_userGestureRequiredForPlay) {
recordAutoplayMetric(AutoplayEnabledThroughLoad);
@@ -1555,11 +1564,17 @@ void HTMLMediaElement::setReadyState(ReadyState state)
if (document().isSandboxed(SandboxAutomaticFeatures)) {
recordAutoplayMetric(AutoplayDisabledBySandbox);
- } else if (!m_userGestureRequiredForPlay) {
- m_paused = false;
- invalidateCachedTime();
- scheduleEvent(EventTypeNames::play);
- scheduleEvent(EventTypeNames::playing);
+ } else {
+ // If the autoplay experiment says that it's okay to play now,
+ // then don't require a user gesture.
+ m_autoplayHelper.onReadyToPlay();
+
+ if (!m_userGestureRequiredForPlay) {
+ m_paused = false;
+ invalidateCachedTime();
+ scheduleEvent(EventTypeNames::play);
+ scheduleEvent(EventTypeNames::playing);
+ }
}
}
@@ -1960,9 +1975,13 @@ void HTMLMediaElement::play()
{
WTF_LOG(Media, "HTMLMediaElement::play(%p)", this);
+ m_autoplayHelper.onPlayMethodCalled();
+
if (!UserGestureIndicator::processingUserGesture()) {
autoplayMediaEncountered();
+
if (m_userGestureRequiredForPlay) {
+ recordAutoplayMetric(PlayMethodFailed);
philipj_slow 2015/08/13 11:12:48 If recordAutoplayMetric is also moved to the helpe
liberato (no reviews please) 2015/09/01 06:54:20 Acknowledged.
String message = ExceptionMessages::failedToExecute("play", "HTMLMediaElement", "API can only be initiated by a user gesture.");
document().executionContext()->addConsoleMessage(ConsoleMessage::create(JSMessageSource, WarningMessageLevel, message));
return;
@@ -2020,21 +2039,13 @@ void HTMLMediaElement::autoplayMediaEncountered()
}
}
-void HTMLMediaElement::gesturelessInitialPlayHalted()
+bool HTMLMediaElement::isBailout() const
{
- ASSERT(m_initialPlayWithoutUserGestures);
- m_initialPlayWithoutUserGestures = false;
-
- recordAutoplayMetric(AutoplayStopped);
-
// We count the user as having bailed-out on the video if they watched
// less than one minute and less than 50% of it.
- double playedTime = currentTime();
- if (playedTime < 60) {
- double progress = playedTime / duration();
- if (progress < 0.5)
- recordAutoplayMetric(AutoplayBailout);
- }
+ const double playedTime = currentTime();
+ const double progress = playedTime / duration();
+ return (playedTime < 60) && (progress < 0.5);
}
void HTMLMediaElement::pause()
@@ -2044,11 +2055,12 @@ void HTMLMediaElement::pause()
if (m_networkState == NETWORK_EMPTY)
scheduleDelayedAction(LoadMediaResource);
+ m_autoplayHelper.onPauseMethodCalled();
+
m_autoplaying = false;
if (!m_paused) {
- if (m_initialPlayWithoutUserGestures)
- gesturelessInitialPlayHalted();
+ recordMetricsIfStopping();
m_paused = true;
scheduleTimeupdateEvent(false);
@@ -2141,6 +2153,8 @@ void HTMLMediaElement::setMuted(bool muted)
m_muted = muted;
+ m_autoplayHelper.onMuteChanged();
philipj_slow 2015/08/13 11:12:48 s/Mute/Muted/
liberato (no reviews please) 2015/09/01 06:54:20 Done.
+
updateVolume();
scheduleEvent(EventTypeNames::volumechange);
@@ -2772,6 +2786,7 @@ void HTMLMediaElement::timeChanged()
m_sentEndEvent = true;
scheduleEvent(EventTypeNames::ended);
}
+ recordMetricsIfStopping();
// If the media element has a current media controller, then report the controller state
// for the media element's current media controller.
updateMediaController();
@@ -2992,6 +3007,7 @@ void HTMLMediaElement::updatePlayState()
mediaControls()->playbackStarted();
startPlaybackProgressTimer();
m_playing = true;
+ recordAutoplayMetric(AnyPlaybackStarted);
} else { // Should not be playing right now
if (isPlaying)
@@ -3111,8 +3127,7 @@ void HTMLMediaElement::stop()
{
WTF_LOG(Media, "HTMLMediaElement::stop(%p)", this);
- if (m_playing && m_initialPlayWithoutUserGestures)
- gesturelessInitialPlayHalted();
+ recordMetricsIfStopping();
philipj_slow 2015/08/13 11:12:48 HTMLMediaElement::stop() is an ActiveDOMObject ove
philipj_slow 2015/09/02 09:31:47 Ping.
// Close the async event queue so that no events are enqueued by userCancelledLoad.
cancelPendingEventsAndCallbacks();
@@ -3721,6 +3736,21 @@ void HTMLMediaElement::selectInitialTracksIfNecessary()
videoTracks().anonymousIndexedGetter(0)->setSelected(true);
}
+bool HTMLMediaElement::isUserGestureRequiredForPlay() const
+{
+ return m_userGestureRequiredForPlay;
+}
+
+void HTMLMediaElement::removeUserGestureRequirement()
+{
+ m_userGestureRequiredForPlay = false;
+}
+
+void HTMLMediaElement::setInitialPlayWithoutUserGestures(bool value)
+{
+ m_initialPlayWithoutUserGestures = value;
philipj_slow 2015/08/13 11:12:48 You didn't add m_initialPlayWithoutUserGestures, b
liberato (no reviews please) 2015/09/01 06:54:20 Done.
+}
+
#if ENABLE(WEB_AUDIO)
void HTMLMediaElement::clearWeakMembers(Visitor* visitor)
{

Powered by Google App Engine
This is Rietveld 408576698