iSampleGrabber,回调方法.代码有效,但可能需要一些爱吗? [英] iSampleGrabber, callback method. Code works, but might need some love?

查看:594
本文介绍了iSampleGrabber,回调方法.代码有效,但可能需要一些爱吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的.这样我就可以使用isamplegrabber回调方法,并且可以将数据放入opencv中.但是由于这对我来说是全新的事实,如果代码正确",我只想获得一些反馈,因为它似乎不是一个很好的代码.

Okay. So I've got my isamplegrabber callback method to work, and I am able to get the data into opencv. But due to the fact that this is totally new to me, I just want to get some feedback if the code is "correct", cause it doesn't seem to be a good one..

首先在我的代码中(来自互联网),我得到了:

At first in my code (from internet) I've got:

#pragma region Formerly located in qedit.h in Windows SDK, now obsoleted and defined within project

void createDirectShowGraph(void);
struct __declspec(uuid("0579154a-2b53-4994-b0d0-e773148eff85"))
ISampleGrabberCB : IUnknown
{
    //
    // Raw methods provided by interface
    //

      virtual HRESULT __stdcall SampleCB (double SampleTime, struct IMediaSample * pSample ) = 0;
      virtual HRESULT __stdcall BufferCB (double SampleTime, unsigned char * pBuffer, long BufferLen ) = 0;
};
static const IID IID_ISampleGrabberCB = { 0x0579154A, 0x2B53, 0x4994, { 0xB0, 0xD0, 0xE7, 0x73, 0x14, 0x8E, 0xFF, 0x85 } };
struct __declspec(uuid("6b652fff-11fe-4fce-92ad-0266b5d7c78f"))
ISampleGrabber : IUnknown
{
    //
    // Raw methods provided by interface
    //
      virtual HRESULT __stdcall SetOneShot (long OneShot ) = 0;
      virtual HRESULT __stdcall SetMediaType (struct _AMMediaType * pType ) = 0;
      virtual HRESULT __stdcall GetConnectedMediaType (struct _AMMediaType * pType ) = 0;
      virtual HRESULT __stdcall SetBufferSamples (long BufferThem ) = 0;
      virtual HRESULT __stdcall GetCurrentBuffer (/*[in,out]*/ long * pBufferSize,
        /*[out]*/ long * pBuffer ) = 0;
      virtual HRESULT __stdcall GetCurrentSample (/*[out,retval]*/ struct IMediaSample * * ppSample ) = 0;
      virtual HRESULT __stdcall SetCallback (struct ISampleGrabberCB * pCallback,long WhichMethodToCallback ) = 0;
};

struct __declspec(uuid("c1f400a0-3f08-11d3-9f0b-006008039e37"))
SampleGrabber;
    // [ default ] interface ISampleGrabber

#pragma endregion

后来,在互联网的大力帮助下,我插入了此内容:

And later, with much help from the internet, I inserted this:

class CFakeCallback : public ISampleGrabberCB 
{
public:
    //some variables and stuff... 
    STDMETHODIMP_(ULONG) AddRef()  { return 2; }
    STDMETHODIMP_(ULONG) Release() { return 1; }

    STDMETHODIMP QueryInterface(REFIID riid, void ** ppv)
    {
        //CheckPointer(ppv, E_POINTER);

        if (riid == IID_ISampleGrabberCB || riid == IID_IUnknown) 
        {
            *ppv = (void *) static_cast<ISampleGrabberCB *>(this);
            return NOERROR;
        }    
        return E_NOINTERFACE;
    }

    STDMETHODIMP SampleCB( double SampleTime, IMediaSample * pSample )
    { 
         //The data for grabbing the frames.
    }

    STDMETHODIMP BufferCB( double SampleTime, BYTE * pBuffer, long BufferLen )
    {

        return 0;
    }

};


CFakeCallback callbackF;

我用:

pSampleGrabber->SetCallback(&callbackF,0);

一切正常,但我想知道. 我需要为回调方法创建一个新的类吗?我可以在"#pragma region ..."中看到所有方法.我不能将这些方法用于回调吗?

Everything works, but I wonder. Do i need to create a new class for the callback method? I can see the all the methods in the "#pragma region..." Can't I use those methods for the callback?

问题:

一个::是否需要为sampleCB/bufferCB方法使用该类"fakeCallback"?还是可以以某种方式使用第一个代码部分中的方法?

one: Do I need to have that class "fakeCallback" for the sampleCB/bufferCB method? Or can I in some way use the methods in the first code-part?

二:虚拟"方法-意味着该方法可以被覆盖"吗?这是我在使用方法sampleCB& bufferCB?

two: "virtual" - method, means that this method can be "overwritten"? is that what I am doing when creating the class fakeCallback, with methods sampleCB & bufferCB?

谢谢!

推荐答案

如果您只想通过使用回调从过滤器图中抓取帧,则需要实现ISampleGrabberCB接口. 您已经实现了CFakeCallback,它是使用ISampleGrabberCB接口所必需的.您可以使用BufferCB或SampleCB来获取样本.因此,在实现CFakeCallback时,您将需要同时覆盖SampleCB和BufferCB.其中一个将包含您的自定义代码以获取示例,而另一个将仅返回S_OK(0).在您的代码中,您使用的是正确的SampleCB.

If you are just want to grab a frame from the filter graph by using callback then you need to implement ISampleGrabberCB interface. You have already implemented CFakeCallback which is required to use ISampleGrabberCB interface. You can grab the sample either using BufferCB or SampleCB. So when you are implementing CFakeCallback you will need to override both SampleCB and BufferCB. One of them will contain you custom code to grab sample while the other will return just S_OK (0). In you code you are using SampleCB which is correct.

但是,如果您不想使用回调方法,则Windows SDK中已经存在SampleGrabber.您只需要在应用程序中包含qedit.h就可以了.

However if you don't want to use callback method then SampleGrabber is already present in Windows SDK. You will just need to include qedit.h in your application and you are done.

这篇关于iSampleGrabber,回调方法.代码有效,但可能需要一些爱吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