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

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

Powered by Google App Engine
This is Rietveld 408576698