hksdkapi.cpp 12 KB

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