Click here to Skip to main content
15,896,730 members
Articles / Desktop Programming / MFC

Preview and Record with MPEG2 Capture device

Rate me:
Please Sign up or sign in to vote.
4.39/5 (16 votes)
11 Jun 20048 min read 220.1K   9.7K   94  
This article shows how to work with MPEG2 capture devices such as Preview, Record. Works with all video renderers. Grab picture and configure MPEG2 Demux.
#pragma once 



#include <objbase.h>
#include <uuids.h>
#include <control.h>
#include <atlbase.h>
#include <strmif.h>
#include <control.h>
#include <evcode.h>
#include <ks.h>
#include <ksmedia.h>
#include <MMSystem.h>
#include <io.h> 
#include <mtype.h>
#include <MMReg.h>
#include <d3d9.h>
#include <vmr9.h>
#include <sbe.h>
#include "VideoDecoder.h"
#include "AudioDecoder.h"
 



class CVideoDecoder;
class CAudioDecoder;


#define AUDIO_FREQ_32_000      0 
#define AUDIO_FREQ_44_100      1 
#define AUDIO_FREQ_48_000      2 

typedef struct _CAP_DEVICES { 
	char DeviceName[500];
	int  device_type; // 0 for mpeg , 1 for other 
} DEVICES;

#define SAFE_RELEASE(i) {if (i) i->Release(); i = NULL;}
typedef enum {STOP_MODE = 11 , RUNNING_MODE = 12 , PAUSE_MODE = 13} eGraphState;



//
//  Global data structure for storing CLSIDs and friendly strings
//
typedef struct _category_info
{
    const CLSID *pclsid;
    TCHAR szName[128];

} CATEGORY_INFO_DS;

// The DirectShow reference documentation lists a set of filter categories
// for which you can enumerate corresponding filters.  See 'Filter Categories'
// under 'DirectShow->Reference->Constants and GUIDS' in the DirectX docs.
const CATEGORY_INFO_DS categories[] = {
    
    &CLSID_AudioInputDeviceCategory, TEXT("Audio Capture Devices\0"),
    &CLSID_AudioCompressorCategory,  TEXT("Audio Compressors\0"),
    &CLSID_AudioRendererCategory,    TEXT("Audio Renderers\0"),
    &CLSID_LegacyAmFilterCategory,   TEXT("DirectShow Filters\0"),
    &CLSID_MidiRendererCategory,     TEXT("Midi Renderers\0"),
    &CLSID_VideoInputDeviceCategory, TEXT("Video Capture Devices\0"),
    &CLSID_VideoCompressorCategory,  TEXT("Video Compressors\0"),
};








class      CDirectShowGraph 
{

public: 

	CDirectShowGraph(CWnd *wnd);
	~CDirectShowGraph(); 

	CVideoDecoder     *videoDecoder;
	CAudioDecoder     *audioDecoder;  


	void SetHostClass(CWnd *pWnd = NULL); 


	AM_MEDIA_TYPE pMediaType;
	AM_MEDIA_TYPE pMediaAudioType;
    MPEG1WAVEFORMAT    mpeg1_wave_format;
	REFERENCE_TIME g_rtTotalTime;
	
	eGraphState   graph_mode;
    eGraphState   GetGraphState () {return graph_mode;};
	void    SetGraphState (eGraphState  state)
	{

		graph_mode = state;
	}

	CWnd  *p_Wnd; 
	
	BOOL     CaptureImage(LPCTSTR szFile);
	void     DisplayCapturedImage(LPCTSTR szFile); 

	void     OnPaint(HWND hwnd); 
	int      MakeGraph();

	HRESULT  InitializeWindowlessVMR(HWND wnd); 
	HRESULT  InitializeWindowlessVMR9(HWND wnd); 

	HRESULT  AddCaptureDevice(); 
	void     FreeAllResources();
	HRESULT  AddMPEG2VideoAnalizer(); 
	HRESULT  AddStreamBufferSource(); 
	HRESULT  AddInfiniteTeeFilter(); 
	int      GetPinType(IMoniker *pMoniker); 

	HRESULT AddMpeg2Demultiplex(int record_to_disk = 0); 
	HRESULT AddVideoRendererFilter(); 
	HRESULT AddDumpFilter(); 
	HRESULT SetupWindow(HWND hwnd); 


	IPin *GET_PIN_ByIndex(IBaseFilter   *Filter , int index, PIN_DIRECTION  direction);
	IPin *GET_PIN(IBaseFilter *Filter , CString   PinName ,  PIN_DIRECTION  direction);


	// All public :) 
	IMediaEventEx     *m_pME ;          
	IGraphBuilder     *pBuilder;
	IBasicAudio       *pBasicAudio;
	IBasicVideo       *pBasicVideo;	
	IAMVideoProcAmp   *pVideoAmp;	
	IAMCrossbar       *pAmCrossbar;	
	IVideoWindow      *m_pVW;
	IMediaSeeking     *m_pMS;
    IMediaFilter      *m_pMF;
	IMediaPosition    *m_pMP;
    IMediaControl     *m_pMC ;
	IAMAnalogVideoDecoder  *pAmAnalogVideo; 
	IGraphBuilder   *m_pGraph;

    
	IVMRWindowlessControl9       *pWC;

	IPin		  *pCaptureDevicePin; 
	IPin          *pDemuxInPin;
	IPin          *pDemuxVideoOutPin;
	IPin          *pMPEG2VideoAnalizerOutPin;
	IPin          *pMPEG2VideoAnalizerInPin;
	IPin          *pDemuxAudioOutPin;
	IPin          *pDefaultSoundeDeviceInPin; 
	IPin          *pInfeeFilterInPin;
	IPin          *pInfeeFilterOutPin[3];
	IPin          *pDumpInPin; 
	IPin          *pRendererInPin; 



	IBaseFilter		*pMPEG2DemuxFilter; 
	IBaseFilter		*pInfeeFilter; 
	IBaseFilter		*pVmr;
	IBaseFilter		*pCaptureDevice; 
	IBaseFilter		*pRenderFilter; 
	IBaseFilter		*pDumpFilter; 
	IBaseFilter     *pCrossbarDevice;

	
	interface IAMVideoControl *pVideoControl;
	interface IFileSinkFilter    *pFileSourceInterface;	
	interface IMpeg2Demultiplexer  *pDemuxInterface;
	interface IMPEG2StreamIdMap    *pDemuxStreamIdMapInterface;
    interface IMPEG2StreamIdMap    *pDemuxStreamIdMapInterface1;



	ULONG g_dwGraphRegister; 
	HRESULT AddGraphToRot(IUnknown *pUnkGraph, DWORD *pdwRegister) ;
	void    RemoveGraphFromRot(DWORD pdwRegister); 



	CVideoDecoder  *GetVideoDecoder()
	{

		return videoDecoder;
	}

	CAudioDecoder  *GetAudioDecoder()
	{

		return audioDecoder;
	}



}; 

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Israel Israel
Enjoy from Cabelim.com

Comments and Discussions