9

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.

Dan Morrow
  • 4,433
  • 2
  • 31
  • 45
  • Does this answer your question? [How to detect microphone usage on OS X?](https://stackoverflow.com/questions/39574616/how-to-detect-microphone-usage-on-os-x) – torkel May 07 '20 at 15:18
  • I just had this exact same idea. was going to ask this guy if he would open source his app https://objective-see.com/products/oversight.html – jin Aug 13 '20 at 03:45
  • Actually, might just be able to parse the logs from Oversight to get what we need – jin Aug 13 '20 at 03:47
  • My most recent answer to [How to detect microphone usage on OS X](https://stackoverflow.com/questions/39574616/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](https://github.com/sindresorhus/is-camera-on-cli) which can be easily installed and offers a CLI as well as API to detect the camera. – kgutwin Oct 23 '20 at 19:43
  • You can use `/usr/bin/log stream --predicate 'eventMessage contains "Post event kCameraStream"'` to check the state of webcams. I just created https://github.com/henrik242/OnAir that wraps around this to turn on/off a bulb using a MQTT message to my smarthome setup. – neu242 Sep 23 '21 at 11:33

1 Answers1

5

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)
David Cotten
  • 71
  • 1
  • 3