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 "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/render_frame_host.h" | |
10 #include "content/public/browser/web_contents.h" | 12 #include "content/public/browser/web_contents.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 WebContents* const web_contents = WebContents::FromRenderFrameHost( | |
22 RenderFrameHost::FromID(render_process_id, render_frame_id)); | |
23 if (!web_contents) | |
24 return NULL; | |
25 | |
26 // Note: Calling CreateForWebContents() multiple times is valid (see usage | |
27 // info for WebContentsUserData). | |
28 AudioStreamMonitor::CreateForWebContents(web_contents); | |
29 return AudioStreamMonitor::FromWebContents(web_contents); | |
30 } | |
31 | |
32 } // namespace | |
33 | |
34 // TODO(dalecurtis): ???? DEFINE_WEB_CONTENTS_USER_DATA_KEY won't link, | |
35 // kLocatorKey is created as a local symbol without the CONTENT_EXPORT | |
36 // addition... without it, nm shows: | |
37 // | |
38 // $ nm content_unittests.audio_stream_monitor_unittest.o | grep kLocatorKey | |
39 // U _ZN7content19...AudioStreamMonitorEE11kLocatorKeyE | |
40 // $ nm libcontent.so | grep AudioStreamMonitor | grep kLocatorkey | |
41 // 000000000139d8f0 b _ZN7content19...AudioStreamMonitorEE11kLocatorKeyE | |
42 // | |
43 template <> | |
44 CONTENT_EXPORT int WebContentsUserData<AudioStreamMonitor>::kLocatorKey = 0; | |
scherkus (not reviewing)
2014/09/09 21:07:41
as far as my code searching skills can tell, Audio
DaleCurtis
2014/09/10 00:51:14
Yeah, that's the only reason I can see this hasn't
| |
45 | |
46 AudioStreamMonitor::AudioStreamMonitor(WebContents* contents) | |
15 : web_contents_(contents), | 47 : web_contents_(contents), |
16 clock_(&default_tick_clock_), | 48 clock_(&default_tick_clock_), |
17 was_recently_audible_(false) { | 49 was_recently_audible_(false) { |
18 DCHECK(web_contents_); | 50 DCHECK(web_contents_); |
19 } | 51 } |
20 | 52 |
21 AudioStreamMonitor::~AudioStreamMonitor() {} | 53 AudioStreamMonitor::~AudioStreamMonitor() {} |
22 | 54 |
23 bool AudioStreamMonitor::WasRecentlyAudible() const { | 55 bool AudioStreamMonitor::WasRecentlyAudible() const { |
24 DCHECK(thread_checker_.CalledOnValidThread()); | 56 DCHECK(thread_checker_.CalledOnValidThread()); |
25 return was_recently_audible_; | 57 return was_recently_audible_; |
26 } | 58 } |
27 | 59 |
60 /* static */ | |
scherkus (not reviewing)
2014/09/09 21:07:41
// static
here and below
DaleCurtis
2014/09/10 00:51:14
Done.
| |
28 void AudioStreamMonitor::StartMonitoringStream( | 61 void AudioStreamMonitor::StartMonitoringStream( |
62 int render_process_id, | |
63 int render_frame_id, | |
64 int stream_id, | |
65 const ReadPowerAndClipCallback& read_power_callback) { | |
66 if (!monitoring_available()) | |
67 return; | |
68 BrowserThread::PostTask(BrowserThread::UI, | |
69 FROM_HERE, | |
70 base::Bind(&StartMonitoringHelper, | |
71 render_process_id, | |
72 render_frame_id, | |
73 stream_id, | |
74 read_power_callback)); | |
75 } | |
76 | |
77 /* static */ | |
78 void AudioStreamMonitor::StopMonitoringStream(int render_process_id, | |
79 int render_frame_id, | |
80 int stream_id) { | |
81 if (!monitoring_available()) | |
82 return; | |
83 BrowserThread::PostTask(BrowserThread::UI, | |
84 FROM_HERE, | |
85 base::Bind(&StopMonitoringHelper, | |
86 render_process_id, | |
87 render_frame_id, | |
88 stream_id)); | |
89 } | |
90 | |
91 /* static */ | |
92 void AudioStreamMonitor::StartMonitoringHelper( | |
93 int render_process_id, | |
94 int render_frame_id, | |
95 int stream_id, | |
96 const ReadPowerAndClipCallback& read_power_callback) { | |
97 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
98 AudioStreamMonitor* const monitor = | |
99 AudioStreamMonitorFromRenderFrame(render_process_id, render_frame_id); | |
100 if (monitor) { | |
101 monitor->StartMonitoringStreamOnUIThread( | |
102 render_process_id, stream_id, read_power_callback); | |
103 } | |
104 } | |
105 | |
106 /* static */ | |
107 void AudioStreamMonitor::StopMonitoringHelper(int render_process_id, | |
108 int render_frame_id, | |
109 int stream_id) { | |
110 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
111 AudioStreamMonitor* const monitor = | |
112 AudioStreamMonitorFromRenderFrame(render_process_id, render_frame_id); | |
113 if (monitor) | |
114 monitor->StopMonitoringStreamOnUIThread(render_process_id, stream_id); | |
115 } | |
116 | |
117 void AudioStreamMonitor::StartMonitoringStreamOnUIThread( | |
118 int render_process_id, | |
29 int stream_id, | 119 int stream_id, |
30 const ReadPowerAndClipCallback& read_power_callback) { | 120 const ReadPowerAndClipCallback& read_power_callback) { |
31 DCHECK(thread_checker_.CalledOnValidThread()); | 121 DCHECK(thread_checker_.CalledOnValidThread()); |
32 DCHECK(!read_power_callback.is_null()); | 122 DCHECK(!read_power_callback.is_null()); |
33 poll_callbacks_[stream_id] = read_power_callback; | 123 poll_callbacks_[StreamID(render_process_id, stream_id)] = read_power_callback; |
34 if (!poll_timer_.IsRunning()) { | 124 if (!poll_timer_.IsRunning()) { |
35 poll_timer_.Start( | 125 poll_timer_.Start( |
36 FROM_HERE, | 126 FROM_HERE, |
37 base::TimeDelta::FromSeconds(1) / kPowerMeasurementsPerSecond, | 127 base::TimeDelta::FromSeconds(1) / kPowerMeasurementsPerSecond, |
38 base::Bind(&AudioStreamMonitor::Poll, base::Unretained(this))); | 128 base::Bind(&AudioStreamMonitor::Poll, base::Unretained(this))); |
39 } | 129 } |
40 } | 130 } |
41 | 131 |
42 void AudioStreamMonitor::StopMonitoringStream(int stream_id) { | 132 void AudioStreamMonitor::StopMonitoringStreamOnUIThread(int render_process_id, |
133 int stream_id) { | |
43 DCHECK(thread_checker_.CalledOnValidThread()); | 134 DCHECK(thread_checker_.CalledOnValidThread()); |
44 poll_callbacks_.erase(stream_id); | 135 poll_callbacks_.erase(StreamID(render_process_id, stream_id)); |
45 if (poll_callbacks_.empty()) | 136 if (poll_callbacks_.empty()) |
46 poll_timer_.Stop(); | 137 poll_timer_.Stop(); |
47 } | 138 } |
48 | 139 |
49 void AudioStreamMonitor::Poll() { | 140 void AudioStreamMonitor::Poll() { |
50 for (StreamPollCallbackMap::const_iterator it = poll_callbacks_.begin(); | 141 for (StreamPollCallbackMap::const_iterator it = poll_callbacks_.begin(); |
51 it != poll_callbacks_.end(); | 142 it != poll_callbacks_.end(); |
52 ++it) { | 143 ++it) { |
53 // TODO(miu): A new UI for delivering specific power level and clipping | 144 // 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 | 145 // information is still in the works. For now, we throw away all |
(...skipping 10 matching lines...) Expand all Loading... | |
65 | 156 |
66 void AudioStreamMonitor::MaybeToggle() { | 157 void AudioStreamMonitor::MaybeToggle() { |
67 const bool indicator_was_on = was_recently_audible_; | 158 const bool indicator_was_on = was_recently_audible_; |
68 const base::TimeTicks off_time = | 159 const base::TimeTicks off_time = |
69 last_blurt_time_ + base::TimeDelta::FromMilliseconds(kHoldOnMilliseconds); | 160 last_blurt_time_ + base::TimeDelta::FromMilliseconds(kHoldOnMilliseconds); |
70 const base::TimeTicks now = clock_->NowTicks(); | 161 const base::TimeTicks now = clock_->NowTicks(); |
71 const bool should_indicator_be_on = now < off_time; | 162 const bool should_indicator_be_on = now < off_time; |
72 | 163 |
73 if (should_indicator_be_on != indicator_was_on) { | 164 if (should_indicator_be_on != indicator_was_on) { |
74 was_recently_audible_ = should_indicator_be_on; | 165 was_recently_audible_ = should_indicator_be_on; |
75 web_contents_->NotifyNavigationStateChanged(content::INVALIDATE_TYPE_TAB); | 166 web_contents_->NotifyNavigationStateChanged(INVALIDATE_TYPE_TAB); |
76 } | 167 } |
77 | 168 |
78 if (!should_indicator_be_on) { | 169 if (!should_indicator_be_on) { |
79 off_timer_.Stop(); | 170 off_timer_.Stop(); |
80 } else if (!off_timer_.IsRunning()) { | 171 } else if (!off_timer_.IsRunning()) { |
81 off_timer_.Start( | 172 off_timer_.Start( |
82 FROM_HERE, | 173 FROM_HERE, |
83 off_time - now, | 174 off_time - now, |
84 base::Bind(&AudioStreamMonitor::MaybeToggle, base::Unretained(this))); | 175 base::Bind(&AudioStreamMonitor::MaybeToggle, base::Unretained(this))); |
85 } | 176 } |
86 } | 177 } |
178 | |
179 } // namespace content | |
OLD | NEW |