Click here to Skip to main content
6,305,776 members and growing! (16,684 online)
Email Password   helpLost your password?
Multimedia » Audio and Video » DirectShow     Intermediate

Simultaneous Previewing & Video Capture using DirectShow

By krssagar

A method for previewing and framegrabbing simultaniously from webcam using DirectShow.
VC6Win2K, WinXP, MFC, ATL, Dev
Posted:15 Apr 2004
Views:214,957
Bookmarked:92 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
23 votes for this article.
Popularity: 4.95 Rating: 3.63 out of 5
4 votes, 17.4%
1
1 vote, 4.3%
2

3
6 votes, 26.1%
4
12 votes, 52.2%
5

Introduction

This article explains a method to perform Previewing and Grabbing simultaneously from a webcam using DirectShow. Other than using the conventional ISampleGrabber interface, this application uses DirectShow VMR Filters & IPin Interfaces.

Background

There are many solutions for capturing and previewing but a few combining both. I tried to make an application which serves this purpose using ISampleGrabber and IVideoWindow interfaces. It worked well on Windows 2000 but showed reluctance on XP.

DirectShow "Windowless Sample" (Windowless mode in the Video Mixing Renderer) gives the clue for capturing frames while previewing.

Using the code

I had created a class named CVMR_Capture. To use this class:

  • Insert the VMR_Capture.h and VMR_Capture.cpp files into your project.
  • Link strmiids.lib, quartz.lib in VC++ Project Settings.
  • Add a static control into your dialog resource.
  • Add a member variable for that static control.
  • Declare a member variable of CVMR_Capture.

The public interfaces provided are:

int EnumDevices(HWND hList);
HRESULT Init(int iDeviceID,HWND hWnd,int iWidth,int iHeight);
DWORD GetFrame(BYTE ** pFrame);
BOOL StopVMR();
BOOL Pause();
DWORD ImageCapture(LPCTSTR szFile);
DWORD GrabFrame();
  • int EnumDevices(HWND hList); (Optional)

    Enumerates the available devices connected to the computer. HWND is the HANDLE to a combo box which displays the connected devices.

  • HRESULT Init(int iDeviceID,HWND hWnd,int iWidth,int iHeight);

    Initializing. iDeviceID is the selected device index (from combo Box), hWnd is the handle of the display window, iWidth and iHeight are the expected resolution (e.g.: 320/240). The display will adjust itself to the size of the window.

  • DWORD GrabFrame();

  • DWORD GetFrame(BYTE ** pFrame);

    Call these functions to get the captured buffer. GrabFrame(); will grab the image. Store it in a buffer and return value is the size of the buffer. User can get the buffer pointer by calling GetFrame(BYTE ** pFrame) which returns the size of the buffer.

    Don't delete the "pFrame" buffer value, which will be deleted by the class destructor. Use the Pointer(pFrame) for processing or copying the memory to a user allocated Buffer.

  • BOOL StopVMR();

    Stops previewing.

  • BOOL Pause();

    Pauses capturing. Call this method again to restart capturing.

  • DWORD ImageCapture(LPCTSTR szFile);

    Capture and write the Raw Image BYTES (RGB) to a file provided.

Special Functions and explanation

bool BindFilter(int deviceId, IBaseFilter **pFilter);
HRESULT InitializeWindowlessVMR(HWND hWnd);
HRESULT InitVideoWindow(HWND hWnd,int width, int height);
void StopCapture();
void CloseInterfaces(void);
void DeleteMediaType(AM_MEDIA_TYPE *pmt);
bool Convert24Image(BYTE *p32Img,BYTE *p24Img,DWORD dwSize32);
  • bool BindFilter(int deviceId, IBaseFilter **pFilter);

    Binds the Filer to the given device ID.

  • HRESULT InitializeWindowlessVMR(HWND hWnd);

    VMR initialization to the HWND provided.

  • HRESULT InitVideoWindow(HWND hWnd,int width, int height);

    Adjust the Display Video Size according to the size of the display window.

  • void StopCapture();

    Stops capturing.

  • void CloseInterfaces(void);

    Releases & deletes the interfaces /allocated memory.

  • void DeleteMediaType(AM_MEDIA_TYPE *pmt);

    Deletes Media Type object.

  • bool Convert24Image(BYTE *p32Img,BYTE *p24Img,DWORD dwSize32);

    The captured image using VMR is 32 bit RGB. This function will convert the 32 bit RGB to 24 bit RGB.

