| 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 return web_contents ? web_contents->audio_stream_monitor() : NULL; |
| 25 if (!web_contents) | |
| 26 return nullptr; | |
| 27 | |
| 28 AudioStateProvider* audio_provider = web_contents->audio_state_provider(); | |
| 29 return audio_provider ? audio_provider->audio_stream_monitor() : nullptr; | |
| 30 } | 25 } |
| 31 | 26 |
| 32 } // namespace | 27 } // namespace |
| 33 | 28 |
| 34 AudioStreamMonitor::AudioStreamMonitor(WebContents* contents) | 29 AudioStreamMonitor::AudioStreamMonitor(WebContents* contents) |
| 35 : AudioStateProvider(contents), | 30 : web_contents_(contents), |
| 36 clock_(&default_tick_clock_) | 31 clock_(&default_tick_clock_), |
| 37 { | 32 was_recently_audible_(false) { |
| 33 DCHECK(web_contents_); |
| 38 } | 34 } |
| 39 | 35 |
| 40 AudioStreamMonitor::~AudioStreamMonitor() {} | 36 AudioStreamMonitor::~AudioStreamMonitor() {} |
| 41 | 37 |
| 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 { | 38 bool AudioStreamMonitor::WasRecentlyAudible() const { |
| 52 DCHECK(thread_checker_.CalledOnValidThread()); | 39 DCHECK(thread_checker_.CalledOnValidThread()); |
| 53 return AudioStateProvider::WasRecentlyAudible(); | 40 return was_recently_audible_; |
| 54 } | 41 } |
| 55 | 42 |
| 56 // static | 43 // static |
| 57 void AudioStreamMonitor::StartMonitoringStream( | 44 void AudioStreamMonitor::StartMonitoringStream( |
| 58 int render_process_id, | 45 int render_process_id, |
| 59 int render_frame_id, | 46 int render_frame_id, |
| 60 int stream_id, | 47 int stream_id, |
| 61 const ReadPowerAndClipCallback& read_power_callback) { | 48 const ReadPowerAndClipCallback& read_power_callback) { |
| 62 if (!media::AudioOutputController::will_monitor_audio_levels()) | 49 if (!monitoring_available()) |
| 63 return; | 50 return; |
| 64 BrowserThread::PostTask(BrowserThread::UI, | 51 BrowserThread::PostTask(BrowserThread::UI, |
| 65 FROM_HERE, | 52 FROM_HERE, |
| 66 base::Bind(&StartMonitoringHelper, | 53 base::Bind(&StartMonitoringHelper, |
| 67 render_process_id, | 54 render_process_id, |
| 68 render_frame_id, | 55 render_frame_id, |
| 69 stream_id, | 56 stream_id, |
| 70 read_power_callback)); | 57 read_power_callback)); |
| 71 } | 58 } |
| 72 | 59 |
| 73 // static | 60 // static |
| 74 void AudioStreamMonitor::StopMonitoringStream(int render_process_id, | 61 void AudioStreamMonitor::StopMonitoringStream(int render_process_id, |
| 75 int render_frame_id, | 62 int render_frame_id, |
| 76 int stream_id) { | 63 int stream_id) { |
| 77 if (!media::AudioOutputController::will_monitor_audio_levels()) | 64 if (!monitoring_available()) |
| 78 return; | 65 return; |
| 79 BrowserThread::PostTask(BrowserThread::UI, | 66 BrowserThread::PostTask(BrowserThread::UI, |
| 80 FROM_HERE, | 67 FROM_HERE, |
| 81 base::Bind(&StopMonitoringHelper, | 68 base::Bind(&StopMonitoringHelper, |
| 82 render_process_id, | 69 render_process_id, |
| 83 render_frame_id, | 70 render_frame_id, |
| 84 stream_id)); | 71 stream_id)); |
| 85 } | 72 } |
| 86 | 73 |
| 87 // static | 74 // static |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 144 const float kSilenceThresholdDBFS = -72.24719896f; | 131 const float kSilenceThresholdDBFS = -72.24719896f; |
| 145 if (power_dbfs >= kSilenceThresholdDBFS) { | 132 if (power_dbfs >= kSilenceThresholdDBFS) { |
| 146 last_blurt_time_ = clock_->NowTicks(); | 133 last_blurt_time_ = clock_->NowTicks(); |
| 147 MaybeToggle(); | 134 MaybeToggle(); |
| 148 break; // No need to poll remaining streams. | 135 break; // No need to poll remaining streams. |
| 149 } | 136 } |
| 150 } | 137 } |
| 151 } | 138 } |
| 152 | 139 |
| 153 void AudioStreamMonitor::MaybeToggle() { | 140 void AudioStreamMonitor::MaybeToggle() { |
| 141 const bool indicator_was_on = was_recently_audible_; |
| 154 const base::TimeTicks off_time = | 142 const base::TimeTicks off_time = |
| 155 last_blurt_time_ + base::TimeDelta::FromMilliseconds(kHoldOnMilliseconds); | 143 last_blurt_time_ + base::TimeDelta::FromMilliseconds(kHoldOnMilliseconds); |
| 156 const base::TimeTicks now = clock_->NowTicks(); | 144 const base::TimeTicks now = clock_->NowTicks(); |
| 157 const bool should_indicator_be_on = now < off_time; | 145 const bool should_indicator_be_on = now < off_time; |
| 158 | 146 |
| 159 Notify(should_indicator_be_on); | 147 if (should_indicator_be_on != indicator_was_on) { |
| 148 was_recently_audible_ = should_indicator_be_on; |
| 149 web_contents_->NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB); |
| 150 } |
| 160 | 151 |
| 161 if (!should_indicator_be_on) { | 152 if (!should_indicator_be_on) { |
| 162 off_timer_.Stop(); | 153 off_timer_.Stop(); |
| 163 } else if (!off_timer_.IsRunning()) { | 154 } else if (!off_timer_.IsRunning()) { |
| 164 off_timer_.Start( | 155 off_timer_.Start( |
| 165 FROM_HERE, | 156 FROM_HERE, |
| 166 off_time - now, | 157 off_time - now, |
| 167 base::Bind(&AudioStreamMonitor::MaybeToggle, base::Unretained(this))); | 158 base::Bind(&AudioStreamMonitor::MaybeToggle, base::Unretained(this))); |
| 168 } | 159 } |
| 169 } | 160 } |
| 170 | 161 |
| 171 } // namespace content | 162 } // namespace content |
| OLD | NEW |