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