C# Jukebox version 3: Cooking a custom library PDF Print E-mail

As others have noticed, the DirectX SDK contains the idl files for DirectShow. So whenever an interface that we need is not exported from the interop assembly generated from quartz.dll. We can create our own type library and use the .Net utility "tlbimp" to create our own custom assembly. Again a zip file with the code is provided.

We'll go through the steps needed to create this custom assembly in this tutorial. We'll return to version 1 of the Jukebox sample in C# and create a custom assembly that will allow us to access the IVideoFrameStep that is needed in order to implement stepping from one frame to the next in a video.

The code to add to the first version of the Jukebox sample is minimal and we'll concentrate on the steps needed to build the custom assembly. We want to access the IVideoFrameStep interface supported by a COM object of type CLSID_FilterGraph but not exported by the interop assembly generated by VS (or tlbimp). As Eric Gunnerson pointed out, we can create a small idl file that defines a library with the interfaces that we need, here is the file (named "ivfs.idl") that I used for our task:

  1.  
  2. import "devenum.idl";
  3. import "axcore.idl";
  4. import "axextend.idl";
  5. [
  6. uuid(2F8A7E2C-F846-48ef-84FB-599751EC62C7),
  7. helpstring("IVideoFrameStep type library")
  8. ]
  9. library IVFSTypeLib
  10. {
  11. importlib("STDOLE2.TLB");
  12. interface IVideoFrameStep;
  13. };
 

You can throw in any of the interfaces supported by a COM CLSID_FilterGraph (as listed in the SDK documentation), and you'll be able to access these interfaces once we have completed building our custom assembly. For the Jukebox sample, I needed the IVideoFrameStep interface, so that's the only one that will appear in my library. The GUID was generated from VS (menu Tools, Create GUID).

Now that I have this idl file, I'll use the MIDL tool to spit out a type library (.tlb file) that we'll feed to "tlbimp". Under VS, MIDL is included in the "common\tools\bin"; so you have to add this path to the paths accessible from the environment (where you start a command shell). Also, MIDL needs the paths for the included file, here devenum.idl,... and also those include by these files e.g. oaidl.idl. The paths for accessing MIDL and the include directories on my machine are set with the following commands taken from the "batch" file (named "build_ivfs.cmd" provided in the zip file that comes with this tutorial):

  1.  
  2. set PATH=%PATH%;"C:\Program Files\Microsoft Visual Studio .NET\Common7\Tools\Bin"
  3. midl /I "C:\Program Files\Microsoft Visual Studio .NET\Vc7\PlatformSDK\Include" \
  4. /I "C:\directx\Include\DShowIDL" ivfs.idl
 

The first line sets the path to the midl compiler (on my machine) and the second line provides the needed "include" directories for the library and the command to run the MIDL compiler and generate a file that we'll named "ivfs.tlb". (You have to change these to reflect the correct locations on your machine). Then calling "tlbimp ivfs.tlb" gives us our custom assembly that we can use to access the IVideoFrameStep interface from C#. First, we need the corresponding using statement:

  1.  
  2. using IVFSTypeLib;
 

This statement uses the name we've defined for the library in the .idl file. The IVideoFrameStep interface reference is declared exactly the same way as we did in the 2nd version of the Jukebox sample.

  1. IVideoFrameStep frameStep = null;
 

Now, to check the ability to step from frame to frame, we rely on exceptions:

  1. //check for the ability to step
  2. frameStep = graphBuilder as IVideoFrameStep;
  3. try
  4. {
  5. frameStep.CanStep( 1, null );
  6. canStep = true;
  7. buttonFramestep.Enabled = true;
  8. }
  9. catch( Exception ) {} //ignore exception, we can't step
 

This code is needed because the CanStep method return an error code to indicate its inability to step, and this error code gets translated into an exception in .Net. If there is an exception, then we can't step and we just ignore it since the boolean member variable canStep is set to false.

The rest of the code is identical to the first version of Jukebox except that we've added the handler for frame stepping (identical to the code in the second version of the Jukebox sample).

While, I prefer this code to the one in Version 2, I like the definitions for structures and constants of the DirectShowLib. So, at the moment, there is no ideal solution to access DirectShow from C# but there are three different approaches that can satisfies everyone needs and tastes.




Bookmark it...
Digg!Reddit!Del.icio.us!Google!Facebook!Slashdot!Technorati!StumbleUpon!Newsvine!Furl!Yahoo!Ma.gnolia!
 
< Prev   Next >
Joomla Template by Joomlashack
Joomla Templates by JoomlaShack Joomla Templates by Compass Design