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

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

Issue 2101613002: Record the offscreen playing duration of autoplaying muted videos (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: fixed comments Created 4 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 unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "core/html/AutoplayUmaHelper.h" 5 #include "core/html/AutoplayUmaHelper.h"
6 6
7 #include "core/dom/Document.h" 7 #include "core/dom/Document.h"
8 #include "core/dom/ElementVisibilityObserver.h" 8 #include "core/dom/ElementVisibilityObserver.h"
9 #include "core/events/Event.h" 9 #include "core/events/Event.h"
10 #include "core/frame/LocalDOMWindow.h" 10 #include "core/frame/LocalDOMWindow.h"
11 #include "core/html/HTMLMediaElement.h" 11 #include "core/html/HTMLMediaElement.h"
12 #include "platform/Histogram.h" 12 #include "platform/Histogram.h"
13 #include "wtf/CurrentTime.h"
13 14
14 namespace blink { 15 namespace blink {
15 16
16 namespace { 17 namespace {
17 18
18 void recordVideoAutoplayMutedPlayMethodBecomesVisibleUma(bool visible) 19 static const double maxUmaDurationMS = 1e6;
19 { 20 static const double umaBucketCount = 20;
20 DEFINE_STATIC_LOCAL(BooleanHistogram, histogram, ("Media.Video.Autoplay.Mute d.PlayMethod.BecomesVisible"));
21 histogram.count(visible);
22 }
23 21
24 } // namespace 22 } // namespace
25 23
26 AutoplayUmaHelper* AutoplayUmaHelper::create(HTMLMediaElement* element) 24 AutoplayUmaHelper* AutoplayUmaHelper::create(HTMLMediaElement* element)
27 { 25 {
28 return new AutoplayUmaHelper(element); 26 return new AutoplayUmaHelper(element);
29 } 27 }
30 28
31 AutoplayUmaHelper::AutoplayUmaHelper(HTMLMediaElement* element) 29 AutoplayUmaHelper::AutoplayUmaHelper(HTMLMediaElement* element)
32 : EventListener(CPPEventListenerType) 30 : EventListener(CPPEventListenerType)
33 , m_source(AutoplaySource::NumberOfSources) 31 , m_source(AutoplaySource::NumberOfSources)
34 , m_element(element) 32 , m_element(element)
35 , m_videoMutedPlayMethodVisibilityObserver(nullptr) { } 33 , m_mutedVideoPlayMethodVisibilityObserver(nullptr)
34 , m_mutedVideoAutoplayStartTimeMS(0)
35 , m_mutedVideoAutoplayOffscreenDurationMS(0)
36 , m_isVisible(false)
37 , m_mutedVideoOffscreenDurationVisibilityObserver(nullptr) { }
36 38
37 AutoplayUmaHelper::~AutoplayUmaHelper() = default; 39 AutoplayUmaHelper::~AutoplayUmaHelper() = default;
38 40
39 bool AutoplayUmaHelper::operator==(const EventListener& other) const 41 bool AutoplayUmaHelper::operator==(const EventListener& other) const
40 { 42 {
41 return this == &other; 43 return this == &other;
42 } 44 }
43 45
44 void AutoplayUmaHelper::onAutoplayInitiated(AutoplaySource source) 46 void AutoplayUmaHelper::onAutoplayInitiated(AutoplaySource source)
45 { 47 {
46 DEFINE_STATIC_LOCAL(EnumerationHistogram, videoHistogram, ("Media.Video.Auto play", static_cast<int>(AutoplaySource::NumberOfSources))); 48 DEFINE_STATIC_LOCAL(EnumerationHistogram, videoHistogram, ("Media.Video.Auto play", static_cast<int>(AutoplaySource::NumberOfSources)));
47 DEFINE_STATIC_LOCAL(EnumerationHistogram, mutedVideoHistogram, ("Media.Video .Autoplay.Muted", static_cast<int>(AutoplaySource::NumberOfSources))); 49 DEFINE_STATIC_LOCAL(EnumerationHistogram, mutedVideoHistogram, ("Media.Video .Autoplay.Muted", static_cast<int>(AutoplaySource::NumberOfSources)));
48 DEFINE_STATIC_LOCAL(EnumerationHistogram, audioHistogram, ("Media.Audio.Auto play", static_cast<int>(AutoplaySource::NumberOfSources))); 50 DEFINE_STATIC_LOCAL(EnumerationHistogram, audioHistogram, ("Media.Audio.Auto play", static_cast<int>(AutoplaySource::NumberOfSources)));
49 51
52 // Autoplay already initiated
53 // TODO(zqzhang): how about having autoplay attribute and calling `play()` i n the script?
54 if (m_source != AutoplaySource::NumberOfSources)
55 return;
56
50 m_source = source; 57 m_source = source;
51 58
52 if (m_element->isHTMLVideoElement()) { 59 if (m_element->isHTMLVideoElement()) {
53 videoHistogram.count(static_cast<int>(m_source)); 60 videoHistogram.count(static_cast<int>(m_source));
54 if (m_element->muted()) 61 if (m_element->muted())
55 mutedVideoHistogram.count(static_cast<int>(m_source)); 62 mutedVideoHistogram.count(static_cast<int>(m_source));
56 } else { 63 } else {
57 audioHistogram.count(static_cast<int>(m_source)); 64 audioHistogram.count(static_cast<int>(m_source));
58 } 65 }
59 66
60 if (m_source == AutoplaySource::Method && m_element->isHTMLVideoElement() && m_element->muted()) 67 m_element->addEventListener(EventTypeNames::playing, this, false);
61 m_element->addEventListener(EventTypeNames::playing, this, false);
62 } 68 }
63 69
64 void AutoplayUmaHelper::recordAutoplayUnmuteStatus(AutoplayUnmuteActionStatus st atus) 70 void AutoplayUmaHelper::recordAutoplayUnmuteStatus(AutoplayUnmuteActionStatus st atus)
65 { 71 {
66 DEFINE_STATIC_LOCAL(EnumerationHistogram, autoplayUnmuteHistogram, ("Media.V ideo.Autoplay.Muted.UnmuteAction", static_cast<int>(AutoplayUnmuteActionStatus:: NumberOfStatus))); 72 DEFINE_STATIC_LOCAL(EnumerationHistogram, autoplayUnmuteHistogram, ("Media.V ideo.Autoplay.Muted.UnmuteAction", static_cast<int>(AutoplayUnmuteActionStatus:: NumberOfStatus)));
67 73
68 autoplayUnmuteHistogram.count(static_cast<int>(status)); 74 autoplayUnmuteHistogram.count(static_cast<int>(status));
69 } 75 }
70 76
71 void AutoplayUmaHelper::didMoveToNewDocument(Document& oldDocument) 77 void AutoplayUmaHelper::didMoveToNewDocument(Document& oldDocument)
72 { 78 {
73 if (!m_videoMutedPlayMethodVisibilityObserver) 79 if (!shouldListenToUnloadEvent())
74 return; 80 return;
75 81
76 oldDocument.domWindow()->removeEventListener(EventTypeNames::unload, this, f alse); 82 oldDocument.domWindow()->removeEventListener(EventTypeNames::unload, this, f alse);
77 m_element->document().domWindow()->addEventListener(EventTypeNames::unload, this, false); 83 m_element->document().domWindow()->addEventListener(EventTypeNames::unload, this, false);
78 } 84 }
79 85
80 void AutoplayUmaHelper::onVisibilityChangedForVideoMutedPlayMethod(bool isVisibl e) 86 void AutoplayUmaHelper::onVisibilityChangedForMutedVideoPlayMethodBecomeVisible( bool isVisible)
81 { 87 {
82 if (!isVisible) 88 if (!isVisible)
83 return; 89 return;
84 90
85 recordVideoAutoplayMutedPlayMethodBecomesVisibleUma(true); 91 maybeStopRecordingMutedVideoPlayMethodBecomeVisible(true);
86 m_videoMutedPlayMethodVisibilityObserver->stop(); 92 }
87 m_videoMutedPlayMethodVisibilityObserver = nullptr; 93
88 m_element->document().domWindow()->removeEventListener(EventTypeNames::unloa d, this, false); 94 void AutoplayUmaHelper::onVisibilityChangedForMutedVideoOffscreenDuration(bool i sVisible)
95 {
96 if (isVisible == m_isVisible)
97 return;
98
99 if (isVisible)
100 m_mutedVideoAutoplayOffscreenDurationMS += monotonicallyIncreasingTimeMS () - m_mutedVideoAutoplayStartTimeMS;
101 else
102 m_mutedVideoAutoplayStartTimeMS = monotonicallyIncreasingTimeMS();
103
104 m_isVisible = isVisible;
89 } 105 }
90 106
91 void AutoplayUmaHelper::handleEvent(ExecutionContext* executionContext, Event* e vent) 107 void AutoplayUmaHelper::handleEvent(ExecutionContext* executionContext, Event* e vent)
92 { 108 {
93 if (event->type() == EventTypeNames::playing) 109 if (event->type() == EventTypeNames::playing)
94 handlePlayingEvent(); 110 handlePlayingEvent();
111 else if (event->type() == EventTypeNames::pause)
112 handlePauseEvent();
95 else if (event->type() == EventTypeNames::unload) 113 else if (event->type() == EventTypeNames::unload)
96 handleUnloadEvent(); 114 handleUnloadEvent();
97 else 115 else
98 NOTREACHED(); 116 NOTREACHED();
99 } 117 }
100 118
119 void AutoplayUmaHelper::handlePlayingEvent()
120 {
121 maybeStartRecordingMutedVideoPlayMethodBecomeVisible();
122 maybeStartRecordingMutedVideoOffscreenDuration();
123
124 m_element->removeEventListener(EventTypeNames::playing, this, false);
125 }
126
127 void AutoplayUmaHelper::handlePauseEvent()
128 {
129 maybeStopRecordingMutedVideoOffscreenDuration();
130 }
131
101 void AutoplayUmaHelper::handleUnloadEvent() 132 void AutoplayUmaHelper::handleUnloadEvent()
102 { 133 {
103 if (m_videoMutedPlayMethodVisibilityObserver) { 134 maybeStopRecordingMutedVideoPlayMethodBecomeVisible(false);
104 recordVideoAutoplayMutedPlayMethodBecomesVisibleUma(false); 135 maybeStopRecordingMutedVideoOffscreenDuration();
105 m_videoMutedPlayMethodVisibilityObserver->stop();
106 m_videoMutedPlayMethodVisibilityObserver = nullptr;
107 m_element->document().domWindow()->removeEventListener(EventTypeNames::u nload, this, false);
108 }
109 } 136 }
110 137
111 void AutoplayUmaHelper::handlePlayingEvent() 138 void AutoplayUmaHelper::maybeStartRecordingMutedVideoPlayMethodBecomeVisible()
112 { 139 {
113 if (m_source == AutoplaySource::Method && m_element->isHTMLVideoElement() && m_element->muted()) { 140 if (m_source != AutoplaySource::Method || !m_element->isHTMLVideoElement() | | !m_element->muted())
114 if (!m_videoMutedPlayMethodVisibilityObserver) { 141 return;
115 m_videoMutedPlayMethodVisibilityObserver = new ElementVisibilityObse rver(m_element, WTF::bind(&AutoplayUmaHelper::onVisibilityChangedForVideoMutedPl ayMethod, wrapPersistent(this))); 142
116 m_videoMutedPlayMethodVisibilityObserver->start(); 143 m_mutedVideoPlayMethodVisibilityObserver = new ElementVisibilityObserver(m_e lement, WTF::bind(&AutoplayUmaHelper::onVisibilityChangedForMutedVideoPlayMethod BecomeVisible, wrapPersistent(this)));
117 m_element->document().domWindow()->addEventListener(EventTypeNames:: unload, this, false); 144 m_mutedVideoPlayMethodVisibilityObserver->start();
118 } 145 m_element->document().domWindow()->addEventListener(EventTypeNames::unload, this, false);
146 }
147
148 void AutoplayUmaHelper::maybeStopRecordingMutedVideoPlayMethodBecomeVisible(bool visible)
149 {
150 DEFINE_STATIC_LOCAL(BooleanHistogram, histogram, ("Media.Video.Autoplay.Mute d.PlayMethod.BecomesVisible"));
151
152 if (!m_mutedVideoPlayMethodVisibilityObserver)
153 return;
154
155 histogram.count(visible);
156 m_mutedVideoPlayMethodVisibilityObserver->stop();
157 m_mutedVideoPlayMethodVisibilityObserver = nullptr;
158 maybeUnregisterUnloadListener();
159 }
160
161 void AutoplayUmaHelper::maybeStartRecordingMutedVideoOffscreenDuration()
162 {
163 if (!m_element->isHTMLVideoElement() || !m_element->muted())
164 return;
165
166 // Start recording muted video playing offscreen duration.
167 m_mutedVideoAutoplayStartTimeMS = monotonicallyIncreasingTimeMS();
168 m_isVisible = false;
169 m_mutedVideoOffscreenDurationVisibilityObserver = new ElementVisibilityObser ver(m_element, WTF::bind(&AutoplayUmaHelper::onVisibilityChangedForMutedVideoOff screenDuration, wrapPersistent(this)));
mlamouri (slow - plz ping) 2016/08/17 19:27:15 wrapWeakPersistent?
Zhiqiang Zhang (Slow) 2016/08/18 10:52:43 Done.
170 m_mutedVideoOffscreenDurationVisibilityObserver->start();
171 m_element->addEventListener(EventTypeNames::pause, this, false);
172 m_element->document().domWindow()->addEventListener(EventTypeNames::unload, this, false);
173 }
174
175 void AutoplayUmaHelper::maybeStopRecordingMutedVideoOffscreenDuration()
176 {
177 if (!m_mutedVideoOffscreenDurationVisibilityObserver)
178 return;
179
180 double timeDeltaMS = m_isVisible ? m_mutedVideoAutoplayOffscreenDurationMS : maxUmaDurationMS;
mlamouri (slow - plz ping) 2016/08/17 19:27:15 I'm not sure I fully understand this logic. If we
Zhiqiang Zhang (Slow) 2016/08/18 10:52:43 OK, not clamping the values now. Just found that h
181 if (timeDeltaMS < 0)
182 timeDeltaMS = 0;
183 if (timeDeltaMS > maxUmaDurationMS)
184 timeDeltaMS = maxUmaDurationMS;
mlamouri (slow - plz ping) 2016/08/17 19:27:15 I think you can use clamp(): https://cs.chromium.o
Zhiqiang Zhang (Slow) 2016/08/18 10:52:43 Not clamping the values as said in the other reply
185
186 if (m_source == AutoplaySource::Attribute) {
187 DEFINE_STATIC_LOCAL(CustomCountHistogram, durationHistogram, ("Media.Vid eo.Autoplay.Muted.Attribute.OffscreenDuration", 0, maxUmaDurationMS, umaBucketCo unt));
188 durationHistogram.count(timeDeltaMS);
189 } else {
190 DEFINE_STATIC_LOCAL(CustomCountHistogram, durationHistogram, ("Media.Vid eo.Autoplay.Muted.PlayMethod.OffscreenDuration", 0, maxUmaDurationMS, umaBucketC ount));
191 durationHistogram.count(timeDeltaMS);
119 } 192 }
120 m_element->removeEventListener(EventTypeNames::playing, this, false); 193 m_mutedVideoOffscreenDurationVisibilityObserver->stop();
194 m_mutedVideoOffscreenDurationVisibilityObserver = nullptr;
195 m_mutedVideoAutoplayOffscreenDurationMS = 0;
196 m_element->removeEventListener(EventTypeNames::pause, this, false);
197 maybeUnregisterUnloadListener();
198 }
199
200 void AutoplayUmaHelper::maybeUnregisterUnloadListener()
201 {
202 if (!shouldListenToUnloadEvent())
203 m_element->document().domWindow()->removeEventListener(EventTypeNames::u nload, this, false);
204 }
205
206 bool AutoplayUmaHelper::shouldListenToUnloadEvent() const
207 {
208 return m_mutedVideoPlayMethodVisibilityObserver || m_mutedVideoOffscreenDura tionVisibilityObserver;
121 } 209 }
122 210
123 DEFINE_TRACE(AutoplayUmaHelper) 211 DEFINE_TRACE(AutoplayUmaHelper)
124 { 212 {
125 EventListener::trace(visitor); 213 EventListener::trace(visitor);
126 visitor->trace(m_element); 214 visitor->trace(m_element);
127 visitor->trace(m_videoMutedPlayMethodVisibilityObserver); 215 visitor->trace(m_mutedVideoPlayMethodVisibilityObserver);
216 visitor->trace(m_mutedVideoOffscreenDurationVisibilityObserver);
128 } 217 }
129 218
130 } // namespace blink 219 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698