简体   繁体   English

ALSA PCM调用的语义

[英]Semantics of ALSA PCM calls

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). 嗨我正在编写一个程序,必须同时从三个输入设备捕获(在这种情况下,它是三个相同的USB网络摄像头)。

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; 首先,ALSA不是基于熟悉的UNIX范例“一切都是文件”所以我不能使用常规poll(3)调用; 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. 它工作但我怀疑有更好的方法来做到这一点,也使用mmap所以我不必再从内核空间复制到用户和内核。

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()? 实际上,snd_pcm_prepare()和snd_pcm_start()等调用的语义是什么? Right now I am not using those, I just go straight to snd_pcm_readi(). 现在我不使用那些,我直接去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! 任何提示将不胜感激!

ALSA is based on the familiar UNIX paradigm "everything is a file"; ALSA基于熟悉的UNIX范例“一切都是文件”; so to handle multiple devices, you should use poll(3). 所以要处理多个设备,你应该使用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. 有多个ALSA插件在多个文件之上实现,因此每个PCM设备可能有多个句柄。 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. 为每个设备调用snd_pcm_poll_descriptors_count以了解您需要多少个pollfd结构,然后调用snd_pcm_poll_descriptors来获取文件句柄和相应的event位。

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. 在循环中,在调用poll ,您不能直接读取pollfd结构中的vales,而是调用snd_pcm_poll_descriptors_revents来将它们转换回来。


To ensure that multiple devices start at the same time, call snd_pcm_link . 要确保多个设备同时启动,请调用snd_pcm_link However, this does not guarantee that the devices will run at the same speed. 但是,这并不能保证设备以相同的速度运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM