www.pudn.com > rtptest.rar > AsyncFilter.cpp


// AsyncFilter.cpp: implementation of the CAsyncFilter class. 
// 
////////////////////////////////////////////////////////////////////// 
 
#include "stdafx.h" 
#include "Client.h" 
#include "AsyncFilter.h" 
#ifdef _DEBUG 
#undef THIS_FILE 
static char THIS_FILE[]=__FILE__; 
#define new DEBUG_NEW 
#endif 
 
////////////////////////////////////////////////////////////////////// 
// Construction/Destruction 
////////////////////////////////////////////////////////////////////// 
//DIVX3:33564944-0000-0010-8000-00AA00389B71 
//extern GUID MEDIASUBTYPE_DIVX3; 
DEFINE_GUID(MEDIASUBTYPE_DIVX3, 
			0x33564944, 0x0000, 0x0010, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71); 
 
CAsyncFilter::CAsyncFilter(HRESULT * phr): 
        CAsyncReader(NAME("Source"), NULL, &m_Stream, phr) 
{ 
	m_mt.majortype = MEDIATYPE_Stream; 
 	//m_mt.subtype = MEDIASUBTYPE_NULL; 
	m_mt.subtype = MEDIASUBTYPE_Avi; 
	//m_mt.subtype = MEDIASUBTYPE_DIVX3; 
	WSADATA	wsaData; 
	if(WSAStartup(MAKEWORD(2,2),&wsaData) != 0) 
	{ 
		return; 
	}; 
	unsigned long addr = ntohl(inet_addr("192.168.0.105")); 
	int nRet; 
	nRet = m_rtpSession.Create(10000); 
	nRet = m_rtpSession.AddDestination(addr,10002); 
	nRet = m_rtpSession.SetMaxPacketSize(60000); 
	m_rtpSession.SetDefaultPayloadType(0); 
	m_rtpSession.SetDefaultMark(false); 
	m_rtpSession.SetDefaultTimeStampIncrement(10); 
	m_Stream.SetRTPSession(&m_rtpSession); 
	m_hPollDataThrd = NULL; 
	m_bRunThread = FALSE; 
	StartPollData(); 
 
	CTRLMSG msg; 
	msg.msgType = QUEST_LENGTH; 
	msg.msgSubType = OTHER; 
	msg.dwLength = 0; 
	m_rtpSession.SendPacket(&msg,sizeof(CTRLMSG)); 
} 
 
CAsyncFilter::~CAsyncFilter() 
{ 
	m_rtpSession.ClearDestinations(); 
	//m_rtpSession.Destroy(); 
	WSACleanup(); 
 
} 
 
void CAsyncFilter::SetMediaLength(LONGLONG llMeidaLen) 
{ 
	m_Stream.SetMediaLength(llMeidaLen); 
} 
 
DWORD WINAPI CAsyncFilter::PollDataProc(void *pParam) 
{ 
	CAsyncFilter *pFilter = (CAsyncFilter*)pParam; 
	int nRet = 0; 
	while(1) 
	{ 
		nRet = pFilter->m_rtpSession.PollData(); 
 
		if (pFilter->m_rtpSession.GotoFirstSourceWithData()) 
		{ 
			do 
 
			{ 
				RTPPacket *pack; 
				 
				pack = pFilter->m_rtpSession.GetNextPacket(); 
				pFilter->ProcessQuery((char*)pack->GetPayload(), 
						pack->GetPayloadLength()); 
				delete pack;				 
			} while (pFilter->m_rtpSession.GotoNextSourceWithData()); 
		} 
		Sleep(1); 
	} 
	return 0; 
} 
 
HRESULT CAsyncFilter::ProcessQuery(char *pbData,int nLength) 
{ 
/*	CTRLMSG msg = {0}; 
	memcpy(&msg,pbData,sizeof(CTRLMSG)); 
	if(QUERYMEDIATYPE == msg.nType) 
	{ 
		m_pPin[0]->SendMediaType(); 
		m_pPin[1]->SendMediaType(); 
	}*/ 
	m_Stream.FillBuffer(pbData,nLength); 
	return S_OK; 
} 
 
HRESULT CAsyncFilter::StartPollData() 
{ 
	if(m_hPollDataThrd != NULL) 
		return S_OK; 
	DWORD dwThreadId; 
	m_hPollDataThrd = CreateThread(NULL, 
                     0, 
                     PollDataProc, 
                     (LPVOID)this, 
                     0, 
                     &dwThreadId); 
	if(!m_hPollDataThrd) 
	{ 
		m_hPollDataThrd = NULL; 
		m_bRunThread = FALSE; 
		return S_FALSE; 
	} 
	m_bRunThread = TRUE; 
	return S_OK; 
} 
 
HRESULT CAsyncFilter::StopPollData() 
{ 
	if(m_hPollDataThrd == NULL) return S_OK; 
	m_bRunThread = FALSE; 
	TerminateThread(m_hPollDataThrd,0); 
	m_hPollDataThrd = NULL; 
	return S_OK; 
}