tags:

views:

43

answers:

2
HRESULT GrabberCB::SampleCB(double SampleTime, IMediaSample *pSample)
{
    //how to copy the pSample to memory for late use?
}

In fact I need to first store it to memory,and late share it with other applications via pipe.

I'm pretty new to this, anyone knows?

A: 

Like any other COM interface, you can simply grab a reference and the object won't be freed until you release it:

pSave = pSample;
pSave->AddRef();

You can then use pSave wherever you want and later call pSave->Release() when you're done.

Sharing the data with other applications is more involved. You can send just the data over to the other application, or send the interface pointer itself by using COM marshalling.

casablanca
But `pSample` is generated on the fly each time the `SampleCB` is run, will doing it this way cause memory overflow?
Alan
As I mentioned, you must free the object by calling `Release()` after you're done.
casablanca
Can I first call `pSave->Release()` ,then `pSave = pSample;pSave->AddRef(); ` inside `SampleCB` ?
Alan
No, you can't `Release` without calling `AddRef` first - if you do, that'll cause undefined behaviour.
casablanca
+1  A: 

Did you consider using BufferCB instead? That way you have access to the buffer right away and don't have to deal with IMediaSample.

STDMETHODIMP BufferCB(double Time, BYTE *pBuffer, long BufferLen)
{
  //copy pBuffer here
}
BrokenGlass