Question:

Why does my DirectShow based video capture application fail to set resolution for the LifeCam VX-6000/VX-3000 webcam when LifeCam software is installed in the system? Is there any way to resolve this issue programmatically?

Here is the code snippet that I am using to set webcam resolution:

   1: IAMStreamConfig *pConfig = NULL;
   2:  
   3: IPin *pPin = NULL;
   4:  
   5: HRESULT hr = 0;
   6:  
   7:
   8:  
   9: hr = pPin->QueryInterface(IID_IAMStreamConfig, (void**)&pConfig);
  10:  
  11:
  12:  
  13: if (pConfig != NULL) {
  14:  
  15: AM_MEDIA_TYPE *mT;
  16:  
  17: pConfig->GetFormat(&mT);
  18:  
  19: if (mT->formattype == FORMAT_VideoInfo) {
  20:  
  21: VIDEOINFOHEADER *info = (VIDEOINFOHEADER *)mT->pbFormat;
  22:  
  23: info->bmiHeader.biBitCount = 24;
  24:  
  25: info->bmiHeader.biWidth = 320;
  26:  
  27: info->bmiHeader.biHeight = 240;
  28:  
  29: }
  30:  
  31: hr = pConfig->SetFormat(mT);
  32:  
  33: }
  34:  
  35: Answer:
  36:  
  37: Here is the proper way to set the webcam resolution that will work regardless of LifeCam software (or any other webcam software) being installed:
  38:  
  39: IAMStreamConfig *pConfig = NULL;
  40:  
  41: IPin *pPin = NULL;
  42:  
  43: HRESULT hr = 0;
  44:  
  45:
  46:  
  47: hr = pPin->QueryInterface(IID_IAMStreamConfig, (void**)&pConfig);
  48:  
  49:
  50:  
  51: bool isFormatSet = false;
  52:  
  53: if (pConfig != NULL) {
  54:  
  55: int count, size;
  56:  
  57: hr = pConfig->GetNumberOfCapabilities(&count, &size);
  58:  
  59: if (FAILED(hr)) return -1;
  60:  
  61: BYTE* pSCC = new BYTE[size]; // this we don't care about
  62:  
  63: AM_MEDIA_TYPE *mT;
  64:  
  65: for (int pos = 0; pos < count && !isFormatSet; pos++) {
  66:  
  67: hr = pConfig->GetStreamCaps(pos, &mT, pSCC);
  68:  
  69: if (FAILED(hr)) return -1;
  70:  
  71: if (mT->formattype == FORMAT_VideoInfo) {
  72:  
  73: VIDEOINFOHEADER *info = (VIDEOINFOHEADER *)mT->pbFormat;
  74:  
  75: if (mT->subtype == MEDIASUBTYPE_RGB24 &&
  76:  
  77: info->bmiHeader.biWidth == 320 &&
  78:  
  79: info->bmiHeader.biHeight == 240) { // you may want to ensure that it's RGB24 too 
  80:  
  81: hr = pConfig->SetFormat(mT);
  82:  
  83: if (FAILED(hr)) return -1;
  84:  
  85: isFormatSet = true;
  86:  
  87: }
  88:  
  89: }
  90:  
  91: DeleteMediaType(mT); // include dshowutil.h
  92:  
  93: }
  94:  
  95: }
  96:  

The issue here is that setting the VIH’s width and height is not good enough.  There is a bunch of values in the AM_MEDIA_TYPE that would need to be changed to make the original code work.

In the new code, now we are not forcing any format but checking capabilities for different formats of the webcam calling IAMStreamConfig::GetNumberOfCapabilities and then if our required format is supported by the webcam, then switching to the new format using AM_MEDIA_TYPE we got from IAMStreamConfig::GetStreamCaps.

Mahatab-ur-Rashid