AV8Buffer.cpp
资源名称:VC++视频传输.rar [点击查看]
上传用户:hxb_1234
上传日期:2010-03-30
资源大小:8328k
文件大小:4k
源码类别:
VC书籍
开发平台:
Visual C++
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // AV8Buffer.cpp
- #include "stdafx.h"
- #include "AV8Buffer.h"
- #include "PlayView.h"
- #include "winsock2.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- extern WSABUF stWSABuf;
- extern HWND hWnd;
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // CAV8Buffer()
- CAV8Buffer::CAV8Buffer(DWORD dwBlock, DWORD dwByte)
- {
- m_dwBlock = dwBlock;
- m_dwByte = dwByte;
- m_pdwIndex = (DWORD *)malloc(dwBlock * sizeof(DWORD));
- m_pdwLength = (DWORD *)malloc(dwBlock * sizeof(DWORD));
- m_pbFull = (BOOL *)malloc(dwBlock * sizeof(BOOL ));
- m_plpArray = (LPSTR *)malloc(dwBlock * sizeof(LPSTR));
- for (DWORD dwIndex = 0; dwIndex < dwBlock; dwIndex++)
- {
- m_pdwIndex [dwIndex] = dwIndex;
- m_pdwLength[dwIndex] = 0;
- m_pbFull [dwIndex] = FALSE;
- m_plpArray [dwIndex] = (LPSTR)malloc(dwByte * sizeof(char));
- }
- m_lEmptyBlock = -1;
- Flag = true;
- }
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // ~CAV8Buffer()
- CAV8Buffer::~CAV8Buffer()
- {
- for (DWORD dwIndex = 0; dwIndex < m_dwBlock; dwIndex++)
- free(m_plpArray[dwIndex]);
- free(m_pdwIndex);
- free(m_pdwLength);
- free(m_pbFull);
- free(m_plpArray);
- }
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // PushIn()
- DWORD CAV8Buffer::PushIn(LPSTR lpData, DWORD dwLength)
- {
- //查询缓冲队列中空块的位置
- if (m_lEmptyBlock < 0)
- m_lEmptyBlock = QueryEmpty();
- //空块的长度
- DWORD dwOldLength = m_pdwLength[m_lEmptyBlock];
- if (m_lEmptyBlock >= 0 && m_plpArray[m_lEmptyBlock])
- {
- memcpy((void *)(m_plpArray[m_lEmptyBlock] + dwOldLength), (const void *)lpData, min(dwLength, m_dwByte - dwOldLength));
- m_pdwLength[m_lEmptyBlock] += min(dwLength, m_dwByte - dwOldLength);
- if (m_pdwLength[m_lEmptyBlock] == m_dwByte)
- {
- m_pbFull[m_lEmptyBlock] = TRUE;
- OrderAll(m_lEmptyBlock);
- //缓冲区满,发送WM_READYSEND消息,启动数据发送线程
- if(Flag)
- ::PostMessage(hWnd,WM_READYSEND,0,0);
- m_lEmptyBlock = -1;
- }
- if (dwLength > m_dwByte - dwOldLength)
- PushIn(lpData + (m_dwByte - dwOldLength), dwLength - (m_dwByte - dwOldLength));
- return min(dwLength, m_dwByte - dwOldLength);
- }
- return 0;
- }
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // PopOut()
- DWORD CAV8Buffer::PopOut(LPSTR lpData, DWORD dwLength)
- {
- for (DWORD dwIndex = 0; dwIndex < m_dwBlock; dwIndex++)
- {
- DWORD dwPopBlock = m_pdwIndex[dwIndex];
- DWORD dwOldLength = m_pdwLength[dwPopBlock];
- if (m_pbFull[dwPopBlock])
- {
- memcpy((void *)lpData, (const void *)m_plpArray[dwPopBlock], min(dwOldLength, dwLength));
- m_pdwLength[dwPopBlock] = 0;
- m_pbFull[dwPopBlock] = FALSE;
- return min(dwOldLength, dwLength);
- }
- }
- return 0;
- }
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // ClearAll()
- void CAV8Buffer::ClearAll()
- {
- for (DWORD dwIndex = 0; dwIndex < m_dwBlock; dwIndex++)
- {
- m_pdwIndex [dwIndex] = dwIndex;
- m_pdwLength[dwIndex] = 0;
- m_pbFull [dwIndex] = FALSE;
- }
- }
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // QueryEmpty()
- LONG CAV8Buffer::QueryEmpty()
- {
- for (DWORD dwIndex = 0; dwIndex < m_dwBlock; dwIndex++)
- if (! m_pbFull[dwIndex])
- return (LONG)dwIndex;
- return -1;
- }
- //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
- // OrderAll()
- void CAV8Buffer::OrderAll(DWORD dwTheLast)
- {
- for (DWORD dwIndex = 0; dwIndex < m_dwBlock; dwIndex++)
- if (m_pdwIndex[dwIndex] == dwTheLast)
- break;
- for (; dwIndex < m_dwBlock - 1; dwIndex++)
- m_pdwIndex[dwIndex] = m_pdwIndex[dwIndex + 1];
- m_pdwIndex[dwIndex] = dwTheLast;
- }
- // The End ///////////////////////////////////////////////////////////////////////////////////////////////////////////////