Disclaimer: Experimental code using DirectShow with the .NET Framework 1.0
Abstract
This experimental code shows how to use DirectShow with .NET and C#. This includes simple media playback, playing DVD discs, capturing video streams to disk and a sample picture grabber.
Note, this article doesn't save you from reading the detailed DirectShow SDK documentation! I will not explain DirectShow, only some of the used .NET Interop technologies!
DirectShow
DirectShow is a standardized Microsoft Win32 API to use any compliant movie or video device from your application. DirectShow is available with the current DirectX version 8.1(b) for Windows 98/ME/2000 and included in XP. Please install the latest version, this article doesn't support anything except 8.1 :
Again, I will not describe any DirectShow interfaces, you have to know them by installing the SDK for C++, reading the SDK documentation and understanding the SDK samples!
DirectShow is exposed as COM components and interfaces, at these two 'levels':
- DirectShow custom interfaces - mainly for C++ programmers.
- DirectShow VB components - designed for VB6, provides a type library.
You can use the DirectShow playback components for VB6 with .NET, as described in this CodeProject article:
DirectShow MediaPlayer in C# (Daniel Strigl)
.NET Interop
While using the VB6 components with the provided type library is easy with .NET, there is no direct way to access the custom DirectShow interfaces. We have to use Interop with one of this approaches:
- Use 'Managed Extensions for C++', as done e.g. by DirectX.NET
- Rewrite all the interfaces from IDL to e.g. C# !
I chose the second strategy for this reasons :
- Uses only one (managed) language (C#)
- Most DirectShow interfaces are not very complex
- DirectShow methods for simple playback/capturing are not time-critical
- We can directly use the (documented) interfaces without limitations, no 'wrapper classes'
Sure, this has some drawbacks:
- Much of initial work for rewriting the interfaces
- You have to understand Interop to use it correctly
- Not very .NET/OO-like
One typical rewrite of an IDL interface in C# looks like this :
[
object,
uuid(93E5A4E0-2D50-11d2-ABFA-00A0C9C6E38D),
pointer_default(unique)
]
interface ICaptureGraphBuilder2 : IUnknown {
HRESULT SetFiltergraph( [in] IGraphBuilder *pfg );
HRESULT GetFiltergraph( [out] IGraphBuilder **ppfg);
....
... using Interop attributes with C# will be translated to :
[ComVisible(true), ComImport,
Guid("93E5A4E0-2D50-11d2-ABFA-00A0C9C6E38D"),
InterfaceType( ComInterfaceType.InterfaceIsIUnknown )]
public interface ICaptureGraphBuilder2
{
[PreserveSig]
int SetFiltergraph( [In] IGraphBuilder pfg );
[PreserveSig]
int GetFiltergraph( [Out] out IGraphBuilder ppfg );
....
Once we have all this interface definitions in C#, we can start calling DirectShow just like we did in C++:
JIF(CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER,
IID_IGraphBuilder, (void **)&pGB));
JIF(pGB->RenderFile(wFile, NULL));
JIF(pGB->QueryInterface(IID_IMediaControl, (void **)&pMC));
....
... we replace
CoCreateInstance
with
Activator.CreateInstance
, and
QueryInterface
just is a simple
cast in C#:
Type comtype = null;
object comobj = null;
try {
comtype = Type.GetTypeFromCLSID( Clsid.FilterGraph );
if( comtype == null )
throw new NotSupportedException(
"DirectX (8.1 or higher) not installed?" );
comobj = Activator.CreateInstance( comtype );
graphBuilder = (IGraphBuilder) comobj; comobj = null;
int hr = graphBuilder.RenderFile( clipFile, null );
if( hr < 0 )
Marshal.ThrowExceptionForHR( hr );
mediaCtrl = (IMediaControl) graphBuilder;
....
Projects Structure
The download contains all this C# source code:
\DirectShow\
\DShowNET\
\DsBugWO.cs
\DsControl.cs
\DsCore.cs
\DsDevice.cs
\DsDVD.cs
\DsExtend.cs
\DsUtils.cs
\DsUuids.cs
\QEdit.cs
\CaptureNET\
\DVDPlayerNET\
\PlayWndNET\
\SampleGrabberNET\
Playback
The first sample included in the download is PlayWndNET. It plays the known video and audio file formats of DirectShow like avi, mpg, wav, mid etc.
DVD Player
For the next sample, DVDPlayerNET you must have a third-party DVD codec installed, like WinDVD or PowerDVD. Then, the C# sample uses the DirectShow DVD interfaces to watch the movie. It also supports menu navigation.
Grab Picture
The most complex sample provided is SampleGrabberNET. It shows a live video stream from a capture device like DV cam, web cam or TV card in a preview window. By pressing the 'Grab' toolbar-button, you can capture a still picture to a 24-Bit RGB bitmap file!
The sample also supports the IAMTVTuner
interface of a TV card, so you can switch the TV tuner channel.
Capturing
The last sample, CaptureNET can be used to capture a live video stream to disk. Note, the few settings can only be done once at startup, and writing to the AVI file starts immediately.
Limitations
- EXPERIMENTAL! don't use it in production quality code.
- The samples only provide partial and very basic functionality.
- I did most tests on Windows XP and few on Windows ME.
- Tested only on a very limited set of devices with only few media formats.
I used a Logitech QuickCam, Sony DV camcorder, Hauppauge WinTV PCI and WinDVD.
- Get the latest driver (WDM) from manufacturer.
- Some devices fail if you select unsupported settings in the dialogs.
- This code will NOT help to solve any DirectShow/WDM configuration problems.
- Get >128MB RAM, >400MHz CPU, fast & huge harddisk.