Hello,
I'm using ALSA to develop an audio-streaming application. I try to
use start_threshold and stop_threshold in combination with small
buffers. However, I think I probably found a bug about this.
I'm setting start_threshold=100 and stop_threshold=50. I'm also using
a buffer of 44 frames. When I call
snd_pcm_writei(the_small_44_frames_buffer), pcm state came to XRUN from
PREPARED directly. I think this is a bug because the stream hasn't
started. It's hard to say a xrun occurred while stream not started.
I'm wondering if this is a ALSA-bug or a misuse of ALSA. A simple bug
test program is attached.
Thank you very much!
Zhang Boyang
p.s.
I dug into kernel code. After writting hardware buffer,
__snd_pcm_lib_xfer() called snd_pcm_update_state(), which set the XRUN
state.
#define PCM_DEVICE "hw:1,0"
/*
gcc -Wall -o bug start_stop_threshold_bug.c -lasound
./bug
*/
/*
* Simple sound playback using ALSA API and libasound.
*
* Compile:
* $ cc -o play sound_playback.c -lasound
*
* Usage:
* $ ./play <sample_rate> <channels> <seconds> < <file>
*
* Examples:
* $ ./play 44100 2 5 < /dev/urandom
* $ ./play 22050 1 8 < /path/to/file.wav
*
* Copyright (C) 2009 Alessandro Ghedini <al3xbio@xxxxxxxxx>
* --------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42):
* Alessandro Ghedini wrote this file. As long as you retain this
* notice you can do whatever you want with this stuff. If we
* meet some day, and you think this stuff is worth it, you can
* buy me a beer in return.
* --------------------------------------------------------------
*/
#include <alsa/asoundlib.h>
#include <stdio.h>
int main(int argc, char **argv) {
unsigned int pcm, tmp;
unsigned rate;
int channels, seconds;
snd_pcm_t *pcm_handle;
snd_pcm_hw_params_t *params;
snd_pcm_uframes_t frames;
char *buff;
int buff_size, loops;
rate = 44100;
channels = 2;
seconds = 10;
int start_threshold = 100;
int stop_threshold = 50;
/* Open the PCM device in playback mode */
if ((pcm = snd_pcm_open(&pcm_handle, PCM_DEVICE,
SND_PCM_STREAM_PLAYBACK, 0) < 0))
printf("ERROR: Can't open \"%s\" PCM device. %s\n",
PCM_DEVICE, snd_strerror(pcm));
/* Allocate parameters object and fill it with default values*/
snd_pcm_hw_params_alloca(¶ms);
snd_pcm_hw_params_any(pcm_handle, params);
/* Set parameters */
snd_pcm_hw_params_set_rate_resample(pcm_handle, params, 0);
if ((pcm = snd_pcm_hw_params_set_access(pcm_handle, params,
SND_PCM_ACCESS_RW_INTERLEAVED) < 0))
printf("ERROR: Can't set interleaved mode. %s\n", snd_strerror(pcm));
if ((pcm = snd_pcm_hw_params_set_format(pcm_handle, params,
SND_PCM_FORMAT_S16_LE) < 0))
printf("ERROR: Can't set format. %s\n", snd_strerror(pcm));
if ((pcm = snd_pcm_hw_params_set_channels(pcm_handle, params, channels) < 0))
printf("ERROR: Can't set channels number. %s\n", snd_strerror(pcm));
if ((pcm = snd_pcm_hw_params_set_rate_near(pcm_handle, params, &rate, 0) < 0))
printf("ERROR: Can't set rate. %s\n", snd_strerror(pcm));
/* Write parameters */
if ((pcm = snd_pcm_hw_params(pcm_handle, params) < 0))
printf("ERROR: Can't set harware parameters. %s\n", snd_strerror(pcm));
snd_pcm_sw_params_t *swparams;
snd_pcm_sw_params_alloca(&swparams);
pcm = snd_pcm_sw_params_current(pcm_handle, swparams);
if (pcm < 0) {
printf("Unable to determine current swparams: %s", snd_strerror(pcm));
return pcm;
}
pcm = snd_pcm_sw_params_set_start_threshold(pcm_handle, swparams, start_threshold);
if (pcm < 0) {
printf("Unable to set start threshold mode: %s", snd_strerror(pcm));
return pcm;
}
pcm = snd_pcm_sw_params_set_stop_threshold(pcm_handle, swparams, stop_threshold);
if (pcm < 0) {
printf("Unable to set stop threshold mode: %s", snd_strerror(pcm));
return pcm;
}
pcm = snd_pcm_sw_params(pcm_handle, swparams);
if (pcm < 0) {
printf("Unable to set sw params: %s", snd_strerror(pcm));
return pcm;
}
/* Resume information */
printf("PCM name: '%s'\n", snd_pcm_name(pcm_handle));
printf("PCM state: %s\n", snd_pcm_state_name(snd_pcm_state(pcm_handle)));
snd_pcm_hw_params_get_channels(params, &tmp);
printf("channels: %i ", tmp);
if (tmp == 1)
printf("(mono)\n");
else if (tmp == 2)
printf("(stereo)\n");
snd_pcm_hw_params_get_rate(params, &tmp, 0);
printf("rate: %d bps\n", tmp);
printf("seconds: %d\n", seconds);
frames = 44;
buff_size = frames * channels * 2 /* 2 -> sample size */;
buff = (char *) malloc(buff_size);
memset(buff, 0, buff_size);
snd_pcm_hw_params_get_period_time(params, &tmp, NULL);
for (loops = (seconds * 1000000) / tmp; loops > 0; loops--) {
printf("==== loop ====\n");
printf("(before write) PCM state: %s\n", snd_pcm_state_name(snd_pcm_state(pcm_handle)));
pcm = snd_pcm_writei(pcm_handle, buff, frames);
printf("written %d\n", pcm);
printf("(after write) PCM state: %s\n", snd_pcm_state_name(snd_pcm_state(pcm_handle)));
if (pcm == -EPIPE) {
printf("XRUN.\n");
snd_pcm_prepare(pcm_handle);
abort();
} else if (pcm < 0) {
printf("ERROR. Can't write to PCM device. %s\n", snd_strerror(pcm));
}
}
snd_pcm_drain(pcm_handle);
snd_pcm_close(pcm_handle);
free(buff);
return 0;
}