Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there an API to check if Mac's Microphone or video camera is in use? [closed]

Yes, I realize I can just look at the green-light when the video camera is on. That's not the point.

I'd like to write a little utility that notices when the mic or video camera is in use. I don't have any interest in knowing what app is using it. I just want to know if the mic / camera on or off.

This is for me as a parent. I was thinking I could get one of those color changing LED lights, and then when the camera/mic is on, my app could detect it, then send a signal to the light to change color. Then when one of my kids walks in, they'd see the light is "red" (meaning, do not disturb) and they'd know I'm on a conference call.

like image 600
Dan Morrow Avatar asked May 07 '20 15:05

Dan Morrow


People also ask

Is my microphone being used Mac?

How Do I Know if My Mac Microphone Is in Use? In macOS, you can tell if your microphone is currently in use by glancing at the menu bar. The menu bar has a Control Center icon, and you'll see a yellow dot right next to that icon if your mic is currently in use.

How do I know if my Mac microphone is on?

Control Center and your Mac's microphone To open Control Center on your Mac, click Control Center in the menu bar. Choose Control Center in the menu bar. An orange dot indicates the microphone on your Mac is in use; you can see which apps are using it at the top of Control Center.

How do I know if my mic is muted Mac?

On a Mac, open System Preferences and select Sound then choose the Input tab. There, slide the Input Volume slider down to the lowest level on the far left, and your mic will be muted until you raise that volume again. Windows actually has a mute button for your mic—it's just hidden inside settings screens.


1 Answers

I have pretty much the exact same problem to solve. This is my prototype solution. It monitors the number of threads of the AppleCamera process. On the test macbook, the base number of threads seems to be 3. When an application uses the camera, the count increases to 4. I plan to implement microphone checking as well. I'm sure my code could be more compact and I could get the shell commands down to a one-liner but I prefer readability.

import subprocess
import pywemo

DEVICE_NAME = "BatSignal"


def count_camera_threads():
    command = ["pgrep", "AppleCamera"]
    process = subprocess.run(command, capture_output=True, text=True)
    pid = process.stdout.replace("\n", "")
    command = ["ps", "M", pid]
    process = subprocess.run(command, capture_output=True, text=True)
    lines = process.stdout
    count = len(lines.splitlines()) - 2
    return count


def get_device(name):
    devices = pywemo.discover_devices()
    for device in devices:
        if device.name == name:
            return device
    return None


if __name__ == "__main__":
    device = get_device(DEVICE_NAME)
    if device is None:
        exit(f"Unable to find '{DEVICE_NAME}' on network")
    while True:
        if count_camera_threads() > 3:
            device.on()
        else:
            device.off()
        time.sleep(1)
like image 160
David Cotten Avatar answered Oct 17 '22 03:10

David Cotten