home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / pc / DirectX SDK / DXSDK / samples / Multimedia / Common / include / asyncrdr.h < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-08  |  6.7 KB  |  227 lines

  1. //------------------------------------------------------------------------------
  2. // File: AsyncRdr.h
  3. //
  4. // Desc: DirectShow sample code - base library for I/O functionality.
  5. //
  6. // Copyright (c) 1992-2001 Microsoft Corporation.  All rights reserved.
  7. //------------------------------------------------------------------------------
  8.  
  9.  
  10. #ifndef __ASYNCRDR_H__
  11. #define __ASYNCRDR_H__
  12.  
  13.  
  14. //
  15. // AsyncRdr
  16. //
  17. // Defines an IO source filter.
  18. //
  19. // This filter (CAsyncReader) supports IBaseFilter and IFileSourceFilter interfaces from the
  20. // filter object itself. It has a single output pin (CAsyncOutputPin)
  21. // which supports IPin and IAsyncReader.
  22. //
  23. // This filter is essentially a wrapper for the CAsyncFile class that does
  24. // all the work.
  25. //
  26.  
  27.  
  28. // the filter class (defined below)
  29. class CAsyncReader;
  30.  
  31.  
  32. // the output pin class
  33. class CAsyncOutputPin
  34.   : public IAsyncReader,
  35.     public CBasePin
  36. {
  37. protected:
  38.     CAsyncReader* m_pReader;
  39.     CAsyncIo * m_pIo;
  40.  
  41.     //  This is set every time we're asked to return an IAsyncReader
  42.     //  interface
  43.     //  This allows us to know if the downstream pin can use
  44.     //  this transport, otherwise we can hook up to thinks like the
  45.     //  dump filter and nothing happens
  46.     BOOL         m_bQueriedForAsyncReader;
  47.  
  48.     HRESULT InitAllocator(IMemAllocator **ppAlloc);
  49.  
  50. public:
  51.     // constructor and destructor
  52.     CAsyncOutputPin(
  53.         HRESULT * phr,
  54.     CAsyncReader *pReader,
  55.     CAsyncIo *pIo,
  56.         CCritSec * pLock);
  57.  
  58.     ~CAsyncOutputPin();
  59.  
  60.     // --- CUnknown ---
  61.  
  62.     // need to expose IAsyncReader
  63.     DECLARE_IUNKNOWN
  64.     STDMETHODIMP NonDelegatingQueryInterface(REFIID, void**);
  65.  
  66.     // --- IPin methods ---
  67.     STDMETHODIMP Connect(
  68.         IPin * pReceivePin,
  69.         const AM_MEDIA_TYPE *pmt   // optional media type
  70.     );
  71.  
  72.     // --- CBasePin methods ---
  73.  
  74.     // return the types we prefer - this will return the known
  75.     // file type
  76.     HRESULT GetMediaType(int iPosition, CMediaType *pMediaType);
  77.  
  78.     // can we support this type?
  79.     HRESULT CheckMediaType(const CMediaType* pType);
  80.  
  81.     // Clear the flag so we see if IAsyncReader is queried for
  82.     HRESULT CheckConnect(IPin *pPin)
  83.     {
  84.         m_bQueriedForAsyncReader = FALSE;
  85.         return CBasePin::CheckConnect(pPin);
  86.     }
  87.  
  88.     // See if it was asked for
  89.     HRESULT CompleteConnect(IPin *pReceivePin)
  90.     {
  91.         if (m_bQueriedForAsyncReader) {
  92.             return CBasePin::CompleteConnect(pReceivePin);
  93.         } else {
  94. #ifdef VFW_E_NO_TRANSPORT
  95.             return VFW_E_NO_TRANSPORT;
  96. #else
  97.             return E_FAIL;
  98. #endif
  99.         }
  100.     }
  101.  
  102.     //  Remove our connection status
  103.     HRESULT BreakConnect()
  104.     {
  105.         m_bQueriedForAsyncReader = FALSE;
  106.         return CBasePin::BreakConnect();
  107.     }
  108.  
  109.     // --- IAsyncReader methods ---
  110.     // pass in your preferred allocator and your preferred properties.
  111.     // method returns the actual allocator to be used. Call GetProperties
  112.     // on returned allocator to learn alignment and prefix etc chosen.
  113.     // this allocator will be not be committed and decommitted by
  114.     // the async reader, only by the consumer.
  115.     STDMETHODIMP RequestAllocator(
  116.                       IMemAllocator* pPreferred,
  117.                       ALLOCATOR_PROPERTIES* pProps,
  118.                       IMemAllocator ** ppActual);
  119.  
  120.     // queue a request for data.
  121.     // media sample start and stop times contain the requested absolute
  122.     // byte position (start inclusive, stop exclusive).
  123.     // may fail if sample not obtained from agreed allocator.
  124.     // may fail if start/stop position does not match agreed alignment.
  125.     // samples allocated from source pin's allocator may fail
  126.     // GetPointer until after returning from WaitForNext.
  127.     STDMETHODIMP Request(
  128.                      IMediaSample* pSample,
  129.                      DWORD dwUser);            // user context
  130.  
  131.     // block until the next sample is completed or the timeout occurs.
  132.     // timeout (millisecs) may be 0 or INFINITE. Samples may not
  133.     // be delivered in order. If there is a read error of any sort, a
  134.     // notification will already have been sent by the source filter,
  135.     // and STDMETHODIMP will be an error.
  136.     STDMETHODIMP WaitForNext(
  137.                       DWORD dwTimeout,
  138.                       IMediaSample** ppSample,  // completed sample
  139.                       DWORD * pdwUser);        // user context
  140.  
  141.     // sync read of data. Sample passed in must have been acquired from
  142.     // the agreed allocator. Start and stop position must be aligned.
  143.     // equivalent to a Request/WaitForNext pair, but may avoid the
  144.     // need for a thread on the source filter.
  145.     STDMETHODIMP SyncReadAligned(
  146.                       IMediaSample* pSample);
  147.  
  148.  
  149.     // sync read. works in stopped state as well as run state.
  150.     // need not be aligned. Will fail if read is beyond actual total
  151.     // length.
  152.     STDMETHODIMP SyncRead(
  153.                       LONGLONG llPosition,    // absolute file position
  154.                       LONG lLength,        // nr bytes required
  155.                       BYTE* pBuffer);        // write data here
  156.  
  157.     // return total length of stream, and currently available length.
  158.     // reads for beyond the available length but within the total length will
  159.     // normally succeed but may block for a long period.
  160.     STDMETHODIMP Length(
  161.                       LONGLONG* pTotal,
  162.                       LONGLONG* pAvailable);
  163.  
  164.     // cause all outstanding reads to return, possibly with a failure code
  165.     // (VFW_E_TIMEOUT) indicating they were cancelled.
  166.     // these are defined on IAsyncReader and IPin
  167.     STDMETHODIMP BeginFlush(void);
  168.     STDMETHODIMP EndFlush(void);
  169.  
  170. };
  171.  
  172.  
  173. //
  174. // The filter object itself. Supports IBaseFilter through
  175. // CBaseFilter and also IFileSourceFilter directly in this object
  176.  
  177. class CAsyncReader : public CBaseFilter
  178. {
  179.  
  180. protected:
  181.     // filter-wide lock
  182.     CCritSec m_csFilter;
  183.  
  184.     // all i/o done here
  185.     CAsyncIo m_Io;
  186.  
  187.     // our output pin
  188.     CAsyncOutputPin m_OutputPin;
  189.  
  190.     // Type we think our data is
  191.     CMediaType            m_mt;
  192.  
  193. public:
  194.         
  195.     // construction / destruction
  196.  
  197.     CAsyncReader(
  198.         TCHAR *pName,
  199.         LPUNKNOWN pUnk,
  200.         CAsyncStream *pStream,
  201.         HRESULT *phr);
  202.     ~CAsyncReader();
  203.  
  204.  
  205.     // --- CBaseFilter methods ---
  206.     int GetPinCount();
  207.     CBasePin *GetPin(int n);
  208.  
  209.     // --- Access our media type
  210.     const CMediaType *LoadType() const
  211.     {
  212.         return &m_mt;
  213.     }
  214.  
  215.     virtual HRESULT Connect(
  216.         IPin * pReceivePin,
  217.         const AM_MEDIA_TYPE *pmt   // optional media type
  218.     )
  219.     {
  220.         return m_OutputPin.CBasePin::Connect(pReceivePin, pmt);
  221.     }
  222. };
  223.  
  224.  
  225.  
  226. #endif //__ASYNCRDR_H__
  227.