OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "content/browser/media/audio_stream_monitor.h" | 5 #include "content/browser/media/audio_stream_monitor.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/bind_helpers.h" | 8 #include "base/bind_helpers.h" |
9 #include "content/browser/web_contents/web_contents_impl.h" | 9 #include "content/browser/web_contents/web_contents_impl.h" |
10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
(...skipping 12 matching lines...) Expand all Loading... |
23 RenderFrameHost::FromID(render_process_id, render_frame_id))); | 23 RenderFrameHost::FromID(render_process_id, render_frame_id))); |
24 | 24 |
25 return web_contents ? web_contents->audio_stream_monitor() : nullptr; | 25 return web_contents ? web_contents->audio_stream_monitor() : nullptr; |
26 } | 26 } |
27 | 27 |
28 } // namespace | 28 } // namespace |
29 | 29 |
30 AudioStreamMonitor::AudioStreamMonitor(WebContents* contents) | 30 AudioStreamMonitor::AudioStreamMonitor(WebContents* contents) |
31 : web_contents_(contents), | 31 : web_contents_(contents), |
32 clock_(&default_tick_clock_), | 32 clock_(&default_tick_clock_), |
33 was_recently_audible_(false) | 33 was_recently_audible_(false), |
| 34 is_audible_(false) |
34 { | 35 { |
35 DCHECK(web_contents_); | 36 DCHECK(web_contents_); |
36 } | 37 } |
37 | 38 |
38 AudioStreamMonitor::~AudioStreamMonitor() {} | 39 AudioStreamMonitor::~AudioStreamMonitor() {} |
39 | 40 |
40 bool AudioStreamMonitor::WasRecentlyAudible() const { | 41 bool AudioStreamMonitor::WasRecentlyAudible() const { |
41 DCHECK(thread_checker_.CalledOnValidThread()); | 42 DCHECK(thread_checker_.CalledOnValidThread()); |
42 return was_recently_audible_; | 43 return was_recently_audible_; |
43 } | 44 } |
44 | 45 |
| 46 bool AudioStreamMonitor::IsCurrentlyAudible() const { |
| 47 DCHECK(thread_checker_.CalledOnValidThread()); |
| 48 return is_audible_; |
| 49 } |
| 50 |
45 // static | 51 // static |
46 void AudioStreamMonitor::StartMonitoringStream( | 52 void AudioStreamMonitor::StartMonitoringStream( |
47 int render_process_id, | 53 int render_process_id, |
48 int render_frame_id, | 54 int render_frame_id, |
49 int stream_id, | 55 int stream_id, |
50 const ReadPowerAndClipCallback& read_power_callback) { | 56 const ReadPowerAndClipCallback& read_power_callback) { |
51 if (!monitoring_available()) | 57 if (!monitoring_available()) |
52 return; | 58 return; |
53 BrowserThread::PostTask(BrowserThread::UI, | 59 BrowserThread::PostTask(BrowserThread::UI, |
54 FROM_HERE, | 60 FROM_HERE, |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 | 123 |
118 void AudioStreamMonitor::StopMonitoringStreamOnUIThread(int render_process_id, | 124 void AudioStreamMonitor::StopMonitoringStreamOnUIThread(int render_process_id, |
119 int stream_id) { | 125 int stream_id) { |
120 DCHECK(thread_checker_.CalledOnValidThread()); | 126 DCHECK(thread_checker_.CalledOnValidThread()); |
121 poll_callbacks_.erase(StreamID(render_process_id, stream_id)); | 127 poll_callbacks_.erase(StreamID(render_process_id, stream_id)); |
122 if (poll_callbacks_.empty()) | 128 if (poll_callbacks_.empty()) |
123 poll_timer_.Stop(); | 129 poll_timer_.Stop(); |
124 } | 130 } |
125 | 131 |
126 void AudioStreamMonitor::Poll() { | 132 void AudioStreamMonitor::Poll() { |
| 133 bool was_audible = is_audible_; |
| 134 is_audible_ = false; |
| 135 |
127 for (StreamPollCallbackMap::const_iterator it = poll_callbacks_.begin(); | 136 for (StreamPollCallbackMap::const_iterator it = poll_callbacks_.begin(); |
128 it != poll_callbacks_.end(); | 137 it != poll_callbacks_.end(); |
129 ++it) { | 138 ++it) { |
130 // TODO(miu): A new UI for delivering specific power level and clipping | 139 // TODO(miu): A new UI for delivering specific power level and clipping |
131 // information is still in the works. For now, we throw away all | 140 // information is still in the works. For now, we throw away all |
132 // information except for "is it audible?" | 141 // information except for "is it audible?" |
133 const float power_dbfs = it->second.Run().first; | 142 const float power_dbfs = it->second.Run().first; |
134 const float kSilenceThresholdDBFS = -72.24719896f; | 143 const float kSilenceThresholdDBFS = -72.24719896f; |
| 144 |
135 if (power_dbfs >= kSilenceThresholdDBFS) { | 145 if (power_dbfs >= kSilenceThresholdDBFS) { |
136 last_blurt_time_ = clock_->NowTicks(); | 146 last_blurt_time_ = clock_->NowTicks(); |
| 147 is_audible_ = true; |
137 MaybeToggle(); | 148 MaybeToggle(); |
138 break; // No need to poll remaining streams. | 149 break; // No need to poll remaining streams. |
139 } | 150 } |
140 } | 151 } |
| 152 |
| 153 if (is_audible_ != was_audible) |
| 154 web_contents_->NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB); |
141 } | 155 } |
142 | 156 |
143 void AudioStreamMonitor::MaybeToggle() { | 157 void AudioStreamMonitor::MaybeToggle() { |
144 const bool indicator_was_on = was_recently_audible_; | 158 const bool indicator_was_on = was_recently_audible_; |
145 const base::TimeTicks off_time = | 159 const base::TimeTicks off_time = |
146 last_blurt_time_ + base::TimeDelta::FromMilliseconds(kHoldOnMilliseconds); | 160 last_blurt_time_ + base::TimeDelta::FromMilliseconds(kHoldOnMilliseconds); |
147 const base::TimeTicks now = clock_->NowTicks(); | 161 const base::TimeTicks now = clock_->NowTicks(); |
148 const bool should_indicator_be_on = now < off_time; | 162 const bool should_indicator_be_on = now < off_time; |
149 | 163 |
150 if (should_indicator_be_on != indicator_was_on) { | 164 if (should_indicator_be_on != indicator_was_on) { |
151 was_recently_audible_ = should_indicator_be_on; | 165 was_recently_audible_ = should_indicator_be_on; |
152 web_contents_->NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB); | 166 web_contents_->NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB); |
153 } | 167 } |
154 | 168 |
155 if (!should_indicator_be_on) { | 169 if (!should_indicator_be_on) { |
156 off_timer_.Stop(); | 170 off_timer_.Stop(); |
157 } else if (!off_timer_.IsRunning()) { | 171 } else if (!off_timer_.IsRunning()) { |
158 off_timer_.Start( | 172 off_timer_.Start( |
159 FROM_HERE, | 173 FROM_HERE, |
160 off_time - now, | 174 off_time - now, |
161 base::Bind(&AudioStreamMonitor::MaybeToggle, base::Unretained(this))); | 175 base::Bind(&AudioStreamMonitor::MaybeToggle, base::Unretained(this))); |
162 } | 176 } |
163 } | 177 } |
164 | 178 |
165 } // namespace content | 179 } // namespace content |
OLD | NEW |