Question

Hi I am writing a program that has to capture from three input devices at the same time (in this case, it's three identical USB webcams).

First of all, ALSA is not based on the familiar UNIX paradigm "everything is a file" so I cannot use the regular poll(3) call; knowing that the data stream should be steady among all devices, for now I do something like:

while(!stop)
  for(i = 0; i < input_device_count; i++)
  {
    snd_pcm_readi(handle[i], buffer, frames);
    write(fd_out[i], buffer, size);
  }

This code iterates over each device and reads from it, writing the result on a file previously open. It works but I suspect there is a better way to do this, also using mmap so I do not have to copy from Kernel-space to user and to Kernel again.

For a moment, let's assume the three inputs stay in sync; the above code still does not guarantee that I will begin to record from the three devices at the same time. Is there a way to guarantee that? In fact, what are the semantics of calls like snd_pcm_prepare() and snd_pcm_start()? Right now I am not using those, I just go straight to snd_pcm_readi().

I have tried to search for code examples but I haven't found anything that has to do with multiple captures at the same time. Any hint would be appreciated!

Was it helpful?

Solution

ALSA is based on the familiar UNIX paradigm "everything is a file"; so to handle multiple devices, you should use poll(3).

There are ALSA plugins that are implemented on top of multiple files, so there might be more than one handle per PCM device. Call snd_pcm_poll_descriptors_count for each device to know how many pollfd structures you need, then call snd_pcm_poll_descriptors to get the file handles and the respective event bits.

In your loop, after calling poll, you must not read the vales in the pollfd structures directly but call snd_pcm_poll_descriptors_revents to translate them back.


To ensure that multiple devices start at the same time, call snd_pcm_link. However, this does not guarantee that the devices will run at the same speed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top