Chromium Code Reviews| OLD | NEW |
|---|---|
| 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_util.h" | 5 #include "media/audio/pulse/pulse_util.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 "base/macros.h" | 10 #include "base/macros.h" |
| (...skipping 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 154 | 154 |
| 155 // Helper macro for CreateInput/OutputStream() to avoid code spam and | 155 // Helper macro for CreateInput/OutputStream() to avoid code spam and |
| 156 // string bloat. | 156 // string bloat. |
| 157 #define RETURN_ON_FAILURE(expression, message) do { \ | 157 #define RETURN_ON_FAILURE(expression, message) do { \ |
| 158 if (!(expression)) { \ | 158 if (!(expression)) { \ |
| 159 DLOG(ERROR) << message; \ | 159 DLOG(ERROR) << message; \ |
| 160 return false; \ | 160 return false; \ |
| 161 } \ | 161 } \ |
| 162 } while (0) | 162 } while (0) |
| 163 | 163 |
| 164 static std::string system_default_input_device; | |
| 165 static std::string system_default_output_device; | |
| 166 | |
| 167 void SystemDefaultDeviceCallback(pa_context* context, | |
|
Henrik Grunell
2016/02/23 11:13:24
Put these functions in the anonymous namespace abo
Henrik Grunell
2016/02/23 11:13:24
Add comments with brief description for these two
rchtara
2016/02/23 14:58:51
Done.
rchtara
2016/02/23 14:58:51
I changed the cl architecture to use stream object
| |
| 168 const pa_server_info* info, | |
| 169 void* mainloop) { | |
| 170 | |
| 171 system_default_input_device = info->default_source_name; | |
|
Henrik Grunell
2016/02/23 11:13:24
Store the system default device names in the strea
rchtara
2016/02/23 14:58:51
Done
| |
| 172 system_default_output_device = info->default_sink_name; | |
| 173 pa_threaded_mainloop* pa_mainloop = | |
| 174 static_cast<pa_threaded_mainloop*>(mainloop); | |
| 175 pa_threaded_mainloop_signal(pa_mainloop, 0); | |
| 176 } | |
| 177 | |
| 178 void GetSystemDefaultDevice(pa_threaded_mainloop* mainloop, | |
| 179 pa_context* context) { | |
| 180 DCHECK(mainloop); | |
| 181 DCHECK(context); | |
| 182 | |
| 183 pa_operation* operation = pa_context_get_server_info( | |
| 184 context, SystemDefaultDeviceCallback, mainloop); | |
| 185 | |
| 186 WaitForOperationCompletion(mainloop, operation); | |
| 187 } | |
| 188 | |
| 164 bool CreateInputStream(pa_threaded_mainloop* mainloop, | 189 bool CreateInputStream(pa_threaded_mainloop* mainloop, |
| 165 pa_context* context, | 190 pa_context* context, |
| 166 pa_stream** stream, | 191 pa_stream** stream, |
| 167 const AudioParameters& params, | 192 const AudioParameters& params, |
| 168 const std::string& device_id, | 193 const std::string& device_id, |
| 169 pa_stream_notify_cb_t stream_callback, | 194 pa_stream_notify_cb_t stream_callback, |
| 170 void* user_data) { | 195 void* user_data) { |
| 171 DCHECK(mainloop); | 196 DCHECK(mainloop); |
| 172 DCHECK(context); | 197 DCHECK(context); |
| 173 | 198 |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 199 // Set server-side capture buffer metrics. Detailed documentation on what | 224 // Set server-side capture buffer metrics. Detailed documentation on what |
| 200 // values should be chosen can be found at | 225 // values should be chosen can be found at |
| 201 // freedesktop.org/software/pulseaudio/doxygen/structpa__buffer__attr.html. | 226 // freedesktop.org/software/pulseaudio/doxygen/structpa__buffer__attr.html. |
| 202 pa_buffer_attr buffer_attributes; | 227 pa_buffer_attr buffer_attributes; |
| 203 const unsigned int buffer_size = params.GetBytesPerBuffer(); | 228 const unsigned int buffer_size = params.GetBytesPerBuffer(); |
| 204 buffer_attributes.maxlength = static_cast<uint32_t>(-1); | 229 buffer_attributes.maxlength = static_cast<uint32_t>(-1); |
| 205 buffer_attributes.tlength = buffer_size; | 230 buffer_attributes.tlength = buffer_size; |
| 206 buffer_attributes.minreq = buffer_size; | 231 buffer_attributes.minreq = buffer_size; |
| 207 buffer_attributes.prebuf = static_cast<uint32_t>(-1); | 232 buffer_attributes.prebuf = static_cast<uint32_t>(-1); |
| 208 buffer_attributes.fragsize = buffer_size; | 233 buffer_attributes.fragsize = buffer_size; |
| 234 | |
| 209 int flags = PA_STREAM_AUTO_TIMING_UPDATE | | 235 int flags = PA_STREAM_AUTO_TIMING_UPDATE | |
| 210 PA_STREAM_INTERPOLATE_TIMING | | 236 PA_STREAM_INTERPOLATE_TIMING | |
| 211 PA_STREAM_ADJUST_LATENCY | | 237 PA_STREAM_ADJUST_LATENCY | |
| 212 PA_STREAM_START_CORKED; | 238 PA_STREAM_START_CORKED; |
| 239 | |
| 240 GetSystemDefaultDevice(mainloop, context); | |
| 241 | |
| 213 RETURN_ON_FAILURE( | 242 RETURN_ON_FAILURE( |
| 214 pa_stream_connect_record( | 243 pa_stream_connect_record( |
| 215 *stream, | 244 *stream, |
| 216 device_id == AudioManagerBase::kDefaultDeviceId ? | 245 device_id == AudioManagerBase::kDefaultDeviceId ? |
| 217 NULL : device_id.c_str(), | 246 system_default_input_device.c_str() : device_id.c_str(), |
| 218 &buffer_attributes, | 247 &buffer_attributes, |
| 219 static_cast<pa_stream_flags_t>(flags)) == 0, | 248 static_cast<pa_stream_flags_t>(flags)) == 0, |
| 220 "pa_stream_connect_record FAILED "); | 249 "pa_stream_connect_record FAILED "); |
| 221 | 250 |
| 222 // Wait for the stream to be ready. | 251 // Wait for the stream to be ready. |
| 223 while (true) { | 252 while (true) { |
| 224 pa_stream_state_t stream_state = pa_stream_get_state(*stream); | 253 pa_stream_state_t stream_state = pa_stream_get_state(*stream); |
| 225 RETURN_ON_FAILURE( | 254 RETURN_ON_FAILURE( |
| 226 PA_STREAM_IS_GOOD(stream_state), "Invalid PulseAudio stream state"); | 255 PA_STREAM_IS_GOOD(stream_state), "Invalid PulseAudio stream state"); |
| 227 if (stream_state == PA_STREAM_READY) | 256 if (stream_state == PA_STREAM_READY) |
| (...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 320 // Setting |minreq| to the exact buffer size leads to more callbacks than | 349 // Setting |minreq| to the exact buffer size leads to more callbacks than |
| 321 // necessary, so we've clipped it to half the buffer size. Regardless of the | 350 // necessary, so we've clipped it to half the buffer size. Regardless of the |
| 322 // requested amount, we'll always fill |params.GetBytesPerBuffer()| though. | 351 // requested amount, we'll always fill |params.GetBytesPerBuffer()| though. |
| 323 pa_buffer_attr pa_buffer_attributes; | 352 pa_buffer_attr pa_buffer_attributes; |
| 324 pa_buffer_attributes.maxlength = static_cast<uint32_t>(-1); | 353 pa_buffer_attributes.maxlength = static_cast<uint32_t>(-1); |
| 325 pa_buffer_attributes.minreq = params.GetBytesPerBuffer() / 2; | 354 pa_buffer_attributes.minreq = params.GetBytesPerBuffer() / 2; |
| 326 pa_buffer_attributes.prebuf = static_cast<uint32_t>(-1); | 355 pa_buffer_attributes.prebuf = static_cast<uint32_t>(-1); |
| 327 pa_buffer_attributes.tlength = params.GetBytesPerBuffer() * 3; | 356 pa_buffer_attributes.tlength = params.GetBytesPerBuffer() * 3; |
| 328 pa_buffer_attributes.fragsize = static_cast<uint32_t>(-1); | 357 pa_buffer_attributes.fragsize = static_cast<uint32_t>(-1); |
| 329 | 358 |
| 359 GetSystemDefaultDevice(*mainloop, *context); | |
| 360 | |
| 330 // Connect playback stream. Like pa_buffer_attr, the pa_stream_flags have a | 361 // Connect playback stream. Like pa_buffer_attr, the pa_stream_flags have a |
| 331 // huge impact on the performance of the stream and were chosen through trial | 362 // huge impact on the performance of the stream and were chosen through trial |
| 332 // and error. | 363 // and error. |
| 333 RETURN_ON_FAILURE( | 364 RETURN_ON_FAILURE( |
| 334 pa_stream_connect_playback( | 365 pa_stream_connect_playback( |
| 335 *stream, | 366 *stream, |
| 336 device_id == AudioManagerBase::kDefaultDeviceId ? | 367 device_id == AudioManagerBase::kDefaultDeviceId ? |
| 337 NULL : device_id.c_str(), | 368 system_default_output_device.c_str() : device_id.c_str(), |
| 338 &pa_buffer_attributes, | 369 &pa_buffer_attributes, |
| 339 static_cast<pa_stream_flags_t>( | 370 static_cast<pa_stream_flags_t>( |
| 340 PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_ADJUST_LATENCY | | 371 PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_ADJUST_LATENCY | |
| 341 PA_STREAM_AUTO_TIMING_UPDATE | PA_STREAM_NOT_MONOTONIC | | 372 PA_STREAM_AUTO_TIMING_UPDATE | PA_STREAM_NOT_MONOTONIC | |
| 342 PA_STREAM_START_CORKED), | 373 PA_STREAM_START_CORKED), |
| 343 NULL, | 374 NULL, |
| 344 NULL) == 0, | 375 NULL) == 0, |
| 345 "pa_stream_connect_playback FAILED "); | 376 "pa_stream_connect_playback FAILED "); |
| 346 | 377 |
| 347 // Wait for the stream to be ready. | 378 // Wait for the stream to be ready. |
| 348 while (true) { | 379 while (true) { |
| 349 pa_stream_state_t stream_state = pa_stream_get_state(*stream); | 380 pa_stream_state_t stream_state = pa_stream_get_state(*stream); |
| 350 RETURN_ON_FAILURE( | 381 RETURN_ON_FAILURE( |
| 351 PA_STREAM_IS_GOOD(stream_state), "Invalid PulseAudio stream state"); | 382 PA_STREAM_IS_GOOD(stream_state), "Invalid PulseAudio stream state"); |
| 352 if (stream_state == PA_STREAM_READY) | 383 if (stream_state == PA_STREAM_READY) |
| 353 break; | 384 break; |
| 354 pa_threaded_mainloop_wait(*mainloop); | 385 pa_threaded_mainloop_wait(*mainloop); |
| 355 } | 386 } |
| 356 | 387 |
| 357 return true; | 388 return true; |
| 358 } | 389 } |
| 359 | 390 |
| 360 #undef RETURN_ON_FAILURE | 391 #undef RETURN_ON_FAILURE |
| 361 | 392 |
| 362 } // namespace pulse | 393 } // namespace pulse |
| 363 | 394 |
| 364 } // namespace media | 395 } // namespace media |
| OLD | NEW |