【问题标题】:alsa: How to programmatically find if a device is busy/in use using it name and without opening italsa:如何以编程方式使用设备名称而不打开设备来查找设备是否忙/正在使用中
【发布时间】:2016-06-16 19:31:14
【问题描述】:

我们有一个带有扬声器和 MIC 设备的 Linux 设备。这些设备在不同的模块之间共享 - 例如 VOIP 呼叫可以使用扬声器,危险警告系统可以使用扬声器,语音提示可以使用扬声器等:

ALSA 中似乎有一个函数使用 pcm 来提供状态。 int snd_pcm_status (snd_pcm_t * pcm, snd_pcm_status_t * 状态)

但是 *pcm 由 snd_pcm_open 返回。我们不想打开设备,因为我们想使用设备的“名称”了解设备的状态

Alsa API 是 here

我们如何在不打开资源/设备并使用其名称的情况下检查它是否繁忙?

【问题讨论】:

    标签: linux resources device alsa


    【解决方案1】:

    在文件/proc/asound/cardZ/pcmYp/subX/status 中提供了与卡Z 上设备Y 的播放流X 相同的信息;当设备未打开时,它只是说“关闭”。

    请注意,您不能使用此信息来决定是否可以打开设备,因为在您阅读此信息后,其他一些进程可能会打开它。检查您是否可以打开它的唯一方法是实际尝试。

    【讨论】:

      【解决方案2】:

      虽然它需要 /dev/dsp,但这似乎可行:

      #!/bin/dash
      
      ## If the speaker is not used by any, returns 0, and prints "free"
      ## Otherwise, returns 1 and prints "not free"
      
      iExit (){
          trap '' 0
          exit $1
      }
      
      iCatch (){
          # The speaker is already in use
          echo not free
          iExit 1
      }
      
      trap iCatch 0
      
      {
          exec 3>&1 1>/dev/dsp
          # If the execution reaches here, the speaker is not used by any
          # Otherwise, it's catched by iCatch
          exec 1>&3
          echo free
          iExit 0
      } 2>/dev/null
      

      如果没有 PulseAudio,在某些 PC 上似乎一次只接受一个输出;在其他人同时播放是允许的。但即使在后一种情况下,上面的代码也可以工作。

      NB:上面的代码与 bash 一起工作;对于 bash,只需使用 if/else 而不是陷阱。

      NB 2:可能缺少 /dev/dsp,具体取决于内核配置。

      【讨论】:

        猜你喜欢
        • 2010-12-24
        • 1970-01-01
        • 1970-01-01
        • 2011-12-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-02-23
        相关资源
        最近更新 更多