|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
AbstractThis 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! DirectShowDirectShow 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':
.NET InteropWhile 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:
// ======== 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... 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(
Projects StructureThe 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
PlaybackThe 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 PlayerFor 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 PictureThe 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 CapturingThe 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
|
||||||||||||||||||||||