cleanup and bufferhandling fix by Joy Ping <joy at pingfm.org>. Bufferhandling fix based on idea by Marius David <marius at rohost.com>

git-svn-id: svn://svn.mplayerhq.hu/mplayer/trunk@6590 b3059339-0415-0410-9bf9-f77b7e298cf2
This commit is contained in:
alex 2002-06-28 16:48:10 +00:00
parent e9cf7c083b
commit 4f40ec312c
2 changed files with 486 additions and 352 deletions

View File

@ -3,16 +3,17 @@
(C) Alex Beregszaszi <alex@naxine.org> (C) Alex Beregszaszi <alex@naxine.org>
modified for better alsa-0.9.0beta8a-support by Joy Winter <joy@pingfm.org> modified for better alsa-0.9.0beta12(rc1)-support by Joy Winter <joy@pingfm.org>
additional AC3 passthrough support by Andy Lo A Foe <andy@alsaplayer.org> additional AC3 passthrough support by Andy Lo A Foe <andy@alsaplayer.org>
This driver is still at alpha stage.
If you want stable sound-support use the OSS emulation instead.
Any bugreports regarding to this driver are welcome either to the mplayer-user-mailinglist or directly to the authors. Any bugreports regarding to this driver are welcome either to the mplayer-user-mailinglist or directly to the authors.
*/ */
#include <errno.h> #include <errno.h>
#include <sys/time.h>
#include <stdlib.h>
//#include <unistd.h>
//#include <string.h>
#include "../config.h" #include "../config.h"
@ -46,13 +47,23 @@ static snd_pcm_format_t alsa_format;
static snd_pcm_hw_params_t *alsa_hwparams; static snd_pcm_hw_params_t *alsa_hwparams;
static snd_pcm_sw_params_t *alsa_swparams; static snd_pcm_sw_params_t *alsa_swparams;
static char *alsa_device; static char *alsa_device;
#define ALSA_DEVICE_SIZE 48
static int alsa_fragsize = 8192; /* possible 4096, original 8192 */ static int alsa_fragsize = OUTBURST; /* possible 4096, original 8192, OUTBURST is set statically to 512 in config.h but now its not used cause chunksize is allocated dynamically. */
static int alsa_fragcount = 8; static int alsa_fragcount = 8;
static int chunk_size = -1; static int chunk_size = -1;
static int start_delay = 1; static int buffer_size = 0;
static int start_delay = 0;
static int stop_delay = 0;
static size_t bits_per_sample, bits_per_frame;
static size_t chunk_bytes;
#define ALSA_DEVICE_SIZE 48
#define BUFFERTIME /* last undef */
#undef SET_PERIOD /* only function now is to set chunksize staticaly, last defined */
#define SW_PARAMS /* last undef */
snd_pcm_t * snd_pcm_t *
spdif_init(int acard, int adevice) spdif_init(int acard, int adevice)
@ -178,12 +189,6 @@ static int control(int cmd, int arg)
return(CONTROL_UNKNOWN); return(CONTROL_UNKNOWN);
} }
#undef start /* orig. undef */
#define buffsize
#define buffertime /* orig. undef? */
#define set_period
#define sw_params /* orig. undef */
#undef set_start_mode /* orig. undef */
/* /*
open & setup audio device open & setup audio device
@ -198,7 +203,7 @@ static int init(int rate_hz, int channels, int format, int flags)
size_t xfer_align; //new size_t xfer_align; //new
snd_pcm_uframes_t start_threshold, stop_threshold; //new snd_pcm_uframes_t start_threshold, stop_threshold; //new
printf("alsa-init: this driver is still at alpha-stage. if you want stable sound support use the OSS emulation instead.\n"); printf("alsa-init: testing and bugreports are welcome.\n");
printf("alsa-init: requested format: %d Hz, %d channels, %s\n", rate_hz, printf("alsa-init: requested format: %d Hz, %d channels, %s\n", rate_hz,
channels, audio_out_format_name(format)); channels, audio_out_format_name(format));
@ -265,26 +270,44 @@ static int init(int rate_hz, int channels, int format, int flags)
break; break;
} }
if (ao_subdevice != NULL) // ?? makes no sense
alsa_device = ao_subdevice;
if (alsa_device == NULL)
{
int tmp_device, tmp_subdevice, err;
if ((err = snd_pcm_info_malloc(&alsa_info)) < 0) if ((err = snd_pcm_info_malloc(&alsa_info)) < 0)
{ {
printf("alsa-init: memory allocation error: %s\n", snd_strerror(err)); printf("alsa-init: memory allocation error: %s\n", snd_strerror(err));
return(0); return(0);
} }
if (ao_subdevice != NULL) if ((alsa_device = alloca(ALSA_DEVICE_SIZE)) == NULL)
alsa_device = ao_subdevice;
if (alsa_device == NULL)
{
if ((alsa_device = malloc(ALSA_DEVICE_SIZE)) == NULL)
{ {
printf("alsa-init: memory allocation error: %s\n", strerror(errno)); printf("alsa-init: memory allocation error: %s\n", strerror(errno));
return(0); return(0);
} }
snprintf(alsa_device, ALSA_DEVICE_SIZE, "hw:%d,%d", if ((tmp_device = snd_pcm_info_get_device(alsa_info)) < 0)
snd_pcm_info_get_device(alsa_info), {
snd_pcm_info_get_subdevice(alsa_info)); printf("alsa-init: cant get device\n");
return(0);
}
if ((tmp_subdevice = snd_pcm_info_get_subdevice(alsa_info)) < 0)
{
printf("alsa-init: cant get subdevice\n");
return(0);
}
if (verbose)
printf("alsa-init: got device=%i, subdevice=%i\n", tmp_device, tmp_subdevice);
if ((err = snprintf(alsa_device, ALSA_DEVICE_SIZE, "hw:%1d,%1d", tmp_device, tmp_subdevice)) <= 0)
{
printf("alsa-init: cant wrote device-id\n");
}
snd_pcm_info_free(alsa_info); snd_pcm_info_free(alsa_info);
} }
@ -298,16 +321,17 @@ static int init(int rate_hz, int channels, int format, int flags)
} }
if (!alsa_handler) { if (!alsa_handler) {
if ((err = snd_pcm_open(&alsa_handler, alsa_device, SND_PCM_STREAM_PLAYBACK, if ((err = snd_pcm_open(&alsa_handler,alsa_device,SND_PCM_STREAM_PLAYBACK,0)) < 0)
0)) < 0)
{ {
printf("alsa-init: playback open error: %s\n", snd_strerror(err)); printf("alsa-init: playback open error: %s\n", snd_strerror(err));
return(0); return(0);
} }
} }
snd_pcm_hw_params_malloc(&alsa_hwparams); snd_pcm_hw_params_alloca(&alsa_hwparams);
snd_pcm_sw_params_alloca(&alsa_swparams); snd_pcm_sw_params_alloca(&alsa_swparams);
// setting hw-parameters
if ((err = snd_pcm_hw_params_any(alsa_handler, alsa_hwparams)) < 0) if ((err = snd_pcm_hw_params_any(alsa_handler, alsa_hwparams)) < 0)
{ {
printf("alsa-init: unable to get initial parameters: %s\n", printf("alsa-init: unable to get initial parameters: %s\n",
@ -340,7 +364,6 @@ static int init(int rate_hz, int channels, int format, int flags)
} }
if ((err = snd_pcm_hw_params_set_rate_near(alsa_handler, alsa_hwparams, ao_data.samplerate, 0)) < 0) if ((err = snd_pcm_hw_params_set_rate_near(alsa_handler, alsa_hwparams, ao_data.samplerate, 0)) < 0)
/* was originally only snd_pcm_hw_params_set_rate jp*/
{ {
printf("alsa-init: unable to set samplerate-2: %s\n", printf("alsa-init: unable to set samplerate-2: %s\n",
snd_strerror(err)); snd_strerror(err));
@ -348,37 +371,18 @@ static int init(int rate_hz, int channels, int format, int flags)
return(0); return(0);
} }
#ifdef set_period
#ifdef SET_PERIOD
{ {
if ((err = snd_pcm_hw_params_set_period_size(alsa_handler, alsa_hwparams, alsa_fragsize / 4, 0)) < 0) if ((err = snd_pcm_hw_params_set_period_size(alsa_handler, alsa_hwparams, alsa_fragsize, 0)) < 0)
{ {
printf("alsa-init: unable to set periodsize: %s\n", printf("alsa-init: unable to set periodsize: %s\n", snd_strerror(err));
snd_strerror(err));
return(0); return(0);
} }
if ((err = snd_pcm_hw_params_set_periods(alsa_handler, alsa_hwparams, alsa_fragcount, 0)) < 0)
{
printf("alsa-init: unable to set periods: %s\n",
snd_strerror(err));
return(0);
}
}
#endif
#ifdef buffsize
if ((err = snd_pcm_hw_params_get_buffer_size(alsa_hwparams)) < 0)
{
printf("alsa-init: unable to get buffer size: %s\n",
snd_strerror(err));
return(0);
} else
{
ao_data.buffersize = err;
if (verbose)
printf("alsa-init: got buffersize %i\n", ao_data.buffersize);
} }
#endif #endif
#ifdef buffertime #ifdef BUFFERTIME
{ {
int alsa_buffer_time = 500000; /* original 60 */ int alsa_buffer_time = 500000; /* original 60 */
@ -397,105 +401,150 @@ static int init(int rate_hz, int channels, int format, int flags)
snd_strerror(err)); snd_strerror(err));
return(0); return(0);
} }
printf("alsa-init: buffer_time: %d, period_time :%d\n", if (verbose)
alsa_buffer_time, err); printf("alsa-init: buffer_time: %d, period_time :%d\n",alsa_buffer_time, err);
} }
#endif #endif
if ((err = snd_pcm_hw_params(alsa_handler, alsa_hwparams)) < 0) /* get chunk-size */
if ((err = snd_pcm_hw_params_get_period_size(alsa_hwparams, 0)) < 0)
{ {
printf("alsa-init: unable to set parameters: %s\n", printf("alsa-init: unable to get chunk-size in hw-params: %s\n", snd_strerror(err));
snd_strerror(err)); return(0);
} else
{
chunk_size = err;
if (verbose) {printf("alsa-init: got chunksize %i\n", chunk_size);}
}
/* get buffer size */
if ((err = snd_pcm_hw_params_get_buffer_size(alsa_hwparams)) < 0)
{
printf("alsa-init: unable to get buffersize in hw-params: %s\n", snd_strerror(err));
return(0);
} else
{
ao_data.buffersize = err;
if (verbose) {printf("alsa-init: got buffersize %i\n", ao_data.buffersize);}
}
if (MAX_OUTBURST > ao_data.buffersize) { //warning if MAX_OUTBURST is bigger than buffersize
printf("alsa-init: WARNING! MAX_OUTBURST exceeds your available buffersize.\nalsa-init: MAX_OUTBURST=%i, buffersize=%i\n",MAX_OUTBURST,ao_data.buffersize);}
if (chunk_size == ao_data.buffersize)
{
printf("alsa-init: Can't use period equal to buffer size (%u == %lu)", chunk_size, (long)buffer_size);
return(0); return(0);
} }
#ifdef sw_params /* finally install hardware parameters */
if ((err = snd_pcm_hw_params(alsa_handler, alsa_hwparams)) < 0)
{ {
chunk_size = snd_pcm_hw_params_get_period_size(alsa_hwparams, 0); printf("alsa-init: unable to set hw-parameters: %s\n",
start_threshold = (double) ao_data.samplerate * start_delay / 1000000; snd_strerror(err));
return(0);
}
// end setting hw-params
#ifdef SW_PARAMS
{
size_t n;
xfer_align = snd_pcm_sw_params_get_xfer_align(alsa_swparams); xfer_align = snd_pcm_sw_params_get_xfer_align(alsa_swparams);
if (xfer_align == 0)
xfer_align = 4;
n = (ao_data.buffersize / xfer_align) * xfer_align;
if (start_delay <= 0) {
start_threshold = n + (double) ao_data.samplerate * start_delay / 1000000;
} else {
start_threshold = (double) ao_data.samplerate * start_delay / 1000000;
}
if (start_threshold < 1)
start_threshold = 1;
if (start_threshold > n)
start_threshold = n;
if (stop_delay <= 0) {
stop_threshold = ao_data.buffersize + (double) ao_data.samplerate * stop_delay / 1000000;
} else {
stop_threshold = (double) ao_data.samplerate * stop_delay / 1000000;
}
if (verbose) {
printf("alsa-init: start_threshold=%lu, stop_threshold=%lu\n",start_threshold,stop_threshold);
printf("alsa-init: n=%i\n", n);
}
if ((err = snd_pcm_sw_params_current(alsa_handler, alsa_swparams)) < 0) if ((err = snd_pcm_sw_params_current(alsa_handler, alsa_swparams)) < 0)
{ {
printf("alsa-init: unable to get parameters: %s\n", printf("alsa-init: unable to get parameters: %s\n",snd_strerror(err));
snd_strerror(err));
return(0); return(0);
} }
if ((err = snd_pcm_sw_params_set_avail_min(alsa_handler, alsa_swparams, chunk_size)) < 0) //set min available frames to consider pcm ready (4)
if ((err = snd_pcm_sw_params_set_avail_min(alsa_handler, alsa_swparams, 4)) < 0)
{ {
printf("alsa-init: unable to set avail_min %s\n",snd_strerror(err)); printf("alsa-init: unable to set avail_min %s\n",snd_strerror(err));
return(0); return(0);
} }
if ((err = snd_pcm_sw_params_set_start_threshold(alsa_handler, alsa_swparams, start_threshold)) < 0) if ((err = snd_pcm_sw_params_set_start_threshold(alsa_handler, alsa_swparams, start_threshold)) < 0)
{ {
printf("alsa-init: unable to set start_threshold %s\n",snd_strerror(err)); printf("alsa-init: unable to set start_threshold %s\n",snd_strerror(err));
return(0); return(0);
} }
}
// if ((err = snd_pcm_sw_params_set_xfer_align(alsa_handler, alsa_swparams, xfer_align)) < 0)
//{
// printf("alsa-init: unable to set xfer_align: %s\n",
// snd_strerror(err));
// return(0);
//}
#ifdef set_start_mode if ((err = snd_pcm_sw_params_set_stop_threshold(alsa_handler, alsa_swparams, stop_threshold)) < 0)
if ((err = snd_pcm_sw_params_set_start_mode(alsa_handler, alsa_swparams,
SND_PCM_START_DATA)) < 0)
{ {
printf("alsa-init: unable to set start mode: %s\n", printf("alsa-init: unable to set stop_threshold %s\n",snd_strerror(err));
snd_strerror(err)); return(0);
}
//transfers stream aligned to 4 in nonblocking-mode it would be 1
if ((err = snd_pcm_sw_params_set_xfer_align(alsa_handler, alsa_swparams, 4)) < 0)
{
printf("alsa-init: unable to set xfer_align: %s\n",snd_strerror(err));
return(0); return(0);
} }
#endif
if ((err = snd_pcm_sw_params(alsa_handler, alsa_swparams)) < 0) if ((err = snd_pcm_sw_params(alsa_handler, alsa_swparams)) < 0)
{ {
printf("alsa-init: unable to set parameters: %s\n", printf("alsa-init: unable to install sw-params\n");
snd_strerror(err));
return(0); return(0);
} }
// snd_pcm_sw_params_default(alsa_handler, alsa_swparams); bits_per_sample = snd_pcm_format_physical_width(alsa_format);
#endif bits_per_frame = bits_per_sample * channels;
chunk_bytes = chunk_size * bits_per_frame / 8;
if (verbose) {
printf("alsa-init: bits per sample (bps)=%i, bits per frame (bpf)=%i, chunk_bytes=%i\n",bits_per_sample,bits_per_frame,chunk_bytes);}
}
#endif //end swparams
if ((err = snd_pcm_prepare(alsa_handler)) < 0) if ((err = snd_pcm_prepare(alsa_handler)) < 0)
{ {
printf("alsa-init: pcm prepare error: %s\n", snd_strerror(err)); printf("alsa-init: pcm prepare error: %s\n", snd_strerror(err));
return(0); return(0);
} }
#ifdef start printf("alsa9: %d Hz/%d channels/%d bpf/%d bytes buffer/%s\n",
if ((err = snd_pcm_start(alsa_handler)) < 0)
{
printf("alsa-init: pcm start error: %s\n", snd_strerror(err));
if (err != -EPIPE)
return(0);
if ((err = snd_pcm_start(alsa_handler)) < 0)
{
printf("alsa-init: pcm start error: %s\n", snd_strerror(err));
return(0);
}
}
#endif
printf("AUDIO: %d Hz/%d channels/%d bpf/%d bytes buffer/%s\n",
ao_data.samplerate, ao_data.channels, ao_data.bps, ao_data.buffersize, ao_data.samplerate, ao_data.channels, ao_data.bps, ao_data.buffersize,
snd_pcm_format_description(alsa_format)); snd_pcm_format_description(alsa_format));
return(1); return(1);
} } // end init
/* close audio device */ /* close audio device */
static void uninit() static void uninit()
{ {
int err; int err;
if (alsa_device != NULL) //if (alsa_device != NULL)
free(alsa_device); //free(alsa_device);
snd_pcm_hw_params_free(alsa_hwparams); //snd_pcm_hw_params_free(alsa_hwparams);
if ((err = snd_pcm_drain(alsa_handler)) < 0) if ((err = snd_pcm_drain(alsa_handler)) < 0)
{ {
@ -503,19 +552,16 @@ static void uninit()
return; return;
} }
#ifdef start
if ((err = snd_pcm_reset(alsa_handler)) < 0)
{
printf("alsa-uninit: pcm reset error: %s\n", snd_strerror(err));
return;
}
#endif
if ((err = snd_pcm_close(alsa_handler)) < 0) if ((err = snd_pcm_close(alsa_handler)) < 0)
{ {
printf("alsa-uninit: pcm close error: %s\n", snd_strerror(err)); printf("alsa-uninit: pcm close error: %s\n", snd_strerror(err));
return; return;
} }
else {
alsa_handler = NULL;
alsa_device = NULL;
printf("alsa-uninit: pcm closed\n");
}
} }
static void audio_pause() static void audio_pause()
@ -546,14 +592,6 @@ static void audio_resume()
printf("alsa-resume: pcm prepare error: %s\n", snd_strerror(err)); printf("alsa-resume: pcm prepare error: %s\n", snd_strerror(err));
return; return;
} }
#ifdef start
if ((err = snd_pcm_start(alsa_handler)) < 0)
{
printf("alsa-resume: pcm start error: %s\n", snd_strerror(err));
return;
}
#endif
} }
/* stop playing and empty buffers (for seeking/pause) */ /* stop playing and empty buffers (for seeking/pause) */
@ -567,62 +605,90 @@ static void reset()
return; return;
} }
#ifdef start
if ((err = snd_pcm_reset(alsa_handler)) < 0)
{
printf("alsa-reset: pcm reset error: %s\n", snd_strerror(err));
return;
}
#endif
if ((err = snd_pcm_prepare(alsa_handler)) < 0) if ((err = snd_pcm_prepare(alsa_handler)) < 0)
{ {
printf("alsa-reset: pcm prepare error: %s\n", snd_strerror(err)); printf("alsa-reset: pcm prepare error: %s\n", snd_strerror(err));
return; return;
} }
#ifdef start
if ((err = snd_pcm_start(alsa_handler)) < 0)
{
printf("alsa-reset: pcm start error: %s\n", snd_strerror(err));
return;
} }
#ifndef timersub
#define timersub(a, b, result) \
do { \
(result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
(result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
if ((result)->tv_usec < 0) { \
--(result)->tv_sec; \
(result)->tv_usec += 1000000; \
} \
} while (0)
#endif #endif
}
/* /*
plays 'len' bytes of 'data' plays 'len' bytes of 'data'
returns: number of bytes played returns: number of bytes played
modified last at 26.06.02 by jp
*/ */
static int play(void* data, int len, int flags) static int play(void* data, int len, int flags)
{ {
int got_len;
got_len = snd_pcm_writei(alsa_handler, data, len / 4); snd_pcm_status_t *status;
//if ((got_len = snd_pcm_writei(alsa_handler, data, (len/ao_data.bps))) != (len/ao_data.bps)) { int num_frames=len/ao_data.bps;
//SHOULD BE FIXED signed short *output_samples=data;
if (got_len == -EPIPE) /* underrun? */ snd_pcm_sframes_t res = 0;
{
printf("alsa-play: alsa underrun, resetting stream\n"); if (!alsa_handler) {
if ((got_len = snd_pcm_prepare(alsa_handler)) < 0) printf("alsa-play: device configuration error");
{ return 0;
printf("alsa-play: playback prepare error: %s\n", snd_strerror(got_len));
return(0);
} }
if ((got_len = snd_pcm_writei(alsa_handler, data, (len/ao_data.bps))) != (len/ao_data.bps))
{ do {
printf("alsa-play: write error after reset: %s - giving up\n", if (res == -EPIPE) { /* underrun */
snd_strerror(got_len)); snd_pcm_status_alloca(&status);
return(0); if ((res = snd_pcm_status(alsa_handler, status))<0) {
printf("alsa-play: buffer underrun. can't determine length");
} else {
if (snd_pcm_status_get_state(status) == SND_PCM_STATE_XRUN) {
struct timeval now, diff, tstamp;
gettimeofday(&now, 0);
snd_pcm_status_get_trigger_tstamp(status, &tstamp);
timersub(&now, &tstamp, &diff);
printf("alsa-play: xrun of at least %.3f msecs. resetting stream",
diff.tv_sec * 1000 + diff.tv_usec / 1000.0);
} else
printf("alsa-play: xrun. can't determine length");
} }
return(len); /* 2nd write was ok */ res = snd_pcm_prepare(alsa_handler);
} }
return(len); else if (res == -ESTRPIPE) { /* suspend */
//} printf("alsa-play: pcm in suspend mode. trying to resume");
while ((res = snd_pcm_resume(alsa_handler)) == -EAGAIN)
sleep(1);
if (res < 0)
res = snd_pcm_prepare(alsa_handler);
} }
if (res >= 0)
res = snd_pcm_writei(alsa_handler, (void *)output_samples, num_frames);
if (res > 0) {
output_samples += ao_data.channels * res;
num_frames -= res;
}
} while (res == -EPIPE || num_frames > 0);
if (res < 0) {
printf("alsa-play: write error %s", snd_strerror(res));
return 0;
}
return res < 0 ? (int)res : len;
}
/* how many byes are free in the buffer */ /* how many byes are free in the buffer */
static int get_space() static int get_space()
{ {
@ -656,6 +722,7 @@ static int get_space()
if (ret < 0) if (ret < 0)
ret = 0; ret = 0;
//printf("alsa-space: free space = %i",ret);
return(ret); return(ret);
} }

View File

@ -3,16 +3,17 @@
(C) Alex Beregszaszi <alex@naxine.org> (C) Alex Beregszaszi <alex@naxine.org>
modified for better alsa-0.9.0beta8a-support by Joy Winter <joy@pingfm.org> modified for better alsa-0.9.0beta12(rc1)-support by Joy Winter <joy@pingfm.org>
additional AC3 passthrough support by Andy Lo A Foe <andy@alsaplayer.org> additional AC3 passthrough support by Andy Lo A Foe <andy@alsaplayer.org>
This driver is still at alpha stage.
If you want stable sound-support use the OSS emulation instead.
Any bugreports regarding to this driver are welcome either to the mplayer-user-mailinglist or directly to the authors. Any bugreports regarding to this driver are welcome either to the mplayer-user-mailinglist or directly to the authors.
*/ */
#include <errno.h> #include <errno.h>
#include <sys/time.h>
#include <stdlib.h>
//#include <unistd.h>
//#include <string.h>
#include "../config.h" #include "../config.h"
@ -46,13 +47,23 @@ static snd_pcm_format_t alsa_format;
static snd_pcm_hw_params_t *alsa_hwparams; static snd_pcm_hw_params_t *alsa_hwparams;
static snd_pcm_sw_params_t *alsa_swparams; static snd_pcm_sw_params_t *alsa_swparams;
static char *alsa_device; static char *alsa_device;
#define ALSA_DEVICE_SIZE 48
static int alsa_fragsize = 8192; /* possible 4096, original 8192 */ static int alsa_fragsize = OUTBURST; /* possible 4096, original 8192, OUTBURST is set statically to 512 in config.h but now its not used cause chunksize is allocated dynamically. */
static int alsa_fragcount = 8; static int alsa_fragcount = 8;
static int chunk_size = -1; static int chunk_size = -1;
static int start_delay = 1; static int buffer_size = 0;
static int start_delay = 0;
static int stop_delay = 0;
static size_t bits_per_sample, bits_per_frame;
static size_t chunk_bytes;
#define ALSA_DEVICE_SIZE 48
#define BUFFERTIME /* last undef */
#undef SET_PERIOD /* only function now is to set chunksize staticaly, last defined */
#define SW_PARAMS /* last undef */
snd_pcm_t * snd_pcm_t *
spdif_init(int acard, int adevice) spdif_init(int acard, int adevice)
@ -178,12 +189,6 @@ static int control(int cmd, int arg)
return(CONTROL_UNKNOWN); return(CONTROL_UNKNOWN);
} }
#undef start /* orig. undef */
#define buffsize
#define buffertime /* orig. undef? */
#define set_period
#define sw_params /* orig. undef */
#undef set_start_mode /* orig. undef */
/* /*
open & setup audio device open & setup audio device
@ -198,7 +203,7 @@ static int init(int rate_hz, int channels, int format, int flags)
size_t xfer_align; //new size_t xfer_align; //new
snd_pcm_uframes_t start_threshold, stop_threshold; //new snd_pcm_uframes_t start_threshold, stop_threshold; //new
printf("alsa-init: this driver is still at alpha-stage. if you want stable sound support use the OSS emulation instead.\n"); printf("alsa-init: testing and bugreports are welcome.\n");
printf("alsa-init: requested format: %d Hz, %d channels, %s\n", rate_hz, printf("alsa-init: requested format: %d Hz, %d channels, %s\n", rate_hz,
channels, audio_out_format_name(format)); channels, audio_out_format_name(format));
@ -265,26 +270,44 @@ static int init(int rate_hz, int channels, int format, int flags)
break; break;
} }
if (ao_subdevice != NULL) // ?? makes no sense
alsa_device = ao_subdevice;
if (alsa_device == NULL)
{
int tmp_device, tmp_subdevice, err;
if ((err = snd_pcm_info_malloc(&alsa_info)) < 0) if ((err = snd_pcm_info_malloc(&alsa_info)) < 0)
{ {
printf("alsa-init: memory allocation error: %s\n", snd_strerror(err)); printf("alsa-init: memory allocation error: %s\n", snd_strerror(err));
return(0); return(0);
} }
if (ao_subdevice != NULL) if ((alsa_device = alloca(ALSA_DEVICE_SIZE)) == NULL)
alsa_device = ao_subdevice;
if (alsa_device == NULL)
{
if ((alsa_device = malloc(ALSA_DEVICE_SIZE)) == NULL)
{ {
printf("alsa-init: memory allocation error: %s\n", strerror(errno)); printf("alsa-init: memory allocation error: %s\n", strerror(errno));
return(0); return(0);
} }
snprintf(alsa_device, ALSA_DEVICE_SIZE, "hw:%d,%d", if ((tmp_device = snd_pcm_info_get_device(alsa_info)) < 0)
snd_pcm_info_get_device(alsa_info), {
snd_pcm_info_get_subdevice(alsa_info)); printf("alsa-init: cant get device\n");
return(0);
}
if ((tmp_subdevice = snd_pcm_info_get_subdevice(alsa_info)) < 0)
{
printf("alsa-init: cant get subdevice\n");
return(0);
}
if (verbose)
printf("alsa-init: got device=%i, subdevice=%i\n", tmp_device, tmp_subdevice);
if ((err = snprintf(alsa_device, ALSA_DEVICE_SIZE, "hw:%1d,%1d", tmp_device, tmp_subdevice)) <= 0)
{
printf("alsa-init: cant wrote device-id\n");
}
snd_pcm_info_free(alsa_info); snd_pcm_info_free(alsa_info);
} }
@ -298,16 +321,17 @@ static int init(int rate_hz, int channels, int format, int flags)
} }
if (!alsa_handler) { if (!alsa_handler) {
if ((err = snd_pcm_open(&alsa_handler, alsa_device, SND_PCM_STREAM_PLAYBACK, if ((err = snd_pcm_open(&alsa_handler,alsa_device,SND_PCM_STREAM_PLAYBACK,0)) < 0)
0)) < 0)
{ {
printf("alsa-init: playback open error: %s\n", snd_strerror(err)); printf("alsa-init: playback open error: %s\n", snd_strerror(err));
return(0); return(0);
} }
} }
snd_pcm_hw_params_malloc(&alsa_hwparams); snd_pcm_hw_params_alloca(&alsa_hwparams);
snd_pcm_sw_params_alloca(&alsa_swparams); snd_pcm_sw_params_alloca(&alsa_swparams);
// setting hw-parameters
if ((err = snd_pcm_hw_params_any(alsa_handler, alsa_hwparams)) < 0) if ((err = snd_pcm_hw_params_any(alsa_handler, alsa_hwparams)) < 0)
{ {
printf("alsa-init: unable to get initial parameters: %s\n", printf("alsa-init: unable to get initial parameters: %s\n",
@ -340,7 +364,6 @@ static int init(int rate_hz, int channels, int format, int flags)
} }
if ((err = snd_pcm_hw_params_set_rate_near(alsa_handler, alsa_hwparams, ao_data.samplerate, 0)) < 0) if ((err = snd_pcm_hw_params_set_rate_near(alsa_handler, alsa_hwparams, ao_data.samplerate, 0)) < 0)
/* was originally only snd_pcm_hw_params_set_rate jp*/
{ {
printf("alsa-init: unable to set samplerate-2: %s\n", printf("alsa-init: unable to set samplerate-2: %s\n",
snd_strerror(err)); snd_strerror(err));
@ -348,37 +371,18 @@ static int init(int rate_hz, int channels, int format, int flags)
return(0); return(0);
} }
#ifdef set_period
#ifdef SET_PERIOD
{ {
if ((err = snd_pcm_hw_params_set_period_size(alsa_handler, alsa_hwparams, alsa_fragsize / 4, 0)) < 0) if ((err = snd_pcm_hw_params_set_period_size(alsa_handler, alsa_hwparams, alsa_fragsize, 0)) < 0)
{ {
printf("alsa-init: unable to set periodsize: %s\n", printf("alsa-init: unable to set periodsize: %s\n", snd_strerror(err));
snd_strerror(err));
return(0); return(0);
} }
if ((err = snd_pcm_hw_params_set_periods(alsa_handler, alsa_hwparams, alsa_fragcount, 0)) < 0)
{
printf("alsa-init: unable to set periods: %s\n",
snd_strerror(err));
return(0);
}
}
#endif
#ifdef buffsize
if ((err = snd_pcm_hw_params_get_buffer_size(alsa_hwparams)) < 0)
{
printf("alsa-init: unable to get buffer size: %s\n",
snd_strerror(err));
return(0);
} else
{
ao_data.buffersize = err;
if (verbose)
printf("alsa-init: got buffersize %i\n", ao_data.buffersize);
} }
#endif #endif
#ifdef buffertime #ifdef BUFFERTIME
{ {
int alsa_buffer_time = 500000; /* original 60 */ int alsa_buffer_time = 500000; /* original 60 */
@ -397,105 +401,150 @@ static int init(int rate_hz, int channels, int format, int flags)
snd_strerror(err)); snd_strerror(err));
return(0); return(0);
} }
printf("alsa-init: buffer_time: %d, period_time :%d\n", if (verbose)
alsa_buffer_time, err); printf("alsa-init: buffer_time: %d, period_time :%d\n",alsa_buffer_time, err);
} }
#endif #endif
if ((err = snd_pcm_hw_params(alsa_handler, alsa_hwparams)) < 0) /* get chunk-size */
if ((err = snd_pcm_hw_params_get_period_size(alsa_hwparams, 0)) < 0)
{ {
printf("alsa-init: unable to set parameters: %s\n", printf("alsa-init: unable to get chunk-size in hw-params: %s\n", snd_strerror(err));
snd_strerror(err)); return(0);
} else
{
chunk_size = err;
if (verbose) {printf("alsa-init: got chunksize %i\n", chunk_size);}
}
/* get buffer size */
if ((err = snd_pcm_hw_params_get_buffer_size(alsa_hwparams)) < 0)
{
printf("alsa-init: unable to get buffersize in hw-params: %s\n", snd_strerror(err));
return(0);
} else
{
ao_data.buffersize = err;
if (verbose) {printf("alsa-init: got buffersize %i\n", ao_data.buffersize);}
}
if (MAX_OUTBURST > ao_data.buffersize) { //warning if MAX_OUTBURST is bigger than buffersize
printf("alsa-init: WARNING! MAX_OUTBURST exceeds your available buffersize.\nalsa-init: MAX_OUTBURST=%i, buffersize=%i\n",MAX_OUTBURST,ao_data.buffersize);}
if (chunk_size == ao_data.buffersize)
{
printf("alsa-init: Can't use period equal to buffer size (%u == %lu)", chunk_size, (long)buffer_size);
return(0); return(0);
} }
#ifdef sw_params /* finally install hardware parameters */
if ((err = snd_pcm_hw_params(alsa_handler, alsa_hwparams)) < 0)
{ {
chunk_size = snd_pcm_hw_params_get_period_size(alsa_hwparams, 0); printf("alsa-init: unable to set hw-parameters: %s\n",
start_threshold = (double) ao_data.samplerate * start_delay / 1000000; snd_strerror(err));
return(0);
}
// end setting hw-params
#ifdef SW_PARAMS
{
size_t n;
xfer_align = snd_pcm_sw_params_get_xfer_align(alsa_swparams); xfer_align = snd_pcm_sw_params_get_xfer_align(alsa_swparams);
if (xfer_align == 0)
xfer_align = 4;
n = (ao_data.buffersize / xfer_align) * xfer_align;
if (start_delay <= 0) {
start_threshold = n + (double) ao_data.samplerate * start_delay / 1000000;
} else {
start_threshold = (double) ao_data.samplerate * start_delay / 1000000;
}
if (start_threshold < 1)
start_threshold = 1;
if (start_threshold > n)
start_threshold = n;
if (stop_delay <= 0) {
stop_threshold = ao_data.buffersize + (double) ao_data.samplerate * stop_delay / 1000000;
} else {
stop_threshold = (double) ao_data.samplerate * stop_delay / 1000000;
}
if (verbose) {
printf("alsa-init: start_threshold=%lu, stop_threshold=%lu\n",start_threshold,stop_threshold);
printf("alsa-init: n=%i\n", n);
}
if ((err = snd_pcm_sw_params_current(alsa_handler, alsa_swparams)) < 0) if ((err = snd_pcm_sw_params_current(alsa_handler, alsa_swparams)) < 0)
{ {
printf("alsa-init: unable to get parameters: %s\n", printf("alsa-init: unable to get parameters: %s\n",snd_strerror(err));
snd_strerror(err));
return(0); return(0);
} }
if ((err = snd_pcm_sw_params_set_avail_min(alsa_handler, alsa_swparams, chunk_size)) < 0) //set min available frames to consider pcm ready (4)
if ((err = snd_pcm_sw_params_set_avail_min(alsa_handler, alsa_swparams, 4)) < 0)
{ {
printf("alsa-init: unable to set avail_min %s\n",snd_strerror(err)); printf("alsa-init: unable to set avail_min %s\n",snd_strerror(err));
return(0); return(0);
} }
if ((err = snd_pcm_sw_params_set_start_threshold(alsa_handler, alsa_swparams, start_threshold)) < 0) if ((err = snd_pcm_sw_params_set_start_threshold(alsa_handler, alsa_swparams, start_threshold)) < 0)
{ {
printf("alsa-init: unable to set start_threshold %s\n",snd_strerror(err)); printf("alsa-init: unable to set start_threshold %s\n",snd_strerror(err));
return(0); return(0);
} }
}
// if ((err = snd_pcm_sw_params_set_xfer_align(alsa_handler, alsa_swparams, xfer_align)) < 0)
//{
// printf("alsa-init: unable to set xfer_align: %s\n",
// snd_strerror(err));
// return(0);
//}
#ifdef set_start_mode if ((err = snd_pcm_sw_params_set_stop_threshold(alsa_handler, alsa_swparams, stop_threshold)) < 0)
if ((err = snd_pcm_sw_params_set_start_mode(alsa_handler, alsa_swparams,
SND_PCM_START_DATA)) < 0)
{ {
printf("alsa-init: unable to set start mode: %s\n", printf("alsa-init: unable to set stop_threshold %s\n",snd_strerror(err));
snd_strerror(err)); return(0);
}
//transfers stream aligned to 4 in nonblocking-mode it would be 1
if ((err = snd_pcm_sw_params_set_xfer_align(alsa_handler, alsa_swparams, 4)) < 0)
{
printf("alsa-init: unable to set xfer_align: %s\n",snd_strerror(err));
return(0); return(0);
} }
#endif
if ((err = snd_pcm_sw_params(alsa_handler, alsa_swparams)) < 0) if ((err = snd_pcm_sw_params(alsa_handler, alsa_swparams)) < 0)
{ {
printf("alsa-init: unable to set parameters: %s\n", printf("alsa-init: unable to install sw-params\n");
snd_strerror(err));
return(0); return(0);
} }
// snd_pcm_sw_params_default(alsa_handler, alsa_swparams); bits_per_sample = snd_pcm_format_physical_width(alsa_format);
#endif bits_per_frame = bits_per_sample * channels;
chunk_bytes = chunk_size * bits_per_frame / 8;
if (verbose) {
printf("alsa-init: bits per sample (bps)=%i, bits per frame (bpf)=%i, chunk_bytes=%i\n",bits_per_sample,bits_per_frame,chunk_bytes);}
}
#endif //end swparams
if ((err = snd_pcm_prepare(alsa_handler)) < 0) if ((err = snd_pcm_prepare(alsa_handler)) < 0)
{ {
printf("alsa-init: pcm prepare error: %s\n", snd_strerror(err)); printf("alsa-init: pcm prepare error: %s\n", snd_strerror(err));
return(0); return(0);
} }
#ifdef start printf("alsa9: %d Hz/%d channels/%d bpf/%d bytes buffer/%s\n",
if ((err = snd_pcm_start(alsa_handler)) < 0)
{
printf("alsa-init: pcm start error: %s\n", snd_strerror(err));
if (err != -EPIPE)
return(0);
if ((err = snd_pcm_start(alsa_handler)) < 0)
{
printf("alsa-init: pcm start error: %s\n", snd_strerror(err));
return(0);
}
}
#endif
printf("AUDIO: %d Hz/%d channels/%d bpf/%d bytes buffer/%s\n",
ao_data.samplerate, ao_data.channels, ao_data.bps, ao_data.buffersize, ao_data.samplerate, ao_data.channels, ao_data.bps, ao_data.buffersize,
snd_pcm_format_description(alsa_format)); snd_pcm_format_description(alsa_format));
return(1); return(1);
} } // end init
/* close audio device */ /* close audio device */
static void uninit() static void uninit()
{ {
int err; int err;
if (alsa_device != NULL) //if (alsa_device != NULL)
free(alsa_device); //free(alsa_device);
snd_pcm_hw_params_free(alsa_hwparams); //snd_pcm_hw_params_free(alsa_hwparams);
if ((err = snd_pcm_drain(alsa_handler)) < 0) if ((err = snd_pcm_drain(alsa_handler)) < 0)
{ {
@ -503,19 +552,16 @@ static void uninit()
return; return;
} }
#ifdef start
if ((err = snd_pcm_reset(alsa_handler)) < 0)
{
printf("alsa-uninit: pcm reset error: %s\n", snd_strerror(err));
return;
}
#endif
if ((err = snd_pcm_close(alsa_handler)) < 0) if ((err = snd_pcm_close(alsa_handler)) < 0)
{ {
printf("alsa-uninit: pcm close error: %s\n", snd_strerror(err)); printf("alsa-uninit: pcm close error: %s\n", snd_strerror(err));
return; return;
} }
else {
alsa_handler = NULL;
alsa_device = NULL;
printf("alsa-uninit: pcm closed\n");
}
} }
static void audio_pause() static void audio_pause()
@ -546,14 +592,6 @@ static void audio_resume()
printf("alsa-resume: pcm prepare error: %s\n", snd_strerror(err)); printf("alsa-resume: pcm prepare error: %s\n", snd_strerror(err));
return; return;
} }
#ifdef start
if ((err = snd_pcm_start(alsa_handler)) < 0)
{
printf("alsa-resume: pcm start error: %s\n", snd_strerror(err));
return;
}
#endif
} }
/* stop playing and empty buffers (for seeking/pause) */ /* stop playing and empty buffers (for seeking/pause) */
@ -567,62 +605,90 @@ static void reset()
return; return;
} }
#ifdef start
if ((err = snd_pcm_reset(alsa_handler)) < 0)
{
printf("alsa-reset: pcm reset error: %s\n", snd_strerror(err));
return;
}
#endif
if ((err = snd_pcm_prepare(alsa_handler)) < 0) if ((err = snd_pcm_prepare(alsa_handler)) < 0)
{ {
printf("alsa-reset: pcm prepare error: %s\n", snd_strerror(err)); printf("alsa-reset: pcm prepare error: %s\n", snd_strerror(err));
return; return;
} }
#ifdef start
if ((err = snd_pcm_start(alsa_handler)) < 0)
{
printf("alsa-reset: pcm start error: %s\n", snd_strerror(err));
return;
} }
#ifndef timersub
#define timersub(a, b, result) \
do { \
(result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
(result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
if ((result)->tv_usec < 0) { \
--(result)->tv_sec; \
(result)->tv_usec += 1000000; \
} \
} while (0)
#endif #endif
}
/* /*
plays 'len' bytes of 'data' plays 'len' bytes of 'data'
returns: number of bytes played returns: number of bytes played
modified last at 26.06.02 by jp
*/ */
static int play(void* data, int len, int flags) static int play(void* data, int len, int flags)
{ {
int got_len;
got_len = snd_pcm_writei(alsa_handler, data, len / 4); snd_pcm_status_t *status;
//if ((got_len = snd_pcm_writei(alsa_handler, data, (len/ao_data.bps))) != (len/ao_data.bps)) { int num_frames=len/ao_data.bps;
//SHOULD BE FIXED signed short *output_samples=data;
if (got_len == -EPIPE) /* underrun? */ snd_pcm_sframes_t res = 0;
{
printf("alsa-play: alsa underrun, resetting stream\n"); if (!alsa_handler) {
if ((got_len = snd_pcm_prepare(alsa_handler)) < 0) printf("alsa-play: device configuration error");
{ return 0;
printf("alsa-play: playback prepare error: %s\n", snd_strerror(got_len));
return(0);
} }
if ((got_len = snd_pcm_writei(alsa_handler, data, (len/ao_data.bps))) != (len/ao_data.bps))
{ do {
printf("alsa-play: write error after reset: %s - giving up\n", if (res == -EPIPE) { /* underrun */
snd_strerror(got_len)); snd_pcm_status_alloca(&status);
return(0); if ((res = snd_pcm_status(alsa_handler, status))<0) {
printf("alsa-play: buffer underrun. can't determine length");
} else {
if (snd_pcm_status_get_state(status) == SND_PCM_STATE_XRUN) {
struct timeval now, diff, tstamp;
gettimeofday(&now, 0);
snd_pcm_status_get_trigger_tstamp(status, &tstamp);
timersub(&now, &tstamp, &diff);
printf("alsa-play: xrun of at least %.3f msecs. resetting stream",
diff.tv_sec * 1000 + diff.tv_usec / 1000.0);
} else
printf("alsa-play: xrun. can't determine length");
} }
return(len); /* 2nd write was ok */ res = snd_pcm_prepare(alsa_handler);
} }
return(len); else if (res == -ESTRPIPE) { /* suspend */
//} printf("alsa-play: pcm in suspend mode. trying to resume");
while ((res = snd_pcm_resume(alsa_handler)) == -EAGAIN)
sleep(1);
if (res < 0)
res = snd_pcm_prepare(alsa_handler);
} }
if (res >= 0)
res = snd_pcm_writei(alsa_handler, (void *)output_samples, num_frames);
if (res > 0) {
output_samples += ao_data.channels * res;
num_frames -= res;
}
} while (res == -EPIPE || num_frames > 0);
if (res < 0) {
printf("alsa-play: write error %s", snd_strerror(res));
return 0;
}
return res < 0 ? (int)res : len;
}
/* how many byes are free in the buffer */ /* how many byes are free in the buffer */
static int get_space() static int get_space()
{ {
@ -656,6 +722,7 @@ static int get_space()
if (ret < 0) if (ret < 0)
ret = 0; ret = 0;
//printf("alsa-space: free space = %i",ret);
return(ret); return(ret);
} }