Points of Interest

This class is tested on Windows 2000 & Windows XP with DirectX9.0. Both VMR7 & VMR9 are supported. I haven't included the DirectShow Event Handling part in this sample. Please attach the required exception/error handling mechanism when you use.

Please drop me an email about any bugs, or suggestions to enhance the class. Your suggestions and guidance will be most valuable.

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

About the Author

krssagar


Member

Occupation: Web Developer
Location: United States United States

Other popular Audio and Video articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 200 (Total in Forum: 200) (Refresh)FirstPrevNext
GeneralCrash on line: hr = m_pGB->Render(m_pCamOutPin); Pinmemberdescartes12:21 24 Mar '09  
GeneralFlickerless problem PinmemberQuyLV21:57 17 Feb '09  
GeneralThanX [modified] Pinmemberangello1:07 5 Jan '09  
GeneralIVMRWindowlessControl9::GetCurrentImage, Excessive use of this function will degrade video playback performance. Pinmemberyouhow200017:28 21 Dec '08  
GeneralRe: IVMRWindowlessControl9::GetCurrentImage, Excessive use of this function will degrade video playback performance. Pinmemberangello1:12 5 Jan '09  
QuestionBmp is flipped horizontally PinmemberMember 24354793:25 12 Nov '08  
AnswerRe: Bmp is flipped horizontally PinmemberQuyLV21:41 17 Feb '09  
GeneralRTP sender/receiver DirectShow filter. PinmemberhINTModuleState6:56 18 Aug '08  
GeneralRe: RTP sender/receiver DirectShow filter. Pinmemberangello1:16 5 Jan '09  
Generaldirectx 9.0 Pinmemberani_ikram19:29 30 Jul '08  
GeneralCOMPILE ERROR PinmemberCollin Xie20:18 7 May '08  
GeneralRe: COMPILE ERROR PinmemberhINTModuleState7:00 18 Aug '08  
GeneralColor adjustment Pinmemberrenoreballos5:08 4 Jan '08  
Generalhi i have some question Pinmembermocderfd22:52 29 Nov '07  
QuestionCapture Image to Bitmap Pinmemberzecapt0:25 12 Oct '07  
AnswerRe: Capture Image to Bitmap [modified] Pinmemberprda3:37 31 Oct '07  
QuestionImageCapture() error Pinmemberkey_467:01 15 Sep '07  
Generaldshow not in directory??helpme.. Pinmemberjefryn18:54 15 Aug '07  
Generaltoo slow Pinmemberwharry85:02 19 Jul '07  
QuestionSave picture in picture video streams to avi file [modified] Pinmemberlonglac10:31 6 Mar '07  
Generalrecompiling capture Pinmemberxavier661:38 2 Feb '07  
GeneralRe: recompiling capture Pinmembermpawluk12:02 31 Mar '07  
QuestionRe: recompiling capture Pinmemberminimal_objects6:44 13 Jun '07  
QuestionCant add VMR9 in DirectX ??? [modified] Pinmember~Jabeen~1:00 21 Dec '06  
AnswerRe: Cant add VMR9 in DirectX ??? PinmemberKhathar22:05 25 Dec '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 15 Apr 2004
Editor: Nishant Sivakumar
Copyright 2004 by krssagar
Everything else Copyright © CodeProject, 1999-2009
Web11 | Advertise on the Code Project