最近项目需要实时读取网络摄像头,并对图像做处理,参考网络资料和海康SDK开发文档,文档中给了两种方式(1)SDK直接解码显示(2)实时流数据回调,用户自行处理码流数据(开发文档上以软解显示为例),自己分别在Windows做了两种方式的时延测试,第一种方式测试时延约为200ms,第二种测试时延较大,达到了秒级,即使使用了双线程也较大,原因暂未查到。在linux下做了第二种方式的测试(双线程),测试时延约为200ms。
写本文的原因:(1)记录相关读取代码,因为开发文档给的代码是在VC上测试代码,其中获取窗口句柄的控制台窗口句柄代码在VS上运行不了,希望给后来者一些参考(2)两份相同的代码,Windos和linux上时延差别较大的原因在此抛砖引玉。咨询过海康的SDK技术支持(直接发邮件,基本都会回复,回复也很快),告诉我测试回调函数的耗时,在自己电脑测试,耗时也很小,200ms左右,即使加了imshow()和waitKey()函数。
以下代码可需要配置Opencv和海康SDK头文件和库文件,其中OpenCV和海康SDK的配置可参考网上资料
(1)SDK 直接解码显示
#include
#include
#include "Windows.h"
#include "HCNetSDK.h"
#include
#include
using namespace std;
using namespace cv;
void CALLBACK 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;
}
}
void main() {
//---------------------------------------
//初始化
NET_DVR_Init();
//设置连接时间与重连时间
NET_DVR_SetConnectTime(2000, 1);
NET_DVR_SetReconnect(10000, true);
LONG lUserID;
NET_DVR_DEVICEINFO_V30 struDeviceInfo;
//char addr[] = "10.180.9.227";
char addr[] = "192.168.30.115";
char name[] = "admin";
char password[] = "admin123";
lUserID = NET_DVR_Login_V30(addr, 8000, name, password, &struDeviceInfo);
if (lUserID < 0)
{
printf("Login error, % d\n", NET_DVR_GetLastError());
NET_DVR_Cleanup();
return;
}
//---------------------------------------
//设置异常消息回调函数
NET_DVR_SetExceptionCallBack_V30(0, NULL, g_ExceptionCallBack, NULL);
//---------------------------------------
//启动预览并设置回调数据流
LONG lRealPlayHandle;
//HWND hWnd = GetConsoleHwnd(); //获取窗口句柄
cvNamedWindow("Mywindow", 0);
HWND h = (HWND)cvGetWindowHandle("Mywindow");
NET_DVR_PREVIEWINFO struPlayInfo = { 0 };
struPlayInfo.hPlayWnd = h; //需要 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
struPlayInfo.bBlocked = 1; //0- 非阻塞取流, 1- 阻塞取流
lRealPlayHandle = NET_DVR_RealPlay_V40(lUserID, &struPlayInfo, NULL, NULL);
if (lRealPlayHandle < 0)
{
printf("NET_DVR_RealPlay_V40 error\n");
NET_DVR_Logout(lUserID);
NET_DVR_Cleanup();
return;
}
waitKey();
Sleep(10000);
//---------------------------------------
//关闭预览
NET_DVR_StopRealPlay(lRealPlayHandle);
//注销用户
NET_DVR_Logout(lUserID);
//释放 SDK 资源
NET_DVR_Cleanup();
return;
}
(2)实时流数据回调,用户自行处理码流数据
#include
#include
#include
#include "Windows.h"
#include "HCNetSDK.h"
#include "plaympeg4.h"
#include
#include
using namespace std;
using namespace cv;
LONG nPort = -1;
volatile int gbHandling = 3;
//解码回调 视频为YUV数据(YV12),音频为PCM数据
void CALLBACK DecCBFun(long nPort, char * pBuf, long nSize, FRAME_INFO * pFrameInfo, long nReserved1, long nReserved2)
{
if (gbHandling)
{
gbHandling--;
return;
}
long lFrameType = pFrameInfo->nType;
if (lFrameType == T_YV12)
{
//LARGE_INTEGER t1, t2, tc;
//QueryPerformanceFrequency(&tc);
//QueryPerformanceCounter(&t1);
Mat pImg(pFrameInfo->nHeight, pFrameInfo->nWidth, CV_8UC3);
Mat src(pFrameInfo->nHeight + pFrameInfo->nHeight / 2, pFrameInfo->nWidth, CV_8UC1, pBuf);
cvtColor(src, pImg, CV_YUV2BGR_YV12);
imshow("IPCamera", pImg);
waitKey(1);
//QueryPerformanceCounter(&t2);
//printf("time is %f\n", (t2.QuadPart - t1.QuadPart)*1.0 / tc.QuadPart);
}
gbHandling = 3;
}
///实时流回调
void CALLBACK fRealDataCallBack(LONG lRealHandle, DWORD dwDataType, BYTE *pBuffer, DWORD dwBufSize, void *pUser)
{
switch (dwDataType)
{
case NET_DVR_SYSHEAD: //系统头
if (!PlayM4_GetPort(&nPort)) //获取播放库未使用的通道号
{
break;
}
//m_iPort = lPort; //第一次回调的是系统头,将获取的播放库port号赋值给全局port,下次回调数据时即使用此port号播放
if (dwBufSize > 0)
{
if (!PlayM4_SetStreamOpenMode(nPort, STREAME_REALTIME)) //设置实时流播放模式
{
break;
}
if (!PlayM4_OpenStream(nPort, pBuffer, dwBufSize, 10 * 1024 * 1024)) //打开流接口
{
break;
}
if (!PlayM4_Play(nPort, NULL)) //播放开始
{
break;
}
if (!PlayM4_SetDecCallBack(nPort, DecCBFun))
{
break;
}
}
break;
case NET_DVR_STREAMDATA: //码流数据
if (dwBufSize > 0 && nPort != -1)
{
if (!PlayM4_InputData(nPort, pBuffer, dwBufSize))
{
cout << "error" << PlayM4_GetLastError(nPort) << endl;
break;
}
}
break;
default: //其他数据
if (dwBufSize > 0 && nPort != -1)
{
if (!PlayM4_InputData(nPort, pBuffer, dwBufSize))
{
break;
}
}
break;
}
}
void CALLBACK 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;
}
}
void main()
{
//---------------------------------------
// 初始化
NET_DVR_Init();
//设置连接时间与重连时间
NET_DVR_SetConnectTime(2000, 1);
NET_DVR_SetReconnect(10000, true);
//---------------------------------------
// 注册设备
LONG lUserID;
NET_DVR_DEVICEINFO_V30 struDeviceInfo;
char addr[] = "192.168.30.115";
char name[] = "admin";
char password[] = "admin123";
lUserID = NET_DVR_Login_V30(addr, 8000, name, password, &struDeviceInfo);
if (lUserID < 0)
{
printf("Login error, %d\n", NET_DVR_GetLastError());
NET_DVR_Cleanup();
return;
}
//---------------------------------------
//设置异常消息回调函数
NET_DVR_SetExceptionCallBack_V30(0, NULL, g_ExceptionCallBack, NULL);
//---------------------------------------
//启动预览并设置回调数据流
LONG lRealPlayHandle;
//cvNamedWindow("Mywindow", 0);
cvNamedWindow("IPCamera", 0);
HWND h = (HWND)cvGetWindowHandle("IPCamera");
if (h == 0)
{
cout << "窗口创建失败" << endl;
}
NET_DVR_PREVIEWINFO struPlayInfo = { 0 };
struPlayInfo.hPlayWnd = h; //需要SDK解码时句柄设为有效值,仅取流不解码时可设为空
struPlayInfo.lChannel = 1; //预览通道号
struPlayInfo.dwStreamType = 1; //0-主码流,1-子码流,2-码流3,3-码流4,以此类推
struPlayInfo.dwLinkMode = 0; //0- TCP方式,1- UDP方式,2- 多播方式,3- RTP方式,4-RTP/RTSP,5-RSTP/HTTP
struPlayInfo.bBlocked = 1; //0- 非阻塞取流, 1- 阻塞取流
lRealPlayHandle = NET_DVR_RealPlay_V40(lUserID, &struPlayInfo, fRealDataCallBack, NULL);
if (lRealPlayHandle < 0)
{
printf("NET_DVR_RealPlay_V40 error\n");
printf("%d\n", NET_DVR_GetLastError());
NET_DVR_Logout(lUserID);
NET_DVR_Cleanup();
return;
}
waitKey();
Sleep(1);
//---------------------------------------
//关闭预览
NET_DVR_StopRealPlay(lRealPlayHandle);
//注销用户
NET_DVR_Logout(lUserID);
//释放SDK资源
NET_DVR_Cleanup();
return;
}
参考: