Click here to Skip to main content
13,894,768 members
Click here to Skip to main content
Add your own
alternative version


57 bookmarked
Posted 22 Feb 2010
Licenced CPOL

A Brief Tutorial On How To Develop DirectShow Source Filter For Beginners

, 22 Feb 2010
Rate this:
Please Sign up or sign in to vote.
The article will give you an idea on how to design a source filter.The source filter creates samples and outputs it from its output pin.


The article is a brief tutorial on how to design a directshow's source filter. Most of them reference the SDK's example. Download the demo and have a test using GraphEdit. It creates a video stream, add the "My Source Filter" to the GraphEdit, right click the "My Source OutPin" and select render pin. Okay, now it works! Press play and you'll see my photo floating on the video screen.

Principle of the Code

Before our development, be sure that the SDK is installed on your computer and configure it well in your VC or VS. After finishing the jobs, now going on:

Step 1

Build a project, for example we select the "Win32 Dynamic-Link Library" .

Step 2

Add the following libraries for your project: strmbasd.lib msvcrtd.lib quartz.lib vfw32.lib winmm.lib.

Include the header files we need:

#include <streams.h>
#include <olectl.h>
#include <initguid.h>
#include "MyStream.h" //This file is not system's ,its a class's header that we create.

Step 3

Find the tool "GUIDGEN.EXE" on your computer. (If you failed, look on the internet.) Start the tool, click the "New GUID" button, a new GUID will be created. Now, go to select the second option, in other words we want to "DEFINE_GUID(...)" format, after that click "copy" button and paste it in your project.

Step 4

Set data types, major type and minor type.

    &MEDIATYPE_Video,       // Major type
    &MEDIASUBTYPE_NULL      // Minor type

For our filter, we need an output pin. Do as follows. Get to set the pin's properties.

const AMOVIESETUP_PIN sudOpPin =
    L"Output",              // Pin string name
    FALSE,                  // Is it rendered
    TRUE,                   // Is it an output
    FALSE,                  // Can we have none
    FALSE,                  // Can we have many
    &CLSID_NULL,            // Connects to filter
    NULL,                   // Connects to pin
    1,                      // Number of types
    &sudOpPinTypes };       // Pin details

Setup the filter's properties:

    &CLSID_MySourceFilter,  	// Filter CLSID
    L"My Source Filter",    	// String name
    MERIT_DO_NOT_USE,       	// Filter merit
    1,                      	// Number pins
    &sudOpPin               	// Pin details

Step 5

Create a class that inherits from CSource. Why do we choose this class? Because we are going to develop a source filter. So, we must choose reasonable base classes.

class CMySourceFilter:public CSource
 // The only allowed way to create Bouncing balls!
    static CUnknown * WINAPI CreateInstance(LPUNKNOWN lpunk, HRESULT *phr);
 CMySourceFilter(LPUNKNOWN lpunk, HRESULT *phr);
