Click here to Skip to main content
15,885,216 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.3K   33.1K   71  
Simple lib for capturing video from web-camera by using Media Foundation
#pragma once

#include <map>
#include <vector>
#include <string>
#include <memory>


#include "videoInput.h"

struct IMFActivate;

struct IMFMediaSource;

struct IMFMediaType;

class ImageGrabberThread;

class RawImage;

typedef std::wstring String;

typedef std::vector<int> vectorNum;

typedef std::map<String, vectorNum> SUBTYPEMap;

typedef std::map<UINT64, SUBTYPEMap> FrameRateMap;

typedef void(*emergensyStopEventCallback)(int, void *);

/// Class for controlling of video device
class videoDevice
{

public:
	videoDevice(void);
	~videoDevice(void);

	void closeDevice();
	
	CamParametrs getParametrs();

	void setParametrs(CamParametrs parametrs);

	void setEmergencyStopEvent(void *userData, void(*func)(int, void *));
	
	long readInfoOfDevice(IMFActivate *pActivate, unsigned int Num);
		
	wchar_t *getName();

	int getCountFormats();

	unsigned int getWidth();
	
	unsigned int getHeight();

	MediaType getFormat(unsigned int id);
	
	bool setupDevice(unsigned int w, unsigned int h, unsigned int idealFramerate = 0);

	bool setupDevice(unsigned int id);

	bool isDeviceSetup();

	bool isDeviceMediaSource();
	
	bool isDeviceRawDataSource();

	bool isFrameNew();

	IMFMediaSource *getMediaSource();

	RawImage *getRawImageOut();

private:

	enum typeLock
	{
		MediaSourceLock,

		RawDataLock,

		OpenLock

	} vd_LockOut;
	
	wchar_t *vd_pFriendlyName;

	std::auto_ptr<ImageGrabberThread> vd_pImGrTh;

	CamParametrs vd_PrevParametrs;

	unsigned int vd_Width;

	unsigned int vd_Height;

	unsigned int vd_CurrentNumber;

	bool vd_IsSetuped;
		
	std::map<UINT64, FrameRateMap> vd_CaptureFormats;
	
	std::vector<MediaType> vd_CurrentFormats;

	IMFMediaSource *vd_pSource;

	emergensyStopEventCallback vd_func;

	void *vd_userData;
	
	long enumerateCaptureFormats(IMFMediaSource *pSource);
	
	long setDeviceFormat(IMFMediaSource *pSource, unsigned long dwFormatIndex);

	void buildLibraryofTypes();

	int findType(unsigned int size, unsigned int frameRate = 0);	
	
	long resetDevice(IMFActivate *pActivate);
	
	long initDevice();

	long checkDevice(IMFAttributes *pAttributes, IMFActivate **pDevice);

};

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