Windows Media Foundation: Controlling Camera Properties

May 19, 2012

I have blogged about Media Foundation before; it’s the “next generation” of DirectShow, introduced in Windows Vista, enhanced in Windows 7 and further enhanced in Windows 8 (more on that in a future post). One of the tasks I encountered recently was to do a video capture from a camera. This is not too difficult to do (once you understand how Media Foundation works) and there’s even two sample in the SDK. But how do you control various camera properties, such as focus, zoom, white balance, etc.?

It turns out Media Foundation does not define any specific interfaces for these tasks. Curiously enough, it implements interfaces defined by its predecessor, DirectShow, on its media source (represented by the IMFMediaSource interface), when that media source is a video camera.

This means that all you need to do is use the usual COM QueryInterface on the media source to get to the relevant functionality, represented by (at least) two DirectShow-defined interfaces, IAMCameraControl and IAMVideoProcAmp. Here’s an example that sets some properties of a camera passed in as a media source:

  1. HRESULT CMFVideoCaptureDlg::SetupCamera(IMFMediaSource* pCameraSource) {
  2.     CComQIPtr<IAMCameraControl> spCameraControl(pCameraSource);
  3.     HRESULT hr = S_OK;
  4.     if(spCameraControl) {
  5.         long min, max, step, def, control;
  6.         hr = spCameraControl->GetRange(CameraControl_Exposure, &min, &max, &step, &def, &control);
  7.         if(SUCCEEDED(hr))
  8.             hr = spCameraControl->Set(CameraControl_Exposure, 1, CameraControl_Flags_Manual);
  9.     }
  10.     CComQIPtr<IAMVideoProcAmp> spVideo(pCameraSource);
  11.     if(spVideo)
  12.         hr = spVideo->Set(VideoProcAmp_WhiteBalance, 0, VideoProcAmp_Flags_Auto);
  13.     return hr;
  14. }

The code uses ATL’s CComQIPtr<> smart pointer class to do the QueryInterface more compactly.

Note that any such modifications may fail, because some properties are not supported by the individual camera or the camera driver. If you know your camera supports some functionality for sure, your best bet is to update the driver. Sometimes a generic camera driver is not enough and the vendor-specific driver is required.

Add comment
facebook linkedin twitter email

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>


one comment

  1. Jules PénuchotJuly 23, 2015 ב 18:28

    Simple yet really helpful. Thanks a lot !