Skip to contentSkip to conversation

snd_pcm_wait() More details required.

DISCUSSION James Courtier-Dutton 3 participants 10 messages Mar 21, 2004 – Mar 24, 2004

lore
Share & tools
lore
#0 lore raw

I need more details on exactly what snd_pcm_wait() is supposed to do. The documentation on the www.alsa-project.org gives: - Wait for a PCM to become ready.

Parameters:
pcm PCM handle
timeout maximum time in milliseconds to wait

Returns:
a positive value on success otherwise a negative error code (-EPIPE
for the xrun and -ESTRPIPE for the suspended status, others for general errors)

Return values:
0 timeout occurred
1 PCM stream is ready for I/O

So, when is a PCM ready?
If a PCM is already in SND_PCM_STATE_RUNNING, when is snd_pcm_wait() supposed to return ?

I have discovered a problem with xine when using it. I have done a work around in xine so that dmix now works in xine. I think that my assumptions regarding snd_pcm_wait() might have been wrong, so I want to
get accurate details from yourselves.
It also might be a good idea to update the documentation for snd_pcm_wait() with more details.

Cheers
James

-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials Free Linux tutorial presented by Daniel Robbins, President and CEO of GenToo technologies. Learn everything from fundamentals to system administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click

#1 lore raw
Quoted 19 lines · James Courtier-Dutton

I need more details on exactly what snd_pcm_wait() is supposed to do. The documentation on the www.alsa-project.org gives: - Wait for a PCM to become ready.

Parameters:
pcm PCM handle
timeout maximum time in milliseconds to wait

Returns:
a positive value on success otherwise a negative error code (-EPIPE
for the xrun and -ESTRPIPE for the suspended status, others for general errors)

Return values:
0 timeout occurred
1 PCM stream is ready for I/O

Quoted 3 lines

So, when is a PCM ready?
If a PCM is already in SND_PCM_STATE_RUNNING, when is snd_pcm_wait() supposed to return ?

When avail >= avail_min.

Quoted 6 lines

I have discovered a problem with xine when using it. I have done a work around in xine so that dmix now works in xine. I think that my assumptions regarding snd_pcm_wait() might have been wrong, so I want to
get accurate details from yourselves.
It also might be a good idea to update the documentation for snd_pcm_wait() with more details.

We had a bug in the resample plugin which causes that snd_pcm_wait() function was broken. Try alsa-lib from CVS.

Jaroslav

-----
Jaroslav Kysela <[email protected]>
Linux Kernel Sound Maintainer
ALSA Project, SuSE Labs

-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials Free Linux tutorial presented by Daniel Robbins, President and CEO of GenToo technologies. Learn everything from fundamentals to system administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click

#2 lore raw
Quoted 6 lines · Jaroslav Kysela
Quoted 3 lines

So, when is a PCM ready?
If a PCM is already in SND_PCM_STATE_RUNNING, when is snd_pcm_wait() supposed to return ?

When avail >= avail_min.

1) Does this depend on period size in any way?
For example, if period size is 6000 frames, and I set avail_min to 2000 frames, will snd_pcm_wait() return when avail=2000 or avail=6000 ? Another example, if persiod size is 1500, and I set avail_min to 2000, will snd_pcm_wait() return when avail=200 or avail=3000 (2*1500)?

2) Is snd_pcm_wait() a call that uses little or no CPU time, much like a poll?

3) It appears from the docs that I can set avail_min at any time, even in SND_PCM_STATE_RUNNING. commands to set it would be: -
snd_pcm_sw_params_current(this->audio_fd, swparams); snd_pcm_sw_params_set_avail_min(this->audio_fd, swparams, new_avail_min_size); snd_pcm_sw_params(this->audio_fd, swparams);

or is there a better way to do it while in SND_PCM_STATE_RUNNING ?

4) If snd_pcm_wait() is waiting, and with another thread we change the avail_min value, will the snd_pcm_wait() use the new value, or is the new value only used on the next call to snd_pcm_wait()

Quoted 8 lines
Quoted 5 lines

I have discovered a problem with xine when using it. I have done a work around in xine so that dmix now works in xine. I think that my assumptions regarding snd_pcm_wait() might have been wrong, so I want to
get accurate details from yourselves.
It also might be a good idea to update the documentation for snd_pcm_wait() with more details.

We had a bug in the resample plugin which causes that snd_pcm_wait() function was broken. Try alsa-lib from CVS.

My current tests were not using the resampler. i.e. app using 48k, sound card using 48k. I will try some tests that would use the resampler.

Quoted 3 lines

Jaroslav

-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials Free Linux tutorial presented by Daniel Robbins, President and CEO of GenToo technologies. Learn everything from fundamentals to system administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click

#3 lore raw
Quoted 7 lines · James Courtier-Dutton
Quoted 6 lines · Jaroslav Kysela
Quoted 4 lines

