OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "chrome/browser/media/audio_stream_indicator.h" | 5 #include "chrome/browser/media/audio_stream_indicator.h" |
6 | 6 |
| 7 #include <limits> |
| 8 |
7 #include "base/bind.h" | 9 #include "base/bind.h" |
8 #include "chrome/browser/tab_contents/tab_util.h" | 10 #include "chrome/browser/tab_contents/tab_util.h" |
9 #include "content/public/browser/browser_thread.h" | 11 #include "content/public/browser/browser_thread.h" |
10 #include "content/public/browser/invalidate_type.h" | 12 #include "content/public/browser/invalidate_type.h" |
11 #include "content/public/browser/render_process_host.h" | 13 #include "content/public/browser/render_process_host.h" |
12 #include "content/public/browser/render_view_host.h" | 14 #include "content/public/browser/render_view_host.h" |
13 #include "content/public/browser/web_contents.h" | 15 #include "content/public/browser/web_contents.h" |
14 | 16 |
15 using content::BrowserThread; | 17 using content::BrowserThread; |
16 using content::WebContents; | 18 using content::WebContents; |
17 | 19 |
18 AudioStreamIndicator::AudioStreamIndicator() {} | 20 AudioStreamIndicator::AudioStreamIndicator() {} |
19 AudioStreamIndicator::~AudioStreamIndicator() {} | 21 AudioStreamIndicator::~AudioStreamIndicator() {} |
20 | 22 |
21 void AudioStreamIndicator::UpdateWebContentsStatus( | 23 void AudioStreamIndicator::UpdateWebContentsStatus( |
22 int render_process_id, int render_view_id, int stream_id, | 24 int render_process_id, int render_view_id, int stream_id, |
23 bool is_playing_and_audible) { | 25 bool is_playing, float power_dbfs, bool clipped) { |
24 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 26 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
25 | 27 |
26 BrowserThread::PostTask( | 28 BrowserThread::PostTask( |
27 BrowserThread::UI, FROM_HERE, | 29 BrowserThread::UI, FROM_HERE, |
28 base::Bind(&AudioStreamIndicator::UpdateWebContentsStatusOnUIThread, this, | 30 base::Bind(&AudioStreamIndicator::UpdateWebContentsStatusOnUIThread, this, |
29 render_process_id, render_view_id, stream_id, | 31 render_process_id, render_view_id, stream_id, |
30 is_playing_and_audible)); | 32 is_playing, power_dbfs, clipped)); |
31 } | 33 } |
32 | 34 |
33 bool AudioStreamIndicator::IsPlayingAudio(WebContents* contents) { | 35 bool AudioStreamIndicator::IsPlayingAudio(const WebContents* contents) { |
34 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 36 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
35 | 37 |
36 RenderViewId id(contents->GetRenderProcessHost()->GetID(), | 38 // TODO(miu): In order to prevent breaking existing uses of this method, the |
37 contents->GetRenderViewHost()->GetRoutingID()); | 39 // old semantics of "playing AND not silent" have been retained here. Once |
38 return audio_streams_.find(id) != audio_streams_.end(); | 40 // the tab audio indicator UI switches over to using the new |
| 41 // GetAudioSignalPower(), this method should really be just "playing." |
| 42 float level; |
| 43 bool ignored; |
| 44 CurrentAudibleLevel(contents, &level, &ignored); |
| 45 return level > 0.0f; |
39 } | 46 } |
40 | 47 |
41 AudioStreamIndicator::RenderViewId::RenderViewId(int render_process_id, | 48 void AudioStreamIndicator::CurrentAudibleLevel( |
42 int render_view_id) | 49 const content::WebContents* contents, float* level, bool* clipped) { |
43 : render_process_id(render_process_id), | 50 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
44 render_view_id(render_view_id) { | |
45 } | |
46 | 51 |
47 bool AudioStreamIndicator::RenderViewId::operator<( | 52 float max_power_dbfs = -std::numeric_limits<float>::infinity(); |
48 const RenderViewId& other) const { | 53 bool has_clipped = false; |
49 if (render_process_id != other.render_process_id) | |
50 return render_process_id < other.render_process_id; | |
51 | 54 |
52 return render_view_id < other.render_view_id; | 55 // Since a RenderView can have more than one stream playing back, return the |
| 56 // maximum of the last-reported power levels. For more information about how |
| 57 // the power level is measured, see media/audio/audio_power_monitor.h. |
| 58 const RenderViewId id(contents->GetRenderProcessHost()->GetID(), |
| 59 contents->GetRenderViewHost()->GetRoutingID()); |
| 60 RenderViewStreamMap::const_iterator view_it = audio_streams_.find(id); |
| 61 if (view_it != audio_streams_.end()) { |
| 62 const StreamPowerLevels& stream_levels = view_it->second; |
| 63 for (StreamPowerLevels::const_iterator stream_it = stream_levels.begin(); |
| 64 stream_it != stream_levels.end(); ++stream_it) { |
| 65 if (stream_it->power_dbfs > max_power_dbfs) |
| 66 max_power_dbfs = stream_it->power_dbfs; |
| 67 has_clipped |= stream_it->clipped; |
| 68 } |
| 69 } |
| 70 |
| 71 // Map the power into an "audible level" in the range [0.0,1.0]. dBFS values |
| 72 // are in the range -inf (minimum power) to 0.0 (maximum power). |
| 73 static const float kSilenceThresholdDBFS = -72.24719896f; |
| 74 if (max_power_dbfs < kSilenceThresholdDBFS) |
| 75 *level = 0.0f; |
| 76 else if (max_power_dbfs > 0.0f) |
| 77 *level = 1.0f; |
| 78 else |
| 79 *level = 1.0f - max_power_dbfs / kSilenceThresholdDBFS; |
| 80 *clipped = has_clipped; |
53 } | 81 } |
54 | 82 |
55 void AudioStreamIndicator::UpdateWebContentsStatusOnUIThread( | 83 void AudioStreamIndicator::UpdateWebContentsStatusOnUIThread( |
56 int render_process_id, | 84 int render_process_id, int render_view_id, int stream_id, |
57 int render_view_id, | 85 bool is_playing, float power_dbfs, bool clipped) { |
58 int stream_id, | |
59 bool is_playing_and_audible) { | |
60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 86 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
61 RenderViewId id(render_process_id, render_view_id); | 87 |
62 if (is_playing_and_audible) { | 88 const RenderViewId id(render_process_id, render_view_id); |
63 audio_streams_[id].insert(stream_id); | 89 if (is_playing) { |
| 90 // Find the StreamPowerLevel instance associated with |stream_id|, or |
| 91 // auto-create a new one. |
| 92 StreamPowerLevels& stream_levels = audio_streams_[id]; |
| 93 StreamPowerLevels::iterator stream_it; |
| 94 for (stream_it = stream_levels.begin(); stream_it != stream_levels.end(); |
| 95 ++stream_it) { |
| 96 if (stream_it->stream_id == stream_id) |
| 97 break; |
| 98 } |
| 99 if (stream_it == stream_levels.end()) { |
| 100 stream_it = stream_levels.insert(stream_levels.end(), StreamPowerLevel()); |
| 101 stream_it->stream_id = stream_id; |
| 102 } |
| 103 |
| 104 // Update power and clip values. |
| 105 stream_it->power_dbfs = power_dbfs; |
| 106 stream_it->clipped = clipped; |
64 } else { | 107 } else { |
65 std::map<RenderViewId, std::set<int> >::iterator it = | 108 // Find and erase the StreamPowerLevel instance associated with |stream_id|. |
66 audio_streams_.find(id); | 109 RenderViewStreamMap::iterator view_it = audio_streams_.find(id); |
67 if (it == audio_streams_.end()) | 110 if (view_it != audio_streams_.end()) { |
68 return; | 111 StreamPowerLevels& stream_levels = view_it->second; |
69 | 112 for (StreamPowerLevels::iterator stream_it = stream_levels.begin(); |
70 it->second.erase(stream_id); | 113 stream_it != stream_levels.end(); ++stream_it) { |
71 if (it->second.empty()) | 114 if (stream_it->stream_id == stream_id) { |
72 audio_streams_.erase(it); | 115 stream_levels.erase(stream_it); |
| 116 if (stream_levels.empty()) |
| 117 audio_streams_.erase(view_it); |
| 118 break; |
| 119 } |
| 120 } |
| 121 } |
73 } | 122 } |
74 | 123 |
| 124 // Trigger UI update. |
75 WebContents* web_contents = tab_util::GetWebContentsByID(render_process_id, | 125 WebContents* web_contents = tab_util::GetWebContentsByID(render_process_id, |
76 render_view_id); | 126 render_view_id); |
77 if (web_contents) | 127 if (web_contents) |
78 web_contents->NotifyNavigationStateChanged(content::INVALIDATE_TYPE_TAB); | 128 web_contents->NotifyNavigationStateChanged(content::INVALIDATE_TYPE_TAB); |
79 } | 129 } |
OLD | NEW |