home *** CD-ROM | disk | FTP | other *** search
/ The Net: Ultimate Internet Guide / WWLCD1.ISO / mac / SiteBldr / AMOVIE / SDK / _SETUP / COMMON.Z / transfrm.h < prev    next >
Encoding:
C/C++ Source or Header  |  1996-04-19  |  9.4 KB  |  302 lines

  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 - 1996  Microsoft Corporation.  All Rights Reserved.
  9. //
  10. //--------------------------------------------------------------------------;
  11.  
  12. //
  13. // base class from which you can derive a simple transform codec.
  14. //
  15. // It assumes the codec has one input and one output stream, and has no
  16. // interest in memory management, interface negotiation or anything else.
  17. //
  18. // derive your class from this, and supply Transform and the media type/format
  19. // negotiation functions. Implement that class, compile and link and
  20. // you're done.
  21.  
  22.  
  23. #ifndef __TRANSFRM__
  24. #define __TRANSFRM__
  25.  
  26. // ======================================================================
  27. // This is the com object that represents a simple transform filter. It
  28. // supports IFilter, IMediaFilter and two pins through nested interfaces
  29. // ======================================================================
  30.  
  31. class CTransformFilter;
  32.  
  33. // ==================================================
  34. // Implements the input pin
  35. // ==================================================
  36.  
  37. class CTransformInputPin : public CBaseInputPin
  38. {
  39.     friend class CTransformFilter;
  40.  
  41. protected:
  42.     CTransformFilter *m_pTransformFilter;
  43.  
  44.  
  45. public:
  46.  
  47.     CTransformInputPin(
  48.         TCHAR *pObjectName,
  49.         CTransformFilter *pTransformFilter,
  50.         HRESULT * phr,
  51.         LPCWSTR pName);
  52.  
  53.     STDMETHODIMP QueryId(LPWSTR * Id)
  54.     {
  55.         CheckPointer(Id,E_INVALIDARG);
  56.         ValidateReadWritePtr(Id,sizeof(LPWSTR));
  57.         *Id = (LPWSTR)CoTaskMemAlloc(8);
  58.         if (*Id==NULL) {
  59.            return E_OUTOFMEMORY;
  60.         }
  61.         lstrcpyW(*Id, L"In");
  62.         return NOERROR;
  63.     }
  64.  
  65.     // Grab and release extra interfaces if required
  66.  
  67.     HRESULT CheckConnect(IPin *pPin);
  68.     HRESULT BreakConnect();
  69.     HRESULT CompleteConnect(IPin *pReceivePin);
  70.  
  71.     // check that we can support this output type
  72.     HRESULT CheckMediaType(const CMediaType* mtIn);
  73.  
  74.     // set the connection media type
  75.     HRESULT SetMediaType(const CMediaType* mt);
  76.  
  77.     // --- IMemInputPin -----
  78.  
  79.     // here's the next block of data from the stream.
  80.     // AddRef it yourself if you need to hold it beyond the end
  81.     // of this call.
  82.     STDMETHODIMP Receive(IMediaSample * pSample);
  83.  
  84.     // provide EndOfStream that passes straight downstream
  85.     // (there is no queued data)
  86.     STDMETHODIMP EndOfStream(void);
  87.  
  88.     // passes it to CTransformFilter::BeginFlush
  89.     STDMETHODIMP BeginFlush(void);
  90.  
  91.     // passes it to CTransformFilter::EndFlush
  92.     STDMETHODIMP EndFlush(void);
  93.  
  94.     STDMETHODIMP NewSegment(
  95.                         REFERENCE_TIME tStart,
  96.                         REFERENCE_TIME tStop,
  97.                         double dRate);
  98.  
  99.     // Pass a Quality notification on to the appropriate sink
  100.     HRESULT PassNotify(Quality q);
  101.  
  102.     // Check if it's OK to process samples
  103.     virtual HRESULT CheckStreaming();
  104.  
  105.     // Media type
  106. public:
  107.     CMediaType& CurrentMediaType() { return m_mt; };
  108.  
  109. };
  110.  
  111. // ==================================================
  112. // Implements the output pin
  113. // ==================================================
  114.  
  115. class CTransformOutputPin : public CBaseOutputPin
  116. {
  117. protected:
  118.     CTransformFilter *m_pTransformFilter;
  119.  
  120. public:
  121.  
  122.     // implement IMediaPosition by passing upstream
  123.     CPosPassThru * m_pPosition;
  124.  
  125.     CTransformOutputPin(
  126.         TCHAR *pObjectName,
  127.         CTransformFilter *pTransformFilter,
  128.         HRESULT * phr,
  129.         LPCWSTR pName);
  130.  
  131.     ~CTransformOutputPin();
  132.  
  133.     // override to expose IMediaPosition
  134.     STDMETHODIMP NonDelegatingQueryInterface(REFIID riid, void **ppv);
  135.  
  136.     // --- CBaseOutputPin ------------
  137.  
  138.     STDMETHODIMP QueryId(LPWSTR * Id)
  139.     {
  140.         CheckPointer(Id,E_INVALIDARG);
  141.         ValidateReadWritePtr(Id,sizeof(LPWSTR));
  142.         *Id = (LPWSTR)CoTaskMemAlloc(8);
  143.         if (*Id==NULL) {
  144.            return E_OUTOFMEMORY;
  145.         }
  146.         lstrcpyW(*Id, L"Out");
  147.         return NOERROR;
  148.     }
  149.  
  150.     // Grab and release extra interfaces if required
  151.  
  152.     HRESULT CheckConnect(IPin *pPin);
  153.     HRESULT BreakConnect();
  154.     HRESULT CompleteConnect(IPin *pReceivePin);
  155.  
  156.     // check that we can support this output type
  157.     HRESULT CheckMediaType(const CMediaType* mtOut);
  158.  
  159.     // set the connection media type
  160.     HRESULT SetMediaType(const CMediaType *pmt);
  161.  
  162.     // called from CBaseOutputPin during connection to ask for
  163.     // the count and size of buffers we need.
  164.     HRESULT DecideBufferSize(
  165.                 IMemAllocator * pAlloc,
  166.                 ALLOCATOR_PROPERTIES *pProp);
  167.  
  168.     // returns the preferred formats for a pin
  169.     HRESULT GetMediaType(int iPosition,CMediaType *pMediaType);
  170.  
  171.     // inherited from IQualityControl via CBasePin
  172.     STDMETHODIMP Notify(IFilter * pSender, Quality q);
  173.  
  174.     // Media type
  175. public:
  176.     CMediaType& CurrentMediaType() { return m_mt; };
  177. };
  178.  
  179.  
  180. class CTransformFilter : public CBaseFilter
  181. {
  182.  
  183. public:
  184.  
  185.     // map getpin/getpincount for base enum of pins to owner
  186.     // override this to return more specialised pin objects
  187.  
  188.     virtual int GetPinCount();
  189.     virtual CBasePin * GetPin(int n);
  190.     STDMETHODIMP FindPin(LPCWSTR Id, IPin **ppPin);
  191.  
  192.     // override state changes to allow derived transform filter
  193.     // to control streaming start/stop
  194.     STDMETHODIMP Stop();
  195.     STDMETHODIMP Pause();
  196.  
  197. public:
  198.  
  199.     CTransformFilter(TCHAR *, LPUNKNOWN, CLSID clsid, HRESULT *);
  200.     ~CTransformFilter();
  201.  
  202.     // =================================================================
  203.     // ----- override these bits ---------------------------------------
  204.     // =================================================================
  205.  
  206.     // These must be supplied in a derived class
  207.  
  208.     virtual HRESULT Transform(IMediaSample * pIn, IMediaSample *pOut);
  209.  
  210.     // check if you can support mtIn
  211.     virtual HRESULT CheckInputType(const CMediaType* mtIn) PURE;
  212.  
  213.     // check if you can support the transform from this input to this output
  214.     virtual HRESULT CheckTransform(const CMediaType* mtIn, const CMediaType* mtOut) PURE;
  215.  
  216.     // this goes in the factory template table to create new instances
  217.     // static CCOMObject * CreateInstance(LPUNKNOWN, HRESULT *);
  218.  
  219.     // call the SetProperties function with appropriate arguments
  220.     virtual HRESULT DecideBufferSize(
  221.                         IMemAllocator * pAllocator,
  222.                         ALLOCATOR_PROPERTIES *pprop) PURE;
  223.  
  224.     // override to suggest OUTPUT pin media types
  225.     virtual HRESULT GetMediaType(int iPosition, CMediaType *pMediaType) PURE;
  226.  
  227.  
  228.  
  229.     // =================================================================
  230.     // ----- Optional Override Methods           -----------------------
  231.     // =================================================================
  232.  
  233.     // you can also override these if you want to know about streaming
  234.     virtual HRESULT StartStreaming();
  235.     virtual HRESULT StopStreaming();
  236.  
  237.     // override if you can do anything constructive with quality notifications
  238.     virtual HRESULT AlterQuality(Quality q);
  239.  
  240.     // override this to know when the media type is actually set
  241.     virtual HRESULT SetMediaType(PIN_DIRECTION direction,const CMediaType *pmt);
  242.  
  243.     // chance to grab extra interfaces on connection
  244.     virtual HRESULT CheckConnect(PIN_DIRECTION dir,IPin *pPin);
  245.     virtual HRESULT BreakConnect(PIN_DIRECTION dir);
  246.     virtual HRESULT CompleteConnect(PIN_DIRECTION direction,IPin *pReceivePin);
  247.  
  248.     // chance to customize the transform process
  249.     virtual HRESULT Receive(IMediaSample *pSample);
  250.     // if you override Receive, you may need to override these three too
  251.     virtual HRESULT EndOfStream(void);
  252.     virtual HRESULT BeginFlush(void);
  253.     virtual HRESULT EndFlush(void);
  254.     virtual HRESULT NewSegment(
  255.                         REFERENCE_TIME tStart,
  256.                         REFERENCE_TIME tStop,
  257.                         double dRate);
  258.  
  259. #ifdef PERF
  260.     // Override to register performance measurement with a less generic string
  261.     // You should do this to avoid confusion with other filters
  262.     virtual void RegisterPerfId()
  263.          {m_idTransform = MSR_REGISTER(TEXT("Transform"));}
  264. #endif // PERF
  265.  
  266.  
  267. // implementation details
  268.  
  269. protected:
  270.  
  271.     int m_idTransform;                 // performance measuring id
  272.     BOOL m_bEOSDelivered;              // have we sent EndOfStream
  273.     BOOL m_bSampleSkipped;             // Did we just skip a frame
  274.     BOOL m_bQualityChanged;            // Have we degraded?
  275.  
  276.     // critical section protecting filter state.
  277.  
  278.     CCritSec m_csFilter;
  279.  
  280.     // critical section stopping state changes (ie Stop) while we're
  281.     // processing a sample.
  282.     //
  283.     // This critical section is held when processing
  284.     // events that occur on the receive thread - Receive() and EndOfStream().
  285.     //
  286.     // If you want to hold both m_csReceive and m_csFilter then grab
  287.     // m_csFilter FIRST - like CTransformFilter::Stop() does.
  288.  
  289.     CCritSec m_csReceive;
  290.  
  291.     // these hold our input and output pins
  292.  
  293.     friend class CTransformInputPin;
  294.     friend class CTransformOutputPin;
  295.     CTransformInputPin *m_pInput;
  296.     CTransformOutputPin *m_pOutput;
  297. };
  298.  
  299. #endif /* __TRANSFRM__ */
  300.  
  301.  
  302.