forked from ZhaoliangGuo/pxScreenLiveStreaming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pxBufferPool.cpp
131 lines (94 loc) · 2.54 KB
/
pxBufferPool.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "StdAfx.h"
#include "pxBufferPool.h"
#include <vector>
using namespace std;
CPxBufferPool::CPxBufferPool(void)
{
::InitializeCriticalSection(&m_csRingBuffer);
m_nCurPos = 0;
m_nBufferSize = DEFAULT_BUFFER_SIZE;
}
CPxBufferPool::~CPxBufferPool(void)
{
::DeleteCriticalSection(&m_csRingBuffer); //ÊÍ·ÅÀïÁÙ½çÇø
m_nCurPos = 0;
}
int CPxBufferPool::Init(int in_nBufferSize, int in_nBufferPoolLen)
{
::EnterCriticalSection(&m_csRingBuffer);
m_nBufferSize = in_nBufferSize;
SPxBuffer sPxBuffer;
for (int i = 0; i < in_nBufferPoolLen; i++)
{
sPxBuffer.lpBuffer = (LPBYTE)VirtualAlloc( NULL ,
in_nBufferSize,
MEM_RESERVE | MEM_COMMIT,
PAGE_READWRITE );
if(NULL == sPxBuffer.lpBuffer)
{
/*DWORD dwError = GetLastError() ;
HRESULT_FROM_WIN32( dwError ) ;*/
break;
}
m_vRingBuffer.push_back(sPxBuffer);
}
m_nBufferListLen = m_vRingBuffer.size();
::LeaveCriticalSection(&m_csRingBuffer);
return m_vRingBuffer.size();
}
void CPxBufferPool::Release()
{
::EnterCriticalSection(&m_csRingBuffer);
SPxBuffer sPxBuffer;
while (!m_vRingBuffer.empty())
{
sPxBuffer = m_vRingBuffer.back();
VirtualFree(sPxBuffer.lpBuffer, 0, MEM_RELEASE);
sPxBuffer.lpBuffer = NULL;
m_vRingBuffer.pop_back();
}
::LeaveCriticalSection(&m_csRingBuffer);
}
int CPxBufferPool::GetEmptyBufferPos()
{
::EnterCriticalSection(&m_csRingBuffer);
int nPos = 0;
// »·ÐÎÄÚ´æ
if ((m_nBufferListLen - 1) == m_nCurPos)
{
m_nCurPos = 0;
nPos = m_nCurPos;
::LeaveCriticalSection(&m_csRingBuffer);
return nPos;
}
nPos = m_nCurPos;
m_nCurPos++;
::LeaveCriticalSection(&m_csRingBuffer);
return nPos;
}
void CPxBufferPool::SetBufferAt(int in_nPos, SPxBuffer *in_psBuffer )
{
::EnterCriticalSection(&m_csRingBuffer);
m_vRingBuffer[in_nPos].eMediaType = in_psBuffer->eMediaType;
memcpy(m_vRingBuffer[in_nPos].lpBuffer, in_psBuffer->lpBuffer, in_psBuffer->nDataLength);
m_vRingBuffer[in_nPos].nDataLength = in_psBuffer->nDataLength;
m_vRingBuffer[in_nPos].ui64Timestamp = in_psBuffer->ui64Timestamp;
m_vRingBuffer[in_nPos].bVideoKeyFrame = in_psBuffer->bVideoKeyFrame;
::LeaveCriticalSection(&m_csRingBuffer);
}
SPxBuffer *CPxBufferPool::GetBufferAt(int in_nPos)
{
::EnterCriticalSection(&m_csRingBuffer);
if (kePxMediaType_Invalid == m_vRingBuffer[in_nPos].bVideoKeyFrame)
{
::LeaveCriticalSection(&m_csRingBuffer);
return NULL;
}
SPxBuffer *psPxBuffer = &m_vRingBuffer[in_nPos];
::LeaveCriticalSection(&m_csRingBuffer);
return psPxBuffer;
}
int CPxBufferPool::GetBufferSize()
{
return m_nBufferSize;
}