| 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..1d3aa7216486273129a76b8e846852884ee6ad20 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,58 +22,106 @@ 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));
|
|
|
| - RenderViewId id(contents->GetRenderProcessHost()->GetID(),
|
| - contents->GetRenderViewHost()->GetRoutingID());
|
| - return audio_streams_.find(id) != audio_streams_.end();
|
| + // 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;
|
| }
|
|
|
| -AudioStreamIndicator::RenderViewId::RenderViewId(int render_process_id,
|
| - int render_view_id)
|
| - : render_process_id(render_process_id),
|
| - render_view_id(render_view_id) {
|
| -}
|
| +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;
|
|
|
| -bool AudioStreamIndicator::RenderViewId::operator<(
|
| - const RenderViewId& other) const {
|
| - if (render_process_id != other.render_process_id)
|
| - return render_process_id < other.render_process_id;
|
| + // 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::const_iterator view_it = audio_streams_.find(id);
|
| + if (view_it != audio_streams_.end()) {
|
| + const StreamPowerLevels& stream_levels = view_it->second;
|
| + for (StreamPowerLevels::const_iterator stream_it = stream_levels.begin();
|
| + stream_it != stream_levels.end(); ++stream_it) {
|
| + if (stream_it->power_dbfs > max_power_dbfs)
|
| + max_power_dbfs = stream_it->power_dbfs;
|
| + has_clipped |= stream_it->clipped;
|
| + }
|
| + }
|
|
|
| - return render_view_id < other.render_view_id;
|
| + // 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;
|
| }
|
|
|
| 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) {
|
| + // Find the StreamPowerLevel instance associated with |stream_id|, or
|
| + // auto-create a new one.
|
| + StreamPowerLevels& stream_levels = audio_streams_[id];
|
| + StreamPowerLevels::iterator stream_it;
|
| + for (stream_it = stream_levels.begin(); stream_it != stream_levels.end();
|
| + ++stream_it) {
|
| + if (stream_it->stream_id == stream_id)
|
| + break;
|
| + }
|
| + if (stream_it == stream_levels.end()) {
|
| + stream_it = stream_levels.insert(stream_levels.end(), StreamPowerLevel());
|
| + stream_it->stream_id = stream_id;
|
| + }
|
| +
|
| + // Update power and clip values.
|
| + stream_it->power_dbfs = power_dbfs;
|
| + stream_it->clipped = clipped;
|
| } else {
|
| - std::map<RenderViewId, std::set<int> >::iterator it =
|
| - audio_streams_.find(id);
|
| - if (it == audio_streams_.end())
|
| - return;
|
| -
|
| - it->second.erase(stream_id);
|
| - if (it->second.empty())
|
| - audio_streams_.erase(it);
|
| + // Find and erase the StreamPowerLevel instance associated with |stream_id|.
|
| + RenderViewStreamMap::iterator view_it = audio_streams_.find(id);
|
| + if (view_it != audio_streams_.end()) {
|
| + StreamPowerLevels& stream_levels = view_it->second;
|
| + for (StreamPowerLevels::iterator stream_it = stream_levels.begin();
|
| + stream_it != stream_levels.end(); ++stream_it) {
|
| + if (stream_it->stream_id == stream_id) {
|
| + stream_levels.erase(stream_it);
|
| + if (stream_levels.empty())
|
| + audio_streams_.erase(view_it);
|
| + break;
|
| + }
|
| + }
|
| + }
|
| }
|
|
|
| + // Trigger UI update.
|
| WebContents* web_contents = tab_util::GetWebContentsByID(render_process_id,
|
| render_view_id);
|
| if (web_contents)
|
|
|