您的位置:首页 > 编程语言

DirectShow 学习(五) CSource类和其一个具体实现例子的源代码分析

2008-12-30 11:49 316 查看
1. CSourceStream类,是CSource类的OutputPin[source.h/source.cpp][/b]
派生自CAMThread和CBaseOutputPin
l 成员变量:
CSource *m_pFilter; // The parent of this stream
l 新增加的virtual函数:
// Override this to provide the worker thread a means of processing a buffer
virtual HRESULT FillBuffer[/b](IMediaSample *pSamp) PURE[/b];
// Called as the thread is created/destroyed - use to perform
// jobs such as start/stop streaming mode
// If OnThreadCreate returns an error the thread will exit.
virtual HRESULT OnThreadCreate[/b](void) {return NOERROR;};
virtual HRESULT OnThreadDestroy[/b](void) {return NOERROR;};
virtual HRESULT OnThreadStartPlay[/b](void) {return NOERROR;};
virtual HRESULT DoBufferProcessingLoop[/b](void); // the loop executed whilst running
{
Command com;
OnThreadStartPlay[/b]();
do
{
while (!CheckRequest[/b](&com))
{
IMediaSample *pSample;
HRESULT hr = GetDeliveryBuffer[/b](&pSample,NULL,NULL,0);
if (FAILED(hr)) { Sleep(1); continue;}
// Virtual function user will override.
hr = FillBuffer[/b](pSample);
if (hr == S_OK)
{ hr = Deliver[/b](pSample); pSample->Release();if(hr != S_OK) return S_OK;}
else if (hr == S_FALSE)
{ pSample->Release();DeliverEndOfStream[/b]();return S_OK;}
else
{
pSample->Release();DeliverEndOfStream[/b]();
m_pFilter->NotifyEvent(EC_ERRORABORT, hr, 0);[/b]
return hr;
}
}
if (com == CMD_RUN || com == CMD_PAUSE) { Reply[/b](NOERROR); }
else if (com != CMD_STOP) { Reply[/b]((DWORD) E_UNEXPECTED);}
}
while (com != CMD_STOP);
return S_FALSE;
}
virtual HRESULT GetMediaType[/b](CMediaType *pMediaType) {return E_UNEXPECTED;}
l 继承的CBasePin的virtual函数:
HRESULT Active[/b](void); // Starts up the worker thread
{
CAutoLock lock(m_pFilter->pStateLock());
if (m_pFilter->IsActive()) {return S_FALSE;}
if (!IsConnected()) {return NOERROR;}
hr = CBaseOutputPin::Active[/b]();
if (FAILED(hr)) {return hr;}
ASSERT(!ThreadExists());
// start the thread
if (!Create[/b]()) {return E_FAIL;}
// Tell thread to initialize. If OnThreadCreate Fails, so does this.
hr = Init[/b]();
if (FAILED(hr)) return hr;
return Pause[/b]();
}
HRESULT Inactive[/b](void); // Exits the worker thread.
{
CAutoLock lock(m_pFilter->pStateLock());
if (!IsConnected()) {return NOERROR;}
// !!! need to do this[/b] before trying to stop the thread, because
// we may be stuck waiting for our own allocator!!!
hr = CBaseOutputPin::Inactive[/b](); // call this first to Decommit the allocator
if (FAILED(hr)) {return hr;}
if (ThreadExists())
{
hr = Stop[/b]();if (FAILED(hr)) {return hr;}
hr = Exit[/b]();if (FAILED(hr)) {return hr;}
Close(); // Wait for the thread to exit, then tidy up.
}
return NOERROR;
}
virtual HRESULT CheckMediaType[/b](const CMediaType *pMediaType);
{
// 默认只支持一种格式,即只调用新增加的GetMediaType[/b]函数得到MediaType,
// 与输入的Type进行比较
}
virtual HRESULT GetMediaType[/b](int iPosition, CMediaType *pMediaType); // List pos. 0-n
{
// 判断iPosition必须为0,返回新增加的GetMediaType[/b]函数
}
l 操作函数:
HRESULT Init[/b](void) { return CallWorker[/b](CMD_INIT); }
HRESULT Exit[/b](void) { return CallWorker[/b](CMD_EXIT); }
HRESULT Run[/b](void) { return CallWorker[/b](CMD_RUN); }
HRESULT Pause[/b](void) { return CallWorker[/b](CMD_PAUSE); }
HRESULT Stop[/b](void) { return CallWorker[/b](CMD_STOP); }
l CAMThread的virtual函数
// override these if you want to add thread commands
// Return codes > 0 indicate an error occured
virtual DWORD ThreadProc[/b](void); // the thread function
{
// 整个函数实现了一个同步的[/b]通讯Thread。
Command com;
do { com = GetRequest[/b]();if (com != CMD_INIT) { Reply((DWORD) E_UNEXPECTED);} }
while (com != CMD_INIT);
hr = OnThreadCreate[/b](); // perform set up tasks
if (FAILED(hr))
{
OnThreadDestroy[/b]();
Reply[/b](hr); // send failed return code from OnThreadCreate
return 1;
}
Reply[/b](NOERROR);
Command cmd;
do
{
cmd = GetRequest[/b]();
switch (cmd) {
case CMD_EXIT: Reply(NOERROR); break;
case CMD_RUN:
case CMD_PAUSE:Reply(NOERROR); DoBufferProcessingLoop[/b]();break;
case CMD_STOP: Reply(NOERROR); break;
default: Reply((DWORD) E_NOTIMPL); break;}
}
while (cmd != CMD_EXIT);
hr = OnThreadDestroy[/b](); // tidy up.
if (FAILED(hr)) { return 1;}
return 0;
}
l Constructor:
// increments the number of pins present on the filter
CSourceStream(TCHAR *pObjectName, HRESULT *phr, CSource *ps, LPCWSTR pPinName)
: CBaseOutputPin(pObjectName, ps, ps->pStateLock[/b](), phr, pPinName),
m_pFilter(ps) {*phr = m_pFilter->AddPin[/b](this);}
l Deconstructor:
~CSourceStream(void) {m_pFilter->RemovePin[/b](this);}
2. CSource类[source.h/source.cpp][/b]
派生自CBaseFilter
l 成员变量:
int m_iPins; // The number of pins on this filter. Updated by CSourceStream constructors & destructors.
CSourceStream **m_paStreams; // the pins on this filter.
CCritSec m_cStateLock; // Lock this to serialize function accesses to the filter state
其中m_iPins初始化为0,m_paStreams初始化为NULL。
l 继承的CBaseFilter的virtual函数:
int GetPinCount[/b](void);
CBasePin *GetPin[/b](int n);
l 新增加的函数:
CCritSec* pStateLock[/b](void) { return &m_cStateLock; } // provide our critical section
HRESULT AddPin[/b](CSourceStream *);
{
CAutoLock lock(&m_cStateLock);
/* Allocate space for this pin and the old ones */
CSourceStream **paStreams = new CSourceStream *[m_iPins + 1];
if (paStreams == NULL) { return E_OUTOFMEMORY;}
// 如果m_paStreams已经存在,则将其复制到paStreams,略去该步代码…
if (m_paStreams != NULL)
{
CopyMemory((PVOID)paStreams, (PVOID)m_paStreams,
m_iPins * sizeof(m_paStreams[0]));
// 其实下面的这行实现复制功能的代码可有可无
paStreams[m_iPins] = pStream;
delete [] m_paStreams;
}
m_paStreams = paStreams;
m_paStreams[m_iPins] = pStream;
m_iPins++;
}
HRESULT RemovePin[/b](CSourceStream *);
{
// 遍历所有的Pin,如果与输入的指针相同,则删除该Pin,如果只有一个Pin存在,释放m_paStreams,
// 否则将其后的Pin全部前移一位。
}
STDMETHODIMP FindPin[/b](LPCWSTR Id, IPin ** ppPin );
{
// 输入的Id其实是一个数字字符串,用WstrToInt(Id) -1的结果调用GetPin,
// 输出Pin必须调用AddRef
}
int FindPinNumber[/b](IPin *iPin);
{
// 遍历所有Pin,进行指针比较,返回相同那个Pin的Number,否则返回-1
}
3. DirectShow 中Source Filter的一个例子Ball[fball.h/fball.cpp][/b]
Filter Ball是一个能生成跳跃小球的Source Filter。
l Filter类CBouncingBall[/b]:
i. 成员函数:
// The only allowed way to create Bouncing balls!
static CUnknown * WINAPI CreateInstance(LPUNKNOWN lpunk, HRESULT *phr);
{创建(new)了一个新的CBouncingBall的Object。}
ii. Constructor(private):
CBouncingBall::CBouncingBall(LPUNKNOWN lpunk, HRESULT *phr) :
CSource(NAME("Bouncing ball"), lpunk, CLSID_BouncingBall)
{
// 创建Output Pin,首先创建数组指针m_paStreams = (CSourceStream **) new CBallStream*[1];
// 创建真正的Output Pin:m_paStreams[0] = new CBallStream(phr, this, L"A Bouncing Ball!");
}
l Pin 类CBallStream[/b]:
派生自CSourceStream。CBall是一个具体实现其功能的类,这里不做分析。
i. 成员变量:
int m_iImageHeight; // The current image height
int m_iImageWidth; // And current image width
int m_iRepeatTime; // Time in msec between frames
const int m_iDefaultRepeatTime; // Initial m_iRepeatTime
BYTE m_BallPixel[4]; // Represents one coloured ball
int m_iPixelSize; // The pixel size in bytes
PALETTEENTRY m_Palette[256]; // The optimal palette for the image
CCritSec m_cSharedState; // Lock on m_rtSampleTime and m_Ball
CRefTime m_rtSampleTime; // The time stamp for each sample
CBall *m_Ball; // The current ball object
ii. 从CBasePin和CBaseOutputPin继承的virtual函数:
// Ask for buffers of the size appropriate to the agreed media type
HRESULT DecideBufferSize[/b](IMemAllocator *pIMemAlloc, ALLOCATOR_PROPERTIES *pProperties);
{
CAutoLock cAutoLock(m_pFilter->pStateLock());
VIDEOINFO *pvi = (VIDEOINFO *) m_mt.Format();
pProperties->cBuffers = 1;
pProperties->cbBuffer = pvi->bmiHeader.biSizeImage;
ALLOCATOR_PROPERTIES Actual;
hr = pAlloc->SetProperties[/b](pProperties,&Actual);
if(Actual.cbBuffer < pProperties->cbBuffer) return E_FAIL;
}
// Set the agreed media type, and set up the necessary ball parameters
HRESULT SetMediaType[/b](const CMediaType *pMediaType);
{
HRESULT hr = CSourceStream::SetMediaType[/b](pMediaType);
VIDEOINFO * pvi = (VIDEOINFO *) m_mt.Format();
switch(pvi->bmiHeader.biBitCount)
{
// 根据位数不同,8,16,32来对成员变量进行不同的设置
}
}
// We will accept 8, 16, 24 or 32 bit video formats, in any
HRESULT CheckMediaType[/b](const CMediaType *pMediaType);
{
if((*(pMediaType->Type()) != MEDIATYPE_Video) || // we only output video
!(pMediaType->IsFixedSize())) // in fixed size samples
return E_INVALIDARG;
// Check for the subtypes we support
const GUID *SubType = pMediaType->Subtype();
if((*SubType != MEDIASUBTYPE_RGB8)
&& (*SubType != MEDIASUBTYPE_RGB565)
&& (*SubType != MEDIASUBTYPE_RGB555)
&& (*SubType != MEDIASUBTYPE_RGB24)
&& (*SubType != MEDIASUBTYPE_RGB32))
return E_INVALIDARG;
// Get the format area of the media type
VIDEOINFO *pvi = (VIDEOINFO *) pMediaType->Format();
if((pvi->bmiHeader.biWidth < 20) || ( abs(pvi->bmiHeader.biHeight) < 20))
return E_INVALIDARG;
// 别的校验…
}
HRESULT GetMediaType[/b](int iPosition, CMediaType *pmt);
{
CAutoLock cAutoLock(m_pFilter->pStateLock());
if((iPosition < 0) || (if(iPosition < 0))) 错误。
VIDEOINFO *pvi = (VIDEOINFO *) pmt->AllocFormatBuffer(sizeof(VIDEOINFO));
ZeroMemory(pvi, sizeof(VIDEOINFO));
switch(iPosition)
{
case 0: // Return our highest quality 32bit format
case 1: // Return our 24bit format
case 2: // 16 bit per pixel RGB565
case 3: // 16 bits per pixel RGB555
case 4: // 8 bit palettised
}
// 填充pmt的另外一些值
}
// Quality control notifications sent to us
STDMETHODIMP Notify[/b](IBaseFilter * pSender, Quality q);
{
// Notify主要设置了一些成员变量以调节速度
}
iii. 从CSourceStream继承的virtual 函数:
// plots a ball into the supplied video frame
HRESULT FillBuffer[/b](IMediaSample *pms);
{
// 得到Sample的数据指针:pms[/b]->GetPointer[/b](&pData);
// 调用m_Ball的成员函数填充该数据领域
// 设置时间pms->SetTime((REFERENCE_TIME *) &rtStart,(REFERENCE_TIME *) &m_rtSampleTime);
// pms->SetSyncPoint(TRUE);
}
// Resets the stream time to zero
HRESULT OnThreadCreate[/b](void);{初始化了变量m_rtSampleTime和m_iRepeatTime}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