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

Side by Side Diff: content/browser/media/audio_stream_monitor.cc

Issue 2948613002: [AudioStreamMonitor] Adds API to collect frame-level audibility. (Closed)
Patch Set: Addressed comments and rebased Created 3 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
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "content/browser/media/audio_stream_monitor.h" 5 #include "content/browser/media/audio_stream_monitor.h"
6 6
7 #include "base/bind.h" 7 #include "base/bind.h"
8 #include "base/bind_helpers.h" 8 #include "base/bind_helpers.h"
9 #include "content/browser/frame_host/render_frame_host_impl.h"
9 #include "content/browser/web_contents/web_contents_impl.h" 10 #include "content/browser/web_contents/web_contents_impl.h"
10 #include "content/public/browser/browser_thread.h" 11 #include "content/public/browser/browser_thread.h"
11 #include "content/public/browser/invalidate_type.h" 12 #include "content/public/browser/invalidate_type.h"
12 #include "content/public/browser/render_frame_host.h"
13 13
14 namespace content { 14 namespace content {
15 15
16 namespace { 16 namespace {
17 17
18 enum class ActionType { STARTING, STOPPING }; 18 enum class ActionType { STARTING, STOPPING };
19 AudioStreamMonitor* StartStopMonitoringHelper(ActionType action_type, 19 AudioStreamMonitor* StartStopMonitoringHelper(ActionType action_type,
20 int render_process_id, 20 int render_process_id,
21 int render_frame_id) { 21 int render_frame_id) {
22 DCHECK_CURRENTLY_ON(BrowserThread::UI); 22 DCHECK_CURRENTLY_ON(BrowserThread::UI);
23 23
24 // It's important that this code uses only the process id for lookup as there 24 // It's important that this code uses only the process id for lookup as there
25 // may not be a RenderFrameHost or WebContents attached to the RenderProcess 25 // may not be a RenderFrameHost or WebContents attached to the RenderProcess
26 // at time of call; e.g., in the event of a crash. 26 // at time of call; e.g., in the event of a crash.
27 RenderProcessHost* const render_process_host = 27 RenderProcessHost* const render_process_host =
28 RenderProcessHost::FromID(render_process_id); 28 RenderProcessHost::FromID(render_process_id);
29 if (!render_process_host) 29 if (!render_process_host)
30 return nullptr; 30 return nullptr;
31 31
32 // TODO(dalecurtis, maxmorin): We should really only be sending these when the
33 // streams are audible or we don't have power level monitoring.
34 if (action_type == ActionType::STARTING)
35 render_process_host->OnAudioStreamAdded();
36 else
37 render_process_host->OnAudioStreamRemoved();
38
39 WebContentsImpl* const web_contents = 32 WebContentsImpl* const web_contents =
40 static_cast<WebContentsImpl*>(WebContents::FromRenderFrameHost( 33 static_cast<WebContentsImpl*>(WebContents::FromRenderFrameHost(
41 RenderFrameHost::FromID(render_process_id, render_frame_id))); 34 RenderFrameHost::FromID(render_process_id, render_frame_id)));
42 return web_contents ? web_contents->audio_stream_monitor() : nullptr; 35 return web_contents ? web_contents->audio_stream_monitor() : nullptr;
43 } 36 }
44 37
45 } // namespace 38 } // namespace
46 39
40 bool AudioStreamMonitor::StreamID::operator<(const StreamID& other) const {
41 return std::tie(render_process_id, render_frame_id, stream_id) <
42 std::tie(other.render_process_id, other.render_frame_id,
43 other.stream_id);
44 }
45
47 AudioStreamMonitor::AudioStreamMonitor(WebContents* contents) 46 AudioStreamMonitor::AudioStreamMonitor(WebContents* contents)
48 : web_contents_(contents), 47 : web_contents_(contents),
49 clock_(&default_tick_clock_), 48 clock_(&default_tick_clock_),
50 was_recently_audible_(false), 49 was_recently_audible_(false),
51 is_audible_(false) { 50 is_audible_(false) {
52 DCHECK(web_contents_); 51 DCHECK(web_contents_);
53 } 52 }
54 53
55 AudioStreamMonitor::~AudioStreamMonitor() {} 54 AudioStreamMonitor::~AudioStreamMonitor() {}
56 55
57 bool AudioStreamMonitor::WasRecentlyAudible() const { 56 bool AudioStreamMonitor::WasRecentlyAudible() const {
58 DCHECK(thread_checker_.CalledOnValidThread()); 57 DCHECK(thread_checker_.CalledOnValidThread());
59 return was_recently_audible_; 58 return was_recently_audible_;
60 } 59 }
61 60
62 bool AudioStreamMonitor::IsCurrentlyAudible() const { 61 bool AudioStreamMonitor::IsCurrentlyAudible() const {
63 DCHECK(thread_checker_.CalledOnValidThread()); 62 DCHECK(thread_checker_.CalledOnValidThread());
64 return is_audible_; 63 return is_audible_;
65 } 64 }
66 65
67 void AudioStreamMonitor::RenderProcessGone(int render_process_id) { 66 void AudioStreamMonitor::RenderProcessGone(int render_process_id) {
68 DCHECK(thread_checker_.CalledOnValidThread()); 67 DCHECK(thread_checker_.CalledOnValidThread());
69 68
70 // Note: It's possible for the RenderProcessHost and WebContents (and thus 69 // Note: It's possible for the RenderProcessHost and WebContents (and thus
71 // this class) to survive the death of the render process and subsequently be 70 // this class) to survive the death of the render process and subsequently be
72 // reused. During this period StartStopMonitoringHelper() will be unable to 71 // reused. During this period StartStopMonitoringHelper() will be unable to
73 // lookup the WebContents using the now-dead |render_frame_id|. We must thus 72 // lookup the WebContents using the now-dead |render_frame_id|. We must thus
74 // have this secondary mechanism for clearing stale callbacks. 73 // have this secondary mechanism for clearing stale callbacks.
75
76 for (auto it = poll_callbacks_.begin(); it != poll_callbacks_.end();) { 74 for (auto it = poll_callbacks_.begin(); it != poll_callbacks_.end();) {
77 if (it->first.first == render_process_id) { 75 if (it->first.render_process_id == render_process_id) {
78 it = poll_callbacks_.erase(it); 76 it = poll_callbacks_.erase(it);
79 OnStreamRemoved(); 77 OnStreamRemoved();
80 } else { 78 } else {
81 ++it; 79 ++it;
82 } 80 }
83 } 81 }
84 82
85 if (poll_callbacks_.empty()) 83 if (poll_callbacks_.empty())
86 poll_timer_.Stop(); 84 poll_timer_.Stop();
87 } 85 }
(...skipping 27 matching lines...) Expand all
115 113
116 // static 114 // static
117 void AudioStreamMonitor::StartMonitoringHelper( 115 void AudioStreamMonitor::StartMonitoringHelper(
118 int render_process_id, 116 int render_process_id,
119 int render_frame_id, 117 int render_frame_id,
120 int stream_id, 118 int stream_id,
121 const ReadPowerAndClipCallback& read_power_callback) { 119 const ReadPowerAndClipCallback& read_power_callback) {
122 DCHECK_CURRENTLY_ON(BrowserThread::UI); 120 DCHECK_CURRENTLY_ON(BrowserThread::UI);
123 if (AudioStreamMonitor* monitor = StartStopMonitoringHelper( 121 if (AudioStreamMonitor* monitor = StartStopMonitoringHelper(
124 ActionType::STARTING, render_process_id, render_frame_id)) { 122 ActionType::STARTING, render_process_id, render_frame_id)) {
125 monitor->StartMonitoringStreamOnUIThread(render_process_id, stream_id, 123 monitor->StartMonitoringStreamOnUIThread(render_process_id, render_frame_id,
126 read_power_callback); 124 stream_id, read_power_callback);
127 } 125 }
128 } 126 }
129 127
130 // static 128 // static
131 void AudioStreamMonitor::StopMonitoringHelper(int render_process_id, 129 void AudioStreamMonitor::StopMonitoringHelper(int render_process_id,
132 int render_frame_id, 130 int render_frame_id,
133 int stream_id) { 131 int stream_id) {
134 DCHECK_CURRENTLY_ON(BrowserThread::UI); 132 DCHECK_CURRENTLY_ON(BrowserThread::UI);
135 if (AudioStreamMonitor* monitor = StartStopMonitoringHelper( 133 if (AudioStreamMonitor* monitor = StartStopMonitoringHelper(
136 ActionType::STOPPING, render_process_id, render_frame_id)) { 134 ActionType::STOPPING, render_process_id, render_frame_id)) {
137 monitor->StopMonitoringStreamOnUIThread(render_process_id, stream_id); 135 monitor->StopMonitoringStreamOnUIThread(render_process_id, render_frame_id,
136 stream_id);
138 } 137 }
139 } 138 }
140 139
141 void AudioStreamMonitor::StartMonitoringStreamOnUIThread( 140 void AudioStreamMonitor::StartMonitoringStreamOnUIThread(
142 int render_process_id, 141 int render_process_id,
142 int render_frame_id,
143 int stream_id, 143 int stream_id,
144 const ReadPowerAndClipCallback& read_power_callback) { 144 const ReadPowerAndClipCallback& read_power_callback) {
145 DCHECK(thread_checker_.CalledOnValidThread()); 145 DCHECK(thread_checker_.CalledOnValidThread());
146 DCHECK(!read_power_callback.is_null()); 146 DCHECK(!read_power_callback.is_null());
147 147
148 const StreamID qualified_id(render_process_id, stream_id); 148 const StreamID qualified_id = {render_process_id, render_frame_id, stream_id};
149 DCHECK(poll_callbacks_.find(qualified_id) == poll_callbacks_.end()); 149 DCHECK(poll_callbacks_.find(qualified_id) == poll_callbacks_.end());
150 150
151 poll_callbacks_[qualified_id] = read_power_callback; 151 poll_callbacks_[qualified_id] = read_power_callback;
152
153 // Sends audible signal to RenderFrameHost when there is no power level
154 // monitoring, otherwise sends the signal when the stream becomes audible.
155 if (!power_level_monitoring_available()) {
156 if (auto* render_frame_host = static_cast<RenderFrameHostImpl*>(
157 RenderFrameHost::FromID(render_process_id, render_frame_id))) {
158 render_frame_host->OnAudioStateChanged(true);
159 }
160 }
161
152 OnStreamAdded(); 162 OnStreamAdded();
153 } 163 }
154 164
155 void AudioStreamMonitor::StopMonitoringStreamOnUIThread(int render_process_id, 165 void AudioStreamMonitor::StopMonitoringStreamOnUIThread(int render_process_id,
166 int render_frame_id,
156 int stream_id) { 167 int stream_id) {
157 DCHECK(thread_checker_.CalledOnValidThread()); 168 DCHECK(thread_checker_.CalledOnValidThread());
158 169
159 // In the event of render process death, these may have already been cleared. 170 // In the event of render process death, these may have already been cleared.
160 auto it = poll_callbacks_.find(StreamID(render_process_id, stream_id)); 171 auto it = poll_callbacks_.find(
172 StreamID{render_process_id, render_frame_id, stream_id});
161 if (it == poll_callbacks_.end()) 173 if (it == poll_callbacks_.end())
162 return; 174 return;
163 175
164 poll_callbacks_.erase(it); 176 poll_callbacks_.erase(it);
177
178 // Sends non-audible signal to RenderFrameHost when there is no power level
179 // monitoring, otherwise sends the signal when the stream becomes non-audible.
180 if (!power_level_monitoring_available()) {
181 if (auto* render_frame_host = static_cast<RenderFrameHostImpl*>(
182 RenderFrameHost::FromID(render_process_id, render_frame_id))) {
183 render_frame_host->OnAudioStateChanged(false);
184 }
185 }
186
165 OnStreamRemoved(); 187 OnStreamRemoved();
166 } 188 }
167 189
168 void AudioStreamMonitor::Poll() { 190 void AudioStreamMonitor::Poll() {
169 bool was_audible = is_audible_; 191 bool was_audible = is_audible_;
170 is_audible_ = false; 192 is_audible_ = false;
171 193
172 for (StreamPollCallbackMap::const_iterator it = poll_callbacks_.begin(); 194 // Record whether or not a RenderFrameHost is audible.
173 it != poll_callbacks_.end(); 195 base::flat_map<RenderFrameHostImpl*, bool> audible_frame_map;
174 ++it) { 196 audible_frame_map.reserve(poll_callbacks_.size());
197 for (auto& kv : poll_callbacks_) {
175 // TODO(miu): A new UI for delivering specific power level and clipping 198 // TODO(miu): A new UI for delivering specific power level and clipping
176 // information is still in the works. For now, we throw away all 199 // information is still in the works. For now, we throw away all
177 // information except for "is it audible?" 200 // information except for "is it audible?"
178 const float power_dbfs = it->second.Run().first; 201 const float power_dbfs = kv.second.Run().first;
179 const float kSilenceThresholdDBFS = -72.24719896f; 202 const float kSilenceThresholdDBFS = -72.24719896f;
180 203
181 if (power_dbfs >= kSilenceThresholdDBFS) { 204 const bool is_stream_audible = power_dbfs >= kSilenceThresholdDBFS;
205 if (!is_audible_ && is_stream_audible) {
miu 2017/06/26 22:57:59 It doesn't seem like the conditional here should b
lpy 2017/06/28 02:14:29 I don't think it changes the logic here. It still
182 last_blurt_time_ = clock_->NowTicks(); 206 last_blurt_time_ = clock_->NowTicks();
183 is_audible_ = true; 207 is_audible_ = true;
184 MaybeToggle(); 208 MaybeToggle();
185 break; // No need to poll remaining streams.
186 } 209 }
210
211 // Record whether or not the RenderFrame is audible, a RenderFrame is
miu 2017/06/26 22:57:59 There are separate sentences here: "audible, a Ren
lpy 2017/06/28 02:14:29 Done.
212 // audible when there is a audio stream in it that is audible.
miu 2017/06/26 22:57:59 nit: s/when there is a audio stream in it/it has a
lpy 2017/06/28 02:14:29 Done.
213 auto* render_frame_host_impl =
214 static_cast<RenderFrameHostImpl*>(RenderFrameHost::FromID(
215 kv.first.render_process_id, kv.first.render_frame_id));
216 // This may be nullptr in tests.
217 if (!render_frame_host_impl)
218 continue;
219 audible_frame_map[render_frame_host_impl] |= is_stream_audible;
220 }
221
222 // Update RenderFrameHost audible state only when state changed.
223 for (auto& kv : audible_frame_map) {
224 auto* render_frame_host_impl = kv.first;
225 bool is_frame_audible = kv.second;
226 if (is_frame_audible != render_frame_host_impl->is_audible())
227 render_frame_host_impl->OnAudioStateChanged(is_frame_audible);
187 } 228 }
188 229
189 if (is_audible_ != was_audible) 230 if (is_audible_ != was_audible)
190 web_contents_->OnAudioStateChanged(is_audible_); 231 web_contents_->OnAudioStateChanged(is_audible_);
191 } 232 }
192 233
193 void AudioStreamMonitor::MaybeToggle() { 234 void AudioStreamMonitor::MaybeToggle() {
miu 2017/06/26 22:57:59 Consider renaming this method. Suggestion: MaybeTo
lpy 2017/06/28 02:14:29 I will leave it to separate cleanup patches.
194 const bool indicator_was_on = was_recently_audible_; 235 const bool indicator_was_on = was_recently_audible_;
195 const base::TimeTicks off_time = 236 const base::TimeTicks off_time =
196 last_blurt_time_ + base::TimeDelta::FromMilliseconds(kHoldOnMilliseconds); 237 last_blurt_time_ + base::TimeDelta::FromMilliseconds(kHoldOnMilliseconds);
197 const base::TimeTicks now = clock_->NowTicks(); 238 const base::TimeTicks now = clock_->NowTicks();
198 const bool should_indicator_be_on = now < off_time; 239 const bool should_indicator_be_on = now < off_time;
199 240
200 if (should_indicator_be_on != indicator_was_on) { 241 if (should_indicator_be_on != indicator_was_on) {
201 was_recently_audible_ = should_indicator_be_on; 242 was_recently_audible_ = should_indicator_be_on;
202 web_contents_->NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB); 243 web_contents_->NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
203 } 244 }
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
237 if (!power_level_monitoring_available()) { 278 if (!power_level_monitoring_available()) {
238 is_audible_ = false; 279 is_audible_ = false;
239 web_contents_->OnAudioStateChanged(false); 280 web_contents_->OnAudioStateChanged(false);
240 MaybeToggle(); 281 MaybeToggle();
241 } else { 282 } else {
242 poll_timer_.Stop(); 283 poll_timer_.Stop();
243 } 284 }
244 } 285 }
245 286
246 } // namespace content 287 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698