Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(309)

Unified Diff: content/browser/media/audio_stream_monitor.cc

Issue 2948613002: [AudioStreamMonitor] Adds API to collect frame-level audibility. (Closed)
Patch Set: Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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..bc0cd4333f8f022c8010ce2fcd7f55e860fb9e28 100644
--- a/content/browser/media/audio_stream_monitor.cc
+++ b/content/browser/media/audio_stream_monitor.cc
@@ -4,6 +4,8 @@
#include "content/browser/media/audio_stream_monitor.h"
+#include <unordered_map>
+
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "content/browser/web_contents/web_contents_impl.h"
@@ -29,13 +31,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)));
@@ -74,7 +69,7 @@ void AudioStreamMonitor::RenderProcessGone(int render_process_id) {
// 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 +117,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 +129,48 @@ 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;
+ // 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.
+ // power level monitoring, otherwise we should send the signal when the stream
+ // becomes non-audible.
+ if (!power_level_monitoring_available())
+ 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.
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);
+ // 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.
+ // power level monitoring, otherwise we should send the signal when the stream
+ // becomes non-audible.
+ if (!power_level_monitoring_available())
+ RenderProcessHost::FromID(render_process_id)->OnAudioStreamRemoved();
OnStreamRemoved();
}
@@ -169,20 +178,48 @@ void AudioStreamMonitor::Poll() {
bool was_audible = is_audible_;
is_audible_ = false;
+ // Record whether or not a RenderFrameHost is audible.
+ 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
for (StreamPollCallbackMap::const_iterator it = poll_callbacks_.begin();
- 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;
+ 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 =
+ RenderFrameHost::FromID(std::get<0>(it->first), std::get<1>(it->first));
+ if (!render_frame_host)
+ continue;
+ if (is_frame_audible.find(render_frame_host) == is_frame_audible.end()) {
+ is_frame_audible[render_frame_host] = is_stream_audible;
+ } else {
+ is_frame_audible[render_frame_host] |= is_stream_audible;
+ }
+ }
+
+ // Update RenderFrameHost and RenderProcessHost audible state only when state
+ // changed.
+ for (auto it = is_frame_audible.begin(); it != is_frame_audible.end(); ++it) {
+ auto* render_frame_host = it->first;
+ bool is_frame_audible = it->second;
+ if (is_frame_audible != render_frame_host->IsAudible()) {
+ render_frame_host->OnAudioStateChanged(is_frame_audible);
+ if (is_frame_audible) {
+ render_frame_host->GetProcess()->OnAudioStreamAdded();
+ } else {
+ render_frame_host->GetProcess()->OnAudioStreamRemoved();
+ }
}
}
« no previous file with comments | « content/browser/media/audio_stream_monitor.h ('k') | content/browser/media/audio_stream_monitor_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698