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

Unified Diff: chrome/browser/media/audio_stream_indicator.cc

Issue 14600025: Replace AudioSilenceDetector with an AudioPowerMonitor. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Replace RMS scheme with 1st-order low-pass filter, per crogers@. Simpler, single-threaded unit tes… Created 7 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: chrome/browser/media/audio_stream_indicator.cc
diff --git a/chrome/browser/media/audio_stream_indicator.cc b/chrome/browser/media/audio_stream_indicator.cc
index 6cd623212e72fcf8dcc722241af868ec291d5755..a66f8d936107c1baa9b4535b0170475527d80929 100644
--- a/chrome/browser/media/audio_stream_indicator.cc
+++ b/chrome/browser/media/audio_stream_indicator.cc
@@ -4,6 +4,8 @@
#include "chrome/browser/media/audio_stream_indicator.h"
+#include <limits>
+
#include "base/bind.h"
#include "chrome/browser/tab_contents/tab_util.h"
#include "content/public/browser/browser_thread.h"
@@ -20,22 +22,66 @@ AudioStreamIndicator::~AudioStreamIndicator() {}
void AudioStreamIndicator::UpdateWebContentsStatus(
int render_process_id, int render_view_id, int stream_id,
- bool is_playing_and_audible) {
+ bool is_playing, float power_dBFS, bool clipped) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::Bind(&AudioStreamIndicator::UpdateWebContentsStatusOnUIThread, this,
render_process_id, render_view_id, stream_id,
- is_playing_and_audible));
+ is_playing, power_dBFS, clipped));
}
-bool AudioStreamIndicator::IsPlayingAudio(WebContents* contents) {
+bool AudioStreamIndicator::IsPlayingAudio(const WebContents* contents) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+ // TODO(miu): In order to prevent breaking existing uses of this method, the
+ // old semantics of "playing AND not silent" have been retained here. Once
+ // the tab audio indicator UI switches over to using the new
+ // GetAudioSignalPower(), this method should really be just "playing."
+ float level;
+ bool ignored;
+ CurrentAudibleLevel(contents, &level, &ignored);
+ return level > 0.0f;
+}
+
+void AudioStreamIndicator::CurrentAudibleLevel(
+ const content::WebContents* contents, float* level, bool* clipped) {
+ DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
+
+ float max_power_dBFS = -std::numeric_limits<float>::infinity();
+ bool has_clipped = false;
- RenderViewId id(contents->GetRenderProcessHost()->GetID(),
- contents->GetRenderViewHost()->GetRoutingID());
- return audio_streams_.find(id) != audio_streams_.end();
+ // Since a RenderView can have more than one stream playing back, return the
+ // maximum of the last-reported power levels. For more information about how
+ // the power level is measured, see media/audio/audio_power_monitor.h.
+ const RenderViewId id(contents->GetRenderProcessHost()->GetID(),
+ contents->GetRenderViewHost()->GetRoutingID());
+ RenderViewStreamMap::iterator stream_it = audio_streams_.find(id);
+ if (stream_it != audio_streams_.end()) {
+ StreamPowerLevelMap& power_levels = stream_it->second;
+ for (StreamPowerLevelMap::iterator power_it = power_levels.begin();
+ power_it != power_levels.end(); ++power_it) {
+ SignalPower& stream_power = power_it->second;
+ if (stream_power.power_dBFS > max_power_dBFS)
+ max_power_dBFS = stream_power.power_dBFS;
+ if (stream_power.clipped) {
+ has_clipped = true;
+ // Clear the flag now that we'll be reporting the clipping.
+ stream_power.clipped = false;
+ }
+ }
+ }
+
+ // Map the power into an "audible level" in the range [0.0,1.0]. dBFS values
+ // are in the range -inf (minimum power) to 0.0 (maximum power).
+ static const float kSilenceThresholdDBFS = -72.24719896f;
+ if (max_power_dBFS < kSilenceThresholdDBFS)
+ *level = 0.0f;
+ else if (max_power_dBFS > 0.0f)
+ *level = 1.0f;
+ else
+ *level = 1.0f - max_power_dBFS / kSilenceThresholdDBFS;
+ *clipped = has_clipped;
}
AudioStreamIndicator::RenderViewId::RenderViewId(int render_process_id,
@@ -53,23 +99,25 @@ bool AudioStreamIndicator::RenderViewId::operator<(
}
void AudioStreamIndicator::UpdateWebContentsStatusOnUIThread(
- int render_process_id,
- int render_view_id,
- int stream_id,
- bool is_playing_and_audible) {
+ int render_process_id, int render_view_id, int stream_id,
+ bool is_playing, float power_dBFS, bool clipped) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
- RenderViewId id(render_process_id, render_view_id);
- if (is_playing_and_audible) {
- audio_streams_[id].insert(stream_id);
+ const RenderViewId id(render_process_id, render_view_id);
+ if (is_playing) {
+ SignalPower& stream_power = audio_streams_[id][stream_id];
+ stream_power.power_dBFS = power_dBFS;
+ stream_power.clipped |= clipped;
} else {
- std::map<RenderViewId, std::set<int> >::iterator it =
- audio_streams_.find(id);
- if (it == audio_streams_.end())
+ RenderViewStreamMap::iterator stream_it = audio_streams_.find(id);
+ if (stream_it == audio_streams_.end())
return;
-
- it->second.erase(stream_id);
- if (it->second.empty())
- audio_streams_.erase(it);
+ StreamPowerLevelMap& power_levels = stream_it->second;
+ StreamPowerLevelMap::iterator power_it = power_levels.find(stream_id);
+ if (power_it == power_levels.end())
+ return;
+ power_levels.erase(power_it);
+ if (power_levels.empty())
+ audio_streams_.erase(stream_it);
}
WebContents* web_contents = tab_util::GetWebContentsByID(render_process_id,

Powered by Google App Engine
This is Rietveld 408576698