125
/* return the address of the driver info structure */
79
126
ao_info *ao_plugin_driver_info(void)
81
128
return &ao_alsa_info;
132
/* initialize internal data structures */
85
133
int ao_plugin_device_init(ao_device *device)
87
135
ao_alsa_internal *internal;
89
internal = (ao_alsa_internal *) malloc(sizeof(ao_alsa_internal));
92
return 0; /* Could not initialize device memory */
94
internal->buf_size = AO_ALSA_BUF_SIZE;
137
internal = (ao_alsa_internal *) calloc(1,sizeof(ao_alsa_internal));
139
if (internal == NULL)
142
internal->buffer_time = AO_ALSA_BUFFER_TIME;
143
internal->period_time = AO_ALSA_PERIOD_TIME;
144
internal->writei = AO_ALSA_WRITEI;
145
internal->access_mask = AO_ALSA_ACCESS_MASK;
98
147
device->internal = internal;
148
device->output_matrix = strdup("L,R,BL,BR,C,LFE,SL,SR");
149
device->output_matrix_order = AO_OUTPUT_MATRIX_FIXED;
100
return 1; /* Memory alloc successful */
155
/* pass application parameters regarding the sound device */
104
156
int ao_plugin_set_option(ao_device *device, const char *key, const char *value)
106
158
ao_alsa_internal *internal = (ao_alsa_internal *) device->internal;
107
if (!strcmp(key, "card"))
108
internal->card = atoi(value);
109
else if (!strcmp(key, "dev"))
110
internal->dev = atoi(value);
111
else if (!strcmp(key, "buf_size"))
112
internal->buf_size = atoi(value);
160
if (!strcmp(key, "dev")) {
162
free (internal->dev);
163
if (!(internal->dev = strdup(value)))
166
else if (!strcmp(key, "buffer_time"))
167
internal->buffer_time = atoi(value) * 1000;
168
else if (!strcmp(key, "period_time"))
169
internal->period_time = atoi(value);
170
else if (!strcmp(key,"use_mmap")) {
171
if(!strcmp(value,"yes") || !strcmp(value,"y") ||
172
!strcmp(value,"true") || !strcmp(value,"t") ||
175
internal->writei = snd_pcm_mmap_writei;
176
internal->access_mask = SND_PCM_ACCESS_MMAP_INTERLEAVED;
179
internal->writei = snd_pcm_writei;
180
internal->access_mask = SND_PCM_ACCESS_RW_INTERLEAVED;
188
/* determine the alsa bitformat for a given bitwidth and endianness */
189
static inline int alsa_get_sample_bitformat(int bitwidth, int bigendian, ao_device *device)
194
case 8 : ret = SND_PCM_FORMAT_S8;
196
case 16 : ret = SND_PCM_FORMAT_S16;
198
case 24 : ret = SND_PCM_FORMAT_S24;
200
case 32 : ret = SND_PCM_FORMAT_S32;
202
default : aerror("invalid bitwidth %d\n", bitwidth);
209
/* setup alsa data format and buffer geometry */
210
static inline int alsa_set_hwparams(ao_device *device,
211
ao_sample_format *format)
213
ao_alsa_internal *internal = (ao_alsa_internal *) device->internal;
214
snd_pcm_hw_params_t *params;
216
unsigned int rate = format->rate;
218
/* allocate the hardware parameter structure */
219
snd_pcm_hw_params_alloca(¶ms);
221
/* fetch all possible hardware parameters */
222
err = snd_pcm_hw_params_any(internal->pcm_handle, params);
224
adebug("snd_pcm_hw_params_any() failed.\n"
225
" Device exists but no matching hardware?\n");
229
/* set the access type */
230
err = snd_pcm_hw_params_set_access(internal->pcm_handle,
231
params, internal->access_mask);
233
adebug("snd_pcm_hw_params_set_access() failed.\n");
237
/* set the sample bitformat */
238
err = snd_pcm_hw_params_set_format(internal->pcm_handle,
239
params, internal->bitformat);
241
adebug("snd_pcm_hw_params_set_format() failed.\n");
245
/* set the number of channels */
246
err = snd_pcm_hw_params_set_channels(internal->pcm_handle,
247
params, (unsigned int)device->output_channels);
249
adebug("snd_pcm_hw_params_set_channels() failed.\n");
253
/* save the sample size in bytes for posterity */
254
internal->sample_size = format->bits * device->output_channels / 8;
256
/* set the sample rate */
257
err = snd_pcm_hw_params_set_rate_near(internal->pcm_handle,
260
adebug("snd_pcm_hw_params_set_rate_near() failed.\n");
263
if (rate > 1.05 * format->rate || rate < 0.95 * format->rate) {
264
awarn("sample rate %i not supported "
265
"by the hardware, using %u\n", format->rate, rate);
268
/* set the length of the hardware sample buffer in microseconds */
269
err = snd_pcm_hw_params_set_buffer_time_near(internal->pcm_handle,
270
params, &(internal->buffer_time), 0);
272
adebug("snd_pcm_hw_params_set_buffer_time_near() failed.\n");
276
/* calculate a period time of one half sample time */
277
if ((internal->period_time == 0) && (rate > 0))
278
internal->period_time =
279
1000000 * AO_ALSA_SAMPLE_XFER / rate;
281
/* set the time per hardware sample transfer */
282
err = snd_pcm_hw_params_set_period_time_near(internal->pcm_handle,
283
params, &(internal->period_time), 0);
285
adebug("snd_pcm_hw_params_set_period_time_near() failed.\n");
289
/* commit the params structure to the hardware via ALSA */
290
err = snd_pcm_hw_params(internal->pcm_handle, params);
292
adebug("snd_pcm_hw_params() failed.\n");
296
/* save the period size in frames for posterity */
297
err = snd_pcm_hw_params_get_period_size(params,
298
&(internal->period_size), 0);
300
adebug("snd_pcm_hw_params_get_period_size() failed.\n");
308
/* setup alsa data transfer behavior */
309
static inline int alsa_set_swparams(ao_device *device)
311
ao_alsa_internal *internal = (ao_alsa_internal *) device->internal;
312
snd_pcm_sw_params_t *params;
315
/* allocate the software parameter structure */
316
snd_pcm_sw_params_alloca(¶ms);
318
/* fetch the current software parameters */
319
err = snd_pcm_sw_params_current(internal->pcm_handle, params);
321
adebug("snd_pcm_sw_params_current() failed.\n");
325
/* allow transfers to start when there is one period */
326
err = snd_pcm_sw_params_set_start_threshold(internal->pcm_handle,
327
params, internal->period_size);
329
adebug("snd_pcm_sw_params_set_start_threshold() failed.\n");
333
/* require a minimum of one full transfer in the buffer */
334
err = snd_pcm_sw_params_set_avail_min(internal->pcm_handle, params,
335
internal->period_size);
337
adebug("snd_pcm_sw_params_set_avail_min() failed.\n");
341
/* do not align transfers; this is obsolete/deprecated in ALSA
342
1.x where the transfer alignemnt is always 1 (except for
343
buggy drivers like VIA 82xx which still demand aligned
344
transfers regardless of setting, in violation of the ALSA
346
err = snd_pcm_sw_params_set_xfer_align(internal->pcm_handle, params, 1);
348
adebug("snd_pcm_sw_params_set_xfer_align() failed.\n");
352
/* force a work-ahead silence buffer; this is a fix, again for
353
VIA 82xx, where non-MMIO transfers will buffer into
354
period-size transfers, but the last transfer is usually
355
undersized and playback falls off the end of the submitted
358
snd_pcm_uframes_t boundary;
359
err = snd_pcm_sw_params_get_boundary(params,&boundary);
361
adebug("snd_pcm_sw_params_get_boundary() failed.\n");
364
err = snd_pcm_sw_params_set_silence_size(internal->pcm_handle, params, boundary);
366
adebug("snd_pcm_sw_params_set_silence_size() failed.\n");
371
/* commit the params structure to ALSA */
372
err = snd_pcm_sw_params(internal->pcm_handle, params);
374
adebug("snd_pcm_sw_params() failed.\n");
382
/* Devices declared in the alsa configuration will usually open
383
without error, even if there's no underlying hardware to support
384
them, eg, opening a 5.1 surround device on setero hardware. The
385
device won't 'fail' until there's an attempt to configure it. */
387
static inline int alsa_test_open(ao_device *device,
389
ao_sample_format *format)
391
ao_alsa_internal *internal = (ao_alsa_internal *) device->internal;
392
snd_pcm_hw_params_t *params;
395
adebug("Trying to open ALSA device '%s'\n",dev);
397
err = snd_pcm_open(&(internal->pcm_handle), dev,
398
SND_PCM_STREAM_PLAYBACK, 0);
401
adebug("Unable to open ALSA device '%s'\n",dev);
405
/* this is a hack and fragile if the exact device detection code
406
flow changes! Nevertheless, this is a useful warning for users.
407
Never fail silently if we can help it! */
408
if(!strcasecmp(dev,"default")){
410
if(device->output_channels>2){
411
awarn("ALSA 'default' device plays only channels 0,1.\n");
412
device->output_channels=2;
416
/* try to set up hw params */
417
err = alsa_set_hwparams(device,format);
419
adebug("Unable to open ALSA device '%s'\n",dev);
420
snd_pcm_close(internal->pcm_handle);
421
internal->pcm_handle = NULL;
425
/* try to set up sw params */
426
err = alsa_set_swparams(device);
428
adebug("Unable to open ALSA device '%s'\n",dev);
429
snd_pcm_close(internal->pcm_handle);
430
internal->pcm_handle = NULL;
438
/* prepare the audio device for playback */
117
439
int ao_plugin_open(ao_device *device, ao_sample_format *format)
119
ao_alsa_internal *internal = (ao_alsa_internal *) device->internal;
121
snd_pcm_channel_params_t param;
441
ao_alsa_internal *internal = (ao_alsa_internal *) device->internal;
124
memset(¶m, 0, sizeof(param));
126
param.channel = SND_PCM_CHANNEL_PLAYBACK;
127
param.mode = SND_PCM_MODE_BLOCK;
129
param.format.interleave = 1;
131
switch (format->bits) {
132
case 8 : param.format.format = SND_PCM_SFMT_S8;
134
case 16 : param.format.format =
135
device->client_byte_format == AO_FMT_BIG ?
136
SND_PCM_SFMT_S16_BE : SND_PCM_SFMT_S16_LE;
137
device->driver_byte_format = device->client_byte_format;
142
if (format->channels == 1 || format->channels == 2)
143
param.format.voices = format->channels;
147
/* Finish filling in the parameter structure */
148
param.format.rate = format->rate;
150
param.start_mode = SND_PCM_START_FULL;
152
param.stop_mode = SND_PCM_STOP_STOP;
154
param.buf.block.frag_size = internal->buf_size;
155
param.buf.block.frags_min = 1;
156
param.buf.block.frags_max = 8;
158
internal->buf = malloc(internal->buf_size);
159
internal->buf_end = 0;
160
if (internal->buf == NULL)
161
return 0; /* Could not alloc swap buffer */
444
/* Get the ALSA bitformat first to make sure it's valid */
445
err = alsa_get_sample_bitformat(format->bits,
446
device->client_byte_format == AO_FMT_BIG,device);
448
aerror("Invalid byte format\n");
452
internal->bitformat = err;
164
454
/* Open the ALSA device */
165
err = snd_pcm_open(&(internal->pcm_handle),
168
SND_PCM_OPEN_PLAYBACK | SND_PCM_OPEN_NONBLOCK);
174
err = snd_pcm_channel_params(internal->pcm_handle, ¶m);
177
snd_pcm_close(internal->pcm_handle);
182
snd_pcm_nonblock_mode(internal->pcm_handle, 0);
183
snd_pcm_channel_prepare(internal->pcm_handle,
184
SND_PCM_CHANNEL_PLAYBACK);
458
/* we don't try just 'default' as it's a plug device that
459
will accept any number of channels but usually plays back
460
everything as stereo. */
461
switch(device->output_channels){
465
err = alsa_test_open(device, tmp="surround71", format);
469
err = alsa_test_open(device, tmp="surround40", format);
473
err = alsa_test_open(device, tmp="surround51", format);
480
awarn("Unable to open surround playback. Trying default device...\n");
485
err = alsa_test_open(device, tmp="default", format);
487
internal->dev=strdup(tmp);
490
err = alsa_test_open(device, internal->dev, format);
493
aerror("Unable to open ALSA device '%s' for playback => %s\n",
494
internal->dev, snd_strerror(err));
498
adebug("Using ALSA device '%s'\n",internal->dev);
500
/* alsa's endinness will be the same as the application's */
501
if (format->bits > 8)
502
device->driver_byte_format = device->client_byte_format;
504
if(strcasecmp(internal->dev,"default")){
505
if(strncasecmp(internal->dev,"surround",8)){
506
if(device->output_channels>2 && device->verbose>=0){
507
awarn("No way to determine hardware %d channel mapping of\n"
508
"ALSA device '%s'.\n",device->output_channels, internal->dev);
509
if(device->inter_matrix){
510
free(device->inter_matrix);
511
device->inter_matrix=NULL;
190
int _alsa_write_buffer(ao_alsa_internal *s)
521
/* recover from an alsa exception */
522
static inline int alsa_error_recovery(ao_alsa_internal *internal, int err, ao_device *device)
192
snd_pcm_channel_status_t status;
193
snd_pcm_t *pcm_handle = s->pcm_handle;
194
int len = s->buf_end;
195
ssize_t written, snd_pcm_write_ret;
199
snd_pcm_write_ret = written = 0;
200
while ((snd_pcm_write_ret >= 0) && (written < len)) {
201
while ((snd_pcm_write_ret = snd_pcm_write(pcm_handle, s->buf, len)) == -EINTR)
203
if (snd_pcm_write_ret > 0)
204
written += snd_pcm_write_ret;
207
memset(&status, 0, sizeof(status));
208
if (snd_pcm_channel_status(pcm_handle, &status) < 0) {
209
fprintf(stderr, "ALSA: could not get channel status\n");
212
if (status.underrun) {
213
/* fprintf(stderr, "ALSA: underrun. resetting channel\n"); */
214
snd_pcm_channel_flush(pcm_handle, SND_PCM_CHANNEL_PLAYBACK);
215
snd_pcm_playback_prepare(pcm_handle);
216
snd_pcm_write(pcm_handle, s->buf, len);
217
if (snd_pcm_channel_status(pcm_handle, &status) < 0) {
218
fprintf(stderr, "ALSA: could not get channel status. giving up\n");
525
/* FIXME: underrun length detection */
526
adebug("underrun, restarting...\n");
527
/* output buffer underrun */
528
err = snd_pcm_prepare(internal->pcm_handle);
531
} else if (err == -ESTRPIPE) {
532
/* application was suspended, wait until suspend flag clears */
533
while ((err = snd_pcm_resume(internal->pcm_handle)) == -EAGAIN)
537
/* unable to wake up pcm device, restart it */
538
err = snd_pcm_prepare(internal->pcm_handle);
221
if (status.underrun) {
222
fprintf(stderr, "ALSA: write error. giving up\n");
545
/* error isn't recoverable */
550
/* play num_bytes of audio data */
231
551
int ao_plugin_play(ao_device *device, const char *output_samples,
232
552
uint_32 num_bytes)
234
554
ao_alsa_internal *internal = (ao_alsa_internal *) device->internal;
238
char *samples = (char *) output_samples;
241
while (packed < num_bytes && ok) {
242
/* Pack the buffer */
243
if (num_bytes-packed < internal->buf_size - internal->buf_end)
244
copy_len = num_bytes - packed;
246
copy_len = internal->buf_size - internal->buf_end;
248
memcpy(internal->buf + internal->buf_end, samples + packed,
251
internal->buf_end += copy_len;
253
if(internal->buf_end == internal->buf_size)
254
ok = _alsa_write_buffer(internal);
555
uint_32 len = num_bytes / internal->sample_size;
556
char *ptr = (char *) output_samples;
559
/* the entire buffer might not transfer at once */
561
/* try to write the entire buffer at once */
562
err = internal->writei(internal->pcm_handle, ptr, len);
564
/* no data transferred or interrupt signal */
565
if (err == -EAGAIN || err == -EINTR) {
570
/* this might be an error, or an exception */
571
err = alsa_error_recovery(internal, err, device);
573
aerror("write error: %s\n",
576
}else /* recovered, continue */
580
/* decrement the sample counter */
583
/* adjust the start pointer */
584
ptr += err * internal->sample_size;
591
/* close the audio device */
261
592
int ao_plugin_close(ao_device *device)
263
ao_alsa_internal *internal = (ao_alsa_internal *) device->internal;
267
result = _alsa_write_buffer(internal);
268
snd_pcm_close(internal->pcm_handle);
594
ao_alsa_internal *internal;
597
if ((internal = (ao_alsa_internal *) device->internal)) {
598
if (internal->pcm_handle) {
599
snd_pcm_drain(internal->pcm_handle);
600
snd_pcm_close(internal->pcm_handle);
601
internal->pcm_handle=NULL;
604
awarn("ao_plugin_close called with uninitialized ao_device->internal\n");
606
awarn("ao_plugin_close called with uninitialized ao_device\n");
612
/* free the internal data structures */
275
613
void ao_plugin_device_clear(ao_device *device)
277
ao_alsa_internal *internal = (ao_alsa_internal *) device->internal;
615
ao_alsa_internal *internal;
618
if ((internal = (ao_alsa_internal *) device->internal)) {
620
free (internal->dev);
622
awarn("ao_plugin_device_clear called with uninitialized ao_device->internal->dev\n");
624
free(device->internal);
626
awarn("ao_plugin_device_clear called with uninitialized ao_device->internal\n");
628
awarn("ao_plugin_device_clear called with uninitialized ao_device\n");