PDA

View Full Version : How to tell if webcam is in use




fastfourier
Feb 7, 2012, 01:47 AM
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



lancestraz
Feb 8, 2012, 06:56 PM
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.

kainjow
Feb 8, 2012, 11:22 PM
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

require 'osx/cocoa'
OSX.require_framework("/System/Library/Frameworks/QTKit.framework")

class Watcher < OSX::NSObject
def initialize
devices = OSX::QTCaptureDevice.inputDevicesWithMediaType(OSX::QTMediaTypeVideo)
abort "No camera found." if !devices
@device = devices.lastObject
@device.addObserver_forKeyPath_options_context(self, "inUseByAnotherApplication", 0, nil)
check()
end

def check
inUse = @device.isInUseByAnotherApplication
if inUse != @lastInUse
if inUse
puts "'#{@device.localizedDisplayName}' is in use."
else
puts "'#{@device.localizedDisplayName}' is not in use."
end
@lastInUse = inUse
end
end

def observeValueForKeyPath_ofObject_change_context(keyPath, object, change, context)
OSX::NSObject.cancelPreviousPerformRequestsWithTarget(self)
self.performSelector_withObject_afterDelay("check", nil, 0.1)
end
end

watcher = Watcher.new
OSX::NSRunLoop.currentRunLoop.run

lancestraz
Feb 9, 2012, 09:17 PM
Oooorrrr not. Nice one, kainjow. I've been out of the game to long haha.