CMediaSocketClient.cpp
上传用户:hhs829
上传日期:2022-06-17
资源大小:586k
文件大小:3k
- //
- // CMediaSocketClient.cpp
- //
- #include "stdafx.h"
- #include "CMediaSocketClient.h"
- #include "CDataAdmin.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- //////////////////////////////////////////////////////////////////////////////
- CMediaSocketClient::CMediaSocketClient()
- {
- m_dwRecvCount = 0;
- m_bGraphStarted = false;
- m_pDataList = NULL;
- m_hCountWnd = NULL;
- m_LocalFile = NULL;
- // m_LocalFile = new CFile("receive.mpg", CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);
- }
- CMediaSocketClient::~CMediaSocketClient()
- {
- if (m_LocalFile)
- {
- m_LocalFile->Close();
- delete m_LocalFile;
- m_LocalFile = NULL;
- }
- m_pDataList = NULL;
- }
- void CMediaSocketClient::ReceivingLoop(void)
- {
- int nret = 0;
- int nMsgType = 0;
- int nDataLen = 0;
- char buff[sizeof(MSG_HEADER) + MPEG1_PACK_SIZE];
- while (m_bReceiving)
- {
- // Receive the message header
- while ((nret = Receive(buff, sizeof(MSG_HEADER))) == E_SOCKET_NOT_READY)
- {
- Sleep(100);
- }
- // When errors occur or socket closed, terminate this loop
- if (nret == E_SOCKET_FAIL || nret == E_SOCKET_CLOSE)
- break;
- // See what type the payload is...
- PMSG_HEADER pMsg = (PMSG_HEADER) buff;
- nMsgType = pMsg->nMsgType; // Payload type retrieved
- nDataLen = pMsg->nDataSize; // Payload size retrieved
- // Continue to receive the payload
- if (nDataLen > 0)
- {
- while ((nret = Receive(buff, nDataLen)) == E_SOCKET_NOT_READY)
- {
- Sleep(100);
- }
- // When errors occur or socket closed, terminate this loop
- if (nret == E_SOCKET_FAIL || nret == E_SOCKET_CLOSE)
- break;
- }
- // Data processing
- switch (nMsgType)
- {
- case DATA_MEDIA: // Mpeg content data!!!
- // Write to file for debuging
- // if (m_LocalFile)
- // {
- // m_LocalFile->Write(buff, MPEG1_PACK_SIZE);
- // }
- // Add to buffer list for filter graph using
- if (m_pDataList)
- {
- PMPEG1_PACK pData = m_pDataList->GetWriteBuffer();
- if (pData == NULL && m_pDataList->GetListSize() < PACK_MAX_COUNT)
- {
- // Allocate more buffer
- pData = new MPEG1_PACK;
- }
- if (pData != NULL)
- {
- memcpy(pData, buff, nDataLen);
- m_pDataList->ReleaseWriteBuffer(pData);
- }
- // Receive data pack counting...
- m_dwRecvCount++;
- if ((m_dwRecvCount >= PACK_INIT_COUNT) &&
- !m_bGraphStarted)
- {
- ::SetEvent(m_pDataList->m_hBufEnough);
- m_bGraphStarted = true;
- }
- //#ifdef _DEBUG
- if (m_hCountWnd != NULL)
- {
- char buff[15];
- itoa(m_dwRecvCount, buff,10);
- ::SendMessage(m_hCountWnd, WM_SETTEXT, 0, (LPARAM)buff);
- }
- //#endif
- }
- break;
- case RECV_EXIT: // Exit this thread
- m_bReceiving = false;
- break;
- default:
- break;
- }
- }
- // If no more data receiving, flush data
- if (m_pDataList)
- {
- m_pDataList->SetFlushing();
- }
- }
- void CMediaSocketClient::SetRecvBuffer(CDataAdmin * inBuf)
- {
- if (m_pDataList == NULL)
- {
- m_pDataList = inBuf;
- }
- }
- void CMediaSocketClient::SetCountWnd(HWND inWnd)
- {
- m_hCountWnd = inWnd;
- }