Click here to Skip to main content
15,896,063 members
Articles / Programming Languages / C#
Article

DirectShow.NET

Rate me:
Please Sign up or sign in to vote.
4.94/5 (193 votes)
22 Jul 2002Public Domain3 min read 4.2M   34K   453   840
DirectShow for DVD and file playback, capture and sample grabber

Sample Image - directshownet.jpg

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 :
// ======== IDL of ICaptureGraphBuilder2 (AXExtend.idl) ======
[
    object,
    uuid(93E5A4E0-2D50-11d2-ABFA-00A0C9C6E38D),
    pointer_default(unique)
]
interface ICaptureGraphBuilder2 : IUnknown {

    // Use this filtergraph
    HRESULT SetFiltergraph( [in] IGraphBuilder *pfg );

    // what filtergraph are you using?
    // *ppfg->Release() when you're done with it
    HRESULT GetFiltergraph( [out] IGraphBuilder **ppfg);
    ....
... using Interop attributes with C# will be translated to :
C#
// ======== C# version of ICaptureGraphBuilder2 (DsExtend.cs) ======

   [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++:

// ======== C++ code to create the COM instance of Filter Graph ========

    JIF(CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, 
                         IID_IGraphBuilder, (void **)&pGB));

    // Have the graph builder construct its the <BR>    // appropriate graph automatically
    JIF(pGB->RenderFile(wFile, NULL));

    // QueryInterface for DirectShow interfaces
    JIF(pGB->QueryInterface(IID_IMediaControl, (void **)&pMC));
    ....
... we replace CoCreateInstance with Activator.CreateInstance, and QueryInterface just is a simple cast in C#:
C#
// ======== C# code to create the COM instance of Filter Graph ========

