AsyncCB.h
Upload User: geng8029
Upload Date: 2021-01-30
Package Size: 187k
Code Size: 3k
Category:

Audio program

Development Platform:

Visual C++

  1. #pragma once
  2. namespace MediaFoundationSamples
  3. {
  4.     //////////////////////////////////////////////////////////////////////////
  5.     //  AsyncCallback [template]
  6.     //
  7.     //  Description: 
  8.     //  Helper class that routes IMFAsyncCallback::Invoke calls to a class
  9.     //  method on the parent class.
  10.     //
  11.     //  Usage:
  12.     //  Add this class as a member variable. In the parent class constructor,
  13.     //  initialize the AsyncCallback class like this:
  14.     //      m_cb(this, &CYourClass::OnInvoke)
  15.     //  where
  16.     //      m_cb       = AsyncCallback object
  17.     //      CYourClass = parent class
  18.     //      OnInvoke   = Method in the parent class to receive Invoke calls.
  19.     //
  20.     //  The parent's OnInvoke method (you can name it anything you like) must
  21.     //  have a signature that matches the InvokeFn typedef below.
  22.     //////////////////////////////////////////////////////////////////////////
  23.     // T: Type of the parent object
  24.     template<class T>
  25.     class AsyncCallback : public IMFAsyncCallback
  26.     {
  27.     public: 
  28.         typedef HRESULT (T::*InvokeFn)(IMFAsyncResult *pAsyncResult);
  29.         AsyncCallback(T *pParent, InvokeFn fn) : m_pParent(pParent), m_pInvokeFn(fn)
  30.         {
  31.         }
  32.         // IUnknown
  33.         STDMETHODIMP QueryInterface(REFIID iid, void** ppv)
  34.         {
  35.             if (!ppv)
  36.             {
  37.                 return E_POINTER;
  38.             }
  39.             if (iid == __uuidof(IUnknown))
  40.             {
  41.                 *ppv = static_cast<IUnknown*>(static_cast<IMFAsyncCallback*>(this));
  42.             }
  43.             else if (iid == __uuidof(IMFAsyncCallback))
  44.             {
  45.                 *ppv = static_cast<IMFAsyncCallback*>(this);
  46.             }
  47.             else
  48.             {
  49.                 *ppv = NULL;
  50.                 return E_NOINTERFACE;
  51.             }
  52.             AddRef();
  53.             return S_OK;
  54.         }
  55.         STDMETHODIMP_(ULONG) AddRef() { 
  56.             // Delegate to parent class.
  57.             return m_pParent->AddRef(); 
  58.         }
  59.         STDMETHODIMP_(ULONG) Release() { 
  60.             // Delegate to parent class.
  61.             return m_pParent->Release(); 
  62.         }
  63.         // IMFAsyncCallback methods
  64.         STDMETHODIMP GetParameters(DWORD*, DWORD*)
  65.         {
  66.             // Implementation of this method is optional.
  67.             return E_NOTIMPL;
  68.         }
  69.         STDMETHODIMP Invoke(IMFAsyncResult* pAsyncResult)
  70.         {
  71.             return (m_pParent->*m_pInvokeFn)(pAsyncResult);
  72.         }
  73.         T *m_pParent;
  74.         InvokeFn m_pInvokeFn;
  75.     };
  76. }; // namespace MediaFoundationSamples