Click here to Skip to main content
11,478,642 members (66,266 online)
Click here to Skip to main content

Creating Custom DirectShow SampleGrabber Filter for Windows Mobile

, 29 Nov 2008 CPOL 324.3K 3.7K 56
Rate this:
Please Sign up or sign in to vote.
This article explains how to write your custom DirectShow SampleGrabber filter for Windows Mobile.

Introduction

This article explains how to write your custom DirectShow SampleGrabber filter for Windows Mobile.

Background

In a recent project of mine, I needed to do some real-time video analysis in .NET CF. However the .NET API only allows for taking still images or recording videos, but it offers no way to access the video frame buffer so that I could parse the frames from the camera on the fly. So I decided to use the DirectShow API, which offers much better control of the video stream flow, but it still lacks the ISampleGrabber interface which is available under the complete DirectShow library for Windows. The remaining option was to write a custom DirectShow filter, which implements the ISampleGrabber interface and allows the developer to get access to the video buffer data. In this article, I will try to explain what I learned in the process.

You could use the sample code as a starting point to write your own filter or take the ready SampleGrabber filter and use it in your project. There are multiple references online which I used in the project, but as far as I am aware there is no complete "How to" on how to solve this problem.

Setting Up Your Visual Studio Project

Before your set up your Visual Studio project, you will need to install Windows Mobile SDK, Windows CE 5.0 and Windows CE 5.0 Platform builder which contains the BaseClasses library. In general, you don't necessarily use the BaseClasses, but it definitely makes your job a lot easier so I took advantage.

First create a Smart Devices DLL project with ATL support. You also need to add the header files from the Windows Mobile SDK and Platform Builder to your include path.

Writing the Filter

In this example, we will use a TransInPlaceFilter, which is a simplified version of the TransformFilter. We don't need to alter the data in any way, just to pass it to the client application. So we create a CSampleGrabber class, which also implements our custom interface with any additional functions. I added just the RegisterCallback function, which passes a function pointer from the client app. This function will be called whenever a MediaSample (a video frame) is passed through the filter, so the client can copy the data and do some processing.

// define the filter class
class CSampleGrabber : 
public CTransInPlaceFilter, public ISampleGrabber
{
  private:
  MANAGEDCALLBACKPROC callback;
  long m_Width;
  long m_Height;
  long m_SampleSize;
  long m_Stride;

  public:
  // instantiation
  CSampleGrabber( IUnknown * pOuter, HRESULT * phr, BOOL ModifiesData );
  ~CSampleGrabber();
  static CUnknown *WINAPI CreateInstance(LPUNKNOWN punk, HRESULT *phr);

  // IUnknown
  DECLARE_IUNKNOWN;
  STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, void ** ppv);

  // CTransInPlaceFilter
  HRESULT CheckInputType(const CMediaType *pmt);
  HRESULT SetMediaType(PIN_DIRECTION direction, const CMediaType *pmt);
  HRESULT Transform(IMediaSample *pMediaSample);
  HRESULT DecideBufferSize(IMemAllocator *pAlloc, ALLOCATOR_PROPERTIES *pProperties);
  HRESULT GetMediaType(int iPosition, CMediaType *pMediaType);
  HRESULT CheckTransform(const CMediaType *mtIn, const CMediaType *mtOut) {
    return NOERROR; 
    }

  // ISampleGrabber
  STDMETHODIMP RegisterCallback(MANAGEDCALLBACKPROC mdelegate);
};

You could implement all interfaces like any other filter. The interesting parts are the Transform and RegisterCallback functions:

// Set the client callback
STDMETHODIMP
CSampleGrabber::RegisterCallback( MANAGEDCALLBACKPROC mdelegate ) 
{
    // Set pointer to managed delegate
    callback = mdelegate;
    return S_OK;
}

// Get the pointer to the raw data and pass it to the applications
HRESULT 
CSampleGrabber::Transform(IMediaSample *pMediaSample)
{
    long Size = 0;
    BYTE *pData;

    if ( !pMediaSample )
        return E_FAIL;

    // Get pointer to the video buffer data
    if( FAILED(pMediaSample->GetPointer(&pData)) ) 
        return E_FAIL;
    Size = pMediaSample->GetSize();

    // invoke managed delegate
    if ( callback )
        callback(pData,Size);

    return S_OK;
}

Using the Code

You can use the SampleGrabber filter directly in your DirectShow applications exactly like any other filter. It is important to register the DLL before trying to instantiate it.

// Create and initialize the SampleGrabber filter
CoCreateInstance( CLSID_SampleGrabber, NULL, CLSCTX_INPROC, 
		IID_IBaseFilter, (void**)&pSampleGrabber );
m_pFilterGraph->AddFilter( pSampleGrabber, FILTERNAME );

