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

Viewed 2610

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.

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)
Related