Click here to Skip to main content
15,896,063 members
Articles / Desktop Programming / ATL

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

Rate me:
Please Sign up or sign in to vote.
4.96/5 (19 votes)
22 Feb 2010CPOL2 min read 76.3K   5.2K   57  
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.
// MySourceFilter.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"
#include <streams.h>
#include <olectl.h>
#include <initguid.h>

#include "MyStream.h"

#pragma warning(disable:4710)  // 'function': function not inlined (optimzation)
// {93304979-AAD1-4442-A1BD-503FC8CC883F}
DEFINE_GUID(CLSID_MySourceFilter, 
0x93304979, 0xaad1, 0x4442, 0xa1, 0xbd, 0x50, 0x3f, 0xc8, 0xcc, 0x88, 0x3f);

// Setup data

const AMOVIESETUP_MEDIATYPE sudOpPinTypes =
{
    &MEDIATYPE_Video,       // Major type
    &MEDIASUBTYPE_NULL      // Minor type
};

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

const AMOVIESETUP_FILTER sudMyax =
{
    &CLSID_MySourceFilter,    // Filter CLSID
    L"My Source Filter",       // String name
    MERIT_DO_NOT_USE,       // Filter merit
    1,                      // Number pins
    &sudOpPin               // Pin details
};

class CMySourceFilter:public CSource
{
public:

    // The only allowed way to create Bouncing balls!
    static CUnknown * WINAPI CreateInstance(LPUNKNOWN lpunk, HRESULT *phr);

private:
	CMySourceFilter(LPUNKNOWN lpunk, HRESULT *phr);
};


CMySourceFilter::CMySourceFilter(LPUNKNOWN lpunk, HRESULT *phr):
CSource(NAME("MySource"),lpunk,CLSID_MySourceFilter)
{
    CAutoLock cAutoLock(&m_cStateLock);
	
    m_paStreams    = (CSourceStream **) new CMyStream*[1];
    if (m_paStreams == NULL) {
        *phr = E_OUTOFMEMORY;
	return;
    }

    m_paStreams[0] = new CMyStream(phr, this, L"My Source OutPin");
    if (m_paStreams[0] == NULL) {
        *phr = E_OUTOFMEMORY;
	return;
    }

}

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


STDAPI DllRegisterServer()
{
    return AMovieDllRegisterServer2( TRUE );

} // DllRegisterServer


//
// DllUnregisterServer
//
STDAPI DllUnregisterServer()
{
    return AMovieDllRegisterServer2( FALSE );

} // DllUnregisterServer

CUnknown * WINAPI CMySourceFilter::CreateInstance(LPUNKNOWN lpunk, HRESULT *phr)
{
    CUnknown *punk = new CMySourceFilter(lpunk, phr);
    if (punk == NULL) {
        *phr = E_OUTOFMEMORY;
    }
    return punk;

} // CreateInstance

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

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


Written By
Engineer
China China
Secret..

Comments and Discussions