cameraplayer.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  1. #include "cameraplayer.h"
  2. #include <QThread>
  3. #include <QDebug>
  4. #include <QApplication>
  5. #include "LHQLogAPI.h"
  6. #include "Transcode.h"
  7. CameraPlayer::CameraPlayer(QObject *parent) : QObject(parent)
  8. {
  9. // qDebug() << "主线程ID:" << QThread::currentThreadId();
  10. /* 加载海康的SDK */
  11. QString appPath = QApplication::applicationDirPath();
  12. if(load_HKSDK_Library(appPath))
  13. {
  14. LH_WRITE_LOG("加载海康SDK成功!");
  15. }
  16. // m_imageQueue = new RingQueue<Image_YUV420*>(10);
  17. m_imageQueue.setQueueCapacity(10);
  18. m_imageQueue.setDefaultValue(nullptr);
  19. m_player = new PlayerGLWidget();
  20. // m_player = new PlayerWidget();
  21. /* 连接信号和槽 */
  22. connect(&m_frameTimer, &QTimer::timeout, this, &CameraPlayer::do_updateFrame);
  23. }
  24. CameraPlayer::~CameraPlayer()
  25. {
  26. stopRealPlay();
  27. /* 退出登录 */
  28. NET_DVR_Logout(m_loginID);
  29. /* 释放SDK资源,重复清除资源会怎么样 */
  30. NET_DVR_Cleanup();
  31. if(m_frameTimer.isActive())
  32. {
  33. m_frameTimer.stop();
  34. }
  35. /* 释放播放器资源 */
  36. if(m_player != nullptr)
  37. {
  38. delete m_player;
  39. m_player = nullptr;
  40. }
  41. if(m_transCode != nullptr)
  42. {
  43. delete m_transCode;
  44. m_transCode = nullptr;
  45. }
  46. }
  47. /* 设置摄像机信息 */
  48. bool CameraPlayer::initCamera(QString cameraIP, int cameraPort, QString cameraUser, QString cameraPwd)
  49. {
  50. /* 初始化海康摄像头SDK */
  51. if(!NET_DVR_Init())
  52. {
  53. LH_WRITE_ERROR("NET_DVR_Init 失败!");
  54. return false;
  55. }
  56. /* 设置超时时间 */
  57. NET_DVR_SetConnectTime(3000,3);
  58. NET_DVR_SetReconnect();
  59. /* 设置异常消息回调函数 */
  60. #ifdef Q_OS_LINUX
  61. NET_DVR_SetExceptionCallBack_V30(0, nullptr, exceptionCallBack, nullptr);
  62. #endif
  63. /* 用户登录,同步登陆 */
  64. NET_DVR_USER_LOGIN_INFO loginInfo = {0};
  65. /* 返回的设备信息,包括序列号、设备类型、通道号等 */
  66. NET_DVR_DEVICEINFO_V40 deviceInfo = {0};
  67. strncpy(loginInfo.sDeviceAddress, cameraIP.toStdString().c_str(), cameraIP.count());
  68. strncpy(loginInfo.sUserName, cameraUser.toStdString().c_str(), cameraUser.count());
  69. strncpy(loginInfo.sPassword, cameraPwd.toStdString().c_str(), cameraPwd.count());
  70. loginInfo.wPort = cameraPort; /* 端口 */
  71. loginInfo.bUseAsynLogin = 0; /* 0是同步登陆, 1是异步登录 */
  72. LH_WRITE_LOG_DEBUG(QString("摄像机登录信息: %1:%2:%3:%4")
  73. .arg(loginInfo.sDeviceAddress)
  74. .arg(loginInfo.wPort)
  75. .arg(loginInfo.sUserName)
  76. .arg(loginInfo.sPassword));
  77. /* 这里是同步登陆,返回值就是设备操作ID,异步登录需要在回调函数中取出返回的ID */
  78. m_loginID = NET_DVR_Login_V40(&loginInfo, &deviceInfo);
  79. if(m_loginID < 0)
  80. {
  81. LH_WRITE_ERROR(QString("登录摄像机 %1:%2 失败,错误代码: %3").arg(cameraIP).arg(cameraPort).arg(NET_DVR_GetLastError()));
  82. NET_DVR_Cleanup(); /* 需不需要在这里清除资源? */
  83. return false;
  84. }
  85. /* 取出通道号 */
  86. m_camInfo.AChannelNum = 0;
  87. m_camInfo.AChannelStart = 0;
  88. m_camInfo.DChannelNum = 0;
  89. m_camInfo.DChannelStart = 0;
  90. m_camInfo.AChannelNum = deviceInfo.struDeviceV30.byChanNum; /* 模拟通道个数 */
  91. m_camInfo.AChannelStart = deviceInfo.struDeviceV30.byStartChan; /* 模拟通道的起始通道号 */
  92. unsigned char h8 = deviceInfo.struDeviceV30.byIPChanNum; /* 数字通道的最大个数需要组合起来 */
  93. unsigned char l8 = deviceInfo.struDeviceV30.byHighDChanNum;
  94. m_camInfo.DChannelNum = h8 * 256 + l8;
  95. m_camInfo.DChannelStart = deviceInfo.struDeviceV30.byStartDChan;/* 数字通道起始号 */
  96. LH_WRITE_LOG_DEBUG(QString("最大模拟通道号数:%1,起始号:%2").arg(m_camInfo.AChannelNum).arg(m_camInfo.AChannelStart));
  97. LH_WRITE_LOG_DEBUG(QString("最大数字通道号数:%1,起始号:%2").arg(m_camInfo.DChannelNum).arg(m_camInfo.DChannelStart));
  98. LH_WRITE_LOG("****** HK SDK初始化完成! ******");
  99. return true;
  100. }
  101. /**
  102. * @brief 开启实时预览,传入通道号,开始接收摄像机的数据,但是并不播放
  103. *
  104. * @param Channel
  105. * @return true
  106. * @return false
  107. */
  108. bool CameraPlayer::startRealPlay(int channel)
  109. {
  110. /* 启动预览并设置回调数据流 */
  111. NET_DVR_PREVIEWINFO playInfo = {0};
  112. playInfo.hPlayWnd = 0; //需要SDK解码时句柄设为有效值,仅取流不解码时可设为空
  113. playInfo.lChannel = channel; //预览通道号
  114. playInfo.dwStreamType = 0; //0-主码流,1-子码流,2-码流3,3-码流4,以此类推
  115. playInfo.dwLinkMode = 0; //0- TCP方式,1- UDP方式,2- 多播方式,3- RTP方式,4-RTP/RTSP,5-RSTP/HTTP
  116. playInfo.bBlocked = 1; //0- 非阻塞取流,1- 阻塞取流
  117. /* 设置回调函数 */
  118. m_realPlayHandle = NET_DVR_RealPlay_V40(m_loginID, &playInfo, realDataCallBack, this);
  119. if (m_realPlayHandle < 0)
  120. {
  121. LH_WRITE_ERROR(QString("NET_DVR_RealPlay_V40()调用错误,错误代码:%1").arg(NET_DVR_GetLastError()));
  122. return false;
  123. }
  124. /* 设置回调函数 */
  125. // NET_DVR_SetStandardDataCallBack(m_realPlayHandle, realDataCallBackStandard, 0);
  126. // NET_DVR_SetRealDataCallBackEx(m_realPlayHandle, realDataCallBack, nullptr);
  127. /* 创建转码实例 */
  128. if(m_transCode == nullptr)
  129. {
  130. m_transCode = new TransCode();
  131. }
  132. return true;
  133. }
  134. /* 关闭预览 */
  135. void CameraPlayer::stopRealPlay()
  136. {
  137. /* 停止定时器 */
  138. if(m_frameTimer.isActive())
  139. {
  140. m_frameTimer.stop();
  141. }
  142. //关闭预览
  143. NET_DVR_StopRealPlay(m_realPlayHandle);
  144. /* 释放播放的通道号 */
  145. if(m_playPort != -1)
  146. {
  147. PlayM4_Stop(m_playPort);
  148. PlayM4_CloseStream(m_playPort);
  149. PlayM4_FreePort(m_playPort);
  150. m_playPort = -1;
  151. }
  152. LH_WRITE_LOG("实时获取数据结束");
  153. /* 清空环形队列 */
  154. clearRingQueue();
  155. }
  156. /* 开始播放预览 */
  157. bool CameraPlayer::startPlay()
  158. {
  159. if(m_frameRate == 0)
  160. {
  161. LH_WRITE_ERROR("帧率为0,无法播放");
  162. return false;
  163. }
  164. if(m_frameTimer.isActive())
  165. {
  166. return true;
  167. }
  168. /* 开启定时器 */
  169. m_frameTimer.setTimerType(Qt::PreciseTimer);
  170. m_frameTimer.setInterval(1000 / m_frameRate);
  171. m_frameTimer.start();
  172. return true;
  173. }
  174. /* 设置播放窗口父指针 */
  175. void CameraPlayer::setPlayWndParent(QWidget* playWnd)
  176. {
  177. m_player->setParent(playWnd);
  178. m_player->show();
  179. }
  180. /* 设置播放窗口大小 */
  181. void CameraPlayer::setPlayWndSize(int width, int height)
  182. {
  183. m_player->resize(width, height);
  184. }
  185. /* 更新一帧数据 */
  186. void CameraPlayer::do_updateFrame()
  187. {
  188. /* 获取一帧 */
  189. if(m_imageQueue.isEmpty())
  190. {
  191. LH_WRITE_LOG("图像环形队列为空");
  192. return;
  193. }
  194. // LH_WRITE_LOG("环形队列图片个数:" + QString::number(m_imageQueue.QueueSize()));
  195. /* 以非阻塞的方式先获取一帧数据,获取成功后再出队 */
  196. auto one = m_imageQueue.front_pop_NoBlock();
  197. if(one != nullptr)
  198. {
  199. m_player->updateFrame(*one);
  200. delete one;
  201. }
  202. }
  203. /**
  204. * @brief 异常回调函数
  205. *
  206. * @param type 异常类型,目前就知道以下几个
  207. * @param userID
  208. * @param handle
  209. * @param user
  210. */
  211. void CameraPlayer::exceptionCallBack(unsigned int type, int userID,int handle,void* user)
  212. {
  213. /* 异常代码是16进制的 */
  214. LH_WRITE_ERROR(QString("调用了异常回调函数,异常代码: %1").arg(type));
  215. switch(type)
  216. {
  217. /* 报警上传时网络异常 */
  218. case EXCEPTION_ALARM:
  219. LH_WRITE_ERROR("报警上传时网络异常!!!");
  220. //TODO: 关闭报警上传
  221. break;
  222. /* 网络预览时异常 */
  223. case EXCEPTION_PREVIEW:
  224. LH_WRITE_ERROR("网络预览时网络异常!!!");
  225. //TODO: 关闭网络预览
  226. break;
  227. /* 预览时重连 */
  228. case EXCEPTION_RECONNECT:
  229. break;
  230. case EXCEPTION_PLAYBACK:
  231. LH_WRITE_ERROR("回放异常!");
  232. break;
  233. default:
  234. break;
  235. }
  236. }
  237. /**
  238. * @brief 实时预览回调函数,没有解码的数据流,这函数会运行在子线程中
  239. *
  240. * @param realHandle 操作句柄
  241. * @param dataType 数据类型
  242. * @param pBuffer 数据指针
  243. * @param bufSize 数据大小
  244. * @param user
  245. */
  246. void CameraPlayer::realDataCallBack(LONG realHandle, DWORD dataType, BYTE *pBuffer,DWORD bufSize,void* user)
  247. {
  248. // LH_WRITE_LOG_DEBUG(QString("realDataCallBack接收到了数据,数据大小为:%1").arg(bufSize));
  249. // LH_WRITE_LOG_DEBUG(QString("realDataCallBack所在的线程: %1").arg(QThread::currentThreadId()));
  250. // qDebug() << "realDataCallBack所在的线程:" << QThread::currentThreadId();
  251. /* 转换传入的指针,是这个类本身 */
  252. auto cameraPlayer = static_cast<CameraPlayer*>(user);
  253. LONG playPort = cameraPlayer->m_playPort;
  254. // PlayM4_GetPort(&realHandle);
  255. switch (dataType)
  256. {
  257. /* 系统头数据 */
  258. case NET_DVR_SYSHEAD:
  259. LH_WRITE_LOG_DEBUG("系统头数据");
  260. /* 获取播放库未使用的通道号,最多貌似可以有500个 */
  261. if(!PlayM4_GetPort(&playPort))
  262. {
  263. LH_WRITE_ERROR("获取播放库未使用的通道号失败");
  264. break;
  265. }
  266. /* 打开流 */
  267. if(bufSize > 0)
  268. {
  269. cameraPlayer->m_playPort = playPort;
  270. /* 最后一个参数是 设置播放器中存放数据流的缓冲区大小,不能太大也不能太小 */
  271. if(!PlayM4_OpenStream(playPort, pBuffer, bufSize, 1024 * 1024 * 10))
  272. {
  273. auto ret = PlayM4_GetLastError(playPort);
  274. LH_WRITE_ERROR(QString("打开流失败,错误代码: %1").arg(ret));
  275. break;
  276. }
  277. /* 设置解码回调函数 */
  278. #if defined (C_WIN32)
  279. bool ret2 = PlayM4_SetDecCallBackMend(playPort, DecCallBack, (long)cameraPlayer);
  280. #else
  281. bool ret2 = PlayM4_SetDecCallBackMend(playPort, DecCallBack, (void*)cameraPlayer);
  282. #endif
  283. if(!ret2)
  284. {
  285. auto ret3 = PlayM4_GetLastError(playPort);
  286. LH_WRITE_ERROR(QString("设置解码回调函数失败,错误代码: %1").arg(ret3));
  287. break;
  288. }
  289. /* 打开视频解码,不传入播放窗口 */
  290. if(!PlayM4_Play(playPort, 0))
  291. {
  292. auto ret4 = PlayM4_GetLastError(playPort);
  293. LH_WRITE_ERROR(QString("打开视频解码失败,错误代码: %1").arg(ret4));
  294. break;
  295. }
  296. /* 打开音频解码,需要码流是复合流 */
  297. if(!PlayM4_PlaySound(playPort))
  298. {
  299. auto ret5 = PlayM4_GetLastError(playPort);
  300. LH_WRITE_ERROR(QString("打开音频解码失败,错误代码: %1").arg(ret5));
  301. break;
  302. }
  303. }else {
  304. LH_WRITE_ERROR("系统头数据大小为0");
  305. }
  306. break;
  307. /* 视频流数据(包括复合流和音视频分开的视频流数据) */
  308. case NET_DVR_STREAMDATA:
  309. // LH_WRITE_LOG_DEBUG("音视频复合流数据");
  310. /* 解码数据 */
  311. if(bufSize > 0 && playPort != -1)
  312. {
  313. auto ret = PlayM4_InputData(playPort, pBuffer, bufSize);
  314. while(!ret)
  315. {
  316. auto errNum = PlayM4_GetLastError(playPort);
  317. if(errNum == PLAYM4_BUF_OVER)
  318. {
  319. LH_WRITE_ERROR("数据解码失败, 缓冲区满, 需要重新输入");
  320. /* 重新发送给解码器 */
  321. ret = PlayM4_InputData(playPort, pBuffer, bufSize);
  322. }else {
  323. LH_WRITE_ERROR(QString("数据解码失败, 其他错误: %1").arg(errNum));
  324. }
  325. // std::this_thread::sleep_for(std::chrono::milliseconds(1));
  326. }
  327. }
  328. break;
  329. /* 其他数据 */
  330. case NET_DVR_AUDIOSTREAMDATA:
  331. LH_WRITE_LOG_DEBUG("音频流数据");
  332. break;
  333. case NET_DVR_PRIVATE_DATA:
  334. LH_WRITE_LOG_DEBUG("私有数据");
  335. break;
  336. default:
  337. LH_WRITE_LOG_DEBUG("其他数据");
  338. break;
  339. }
  340. }
  341. /* 标准数据流的预览回调函数 */
  342. void CameraPlayer::realDataCallBackStandard(LONG realHandle, DWORD dataType, BYTE *pBuffer,DWORD bufSize,DWORD user)
  343. {
  344. LH_WRITE_LOG_DEBUG(QString("realDataCallBackStandard接收到了数据,数据大小为:%1").arg(bufSize));
  345. switch(dataType)
  346. {
  347. case NET_DVR_SYSHEAD:
  348. LH_WRITE_LOG("系统头数据");
  349. break;
  350. case NET_DVR_STREAMDATA:
  351. LH_WRITE_LOG("音视频流复合数据");
  352. break;
  353. case NET_DVR_STD_VIDEODATA:
  354. LH_WRITE_LOG("标准视频流数据");
  355. break;
  356. case NET_DVR_STD_AUDIODATA:
  357. LH_WRITE_LOG("标准音频流数据");
  358. break;
  359. default:
  360. LH_WRITE_LOG("其他数据");
  361. break;
  362. }
  363. }
  364. /**
  365. * @brief 解码回调函数,这个函数会运行在一个新的子线程中
  366. *
  367. * @param nPort 播放端口号
  368. * @param pBuf 需要解码的数据
  369. * @param nSize 数据大小
  370. * @param pFrameInfo 流格式
  371. * @param nUser 用户数据指针
  372. * @param nReserved2
  373. */
  374. #if defined (C_WIN32)
  375. void CameraPlayer::DecCallBack(long nPort, char * pBuf, long nSize,FRAME_INFO * pFrameInfo, long nUser, long nReserved2)
  376. #elif defined (C_WIN64)
  377. void CameraPlayer::DecCallBack(long nPort, char * pBuf, long nSize,FRAME_INFO * pFrameInfo, void* nUser, void* nReserved2)
  378. #elif defined(C_LINUX)
  379. void CameraPlayer::DecCallBack(int nPort, char * pBuf, int nSize,FRAME_INFO * pFrameInfo, void* nUser, int nReserved2)
  380. #endif
  381. {
  382. /* 获取当前时间 */
  383. // auto now = std::chrono::system_clock::now();
  384. auto player = static_cast<CameraPlayer*>((void*)nUser);
  385. // LH_WRITE_LOG(QString("解码回调函数,解码通道号: %1 %2").arg(nPort).arg(player->m_playPort));
  386. // qDebug() << "DecCallBack所在的线程:" << QThread::currentThreadId();
  387. /* 获取视频帧率,如果是音频,就是采样率 */
  388. int frameRate = pFrameInfo->nFrameRate;
  389. /* 视频数据是 T_YV12 音频数据是 T_AUDIO16 */
  390. if(pFrameInfo->nType == T_YV12)
  391. {
  392. /* 获取视频帧率 */
  393. if(player->m_frameRate == 0)
  394. {
  395. player->m_frameRate = frameRate;
  396. }
  397. int width = pFrameInfo->nWidth;
  398. int height = pFrameInfo->nHeight;
  399. player->m_imageSize = QSize(width, height);
  400. // LH_WRITE_LOG_DEBUG(QString("视频宽高: %1 x %2, 视频帧率: %3, 当前队列数目: %4").arg(width).arg(height).arg(frameRate).arg(player->m_imageQueue.QueueSize()));
  401. /* 截图标志位,保存图片 */
  402. /* 转换成yuv,保存到环形队列中 */
  403. Image_YUV420* image = new Image_YUV420();
  404. player->m_transCode->YV12ToYUV420((unsigned char*)pBuf, *image, width, height);
  405. // Image_QImage* image = new Image_QImage();
  406. // player->m_transCode->YV12ToQImage((unsigned char*)pBuf, *image, width, height);
  407. /* 判断环形队列是否满了,满了就出队一个 */
  408. if(player->m_imageQueue.isFull())
  409. {
  410. // LH_WRITE_LOG_DEBUG("图像环形队列满了,出队一个");
  411. auto one = player->m_imageQueue.front_pop();
  412. delete one;
  413. }
  414. player->m_imageQueue.push_NoBlock(image);
  415. }
  416. else if(pFrameInfo->nType == T_AUDIO16)
  417. {
  418. // LH_WRITE_LOG("音频数据");
  419. } else
  420. {
  421. LH_WRITE_LOG_DEBUG(QString("其他数据: %1").arg(pFrameInfo->nType));
  422. }
  423. /* 计算解码时间 */
  424. // auto lastTime = std::chrono::system_clock::now();
  425. // auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(lastTime - now);
  426. // LH_WRITE_LOG_DEBUG(QString("解码时间: %1 ms").arg(duration.count()));
  427. }
  428. /* 清空环形队列 */
  429. void CameraPlayer::clearRingQueue()
  430. {
  431. int num = m_imageQueue.QueueSize();
  432. for(int i = 0; i < num; i++)
  433. {
  434. auto one = m_imageQueue.front_pop_NoBlock();
  435. if(one != nullptr)
  436. {
  437. delete one;
  438. }
  439. }
  440. }