pkg/driver/camera/camera_windows.cpp
// openCamera opens a camera and stores interface handler to camera*.
// camera* should be freed by freeCamera() after use.
openCamera opens a camera and stores interface handler to camera*.
camera* should be freed by freeCamera() after use.
openCamera opens a camera and stores interface handler to camera * . camera * should be freed by freeCamera () after use .
int openCamera(camera* cam, const char** errstr)
{
  cam->grabber = nullptr;
  cam->mediaControl = nullptr;
  cam->callback = nullptr;
  IMoniker* moniker = nullptr;
  IGraphBuilder* graphBuilder = nullptr;
  IBaseFilter* captureFilter = nullptr;
  IMediaControl* mediaControl = nullptr;
  IBaseFilter* grabberFilter = nullptr;
  ISampleGrabber* grabber = nullptr;
  IBaseFilter* nullFilter = nullptr;
  IPin* src = nullptr;
  IPin* dst = nullptr;
  IPin* end = nullptr;
  IPin* nul = nullptr;
  if (!selectCamera(cam, &moniker, errstr))
  {
    goto fail;
  }
  moniker->BindToObject(0, 0, IID_IBaseFilter, (void**)&captureFilter);
  safeRelease(&moniker);
  if (FAILED(CoCreateInstance(
          CLSID_FilterGraph, nullptr, CLSCTX_INPROC,
          IID_IGraphBuilder, (void**)&graphBuilder)))
  {
    *errstr = errGraphBuilder;
    goto fail;
  }
  if (FAILED(graphBuilder->QueryInterface(
          IID_IMediaControl, (void**)&mediaControl)))
  {
    *errstr = errNoControl;
    goto fail;
  }
  if (FAILED(graphBuilder->AddFilter(captureFilter, L"capture")))
  {
    *errstr = errAddFilter;
    goto fail;
  }
  if (FAILED(CoCreateInstance(
          CLSID_SampleGrabber, nullptr, CLSCTX_INPROC,
          IID_IBaseFilter, (void**)&grabberFilter)))
  {
    *errstr = errGrabber;
    goto fail;
  }
  if (FAILED(grabberFilter->QueryInterface(IID_ISampleGrabber, (void**)&grabber)))
  {
    *errstr = errGrabber;
    goto fail;
  }
  {
    AM_MEDIA_TYPE mediaType;
    memset(&mediaType, 0, sizeof(mediaType));
    mediaType.majortype = MEDIATYPE_Video;
    mediaType.subtype = MEDIASUBTYPE_YUY2;
    mediaType.formattype = FORMAT_VideoInfo;
    mediaType.bFixedSizeSamples = 1;
    mediaType.cbFormat = sizeof(VIDEOINFOHEADER);
    VIDEOINFOHEADER videoInfoHdr;
    memset(&videoInfoHdr, 0, sizeof(VIDEOINFOHEADER));
    videoInfoHdr.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    videoInfoHdr.bmiHeader.biWidth = cam->width;
    videoInfoHdr.bmiHeader.biHeight = cam->height;
    videoInfoHdr.bmiHeader.biPlanes = 1;
    videoInfoHdr.bmiHeader.biBitCount = 16;
    videoInfoHdr.bmiHeader.biCompression = MAKEFOURCC('Y', 'U', 'Y', '2');
    mediaType.pbFormat = (BYTE*)&videoInfoHdr;
    if (FAILED(grabber->SetMediaType(&mediaType)))
    {
      *errstr = errGrabber;
      goto fail;
    }
  }
  if (FAILED(graphBuilder->AddFilter(grabberFilter, L"grabber")))
  {
    *errstr = errAddFilter;
    goto fail;
  }
  if (FAILED(CoCreateInstance(
          CLSID_NullRenderer, nullptr, CLSCTX_INPROC,
          IID_IBaseFilter, (void**)&nullFilter)))
  {
    *errstr = errTerminator;
    goto fail;
  }
  if (FAILED(graphBuilder->AddFilter(nullFilter, L"bull")))
  {
    *errstr = errAddFilter;
    goto fail;
  }
  HRESULT hr;
  src = getPin(captureFilter, PINDIR_OUTPUT);
  dst = getPin(grabberFilter, PINDIR_INPUT);
  if (src == nullptr || dst == nullptr ||
      FAILED(hr = graphBuilder->Connect(src, dst)))
  {
    *errstr = errConnectFilters;
    goto fail;
  }
  safeRelease(&src);
  safeRelease(&dst);
  end = getPin(grabberFilter, PINDIR_OUTPUT);
  nul = getPin(nullFilter, PINDIR_INPUT);
  if (end == nullptr || nul == nullptr ||
      FAILED(hr = graphBuilder->Connect(end, nul)))
  {
    *errstr = errConnectFilters;
    goto fail;
  }
  safeRelease(&end);
  safeRelease(&nul);
  safeRelease(&nullFilter);
  safeRelease(&captureFilter);
  safeRelease(&grabberFilter);
  safeRelease(&graphBuilder);
  {
    SampleGrabberCallback* cb = new SampleGrabberCallback(cam);
    grabber->SetCallback(cb, 1);
    cam->grabber = (void*)grabber;
    cam->mediaControl = (void*)mediaControl;
    cam->callback = (void*)cb;
    grabber->SetBufferSamples(true);
    mediaControl->Run();
  }
  return 0;
fail:
  safeRelease(&src);
  safeRelease(&dst);
  safeRelease(&end);
  safeRelease(&nul);
  safeRelease(&nullFilter);
  safeRelease(&grabber);
  safeRelease(&grabberFilter);
  safeRelease(&mediaControl);
  safeRelease(&captureFilter);
  safeRelease(&graphBuilder);
  safeRelease(&moniker);
  return 1;
}
int openCamera ( camera * cam , const char * * errstr ) { cam -> grabber = nullptr ; cam -> mediaControl = nullptr ; cam -> callback = nullptr ; IMoniker * moniker = nullptr ; IGraphBuilder * graphBuilder = nullptr ; IBaseFilter * captureFilter = nullptr ; IMediaControl * mediaControl = nullptr ; IBaseFilter * grabberFilter = nullptr ; ISampleGrabber * grabber = nullptr ; IBaseFilter * nullFilter = nullptr ; IPin * src = nullptr ; IPin * dst = nullptr ; IPin * end = nullptr ; IPin * nul = nullptr ; if ( ! selectCamera ( cam , & moniker , errstr ) ) { goto fail ; } moniker -> BindToObject ( 0 , 0 , IID_IBaseFilter , ( void * * ) & captureFilter ) ; safeRelease ( & moniker ) ; if ( FAILED ( CoCreateInstance ( CLSID_FilterGraph , nullptr , CLSCTX_INPROC , IID_IGraphBuilder , ( void * * ) & graphBuilder ) ) ) { * errstr = errGraphBuilder ; goto fail ; } if ( FAILED ( graphBuilder -> QueryInterface ( IID_IMediaControl , ( void * * ) & mediaControl ) ) ) { * errstr = errNoControl ; goto fail ; } if ( FAILED ( graphBuilder -> AddFilter ( captureFilter , L" " ) ) ) { * errstr = errAddFilter ; goto fail ; } if ( FAILED ( CoCreateInstance ( CLSID_SampleGrabber , nullptr , CLSCTX_INPROC , IID_IBaseFilter , ( void * * ) & grabberFilter ) ) ) { * errstr = errGrabber ; goto fail ; } if ( FAILED ( grabberFilter -> QueryInterface ( IID_ISampleGrabber , ( void * * ) & grabber ) ) ) { * errstr = errGrabber ; goto fail ; } { AM_MEDIA_TYPE mediaType ; memset ( & mediaType , 0 , sizeof ( mediaType ) ) ; mediaType . majortype = MEDIATYPE_Video ; mediaType . subtype = MEDIASUBTYPE_YUY2 ; mediaType . formattype = FORMAT_VideoInfo ; mediaType . bFixedSizeSamples = 1 ; mediaType . cbFormat = sizeof ( VIDEOINFOHEADER ) ; VIDEOINFOHEADER videoInfoHdr ; memset ( & videoInfoHdr , 0 , sizeof ( VIDEOINFOHEADER ) ) ; videoInfoHdr . bmiHeader . biSize = sizeof ( BITMAPINFOHEADER ) ; videoInfoHdr . bmiHeader . biWidth = cam -> width ; videoInfoHdr . bmiHeader . biHeight = cam -> height ; videoInfoHdr . bmiHeader . biPlanes = 1 ; videoInfoHdr . bmiHeader . biBitCount = 16 ; videoInfoHdr . bmiHeader . biCompression = MAKEFOURCC ( ' ' , ' ' , ' ' , ' ' ) ; mediaType . pbFormat = ( BYTE * ) & videoInfoHdr ; if ( FAILED ( grabber -> SetMediaType ( & mediaType ) ) ) { * errstr = errGrabber ; goto fail ; } } if ( FAILED ( graphBuilder -> AddFilter ( grabberFilter , L" " ) ) ) { * errstr = errAddFilter ; goto fail ; } if ( FAILED ( CoCreateInstance ( CLSID_NullRenderer , nullptr , CLSCTX_INPROC , IID_IBaseFilter , ( void * * ) & nullFilter ) ) ) { * errstr = errTerminator ; goto fail ; } if ( FAILED ( graphBuilder -> AddFilter ( nullFilter , L" " ) ) ) { * errstr = errAddFilter ; goto fail ; } HRESULT hr ; src = getPin ( captureFilter , PINDIR_OUTPUT ) ; dst = getPin ( grabberFilter , PINDIR_INPUT ) ; if ( src == nullptr || dst == nullptr || FAILED ( hr = graphBuilder -> Connect ( src , dst ) ) ) { * errstr = errConnectFilters ; goto fail ; } safeRelease ( & src ) ; safeRelease ( & dst ) ; end = getPin ( grabberFilter , PINDIR_OUTPUT ) ; nul = getPin ( nullFilter , PINDIR_INPUT ) ; if ( end == nullptr || nul == nullptr || FAILED ( hr = graphBuilder -> Connect ( end , nul ) ) ) { * errstr = errConnectFilters ; goto fail ; } safeRelease ( & end ) ; safeRelease ( & nul ) ; safeRelease ( & nullFilter ) ; safeRelease ( & captureFilter ) ; safeRelease ( & grabberFilter ) ; safeRelease ( & graphBuilder ) ; { SampleGrabberCallback * cb = new SampleGrabberCallback ( cam ) ; grabber -> SetCallback ( cb , 1 ) ; cam -> grabber = ( void * ) grabber ; cam -> mediaControl = ( void * ) mediaControl ; cam -> callback = ( void * ) cb ; grabber -> SetBufferSamples ( true ) ; mediaControl -> Run ( ) ; } return 0 ; fail : safeRelease ( & src ) ; safeRelease ( & dst ) ; safeRelease ( & end ) ; safeRelease ( & nul ) ; safeRelease ( & nullFilter ) ; safeRelease ( & grabber ) ; safeRelease ( & grabberFilter ) ; safeRelease ( & mediaControl ) ; safeRelease ( & captureFilter ) ; safeRelease ( & graphBuilder ) ; safeRelease ( & moniker ) ; return 1 ; }
openCamera opens a camera and stores interface handler to camera*.
openCamera opens a camera and stores interface handler to camera * .