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

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: Move it with style! 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/public/browser/browser_thread.h"
9 #include "content/public/browser/invalidate_type.h" 10 #include "content/public/browser/invalidate_type.h"
11 #include "content/public/browser/power_save_blocker.h"
12 #include "content/public/browser/render_frame_host.h"
10 #include "content/public/browser/web_contents.h" 13 #include "content/public/browser/web_contents.h"
11 14
12 DEFINE_WEB_CONTENTS_USER_DATA_KEY(AudioStreamMonitor); 15 namespace content {
16
17 namespace {
18
19 AudioStreamMonitor* AudioStreamMonitorFromRenderFrame(int render_process_id,
20 int render_frame_id) {
21 DCHECK_CURRENTLY_ON(BrowserThread::UI);
22 content::WebContents* const web_contents =
jam 2014/09/02 19:50:33 nit: remove content:: from this file
DaleCurtis 2014/09/04 21:59:06 Done.
23 content::WebContents::FromRenderFrameHost(
24 content::RenderFrameHost::FromID(render_process_id, render_frame_id));
25 if (!web_contents)
26 return NULL;
27
28 // Note: Calling CreateForWebContents() multiple times is valid (see usage
29 // info for content::WebContentsUserData).
30 AudioStreamMonitor::CreateForWebContents(web_contents);
31 return AudioStreamMonitor::FromWebContents(web_contents);
32 }
33
34 } // namespace
35
36 // TODO(dalecurtis): ???? DEFINE_WEB_CONTENTS_USER_DATA_KEY won't link,
DaleCurtis 2014/09/03 18:48:55 No ideas on what's going on here? :)
miu 2014/09/03 22:41:53 If you were compiling on Windows, I would suggest:
DaleCurtis 2014/09/04 21:59:06 Correct. I tried NON_EXPORTED_BASE before creating
37 // kLocatorKey is created as a local symbol without the CONTENT_EXPORT
38 // addition... without it, nm shows:
39 //
40 // $ nm content_unittests.audio_stream_monitor_unittest.o | grep kLocatorKey
41 // U _ZN7content19...AudioStreamMonitorEE11kLocatorKeyE
42 // $ nm libcontent.so | grep AudioStreamMonitor | grep kLocatorkey
43 // 000000000139d8f0 b _ZN7content19...AudioStreamMonitorEE11kLocatorKeyE
44 //
45 template <>
46 CONTENT_EXPORT int
47 content::WebContentsUserData<AudioStreamMonitor>::kLocatorKey = 0;
13 48
14 AudioStreamMonitor::AudioStreamMonitor(content::WebContents* contents) 49 AudioStreamMonitor::AudioStreamMonitor(content::WebContents* contents)
15 : web_contents_(contents), 50 : web_contents_(contents),
16 clock_(&default_tick_clock_), 51 clock_(&default_tick_clock_),
17 was_recently_audible_(false) { 52 was_recently_audible_(false) {
18 DCHECK(web_contents_); 53 DCHECK(web_contents_);
19 } 54 }
20 55
21 AudioStreamMonitor::~AudioStreamMonitor() {} 56 AudioStreamMonitor::~AudioStreamMonitor() {}
22 57
23 bool AudioStreamMonitor::WasRecentlyAudible() const { 58 bool AudioStreamMonitor::WasRecentlyAudible() const {
24 DCHECK(thread_checker_.CalledOnValidThread()); 59 DCHECK(thread_checker_.CalledOnValidThread());
25 return was_recently_audible_; 60 return was_recently_audible_;
26 } 61 }
27 62
63 /* static */
28 void AudioStreamMonitor::StartMonitoringStream( 64 void AudioStreamMonitor::StartMonitoringStream(
65 int render_process_id,
66 int render_frame_id,
67 int stream_id,
68 const ReadPowerAndClipCallback& read_power_callback) {
69 BrowserThread::PostTask(BrowserThread::UI,
70 FROM_HERE,
71 base::Bind(&StartMonitoringHelper,
72 render_process_id,
73 render_frame_id,
74 stream_id,
75 read_power_callback));
76 }
77
78 /* static */
79 void AudioStreamMonitor::StopMonitoringStream(int render_process_id,
80 int render_frame_id,
81 int stream_id) {
82 BrowserThread::PostTask(BrowserThread::UI,
83 FROM_HERE,
84 base::Bind(&StopMonitoringHelper,
85 render_process_id,
86 render_frame_id,
87 stream_id));
88 }
89
90 /* static */
91 void AudioStreamMonitor::StartMonitoringHelper(
92 int render_process_id,
93 int render_frame_id,
94 int stream_id,
95 const ReadPowerAndClipCallback& read_power_callback) {
96 DCHECK_CURRENTLY_ON(BrowserThread::UI);
97 AudioStreamMonitor* const monitor =
98 AudioStreamMonitorFromRenderFrame(render_process_id, render_frame_id);
99 if (monitor)
100 monitor->StartMonitoringStreamOnUIThread(stream_id, read_power_callback);
101 }
102
103 /* static */
104 void AudioStreamMonitor::StopMonitoringHelper(int render_process_id,
105 int render_frame_id,
106 int stream_id) {
107 DCHECK_CURRENTLY_ON(BrowserThread::UI);
108 AudioStreamMonitor* const monitor =
109 AudioStreamMonitorFromRenderFrame(render_process_id, render_frame_id);
110 if (monitor)
111 monitor->StopMonitoringStreamOnUIThread(stream_id);
112 }
113
114 void AudioStreamMonitor::StartMonitoringStreamOnUIThread(
29 int stream_id, 115 int stream_id,
30 const ReadPowerAndClipCallback& read_power_callback) { 116 const ReadPowerAndClipCallback& read_power_callback) {
31 DCHECK(thread_checker_.CalledOnValidThread()); 117 DCHECK(thread_checker_.CalledOnValidThread());
32 DCHECK(!read_power_callback.is_null()); 118 DCHECK(!read_power_callback.is_null());
33 poll_callbacks_[stream_id] = read_power_callback; 119 poll_callbacks_[stream_id] = read_power_callback;
34 if (!poll_timer_.IsRunning()) { 120 if (!poll_timer_.IsRunning()) {
35 poll_timer_.Start( 121 poll_timer_.Start(
36 FROM_HERE, 122 FROM_HERE,
37 base::TimeDelta::FromSeconds(1) / kPowerMeasurementsPerSecond, 123 base::TimeDelta::FromSeconds(1) / kPowerMeasurementsPerSecond,
38 base::Bind(&AudioStreamMonitor::Poll, base::Unretained(this))); 124 base::Bind(&AudioStreamMonitor::Poll, base::Unretained(this)));
39 } 125 }
40 } 126 }
41 127
42 void AudioStreamMonitor::StopMonitoringStream(int stream_id) { 128 void AudioStreamMonitor::StopMonitoringStreamOnUIThread(int stream_id) {
43 DCHECK(thread_checker_.CalledOnValidThread()); 129 DCHECK(thread_checker_.CalledOnValidThread());
44 poll_callbacks_.erase(stream_id); 130 poll_callbacks_.erase(stream_id);
45 if (poll_callbacks_.empty()) 131 if (poll_callbacks_.empty())
46 poll_timer_.Stop(); 132 poll_timer_.Stop();
47 } 133 }
48 134
49 void AudioStreamMonitor::Poll() { 135 void AudioStreamMonitor::Poll() {
50 for (StreamPollCallbackMap::const_iterator it = poll_callbacks_.begin(); 136 for (StreamPollCallbackMap::const_iterator it = poll_callbacks_.begin();
51 it != poll_callbacks_.end(); 137 it != poll_callbacks_.end();
52 ++it) { 138 ++it) {
(...skipping 17 matching lines...) Expand all
70 const base::TimeTicks now = clock_->NowTicks(); 156 const base::TimeTicks now = clock_->NowTicks();
71 const bool should_indicator_be_on = now < off_time; 157 const bool should_indicator_be_on = now < off_time;
72 158
73 if (should_indicator_be_on != indicator_was_on) { 159 if (should_indicator_be_on != indicator_was_on) {
74 was_recently_audible_ = should_indicator_be_on; 160 was_recently_audible_ = should_indicator_be_on;
75 web_contents_->NotifyNavigationStateChanged(content::INVALIDATE_TYPE_TAB); 161 web_contents_->NotifyNavigationStateChanged(content::INVALIDATE_TYPE_TAB);
76 } 162 }
77 163
78 if (!should_indicator_be_on) { 164 if (!should_indicator_be_on) {
79 off_timer_.Stop(); 165 off_timer_.Stop();
166 blocker_.reset();
80 } else if (!off_timer_.IsRunning()) { 167 } else if (!off_timer_.IsRunning()) {
168 if (!blocker_) {
169 blocker_ = content::PowerSaveBlocker::Create(
miu 2014/09/02 21:04:39 Can we move this into WebContentsImpl now? Meanin
DaleCurtis 2014/09/03 18:48:55 Let me play with it. I think that's a great idea i
DaleCurtis 2014/09/04 21:59:06 Done.
170 content::PowerSaveBlocker::kPowerSaveBlockPreventAppSuspension,
171 "Playing Audio");
172 }
81 off_timer_.Start( 173 off_timer_.Start(
82 FROM_HERE, 174 FROM_HERE,
83 off_time - now, 175 off_time - now,
84 base::Bind(&AudioStreamMonitor::MaybeToggle, base::Unretained(this))); 176 base::Bind(&AudioStreamMonitor::MaybeToggle, base::Unretained(this)));
85 } 177 }
86 } 178 }
179
180 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698