| HOWTO: Write a DirectShow filter in C# (with a zest of MC++) |
|
I have just written a couple of DirectShow filters in C#. For example, I have ported the YUV filter from the MSDN mag to C#. The official party line from Redmond is that filters shouldn't be written in C# for "performance reasons". I've always been suspicious of wide, sweeping statements like this, especially when people are not yet familiar with a new technology. For the past few months, I have played with DirectShow and C# and performance hasn't been an issue. I can't say the same about resources management and threadings; the interactions between managed and native concepts in this area have been more puzzling. I believe that most people who have looked at the possibilities of writing DirectShow filters in C# walked away after looking at the task of porting the DirectShow base classes to C#. DMOs in C# are a possibility as David Wohlferd pointed out recently (in a comment of my Sept. 3th blog entry, and should be available in the next version of DirectShowLib). I have written my filters using Cutler's DirectShow Wizard from the DirectShow book. First I compiled the blank transform filter and register the filter, than I add the /clr switch (and a few project changes) and use the gcroot template to embed a pointer to my C# class that implement the filter functionality. Then, in all the member functions of the Wizard generated C++ code that need to be overriden, I forward a call to a corresponding method of my C# class. This is the "zest of MC++" that I alluded in the title. I had to create a custom library for the IMemAllocator and IMemInputPin interfaces to be able to implement the DecideBufferSize method properly. But there was nothing unusual here. After testing the approach on a couple of filters, I realized that being able to access the interfaces used in the DirectShow base classes is sufficient for most needs. And this is not hard to do in C#. Moreover, mixing MC++ and C# allow us to have a tigher control on the division of labour between the native and managed functionality.
I can't recommend developing DirectShow filters in C# with this approach without further
investigation. But for a prototype, or a "quick and dirty job", I believe it
is a path that should be explored. |
| < Prev | Next > |
|---|











