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

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

Issue 2496173003: Communicate audio state to renderer process on Android. (Closed)
Patch Set: Second iteration Created 4 years, 1 month 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
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 "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"
(...skipping 13 matching lines...) Expand all
24 24
25 return web_contents ? web_contents->audio_stream_monitor() : nullptr; 25 return web_contents ? web_contents->audio_stream_monitor() : nullptr;
26 } 26 }
27 27
28 } // namespace 28 } // namespace
29 29
30 AudioStreamMonitor::AudioStreamMonitor(WebContents* contents) 30 AudioStreamMonitor::AudioStreamMonitor(WebContents* contents)
31 : web_contents_(contents), 31 : web_contents_(contents),
32 clock_(&default_tick_clock_), 32 clock_(&default_tick_clock_),
33 was_recently_audible_(false), 33 was_recently_audible_(false),
34 is_audible_(false) 34 is_audible_(false),
35 { 35 active_streams_(0) {
36 DCHECK(web_contents_); 36 DCHECK(web_contents_);
37 } 37 }
38 38
39 AudioStreamMonitor::~AudioStreamMonitor() {} 39 AudioStreamMonitor::~AudioStreamMonitor() {}
40 40
41 bool AudioStreamMonitor::WasRecentlyAudible() const { 41 bool AudioStreamMonitor::WasRecentlyAudible() const {
42 DCHECK(thread_checker_.CalledOnValidThread()); 42 DCHECK(thread_checker_.CalledOnValidThread());
43 return was_recently_audible_; 43 return was_recently_audible_;
44 } 44 }
45 45
46 bool AudioStreamMonitor::IsCurrentlyAudible() const { 46 bool AudioStreamMonitor::IsCurrentlyAudible() const {
47 DCHECK(thread_checker_.CalledOnValidThread()); 47 DCHECK(thread_checker_.CalledOnValidThread());
48 return is_audible_; 48 return is_audible_;
49 } 49 }
50 50
51 // static 51 // static
52 void AudioStreamMonitor::StartMonitoringStream( 52 void AudioStreamMonitor::StartMonitoringStream(
53 int render_process_id, 53 int render_process_id,
54 int render_frame_id, 54 int render_frame_id,
55 int stream_id, 55 int stream_id,
56 const ReadPowerAndClipCallback& read_power_callback) { 56 const ReadPowerAndClipCallback& read_power_callback) {
57 if (!monitoring_available())
58 return;
59 BrowserThread::PostTask(BrowserThread::UI, 57 BrowserThread::PostTask(BrowserThread::UI,
60 FROM_HERE, 58 FROM_HERE,
61 base::Bind(&StartMonitoringHelper, 59 base::Bind(&StartMonitoringHelper,
62 render_process_id, 60 render_process_id,
63 render_frame_id, 61 render_frame_id,
64 stream_id, 62 stream_id,
65 read_power_callback)); 63 read_power_callback));
66 } 64 }
67 65
68 // static 66 // static
69 void AudioStreamMonitor::StopMonitoringStream(int render_process_id, 67 void AudioStreamMonitor::StopMonitoringStream(int render_process_id,
70 int render_frame_id, 68 int render_frame_id,
71 int stream_id) { 69 int stream_id) {
72 if (!media::AudioOutputController::will_monitor_audio_levels())
73 return;
74 BrowserThread::PostTask(BrowserThread::UI, 70 BrowserThread::PostTask(BrowserThread::UI,
75 FROM_HERE, 71 FROM_HERE,
76 base::Bind(&StopMonitoringHelper, 72 base::Bind(&StopMonitoringHelper,
77 render_process_id, 73 render_process_id,
78 render_frame_id, 74 render_frame_id,
79 stream_id)); 75 stream_id));
80 } 76 }
81 77
82 // static 78 // static
83 void AudioStreamMonitor::StartMonitoringHelper( 79 void AudioStreamMonitor::StartMonitoringHelper(
84 int render_process_id, 80 int render_process_id,
85 int render_frame_id, 81 int render_frame_id,
86 int stream_id, 82 int stream_id,
87 const ReadPowerAndClipCallback& read_power_callback) { 83 const ReadPowerAndClipCallback& read_power_callback) {
88 DCHECK_CURRENTLY_ON(BrowserThread::UI); 84 DCHECK_CURRENTLY_ON(BrowserThread::UI);
89 AudioStreamMonitor* const monitor = 85 AudioStreamMonitor* const monitor =
90 AudioStreamMonitorFromRenderFrame(render_process_id, render_frame_id); 86 AudioStreamMonitorFromRenderFrame(render_process_id, render_frame_id);
91 if (monitor) { 87 if (!monitor)
92 monitor->StartMonitoringStreamOnUIThread( 88 return;
93 render_process_id, stream_id, read_power_callback); 89
94 } 90 monitor->OnStreamAdded();
91
92 if (!power_level_monitoring_available())
93 return;
94
95 monitor->StartMonitoringStreamOnUIThread(render_process_id, stream_id,
96 read_power_callback);
95 } 97 }
96 98
97 // static 99 // static
98 void AudioStreamMonitor::StopMonitoringHelper(int render_process_id, 100 void AudioStreamMonitor::StopMonitoringHelper(int render_process_id,
99 int render_frame_id, 101 int render_frame_id,
100 int stream_id) { 102 int stream_id) {
101 DCHECK_CURRENTLY_ON(BrowserThread::UI); 103 DCHECK_CURRENTLY_ON(BrowserThread::UI);
102 AudioStreamMonitor* const monitor = 104 AudioStreamMonitor* const monitor =
103 AudioStreamMonitorFromRenderFrame(render_process_id, render_frame_id); 105 AudioStreamMonitorFromRenderFrame(render_process_id, render_frame_id);
104 if (monitor) 106 if (!monitor)
105 monitor->StopMonitoringStreamOnUIThread(render_process_id, stream_id); 107 return;
108
109 monitor->OnStreamRemoved();
110
111 if (!power_level_monitoring_available())
112 return;
113
114 monitor->StopMonitoringStreamOnUIThread(render_process_id, stream_id);
106 } 115 }
107 116
108 void AudioStreamMonitor::StartMonitoringStreamOnUIThread( 117 void AudioStreamMonitor::StartMonitoringStreamOnUIThread(
109 int render_process_id, 118 int render_process_id,
110 int stream_id, 119 int stream_id,
111 const ReadPowerAndClipCallback& read_power_callback) { 120 const ReadPowerAndClipCallback& read_power_callback) {
112 DCHECK(thread_checker_.CalledOnValidThread()); 121 DCHECK(thread_checker_.CalledOnValidThread());
113 DCHECK(!read_power_callback.is_null()); 122 DCHECK(!read_power_callback.is_null());
114 poll_callbacks_[StreamID(render_process_id, stream_id)] = read_power_callback; 123 poll_callbacks_[StreamID(render_process_id, stream_id)] = read_power_callback;
115 if (!poll_timer_.IsRunning()) { 124 if (!poll_timer_.IsRunning()) {
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
169 if (!should_indicator_be_on) { 178 if (!should_indicator_be_on) {
170 off_timer_.Stop(); 179 off_timer_.Stop();
171 } else if (!off_timer_.IsRunning()) { 180 } else if (!off_timer_.IsRunning()) {
172 off_timer_.Start( 181 off_timer_.Start(
173 FROM_HERE, 182 FROM_HERE,
174 off_time - now, 183 off_time - now,
175 base::Bind(&AudioStreamMonitor::MaybeToggle, base::Unretained(this))); 184 base::Bind(&AudioStreamMonitor::MaybeToggle, base::Unretained(this)));
176 } 185 }
177 } 186 }
178 187
188 void AudioStreamMonitor::OnStreamAdded() {
189 ++active_streams_;
DaleCurtis 2016/11/15 19:26:01 DCHECK_CURRENTLY_ON() ?
altimin 2016/11/16 12:52:46 Done.
190
191 if (power_level_monitoring_available())
192 return;
193
194 if (active_streams_ == 1) {
195 is_audible_ = true;
196 web_contents_->OnAudioStateChanged(true);
197 MaybeToggle();
198 }
199 }
200
201 void AudioStreamMonitor::OnStreamRemoved() {
202 --active_streams_;
203
204 if (power_level_monitoring_available())
205 return;
206
207 if (active_streams_ == 0) {
208 is_audible_ = false;
209 web_contents_->OnAudioStateChanged(false);
210 MaybeToggle();
211 }
212 }
213
179 } // namespace content 214 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698