All of lore.kernel.org
 help / color / mirror / Atom feed
* Unable to poll() or select() with PCM capture device
@ 2007-02-27 13:20 Christophe Osuna
  2007-02-27 17:31 ` Clemens Ladisch
  2007-02-28  9:21 ` Christophe Osuna
  0 siblings, 2 replies; 3+ messages in thread
From: Christophe Osuna @ 2007-02-27 13:20 UTC (permalink / raw)
  To: alsa-devel

Hi,

I am trying to use select() on ALSA devices but it does not work. I
must be missing something. Does anyone have an idea of what is wrong
with my code?

The following function works (it prints '+' whenever a new frame is ready):

void capture (snd_pcm_t *capture_handle, short *buffer,
			     int samples)
{
	while (1) {
		snd_pcm_readi (capture_handle, buffer, samples);
		printf ("+");
		fflush (stdout);
	}
}


The following uses select(). I can't get any '+' to be printed. If I
'ls -l' the file descriptor in /proc/<pid>/fd/ I can see that it is
the file descriptor of the capture device (as expected). If I use a
playback device (SND_PCM_STREAM_PLAYBACK) and the writefds of select
() the program is not blocked in the select ().

void capture_with_select (snd_pcm_t *capture_handle, short *buffer,
			  int samples)
{
	struct pollfd ufds;
	fd_set fds;

	int count = snd_pcm_poll_descriptors_count (capture_handle);
	
	if (snd_pcm_poll_descriptors(capture_handle, &ufds, count) < 0) {
		printf ("Can't get poll descriptor\n");
		exit (EXIT_FAILURE);
	}

	printf ("File descriptor: %d\n", ufds.fd);

	while (1) {
		FD_ZERO (&fds);
		FD_SET (ufds.fd, &fds);

		select (ufds.fd + 1, &fds, NULL, NULL, NULL);

		snd_pcm_readi (capture_handle, buffer, samples);
		printf ("+");
		fflush (stdout);
	}
}


Using poll() gives the same results; the program is blocked in poll()
when using a capture device.

void capture_with_poll (snd_pcm_t *capture_handle, short *buffer,
			int samples)
{
	struct pollfd ufds;
	unsigned short revents;

	int count = snd_pcm_poll_descriptors_count (capture_handle);
	
	if (snd_pcm_poll_descriptors(capture_handle, &ufds, count) < 0) {
		printf ("Can't get poll descriptor\n");
		exit (EXIT_FAILURE);
	}

	printf ("File descriptor: %d\n", ufds.fd);

	while (1) {
		poll (&ufds, count, -1);
		snd_pcm_poll_descriptors_revents (capture_handle, &ufds, count,
						  &revents);

		if (revents & POLLIN) {
			snd_pcm_readi (capture_handle, buffer, samples);
			printf ("+");
			fflush (stdout);
		}
		else {
			printf ("?");
			fflush (stdout);
		}
	}
}

Tested on:
_ Mandriva 2006, ALSA 1.0.9, USB sound device
_ SUSE 10.1, ALSA 1.0.11, USB sound device, SB Live! and ICH7 AC'97

Any idea of what is wrong and why it works with playback device but
not capture device?

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Unable to poll() or select() with PCM capture device
  2007-02-27 13:20 Unable to poll() or select() with PCM capture device Christophe Osuna
@ 2007-02-27 17:31 ` Clemens Ladisch
  2007-02-28  9:21 ` Christophe Osuna
  1 sibling, 0 replies; 3+ messages in thread
From: Clemens Ladisch @ 2007-02-27 17:31 UTC (permalink / raw)
  To: Christophe Osuna, alsa-devel

Christophe Osuna wrote:
> I am trying to use select() on ALSA devices but it does not work. I
> must be missing something. Does anyone have an idea of what is wrong
> with my code?

Did you start the device, or did you configure it to start automatically?


Regards,
Clemens

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Unable to poll() or select() with PCM capture device
  2007-02-27 13:20 Unable to poll() or select() with PCM capture device Christophe Osuna
  2007-02-27 17:31 ` Clemens Ladisch
@ 2007-02-28  9:21 ` Christophe Osuna
  1 sibling, 0 replies; 3+ messages in thread
From: Christophe Osuna @ 2007-02-28  9:21 UTC (permalink / raw)
  To: alsa-devel

> I am trying to use select() on ALSA devices but it does not work. I
> must be missing something. Does anyone have an idea of what is wrong
> with my code?

It seems pretty obvious once the problem has been solved: the sound
card has to be started before making any select() or poll() call.

-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2007-02-28  9:21 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-02-27 13:20 Unable to poll() or select() with PCM capture device Christophe Osuna
2007-02-27 17:31 ` Clemens Ladisch
2007-02-28  9:21 ` Christophe Osuna

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.