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_in_dbfs) { |
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_in_dbfs)); |
31 } | 33 } |
32 | 34 |
33 bool AudioStreamIndicator::IsPlayingAudio(WebContents* contents) { | 35 bool AudioStreamIndicator::IsPlayingAudio(WebContents* contents) const { |
34 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 36 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
35 | 37 |
36 RenderViewId id(contents->GetRenderProcessHost()->GetID(), | 38 RenderViewId id(contents->GetRenderProcessHost()->GetID(), |
37 contents->GetRenderViewHost()->GetRoutingID()); | 39 contents->GetRenderViewHost()->GetRoutingID()); |
38 return audio_streams_.find(id) != audio_streams_.end(); | 40 return audio_streams_.find(id) != audio_streams_.end(); |
39 } | 41 } |
40 | 42 |
43 float AudioStreamIndicator::GetAudioPowerLevelInDBFS( | |
DaleCurtis
2013/05/16 18:24:45
Seems like there should be a simpler way to do thi
miu
2013/05/16 21:44:11
Acknowledged.
| |
44 content::WebContents* contents) const { | |
45 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
46 | |
47 // Since a RenderView can have more than one stream playing back, return the | |
48 // maximum of the last-reported power levels. For more information about how | |
49 // the power level is measured, see media/audio/audio_power_monitor.h. | |
50 float max_power_dbfs = -std::numeric_limits<float>::infinity(); | |
51 RenderViewId id(contents->GetRenderProcessHost()->GetID(), | |
52 contents->GetRenderViewHost()->GetRoutingID()); | |
53 RenderViewStreamMap::const_iterator stream_it = audio_streams_.find(id); | |
54 if (stream_it != audio_streams_.end()) { | |
55 const StreamPowerLevelMap& power_levels = stream_it->second; | |
56 for (StreamPowerLevelMap::const_iterator power_it = power_levels.begin(); | |
57 power_it != power_levels.end(); ++power_it) { | |
58 if (power_it->second > max_power_dbfs) | |
59 max_power_dbfs = power_it->second; | |
60 } | |
61 } | |
62 return max_power_dbfs; | |
63 } | |
64 | |
41 AudioStreamIndicator::RenderViewId::RenderViewId(int render_process_id, | 65 AudioStreamIndicator::RenderViewId::RenderViewId(int render_process_id, |
42 int render_view_id) | 66 int render_view_id) |
43 : render_process_id(render_process_id), | 67 : render_process_id(render_process_id), |
44 render_view_id(render_view_id) { | 68 render_view_id(render_view_id) { |
45 } | 69 } |
46 | 70 |
47 bool AudioStreamIndicator::RenderViewId::operator<( | 71 bool AudioStreamIndicator::RenderViewId::operator<( |
48 const RenderViewId& other) const { | 72 const RenderViewId& other) const { |
49 if (render_process_id != other.render_process_id) | 73 if (render_process_id != other.render_process_id) |
50 return render_process_id < other.render_process_id; | 74 return render_process_id < other.render_process_id; |
51 | 75 |
52 return render_view_id < other.render_view_id; | 76 return render_view_id < other.render_view_id; |
53 } | 77 } |
54 | 78 |
55 void AudioStreamIndicator::UpdateWebContentsStatusOnUIThread( | 79 void AudioStreamIndicator::UpdateWebContentsStatusOnUIThread( |
56 int render_process_id, | 80 int render_process_id, |
57 int render_view_id, | 81 int render_view_id, |
58 int stream_id, | 82 int stream_id, |
59 bool is_playing_and_audible) { | 83 bool is_playing, |
84 float power_in_dbfs) { | |
60 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 85 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
61 RenderViewId id(render_process_id, render_view_id); | 86 RenderViewId id(render_process_id, render_view_id); |
62 if (is_playing_and_audible) { | 87 if (is_playing) { |
63 audio_streams_[id].insert(stream_id); | 88 audio_streams_[id][stream_id] = power_in_dbfs; |
64 } else { | 89 } else { |
65 std::map<RenderViewId, std::set<int> >::iterator it = | 90 RenderViewStreamMap::iterator stream_it = audio_streams_.find(id); |
66 audio_streams_.find(id); | 91 if (stream_it == audio_streams_.end()) |
67 if (it == audio_streams_.end()) | |
68 return; | 92 return; |
69 | 93 StreamPowerLevelMap& power_levels = stream_it->second; |
70 it->second.erase(stream_id); | 94 StreamPowerLevelMap::iterator power_it = power_levels.find(stream_id); |
71 if (it->second.empty()) | 95 if (power_it == power_levels.end()) |
72 audio_streams_.erase(it); | 96 return; |
97 power_levels.erase(power_it); | |
98 if (power_levels.empty()) | |
99 audio_streams_.erase(stream_it); | |
73 } | 100 } |
74 | 101 |
75 WebContents* web_contents = tab_util::GetWebContentsByID(render_process_id, | 102 WebContents* web_contents = tab_util::GetWebContentsByID(render_process_id, |
76 render_view_id); | 103 render_view_id); |
77 if (web_contents) | 104 if (web_contents) |
78 web_contents->NotifyNavigationStateChanged(content::INVALIDATE_TYPE_TAB); | 105 web_contents->NotifyNavigationStateChanged(content::INVALIDATE_TYPE_TAB); |
79 } | 106 } |
OLD | NEW |