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

Side by Side Diff: media/audio/pulse/pulse_input.cc

Issue 1711823004: Let default device in PulseAudio be the system default device (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: latest Created 4 years, 9 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
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "media/audio/pulse/pulse_input.h" 5 #include "media/audio/pulse/pulse_input.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "media/audio/pulse/audio_manager_pulse.h" 10 #include "media/audio/pulse/audio_manager_pulse.h"
11 #include "media/audio/pulse/pulse_util.h" 11 #include "media/audio/pulse/pulse_util.h"
12 12
13 namespace media { 13 namespace media {
14 14
15 namespace {
16
17 // pa_context_get_server_info callback. It's used by
18 // GetSystemDefaultInputDevice to set |default_system_device_name_| to the
19 // default system input device.
20 void GetSystemDefaultInputDeviceCallback(pa_context* context,
21 const pa_server_info* info,
22 void* user_data) {
23 media::PulseAudioInputStream* stream =
24 reinterpret_cast<media::PulseAudioInputStream*>(user_data);
25 stream->SetDefaultSystemDeviceName(info->default_source_name);
26 pa_threaded_mainloop_signal(stream->GetPAMainloop(), 0);
27 }
28
29 } // namespace
30
15 using pulse::AutoPulseLock; 31 using pulse::AutoPulseLock;
16 using pulse::WaitForOperationCompletion; 32 using pulse::WaitForOperationCompletion;
17 33
18 // Number of blocks of buffers used in the |fifo_|. 34 // Number of blocks of buffers used in the |fifo_|.
19 const int kNumberOfBlocksBufferInFifo = 2; 35 const int kNumberOfBlocksBufferInFifo = 2;
20 36
21 PulseAudioInputStream::PulseAudioInputStream(AudioManagerPulse* audio_manager, 37 PulseAudioInputStream::PulseAudioInputStream(AudioManagerPulse* audio_manager,
22 const std::string& device_name, 38 const std::string& device_name,
23 const AudioParameters& params, 39 const AudioParameters& params,
24 pa_threaded_mainloop* mainloop, 40 pa_threaded_mainloop* mainloop,
(...skipping 19 matching lines...) Expand all
44 60
45 PulseAudioInputStream::~PulseAudioInputStream() { 61 PulseAudioInputStream::~PulseAudioInputStream() {
46 // All internal structures should already have been freed in Close(), 62 // All internal structures should already have been freed in Close(),
47 // which calls AudioManagerPulse::Release which deletes this object. 63 // which calls AudioManagerPulse::Release which deletes this object.
48 DCHECK(!handle_); 64 DCHECK(!handle_);
49 } 65 }
50 66
51 bool PulseAudioInputStream::Open() { 67 bool PulseAudioInputStream::Open() {
52 DCHECK(thread_checker_.CalledOnValidThread()); 68 DCHECK(thread_checker_.CalledOnValidThread());
53 AutoPulseLock auto_lock(pa_mainloop_); 69 AutoPulseLock auto_lock(pa_mainloop_);
70 std::string device_name_to_use = device_name_;
71 if (device_name_ == AudioManagerBase::kDefaultDeviceId) {
72 GetSystemDefaultInputDevice();
73 device_name_to_use = default_system_device_name_;
74 }
75
54 if (!pulse::CreateInputStream(pa_mainloop_, pa_context_, &handle_, params_, 76 if (!pulse::CreateInputStream(pa_mainloop_, pa_context_, &handle_, params_,
55 device_name_, &StreamNotifyCallback, this)) { 77 device_name_to_use, &StreamNotifyCallback,
78 this)) {
56 return false; 79 return false;
57 } 80 }
58 81
59 DCHECK(handle_); 82 DCHECK(handle_);
60 83
61 return true; 84 return true;
62 } 85 }
63 86
64 void PulseAudioInputStream::Start(AudioInputCallback* callback) { 87 void PulseAudioInputStream::Start(AudioInputCallback* callback) {
65 DCHECK(thread_checker_.CalledOnValidThread()); 88 DCHECK(thread_checker_.CalledOnValidThread());
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after
190 return volume_; 213 return volume_;
191 } 214 }
192 } 215 }
193 216
194 bool PulseAudioInputStream::IsMuted() { 217 bool PulseAudioInputStream::IsMuted() {
195 DCHECK(thread_checker_.CalledOnValidThread()); 218 DCHECK(thread_checker_.CalledOnValidThread());
196 GetSourceInformation(&MuteCallback); 219 GetSourceInformation(&MuteCallback);
197 return muted_; 220 return muted_;
198 } 221 }
199 222
223 pa_threaded_mainloop* PulseAudioInputStream::GetPAMainloop() {
224 return pa_mainloop_;
Henrik Grunell 2016/03/10 20:07:34 Could this function be called at the same time as
rchtara 2016/03/11 10:13:09 PulseAudioInputStream::Open is going to lock the m
Henrik Grunell 2016/03/11 18:54:41 Acknowledged.
rchtara 2016/03/13 15:12:00 Done.
225 }
226
227 void PulseAudioInputStream::SetDefaultSystemDeviceName(
228 const std::string& name) {
229 default_system_device_name_ = name;
230 }
231
200 // static, used by pa_stream_set_read_callback. 232 // static, used by pa_stream_set_read_callback.
201 void PulseAudioInputStream::ReadCallback(pa_stream* handle, 233 void PulseAudioInputStream::ReadCallback(pa_stream* handle,
202 size_t length, 234 size_t length,
203 void* user_data) { 235 void* user_data) {
204 PulseAudioInputStream* stream = 236 PulseAudioInputStream* stream =
205 reinterpret_cast<PulseAudioInputStream*>(user_data); 237 reinterpret_cast<PulseAudioInputStream*>(user_data);
206 238
207 stream->ReadData(); 239 stream->ReadData();
208 } 240 }
209 241
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
261 reinterpret_cast<PulseAudioInputStream*>(user_data); 293 reinterpret_cast<PulseAudioInputStream*>(user_data);
262 294
263 if (s && stream->callback_ && 295 if (s && stream->callback_ &&
264 pa_stream_get_state(s) == PA_STREAM_FAILED) { 296 pa_stream_get_state(s) == PA_STREAM_FAILED) {
265 stream->callback_->OnError(stream); 297 stream->callback_->OnError(stream);
266 } 298 }
267 299
268 pa_threaded_mainloop_signal(stream->pa_mainloop_, 0); 300 pa_threaded_mainloop_signal(stream->pa_mainloop_, 0);
269 } 301 }
270 302
303 void PulseAudioInputStream::GetSystemDefaultInputDevice() {
304 DCHECK(pa_mainloop_);
305 DCHECK(pa_context_);
306 pa_operation* operation = pa_context_get_server_info(
307 pa_context_, GetSystemDefaultInputDeviceCallback, this);
308 WaitForOperationCompletion(pa_mainloop_, operation);
309 }
310
271 void PulseAudioInputStream::ReadData() { 311 void PulseAudioInputStream::ReadData() {
272 uint32_t hardware_delay = pulse::GetHardwareLatencyInBytes( 312 uint32_t hardware_delay = pulse::GetHardwareLatencyInBytes(
273 handle_, params_.sample_rate(), params_.GetBytesPerFrame()); 313 handle_, params_.sample_rate(), params_.GetBytesPerFrame());
274 314
275 // Update the AGC volume level once every second. Note that, 315 // Update the AGC volume level once every second. Note that,
276 // |volume| is also updated each time SetVolume() is called 316 // |volume| is also updated each time SetVolume() is called
277 // through IPC by the render-side AGC. 317 // through IPC by the render-side AGC.
278 // We disregard the |normalized_volume| from GetAgcVolume() 318 // We disregard the |normalized_volume| from GetAgcVolume()
279 // and use the value calculated by |volume_|. 319 // and use the value calculated by |volume_|.
280 double normalized_volume = 0.0; 320 double normalized_volume = 0.0;
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
326 return false; 366 return false;
327 367
328 size_t index = pa_stream_get_device_index(handle_); 368 size_t index = pa_stream_get_device_index(handle_);
329 pa_operation* operation = 369 pa_operation* operation =
330 pa_context_get_source_info_by_index(pa_context_, index, callback, this); 370 pa_context_get_source_info_by_index(pa_context_, index, callback, this);
331 WaitForOperationCompletion(pa_mainloop_, operation); 371 WaitForOperationCompletion(pa_mainloop_, operation);
332 return true; 372 return true;
333 } 373 }
334 374
335 } // namespace media 375 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698