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" |
11 #include "content/public/browser/invalidate_type.h" | 11 #include "content/public/browser/invalidate_type.h" |
12 #include "content/public/browser/render_frame_host.h" | 12 #include "content/public/browser/render_frame_host.h" |
13 | 13 |
14 namespace content { | 14 namespace content { |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 AudioStreamMonitor* AudioStreamMonitorFromRenderFrame(int render_process_id, | 18 AudioStreamMonitor* AudioStreamMonitorFromRenderFrame(int render_process_id, |
19 int render_frame_id) { | 19 int render_frame_id) { |
20 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 20 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
21 WebContentsImpl* const web_contents = | 21 WebContentsImpl* const web_contents = |
22 static_cast<WebContentsImpl*>(WebContents::FromRenderFrameHost( | 22 static_cast<WebContentsImpl*>(WebContents::FromRenderFrameHost( |
23 RenderFrameHost::FromID(render_process_id, render_frame_id))); | 23 RenderFrameHost::FromID(render_process_id, render_frame_id))); |
24 | 24 |
25 if (!web_contents) | 25 return web_contents ? web_contents->audio_stream_monitor() : nullptr; |
26 return nullptr; | |
27 | |
28 AudioStateProvider* audio_provider = web_contents->audio_state_provider(); | |
29 return audio_provider ? audio_provider->audio_stream_monitor() : nullptr; | |
30 } | 26 } |
31 | 27 |
32 } // namespace | 28 } // namespace |
33 | 29 |
34 AudioStreamMonitor::AudioStreamMonitor(WebContents* contents) | 30 AudioStreamMonitor::AudioStreamMonitor(WebContents* contents) |
35 : AudioStateProvider(contents), | 31 : web_contents_(contents), |
36 clock_(&default_tick_clock_) | 32 clock_(&default_tick_clock_), |
| 33 was_recently_audible_(false) |
37 { | 34 { |
| 35 DCHECK(web_contents_); |
38 } | 36 } |
39 | 37 |
40 AudioStreamMonitor::~AudioStreamMonitor() {} | 38 AudioStreamMonitor::~AudioStreamMonitor() {} |
41 | 39 |
42 bool AudioStreamMonitor::IsAudioStateAvailable() const { | |
43 return media::AudioOutputController::will_monitor_audio_levels(); | |
44 } | |
45 | |
46 // This provider is the monitor. | |
47 AudioStreamMonitor* AudioStreamMonitor::audio_stream_monitor() { | |
48 return this; | |
49 } | |
50 | |
51 bool AudioStreamMonitor::WasRecentlyAudible() const { | 40 bool AudioStreamMonitor::WasRecentlyAudible() const { |
52 DCHECK(thread_checker_.CalledOnValidThread()); | 41 DCHECK(thread_checker_.CalledOnValidThread()); |
53 return AudioStateProvider::WasRecentlyAudible(); | 42 return was_recently_audible_; |
54 } | 43 } |
55 | 44 |
56 // static | 45 // static |
57 void AudioStreamMonitor::StartMonitoringStream( | 46 void AudioStreamMonitor::StartMonitoringStream( |
58 int render_process_id, | 47 int render_process_id, |
59 int render_frame_id, | 48 int render_frame_id, |
60 int stream_id, | 49 int stream_id, |
61 const ReadPowerAndClipCallback& read_power_callback) { | 50 const ReadPowerAndClipCallback& read_power_callback) { |
62 if (!media::AudioOutputController::will_monitor_audio_levels()) | 51 if (!monitoring_available()) |
63 return; | 52 return; |
64 BrowserThread::PostTask(BrowserThread::UI, | 53 BrowserThread::PostTask(BrowserThread::UI, |
65 FROM_HERE, | 54 FROM_HERE, |
66 base::Bind(&StartMonitoringHelper, | 55 base::Bind(&StartMonitoringHelper, |
67 render_process_id, | 56 render_process_id, |
68 render_frame_id, | 57 render_frame_id, |
69 stream_id, | 58 stream_id, |
70 read_power_callback)); | 59 read_power_callback)); |
71 } | 60 } |
72 | 61 |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 const float kSilenceThresholdDBFS = -72.24719896f; | 134 const float kSilenceThresholdDBFS = -72.24719896f; |
146 if (power_dbfs >= kSilenceThresholdDBFS) { | 135 if (power_dbfs >= kSilenceThresholdDBFS) { |
147 last_blurt_time_ = clock_->NowTicks(); | 136 last_blurt_time_ = clock_->NowTicks(); |
148 MaybeToggle(); | 137 MaybeToggle(); |
149 break; // No need to poll remaining streams. | 138 break; // No need to poll remaining streams. |
150 } | 139 } |
151 } | 140 } |
152 } | 141 } |
153 | 142 |
154 void AudioStreamMonitor::MaybeToggle() { | 143 void AudioStreamMonitor::MaybeToggle() { |
| 144 const bool indicator_was_on = was_recently_audible_; |
155 const base::TimeTicks off_time = | 145 const base::TimeTicks off_time = |
156 last_blurt_time_ + base::TimeDelta::FromMilliseconds(kHoldOnMilliseconds); | 146 last_blurt_time_ + base::TimeDelta::FromMilliseconds(kHoldOnMilliseconds); |
157 const base::TimeTicks now = clock_->NowTicks(); | 147 const base::TimeTicks now = clock_->NowTicks(); |
158 const bool should_indicator_be_on = now < off_time; | 148 const bool should_indicator_be_on = now < off_time; |
159 | 149 |
160 Notify(should_indicator_be_on); | 150 if (should_indicator_be_on != indicator_was_on) { |
| 151 was_recently_audible_ = should_indicator_be_on; |
| 152 web_contents_->NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB); |
| 153 } |
161 | 154 |
162 if (!should_indicator_be_on) { | 155 if (!should_indicator_be_on) { |
163 off_timer_.Stop(); | 156 off_timer_.Stop(); |
164 } else if (!off_timer_.IsRunning()) { | 157 } else if (!off_timer_.IsRunning()) { |
165 off_timer_.Start( | 158 off_timer_.Start( |
166 FROM_HERE, | 159 FROM_HERE, |
167 off_time - now, | 160 off_time - now, |
168 base::Bind(&AudioStreamMonitor::MaybeToggle, base::Unretained(this))); | 161 base::Bind(&AudioStreamMonitor::MaybeToggle, base::Unretained(this))); |
169 } | 162 } |
170 } | 163 } |
171 | 164 |
172 } // namespace content | 165 } // namespace content |
OLD | NEW |