// Get a pointer to the ISampleGrabber interface
pSampleGrabber->QueryInterface( IID_ISampleGrabber, (void**)&m_pISampleGrabber );

// Register the client callback
if ( m_pISampleGrabber )
  m_pISampleGrabber->RegisterCallback( &CGraphBuilder::OnSampleProcessed );

When the client receives the frame sample, it should copy it to local buffer as soon as possible and return. This allows the filter to carry on and not wait for the client to process the data, which will considerably slow down the whole graph.

You could also make a C++ DLL which creates the filter graph and manages it and calls it through P/Invoke from your .NET CF applications. Creating filter graphs in .NET CF directly should be a bit trickier, as the .NET CF lacks C++. NET support, but maybe possible.

Online References

History

  • 23.08.2008 Article created
  • 26.08.2008 v1.1 Created.
    Fixes:
    • Release now builds OK, definition of NonDelegatingRelease added
    • Included C++ and C# client applications which show how to use the filter
    • The sample code now compiles under WM SDK 5
  • 28.11.2008
    • Added more sample code: CameraCaptureDLL project includes the CameraCapture sample and the builtin SampleGrabber filter. The TestCameraCapture project is a sample C# application that uses the filter to get the real-time frame data.
    • Note: The sample code demonstrates the basic concepts, but there are a couple of issues with it. For example, it might get stuck when trying to stop the filter graph. This is due to the fact that the filter is processing data while you try to do that (sorry but I didn't have time to fix that). Although buggy, you should be able to get the camera data.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Angel Kafazov
Technical Lead CST
Bulgaria Bulgaria
I am software engineering working for CST. My interests cover areas of mobile development, unified communications, VOIP, telecommunications and many more.

For further information please contant me at akafazov@cst-bg.net
Follow on   Twitter

Comments and Discussions

 
QuestionCan this project build for WinCE? Pin
Member 1062955627-Feb-14 0:02
memberMember 1062955627-Feb-14 0:02 
Questionblurred image LG GM750 smartphone Pin
Lawrence Tsang24-Oct-11 19:35
memberLawrence Tsang24-Oct-11 19:35 
AnswerRe: blurred image LG GM750 smartphone Pin
Lawrence Tsang26-Oct-11 22:00
memberLawrence Tsang26-Oct-11 22:00 
Generalcrash cocreateInstance CSampleGrabeer Pin
joseanaya2-May-11 16:03
memberjoseanaya2-May-11 16:03 
GeneralRegistering the DLL Pin
fakepoo3-Mar-11 13:58
memberfakepoo3-Mar-11 13:58 
GeneralRe: Registering the DLL Pin
Angel Kafazov7-Mar-11 1:45
memberAngel Kafazov7-Mar-11 1:45 
GeneralISamplegrabber not working on winmo device [modified] Pin
Member 372985127-Nov-10 21:56
memberMember 372985127-Nov-10 21:56 
General[Exception] System.IO.IOException [modified] Pin
bubb13t3a10-Oct-10 16:38
memberbubb13t3a10-Oct-10 16:38 
GeneralRe: [Exception] System.IO.IOException [modified] Pin
Y07004016-Oct-10 21:48
memberY07004016-Oct-10 21:48 
GeneralRe: [Exception] System.IO.IOException Pin
bubb13t3a19-Oct-10 23:45
memberbubb13t3a19-Oct-10 23:45 
GeneralRe: [Exception] System.IO.IOException Pin
Y07004020-Oct-10 0:03
memberY07004020-Oct-10 0:03 
GeneralRe: [Exception] System.IO.IOException Pin
bubb13t3a20-Oct-10 0:55
memberbubb13t3a20-Oct-10 0:55 
GeneralRe: [Exception] System.IO.IOException Pin
Y07004020-Oct-10 17:46
memberY07004020-Oct-10 17:46 
GeneralRe: [Exception] System.IO.IOException Pin
bubb13t3a24-Oct-10 18:27
memberbubb13t3a24-Oct-10 18:27 
GeneralRe: [Exception] System.IO.IOException Pin
Y07004025-Oct-10 6:01
memberY07004025-Oct-10 6:01 
QuestionFrameGrabber only works in Emulator, not on actuall Device Pin
trimber9-Aug-10 1:43
membertrimber9-Aug-10 1:43 
AnswerRe: FrameGrabber only works in Emulator, not on actuall Device Pin
Y07004019-Aug-10 18:58
memberY07004019-Aug-10 18:58 
AnswerRe: FrameGrabber only works in Emulator, not on actuall Device Pin
bubb13t3a10-Oct-10 16:44
memberbubb13t3a10-Oct-10 16:44 
Generalchanging resolution Pin
s0pHoc4-Aug-10 5:05
members0pHoc4-Aug-10 5:05 
AnswerRe: changing resolution [modified] Pin
Y07004019-Aug-10 17:21
memberY07004019-Aug-10 17:21 
Generalunresolved external symbol "public: virtual unsigned long __cdecl CBaseFilter::NonDelegatingRelease(void)" Pin
neosurya3-Jun-10 13:40
memberneosurya3-Jun-10 13:40 
GeneralRe: unresolved external symbol "public: virtual unsigned long __cdecl CBaseFilter::NonDelegatingRelease(void)" Pin
Linda Rawson28-Sep-10 7:09
memberLinda Rawson28-Sep-10 7:09 
AnswerRe: unresolved external symbol "public: virtual unsigned long __cdecl CBaseFilter::NonDelegatingRelease(void)" Pin
Y0700402-Oct-10 23:07
memberY0700402-Oct-10 23:07 
Questionhow to get a samplegrabber instance simply by using method cocreateinstance Pin
stonewind2-May-10 4:51
memberstonewind2-May-10 4:51 
AnswerRe: how to get a samplegrabber instance simply by using method cocreateinstance Pin
Y0700402-May-10 16:07
memberY0700402-May-10 16:07 
GeneralRe: how to get a samplegrabber instance simply by using method cocreateinstance Pin
stonewind2-May-10 21:41
memberstonewind2-May-10 21:41 
GeneralRe: how to get a samplegrabber instance simply by using method cocreateinstance Pin
Y0700402-May-10 22:07
memberY0700402-May-10 22:07 
GeneralRe: how to get a samplegrabber instance simply by using method cocreateinstance [modified] Pin
stonewind2-May-10 23:28
memberstonewind2-May-10 23:28 
GeneralRe: how to get a samplegrabber instance simply by using method cocreateinstance Pin
Y0700403-May-10 16:18
memberY0700403-May-10 16:18 
GeneralRe: how to get a samplegrabber instance simply by using method cocreateinstance Pin
stonewind3-May-10 20:43
memberstonewind3-May-10 20:43 
GeneralRe: how to get a samplegrabber instance simply by using method cocreateinstance Pin
Y0700403-May-10 23:14
memberY0700403-May-10 23:14 
GeneralRe: how to get a samplegrabber instance simply by using method cocreateinstance Pin
stonewind4-May-10 21:38
memberstonewind4-May-10 21:38 
GeneralRe: how to get a samplegrabber instance simply by using method cocreateinstance Pin
Y0700404-May-10 22:14
memberY0700404-May-10 22:14 
GeneralRe: how to get a samplegrabber instance simply by using method cocreateinstance Pin
stonewind4-May-10 23:15
memberstonewind4-May-10 23:15 
GeneralRe: how to get a samplegrabber instance simply by using method cocreateinstance Pin
Y0700405-May-10 0:13
memberY0700405-May-10 0:13 
GeneralRe: how to get a samplegrabber instance simply by using method cocreateinstance Pin
stonewind5-May-10 23:54
memberstonewind5-May-10 23:54 
QuestionErrors Occured Pin
stonewind27-Apr-10 23:34
memberstonewind27-Apr-10 23:34 
AnswerRe: Errors Occured [modified] Pin
Y07004028-Apr-10 16:35
memberY07004028-Apr-10 16:35 
GeneralRe: Errors Occured Pin
stonewind2-May-10 5:17
memberstonewind2-May-10 5:17 
GeneralSetting owner Pin
Big D7-Mar-10 22:22
memberBig D7-Mar-10 22:22 
AnswerRe: Setting owner Pin
Angel Kafazov8-Mar-10 8:43
memberAngel Kafazov8-Mar-10 8:43 
GeneralRe: Setting owner Pin
Big D18-Mar-10 5:31
memberBig D18-Mar-10 5:31 
AnswerRe: Setting owner Pin
Angel Kafazov18-Mar-10 5:48
memberAngel Kafazov18-Mar-10 5:48 
GeneralRe: Setting owner Pin
baltazar0074-Apr-10 4:28
memberbaltazar0074-Apr-10 4:28 
AnswerRe: Setting owner [modified] Pin
Y07004018-Apr-10 18:53
memberY07004018-Apr-10 18:53 
GeneralHTC HD2 Pin
Marten1238-Feb-10 13:19
memberMarten1238-Feb-10 13:19 
AnswerRe: HTC HD2 [modified] Pin
Y07004014-Apr-10 18:35
memberY07004014-Apr-10 18:35 
GeneralError under WM5 Pin
sunbite12-Jan-10 0:08
membersunbite12-Jan-10 0:08 
AnswerRe: Error under WM5 Pin
Angel Kafazov12-Jan-10 1:41
memberAngel Kafazov12-Jan-10 1:41 
GeneralRe: Error under WM5 Pin
sunbite12-Jan-10 2:19
membersunbite12-Jan-10 2:19 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 29 Nov 2008
Article Copyright 2008 by Angel Kafazov
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid