Click here to Skip to main content
15,884,099 members
Articles / Programming Languages / C++

Capturing Video from Web-camera on Windows 7 and 8 by using Media Foundation

Rate me:
Please Sign up or sign in to vote.
4.96/5 (25 votes)
10 Apr 2013CPOL5 min read 280.2K   33.1K   71  
Simple lib for capturing video from web-camera by using Media Foundation
#pragma once

#include <mfidl.h>
#include <memory>

struct IMFMediaSource;

class RawImage;

// Class for grabbing image from video stream
class ImageGrabber : public IMFSampleGrabberSinkCallback
{
public:
	~ImageGrabber(void);

	HRESULT initImageGrabber(IMFMediaSource *pSource, GUID VideoFormat);

	HRESULT startGrabbing(void);

	void stopGrabbing();

	RawImage *getRawImage();

	// Function of creation of the instance of the class
	static HRESULT CreateInstance(ImageGrabber **ppIG,unsigned int deviceID);

private:

	bool ig_RIE;

	bool ig_Close;
	
	long m_cRef;

	unsigned int ig_DeviceID;
	
	IMFMediaSource *ig_pSource;

	IMFMediaSession *ig_pSession;

	IMFTopology *ig_pTopology;
	
	RawImage *ig_RIOut;

	std::auto_ptr<RawImage> ig_RIFirst;

	std::auto_ptr<RawImage> ig_RISecond;
	
			
	ImageGrabber(unsigned int deviceID);
			
	HRESULT CreateTopology(IMFMediaSource *pSource, IMFActivate *pSinkActivate, IMFTopology **ppTopo);

	HRESULT AddSourceNode(
    IMFTopology *pTopology,           
    IMFMediaSource *pSource,          
    IMFPresentationDescriptor *pPD,   
    IMFStreamDescriptor *pSD,         
    IMFTopologyNode **ppNode);

	HRESULT AddOutputNode(
    IMFTopology *pTopology,     
    IMFActivate *pActivate,     
    DWORD dwId,                 
    IMFTopologyNode **ppNode);
	
	// IUnknown methods
	STDMETHODIMP QueryInterface(REFIID iid, void** ppv);
    STDMETHODIMP_(ULONG) AddRef();
    STDMETHODIMP_(ULONG) Release();

    // IMFClockStateSink methods
    STDMETHODIMP OnClockStart(MFTIME hnsSystemTime, LONGLONG llClockStartOffset);
    STDMETHODIMP OnClockStop(MFTIME hnsSystemTime);
    STDMETHODIMP OnClockPause(MFTIME hnsSystemTime);
    STDMETHODIMP OnClockRestart(MFTIME hnsSystemTime);
    STDMETHODIMP OnClockSetRate(MFTIME hnsSystemTime, float flRate);

    // IMFSampleGrabberSinkCallback methods
    STDMETHODIMP OnSetPresentationClock(IMFPresentationClock* pClock);
    STDMETHODIMP OnProcessSample(REFGUID guidMajorMediaType, DWORD dwSampleFlags,
        LONGLONG llSampleTime, LONGLONG llSampleDuration, const BYTE * pSampleBuffer,
        DWORD dwSampleSize);
    STDMETHODIMP OnShutdown();


};

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
Software Developer
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions