Chromium Code Reviews| 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 <unordered_map> | |
| 8 | |
| 7 #include "base/bind.h" | 9 #include "base/bind.h" |
| 8 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.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" | 14 #include "content/public/browser/render_frame_host.h" |
| 13 | 15 |
| 14 namespace content { | 16 namespace content { |
| 15 | 17 |
| 16 namespace { | 18 namespace { |
| 17 | 19 |
| 18 enum class ActionType { STARTING, STOPPING }; | 20 enum class ActionType { STARTING, STOPPING }; |
| 19 AudioStreamMonitor* StartStopMonitoringHelper(ActionType action_type, | 21 AudioStreamMonitor* StartStopMonitoringHelper(ActionType action_type, |
| 20 int render_process_id, | 22 int render_process_id, |
| 21 int render_frame_id) { | 23 int render_frame_id) { |
| 22 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 24 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 23 | 25 |
| 24 // It's important that this code uses only the process id for lookup as there | 26 // 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 | 27 // may not be a RenderFrameHost or WebContents attached to the RenderProcess |
| 26 // at time of call; e.g., in the event of a crash. | 28 // at time of call; e.g., in the event of a crash. |
| 27 RenderProcessHost* const render_process_host = | 29 RenderProcessHost* const render_process_host = |
| 28 RenderProcessHost::FromID(render_process_id); | 30 RenderProcessHost::FromID(render_process_id); |
| 29 if (!render_process_host) | 31 if (!render_process_host) |
| 30 return nullptr; | 32 return nullptr; |
| 31 | 33 |
| 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 = | 34 WebContentsImpl* const web_contents = |
| 40 static_cast<WebContentsImpl*>(WebContents::FromRenderFrameHost( | 35 static_cast<WebContentsImpl*>(WebContents::FromRenderFrameHost( |
| 41 RenderFrameHost::FromID(render_process_id, render_frame_id))); | 36 RenderFrameHost::FromID(render_process_id, render_frame_id))); |
| 42 return web_contents ? web_contents->audio_stream_monitor() : nullptr; | 37 return web_contents ? web_contents->audio_stream_monitor() : nullptr; |
| 43 } | 38 } |
| 44 | 39 |
| 45 } // namespace | 40 } // namespace |
| 46 | 41 |
| 47 AudioStreamMonitor::AudioStreamMonitor(WebContents* contents) | 42 AudioStreamMonitor::AudioStreamMonitor(WebContents* contents) |
| 48 : web_contents_(contents), | 43 : web_contents_(contents), |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 65 } | 60 } |
| 66 | 61 |
| 67 void AudioStreamMonitor::RenderProcessGone(int render_process_id) { | 62 void AudioStreamMonitor::RenderProcessGone(int render_process_id) { |
| 68 DCHECK(thread_checker_.CalledOnValidThread()); | 63 DCHECK(thread_checker_.CalledOnValidThread()); |
| 69 | 64 |
| 70 // Note: It's possible for the RenderProcessHost and WebContents (and thus | 65 // 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 | 66 // this class) to survive the death of the render process and subsequently be |
| 72 // reused. During this period StartStopMonitoringHelper() will be unable to | 67 // reused. During this period StartStopMonitoringHelper() will be unable to |
| 73 // lookup the WebContents using the now-dead |render_frame_id|. We must thus | 68 // lookup the WebContents using the now-dead |render_frame_id|. We must thus |
| 74 // have this secondary mechanism for clearing stale callbacks. | 69 // have this secondary mechanism for clearing stale callbacks. |
| 75 | 70 std::unordered_set<int> audible_frame_removed; |
| 76 for (auto it = poll_callbacks_.begin(); it != poll_callbacks_.end();) { | 71 for (auto it = poll_callbacks_.begin(); it != poll_callbacks_.end();) { |
| 77 if (it->first.first == render_process_id) { | 72 if (std::get<0>(it->first) == render_process_id) { |
| 73 int render_frame_id = std::get<1>(it->first); | |
| 74 auto* render_frame_host = | |
| 75 RenderFrameHost::FromID(render_process_id, render_frame_id); | |
| 76 if (render_frame_host && render_frame_host->IsAudible()) { | |
| 77 if (audible_frame_removed.find(render_frame_id) == | |
| 78 audible_frame_removed.end()) { | |
| 79 if (auto* render_process_host = | |
| 80 RenderProcessHost::FromID(render_process_id)) | |
|
nasko
2017/06/20 16:17:45
innermost if statement also requires {}, since it
lpy
2017/06/20 22:28:53
Done.
| |
| 81 render_process_host->OnAudioStreamRemoved(); | |
| 82 audible_frame_removed.insert(render_frame_id); | |
| 83 } | |
| 84 } | |
| 78 it = poll_callbacks_.erase(it); | 85 it = poll_callbacks_.erase(it); |
| 79 OnStreamRemoved(); | 86 OnStreamRemoved(); |
| 80 } else { | 87 } else { |
| 81 ++it; | 88 ++it; |
| 82 } | 89 } |
| 83 } | 90 } |
| 84 | 91 |
| 85 if (poll_callbacks_.empty()) | 92 if (poll_callbacks_.empty()) |
| 86 poll_timer_.Stop(); | 93 poll_timer_.Stop(); |
| 87 } | 94 } |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 115 | 122 |
| 116 // static | 123 // static |
| 117 void AudioStreamMonitor::StartMonitoringHelper( | 124 void AudioStreamMonitor::StartMonitoringHelper( |
| 118 int render_process_id, | 125 int render_process_id, |
| 119 int render_frame_id, | 126 int render_frame_id, |
| 120 int stream_id, | 127 int stream_id, |
| 121 const ReadPowerAndClipCallback& read_power_callback) { | 128 const ReadPowerAndClipCallback& read_power_callback) { |
| 122 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 129 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 123 if (AudioStreamMonitor* monitor = StartStopMonitoringHelper( | 130 if (AudioStreamMonitor* monitor = StartStopMonitoringHelper( |
| 124 ActionType::STARTING, render_process_id, render_frame_id)) { | 131 ActionType::STARTING, render_process_id, render_frame_id)) { |
| 125 monitor->StartMonitoringStreamOnUIThread(render_process_id, stream_id, | 132 monitor->StartMonitoringStreamOnUIThread(render_process_id, render_frame_id, |
| 126 read_power_callback); | 133 stream_id, read_power_callback); |
| 127 } | 134 } |
| 128 } | 135 } |
| 129 | 136 |
| 130 // static | 137 // static |
| 131 void AudioStreamMonitor::StopMonitoringHelper(int render_process_id, | 138 void AudioStreamMonitor::StopMonitoringHelper(int render_process_id, |
| 132 int render_frame_id, | 139 int render_frame_id, |
| 133 int stream_id) { | 140 int stream_id) { |
| 134 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 141 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 135 if (AudioStreamMonitor* monitor = StartStopMonitoringHelper( | 142 if (AudioStreamMonitor* monitor = StartStopMonitoringHelper( |
| 136 ActionType::STOPPING, render_process_id, render_frame_id)) { | 143 ActionType::STOPPING, render_process_id, render_frame_id)) { |
| 137 monitor->StopMonitoringStreamOnUIThread(render_process_id, stream_id); | 144 monitor->StopMonitoringStreamOnUIThread(render_process_id, render_frame_id, |
| 145 stream_id); | |
| 138 } | 146 } |
| 139 } | 147 } |
| 140 | 148 |
| 141 void AudioStreamMonitor::StartMonitoringStreamOnUIThread( | 149 void AudioStreamMonitor::StartMonitoringStreamOnUIThread( |
| 142 int render_process_id, | 150 int render_process_id, |
| 151 int render_frame_id, | |
| 143 int stream_id, | 152 int stream_id, |
| 144 const ReadPowerAndClipCallback& read_power_callback) { | 153 const ReadPowerAndClipCallback& read_power_callback) { |
| 145 DCHECK(thread_checker_.CalledOnValidThread()); | 154 DCHECK(thread_checker_.CalledOnValidThread()); |
| 146 DCHECK(!read_power_callback.is_null()); | 155 DCHECK(!read_power_callback.is_null()); |
| 147 | 156 |
| 148 const StreamID qualified_id(render_process_id, stream_id); | 157 const StreamID qualified_id(render_process_id, render_frame_id, stream_id); |
| 149 DCHECK(poll_callbacks_.find(qualified_id) == poll_callbacks_.end()); | 158 DCHECK(poll_callbacks_.find(qualified_id) == poll_callbacks_.end()); |
| 150 | 159 |
| 151 poll_callbacks_[qualified_id] = read_power_callback; | 160 poll_callbacks_[qualified_id] = read_power_callback; |
| 161 // Sends audio-added signal to RenderProcessHost when there is no power level | |
| 162 // monitoring, sends the signal when the stream becomes non-audible. | |
| 163 if (!power_level_monitoring_available()) { | |
| 164 if (auto* render_process_host = | |
| 165 RenderProcessHost::FromID(render_process_id)) | |
|
nasko
2017/06/20 16:17:45
Same here about needing {} on multiline if stateme
lpy
2017/06/20 22:28:53
Done.
| |
| 166 render_process_host->OnAudioStreamAdded(); | |
| 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( |
| 161 if (it == poll_callbacks_.end()) | 178 StreamID(render_process_id, render_frame_id, stream_id)); |
| 179 if (it == poll_callbacks_.end()) { | |
|
DaleCurtis
2017/06/20 00:32:53
No need for {} on single line conditional.
lpy
2017/06/20 22:28:53
Done.
| |
| 162 return; | 180 return; |
| 181 } | |
| 163 | 182 |
| 164 poll_callbacks_.erase(it); | 183 poll_callbacks_.erase(it); |
| 184 // Sends audio-removed signal to RenderProcessHost when there is no power | |
| 185 // level monitoring, otherwise sends the signal when the stream becomes | |
| 186 // non-audible. | |
| 187 if (!power_level_monitoring_available()) { | |
| 188 if (auto* render_process_host = | |
| 189 RenderProcessHost::FromID(render_process_id)) | |
| 190 render_process_host->OnAudioStreamRemoved(); | |
| 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 |
| 200 // Record whether or not a RenderFrameHost is audible. | |
| 201 std::unordered_map<RenderFrameHost*, bool> is_frame_audible; | |
|
DaleCurtis
2017/06/20 00:32:53
I don't think there should be very many of these,
lpy
2017/06/20 22:28:53
Done.
| |
| 172 for (StreamPollCallbackMap::const_iterator it = poll_callbacks_.begin(); | 202 for (StreamPollCallbackMap::const_iterator it = poll_callbacks_.begin(); |
| 173 it != poll_callbacks_.end(); | 203 it != poll_callbacks_.end(); ++it) { |
| 174 ++it) { | |
| 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 = it->second.Run().first; |
| 179 const float kSilenceThresholdDBFS = -72.24719896f; | 208 const float kSilenceThresholdDBFS = -72.24719896f; |
| 180 | 209 |
| 181 if (power_dbfs >= kSilenceThresholdDBFS) { | 210 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. | 215 } |
| 216 | |
| 217 // Record whether or not the RenderFrame is audible, a RenderFrame is | |
| 218 // audible when there is a audio stream in it that is audible. | |
| 219 auto* render_frame_host = | |
| 220 RenderFrameHost::FromID(std::get<0>(it->first), std::get<1>(it->first)); | |
| 221 if (!render_frame_host) | |
|
DaleCurtis
2017/06/20 00:32:53
I think this should be impossible here, we would h
lpy
2017/06/20 22:28:53
Done.
| |
| 222 continue; | |
| 223 if (is_frame_audible.find(render_frame_host) == is_frame_audible.end()) { | |
| 224 is_frame_audible[render_frame_host] = is_stream_audible; | |
| 225 } else { | |
| 226 is_frame_audible[render_frame_host] |= is_stream_audible; | |
|
DaleCurtis
2017/06/20 00:32:53
I think you just need this line. Default value of
lpy
2017/06/20 22:28:53
Done.
| |
| 186 } | 227 } |
| 187 } | 228 } |
| 188 | 229 |
| 230 // Update RenderFrameHost and RenderProcessHost audible state only when state | |
|
DaleCurtis
2017/06/20 00:32:53
Do we want to be responsible for both RenderFrameH
lpy
2017/06/20 22:28:53
Done.
I defer to RenderFrameHosts to update their
| |
| 231 // changed. | |
| 232 for (auto it = is_frame_audible.begin(); it != is_frame_audible.end(); ++it) { | |
|
DaleCurtis
2017/06/20 00:32:53
for (auto& kv : ...) { }
lpy
2017/06/20 22:28:53
Done.
| |
| 233 auto* render_frame_host = it->first; | |
| 234 bool is_frame_audible = it->second; | |
| 235 if (is_frame_audible != render_frame_host->IsAudible()) { | |
| 236 render_frame_host->OnAudioStateChanged(is_frame_audible); | |
| 237 auto* render_process_host = render_frame_host->GetProcess(); | |
| 238 if (!render_process_host) | |
|
DaleCurtis
2017/06/20 00:32:53
Again shouldn't be possible at this point.
lpy
2017/06/20 22:28:53
Done.
| |
| 239 continue; | |
| 240 if (is_frame_audible) { | |
| 241 render_process_host->OnAudioStreamAdded(); | |
| 242 } else { | |
| 243 render_process_host->OnAudioStreamRemoved(); | |
| 244 } | |
| 245 } | |
| 246 } | |
| 247 | |
| 189 if (is_audible_ != was_audible) | 248 if (is_audible_ != was_audible) |
| 190 web_contents_->OnAudioStateChanged(is_audible_); | 249 web_contents_->OnAudioStateChanged(is_audible_); |
| 191 } | 250 } |
| 192 | 251 |
| 193 void AudioStreamMonitor::MaybeToggle() { | 252 void AudioStreamMonitor::MaybeToggle() { |
| 194 const bool indicator_was_on = was_recently_audible_; | 253 const bool indicator_was_on = was_recently_audible_; |
| 195 const base::TimeTicks off_time = | 254 const base::TimeTicks off_time = |
| 196 last_blurt_time_ + base::TimeDelta::FromMilliseconds(kHoldOnMilliseconds); | 255 last_blurt_time_ + base::TimeDelta::FromMilliseconds(kHoldOnMilliseconds); |
| 197 const base::TimeTicks now = clock_->NowTicks(); | 256 const base::TimeTicks now = clock_->NowTicks(); |
| 198 const bool should_indicator_be_on = now < off_time; | 257 const bool should_indicator_be_on = now < off_time; |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 237 if (!power_level_monitoring_available()) { | 296 if (!power_level_monitoring_available()) { |
| 238 is_audible_ = false; | 297 is_audible_ = false; |
| 239 web_contents_->OnAudioStateChanged(false); | 298 web_contents_->OnAudioStateChanged(false); |
| 240 MaybeToggle(); | 299 MaybeToggle(); |
| 241 } else { | 300 } else { |
| 242 poll_timer_.Stop(); | 301 poll_timer_.Stop(); |
| 243 } | 302 } |
| 244 } | 303 } |
| 245 | 304 |
| 246 } // namespace content | 305 } // namespace content |
| OLD | NEW |