Click here to Skip to main content
15,888,733 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

 
QuestionImageCapture() error Pin
key_4615-Sep-07 6:01
key_4615-Sep-07 6:01 
Generaldshow not in directory??helpme.. Pin
jefryn15-Aug-07 17:54
jefryn15-Aug-07 17:54 
Generaltoo slow Pin
wharry819-Jul-07 4:02
wharry819-Jul-07 4:02 
QuestionSave picture in picture video streams to avi file [modified] Pin
longlac6-Mar-07 9:31
longlac6-Mar-07 9:31 
Generalrecompiling capture Pin
xavier662-Feb-07 0:38
xavier662-Feb-07 0:38 
GeneralRe: recompiling capture Pin
mpawluk31-Mar-07 11:02
mpawluk31-Mar-07 11:02 
QuestionRe: recompiling capture Pin
minimal_objects13-Jun-07 5:44
minimal_objects13-Jun-07 5:44 
QuestionCant add VMR9 in DirectX ??? [modified] Pin
~Jabeen~21-Dec-06 0:00
~Jabeen~21-Dec-06 0:00 
Hi all

This is a very strange problem I have seen ever

I am developing my application based on Direct X

I am rendering video through “Video Mixing Renderer 9” (VMR9) on my PC the application is fine. On another PC my application is giving error that it can’t get the renderer filter.

Using Graph Edit I created Video Capture Sources and then rendered the Capture Pin. As observed a Video Renderer is added, finally video is previewed. As my application demanded windowless mode, I have replaced Video Renderer with VMR9.

On my PC this Graph Edit procedure is working fine. On another PC when I am trying to replace the Video Renderer with VMR it is giving following error. So my application gives error too that it cant add the VMR9 filter

The filter could not be created. Resources used by this filter may be already in use

Although I have checked the resources. No camera or such resources usage is observed. Also there is no connection between the pins when I am adding the filter.

How is this???? I don’t know how to resolve this.

In coding I would have used Video Renderer but I need Windowless mode which is provided by VMR only I want to display video in client area). Any alternate is also appreciated.

But can any body solve this issue that why I can’t add the filter. Or is there any other substitute of VMR?

i have searched out that problem i am facing has Error code 0x0273 in "DirectX Error Lookup"
(return code:0x80040273)

but how can i resolve this ? Some one told me reinstalling windows probably solve the problem

but my application is a dll and is developed statically linked with MFC so it has all prerequisites for it.

I wonder if any client of this dll faces same scenrio and not know the reason how will my application work

does anyone knows the alternate solution in DirectX (i want to display video in Client area that is Windowless mode).

I need DirectX because i want to capture video through Capture Resources

but i cant resolve this problem



Jabeen

AnswerRe: Cant add VMR9 in DirectX ??? Pin
Khathar25-Dec-06 21:05
Khathar25-Dec-06 21:05 
QuestionRe: Cant add VMR9 in DirectX ??? Pin
S.K. Gupta27-Dec-06 18:36
S.K. Gupta27-Dec-06 18:36 
Questionwhere is Dshow.h? Pin
Khathar18-Dec-06 20:38
Khathar18-Dec-06 20:38 
AnswerRe: where is Dshow.h? Pin
~Jabeen~25-Dec-06 17:35
~Jabeen~25-Dec-06 17:35 
GeneralCapture and preview stop if computer is locked Pin
gerbilvomit27-Nov-06 10:48
gerbilvomit27-Nov-06 10:48 
GeneralRe: Capture and preview stop if computer is locked Pin
Member 365746024-Feb-08 21:14
Member 365746024-Feb-08 21:14 
GeneralImage not refreshing Pin
nripun14-Nov-06 0:35
nripun14-Nov-06 0:35 
GeneralSave the captured video Pin
shital_harode@rediffmail.com6-Nov-06 0:56
shital_harode@rediffmail.com6-Nov-06 0:56 
GeneralSave the captured video Pin
shital_harode@rediffmail.com6-Nov-06 0:55
shital_harode@rediffmail.com6-Nov-06 0:55 
GeneralSave the captured video Pin
shital_harode@rediffmail.com6-Nov-06 0:55
shital_harode@rediffmail.com6-Nov-06 0:55 
AnswerRe: Save the captured video Pin
Khathar27-Dec-06 0:11
Khathar27-Dec-06 0:11 
GeneralSal.h Pin
Kontaxis23-Oct-06 12:46
Kontaxis23-Oct-06 12:46 
GeneralYUV to RGB conversion Pin
deji10129-Sep-06 6:20
deji10129-Sep-06 6:20 
Questionfailed: CoCreateInstance ? Pin
jalsa G27-Sep-06 21:15
jalsa G27-Sep-06 21:15 
QuestionQuery in Identifying the device? Pin
k. satheesh_177-Sep-06 20:15
k. satheesh_177-Sep-06 20:15 
AnswerRe: Query in Identifying the device? Pin
k. satheesh_179-Sep-06 10:14
k. satheesh_179-Sep-06 10:14 
QuestionProblem in displaying the caputured image which is written in to a bmp file Pin
k. satheesh_177-Sep-06 19:21
k. satheesh_177-Sep-06 19:21 

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.