asyncrdr.h
上传用户:hxb_1234
上传日期:2010-03-30
资源大小:8328k
文件大小:6k
源码类别:

VC书籍

开发平台:

Visual C++

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