| 1 |
#if defined (UNIX) && defined (ALSA) |
| 2 |
/* |
| 3 |
* $Id$ |
| 4 |
* PortAudio Portable Real-Time Audio Library |
| 5 |
* Latest Version at: http://www.portaudio.com |
| 6 |
* ALSA implementation by Joshua Haberman and Arve Knudsen |
| 7 |
* |
| 8 |
* Copyright (c) 2002 Joshua Haberman <[email protected]> |
| 9 |
* Copyright (c) 2005-2009 Arve Knudsen <[email protected]> |
| 10 |
* Copyright (c) 2008 Kevin Kofler <[email protected]> |
| 11 |
* |
| 12 |
* Based on the Open Source API proposed by Ross Bencina |
| 13 |
* Copyright (c) 1999-2002 Ross Bencina, Phil Burk |
| 14 |
* |
| 15 |
* Permission is hereby granted, free of charge, to any person obtaining |
| 16 |
* a copy of this software and associated documentation files |
| 17 |
* (the "Software"), to deal in the Software without restriction, |
| 18 |
* including without limitation the rights to use, copy, modify, merge, |
| 19 |
* publish, distribute, sublicense, and/or sell copies of the Software, |
| 20 |
* and to permit persons to whom the Software is furnished to do so, |
| 21 |
* subject to the following conditions: |
| 22 |
* |
| 23 |
* The above copyright notice and this permission notice shall be |
| 24 |
* included in all copies or substantial portions of the Software. |
| 25 |
* |
| 26 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 27 |
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 28 |
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 29 |
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR |
| 30 |
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 31 |
* CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 32 |
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 33 |
*/ |
| 34 |
|
| 35 |
/* |
| 36 |
* The text above constitutes the entire PortAudio license; however, |
| 37 |
* the PortAudio community also makes the following non-binding requests: |
| 38 |
* |
| 39 |
* Any person wishing to distribute modifications to the Software is |
| 40 |
* requested to send the modifications to the original developer so that |
| 41 |
* they can be incorporated into the canonical version. It is also |
| 42 |
* requested that these non-binding requests be included along with the |
| 43 |
* license above. |
| 44 |
*/ |
| 45 |
|
| 46 |
/** |
| 47 |
@file |
| 48 |
@ingroup hostapi_src |
| 49 |
*/ |
| 50 |
|
| 51 |
#define ALSA_PCM_NEW_HW_PARAMS_API |
| 52 |
#define ALSA_PCM_NEW_SW_PARAMS_API |
| 53 |
#include <alsa/asoundlib.h> |
| 54 |
#undef ALSA_PCM_NEW_HW_PARAMS_API |
| 55 |
#undef ALSA_PCM_NEW_SW_PARAMS_API |
| 56 |
|
| 57 |
#include <sys/poll.h> |
| 58 |
#include <string.h> /* strlen() */ |
| 59 |
#include <limits.h> |
| 60 |
#include <math.h> |
| 61 |
#include <pthread.h> |
| 62 |
#include <signal.h> |
| 63 |
#include <time.h> |
| 64 |
#include <sys/mman.h> |
| 65 |
#include <signal.h> /* For sig_atomic_t */ |
| 66 |
#ifdef PA_ALSA_DYNAMIC |
| 67 |
#include <dlfcn.h> /* For dlXXX functions */ |
| 68 |
#endif |
| 69 |
|
| 70 |
#include "portaudio.h" |
| 71 |
#include "pa_util.h" |
| 72 |
#include "pa_unix_util.h" |
| 73 |
#include "pa_allocation.h" |
| 74 |
#include "pa_hostapi.h" |
| 75 |
#include "pa_stream.h" |
| 76 |
#include "pa_cpuload.h" |
| 77 |
#include "pa_process.h" |
| 78 |
#include "pa_endianness.h" |
| 79 |
#include "pa_debugprint.h" |
| 80 |
|
| 81 |
#include "pa_linux_alsa.h" |
| 82 |
|
| 83 |
/* Add missing define (for compatibility with older ALSA versions) */ |
| 84 |
#ifndef SND_PCM_TSTAMP_ENABLE |
| 85 |
#define SND_PCM_TSTAMP_ENABLE SND_PCM_TSTAMP_MMAP |
| 86 |
#endif |
| 87 |
|
| 88 |
/* Combine version elements into a single (unsigned) integer */ |
| 89 |
#define ALSA_VERSION_INT(major, minor, subminor) ((major << 16) | (minor << 8) | subminor) |
| 90 |
|
| 91 |
/* The acceptable tolerance of sample rate set, to that requested (as a ratio, eg 50 is 2%, 100 is 1%) */ |
| 92 |
#define RATE_MAX_DEVIATE_RATIO 100 |
| 93 |
|
| 94 |
/* Defines Alsa function types and pointers to these functions. */ |
| 95 |
#define _PA_DEFINE_FUNC(x) typedef typeof(x) x##_ft; static x##_ft *alsa_##x = 0 |
| 96 |
|
| 97 |
/* Alloca helper. */ |
| 98 |
#define __alsa_snd_alloca(ptr,type) do { size_t __alsa_alloca_size = alsa_##type##_sizeof(); (*ptr) = (type##_t *) alloca(__alsa_alloca_size); memset(*ptr, 0, __alsa_alloca_size); } while (0) |
| 99 |
|
| 100 |
_PA_DEFINE_FUNC(snd_pcm_open); |
| 101 |
_PA_DEFINE_FUNC(snd_pcm_close); |
| 102 |
_PA_DEFINE_FUNC(snd_pcm_nonblock); |
| 103 |
_PA_DEFINE_FUNC(snd_pcm_frames_to_bytes); |
| 104 |
_PA_DEFINE_FUNC(snd_pcm_prepare); |
| 105 |
_PA_DEFINE_FUNC(snd_pcm_start); |
| 106 |
_PA_DEFINE_FUNC(snd_pcm_resume); |
| 107 |
_PA_DEFINE_FUNC(snd_pcm_wait); |
| 108 |
_PA_DEFINE_FUNC(snd_pcm_state); |
| 109 |
_PA_DEFINE_FUNC(snd_pcm_avail_update); |
| 110 |
_PA_DEFINE_FUNC(snd_pcm_areas_silence); |
| 111 |
_PA_DEFINE_FUNC(snd_pcm_mmap_begin); |
| 112 |
_PA_DEFINE_FUNC(snd_pcm_mmap_commit); |
| 113 |
_PA_DEFINE_FUNC(snd_pcm_readi); |
| 114 |
_PA_DEFINE_FUNC(snd_pcm_readn); |
| 115 |
_PA_DEFINE_FUNC(snd_pcm_writei); |
| 116 |
_PA_DEFINE_FUNC(snd_pcm_writen); |
| 117 |
_PA_DEFINE_FUNC(snd_pcm_drain); |
| 118 |
_PA_DEFINE_FUNC(snd_pcm_recover); |
| 119 |
_PA_DEFINE_FUNC(snd_pcm_drop); |
| 120 |
_PA_DEFINE_FUNC(snd_pcm_area_copy); |
| 121 |
_PA_DEFINE_FUNC(snd_pcm_poll_descriptors); |
| 122 |
_PA_DEFINE_FUNC(snd_pcm_poll_descriptors_count); |
| 123 |
_PA_DEFINE_FUNC(snd_pcm_poll_descriptors_revents); |
| 124 |
_PA_DEFINE_FUNC(snd_pcm_format_size); |
| 125 |
_PA_DEFINE_FUNC(snd_pcm_link); |
| 126 |
_PA_DEFINE_FUNC(snd_pcm_delay); |
| 127 |
|
| 128 |
_PA_DEFINE_FUNC(snd_pcm_hw_params_sizeof); |
| 129 |
_PA_DEFINE_FUNC(snd_pcm_hw_params_malloc); |
| 130 |
_PA_DEFINE_FUNC(snd_pcm_hw_params_free); |
| 131 |
_PA_DEFINE_FUNC(snd_pcm_hw_params_any); |
| 132 |
_PA_DEFINE_FUNC(snd_pcm_hw_params_set_access); |
| 133 |
_PA_DEFINE_FUNC(snd_pcm_hw_params_set_format); |
| 134 |
_PA_DEFINE_FUNC(snd_pcm_hw_params_set_channels); |
| 135 |
//_PA_DEFINE_FUNC(snd_pcm_hw_params_set_periods_near); |
| 136 |
_PA_DEFINE_FUNC(snd_pcm_hw_params_set_rate_near); //!!! |
| 137 |
_PA_DEFINE_FUNC(snd_pcm_hw_params_set_rate); |
| 138 |
_PA_DEFINE_FUNC(snd_pcm_hw_params_set_rate_resample); |
| 139 |
//_PA_DEFINE_FUNC(snd_pcm_hw_params_set_buffer_time_near); |
| 140 |
_PA_DEFINE_FUNC(snd_pcm_hw_params_set_buffer_size); |
| 141 |
_PA_DEFINE_FUNC(snd_pcm_hw_params_set_buffer_size_near); //!!! |
| 142 |
_PA_DEFINE_FUNC(snd_pcm_hw_params_set_buffer_size_min); |
| 143 |
//_PA_DEFINE_FUNC(snd_pcm_hw_params_set_period_time_near); |
| 144 |
_PA_DEFINE_FUNC(snd_pcm_hw_params_set_period_size_near); |
| 145 |
_PA_DEFINE_FUNC(snd_pcm_hw_params_set_periods_integer); |
| 146 |
_PA_DEFINE_FUNC(snd_pcm_hw_params_set_periods_min); |
| 147 |
|
| 148 |
_PA_DEFINE_FUNC(snd_pcm_hw_params_get_buffer_size); |
| 149 |
//_PA_DEFINE_FUNC(snd_pcm_hw_params_get_period_size); |
| 150 |
//_PA_DEFINE_FUNC(snd_pcm_hw_params_get_access); |
| 151 |
//_PA_DEFINE_FUNC(snd_pcm_hw_params_get_periods); |
| 152 |
//_PA_DEFINE_FUNC(snd_pcm_hw_params_get_rate); |
| 153 |
_PA_DEFINE_FUNC(snd_pcm_hw_params_get_channels_min); |
| 154 |
_PA_DEFINE_FUNC(snd_pcm_hw_params_get_channels_max); |
| 155 |
|
| 156 |
_PA_DEFINE_FUNC(snd_pcm_hw_params_test_period_size); |
| 157 |
_PA_DEFINE_FUNC(snd_pcm_hw_params_test_format); |
| 158 |
_PA_DEFINE_FUNC(snd_pcm_hw_params_test_access); |
| 159 |
_PA_DEFINE_FUNC(snd_pcm_hw_params_dump); |
| 160 |
_PA_DEFINE_FUNC(snd_pcm_hw_params); |
| 161 |
|
| 162 |
_PA_DEFINE_FUNC(snd_pcm_hw_params_get_periods_min); |
| 163 |
_PA_DEFINE_FUNC(snd_pcm_hw_params_get_periods_max); |
| 164 |
_PA_DEFINE_FUNC(snd_pcm_hw_params_set_period_size); |
| 165 |
_PA_DEFINE_FUNC(snd_pcm_hw_params_get_period_size_min); |
| 166 |
_PA_DEFINE_FUNC(snd_pcm_hw_params_get_period_size_max); |
| 167 |
_PA_DEFINE_FUNC(snd_pcm_hw_params_get_buffer_size_max); |
| 168 |
_PA_DEFINE_FUNC(snd_pcm_hw_params_get_rate_min); |
| 169 |
_PA_DEFINE_FUNC(snd_pcm_hw_params_get_rate_max); |
| 170 |
_PA_DEFINE_FUNC(snd_pcm_hw_params_get_rate_numden); |
| 171 |
#define alsa_snd_pcm_hw_params_alloca(ptr) __alsa_snd_alloca(ptr, snd_pcm_hw_params) |
| 172 |
|
| 173 |
_PA_DEFINE_FUNC(snd_pcm_sw_params_sizeof); |
| 174 |
_PA_DEFINE_FUNC(snd_pcm_sw_params_malloc); |
| 175 |
_PA_DEFINE_FUNC(snd_pcm_sw_params_current); |
| 176 |
_PA_DEFINE_FUNC(snd_pcm_sw_params_set_avail_min); |
| 177 |
_PA_DEFINE_FUNC(snd_pcm_sw_params); |
| 178 |
_PA_DEFINE_FUNC(snd_pcm_sw_params_free); |
| 179 |
_PA_DEFINE_FUNC(snd_pcm_sw_params_set_start_threshold); |
| 180 |
_PA_DEFINE_FUNC(snd_pcm_sw_params_set_stop_threshold); |
| 181 |
_PA_DEFINE_FUNC(snd_pcm_sw_params_get_boundary); |
| 182 |
_PA_DEFINE_FUNC(snd_pcm_sw_params_set_silence_threshold); |
| 183 |
_PA_DEFINE_FUNC(snd_pcm_sw_params_set_silence_size); |
| 184 |
_PA_DEFINE_FUNC(snd_pcm_sw_params_set_xfer_align); |
| 185 |
_PA_DEFINE_FUNC(snd_pcm_sw_params_set_tstamp_mode); |
| 186 |
#define alsa_snd_pcm_sw_params_alloca(ptr) __alsa_snd_alloca(ptr, snd_pcm_sw_params) |
| 187 |
|
| 188 |
_PA_DEFINE_FUNC(snd_pcm_info); |
| 189 |
_PA_DEFINE_FUNC(snd_pcm_info_sizeof); |
| 190 |
_PA_DEFINE_FUNC(snd_pcm_info_malloc); |
| 191 |
_PA_DEFINE_FUNC(snd_pcm_info_free); |
| 192 |
_PA_DEFINE_FUNC(snd_pcm_info_set_device); |
| 193 |
_PA_DEFINE_FUNC(snd_pcm_info_set_subdevice); |
| 194 |
_PA_DEFINE_FUNC(snd_pcm_info_set_stream); |
| 195 |
_PA_DEFINE_FUNC(snd_pcm_info_get_name); |
| 196 |
_PA_DEFINE_FUNC(snd_pcm_info_get_card); |
| 197 |
#define alsa_snd_pcm_info_alloca(ptr) __alsa_snd_alloca(ptr, snd_pcm_info) |
| 198 |
|
| 199 |
_PA_DEFINE_FUNC(snd_ctl_pcm_next_device); |
| 200 |
_PA_DEFINE_FUNC(snd_ctl_pcm_info); |
| 201 |
_PA_DEFINE_FUNC(snd_ctl_open); |
| 202 |
_PA_DEFINE_FUNC(snd_ctl_close); |
| 203 |
_PA_DEFINE_FUNC(snd_ctl_card_info_malloc); |
| 204 |
_PA_DEFINE_FUNC(snd_ctl_card_info_free); |
| 205 |
_PA_DEFINE_FUNC(snd_ctl_card_info); |
| 206 |
_PA_DEFINE_FUNC(snd_ctl_card_info_sizeof); |
| 207 |
_PA_DEFINE_FUNC(snd_ctl_card_info_get_name); |
| 208 |
#define alsa_snd_ctl_card_info_alloca(ptr) __alsa_snd_alloca(ptr, snd_ctl_card_info) |
| 209 |
|
| 210 |
_PA_DEFINE_FUNC(snd_config); |
| 211 |
_PA_DEFINE_FUNC(snd_config_update); |
| 212 |
_PA_DEFINE_FUNC(snd_config_search); |
| 213 |
_PA_DEFINE_FUNC(snd_config_iterator_entry); |
| 214 |
_PA_DEFINE_FUNC(snd_config_iterator_first); |
| 215 |
_PA_DEFINE_FUNC(snd_config_iterator_end); |
| 216 |
_PA_DEFINE_FUNC(snd_config_iterator_next); |
| 217 |
_PA_DEFINE_FUNC(snd_config_get_string); |
| 218 |
_PA_DEFINE_FUNC(snd_config_get_id); |
| 219 |
_PA_DEFINE_FUNC(snd_config_update_free_global); |
| 220 |
|
| 221 |
_PA_DEFINE_FUNC(snd_pcm_status); |
| 222 |
_PA_DEFINE_FUNC(snd_pcm_status_sizeof); |
| 223 |
_PA_DEFINE_FUNC(snd_pcm_status_get_tstamp); |
| 224 |
_PA_DEFINE_FUNC(snd_pcm_status_get_state); |
| 225 |
_PA_DEFINE_FUNC(snd_pcm_status_get_trigger_tstamp); |
| 226 |
_PA_DEFINE_FUNC(snd_pcm_status_get_delay); |
| 227 |
#define alsa_snd_pcm_status_alloca(ptr) __alsa_snd_alloca(ptr, snd_pcm_status) |
| 228 |
|
| 229 |
_PA_DEFINE_FUNC(snd_card_next); |
| 230 |
_PA_DEFINE_FUNC(snd_asoundlib_version); |
| 231 |
_PA_DEFINE_FUNC(snd_strerror); |
| 232 |
_PA_DEFINE_FUNC(snd_output_stdio_attach); |
| 233 |
|
| 234 |
#define alsa_snd_config_for_each(pos, next, node)\ |
| 235 |
for (pos = alsa_snd_config_iterator_first(node),\ |
| 236 |
next = alsa_snd_config_iterator_next(pos);\ |
| 237 |
pos != alsa_snd_config_iterator_end(node); pos = next, next = alsa_snd_config_iterator_next(pos)) |
| 238 |
|
| 239 |
#undef _PA_DEFINE_FUNC |
| 240 |
|
| 241 |
/* Redefine 'PA_ALSA_PATHNAME' to a different Alsa library name if desired. */ |
| 242 |
#ifndef PA_ALSA_PATHNAME |
| 243 |
#define PA_ALSA_PATHNAME "libasound.so" |
| 244 |
#endif |
| 245 |
static const char *g_AlsaLibName = PA_ALSA_PATHNAME; |
| 246 |
|
| 247 |
/* Handle to dynamically loaded library. */ |
| 248 |
static void *g_AlsaLib = NULL; |
| 249 |
|
| 250 |
#ifdef PA_ALSA_DYNAMIC |
| 251 |
|
| 252 |
#define _PA_LOCAL_IMPL(x) __pa_local_##x |
| 253 |
|
| 254 |
int _PA_LOCAL_IMPL(snd_pcm_hw_params_set_rate_near) (snd_pcm_t *pcm, snd_pcm_hw_params_t *params, unsigned int *val, int *dir) |
| 255 |
{ |
| 256 |
int ret; |
| 257 |
|
| 258 |
if(( ret = alsa_snd_pcm_hw_params_set_rate(pcm, params, (*val), (*dir)) ) < 0 ) |
| 259 |
return ret; |
| 260 |
|
| 261 |
return 0; |
| 262 |
} |
| 263 |
|
| 264 |
int _PA_LOCAL_IMPL(snd_pcm_hw_params_set_buffer_size_near) (snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val) |
| 265 |
{ |
| 266 |
int ret; |
| 267 |
|
| 268 |
if(( ret = alsa_snd_pcm_hw_params_set_buffer_size(pcm, params, (*val)) ) < 0 ) |
| 269 |
return ret; |
| 270 |
|
| 271 |
return 0; |
| 272 |
} |
| 273 |
|
| 274 |
int _PA_LOCAL_IMPL(snd_pcm_hw_params_set_period_size_near) (snd_pcm_t *pcm, snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val, int *dir) |
| 275 |
{ |
| 276 |
int ret; |
| 277 |
|
| 278 |
if(( ret = alsa_snd_pcm_hw_params_set_period_size(pcm, params, (*val), (*dir)) ) < 0 ) |
| 279 |
return ret; |
| 280 |
|
| 281 |
return 0; |
| 282 |
} |
| 283 |
|
| 284 |
int _PA_LOCAL_IMPL(snd_pcm_hw_params_get_channels_min) (const snd_pcm_hw_params_t *params, unsigned int *val) |
| 285 |
{ |
| 286 |
(*val) = 1; |
| 287 |
return 0; |
| 288 |
} |
| 289 |
|
| 290 |
int _PA_LOCAL_IMPL(snd_pcm_hw_params_get_channels_max) (const snd_pcm_hw_params_t *params, unsigned int *val) |
| 291 |
{ |
| 292 |
(*val) = 2; |
| 293 |
return 0; |
| 294 |
} |
| 295 |
|
| 296 |
int _PA_LOCAL_IMPL(snd_pcm_hw_params_get_periods_min) (const snd_pcm_hw_params_t *params, unsigned int *val, int *dir) |
| 297 |
{ |
| 298 |
(*val) = 2; |
| 299 |
return 0; |
| 300 |
} |
| 301 |
|
| 302 |
int _PA_LOCAL_IMPL(snd_pcm_hw_params_get_periods_max) (const snd_pcm_hw_params_t *params, unsigned int *val, int *dir) |
| 303 |
{ |
| 304 |
(*val) = 8; |
| 305 |
return 0; |
| 306 |
} |
| 307 |
|
| 308 |
int _PA_LOCAL_IMPL(snd_pcm_hw_params_get_period_size_min) (const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *frames, int *dir) |
| 309 |
{ |
| 310 |
(*frames) = 64; |
| 311 |
return 0; |
| 312 |
} |
| 313 |
|
| 314 |
int _PA_LOCAL_IMPL(snd_pcm_hw_params_get_period_size_max) (const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *frames, int *dir) |
| 315 |
{ |
| 316 |
(*frames) = 512; |
| 317 |
return 0; |
| 318 |
} |
| 319 |
|
| 320 |
int _PA_LOCAL_IMPL(snd_pcm_hw_params_get_buffer_size_max) (const snd_pcm_hw_params_t *params, snd_pcm_uframes_t *val) |
| 321 |
{ |
| 322 |
int ret; |
| 323 |
int dir = 0; |
| 324 |
snd_pcm_uframes_t pmax = 0; |
| 325 |
unsigned int pcnt = 0; |
| 326 |
|
| 327 |
dir = 0; |
| 328 |
if(( ret = _PA_LOCAL_IMPL(snd_pcm_hw_params_get_period_size_max)(params, &pmax, &dir) ) < 0 ) |
| 329 |
return ret; |
| 330 |
dir = 0; |
| 331 |
if(( ret = _PA_LOCAL_IMPL(snd_pcm_hw_params_get_periods_max)(params, &pcnt, &dir) ) < 0 ) |
| 332 |
return ret; |
| 333 |
|
| 334 |
(*val) = pmax * pcnt; |
| 335 |
return 0; |
| 336 |
} |
| 337 |
|
| 338 |
int _PA_LOCAL_IMPL(snd_pcm_hw_params_get_rate_min) (const snd_pcm_hw_params_t *params, unsigned int *val, int *dir) |
| 339 |
{ |
| 340 |
(*val) = 44100; |
| 341 |
return 0; |
| 342 |
} |
| 343 |
|
| 344 |
int _PA_LOCAL_IMPL(snd_pcm_hw_params_get_rate_max) (const snd_pcm_hw_params_t *params, unsigned int *val, int *dir) |
| 345 |
{ |
| 346 |
(*val) = 44100; |
| 347 |
return 0; |
| 348 |
} |
| 349 |
|
| 350 |
#endif // PA_ALSA_DYNAMIC |
| 351 |
|
| 352 |
/* Trying to load Alsa library dynamically if 'PA_ALSA_DYNAMIC' is defined, othervise |
| 353 |
will link during compilation. |
| 354 |
*/ |
| 355 |
static int PaAlsa_LoadLibrary() |
| 356 |
{ |
| 357 |
#ifdef PA_ALSA_DYNAMIC |
| 358 |
|
| 359 |
PA_DEBUG(( "%s: loading ALSA library file - %s\n", __FUNCTION__, g_AlsaLibName )); |
| 360 |
|
| 361 |
dlerror(); |
| 362 |
g_AlsaLib = dlopen(g_AlsaLibName, (RTLD_NOW|RTLD_GLOBAL) ); |
| 363 |
if (g_AlsaLib == NULL) |
| 364 |
{ |
| 365 |
PA_DEBUG(( "%s: failed dlopen() ALSA library file - %s, error: %s\n", __FUNCTION__, g_AlsaLibName, dlerror() )); |
| 366 |
return 0; |
| 367 |
} |
| 368 |
|
| 369 |
PA_DEBUG(( "%s: loading ALSA API\n", __FUNCTION__ )); |
| 370 |
|
| 371 |
#define _PA_LOAD_FUNC(x) do { \ |
| 372 |
alsa_##x = dlsym( g_AlsaLib, #x ); \ |
| 373 |
if( alsa_##x == NULL ) { \ |
| 374 |
PA_DEBUG(( "%s: symbol [%s] not found in - %s, error: %s\n", __FUNCTION__, #x, g_AlsaLibName, dlerror() )); }\ |
| 375 |
} while(0) |
| 376 |
|
| 377 |
#else |
| 378 |
|
| 379 |
#define _PA_LOAD_FUNC(x) alsa_##x = &x |
| 380 |
|
| 381 |
#endif |
| 382 |
|
| 383 |
_PA_LOAD_FUNC(snd_pcm_open); |
| 384 |
_PA_LOAD_FUNC(snd_pcm_close); |
| 385 |
_PA_LOAD_FUNC(snd_pcm_nonblock); |
| 386 |
_PA_LOAD_FUNC(snd_pcm_frames_to_bytes); |
| 387 |
_PA_LOAD_FUNC(snd_pcm_prepare); |
| 388 |
_PA_LOAD_FUNC(snd_pcm_start); |
| 389 |
_PA_LOAD_FUNC(snd_pcm_resume); |
| 390 |
_PA_LOAD_FUNC(snd_pcm_wait); |
| 391 |
_PA_LOAD_FUNC(snd_pcm_state); |
| 392 |
_PA_LOAD_FUNC(snd_pcm_avail_update); |
| 393 |
_PA_LOAD_FUNC(snd_pcm_areas_silence); |
| 394 |
_PA_LOAD_FUNC(snd_pcm_mmap_begin); |
| 395 |
_PA_LOAD_FUNC(snd_pcm_mmap_commit); |
| 396 |
_PA_LOAD_FUNC(snd_pcm_readi); |
| 397 |
_PA_LOAD_FUNC(snd_pcm_readn); |
| 398 |
_PA_LOAD_FUNC(snd_pcm_writei); |
| 399 |
_PA_LOAD_FUNC(snd_pcm_writen); |
| 400 |
_PA_LOAD_FUNC(snd_pcm_drain); |
| 401 |
_PA_LOAD_FUNC(snd_pcm_recover); |
| 402 |
_PA_LOAD_FUNC(snd_pcm_drop); |
| 403 |
_PA_LOAD_FUNC(snd_pcm_area_copy); |
| 404 |
_PA_LOAD_FUNC(snd_pcm_poll_descriptors); |
| 405 |
_PA_LOAD_FUNC(snd_pcm_poll_descriptors_count); |
| 406 |
_PA_LOAD_FUNC(snd_pcm_poll_descriptors_revents); |
| 407 |
_PA_LOAD_FUNC(snd_pcm_format_size); |
| 408 |
_PA_LOAD_FUNC(snd_pcm_link); |
| 409 |
_PA_LOAD_FUNC(snd_pcm_delay); |
| 410 |
|
| 411 |
_PA_LOAD_FUNC(snd_pcm_hw_params_sizeof); |
| 412 |
_PA_LOAD_FUNC(snd_pcm_hw_params_malloc); |
| 413 |
_PA_LOAD_FUNC(snd_pcm_hw_params_free); |
| 414 |
_PA_LOAD_FUNC(snd_pcm_hw_params_any); |
| 415 |
_PA_LOAD_FUNC(snd_pcm_hw_params_set_access); |
| 416 |
_PA_LOAD_FUNC(snd_pcm_hw_params_set_format); |
| 417 |
_PA_LOAD_FUNC(snd_pcm_hw_params_set_channels); |
| 418 |
// _PA_LOAD_FUNC(snd_pcm_hw_params_set_periods_near); |
| 419 |
_PA_LOAD_FUNC(snd_pcm_hw_params_set_rate_near); |
| 420 |
_PA_LOAD_FUNC(snd_pcm_hw_params_set_rate); |
| 421 |
_PA_LOAD_FUNC(snd_pcm_hw_params_set_rate_resample); |
| 422 |
// _PA_LOAD_FUNC(snd_pcm_hw_params_set_buffer_time_near); |
| 423 |
_PA_LOAD_FUNC(snd_pcm_hw_params_set_buffer_size); |
| 424 |
_PA_LOAD_FUNC(snd_pcm_hw_params_set_buffer_size_near); |
| 425 |
_PA_LOAD_FUNC(snd_pcm_hw_params_set_buffer_size_min); |
| 426 |
// _PA_LOAD_FUNC(snd_pcm_hw_params_set_period_time_near); |
| 427 |
_PA_LOAD_FUNC(snd_pcm_hw_params_set_period_size_near); |
| 428 |
_PA_LOAD_FUNC(snd_pcm_hw_params_set_periods_integer); |
| 429 |
_PA_LOAD_FUNC(snd_pcm_hw_params_set_periods_min); |
| 430 |
|
| 431 |
_PA_LOAD_FUNC(snd_pcm_hw_params_get_buffer_size); |
| 432 |
// _PA_LOAD_FUNC(snd_pcm_hw_params_get_period_size); |
| 433 |
// _PA_LOAD_FUNC(snd_pcm_hw_params_get_access); |
| 434 |
// _PA_LOAD_FUNC(snd_pcm_hw_params_get_periods); |
| 435 |
// _PA_LOAD_FUNC(snd_pcm_hw_params_get_rate); |
| 436 |
_PA_LOAD_FUNC(snd_pcm_hw_params_get_channels_min); |
| 437 |
_PA_LOAD_FUNC(snd_pcm_hw_params_get_channels_max); |
| 438 |
|
| 439 |
_PA_LOAD_FUNC(snd_pcm_hw_params_test_period_size); |
| 440 |
_PA_LOAD_FUNC(snd_pcm_hw_params_test_format); |
| 441 |
_PA_LOAD_FUNC(snd_pcm_hw_params_test_access); |
| 442 |
_PA_LOAD_FUNC(snd_pcm_hw_params_dump); |
| 443 |
_PA_LOAD_FUNC(snd_pcm_hw_params); |
| 444 |
|
| 445 |
_PA_LOAD_FUNC(snd_pcm_hw_params_get_periods_min); |
| 446 |
_PA_LOAD_FUNC(snd_pcm_hw_params_get_periods_max); |
| 447 |
_PA_LOAD_FUNC(snd_pcm_hw_params_set_period_size); |
| 448 |
_PA_LOAD_FUNC(snd_pcm_hw_params_get_period_size_min); |
| 449 |
_PA_LOAD_FUNC(snd_pcm_hw_params_get_period_size_max); |
| 450 |
_PA_LOAD_FUNC(snd_pcm_hw_params_get_buffer_size_max); |
| 451 |
_PA_LOAD_FUNC(snd_pcm_hw_params_get_rate_min); |
| 452 |
_PA_LOAD_FUNC(snd_pcm_hw_params_get_rate_max); |
| 453 |
_PA_LOAD_FUNC(snd_pcm_hw_params_get_rate_numden); |
| 454 |
|
| 455 |
_PA_LOAD_FUNC(snd_pcm_sw_params_sizeof); |
| 456 |
_PA_LOAD_FUNC(snd_pcm_sw_params_malloc); |
| 457 |
_PA_LOAD_FUNC(snd_pcm_sw_params_current); |
| 458 |
_PA_LOAD_FUNC(snd_pcm_sw_params_set_avail_min); |
| 459 |
_PA_LOAD_FUNC(snd_pcm_sw_params); |
| 460 |
_PA_LOAD_FUNC(snd_pcm_sw_params_free); |
| 461 |
_PA_LOAD_FUNC(snd_pcm_sw_params_set_start_threshold); |
| 462 |
_PA_LOAD_FUNC(snd_pcm_sw_params_set_stop_threshold); |
| 463 |
_PA_LOAD_FUNC(snd_pcm_sw_params_get_boundary); |
| 464 |
_PA_LOAD_FUNC(snd_pcm_sw_params_set_silence_threshold); |
| 465 |
_PA_LOAD_FUNC(snd_pcm_sw_params_set_silence_size); |
| 466 |
_PA_LOAD_FUNC(snd_pcm_sw_params_set_xfer_align); |
| 467 |
_PA_LOAD_FUNC(snd_pcm_sw_params_set_tstamp_mode); |
| 468 |
|
| 469 |
_PA_LOAD_FUNC(snd_pcm_info); |
| 470 |
_PA_LOAD_FUNC(snd_pcm_info_sizeof); |
| 471 |
_PA_LOAD_FUNC(snd_pcm_info_malloc); |
| 472 |
_PA_LOAD_FUNC(snd_pcm_info_free); |
| 473 |
_PA_LOAD_FUNC(snd_pcm_info_set_device); |
| 474 |
_PA_LOAD_FUNC(snd_pcm_info_set_subdevice); |
| 475 |
_PA_LOAD_FUNC(snd_pcm_info_set_stream); |
| 476 |
_PA_LOAD_FUNC(snd_pcm_info_get_name); |
| 477 |
_PA_LOAD_FUNC(snd_pcm_info_get_card); |
| 478 |
|
| 479 |
_PA_LOAD_FUNC(snd_ctl_pcm_next_device); |
| 480 |
_PA_LOAD_FUNC(snd_ctl_pcm_info); |
| 481 |
_PA_LOAD_FUNC(snd_ctl_open); |
| 482 |
_PA_LOAD_FUNC(snd_ctl_close); |
| 483 |
_PA_LOAD_FUNC(snd_ctl_card_info_malloc); |
| 484 |
_PA_LOAD_FUNC(snd_ctl_card_info_free); |
| 485 |
_PA_LOAD_FUNC(snd_ctl_card_info); |
| 486 |
_PA_LOAD_FUNC(snd_ctl_card_info_sizeof); |
| 487 |
_PA_LOAD_FUNC(snd_ctl_card_info_get_name); |
| 488 |
|
| 489 |
_PA_LOAD_FUNC(snd_config); |
| 490 |
_PA_LOAD_FUNC(snd_config_update); |
| 491 |
_PA_LOAD_FUNC(snd_config_search); |
| 492 |
_PA_LOAD_FUNC(snd_config_iterator_entry); |
| 493 |
_PA_LOAD_FUNC(snd_config_iterator_first); |
| 494 |
_PA_LOAD_FUNC(snd_config_iterator_end); |
| 495 |
_PA_LOAD_FUNC(snd_config_iterator_next); |
| 496 |
_PA_LOAD_FUNC(snd_config_get_string); |
| 497 |
_PA_LOAD_FUNC(snd_config_get_id); |
| 498 |
_PA_LOAD_FUNC(snd_config_update_free_global); |
| 499 |
|
| 500 |
_PA_LOAD_FUNC(snd_pcm_status); |
| 501 |
_PA_LOAD_FUNC(snd_pcm_status_sizeof); |
| 502 |
_PA_LOAD_FUNC(snd_pcm_status_get_tstamp); |
| 503 |
_PA_LOAD_FUNC(snd_pcm_status_get_state); |
| 504 |
_PA_LOAD_FUNC(snd_pcm_status_get_trigger_tstamp); |
| 505 |
_PA_LOAD_FUNC(snd_pcm_status_get_delay); |
| 506 |
|
| 507 |
_PA_LOAD_FUNC(snd_card_next); |
| 508 |
_PA_LOAD_FUNC(snd_asoundlib_version); |
| 509 |
_PA_LOAD_FUNC(snd_strerror); |
| 510 |
_PA_LOAD_FUNC(snd_output_stdio_attach); |
| 511 |
#undef _PA_LOAD_FUNC |
| 512 |
|
| 513 |
#ifdef PA_ALSA_DYNAMIC |
| 514 |
PA_DEBUG(( "%s: loaded ALSA API - ok\n", __FUNCTION__ )); |
| 515 |
|
| 516 |
#define _PA_VALIDATE_LOAD_REPLACEMENT(x)\ |
| 517 |
do {\ |
| 518 |
if( alsa_##x == NULL )\ |
| 519 |
{\ |
| 520 |
alsa_##x = &_PA_LOCAL_IMPL(x);\ |
| 521 |
PA_DEBUG(( "%s: replacing [%s] with local implementation\n", __FUNCTION__, #x ));\ |
| 522 |
}\ |
| 523 |
} while (0) |
| 524 |
|
| 525 |
_PA_VALIDATE_LOAD_REPLACEMENT(snd_pcm_hw_params_set_rate_near); |
| 526 |
_PA_VALIDATE_LOAD_REPLACEMENT(snd_pcm_hw_params_set_buffer_size_near); |
| 527 |
_PA_VALIDATE_LOAD_REPLACEMENT(snd_pcm_hw_params_set_period_size_near); |
| 528 |
_PA_VALIDATE_LOAD_REPLACEMENT(snd_pcm_hw_params_get_channels_min); |
| 529 |
_PA_VALIDATE_LOAD_REPLACEMENT(snd_pcm_hw_params_get_channels_max); |
| 530 |
_PA_VALIDATE_LOAD_REPLACEMENT(snd_pcm_hw_params_get_periods_min); |
| 531 |
_PA_VALIDATE_LOAD_REPLACEMENT(snd_pcm_hw_params_get_periods_max); |
| 532 |
_PA_VALIDATE_LOAD_REPLACEMENT(snd_pcm_hw_params_get_period_size_min); |
| 533 |
_PA_VALIDATE_LOAD_REPLACEMENT(snd_pcm_hw_params_get_period_size_max); |
| 534 |
_PA_VALIDATE_LOAD_REPLACEMENT(snd_pcm_hw_params_get_buffer_size_max); |
| 535 |
_PA_VALIDATE_LOAD_REPLACEMENT(snd_pcm_hw_params_get_rate_min); |
| 536 |
_PA_VALIDATE_LOAD_REPLACEMENT(snd_pcm_hw_params_get_rate_max); |
| 537 |
|
| 538 |
#undef _PA_LOCAL_IMPL |
| 539 |
#undef _PA_VALIDATE_LOAD_REPLACEMENT |
| 540 |
|
| 541 |
#endif // PA_ALSA_DYNAMIC |
| 542 |
|
| 543 |
return 1; |
| 544 |
} |
| 545 |
|
| 546 |
void PaAlsa_SetLibraryPathName( const char *pathName ) |
| 547 |
{ |
| 548 |
#ifdef PA_ALSA_DYNAMIC |
| 549 |
g_AlsaLibName = pathName; |
| 550 |
#else |
| 551 |
(void)pathName; |
| 552 |
#endif |
| 553 |
} |
| 554 |
|
| 555 |
/* Close handle to Alsa library. */ |
| 556 |
static void PaAlsa_CloseLibrary() |
| 557 |
{ |
| 558 |
#ifdef PA_ALSA_DYNAMIC |
| 559 |
dlclose(g_AlsaLib); |
| 560 |
g_AlsaLib = NULL; |
| 561 |
#endif |
| 562 |
} |
| 563 |
|
| 564 |
/* Check return value of ALSA function, and map it to PaError */ |
| 565 |
#define ENSURE_(expr, code) \ |
| 566 |
do { \ |
| 567 |
int __pa_unsure_error_id;\ |
| 568 |
if( UNLIKELY( (__pa_unsure_error_id = (expr)) < 0 ) ) \ |
| 569 |
{ \ |
| 570 |
/* PaUtil_SetLastHostErrorInfo should only be used in the main thread */ \ |
| 571 |
if( (code) == paUnanticipatedHostError && pthread_equal( pthread_self(), paUnixMainThread) ) \ |
| 572 |
{ \ |
| 573 |
PaUtil_SetLastHostErrorInfo( paALSA, __pa_unsure_error_id, alsa_snd_strerror( __pa_unsure_error_id ) ); \ |
| 574 |
} \ |
| 575 |
PaUtil_DebugPrint( "Expression '" #expr "' failed in '" __FILE__ "', line: " STRINGIZE( __LINE__ ) "\n" ); \ |
| 576 |
if( (code) == paUnanticipatedHostError ) \ |
| 577 |
PA_DEBUG(( "Host error description: %s\n", alsa_snd_strerror( __pa_unsure_error_id ) )); \ |
| 578 |
result = (code); \ |
| 579 |
goto error; \ |
| 580 |
} \ |
| 581 |
} while (0) |
| 582 |
|
| 583 |
#define ASSERT_CALL_(expr, success) \ |
| 584 |
do {\ |
| 585 |
int __pa_assert_error_id;\ |
| 586 |
__pa_assert_error_id = (expr);\ |
| 587 |
assert( success == __pa_assert_error_id );\ |
| 588 |
} while (0) |
| 589 |
|
| 590 |
static int numPeriods_ = 4; |
| 591 |
static int busyRetries_ = 100; |
| 592 |
|
| 593 |
int PaAlsa_SetNumPeriods( int numPeriods ) |
| 594 |
{ |
| 595 |
numPeriods_ = numPeriods; |
| 596 |
return paNoError; |
| 597 |
} |
| 598 |
|
| 599 |
typedef enum |
| 600 |
{ |
| 601 |
StreamDirection_In, |
| 602 |
StreamDirection_Out |
| 603 |
} StreamDirection; |
| 604 |
|
| 605 |
typedef struct |
| 606 |
{ |
| 607 |
PaSampleFormat hostSampleFormat; |
| 608 |
int numUserChannels, numHostChannels; |
| 609 |
int userInterleaved, hostInterleaved; |
| 610 |
int canMmap; |
| 611 |
void *nonMmapBuffer; |
| 612 |
unsigned int nonMmapBufferSize; |
| 613 |
PaDeviceIndex device; /* Keep the device index */ |
| 614 |
int deviceIsPlug; /* Distinguish plug types from direct 'hw:' devices */ |
| 615 |
int useReventFix; /* Alsa older than 1.0.16, plug devices need a fix */ |
| 616 |
|
| 617 |
snd_pcm_t *pcm; |
| 618 |
snd_pcm_uframes_t framesPerPeriod, alsaBufferSize; |
| 619 |
snd_pcm_format_t nativeFormat; |
| 620 |
unsigned int nfds; |
| 621 |
int ready; /* Marked ready from poll */ |
| 622 |
void **userBuffers; |
| 623 |
snd_pcm_uframes_t offset; |
| 624 |
StreamDirection streamDir; |
| 625 |
|
| 626 |
snd_pcm_channel_area_t *channelAreas; /* Needed for channel adaption */ |
| 627 |
} PaAlsaStreamComponent; |
| 628 |
|
| 629 |
/* Implementation specific stream structure */ |
| 630 |
typedef struct PaAlsaStream |
| 631 |
{ |
| 632 |
PaUtilStreamRepresentation streamRepresentation; |
| 633 |
PaUtilCpuLoadMeasurer cpuLoadMeasurer; |
| 634 |
PaUtilBufferProcessor bufferProcessor; |
| 635 |
PaUnixThread thread; |
| 636 |
|
| 637 |
unsigned long framesPerUserBuffer, maxFramesPerHostBuffer; |
| 638 |
|
| 639 |
int primeBuffers; |
| 640 |
int callbackMode; /* bool: are we running in callback mode? */ |
| 641 |
int pcmsSynced; /* Have we successfully synced pcms */ |
| 642 |
int rtSched; |
| 643 |
|
| 644 |
/* the callback thread uses these to poll the sound device(s), waiting |
| 645 |
* for data to be ready/available */ |
| 646 |
struct pollfd* pfds; |
| 647 |
int pollTimeout; |
| 648 |
|
| 649 |
/* Used in communication between threads */ |
| 650 |
volatile sig_atomic_t callback_finished; /* bool: are we in the "callback finished" state? */ |
| 651 |
volatile sig_atomic_t callbackAbort; /* Drop frames? */ |
| 652 |
volatile sig_atomic_t isActive; /* Is stream in active state? (Between StartStream and StopStream || !paContinue) */ |
| 653 |
PaUnixMutex stateMtx; /* Used to synchronize access to stream state */ |
| 654 |
|
| 655 |
int neverDropInput; |
| 656 |
|
| 657 |
PaTime underrun; |
| 658 |
PaTime overrun; |
| 659 |
|
| 660 |
PaAlsaStreamComponent capture, playback; |
| 661 |
} |
| 662 |
PaAlsaStream; |
| 663 |
|
| 664 |
/* PaAlsaHostApiRepresentation - host api datastructure specific to this implementation */ |
| 665 |
|
| 666 |
typedef struct PaAlsaHostApiRepresentation |
| 667 |
{ |
| 668 |
PaUtilHostApiRepresentation baseHostApiRep; |
| 669 |
PaUtilStreamInterface callbackStreamInterface; |
| 670 |
PaUtilStreamInterface blockingStreamInterface; |
| 671 |
|
| 672 |
PaUtilAllocationGroup *allocations; |
| 673 |
|
| 674 |
PaHostApiIndex hostApiIndex; |
| 675 |
PaUint32 alsaLibVersion; /* Retrieved from the library at run-time */ |
| 676 |
} |
| 677 |
PaAlsaHostApiRepresentation; |
| 678 |
|
| 679 |
typedef struct PaAlsaDeviceInfo |
| 680 |
{ |
| 681 |
PaDeviceInfo baseDeviceInfo; |
| 682 |
char *alsaName; |
| 683 |
int isPlug; |
| 684 |
int minInputChannels; |
| 685 |
int minOutputChannels; |
| 686 |
} |
| 687 |
PaAlsaDeviceInfo; |
| 688 |
|
| 689 |
/* prototypes for functions declared in this file */ |
| 690 |
|
| 691 |
static void Terminate( struct PaUtilHostApiRepresentation *hostApi ); |
| 692 |
static PaError IsFormatSupported( struct PaUtilHostApiRepresentation *hostApi, |
| 693 |
const PaStreamParameters *inputParameters, |
| 694 |
const PaStreamParameters *outputParameters, |
| 695 |
double sampleRate ); |
| 696 |
static PaError OpenStream( struct PaUtilHostApiRepresentation *hostApi, |
| 697 |
PaStream** s, |
| 698 |
const PaStreamParameters *inputParameters, |
| 699 |
const PaStreamParameters *outputParameters, |
| 700 |
double sampleRate, |
| 701 |
unsigned long framesPerBuffer, |
| 702 |
PaStreamFlags streamFlags, |
| 703 |
PaStreamCallback *callback, |
| 704 |
void *userData ); |
| 705 |
static PaError CloseStream( PaStream* stream ); |
| 706 |
static PaError StartStream( PaStream *stream ); |
| 707 |
static PaError StopStream( PaStream *stream ); |
| 708 |
static PaError AbortStream( PaStream *stream ); |
| 709 |
static PaError IsStreamStopped( PaStream *s ); |
| 710 |
static PaError IsStreamActive( PaStream *stream ); |
| 711 |
static PaTime GetStreamTime( PaStream *stream ); |
| 712 |
static double GetStreamCpuLoad( PaStream* stream ); |
| 713 |
static PaError BuildDeviceList( PaAlsaHostApiRepresentation *hostApi ); |
| 714 |
static int SetApproximateSampleRate( snd_pcm_t *pcm, snd_pcm_hw_params_t *hwParams, double sampleRate ); |
| 715 |
static int GetExactSampleRate( snd_pcm_hw_params_t *hwParams, double *sampleRate ); |
| 716 |
static PaUint32 PaAlsaVersionNum(void); |
| 717 |
|
| 718 |
/* Callback prototypes */ |
| 719 |
static void *CallbackThreadFunc( void *userData ); |
| 720 |
|
| 721 |
/* Blocking prototypes */ |
| 722 |
static signed long GetStreamReadAvailable( PaStream* s ); |
| 723 |
static signed long GetStreamWriteAvailable( PaStream* s ); |
| 724 |
static PaError ReadStream( PaStream* stream, void *buffer, unsigned long frames ); |
| 725 |
static PaError WriteStream( PaStream* stream, const void *buffer, unsigned long frames ); |
| 726 |
|
| 727 |
|
| 728 |
static const PaAlsaDeviceInfo *GetDeviceInfo( const PaUtilHostApiRepresentation *hostApi, int device ) |
| 729 |
{ |
| 730 |
return (const PaAlsaDeviceInfo *)hostApi->deviceInfos[device]; |
| 731 |
} |
| 732 |
|
| 733 |
/** Uncommented because AlsaErrorHandler is unused for anything good yet. If AlsaErrorHandler is |
| 734 |
to be used, do not forget to register this callback in PaAlsa_Initialize, and unregister in Terminate. |
| 735 |
*/ |
| 736 |
/*static void AlsaErrorHandler(const char *file, int line, const char *function, int err, const char *fmt, ...) |
| 737 |
{ |
| 738 |
}*/ |
| 739 |
|
| 740 |
PaError PaAlsa_Initialize( PaUtilHostApiRepresentation **hostApi, PaHostApiIndex hostApiIndex ) |
| 741 |
{ |
| 742 |
PaError result = paNoError; |
| 743 |
PaAlsaHostApiRepresentation *alsaHostApi = NULL; |
| 744 |
|
| 745 |
/* Try loading Alsa library. */ |
| 746 |
if (!PaAlsa_LoadLibrary()) |
| 747 |
return paHostApiNotFound; |
| 748 |
|
| 749 |
PA_UNLESS( alsaHostApi = (PaAlsaHostApiRepresentation*) PaUtil_AllocateMemory( |
| 750 |
sizeof(PaAlsaHostApiRepresentation) ), paInsufficientMemory ); |
| 751 |
PA_UNLESS( alsaHostApi->allocations = PaUtil_CreateAllocationGroup(), paInsufficientMemory ); |
| 752 |
alsaHostApi->hostApiIndex = hostApiIndex; |
| 753 |
alsaHostApi->alsaLibVersion = PaAlsaVersionNum(); |
| 754 |
|
| 755 |
*hostApi = (PaUtilHostApiRepresentation*)alsaHostApi; |
| 756 |
(*hostApi)->info.structVersion = 1; |
| 757 |
(*hostApi)->info.type = paALSA; |
| 758 |
(*hostApi)->info.name = "ALSA"; |
| 759 |
|
| 760 |
(*hostApi)->Terminate = Terminate; |
| 761 |
(*hostApi)->OpenStream = OpenStream; |
| 762 |
(*hostApi)->IsFormatSupported = IsFormatSupported; |
| 763 |
|
| 764 |
/** If AlsaErrorHandler is to be used, do not forget to unregister callback pointer in |
| 765 |
Terminate function. |
| 766 |
*/ |
| 767 |
/*ENSURE_( snd_lib_error_set_handler(AlsaErrorHandler), paUnanticipatedHostError );*/ |
| 768 |
|
| 769 |
PA_ENSURE( BuildDeviceList( alsaHostApi ) ); |
| 770 |
|
| 771 |
PaUtil_InitializeStreamInterface( &alsaHostApi->callbackStreamInterface, |
| 772 |
CloseStream, StartStream, |
| 773 |
StopStream, AbortStream, |
| 774 |
IsStreamStopped, IsStreamActive, |
| 775 |
GetStreamTime, GetStreamCpuLoad, |
| 776 |
PaUtil_DummyRead, PaUtil_DummyWrite, |
| 777 |
PaUtil_DummyGetReadAvailable, |
| 778 |
PaUtil_DummyGetWriteAvailable ); |
| 779 |
|
| 780 |
PaUtil_InitializeStreamInterface( &alsaHostApi->blockingStreamInterface, |
| 781 |
CloseStream, StartStream, |
| 782 |
StopStream, AbortStream, |
| 783 |
IsStreamStopped, IsStreamActive, |
| 784 |
GetStreamTime, PaUtil_DummyGetCpuLoad, |
| 785 |
ReadStream, WriteStream, |
| 786 |
GetStreamReadAvailable, |
| 787 |
GetStreamWriteAvailable ); |
| 788 |
|
| 789 |
PA_ENSURE( PaUnixThreading_Initialize() ); |
| 790 |
|
| 791 |
return result; |
| 792 |
|
| 793 |
error: |
| 794 |
if( alsaHostApi ) |
| 795 |
{ |
| 796 |
if( alsaHostApi->allocations ) |
| 797 |
{ |
| 798 |
PaUtil_FreeAllAllocations( alsaHostApi->allocations ); |
| 799 |
PaUtil_DestroyAllocationGroup( alsaHostApi->allocations ); |
| 800 |
} |
| 801 |
|
| 802 |
PaUtil_FreeMemory( alsaHostApi ); |
| 803 |
} |
| 804 |
|
| 805 |
return result; |
| 806 |
} |
| 807 |
|
| 808 |
static void Terminate( struct PaUtilHostApiRepresentation *hostApi ) |
| 809 |
{ |
| 810 |
PaAlsaHostApiRepresentation *alsaHostApi = (PaAlsaHostApiRepresentation*)hostApi; |
| 811 |
|
| 812 |
assert( hostApi ); |
| 813 |
|
| 814 |
/** See AlsaErrorHandler and PaAlsa_Initialize for details. |
| 815 |
*/ |
| 816 |
/*snd_lib_error_set_handler(NULL);*/ |
| 817 |
|
| 818 |
if( alsaHostApi->allocations ) |
| 819 |
{ |
| 820 |
PaUtil_FreeAllAllocations( alsaHostApi->allocations ); |
| 821 |
PaUtil_DestroyAllocationGroup( alsaHostApi->allocations ); |
| 822 |
} |
| 823 |
|
| 824 |
PaUtil_FreeMemory( alsaHostApi ); |
| 825 |
alsa_snd_config_update_free_global(); |
| 826 |
|
| 827 |
/* Close Alsa library. */ |
| 828 |
PaAlsa_CloseLibrary(); |
| 829 |
} |
| 830 |
|
| 831 |
/** Determine max channels and default latencies. |
| 832 |
* |
| 833 |
* This function provides functionality to grope an opened (might be opened for capture or playback) pcm device for |
| 834 |
* traits like max channels, suitable default latencies and default sample rate. Upon error, max channels is set to zero, |
| 835 |
* and a suitable result returned. The device is closed before returning. |
| 836 |
*/ |
| 837 |
static PaError GropeDevice( snd_pcm_t* pcm, int isPlug, StreamDirection mode, int openBlocking, |
| 838 |
PaAlsaDeviceInfo* devInfo ) |
| 839 |
{ |
| 840 |
PaError result = paNoError; |
| 841 |
snd_pcm_hw_params_t *hwParams; |
| 842 |
snd_pcm_uframes_t alsaBufferFrames, alsaPeriodFrames; |
| 843 |
unsigned int minChans, maxChans; |
| 844 |
int* minChannels, * maxChannels; |
| 845 |
double * defaultLowLatency, * defaultHighLatency, * defaultSampleRate = |
| 846 |
&devInfo->baseDeviceInfo.defaultSampleRate; |
| 847 |
double defaultSr = *defaultSampleRate; |
| 848 |
|
| 849 |
assert( pcm ); |
| 850 |
|
| 851 |
PA_DEBUG(( "%s: collecting info ..\n", __FUNCTION__ )); |
| 852 |
|
| 853 |
if( StreamDirection_In == mode ) |
| 854 |
{ |
| 855 |
minChannels = &devInfo->minInputChannels; |
| 856 |
maxChannels = &devInfo->baseDeviceInfo.maxInputChannels; |
| 857 |
defaultLowLatency = &devInfo->baseDeviceInfo.defaultLowInputLatency; |
| 858 |
defaultHighLatency = &devInfo->baseDeviceInfo.defaultHighInputLatency; |
| 859 |
} |
| 860 |
else |
| 861 |
{ |
| 862 |
minChannels = &devInfo->minOutputChannels; |
| 863 |
maxChannels = &devInfo->baseDeviceInfo.maxOutputChannels; |
| 864 |
defaultLowLatency = &devInfo->baseDeviceInfo.defaultLowOutputLatency; |
| 865 |
defaultHighLatency = &devInfo->baseDeviceInfo.defaultHighOutputLatency; |
| 866 |
} |
| 867 |
|
| 868 |
ENSURE_( alsa_snd_pcm_nonblock( pcm, 0 ), paUnanticipatedHostError ); |
| 869 |
|
| 870 |
alsa_snd_pcm_hw_params_alloca( &hwParams ); |
| 871 |
alsa_snd_pcm_hw_params_any( pcm, hwParams ); |
| 872 |
|
| 873 |
if( defaultSr >= 0 ) |
| 874 |
{ |
| 875 |
/* Could be that the device opened in one mode supports samplerates that the other mode wont have, |
| 876 |
* so try again .. */ |
| 877 |
if( SetApproximateSampleRate( pcm, hwParams, defaultSr ) < 0 ) |
| 878 |
{ |
| 879 |
defaultSr = -1.; |
| 880 |
alsa_snd_pcm_hw_params_any( pcm, hwParams ); /* Clear any params (rate) that might have been set */ |
| 881 |
PA_DEBUG(( "%s: Original default samplerate failed, trying again ..\n", __FUNCTION__ )); |
| 882 |
} |
| 883 |
} |
| 884 |
|
| 885 |
if( defaultSr < 0. ) /* Default sample rate not set */ |
| 886 |
{ |
| 887 |
unsigned int sampleRate = 44100; /* Will contain approximate rate returned by alsa-lib */ |
| 888 |
|
| 889 |
/* Don't allow rate resampling when probing for the default rate (but ignore if this call fails) */ |
| 890 |
alsa_snd_pcm_hw_params_set_rate_resample( pcm, hwParams, 0 ); |
| 891 |
if( alsa_snd_pcm_hw_params_set_rate_near( pcm, hwParams, &sampleRate, NULL ) < 0 ) |
| 892 |
{ |
| 893 |
result = paUnanticipatedHostError; |
| 894 |
goto error; |
| 895 |
} |
| 896 |
ENSURE_( GetExactSampleRate( hwParams, &defaultSr ), paUnanticipatedHostError ); |
| 897 |
} |
| 898 |
|
| 899 |
ENSURE_( alsa_snd_pcm_hw_params_get_channels_min( hwParams, &minChans ), paUnanticipatedHostError ); |
| 900 |
ENSURE_( alsa_snd_pcm_hw_params_get_channels_max( hwParams, &maxChans ), paUnanticipatedHostError ); |
| 901 |
assert( maxChans <= INT_MAX ); |
| 902 |
assert( maxChans > 0 ); /* Weird linking issue could cause wrong version of ALSA symbols to be called, |
| 903 |
resulting in zeroed values */ |
| 904 |
|
| 905 |
/* XXX: Limit to sensible number (ALSA plugins accept a crazy amount of channels)? */ |
| 906 |
if( isPlug && maxChans > 128 ) |
| 907 |
{ |
| 908 |
maxChans = 128; |
| 909 |
PA_DEBUG(( "%s: Limiting number of plugin channels to %u\n", __FUNCTION__, maxChans )); |
| 910 |
} |
| 911 |
|
| 912 |
/* TWEAKME: |
| 913 |
* Giving values for default min and max latency is not straightforward. |
| 914 |
* * for low latency, we want to give the lowest value that will work reliably. |
| 915 |
* This varies based on the sound card, kernel, CPU, etc. Better to give |
| 916 |
* sub-optimal latency than to give a number too low and cause dropouts. |
| 917 |
* * for high latency we want to give a large enough value that dropouts are basically impossible. |
| 918 |
* This doesn't really require as much tweaking, since providing too large a number will |
| 919 |
* just cause us to select the nearest setting that will work at stream config time. |
| 920 |
*/ |
| 921 |
/* Try low latency values, (sometimes the buffer & period that result are larger) */ |
| 922 |
alsaBufferFrames = 512; |
| 923 |
alsaPeriodFrames = 128; |
| 924 |
ENSURE_( alsa_snd_pcm_hw_params_set_buffer_size_near( pcm, hwParams, &alsaBufferFrames ), paUnanticipatedHostError ); |
| 925 |
ENSURE_( alsa_snd_pcm_hw_params_set_period_size_near( pcm, hwParams, &alsaPeriodFrames, NULL ), paUnanticipatedHostError ); |
| 926 |
*defaultLowLatency = (double) (alsaBufferFrames - alsaPeriodFrames) / defaultSr; |
| 927 |
|
| 928 |
/* Base the high latency case on values four times larger */ |
| 929 |
alsaBufferFrames = 2048; |
| 930 |
alsaPeriodFrames = 512; |
| 931 |
/* Have to reset hwParams, to set new buffer size; need to also set sample rate again */ |
| 932 |
ENSURE_( alsa_snd_pcm_hw_params_any( pcm, hwParams ), paUnanticipatedHostError ); |
| 933 |
ENSURE_( SetApproximateSampleRate( pcm, hwParams, defaultSr ), paUnanticipatedHostError ); |
| 934 |
ENSURE_( alsa_snd_pcm_hw_params_set_buffer_size_near( pcm, hwParams, &alsaBufferFrames ), paUnanticipatedHostError ); |
| 935 |
ENSURE_( alsa_snd_pcm_hw_params_set_period_size_near( pcm, hwParams, &alsaPeriodFrames, NULL ), paUnanticipatedHostError ); |
| 936 |
*defaultHighLatency = (double) (alsaBufferFrames - alsaPeriodFrames) / defaultSr; |
| 937 |
|
| 938 |
*minChannels = (int)minChans; |
| 939 |
*maxChannels = (int)maxChans; |
| 940 |
*defaultSampleRate = defaultSr; |
| 941 |
|
| 942 |
end: |
| 943 |
alsa_snd_pcm_close( pcm ); |
| 944 |
return result; |
| 945 |
|
| 946 |
error: |
| 947 |
goto end; |
| 948 |
} |
| 949 |
|
| 950 |
/* Initialize device info with invalid values (maxInputChannels and maxOutputChannels are set to zero since these indicate |
| 951 |
* whether input/output is available) */ |
| 952 |
static void InitializeDeviceInfo( PaDeviceInfo *deviceInfo ) |
| 953 |
{ |
| 954 |
deviceInfo->structVersion = -1; |
| 955 |
deviceInfo->name = NULL; |
| 956 |
deviceInfo->hostApi = -1; |
| 957 |
deviceInfo->maxInputChannels = 0; |
| 958 |
deviceInfo->maxOutputChannels = 0; |
| 959 |
deviceInfo->defaultLowInputLatency = -1.; |
| 960 |
deviceInfo->defaultLowOutputLatency = -1.; |
| 961 |
deviceInfo->defaultHighInputLatency = -1.; |
| 962 |
deviceInfo->defaultHighOutputLatency = -1.; |
| 963 |
deviceInfo->defaultSampleRate = -1.; |
| 964 |
} |
| 965 |
|
| 966 |
|
| 967 |
/* Retrieve the version of the runtime Alsa-lib, as a single number equivalent to |
| 968 |
* SND_LIB_VERSION. Only a version string is available ("a.b.c") so this has to be converted. |
| 969 |
* Assume 'a' and 'b' are single digits only. |
| 970 |
*/ |
| 971 |
static PaUint32 PaAlsaVersionNum(void) |
| 972 |
{ |
| 973 |
char* verStr; |
| 974 |
PaUint32 verNum; |
| 975 |
|
| 976 |
verStr = (char*) alsa_snd_asoundlib_version(); |
| 977 |
verNum = ALSA_VERSION_INT( atoi(verStr), atoi(verStr + 2), atoi(verStr + 4) ); |
| 978 |
PA_DEBUG(( "ALSA version (build): " SND_LIB_VERSION_STR "\nALSA version (runtime): %s\n", verStr )); |
| 979 |
|
| 980 |
return verNum; |
| 981 |
} |
| 982 |
|
| 983 |
|
| 984 |
/* Helper struct */ |
| 985 |
typedef struct |
| 986 |
{ |
| 987 |
char *alsaName; |
| 988 |
char *name; |
| 989 |
int isPlug; |
| 990 |
int hasPlayback; |
| 991 |
int hasCapture; |
| 992 |
} HwDevInfo; |
| 993 |
|
| 994 |
|
| 995 |
HwDevInfo predefinedNames[] = { |
| 996 |
{ "center_lfe", NULL, 0, 1, 0 }, |
| 997 |
/* { "default", NULL, 0, 1, 1 }, */ |
| 998 |
{ "dmix", NULL, 0, 1, 0 }, |
| 999 |
/* { "dpl", NULL, 0, 1, 0 }, */ |
| 1000 |
/* { "dsnoop", NULL, 0, 0, 1 }, */ |
| 1001 |
{ "front", NULL, 0, 1, 0 }, |
| 1002 |
{ "iec958", NULL, 0, 1, 0 }, |
| 1003 |
/* { "modem", NULL, 0, 1, 0 }, */ |
| 1004 |
{ "rear", NULL, 0, 1, 0 }, |
| 1005 |
{ "side", NULL, 0, 1, 0 }, |
| 1006 |
/* { "spdif", NULL, 0, 0, 0 }, */ |
| 1007 |
{ "surround40", NULL, 0, 1, 0 }, |
| 1008 |
{ "surround41", NULL, 0, 1, 0 }, |
| 1009 |
{ "surround50", NULL, 0, 1, 0 }, |
| 1010 |
{ "surround51", NULL, 0, 1, 0 }, |
| 1011 |
{ "surround71", NULL, 0, 1, 0 }, |
| 1012 |
|
| 1013 |
{ "AndroidPlayback_Earpiece_normal", NULL, 0, 1, 0 }, |
| 1014 |
{ "AndroidPlayback_Speaker_normal", NULL, 0, 1, 0 }, |
| 1015 |
{ "AndroidPlayback_Bluetooth_normal", NULL, 0, 1, 0 }, |
| 1016 |
{ "AndroidPlayback_Headset_normal", NULL, 0, 1, 0 }, |
| 1017 |
{ "AndroidPlayback_Speaker_Headset_normal", NULL, 0, 1, 0 }, |
| 1018 |
{ "AndroidPlayback_Bluetooth-A2DP_normal", NULL, 0, 1, 0 }, |
| 1019 |
{ "AndroidPlayback_ExtraDockSpeaker_normal", NULL, 0, 1, 0 }, |
| 1020 |
{ "AndroidPlayback_TvOut_normal", NULL, 0, 1, 0 }, |
| 1021 |
|
| 1022 |
{ "AndroidRecord_Microphone", NULL, 0, 0, 1 }, |
| 1023 |
{ "AndroidRecord_Earpiece_normal", NULL, 0, 0, 1 }, |
| 1024 |
{ "AndroidRecord_Speaker_normal", NULL, 0, 0, 1 }, |
| 1025 |
{ "AndroidRecord_Headset_normal", NULL, 0, 0, 1 }, |
| 1026 |
{ "AndroidRecord_Bluetooth_normal", NULL, 0, 0, 1 }, |
| 1027 |
{ "AndroidRecord_Speaker_Headset_normal", NULL, 0, 0, 1 }, |
| 1028 |
|
| 1029 |
{ NULL, NULL, 0, 1, 0 } |
| 1030 |
}; |
| 1031 |
|
| 1032 |
static const HwDevInfo *FindDeviceName( const char *name ) |
| 1033 |
{ |
| 1034 |
int i; |
| 1035 |
|
| 1036 |
for( i = 0; predefinedNames[i].alsaName; i++ ) |
| 1037 |
{ |
| 1038 |
if( strcmp( name, predefinedNames[i].alsaName ) == 0 ) |
| 1039 |
{ |
| 1040 |
return &predefinedNames[i]; |
| 1041 |
} |
| 1042 |
} |
| 1043 |
|
| 1044 |
return NULL; |
| 1045 |
} |
| 1046 |
|
| 1047 |
static PaError PaAlsa_StrDup( PaAlsaHostApiRepresentation *alsaApi, |
| 1048 |
char **dst, |
| 1049 |
const char *src) |
| 1050 |
{ |
| 1051 |
PaError result = paNoError; |
| 1052 |
int len = strlen( src ) + 1; |
| 1053 |
|
| 1054 |
/* PA_DEBUG(("PaStrDup %s %d\n", src, len)); */ |
| 1055 |
|
| 1056 |
PA_UNLESS( *dst = (char *)PaUtil_GroupAllocateMemory( alsaApi->allocations, len ), |
| 1057 |
paInsufficientMemory ); |
| 1058 |
strncpy( *dst, src, len ); |
| 1059 |
|
| 1060 |
error: |
| 1061 |
return result; |
| 1062 |
} |
| 1063 |
|
| 1064 |
/* Disregard some standard plugins |
| 1065 |
*/ |
| 1066 |
static int IgnorePlugin( const char *pluginId ) |
| 1067 |
{ |
| 1068 |
static const char *ignoredPlugins[] = {"hw", "plughw", "plug", "dsnoop", "tee", |
| 1069 |
"file", "null", "shm", "cards", "rate_convert", NULL}; |
| 1070 |
int i = 0; |
| 1071 |
|
| 1072 |
if( getenv( "PA_ALSA_IGNORE_ALL_PLUGINS" ) && atoi( getenv( "PA_ALSA_IGNORE_ALL_PLUGINS") ) ) |
| 1073 |
return 1; |
| 1074 |
|
| 1075 |
while( ignoredPlugins[i] ) |
| 1076 |
{ |
| 1077 |
if( !strcmp( pluginId, ignoredPlugins[i] ) ) |
| 1078 |
{ |
| 1079 |
return 1; |
| 1080 |
} |
| 1081 |
++i; |
| 1082 |
} |
| 1083 |
|
| 1084 |
return 0; |
| 1085 |
} |
| 1086 |
|
| 1087 |
/* Skip past parts at the beginning of a (pcm) info name that are already in the card name, to avoid duplication */ |
| 1088 |
static char *SkipCardDetailsInName( char *infoSkipName, char *cardRefName ) |
| 1089 |
{ |
| 1090 |
char *lastSpacePosn = infoSkipName; |
| 1091 |
|
| 1092 |
/* Skip matching chars; but only in chunks separated by ' ' (not part words etc), so track lastSpacePosn */ |
| 1093 |
while( *cardRefName ) |
| 1094 |
{ |
| 1095 |
while( *infoSkipName && *cardRefName && *infoSkipName == *cardRefName) |
| 1096 |
{ |
| 1097 |
infoSkipName++; |
| 1098 |
cardRefName++; |
| 1099 |
if( *infoSkipName == ' ' || *infoSkipName == '\0' ) |
| 1100 |
lastSpacePosn = infoSkipName; |
| 1101 |
} |
| 1102 |
infoSkipName = lastSpacePosn; |
| 1103 |
/* Look for another chunk; post-increment means ends pointing to next char */ |
| 1104 |
while( *cardRefName && ( *cardRefName++ != ' ' )); |
| 1105 |
} |
| 1106 |
if( *infoSkipName == '\0' ) |
| 1107 |
return "-"; /* The 2 names were identical; instead of a nul-string, return a marker string */ |
| 1108 |
|
| 1109 |
/* Now want to move to the first char after any spaces */ |
| 1110 |
while( *lastSpacePosn && *lastSpacePosn == ' ' ) |
| 1111 |
lastSpacePosn++; |
| 1112 |
/* Skip a single separator char if present in the remaining pcm name; (pa will add its own) */ |
| 1113 |
if(( *lastSpacePosn == '-' || *lastSpacePosn == ':' ) && *(lastSpacePosn + 1) == ' ' ) |
| 1114 |
lastSpacePosn += 2; |
| 1115 |
|
| 1116 |
return lastSpacePosn; |
| 1117 |
} |
| 1118 |
|
| 1119 |
/** Open PCM device. |
| 1120 |
* |
| 1121 |
* Wrapper around alsa_snd_pcm_open which may repeatedly retry opening a device if it is busy, for |
| 1122 |
* a certain time. This is because dmix may temporarily hold on to a device after it (dmix) |
| 1123 |
* has been opened and closed. |
| 1124 |
* @param mode: Open mode (e.g., SND_PCM_BLOCKING). |
| 1125 |
* @param waitOnBusy: Retry opening busy device for up to one second? |
| 1126 |
**/ |
| 1127 |
static int OpenPcm( snd_pcm_t **pcmp, const char *name, snd_pcm_stream_t stream, int mode, int waitOnBusy ) |
| 1128 |
{ |
| 1129 |
int ret, tries = 0, maxTries = waitOnBusy ? busyRetries_ : 0; |
| 1130 |
|
| 1131 |
ret = alsa_snd_pcm_open( pcmp, name, stream, mode ); |
| 1132 |
|
| 1133 |
for( tries = 0; tries < maxTries && -EBUSY == ret; ++tries ) |
| 1134 |
{ |
| 1135 |
Pa_Sleep( 10 ); |
| 1136 |
ret = alsa_snd_pcm_open( pcmp, name, stream, mode ); |
| 1137 |
if( -EBUSY != ret ) |
| 1138 |
{ |
| 1139 |
PA_DEBUG(( "%s: Successfully opened initially busy device after %d tries\n", __FUNCTION__, tries )); |
| 1140 |
} |
| 1141 |
} |
| 1142 |
if( -EBUSY == ret ) |
| 1143 |
{ |
| 1144 |
PA_DEBUG(( "%s: Failed to open busy device '%s'\n", __FUNCTION__, name )); |
| 1145 |
} |
| 1146 |
else |
| 1147 |
{ |
| 1148 |
if( ret < 0 ) |
| 1149 |
PA_DEBUG(( "%s: Opened device '%s' ptr[%p] - result: [%d:%s]\n", __FUNCTION__, name, *pcmp, ret, alsa_snd_strerror(ret) )); |
| 1150 |
} |
| 1151 |
|
| 1152 |
return ret; |
| 1153 |
} |
| 1154 |
|
| 1155 |
static PaError FillInDevInfo( PaAlsaHostApiRepresentation *alsaApi, HwDevInfo* deviceHwInfo, int blocking, |
| 1156 |
PaAlsaDeviceInfo* devInfo, int* devIdx ) |
| 1157 |
{ |
| 1158 |
PaError result = 0; |
| 1159 |
PaDeviceInfo *baseDeviceInfo = &devInfo->baseDeviceInfo; |
| 1160 |
snd_pcm_t *pcm = NULL; |
| 1161 |
PaUtilHostApiRepresentation *baseApi = &alsaApi->baseHostApiRep; |
| 1162 |
|
| 1163 |
PA_DEBUG(( "%s: Filling device info for: %s\n", __FUNCTION__, deviceHwInfo->name )); |
| 1164 |
|
| 1165 |
/* Zero fields */ |
| 1166 |
InitializeDeviceInfo( baseDeviceInfo ); |
| 1167 |
|
| 1168 |
/* To determine device capabilities, we must open the device and query the |
| 1169 |
* hardware parameter configuration space */ |
| 1170 |
|
| 1171 |
/* Query capture */ |
| 1172 |
if( deviceHwInfo->hasCapture && |
| 1173 |
OpenPcm( &pcm, deviceHwInfo->alsaName, SND_PCM_STREAM_CAPTURE, blocking, 0 ) >= 0 ) |
| 1174 |
{ |
| 1175 |
if( GropeDevice( pcm, deviceHwInfo->isPlug, StreamDirection_In, blocking, devInfo ) != paNoError ) |
| 1176 |
{ |
| 1177 |
/* Error */ |
| 1178 |
PA_DEBUG(( "%s: Failed groping %s for capture\n", __FUNCTION__, deviceHwInfo->alsaName )); |
| 1179 |
goto end; |
| 1180 |
} |
| 1181 |
} |
| 1182 |
|
| 1183 |
/* Query playback */ |
| 1184 |
if( deviceHwInfo->hasPlayback && |
| 1185 |
OpenPcm( &pcm, deviceHwInfo->alsaName, SND_PCM_STREAM_PLAYBACK, blocking, 0 ) >= 0 ) |
| 1186 |
{ |
| 1187 |
if( GropeDevice( pcm, deviceHwInfo->isPlug, StreamDirection_Out, blocking, devInfo ) != paNoError ) |
| 1188 |
{ |
| 1189 |
/* Error */ |
| 1190 |
PA_DEBUG(( "%s: Failed groping %s for playback\n", __FUNCTION__, deviceHwInfo->alsaName )); |
| 1191 |
goto end; |
| 1192 |
} |
| 1193 |
} |
| 1194 |
|
| 1195 |
baseDeviceInfo->structVersion = 2; |
| 1196 |
baseDeviceInfo->hostApi = alsaApi->hostApiIndex; |
| 1197 |
baseDeviceInfo->name = deviceHwInfo->name; |
| 1198 |
devInfo->alsaName = deviceHwInfo->alsaName; |
| 1199 |
devInfo->isPlug = deviceHwInfo->isPlug; |
| 1200 |
|
| 1201 |
/* A: Storing pointer to PaAlsaDeviceInfo object as pointer to PaDeviceInfo object. |
| 1202 |
* Should now be safe to add device info, unless the device supports neither capture nor playback |
| 1203 |
*/ |
| 1204 |
if( baseDeviceInfo->maxInputChannels > 0 || baseDeviceInfo->maxOutputChannels > 0 ) |
| 1205 |
{ |
| 1206 |
/* Make device default if there isn't already one or it is the ALSA "default" device */ |
| 1207 |
if( ( baseApi->info.defaultInputDevice == paNoDevice || |
| 1208 |
!strcmp( deviceHwInfo->alsaName, "default" ) ) && baseDeviceInfo->maxInputChannels > 0 ) |
| 1209 |
{ |
| 1210 |
baseApi->info.defaultInputDevice = *devIdx; |
| 1211 |
PA_DEBUG(( "Default input device: %s\n", deviceHwInfo->name )); |
| 1212 |
} |
| 1213 |
if( ( baseApi->info.defaultOutputDevice == paNoDevice || |
| 1214 |
!strcmp( deviceHwInfo->alsaName, "default" ) ) && baseDeviceInfo->maxOutputChannels > 0 ) |
| 1215 |
{ |
| 1216 |
baseApi->info.defaultOutputDevice = *devIdx; |
| 1217 |
PA_DEBUG(( "Default output device: %s\n", deviceHwInfo->name )); |
| 1218 |
} |
| 1219 |
PA_DEBUG(( "%s: Adding device %s: %d\n", __FUNCTION__, deviceHwInfo->name, *devIdx )); |
| 1220 |
baseApi->deviceInfos[*devIdx] = (PaDeviceInfo *) devInfo; |
| 1221 |
(*devIdx) += 1; |
| 1222 |
} |
| 1223 |
else |
| 1224 |
{ |
| 1225 |
PA_DEBUG(( "%s: Skipped device: %s, all channels == 0\n", __FUNCTION__, deviceHwInfo->name )); |
| 1226 |
} |
| 1227 |
|
| 1228 |
end: |
| 1229 |
return result; |
| 1230 |
} |
| 1231 |
|
| 1232 |
/* Build PaDeviceInfo list, ignore devices for which we cannot determine capabilities (possibly busy, sigh) */ |
| 1233 |
static PaError BuildDeviceList( PaAlsaHostApiRepresentation *alsaApi ) |
| 1234 |
{ |
| 1235 |
PaUtilHostApiRepresentation *baseApi = &alsaApi->baseHostApiRep; |
| 1236 |
PaAlsaDeviceInfo *deviceInfoArray; |
| 1237 |
int cardIdx = -1, devIdx = 0; |
| 1238 |
snd_ctl_card_info_t *cardInfo; |
| 1239 |
PaError result = paNoError; |
| 1240 |
size_t numDeviceNames = 0, maxDeviceNames = 1, i; |
| 1241 |
HwDevInfo *hwDevInfos = NULL; |
| 1242 |
snd_config_t *topNode = NULL; |
| 1243 |
snd_pcm_info_t *pcmInfo; |
| 1244 |
int res; |
| 1245 |
int blocking = SND_PCM_NONBLOCK; |
| 1246 |
int usePlughw = 0; |
| 1247 |
char *hwPrefix = ""; |
| 1248 |
char alsaCardName[50]; |
| 1249 |
#ifdef PA_ENABLE_DEBUG_OUTPUT |
| 1250 |
PaTime startTime = PaUtil_GetTime(); |
| 1251 |
#endif |
| 1252 |
|
| 1253 |
if( getenv( "PA_ALSA_INITIALIZE_BLOCK" ) && atoi( getenv( "PA_ALSA_INITIALIZE_BLOCK" ) ) ) |
| 1254 |
blocking = 0; |
| 1255 |
|
| 1256 |
/* If PA_ALSA_PLUGHW is 1 (non-zero), use the plughw: pcm throughout instead of hw: */ |
| 1257 |
if( getenv( "PA_ALSA_PLUGHW" ) && atoi( getenv( "PA_ALSA_PLUGHW" ) ) ) |
| 1258 |
{ |
| 1259 |
usePlughw = 1; |
| 1260 |
hwPrefix = "plug"; |
| 1261 |
PA_DEBUG(( "%s: Using Plughw\n", __FUNCTION__ )); |
| 1262 |
} |
| 1263 |
|
| 1264 |
/* These two will be set to the first working input and output device, respectively */ |
| 1265 |
baseApi->info.defaultInputDevice = paNoDevice; |
| 1266 |
baseApi->info.defaultOutputDevice = paNoDevice; |
| 1267 |
|
| 1268 |
/* Gather info about hw devices |
| 1269 |
|
| 1270 |
* alsa_snd_card_next() modifies the integer passed to it to be: |
| 1271 |
* the index of the first card if the parameter is -1 |
| 1272 |
* the index of the next card if the parameter is the index of a card |
| 1273 |
* -1 if there are no more cards |
| 1274 |
* |
| 1275 |
* The function itself returns 0 if it succeeded. */ |
| 1276 |
cardIdx = -1; |
| 1277 |
alsa_snd_ctl_card_info_alloca( &cardInfo ); |
| 1278 |
alsa_snd_pcm_info_alloca( &pcmInfo ); |
| 1279 |
while( alsa_snd_card_next( &cardIdx ) == 0 && cardIdx >= 0 ) |
| 1280 |
{ |
| 1281 |
char *cardName; |
| 1282 |
int devIdx = -1; |
| 1283 |
snd_ctl_t *ctl; |
| 1284 |
char buf[50]; |
| 1285 |
|
| 1286 |
snprintf( alsaCardName, sizeof (alsaCardName), "hw:%d", cardIdx ); |
| 1287 |
|
| 1288 |
/* Acquire name of card */ |
| 1289 |
if( alsa_snd_ctl_open( &ctl, alsaCardName, 0 ) < 0 ) |
| 1290 |
{ |
| 1291 |
/* Unable to open card :( */ |
| 1292 |
PA_DEBUG(( "%s: Unable to open device %s\n", __FUNCTION__, alsaCardName )); |
| 1293 |
continue; |
| 1294 |
} |
| 1295 |
alsa_snd_ctl_card_info( ctl, cardInfo ); |
| 1296 |
|
| 1297 |
PA_ENSURE( PaAlsa_StrDup( alsaApi, &cardName, alsa_snd_ctl_card_info_get_name( cardInfo )) ); |
| 1298 |
|
| 1299 |
while( alsa_snd_ctl_pcm_next_device( ctl, &devIdx ) == 0 && devIdx >= 0 ) |
| 1300 |
{ |
| 1301 |
char *alsaDeviceName, *deviceName, *infoName; |
| 1302 |
size_t len; |
| 1303 |
int hasPlayback = 0, hasCapture = 0; |
| 1304 |
|
| 1305 |
snprintf( buf, sizeof (buf), "%s%s,%d", hwPrefix, alsaCardName, devIdx ); |
| 1306 |
|
| 1307 |
/* Obtain info about this particular device */ |
| 1308 |
alsa_snd_pcm_info_set_device( pcmInfo, devIdx ); |
| 1309 |
alsa_snd_pcm_info_set_subdevice( pcmInfo, 0 ); |
| 1310 |
alsa_snd_pcm_info_set_stream( pcmInfo, SND_PCM_STREAM_CAPTURE ); |
| 1311 |
if( alsa_snd_ctl_pcm_info( ctl, pcmInfo ) >= 0 ) |
| 1312 |
{ |
| 1313 |
hasCapture = 1; |
| 1314 |
} |
| 1315 |
|
| 1316 |
alsa_snd_pcm_info_set_stream( pcmInfo, SND_PCM_STREAM_PLAYBACK ); |
| 1317 |
if( alsa_snd_ctl_pcm_info( ctl, pcmInfo ) >= 0 ) |
| 1318 |
{ |
| 1319 |
hasPlayback = 1; |
| 1320 |
} |
| 1321 |
|
| 1322 |
if( !hasPlayback && !hasCapture ) |
| 1323 |
{ |
| 1324 |
/* Error */ |
| 1325 |
continue; |
| 1326 |
} |
| 1327 |
|
| 1328 |
infoName = SkipCardDetailsInName( (char *)alsa_snd_pcm_info_get_name( pcmInfo ), cardName ); |
| 1329 |
|
| 1330 |
/* The length of the string written by snprintf plus terminating 0 */ |
| 1331 |
len = snprintf( NULL, 0, "%s: %s (%s)", cardName, infoName, buf ) + 1; |
| 1332 |
PA_UNLESS( deviceName = (char *)PaUtil_GroupAllocateMemory( alsaApi->allocations, len ), |
| 1333 |
paInsufficientMemory ); |
| 1334 |
snprintf( deviceName, len, "%s: %s (%s)", cardName, infoName, buf ); |
| 1335 |
|
| 1336 |
PA_DEBUG(( "%s: Found device [%d]: %s\n", __FUNCTION__, numDeviceNames, deviceName )); |
| 1337 |
|
| 1338 |
++numDeviceNames; |
| 1339 |
if( !hwDevInfos || numDeviceNames > maxDeviceNames ) |
| 1340 |
{ |
| 1341 |
maxDeviceNames *= 2; |
| 1342 |
PA_UNLESS( hwDevInfos = (HwDevInfo *) realloc( hwDevInfos, maxDeviceNames * sizeof (HwDevInfo) ), |
| 1343 |
paInsufficientMemory ); |
| 1344 |
} |
| 1345 |
|
| 1346 |
PA_ENSURE( PaAlsa_StrDup( alsaApi, &alsaDeviceName, buf ) ); |
| 1347 |
|
| 1348 |
hwDevInfos[ numDeviceNames - 1 ].alsaName = alsaDeviceName; |
| 1349 |
hwDevInfos[ numDeviceNames - 1 ].name = deviceName; |
| 1350 |
hwDevInfos[ numDeviceNames - 1 ].isPlug = usePlughw; |
| 1351 |
hwDevInfos[ numDeviceNames - 1 ].hasPlayback = hasPlayback; |
| 1352 |
hwDevInfos[ numDeviceNames - 1 ].hasCapture = hasCapture; |
| 1353 |
} |
| 1354 |
alsa_snd_ctl_close( ctl ); |
| 1355 |
} |
| 1356 |
|
| 1357 |
/* Iterate over plugin devices */ |
| 1358 |
if( NULL == (*alsa_snd_config) ) |
| 1359 |
{ |
| 1360 |
/* alsa_snd_config_update is called implicitly by some functions, if this hasn't happened snd_config will be NULL (bleh) */ |
| 1361 |
ENSURE_( alsa_snd_config_update(), paUnanticipatedHostError ); |
| 1362 |
PA_DEBUG(( "Updating snd_config\n" )); |
| 1363 |
} |
| 1364 |
assert( *alsa_snd_config ); |
| 1365 |
if( ( res = alsa_snd_config_search( *alsa_snd_config, "pcm", &topNode ) ) >= 0 ) |
| 1366 |
{ |
| 1367 |
snd_config_iterator_t i, next; |
| 1368 |
|
| 1369 |
alsa_snd_config_for_each( i, next, topNode ) |
| 1370 |
{ |
| 1371 |
const char *tpStr = "unknown", *idStr = NULL; |
| 1372 |
int err = 0; |
| 1373 |
|
| 1374 |
char *alsaDeviceName, *deviceName; |
| 1375 |
const HwDevInfo *predefined = NULL; |
| 1376 |
snd_config_t *n = alsa_snd_config_iterator_entry( i ), * tp = NULL;; |
| 1377 |
|
| 1378 |
if( (err = alsa_snd_config_search( n, "type", &tp )) < 0 ) |
| 1379 |
{ |
| 1380 |
if( -ENOENT != err ) |
| 1381 |
{ |
| 1382 |
ENSURE_(err, paUnanticipatedHostError); |
| 1383 |
} |
| 1384 |
} |
| 1385 |
else |
| 1386 |
{ |
| 1387 |
ENSURE_( alsa_snd_config_get_string( tp, &tpStr ), paUnanticipatedHostError ); |
| 1388 |
} |
| 1389 |
ENSURE_( alsa_snd_config_get_id( n, &idStr ), paUnanticipatedHostError ); |
| 1390 |
if( IgnorePlugin( idStr ) ) |
| 1391 |
{ |
| 1392 |
PA_DEBUG(( "%s: Ignoring ALSA plugin device [%s] of type [%s]\n", __FUNCTION__, idStr, tpStr )); |
| 1393 |
continue; |
| 1394 |
} |
| 1395 |
PA_DEBUG(( "%s: Found plugin [%s] of type [%s]\n", __FUNCTION__, idStr, tpStr )); |
| 1396 |
|
| 1397 |
PA_UNLESS( alsaDeviceName = (char*)PaUtil_GroupAllocateMemory( alsaApi->allocations, |
| 1398 |
strlen(idStr) + 6 ), paInsufficientMemory ); |
| 1399 |
strcpy( alsaDeviceName, idStr ); |
| 1400 |
PA_UNLESS( deviceName = (char*)PaUtil_GroupAllocateMemory( alsaApi->allocations, |
| 1401 |
strlen(idStr) + 1 ), paInsufficientMemory ); |
| 1402 |
strcpy( deviceName, idStr ); |
| 1403 |
|
| 1404 |
++numDeviceNames; |
| 1405 |
if( !hwDevInfos || numDeviceNames > maxDeviceNames ) |
| 1406 |
{ |
| 1407 |
maxDeviceNames *= 2; |
| 1408 |
PA_UNLESS( hwDevInfos = (HwDevInfo *) realloc( hwDevInfos, maxDeviceNames * sizeof (HwDevInfo) ), |
| 1409 |
paInsufficientMemory ); |
| 1410 |
} |
| 1411 |
|
| 1412 |
predefined = FindDeviceName( alsaDeviceName ); |
| 1413 |
|
| 1414 |
hwDevInfos[numDeviceNames - 1].alsaName = alsaDeviceName; |
| 1415 |
hwDevInfos[numDeviceNames - 1].name = deviceName; |
| 1416 |
hwDevInfos[numDeviceNames - 1].isPlug = 1; |
| 1417 |
|
| 1418 |
if( predefined ) |
| 1419 |
{ |
| 1420 |
hwDevInfos[numDeviceNames - 1].hasPlayback = predefined->hasPlayback; |
| 1421 |
hwDevInfos[numDeviceNames - 1].hasCapture = predefined->hasCapture; |
| 1422 |
} |
| 1423 |
else |
| 1424 |
{ |
| 1425 |
hwDevInfos[numDeviceNames - 1].hasPlayback = 1; |
| 1426 |
hwDevInfos[numDeviceNames - 1].hasCapture = 1; |
| 1427 |
} |
| 1428 |
} |
| 1429 |
} |
| 1430 |
else |
| 1431 |
PA_DEBUG(( "%s: Iterating over ALSA plugins failed: %s\n", __FUNCTION__, alsa_snd_strerror( res ) )); |
| 1432 |
|
| 1433 |
/* allocate deviceInfo memory based on the number of devices */ |
| 1434 |
PA_UNLESS( baseApi->deviceInfos = (PaDeviceInfo**)PaUtil_GroupAllocateMemory( |
| 1435 |
alsaApi->allocations, sizeof(PaDeviceInfo*) * (numDeviceNames) ), paInsufficientMemory ); |
| 1436 |
|
| 1437 |
/* allocate all device info structs in a contiguous block */ |
| 1438 |
PA_UNLESS( deviceInfoArray = (PaAlsaDeviceInfo*)PaUtil_GroupAllocateMemory( |
| 1439 |
alsaApi->allocations, sizeof(PaAlsaDeviceInfo) * numDeviceNames ), paInsufficientMemory ); |
| 1440 |
|
| 1441 |
/* Loop over list of cards, filling in info. If a device is deemed unavailable (can't get name), |
| 1442 |
* it's ignored. |
| 1443 |
* |
| 1444 |
* Note that we do this in two stages. This is a workaround owing to the fact that the 'dmix' |
| 1445 |
* plugin may cause the underlying hardware device to be busy for a short while even after it |
| 1446 |
* (dmix) is closed. The 'default' plugin may also point to the dmix plugin, so the same goes |
| 1447 |
* for this. |
| 1448 |
*/ |
| 1449 |
PA_DEBUG(( "%s: Filling device info for %d devices\n", __FUNCTION__, numDeviceNames )); |
| 1450 |
for( i = 0, devIdx = 0; i < numDeviceNames; ++i ) |
| 1451 |
{ |
| 1452 |
PaAlsaDeviceInfo* devInfo = &deviceInfoArray[i]; |
| 1453 |
HwDevInfo* hwInfo = &hwDevInfos[i]; |
| 1454 |
if( !strcmp( hwInfo->name, "dmix" ) || !strcmp( hwInfo->name, "default" ) ) |
| 1455 |
{ |
| 1456 |
continue; |
| 1457 |
} |
| 1458 |
|
| 1459 |
PA_ENSURE( FillInDevInfo( alsaApi, hwInfo, blocking, devInfo, &devIdx ) ); |
| 1460 |
} |
| 1461 |
assert( devIdx <= numDeviceNames ); |
| 1462 |
/* Now inspect 'dmix' and 'default' plugins */ |
| 1463 |
for( i = 0; i < numDeviceNames; ++i ) |
| 1464 |
{ |
| 1465 |
PaAlsaDeviceInfo* devInfo = &deviceInfoArray[i]; |
| 1466 |
HwDevInfo* hwInfo = &hwDevInfos[i]; |
| 1467 |
if( strcmp( hwInfo->name, "dmix" ) && strcmp( hwInfo->name, "default" ) ) |
| 1468 |
{ |
| 1469 |
continue; |
| 1470 |
} |
| 1471 |
|
| 1472 |
PA_ENSURE( FillInDevInfo( alsaApi, hwInfo, blocking, devInfo, &devIdx ) ); |
| 1473 |
} |
| 1474 |
free( hwDevInfos ); |
| 1475 |
|
| 1476 |
baseApi->info.deviceCount = devIdx; /* Number of successfully queried devices */ |
| 1477 |
|
| 1478 |
#ifdef PA_ENABLE_DEBUG_OUTPUT |
| 1479 |
PA_DEBUG(( "%s: Building device list took %f seconds\n", __FUNCTION__, PaUtil_GetTime() - startTime )); |
| 1480 |
#endif |
| 1481 |
|
| 1482 |
end: |
| 1483 |
return result; |
| 1484 |
|
| 1485 |
error: |
| 1486 |
/* No particular action */ |
| 1487 |
goto end; |
| 1488 |
} |
| 1489 |
|
| 1490 |
/* Check against known device capabilities */ |
| 1491 |
static PaError ValidateParameters( const PaStreamParameters *parameters, PaUtilHostApiRepresentation *hostApi, StreamDirection mode ) |
| 1492 |
{ |
| 1493 |
PaError result = paNoError; |
| 1494 |
int maxChans; |
| 1495 |
const PaAlsaDeviceInfo *deviceInfo = NULL; |
| 1496 |
assert( parameters ); |
| 1497 |
|
| 1498 |
if( parameters->device != paUseHostApiSpecificDeviceSpecification ) |
| 1499 |
{ |
| 1500 |
assert( parameters->device < hostApi->info.deviceCount ); |
| 1501 |
PA_UNLESS( parameters->hostApiSpecificStreamInfo == NULL, paBadIODeviceCombination ); |
| 1502 |
deviceInfo = GetDeviceInfo( hostApi, parameters->device ); |
| 1503 |
} |
| 1504 |
else |
| 1505 |
{ |
| 1506 |
const PaAlsaStreamInfo *streamInfo = parameters->hostApiSpecificStreamInfo; |
| 1507 |
|
| 1508 |
PA_UNLESS( parameters->device == paUseHostApiSpecificDeviceSpecification, paInvalidDevice ); |
| 1509 |
PA_UNLESS( streamInfo->size == sizeof (PaAlsaStreamInfo) && streamInfo->version == 1, |
| 1510 |
paIncompatibleHostApiSpecificStreamInfo ); |
| 1511 |
PA_UNLESS( streamInfo->deviceString != NULL, paInvalidDevice ); |
| 1512 |
|
| 1513 |
/* Skip further checking */ |
| 1514 |
return paNoError; |
| 1515 |
} |
| 1516 |
|
| 1517 |
assert( deviceInfo ); |
| 1518 |
assert( parameters->hostApiSpecificStreamInfo == NULL ); |
| 1519 |
maxChans = ( StreamDirection_In == mode ? deviceInfo->baseDeviceInfo.maxInputChannels : |
| 1520 |
deviceInfo->baseDeviceInfo.maxOutputChannels ); |
| 1521 |
PA_UNLESS( parameters->channelCount <= maxChans, paInvalidChannelCount ); |
| 1522 |
|
| 1523 |
error: |
| 1524 |
return result; |
| 1525 |
} |
| 1526 |
|
| 1527 |
/* Given an open stream, what sample formats are available? */ |
| 1528 |
static PaSampleFormat GetAvailableFormats( snd_pcm_t *pcm ) |
| 1529 |
{ |
| 1530 |
PaSampleFormat available = 0; |
| 1531 |
snd_pcm_hw_params_t *hwParams; |
| 1532 |
alsa_snd_pcm_hw_params_alloca( &hwParams ); |
| 1533 |
|
| 1534 |
alsa_snd_pcm_hw_params_any( pcm, hwParams ); |
| 1535 |
|
| 1536 |
if( alsa_snd_pcm_hw_params_test_format( pcm, hwParams, SND_PCM_FORMAT_FLOAT ) >= 0) |
| 1537 |
available |= paFloat32; |
| 1538 |
|
| 1539 |
if( alsa_snd_pcm_hw_params_test_format( pcm, hwParams, SND_PCM_FORMAT_S32 ) >= 0) |
| 1540 |
available |= paInt32; |
| 1541 |
|
| 1542 |
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
| 1543 |
if( alsa_snd_pcm_hw_params_test_format( pcm, hwParams, SND_PCM_FORMAT_S24_3LE ) >= 0) |
| 1544 |
available |= paInt24; |
| 1545 |
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ |
| 1546 |
if( alsa_snd_pcm_hw_params_test_format( pcm, hwParams, SND_PCM_FORMAT_S24_3BE ) >= 0) |
| 1547 |
available |= paInt24; |
| 1548 |
#endif |
| 1549 |
|
| 1550 |
if( alsa_snd_pcm_hw_params_test_format( pcm, hwParams, SND_PCM_FORMAT_S16 ) >= 0) |
| 1551 |
available |= paInt16; |
| 1552 |
|
| 1553 |
if( alsa_snd_pcm_hw_params_test_format( pcm, hwParams, SND_PCM_FORMAT_U8 ) >= 0) |
| 1554 |
available |= paUInt8; |
| 1555 |
|
| 1556 |
if( alsa_snd_pcm_hw_params_test_format( pcm, hwParams, SND_PCM_FORMAT_S8 ) >= 0) |
| 1557 |
available |= paInt8; |
| 1558 |
|
| 1559 |
return available; |
| 1560 |
} |
| 1561 |
|
| 1562 |
/* Output to console all formats supported by device */ |
| 1563 |
static void LogAllAvailableFormats( snd_pcm_t *pcm ) |
| 1564 |
{ |
| 1565 |
PaSampleFormat available = 0; |
| 1566 |
snd_pcm_hw_params_t *hwParams; |
| 1567 |
alsa_snd_pcm_hw_params_alloca( &hwParams ); |
| 1568 |
|
| 1569 |
alsa_snd_pcm_hw_params_any( pcm, hwParams ); |
| 1570 |
|
| 1571 |
PA_DEBUG(( " --- Supported Formats ---\n" )); |
| 1572 |
|
| 1573 |
if( alsa_snd_pcm_hw_params_test_format( pcm, hwParams, SND_PCM_FORMAT_S8 ) >= 0) |
| 1574 |
PA_DEBUG(( "SND_PCM_FORMAT_S8\n" )); |
| 1575 |
if( alsa_snd_pcm_hw_params_test_format( pcm, hwParams, SND_PCM_FORMAT_U8 ) >= 0) |
| 1576 |
PA_DEBUG(( "SND_PCM_FORMAT_U8\n" )); |
| 1577 |
|
| 1578 |
if( alsa_snd_pcm_hw_params_test_format( pcm, hwParams, SND_PCM_FORMAT_S16_LE ) >= 0) |
| 1579 |
PA_DEBUG(( "SND_PCM_FORMAT_S16_LE\n" )); |
| 1580 |
if( alsa_snd_pcm_hw_params_test_format( pcm, hwParams, SND_PCM_FORMAT_S16_BE ) >= 0) |
| 1581 |
PA_DEBUG(( "SND_PCM_FORMAT_S16_BE\n" )); |
| 1582 |
|
| 1583 |
if( alsa_snd_pcm_hw_params_test_format( pcm, hwParams, SND_PCM_FORMAT_U16_LE ) >= 0) |
| 1584 |
PA_DEBUG(( "SND_PCM_FORMAT_U16_LE\n" )); |
| 1585 |
if( alsa_snd_pcm_hw_params_test_format( pcm, hwParams, SND_PCM_FORMAT_U16_BE ) >= 0) |
| 1586 |
PA_DEBUG(( "SND_PCM_FORMAT_U16_BE\n" )); |
| 1587 |
|
| 1588 |
if( alsa_snd_pcm_hw_params_test_format( pcm, hwParams, SND_PCM_FORMAT_S24_LE ) >= 0) |
| 1589 |
PA_DEBUG(( "SND_PCM_FORMAT_S24_LE\n" )); |
| 1590 |
if( alsa_snd_pcm_hw_params_test_format( pcm, hwParams, SND_PCM_FORMAT_S24_BE ) >= 0) |
| 1591 |
PA_DEBUG(( "SND_PCM_FORMAT_S24_BE\n" )); |
| 1592 |
|
| 1593 |
if( alsa_snd_pcm_hw_params_test_format( pcm, hwParams, SND_PCM_FORMAT_U24_LE ) >= 0) |
| 1594 |
PA_DEBUG(( "SND_PCM_FORMAT_U24_LE\n" )); |
| 1595 |
if( alsa_snd_pcm_hw_params_test_format( pcm, hwParams, SND_PCM_FORMAT_U24_BE ) >= 0) |
| 1596 |
PA_DEBUG(( "SND_PCM_FORMAT_U24_BE\n" )); |
| 1597 |
|
| 1598 |
if( alsa_snd_pcm_hw_params_test_format( pcm, hwParams, SND_PCM_FORMAT_FLOAT_LE ) >= 0) |
| 1599 |
PA_DEBUG(( "SND_PCM_FORMAT_FLOAT_LE\n" )); |
| 1600 |
if( alsa_snd_pcm_hw_params_test_format( pcm, hwParams, SND_PCM_FORMAT_FLOAT_BE ) >= 0) |
| 1601 |
PA_DEBUG(( "SND_PCM_FORMAT_FLOAT_BE\n" )); |
| 1602 |
|
| 1603 |
if( alsa_snd_pcm_hw_params_test_format( pcm, hwParams, SND_PCM_FORMAT_FLOAT64_LE ) >= 0) |
| 1604 |
PA_DEBUG(( "SND_PCM_FORMAT_FLOAT64_LE\n" )); |
| 1605 |
if( alsa_snd_pcm_hw_params_test_format( pcm, hwParams, SND_PCM_FORMAT_FLOAT64_BE ) >= 0) |
| 1606 |
PA_DEBUG(( "SND_PCM_FORMAT_FLOAT64_BE\n" )); |
| 1607 |
|
| 1608 |
if( alsa_snd_pcm_hw_params_test_format( pcm, hwParams, SND_PCM_FORMAT_IEC958_SUBFRAME_LE ) >= 0) |
| 1609 |
PA_DEBUG(( "SND_PCM_FORMAT_IEC958_SUBFRAME_LE\n" )); |
| 1610 |
if( alsa_snd_pcm_hw_params_test_format( pcm, hwParams, SND_PCM_FORMAT_IEC958_SUBFRAME_BE ) >= 0) |
| 1611 |
PA_DEBUG(( "SND_PCM_FORMAT_IEC958_SUBFRAME_BE\n" )); |
| 1612 |
|
| 1613 |
if( alsa_snd_pcm_hw_params_test_format( pcm, hwParams, SND_PCM_FORMAT_MU_LAW ) >= 0) |
| 1614 |
PA_DEBUG(( "SND_PCM_FORMAT_MU_LAW\n" )); |
| 1615 |
if( alsa_snd_pcm_hw_params_test_format( pcm, hwParams, SND_PCM_FORMAT_A_LAW ) >= 0) |
| 1616 |
PA_DEBUG(( "SND_PCM_FORMAT_A_LAW\n" )); |
| 1617 |
|
| 1618 |
if( alsa_snd_pcm_hw_params_test_format( pcm, hwParams, SND_PCM_FORMAT_IMA_ADPCM ) >= 0) |
| 1619 |
PA_DEBUG(( "SND_PCM_FORMAT_IMA_ADPCM\n" )); |
| 1620 |
if( alsa_snd_pcm_hw_params_test_format( pcm, hwParams, SND_PCM_FORMAT_MPEG ) >= 0) |
| 1621 |
PA_DEBUG(( "SND_PCM_FORMAT_MPEG\n" )); |
| 1622 |
|
| 1623 |
if( alsa_snd_pcm_hw_params_test_format( pcm, hwParams, SND_PCM_FORMAT_GSM ) >= 0) |
| 1624 |
PA_DEBUG(( "SND_PCM_FORMAT_GSM\n" )); |
| 1625 |
if( alsa_snd_pcm_hw_params_test_format( pcm, hwParams, SND_PCM_FORMAT_SPECIAL ) >= 0) |
| 1626 |
PA_DEBUG(( "SND_PCM_FORMAT_SPECIAL\n" )); |
| 1627 |
|
| 1628 |
if( alsa_snd_pcm_hw_params_test_format( pcm, hwParams, SND_PCM_FORMAT_S24_3LE ) >= 0) |
| 1629 |
PA_DEBUG(( "SND_PCM_FORMAT_S24_3LE\n" )); |
| 1630 |
if( alsa_snd_pcm_hw_params_test_format( pcm, hwParams, SND_PCM_FORMAT_S24_3BE ) >= 0) |
| 1631 |
PA_DEBUG(( "SND_PCM_FORMAT_S24_3BE\n" )); |
| 1632 |
|
| 1633 |
if( alsa_snd_pcm_hw_params_test_format( pcm, hwParams, SND_PCM_FORMAT_U24_3LE ) >= 0) |
| 1634 |
PA_DEBUG(( "SND_PCM_FORMAT_U24_3LE\n" )); |
| 1635 |
if( alsa_snd_pcm_hw_params_test_format( pcm, hwParams, SND_PCM_FORMAT_U24_3BE ) >= 0) |
| 1636 |
PA_DEBUG(( "SND_PCM_FORMAT_U24_3BE\n" )); |
| 1637 |
|
| 1638 |
if( alsa_snd_pcm_hw_params_test_format( pcm, hwParams, SND_PCM_FORMAT_S20_3LE ) >= 0) |
| 1639 |
PA_DEBUG(( "SND_PCM_FORMAT_S20_3LE\n" )); |
| 1640 |
if( alsa_snd_pcm_hw_params_test_format( pcm, hwParams, SND_PCM_FORMAT_S20_3BE ) >= 0) |
| 1641 |
PA_DEBUG(( "SND_PCM_FORMAT_S20_3BE\n" )); |
| 1642 |
|
| 1643 |
if( alsa_snd_pcm_hw_params_test_format( pcm, hwParams, SND_PCM_FORMAT_U20_3LE ) >= 0) |
| 1644 |
PA_DEBUG(( "SND_PCM_FORMAT_U20_3LE\n" )); |
| 1645 |
if( alsa_snd_pcm_hw_params_test_format( pcm, hwParams, SND_PCM_FORMAT_U20_3BE ) >= 0) |
| 1646 |
PA_DEBUG(( "SND_PCM_FORMAT_U20_3BE\n" )); |
| 1647 |
|
| 1648 |
if( alsa_snd_pcm_hw_params_test_format( pcm, hwParams, SND_PCM_FORMAT_S18_3LE ) >= 0) |
| 1649 |
PA_DEBUG(( "SND_PCM_FORMAT_S18_3LE\n" )); |
| 1650 |
if( alsa_snd_pcm_hw_params_test_format( pcm, hwParams, SND_PCM_FORMAT_S18_3BE ) >= 0) |
| 1651 |
PA_DEBUG(( "SND_PCM_FORMAT_S18_3BE\n" )); |
| 1652 |
|
| 1653 |
if( alsa_snd_pcm_hw_params_test_format( pcm, hwParams, SND_PCM_FORMAT_U18_3LE ) >= 0) |
| 1654 |
PA_DEBUG(( "SND_PCM_FORMAT_U18_3LE\n" )); |
| 1655 |
if( alsa_snd_pcm_hw_params_test_format( pcm, hwParams, SND_PCM_FORMAT_U18_3BE ) >= 0) |
| 1656 |
PA_DEBUG(( "SND_PCM_FORMAT_U18_3BE\n" )); |
| 1657 |
|
| 1658 |
if( alsa_snd_pcm_hw_params_test_format( pcm, hwParams, SND_PCM_FORMAT_S16 ) >= 0) |
| 1659 |
PA_DEBUG(( "SND_PCM_FORMAT_S16\n" )); |
| 1660 |
if( alsa_snd_pcm_hw_params_test_format( pcm, hwParams, SND_PCM_FORMAT_U16 ) >= 0) |
| 1661 |
PA_DEBUG(( "SND_PCM_FORMAT_U16\n" )); |
| 1662 |
|
| 1663 |
if( alsa_snd_pcm_hw_params_test_format( pcm, hwParams, SND_PCM_FORMAT_S24 ) >= 0) |
| 1664 |
PA_DEBUG(( "SND_PCM_FORMAT_S24\n" )); |
| 1665 |
if( alsa_snd_pcm_hw_params_test_format( pcm, hwParams, SND_PCM_FORMAT_U24 ) >= 0) |
| 1666 |
PA_DEBUG(( "SND_PCM_FORMAT_U24\n" )); |
| 1667 |
|
| 1668 |
if( alsa_snd_pcm_hw_params_test_format( pcm, hwParams, SND_PCM_FORMAT_S32 ) >= 0) |
| 1669 |
PA_DEBUG(( "SND_PCM_FORMAT_S32\n" )); |
| 1670 |
if( alsa_snd_pcm_hw_params_test_format( pcm, hwParams, SND_PCM_FORMAT_U32 ) >= 0) |
| 1671 |
PA_DEBUG(( "SND_PCM_FORMAT_U32\n" )); |
| 1672 |
|
| 1673 |
if( alsa_snd_pcm_hw_params_test_format( pcm, hwParams, SND_PCM_FORMAT_FLOAT ) >= 0) |
| 1674 |
PA_DEBUG(( "SND_PCM_FORMAT_FLOAT\n" )); |
| 1675 |
if( alsa_snd_pcm_hw_params_test_format( pcm, hwParams, SND_PCM_FORMAT_FLOAT64 ) >= 0) |
| 1676 |
PA_DEBUG(( "SND_PCM_FORMAT_FLOAT64\n" )); |
| 1677 |
|
| 1678 |
if( alsa_snd_pcm_hw_params_test_format( pcm, hwParams, SND_PCM_FORMAT_IEC958_SUBFRAME ) >= 0) |
| 1679 |
PA_DEBUG(( "SND_PCM_FORMAT_IEC958_SUBFRAME\n" )); |
| 1680 |
|
| 1681 |
PA_DEBUG(( " -------------------------\n" )); |
| 1682 |
} |
| 1683 |
|
| 1684 |
static snd_pcm_format_t Pa2AlsaFormat( PaSampleFormat paFormat ) |
| 1685 |
{ |
| 1686 |
switch( paFormat ) |
| 1687 |
{ |
| 1688 |
case paFloat32: |
| 1689 |
return SND_PCM_FORMAT_FLOAT; |
| 1690 |
|
| 1691 |
case paInt16: |
| 1692 |
return SND_PCM_FORMAT_S16; |
| 1693 |
|
| 1694 |
case paInt24: |
| 1695 |
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
| 1696 |
return SND_PCM_FORMAT_S24_3LE; |
| 1697 |
#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ |
| 1698 |
return SND_PCM_FORMAT_S24_3BE; |
| 1699 |
#endif |
| 1700 |
|
| 1701 |
case paInt32: |
| 1702 |
return SND_PCM_FORMAT_S32; |
| 1703 |
|
| 1704 |
case paInt8: |
| 1705 |
return SND_PCM_FORMAT_S8; |
| 1706 |
|
| 1707 |
case paUInt8: |
| 1708 |
return SND_PCM_FORMAT_U8; |
| 1709 |
|
| 1710 |
default: |
| 1711 |
return SND_PCM_FORMAT_UNKNOWN; |
| 1712 |
} |
| 1713 |
} |
| 1714 |
|
| 1715 |
/** Open an ALSA pcm handle. |
| 1716 |
* |
| 1717 |
* The device to be open can be specified by name in a custom PaAlsaStreamInfo struct, or it will be by |
| 1718 |
* the Portaudio device number supplied in the stream parameters. |
| 1719 |
*/ |
| 1720 |
static PaError AlsaOpen( const PaUtilHostApiRepresentation *hostApi, const PaStreamParameters *params, StreamDirection |
| 1721 |
streamDir, snd_pcm_t **pcm ) |
| 1722 |
{ |
| 1723 |
PaError result = paNoError; |
| 1724 |
int ret; |
| 1725 |
const char* deviceName = ""; |
| 1726 |
const PaAlsaDeviceInfo *deviceInfo = NULL; |
| 1727 |
PaAlsaStreamInfo *streamInfo = (PaAlsaStreamInfo *)params->hostApiSpecificStreamInfo; |
| 1728 |
|
| 1729 |
if( !streamInfo ) |
| 1730 |
{ |
| 1731 |
deviceInfo = GetDeviceInfo( hostApi, params->device ); |
| 1732 |
deviceName = deviceInfo->alsaName; |
| 1733 |
} |
| 1734 |
else |
| 1735 |
deviceName = streamInfo->deviceString; |
| 1736 |
|
| 1737 |
PA_DEBUG(( "%s: Opening device %s\n", __FUNCTION__, deviceName )); |
| 1738 |
if( (ret = OpenPcm( pcm, deviceName, streamDir == StreamDirection_In ? SND_PCM_STREAM_CAPTURE : SND_PCM_STREAM_PLAYBACK, |
| 1739 |
SND_PCM_NONBLOCK, 1 )) < 0 ) |
| 1740 |
{ |
| 1741 |
/* Not to be closed */ |
| 1742 |
*pcm = NULL; |
| 1743 |
ENSURE_( ret, -EBUSY == ret ? paDeviceUnavailable : paBadIODeviceCombination ); |
| 1744 |
} |
| 1745 |
ENSURE_( alsa_snd_pcm_nonblock( *pcm, 0 ), paUnanticipatedHostError ); |
| 1746 |
|
| 1747 |
end: |
| 1748 |
return result; |
| 1749 |
|
| 1750 |
error: |
| 1751 |
goto end; |
| 1752 |
} |
| 1753 |
|
| 1754 |
static PaError TestParameters( const PaUtilHostApiRepresentation *hostApi, const PaStreamParameters *parameters, |
| 1755 |
double sampleRate, StreamDirection streamDir ) |
| 1756 |
{ |
| 1757 |
PaError result = paNoError; |
| 1758 |
snd_pcm_t *pcm = NULL; |
| 1759 |
PaSampleFormat availableFormats; |
| 1760 |
/* We are able to adapt to a number of channels less than what the device supports */ |
| 1761 |
unsigned int numHostChannels; |
| 1762 |
PaSampleFormat hostFormat; |
| 1763 |
snd_pcm_hw_params_t *hwParams; |
| 1764 |
alsa_snd_pcm_hw_params_alloca( &hwParams ); |
| 1765 |
|
| 1766 |
if( !parameters->hostApiSpecificStreamInfo ) |
| 1767 |
{ |
| 1768 |
const PaAlsaDeviceInfo *devInfo = GetDeviceInfo( hostApi, parameters->device ); |
| 1769 |
numHostChannels = PA_MAX( parameters->channelCount, StreamDirection_In == streamDir ? |
| 1770 |
devInfo->minInputChannels : devInfo->minOutputChannels ); |
| 1771 |
} |
| 1772 |
else |
| 1773 |
numHostChannels = parameters->channelCount; |
| 1774 |
|
| 1775 |
PA_ENSURE( AlsaOpen( hostApi, parameters, streamDir, &pcm ) ); |
| 1776 |
|
| 1777 |
alsa_snd_pcm_hw_params_any( pcm, hwParams ); |
| 1778 |
|
| 1779 |
if( SetApproximateSampleRate( pcm, hwParams, sampleRate ) < 0 ) |
| 1780 |
{ |
| 1781 |
result = paInvalidSampleRate; |
| 1782 |
goto error; |
| 1783 |
} |
| 1784 |
|
| 1785 |
if( alsa_snd_pcm_hw_params_set_channels( pcm, hwParams, numHostChannels ) < 0 ) |
| 1786 |
{ |
| 1787 |
result = paInvalidChannelCount; |
| 1788 |
goto error; |
| 1789 |
} |
| 1790 |
|
| 1791 |
/* See if we can find a best possible match */ |
| 1792 |
availableFormats = GetAvailableFormats( pcm ); |
| 1793 |
PA_ENSURE( hostFormat = PaUtil_SelectClosestAvailableFormat( availableFormats, parameters->sampleFormat ) ); |
| 1794 |
|
| 1795 |
/* Some specific hardware (reported: Audio8 DJ) can fail with assertion during this step. */ |
| 1796 |
ENSURE_( alsa_snd_pcm_hw_params_set_format( pcm, hwParams, Pa2AlsaFormat( hostFormat ) ), paUnanticipatedHostError ); |
| 1797 |
|
| 1798 |
{ |
| 1799 |
/* It happens that this call fails because the device is busy */ |
| 1800 |
int ret = 0; |
| 1801 |
if( ( ret = alsa_snd_pcm_hw_params( pcm, hwParams ) ) < 0 ) |
| 1802 |
{ |
| 1803 |
if( -EINVAL == ret ) |
| 1804 |
{ |
| 1805 |
/* Don't know what to return here */ |
| 1806 |
result = paBadIODeviceCombination; |
| 1807 |
goto error; |
| 1808 |
} |
| 1809 |
else if( -EBUSY == ret ) |
| 1810 |
{ |
| 1811 |
result = paDeviceUnavailable; |
| 1812 |
PA_DEBUG(( "%s: Device is busy\n", __FUNCTION__ )); |
| 1813 |
} |
| 1814 |
else |
| 1815 |
{ |
| 1816 |
result = paUnanticipatedHostError; |
| 1817 |
} |
| 1818 |
|
| 1819 |
ENSURE_( ret, result ); |
| 1820 |
} |
| 1821 |
} |
| 1822 |
|
| 1823 |
end: |
| 1824 |
if( pcm ) |
| 1825 |
{ |
| 1826 |
alsa_snd_pcm_close( pcm ); |
| 1827 |
} |
| 1828 |
return result; |
| 1829 |
|
| 1830 |
error: |
| 1831 |
goto end; |
| 1832 |
} |
| 1833 |
|
| 1834 |
static PaError IsFormatSupported( struct PaUtilHostApiRepresentation *hostApi, |
| 1835 |
const PaStreamParameters *inputParameters, |
| 1836 |
const PaStreamParameters *outputParameters, |
| 1837 |
double sampleRate ) |
| 1838 |
{ |
| 1839 |
int inputChannelCount = 0, outputChannelCount = 0; |
| 1840 |
PaSampleFormat inputSampleFormat, outputSampleFormat; |
| 1841 |
PaError result = paFormatIsSupported; |
| 1842 |
|
| 1843 |
if( inputParameters ) |
| 1844 |
{ |
| 1845 |
PA_ENSURE( ValidateParameters( inputParameters, hostApi, StreamDirection_In ) ); |
| 1846 |
|
| 1847 |
inputChannelCount = inputParameters->channelCount; |
| 1848 |
inputSampleFormat = inputParameters->sampleFormat; |
| 1849 |
} |
| 1850 |
|
| 1851 |
if( outputParameters ) |
| 1852 |
{ |
| 1853 |
PA_ENSURE( ValidateParameters( outputParameters, hostApi, StreamDirection_Out ) ); |
| 1854 |
|
| 1855 |
outputChannelCount = outputParameters->channelCount; |
| 1856 |
outputSampleFormat = outputParameters->sampleFormat; |
| 1857 |
} |
| 1858 |
|
| 1859 |
if( inputChannelCount ) |
| 1860 |
{ |
| 1861 |
if( ( result = TestParameters( hostApi, inputParameters, sampleRate, StreamDirection_In ) ) |
| 1862 |
!= paNoError ) |
| 1863 |
goto error; |
| 1864 |
} |
| 1865 |
if ( outputChannelCount ) |
| 1866 |
{ |
| 1867 |
if( ( result = TestParameters( hostApi, outputParameters, sampleRate, StreamDirection_Out ) ) |
| 1868 |
!= paNoError ) |
| 1869 |
goto error; |
| 1870 |
} |
| 1871 |
|
| 1872 |
return paFormatIsSupported; |
| 1873 |
|
| 1874 |
error: |
| 1875 |
return result; |
| 1876 |
} |
| 1877 |
|
| 1878 |
|
| 1879 |
static PaError PaAlsaStreamComponent_Initialize( PaAlsaStreamComponent *self, PaAlsaHostApiRepresentation *alsaApi, |
| 1880 |
const PaStreamParameters *params, StreamDirection streamDir, int callbackMode ) |
| 1881 |
{ |
| 1882 |
PaError result = paNoError; |
| 1883 |
PaSampleFormat userSampleFormat = params->sampleFormat, hostSampleFormat = paNoError; |
| 1884 |
assert( params->channelCount > 0 ); |
| 1885 |
|
| 1886 |
/* Make sure things have an initial value */ |
| 1887 |
memset( self, 0, sizeof (PaAlsaStreamComponent) ); |
| 1888 |
|
| 1889 |
if( NULL == params->hostApiSpecificStreamInfo ) |
| 1890 |
{ |
| 1891 |
const PaAlsaDeviceInfo *devInfo = GetDeviceInfo( &alsaApi->baseHostApiRep, params->device ); |
| 1892 |
self->numHostChannels = PA_MAX( params->channelCount, StreamDirection_In == streamDir ? devInfo->minInputChannels |
| 1893 |
: devInfo->minOutputChannels ); |
| 1894 |
self->deviceIsPlug = devInfo->isPlug; |
| 1895 |
PA_DEBUG(( "%s: Host Chans %c %i\n", __FUNCTION__, streamDir == StreamDirection_In ? 'C' : 'P', self->numHostChannels )); |
| 1896 |
} |
| 1897 |
else |
| 1898 |
{ |
| 1899 |
/* We're blissfully unaware of the minimum channelCount */ |
| 1900 |
self->numHostChannels = params->channelCount; |
| 1901 |
/* Check if device name does not start with hw: to determine if it is a 'plug' device */ |
| 1902 |
if( strncmp( "hw:", ((PaAlsaStreamInfo *)params->hostApiSpecificStreamInfo)->deviceString, 3 ) != 0 ) |
| 1903 |
self->deviceIsPlug = 1; /* An Alsa plug device, not a direct hw device */ |
| 1904 |
} |
| 1905 |
if( self->deviceIsPlug && alsaApi->alsaLibVersion < ALSA_VERSION_INT( 1, 0, 16 ) ) |
| 1906 |
self->useReventFix = 1; /* Prior to Alsa1.0.16, plug devices may stutter without this fix */ |
| 1907 |
|
| 1908 |
self->device = params->device; |
| 1909 |
|
| 1910 |
PA_ENSURE( AlsaOpen( &alsaApi->baseHostApiRep, params, streamDir, &self->pcm ) ); |
| 1911 |
self->nfds = alsa_snd_pcm_poll_descriptors_count( self->pcm ); |
| 1912 |
|
| 1913 |
PA_ENSURE( hostSampleFormat = PaUtil_SelectClosestAvailableFormat( GetAvailableFormats( self->pcm ), userSampleFormat ) ); |
| 1914 |
|
| 1915 |
self->hostSampleFormat = hostSampleFormat; |
| 1916 |
self->nativeFormat = Pa2AlsaFormat( hostSampleFormat ); |
| 1917 |
self->hostInterleaved = self->userInterleaved = !( userSampleFormat & paNonInterleaved ); |
| 1918 |
self->numUserChannels = params->channelCount; |
| 1919 |
self->streamDir = streamDir; |
| 1920 |
self->canMmap = 0; |
| 1921 |
self->nonMmapBuffer = NULL; |
| 1922 |
self->nonMmapBufferSize = 0; |
| 1923 |
|
| 1924 |
if( !callbackMode && !self->userInterleaved ) |
| 1925 |
{ |
| 1926 |
/* Pre-allocate non-interleaved user provided buffers */ |
| 1927 |
PA_UNLESS( self->userBuffers = PaUtil_AllocateMemory( sizeof (void *) * self->numUserChannels ), |
| 1928 |
paInsufficientMemory ); |
| 1929 |
} |
| 1930 |
|
| 1931 |
error: |
| 1932 |
|
| 1933 |
/* Log all available formats. */ |
| 1934 |
if ( hostSampleFormat == paSampleFormatNotSupported ) |
| 1935 |
{ |
| 1936 |
LogAllAvailableFormats( self->pcm ); |
| 1937 |
PA_DEBUG(( "%s: Please provide the log output to PortAudio developers, your hardware does not have any sample format implemented yet.\n", __FUNCTION__ )); |
| 1938 |
} |
| 1939 |
|
| 1940 |
return result; |
| 1941 |
} |
| 1942 |
|
| 1943 |
static void PaAlsaStreamComponent_Terminate( PaAlsaStreamComponent *self ) |
| 1944 |
{ |
| 1945 |
alsa_snd_pcm_close( self->pcm ); |
| 1946 |
PaUtil_FreeMemory( self->userBuffers ); /* (Ptr can be NULL; PaUtil_FreeMemory includes a NULL check) */ |
| 1947 |
PaUtil_FreeMemory( self->nonMmapBuffer ); |
| 1948 |
} |
| 1949 |
|
| 1950 |
/* |
| 1951 |
static int nearbyint_(float value) { |
| 1952 |
if( value - (int)value > .5 ) |
| 1953 |
return (int)ceil( value ); |
| 1954 |
return (int)floor( value ); |
| 1955 |
} |
| 1956 |
*/ |
| 1957 |
|
| 1958 |
/** Initiate configuration, preparing for determining a period size suitable for both capture and playback components. |
| 1959 |
* |
| 1960 |
*/ |
| 1961 |
static PaError PaAlsaStreamComponent_InitialConfigure( PaAlsaStreamComponent *self, const PaStreamParameters *params, |
| 1962 |
int primeBuffers, snd_pcm_hw_params_t *hwParams, double *sampleRate ) |
| 1963 |
{ |
| 1964 |
/* Configuration consists of setting all of ALSA's parameters. |
| 1965 |
* These parameters come in two flavors: hardware parameters |
| 1966 |
* and software parameters. Hardware parameters will affect |
| 1967 |
* the way the device is initialized, software parameters |
| 1968 |
* affect the way ALSA interacts with me, the user-level client. |
| 1969 |
*/ |
| 1970 |
|
| 1971 |
PaError result = paNoError; |
| 1972 |
snd_pcm_access_t accessMode, alternateAccessMode; |
| 1973 |
int dir = 0; |
| 1974 |
snd_pcm_t *pcm = self->pcm; |
| 1975 |
double sr = *sampleRate; |
| 1976 |
unsigned int minPeriods = 2; |
| 1977 |
|
| 1978 |
/* self->framesPerPeriod = framesPerHostBuffer; */ |
| 1979 |
|
| 1980 |
/* ... fill up the configuration space with all possible |
| 1981 |
* combinations of parameters this device will accept */ |
| 1982 |
ENSURE_( alsa_snd_pcm_hw_params_any( pcm, hwParams ), paUnanticipatedHostError ); |
| 1983 |
|
| 1984 |
ENSURE_( alsa_snd_pcm_hw_params_set_periods_integer( pcm, hwParams ), paUnanticipatedHostError ); |
| 1985 |
/* I think there should be at least 2 periods (even though ALSA doesn't appear to enforce this) */ |
| 1986 |
dir = 0; |
| 1987 |
ENSURE_( alsa_snd_pcm_hw_params_set_periods_min( pcm, hwParams, &minPeriods, &dir ), paUnanticipatedHostError ); |
| 1988 |
|
| 1989 |
if( self->userInterleaved ) |
| 1990 |
{ |
| 1991 |
accessMode = SND_PCM_ACCESS_MMAP_INTERLEAVED; |
| 1992 |
alternateAccessMode = SND_PCM_ACCESS_MMAP_NONINTERLEAVED; |
| 1993 |
|
| 1994 |
/* test if MMAP supported */ |
| 1995 |
self->canMmap = alsa_snd_pcm_hw_params_test_access( pcm, hwParams, accessMode ) >= 0 || |
| 1996 |
alsa_snd_pcm_hw_params_test_access( pcm, hwParams, alternateAccessMode ) >= 0; |
| 1997 |
|
| 1998 |
PA_DEBUG(( "%s: device MMAP SND_PCM_ACCESS_MMAP_INTERLEAVED: %s\n", __FUNCTION__, ( alsa_snd_pcm_hw_params_test_access( pcm, hwParams, accessMode ) >= 0 ? "YES" : "NO" ) )); |
| 1999 |
PA_DEBUG(( "%s: device MMAP SND_PCM_ACCESS_MMAP_NONINTERLEAVED: %s\n", __FUNCTION__, ( alsa_snd_pcm_hw_params_test_access( pcm, hwParams, alternateAccessMode ) >= 0 ? "YES" : "NO" ) )); |
| 2000 |
|
| 2001 |
if( !self->canMmap ) |
| 2002 |
{ |
| 2003 |
accessMode = SND_PCM_ACCESS_RW_INTERLEAVED; |
| 2004 |
alternateAccessMode = SND_PCM_ACCESS_RW_NONINTERLEAVED; |
| 2005 |
} |
| 2006 |
} |
| 2007 |
else |
| 2008 |
{ |
| 2009 |
accessMode = SND_PCM_ACCESS_MMAP_NONINTERLEAVED; |
| 2010 |
alternateAccessMode = SND_PCM_ACCESS_MMAP_INTERLEAVED; |
| 2011 |
|
| 2012 |
/* test if MMAP supported */ |
| 2013 |
self->canMmap = alsa_snd_pcm_hw_params_test_access( pcm, hwParams, accessMode ) >= 0 || |
| 2014 |
alsa_snd_pcm_hw_params_test_access( pcm, hwParams, alternateAccessMode ) >= 0; |
| 2015 |
|
| 2016 |
PA_DEBUG((" %s: device MMAP SND_PCM_ACCESS_MMAP_NONINTERLEAVED: %s\n", __FUNCTION__, ( alsa_snd_pcm_hw_params_test_access( pcm, hwParams, accessMode ) >= 0 ? "YES" : "NO" ) )); |
| 2017 |
PA_DEBUG(( "%s: device MMAP SND_PCM_ACCESS_MMAP_INTERLEAVED: %s\n", __FUNCTION__, ( alsa_snd_pcm_hw_params_test_access( pcm, hwParams, alternateAccessMode ) >= 0 ? "YES" : "NO" ) )); |
| 2018 |
|
| 2019 |
if( !self->canMmap ) |
| 2020 |
{ |
| 2021 |
accessMode = SND_PCM_ACCESS_RW_NONINTERLEAVED; |
| 2022 |
alternateAccessMode = SND_PCM_ACCESS_RW_INTERLEAVED; |
| 2023 |
} |
| 2024 |
} |
| 2025 |
|
| 2026 |
PA_DEBUG(( "%s: device can MMAP: %s\n", __FUNCTION__, ( self->canMmap ? "YES" : "NO" ) )); |
| 2027 |
|
| 2028 |
/* If requested access mode fails, try alternate mode */ |
| 2029 |
if( alsa_snd_pcm_hw_params_set_access( pcm, hwParams, accessMode ) < 0 ) |
| 2030 |
{ |
| 2031 |
int err = 0; |
| 2032 |
if( ( err = alsa_snd_pcm_hw_params_set_access( pcm, hwParams, alternateAccessMode )) < 0 ) |
| 2033 |
{ |
| 2034 |
result = paUnanticipatedHostError; |
| 2035 |
PaUtil_SetLastHostErrorInfo( paALSA, err, alsa_snd_strerror( err ) ); |
| 2036 |
goto error; |
| 2037 |
} |
| 2038 |
/* Flip mode */ |
| 2039 |
self->hostInterleaved = !self->userInterleaved; |
| 2040 |
} |
| 2041 |
|
| 2042 |
/* Some specific hardware (reported: Audio8 DJ) can fail with assertion during this step. */ |
| 2043 |
ENSURE_( alsa_snd_pcm_hw_params_set_format( pcm, hwParams, self->nativeFormat ), paUnanticipatedHostError ); |
| 2044 |
|
| 2045 |
if( ( result = SetApproximateSampleRate( pcm, hwParams, sr )) != paUnanticipatedHostError ) |
| 2046 |
{ |
| 2047 |
ENSURE_( GetExactSampleRate( hwParams, &sr ), paUnanticipatedHostError ); |
| 2048 |
if( result == paInvalidSampleRate ) /* From the SetApproximateSampleRate() call above */ |
| 2049 |
{ /* The sample rate was returned as 'out of tolerance' of the one requested */ |
| 2050 |
PA_DEBUG(( "%s: Wanted %.3f, closest sample rate was %.3f\n", __FUNCTION__, sampleRate, sr )); |
| 2051 |
PA_ENSURE( paInvalidSampleRate ); |
| 2052 |
} |
| 2053 |
} |
| 2054 |
else |
| 2055 |
{ |
| 2056 |
PA_ENSURE( paUnanticipatedHostError ); |
| 2057 |
} |
| 2058 |
|
| 2059 |
ENSURE_( alsa_snd_pcm_hw_params_set_channels( pcm, hwParams, self->numHostChannels ), paInvalidChannelCount ); |
| 2060 |
|
| 2061 |
*sampleRate = sr; |
| 2062 |
|
| 2063 |
end: |
| 2064 |
return result; |
| 2065 |
|
| 2066 |
error: |
| 2067 |
/* No particular action */ |
| 2068 |
goto end; |
| 2069 |
} |
| 2070 |
|
| 2071 |
/** Finish the configuration of the component's ALSA device. |
| 2072 |
* |
| 2073 |
* As part of this method, the component's alsaBufferSize attribute will be set. |
| 2074 |
* @param latency: The latency for this component. |
| 2075 |
*/ |
| 2076 |
static PaError PaAlsaStreamComponent_FinishConfigure( PaAlsaStreamComponent *self, snd_pcm_hw_params_t* hwParams, |
| 2077 |
const PaStreamParameters *params, int primeBuffers, double sampleRate, PaTime* latency ) |
| 2078 |
{ |
| 2079 |
PaError result = paNoError; |
| 2080 |
snd_pcm_sw_params_t* swParams; |
| 2081 |
snd_pcm_uframes_t bufSz = 0; |
| 2082 |
*latency = -1.; |
| 2083 |
|
| 2084 |
alsa_snd_pcm_sw_params_alloca( &swParams ); |
| 2085 |
|
| 2086 |
bufSz = params->suggestedLatency * sampleRate + self->framesPerPeriod; |
| 2087 |
ENSURE_( alsa_snd_pcm_hw_params_set_buffer_size_near( self->pcm, hwParams, &bufSz ), paUnanticipatedHostError ); |
| 2088 |
|
| 2089 |
/* Set the parameters! */ |
| 2090 |
{ |
| 2091 |
int r = alsa_snd_pcm_hw_params( self->pcm, hwParams ); |
| 2092 |
#ifdef PA_ENABLE_DEBUG_OUTPUT |
| 2093 |
if( r < 0 ) |
| 2094 |
{ |
| 2095 |
snd_output_t *output = NULL; |
| 2096 |
alsa_snd_output_stdio_attach( &output, stderr, 0 ); |
| 2097 |
alsa_snd_pcm_hw_params_dump( hwParams, output ); |
| 2098 |
} |
| 2099 |
#endif |
| 2100 |
ENSURE_( r, paUnanticipatedHostError ); |
| 2101 |
} |
| 2102 |
if( alsa_snd_pcm_hw_params_get_buffer_size != NULL ) |
| 2103 |
{ |
| 2104 |
ENSURE_( alsa_snd_pcm_hw_params_get_buffer_size( hwParams, &self->alsaBufferSize ), paUnanticipatedHostError ); |
| 2105 |
} |
| 2106 |
else |
| 2107 |
{ |
| 2108 |
self->alsaBufferSize = bufSz; |
| 2109 |
} |
| 2110 |
|
| 2111 |
/* Latency in seconds */ |
| 2112 |
*latency = (self->alsaBufferSize - self->framesPerPeriod) / sampleRate; |
| 2113 |
|
| 2114 |
/* Now software parameters... */ |
| 2115 |
ENSURE_( alsa_snd_pcm_sw_params_current( self->pcm, swParams ), paUnanticipatedHostError ); |
| 2116 |
|
| 2117 |
ENSURE_( alsa_snd_pcm_sw_params_set_start_threshold( self->pcm, swParams, self->framesPerPeriod ), paUnanticipatedHostError ); |
| 2118 |
ENSURE_( alsa_snd_pcm_sw_params_set_stop_threshold( self->pcm, swParams, self->alsaBufferSize ), paUnanticipatedHostError ); |
| 2119 |
|
| 2120 |
/* Silence buffer in the case of underrun */ |
| 2121 |
if( !primeBuffers ) /* XXX: Make sense? */ |
| 2122 |
{ |
| 2123 |
snd_pcm_uframes_t boundary; |
| 2124 |
ENSURE_( alsa_snd_pcm_sw_params_get_boundary( swParams, &boundary ), paUnanticipatedHostError ); |
| 2125 |
ENSURE_( alsa_snd_pcm_sw_params_set_silence_threshold( self->pcm, swParams, 0 ), paUnanticipatedHostError ); |
| 2126 |
ENSURE_( alsa_snd_pcm_sw_params_set_silence_size( self->pcm, swParams, boundary ), paUnanticipatedHostError ); |
| 2127 |
} |
| 2128 |
|
| 2129 |
ENSURE_( alsa_snd_pcm_sw_params_set_avail_min( self->pcm, swParams, self->framesPerPeriod ), paUnanticipatedHostError ); |
| 2130 |
ENSURE_( alsa_snd_pcm_sw_params_set_xfer_align( self->pcm, swParams, 1 ), paUnanticipatedHostError ); |
| 2131 |
ENSURE_( alsa_snd_pcm_sw_params_set_tstamp_mode( self->pcm, swParams, SND_PCM_TSTAMP_ENABLE ), paUnanticipatedHostError ); |
| 2132 |
|
| 2133 |
/* Set the parameters! */ |
| 2134 |
ENSURE_( alsa_snd_pcm_sw_params( self->pcm, swParams ), paUnanticipatedHostError ); |
| 2135 |
|
| 2136 |
error: |
| 2137 |
return result; |
| 2138 |
} |
| 2139 |
|
| 2140 |
static PaError PaAlsaStream_Initialize( PaAlsaStream *self, PaAlsaHostApiRepresentation *alsaApi, const PaStreamParameters *inParams, |
| 2141 |
const PaStreamParameters *outParams, double sampleRate, unsigned long framesPerUserBuffer, PaStreamCallback callback, |
| 2142 |
PaStreamFlags streamFlags, void *userData ) |
| 2143 |
{ |
| 2144 |
PaError result = paNoError; |
| 2145 |
assert( self ); |
| 2146 |
|
| 2147 |
memset( self, 0, sizeof( PaAlsaStream ) ); |
| 2148 |
|
| 2149 |
if( NULL != callback ) |
| 2150 |
{ |
| 2151 |
PaUtil_InitializeStreamRepresentation( &self->streamRepresentation, |
| 2152 |
&alsaApi->callbackStreamInterface, |
| 2153 |
callback, userData ); |
| 2154 |
self->callbackMode = 1; |
| 2155 |
} |
| 2156 |
else |
| 2157 |
{ |
| 2158 |
PaUtil_InitializeStreamRepresentation( &self->streamRepresentation, |
| 2159 |
&alsaApi->blockingStreamInterface, |
| 2160 |
NULL, userData ); |
| 2161 |
} |
| 2162 |
|
| 2163 |
self->framesPerUserBuffer = framesPerUserBuffer; |
| 2164 |
self->neverDropInput = streamFlags & paNeverDropInput; |
| 2165 |
/* XXX: Ignore paPrimeOutputBuffersUsingStreamCallback until buffer priming is fully supported in pa_process.c */ |
| 2166 |
/* |
| 2167 |
if( outParams & streamFlags & paPrimeOutputBuffersUsingStreamCallback ) |
| 2168 |
self->primeBuffers = 1; |
| 2169 |
*/ |
| 2170 |
memset( &self->capture, 0, sizeof (PaAlsaStreamComponent) ); |
| 2171 |
memset( &self->playback, 0, sizeof (PaAlsaStreamComponent) ); |
| 2172 |
if( inParams ) |
| 2173 |
{ |
| 2174 |
PA_ENSURE( PaAlsaStreamComponent_Initialize( &self->capture, alsaApi, inParams, StreamDirection_In, NULL != callback ) ); |
| 2175 |
} |
| 2176 |
if( outParams ) |
| 2177 |
{ |
| 2178 |
PA_ENSURE( PaAlsaStreamComponent_Initialize( &self->playback, alsaApi, outParams, StreamDirection_Out, NULL != callback ) ); |
| 2179 |
} |
| 2180 |
|
| 2181 |
assert( self->capture.nfds || self->playback.nfds ); |
| 2182 |
|
| 2183 |
PA_UNLESS( self->pfds = (struct pollfd*)PaUtil_AllocateMemory( ( self->capture.nfds + |
| 2184 |
self->playback.nfds ) * sizeof( struct pollfd ) ), paInsufficientMemory ); |
| 2185 |
|
| 2186 |
PaUtil_InitializeCpuLoadMeasurer( &self->cpuLoadMeasurer, sampleRate ); |
| 2187 |
ASSERT_CALL_( PaUnixMutex_Initialize( &self->stateMtx ), paNoError ); |
| 2188 |
|
| 2189 |
error: |
| 2190 |
return result; |
| 2191 |
} |
| 2192 |
|
| 2193 |
/** Free resources associated with stream, and eventually stream itself. |
| 2194 |
* |
| 2195 |
* Frees allocated memory, and terminates individual StreamComponents. |
| 2196 |
*/ |
| 2197 |
static void PaAlsaStream_Terminate( PaAlsaStream *self ) |
| 2198 |
{ |
| 2199 |
assert( self ); |
| 2200 |
|
| 2201 |
if( self->capture.pcm ) |
| 2202 |
{ |
| 2203 |
PaAlsaStreamComponent_Terminate( &self->capture ); |
| 2204 |
} |
| 2205 |
if( self->playback.pcm ) |
| 2206 |
{ |
| 2207 |
PaAlsaStreamComponent_Terminate( &self->playback ); |
| 2208 |
} |
| 2209 |
|
| 2210 |
PaUtil_FreeMemory( self->pfds ); |
| 2211 |
ASSERT_CALL_( PaUnixMutex_Terminate( &self->stateMtx ), paNoError ); |
| 2212 |
|
| 2213 |
PaUtil_FreeMemory( self ); |
| 2214 |
} |
| 2215 |
|
| 2216 |
/** Calculate polling timeout |
| 2217 |
* |
| 2218 |
* @param frames Time to wait |
| 2219 |
* @return Polling timeout in milliseconds |
| 2220 |
*/ |
| 2221 |
static int CalculatePollTimeout( const PaAlsaStream *stream, unsigned long frames ) |
| 2222 |
{ |
| 2223 |
assert( stream->streamRepresentation.streamInfo.sampleRate > 0.0 ); |
| 2224 |
/* Period in msecs, rounded up */ |
| 2225 |
return (int)ceil( 1000 * frames / stream->streamRepresentation.streamInfo.sampleRate ); |
| 2226 |
} |
| 2227 |
|
| 2228 |
/** Align value in backward direction. |
| 2229 |
* |
| 2230 |
* @param v: Value to align. |
| 2231 |
* @param align: Alignment. |
| 2232 |
*/ |
| 2233 |
static unsigned long PaAlsa_AlignBackward(unsigned long v, unsigned long align) |
| 2234 |
{ |
| 2235 |
return ( v - ( align ? v % align : 0 ) ); |
| 2236 |
} |
| 2237 |
|
| 2238 |
/** Align value in forward direction. |
| 2239 |
* |
| 2240 |
* @param v: Value to align. |
| 2241 |
* @param align: Alignment. |
| 2242 |
*/ |
| 2243 |
static unsigned long PaAlsa_AlignForward(unsigned long v, unsigned long align) |
| 2244 |
{ |
| 2245 |
unsigned long remainder = ( align ? ( v % align ) : 0); |
| 2246 |
return ( remainder != 0 ? v + ( align - remainder ) : v ); |
| 2247 |
} |
| 2248 |
|
| 2249 |
/** Get size of host buffer maintained from the number of user frames, sample rate and suggested latency. Minimum double buffering |
| 2250 |
* is maintained to allow 100% CPU usage inside user callback. |
| 2251 |
* |
| 2252 |
* @param userFramesPerBuffer: User buffer size in number of frames. |
| 2253 |
* @param suggestedLatency: User provided desired latency. |
| 2254 |
* @param sampleRate: Sample rate. |
| 2255 |
*/ |
| 2256 |
static unsigned long PaAlsa_GetFramesPerHostBuffer(unsigned long userFramesPerBuffer, PaTime suggestedLatency, double sampleRate) |
| 2257 |
{ |
| 2258 |
unsigned long frames = userFramesPerBuffer + PA_MAX( userFramesPerBuffer, (unsigned long)( suggestedLatency * sampleRate ) ); |
| 2259 |
return frames; |
| 2260 |
} |
| 2261 |
|
| 2262 |
/** Determine size per host buffer. |
| 2263 |
* |
| 2264 |
* During this method call, the component's framesPerPeriod attribute gets computed, and the corresponding period size |
| 2265 |
* gets configured for the device. |
| 2266 |
* @param accurate: If the configured period size is non-integer, this will be set to 0. |
| 2267 |
*/ |
| 2268 |
static PaError PaAlsaStreamComponent_DetermineFramesPerBuffer( PaAlsaStreamComponent* self, const PaStreamParameters* params, |
| 2269 |
unsigned long framesPerUserBuffer, double sampleRate, snd_pcm_hw_params_t* hwParams, int* accurate ) |
| 2270 |
{ |
| 2271 |
PaError result = paNoError; |
| 2272 |
unsigned long bufferSize, framesPerHostBuffer; |
| 2273 |
int dir = 0; |
| 2274 |
|
| 2275 |
/* Calculate host buffer size */ |
| 2276 |
bufferSize = PaAlsa_GetFramesPerHostBuffer(framesPerUserBuffer, params->suggestedLatency, sampleRate); |
| 2277 |
|
| 2278 |
/* Log */ |
| 2279 |
PA_DEBUG(( "%s: user-buffer (frames) = %lu\n", __FUNCTION__, framesPerUserBuffer )); |
| 2280 |
PA_DEBUG(( "%s: user-buffer (sec) = %f\n", __FUNCTION__, (double)(framesPerUserBuffer / sampleRate) )); |
| 2281 |
PA_DEBUG(( "%s: suggested latency (sec) = %f\n", __FUNCTION__, params->suggestedLatency )); |
| 2282 |
PA_DEBUG(( "%s: suggested host buffer (frames) = %lu\n", __FUNCTION__, bufferSize )); |
| 2283 |
PA_DEBUG(( "%s: suggested host buffer (sec) = %f\n", __FUNCTION__, (double)(bufferSize / sampleRate) )); |
| 2284 |
|
| 2285 |
#ifdef PA_ALSA_USE_OBSOLETE_HOST_BUFFER_CALC |
| 2286 |
|
| 2287 |
if( framesPerUserBuffer != paFramesPerBufferUnspecified ) |
| 2288 |
{ |
| 2289 |
/* Preferably the host buffer size should be a multiple of the user buffer size */ |
| 2290 |
|
| 2291 |
if( bufferSize > framesPerUserBuffer ) |
| 2292 |
{ |
| 2293 |
snd_pcm_uframes_t remainder = bufferSize % framesPerUserBuffer; |
| 2294 |
if( remainder > framesPerUserBuffer / 2. ) |
| 2295 |
bufferSize += framesPerUserBuffer - remainder; |
| 2296 |
else |
| 2297 |
bufferSize -= remainder; |
| 2298 |
|
| 2299 |
assert( bufferSize % framesPerUserBuffer == 0 ); |
| 2300 |
} |
| 2301 |
else if( framesPerUserBuffer % bufferSize != 0 ) |
| 2302 |
{ |
| 2303 |
/* Find a good compromise between user specified latency and buffer size */ |
| 2304 |
if( bufferSize > framesPerUserBuffer * .75 ) |
| 2305 |
{ |
| 2306 |
bufferSize = framesPerUserBuffer; |
| 2307 |
} |
| 2308 |
else |
| 2309 |
{ |
| 2310 |
snd_pcm_uframes_t newSz = framesPerUserBuffer; |
| 2311 |
while( newSz / 2 >= bufferSize ) |
| 2312 |
{ |
| 2313 |
if( framesPerUserBuffer % (newSz / 2) != 0 ) |
| 2314 |
{ |
| 2315 |
/* No use dividing any further */ |
| 2316 |
break; |
| 2317 |
} |
| 2318 |
newSz /= 2; |
| 2319 |
} |
| 2320 |
bufferSize = newSz; |
| 2321 |
} |
| 2322 |
|
| 2323 |
assert( framesPerUserBuffer % bufferSize == 0 ); |
| 2324 |
} |
| 2325 |
} |
| 2326 |
|
| 2327 |
#endif |
| 2328 |
|
| 2329 |
{ |
| 2330 |
unsigned numPeriods = numPeriods_, maxPeriods = 0, minPeriods = numPeriods_; |
| 2331 |
|
| 2332 |
/* It may be that the device only supports 2 periods for instance */ |
| 2333 |
dir = 0; |
| 2334 |
ENSURE_( alsa_snd_pcm_hw_params_get_periods_min( hwParams, &minPeriods, &dir ), paUnanticipatedHostError ); |
| 2335 |
dir = 0; |
| 2336 |
ENSURE_( alsa_snd_pcm_hw_params_get_periods_max( hwParams, &maxPeriods, &dir ), paUnanticipatedHostError ); |
| 2337 |
assert( maxPeriods > 1 ); |
| 2338 |
|
| 2339 |
/* Clamp to min/max */ |
| 2340 |
numPeriods = PA_MIN(maxPeriods, PA_MAX(minPeriods, numPeriods)); |
| 2341 |
|
| 2342 |
PA_DEBUG(( "%s: periods min = %lu, max = %lu, req = %lu \n", __FUNCTION__, minPeriods, maxPeriods, numPeriods )); |
| 2343 |
|
| 2344 |
#ifndef PA_ALSA_USE_OBSOLETE_HOST_BUFFER_CALC |
| 2345 |
|
| 2346 |
/* Calculate period size */ |
| 2347 |
framesPerHostBuffer = (bufferSize / numPeriods); |
| 2348 |
|
| 2349 |
/* Align & test size */ |
| 2350 |
if( framesPerUserBuffer != paFramesPerBufferUnspecified ) |
| 2351 |
{ |
| 2352 |
/* Align to user buffer size */ |
| 2353 |
framesPerHostBuffer = PaAlsa_AlignForward(framesPerHostBuffer, framesPerUserBuffer); |
| 2354 |
|
| 2355 |
/* Test (borrowed from older implementation) */ |
| 2356 |
if( framesPerHostBuffer < framesPerUserBuffer ) |
| 2357 |
{ |
| 2358 |
assert( framesPerUserBuffer % framesPerHostBuffer == 0 ); |
| 2359 |
if( alsa_snd_pcm_hw_params_test_period_size( self->pcm, hwParams, framesPerHostBuffer, 0 ) < 0 ) |
| 2360 |
{ |
| 2361 |
if( alsa_snd_pcm_hw_params_test_period_size( self->pcm, hwParams, framesPerHostBuffer * 2, 0 ) == 0 ) |
| 2362 |
framesPerHostBuffer *= 2; |
| 2363 |
else if( alsa_snd_pcm_hw_params_test_period_size( self->pcm, hwParams, framesPerHostBuffer / 2, 0 ) == 0 ) |
| 2364 |
framesPerHostBuffer /= 2; |
| 2365 |
} |
| 2366 |
} |
| 2367 |
else |
| 2368 |
{ |
| 2369 |
assert( framesPerHostBuffer % framesPerUserBuffer == 0 ); |
| 2370 |
if( alsa_snd_pcm_hw_params_test_period_size( self->pcm, hwParams, framesPerHostBuffer, 0 ) < 0 ) |
| 2371 |
{ |
| 2372 |
if( alsa_snd_pcm_hw_params_test_period_size( self->pcm, hwParams, framesPerHostBuffer + framesPerUserBuffer, 0 ) == 0 ) |
| 2373 |
framesPerHostBuffer += framesPerUserBuffer; |
| 2374 |
else if( alsa_snd_pcm_hw_params_test_period_size( self->pcm, hwParams, framesPerHostBuffer - framesPerUserBuffer, 0 ) == 0 ) |
| 2375 |
framesPerHostBuffer -= framesPerUserBuffer; |
| 2376 |
} |
| 2377 |
} |
| 2378 |
} |
| 2379 |
#endif |
| 2380 |
|
| 2381 |
#ifdef PA_ALSA_USE_OBSOLETE_HOST_BUFFER_CALC |
| 2382 |
|
| 2383 |
if( framesPerUserBuffer != paFramesPerBufferUnspecified ) |
| 2384 |
{ |
| 2385 |
/* Try to get a power-of-two of the user buffer size. */ |
| 2386 |
framesPerHostBuffer = framesPerUserBuffer; |
| 2387 |
if( framesPerHostBuffer < bufferSize ) |
| 2388 |
{ |
| 2389 |
while( bufferSize / framesPerHostBuffer > numPeriods ) |
| 2390 |
{ |
| 2391 |
framesPerHostBuffer *= 2; |
| 2392 |
} |
| 2393 |
/* One extra period is preferable to one less (should be more robust) */ |
| 2394 |
if( bufferSize / framesPerHostBuffer < numPeriods ) |
| 2395 |
{ |
| 2396 |
framesPerHostBuffer /= 2; |
| 2397 |
} |
| 2398 |
} |
| 2399 |
else |
| 2400 |
{ |
| 2401 |
while( bufferSize / framesPerHostBuffer < numPeriods ) |
| 2402 |
{ |
| 2403 |
if( framesPerUserBuffer % ( framesPerHostBuffer / 2 ) != 0 ) |
| 2404 |
{ |
| 2405 |
/* Can't be divided any further */ |
| 2406 |
break; |
| 2407 |
} |
| 2408 |
framesPerHostBuffer /= 2; |
| 2409 |
} |
| 2410 |
} |
| 2411 |
|
| 2412 |
if( framesPerHostBuffer < framesPerUserBuffer ) |
| 2413 |
{ |
| 2414 |
assert( framesPerUserBuffer % framesPerHostBuffer == 0 ); |
| 2415 |
if( alsa_snd_pcm_hw_params_test_period_size( self->pcm, hwParams, framesPerHostBuffer, 0 ) < 0 ) |
| 2416 |
{ |
| 2417 |
if( alsa_snd_pcm_hw_params_test_period_size( self->pcm, hwParams, framesPerHostBuffer * 2, 0 ) == 0 ) |
| 2418 |
framesPerHostBuffer *= 2; |
| 2419 |
else if( alsa_snd_pcm_hw_params_test_period_size( self->pcm, hwParams, framesPerHostBuffer / 2, 0 ) == 0 ) |
| 2420 |
framesPerHostBuffer /= 2; |
| 2421 |
} |
| 2422 |
} |
| 2423 |
else |
| 2424 |
{ |
| 2425 |
assert( framesPerHostBuffer % framesPerUserBuffer == 0 ); |
| 2426 |
if( alsa_snd_pcm_hw_params_test_period_size( self->pcm, hwParams, framesPerHostBuffer, 0 ) < 0 ) |
| 2427 |
{ |
| 2428 |
if( alsa_snd_pcm_hw_params_test_period_size( self->pcm, hwParams, framesPerHostBuffer + framesPerUserBuffer, 0 ) == 0 ) |
| 2429 |
framesPerHostBuffer += framesPerUserBuffer; |
| 2430 |
else if( alsa_snd_pcm_hw_params_test_period_size( self->pcm, hwParams, framesPerHostBuffer - framesPerUserBuffer, 0 ) == 0 ) |
| 2431 |
framesPerHostBuffer -= framesPerUserBuffer; |
| 2432 |
} |
| 2433 |
} |
| 2434 |
} |
| 2435 |
else |
| 2436 |
{ |
| 2437 |
framesPerHostBuffer = bufferSize / numPeriods; |
| 2438 |
} |
| 2439 |
|
| 2440 |
/* non-mmap mode needs a reasonably-sized buffer or it'll stutter */ |
| 2441 |
if( !self->canMmap && framesPerHostBuffer < 2048 ) |
| 2442 |
framesPerHostBuffer = 2048; |
| 2443 |
#endif |
| 2444 |
PA_DEBUG(( "%s: suggested host buffer period = %lu \n", __FUNCTION__, framesPerHostBuffer )); |
| 2445 |
} |
| 2446 |
|
| 2447 |
{ |
| 2448 |
/* Get min/max period sizes and adjust our chosen */ |
| 2449 |
snd_pcm_uframes_t min = 0, max = 0, minmax_diff; |
| 2450 |
ENSURE_( alsa_snd_pcm_hw_params_get_period_size_min( hwParams, &min, NULL ), paUnanticipatedHostError ); |
| 2451 |
ENSURE_( alsa_snd_pcm_hw_params_get_period_size_max( hwParams, &max, NULL ), paUnanticipatedHostError ); |
| 2452 |
minmax_diff = max - min; |
| 2453 |
|
| 2454 |
if( framesPerHostBuffer < min ) |
| 2455 |
{ |
| 2456 |
PA_DEBUG(( "%s: The determined period size (%lu) is less than minimum (%lu)\n", __FUNCTION__, framesPerHostBuffer, min )); |
| 2457 |
framesPerHostBuffer = (( minmax_diff == 2 ) ? min + 1 : min ); |
| 2458 |
} |
| 2459 |
else if( framesPerHostBuffer > max ) |
| 2460 |
{ |
| 2461 |
PA_DEBUG(( "%s: The determined period size (%lu) is greater than maximum (%lu)\n", __FUNCTION__, framesPerHostBuffer, max )); |
| 2462 |
framesPerHostBuffer = (( minmax_diff == 2 ) ? max - 1 : max ); |
| 2463 |
} |
| 2464 |
|
| 2465 |
PA_DEBUG(( "%s: device period minimum = %lu\n", __FUNCTION__, min )); |
| 2466 |
PA_DEBUG(( "%s: device period maximum = %lu\n", __FUNCTION__, max )); |
| 2467 |
PA_DEBUG(( "%s: host buffer period = %lu\n", __FUNCTION__, framesPerHostBuffer )); |
| 2468 |
PA_DEBUG(( "%s: host buffer period latency = %f\n", __FUNCTION__, (double)( framesPerHostBuffer / sampleRate ) )); |
| 2469 |
|
| 2470 |
/* Try setting period size */ |
| 2471 |
dir = 0; |
| 2472 |
ENSURE_( alsa_snd_pcm_hw_params_set_period_size_near( self->pcm, hwParams, &framesPerHostBuffer, &dir ), paUnanticipatedHostError ); |
| 2473 |
if( dir != 0 ) |
| 2474 |
{ |
| 2475 |
PA_DEBUG(( "%s: The configured period size is non-integer.\n", __FUNCTION__, dir )); |
| 2476 |
*accurate = 0; |
| 2477 |
} |
| 2478 |
} |
| 2479 |
|
| 2480 |
/* Set result */ |
| 2481 |
self->framesPerPeriod = framesPerHostBuffer; |
| 2482 |
|
| 2483 |
error: |
| 2484 |
return result; |
| 2485 |
} |
| 2486 |
|
| 2487 |
/* We need to determine how many frames per host buffer (period) to use. Our |
| 2488 |
* goals are to provide the best possible performance, but also to |
| 2489 |
* honor the requested latency settings as closely as we can. Therefore this |
| 2490 |
* decision is based on: |
| 2491 |
* |
| 2492 |
* - the period sizes that playback and/or capture support. The |
| 2493 |
* host buffer size has to be one of these. |
| 2494 |
* - the number of periods that playback and/or capture support. |
| 2495 |
* |
| 2496 |
* We want to make period_size*(num_periods-1) to be as close as possible |
| 2497 |
* to latency*rate for both playback and capture. |
| 2498 |
* |
| 2499 |
* This method will determine suitable period sizes for capture and playback handles, and report the maximum number of |
| 2500 |
* frames per host buffer. The latter is relevant, in case we should be so unfortunate that the period size differs |
| 2501 |
* between capture and playback. If this should happen, the stream's hostBufferSizeMode attribute will be set to |
| 2502 |
* paUtilBoundedHostBufferSize, because the best we can do is limit the size of individual host buffers to the upper |
| 2503 |
* bound. The size of host buffers scheduled for processing should only matter if the user has specified a buffer size, |
| 2504 |
* but when he/she does we must strive for an optimal configuration. By default we'll opt for a fixed host buffer size, |
| 2505 |
* which should be fine if the period size is the same for capture and playback. In general, if there is a specified user |
| 2506 |
* buffer size, this method tries it best to determine a period size which is a multiple of the user buffer size. |
| 2507 |
* |
| 2508 |
* The framesPerPeriod attributes of the individual capture and playback components of the stream are set to corresponding |
| 2509 |
* values determined here. Since these should be reported as |
| 2510 |
* |
| 2511 |
* This is one of those blocks of code that will just take a lot of |
| 2512 |
* refinement to be any good. |
| 2513 |
* |
| 2514 |
* In the full-duplex case it is possible that the routine was unable |
| 2515 |
* to find a number of frames per buffer acceptable to both devices |
| 2516 |
* TODO: Implement an algorithm to find the value closest to acceptance |
| 2517 |
* by both devices, to minimize difference between period sizes? |
| 2518 |
* |
| 2519 |
* @param determinedFramesPerHostBuffer: The determined host buffer size. |
| 2520 |
*/ |
| 2521 |
static PaError PaAlsaStream_DetermineFramesPerBuffer( PaAlsaStream* self, double sampleRate, const PaStreamParameters* inputParameters, |
| 2522 |
const PaStreamParameters* outputParameters, unsigned long framesPerUserBuffer, snd_pcm_hw_params_t* hwParamsCapture, |
| 2523 |
snd_pcm_hw_params_t* hwParamsPlayback, PaUtilHostBufferSizeMode* hostBufferSizeMode ) |
| 2524 |
{ |
| 2525 |
PaError result = paNoError; |
| 2526 |
unsigned long framesPerHostBuffer = 0; |
| 2527 |
int dir = 0; |
| 2528 |
int accurate = 1; |
| 2529 |
unsigned numPeriods = numPeriods_; |
| 2530 |
|
| 2531 |
if( self->capture.pcm && self->playback.pcm ) |
| 2532 |
{ |
| 2533 |
if( framesPerUserBuffer == paFramesPerBufferUnspecified ) |
| 2534 |
{ |
| 2535 |
/* Come up with a common desired latency */ |
| 2536 |
snd_pcm_uframes_t desiredBufSz, e, minPeriodSize, maxPeriodSize, optimalPeriodSize, periodSize, |
| 2537 |
minCapture, minPlayback, maxCapture, maxPlayback; |
| 2538 |
|
| 2539 |
dir = 0; |
| 2540 |
ENSURE_( alsa_snd_pcm_hw_params_get_period_size_min( hwParamsCapture, &minCapture, &dir ), paUnanticipatedHostError ); |
| 2541 |
dir = 0; |
| 2542 |
ENSURE_( alsa_snd_pcm_hw_params_get_period_size_min( hwParamsPlayback, &minPlayback, &dir ), paUnanticipatedHostError ); |
| 2543 |
dir = 0; |
| 2544 |
ENSURE_( alsa_snd_pcm_hw_params_get_period_size_max( hwParamsCapture, &maxCapture, &dir ), paUnanticipatedHostError ); |
| 2545 |
dir = 0; |
| 2546 |
ENSURE_( alsa_snd_pcm_hw_params_get_period_size_max( hwParamsPlayback, &maxPlayback, &dir ), paUnanticipatedHostError ); |
| 2547 |
minPeriodSize = PA_MAX( minPlayback, minCapture ); |
| 2548 |
maxPeriodSize = PA_MIN( maxPlayback, maxCapture ); |
| 2549 |
PA_UNLESS( minPeriodSize <= maxPeriodSize, paBadIODeviceCombination ); |
| 2550 |
|
| 2551 |
desiredBufSz = (snd_pcm_uframes_t)( PA_MIN( outputParameters->suggestedLatency, inputParameters->suggestedLatency ) |
| 2552 |
* sampleRate ); |
| 2553 |
/* Clamp desiredBufSz */ |
| 2554 |
{ |
| 2555 |
snd_pcm_uframes_t maxBufferSize; |
| 2556 |
snd_pcm_uframes_t maxBufferSizeCapture, maxBufferSizePlayback; |
| 2557 |
ENSURE_( alsa_snd_pcm_hw_params_get_buffer_size_max( hwParamsCapture, &maxBufferSizeCapture ), paUnanticipatedHostError ); |
| 2558 |
ENSURE_( alsa_snd_pcm_hw_params_get_buffer_size_max( hwParamsPlayback, &maxBufferSizePlayback ), paUnanticipatedHostError ); |
| 2559 |
maxBufferSize = PA_MIN( maxBufferSizeCapture, maxBufferSizePlayback ); |
| 2560 |
|
| 2561 |
desiredBufSz = PA_MIN( desiredBufSz, maxBufferSize ); |
| 2562 |
} |
| 2563 |
|
| 2564 |
/* Find the closest power of 2 */ |
| 2565 |
e = ilogb( minPeriodSize ); |
| 2566 |
if( minPeriodSize & ( minPeriodSize - 1 ) ) |
| 2567 |
e += 1; |
| 2568 |
periodSize = (snd_pcm_uframes_t)pow( 2, e ); |
| 2569 |
|
| 2570 |
while( periodSize <= maxPeriodSize ) |
| 2571 |
{ |
| 2572 |
if( alsa_snd_pcm_hw_params_test_period_size( self->playback.pcm, hwParamsPlayback, periodSize, 0 ) >= 0 && |
| 2573 |
alsa_snd_pcm_hw_params_test_period_size( self->capture.pcm, hwParamsCapture, periodSize, 0 ) >= 0 ) |
| 2574 |
{ |
| 2575 |
/* OK! */ |
| 2576 |
break; |
| 2577 |
} |
| 2578 |
|
| 2579 |
periodSize *= 2; |
| 2580 |
} |
| 2581 |
|
| 2582 |
optimalPeriodSize = PA_MAX( desiredBufSz / numPeriods, minPeriodSize ); |
| 2583 |
optimalPeriodSize = PA_MIN( optimalPeriodSize, maxPeriodSize ); |
| 2584 |
|
| 2585 |
/* Find the closest power of 2 */ |
| 2586 |
e = ilogb( optimalPeriodSize ); |
| 2587 |
if( optimalPeriodSize & (optimalPeriodSize - 1) ) |
| 2588 |
e += 1; |
| 2589 |
optimalPeriodSize = (snd_pcm_uframes_t)pow( 2, e ); |
| 2590 |
|
| 2591 |
while( optimalPeriodSize >= periodSize ) |
| 2592 |
{ |
| 2593 |
if( alsa_snd_pcm_hw_params_test_period_size( self->capture.pcm, hwParamsCapture, optimalPeriodSize, 0 ) |
| 2594 |
>= 0 && alsa_snd_pcm_hw_params_test_period_size( self->playback.pcm, hwParamsPlayback, |
| 2595 |
optimalPeriodSize, 0 ) >= 0 ) |
| 2596 |
{ |
| 2597 |
break; |
| 2598 |
} |
| 2599 |
optimalPeriodSize /= 2; |
| 2600 |
} |
| 2601 |
|
| 2602 |
if( optimalPeriodSize > periodSize ) |
| 2603 |
periodSize = optimalPeriodSize; |
| 2604 |
|
| 2605 |
if( periodSize <= maxPeriodSize ) |
| 2606 |
{ |
| 2607 |
/* Looks good, the periodSize _should_ be acceptable by both devices */ |
| 2608 |
ENSURE_( alsa_snd_pcm_hw_params_set_period_size( self->capture.pcm, hwParamsCapture, periodSize, 0 ), |
| 2609 |
paUnanticipatedHostError ); |
| 2610 |
ENSURE_( alsa_snd_pcm_hw_params_set_period_size( self->playback.pcm, hwParamsPlayback, periodSize, 0 ), |
| 2611 |
paUnanticipatedHostError ); |
| 2612 |
self->capture.framesPerPeriod = self->playback.framesPerPeriod = periodSize; |
| 2613 |
framesPerHostBuffer = periodSize; |
| 2614 |
} |
| 2615 |
else |
| 2616 |
{ |
| 2617 |
/* Unable to find a common period size, oh well */ |
| 2618 |
optimalPeriodSize = PA_MAX( desiredBufSz / numPeriods, minPeriodSize ); |
| 2619 |
optimalPeriodSize = PA_MIN( optimalPeriodSize, maxPeriodSize ); |
| 2620 |
|
| 2621 |
self->capture.framesPerPeriod = optimalPeriodSize; |
| 2622 |
dir = 0; |
| 2623 |
ENSURE_( alsa_snd_pcm_hw_params_set_period_size_near( self->capture.pcm, hwParamsCapture, &self->capture.framesPerPeriod, &dir ), |
| 2624 |
paUnanticipatedHostError ); |
| 2625 |
self->playback.framesPerPeriod = optimalPeriodSize; |
| 2626 |
dir = 0; |
| 2627 |
ENSURE_( alsa_snd_pcm_hw_params_set_period_size_near( self->playback.pcm, hwParamsPlayback, &self->playback.framesPerPeriod, &dir ), |
| 2628 |
paUnanticipatedHostError ); |
| 2629 |
framesPerHostBuffer = PA_MAX( self->capture.framesPerPeriod, self->playback.framesPerPeriod ); |
| 2630 |
*hostBufferSizeMode = paUtilBoundedHostBufferSize; |
| 2631 |
} |
| 2632 |
} |
| 2633 |
else |
| 2634 |
{ |
| 2635 |
/* We choose the simple route and determine a suitable number of frames per buffer for one component of |
| 2636 |
* the stream, then we hope that this will work for the other component too (it should!). |
| 2637 |
*/ |
| 2638 |
|
| 2639 |
unsigned maxPeriods = 0; |
| 2640 |
PaAlsaStreamComponent* first = &self->capture, * second = &self->playback; |
| 2641 |
const PaStreamParameters* firstStreamParams = inputParameters; |
| 2642 |
snd_pcm_hw_params_t* firstHwParams = hwParamsCapture, * secondHwParams = hwParamsPlayback; |
| 2643 |
|
| 2644 |
dir = 0; |
| 2645 |
ENSURE_( alsa_snd_pcm_hw_params_get_periods_max( hwParamsPlayback, &maxPeriods, &dir ), paUnanticipatedHostError ); |
| 2646 |
if( maxPeriods < numPeriods ) |
| 2647 |
{ |
| 2648 |
/* The playback component is trickier to get right, try that first */ |
| 2649 |
first = &self->playback; |
| 2650 |
second = &self->capture; |
| 2651 |
firstStreamParams = outputParameters; |
| 2652 |
firstHwParams = hwParamsPlayback; |
| 2653 |
secondHwParams = hwParamsCapture; |
| 2654 |
} |
| 2655 |
|
| 2656 |
PA_ENSURE( PaAlsaStreamComponent_DetermineFramesPerBuffer( first, firstStreamParams, framesPerUserBuffer, |
| 2657 |
sampleRate, firstHwParams, &accurate ) ); |
| 2658 |
|
| 2659 |
second->framesPerPeriod = first->framesPerPeriod; |
| 2660 |
dir = 0; |
| 2661 |
ENSURE_( alsa_snd_pcm_hw_params_set_period_size_near( second->pcm, secondHwParams, &second->framesPerPeriod, &dir ), |
| 2662 |
paUnanticipatedHostError ); |
| 2663 |
if( self->capture.framesPerPeriod == self->playback.framesPerPeriod ) |
| 2664 |
{ |
| 2665 |
framesPerHostBuffer = self->capture.framesPerPeriod; |
| 2666 |
} |
| 2667 |
else |
| 2668 |
{ |
| 2669 |
framesPerHostBuffer = PA_MAX( self->capture.framesPerPeriod, self->playback.framesPerPeriod ); |
| 2670 |
*hostBufferSizeMode = paUtilBoundedHostBufferSize; |
| 2671 |
} |
| 2672 |
} |
| 2673 |
} |
| 2674 |
else /* half-duplex is a slightly simpler case */ |
| 2675 |
{ |
| 2676 |
if( self->capture.pcm ) |
| 2677 |
{ |
| 2678 |
PA_ENSURE( PaAlsaStreamComponent_DetermineFramesPerBuffer( &self->capture, inputParameters, framesPerUserBuffer, |
| 2679 |
sampleRate, hwParamsCapture, &accurate) ); |
| 2680 |
framesPerHostBuffer = self->capture.framesPerPeriod; |
| 2681 |
} |
| 2682 |
else |
| 2683 |
{ |
| 2684 |
assert( self->playback.pcm ); |
| 2685 |
PA_ENSURE( PaAlsaStreamComponent_DetermineFramesPerBuffer( &self->playback, outputParameters, framesPerUserBuffer, |
| 2686 |
sampleRate, hwParamsPlayback, &accurate ) ); |
| 2687 |
framesPerHostBuffer = self->playback.framesPerPeriod; |
| 2688 |
} |
| 2689 |
} |
| 2690 |
|
| 2691 |
PA_UNLESS( framesPerHostBuffer != 0, paInternalError ); |
| 2692 |
self->maxFramesPerHostBuffer = framesPerHostBuffer; |
| 2693 |
|
| 2694 |
if( !self->playback.canMmap || !accurate ) |
| 2695 |
{ |
| 2696 |
/* Don't know the exact size per host buffer */ |
| 2697 |
*hostBufferSizeMode = paUtilBoundedHostBufferSize; |
| 2698 |
/* Raise upper bound */ |
| 2699 |
if( !accurate ) |
| 2700 |
++self->maxFramesPerHostBuffer; |
| 2701 |
} |
| 2702 |
|
| 2703 |
error: |
| 2704 |
return result; |
| 2705 |
} |
| 2706 |
|
| 2707 |
/** Set up ALSA stream parameters. |
| 2708 |
* |
| 2709 |
*/ |
| 2710 |
static PaError PaAlsaStream_Configure( PaAlsaStream *self, const PaStreamParameters *inParams, const PaStreamParameters* |
| 2711 |
outParams, double sampleRate, unsigned long framesPerUserBuffer, double* inputLatency, double* outputLatency, |
| 2712 |
PaUtilHostBufferSizeMode* hostBufferSizeMode ) |
| 2713 |
{ |
| 2714 |
PaError result = paNoError; |
| 2715 |
double realSr = sampleRate; |
| 2716 |
snd_pcm_hw_params_t* hwParamsCapture, * hwParamsPlayback; |
| 2717 |
|
| 2718 |
alsa_snd_pcm_hw_params_alloca( &hwParamsCapture ); |
| 2719 |
alsa_snd_pcm_hw_params_alloca( &hwParamsPlayback ); |
| 2720 |
|
| 2721 |
if( self->capture.pcm ) |
| 2722 |
PA_ENSURE( PaAlsaStreamComponent_InitialConfigure( &self->capture, inParams, self->primeBuffers, hwParamsCapture, |
| 2723 |
&realSr ) ); |
| 2724 |
if( self->playback.pcm ) |
| 2725 |
PA_ENSURE( PaAlsaStreamComponent_InitialConfigure( &self->playback, outParams, self->primeBuffers, hwParamsPlayback, |
| 2726 |
&realSr ) ); |
| 2727 |
|
| 2728 |
PA_ENSURE( PaAlsaStream_DetermineFramesPerBuffer( self, realSr, inParams, outParams, framesPerUserBuffer, |
| 2729 |
hwParamsCapture, hwParamsPlayback, hostBufferSizeMode ) ); |
| 2730 |
|
| 2731 |
if( self->capture.pcm ) |
| 2732 |
{ |
| 2733 |
assert( self->capture.framesPerPeriod != 0 ); |
| 2734 |
PA_ENSURE( PaAlsaStreamComponent_FinishConfigure( &self->capture, hwParamsCapture, inParams, self->primeBuffers, realSr, |
| 2735 |
inputLatency ) ); |
| 2736 |
PA_DEBUG(( "%s: Capture period size: %lu, latency: %f\n", __FUNCTION__, self->capture.framesPerPeriod, *inputLatency )); |
| 2737 |
} |
| 2738 |
if( self->playback.pcm ) |
| 2739 |
{ |
| 2740 |
assert( self->playback.framesPerPeriod != 0 ); |
| 2741 |
PA_ENSURE( PaAlsaStreamComponent_FinishConfigure( &self->playback, hwParamsPlayback, outParams, self->primeBuffers, realSr, |
| 2742 |
outputLatency ) ); |
| 2743 |
PA_DEBUG(( "%s: Playback period size: %lu, latency: %f\n", __FUNCTION__, self->playback.framesPerPeriod, *outputLatency )); |
| 2744 |
} |
| 2745 |
|
| 2746 |
/* Should be exact now */ |
| 2747 |
self->streamRepresentation.streamInfo.sampleRate = realSr; |
| 2748 |
|
| 2749 |
/* this will cause the two streams to automatically start/stop/prepare in sync. |
| 2750 |
* We only need to execute these operations on one of the pair. |
| 2751 |
* A: We don't want to do this on a blocking stream. |
| 2752 |
*/ |
| 2753 |
if( self->callbackMode && self->capture.pcm && self->playback.pcm ) |
| 2754 |
{ |
| 2755 |
int err = alsa_snd_pcm_link( self->capture.pcm, self->playback.pcm ); |
| 2756 |
if( err == 0 ) |
| 2757 |
self->pcmsSynced = 1; |
| 2758 |
else |
| 2759 |
PA_DEBUG(( "%s: Unable to sync pcms: %s\n", __FUNCTION__, alsa_snd_strerror( err ) )); |
| 2760 |
} |
| 2761 |
|
| 2762 |
{ |
| 2763 |
unsigned long minFramesPerHostBuffer = PA_MIN( self->capture.pcm ? self->capture.framesPerPeriod : ULONG_MAX, |
| 2764 |
self->playback.pcm ? self->playback.framesPerPeriod : ULONG_MAX ); |
| 2765 |
self->pollTimeout = CalculatePollTimeout( self, minFramesPerHostBuffer ); /* Period in msecs, rounded up */ |
| 2766 |
|
| 2767 |
/* Time before watchdog unthrottles realtime thread == 1/4 of period time in msecs */ |
| 2768 |
/* self->threading.throttledSleepTime = (unsigned long) (minFramesPerHostBuffer / sampleRate / 4 * 1000); */ |
| 2769 |
} |
| 2770 |
|
| 2771 |
if( self->callbackMode ) |
| 2772 |
{ |
| 2773 |
/* If the user expects a certain number of frames per callback we will either have to rely on block adaption |
| 2774 |
* (framesPerHostBuffer is not an integer multiple of framesPerPeriod) or we can simply align the number |
| 2775 |
* of host buffer frames with what the user specified */ |
| 2776 |
if( self->framesPerUserBuffer != paFramesPerBufferUnspecified ) |
| 2777 |
{ |
| 2778 |
/* self->alignFrames = 1; */ |
| 2779 |
|
| 2780 |
/* Unless the ratio between number of host and user buffer frames is an integer we will have to rely |
| 2781 |
* on block adaption */ |
| 2782 |
/* |
| 2783 |
if( framesPerHostBuffer % framesPerPeriod != 0 || (self->capture.pcm && self->playback.pcm && |
| 2784 |
self->capture.framesPerPeriod != self->playback.framesPerPeriod) ) |
| 2785 |
self->useBlockAdaption = 1; |
| 2786 |
else |
| 2787 |
self->alignFrames = 1; |
| 2788 |
*/ |
| 2789 |
} |
| 2790 |
} |
| 2791 |
|
| 2792 |
error: |
| 2793 |
return result; |
| 2794 |
} |
| 2795 |
|
| 2796 |
static PaError OpenStream( struct PaUtilHostApiRepresentation *hostApi, |
| 2797 |
PaStream** s, |
| 2798 |
const PaStreamParameters *inputParameters, |
| 2799 |
const PaStreamParameters *outputParameters, |
| 2800 |
double sampleRate, |
| 2801 |
unsigned long framesPerBuffer, |
| 2802 |
PaStreamFlags streamFlags, |
| 2803 |
PaStreamCallback* callback, |
| 2804 |
void *userData ) |
| 2805 |
{ |
| 2806 |
PaError result = paNoError; |
| 2807 |
PaAlsaHostApiRepresentation *alsaHostApi = (PaAlsaHostApiRepresentation*)hostApi; |
| 2808 |
PaAlsaStream *stream = NULL; |
| 2809 |
PaSampleFormat hostInputSampleFormat = 0, hostOutputSampleFormat = 0; |
| 2810 |
PaSampleFormat inputSampleFormat = 0, outputSampleFormat = 0; |
| 2811 |
int numInputChannels = 0, numOutputChannels = 0; |
| 2812 |
PaTime inputLatency, outputLatency; |
| 2813 |
/* Operate with fixed host buffer size by default, since other modes will invariably lead to block adaption */ |
| 2814 |
/* XXX: Use Bounded by default? Output tends to get stuttery with Fixed ... */ |
| 2815 |
PaUtilHostBufferSizeMode hostBufferSizeMode = paUtilFixedHostBufferSize; |
| 2816 |
|
| 2817 |
if( ( streamFlags & paPlatformSpecificFlags ) != 0 ) |
| 2818 |
return paInvalidFlag; |
| 2819 |
|
| 2820 |
if( inputParameters ) |
| 2821 |
{ |
| 2822 |
PA_ENSURE( ValidateParameters( inputParameters, hostApi, StreamDirection_In ) ); |
| 2823 |
|
| 2824 |
numInputChannels = inputParameters->channelCount; |
| 2825 |
inputSampleFormat = inputParameters->sampleFormat; |
| 2826 |
} |
| 2827 |
if( outputParameters ) |
| 2828 |
{ |
| 2829 |
PA_ENSURE( ValidateParameters( outputParameters, hostApi, StreamDirection_Out ) ); |
| 2830 |
|
| 2831 |
numOutputChannels = outputParameters->channelCount; |
| 2832 |
outputSampleFormat = outputParameters->sampleFormat; |
| 2833 |
} |
| 2834 |
|
| 2835 |
/* XXX: Why do we support this anyway? */ |
| 2836 |
if( framesPerBuffer == paFramesPerBufferUnspecified && getenv( "PA_ALSA_PERIODSIZE" ) != NULL ) |
| 2837 |
{ |
| 2838 |
PA_DEBUG(( "%s: Getting framesPerBuffer (Alsa period-size) from environment\n", __FUNCTION__ )); |
| 2839 |
framesPerBuffer = atoi( getenv("PA_ALSA_PERIODSIZE") ); |
| 2840 |
} |
| 2841 |
|
| 2842 |
PA_UNLESS( stream = (PaAlsaStream*)PaUtil_AllocateMemory( sizeof(PaAlsaStream) ), paInsufficientMemory ); |
| 2843 |
PA_ENSURE( PaAlsaStream_Initialize( stream, alsaHostApi, inputParameters, outputParameters, sampleRate, |
| 2844 |
framesPerBuffer, callback, streamFlags, userData ) ); |
| 2845 |
|
| 2846 |
PA_ENSURE( PaAlsaStream_Configure( stream, inputParameters, outputParameters, sampleRate, framesPerBuffer, |
| 2847 |
&inputLatency, &outputLatency, &hostBufferSizeMode ) ); |
| 2848 |
hostInputSampleFormat = stream->capture.hostSampleFormat | (!stream->capture.hostInterleaved ? paNonInterleaved : 0); |
| 2849 |
hostOutputSampleFormat = stream->playback.hostSampleFormat | (!stream->playback.hostInterleaved ? paNonInterleaved : 0); |
| 2850 |
|
| 2851 |
PA_ENSURE( PaUtil_InitializeBufferProcessor( &stream->bufferProcessor, |
| 2852 |
numInputChannels, inputSampleFormat, hostInputSampleFormat, |
| 2853 |
numOutputChannels, outputSampleFormat, hostOutputSampleFormat, |
| 2854 |
sampleRate, streamFlags, framesPerBuffer, stream->maxFramesPerHostBuffer, |
| 2855 |
hostBufferSizeMode, callback, userData ) ); |
| 2856 |
|
| 2857 |
/* Ok, buffer processor is initialized, now we can deduce it's latency */ |
| 2858 |
if( numInputChannels > 0 ) |
| 2859 |
stream->streamRepresentation.streamInfo.inputLatency = inputLatency + (PaTime)( |
| 2860 |
PaUtil_GetBufferProcessorInputLatencyFrames( &stream->bufferProcessor ) / sampleRate); |
| 2861 |
if( numOutputChannels > 0 ) |
| 2862 |
stream->streamRepresentation.streamInfo.outputLatency = outputLatency + (PaTime)( |
| 2863 |
PaUtil_GetBufferProcessorOutputLatencyFrames( &stream->bufferProcessor ) / sampleRate); |
| 2864 |
|
| 2865 |
PA_DEBUG(( "%s: Stream: framesPerBuffer = %lu, maxFramesPerHostBuffer = %lu, latency i=%f, o=%f\n", __FUNCTION__, framesPerBuffer, stream->maxFramesPerHostBuffer, stream->streamRepresentation.streamInfo.inputLatency, stream->streamRepresentation.streamInfo.outputLatency)); |
| 2866 |
|
| 2867 |
*s = (PaStream*)stream; |
| 2868 |
|
| 2869 |
return result; |
| 2870 |
|
| 2871 |
error: |
| 2872 |
if( stream ) |
| 2873 |
{ |
| 2874 |
PA_DEBUG(( "%s: Stream in error, terminating\n", __FUNCTION__ )); |
| 2875 |
PaAlsaStream_Terminate( stream ); |
| 2876 |
} |
| 2877 |
|
| 2878 |
return result; |
| 2879 |
} |
| 2880 |
|
| 2881 |
static PaError CloseStream( PaStream* s ) |
| 2882 |
{ |
| 2883 |
PaError result = paNoError; |
| 2884 |
PaAlsaStream *stream = (PaAlsaStream*)s; |
| 2885 |
|
| 2886 |
PaUtil_TerminateBufferProcessor( &stream->bufferProcessor ); |
| 2887 |
PaUtil_TerminateStreamRepresentation( &stream->streamRepresentation ); |
| 2888 |
|
| 2889 |
PaAlsaStream_Terminate( stream ); |
| 2890 |
|
| 2891 |
return result; |
| 2892 |
} |
| 2893 |
|
| 2894 |
static void SilenceBuffer( PaAlsaStream *stream ) |
| 2895 |
{ |
| 2896 |
const snd_pcm_channel_area_t *areas; |
| 2897 |
snd_pcm_uframes_t frames = (snd_pcm_uframes_t)alsa_snd_pcm_avail_update( stream->playback.pcm ), offset; |
| 2898 |
|
| 2899 |
alsa_snd_pcm_mmap_begin( stream->playback.pcm, &areas, &offset, &frames ); |
| 2900 |
alsa_snd_pcm_areas_silence( areas, offset, stream->playback.numHostChannels, frames, stream->playback.nativeFormat ); |
| 2901 |
alsa_snd_pcm_mmap_commit( stream->playback.pcm, offset, frames ); |
| 2902 |
} |
| 2903 |
|
| 2904 |
/** Start/prepare pcm(s) for streaming. |
| 2905 |
* |
| 2906 |
* Depending on whether the stream is in callback or blocking mode, we will respectively start or simply |
| 2907 |
* prepare the playback pcm. If the buffer has _not_ been primed, we will in callback mode prepare and |
| 2908 |
* silence the buffer before starting playback. In blocking mode we simply prepare, as the playback will |
| 2909 |
* be started automatically as the user writes to output. |
| 2910 |
* |
| 2911 |
* The capture pcm, however, will simply be prepared and started. |
| 2912 |
*/ |
| 2913 |
static PaError AlsaStart( PaAlsaStream *stream, int priming ) |
| 2914 |
{ |
| 2915 |
PaError result = paNoError; |
| 2916 |
|
| 2917 |
if( stream->playback.pcm ) |
| 2918 |
{ |
| 2919 |
if( stream->callbackMode ) |
| 2920 |
{ |
| 2921 |
if( !priming ) |
| 2922 |
{ |
| 2923 |
/* Buffer isn't primed, so prepare and silence */ |
| 2924 |
ENSURE_( alsa_snd_pcm_prepare( stream->playback.pcm ), paUnanticipatedHostError ); |
| 2925 |
if( stream->playback.canMmap ) |
| 2926 |
SilenceBuffer( stream ); |
| 2927 |
} |
| 2928 |
if( stream->playback.canMmap ) |
| 2929 |
ENSURE_( alsa_snd_pcm_start( stream->playback.pcm ), paUnanticipatedHostError ); |
| 2930 |
} |
| 2931 |
else |
| 2932 |
ENSURE_( alsa_snd_pcm_prepare( stream->playback.pcm ), paUnanticipatedHostError ); |
| 2933 |
} |
| 2934 |
if( stream->capture.pcm && !stream->pcmsSynced ) |
| 2935 |
{ |
| 2936 |
ENSURE_( alsa_snd_pcm_prepare( stream->capture.pcm ), paUnanticipatedHostError ); |
| 2937 |
/* For a blocking stream we want to start capture as well, since nothing will happen otherwise */ |
| 2938 |
ENSURE_( alsa_snd_pcm_start( stream->capture.pcm ), paUnanticipatedHostError ); |
| 2939 |
} |
| 2940 |
|
| 2941 |
end: |
| 2942 |
return result; |
| 2943 |
error: |
| 2944 |
goto end; |
| 2945 |
} |
| 2946 |
|
| 2947 |
/** Utility function for determining if pcms are in running state. |
| 2948 |
* |
| 2949 |
*/ |
| 2950 |
#if 0 |
| 2951 |
static int IsRunning( PaAlsaStream *stream ) |
| 2952 |
{ |
| 2953 |
int result = 0; |
| 2954 |
|
| 2955 |
PA_ENSURE( PaUnixMutex_Lock( &stream->stateMtx ) ); |
| 2956 |
if( stream->capture.pcm ) |
| 2957 |
{ |
| 2958 |
snd_pcm_state_t capture_state = alsa_snd_pcm_state( stream->capture.pcm ); |
| 2959 |
|
| 2960 |
if( capture_state == SND_PCM_STATE_RUNNING || capture_state == SND_PCM_STATE_XRUN |
| 2961 |
|| capture_state == SND_PCM_STATE_DRAINING ) |
| 2962 |
{ |
| 2963 |
result = 1; |
| 2964 |
goto end; |
| 2965 |
} |
| 2966 |
} |
| 2967 |
|
| 2968 |
if( stream->playback.pcm ) |
| 2969 |
{ |
| 2970 |
snd_pcm_state_t playback_state = alsa_snd_pcm_state( stream->playback.pcm ); |
| 2971 |
|
| 2972 |
if( playback_state == SND_PCM_STATE_RUNNING || playback_state == SND_PCM_STATE_XRUN |
| 2973 |
|| playback_state == SND_PCM_STATE_DRAINING ) |
| 2974 |
{ |
| 2975 |
result = 1; |
| 2976 |
goto end; |
| 2977 |
} |
| 2978 |
} |
| 2979 |
|
| 2980 |
end: |
| 2981 |
ASSERT_CALL_( PaUnixMutex_Unlock( &stream->stateMtx ), paNoError ); |
| 2982 |
return result; |
| 2983 |
error: |
| 2984 |
goto error; |
| 2985 |
} |
| 2986 |
#endif |
| 2987 |
|
| 2988 |
static PaError StartStream( PaStream *s ) |
| 2989 |
{ |
| 2990 |
PaError result = paNoError; |
| 2991 |
PaAlsaStream* stream = (PaAlsaStream*)s; |
| 2992 |
int streamStarted = 0; /* So we can know whether we need to take the stream down */ |
| 2993 |
|
| 2994 |
/* Ready the processor */ |
| 2995 |
PaUtil_ResetBufferProcessor( &stream->bufferProcessor ); |
| 2996 |
|
| 2997 |
/* Set now, so we can test for activity further down */ |
| 2998 |
stream->isActive = 1; |
| 2999 |
|
| 3000 |
if( stream->callbackMode ) |
| 3001 |
{ |
| 3002 |
PA_ENSURE( PaUnixThread_New( &stream->thread, &CallbackThreadFunc, stream, 1., stream->rtSched ) ); |
| 3003 |
} |
| 3004 |
else |
| 3005 |
{ |
| 3006 |
PA_ENSURE( AlsaStart( stream, 0 ) ); |
| 3007 |
streamStarted = 1; |
| 3008 |
} |
| 3009 |
|
| 3010 |
end: |
| 3011 |
return result; |
| 3012 |
error: |
| 3013 |
if( streamStarted ) |
| 3014 |
{ |
| 3015 |
AbortStream( stream ); |
| 3016 |
} |
| 3017 |
stream->isActive = 0; |
| 3018 |
|
| 3019 |
goto end; |
| 3020 |
} |
| 3021 |
|
| 3022 |
/** Stop PCM handle, either softly or abruptly. |
| 3023 |
*/ |
| 3024 |
static PaError AlsaStop( PaAlsaStream *stream, int abort ) |
| 3025 |
{ |
| 3026 |
PaError result = paNoError; |
| 3027 |
/* XXX: alsa_snd_pcm_drain tends to lock up, avoid it until we find out more */ |
| 3028 |
abort = 1; |
| 3029 |
/* |
| 3030 |
if( stream->capture.pcm && !strcmp( Pa_GetDeviceInfo( stream->capture.device )->name, |
| 3031 |
"dmix" ) ) |
| 3032 |
{ |
| 3033 |
abort = 1; |
| 3034 |
} |
| 3035 |
else if( stream->playback.pcm && !strcmp( Pa_GetDeviceInfo( stream->playback.device )->name, |
| 3036 |
"dmix" ) ) |
| 3037 |
{ |
| 3038 |
abort = 1; |
| 3039 |
} |
| 3040 |
*/ |
| 3041 |
|
| 3042 |
if( abort ) |
| 3043 |
{ |
| 3044 |
if( stream->playback.pcm ) |
| 3045 |
{ |
| 3046 |
ENSURE_( alsa_snd_pcm_drop( stream->playback.pcm ), paUnanticipatedHostError ); |
| 3047 |
} |
| 3048 |
if( stream->capture.pcm && !stream->pcmsSynced ) |
| 3049 |
{ |
| 3050 |
ENSURE_( alsa_snd_pcm_drop( stream->capture.pcm ), paUnanticipatedHostError ); |
| 3051 |
} |
| 3052 |
|
| 3053 |
PA_DEBUG(( "%s: Dropped frames\n", __FUNCTION__ )); |
| 3054 |
} |
| 3055 |
else |
| 3056 |
{ |
| 3057 |
if( stream->playback.pcm ) |
| 3058 |
{ |
| 3059 |
ENSURE_( alsa_snd_pcm_nonblock( stream->playback.pcm, 0 ), paUnanticipatedHostError ); |
| 3060 |
if( alsa_snd_pcm_drain( stream->playback.pcm ) < 0 ) |
| 3061 |
{ |
| 3062 |
PA_DEBUG(( "%s: Draining playback handle failed!\n", __FUNCTION__ )); |
| 3063 |
} |
| 3064 |
} |
| 3065 |
if( stream->capture.pcm && !stream->pcmsSynced ) |
| 3066 |
{ |
| 3067 |
/* We don't need to retrieve any remaining frames */ |
| 3068 |
if( alsa_snd_pcm_drain( stream->capture.pcm ) < 0 ) |
| 3069 |
{ |
| 3070 |
PA_DEBUG(( "%s: Draining capture handle failed!\n", __FUNCTION__ )); |
| 3071 |
} |
| 3072 |
} |
| 3073 |
} |
| 3074 |
|
| 3075 |
end: |
| 3076 |
return result; |
| 3077 |
error: |
| 3078 |
goto end; |
| 3079 |
} |
| 3080 |
|
| 3081 |
/** Stop or abort stream. |
| 3082 |
* |
| 3083 |
* If a stream is in callback mode we will have to inspect whether the background thread has |
| 3084 |
* finished, or we will have to take it out. In either case we join the thread before |
| 3085 |
* returning. In blocking mode, we simply tell ALSA to stop abruptly (abort) or finish |
| 3086 |
* buffers (drain) |
| 3087 |
* |
| 3088 |
* Stream will be considered inactive (!PaAlsaStream::isActive) after a call to this function |
| 3089 |
*/ |
| 3090 |
static PaError RealStop( PaAlsaStream *stream, int abort ) |
| 3091 |
{ |
| 3092 |
PaError result = paNoError; |
| 3093 |
|
| 3094 |
/* First deal with the callback thread, cancelling and/or joining |
| 3095 |
* it if necessary |
| 3096 |
*/ |
| 3097 |
if( stream->callbackMode ) |
| 3098 |
{ |
| 3099 |
PaError threadRes; |
| 3100 |
stream->callbackAbort = abort; |
| 3101 |
|
| 3102 |
if( !abort ) |
| 3103 |
{ |
| 3104 |
PA_DEBUG(( "Stopping callback\n" )); |
| 3105 |
} |
| 3106 |
PA_ENSURE( PaUnixThread_Terminate( &stream->thread, !abort, &threadRes ) ); |
| 3107 |
if( threadRes != paNoError ) |
| 3108 |
{ |
| 3109 |
PA_DEBUG(( "Callback thread returned: %d\n", threadRes )); |
| 3110 |
} |
| 3111 |
#if 0 |
| 3112 |
if( watchdogRes != paNoError ) |
| 3113 |
PA_DEBUG(( "Watchdog thread returned: %d\n", watchdogRes )); |
| 3114 |
#endif |
| 3115 |
|
| 3116 |
stream->callback_finished = 0; |
| 3117 |
} |
| 3118 |
else |
| 3119 |
{ |
| 3120 |
PA_ENSURE( AlsaStop( stream, abort ) ); |
| 3121 |
} |
| 3122 |
|
| 3123 |
stream->isActive = 0; |
| 3124 |
|
| 3125 |
end: |
| 3126 |
return result; |
| 3127 |
|
| 3128 |
error: |
| 3129 |
goto end; |
| 3130 |
} |
| 3131 |
|
| 3132 |
static PaError StopStream( PaStream *s ) |
| 3133 |
{ |
| 3134 |
return RealStop( (PaAlsaStream *) s, 0 ); |
| 3135 |
} |
| 3136 |
|
| 3137 |
static PaError AbortStream( PaStream *s ) |
| 3138 |
{ |
| 3139 |
return RealStop( (PaAlsaStream * ) s, 1 ); |
| 3140 |
} |
| 3141 |
|
| 3142 |
/** The stream is considered stopped before StartStream, or AFTER a call to Abort/StopStream (callback |
| 3143 |
* returning !paContinue is not considered) |
| 3144 |
* |
| 3145 |
*/ |
| 3146 |
static PaError IsStreamStopped( PaStream *s ) |
| 3147 |
{ |
| 3148 |
PaAlsaStream *stream = (PaAlsaStream *)s; |
| 3149 |
|
| 3150 |
/* callback_finished indicates we need to join callback thread (ie. in Abort/StopStream) */ |
| 3151 |
return !IsStreamActive( s ) && !stream->callback_finished; |
| 3152 |
} |
| 3153 |
|
| 3154 |
static PaError IsStreamActive( PaStream *s ) |
| 3155 |
{ |
| 3156 |
PaAlsaStream *stream = (PaAlsaStream*)s; |
| 3157 |
return stream->isActive; |
| 3158 |
} |
| 3159 |
|
| 3160 |
static PaTime GetStreamTime( PaStream *s ) |
| 3161 |
{ |
| 3162 |
PaAlsaStream *stream = (PaAlsaStream*)s; |
| 3163 |
|
| 3164 |
snd_timestamp_t timestamp; |
| 3165 |
snd_pcm_status_t* status; |
| 3166 |
alsa_snd_pcm_status_alloca( &status ); |
| 3167 |
|
| 3168 |
/* TODO: what if we have both? does it really matter? */ |
| 3169 |
|
| 3170 |
/* TODO: if running in callback mode, this will mean |
| 3171 |
* libasound routines are being called from multiple threads. |
| 3172 |
* need to verify that libasound is thread-safe. */ |
| 3173 |
|
| 3174 |
if( stream->capture.pcm ) |
| 3175 |
{ |
| 3176 |
alsa_snd_pcm_status( stream->capture.pcm, status ); |
| 3177 |
} |
| 3178 |
else if( stream->playback.pcm ) |
| 3179 |
{ |
| 3180 |
alsa_snd_pcm_status( stream->playback.pcm, status ); |
| 3181 |
} |
| 3182 |
|
| 3183 |
alsa_snd_pcm_status_get_tstamp( status, ×tamp ); |
| 3184 |
return timestamp.tv_sec + (PaTime)timestamp.tv_usec / 1e6; |
| 3185 |
} |
| 3186 |
|
| 3187 |
static double GetStreamCpuLoad( PaStream* s ) |
| 3188 |
{ |
| 3189 |
PaAlsaStream *stream = (PaAlsaStream*)s; |
| 3190 |
|
| 3191 |
return PaUtil_GetCpuLoad( &stream->cpuLoadMeasurer ); |
| 3192 |
} |
| 3193 |
|
| 3194 |
/* Set the stream sample rate to a nominal value requested; allow only a defined tolerance range */ |
| 3195 |
static int SetApproximateSampleRate( snd_pcm_t *pcm, snd_pcm_hw_params_t *hwParams, double sampleRate ) |
| 3196 |
{ |
| 3197 |
PaError result = paNoError; |
| 3198 |
unsigned int reqRate, setRate, deviation; |
| 3199 |
|
| 3200 |
assert( pcm && hwParams ); |
| 3201 |
|
| 3202 |
/* The Alsa sample rate is set by integer value; also the actual rate may differ */ |
| 3203 |
reqRate = setRate = (unsigned int) sampleRate; |
| 3204 |
|
| 3205 |
ENSURE_( alsa_snd_pcm_hw_params_set_rate_near( pcm, hwParams, &setRate, NULL ), paUnanticipatedHostError ); |
| 3206 |
/* The value actually set will be put in 'setRate' (may be way off); check the deviation as a proportion |
| 3207 |
* of the requested-rate with reference to the max-deviate-ratio (larger values allow less deviation) */ |
| 3208 |
deviation = abs( (int)setRate - (int)reqRate ); |
| 3209 |
if( deviation > 0 && deviation * RATE_MAX_DEVIATE_RATIO > reqRate ) |
| 3210 |
result = paInvalidSampleRate; |
| 3211 |
|
| 3212 |
end: |
| 3213 |
return result; |
| 3214 |
|
| 3215 |
error: |
| 3216 |
/* Log */ |
| 3217 |
{ |
| 3218 |
unsigned int _min = 0, _max = 0; |
| 3219 |
int _dir = 0; |
| 3220 |
ENSURE_( alsa_snd_pcm_hw_params_get_rate_min( hwParams, &_min, &_dir ), paUnanticipatedHostError ); |
| 3221 |
_dir = 0; |
| 3222 |
ENSURE_( alsa_snd_pcm_hw_params_get_rate_max( hwParams, &_max, &_dir ), paUnanticipatedHostError ); |
| 3223 |
PA_DEBUG(( "%s: SR min = %u, max = %u, req = %u\n", __FUNCTION__, _min, _max, reqRate )); |
| 3224 |
} |
| 3225 |
goto end; |
| 3226 |
} |
| 3227 |
|
| 3228 |
/* Return exact sample rate in param sampleRate */ |
| 3229 |
static int GetExactSampleRate( snd_pcm_hw_params_t *hwParams, double *sampleRate ) |
| 3230 |
{ |
| 3231 |
unsigned int num, den = 1; |
| 3232 |
int err; |
| 3233 |
|
| 3234 |
assert( hwParams ); |
| 3235 |
|
| 3236 |
err = alsa_snd_pcm_hw_params_get_rate_numden( hwParams, &num, &den ); |
| 3237 |
*sampleRate = (double) num / den; |
| 3238 |
|
| 3239 |
return err; |
| 3240 |
} |
| 3241 |
|
| 3242 |
/* Utility functions for blocking/callback interfaces */ |
| 3243 |
|
| 3244 |
/* Atomic restart of stream (we don't want the intermediate state visible) */ |
| 3245 |
static PaError AlsaRestart( PaAlsaStream *stream ) |
| 3246 |
{ |
| 3247 |
PaError result = paNoError; |
| 3248 |
|
| 3249 |
PA_ENSURE( PaUnixMutex_Lock( &stream->stateMtx ) ); |
| 3250 |
PA_ENSURE( AlsaStop( stream, 0 ) ); |
| 3251 |
PA_ENSURE( AlsaStart( stream, 0 ) ); |
| 3252 |
|
| 3253 |
PA_DEBUG(( "%s: Restarted audio\n", __FUNCTION__ )); |
| 3254 |
|
| 3255 |
error: |
| 3256 |
PA_ENSURE( PaUnixMutex_Unlock( &stream->stateMtx ) ); |
| 3257 |
|
| 3258 |
return result; |
| 3259 |
} |
| 3260 |
|
| 3261 |
/** Recover from xrun state. |
| 3262 |
* |
| 3263 |
*/ |
| 3264 |
static PaError PaAlsaStream_HandleXrun( PaAlsaStream *self ) |
| 3265 |
{ |
| 3266 |
PaError result = paNoError; |
| 3267 |
snd_pcm_status_t *st; |
| 3268 |
PaTime now = PaUtil_GetTime(); |
| 3269 |
snd_timestamp_t t; |
| 3270 |
int restartAlsa = 0; /* do not restart Alsa by default */ |
| 3271 |
|
| 3272 |
alsa_snd_pcm_status_alloca( &st ); |
| 3273 |
|
| 3274 |
if( self->playback.pcm ) |
| 3275 |
{ |
| 3276 |
alsa_snd_pcm_status( self->playback.pcm, st ); |
| 3277 |
if( alsa_snd_pcm_status_get_state( st ) == SND_PCM_STATE_XRUN ) |
| 3278 |
{ |
| 3279 |
alsa_snd_pcm_status_get_trigger_tstamp( st, &t ); |
| 3280 |
self->underrun = now * 1000 - ( (PaTime)t.tv_sec * 1000 + (PaTime)t.tv_usec / 1000 ); |
| 3281 |
|
| 3282 |
if( !self->playback.canMmap ) |
| 3283 |
{ |
| 3284 |
if( alsa_snd_pcm_recover( self->playback.pcm, -EPIPE, 0 ) < 0 ) |
| 3285 |
{ |
| 3286 |
PA_DEBUG(( "%s: [playback] non-MMAP-PCM failed recovering from XRUN, will restart Alsa\n", __FUNCTION__ )); |
| 3287 |
++ restartAlsa; /* did not manage to recover */ |
| 3288 |
} |
| 3289 |
} |
| 3290 |
else |
| 3291 |
++ restartAlsa; /* always restart MMAPed device */ |
| 3292 |
} |
| 3293 |
} |
| 3294 |
if( self->capture.pcm ) |
| 3295 |
{ |
| 3296 |
alsa_snd_pcm_status( self->capture.pcm, st ); |
| 3297 |
if( alsa_snd_pcm_status_get_state( st ) == SND_PCM_STATE_XRUN ) |
| 3298 |
{ |
| 3299 |
alsa_snd_pcm_status_get_trigger_tstamp( st, &t ); |
| 3300 |
self->overrun = now * 1000 - ((PaTime) t.tv_sec * 1000 + (PaTime) t.tv_usec / 1000); |
| 3301 |
|
| 3302 |
if (!self->capture.canMmap) |
| 3303 |
{ |
| 3304 |
if (alsa_snd_pcm_recover( self->capture.pcm, -EPIPE, 0 ) < 0) |
| 3305 |
{ |
| 3306 |
PA_DEBUG(( "%s: [capture] non-MMAP-PCM failed recovering from XRUN, will restart Alsa\n", __FUNCTION__ )); |
| 3307 |
++ restartAlsa; /* did not manage to recover */ |
| 3308 |
} |
| 3309 |
} |
| 3310 |
else |
| 3311 |
++ restartAlsa; /* always restart MMAPed device */ |
| 3312 |
} |
| 3313 |
} |
| 3314 |
|
| 3315 |
if( restartAlsa ) |
| 3316 |
{ |
| 3317 |
PA_DEBUG(( "%s: restarting Alsa to recover from XRUN\n", __FUNCTION__ )); |
| 3318 |
PA_ENSURE( AlsaRestart( self ) ); |
| 3319 |
} |
| 3320 |
|
| 3321 |
end: |
| 3322 |
return result; |
| 3323 |
error: |
| 3324 |
goto end; |
| 3325 |
} |
| 3326 |
|
| 3327 |
/** Decide if we should continue polling for specified direction, eventually adjust the poll timeout. |
| 3328 |
* |
| 3329 |
*/ |
| 3330 |
static PaError ContinuePoll( const PaAlsaStream *stream, StreamDirection streamDir, int *pollTimeout, int *continuePoll ) |
| 3331 |
{ |
| 3332 |
PaError result = paNoError; |
| 3333 |
snd_pcm_sframes_t delay, margin; |
| 3334 |
int err; |
| 3335 |
const PaAlsaStreamComponent *component = NULL, *otherComponent = NULL; |
| 3336 |
|
| 3337 |
*continuePoll = 1; |
| 3338 |
|
| 3339 |
if( StreamDirection_In == streamDir ) |
| 3340 |
{ |
| 3341 |
component = &stream->capture; |
| 3342 |
otherComponent = &stream->playback; |
| 3343 |
} |
| 3344 |
else |
| 3345 |
{ |
| 3346 |
component = &stream->playback; |
| 3347 |
otherComponent = &stream->capture; |
| 3348 |
} |
| 3349 |
|
| 3350 |
/* ALSA docs say that negative delay should indicate xrun, but in my experience alsa_snd_pcm_delay returns -EPIPE */ |
| 3351 |
if( ( err = alsa_snd_pcm_delay( otherComponent->pcm, &delay ) ) < 0 ) |
| 3352 |
{ |
| 3353 |
if( err == -EPIPE ) |
| 3354 |
{ |
| 3355 |
/* Xrun */ |
| 3356 |
*continuePoll = 0; |
| 3357 |
goto error; |
| 3358 |
} |
| 3359 |
|
| 3360 |
ENSURE_( err, paUnanticipatedHostError ); |
| 3361 |
} |
| 3362 |
|
| 3363 |
if( StreamDirection_Out == streamDir ) |
| 3364 |
{ |
| 3365 |
/* Number of eligible frames before capture overrun */ |
| 3366 |
delay = otherComponent->alsaBufferSize - delay; |
| 3367 |
} |
| 3368 |
margin = delay - otherComponent->framesPerPeriod / 2; |
| 3369 |
|
| 3370 |
if( margin < 0 ) |
| 3371 |
{ |
| 3372 |
PA_DEBUG(( "%s: Stopping poll for %s\n", __FUNCTION__, StreamDirection_In == streamDir ? "capture" : "playback" )); |
| 3373 |
*continuePoll = 0; |
| 3374 |
} |
| 3375 |
else if( margin < otherComponent->framesPerPeriod ) |
| 3376 |
{ |
| 3377 |
*pollTimeout = CalculatePollTimeout( stream, margin ); |
| 3378 |
PA_DEBUG(( "%s: Trying to poll again for %s frames, pollTimeout: %d\n", |
| 3379 |
__FUNCTION__, StreamDirection_In == streamDir ? "capture" : "playback", *pollTimeout )); |
| 3380 |
} |
| 3381 |
|
| 3382 |
error: |
| 3383 |
return result; |
| 3384 |
} |
| 3385 |
|
| 3386 |
/* Callback interface */ |
| 3387 |
|
| 3388 |
static void OnExit( void *data ) |
| 3389 |
{ |
| 3390 |
PaAlsaStream *stream = (PaAlsaStream *) data; |
| 3391 |
|
| 3392 |
assert( data ); |
| 3393 |
|
| 3394 |
PaUtil_ResetCpuLoadMeasurer( &stream->cpuLoadMeasurer ); |
| 3395 |
|
| 3396 |
stream->callback_finished = 1; /* Let the outside world know stream was stopped in callback */ |
| 3397 |
PA_DEBUG(( "%s: Stopping ALSA handles\n", __FUNCTION__ )); |
| 3398 |
AlsaStop( stream, stream->callbackAbort ); |
| 3399 |
|
| 3400 |
PA_DEBUG(( "%s: Stoppage\n", __FUNCTION__ )); |
| 3401 |
|
| 3402 |
/* Eventually notify user all buffers have played */ |
| 3403 |
if( stream->streamRepresentation.streamFinishedCallback ) |
| 3404 |
{ |
| 3405 |
stream->streamRepresentation.streamFinishedCallback( stream->streamRepresentation.userData ); |
| 3406 |
} |
| 3407 |
stream->isActive = 0; |
| 3408 |
} |
| 3409 |
|
| 3410 |
static void CalculateTimeInfo( PaAlsaStream *stream, PaStreamCallbackTimeInfo *timeInfo ) |
| 3411 |
{ |
| 3412 |
snd_pcm_status_t *capture_status, *playback_status; |
| 3413 |
snd_timestamp_t capture_timestamp, playback_timestamp; |
| 3414 |
PaTime capture_time = 0., playback_time = 0.; |
| 3415 |
|
| 3416 |
alsa_snd_pcm_status_alloca( &capture_status ); |
| 3417 |
alsa_snd_pcm_status_alloca( &playback_status ); |
| 3418 |
|
| 3419 |
if( stream->capture.pcm ) |
| 3420 |
{ |
| 3421 |
snd_pcm_sframes_t capture_delay; |
| 3422 |
|
| 3423 |
alsa_snd_pcm_status( stream->capture.pcm, capture_status ); |
| 3424 |
alsa_snd_pcm_status_get_tstamp( capture_status, &capture_timestamp ); |
| 3425 |
|
| 3426 |
capture_time = capture_timestamp.tv_sec + |
| 3427 |
( (PaTime)capture_timestamp.tv_usec / 1000000.0 ); |
| 3428 |
timeInfo->currentTime = capture_time; |
| 3429 |
|
| 3430 |
capture_delay = alsa_snd_pcm_status_get_delay( capture_status ); |
| 3431 |
timeInfo->inputBufferAdcTime = timeInfo->currentTime - |
| 3432 |
(PaTime)capture_delay / stream->streamRepresentation.streamInfo.sampleRate; |
| 3433 |
} |
| 3434 |
if( stream->playback.pcm ) |
| 3435 |
{ |
| 3436 |
snd_pcm_sframes_t playback_delay; |
| 3437 |
|
| 3438 |
alsa_snd_pcm_status( stream->playback.pcm, playback_status ); |
| 3439 |
alsa_snd_pcm_status_get_tstamp( playback_status, &playback_timestamp ); |
| 3440 |
|
| 3441 |
playback_time = playback_timestamp.tv_sec + |
| 3442 |
((PaTime)playback_timestamp.tv_usec / 1000000.0); |
| 3443 |
|
| 3444 |
if( stream->capture.pcm ) /* Full duplex */ |
| 3445 |
{ |
| 3446 |
/* Hmm, we have both a playback and a capture timestamp. |
| 3447 |
* Hopefully they are the same... */ |
| 3448 |
if( fabs( capture_time - playback_time ) > 0.01 ) |
| 3449 |
PA_DEBUG(( "Capture time and playback time differ by %f\n", fabs( capture_time-playback_time ) )); |
| 3450 |
} |
| 3451 |
else |
| 3452 |
timeInfo->currentTime = playback_time; |
| 3453 |
|
| 3454 |
playback_delay = alsa_snd_pcm_status_get_delay( playback_status ); |
| 3455 |
timeInfo->outputBufferDacTime = timeInfo->currentTime + |
| 3456 |
(PaTime)playback_delay / stream->streamRepresentation.streamInfo.sampleRate; |
| 3457 |
} |
| 3458 |
} |
| 3459 |
|
| 3460 |
/** Called after buffer processing is finished. |
| 3461 |
* |
| 3462 |
* A number of mmapped frames is committed, it is possible that an xrun has occurred in the meantime. |
| 3463 |
* |
| 3464 |
* @param numFrames The number of frames that has been processed |
| 3465 |
* @param xrun Return whether an xrun has occurred |
| 3466 |
*/ |
| 3467 |
static PaError PaAlsaStreamComponent_EndProcessing( PaAlsaStreamComponent *self, unsigned long numFrames, int *xrun ) |
| 3468 |
{ |
| 3469 |
PaError result = paNoError; |
| 3470 |
int res = 0; |
| 3471 |
|
| 3472 |
/* @concern FullDuplex It is possible that only one direction is marked ready after polling, and processed |
| 3473 |
* afterwards |
| 3474 |
*/ |
| 3475 |
if( !self->ready ) |
| 3476 |
goto end; |
| 3477 |
|
| 3478 |
if( !self->canMmap && StreamDirection_Out == self->streamDir ) |
| 3479 |
{ |
| 3480 |
/* Play sound */ |
| 3481 |
if( self->hostInterleaved ) |
| 3482 |
res = alsa_snd_pcm_writei( self->pcm, self->nonMmapBuffer, numFrames ); |
| 3483 |
else |
| 3484 |
{ |
| 3485 |
void *bufs[self->numHostChannels]; |
| 3486 |
int bufsize = alsa_snd_pcm_format_size( self->nativeFormat, self->framesPerPeriod + 1 ); |
| 3487 |
unsigned char *buffer = self->nonMmapBuffer; |
| 3488 |
int i; |
| 3489 |
for( i = 0; i < self->numHostChannels; ++i ) |
| 3490 |
{ |
| 3491 |
bufs[i] = buffer; |
| 3492 |
buffer += bufsize; |
| 3493 |
} |
| 3494 |
res = alsa_snd_pcm_writen( self->pcm, bufs, numFrames ); |
| 3495 |
} |
| 3496 |
} |
| 3497 |
|
| 3498 |
if( self->canMmap ) |
| 3499 |
res = alsa_snd_pcm_mmap_commit( self->pcm, self->offset, numFrames ); |
| 3500 |
|
| 3501 |
if( res == -EPIPE || res == -ESTRPIPE ) |
| 3502 |
{ |
| 3503 |
*xrun = 1; |
| 3504 |
} |
| 3505 |
else |
| 3506 |
{ |
| 3507 |
ENSURE_( res, paUnanticipatedHostError ); |
| 3508 |
} |
| 3509 |
|
| 3510 |
end: |
| 3511 |
error: |
| 3512 |
return result; |
| 3513 |
} |
| 3514 |
|
| 3515 |
/* Extract buffer from channel area */ |
| 3516 |
static unsigned char *ExtractAddress( const snd_pcm_channel_area_t *area, snd_pcm_uframes_t offset ) |
| 3517 |
{ |
| 3518 |
return (unsigned char *) area->addr + ( area->first + offset * area->step ) / 8; |
| 3519 |
} |
| 3520 |
|
| 3521 |
/** Do necessary adaption between user and host channels. |
| 3522 |
* |
| 3523 |
@concern ChannelAdaption Adapting between user and host channels can involve silencing unused channels and |
| 3524 |
duplicating mono information if host outputs come in pairs. |
| 3525 |
*/ |
| 3526 |
static PaError PaAlsaStreamComponent_DoChannelAdaption( PaAlsaStreamComponent *self, PaUtilBufferProcessor *bp, int numFrames ) |
| 3527 |
{ |
| 3528 |
PaError result = paNoError; |
| 3529 |
unsigned char *p; |
| 3530 |
int i; |
| 3531 |
int unusedChans = self->numHostChannels - self->numUserChannels; |
| 3532 |
unsigned char *src, *dst; |
| 3533 |
int convertMono = ( self->numHostChannels % 2 ) == 0 && ( self->numUserChannels % 2 ) != 0; |
| 3534 |
|
| 3535 |
assert( StreamDirection_Out == self->streamDir ); |
| 3536 |
|
| 3537 |
if( self->hostInterleaved ) |
| 3538 |
{ |
| 3539 |
int swidth = alsa_snd_pcm_format_size( self->nativeFormat, 1 ); |
| 3540 |
unsigned char *buffer = self->canMmap ? ExtractAddress( self->channelAreas, self->offset ) : self->nonMmapBuffer; |
| 3541 |
|
| 3542 |
/* Start after the last user channel */ |
| 3543 |
p = buffer + self->numUserChannels * swidth; |
| 3544 |
|
| 3545 |
if( convertMono ) |
| 3546 |
{ |
| 3547 |
/* Convert the last user channel into stereo pair */ |
| 3548 |
src = buffer + ( self->numUserChannels - 1 ) * swidth; |
| 3549 |
for( i = 0; i < numFrames; ++i ) |
| 3550 |
{ |
| 3551 |
dst = src + swidth; |
| 3552 |
memcpy( dst, src, swidth ); |
| 3553 |
src += self->numHostChannels * swidth; |
| 3554 |
} |
| 3555 |
|
| 3556 |
/* Don't touch the channel we just wrote to */ |
| 3557 |
p += swidth; |
| 3558 |
--unusedChans; |
| 3559 |
} |
| 3560 |
|
| 3561 |
if( unusedChans > 0 ) |
| 3562 |
{ |
| 3563 |
/* Silence unused output channels */ |
| 3564 |
for( i = 0; i < numFrames; ++i ) |
| 3565 |
{ |
| 3566 |
memset( p, 0, swidth * unusedChans ); |
| 3567 |
p += self->numHostChannels * swidth; |
| 3568 |
} |
| 3569 |
} |
| 3570 |
} |
| 3571 |
else |
| 3572 |
{ |
| 3573 |
/* We extract the last user channel */ |
| 3574 |
if( convertMono ) |
| 3575 |
{ |
| 3576 |
ENSURE_( alsa_snd_pcm_area_copy( self->channelAreas + self->numUserChannels, self->offset, self->channelAreas + |
| 3577 |
( self->numUserChannels - 1 ), self->offset, numFrames, self->nativeFormat ), paUnanticipatedHostError ); |
| 3578 |
--unusedChans; |
| 3579 |
} |
| 3580 |
if( unusedChans > 0 ) |
| 3581 |
{ |
| 3582 |
alsa_snd_pcm_areas_silence( self->channelAreas + ( self->numHostChannels - unusedChans ), self->offset, unusedChans, numFrames, |
| 3583 |
self->nativeFormat ); |
| 3584 |
} |
| 3585 |
} |
| 3586 |
|
| 3587 |
error: |
| 3588 |
return result; |
| 3589 |
} |
| 3590 |
|
| 3591 |
static PaError PaAlsaStream_EndProcessing( PaAlsaStream *self, unsigned long numFrames, int *xrunOccurred ) |
| 3592 |
{ |
| 3593 |
PaError result = paNoError; |
| 3594 |
int xrun = 0; |
| 3595 |
|
| 3596 |
if( self->capture.pcm ) |
| 3597 |
{ |
| 3598 |
PA_ENSURE( PaAlsaStreamComponent_EndProcessing( &self->capture, numFrames, &xrun ) ); |
| 3599 |
} |
| 3600 |
if( self->playback.pcm ) |
| 3601 |
{ |
| 3602 |
if( self->playback.numHostChannels > self->playback.numUserChannels ) |
| 3603 |
{ |
| 3604 |
PA_ENSURE( PaAlsaStreamComponent_DoChannelAdaption( &self->playback, &self->bufferProcessor, numFrames ) ); |
| 3605 |
} |
| 3606 |
PA_ENSURE( PaAlsaStreamComponent_EndProcessing( &self->playback, numFrames, &xrun ) ); |
| 3607 |
} |
| 3608 |
|
| 3609 |
error: |
| 3610 |
*xrunOccurred = xrun; |
| 3611 |
return result; |
| 3612 |
} |
| 3613 |
|
| 3614 |
/** Update the number of available frames. |
| 3615 |
* |
| 3616 |
*/ |
| 3617 |
static PaError PaAlsaStreamComponent_GetAvailableFrames( PaAlsaStreamComponent *self, unsigned long *numFrames, int *xrunOccurred ) |
| 3618 |
{ |
| 3619 |
PaError result = paNoError; |
| 3620 |
snd_pcm_sframes_t framesAvail = alsa_snd_pcm_avail_update( self->pcm ); |
| 3621 |
*xrunOccurred = 0; |
| 3622 |
|
| 3623 |
if( -EPIPE == framesAvail ) |
| 3624 |
{ |
| 3625 |
*xrunOccurred = 1; |
| 3626 |
framesAvail = 0; |
| 3627 |
} |
| 3628 |
else |
| 3629 |
{ |
| 3630 |
ENSURE_( framesAvail, paUnanticipatedHostError ); |
| 3631 |
} |
| 3632 |
|
| 3633 |
*numFrames = framesAvail; |
| 3634 |
|
| 3635 |
error: |
| 3636 |
return result; |
| 3637 |
} |
| 3638 |
|
| 3639 |
/** Fill in pollfd objects. |
| 3640 |
*/ |
| 3641 |
static PaError PaAlsaStreamComponent_BeginPolling( PaAlsaStreamComponent* self, struct pollfd* pfds ) |
| 3642 |
{ |
| 3643 |
int nfds = alsa_snd_pcm_poll_descriptors( self->pcm, pfds, self->nfds ); |
| 3644 |
/* If alsa returns anything else, like -EPIPE return */ |
| 3645 |
if( nfds != self->nfds ) |
| 3646 |
{ |
| 3647 |
return paUnanticipatedHostError; |
| 3648 |
} |
| 3649 |
self->ready = 0; |
| 3650 |
|
| 3651 |
return paNoError; |
| 3652 |
} |
| 3653 |
|
| 3654 |
/** Examine results from poll(). |
| 3655 |
* |
| 3656 |
* @param pfds pollfds to inspect |
| 3657 |
* @param shouldPoll Should we continue to poll |
| 3658 |
* @param xrun Has an xrun occurred |
| 3659 |
*/ |
| 3660 |
static PaError PaAlsaStreamComponent_EndPolling( PaAlsaStreamComponent* self, struct pollfd* pfds, int* shouldPoll, int* xrun ) |
| 3661 |
{ |
| 3662 |
PaError result = paNoError; |
| 3663 |
unsigned short revents; |
| 3664 |
|
| 3665 |
ENSURE_( alsa_snd_pcm_poll_descriptors_revents( self->pcm, pfds, self->nfds, &revents ), paUnanticipatedHostError ); |
| 3666 |
if( revents != 0 ) |
| 3667 |
{ |
| 3668 |
if( revents & POLLERR ) |
| 3669 |
{ |
| 3670 |
*xrun = 1; |
| 3671 |
} |
| 3672 |
else if( revents & POLLHUP ) |
| 3673 |
{ |
| 3674 |
*xrun = 1; |
| 3675 |
PA_DEBUG(( "%s: revents has POLLHUP, processing as XRUN\n", __FUNCTION__ )); |
| 3676 |
} |
| 3677 |
else |
| 3678 |
self->ready = 1; |
| 3679 |
|
| 3680 |
*shouldPoll = 0; |
| 3681 |
} |
| 3682 |
else /* (A zero revent occurred) */ |
| 3683 |
/* Work around an issue with Alsa older than 1.0.16 using some plugins (eg default with plug + dmix) where |
| 3684 |
* POLLIN or POLLOUT are zeroed by Alsa-lib if _mmap_avail() is a few frames short of avail_min at period |
| 3685 |
* boundary, possibly due to erratic dma interrupts at period boundary? Treat as a valid event. |
| 3686 |
*/ |
| 3687 |
if( self->useReventFix ) |
| 3688 |
{ |
| 3689 |
self->ready = 1; |
| 3690 |
*shouldPoll = 0; |
| 3691 |
} |
| 3692 |
|
| 3693 |
error: |
| 3694 |
return result; |
| 3695 |
} |
| 3696 |
|
| 3697 |
/** Return the number of available frames for this stream. |
| 3698 |
* |
| 3699 |
* @concern FullDuplex The minimum available for the two directions is calculated, it might be desirable to ignore |
| 3700 |
* one direction however (not marked ready from poll), so this is controlled by queryCapture and queryPlayback. |
| 3701 |
* |
| 3702 |
* @param queryCapture Check available for capture |
| 3703 |
* @param queryPlayback Check available for playback |
| 3704 |
* @param available The returned number of frames |
| 3705 |
* @param xrunOccurred Return whether an xrun has occurred |
| 3706 |
*/ |
| 3707 |
static PaError PaAlsaStream_GetAvailableFrames( PaAlsaStream *self, int queryCapture, int queryPlayback, unsigned long |
| 3708 |
*available, int *xrunOccurred ) |
| 3709 |
{ |
| 3710 |
PaError result = paNoError; |
| 3711 |
unsigned long captureFrames, playbackFrames; |
| 3712 |
*xrunOccurred = 0; |
| 3713 |
|
| 3714 |
assert( queryCapture || queryPlayback ); |
| 3715 |
|
| 3716 |
if( queryCapture ) |
| 3717 |
{ |
| 3718 |
assert( self->capture.pcm ); |
| 3719 |
PA_ENSURE( PaAlsaStreamComponent_GetAvailableFrames( &self->capture, &captureFrames, xrunOccurred ) ); |
| 3720 |
if( *xrunOccurred ) |
| 3721 |
{ |
| 3722 |
goto end; |
| 3723 |
} |
| 3724 |
} |
| 3725 |
if( queryPlayback ) |
| 3726 |
{ |
| 3727 |
assert( self->playback.pcm ); |
| 3728 |
PA_ENSURE( PaAlsaStreamComponent_GetAvailableFrames( &self->playback, &playbackFrames, xrunOccurred ) ); |
| 3729 |
if( *xrunOccurred ) |
| 3730 |
{ |
| 3731 |
goto end; |
| 3732 |
} |
| 3733 |
} |
| 3734 |
|
| 3735 |
if( queryCapture && queryPlayback ) |
| 3736 |
{ |
| 3737 |
*available = PA_MIN( captureFrames, playbackFrames ); |
| 3738 |
/*PA_DEBUG(("capture: %lu, playback: %lu, combined: %lu\n", captureFrames, playbackFrames, *available));*/ |
| 3739 |
} |
| 3740 |
else if( queryCapture ) |
| 3741 |
{ |
| 3742 |
*available = captureFrames; |
| 3743 |
} |
| 3744 |
else |
| 3745 |
{ |
| 3746 |
*available = playbackFrames; |
| 3747 |
} |
| 3748 |
|
| 3749 |
end: |
| 3750 |
error: |
| 3751 |
return result; |
| 3752 |
} |
| 3753 |
|
| 3754 |
/** Wait for and report available buffer space from ALSA. |
| 3755 |
* |
| 3756 |
* Unless ALSA reports a minimum of frames available for I/O, we poll the ALSA filedescriptors for more. |
| 3757 |
* Both of these operations can uncover xrun conditions. |
| 3758 |
* |
| 3759 |
* @concern Xruns Both polling and querying available frames can report an xrun condition. |
| 3760 |
* |
| 3761 |
* @param framesAvail Return the number of available frames |
| 3762 |
* @param xrunOccurred Return whether an xrun has occurred |
| 3763 |
*/ |
| 3764 |
static PaError PaAlsaStream_WaitForFrames( PaAlsaStream *self, unsigned long *framesAvail, int *xrunOccurred ) |
| 3765 |
{ |
| 3766 |
PaError result = paNoError; |
| 3767 |
int pollPlayback = self->playback.pcm != NULL, pollCapture = self->capture.pcm != NULL; |
| 3768 |
int pollTimeout = self->pollTimeout; |
| 3769 |
int xrun = 0, timeouts = 0; |
| 3770 |
int pollResults; |
| 3771 |
|
| 3772 |
assert( self ); |
| 3773 |
assert( framesAvail ); |
| 3774 |
|
| 3775 |
if( !self->callbackMode ) |
| 3776 |
{ |
| 3777 |
/* In blocking mode we will only wait if necessary */ |
| 3778 |
PA_ENSURE( PaAlsaStream_GetAvailableFrames( self, self->capture.pcm != NULL, self->playback.pcm != NULL, |
| 3779 |
framesAvail, &xrun ) ); |
| 3780 |
if( xrun ) |
| 3781 |
{ |
| 3782 |
goto end; |
| 3783 |
} |
| 3784 |
|
| 3785 |
if( *framesAvail > 0 ) |
| 3786 |
{ |
| 3787 |
/* Mark pcms ready from poll */ |
| 3788 |
if( self->capture.pcm ) |
| 3789 |
self->capture.ready = 1; |
| 3790 |
if( self->playback.pcm ) |
| 3791 |
self->playback.ready = 1; |
| 3792 |
|
| 3793 |
goto end; |
| 3794 |
} |
| 3795 |
} |
| 3796 |
|
| 3797 |
while( pollPlayback || pollCapture ) |
| 3798 |
{ |
| 3799 |
int totalFds = 0; |
| 3800 |
struct pollfd *capturePfds = NULL, *playbackPfds = NULL; |
| 3801 |
|
| 3802 |
#ifdef PTHREAD_CANCELED |
| 3803 |
pthread_testcancel(); |
| 3804 |
#endif |
| 3805 |
if( pollCapture ) |
| 3806 |
{ |
| 3807 |
capturePfds = self->pfds; |
| 3808 |
PaError res = PaAlsaStreamComponent_BeginPolling( &self->capture, capturePfds ); |
| 3809 |
if( res != paNoError) |
| 3810 |
{ |
| 3811 |
xrun = 1; |
| 3812 |
goto end; |
| 3813 |
} |
| 3814 |
totalFds += self->capture.nfds; |
| 3815 |
} |
| 3816 |
if( pollPlayback ) |
| 3817 |
{ |
| 3818 |
/* self->pfds is in effect an array of fds; if necessary, index past the capture fds */ |
| 3819 |
playbackPfds = self->pfds + (pollCapture ? self->capture.nfds : 0); |
| 3820 |
PaError res = PaAlsaStreamComponent_BeginPolling( &self->playback, playbackPfds ); |
| 3821 |
if( res != paNoError) |
| 3822 |
{ |
| 3823 |
xrun = 1; |
| 3824 |
goto end; |
| 3825 |
} |
| 3826 |
totalFds += self->playback.nfds; |
| 3827 |
} |
| 3828 |
|
| 3829 |
#ifdef PTHREAD_CANCELED |
| 3830 |
if( self->callbackMode ) |
| 3831 |
{ |
| 3832 |
/* To allow 'Abort' to terminate the callback thread, enable cancelability just for poll() (& disable after) */ |
| 3833 |
pthread_setcancelstate( PTHREAD_CANCEL_ENABLE, NULL ); |
| 3834 |
} |
| 3835 |
#endif |
| 3836 |
|
| 3837 |
pollResults = poll( self->pfds, totalFds, pollTimeout ); |
| 3838 |
|
| 3839 |
#ifdef PTHREAD_CANCELED |
| 3840 |
if( self->callbackMode ) |
| 3841 |
{ |
| 3842 |
pthread_setcancelstate( PTHREAD_CANCEL_DISABLE, NULL ); |
| 3843 |
} |
| 3844 |
#endif |
| 3845 |
|
| 3846 |
if( pollResults < 0 ) |
| 3847 |
{ |
| 3848 |
/* XXX: Depend on preprocessor condition? */ |
| 3849 |
if( errno == EINTR ) |
| 3850 |
{ |
| 3851 |
/* gdb */ |
| 3852 |
Pa_Sleep( 1 ); /* avoid hot loop */ |
| 3853 |
continue; |
| 3854 |
} |
| 3855 |
|
| 3856 |
/* TODO: Add macro for checking system calls */ |
| 3857 |
PA_ENSURE( paInternalError ); |
| 3858 |
} |
| 3859 |
else if( pollResults == 0 ) |
| 3860 |
{ |
| 3861 |
/* Suspended, paused or failed device can provide 0 poll results. To avoid deadloop in such situation |
| 3862 |
* we simply run counter 'timeouts' which detects 0 poll result and accumulates. As soon as 2048 timouts (around 2 seconds) |
| 3863 |
* are achieved we simply fail function with paTimedOut to notify waiting methods that device is not capable |
| 3864 |
* of providing audio data anymore and needs some corresponding recovery action. |
| 3865 |
* Note that 'timeouts' is reset to 0 if poll() managed to return non 0 results. |
| 3866 |
*/ |
| 3867 |
|
| 3868 |
/*PA_DEBUG(( "%s: poll == 0 results, timed out, %d times left\n", __FUNCTION__, 2048 - timeouts ));*/ |
| 3869 |
++ timeouts; |
| 3870 |
if( timeouts > 1 ) /* sometimes device times out, but normally once, so we do not sleep any time */ |
| 3871 |
{ |
| 3872 |
Pa_Sleep( 1 ); /* avoid hot loop */ |
| 3873 |
} |
| 3874 |
/* not else ! */ |
| 3875 |
if( timeouts >= 2048 ) /* audio device not working, shall return error to notify waiters */ |
| 3876 |
{ |
| 3877 |
*framesAvail = 0; /* no frames available for processing */ |
| 3878 |
xrun = 1; /* try recovering device */ |
| 3879 |
|
| 3880 |
PA_DEBUG(( "%s: poll timed out\n", __FUNCTION__, timeouts )); |
| 3881 |
goto end;/*PA_ENSURE( paTimedOut );*/ |
| 3882 |
} |
| 3883 |
} |
| 3884 |
else if( pollResults > 0 ) |
| 3885 |
{ |
| 3886 |
/* reset timouts counter */ |
| 3887 |
timeouts = 0; |
| 3888 |
|
| 3889 |
/* check the return status of our pfds */ |
| 3890 |
if( pollCapture ) |
| 3891 |
{ |
| 3892 |
PA_ENSURE( PaAlsaStreamComponent_EndPolling( &self->capture, capturePfds, &pollCapture, &xrun ) ); |
| 3893 |
} |
| 3894 |
if( pollPlayback ) |
| 3895 |
{ |
| 3896 |
PA_ENSURE( PaAlsaStreamComponent_EndPolling( &self->playback, playbackPfds, &pollPlayback, &xrun ) ); |
| 3897 |
} |
| 3898 |
if( xrun ) |
| 3899 |
{ |
| 3900 |
break; |
| 3901 |
} |
| 3902 |
} |
| 3903 |
|
| 3904 |
/* @concern FullDuplex If only one of two pcms is ready we may want to compromise between the two. |
| 3905 |
* If there is less than half a period's worth of samples left of frames in the other pcm's buffer we will |
| 3906 |
* stop polling. |
| 3907 |
*/ |
| 3908 |
if( self->capture.pcm && self->playback.pcm ) |
| 3909 |
{ |
| 3910 |
if( pollCapture && !pollPlayback ) |
| 3911 |
{ |
| 3912 |
PA_ENSURE( ContinuePoll( self, StreamDirection_In, &pollTimeout, &pollCapture ) ); |
| 3913 |
} |
| 3914 |
else if( pollPlayback && !pollCapture ) |
| 3915 |
{ |
| 3916 |
PA_ENSURE( ContinuePoll( self, StreamDirection_Out, &pollTimeout, &pollPlayback ) ); |
| 3917 |
} |
| 3918 |
} |
| 3919 |
} |
| 3920 |
|
| 3921 |
if( !xrun ) |
| 3922 |
{ |
| 3923 |
/* Get the number of available frames for the pcms that are marked ready. |
| 3924 |
* @concern FullDuplex If only one direction is marked ready (from poll), the number of frames available for |
| 3925 |
* the other direction is returned. Output is normally preferred over capture however, so capture frames may be |
| 3926 |
* discarded to avoid overrun unless paNeverDropInput is specified. |
| 3927 |
*/ |
| 3928 |
int captureReady = self->capture.pcm ? self->capture.ready : 0, |
| 3929 |
playbackReady = self->playback.pcm ? self->playback.ready : 0; |
| 3930 |
PA_ENSURE( PaAlsaStream_GetAvailableFrames( self, captureReady, playbackReady, framesAvail, &xrun ) ); |
| 3931 |
|
| 3932 |
if( self->capture.pcm && self->playback.pcm ) |
| 3933 |
{ |
| 3934 |
if( !self->playback.ready && !self->neverDropInput ) |
| 3935 |
{ |
| 3936 |
/* Drop input, a period's worth */ |
| 3937 |
assert( self->capture.ready ); |
| 3938 |
PaAlsaStreamComponent_EndProcessing( &self->capture, PA_MIN( self->capture.framesPerPeriod, |
| 3939 |
*framesAvail ), &xrun ); |
| 3940 |
*framesAvail = 0; |
| 3941 |
self->capture.ready = 0; |
| 3942 |
} |
| 3943 |
} |
| 3944 |
else if( self->capture.pcm ) |
| 3945 |
assert( self->capture.ready ); |
| 3946 |
else |
| 3947 |
assert( self->playback.ready ); |
| 3948 |
} |
| 3949 |
|
| 3950 |
end: |
| 3951 |
error: |
| 3952 |
if( xrun ) |
| 3953 |
{ |
| 3954 |
/* Recover from the xrun state */ |
| 3955 |
PA_ENSURE( PaAlsaStream_HandleXrun( self ) ); |
| 3956 |
*framesAvail = 0; |
| 3957 |
} |
| 3958 |
else |
| 3959 |
{ |
| 3960 |
if( 0 != *framesAvail ) |
| 3961 |
{ |
| 3962 |
/* If we're reporting frames eligible for processing, one of the handles better be ready */ |
| 3963 |
PA_UNLESS( self->capture.ready || self->playback.ready, paInternalError ); |
| 3964 |
} |
| 3965 |
} |
| 3966 |
*xrunOccurred = xrun; |
| 3967 |
|
| 3968 |
return result; |
| 3969 |
} |
| 3970 |
|
| 3971 |
/** Register per-channel ALSA buffer information with buffer processor. |
| 3972 |
* |
| 3973 |
* Mmapped buffer space is acquired from ALSA, and registered with the buffer processor. Differences between the |
| 3974 |
* number of host and user channels is taken into account. |
| 3975 |
* |
| 3976 |
* @param numFrames On entrance the number of requested frames, on exit the number of contiguously accessible frames. |
| 3977 |
*/ |
| 3978 |
static PaError PaAlsaStreamComponent_RegisterChannels( PaAlsaStreamComponent* self, PaUtilBufferProcessor* bp, |
| 3979 |
unsigned long* numFrames, int* xrun ) |
| 3980 |
{ |
| 3981 |
PaError result = paNoError; |
| 3982 |
const snd_pcm_channel_area_t *areas, *area; |
| 3983 |
void (*setChannel)(PaUtilBufferProcessor *, unsigned int, void *, unsigned int) = |
| 3984 |
StreamDirection_In == self->streamDir ? PaUtil_SetInputChannel : PaUtil_SetOutputChannel; |
| 3985 |
unsigned char *buffer, *p; |
| 3986 |
int i; |
| 3987 |
unsigned long framesAvail; |
| 3988 |
|
| 3989 |
/* This _must_ be called before mmap_begin */ |
| 3990 |
PA_ENSURE( PaAlsaStreamComponent_GetAvailableFrames( self, &framesAvail, xrun ) ); |
| 3991 |
if( *xrun ) |
| 3992 |
{ |
| 3993 |
*numFrames = 0; |
| 3994 |
goto end; |
| 3995 |
} |
| 3996 |
|
| 3997 |
if( self->canMmap ) |
| 3998 |
{ |
| 3999 |
ENSURE_( alsa_snd_pcm_mmap_begin( self->pcm, &areas, &self->offset, numFrames ), paUnanticipatedHostError ); |
| 4000 |
/* @concern ChannelAdaption Buffer address is recorded so we can do some channel adaption later */ |
| 4001 |
self->channelAreas = (snd_pcm_channel_area_t *)areas; |
| 4002 |
} |
| 4003 |
else |
| 4004 |
{ |
| 4005 |
unsigned int bufferSize = self->numHostChannels * alsa_snd_pcm_format_size( self->nativeFormat, *numFrames ); |
| 4006 |
if( bufferSize > self->nonMmapBufferSize ) |
| 4007 |
{ |
| 4008 |
self->nonMmapBuffer = realloc( self->nonMmapBuffer, ( self->nonMmapBufferSize = bufferSize ) ); |
| 4009 |
if( !self->nonMmapBuffer ) |
| 4010 |
{ |
| 4011 |
result = paInsufficientMemory; |
| 4012 |
goto error; |
| 4013 |
} |
| 4014 |
} |
| 4015 |
} |
| 4016 |
|
| 4017 |
if( self->hostInterleaved ) |
| 4018 |
{ |
| 4019 |
int swidth = alsa_snd_pcm_format_size( self->nativeFormat, 1 ); |
| 4020 |
|
| 4021 |
p = buffer = self->canMmap ? ExtractAddress( areas, self->offset ) : self->nonMmapBuffer; |
| 4022 |
for( i = 0; i < self->numUserChannels; ++i ) |
| 4023 |
{ |
| 4024 |
/* We're setting the channels up to userChannels, but the stride will be hostChannels samples */ |
| 4025 |
setChannel( bp, i, p, self->numHostChannels ); |
| 4026 |
p += swidth; |
| 4027 |
} |
| 4028 |
} |
| 4029 |
else |
| 4030 |
{ |
| 4031 |
if( self->canMmap ) |
| 4032 |
{ |
| 4033 |
for( i = 0; i < self->numUserChannels; ++i ) |
| 4034 |
{ |
| 4035 |
area = areas + i; |
| 4036 |
buffer = ExtractAddress( area, self->offset ); |
| 4037 |
setChannel( bp, i, buffer, 1 ); |
| 4038 |
} |
| 4039 |
} |
| 4040 |
else |
| 4041 |
{ |
| 4042 |
unsigned int buf_per_ch_size = self->nonMmapBufferSize / self->numHostChannels; |
| 4043 |
buffer = self->nonMmapBuffer; |
| 4044 |
for( i = 0; i < self->numUserChannels; ++i ) |
| 4045 |
{ |
| 4046 |
setChannel( bp, i, buffer, 1 ); |
| 4047 |
buffer += buf_per_ch_size; |
| 4048 |
} |
| 4049 |
} |
| 4050 |
} |
| 4051 |
|
| 4052 |
if( !self->canMmap && StreamDirection_In == self->streamDir ) |
| 4053 |
{ |
| 4054 |
/* Read sound */ |
| 4055 |
int res; |
| 4056 |
if( self->hostInterleaved ) |
| 4057 |
res = alsa_snd_pcm_readi( self->pcm, self->nonMmapBuffer, *numFrames ); |
| 4058 |
else |
| 4059 |
{ |
| 4060 |
void *bufs[self->numHostChannels]; |
| 4061 |
unsigned int buf_per_ch_size = self->nonMmapBufferSize / self->numHostChannels; |
| 4062 |
unsigned char *buffer = self->nonMmapBuffer; |
| 4063 |
int i; |
| 4064 |
for( i = 0; i < self->numHostChannels; ++i ) |
| 4065 |
{ |
| 4066 |
bufs[i] = buffer; |
| 4067 |
buffer += buf_per_ch_size; |
| 4068 |
} |
| 4069 |
res = alsa_snd_pcm_readn( self->pcm, bufs, *numFrames ); |
| 4070 |
} |
| 4071 |
if( res == -EPIPE || res == -ESTRPIPE ) |
| 4072 |
{ |
| 4073 |
*xrun = 1; |
| 4074 |
*numFrames = 0; |
| 4075 |
} |
| 4076 |
} |
| 4077 |
|
| 4078 |
end: |
| 4079 |
error: |
| 4080 |
return result; |
| 4081 |
} |
| 4082 |
|
| 4083 |
/** Initiate buffer processing. |
| 4084 |
* |
| 4085 |
* ALSA buffers are registered with the PA buffer processor and the buffer size (in frames) set. |
| 4086 |
* |
| 4087 |
* @concern FullDuplex If both directions are being processed, the minimum amount of frames for the two directions is |
| 4088 |
* calculated. |
| 4089 |
* |
| 4090 |
* @param numFrames On entrance the number of available frames, on exit the number of received frames |
| 4091 |
* @param xrunOccurred Return whether an xrun has occurred |
| 4092 |
*/ |
| 4093 |
static PaError PaAlsaStream_SetUpBuffers( PaAlsaStream* self, unsigned long* numFrames, int* xrunOccurred ) |
| 4094 |
{ |
| 4095 |
PaError result = paNoError; |
| 4096 |
unsigned long captureFrames = ULONG_MAX, playbackFrames = ULONG_MAX, commonFrames = 0; |
| 4097 |
int xrun = 0; |
| 4098 |
|
| 4099 |
if( *xrunOccurred ) |
| 4100 |
{ |
| 4101 |
*numFrames = 0; |
| 4102 |
return result; |
| 4103 |
} |
| 4104 |
/* If we got here at least one of the pcm's should be marked ready */ |
| 4105 |
PA_UNLESS( self->capture.ready || self->playback.ready, paInternalError ); |
| 4106 |
|
| 4107 |
/* Extract per-channel ALSA buffer pointers and register them with the buffer processor. |
| 4108 |
* It is possible that a direction is not marked ready however, because it is out of sync with the other. |
| 4109 |
*/ |
| 4110 |
if( self->capture.pcm && self->capture.ready ) |
| 4111 |
{ |
| 4112 |
captureFrames = *numFrames; |
| 4113 |
PA_ENSURE( PaAlsaStreamComponent_RegisterChannels( &self->capture, &self->bufferProcessor, &captureFrames, |
| 4114 |
&xrun ) ); |
| 4115 |
} |
| 4116 |
if( self->playback.pcm && self->playback.ready ) |
| 4117 |
{ |
| 4118 |
playbackFrames = *numFrames; |
| 4119 |
PA_ENSURE( PaAlsaStreamComponent_RegisterChannels( &self->playback, &self->bufferProcessor, &playbackFrames, |
| 4120 |
&xrun ) ); |
| 4121 |
} |
| 4122 |
if( xrun ) |
| 4123 |
{ |
| 4124 |
/* Nothing more to do */ |
| 4125 |
assert( 0 == commonFrames ); |
| 4126 |
goto end; |
| 4127 |
} |
| 4128 |
|
| 4129 |
commonFrames = PA_MIN( captureFrames, playbackFrames ); |
| 4130 |
/* assert( commonFrames <= *numFrames ); */ |
| 4131 |
if( commonFrames > *numFrames ) |
| 4132 |
{ |
| 4133 |
/* Hmmm ... how come there are more frames available than we requested!? Blah. */ |
| 4134 |
PA_DEBUG(( "%s: Common available frames are reported to be more than number requested: %lu, %lu, callbackMode: %d\n", __FUNCTION__, |
| 4135 |
commonFrames, *numFrames, self->callbackMode )); |
| 4136 |
if( self->capture.pcm ) |
| 4137 |
{ |
| 4138 |
PA_DEBUG(( "%s: captureFrames: %lu, capture.ready: %d\n", __FUNCTION__, captureFrames, self->capture.ready )); |
| 4139 |
} |
| 4140 |
if( self->playback.pcm ) |
| 4141 |
{ |
| 4142 |
PA_DEBUG(( "%s: playbackFrames: %lu, playback.ready: %d\n", __FUNCTION__, playbackFrames, self->playback.ready )); |
| 4143 |
} |
| 4144 |
|
| 4145 |
commonFrames = 0; |
| 4146 |
goto end; |
| 4147 |
} |
| 4148 |
|
| 4149 |
/* Inform PortAudio of the number of frames we got. |
| 4150 |
* @concern FullDuplex We might be experiencing underflow in either end; if its an input underflow, we go on |
| 4151 |
* with output. If its output underflow however, depending on the paNeverDropInput flag, we may want to simply |
| 4152 |
* discard the excess input or call the callback with paOutputOverflow flagged. |
| 4153 |
*/ |
| 4154 |
if( self->capture.pcm ) |
| 4155 |
{ |
| 4156 |
if( self->capture.ready ) |
| 4157 |
{ |
| 4158 |
PaUtil_SetInputFrameCount( &self->bufferProcessor, commonFrames ); |
| 4159 |
} |
| 4160 |
else |
| 4161 |
{ |
| 4162 |
/* We have input underflow */ |
| 4163 |
PaUtil_SetNoInput( &self->bufferProcessor ); |
| 4164 |
} |
| 4165 |
} |
| 4166 |
if( self->playback.pcm ) |
| 4167 |
{ |
| 4168 |
if( self->playback.ready ) |
| 4169 |
{ |
| 4170 |
PaUtil_SetOutputFrameCount( &self->bufferProcessor, commonFrames ); |
| 4171 |
} |
| 4172 |
else |
| 4173 |
{ |
| 4174 |
/* We have output underflow, but keeping input data (paNeverDropInput) */ |
| 4175 |
assert( self->neverDropInput ); |
| 4176 |
assert( self->capture.pcm != NULL ); |
| 4177 |
PA_DEBUG(( "%s: Setting output buffers to NULL\n", __FUNCTION__ )); |
| 4178 |
PaUtil_SetNoOutput( &self->bufferProcessor ); |
| 4179 |
} |
| 4180 |
} |
| 4181 |
|
| 4182 |
end: |
| 4183 |
*numFrames = commonFrames; |
| 4184 |
error: |
| 4185 |
if( xrun ) |
| 4186 |
{ |
| 4187 |
PA_ENSURE( PaAlsaStream_HandleXrun( self ) ); |
| 4188 |
*numFrames = 0; |
| 4189 |
} |
| 4190 |
*xrunOccurred = xrun; |
| 4191 |
|
| 4192 |
return result; |
| 4193 |
} |
| 4194 |
|
| 4195 |
/** Callback thread's function. |
| 4196 |
* |
| 4197 |
* Roughly, the workflow can be described in the following way: The number of available frames that can be processed |
| 4198 |
* directly is obtained from ALSA, we then request as much directly accessible memory as possible within this amount |
| 4199 |
* from ALSA. The buffer memory is registered with the PA buffer processor and processing is carried out with |
| 4200 |
* PaUtil_EndBufferProcessing. Finally, the number of processed frames is reported to ALSA. The processing can |
| 4201 |
* happen in several iterations until we have consumed the known number of available frames (or an xrun is detected). |
| 4202 |
*/ |
| 4203 |
static void *CallbackThreadFunc( void *userData ) |
| 4204 |
{ |
| 4205 |
PaError result = paNoError; |
| 4206 |
PaAlsaStream *stream = (PaAlsaStream*) userData; |
| 4207 |
PaStreamCallbackTimeInfo timeInfo = {0, 0, 0}; |
| 4208 |
snd_pcm_sframes_t startThreshold = 0; |
| 4209 |
int callbackResult = paContinue; |
| 4210 |
PaStreamCallbackFlags cbFlags = 0; /* We might want to keep state across iterations */ |
| 4211 |
int streamStarted = 0; |
| 4212 |
|
| 4213 |
assert( stream ); |
| 4214 |
/* Not implemented */ |
| 4215 |
assert( !stream->primeBuffers ); |
| 4216 |
|
| 4217 |
/* Execute OnExit when exiting */ |
| 4218 |
pthread_cleanup_push( &OnExit, stream ); |
| 4219 |
#ifdef PTHREAD_CANCELED |
| 4220 |
/* 'Abort' will use thread cancellation to terminate the callback thread, but the Alsa-lib functions |
| 4221 |
* are NOT cancel-safe, (and can end up in an inconsistent state). So, disable cancelability for |
| 4222 |
* the thread here, and just re-enable it for the poll() in PaAlsaStream_WaitForFrames(). */ |
| 4223 |
pthread_testcancel(); |
| 4224 |
pthread_setcancelstate( PTHREAD_CANCEL_DISABLE, NULL ); |
| 4225 |
#endif |
| 4226 |
|
| 4227 |
/* @concern StreamStart If the output is being primed the output pcm needs to be prepared, otherwise the |
| 4228 |
* stream is started immediately. The latter involves signaling the waiting main thread. |
| 4229 |
*/ |
| 4230 |
if( stream->primeBuffers ) |
| 4231 |
{ |
| 4232 |
snd_pcm_sframes_t avail; |
| 4233 |
|
| 4234 |
if( stream->playback.pcm ) |
| 4235 |
ENSURE_( alsa_snd_pcm_prepare( stream->playback.pcm ), paUnanticipatedHostError ); |
| 4236 |
if( stream->capture.pcm && !stream->pcmsSynced ) |
| 4237 |
ENSURE_( alsa_snd_pcm_prepare( stream->capture.pcm ), paUnanticipatedHostError ); |
| 4238 |
|
| 4239 |
/* We can't be certain that the whole ring buffer is available for priming, but there should be |
| 4240 |
* at least one period */ |
| 4241 |
avail = alsa_snd_pcm_avail_update( stream->playback.pcm ); |
| 4242 |
startThreshold = avail - (avail % stream->playback.framesPerPeriod); |
| 4243 |
assert( startThreshold >= stream->playback.framesPerPeriod ); |
| 4244 |
} |
| 4245 |
else |
| 4246 |
{ |
| 4247 |
PA_ENSURE( PaUnixThread_PrepareNotify( &stream->thread ) ); |
| 4248 |
/* Buffer will be zeroed */ |
| 4249 |
PA_ENSURE( AlsaStart( stream, 0 ) ); |
| 4250 |
PA_ENSURE( PaUnixThread_NotifyParent( &stream->thread ) ); |
| 4251 |
|
| 4252 |
streamStarted = 1; |
| 4253 |
} |
| 4254 |
|
| 4255 |
while( 1 ) |
| 4256 |
{ |
| 4257 |
unsigned long framesAvail, framesGot; |
| 4258 |
int xrun = 0; |
| 4259 |
|
| 4260 |
#ifdef PTHREAD_CANCELED |
| 4261 |
pthread_testcancel(); |
| 4262 |
#endif |
| 4263 |
|
| 4264 |
/* @concern StreamStop if the main thread has requested a stop and the stream has not been effectively |
| 4265 |
* stopped we signal this condition by modifying callbackResult (we'll want to flush buffered output). |
| 4266 |
*/ |
| 4267 |
if( PaUnixThread_StopRequested( &stream->thread ) && paContinue == callbackResult ) |
| 4268 |
{ |
| 4269 |
PA_DEBUG(( "Setting callbackResult to paComplete\n" )); |
| 4270 |
callbackResult = paComplete; |
| 4271 |
} |
| 4272 |
|
| 4273 |
if( paContinue != callbackResult ) |
| 4274 |
{ |
| 4275 |
stream->callbackAbort = ( paAbort == callbackResult ); |
| 4276 |
if( stream->callbackAbort || |
| 4277 |
/** @concern BlockAdaption: Go on if adaption buffers are empty */ |
| 4278 |
PaUtil_IsBufferProcessorOutputEmpty( &stream->bufferProcessor ) ) |
| 4279 |
{ |
| 4280 |
goto end; |
| 4281 |
} |
| 4282 |
|
| 4283 |
PA_DEBUG(( "%s: Flushing buffer processor\n", __FUNCTION__ )); |
| 4284 |
/* There is still buffered output that needs to be processed */ |
| 4285 |
} |
| 4286 |
|
| 4287 |
/* Wait for data to become available, this comes down to polling the ALSA file descriptors until we have |
| 4288 |
* a number of available frames. |
| 4289 |
*/ |
| 4290 |
PA_ENSURE( PaAlsaStream_WaitForFrames( stream, &framesAvail, &xrun ) ); |
| 4291 |
if( xrun ) |
| 4292 |
{ |
| 4293 |
assert( 0 == framesAvail ); |
| 4294 |
continue; |
| 4295 |
|
| 4296 |
/* XXX: Report xruns to the user? A situation is conceivable where the callback is never invoked due |
| 4297 |
* to constant xruns, it might be desirable to notify the user of this. |
| 4298 |
*/ |
| 4299 |
} |
| 4300 |
|
| 4301 |
/* Consume buffer space. Once we have a number of frames available for consumption we must retrieve the |
| 4302 |
* mmapped buffers from ALSA, this is contiguously accessible memory however, so we may receive smaller |
| 4303 |
* portions at a time than is available as a whole. Therefore we should be prepared to process several |
| 4304 |
* chunks successively. The buffers are passed to the PA buffer processor. |
| 4305 |
*/ |
| 4306 |
while( framesAvail > 0 ) |
| 4307 |
{ |
| 4308 |
xrun = 0; |
| 4309 |
|
| 4310 |
/** @concern Xruns Under/overflows are to be reported to the callback */ |
| 4311 |
if( stream->underrun > 0.0 ) |
| 4312 |
{ |
| 4313 |
cbFlags |= paOutputUnderflow; |
| 4314 |
stream->underrun = 0.0; |
| 4315 |
} |
| 4316 |
if( stream->overrun > 0.0 ) |
| 4317 |
{ |
| 4318 |
cbFlags |= paInputOverflow; |
| 4319 |
stream->overrun = 0.0; |
| 4320 |
} |
| 4321 |
if( stream->capture.pcm && stream->playback.pcm ) |
| 4322 |
{ |
| 4323 |
/** @concern FullDuplex It's possible that only one direction is being processed to avoid an |
| 4324 |
* under- or overflow, this should be reported correspondingly */ |
| 4325 |
if( !stream->capture.ready ) |
| 4326 |
{ |
| 4327 |
cbFlags |= paInputUnderflow; |
| 4328 |
PA_DEBUG(( "%s: Input underflow\n", __FUNCTION__ )); |
| 4329 |
} |
| 4330 |
else if( !stream->playback.ready ) |
| 4331 |
{ |
| 4332 |
cbFlags |= paOutputOverflow; |
| 4333 |
PA_DEBUG(( "%s: Output overflow\n", __FUNCTION__ )); |
| 4334 |
} |
| 4335 |
} |
| 4336 |
|
| 4337 |
#if 0 |
| 4338 |
CallbackUpdate( &stream->threading ); |
| 4339 |
#endif |
| 4340 |
|
| 4341 |
CalculateTimeInfo( stream, &timeInfo ); |
| 4342 |
PaUtil_BeginBufferProcessing( &stream->bufferProcessor, &timeInfo, cbFlags ); |
| 4343 |
cbFlags = 0; |
| 4344 |
|
| 4345 |
/* CPU load measurement should include processing activity external to the stream callback */ |
| 4346 |
PaUtil_BeginCpuLoadMeasurement( &stream->cpuLoadMeasurer ); |
| 4347 |
|
| 4348 |
framesGot = framesAvail; |
| 4349 |
if( paUtilFixedHostBufferSize == stream->bufferProcessor.hostBufferSizeMode ) |
| 4350 |
{ |
| 4351 |
/* We've committed to a fixed host buffer size, stick to that */ |
| 4352 |
framesGot = framesGot >= stream->maxFramesPerHostBuffer ? stream->maxFramesPerHostBuffer : 0; |
| 4353 |
} |
| 4354 |
else |
| 4355 |
{ |
| 4356 |
/* We've committed to an upper bound on the size of host buffers */ |
| 4357 |
assert( paUtilBoundedHostBufferSize == stream->bufferProcessor.hostBufferSizeMode ); |
| 4358 |
framesGot = PA_MIN( framesGot, stream->maxFramesPerHostBuffer ); |
| 4359 |
} |
| 4360 |
PA_ENSURE( PaAlsaStream_SetUpBuffers( stream, &framesGot, &xrun ) ); |
| 4361 |
/* Check the host buffer size against the buffer processor configuration */ |
| 4362 |
framesAvail -= framesGot; |
| 4363 |
|
| 4364 |
if( framesGot > 0 ) |
| 4365 |
{ |
| 4366 |
assert( !xrun ); |
| 4367 |
PaUtil_EndBufferProcessing( &stream->bufferProcessor, &callbackResult ); |
| 4368 |
PA_ENSURE( PaAlsaStream_EndProcessing( stream, framesGot, &xrun ) ); |
| 4369 |
} |
| 4370 |
PaUtil_EndCpuLoadMeasurement( &stream->cpuLoadMeasurer, framesGot ); |
| 4371 |
|
| 4372 |
if( 0 == framesGot ) |
| 4373 |
{ |
| 4374 |
/* Go back to polling for more frames */ |
| 4375 |
break; |
| 4376 |
} |
| 4377 |
|
| 4378 |
if( paContinue != callbackResult ) |
| 4379 |
break; |
| 4380 |
} |
| 4381 |
} |
| 4382 |
|
| 4383 |
end: |
| 4384 |
; /* Hack to fix "label at end of compound statement" error caused by pthread_cleanup_pop(1) macro. */ |
| 4385 |
/* Match pthread_cleanup_push */ |
| 4386 |
pthread_cleanup_pop( 1 ); |
| 4387 |
|
| 4388 |
PA_DEBUG(( "%s: Thread %d exiting\n ", __FUNCTION__, pthread_self() )); |
| 4389 |
PaUnixThreading_EXIT( result ); |
| 4390 |
|
| 4391 |
error: |
| 4392 |
PA_DEBUG(( "%s: Thread %d is canceled due to error %d\n ", __FUNCTION__, pthread_self(), result )); |
| 4393 |
goto end; |
| 4394 |
} |
| 4395 |
|
| 4396 |
/* Blocking interface */ |
| 4397 |
|
| 4398 |
static PaError ReadStream( PaStream* s, void *buffer, unsigned long frames ) |
| 4399 |
{ |
| 4400 |
PaError result = paNoError; |
| 4401 |
PaAlsaStream *stream = (PaAlsaStream*)s; |
| 4402 |
unsigned long framesGot, framesAvail; |
| 4403 |
void *userBuffer; |
| 4404 |
snd_pcm_t *save = stream->playback.pcm; |
| 4405 |
|
| 4406 |
assert( stream ); |
| 4407 |
|
| 4408 |
PA_UNLESS( stream->capture.pcm, paCanNotReadFromAnOutputOnlyStream ); |
| 4409 |
|
| 4410 |
/* Disregard playback */ |
| 4411 |
stream->playback.pcm = NULL; |
| 4412 |
|
| 4413 |
if( stream->overrun > 0. ) |
| 4414 |
{ |
| 4415 |
result = paInputOverflowed; |
| 4416 |
stream->overrun = 0.0; |
| 4417 |
} |
| 4418 |
|
| 4419 |
if( stream->capture.userInterleaved ) |
| 4420 |
{ |
| 4421 |
userBuffer = buffer; |
| 4422 |
} |
| 4423 |
else |
| 4424 |
{ |
| 4425 |
/* Copy channels into local array */ |
| 4426 |
userBuffer = stream->capture.userBuffers; |
| 4427 |
memcpy( userBuffer, buffer, sizeof (void *) * stream->capture.numUserChannels ); |
| 4428 |
} |
| 4429 |
|
| 4430 |
/* Start stream if in prepared state */ |
| 4431 |
if( alsa_snd_pcm_state( stream->capture.pcm ) == SND_PCM_STATE_PREPARED ) |
| 4432 |
{ |
| 4433 |
ENSURE_( alsa_snd_pcm_start( stream->capture.pcm ), paUnanticipatedHostError ); |
| 4434 |
} |
| 4435 |
|
| 4436 |
while( frames > 0 ) |
| 4437 |
{ |
| 4438 |
int xrun = 0; |
| 4439 |
PA_ENSURE( PaAlsaStream_WaitForFrames( stream, &framesAvail, &xrun ) ); |
| 4440 |
framesGot = PA_MIN( framesAvail, frames ); |
| 4441 |
|
| 4442 |
PA_ENSURE( PaAlsaStream_SetUpBuffers( stream, &framesGot, &xrun ) ); |
| 4443 |
if( framesGot > 0 ) |
| 4444 |
{ |
| 4445 |
framesGot = PaUtil_CopyInput( &stream->bufferProcessor, &userBuffer, framesGot ); |
| 4446 |
PA_ENSURE( PaAlsaStream_EndProcessing( stream, framesGot, &xrun ) ); |
| 4447 |
frames -= framesGot; |
| 4448 |
} |
| 4449 |
} |
| 4450 |
|
| 4451 |
end: |
| 4452 |
stream->playback.pcm = save; |
| 4453 |
return result; |
| 4454 |
error: |
| 4455 |
goto end; |
| 4456 |
} |
| 4457 |
|
| 4458 |
static PaError WriteStream( PaStream* s, const void *buffer, unsigned long frames ) |
| 4459 |
{ |
| 4460 |
PaError result = paNoError; |
| 4461 |
signed long err; |
| 4462 |
PaAlsaStream *stream = (PaAlsaStream*)s; |
| 4463 |
snd_pcm_uframes_t framesGot, framesAvail; |
| 4464 |
const void *userBuffer; |
| 4465 |
snd_pcm_t *save = stream->capture.pcm; |
| 4466 |
|
| 4467 |
assert( stream ); |
| 4468 |
|
| 4469 |
PA_UNLESS( stream->playback.pcm, paCanNotWriteToAnInputOnlyStream ); |
| 4470 |
|
| 4471 |
/* Disregard capture */ |
| 4472 |
stream->capture.pcm = NULL; |
| 4473 |
|
| 4474 |
if( stream->underrun > 0. ) |
| 4475 |
{ |
| 4476 |
result = paOutputUnderflowed; |
| 4477 |
stream->underrun = 0.0; |
| 4478 |
} |
| 4479 |
|
| 4480 |
if( stream->playback.userInterleaved ) |
| 4481 |
userBuffer = buffer; |
| 4482 |
else /* Copy channels into local array */ |
| 4483 |
{ |
| 4484 |
userBuffer = stream->playback.userBuffers; |
| 4485 |
memcpy( (void *)userBuffer, buffer, sizeof (void *) * stream->playback.numUserChannels ); |
| 4486 |
} |
| 4487 |
|
| 4488 |
while( frames > 0 ) |
| 4489 |
{ |
| 4490 |
int xrun = 0; |
| 4491 |
snd_pcm_uframes_t hwAvail; |
| 4492 |
|
| 4493 |
PA_ENSURE( PaAlsaStream_WaitForFrames( stream, &framesAvail, &xrun ) ); |
| 4494 |
framesGot = PA_MIN( framesAvail, frames ); |
| 4495 |
|
| 4496 |
PA_ENSURE( PaAlsaStream_SetUpBuffers( stream, &framesGot, &xrun ) ); |
| 4497 |
if( framesGot > 0 ) |
| 4498 |
{ |
| 4499 |
framesGot = PaUtil_CopyOutput( &stream->bufferProcessor, &userBuffer, framesGot ); |
| 4500 |
PA_ENSURE( PaAlsaStream_EndProcessing( stream, framesGot, &xrun ) ); |
| 4501 |
frames -= framesGot; |
| 4502 |
} |
| 4503 |
|
| 4504 |
/* Start stream after one period of samples worth */ |
| 4505 |
|
| 4506 |
/* Frames residing in buffer */ |
| 4507 |
PA_ENSURE( err = GetStreamWriteAvailable( stream ) ); |
| 4508 |
framesAvail = err; |
| 4509 |
hwAvail = stream->playback.alsaBufferSize - framesAvail; |
| 4510 |
|
| 4511 |
if( alsa_snd_pcm_state( stream->playback.pcm ) == SND_PCM_STATE_PREPARED && |
| 4512 |
hwAvail >= stream->playback.framesPerPeriod ) |
| 4513 |
{ |
| 4514 |
ENSURE_( alsa_snd_pcm_start( stream->playback.pcm ), paUnanticipatedHostError ); |
| 4515 |
} |
| 4516 |
} |
| 4517 |
|
| 4518 |
end: |
| 4519 |
stream->capture.pcm = save; |
| 4520 |
return result; |
| 4521 |
error: |
| 4522 |
goto end; |
| 4523 |
} |
| 4524 |
|
| 4525 |
/* Return frames available for reading. In the event of an overflow, the capture pcm will be restarted */ |
| 4526 |
static signed long GetStreamReadAvailable( PaStream* s ) |
| 4527 |
{ |
| 4528 |
PaError result = paNoError; |
| 4529 |
PaAlsaStream *stream = (PaAlsaStream*)s; |
| 4530 |
unsigned long avail; |
| 4531 |
int xrun; |
| 4532 |
|
| 4533 |
PA_ENSURE( PaAlsaStreamComponent_GetAvailableFrames( &stream->capture, &avail, &xrun ) ); |
| 4534 |
if( xrun ) |
| 4535 |
{ |
| 4536 |
PA_ENSURE( PaAlsaStream_HandleXrun( stream ) ); |
| 4537 |
PA_ENSURE( PaAlsaStreamComponent_GetAvailableFrames( &stream->capture, &avail, &xrun ) ); |
| 4538 |
if( xrun ) |
| 4539 |
PA_ENSURE( paInputOverflowed ); |
| 4540 |
} |
| 4541 |
|
| 4542 |
return (signed long)avail; |
| 4543 |
|
| 4544 |
error: |
| 4545 |
return result; |
| 4546 |
} |
| 4547 |
|
| 4548 |
static signed long GetStreamWriteAvailable( PaStream* s ) |
| 4549 |
{ |
| 4550 |
PaError result = paNoError; |
| 4551 |
PaAlsaStream *stream = (PaAlsaStream*)s; |
| 4552 |
unsigned long avail; |
| 4553 |
int xrun; |
| 4554 |
|
| 4555 |
PA_ENSURE( PaAlsaStreamComponent_GetAvailableFrames( &stream->playback, &avail, &xrun ) ); |
| 4556 |
if( xrun ) |
| 4557 |
{ |
| 4558 |
snd_pcm_sframes_t savail; |
| 4559 |
|
| 4560 |
PA_ENSURE( PaAlsaStream_HandleXrun( stream ) ); |
| 4561 |
savail = alsa_snd_pcm_avail_update( stream->playback.pcm ); |
| 4562 |
|
| 4563 |
/* savail should not contain -EPIPE now, since PaAlsaStream_HandleXrun will only prepare the pcm */ |
| 4564 |
ENSURE_( savail, paUnanticipatedHostError ); |
| 4565 |
|
| 4566 |
avail = (unsigned long) savail; |
| 4567 |
} |
| 4568 |
|
| 4569 |
return (signed long)avail; |
| 4570 |
|
| 4571 |
error: |
| 4572 |
return result; |
| 4573 |
} |
| 4574 |
|
| 4575 |
/* Extensions */ |
| 4576 |
|
| 4577 |
void PaAlsa_InitializeStreamInfo( PaAlsaStreamInfo *info ) |
| 4578 |
{ |
| 4579 |
info->size = sizeof (PaAlsaStreamInfo); |
| 4580 |
info->hostApiType = paALSA; |
| 4581 |
info->version = 1; |
| 4582 |
info->deviceString = NULL; |
| 4583 |
} |
| 4584 |
|
| 4585 |
void PaAlsa_EnableRealtimeScheduling( PaStream *s, int enable ) |
| 4586 |
{ |
| 4587 |
PaAlsaStream *stream = (PaAlsaStream *) s; |
| 4588 |
stream->rtSched = enable; |
| 4589 |
} |
| 4590 |
|
| 4591 |
#if 0 |
| 4592 |
void PaAlsa_EnableWatchdog( PaStream *s, int enable ) |
| 4593 |
{ |
| 4594 |
PaAlsaStream *stream = (PaAlsaStream *) s; |
| 4595 |
stream->thread.useWatchdog = enable; |
| 4596 |
} |
| 4597 |
#endif |
| 4598 |
|
| 4599 |
static PaError GetAlsaStreamPointer( PaStream* s, PaAlsaStream** stream ) |
| 4600 |
{ |
| 4601 |
PaError result = paNoError; |
| 4602 |
PaUtilHostApiRepresentation* hostApi; |
| 4603 |
PaAlsaHostApiRepresentation* alsaHostApi; |
| 4604 |
|
| 4605 |
PA_ENSURE( PaUtil_ValidateStreamPointer( s ) ); |
| 4606 |
PA_ENSURE( PaUtil_GetHostApiRepresentation( &hostApi, paALSA ) ); |
| 4607 |
alsaHostApi = (PaAlsaHostApiRepresentation*)hostApi; |
| 4608 |
|
| 4609 |
PA_UNLESS( PA_STREAM_REP( s )->streamInterface == &alsaHostApi->callbackStreamInterface |
| 4610 |
|| PA_STREAM_REP( s )->streamInterface == &alsaHostApi->blockingStreamInterface, |
| 4611 |
paIncompatibleStreamHostApi ); |
| 4612 |
|
| 4613 |
*stream = (PaAlsaStream*)s; |
| 4614 |
error: |
| 4615 |
return paNoError; |
| 4616 |
} |
| 4617 |
|
| 4618 |
PaError PaAlsa_GetStreamInputCard( PaStream* s, int* card ) |
| 4619 |
{ |
| 4620 |
PaAlsaStream *stream; |
| 4621 |
PaError result = paNoError; |
| 4622 |
snd_pcm_info_t* pcmInfo; |
| 4623 |
|
| 4624 |
PA_ENSURE( GetAlsaStreamPointer( s, &stream ) ); |
| 4625 |
|
| 4626 |
/* XXX: More descriptive error? */ |
| 4627 |
PA_UNLESS( stream->capture.pcm, paDeviceUnavailable ); |
| 4628 |
|
| 4629 |
alsa_snd_pcm_info_alloca( &pcmInfo ); |
| 4630 |
PA_ENSURE( alsa_snd_pcm_info( stream->capture.pcm, pcmInfo ) ); |
| 4631 |
*card = alsa_snd_pcm_info_get_card( pcmInfo ); |
| 4632 |
|
| 4633 |
error: |
| 4634 |
return result; |
| 4635 |
} |
| 4636 |
|
| 4637 |
PaError PaAlsa_GetStreamOutputCard( PaStream* s, int* card ) |
| 4638 |
{ |
| 4639 |
PaAlsaStream *stream; |
| 4640 |
PaError result = paNoError; |
| 4641 |
snd_pcm_info_t* pcmInfo; |
| 4642 |
|
| 4643 |
PA_ENSURE( GetAlsaStreamPointer( s, &stream ) ); |
| 4644 |
|
| 4645 |
/* XXX: More descriptive error? */ |
| 4646 |
PA_UNLESS( stream->playback.pcm, paDeviceUnavailable ); |
| 4647 |
|
| 4648 |
alsa_snd_pcm_info_alloca( &pcmInfo ); |
| 4649 |
PA_ENSURE( alsa_snd_pcm_info( stream->playback.pcm, pcmInfo ) ); |
| 4650 |
*card = alsa_snd_pcm_info_get_card( pcmInfo ); |
| 4651 |
|
| 4652 |
error: |
| 4653 |
return result; |
| 4654 |
} |
| 4655 |
|
| 4656 |
PaError PaAlsa_SetRetriesBusy( int retries ) |
| 4657 |
{ |
| 4658 |
busyRetries_ = retries; |
| 4659 |
return paNoError; |
| 4660 |
} |
| 4661 |
#endif |