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 "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 |
| 47 AudioStreamMonitor::AudioStreamMonitor(WebContents* contents) | 41 AudioStreamMonitor::AudioStreamMonitor(WebContents* contents) |
| 48 : web_contents_(contents), | 42 : web_contents_(contents), |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 65 } | 59 } |
| 66 | 60 |
| 67 void AudioStreamMonitor::RenderProcessGone(int render_process_id) { | 61 void AudioStreamMonitor::RenderProcessGone(int render_process_id) { |
| 68 DCHECK(thread_checker_.CalledOnValidThread()); | 62 DCHECK(thread_checker_.CalledOnValidThread()); |
| 69 | 63 |
| 70 // Note: It's possible for the RenderProcessHost and WebContents (and thus | 64 // 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 | 65 // this class) to survive the death of the render process and subsequently be |
| 72 // reused. During this period StartStopMonitoringHelper() will be unable to | 66 // reused. During this period StartStopMonitoringHelper() will be unable to |
| 73 // lookup the WebContents using the now-dead |render_frame_id|. We must thus | 67 // lookup the WebContents using the now-dead |render_frame_id|. We must thus |
| 74 // have this secondary mechanism for clearing stale callbacks. | 68 // have this secondary mechanism for clearing stale callbacks. |
| 75 | |
| 76 for (auto it = poll_callbacks_.begin(); it != poll_callbacks_.end();) { | 69 for (auto it = poll_callbacks_.begin(); it != poll_callbacks_.end();) { |
| 77 if (it->first.first == render_process_id) { | 70 if (std::get<0>(it->first) == render_process_id) { |
| 78 it = poll_callbacks_.erase(it); | 71 it = poll_callbacks_.erase(it); |
| 79 OnStreamRemoved(); | 72 OnStreamRemoved(); |
| 80 } else { | 73 } else { |
| 81 ++it; | 74 ++it; |
| 82 } | 75 } |
| 83 } | 76 } |
| 84 | 77 |
| 85 if (poll_callbacks_.empty()) | 78 if (poll_callbacks_.empty()) |
| 86 poll_timer_.Stop(); | 79 poll_timer_.Stop(); |
| 87 } | 80 } |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 115 | 108 |
| 116 // static | 109 // static |
| 117 void AudioStreamMonitor::StartMonitoringHelper( | 110 void AudioStreamMonitor::StartMonitoringHelper( |
| 118 int render_process_id, | 111 int render_process_id, |
| 119 int render_frame_id, | 112 int render_frame_id, |
| 120 int stream_id, | 113 int stream_id, |
| 121 const ReadPowerAndClipCallback& read_power_callback) { | 114 const ReadPowerAndClipCallback& read_power_callback) { |
| 122 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 115 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 123 if (AudioStreamMonitor* monitor = StartStopMonitoringHelper( | 116 if (AudioStreamMonitor* monitor = StartStopMonitoringHelper( |
| 124 ActionType::STARTING, render_process_id, render_frame_id)) { | 117 ActionType::STARTING, render_process_id, render_frame_id)) { |
| 125 monitor->StartMonitoringStreamOnUIThread(render_process_id, stream_id, | 118 monitor->StartMonitoringStreamOnUIThread(render_process_id, render_frame_id, |
| 126 read_power_callback); | 119 stream_id, read_power_callback); |
| 127 } | 120 } |
| 128 } | 121 } |
| 129 | 122 |
| 130 // static | 123 // static |
| 131 void AudioStreamMonitor::StopMonitoringHelper(int render_process_id, | 124 void AudioStreamMonitor::StopMonitoringHelper(int render_process_id, |
| 132 int render_frame_id, | 125 int render_frame_id, |
| 133 int stream_id) { | 126 int stream_id) { |
| 134 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 127 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 135 if (AudioStreamMonitor* monitor = StartStopMonitoringHelper( | 128 if (AudioStreamMonitor* monitor = StartStopMonitoringHelper( |
| 136 ActionType::STOPPING, render_process_id, render_frame_id)) { | 129 ActionType::STOPPING, render_process_id, render_frame_id)) { |
| 137 monitor->StopMonitoringStreamOnUIThread(render_process_id, stream_id); | 130 monitor->StopMonitoringStreamOnUIThread(render_process_id, render_frame_id, |
| 131 stream_id); | |
| 138 } | 132 } |
| 139 } | 133 } |
| 140 | 134 |
| 141 void AudioStreamMonitor::StartMonitoringStreamOnUIThread( | 135 void AudioStreamMonitor::StartMonitoringStreamOnUIThread( |
| 142 int render_process_id, | 136 int render_process_id, |
| 137 int render_frame_id, | |
| 143 int stream_id, | 138 int stream_id, |
| 144 const ReadPowerAndClipCallback& read_power_callback) { | 139 const ReadPowerAndClipCallback& read_power_callback) { |
| 145 DCHECK(thread_checker_.CalledOnValidThread()); | 140 DCHECK(thread_checker_.CalledOnValidThread()); |
| 146 DCHECK(!read_power_callback.is_null()); | 141 DCHECK(!read_power_callback.is_null()); |
| 147 | 142 |
| 148 const StreamID qualified_id(render_process_id, stream_id); | 143 const StreamID qualified_id(render_process_id, render_frame_id, stream_id); |
| 149 DCHECK(poll_callbacks_.find(qualified_id) == poll_callbacks_.end()); | 144 DCHECK(poll_callbacks_.find(qualified_id) == poll_callbacks_.end()); |
| 150 | 145 |
| 151 poll_callbacks_[qualified_id] = read_power_callback; | 146 poll_callbacks_[qualified_id] = read_power_callback; |
| 147 // Sends audible signal to RenderFrameHost when there is no power level | |
|
DaleCurtis
2017/06/22 00:35:55
Line above.
lpy
2017/06/22 02:28:52
Done.
| |
| 148 // monitoring, otherwise sends the signal when the stream becomes audible. | |
| 149 if (!power_level_monitoring_available()) { | |
| 150 if (auto* render_frame_host = static_cast<RenderFrameHostImpl*>( | |
| 151 RenderFrameHost::FromID(render_process_id, render_frame_id))) { | |
| 152 render_frame_host->OnAudioStateChanged(true); | |
| 153 } | |
| 154 } | |
|
DaleCurtis
2017/06/22 00:35:55
Line below.
lpy
2017/06/22 02:28:52
Done.
| |
| 152 OnStreamAdded(); | 155 OnStreamAdded(); |
| 153 } | 156 } |
| 154 | 157 |
| 155 void AudioStreamMonitor::StopMonitoringStreamOnUIThread(int render_process_id, | 158 void AudioStreamMonitor::StopMonitoringStreamOnUIThread(int render_process_id, |
| 159 int render_frame_id, | |
| 156 int stream_id) { | 160 int stream_id) { |
| 157 DCHECK(thread_checker_.CalledOnValidThread()); | 161 DCHECK(thread_checker_.CalledOnValidThread()); |
| 158 | 162 |
| 159 // In the event of render process death, these may have already been cleared. | 163 // 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)); | 164 auto it = poll_callbacks_.find( |
| 165 StreamID(render_process_id, render_frame_id, stream_id)); | |
| 161 if (it == poll_callbacks_.end()) | 166 if (it == poll_callbacks_.end()) |
| 162 return; | 167 return; |
| 163 | 168 |
| 164 poll_callbacks_.erase(it); | 169 poll_callbacks_.erase(it); |
| 170 // Sends non-audible signal to RenderFrameHost when there is no power level | |
|
DaleCurtis
2017/06/22 00:35:55
Line above.
lpy
2017/06/22 02:28:52
Done.
| |
| 171 // monitoring, otherwise sends the signal when the stream becomes non-audible. | |
| 172 if (!power_level_monitoring_available()) { | |
| 173 if (auto* render_frame_host = static_cast<RenderFrameHostImpl*>( | |
| 174 RenderFrameHost::FromID(render_process_id, render_frame_id))) { | |
| 175 render_frame_host->OnAudioStateChanged(false); | |
| 176 } | |
| 177 } | |
| 178 | |
| 165 OnStreamRemoved(); | 179 OnStreamRemoved(); |
| 166 } | 180 } |
| 167 | 181 |
| 168 void AudioStreamMonitor::Poll() { | 182 void AudioStreamMonitor::Poll() { |
| 169 bool was_audible = is_audible_; | 183 bool was_audible = is_audible_; |
| 170 is_audible_ = false; | 184 is_audible_ = false; |
| 171 | 185 |
| 186 // Record whether or not a RenderFrameHost is audible. | |
| 187 base::flat_map<RenderFrameHostImpl*, bool> audible_frame_map; | |
| 188 audible_frame_map.reserve(poll_callbacks_.size()); | |
| 172 for (StreamPollCallbackMap::const_iterator it = poll_callbacks_.begin(); | 189 for (StreamPollCallbackMap::const_iterator it = poll_callbacks_.begin(); |
|
DaleCurtis
2017/06/22 00:35:55
We don't seem to be actually using the iterator, s
lpy
2017/06/22 02:28:52
Done.
| |
| 173 it != poll_callbacks_.end(); | 190 it != poll_callbacks_.end(); ++it) { |
| 174 ++it) { | |
| 175 // TODO(miu): A new UI for delivering specific power level and clipping | 191 // 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 | 192 // information is still in the works. For now, we throw away all |
| 177 // information except for "is it audible?" | 193 // information except for "is it audible?" |
| 178 const float power_dbfs = it->second.Run().first; | 194 const float power_dbfs = it->second.Run().first; |
| 179 const float kSilenceThresholdDBFS = -72.24719896f; | 195 const float kSilenceThresholdDBFS = -72.24719896f; |
| 180 | 196 |
| 181 if (power_dbfs >= kSilenceThresholdDBFS) { | 197 bool is_stream_audible = power_dbfs >= kSilenceThresholdDBFS; |
|
DaleCurtis
2017/06/22 00:35:54
const for consistency.
lpy
2017/06/22 02:28:52
Done.
| |
| 198 if (!is_audible_ && is_stream_audible) { | |
| 182 last_blurt_time_ = clock_->NowTicks(); | 199 last_blurt_time_ = clock_->NowTicks(); |
| 183 is_audible_ = true; | 200 is_audible_ = true; |
| 184 MaybeToggle(); | 201 MaybeToggle(); |
| 185 break; // No need to poll remaining streams. | 202 } |
| 203 | |
| 204 // Record whether or not the RenderFrame is audible, a RenderFrame is | |
| 205 // audible when there is a audio stream in it that is audible. | |
| 206 auto* render_frame_host_impl = | |
| 207 static_cast<RenderFrameHostImpl*>(RenderFrameHost::FromID( | |
| 208 std::get<0>(it->first), std::get<1>(it->first))); | |
| 209 // This may be nullptr in tests. | |
|
DaleCurtis
2017/06/22 00:35:55
Which tests? It'd be better to avoid this conditio
lpy
2017/06/22 02:28:52
AudioStreamMonitorTest.HandlesMultipleStreamsBlurt
DaleCurtis
2017/06/22 03:15:40
Ah, probably they shouldn't do that, or should set
| |
| 210 if (!render_frame_host_impl) | |
| 211 continue; | |
| 212 audible_frame_map[render_frame_host_impl] |= is_stream_audible; | |
| 213 } | |
| 214 | |
| 215 // Update RenderFrameHost audible state only when state changed. | |
| 216 for (auto& kv : audible_frame_map) { | |
| 217 auto* render_frame_host_impl = kv.first; | |
| 218 bool is_frame_audible = kv.second; | |
| 219 if (is_frame_audible != render_frame_host_impl->IsAudible()) { | |
|
DaleCurtis
2017/06/22 00:35:55
No {} for single line conditionals.
lpy
2017/06/22 02:28:52
Done.
| |
| 220 render_frame_host_impl->OnAudioStateChanged(is_frame_audible); | |
| 186 } | 221 } |
| 187 } | 222 } |
| 188 | 223 |
| 189 if (is_audible_ != was_audible) | 224 if (is_audible_ != was_audible) |
| 190 web_contents_->OnAudioStateChanged(is_audible_); | 225 web_contents_->OnAudioStateChanged(is_audible_); |
| 191 } | 226 } |
| 192 | 227 |
| 193 void AudioStreamMonitor::MaybeToggle() { | 228 void AudioStreamMonitor::MaybeToggle() { |
| 194 const bool indicator_was_on = was_recently_audible_; | 229 const bool indicator_was_on = was_recently_audible_; |
| 195 const base::TimeTicks off_time = | 230 const base::TimeTicks off_time = |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 237 if (!power_level_monitoring_available()) { | 272 if (!power_level_monitoring_available()) { |
| 238 is_audible_ = false; | 273 is_audible_ = false; |
| 239 web_contents_->OnAudioStateChanged(false); | 274 web_contents_->OnAudioStateChanged(false); |
| 240 MaybeToggle(); | 275 MaybeToggle(); |
| 241 } else { | 276 } else { |
| 242 poll_timer_.Stop(); | 277 poll_timer_.Stop(); |
| 243 } | 278 } |
| 244 } | 279 } |
| 245 | 280 |
| 246 } // namespace content | 281 } // namespace content |
| OLD | NEW |