-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathHIKCapture.cpp
More file actions
377 lines (333 loc) · 9.3 KB
/
Copy pathHIKCapture.cpp
File metadata and controls
377 lines (333 loc) · 9.3 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include "HIKcapture.h"
HANDLE HIKCapture::hTh1, HIKCapture::hTh2;
unsigned HIKCapture::thID1, HIKCapture::thID2;
int HIKCapture::iMaxFrameCacheNum;
int HIKCapture::giCameraFrameWidth;
int HIKCapture::giCameraFrameHeight;
CRITICAL_SECTION HIKCapture::g_cs_frameList;
std::list<char *> HIKCapture::g_frameList;
int HIKCapture::iFrameRow;
int HIKCapture::iFrameCol;
int HIKCapture::iFPS;
LONG HIKCapture::lPort;
HWND HIKCapture::hWnd;
cv::Mat* HIKCapture::mFrameQuo;
HIKCapture::HIKCapture()
{
HWND hWnd = NULL;
iFrameRow = 720;
iFrameCol = 1280;
iFPS=20;
iMaxFrameCacheNum=30;
}
IplImage* HIKCapture::resizing(IplImage *source)
{
IplImage *desI = cvCreateImage(cvSize(iFrameCol, iFrameRow), source->depth, source->nChannels);
cvResize(source, desI);
return desI;
}
cv::Mat HIKCapture::Yv12ToRGB(uchar *pBuffer, int width, int height)
{
cv::Mat result(height, width, CV_8UC3);
uchar y, cb, cr;
long ySize = width*height;
long uSize;
uSize = ySize >> 2;
//assert(bufferSize == ySize + uSize * 2);
uchar *output = result.data;
uchar *pY = pBuffer;
uchar *pU = pY + ySize;
uchar *pV = pU + uSize;
uchar r, g, b;
for (int i = 0; i<uSize; ++i)
{
for (int j = 0; j<4; ++j)
{
y = pY[i * 4 + j];
cb = pU[i];
cr = pV[i];
//ITU-R standard
b = saturate_cast<uchar>(y + 1.772*(cb - 128));
g = saturate_cast<uchar>(y - 0.344*(cb - 128) - 0.714*(cr - 128));
r = saturate_cast<uchar>(y + 1.402*(cr - 128));
*output++ = b;
*output++ = g;
*output++ = r;
}
}
return result;
}
void HIKCapture::yv12toYUV(char *outYuv, char *inYv12, int width, int height, int widthStep)
{
int col, row;
unsigned int Y, U, V;
int tmp;
int idx;
for (row = 0; row<height; row++)
{
idx = row * widthStep;
int rowptr = row*width;
for (col = 0; col<width; col++)
{
tmp = (row / 2)*(width / 2) + (col / 2);
Y = (unsigned int)inYv12[row*width + col];
U = (unsigned int)inYv12[width*height + width*height / 4 + tmp];
V = (unsigned int)inYv12[width*height + tmp];
outYuv[idx + col * 3] = Y;
outYuv[idx + col * 3 + 1] = U;
outYuv[idx + col * 3 + 2] = V;
}
}
//cout << "frame color transformed: " << "col = " << col << "; row = " << row << endl;
}
void CALLBACK HIKCapture::DecCBFun(long lPort, char * pBuf, long nSize, FRAME_INFO * pFrameInfo, long nReserved1, long nReserved2)
{
long lFrameType = pFrameInfo->nType;
giCameraFrameWidth = pFrameInfo->nWidth;
giCameraFrameHeight = pFrameInfo->nHeight;
//cout << "you are here" << endl;
if (lFrameType == T_YV12)
{
EnterCriticalSection(&g_cs_frameList);
g_frameList.push_back(pBuf);
if (g_frameList.size() > iMaxFrameCacheNum)
{
g_frameList.pop_front();
//cout << "cache max volume" << endl;
}
//cout << "frame pushed" << endl;
LeaveCriticalSection(&g_cs_frameList);
}
}
void CALLBACK HIKCapture::fRealDataCallBack(LONG lRealHandle, DWORD dwDataType, BYTE *pBuffer, DWORD dwBufSize, void *pUser)
{
//HWND hWnd = GetConsoleWindow();
//DWORD dRet;
switch (dwDataType)
{
case NET_DVR_SYSHEAD: //system head
if (!PlayM4_GetPort(&lPort)) //unavailable port
{
break;
}
if (dwBufSize > 0)
{
if (!PlayM4_SetStreamOpenMode(lPort, STREAME_REALTIME)) //set strean mode
{
break;
}
if (!PlayM4_OpenStream(lPort, pBuffer, dwBufSize, iFrameCol * iFrameRow))
{
//dRet = PlayM4_GetLastError(lPort);
break;
}
//decoding callback, no show
if (!PlayM4_SetDecCallBack(lPort, DecCBFun))//DecCBFun
{
//dRet = PlayM4_GetLastError(lPort);
break;
}
//open decoding
if (!PlayM4_Play(lPort,hWnd)) // play the video stream
{
//dRet = PlayM4_GetLastError(lPort);
break;
}
}
break;
case NET_DVR_STREAMDATA:
if (dwBufSize > 0 && lPort != -1)
{
if (!PlayM4_InputData(lPort, pBuffer, dwBufSize))
{
break;
}
}
break;
default:
if (dwBufSize > 0 && lPort != -1)
{
if (!PlayM4_InputData(lPort, pBuffer, dwBufSize))
{
break;
}
}
break;
}
}
void CALLBACK HIKCapture::g_RealDataCallBack_V30(LONG lRealHandle, DWORD dwDataType, BYTE *pBuffer, DWORD dwBufSize, void* dwUser)
{
HWND hWnd = GetConsoleWindow();
switch (dwDataType)
{
case NET_DVR_SYSHEAD:
if (!PlayM4_GetPort(&lPort)) //获取播放库未使用的通道号
{
break;
}
//m_iPort = lPort; //第一次回调的是系统头,将获取的播放库port号赋值给全局port,下次回调数据时即使用此port号播放
if (dwBufSize > 0)
{
if (!PlayM4_SetStreamOpenMode(lPort, STREAME_REALTIME)) //设置实时流播放模式
{
break;
}
if (!PlayM4_OpenStream(lPort, pBuffer, dwBufSize, 1280 * 720))
{
break;
}
if (!PlayM4_Play(lPort, hWnd))
{
break;
}
}
break;
case NET_DVR_STREAMDATA:
if (dwBufSize > 0 && lPort != -1)
{
if (!PlayM4_InputData(lPort, pBuffer, dwBufSize))
{
break;
}
}
break;
default:
if (dwBufSize > 0 && lPort != -1)
{
if (!PlayM4_InputData(lPort, pBuffer, dwBufSize))
{
break;
}
}
break;
}
}
void CALLBACK HIKCapture::g_ExceptionCallBack(DWORD dwType, LONG lUserID, LONG lHandle, void *pUser)
{
char tempbuf[256] = { 0 };
switch (dwType)
{
case EXCEPTION_RECONNECT:
printf("----------reconnect--------%d\n", time(NULL));
break;
default:
break;
}
}
unsigned __stdcall HIKCapture::CameraThread(void *param)
{
cout << "Enter camera thread" << endl;
//---------------------------------------
// 初始化
NET_DVR_Init();
//设置连接时间与重连时间
NET_DVR_SetConnectTime(2000, 1);
NET_DVR_SetReconnect(10000, true);
//---------------------------------------
//设置异常消息回调函数
NET_DVR_SetExceptionCallBack_V30(0, NULL, g_ExceptionCallBack, NULL);
// 注册设备
LONG lUserID;
//登录参数,包括设备地址、登录用户、密码等
NET_DVR_USER_LOGIN_INFO struLoginInfo = { 0 };
struLoginInfo.bUseAsynLogin = 0; //同步登录方式
strcpy_s(struLoginInfo.sDeviceAddress, "0.0.0.0"); //Set your camera IP hera
struLoginInfo.wPort = 8000; //camera port
strcpy_s(struLoginInfo.sUserName, "admin"); //login user ID
strcpy_s(struLoginInfo.sPassword, "adminadmin"); //login user passwd
//设备信息, 输出参数
NET_DVR_DEVICEINFO_V40 struDeviceInfoV40 = { 0 };
cout << "start login" << endl;
lUserID = NET_DVR_Login_V40(&struLoginInfo, &struDeviceInfoV40);
if (lUserID < 0)
{
printf("Login failed, error code: %d\n", NET_DVR_GetLastError());
NET_DVR_Cleanup();
return 0;
}
cout << "login success" << endl;
LONG lRealPlayHandle;
//HWND hWnd = GetConsoleWindow(); Do not set the handle twice, or you get shacking image.
NET_DVR_PREVIEWINFO struPlayInfo = { 0 };
struPlayInfo.hPlayWnd = hWnd; //需要SDK解码时句柄设为有效值,仅取流不解码时可设为空
struPlayInfo.lChannel = 1; //预览通道号
struPlayInfo.dwStreamType = 0; //0-主码流,1-子码流,2-码流3,3-码流4,以此类推
struPlayInfo.dwLinkMode = 0; //0- TCP方式,1- UDP方式,2- 多播方式,3- RTP方式,4-RTP/RTSP,5-RSTP/HTTP
//lRealPlayHandle = NET_DVR_RealPlay_V40(lUserID, &struPlayInfo, g_RealDataCallBack_V30, NULL);
lRealPlayHandle = NET_DVR_RealPlay_V40(lUserID, &struPlayInfo, fRealDataCallBack, NULL);
if (lRealPlayHandle < 0)
{
printf("NET_DVR_RealPlay_V40 error, %d\n", NET_DVR_GetLastError());
NET_DVR_Logout(lUserID);
NET_DVR_Cleanup();
return 0;
}
//cvWaitKey(0);
Sleep(-1); //-1 : forever;
//---------------------------------------
//关闭预览
if (!NET_DVR_StopRealPlay(lRealPlayHandle))
{
printf("NET_DVR_StopRealPlay error! Error number: %d\n", NET_DVR_GetLastError());
return 0;
}
//log out
NET_DVR_Logout(lUserID);
NET_DVR_Cleanup();
_endthreadex(0);
return 0;
}
unsigned __stdcall HIKCapture::ImgConvertThread(void *param)
{
cout << "Img convert thread started" << endl;
list<char *>::iterator it;
IplImage* pImgYCrCb;
IplImage* pImg;
IplImage* pImResize;
cv::Mat frameTmp;
while(1)
{
//cout << "convert" << endl;
if (g_frameList.size()>0){
EnterCriticalSection(&g_cs_frameList);
it = g_frameList.end();
it--;
pImgYCrCb = cvCreateImage(cvSize(giCameraFrameWidth, giCameraFrameHeight), 8, 3);
yv12toYUV(pImgYCrCb->imageData, (*(it)), giCameraFrameWidth, giCameraFrameHeight, pImgYCrCb->widthStep);
LeaveCriticalSection(&g_cs_frameList);
pImg = cvCreateImage(cvSize(giCameraFrameWidth, giCameraFrameHeight), 8, 3);
cvCvtColor(pImgYCrCb, pImg, CV_YCrCb2RGB);
pImResize = resizing(pImg);
frameTmp = cv::cvarrToMat(pImResize); // IplImage to MAT;
mFrameQuo = &frameTmp;
imshow("HIK Camera", *mFrameQuo);
cvWaitKey(1);
cvReleaseImage(&pImgYCrCb);
cvReleaseImage(&pImResize);
cvReleaseImage(&pImg);
Sleep(1000 / iFPS); //set FPS
}
}
cout << "end frame selection thread" << endl;
Sleep(-1); //-1 : forever;
_endthreadex(0);
return 0;
}
void HIKCapture::initialHIKCamera()
{
InitializeCriticalSection(&g_cs_frameList);
cout << "start camera thread" << endl;
hTh1 = (HANDLE)_beginthreadex(NULL, 0, &CameraThread, NULL, 0, &thID1);
cout << "camera thread created" << endl;
cout << "start imgConvert thread" << endl;
hTh2 = (HANDLE)_beginthreadex(NULL, 0, &ImgConvertThread, NULL, 0, &thID2);
cout << "imgConvert thread created" << endl;
//WaitForSingleObject(hTh1, INFINITE);
Sleep(-1);
}
void HIKCapture::clearHIKCameraSession()
{
CloseHandle(hTh1);
CloseHandle(hTh2);
return;
}