Tobias Timpe
Tobias Timpe

Reputation: 770

Get current state of webcam in C#

I am trying to figure out how to check if a webcam/video capture device is already being used by another application without actually activating it.

My current approach is to use the AForge.NET library and using the .IsRunning property of the VideoCaptureDevice object like this:

 var videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
                
 foreach (FilterInfo videoDevice in videoDevices)
 {
    VideoCaptureDevice camera new AForge.Video.DirectShow.VideoCaptureDevice(videoDevice.MonikerString);
    Debug.Print(camera.IsRunning)
 }

I guess the IsRunning property only works on VideoCaptureDevices that have been started using the library and I need lower-level DirectShow access to the device.

While there are many ways to use DirectShow in C#, I have been unable to find a way to check the state even using DirectShow in C++. Is there some magic I need to perform here?

Thanks

Tobias Timpe

Upvotes: 3

Views: 2631

Answers (1)

Matt Whitfield
Matt Whitfield

Reputation: 6574

I'm not entirely sure if this will be helpful to you, but I found your question because I wanted to write a custom app to control my busylight. This is very much 'Works On My Machine' certified - it's not an attempt to give a general answer. However, I figure it may help you, and possibly the next person who comes across this page while googling...

private static bool IsWebCamInUse()
{
    using (var key = Registry.CurrentUser.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\CapabilityAccessManager\ConsentStore\webcam\NonPackaged"))
    {
        foreach (var subKeyName in key.GetSubKeyNames())
        {
            using (var subKey = key.OpenSubKey(subKeyName))
            {
                if (subKey.GetValueNames().Contains("LastUsedTimeStop"))
                {
                    var endTime = subKey.GetValue("LastUsedTimeStop") is long ? (long) subKey.GetValue("LastUsedTimeStop") : -1;
                    if (endTime <= 0)
                    {
                        return true;
                    }
                }
            }
        }
    }

    return false;
}

Upvotes: 10

Related Questions