5,286,006 members and growing! (20,067 online)
Email Password   helpLost your password?
Multimedia » DirectX » General     Advanced License: A Public Domain dedication

DirectShow.NET

By NETMaster

DirectShow for DVD and file playback, capture and sample grabber
C#Windows, .NET, .NET 1.0, Win2K, WinXPVS.NET2002, VS, Dev

Posted: 21 Jul 2002
Updated: 22 Jul 2002
Views: 736,979
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
136 votes for this Article.
Popularity: 10.35 Rating: 4.85 out of 5
2 votes, 1.6%
1
0 votes, 0.0%
2
1 vote, 0.8%
3
12 votes, 9.3%
4
114 votes, 88.4%
5

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# 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 
// 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# 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( 
"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

About the Author

NETMaster



Occupation: Web Developer
Location: Switzerland Switzerland

Other popular DirectX articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 720 (Total in Forum: 720) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralHow to render Digital video file(.dv) using directshowmembersan07620:32 17 Jun '08  
QuestionCompact frameworkmemberAbhishek Ramesh Keshav12:41 17 Jun '08  
QuestionPlay big filesmemberjjdelgado7:25 17 Jun '08  
Generalhow to configure output sampling rate and number of channels??memberwzefzaf3:21 15 Jun '08  
GeneralAleatory Error "Could Not setup grahp"memberlazartefederico14:12 9 Jun '08  
QuestionWhy all the applications alert a message like "No video capture devices found!" during runtimememberrzzfch52019:25 5 Jun '08  
Generalwhy not just use tlbimp.exe to generate .NET wrapper interfaces for DirectShow?memberMember 27098057:01 2 Jun '08  
AnswerRe: why not just use tlbimp.exe to generate .NET wrapper interfaces for DirectShow? [modified]memberNETMaster8:04 2 Jun '08  
GeneralThanksmemberdivyesh143221:35 25 May '08  
GeneralSave avi without open mediacontrol windowmemberjotache9:10 15 May '08  
AnswerRe: Save avi without open mediacontrol windowmemberJasper Gielen5:15 26 May '08  
GeneralRe: Save avi without open mediacontrol windowmemberjotache18:10 26 May '08  
GeneralWhat I need to installmemberalhambra-eidos5:48 6 May '08  
QuestionHow Can I start Programming for CaptureCard ?memberen.Mahdi22:30 26 Apr '08  
Generalvideo capture [modified]memberSwapnil9630:07 24 Apr '08  
GeneralRe: video capturememberalhambra-eidos2:52 21 May '08  
Generaldefault values for property of capturememberOfori Boadu2:33 17 Apr '08  
GeneralPlay Vob filememberjcvu9:43 16 Apr '08  
GeneralCaptureNET FileSize 1.75GB???? How can i chnage this?memberasdasasasasasasas asas3:40 25 Mar '08  
GeneralRe: CaptureNET FileSize 1.75GB???? How can i chnage this?memberalhambra-eidos5:45 6 May '08  
QuestionHow to add text ?memberJurco012:40 19 Feb '08  
GeneralRe-Initialization of GraphmemberB!Z1:57 3 Feb '08  
GeneralMirror Video Stream on videoPanelmembervvolf_faktor3:35 22 Jan '08  
GeneralSampleGrabberNET in VB.NET ?memberstudent2510:24 30 Dec '07  
GeneralRe: SampleGrabberNET in VB.NET ?memberMember 347634817:35 4 Jan '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 22 Jul 2002
Editor: Nishant Sivakumar
Copyright 2002 by NETMaster
Everything else Copyright © CodeProject, 1999-2008
Web10 | Advertise on the Code Project