    Type comtype = null;
    object comobj = null;
    try {
        comtype = Type.GetTypeFromCLSID( Clsid.FilterGraph );
        if( comtype == null )
            throw new NotSupportedException( <BR>                "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\              // the DirectShow interface definitions :
              \DsBugWO.cs      // workaround for a bug 
              \DsControl.cs    // ported from control.odl 
              \DsCore.cs       // ported from axcore.idl 
              \DsDevice.cs     // device enumerator, helper functions 
              \DsDVD.cs        // DVD interfaces from dvdif.idl 
              \DsExtend.cs     // ported from axextend.idl 
              \DsUtils.cs      // utility classes, SDK Common sources 
              \DsUuids.cs      // UUIDs and CLSIDs from uuids.h 
              \QEdit.cs        // grabber interfaces from qedit.idl 

     \CaptureNET\            // video stream capture sample 
     \DVDPlayerNET\          // DVD player sample 
     \PlayWndNET\            // simple media file playback 
     \SampleGrabberNET\      // picture grabber 

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.

DirectShow playback

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.

DirectShow DVD

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!

DirectShow picture grabber

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.

DirectShow Capturing

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.

License

This article, along with any associated source code and files, is licensed under A Public Domain dedication


Written By
Web Developer
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: adding my own filter fails Pin
Member 210160121-Jul-05 5:50
Member 210160121-Jul-05 5:50 
GeneralRe: adding my own filter fails Pin
lgs.lsg21-Jul-05 16:54
lgs.lsg21-Jul-05 16:54 
GeneralRe: adding my own filter fails Pin
Member 210160121-Jul-05 23:08
Member 210160121-Jul-05 23:08 
GeneralRe: adding my own filter fails Pin
lgs.lsg22-Jul-05 11:36
lgs.lsg22-Jul-05 11:36 
GeneralDoes not work with my PVR USB2 Pin
badubo15-Jul-05 11:19
badubo15-Jul-05 11:19 
GeneralRe: Does not work with my PVR USB2 Pin
lgs.lsg16-Jul-05 14:40
lgs.lsg16-Jul-05 14:40 
GeneralRe: Does not work with my PVR USB2 Pin
badubo17-Jul-05 3:24
badubo17-Jul-05 3:24 
GeneralRe: Does not work with my PVR USB2 Pin
lgs.lsg17-Jul-05 8:22
lgs.lsg17-Jul-05 8:22 
Well, the good news is that if you can make it work thru GraphEdit, you can make it work thru DirectShow. It may just require some tweaking.

I will say this is one of the more complicated graphs I have seen for viewing video. Probably because they are using both MPEG + CC. I would be surprised to learn that Hauppage ONLY outputs mpeg. But even if they do, it can be made to work.

There is a library based on this code at (http://www.codeproject.com/cs/media/DirectXCapture.asp[^]) that works with video tuners. That might be worth a shot. Try running the CaptureTest program and see if it works. At a minimum, you would need to

1) Select a device with Device/Video Devices. (Do Device/Audio device too if you like)
2) Select a Video Source with Options/Video Source.
3) If you are using Video Tuner (rather than Video Composite), you will need to set Options/TV Tuner Channel and Options/TV Tuner Source.

From here, try Options/Preview. You may need to play with some of the settings under Options/Property Pages.

There is a version of CaptureTest with a few extra features and a few bug fixes available. You would first need to get the DirectShowNet.SourceForge.net[^] library, then get the CaptureTest from http://www.LimeGreenSocks.com/DShow[^].

Also at LimeGreenSocks there is a program that dumps out the capabilities of video capture devices (DumpVCap). It would be interesting to see what it shows for your device. If you would be willing to email me a copy of the output, my address is at the bottom of the LimeGreenSocks page.
Questionhow to enable dvd menu Pin
abstarsss11-Jul-05 17:10
abstarsss11-Jul-05 17:10 
AnswerRe: how to enable dvd menu Pin
lgs.lsg13-Jul-05 22:37
lgs.lsg13-Jul-05 22:37 
GeneralRe: how to enable dvd menu Pin
abstarsss14-Jul-05 16:22
abstarsss14-Jul-05 16:22 
GeneralRe: how to enable dvd menu Pin
lgs.lsg15-Jul-05 9:39
lgs.lsg15-Jul-05 9:39 
GeneralRe: how to enable dvd menu Pin
abstarsss15-Jul-05 11:50
abstarsss15-Jul-05 11:50 
GeneralRe: how to enable dvd menu Pin
lgs.lsg15-Jul-05 12:16
lgs.lsg15-Jul-05 12:16 
GeneralRe: how to enable dvd menu Pin
abstar15-Jul-05 19:59
abstar15-Jul-05 19:59 
QuestionHow to locate IDL files Pin
Anonymous11-Jul-05 17:00
Anonymous11-Jul-05 17:00 
AnswerRe: How to locate IDL files Pin
lgs.lsg13-Jul-05 22:33
lgs.lsg13-Jul-05 22:33 
GeneralIEnumMediaType Pin
catchbubbles1-Jul-05 0:11
catchbubbles1-Jul-05 0:11 
GeneralRe: IEnumMediaType Pin
lgs.lsg2-Jul-05 11:46
lgs.lsg2-Jul-05 11:46 
GeneralDsGetPin Missing in DShowNET Pin
rasatavohary23-Jun-05 22:27
rasatavohary23-Jun-05 22:27 
GeneralRe: DsGetPin Missing in DShowNET Pin
rasatavohary23-Jun-05 22:31
rasatavohary23-Jun-05 22:31 
GeneralNew DirectShow library for C# &amp; VB available Pin
lgs.lsg20-Jun-05 20:09
lgs.lsg20-Jun-05 20:09 
Generalproblem creating vmr 9 Pin
Member 161799916-Jun-05 6:27
Member 161799916-Jun-05 6:27 
GeneralRe: problem creating vmr 9 Pin
lgs.lsg16-Jun-05 21:25
lgs.lsg16-Jun-05 21:25 
GeneralRe: problem creating vmr 9 Pin
Member 161799917-Jun-05 2:57
Member 161799917-Jun-05 2:57 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.