source.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. //------------------------------------------------------------------------------
  2. // File: Source.h
  3. //
  4. // Desc: DirectShow base classes - defines classes to simplify creation of
  5. // ActiveX source filters that support continuous generation of data.
  6. // No support is provided for IMediaControl or IMediaPosition.
  7. //
  8. // Copyright (c) 1992-2001 Microsoft Corporation. All rights reserved.
  9. //------------------------------------------------------------------------------
  10. //
  11. // Derive your source filter from CSource.
  12. // During construction either:
  13. // Create some CSourceStream objects to manage your pins
  14. // Provide the user with a means of doing so eg, an IPersistFile interface.
  15. //
  16. // CSource provides:
  17. // IBaseFilter interface management
  18. // IMediaFilter interface management, via CBaseFilter
  19. // Pin counting for CBaseFilter
  20. //
  21. // Derive a class from CSourceStream to manage your output pin types
  22. // Implement GetMediaType/1 to return the type you support. If you support multiple
  23. // types then overide GetMediaType/3, CheckMediaType and GetMediaTypeCount.
  24. // Implement Fillbuffer() to put data into one buffer.
  25. //
  26. // CSourceStream provides:
  27. // IPin management via CBaseOutputPin
  28. // Worker thread management
  29. #ifndef __CSOURCE__
  30. #define __CSOURCE__
  31. class CSourceStream; // The class that will handle each pin
  32. //
  33. // CSource
  34. //
  35. // Override construction to provide a means of creating
  36. // CSourceStream derived objects - ie a way of creating pins.
  37. class CSource : public CBaseFilter {
  38. public:
  39. CSource(__in_opt LPCTSTR pName, __inout_opt LPUNKNOWN lpunk, CLSID clsid, __inout HRESULT *phr);
  40. CSource(__in_opt LPCTSTR pName, __inout_opt LPUNKNOWN lpunk, CLSID clsid);
  41. #ifdef UNICODE
  42. CSource(__in_opt LPCSTR pName, __inout_opt LPUNKNOWN lpunk, CLSID clsid, __inout HRESULT *phr);
  43. CSource(__in_opt LPCSTR pName, __inout_opt LPUNKNOWN lpunk, CLSID clsid);
  44. #endif
  45. ~CSource();
  46. int GetPinCount(void);
  47. CBasePin *GetPin(int n);
  48. // -- Utilities --
  49. CCritSec* pStateLock(void) { return &m_cStateLock; } // provide our critical section
  50. HRESULT AddPin(__in CSourceStream *);
  51. HRESULT RemovePin(__in CSourceStream *);
  52. STDMETHODIMP FindPin(
  53. LPCWSTR Id,
  54. __deref_out IPin ** ppPin
  55. );
  56. int FindPinNumber(__in IPin *iPin);
  57. protected:
  58. int m_iPins; // The number of pins on this filter. Updated by CSourceStream
  59. // constructors & destructors.
  60. CSourceStream **m_paStreams; // the pins on this filter.
  61. CCritSec m_cStateLock; // Lock this to serialize function accesses to the filter state
  62. };
  63. //
  64. // CSourceStream
  65. //
  66. // Use this class to manage a stream of data that comes from a
  67. // pin.
  68. // Uses a worker thread to put data on the pin.
  69. class CSourceStream : public CAMThread, public CBaseOutputPin {
  70. public:
  71. CSourceStream(__in_opt LPCTSTR pObjectName,
  72. __inout HRESULT *phr,
  73. __inout CSource *pms,
  74. __in_opt LPCWSTR pName);
  75. #ifdef UNICODE
  76. CSourceStream(__in_opt LPCSTR pObjectName,
  77. __inout HRESULT *phr,
  78. __inout CSource *pms,
  79. __in_opt LPCWSTR pName);
  80. #endif
  81. virtual ~CSourceStream(void); // virtual destructor ensures derived class destructors are called too.
  82. protected:
  83. CSource *m_pFilter; // The parent of this stream
  84. // *
  85. // * Data Source
  86. // *
  87. // * The following three functions: FillBuffer, OnThreadCreate/Destroy, are
  88. // * called from within the ThreadProc. They are used in the creation of
  89. // * the media samples this pin will provide
  90. // *
  91. // Override this to provide the worker thread a means
  92. // of processing a buffer
  93. virtual HRESULT FillBuffer(IMediaSample *pSamp) PURE;
  94. // Called as the thread is created/destroyed - use to perform
  95. // jobs such as start/stop streaming mode
  96. // If OnThreadCreate returns an error the thread will exit.
  97. virtual HRESULT OnThreadCreate(void) {return NOERROR;};
  98. virtual HRESULT OnThreadDestroy(void) {return NOERROR;};
  99. virtual HRESULT OnThreadStartPlay(void) {return NOERROR;};
  100. // *
  101. // * Worker Thread
  102. // *
  103. HRESULT Active(void); // Starts up the worker thread
  104. HRESULT Inactive(void); // Exits the worker thread.
  105. public:
  106. // thread commands
  107. enum Command {CMD_INIT, CMD_PAUSE, CMD_RUN, CMD_STOP, CMD_EXIT};
  108. HRESULT Init(void) { return CallWorker(CMD_INIT); }
  109. HRESULT Exit(void) { return CallWorker(CMD_EXIT); }
  110. HRESULT Run(void) { return CallWorker(CMD_RUN); }
  111. HRESULT Pause(void) { return CallWorker(CMD_PAUSE); }
  112. HRESULT Stop(void) { return CallWorker(CMD_STOP); }
  113. protected:
  114. Command GetRequest(void) { return (Command) CAMThread::GetRequest(); }
  115. BOOL CheckRequest(Command *pCom) { return CAMThread::CheckRequest( (DWORD *) pCom); }
  116. // override these if you want to add thread commands
  117. virtual DWORD ThreadProc(void); // the thread function
  118. virtual HRESULT DoBufferProcessingLoop(void); // the loop executed whilst running
  119. // *
  120. // * AM_MEDIA_TYPE support
  121. // *
  122. // If you support more than one media type then override these 2 functions
  123. virtual HRESULT CheckMediaType(const CMediaType *pMediaType);
  124. virtual HRESULT GetMediaType(int iPosition, __inout CMediaType *pMediaType); // List pos. 0-n
  125. // If you support only one type then override this fn.
  126. // This will only be called by the default implementations
  127. // of CheckMediaType and GetMediaType(int, CMediaType*)
  128. // You must override this fn. or the above 2!
  129. virtual HRESULT GetMediaType(__inout CMediaType *pMediaType) {return E_UNEXPECTED;}
  130. STDMETHODIMP QueryId(
  131. __deref_out LPWSTR * Id
  132. );
  133. };
  134. #endif // __CSOURCE__