Linux

修復了從 USB 到 ALSA 設備列表的卡映射

  • September 14, 2020

我有一些通過 USB 連接的網路攝像頭設備,我想使用arecord. 設備列表如下(卡 1 到 4):

t:~$ arecord --list-devices
**** List of CAPTURE Hardware Devices ****
card 0: PCH [HDA Intel PCH], device 0: ALC Analog [ALC Analog]
 Subdevices: 1/1
 Subdevice #0: subdevice #0
card 1: TT [CA TT], device 0: USB Audio [USB Audio]
 Subdevices: 1/1
 Subdevice #0: subdevice #0
card 2: TT_1 [CA TT], device 0: USB Audio [USB Audio]
 Subdevices: 1/1
 Subdevice #0: subdevice #0
card 3: TT_2 [CA TT], device 0: USB Audio [USB Audio]
 Subdevices: 1/1
 Subdevice #0: subdevice #0
card 4: TT_3 [CA TT], device 0: USB Audio [USB Audio]
 Subdevices: 1/1
 Subdevice #0: subdevice #0

當我列出 PulseAudio 看到的設備時,我有以下內容(部分列表僅顯示網路攝像頭):

t:~$pactl list
Module #0
       Name: module-device-restore 
....
Card #11
   Name: alsa_card.usb-VS_Inc_CA_TT-02
   Driver: module-alsa-card.c
   Owner Module: 38
   Properties:
       alsa.card = "1"
       alsa.card_name = "CA TT"
       alsa.long_card_name = "VS Inc CA TT at usb-0000:00:14.0-3"
       alsa.driver_name = "snd_usb_audio"
       device.bus_path = "pci-0000:00:14.0-usb-0:3:1.2"
       sysfs.path = "/devices/pci0000:00/0000:00:14.0/usb2/2-3/2-3:1.2/sound/card1"
       udev.id = "usb-VS_Inc_CA_TT-02"
       device.bus = "usb"
       device.vendor.id = "1a1a"
       device.vendor.name = "Technology Inc."
       device.product.id = "2a2a"
       device.product.name = "CA TT"
       device.serial = "VS_Inc_CA_TT"
       device.form_factor = "webcam"
       device.string = "1"
       device.description = "CA TT"
       module-udev-detect.discovered = "1"
       device.icon_name = "camera-web-usb"
   Profiles:
       input:analog-stereo: Analog Stereo Input (sinks: 0, sources: 1, priority: 60, available: yes)
       input:iec958-stereo: Digital Stereo (IEC958) Input (sinks: 0, sources: 1, priority: 55, available: yes)
       off: Off (sinks: 0, sources: 0, priority: 0, available: yes)
   Active Profile: input:analog-stereo
   Ports:
       analog-input: Analog Input (priority: 10000, latency offset: 0 usec)
           Part of profile(s): input:analog-stereo
       iec958-stereo-input: Digital Input (S/PDIF) (priority: 0, latency offset: 0 usec)
           Part of profile(s): input:iec958-stereo

Card #12
   Name: alsa_card.usb-VS_Inc_CA_TT-02.2
   Driver: module-alsa-card.c
   Owner Module: 39
   Properties:
       alsa.card = "2"
       alsa.card_name = "CA TT"
       alsa.long_card_name = "VS Inc CA TT at usb-0000:00:14.0-4"
       alsa.driver_name = "snd_usb_audio"
       device.bus_path = "pci-0000:00:14.0-usb-0:4:1.2"
       sysfs.path = "/devices/pci0000:00/0000:00:14.0/usb2/2-4/2-4:1.2/sound/card2"
       udev.id = "usb-VS_Inc_CA_TT-02"
       device.bus = "usb"
       device.vendor.id = "1a1a"
       device.vendor.name = "Technology Inc."
       device.product.id = "2a2a"
       device.product.name = "CA TT"
       device.serial = "VS_Inc_CA_TT"
       device.form_factor = "webcam"
       device.string = "2"
       device.description = "CA TT"
       module-udev-detect.discovered = "1"
       device.icon_name = "camera-web-usb"
   Profiles:
       input:analog-stereo: Analog Stereo Input (sinks: 0, sources: 1, priority: 60, available: yes)
       input:iec958-stereo: Digital Stereo (IEC958) Input (sinks: 0, sources: 1, priority: 55, available: yes)
       off: Off (sinks: 0, sources: 0, priority: 0, available: yes)
   Active Profile: input:analog-stereo
   Ports:
       analog-input: Analog Input (priority: 10000, latency offset: 0 usec)
           Part of profile(s): input:analog-stereo
       iec958-stereo-input: Digital Input (S/PDIF) (priority: 0, latency offset: 0 usec)
           Part of profile(s): input:iec958-stereo

