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