Index: content/browser/media/audio_stream_monitor.cc |
diff --git a/content/browser/media/audio_stream_monitor.cc b/content/browser/media/audio_stream_monitor.cc |
index bab834778a95e7a702d31e37174cb46003d5b183..2732f8d7cc5c5116a2929a8fdf5bc4e0489b3be6 100644 |
--- a/content/browser/media/audio_stream_monitor.cc |
+++ b/content/browser/media/audio_stream_monitor.cc |
@@ -6,10 +6,11 @@ |
#include "base/bind.h" |
#include "base/bind_helpers.h" |
+#include "base/containers/flat_map.h" |
+#include "content/browser/frame_host/render_frame_host_impl.h" |
#include "content/browser/web_contents/web_contents_impl.h" |
#include "content/public/browser/browser_thread.h" |
#include "content/public/browser/invalidate_type.h" |
-#include "content/public/browser/render_frame_host.h" |
namespace content { |
@@ -29,13 +30,6 @@ AudioStreamMonitor* StartStopMonitoringHelper(ActionType action_type, |
if (!render_process_host) |
return nullptr; |
- // TODO(dalecurtis, maxmorin): We should really only be sending these when the |
- // streams are audible or we don't have power level monitoring. |
- if (action_type == ActionType::STARTING) |
- render_process_host->OnAudioStreamAdded(); |
- else |
- render_process_host->OnAudioStreamRemoved(); |
- |
WebContentsImpl* const web_contents = |
static_cast<WebContentsImpl*>(WebContents::FromRenderFrameHost( |
RenderFrameHost::FromID(render_process_id, render_frame_id))); |
@@ -72,9 +66,8 @@ void AudioStreamMonitor::RenderProcessGone(int render_process_id) { |
// reused. During this period StartStopMonitoringHelper() will be unable to |
// lookup the WebContents using the now-dead |render_frame_id|. We must thus |
// have this secondary mechanism for clearing stale callbacks. |
- |
for (auto it = poll_callbacks_.begin(); it != poll_callbacks_.end();) { |
- if (it->first.first == render_process_id) { |
+ if (std::get<0>(it->first) == render_process_id) { |
it = poll_callbacks_.erase(it); |
OnStreamRemoved(); |
} else { |
@@ -122,8 +115,8 @@ void AudioStreamMonitor::StartMonitoringHelper( |
DCHECK_CURRENTLY_ON(BrowserThread::UI); |
if (AudioStreamMonitor* monitor = StartStopMonitoringHelper( |
ActionType::STARTING, render_process_id, render_frame_id)) { |
- monitor->StartMonitoringStreamOnUIThread(render_process_id, stream_id, |
- read_power_callback); |
+ monitor->StartMonitoringStreamOnUIThread(render_process_id, render_frame_id, |
+ stream_id, read_power_callback); |
} |
} |
@@ -134,34 +127,55 @@ void AudioStreamMonitor::StopMonitoringHelper(int render_process_id, |
DCHECK_CURRENTLY_ON(BrowserThread::UI); |
if (AudioStreamMonitor* monitor = StartStopMonitoringHelper( |
ActionType::STOPPING, render_process_id, render_frame_id)) { |
- monitor->StopMonitoringStreamOnUIThread(render_process_id, stream_id); |
+ monitor->StopMonitoringStreamOnUIThread(render_process_id, render_frame_id, |
+ stream_id); |
} |
} |
void AudioStreamMonitor::StartMonitoringStreamOnUIThread( |
int render_process_id, |
+ int render_frame_id, |
int stream_id, |
const ReadPowerAndClipCallback& read_power_callback) { |
DCHECK(thread_checker_.CalledOnValidThread()); |
DCHECK(!read_power_callback.is_null()); |
- const StreamID qualified_id(render_process_id, stream_id); |
+ const StreamID qualified_id(render_process_id, render_frame_id, stream_id); |
DCHECK(poll_callbacks_.find(qualified_id) == poll_callbacks_.end()); |
poll_callbacks_[qualified_id] = read_power_callback; |
+ // 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.
|
+ // monitoring, otherwise sends the signal when the stream becomes audible. |
+ if (!power_level_monitoring_available()) { |
+ if (auto* render_frame_host = static_cast<RenderFrameHostImpl*>( |
+ RenderFrameHost::FromID(render_process_id, render_frame_id))) { |
+ render_frame_host->OnAudioStateChanged(true); |
+ } |
+ } |
DaleCurtis
2017/06/22 00:35:55
Line below.
lpy
2017/06/22 02:28:52
Done.
|
OnStreamAdded(); |
} |
void AudioStreamMonitor::StopMonitoringStreamOnUIThread(int render_process_id, |
+ int render_frame_id, |
int stream_id) { |
DCHECK(thread_checker_.CalledOnValidThread()); |
// In the event of render process death, these may have already been cleared. |
- auto it = poll_callbacks_.find(StreamID(render_process_id, stream_id)); |
+ auto it = poll_callbacks_.find( |
+ StreamID(render_process_id, render_frame_id, stream_id)); |
if (it == poll_callbacks_.end()) |
return; |
poll_callbacks_.erase(it); |
+ // 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.
|
+ // monitoring, otherwise sends the signal when the stream becomes non-audible. |
+ if (!power_level_monitoring_available()) { |
+ if (auto* render_frame_host = static_cast<RenderFrameHostImpl*>( |
+ RenderFrameHost::FromID(render_process_id, render_frame_id))) { |
+ render_frame_host->OnAudioStateChanged(false); |
+ } |
+ } |
+ |
OnStreamRemoved(); |
} |
@@ -169,20 +183,41 @@ void AudioStreamMonitor::Poll() { |
bool was_audible = is_audible_; |
is_audible_ = false; |
+ // Record whether or not a RenderFrameHost is audible. |
+ base::flat_map<RenderFrameHostImpl*, bool> audible_frame_map; |
+ audible_frame_map.reserve(poll_callbacks_.size()); |
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.
|
- it != poll_callbacks_.end(); |
- ++it) { |
+ it != poll_callbacks_.end(); ++it) { |
// TODO(miu): A new UI for delivering specific power level and clipping |
// information is still in the works. For now, we throw away all |
// information except for "is it audible?" |
const float power_dbfs = it->second.Run().first; |
const float kSilenceThresholdDBFS = -72.24719896f; |
- if (power_dbfs >= kSilenceThresholdDBFS) { |
+ 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.
|
+ if (!is_audible_ && is_stream_audible) { |
last_blurt_time_ = clock_->NowTicks(); |
is_audible_ = true; |
MaybeToggle(); |
- break; // No need to poll remaining streams. |
+ } |
+ |
+ // Record whether or not the RenderFrame is audible, a RenderFrame is |
+ // audible when there is a audio stream in it that is audible. |
+ auto* render_frame_host_impl = |
+ static_cast<RenderFrameHostImpl*>(RenderFrameHost::FromID( |
+ std::get<0>(it->first), std::get<1>(it->first))); |
+ // 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
|
+ if (!render_frame_host_impl) |
+ continue; |
+ audible_frame_map[render_frame_host_impl] |= is_stream_audible; |
+ } |
+ |
+ // Update RenderFrameHost audible state only when state changed. |
+ for (auto& kv : audible_frame_map) { |
+ auto* render_frame_host_impl = kv.first; |
+ bool is_frame_audible = kv.second; |
+ 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.
|
+ render_frame_host_impl->OnAudioStateChanged(is_frame_audible); |
} |
} |