hksdkapi.cpp 12 KB

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