hksdkapi.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. #include "hksdkapi.h"
  2. #include "LHQLogAPI.h"
  3. #include <QLibrary>
  4. /* ========================================================================
  5. * ************************* 定义海康的SDK接口 *****************************
  6. * ======================================================================== */
  7. /* 网络登陆相关的函数指针 */
  8. using pNet_DEV_Init = bool(*)();
  9. using pNET_DVR_Cleanup = bool(*)();
  10. using pNET_DVR_SetConnectTime = bool(*)(DWORD dwWaitTime, DWORD dwTryTimes);
  11. using pNET_DVR_SetReconnect = bool(*)(DWORD dwInterval, BOOL bEnableRecon);
  12. using pNET_DVR_Login_V40 = LONG(*)(LPNET_DVR_USER_LOGIN_INFO pLoginInfo,LPNET_DVR_DEVICEINFO_V40 lpDeviceInfo);
  13. using pNET_DVR_Logout = BOOL(*)(LONG lUserID);
  14. using pNET_DVR_GetLastError = DWORD(*)();
  15. using pNET_DVR_RealPlay_V40 = LONG(*)(LONG lUserID, LPNET_DVR_PREVIEWINFO lpPreviewInfo, REALDATACALLBACK fRealDataCallBack_V30, void* pUser);
  16. using pNET_DVR_StopRealPlay = bool(*)(LONG lRealHandle);
  17. using pNET_DVR_SetExceptionCallBack_V30 = BOOL(*)(UINT reserved1, void* reserved2, void (CALLBACK* fExceptionCallBack)(DWORD dwType, LONG lUserID, LONG lHandle, void *pUser), void *pUser);
  18. /* 播放相关的函数指针 */
  19. using pPlayM4_GetPort = BOOL(*)(LONG* nPort);
  20. using pPlayM4_FreePort = BOOL(*)(LONG nPort);
  21. using pPlayM4_Stop = BOOL(*)(LONG nPort);
  22. using pPlayM4_CloseStream = BOOL(*)(LONG nPort);
  23. using pPlayM4_PlaySound = BOOL(*)(LONG nPort);
  24. using pPlayM4_InputData = BOOL(*)(LONG nPort,PBYTE pBuf,DWORD nSize);
  25. using pPlayM4_GetLastError = DWORD(*)(LONG nPort);
  26. #if defined (Q_OS_WIN)
  27. using pPlayM4_Play = BOOL(*)(LONG nPort, HWND hWnd);
  28. using pPlayM4_OpenStream = BOOL(*)(LONG nPort,PBYTE pFileHeadBuf,DWORD nSize,DWORD nBufPoolSize);
  29. using pPlayM4_SetDecCallBackMend = BOOL(*)(LONG nPort, void (CALLBACK* DecCBFun)(long nPort, char * pBuf, long nSize, FRAME_INFO * pFrameInfo, void* nUser,void* nReserved2), void* nUser);
  30. #elif defined (Q_OS_LINUX)
  31. using pPlayM4_Play = int(*)(int nPort, PLAYM4_HWND hWnd);
  32. using pPlayM4_OpenStream = int(*)(int nPort,unsigned char * pFileHeadBuf,unsigned int nSize,unsigned int nBufPoolSize);
  33. using pPlayM4_SetDecCallBackMend = int(*)(int nPort,void (CALLBACK* DecCBFun)(int nPort,char * pBuf,int nSize,FRAME_INFO * pFrameInfo, void* nUser,int nReserved2), void* nUser);
  34. #endif
  35. /* 网络登录相关的函数指针 */
  36. pNet_DEV_Init g_NET_DVR_Init = nullptr;
  37. pNET_DVR_Cleanup g_NET_DVR_Cleanup = nullptr;
  38. pNET_DVR_SetConnectTime g_NET_DVR_SetConnectTime = nullptr;
  39. pNET_DVR_SetReconnect g_NET_DVR_SetReconnect = nullptr;
  40. pNET_DVR_Login_V40 g_NET_DVR_Login_V40 = nullptr;
  41. pNET_DVR_Logout g_NET_DVR_Logout = nullptr;
  42. pNET_DVR_GetLastError g_NET_DVR_GetLastError = nullptr;
  43. pNET_DVR_RealPlay_V40 g_NET_DVR_RealPlay_V40 = nullptr;
  44. pNET_DVR_StopRealPlay g_NET_DVR_StopRealPlay = nullptr;
  45. pNET_DVR_SetExceptionCallBack_V30 g_NET_DVR_SetExceptionCallBack_V30 = nullptr;
  46. /* 播放相关的函数指针 */
  47. pPlayM4_GetPort g_PlayM4_GetPort = nullptr;
  48. pPlayM4_FreePort g_PlayM4_FreePort = nullptr;
  49. pPlayM4_Stop g_PlayM4_Stop = nullptr;
  50. pPlayM4_OpenStream g_PlayM4_OpenStream = nullptr;
  51. pPlayM4_CloseStream g_PlayM4_CloseStream = nullptr;
  52. pPlayM4_SetDecCallBackMend g_PlayM4_SetDecCallBackMend = nullptr;
  53. pPlayM4_Play g_PlayM4_Play = nullptr;
  54. pPlayM4_PlaySound g_PlayM4_PlaySound = nullptr;
  55. pPlayM4_InputData g_PlayM4_InputData = nullptr;
  56. pPlayM4_GetLastError g_PlayM4_GetLastError = nullptr;
  57. /* ========================================================================
  58. * ****************************** API函数 **********************************
  59. * ======================================================================== */
  60. /* 加载动态库 */
  61. bool load_HKSDK_Library(QString libPath)
  62. {
  63. if(libPath.isEmpty())
  64. {
  65. return false;
  66. }
  67. /* 加载网络动态库 */
  68. #if defined(Q_OS_WIN)
  69. QString libNetPath = libPath + "/HCNetSDK.dll";
  70. QString libPlayPath = libPath + "/PlayCtrl.dll";
  71. #elif defined(Q_OS_LINUX)
  72. QString libNetPath = libPath + "/libhcnetsdk.so";
  73. QString libPlayPath = libPath + "/libPlayCtrl.so";
  74. #endif
  75. QLibrary libNet(libNetPath);
  76. if(!libNet.load())
  77. {
  78. LH_WRITE_ERROR(QString("load 《HCNetSDK》 library failed, path: %1").arg(libPath));
  79. return false;
  80. }
  81. g_NET_DVR_Init = (pNet_DEV_Init)libNet.resolve("NET_DVR_Init");
  82. g_NET_DVR_Cleanup = (pNET_DVR_Cleanup)libNet.resolve("NET_DVR_Cleanup");
  83. g_NET_DVR_SetConnectTime = (pNET_DVR_SetConnectTime)libNet.resolve("NET_DVR_SetConnectTime");
  84. g_NET_DVR_SetReconnect = (pNET_DVR_SetReconnect)libNet.resolve("NET_DVR_SetReconnect");
  85. g_NET_DVR_Login_V40 = (pNET_DVR_Login_V40)libNet.resolve("NET_DVR_Login_V40");
  86. g_NET_DVR_Logout = (pNET_DVR_Logout)libNet.resolve("NET_DVR_Logout");
  87. g_NET_DVR_GetLastError = (pNET_DVR_GetLastError)libNet.resolve("NET_DVR_GetLastError");
  88. g_NET_DVR_RealPlay_V40 = (pNET_DVR_RealPlay_V40)libNet.resolve("NET_DVR_RealPlay_V40");
  89. g_NET_DVR_StopRealPlay = (pNET_DVR_StopRealPlay)libNet.resolve("NET_DVR_StopRealPlay");
  90. g_NET_DVR_SetExceptionCallBack_V30 = (pNET_DVR_SetExceptionCallBack_V30)libNet.resolve("NET_DVR_SetExceptionCallBack_V30");
  91. if(g_NET_DVR_Init == nullptr || g_NET_DVR_Cleanup == nullptr || g_NET_DVR_SetConnectTime == nullptr ||
  92. g_NET_DVR_SetReconnect == nullptr || g_NET_DVR_Login_V40 == nullptr || g_NET_DVR_Logout == nullptr ||
  93. g_NET_DVR_GetLastError == nullptr || g_NET_DVR_RealPlay_V40 == nullptr || g_NET_DVR_StopRealPlay == nullptr ||
  94. g_NET_DVR_SetExceptionCallBack_V30 == nullptr)
  95. {
  96. LH_WRITE_ERROR("load HCNetSDK library failed, resolve function failed!");
  97. return false;
  98. }
  99. /* 加载播放动态库 */
  100. QLibrary libPlayM4(libPlayPath);
  101. if(!libPlayM4.load())
  102. {
  103. LH_WRITE_ERROR(QString("load 《PlayCtrl》 library failed, path: %1").arg(libPath));
  104. return false;
  105. }
  106. g_PlayM4_GetPort = (pPlayM4_GetPort)libPlayM4.resolve("PlayM4_GetPort");
  107. g_PlayM4_FreePort = (pPlayM4_FreePort)libPlayM4.resolve("PlayM4_FreePort");
  108. g_PlayM4_Stop = (pPlayM4_Stop)libPlayM4.resolve("PlayM4_Stop");
  109. g_PlayM4_OpenStream = (pPlayM4_OpenStream)libPlayM4.resolve("PlayM4_OpenStream");
  110. g_PlayM4_CloseStream = (pPlayM4_CloseStream)libPlayM4.resolve("PlayM4_CloseStream");
  111. g_PlayM4_SetDecCallBackMend = (pPlayM4_SetDecCallBackMend)libPlayM4.resolve("PlayM4_SetDecCallBackMend");
  112. g_PlayM4_Play = (pPlayM4_Play)libPlayM4.resolve("PlayM4_Play");
  113. g_PlayM4_PlaySound = (pPlayM4_PlaySound)libPlayM4.resolve("PlayM4_PlaySound");
  114. g_PlayM4_InputData = (pPlayM4_InputData)libPlayM4.resolve("PlayM4_InputData");
  115. g_PlayM4_GetLastError = (pPlayM4_GetLastError)libPlayM4.resolve("PlayM4_GetLastError");
  116. if(g_PlayM4_GetPort == nullptr || g_PlayM4_FreePort == nullptr || g_PlayM4_Stop == nullptr ||
  117. g_PlayM4_OpenStream == nullptr || g_PlayM4_CloseStream == nullptr || g_PlayM4_SetDecCallBackMend == nullptr ||
  118. g_PlayM4_Play == nullptr || g_PlayM4_PlaySound == nullptr || g_PlayM4_InputData == nullptr || g_PlayM4_GetLastError == nullptr)
  119. {
  120. LH_WRITE_ERROR("load PlayCtrl library failed, resolve function failed!");
  121. return false;
  122. }
  123. return true;
  124. }
  125. bool NET_DVR_Init()
  126. {
  127. if(g_NET_DVR_Init == nullptr)
  128. {
  129. LH_WRITE_ERROR("NET_DVR_Init is nullptr");
  130. return false;
  131. }
  132. return g_NET_DVR_Init();
  133. }
  134. bool NET_DVR_Cleanup()
  135. {
  136. if(g_NET_DVR_Cleanup == nullptr)
  137. {
  138. LH_WRITE_ERROR("NET_DVR_Cleanup is nullptr");
  139. return false;
  140. }
  141. return g_NET_DVR_Cleanup();
  142. }
  143. bool NET_DVR_SetConnectTime(DWORD dwWaitTime, DWORD dwTryTimes)
  144. {
  145. if(g_NET_DVR_SetConnectTime == nullptr)
  146. {
  147. LH_WRITE_ERROR("NET_DVR_SetConnectTime is nullptr");
  148. return false;
  149. }
  150. return g_NET_DVR_SetConnectTime(dwWaitTime, dwTryTimes);
  151. }
  152. bool NET_DVR_SetReconnect(DWORD dwInterval, BOOL bEnableRecon)
  153. {
  154. if(g_NET_DVR_SetReconnect == nullptr)
  155. {
  156. LH_WRITE_ERROR("NET_DVR_SetReconnect is nullptr");
  157. return false;
  158. }
  159. return g_NET_DVR_SetReconnect(dwInterval, bEnableRecon);
  160. }
  161. LONG NET_DVR_Login_V40(LPNET_DVR_USER_LOGIN_INFO pLoginInfo,LPNET_DVR_DEVICEINFO_V40 lpDeviceInfo)
  162. {
  163. if(g_NET_DVR_Login_V40 == nullptr)
  164. {
  165. LH_WRITE_ERROR("NET_DVR_Login_V40 is nullptr");
  166. return -1;
  167. }
  168. return g_NET_DVR_Login_V40(pLoginInfo, lpDeviceInfo);
  169. }
  170. bool NET_DVR_Logout(LONG lUserID)
  171. {
  172. if(g_NET_DVR_Logout == nullptr)
  173. {
  174. LH_WRITE_ERROR("NET_DVR_Logout is nullptr");
  175. return false;
  176. }
  177. return g_NET_DVR_Logout(lUserID);
  178. }
  179. DWORD NET_DVR_GetLastError()
  180. {
  181. if(g_NET_DVR_GetLastError == nullptr)
  182. {
  183. LH_WRITE_ERROR("NET_DVR_GetLastError is nullptr");
  184. return -1;
  185. }
  186. return g_NET_DVR_GetLastError();
  187. }
  188. LONG NET_DVR_RealPlay_V40(LONG lUserID, LPNET_DVR_PREVIEWINFO lpPreviewInfo, REALDATACALLBACK fRealDataCallBack_V30, void* pUser)
  189. {
  190. if(g_NET_DVR_RealPlay_V40 == nullptr)
  191. {
  192. LH_WRITE_ERROR("NET_DVR_RealPlay_V40 is nullptr");
  193. return -1;
  194. }
  195. return g_NET_DVR_RealPlay_V40(lUserID, lpPreviewInfo, fRealDataCallBack_V30, pUser);
  196. }
  197. bool NET_DVR_StopRealPlay(LONG lRealHandle)
  198. {
  199. if(g_NET_DVR_StopRealPlay == nullptr)
  200. {
  201. LH_WRITE_ERROR("NET_DVR_StopRealPlay is nullptr");
  202. return false;
  203. }
  204. return g_NET_DVR_StopRealPlay(lRealHandle);
  205. }
  206. bool NET_DVR_SetExceptionCallBack_V30(UINT reserved1, void* reserved2, void (CALLBACK* fExceptionCallBack)(DWORD dwType, LONG lUserID, LONG lHandle, void* pUser), void* pUser)
  207. {
  208. if(g_NET_DVR_SetExceptionCallBack_V30 == nullptr)
  209. {
  210. LH_WRITE_ERROR("NET_DVR_SetExceptionCallBack_V30 is nullptr");
  211. return false;
  212. }
  213. return g_NET_DVR_SetExceptionCallBack_V30(reserved1, reserved2, fExceptionCallBack, pUser);
  214. }
  215. bool PlayM4_GetPort(LONG* nPort)
  216. {
  217. if(g_PlayM4_GetPort == nullptr)
  218. {
  219. LH_WRITE_ERROR("PlayM4_GetPort is nullptr");
  220. return false;
  221. }
  222. return g_PlayM4_GetPort(nPort);
  223. }
  224. bool PlayM4_FreePort(LONG nPort)
  225. {
  226. if(g_PlayM4_FreePort == nullptr)
  227. {
  228. LH_WRITE_ERROR("PlayM4_FreePort is nullptr");
  229. return false;
  230. }
  231. return g_PlayM4_FreePort(nPort);
  232. }
  233. bool PlayM4_Stop(LONG nPort)
  234. {
  235. if(g_PlayM4_Stop == nullptr)
  236. {
  237. LH_WRITE_ERROR("PlayM4_Stop is nullptr");
  238. return false;
  239. }
  240. return g_PlayM4_Stop(nPort);
  241. }
  242. bool PlayM4_CloseStream(LONG nPort)
  243. {
  244. if(g_PlayM4_CloseStream == nullptr)
  245. {
  246. LH_WRITE_ERROR("PlayM4_CloseStream is nullptr");
  247. return false;
  248. }
  249. return g_PlayM4_CloseStream(nPort);
  250. }
  251. bool PlayM4_PlaySound(LONG nPort)
  252. {
  253. if(g_PlayM4_PlaySound == nullptr)
  254. {
  255. LH_WRITE_ERROR("PlayM4_PlaySound is nullptr");
  256. return false;
  257. }
  258. return g_PlayM4_PlaySound(nPort);
  259. }
  260. bool PlayM4_InputData(LONG nPort,PBYTE pBuf,DWORD nSize)
  261. {
  262. if(g_PlayM4_InputData == nullptr)
  263. {
  264. LH_WRITE_ERROR("PlayM4_InputData is nullptr");
  265. return false;
  266. }
  267. return g_PlayM4_InputData(nPort, pBuf, nSize);
  268. }
  269. DWORD PlayM4_GetLastError(LONG nPort)
  270. {
  271. if(g_PlayM4_GetLastError == nullptr)
  272. {
  273. LH_WRITE_ERROR("PlayM4_GetLastError is nullptr");
  274. return -1;
  275. }
  276. return g_PlayM4_GetLastError(nPort);
  277. }
  278. #if defined (Q_OS_WIN)
  279. bool PlayM4_Play(LONG nPort, HWND hWnd)
  280. #elif defined (Q_OS_LINUX)
  281. int PlayM4_Play(int nPort, PLAYM4_HWND hWnd)
  282. #endif
  283. {
  284. if(g_PlayM4_Play == nullptr)
  285. {
  286. LH_WRITE_ERROR("PlayM4_Play is nullptr");
  287. return false;
  288. }
  289. return g_PlayM4_Play(nPort, hWnd);
  290. }
  291. #if defined (Q_OS_WIN)
  292. bool PlayM4_OpenStream(LONG nPort,PBYTE pFileHeadBuf,DWORD nSize,DWORD nBufPoolSize)
  293. #elif defined (Q_OS_LINUX)
  294. int PlayM4_OpenStream(int nPort,unsigned char * pFileHeadBuf,unsigned int nSize,unsigned int nBufPoolSize)
  295. #endif
  296. {
  297. if(g_PlayM4_OpenStream == nullptr)
  298. {
  299. LH_WRITE_ERROR("PlayM4_OpenStream is nullptr");
  300. return false;
  301. }
  302. return g_PlayM4_OpenStream(nPort, pFileHeadBuf, nSize, nBufPoolSize);
  303. }
  304. #if defined (Q_OS_WIN)
  305. bool PlayM4_SetDecCallBackMend(LONG nPort, void (CALLBACK* DecCBFun)(long nPort, char * pBuf, long nSize, FRAME_INFO * pFrameInfo, void* nUser,void* nReserved2), void* nUser)
  306. #elif defined (Q_OS_LINUX)
  307. int PlayM4_SetDecCallBackMend(int nPort, void (CALLBACK* DecCBFun)(int nPort, char * pBuf, int nSize, FRAME_INFO * pFrameInfo, void* nUser, int nReserved2), void* nUser)
  308. #endif
  309. {
  310. if(g_PlayM4_SetDecCallBackMend == nullptr)
  311. {
  312. LH_WRITE_ERROR("PlayM4_SetDecCallBackMend is nullptr");
  313. return false;
  314. }
  315. return g_PlayM4_SetDecCallBackMend(nPort, DecCBFun, nUser);
  316. }