| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
#include <stdio.h> |
| 24 |
#include <sys/ioctl.h> |
| 25 |
#include <alsa/asoundlib.h> |
| 26 |
#include <alsa/pcm_external.h> |
| 27 |
#include "list.h" |
| 28 |
#include "debug.h" |
| 29 |
#include "dsp-protocol.h" |
| 30 |
#include "constants.h" |
| 31 |
|
| 32 |
#define ARRAY_SIZE(ary) (sizeof(ary)/sizeof(ary[0])) |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
typedef struct { |
| 37 |
char *device; |
| 38 |
struct list_head list; |
| 39 |
} device_list_t; |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
typedef struct snd_pcm_alsa_dsp { |
| 47 |
snd_pcm_ioplug_t io; |
| 48 |
dsp_protocol_t *dsp_protocol; |
| 49 |
int format; |
| 50 |
int sample_rate; |
| 51 |
int bytes_per_frame; |
| 52 |
snd_pcm_sframes_t hw_pointer; |
| 53 |
device_list_t playback_devices; |
| 54 |
device_list_t recording_devices; |
| 55 |
} snd_pcm_alsa_dsp_t; |
| 56 |
|
| 57 |
static snd_pcm_alsa_dsp_t *free_ref; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
static int alsa_dsp_start(snd_pcm_ioplug_t * io) |
| 66 |
{ |
| 67 |
snd_pcm_alsa_dsp_t *alsa_dsp = io->private_data; |
| 68 |
int ret; |
| 69 |
DENTER(); |
| 70 |
DPRINT("IO_STREAM %d == SND_PCM_STREAM_PLAYBACK %d\n", io->stream, |
| 71 |
io->stream == SND_PCM_STREAM_PLAYBACK); |
| 72 |
if (io->stream != SND_PCM_STREAM_PLAYBACK) |
| 73 |
dsp_protocol_set_mic_enabled(alsa_dsp->dsp_protocol, 1); |
| 74 |
ret = dsp_protocol_send_play(alsa_dsp->dsp_protocol); |
| 75 |
DLEAVE(ret); |
| 76 |
return ret; |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
static int alsa_dsp_stop(snd_pcm_ioplug_t * io) |
| 87 |
{ |
| 88 |
snd_pcm_alsa_dsp_t *alsa_dsp = io->private_data; |
| 89 |
int ret; |
| 90 |
DENTER(); |
| 91 |
ret = dsp_protocol_send_stop(alsa_dsp->dsp_protocol); |
| 92 |
if (io->stream != SND_PCM_STREAM_PLAYBACK) |
| 93 |
dsp_protocol_set_mic_enabled(alsa_dsp->dsp_protocol, 0); |
| 94 |
|
| 95 |
DLEAVE(ret); |
| 96 |
return ret; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
static snd_pcm_sframes_t alsa_dsp_pointer(snd_pcm_ioplug_t * io) |
| 108 |
{ |
| 109 |
snd_pcm_alsa_dsp_t *alsa_dsp = io->private_data; |
| 110 |
snd_pcm_sframes_t ret; |
| 111 |
DENTER(); |
| 112 |
ret = alsa_dsp->hw_pointer; |
| 113 |
if (alsa_dsp->hw_pointer == 0) |
| 114 |
alsa_dsp->hw_pointer = |
| 115 |
io->period_size * alsa_dsp->bytes_per_frame; |
| 116 |
else |
| 117 |
alsa_dsp->hw_pointer = 0; |
| 118 |
DLEAVE((int)ret); |
| 119 |
return ret; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
static snd_pcm_sframes_t alsa_dsp_transfer(snd_pcm_ioplug_t * io, |
| 131 |
const snd_pcm_channel_area_t * areas, |
| 132 |
snd_pcm_uframes_t offset, |
| 133 |
snd_pcm_uframes_t size) |
| 134 |
{ |
| 135 |
snd_pcm_alsa_dsp_t *alsa_dsp = io->private_data; |
| 136 |
DENTER(); |
| 137 |
char *buf; |
| 138 |
int bytes, words; |
| 139 |
ssize_t result; |
| 140 |
|
| 141 |
bytes = size * alsa_dsp->bytes_per_frame; |
| 142 |
DPRINT("***** Info: samples %lu * bpf %d => bytes %d\n", |
| 143 |
size, alsa_dsp->bytes_per_frame, bytes); |
| 144 |
if (bytes > alsa_dsp->dsp_protocol->mmap_buffer_size) { |
| 145 |
DERROR("Requested too much data transfer (requested %d, playing only %d)\n", |
| 146 |
bytes, alsa_dsp->dsp_protocol->mmap_buffer_size); |
| 147 |
bytes = alsa_dsp->dsp_protocol->mmap_buffer_size; |
| 148 |
} |
| 149 |
words = bytes / 2; |
| 150 |
if (alsa_dsp->dsp_protocol->state != STATE_PLAYING) { |
| 151 |
DPRINT("I did nothing - No start sent\n"); |
| 152 |
alsa_dsp_start(io); |
| 153 |
} |
| 154 |
|
| 155 |
buf = (char *)areas->addr + (areas->first + areas->step * offset) / 8; |
| 156 |
if (io->stream == SND_PCM_STREAM_PLAYBACK) |
| 157 |
result = |
| 158 |
dsp_protocol_send_audio_data(alsa_dsp->dsp_protocol, buf, |
| 159 |
words); |
| 160 |
else |
| 161 |
result = |
| 162 |
dsp_protocol_receive_audio_data(alsa_dsp->dsp_protocol, buf, |
| 163 |
words); |
| 164 |
result *= 2; |
| 165 |
result /= alsa_dsp->bytes_per_frame; |
| 166 |
alsa_dsp->hw_pointer += result; |
| 167 |
DLEAVE(result); |
| 168 |
return result; |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
static int free_device_list(device_list_t * device_list) |
| 179 |
{ |
| 180 |
struct list_head *pos, *q; |
| 181 |
device_list_t *tmp; |
| 182 |
list_for_each_safe(pos, q, &device_list->list) { |
| 183 |
tmp = list_entry(pos, device_list_t, list); |
| 184 |
list_del(pos); |
| 185 |
free(tmp->device); |
| 186 |
free(tmp); |
| 187 |
} |
| 188 |
return 0; |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
static int alsa_dsp_close(snd_pcm_ioplug_t * io) |
| 200 |
{ |
| 201 |
snd_pcm_alsa_dsp_t *alsa_dsp = io->private_data; |
| 202 |
int ret = 0; |
| 203 |
DENTER(); |
| 204 |
ret = dsp_protocol_close_node(alsa_dsp->dsp_protocol); |
| 205 |
dsp_protocol_destroy(&(alsa_dsp->dsp_protocol)); |
| 206 |
free_device_list(&(alsa_dsp->playback_devices)); |
| 207 |
free_device_list(&(alsa_dsp->recording_devices)); |
| 208 |
DLEAVE(ret); |
| 209 |
return ret; |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
static int map_value(int *map, int value, int steps) |
| 222 |
{ |
| 223 |
int i; |
| 224 |
for (i = 0; i < steps; i++) |
| 225 |
if (map[i * 2] == value) |
| 226 |
return map[i * 2 + 1]; |
| 227 |
return -1; |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
static int alsa_dsp_hw_params(snd_pcm_ioplug_t * io, |
| 239 |
snd_pcm_hw_params_t * params) |
| 240 |
{ |
| 241 |
snd_pcm_alsa_dsp_t *alsa_dsp = io->private_data; |
| 242 |
int ret = 0; |
| 243 |
int map_sample_rates[] = { |
| 244 |
8000, SAMPLE_RATE_8KHZ, |
| 245 |
11025, SAMPLE_RATE_11_025KHZ, |
| 246 |
12000, SAMPLE_RATE_12KHZ, |
| 247 |
16000, SAMPLE_RATE_16KHZ, |
| 248 |
22050, SAMPLE_RATE_22_05KHZ, |
| 249 |
24000, SAMPLE_RATE_24KHZ, |
| 250 |
32000, SAMPLE_RATE_32KHZ, |
| 251 |
44100, SAMPLE_RATE_44_1KHZ, |
| 252 |
48000, SAMPLE_RATE_48KHZ |
| 253 |
}; |
| 254 |
int map_formats[] = { |
| 255 |
SND_PCM_FORMAT_A_LAW, DSP_AFMT_ALAW, |
| 256 |
SND_PCM_FORMAT_MU_LAW, DSP_AFMT_ULAW, |
| 257 |
SND_PCM_FORMAT_S16_LE, DSP_AFMT_S16_LE, |
| 258 |
SND_PCM_FORMAT_U8, DSP_AFMT_U8, |
| 259 |
SND_PCM_FORMAT_S8, DSP_AFMT_S8, |
| 260 |
SND_PCM_FORMAT_S16_BE, DSP_AFMT_S16_BE, |
| 261 |
SND_PCM_FORMAT_U16_LE, DSP_AFMT_U16_LE, |
| 262 |
SND_PCM_FORMAT_U16_BE, DSP_AFMT_U16_BE |
| 263 |
}; |
| 264 |
DENTER(); |
| 265 |
DPRINT("Checking Format- Ret %d\n", ret); |
| 266 |
alsa_dsp->format = map_value(map_formats, io->format, |
| 267 |
io->stream == |
| 268 |
SND_PCM_STREAM_PLAYBACK ? |
| 269 |
ARRAY_SIZE(map_formats) : 3); |
| 270 |
if (alsa_dsp->format < 0) { |
| 271 |
DERROR("*** ALSA-DSP: unsupported format %s\n", |
| 272 |
snd_pcm_format_name(io->format)); |
| 273 |
ret = -EINVAL; |
| 274 |
} |
| 275 |
DPRINT("Format is Ok. Checking rate. Ret %d\n", ret); |
| 276 |
|
| 277 |
alsa_dsp->sample_rate = map_value(map_sample_rates, io->rate, |
| 278 |
io->stream == |
| 279 |
SND_PCM_STREAM_PLAYBACK ? |
| 280 |
ARRAY_SIZE(map_sample_rates) : 1); |
| 281 |
if (alsa_dsp->sample_rate < 0) { |
| 282 |
ret = -EINVAL; |
| 283 |
DERROR("** ALSA - DSP - Unsuported Sample Rate! **\n"); |
| 284 |
} |
| 285 |
DPRINT("Rate is ok. Calculating WPF. Ret %d\n", ret); |
| 286 |
|
| 287 |
alsa_dsp->bytes_per_frame = |
| 288 |
((snd_pcm_format_physical_width(io->format) * io->channels) / 8); |
| 289 |
DPRINT("WPF: %d width %d channels %d\n", alsa_dsp->bytes_per_frame, |
| 290 |
snd_pcm_format_physical_width(io->format), io->channels); |
| 291 |
|
| 292 |
DLEAVE(ret); |
| 293 |
return ret; |
| 294 |
} |
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
static int alsa_dsp_prepare(snd_pcm_ioplug_t * io) |
| 305 |
{ |
| 306 |
snd_pcm_alsa_dsp_t *alsa_dsp = io->private_data; |
| 307 |
audio_params_data_t params; |
| 308 |
speech_params_data_t sparams; |
| 309 |
int ret = 0; |
| 310 |
char *tmp; |
| 311 |
DENTER(); |
| 312 |
|
| 313 |
alsa_dsp->hw_pointer = 0; |
| 314 |
if (alsa_dsp->dsp_protocol->state != STATE_INITIALISED) { |
| 315 |
tmp = strdup(alsa_dsp->dsp_protocol->device); |
| 316 |
ret = dsp_protocol_close_node(alsa_dsp->dsp_protocol); |
| 317 |
if (!ret) |
| 318 |
dsp_protocol_open_node(alsa_dsp->dsp_protocol, tmp); |
| 319 |
free(tmp); |
| 320 |
} |
| 321 |
if (ret == 0) { |
| 322 |
if (io->stream == SND_PCM_STREAM_PLAYBACK) { |
| 323 |
params.dsp_cmd = DSP_CMD_SET_PARAMS; |
| 324 |
params.dsp_audio_fmt = alsa_dsp->format; |
| 325 |
params.sample_rate = alsa_dsp->sample_rate; |
| 326 |
params.number_channels = io->channels; |
| 327 |
params.ds_stream_id = 0; |
| 328 |
params.stream_priority = 0; |
| 329 |
if (dsp_protocol_send_audio_params |
| 330 |
(alsa_dsp->dsp_protocol, ¶ms) < 0) { |
| 331 |
ret = -EIO; |
| 332 |
DERROR("Error in send params data\n"); |
| 333 |
} else |
| 334 |
DPRINT("Sending params data is ok\n"); |
| 335 |
} else { |
| 336 |
sparams.dsp_cmd = DSP_CMD_SET_SPEECH_PARAMS; |
| 337 |
sparams.audio_fmt = alsa_dsp->format; |
| 338 |
sparams.sample_rate = alsa_dsp->sample_rate; |
| 339 |
sparams.ds_stream_id = 0; |
| 340 |
sparams.stream_priority = 0; |
| 341 |
sparams.frame_size = io->period_size; |
| 342 |
DPRINT("frame size %u\n", sparams.frame_size); |
| 343 |
if (dsp_protocol_send_speech_params |
| 344 |
(alsa_dsp->dsp_protocol, &sparams) < 0) { |
| 345 |
ret = -EIO; |
| 346 |
DERROR("Error in send speech params data\n"); |
| 347 |
} else |
| 348 |
DPRINT("Sending speech params data is ok\n"); |
| 349 |
|
| 350 |
} |
| 351 |
} |
| 352 |
DLEAVE(ret); |
| 353 |
return ret; |
| 354 |
} |
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
static int alsa_dsp_pause(snd_pcm_ioplug_t * io, int enable) |
| 364 |
{ |
| 365 |
snd_pcm_alsa_dsp_t *alsa_dsp = io->private_data; |
| 366 |
int ret; |
| 367 |
DENTER(); |
| 368 |
ret = dsp_protocol_send_pause(alsa_dsp->dsp_protocol); |
| 369 |
DLEAVE(ret); |
| 370 |
return ret; |
| 371 |
} |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
static int alsa_dsp_resume(snd_pcm_ioplug_t * io) |
| 381 |
{ |
| 382 |
snd_pcm_alsa_dsp_t *alsa_dsp = io->private_data; |
| 383 |
int ret; |
| 384 |
DENTER(); |
| 385 |
ret = dsp_protocol_send_play(alsa_dsp->dsp_protocol); |
| 386 |
DLEAVE(ret); |
| 387 |
return ret; |
| 388 |
} |
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
static int alsa_dsp_configure_constraints(snd_pcm_alsa_dsp_t * alsa_dsp) |
| 400 |
{ |
| 401 |
snd_pcm_ioplug_t *io = &alsa_dsp->io; |
| 402 |
static const snd_pcm_access_t access_list[] = { |
| 403 |
SND_PCM_ACCESS_RW_INTERLEAVED |
| 404 |
}; |
| 405 |
static const unsigned int formats[] = { |
| 406 |
SND_PCM_FORMAT_U8, |
| 407 |
SND_PCM_FORMAT_S16_LE, |
| 408 |
SND_PCM_FORMAT_S16_BE, |
| 409 |
SND_PCM_FORMAT_S8, |
| 410 |
SND_PCM_FORMAT_U16_LE, |
| 411 |
SND_PCM_FORMAT_U16_BE, |
| 412 |
SND_PCM_FORMAT_A_LAW, |
| 413 |
SND_PCM_FORMAT_MU_LAW |
| 414 |
}; |
| 415 |
static const unsigned int formats_recor[] = { |
| 416 |
SND_PCM_FORMAT_S16_LE, |
| 417 |
SND_PCM_FORMAT_A_LAW, |
| 418 |
SND_PCM_FORMAT_MU_LAW |
| 419 |
}; |
| 420 |
static const unsigned int bytes_list[] = { |
| 421 |
1U << 11, 1U << 12 |
| 422 |
}; |
| 423 |
static const unsigned int bytes_list_rec_8bit[] = { |
| 424 |
|
| 425 |
80, 160, 240, 320, 400, 480, 560, 640, 720, 800 |
| 426 |
}; |
| 427 |
|
| 428 |
int ret, err; |
| 429 |
DENTER(); |
| 430 |
|
| 431 |
if ((err = snd_pcm_ioplug_set_param_list(io, SND_PCM_IOPLUG_HW_ACCESS, |
| 432 |
ARRAY_SIZE(access_list), |
| 433 |
access_list)) < 0) { |
| 434 |
ret = err; |
| 435 |
goto out; |
| 436 |
} |
| 437 |
if (io->stream == SND_PCM_STREAM_PLAYBACK) { |
| 438 |
|
| 439 |
if ((err = |
| 440 |
snd_pcm_ioplug_set_param_list(io, SND_PCM_IOPLUG_HW_FORMAT, |
| 441 |
ARRAY_SIZE(formats), |
| 442 |
formats)) < 0) { |
| 443 |
ret = err; |
| 444 |
goto out; |
| 445 |
} |
| 446 |
|
| 447 |
if ((err = |
| 448 |
snd_pcm_ioplug_set_param_minmax(io, |
| 449 |
SND_PCM_IOPLUG_HW_CHANNELS, |
| 450 |
1, 2)) < 0) { |
| 451 |
ret = err; |
| 452 |
goto out; |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
if ((err = |
| 457 |
snd_pcm_ioplug_set_param_minmax(io, SND_PCM_IOPLUG_HW_RATE, |
| 458 |
8000, 48000)) < 0) { |
| 459 |
ret = err; |
| 460 |
goto out; |
| 461 |
} |
| 462 |
|
| 463 |
if ((err = |
| 464 |
snd_pcm_ioplug_set_param_list(io, |
| 465 |
SND_PCM_IOPLUG_HW_PERIOD_BYTES, |
| 466 |
ARRAY_SIZE(bytes_list), |
| 467 |
bytes_list)) < 0) { |
| 468 |
ret = err; |
| 469 |
goto out; |
| 470 |
} |
| 471 |
|
| 472 |
if ((err = |
| 473 |
snd_pcm_ioplug_set_param_list(io, |
| 474 |
SND_PCM_IOPLUG_HW_BUFFER_BYTES, |
| 475 |
ARRAY_SIZE(bytes_list), |
| 476 |
bytes_list)) < 0) { |
| 477 |
ret = err; |
| 478 |
goto out; |
| 479 |
} |
| 480 |
|
| 481 |
} else { |
| 482 |
|
| 483 |
if ((err = |
| 484 |
snd_pcm_ioplug_set_param_list(io, |
| 485 |
SND_PCM_IOPLUG_HW_FORMAT, |
| 486 |
ARRAY_SIZE(formats_recor), |
| 487 |
formats_recor)) < 0) { |
| 488 |
ret = err; |
| 489 |
goto out; |
| 490 |
} |
| 491 |
|
| 492 |
if ((err = snd_pcm_ioplug_set_param_minmax(io, |
| 493 |
SND_PCM_IOPLUG_HW_CHANNELS, |
| 494 |
1, 1)) < 0) { |
| 495 |
ret = err; |
| 496 |
goto out; |
| 497 |
} |
| 498 |
|
| 499 |
|
| 500 |
if ((err = |
| 501 |
snd_pcm_ioplug_set_param_minmax(io, |
| 502 |
SND_PCM_IOPLUG_HW_RATE, |
| 503 |
8000, 8000)) < 0) { |
| 504 |
ret = err; |
| 505 |
goto out; |
| 506 |
} |
| 507 |
|
| 508 |
if ((err = |
| 509 |
snd_pcm_ioplug_set_param_list(io, |
| 510 |
SND_PCM_IOPLUG_HW_PERIOD_BYTES, |
| 511 |
ARRAY_SIZE |
| 512 |
(bytes_list_rec_8bit), |
| 513 |
bytes_list_rec_8bit)) < 0) { |
| 514 |
ret = err; |
| 515 |
goto out; |
| 516 |
} |
| 517 |
|
| 518 |
if ((err = |
| 519 |
snd_pcm_ioplug_set_param_list(io, |
| 520 |
SND_PCM_IOPLUG_HW_BUFFER_BYTES, |
| 521 |
ARRAY_SIZE |
| 522 |
(bytes_list_rec_8bit), |
| 523 |
bytes_list_rec_8bit)) < 0) { |
| 524 |
ret = err; |
| 525 |
goto out; |
| 526 |
} |
| 527 |
|
| 528 |
} |
| 529 |
|
| 530 |
if ((err = snd_pcm_ioplug_set_param_minmax(io, |
| 531 |
SND_PCM_IOPLUG_HW_PERIODS, |
| 532 |
2, 1024)) < 0) { |
| 533 |
ret = err; |
| 534 |
goto out; |
| 535 |
} |
| 536 |
ret = 0; |
| 537 |
out: |
| 538 |
DLEAVE(ret); |
| 539 |
return ret; |
| 540 |
} |
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
static snd_pcm_ioplug_callback_t alsa_dsp_callback = { |
| 546 |
.start = alsa_dsp_start, |
| 547 |
.stop = alsa_dsp_stop, |
| 548 |
.pointer = alsa_dsp_pointer, |
| 549 |
.transfer = alsa_dsp_transfer, |
| 550 |
.close = alsa_dsp_close, |
| 551 |
.hw_params = alsa_dsp_hw_params, |
| 552 |
.prepare = alsa_dsp_prepare, |
| 553 |
.pause = alsa_dsp_pause, |
| 554 |
.resume = alsa_dsp_resume, |
| 555 |
}; |
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
static int alsa_dsp_open_dsp_task(snd_pcm_alsa_dsp_t * alsa_dsp, |
| 567 |
device_list_t * device_list) |
| 568 |
{ |
| 569 |
int err = -EINVAL; |
| 570 |
device_list_t *tmp; |
| 571 |
DENTER(); |
| 572 |
DPRINT("Looking for a dsp device node \n"); |
| 573 |
list_for_each_entry(tmp, &(device_list->list), list) { |
| 574 |
DPRINT("Trying to use %s\n", tmp->device); |
| 575 |
if ((err = |
| 576 |
dsp_protocol_open_node(alsa_dsp->dsp_protocol, |
| 577 |
tmp->device)) < 0) { |
| 578 |
DPRINT("%s is not available now\n", tmp->device); |
| 579 |
dsp_protocol_close_node(alsa_dsp->dsp_protocol); |
| 580 |
} else |
| 581 |
break; |
| 582 |
} |
| 583 |
if (err < 0) { |
| 584 |
DPRINT("No valid dsp task nodes for now. Exiting.\n"); |
| 585 |
} |
| 586 |
DLEAVE(err); |
| 587 |
return err; |
| 588 |
} |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
static int fill_string_list(snd_config_t * n, device_list_t * device_list) |
| 600 |
{ |
| 601 |
snd_config_iterator_t j, nextj; |
| 602 |
device_list_t *tmp; |
| 603 |
long idx = 0; |
| 604 |
int ret; |
| 605 |
DENTER(); |
| 606 |
INIT_LIST_HEAD(&device_list->list); |
| 607 |
snd_config_for_each(j, nextj, n) { |
| 608 |
snd_config_t *s = snd_config_iterator_entry(j); |
| 609 |
const char *id_number; |
| 610 |
long k; |
| 611 |
if (snd_config_get_id(s, &id_number) < 0) |
| 612 |
continue; |
| 613 |
if (safe_strtol(id_number, &k) < 0) { |
| 614 |
SNDERR("id of field %s is not an integer", id_number); |
| 615 |
ret = -EINVAL; |
| 616 |
goto out; |
| 617 |
} |
| 618 |
if (k == idx) { |
| 619 |
idx++; |
| 620 |
|
| 621 |
tmp = (device_list_t *) malloc(sizeof(device_list_t)); |
| 622 |
if (snd_config_get_ascii(s, &(tmp->device)) < 0) { |
| 623 |
SNDERR("invalid ascii string for id %s\n", |
| 624 |
id_number); |
| 625 |
ret = -EINVAL; |
| 626 |
goto out; |
| 627 |
} |
| 628 |
|
| 629 |
list_add(&(tmp->list), &(device_list->list)); |
| 630 |
} |
| 631 |
|
| 632 |
} |
| 633 |
ret = 0; |
| 634 |
out: |
| 635 |
DLEAVE(ret); |
| 636 |
return ret; |
| 637 |
} |
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
SND_PCM_PLUGIN_DEFINE_FUNC(alsa_dsp) |
| 646 |
{ |
| 647 |
snd_config_iterator_t i, next; |
| 648 |
snd_pcm_alsa_dsp_t *alsa_dsp; |
| 649 |
int err; |
| 650 |
int ret; |
| 651 |
DENTER(); |
| 652 |
|
| 653 |
|
| 654 |
alsa_dsp = calloc(1, sizeof(snd_pcm_alsa_dsp_t)); |
| 655 |
if (alsa_dsp == NULL) { |
| 656 |
ret = -ENOMEM; |
| 657 |
goto out; |
| 658 |
} |
| 659 |
|
| 660 |
|
| 661 |
snd_config_for_each(i, next, conf) { |
| 662 |
snd_config_t *n = snd_config_iterator_entry(i); |
| 663 |
const char *id; |
| 664 |
if (snd_config_get_id(n, &id) < 0) |
| 665 |
continue; |
| 666 |
if (strcmp(id, "comment") == 0 || strcmp(id, "type") == 0 || strcmp(id, "hint") == 0) |
| 667 |
continue; |
| 668 |
if (strcmp(id, "playback_device_file") == 0) { |
| 669 |
if (snd_config_get_type(n) == SND_CONFIG_TYPE_COMPOUND){ |
| 670 |
if ((err = |
| 671 |
fill_string_list(n, |
| 672 |
&(alsa_dsp->playback_devices))) < 0) { |
| 673 |
SNDERR("Could not fill string" |
| 674 |
" list for playback devices\n"); |
| 675 |
goto error; |
| 676 |
} |
| 677 |
} else { |
| 678 |
SNDERR("Invalid type for %s", id); |
| 679 |
err = -EINVAL; |
| 680 |
goto error; |
| 681 |
} |
| 682 |
|
| 683 |
continue; |
| 684 |
} |
| 685 |
if (strcmp(id, "recording_device_file") == 0) { |
| 686 |
if (snd_config_get_type(n) == SND_CONFIG_TYPE_COMPOUND){ |
| 687 |
if ((err = |
| 688 |
fill_string_list(n, |
| 689 |
&(alsa_dsp->recording_devices))) < 0){ |
| 690 |
SNDERR("Could not fill string" |
| 691 |
" list for recording devices\n"); |
| 692 |
goto error; |
| 693 |
} |
| 694 |
} else { |
| 695 |
SNDERR("Invalid type for %s", id); |
| 696 |
err = -EINVAL; |
| 697 |
goto error; |
| 698 |
} |
| 699 |
|
| 700 |
continue; |
| 701 |
} |
| 702 |
SNDERR("Unknown field %s", id); |
| 703 |
err = -EINVAL; |
| 704 |
goto error; |
| 705 |
} |
| 706 |
|
| 707 |
if ((err = dsp_protocol_create(&(alsa_dsp->dsp_protocol))) < 0) |
| 708 |
goto error; |
| 709 |
if ((err = alsa_dsp_open_dsp_task(alsa_dsp, |
| 710 |
(stream == SND_PCM_STREAM_PLAYBACK) ? |
| 711 |
&(alsa_dsp->playback_devices) : |
| 712 |
&(alsa_dsp->recording_devices))) < 0) |
| 713 |
goto error; |
| 714 |
|
| 715 |
alsa_dsp->io.version = SND_PCM_IOPLUG_VERSION; |
| 716 |
alsa_dsp->io.name = "Alsa - DSP PCM Plugin"; |
| 717 |
alsa_dsp->io.mmap_rw = 0; |
| 718 |
alsa_dsp->io.callback = &alsa_dsp_callback; |
| 719 |
alsa_dsp->io.poll_fd = alsa_dsp->dsp_protocol->fd; |
| 720 |
alsa_dsp->io.poll_events = stream == SND_PCM_STREAM_PLAYBACK ? |
| 721 |
POLLOUT : POLLIN; |
| 722 |
|
| 723 |
alsa_dsp->io.private_data = alsa_dsp; |
| 724 |
free_ref = alsa_dsp; |
| 725 |
|
| 726 |
if ((err = snd_pcm_ioplug_create(&alsa_dsp->io, name, |
| 727 |
stream, mode)) < 0) |
| 728 |
goto error; |
| 729 |
|
| 730 |
|
| 731 |
if ((err = alsa_dsp_configure_constraints(alsa_dsp)) < 0) { |
| 732 |
snd_pcm_ioplug_delete(&alsa_dsp->io); |
| 733 |
goto error; |
| 734 |
} |
| 735 |
*pcmp = alsa_dsp->io.pcm; |
| 736 |
ret = 0; |
| 737 |
goto out; |
| 738 |
error: |
| 739 |
ret = err; |
| 740 |
free(alsa_dsp); |
| 741 |
out: |
| 742 |
DLEAVE(ret); |
| 743 |
return ret; |
| 744 |
} |
| 745 |
|
| 746 |
|
| 747 |
static void alsa_dsp_descructor(void) __attribute__ ((destructor)); |
| 748 |
|
| 749 |
static void alsa_dsp_descructor(void) |
| 750 |
{ |
| 751 |
DENTER(); |
| 752 |
DPRINT("alsa dsp destructor\n"); |
| 753 |
DPRINT("checking for memories leaks and releasing resources\n"); |
| 754 |
if (free_ref) { |
| 755 |
if (free_ref->dsp_protocol) { |
| 756 |
dsp_protocol_close_node(free_ref->dsp_protocol); |
| 757 |
dsp_protocol_destroy(&(free_ref->dsp_protocol)); |
| 758 |
} |
| 759 |
free_device_list(&(free_ref->playback_devices)); |
| 760 |
|
| 761 |
free_device_list(&(free_ref->recording_devices)); |
| 762 |
|
| 763 |
free(free_ref); |
| 764 |
free_ref = NULL; |
| 765 |
} |
| 766 |
DLEAVE(0); |
| 767 |
|
| 768 |
} |
| 769 |
|
| 770 |
SND_PCM_PLUGIN_SYMBOL(alsa_dsp); |