Not logged in, Join Here! or Log In Below:  
 
News Articles Search    
 

 Home / General Programming / DirectShow, using Sample Grabber Callback function Account Manager
 
Archive Notice: This thread is old and no longer active. It is here for reference purposes. This thread was created on an older version of the flipcode forums, before the site closed in 2005. Please keep that in mind as you view this thread, as many of the topics and opinions may be outdated.
 
weslor

December 27, 2004, 06:32 AM

Hello everybody

I would like to apply a callbackfuntion to the sample grabber for directshow. I have no errors on my code, but the callback-function is not working.

I ame searching for two days on this same code over and over again and seem not to find the problem, can please someone help me?

The callback function should create a file c:Example.bmp , however this is not happing, but no errors to.

I hope and pray someone can help me.

My application:

1. I create the class to hold the callback function
class SampleGrabberCallback : public ISampleGrabberCB

  1.  {
  2. public:
  3.     // Fake referance counting.
  4.     STDMETHODIMP_(ULONG) AddRef() { return 1; }
  5.     STDMETHODIMP_(ULONG) Release() { return 2; }
  6.  
  7.     STDMETHODIMP QueryInterface(REFIID riid, void **ppvObject)
  8.     {
  9.    if (NULL == ppvObject) return E_POINTER;
  10.    if (riid == __uuidof(IUnknown))
  11.    {
  12.   *ppvObject = static_cast<IUnknown*>(this);
  13.    return S_OK;
  14.    }
  15.    if (riid == __uuidof(ISampleGrabberCB))
  16.    {
  17.   *ppvObject = static_cast<ISampleGrabberCB*>(this);
  18.    return S_OK;
  19.    }
  20.    return E_NOTIMPL;
  21.     }
  22.  
  23.     STDMETHODIMP SampleCB(double Time, IMediaSample *pSample)
  24.     {
  25.    return E_NOTIMPL;
  26.     }
  27.  
  28.     STDMETHODIMP BufferCB(double Time, BYTE *pBuffer, long BufferLen)
  29.     {
  30.    if ((g_StillMediaType.majortype != MEDIATYPE_Video) ||
  31.   (g_StillMediaType.formattype != FORMAT_VideoInfo) ||
  32.   (g_StillMediaType.cbFormat < sizeof(VIDEOINFOHEADER)) ||
  33.   (g_StillMediaType.pbFormat == NULL))
  34.    {
  35.   return VFW_E_INVALIDMEDIATYPE;
  36.    }
  37.    HANDLE hf = CreateFile("C:\Example.bmp", GENERIC_WRITE,  
  38.    FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, 0, NULL);
  39.    if (hf == INVALID_HANDLE_VALUE)
  40.    {
  41.   return E_FAIL;
  42.    }
  43.    long cbBitmapInfoSize = g_StillMediaType.cbFormat - SIZE_PREHEADER;
  44.    VIDEOINFOHEADER *pVideoHeader =
  45.       (VIDEOINFOHEADER*)g_StillMediaType.pbFormat;
  46.  
  47.    BITMAPFILEHEADER bfh;
  48.    ZeroMemory(&bfh, sizeof(bfh));
  49.    bfh.bfType = 'MB';  // Little-endian for "MB".
  50.    bfh.bfSize = sizeof( bfh ) + BufferLen + cbBitmapInfoSize;
  51.    bfh.bfOffBits = sizeof( BITMAPFILEHEADER ) + cbBitmapInfoSize;
  52.    
  53.    // Write the file header.
  54.    DWORD dwWritten = 0;
  55.    WriteFile( hf, &bfh, sizeof( bfh ), &dwWritten, NULL );
  56.    WriteFile(hf, HEADER(pVideoHeader), cbBitmapInfoSize, &dwWritten, NULL);    
  57.    WriteFile( hf, pBuffer, BufferLen, &dwWritten, NULL );
  58.    CloseHandle( hf );
  59.    return S_OK;
  60.  
  61.     }
  62. };


2. I define global instance of the class.

  1.  SampleGrabberCallback g_StillCapCB;


3. Building graph, graphbuilder, sample grabber, ..

... building graph (ppgraph2), ICaptureGraphBuilder2(ppbuild2), and render CAPTURE ...

  1.  // Create the Sample Grabber.
  2. // Global instance of the class.
  3. // Add the Sample Grabber filter to the graph.
  4.  
  5. hr = CoCreateInstance(CLSID_SampleGrabber, NULL, CLSCTX_INPROC_SERVER,
  6.     IID_IBaseFilter, (void**)&pSG_Filter);
  7.  
  8. hr = ppGraph2->AddFilter(pSG_Filter, L"SampleGrab");
  9.  
  10. // Add the Null Renderer filter to the graph.
  11.  
  12. hr = CoCreateInstance(CLSID_NullRenderer, NULL, CLSCTX_INPROC_SERVER,
  13.     IID_IBaseFilter, (void**)&pNull);
  14.  
  15. hr = ppGraph2->AddFilter(pNull, L"NullRender");
  16.  
  17. hr = ppBuild2->RenderStream(
  18.     &PIN_CATEGORY_STILL, // Connect this pin ...
  19.     &MEDIATYPE_Video,    // with this media type ...
  20.     pCap2,      // on this filter ...
  21.     pSG_Filter,     // to the Sample Grabber ...
  22.     pNull);    // ... and finally to the Null Renderer.
  23.  
  24. // Configure the Sample Grabber.
  25.  
  26. hr = pSG_Filter->QueryInterface(IID_ISampleGrabber, (void**)&pSG);
  27.  
  28. pSG->SetOneShot(FALSE);
  29. pSG->SetBufferSamples(TRUE);
  30. pSG->SetCallback(&g_StillCapCB, 0);
  31.  
  32. //Get the media type that the still pin used to connect with the Sample Grabber:  
  33. // Store the media type for later use.
  34. hr = pSG->GetConnectedMediaType(&g_StillMediaType);
  35. pSG->Release();
  36.  
  37. //add media controlers to graph
  38. ppGraph2->QueryInterface(IID_IMediaControl, (void **)&pControl2);
  39. ppGraph2->QueryInterface(IID_IMediaEvent, (void **)&pEvent2);
  40.  
  41. //query the Filter Graph Manager for the IVideoWindow interface:  
  42. ppGraph2->QueryInterface(IID_IVideoWindow, (void **)&pVidWin2);
  43. //assign the handler
  44. pVidWin2->put_Owner((OAHWND)pWnd2->GetSafeHwnd());
  45. //voeg style toe
  46. pVidWin2->put_WindowStyle(WS_CHILD | WS_CLIPSIBLINGS);
  47. //stretch window over eigen window
  48. RECT grc;
  49. GetClientRect(pWnd2->GetSafeHwnd(), &grc);
  50. pVidWin2->SetWindowPosition(0, 0, grc.right, grc.bottom);
  51.  
  52. pControl2->Run();


When I add a debug (writes text to file) to the callbackfunction, the debug is not printed, I assume the callback function is not called, can someone help me what I do wrong as newbie? THX.

GreetingZ

Wes

 
alvarado

February 14, 2005, 03:19 PM

You've implemented BufferCB() but you told ISampleGrabber to use SampleCB() by passing it 0 in the call to SetCallback(). Change that to a 1 and it will start calling BufferCB().

 
This thread contains 2 messages.
 
 
Hosting by Solid Eight Studios, maker of PhotoTangler Collage Maker.