DirectShowNet - Error when recreating graph for webcam after unplug/replug back in

0

I've spent almost 4 hours trying to figure out a solution for this issue. Basically, I am writing an application in C# using DirectShowNet that shows video from a webcam, allows you to take snapshots, and change resolution of the webcam. I have a device manager that detects when a webcam is unplugged, then replugged back in. However, I'm having difficultly recreating the graph after the device has been plugged back in.

The first thing I do is create the FilterGraph object and cast it to a IFilterGraph3. Then, I make this call:

graphBuilder.AddSourceFilterForMoniker(deviceMoniker, null, deviceName, out sourceFilter)

This call works when you initially launch the application, and even if you Dispose of the graph and create a new one. However, the only time is doesn't work is when trying to create the graph after the device is plugged back in.

On one machine (Win8 x64), I am receiving this error:

REGDB_E_CLASSNOTREG - 0x80040154 - Class not registered

On another machine (Win7 x64), I am receiving this error:

REGDB_E_IIDNOTREG - 0x80040155 - Interface not registered

I have tested both building this code as Any CPU and x86. Also, I have tested it on other machines, but independent of the OS, I still get the Interface not registered error. I have tried using both a Logitech C920 and a Microsoft LifeCam Studio. Both result in the same error. I have also tried the alternative, using the IMoniker method:

deviceMoniker.BindToObject(null, null, ref baseFilterId, out source);

Same error occurs. I feel like there may be something that is lingering after the device is disconnected that I am not cleaning up to allow the graph to grab the source (webcam filter) properly. Any ideas would be appreciated.

General HRESULT error codes can be found here: COM Error Codes (Generic)

c#
com
webcam
directshow
directshow.net
asked on Stack Overflow Aug 7, 2013 by Michael Yanni

1 Answer

2

So, basically, I just woke up one morning, got to work, and stared at the code and went, "Wait, I know why this isn't working...". It is a threading issue. The initial graphs I was creating were on a different thread than the graphs I was trying to create later. That's why it only happens when I recreate the graphs.

I was also having a problem with IVideoWindow's put_Owner method when the device is plugged in after the application has already started. In this case, the device wasn't unplugged while executing, and put_Owner would freeze/deadlock trying to that method.

Overall, I have a thread that is detecting when a device is unplugged/plugged in using DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice). So, when I go to create these graphs, they are not on the main UI thread where the other graphs were originally created. To solve this, I have my class that is handling the devices take in an Action<Action> invoker which allows you to provide a method wrapper. In this case, I pass it Control.Invoke of the WinForms form so that the calls on the device monitoring thread are wrapped by this invoker method within the UI thread. Completely solved my graph creation issues.

I am HIGHLY SURPRISED no one else ran into this before. I couldn't find anyone else mentioning threading when discussing issues related to this error. Very strange.

answered on Stack Overflow Aug 12, 2013 by Michael Yanni

User contributions licensed under CC BY-SA 3.0