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 18 matching lines...) Expand all Loading... | |
| 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 |
| 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) { |
| 78 it = poll_callbacks_.erase(it); | 73 it = poll_callbacks_.erase(it); |
| 79 OnStreamRemoved(); | 74 OnStreamRemoved(); |
| 80 } else { | 75 } else { |
| 81 ++it; | 76 ++it; |
| 82 } | 77 } |
| 83 } | 78 } |
| 84 | 79 |
| 85 if (poll_callbacks_.empty()) | 80 if (poll_callbacks_.empty()) |
| 86 poll_timer_.Stop(); | 81 poll_timer_.Stop(); |
| 87 } | 82 } |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 115 | 110 |
| 116 // static | 111 // static |
| 117 void AudioStreamMonitor::StartMonitoringHelper( | 112 void AudioStreamMonitor::StartMonitoringHelper( |
| 118 int render_process_id, | 113 int render_process_id, |
| 119 int render_frame_id, | 114 int render_frame_id, |
| 120 int stream_id, | 115 int stream_id, |
| 121 const ReadPowerAndClipCallback& read_power_callback) { | 116 const ReadPowerAndClipCallback& read_power_callback) { |
| 122 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 117 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 123 if (AudioStreamMonitor* monitor = StartStopMonitoringHelper( | 118 if (AudioStreamMonitor* monitor = StartStopMonitoringHelper( |
| 124 ActionType::STARTING, render_process_id, render_frame_id)) { | 119 ActionType::STARTING, render_process_id, render_frame_id)) { |
| 125 monitor->StartMonitoringStreamOnUIThread(render_process_id, stream_id, | 120 monitor->StartMonitoringStreamOnUIThread(render_process_id, render_frame_id, |
| 126 read_power_callback); | 121 stream_id, read_power_callback); |
| 127 } | 122 } |
| 128 } | 123 } |
| 129 | 124 |
| 130 // static | 125 // static |
| 131 void AudioStreamMonitor::StopMonitoringHelper(int render_process_id, | 126 void AudioStreamMonitor::StopMonitoringHelper(int render_process_id, |
| 132 int render_frame_id, | 127 int render_frame_id, |
| 133 int stream_id) { | 128 int stream_id) { |
| 134 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 129 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 135 if (AudioStreamMonitor* monitor = StartStopMonitoringHelper( | 130 if (AudioStreamMonitor* monitor = StartStopMonitoringHelper( |
| 136 ActionType::STOPPING, render_process_id, render_frame_id)) { | 131 ActionType::STOPPING, render_process_id, render_frame_id)) { |
| 137 monitor->StopMonitoringStreamOnUIThread(render_process_id, stream_id); | 132 monitor->StopMonitoringStreamOnUIThread(render_process_id, render_frame_id, |
| 133 stream_id); | |
| 138 } | 134 } |
| 139 } | 135 } |
| 140 | 136 |
| 141 void AudioStreamMonitor::StartMonitoringStreamOnUIThread( | 137 void AudioStreamMonitor::StartMonitoringStreamOnUIThread( |
| 142 int render_process_id, | 138 int render_process_id, |
| 139 int render_frame_id, | |
| 143 int stream_id, | 140 int stream_id, |
| 144 const ReadPowerAndClipCallback& read_power_callback) { | 141 const ReadPowerAndClipCallback& read_power_callback) { |
| 145 DCHECK(thread_checker_.CalledOnValidThread()); | 142 DCHECK(thread_checker_.CalledOnValidThread()); |
| 146 DCHECK(!read_power_callback.is_null()); | 143 DCHECK(!read_power_callback.is_null()); |
| 147 | 144 |
| 148 const StreamID qualified_id(render_process_id, stream_id); | 145 const StreamID qualified_id(render_process_id, render_frame_id, stream_id); |
| 149 DCHECK(poll_callbacks_.find(qualified_id) == poll_callbacks_.end()); | 146 DCHECK(poll_callbacks_.find(qualified_id) == poll_callbacks_.end()); |
| 150 | 147 |
| 151 poll_callbacks_[qualified_id] = read_power_callback; | 148 poll_callbacks_[qualified_id] = read_power_callback; |
| 149 // We should send audio-added signal to RenderProcessHost when we don't have | |
|
miu
2017/06/19 20:44:01
style nit: Try not to use "we" in comments. :)
lpy
2017/06/20 00:19:21
Done.
| |
| 150 // power level monitoring, otherwise we should send the signal when the stream | |
| 151 // becomes non-audible. | |
| 152 if (!power_level_monitoring_available()) | |
| 153 RenderProcessHost::FromID(render_process_id)->OnAudioStreamAdded(); | |
|
miu
2017/06/19 20:44:01
Always null-check the result of RPH::FromID() (her
lpy
2017/06/20 00:19:21
Done.
| |
| 152 OnStreamAdded(); | 154 OnStreamAdded(); |
| 153 } | 155 } |
| 154 | 156 |
| 155 void AudioStreamMonitor::StopMonitoringStreamOnUIThread(int render_process_id, | 157 void AudioStreamMonitor::StopMonitoringStreamOnUIThread(int render_process_id, |
| 158 int render_frame_id, | |
| 156 int stream_id) { | 159 int stream_id) { |
| 157 DCHECK(thread_checker_.CalledOnValidThread()); | 160 DCHECK(thread_checker_.CalledOnValidThread()); |
| 158 | 161 |
| 159 // In the event of render process death, these may have already been cleared. | 162 // 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)); | 163 auto it = poll_callbacks_.find( |
| 164 StreamID(render_process_id, render_frame_id, stream_id)); | |
| 161 if (it == poll_callbacks_.end()) | 165 if (it == poll_callbacks_.end()) |
| 162 return; | 166 return; |
| 163 | 167 |
| 164 poll_callbacks_.erase(it); | 168 poll_callbacks_.erase(it); |
| 169 // We should send audio-removed signal to RenderProcessHost when we don't have | |
|
miu
2017/06/19 20:44:01
style nit: ditto (no "we")
lpy
2017/06/20 00:19:21
Done.
| |
| 170 // power level monitoring, otherwise we should send the signal when the stream | |
| 171 // becomes non-audible. | |
| 172 if (!power_level_monitoring_available()) | |
| 173 RenderProcessHost::FromID(render_process_id)->OnAudioStreamRemoved(); | |
| 165 OnStreamRemoved(); | 174 OnStreamRemoved(); |
| 166 } | 175 } |
| 167 | 176 |
| 168 void AudioStreamMonitor::Poll() { | 177 void AudioStreamMonitor::Poll() { |
| 169 bool was_audible = is_audible_; | 178 bool was_audible = is_audible_; |
| 170 is_audible_ = false; | 179 is_audible_ = false; |
| 171 | 180 |
| 181 // Record whether or not a RenderFrameHost is audible. | |
| 182 std::unordered_map<RenderFrameHost*, bool> is_frame_audible; | |
|
miu
2017/06/19 20:44:01
It feels expensive to build-up a data structure on
lpy
2017/06/20 00:19:21
https://docs.google.com/document/d/15wWebVCLU3UM-O
| |
| 172 for (StreamPollCallbackMap::const_iterator it = poll_callbacks_.begin(); | 183 for (StreamPollCallbackMap::const_iterator it = poll_callbacks_.begin(); |
| 173 it != poll_callbacks_.end(); | 184 it != poll_callbacks_.end(); ++it) { |
| 174 ++it) { | |
| 175 // TODO(miu): A new UI for delivering specific power level and clipping | 185 // 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 | 186 // information is still in the works. For now, we throw away all |
| 177 // information except for "is it audible?" | 187 // information except for "is it audible?" |
| 178 const float power_dbfs = it->second.Run().first; | 188 const float power_dbfs = it->second.Run().first; |
| 179 const float kSilenceThresholdDBFS = -72.24719896f; | 189 const float kSilenceThresholdDBFS = -72.24719896f; |
| 180 | 190 |
| 181 if (power_dbfs >= kSilenceThresholdDBFS) { | 191 bool is_stream_audible = power_dbfs >= kSilenceThresholdDBFS; |
| 192 if (!is_audible_ && is_stream_audible) { | |
| 182 last_blurt_time_ = clock_->NowTicks(); | 193 last_blurt_time_ = clock_->NowTicks(); |
| 183 is_audible_ = true; | 194 is_audible_ = true; |
| 184 MaybeToggle(); | 195 MaybeToggle(); |
| 185 break; // No need to poll remaining streams. | 196 } |
| 197 | |
| 198 // Record whether or not the RenderFrame is audible, a RenderFrame is | |
| 199 // audible when there is a audio stream in it that is audible. | |
| 200 auto* render_frame_host = | |
| 201 RenderFrameHost::FromID(std::get<0>(it->first), std::get<1>(it->first)); | |
| 202 if (!render_frame_host) | |
| 203 continue; | |
| 204 if (is_frame_audible.find(render_frame_host) == is_frame_audible.end()) { | |
| 205 is_frame_audible[render_frame_host] = is_stream_audible; | |
| 206 } else { | |
| 207 is_frame_audible[render_frame_host] |= is_stream_audible; | |
| 186 } | 208 } |
| 187 } | 209 } |
| 188 | 210 |
| 211 // Update RenderFrameHost and RenderProcessHost audible state only when state | |
| 212 // changed. | |
| 213 for (auto it = is_frame_audible.begin(); it != is_frame_audible.end(); ++it) { | |
| 214 auto* render_frame_host = it->first; | |
| 215 bool is_frame_audible = it->second; | |
| 216 if (is_frame_audible != render_frame_host->IsAudible()) { | |
| 217 render_frame_host->OnAudioStateChanged(is_frame_audible); | |
| 218 if (is_frame_audible) { | |
| 219 render_frame_host->GetProcess()->OnAudioStreamAdded(); | |
| 220 } else { | |
| 221 render_frame_host->GetProcess()->OnAudioStreamRemoved(); | |
| 222 } | |
| 223 } | |
| 224 } | |
| 225 | |
| 189 if (is_audible_ != was_audible) | 226 if (is_audible_ != was_audible) |
| 190 web_contents_->OnAudioStateChanged(is_audible_); | 227 web_contents_->OnAudioStateChanged(is_audible_); |
| 191 } | 228 } |
| 192 | 229 |
| 193 void AudioStreamMonitor::MaybeToggle() { | 230 void AudioStreamMonitor::MaybeToggle() { |
| 194 const bool indicator_was_on = was_recently_audible_; | 231 const bool indicator_was_on = was_recently_audible_; |
| 195 const base::TimeTicks off_time = | 232 const base::TimeTicks off_time = |
| 196 last_blurt_time_ + base::TimeDelta::FromMilliseconds(kHoldOnMilliseconds); | 233 last_blurt_time_ + base::TimeDelta::FromMilliseconds(kHoldOnMilliseconds); |
| 197 const base::TimeTicks now = clock_->NowTicks(); | 234 const base::TimeTicks now = clock_->NowTicks(); |
| 198 const bool should_indicator_be_on = now < off_time; | 235 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()) { | 274 if (!power_level_monitoring_available()) { |
| 238 is_audible_ = false; | 275 is_audible_ = false; |
| 239 web_contents_->OnAudioStateChanged(false); | 276 web_contents_->OnAudioStateChanged(false); |
| 240 MaybeToggle(); | 277 MaybeToggle(); |
| 241 } else { | 278 } else { |
| 242 poll_timer_.Stop(); | 279 poll_timer_.Stop(); |
| 243 } | 280 } |
| 244 } | 281 } |
| 245 | 282 |
| 246 } // namespace content | 283 } // namespace content |
| OLD | NEW |