'Device or resource busy' error thrown when trying to record audio using arecord

22,301

The ipc_key is used for communication between the programs that share the same device. This means that you have to use different values for different hardware devices, but that all virtual devices that access the same hardware device (i.e., the same slave usb_audio_1) must use the same ID.

Share:
22,301

Related videos on Youtube

skrowten_hermit
Author by

skrowten_hermit

Updated on September 18, 2022

Comments

  • skrowten_hermit
    skrowten_hermit over 1 year

    I'm trying to record audio that is being played on separate channels using arecord. I do this by executing the following command in separate threads in a python script:

    arecord -D plug:"+str(in_id)+" -c 1 -d "+str(duration)+" -f S16_LE -r "+str(rate)+" "+rec_filename
    

    where in_id is the input channel variable that I pass to each of the threads. But then, the following error is thrown:

    ALSA lib pcm_dsnoop.c:606:(snd_pcm_dsnoop_open) unable to open slave
    arecord: main:722: audio open error: Device or resource busy
    

    When I run lsof /dev/snd/pcm* right before I start recording using arecord in my record function in the program, I see the following:

    COMMAND   PID        USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
    aplay   12236       user4  mem    CHR  116,3           493 /dev/snd/pcmC1D0p
    aplay   12236       user4    4u   CHR  116,3      0t0  493 /dev/snd/pcmC1D0p
    

    My device definition for input channels in .asoundrc is as follows:

    pcm.!default {
        type plug
        slave {
           pcm "hw:1,0"
        }
    }
    
    ctl.!default { 
        type hw
        card 1
    }
    
    pcm_slave.usb_audio_1 {
        pcm "hw:1,0"
        channels 8
        rate 44100
        buffer_size 4096
        period_size 1024
    }
    
    pcm.outch<num> {
        type dshare
        ipc_key <id>
        slave usb_audio_1
        bindings [ <ch_num> ]
        hint.description "USB output/playback channel <num> (from output port <num>)"
    }
    
    pcm.inch<num> {
        type dsnoop
        ipc_key <id>
        slave usb_audio_1
        bindings [ <ch_num> ]
        hint.description "USB input/capture channel <num> (from input port <num>)"
    }
    

    where <num> takes all values between 1 and 8 and <ch_num> takes all values between 0 and 7.

    Since, the error clearly says there is a definite problem with dsnoop, does that mean that it is not meant for simultaneous/multi-channel recording? I mean is there a conflict between dsnoop and dshare or if dsnoop can record/capture from only one channel at a time? Is there another way I can achieve this?

    • Admin
      Admin about 7 years
      Does a single such arecord call work on the command line?
    • Admin
      Admin about 7 years
      Yes, single arecord call works fine. I can combine arecord with aplay on pipes as well.
    • Admin
      Admin about 7 years
      Does the first arecord call from Python work?
    • Admin
      Admin about 7 years
      After the execution of the program, lsof /dev/snd/pcm* returns nothing.
    • Admin
      Admin about 7 years
      To be sure, run it from your Python program, just before arecord.
    • Admin
      Admin about 7 years
      I have updated my post with the result of lsof /dev/snd/pcm* during the execution of the program.
    • Admin
      Admin about 7 years
      All the output channels are defined with ipc_key values as 1111,1112,1113...1118 and output channels are defined with ipc_key value as 2111,2112,2113...2118.
  • skrowten_hermit
    skrowten_hermit about 7 years
    Not even in my wildest dreams I had suspected ipc_key to be the cause of this! Obviously, my understanding was flawed. Thanks a lot @CL. for clearing it. Just out of curiosity, in my case, can I put the ipc_key in some other container definition block in order to avoid such mistakes. I mean it is only logical to have a master block in the hierarchy with a common field like ipc_key right?