Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(415)

Side by Side Diff: content/browser/media/audio_stream_monitor.cc

Issue 478543003: Use AudioStreamMonitor to control power save blocking. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Simplify OS_CHROMEOS exclusions. Created 6 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 "chrome/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"
10 #include "content/public/browser/browser_thread.h"
9 #include "content/public/browser/invalidate_type.h" 11 #include "content/public/browser/invalidate_type.h"
10 #include "content/public/browser/web_contents.h" 12 #include "content/public/browser/render_frame_host.h"
11 13
12 DEFINE_WEB_CONTENTS_USER_DATA_KEY(AudioStreamMonitor); 14 namespace content {
13 15
14 AudioStreamMonitor::AudioStreamMonitor(content::WebContents* contents) 16 namespace {
17
18 AudioStreamMonitor* AudioStreamMonitorFromRenderFrame(int render_process_id,
19 int render_frame_id) {
20 DCHECK_CURRENTLY_ON(BrowserThread::UI);
21 WebContentsImpl* const web_contents =
22 static_cast<WebContentsImpl*>(WebContents::FromRenderFrameHost(
23 RenderFrameHost::FromID(render_process_id, render_frame_id)));
24 return web_contents ? web_contents->audio_stream_monitor() : NULL;
25 }
26
27 } // namespace
28
29 AudioStreamMonitor::AudioStreamMonitor(WebContents* contents)
15 : web_contents_(contents), 30 : web_contents_(contents),
16 clock_(&default_tick_clock_), 31 clock_(&default_tick_clock_),
17 was_recently_audible_(false) { 32 was_recently_audible_(false) {
18 DCHECK(web_contents_); 33 DCHECK(web_contents_);
19 } 34 }
20 35
21 AudioStreamMonitor::~AudioStreamMonitor() {} 36 AudioStreamMonitor::~AudioStreamMonitor() {}
22 37
23 bool AudioStreamMonitor::WasRecentlyAudible() const { 38 bool AudioStreamMonitor::WasRecentlyAudible() const {
24 DCHECK(thread_checker_.CalledOnValidThread()); 39 DCHECK(thread_checker_.CalledOnValidThread());
25 return was_recently_audible_; 40 return was_recently_audible_;
26 } 41 }
27 42
43 // static
28 void AudioStreamMonitor::StartMonitoringStream( 44 void AudioStreamMonitor::StartMonitoringStream(
45 int render_process_id,
46 int render_frame_id,
47 int stream_id,
48 const ReadPowerAndClipCallback& read_power_callback) {
49 if (!monitoring_available())
50 return;
51 BrowserThread::PostTask(BrowserThread::UI,
52 FROM_HERE,
53 base::Bind(&StartMonitoringHelper,
54 render_process_id,
55 render_frame_id,
56 stream_id,
57 read_power_callback));
58 }
59
60 // static
61 void AudioStreamMonitor::StopMonitoringStream(int render_process_id,
62 int render_frame_id,
63 int stream_id) {
64 if (!monitoring_available())
65 return;
66 BrowserThread::PostTask(BrowserThread::UI,
67 FROM_HERE,
68 base::Bind(&StopMonitoringHelper,
69 render_process_id,
70 render_frame_id,
71 stream_id));
72 }
73
74 // static
75 void AudioStreamMonitor::StartMonitoringHelper(
76 int render_process_id,
77 int render_frame_id,
78 int stream_id,
79 const ReadPowerAndClipCallback& read_power_callback) {
80 DCHECK_CURRENTLY_ON(BrowserThread::UI);
81 AudioStreamMonitor* const monitor =
82 AudioStreamMonitorFromRenderFrame(render_process_id, render_frame_id);
83 if (monitor) {
84 monitor->StartMonitoringStreamOnUIThread(
85 render_process_id, stream_id, read_power_callback);
86 }
87 }
88
89 // static
90 void AudioStreamMonitor::StopMonitoringHelper(int render_process_id,
91 int render_frame_id,
92 int stream_id) {
93 DCHECK_CURRENTLY_ON(BrowserThread::UI);
94 AudioStreamMonitor* const monitor =
95 AudioStreamMonitorFromRenderFrame(render_process_id, render_frame_id);
96 if (monitor)
97 monitor->StopMonitoringStreamOnUIThread(render_process_id, stream_id);
98 }
99
100 void AudioStreamMonitor::StartMonitoringStreamOnUIThread(
101 int render_process_id,
29 int stream_id, 102 int stream_id,
30 const ReadPowerAndClipCallback& read_power_callback) { 103 const ReadPowerAndClipCallback& read_power_callback) {
31 DCHECK(thread_checker_.CalledOnValidThread()); 104 DCHECK(thread_checker_.CalledOnValidThread());
32 DCHECK(!read_power_callback.is_null()); 105 DCHECK(!read_power_callback.is_null());
33 poll_callbacks_[stream_id] = read_power_callback; 106 poll_callbacks_[StreamID(render_process_id, stream_id)] = read_power_callback;
34 if (!poll_timer_.IsRunning()) { 107 if (!poll_timer_.IsRunning()) {
35 poll_timer_.Start( 108 poll_timer_.Start(
36 FROM_HERE, 109 FROM_HERE,
37 base::TimeDelta::FromSeconds(1) / kPowerMeasurementsPerSecond, 110 base::TimeDelta::FromSeconds(1) / kPowerMeasurementsPerSecond,
38 base::Bind(&AudioStreamMonitor::Poll, base::Unretained(this))); 111 base::Bind(&AudioStreamMonitor::Poll, base::Unretained(this)));
39 } 112 }
40 } 113 }
41 114
42 void AudioStreamMonitor::StopMonitoringStream(int stream_id) { 115 void AudioStreamMonitor::StopMonitoringStreamOnUIThread(int render_process_id,
116 int stream_id) {
43 DCHECK(thread_checker_.CalledOnValidThread()); 117 DCHECK(thread_checker_.CalledOnValidThread());
44 poll_callbacks_.erase(stream_id); 118 poll_callbacks_.erase(StreamID(render_process_id, stream_id));
45 if (poll_callbacks_.empty()) 119 if (poll_callbacks_.empty())
46 poll_timer_.Stop(); 120 poll_timer_.Stop();
47 } 121 }
48 122
49 void AudioStreamMonitor::Poll() { 123 void AudioStreamMonitor::Poll() {
50 for (StreamPollCallbackMap::const_iterator it = poll_callbacks_.begin(); 124 for (StreamPollCallbackMap::const_iterator it = poll_callbacks_.begin();
51 it != poll_callbacks_.end(); 125 it != poll_callbacks_.end();
52 ++it) { 126 ++it) {
53 // TODO(miu): A new UI for delivering specific power level and clipping 127 // TODO(miu): A new UI for delivering specific power level and clipping
54 // information is still in the works. For now, we throw away all 128 // information is still in the works. For now, we throw away all
(...skipping 10 matching lines...) Expand all
65 139
66 void AudioStreamMonitor::MaybeToggle() { 140 void AudioStreamMonitor::MaybeToggle() {
67 const bool indicator_was_on = was_recently_audible_; 141 const bool indicator_was_on = was_recently_audible_;
68 const base::TimeTicks off_time = 142 const base::TimeTicks off_time =
69 last_blurt_time_ + base::TimeDelta::FromMilliseconds(kHoldOnMilliseconds); 143 last_blurt_time_ + base::TimeDelta::FromMilliseconds(kHoldOnMilliseconds);
70 const base::TimeTicks now = clock_->NowTicks(); 144 const base::TimeTicks now = clock_->NowTicks();
71 const bool should_indicator_be_on = now < off_time; 145 const bool should_indicator_be_on = now < off_time;
72 146
73 if (should_indicator_be_on != indicator_was_on) { 147 if (should_indicator_be_on != indicator_was_on) {
74 was_recently_audible_ = should_indicator_be_on; 148 was_recently_audible_ = should_indicator_be_on;
75 web_contents_->NotifyNavigationStateChanged(content::INVALIDATE_TYPE_TAB); 149 web_contents_->NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB);
76 } 150 }
77 151
78 if (!should_indicator_be_on) { 152 if (!should_indicator_be_on) {
79 off_timer_.Stop(); 153 off_timer_.Stop();
80 } else if (!off_timer_.IsRunning()) { 154 } else if (!off_timer_.IsRunning()) {
81 off_timer_.Start( 155 off_timer_.Start(
82 FROM_HERE, 156 FROM_HERE,
83 off_time - now, 157 off_time - now,
84 base::Bind(&AudioStreamMonitor::MaybeToggle, base::Unretained(this))); 158 base::Bind(&AudioStreamMonitor::MaybeToggle, base::Unretained(this)));
85 } 159 }
86 } 160 }
161
162 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/media/audio_stream_monitor.h ('k') | content/browser/media/audio_stream_monitor_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698