Click here to Skip to main content
15,888,803 members
Articles / Desktop Programming / ATL
Article

Simultaneous Previewing & Video Capture using DirectShow

Rate me:
Please Sign up or sign in to vote.
4.35/5 (33 votes)
15 Apr 2004CPOL3 min read 648K   20.1K   140   210
A method for previewing and framegrabbing simultaniously from webcam using DirectShow.

Image 1

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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: Program crashes on m_VMRCap.Init Pin
krssagar1-Nov-04 12:21
krssagar1-Nov-04 12:21 
GeneralRe: Program crashes on m_VMRCap.Init Pin
Dmitry Polenur2-Nov-04 7:56
Dmitry Polenur2-Nov-04 7:56 
GeneralRe Re: Program crashes on m_VMRCap.Init Pin
krssagar2-Nov-04 15:20
krssagar2-Nov-04 15:20 
Generalfailed at m_pWC->GetCurrentImage(&lpCurrImage) Pin
cdchb12328-Sep-04 10:31
cdchb12328-Sep-04 10:31 
GeneralRe: failed at m_pWC->GetCurrentImage(&lpCurrImage) Pin
krssagar28-Oct-04 15:43
krssagar28-Oct-04 15:43 
GeneralRe: failed at m_pWC->GetCurrentImage(&lpCurrImage) Pin
cdchb1231-Nov-04 11:16
cdchb1231-Nov-04 11:16 
GeneralRe: failed at m_pWC->GetCurrentImage(&lpCurrImage) Pin
krssagar1-Nov-04 12:17
krssagar1-Nov-04 12:17 
GeneralRe: failed at m_pWC->GetCurrentImage(&lpCurrImage) Pin
cdchb1232-Nov-04 3:12
cdchb1232-Nov-04 3:12 
GeneralRe: failed at m_pWC->GetCurrentImage(&lpCurrImage) Pin
krssagar2-Nov-04 15:23
krssagar2-Nov-04 15:23 
GeneralErrors Compiling Pin
Anonymous12-Sep-04 11:36
Anonymous12-Sep-04 11:36 
GeneralRe: Errors Compiling Pin
spathma12-Sep-04 11:59
spathma12-Sep-04 11:59 
GeneralRe: Errors Compiling Pin
Jeff Hu9-Oct-04 6:15
Jeff Hu9-Oct-04 6:15 
GeneralRe: Errors Compiling Pin
andresvill13-Nov-04 17:39
andresvill13-Nov-04 17:39 
GeneralRe: Errors Compiling Pin
Vitaly Tomilov29-Nov-04 22:46
Vitaly Tomilov29-Nov-04 22:46 
GeneralRe: Errors Compiling Pin
Anonymous24-Jun-05 5:50
Anonymous24-Jun-05 5:50 
GeneralRe: Errors Compiling Pin
Vitaly Tomilov24-Jun-05 6:25
Vitaly Tomilov24-Jun-05 6:25 
GeneralMinor bug Pin
malmopag6-Sep-04 13:04
malmopag6-Sep-04 13:04 
GeneralRe: Minor bug Pin
krssagar10-Sep-04 15:47
krssagar10-Sep-04 15:47 
GeneralDIB Pointer Trouble! Pin
Brooks Y.5-Sep-04 10:03
Brooks Y.5-Sep-04 10:03 
GeneralRe: DIB Pointer Trouble! Pin
krssagar10-Sep-04 15:58
krssagar10-Sep-04 15:58 
GeneralRe: New Trouble, Flaw in VMR9 Class? Pin
Brooks Y.11-Sep-04 6:51
Brooks Y.11-Sep-04 6:51 
GeneralRe: New Trouble, Flaw in VMR9 Class? Pin
krssagar11-Sep-04 13:44
krssagar11-Sep-04 13:44 
GeneralRe: New Trouble, Flaw in VMR9 Class? Pin
Brooks Y.13-Sep-04 9:53
Brooks Y.13-Sep-04 9:53 
GeneralRe: New Trouble, Flaw in VMR9 Class? Pin
krssagar13-Sep-04 23:07
krssagar13-Sep-04 23:07 
GeneralRe: New Trouble, Flaw in VMR9 Class? Pin
Brooks Y.14-Sep-04 9:24
Brooks Y.14-Sep-04 9:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.