locked
Sample grabber callback not working RRS feed

  • Question

  • Hi,

    I'm using a sample grabber to grab frames from video files and cameras, but after setting the callback function the SampleCB never gets called.
    Here's some of the code:

    class SampleGrabberCallBack : public ISampleGrabberCB 
        public
        // Fake referance counting. 
        STDMETHODIMP_(ULONG) AddRef() { return 1; } 
        STDMETHODIMP_(ULONG) Release() { return 2; } 
     
        AM_MEDIA_TYPE g_StillMediaType; 
     
        STDMETHODIMP QueryInterface(REFIID riid, void **ppvObject) 
        { 
            if(NULL == ppvObject) return E_POINTER; 
            if(riid == __uuidof(IUnknown)) 
            { 
                *ppvObject = static_cast<IUnknown*>(this); 
                 return S_OK; 
            } 
            if(riid == __uuidof(ISampleGrabberCB)) 
            { 
                *ppvObject = static_cast<ISampleGrabberCB*>(this); 
                 return S_OK; 
            } 
            return E_NOTIMPL; 
        } 
     
        STDMETHODIMP SampleCB(double Time, IMediaSample *pSample) 
        { 
            MainForm->Tag++; MainForm->Caption = MainForm->Tag; //just for testing 
        } 
        STDMETHODIMP BufferCB(double Time, BYTE *pBuffer, long BufferLen) 
    ... 
    }; 
     
    SampleGrabberCallBack *GrabberCB; 
     
    build graph function: 
     
    //Add sample grabber 
        AM_MEDIA_TYPE GrabType; 
        ZeroMemory(&GrabType, sizeof(AM_MEDIA_TYPE)); 
        GrabType.majortype = MEDIATYPE_Video; 
        GrabType.subtype = MEDIASUBTYPE_RGB24; 
     
        hr = CoCreateInstance(CLSID_SampleGrabber, NULL, CLSCTX_INPROC_SERVER, 
                    IID_IBaseFilter, (void**)&pvGrabberBase); 
        if(FAILED(hr)) 
        { 
            GraphError = "Could not create grabber base."
            ReleaseGraphComponents_Video(); 
            UninitializeCOM(); 
            return hr; 
        } 
        hr = pvGraph->AddFilter(pvGrabberBase, L"Sample Grabber"); 
        if(FAILED(hr)) 
        { 
            GraphError = "Could not add grabber base."
            ReleaseGraphComponents_Video(); 
            UninitializeCOM(); 
            return hr; 
        } 
        hr = pvGrabberBase->QueryInterface(IID_ISampleGrabber, (void**)&pvGrabber); 
        if(FAILED(hr)) 
        { 
            GraphError = "Could not create sample grabber."
            ReleaseGraphComponents_Video(); 
            UninitializeCOM(); 
            return hr; 
        } 
        hr = pvGrabber->SetMediaType(&GrabType); 
        if(FAILED(hr)) 
        { 
            GraphError = "Could not set media type."
            ReleaseGraphComponents_Video(); 
            UninitializeCOM(); 
            return hr; 
        } 
        pvGrabber->SetOneShot(false); 
        pvGrabber->SetBufferSamples(false); 
        pvGrabber->SetCallback(GrabberCB, 0); 
     
    If I understand correctly the SampleCB() function should be called on each frame, true? Am I forgetting something maybe?

    Thanks


    Monday, January 5, 2009 11:46 AM

Answers

  • I found the problem :)
    I should have used "SampleGrabberCallBack GrabberCB;" (without *) and "pvGrabber->SetCallback(&GrabberCB, 0);"
    Now it works.

    Thanks for the help!
    • Marked as answer by Luukje Monday, January 5, 2009 1:04 PM
    Monday, January 5, 2009 1:04 PM

All replies

  • First of all, check result "hr = ...->SetCallback(...)". The question is what is your "SampleGrabberCallBack *GrabberCB" variable? Is it still valid at the moment you run the graph? You are correct, you should receive a call on your callback per frame.

    http://alax.info/blog/tag/directshow
    Monday, January 5, 2009 12:06 PM
  • You're right about checking the hresult, but it returns S_OK. The "SampleGrabberCallBack *GrabberCB;" is defined in the form's class (private).
    The "class SampleGrabberCallBack : public ISampleGrabberCB{...}" is located in an hpp file, that is included from the from's header.
    Monday, January 5, 2009 12:58 PM
  • I found the problem :)
    I should have used "SampleGrabberCallBack GrabberCB;" (without *) and "pvGrabber->SetCallback(&GrabberCB, 0);"
    Now it works.

    Thanks for the help!
    • Marked as answer by Luukje Monday, January 5, 2009 1:04 PM
    Monday, January 5, 2009 1:04 PM