source.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  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. {
  39. public:
  40. CSource(__in_opt LPCTSTR pName, __inout_opt LPUNKNOWN lpunk, CLSID clsid, __inout HRESULT *phr);
  41. CSource(__in_opt LPCTSTR pName, __inout_opt LPUNKNOWN lpunk, CLSID clsid);
  42. #ifdef UNICODE
  43. CSource(__in_opt LPCSTR pName, __inout_opt LPUNKNOWN lpunk, CLSID clsid, __inout HRESULT *phr);
  44. CSource(__in_opt LPCSTR pName, __inout_opt LPUNKNOWN lpunk, CLSID clsid);
  45. #endif
  46. ~CSource();
  47. int GetPinCount(void);
  48. CBasePin *GetPin(int n);
  49. // -- Utilities --
  50. CCritSec* pStateLock(void) {
  51. return &m_cStateLock; // provide our critical section
  52. }
  53. HRESULT AddPin(__in CSourceStream *);
  54. HRESULT RemovePin(__in CSourceStream *);
  55. STDMETHODIMP FindPin(
  56. LPCWSTR Id,
  57. __deref_out IPin ** ppPin
  58. );
  59. int FindPinNumber(__in IPin *iPin);
  60. protected:
  61. int m_iPins; // The number of pins on this filter. Updated by CSourceStream
  62. // constructors & destructors.
  63. CSourceStream **m_paStreams; // the pins on this filter.
  64. CCritSec m_cStateLock; // Lock this to serialize function accesses to the filter state
  65. };
  66. //
  67. // CSourceStream
  68. //
  69. // Use this class to manage a stream of data that comes from a
  70. // pin.
  71. // Uses a worker thread to put data on the pin.
  72. class CSourceStream : public CAMThread, public CBaseOutputPin
  73. {
  74. public:
  75. CSourceStream(__in_opt LPCTSTR pObjectName,
  76. __inout HRESULT *phr,
  77. __inout CSource *pms,
  78. __in_opt LPCWSTR pName);
  79. #ifdef UNICODE
  80. CSourceStream(__in_opt LPCSTR pObjectName,
  81. __inout HRESULT *phr,
  82. __inout CSource *pms,
  83. __in_opt LPCWSTR pName);
  84. #endif
  85. virtual ~CSourceStream(void); // virtual destructor ensures derived class destructors are called too.
  86. protected:
  87. CSource *m_pFilter; // The parent of this stream
  88. // *
  89. // * Data Source
  90. // *
  91. // * The following three functions: FillBuffer, OnThreadCreate/Destroy, are
  92. // * called from within the ThreadProc. They are used in the creation of
  93. // * the media samples this pin will provide
  94. // *
  95. // Override this to provide the worker thread a means
  96. // of processing a buffer
  97. virtual HRESULT FillBuffer(IMediaSample *pSamp) PURE;
  98. // Called as the thread is created/destroyed - use to perform
  99. // jobs such as start/stop streaming mode
  100. // If OnThreadCreate returns an error the thread will exit.
  101. virtual HRESULT OnThreadCreate(void) {
  102. return NOERROR;
  103. };
  104. virtual HRESULT OnThreadDestroy(void) {
  105. return NOERROR;
  106. };
  107. virtual HRESULT OnThreadStartPlay(void) {
  108. return NOERROR;
  109. };
  110. // *
  111. // * Worker Thread
  112. // *
  113. HRESULT Active(void); // Starts up the worker thread
  114. HRESULT Inactive(void); // Exits the worker thread.
  115. public:
  116. // thread commands
  117. enum Command {CMD_INIT, CMD_PAUSE, CMD_RUN, CMD_STOP, CMD_EXIT};
  118. HRESULT Init(void) {
  119. return CallWorker(CMD_INIT);
  120. }
  121. HRESULT Exit(void) {
  122. return CallWorker(CMD_EXIT);
  123. }
  124. HRESULT Run(void) {
  125. return CallWorker(CMD_RUN);
  126. }
  127. HRESULT Pause(void) {
  128. return CallWorker(CMD_PAUSE);
  129. }
  130. HRESULT Stop(void) {
  131. return CallWorker(CMD_STOP);
  132. }
  133. protected:
  134. Command GetRequest(void) {
  135. return (Command) CAMThread::GetRequest();
  136. }
  137. BOOL CheckRequest(Command *pCom) {
  138. return CAMThread::CheckRequest( (DWORD *) pCom);
  139. }
  140. // override these if you want to add thread commands
  141. virtual DWORD ThreadProc(void); // the thread function
  142. virtual HRESULT DoBufferProcessingLoop(void); // the loop executed whilst running
  143. // *
  144. // * AM_MEDIA_TYPE support
  145. // *
  146. // If you support more than one media type then override these 2 functions
  147. virtual HRESULT CheckMediaType(const CMediaType *pMediaType);
  148. virtual HRESULT GetMediaType(int iPosition, __inout CMediaType *pMediaType); // List pos. 0-n
  149. // If you support only one type then override this fn.
  150. // This will only be called by the default implementations
  151. // of CheckMediaType and GetMediaType(int, CMediaType*)
  152. // You must override this fn. or the above 2!
  153. virtual HRESULT GetMediaType(__inout CMediaType *pMediaType) {
  154. return E_UNEXPECTED;
  155. }
  156. STDMETHODIMP QueryId(
  157. __deref_out LPWSTR * Id
  158. );
  159. };
  160. #endif // __CSOURCE__