CMySourceFilter::CMySourceFilter(LPUNKNOWN lpunk, HRESULT *phr):
    CAutoLock cAutoLock(&m_cStateLock);
    m_paStreams    = (CSourceStream **) new CMyStream*[1];
    if (m_paStreams == NULL) {
        *phr = E_OUTOFMEMORY;
m_paStreams[0] = new CMyStream(phr, this, L"My Source OutPin");
    if (m_paStreams[0] == NULL) {
        *phr = E_OUTOFMEMORY;
CUnknown * WINAPI CMySourceFilter::CreateInstance(LPUNKNOWN lpunk, HRESULT *phr)
    CUnknown *punk = new CMySourceFilter(lpunk, phr);
    if (punk == NULL) {
        *phr = E_OUTOFMEMORY;
    return punk;
} // CreateInstance

Having finished our source filter class, how could we create our new instances. Perhaps an array, please look at the following snippets:

CFactoryTemplate g_Templates[] = {
  { L"My Source Filter"
  , &CLSID_MySourceFilter
  , CMySourceFilter::CreateInstance 
  , NULL
  , &sudMyax }
int g_cTemplates = sizeof(g_Templates) / sizeof(g_Templates[0]);

Step 6

Add the two functions for register and unregister:

STDAPI DllRegisterServer()
    return AMovieDllRegisterServer2( TRUE );
} // DllRegisterServer
// DllUnregisterServer
STDAPI DllUnregisterServer()
    return AMovieDllRegisterServer2( FALSE );
} // DllUnregisterServer

Step 7

For creating our filter in a DLL, we also need to export some functions. Create a new DEF file:

DllRegisterServer PRIVATE
DllUnregisterServer PRIVATE
DllGetClassObject PRIVATE
DllCanUnloadNow PRIVATE

Okay, so far. Our filter has finished without pins. Of course, you could test it in the GraphEdit.exe. You only look at its information, it can do nothing now. We want to add an output pin, the pin output video we want. Moving on...

Step 8

Now we need insert a new class inherited from CSourceStream. It could generate samples and push it to the next filters.

class CMyStream  : public CSourceStream
 STDMETHOD(Notify)(IBaseFilter * pSender, Quality q);
 HRESULT CheckMediaType(const CMediaType *pMediaType);
 HRESULT GetMediaType(int iPosition,CMediaType *pMediaType);
 HRESULT DecideBufferSize(IMemAllocator *pAlloc,ALLOCATOR_PROPERTIES *pProperties);
 HRESULT FillBuffer(IMediaSample *pms);
 CMyStream(HRESULT *phr,CSource *pms,LPCWSTR pName);
 virtual ~CMyStream();
  CRefTime m_rtSampleTime;         // The time stamp for each sample
     int m_iRepeatTime;            // Time in msec between frames
  int xPos,yPos;
  BOOL xLock,yLock;

How does the pin work? Look for more on MSDN for more details, click here!

The streaming thread runs a loop with the following structure:

until (stopped)
1. Get a media sample from the allocator.
2. Fill the sample with data.
3. Time stamp the sample. 
4. Deliver the sample downstream.

Okay, want more details? Download the resource. Tired!

Points of Interest

I am so tired after writing the article, but I'm very glad. Hope it will be useful to you.


  • 22nd February, 2010: Initial post


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


About the Author

Aric Wang
China China

You may also be interested in...


Comments and Discussions

Questioncan u give me your email address? i'm just beginning to learn DS, maybe u can give me some help. Pin
Mr Pei4-Feb-14 22:05
memberMr Pei4-Feb-14 22:05 
AnswerRe: can u give me your email address? i'm just beginning to learn DS, maybe u can give me some help. Pin
Aric Wang12-Apr-14 21:20
memberAric Wang12-Apr-14 21:20 
QuestionCan you guide me on this:- Pin
Robert Clove5-Mar-13 23:09
memberRobert Clove5-Mar-13 23:09 
GeneralMy vote of 5 Pin
toann18-Dec-12 5:11
membertoann18-Dec-12 5:11 
QuestionMeaning of: "Time stamp the sample" Pin
Huy Vo Quang5-Oct-12 16:12
memberHuy Vo Quang5-Oct-12 16:12 
BugError Pin
vaadim8-Nov-11 6:35
membervaadim8-Nov-11 6:35 
GeneralRe: Error Pin
Aric Wang28-Mar-12 16:53
memberAric Wang28-Mar-12 16:53 
Generalproblem with lib files Pin
itaybo10-Apr-11 3:58
memberitaybo10-Apr-11 3:58 
GeneralRe: problem with lib files Pin
Aric Wang28-Mar-12 16:50
memberAric Wang28-Mar-12 16:50 
GeneralIncluded filter does not work Pin
MikeVersteegDotCom11-Jan-11 5:44
memberMikeVersteegDotCom11-Jan-11 5:44 
GeneralRe: Included filter does not work Pin
Aric Wang28-Mar-12 16:45
memberAric Wang28-Mar-12 16:45 
QuestionFilter don't work Pin
andrey_zhuravlev26-Feb-10 4:52
memberandrey_zhuravlev26-Feb-10 4:52 
AnswerRe: Filter don't work Pin
Aric Wang26-Feb-10 16:10
memberAric Wang26-Feb-10 16:10 
AnswerRe: Filter don't work Pin
Aric Wang26-Feb-10 16:13
memberAric Wang26-Feb-10 16:13 
AnswerRe: Filter don't work Pin
andrey_zhuravlev27-Feb-10 2:26
memberandrey_zhuravlev27-Feb-10 2:26 
I found why it don't work. MyStream.cpp -> ... FillBuffer(...)-> !if(pbmi)!
'pbmi' uninitialized. I commented this "if" and the filter has earned.
Thank you for the article. Maybe in the next few days I will ask you a few questions. Can I?
GeneralRe: Filter don't work Pin
Aric Wang27-Feb-10 2:58
memberAric Wang27-Feb-10 2:58 
GeneralRe: Filter don't work Pin
Aric Wang27-Feb-10 3:00
memberAric Wang27-Feb-10 3:00 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web04 | 2.8.190306.1 | Last Updated 22 Feb 2010
Article Copyright 2010 by Aric Wang
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid