DirectShow cast sampleGrabber to ISampleGrabber

℡╲_俬逩灬. 提交于 2019-12-23 13:00:00

问题


I have a strange error that I cannot wrap my head around. I have a graph created in a separate thread that runs and I'm trying to access the IBaseFilter sampleGrabber outside the thread which worked in a console application but I moved the code to a new project and where I'm trying to cast sampleGrabber to ISampleGrabber the runtime complains with a null reference exception. If I debug sampleGrabber it does have the interface ISampleGrabber however I cannot cast it anymore. Moving the code inside the thread running the graph allows me to cast it but its not ideal for my application.

How can a null reference exception appear by casting what clearly is a sampleGrabber to ISampleGrabber fail?


回答1:


The problem is that DirectShow filters are early COM classes that implement a subset of COM in part of reference counting, interfaces, monikers, persistence - basically all good things lasted for years - however they ignore apartments completely. DirectShow is multithreaded on its own and it is typical that there is a controlling thread, and there are worker streaming threads aside. DirectShow concepts assume you can easily pass interface pointers between threads and no marshaling is involved, expected and required.

Then came .NET with checking COM wrappers, and DirectShow.NET wrapped interface pointers as if they were fully featured apartment-aware COM pointers. At the same time Microsoft stopped giving updates to DirectShow (such as for example supplying Sample Grabber with free threaded marshaler) and eventually you hit the issue when on .NET you cannot do a supposedly simple thing with the interface pointer.

There is still absolutely no problem working with APIs in native code domain because you can skip marshaling there and work with direct pointers.

You build the graph on one apartment, and then you get a call back from Sample Grabber on another apartment (or, otherwise, in your scenario you just do something on a worker thread). You cannot use original interface pointers, esp. those in member variables, because .NET runtime check would hit apartment mismatch, esp, trying to marshal interface pointer for another apartment.

If it were your custom filter with source code, you could have added a custom IMarshal implementation or leverage free threaded marshaler to fix the .NET issue on native code side, or otherwise add a helper to pass pointers across apartments.

In .NET code domain the best approach would be to avoid working with pointers from multiple apartments. There possibly are choices, but the easiest one I think of off the top of my head is to

  • work in MTA to be able to have multiple threads accessing DirectShow interface pointers
  • use CLSID_FilterGraphNoThread version of Filter Graph Manager
  • initialize and terminate filter graph on a dedicated thread, which during graph operation dispatches window messages

That is, either use STA and no extra threads touching pointers, or otherwise not use STA.




回答2:


After playing around with IGlobalInterface and pondering over Roman's comment, I realized setting up the function that handles samplegrabber is best to be in another thread, thus bypassing STA.



来源:https://stackoverflow.com/questions/17282185/directshow-cast-samplegrabber-to-isamplegrabber

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!