Can I query which processes (if any) are currently accessing the microphone?

5,828

Solution 1

First identify your microphone device file; should be something similar to /dev/snd/pcmC0D0c. To help you find the device file, you can start a test recording with arecord or such, then do lsof | grep '/dev/snd'; it will list all programs and their associated device file.

Then you can peek usage of the microphone using fuser /dev/snd/pcmC0D0c. It will return the PID of the program accessing the device, if said device is opened.

You may prefer to loop on inotifywait /dev/snd/pcmC0D0c alternatively, to detect changes in state instead of constantly polling the device for status.

Solution 2

The field owner_pid in the procfs file status of a PCM device shows which program has opened it:

$ grep owner_pid /proc/asound/card*/pcm*/sub*/status
/proc/asound/card2/pcm0p/sub0/status:owner_pid   : 1803
$ ps -p 1803
  PID TTY          TIME CMD
 1803 pts/0    00:00:00 aplay
Share:
5,828

Related videos on Youtube

jmite
Author by

jmite

I am Joey Eremondi, a PhD Student at the University of British Columbia. I do research in Programming Languages and Theory of Computation, particularly with dependent types. My Masters Thesis was on improving error messages for higher order unification. I've also co-authored a few papers on reversal-bounded counter automata. I have an M.Sc in Computing Science from Utrecht University, a B.Sc. Honours in Computer Science, and a B.Sc. 4-year in Mathematics, both from the University of Saskatchewan.

Updated on September 18, 2022

Comments

  • jmite
    jmite over 1 year

    I'm running Debian (technically Raspbian), trying to get a Star-Trek style voice-command system. I've got it mostly up and running, but in the interests of privacy and all that jazz, I don't want my microphone always recording.

    I'm wondering, is there a way to poll which processes, if any, are currently accessing the microphone? The idea is that I'd make myself a little indicator to tell if the microphone was currently recording audio or not.

    My ultimate goal is to turn the microphone on and off using a TV-remote, and to have an LED indicator for if it's on or off. I've got all the hardware stuff working, I just need the software end now.

    I believe that right now I'm configured in pure ALSA i.e. not PulseAudio, though I could be wrong.