1

Perhaps the problem i have is a bit specific but I'm sure the solution would be interesting for a lot of people.

Now to the point. I have an ActiveX control that plays streaming video. My goal is to get to every frame it plays and display them in external c# application over some windows control, a panel, for instance.

Here is the sample DirectShow transform filter:

STDMETHODIMP CTransform::Transform(BSTR bsResource, struct U_VideoFrame *pInFrame, struct U_VideoFrameData **pOutFrameData)
    {
        //Must allocate memory this way, the output size must be equal to input size
        *pOutFrameData = (U_VideoFrameData*)CoTaskMemAlloc(sizeof(U_VideoFrameData));
        (*pOutFrameData)->pFrame = (BYTE*)CoTaskMemAlloc(pInFrame->Frame.nLength);
        (*pOutFrameData)->nLength = pInFrame->Frame.nLength;

        //Now transform data contained in (*pOutFrameData)->pFrame; 
        //We simply copy data here
        memcpy((*pOutFrameData)->pFrame, pInFrame->Frame.pFrame, pInFrame->Frame.nLength);

        return S_OK;
    }

My idea is that somewhere inside this method I should place a callback function that will call my managed code and pass pInFrame to it. How can I do it? Please help

P.S. I have read the great article Howto implement callback interface from unmanaged DLL to .net app. It works as described (of course). However, when I modify the code above to this:

  typedef int (__stdcall * Callback)(const char* text);
static Callback Handler = 0;

extern "C" __declspec(dllexport)
void __stdcall SetCallback(Callback handler) {
  Handler = handler;
}

extern "C" __declspec(dllexport)
void __stdcall TestCallback() {
  int retval = Handler("hello world");
}


// CTransform

STDMETHODIMP CTransform::Transform(BSTR bsResource, struct U_VideoFrame *pInFrame, struct U_VideoFrameData **pOutFrameData)
{
    //Must allocate memory this way, the output size must be equal to input size
    *pOutFrameData = (U_VideoFrameData*)CoTaskMemAlloc(sizeof(U_VideoFrameData));
    (*pOutFrameData)->pFrame = (BYTE*)CoTaskMemAlloc(pInFrame->Frame.nLength);
    (*pOutFrameData)->nLength = pInFrame->Frame.nLength;

    //Now transform data contained in (*pOutFrameData)->pFrame; 
    //We simply copy data here
    memcpy((*pOutFrameData)->pFrame, pInFrame->Frame.pFrame, pInFrame->Frame.nLength);
    if (Handler != 0)
        int retval = Handler("Transform");
    return S_OK;
}

then the event does not fire from Transform method. TestCallback() method works

I'm stuck. Any help will be greatly appreciated.

Community
  • 1
  • 1
BesoC
  • 11
  • 2

0 Answers0