FishEyeWindow.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. #include "stdafx.h"
  2. #include <Windows.h>
  3. #include <string>
  4. #include <stdio.h>
  5. #include "HCNetSDK.h"
  6. #include "plaympeg4.h"
  7. #include "FishEyeWindow.h"
  8. CFishEyeWindow::CFishEyeWindow()
  9. {
  10. }
  11. void CFishEyeWindow::Start(HWND hWnd,const char * ip)
  12. {
  13. m_hParent = hWnd;
  14. szAddress = ip;
  15. m_Thread = std::thread(&CFishEyeWindow::Run, this);
  16. }
  17. void CALLBACK g_ExceptionCallBack(DWORD dwType, LONG lUserID, LONG lHandle, void* pUser)
  18. {
  19. char tempbuf[256] = { 0 };
  20. switch (dwType)
  21. {
  22. case EXCEPTION_RECONNECT: //预览时重连
  23. printf("----------reconnect--------%d\n", time(NULL));
  24. break;
  25. default:
  26. break;
  27. }
  28. }
  29. LONG lPort = -1;
  30. void CALLBACK g_RealDataCallBack_V30(LONG lRealHandle, DWORD dwDataType, BYTE* pBuffer, DWORD dwBufSize, void* dwUser)
  31. {
  32. HWND hWnd = (HWND)dwUser;
  33. switch (dwDataType)
  34. {
  35. case NET_DVR_SYSHEAD: //系统头
  36. if (lPort >= 0)
  37. {
  38. break; //该通道取流之前已经获取到句柄,后续接口不需要再调用
  39. }
  40. if (!PlayM4_GetPort(&lPort)) //获取播放库未使用的通道号
  41. {
  42. break;
  43. }
  44. //m_iPort = lPort; //第一次回调的是系统头,将获取的播放库port号赋值给全局port,下次回调数据时即使用此port号播放
  45. if (dwBufSize > 0)
  46. {
  47. if (!PlayM4_SetStreamOpenMode(lPort, STREAME_REALTIME)) //设置实时流播放模式
  48. {
  49. break;
  50. }
  51. if (!PlayM4_OpenStream(lPort, pBuffer, dwBufSize, 1024 * 1024)) //打开流接口
  52. {
  53. break;
  54. }
  55. if (!PlayM4_Play(lPort, hWnd)) //播放开始
  56. {
  57. break;
  58. }
  59. }
  60. break;
  61. case NET_DVR_STREAMDATA: //码流数据
  62. if (dwBufSize > 0 && lPort != -1)
  63. {
  64. if (!PlayM4_InputData(lPort, pBuffer, dwBufSize))
  65. {
  66. break;
  67. }
  68. }
  69. break;
  70. default: //其他数据
  71. if (dwBufSize > 0 && lPort != -1)
  72. {
  73. if (!PlayM4_InputData(lPort, pBuffer, dwBufSize))
  74. {
  75. break;
  76. }
  77. }
  78. break;
  79. }
  80. }
  81. void CFishEyeWindow::Run()
  82. {
  83. m_bRun = true;
  84. WNDCLASSEX wcex;
  85. wcex.cbSize = sizeof(WNDCLASSEX);
  86. wcex.style = CS_HREDRAW | CS_VREDRAW;
  87. wcex.lpfnWndProc = CFishEyeWindow::WndProc;
  88. wcex.cbClsExtra = 0;
  89. wcex.cbWndExtra = 0;
  90. wcex.hInstance = GetModuleHandle(nullptr);
  91. wcex.hIcon = 0;
  92. wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
  93. wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  94. wcex.lpszMenuName = NULL;
  95. char cName[MAX_PATH] = { 0 };
  96. GetModuleFileNameA(wcex.hInstance, cName, sizeof(cName));
  97. const char* szApp = "FisishEye";
  98. wcex.lpszClassName = szApp;
  99. wcex.hIconSm = 0;
  100. RegisterClassEx(&wcex);
  101. m_hWnd = CreateWindow(szApp, nullptr, WS_POPUP| WS_CHILD, 1920, 0, 480, 480, m_hParent, NULL, wcex.hInstance, 0);
  102. SetWindowLongPtr(m_hWnd, GWLP_USERDATA, (LONG_PTR)this);
  103. ShowWindow(m_hWnd, SW_SHOW);
  104. UpdateWindow(m_hWnd);
  105. //---------------------------------------
  106. // 初始化
  107. NET_DVR_Init();
  108. //设置连接时间与重连时间
  109. NET_DVR_SetConnectTime(2000, 1);
  110. NET_DVR_SetReconnect(10000, true);
  111. //---------------------------------------
  112. //设置异常消息回调函数
  113. NET_DVR_SetExceptionCallBack_V30(0, NULL, g_ExceptionCallBack, NULL);
  114. NET_DVR_USER_LOGIN_INFO struLoginInfo = { 0 };
  115. struLoginInfo.bUseAsynLogin = 0; //同步登录方式
  116. strcpy_s(struLoginInfo.sDeviceAddress, szAddress.c_str()); //设备IP地址
  117. struLoginInfo.wPort = 8000; //设备服务端口
  118. strcpy_s(struLoginInfo.sUserName, "admin"); //设备登录用户名
  119. strcpy_s(struLoginInfo.sPassword, "Aa123456"); //设备登录密码
  120. //设备信息, 输出参数
  121. NET_DVR_DEVICEINFO_V40 struDeviceInfoV40 = { 0 };
  122. lUserID = NET_DVR_Login_V40(&struLoginInfo, &struDeviceInfoV40);
  123. if (lUserID < 0)
  124. {
  125. printf("Login failed, error code: %d\n", NET_DVR_GetLastError());
  126. NET_DVR_Cleanup();
  127. return;
  128. }
  129. NET_DVR_PREVIEWINFO struPlayInfo = { 0 };
  130. struPlayInfo.hPlayWnd = NULL; //需要SDK解码时句柄设为有效值,仅取流不解码时可设为空
  131. struPlayInfo.lChannel = 1; //预览通道号
  132. struPlayInfo.dwStreamType = 0; //0-主码流,1-子码流,2-码流3,3-码流4,以此类推
  133. struPlayInfo.dwLinkMode = 1; //0- TCP方式,1- UDP方式,2- 多播方式,3- RTP方式,4-RTP/RTSP,5-RSTP/HTTP
  134. struPlayInfo.bBlocked = 1; //0- 非阻塞取流,1- 阻塞取流
  135. lRealPlayHandle = NET_DVR_RealPlay_V40(lUserID, &struPlayInfo, g_RealDataCallBack_V30, m_hWnd);
  136. if (lRealPlayHandle < 0)
  137. {
  138. printf("NET_DVR_RealPlay_V40 error, %d\n", NET_DVR_GetLastError());
  139. NET_DVR_Logout(lUserID);
  140. NET_DVR_Cleanup();
  141. return;
  142. }
  143. MSG msg;
  144. while (GetMessage(&msg, nullptr, 0, 0))
  145. {
  146. if (msg.message == WM_DESTROY) break;
  147. TranslateMessage(&msg);
  148. DispatchMessage(&msg);
  149. }
  150. }
  151. LRESULT CFishEyeWindow::WndProc(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
  152. {
  153. switch (wMsg)
  154. {
  155. case WM_CREATE:
  156. break;
  157. case WM_DESTROY:
  158. PostQuitMessage(0);
  159. break;
  160. }
  161. return DefWindowProc(hWnd, wMsg, wParam, lParam);
  162. }