I have a NoIR cam working with my Pi2 using the uv4l driver. I also have an external 12v IR led array that I would like to set up with the Pi. My plan was to set up a Pi controlled relay to turn on and off power to the led array so that it wasn't on all the time. Optimally, I'd like to have the relay triggered by some event created by a connection to the camera, in other words, for the leds to only be on when the cam is being viewed. does anyone know if this is possible, to have a connection to the camera be a triggering event for GPIO? The GPIO to the relay stuff I can figure out, it's just that I'm a total linux noob, so I'm not sure about how connections to the camera are registered, and how i can tap into that info to use it as an event. Any ideas?
How easy this is depends on how
raspivid works. I don't have my camera connected right now so I cannot check without digging into the source, but it is probably easier for you to just try my suggestion yourself.
The camera does have a device node, probably
/dev/video0 if there is only one camera connected. If
raspivid uses this,
lsof /dev/video0 should indicate whether any userspace processes are accessing it.
mmap() hacks on
/dev/mem instead of kernel drivers is a common practice on the pi, and if
raspivid does something like that, there will be no way to tell when it is running unless you just look for an application with that name (e.g.,
ps -C raspivid), which is only a problem if you are worried someone is going to use the system and try and circumnavigate your check, since that would then be as easy as copying
raspivid and running it with a different name.
As for doing this programmatically, running
lsof leads me to believe it actually checks all the file descriptors for all running processes via
/proc (which is probably why it is sometimes used with the setuid bit set, or else will not provide all the answers to an unprivileged user). You could do something like this yourself, or just parse lsof's output.
For more information, see
man lsof. It is a widely used tool with copious online commentaries.
You can easily do it by using
lsof (and UV4L):
sudo lsof /dev/video0
The above command prints out some lines if and only if the camera is being accessed. Based on the presence of the output, you can easily write your script to turn on your leds via GPIOs.
lsof manual to know how to run it in repeated mode for automatic, efficient polling every X seconds.
raspivid does not create or use any /dev/video nodes, so
lsof won't work with it.