从ActiveX流媒体控件捕获帧

时间:2011-12-16 15:30:09

标签: c# dll directshow

也许我遇到的问题有点具体,但我确信这个解决方案会让很多人感兴趣。

现在到了这一点。我有一个播放流媒体视频的ActiveX控件。我的目标是到达它播放的每一帧,并通过一些Windows控件,例如面板在外部c#应用程序中显示它们。

以下是DirectShow转换过滤器示例:

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;
    }

我的想法是,在这个方法的某个地方,我应该放置一个回调函数,它将调用我的托管代码并将pInFrame传递给它。我该怎么做?请帮忙

P.S。我读过很棒的文章Howto implement callback interface from unmanaged DLL to .net app。它的工作原理(当然)。但是,当我将上面的代码修改为:

  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;
}

然后事件不会从Transform方法触发。 TestCallback()方法有效

我被困住了。任何帮助将不胜感激。

0 个答案:

没有答案