So, when is a PCM ready?
If a PCM is already in SND_PCM_STATE_RUNNING, when is snd_pcm_wait() supposed to return ?

Quoted 7 lines
Quoted 1 line

When avail >= avail_min.

1) Does this depend on period size in any way?
For example, if period size is 6000 frames, and I set avail_min to 2000 frames, will snd_pcm_wait() return when avail=2000 or avail=6000 ? Another example, if persiod size is 1500, and I set avail_min to 2000, will snd_pcm_wait() return when avail=200 or avail=3000 (2*1500)?

Pointers are updated in the interrupt or when you call snd_pcm_hwsync() or snd_pcm_delay().

Quoted 2 lines

2) Is snd_pcm_wait() a call that uses little or no CPU time, much like a poll?

snd_pcm_wait() uses poll().

Quoted 9 lines

3) It appears from the docs that I can set avail_min at any time, even in SND_PCM_STATE_RUNNING.
commands to set it would be: - snd_pcm_sw_params_current(this->audio_fd, swparams); snd_pcm_sw_params_set_avail_min(this->audio_fd, swparams, new_avail_min_size);
snd_pcm_sw_params(this->audio_fd, swparams);

or is there a better way to do it while in SND_PCM_STATE_RUNNING ?

No, you may cache the sw_params structure.

Quoted 3 lines

4) If snd_pcm_wait() is waiting, and with another thread we change the avail_min value, will the snd_pcm_wait() use the new value, or is the new value only used on the next call to snd_pcm_wait()

The threshold will be used immediately.

Quoted 2 lines

My current tests were not using the resampler. i.e. app using 48k, sound card using 48k. I will try some tests that would use the resampler.

Then I need more info how I can reproduce the bug.

Jaroslav

-----
Jaroslav Kysela <[email protected]>
Linux Kernel Sound Maintainer
ALSA Project, SuSE Labs

-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials Free Linux tutorial presented by Daniel Robbins, President and CEO of GenToo technologies. Learn everything from fundamentals to system administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click

#4 lore raw
Quoted 6 lines · Jaroslav Kysela
Quoted 2 lines · James Courtier-Dutton

My current tests were not using the resampler. i.e. app using 48k, sound card using 48k. I will try some tests that would use the resampler.

Then I need more info how I can reproduce the bug.

The following test program shows strange behaviour:

// ---------- bite off here ---------------------------------------- #include <alsa/asoundlib.h>

#define CHECK(command) do { \
	err = (command); \
	if (err < 0) { \
		printf("%s failed: %s\n", #command, snd_strerror(err)); \
		return 1; \
	} \
	} while (0)

int main(int argc, char *argv[])

{
	snd_pcm_t *pcm;
	snd_pcm_hw_params_t *hw_params;
	snd_pcm_sw_params_t *sw_params;
	int err;
	static char silence[12000 * 2 * 2];

	if (argc < 2) return 1;
	CHECK(snd_pcm_open(&pcm, argv[1], SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK));
	snd_pcm_hw_params_alloca(&hw_params);
	CHECK(snd_pcm_hw_params_any(pcm, hw_params));
	CHECK(snd_pcm_hw_params_set_access(pcm, hw_params, SND_PCM_ACCESS_RW_INTERLEAVED));
	CHECK(snd_pcm_hw_params_set_format(pcm, hw_params, SND_PCM_FORMAT_S16));
	CHECK(snd_pcm_hw_params_set_channels(pcm, hw_params, 2));
	CHECK(snd_pcm_hw_params_set_rate(pcm, hw_params, 48000, 0));
	CHECK(snd_pcm_hw_params_set_buffer_size(pcm, hw_params, 12000));
	CHECK(snd_pcm_hw_params_set_period_size(pcm, hw_params, 6000, 0));
	CHECK(snd_pcm_hw_params(pcm, hw_params));
	snd_pcm_sw_params_alloca(&sw_params);
	CHECK(snd_pcm_sw_params_current(pcm, sw_params));
	CHECK(snd_pcm_sw_params_set_start_threshold(pcm, sw_params, 12000));
	CHECK(snd_pcm_sw_params_set_avail_min(pcm, sw_params, 6000));
	CHECK(snd_pcm_sw_params(pcm, sw_params));
	for (;;) {
		CHECK(snd_pcm_wait(pcm, 2000));
		if (err == 0)
			printf("timeout!\n");
		err = snd_pcm_writei(pcm, silence, 12000);
		if (err >= 0)
			printf("%d frames written\n", err);
		else if (err == -EAGAIN)
			printf("pcm not ready!\n");
		else
			CHECK(err);
	}
}

// ---------- bite off here ----------------------------------------

With an AC'97 device:

