| 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 return web_contents ? web_contents->audio_stream_monitor() : NULL; | 24 |
| 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; |
| 25 } | 30 } |
| 26 | 31 |
| 27 } // namespace | 32 } // namespace |
| 28 | 33 |
| 29 AudioStreamMonitor::AudioStreamMonitor(WebContents* contents) | 34 AudioStreamMonitor::AudioStreamMonitor(WebContents* contents) |
| 30 : web_contents_(contents), | 35 : AudioStateProvider(contents), |
| 31 clock_(&default_tick_clock_), | 36 clock_(&default_tick_clock_) |
| 32 was_recently_audible_(false) { | 37 { |
| 33 DCHECK(web_contents_); | |
| 34 } | 38 } |
| 35 | 39 |
| 36 AudioStreamMonitor::~AudioStreamMonitor() {} | 40 AudioStreamMonitor::~AudioStreamMonitor() {} |
| 37 | 41 |
| 42 |
| 43 // This provider is the monitor. |
| 44 AudioStreamMonitor* AudioStreamMonitor::audio_stream_monitor() { |
| 45 return this; |
| 46 } |
| 47 |
| 38 bool AudioStreamMonitor::WasRecentlyAudible() const { | 48 bool AudioStreamMonitor::WasRecentlyAudible() const { |
| 39 DCHECK(thread_checker_.CalledOnValidThread()); | 49 DCHECK(thread_checker_.CalledOnValidThread()); |
| 40 return was_recently_audible_; | 50 return AudioStateProvider::WasRecentlyAudible(); |
| 41 } | 51 } |
| 42 | 52 |
| 43 // static | 53 // static |
| 44 void AudioStreamMonitor::StartMonitoringStream( | 54 void AudioStreamMonitor::StartMonitoringStream( |
| 45 int render_process_id, | 55 int render_process_id, |
| 46 int render_frame_id, | 56 int render_frame_id, |
| 47 int stream_id, | 57 int stream_id, |
| 48 const ReadPowerAndClipCallback& read_power_callback) { | 58 const ReadPowerAndClipCallback& read_power_callback) { |
| 49 if (!monitoring_available()) | 59 if (!monitoring_available()) |
| 50 return; | 60 return; |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 const float kSilenceThresholdDBFS = -72.24719896f; | 141 const float kSilenceThresholdDBFS = -72.24719896f; |
| 132 if (power_dbfs >= kSilenceThresholdDBFS) { | 142 if (power_dbfs >= kSilenceThresholdDBFS) { |
| 133 last_blurt_time_ = clock_->NowTicks(); | 143 last_blurt_time_ = clock_->NowTicks(); |
| 134 MaybeToggle(); | 144 MaybeToggle(); |
| 135 break; // No need to poll remaining streams. | 145 break; // No need to poll remaining streams. |
| 136 } | 146 } |
| 137 } | 147 } |
| 138 } | 148 } |
| 139 | 149 |
| 140 void AudioStreamMonitor::MaybeToggle() { | 150 void AudioStreamMonitor::MaybeToggle() { |
| 141 const bool indicator_was_on = was_recently_audible_; | |
| 142 const base::TimeTicks off_time = | 151 const base::TimeTicks off_time = |
| 143 last_blurt_time_ + base::TimeDelta::FromMilliseconds(kHoldOnMilliseconds); | 152 last_blurt_time_ + base::TimeDelta::FromMilliseconds(kHoldOnMilliseconds); |
| 144 const base::TimeTicks now = clock_->NowTicks(); | 153 const base::TimeTicks now = clock_->NowTicks(); |
| 145 const bool should_indicator_be_on = now < off_time; | 154 const bool should_indicator_be_on = now < off_time; |
| 146 | 155 |
| 147 if (should_indicator_be_on != indicator_was_on) { | 156 Notify(should_indicator_be_on); |
| 148 was_recently_audible_ = should_indicator_be_on; | |
| 149 web_contents_->NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB); | |
| 150 } | |
| 151 | 157 |
| 152 if (!should_indicator_be_on) { | 158 if (!should_indicator_be_on) { |
| 153 off_timer_.Stop(); | 159 off_timer_.Stop(); |
| 154 } else if (!off_timer_.IsRunning()) { | 160 } else if (!off_timer_.IsRunning()) { |
| 155 off_timer_.Start( | 161 off_timer_.Start( |
| 156 FROM_HERE, | 162 FROM_HERE, |
| 157 off_time - now, | 163 off_time - now, |
| 158 base::Bind(&AudioStreamMonitor::MaybeToggle, base::Unretained(this))); | 164 base::Bind(&AudioStreamMonitor::MaybeToggle, base::Unretained(this))); |
| 159 } | 165 } |
| 160 } | 166 } |
| 161 | 167 |
| 162 } // namespace content | 168 } // namespace content |
| OLD | NEW |