views:

273

answers:

1

I have a sample grabber hooked into my directshow graph, based on this example http://msdn.microsoft.com/en-us/library/dd407288(VS.85).aspx the problem is that it uses one shot and buffers. I want to continuously grab samples, and i'd rather have a callback than i guess polling for the samples.

How do use the SetCallback method?

SetCallback(ISampleGrabberCB *pCallback, long WhichMethodToCallback)

how do I point pCallback to my own method?

A: 

I come from a c# background, and thought that at some level i could just pass a reference to a method. This doesn't seem to be case. Instead it requires you create a class the implements its interface that defines the method that it will call. You then pass an instance of the class to the filter in the SetCallback method. Certainly seems long winded in comparison to a delegate or lambda expression

Here is an example of a class implementing ISampleGrabberCB

class SampleGrabberCallback : public ISampleGrabberCB
{
public:
    // Fake referance counting.
    STDMETHODIMP_(ULONG) AddRef() { return 1; }
    STDMETHODIMP_(ULONG) Release() { return 2; }

    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)
    {
        return E_NOTIMPL;
    }

    STDMETHODIMP BufferCB(double Time, BYTE *pBuffer, long BufferLen)
    {
        return E_NOTIMPL;    
    }
};
Mr Bell
This is the way to do it. (Maybe really implement the reference counting ..), but this will work.
Christopher
Whats the deal with the reference counting. Why is it important? Will terrible things happen if I dont?
Mr Bell
The reference counting functions are the COM standard way to ensure that the class will be deleted at the correct time. You can just inherit from CUnknown in the MS baseclasses sample project to get the correct implementation of that.
Alan