$ ./waittest hw:0
12000 frames written
6000 frames written
6000 frames written
6000 frames written
6000 frames written
...
$ ./waittest dmix:hw:0
timeout!
12000 frames written
pcm is not ready!
6000 frames written
6000 frames written
6000 frames written
6000 frames written
...

When running with dmix, the first call to snd_pcm_wait() times out although the pcm is ready (it seems the hardware pcm is still stopped and doesn't generate interrupts), and the second call to snd_pcm_wait() returns 1 although the pcm is not yet ready.

Regards,
Clemens

-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials Free Linux tutorial presented by Daniel Robbins, President and CEO of GenToo technologies. Learn everything from fundamentals to system administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click

#5 lore raw
Quoted 10 lines · Clemens Ladisch
Quoted 6 lines · Jaroslav Kysela
Quoted 2 lines · James Courtier-Dutton

My current tests were not using the resampler. i.e. app using 48k, sound card using 48k. I will try some tests that would use the resampler.

Then I need more info how I can reproduce the bug.

The following test program shows strange behaviour:

Thanks. It's fixed in CVS now.

Jaroslav

-----
Jaroslav Kysela <[email protected]>
Linux Kernel Sound Maintainer
ALSA Project, SuSE Labs

-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials Free Linux tutorial presented by Daniel Robbins, President and CEO of GenToo technologies. Learn everything from fundamentals to system administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click

#6 lore raw
Quoted 4 lines · Jaroslav Kysela
Quoted 1 line · Clemens Ladisch

The following test program shows strange behaviour:

Thanks. It's fixed in CVS now.

The second problem persists, i.e., snd_pcm_wait() returns although the pcm isn't actually ready:

$ ./waittest dmix:hw:0
12000 frames written
pcm is not ready!
6000 frames written
6000 frames written
6000 frames written
...

Regards,
Clemens

-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials Free Linux tutorial presented by Daniel Robbins, President and CEO of GenToo technologies. Learn everything from fundamentals to system administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click

#7 lore raw
Quoted 6 lines · Clemens Ladisch
Quoted 4 lines · Jaroslav Kysela
Quoted 1 line · Clemens Ladisch

The following test program shows strange behaviour:

Thanks. It's fixed in CVS now.

Quoted 7 lines

The second problem persists, i.e., snd_pcm_wait() returns although the pcm isn't actually ready:

$ ./waittest dmix:hw:0
12000 frames written
pcm is not ready!
6000 frames written

Fixed again. I was not able reproduce this problem with some hardware.

Jaroslav

-----
Jaroslav Kysela <[email protected]>
Linux Kernel Sound Maintainer
ALSA Project, SuSE Labs

-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials Free Linux tutorial presented by Daniel Robbins, President and CEO of GenToo technologies. Learn everything from fundamentals to system administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click

#8 lore raw
In reply to James Courtier-Dutton
Quoted 5 lines · James Courtier-Dutton

I need more details on exactly what snd_pcm_wait() is supposed to do. The documentation on the www.alsa-project.org gives: - Wait for a PCM to become ready.
...
So, when is a PCM ready?

When you can read/write some data without blocking, i.e., avail >= avail_min.

Quoted 4 lines

I have discovered a problem with xine when using it. I have done a work around in xine so that dmix now works in xine. I think that my assumptions regarding snd_pcm_wait() might have been wrong, so I want to get accurate details from yourselves.

I think the implementation of dmix is broken in this regard. If I read the code correctly, snd_pcm_direct_poll_revents() returns POLLIN/OUT even if the pcm is empty.

Jaroslav, shouldn't this function reset the events variable when empty is set?

Regards,
Clemens

-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials Free Linux tutorial presented by Daniel Robbins, President and CEO of GenToo technologies. Learn everything from fundamentals to system administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click

#9 lore raw
Quoted 11 lines · Clemens Ladisch
Quoted 4 lines

I have discovered a problem with xine when using it. I have done a work around in xine so that dmix now works in xine. I think that my assumptions regarding snd_pcm_wait() might have been wrong, so I want to get accurate details from yourselves.

I think the implementation of dmix is broken in this regard. If I read the code correctly, snd_pcm_direct_poll_revents() returns POLLIN/OUT even if the pcm is empty.

Jaroslav, shouldn't this function reset the events variable when empty is set?

No, in case when no timer events are available, the revents should be zero from the kernel space, so we don't need to explicitly clear this variable.

Jaroslav

-----
Jaroslav Kysela <[email protected]>
Linux Kernel Sound Maintainer
ALSA Project, SuSE Labs

-------------------------------------------------------
This SF.Net email is sponsored by: IBM Linux Tutorials Free Linux tutorial presented by Daniel Robbins, President and CEO of GenToo technologies. Learn everything from fundamentals to system administration.http://ads.osdn.com/?ad_id=1470&alloc_id=3638&op=click