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

1

10

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.

Stylopodium answered 7/5, 2020 at 15:16 Comment(5)
Does this answer your question? How to detect microphone usage on OS X?Newfeld
I just had this exact same idea. was going to ask this guy if he would open source his app objective-see.com/products/oversight.htmlDinge
Actually, might just be able to parse the logs from Oversight to get what we needDinge
My most recent answer to How to detect microphone usage on OS X gives a PyObjC code snippet that can detect the status of the microphone. For getting the camera status, there is an equivalent snippet of code, but I also recommend is-camera-on-cli which can be easily installed and offers a CLI as well as API to detect the camera.Impoverished
You can use /usr/bin/log stream --predicate 'eventMessage contains "Post event kCameraStream"' to check the state of webcams. I just created github.com/henrik242/OnAir that wraps around this to turn on/off a bulb using a MQTT message to my smarthome setup.Poncho
L
7

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 time
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)
Lepp answered 8/7, 2020 at 15:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.