Card #13
   Name: alsa_card.usb-VS_Inc_CA_TT-02.3
   Driver: module-alsa-card.c
   Owner Module: 40
   Properties:
       alsa.card = "3"
       alsa.card_name = "CA TT"
       alsa.long_card_name = "VS Inc CA TT at usb-0000:00:14.0-1"
       alsa.driver_name = "snd_usb_audio"
       device.bus_path = "pci-0000:00:14.0-usb-0:1:1.2"
       sysfs.path = "/devices/pci0000:00/0000:00:14.0/usb2/2-1/2-1:1.2/sound/card3"
       udev.id = "usb-VS_Inc_CA_TT-02"
       device.bus = "usb"
       device.vendor.id = "1a1a"
       device.vendor.name = "Technology Inc."
       device.product.id = "2a2a"
       device.product.name = "CA TT"
       device.serial = "VS_Inc_CA_TT"
       device.form_factor = "webcam"
       device.string = "3"
       device.description = "CA TT"
       module-udev-detect.discovered = "1"
       device.icon_name = "camera-web-usb"
   Profiles:
       input:analog-stereo: Analog Stereo Input (sinks: 0, sources: 1, priority: 60, available: yes)
       input:iec958-stereo: Digital Stereo (IEC958) Input (sinks: 0, sources: 1, priority: 55, available: yes)
       off: Off (sinks: 0, sources: 0, priority: 0, available: yes)
   Active Profile: input:analog-stereo
   Ports:
       analog-input: Analog Input (priority: 10000, latency offset: 0 usec)
           Part of profile(s): input:analog-stereo
       iec958-stereo-input: Digital Input (S/PDIF) (priority: 0, latency offset: 0 usec)
           Part of profile(s): input:iec958-stereo

Card #14
   Name: alsa_card.usb-VS_Inc_CA_TT-02.4
   Driver: module-alsa-card.c
   Owner Module: 41
   Properties:
       alsa.card = "4"
       alsa.card_name = "CA TT"
       alsa.long_card_name = "VS Inc CA TT at usb-0000:00:14.0-2"
       alsa.driver_name = "snd_usb_audio"
       device.bus_path = "pci-0000:00:14.0-usb-0:2:1.2"
       sysfs.path = "/devices/pci0000:00/0000:00:14.0/usb2/2-2/2-2:1.2/sound/card4"
       udev.id = "usb-VS_Inc_CA_TT-02"
       device.bus = "usb"
       device.vendor.id = "1a1a"
       device.vendor.name = "Technology Inc."
       device.product.id = "2a2a"
       device.product.name = "CA TT"
       device.serial = "VS_Inc_CA_TT"
       device.form_factor = "webcam"
       device.string = "4"
       device.description = "CA TT"
       module-udev-detect.discovered = "1"
       device.icon_name = "camera-web-usb"
   Profiles:
       input:analog-stereo: Analog Stereo Input (sinks: 0, sources: 1, priority: 60, available: yes)
       input:iec958-stereo: Digital Stereo (IEC958) Input (sinks: 0, sources: 1, priority: 55, available: yes)
       off: Off (sinks: 0, sources: 0, priority: 0, available: yes)
   Active Profile: input:analog-stereo
   Ports:
       analog-input: Analog Input (priority: 10000, latency offset: 0 usec)
           Part of profile(s): input:analog-stereo
       iec958-stereo-input: Digital Input (S/PDIF) (priority: 0, latency offset: 0 usec)
           Part of profile(s): input:iec958-stereo

arecord --list-devices現在,我正在編寫一個 shell 腳本,它從一個固定的配置文件中讀取卡號,如果其中一個網路攝像頭被拔掉,我不希望其中的卡號發生變化。我想確保連接到第一個 USB 埠 ( /devices/pci0000:00/0000:00:14.0/usb2/2-3/2-3:1.2)的網路攝像頭始終映射到卡 1,第二個映射到卡 2,依此類推。換句話說,我希望網路攝像頭/USB 埠到卡號之間的映射是固定的。

這可能嗎?

無法更改具有相同 USB ID 的設備的卡號*。*

但是可以使用 udev 規則來更改卡IDTT_2上面的等),也可以與arecord. 請參閱https://alsa.opensrc.org/Udev上的“辨識兩個相同的音頻設備” :

SUBSYSTEM!="sound", GOTO="my_usb_audio_end"
ACTION!="add", GOTO="my_usb_audio_end"

DEVPATH=="/devices/pci0000:00/0000:00:13.2/usb2/2-6/2-6.2/2-6.2:1.0/sound/card?", ATTR{id}="UA1A_A"
DEVPATH=="/devices/pci0000:00/0000:00:13.2/usb2/2-6/2-6.3/2-6.3:1.0/sound/card?", ATTR{id}="UA1A_B"

LABEL="my_usb_audio_end"
$ arecord -D default:UA1A_A something.wav

引用自:https://unix.stackexchange.com/questions/609302