Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

fastfourier

macrumors newbie
Original poster
Feb 6, 2012
1
0
Hi everybody

I've done a bit of poking around on this issue, but no dice. Hopefully someone can help me...

I need some way of detecting when the webcam on my computer is in use (actually it is an external camera connected via firewire). I would presume it would go along the lines of trying to open the webcam device, and if it fails, then the webcam is in use. That's a bit inelegant though - so is there a better way? Maybe something that can be done through bash scripts?

I like to work in pitch black, but I have a lot of Skype conversations. I'd like to have my home automation server fade up the lights in my studio when the webcam comes on. This is the only bit I haven't figured out.

Can you help?

thanks

O
 
I think it would be easier to write a program that detects the launch of a Skype video chat; vs. trying to interface with your web-cam.
 
Here's a script that can detect it. I only tested with Skype though.

Save it to a file such as cam.rb and run it like: ruby cam.rb

Code:
[color=#aa0d91]require[/color] [color=#1c00cf]'osx/cocoa'[/color]
OSX.require_framework([color=#c41a16]"/System/Library/Frameworks/QTKit.framework"[/color])

[color=#aa0d91]class[/color] Watcher < OSX::NSObject
  [color=#aa0d91]def[/color] initialize
    devices = OSX::QTCaptureDevice.inputDevicesWithMediaType(OSX::QTMediaTypeVideo)
    abort [color=#c41a16]"No camera found."[/color] [color=#aa0d91]if[/color] !devices
    @device = devices.lastObject
    @device.addObserver_forKeyPath_options_context([color=#aa0d91]self[/color], [color=#c41a16]"inUseByAnotherApplication"[/color], [color=#1c00cf]0[/color], [color=#aa0d91]nil[/color])
    check()
  [color=#aa0d91]end[/color]
  
  [color=#aa0d91]def[/color] check
    inUse = @device.isInUseByAnotherApplication
    [color=#aa0d91]if[/color] inUse != @lastInUse
      [color=#aa0d91]if[/color] inUse
        puts [color=#c41a16]"'#{@device.localizedDisplayName}' is in use."[/color]
      [color=#aa0d91]else[/color]
        puts [color=#c41a16]"'#{@device.localizedDisplayName}' is not in use."[/color]
      [color=#aa0d91]end[/color]
      @lastInUse = inUse
    [color=#aa0d91]end[/color]
  [color=#aa0d91]end[/color]
  
  [color=#aa0d91]def[/color] observeValueForKeyPath_ofObject_change_context(keyPath, object, change, context)
    OSX::NSObject.cancelPreviousPerformRequestsWithTarget([color=#aa0d91]self[/color])
    [color=#aa0d91]self[/color].performSelector_withObject_afterDelay([color=#c41a16]"check"[/color], [color=#aa0d91]nil[/color], [color=#1c00cf]0.1[/color])
  [color=#aa0d91]end[/color]
[color=#aa0d91]end[/color]

watcher = Watcher.new
OSX::NSRunLoop.currentRunLoop.run
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.