Click here to Skip to main content
12,548,135 members (50,153 online)
Click here to Skip to main content
Add your own
alternative version


54 bookmarked

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

, 22 Feb 2010 CPOL
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
Although I was a college studnet now as the same I was also a teacher of C/C++/3DS MAX/website for National Computer Rank Examination.I began to study computer when I was a child 8 year's old.I use BASIC/C/C++/VB/VF/VC/DELPHI/.NET to programming.When in college I was the chairman of Computer Association of our school and I take part in ACM Competition twice in two years.I also was the only person who take part in the National Mathematical Contest in Modeling twice in two years of our school.I like computer programming very much.Hope to be your friend.thx!

You may also be interested in...


Comments and Discussions

GeneralRe: Included filter does not work Pin
Aric Green28-Mar-12 16:45
memberAric Green28-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 Green26-Feb-10 16:10
memberAric Green26-Feb-10 16:10 
AnswerRe: Filter don't work Pin
Aric Green26-Feb-10 16:13
memberAric Green26-Feb-10 16:13 
AnswerRe: Filter don't work Pin
andrey_zhuravlev27-Feb-10 2:26
memberandrey_zhuravlev27-Feb-10 2:26 
GeneralRe: Filter don't work Pin
Aric Green27-Feb-10 2:58
memberAric Green27-Feb-10 2:58 
GeneralRe: Filter don't work Pin
Aric Green27-Feb-10 3:00
memberAric Green27-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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161021.1 | Last Updated 22 Feb 2010
Article Copyright 2010 by Aric Wang
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid