DecodeVedio.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680
  1. #include "DecodeVedio.h"
  2. #include "spdlog/spdlog.h"
  3. #include <QImage>
  4. #include <QThread>
  5. extern "C"
  6. {
  7. #include <libavcodec/avcodec.h>
  8. #include <libavformat/avformat.h>
  9. #include <libswscale/swscale.h>
  10. #include <libavutil/imgutils.h>
  11. }
  12. DecodeVedio::DecodeVedio(QThread* thread, QObject* parent) : QObject(parent) , m_thread(thread)
  13. {
  14. /* 在连接之前调用,移动到新的线程 */
  15. this->moveToThread(thread);
  16. m_startThread.setSingleShot(true);
  17. connect(&m_startThread, &QTimer::timeout, this, &DecodeVedio::do_startDecodeVedio);
  18. thread->start();
  19. }
  20. DecodeVedio::~DecodeVedio()
  21. {
  22. exitThread();
  23. if(m_thread != nullptr)
  24. {
  25. if(m_thread->isRunning())
  26. {
  27. m_thread->quit();
  28. m_thread->wait();
  29. }
  30. }
  31. if(m_initFFmpeg)
  32. {
  33. freeFFmpeg();
  34. }
  35. if(!m_queueImage.isEmpty())
  36. {
  37. int size = m_queueImage.count();
  38. for(int i = 0; i < size; i++)
  39. {
  40. auto image = m_queueImage.dequeue();
  41. if (image)
  42. {
  43. delete image;
  44. }
  45. }
  46. }
  47. }
  48. /* 开始解码视频 */
  49. void DecodeVedio::startDecodeVedio()
  50. {
  51. if(m_isRunning)
  52. {
  53. return;
  54. }
  55. if(!m_initFFmpeg)
  56. {
  57. SPDLOG_WARN("未初始化FFMPEG...");
  58. return;
  59. }
  60. /* 开启定时器,进入槽函数,就进入了新的线程 */
  61. m_startThread.start(0);
  62. }
  63. /* 停止解码视频,退出工作函数,线程未停止 */
  64. void DecodeVedio::stopDecodeVedio()
  65. {
  66. if(!m_isRunning)
  67. {
  68. return;
  69. }
  70. m_isRunning = false;
  71. m_pauseDecode = false;
  72. /* 唤醒阻塞住的解码线程 */
  73. m_condQueueNoFull.wakeAll();
  74. // /* 等待线程执行结束 */
  75. while(!m_threadStopped)
  76. {
  77. /* 睡眠10ms */
  78. std::this_thread::sleep_for(std::chrono::milliseconds(10));
  79. }
  80. }
  81. /**
  82. * @brief 设置当前播放位置,单位是微秒
  83. * 这里需要去掉队列中已有的图片数目对应的时长
  84. *
  85. * @param pos
  86. */
  87. void DecodeVedio::setCurrentPos(quint64 pos)
  88. {
  89. /* 暂停解码 */
  90. m_pauseDecode = true;
  91. // SPDLOG_DEBUG("setCurrentPos threadID:{}",QThread::currentThreadId());
  92. while (m_decodeStatus) {
  93. /* 需要唤醒阻塞住的线程 */
  94. m_condQueueNoFull.wakeAll();
  95. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  96. }
  97. m_isSeek = true;
  98. /* 这个是int64最大的正数值 */
  99. if(pos > 9223372036854775807)
  100. {
  101. pos = 0;
  102. }
  103. /* 去掉队列中已有的图片数目对应的时长,单位是us */
  104. qint64 queueNumFPS = m_queueImage.count() * ( 1000.0f / m_fps ) * 1000 ;
  105. qint64 target_pos = m_startPos + pos - queueNumFPS;
  106. qint64 end_pos = m_startPos + m_duration;
  107. /* 检查是否超过视频大小 */
  108. if(target_pos > end_pos)
  109. {
  110. /* 跳转到最后 */
  111. target_pos = end_pos;
  112. // SPDLOG_DEBUG("现在的位置:{} 时长:{} 跳转到最后30帧",target_pos, m_duration);
  113. }
  114. if(target_pos < m_startPos)
  115. {
  116. target_pos = m_startPos;
  117. // SPDLOG_DEBUG("现在的位置:{} 时长:{} 跳转到开始",target_pos, m_startPos);
  118. }
  119. m_currentPos = target_pos;
  120. SPDLOG_DEBUG("设置播放位置:{}",target_pos);
  121. /* 第二个参数设置为-1,表示所有流都跳转 */
  122. int ret = av_seek_frame(m_pFormatContext, -1, target_pos, AVSEEK_FLAG_BACKWARD);
  123. /* 清空缓存 */
  124. avcodec_flush_buffers(m_pCodecCtx);
  125. if(ret < 0)
  126. {
  127. SPDLOG_WARN("跳转失败...");
  128. }
  129. /* 清空队列 */
  130. m_mutexQueue.lock();
  131. int size = m_queueImage.count();
  132. for(int i = 0; i < size; i++)
  133. {
  134. auto image = m_queueImage.dequeue();
  135. if (image)
  136. {
  137. delete image;
  138. }
  139. }
  140. m_mutexQueue.unlock();
  141. m_condQueueNoFull.wakeAll();
  142. SPDLOG_INFO("跳转完成 , queue count {}", m_queueImage.count());
  143. /* 继续解码 */
  144. m_pauseDecode = false;
  145. // SPDLOG_DEBUG("继续解码... {}", m_pauseDecode.load());
  146. }
  147. /* 获取当前播放位置 */
  148. qint64 DecodeVedio::getCurrentPos()
  149. {
  150. return m_currentPos - m_startPos;
  151. }
  152. /* 获取视频时长 */
  153. qint64 DecodeVedio::getDuration()
  154. {
  155. return m_duration;
  156. }
  157. /* 唤醒队列不满条件变量 */
  158. void DecodeVedio::wakeUpCondQueueNoEmpty()
  159. {
  160. m_condQueueNoFull.wakeAll();
  161. }
  162. /**
  163. * @brief 获取一帧图像,队列为空就返回nullptr,这个函数应该是运行在UI线程中的
  164. * @warning 传出这个指针后,队列就出队了,内存需要外面获取的实例释放
  165. * @return QImage* 一帧图像的指针
  166. */
  167. QImage* DecodeVedio::getOneImage()
  168. {
  169. if(m_queueImage.count() == 0)
  170. {
  171. // SPDLOG_TRACE("队列为空...");
  172. return nullptr;
  173. }
  174. // SPDLOG_TRACE("******************************** 队列中图片个数:{} ",m_queueImage.count());
  175. m_mutexQueue.lock();
  176. auto image = m_queueImage.dequeue();
  177. m_mutexQueue.unlock();
  178. /* 唤醒可能阻塞住的解码线程,队列中的图片低于20之后再唤醒 */
  179. if(m_queueImage.count() < 20)
  180. {
  181. m_condQueueNoFull.wakeAll();
  182. }
  183. return image;
  184. }
  185. /* 获取一帧图像,直到有图像为止 */
  186. QImage* DecodeVedio::getOneImageUntilHave()
  187. {
  188. QImage* image = nullptr;
  189. if(m_queueImage.count() == 0)
  190. {
  191. m_mutexQueue.lock();
  192. m_condQueueNoEmpty.wait(&m_mutexQueue);
  193. image = m_queueImage.dequeue();
  194. m_mutexQueue.unlock();
  195. }
  196. return image;
  197. }
  198. /**
  199. * @brief 获取每秒的帧数
  200. *
  201. * @return int 正值表示帧数,-1表示未知,-2表示HEVC
  202. */
  203. int DecodeVedio::getFrameCount()
  204. {
  205. AVFormatContext *pFormatCtx = NULL;
  206. avformat_open_input(&pFormatCtx, m_fileName.toStdString().c_str(), NULL, NULL);
  207. avformat_find_stream_info(pFormatCtx, NULL);
  208. /* 找到视频流 */
  209. int videoStreamIndex = -1;
  210. for (int i = 0; i < pFormatCtx->nb_streams; i++)
  211. {
  212. if (pFormatCtx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
  213. videoStreamIndex = i;
  214. break;
  215. }
  216. }
  217. /* 获取视频格式是hevc还是h.264 */
  218. enum AVCodecID codecID = pFormatCtx->streams[videoStreamIndex]->codecpar->codec_id;
  219. if (codecID == AV_CODEC_ID_HEVC)
  220. {
  221. SPDLOG_DEBUG("视频格式是HEVC");
  222. m_fps = -2;
  223. // AVPacket packet;
  224. // int frameCount = 0;
  225. // double timeBase = av_q2d(pFormatCtx->streams[videoStreamIndex]->time_base);
  226. // double duration = 2.0; // Duration in seconds
  227. // while (av_read_frame(pFormatCtx, &packet) >= 0)
  228. // {
  229. // if (packet.stream_index == videoStreamIndex)
  230. // {
  231. // double packetTime = packet.pts * timeBase;
  232. // if (packetTime <= duration) {
  233. // frameCount++;
  234. // } else {
  235. // break;
  236. // }
  237. // }
  238. // av_packet_unref(&packet);
  239. // }
  240. // fps = frameCount / duration;
  241. }
  242. else if(codecID == AV_CODEC_ID_H264)
  243. {
  244. SPDLOG_DEBUG("视频格式是H.264");
  245. /* 获取到分子和分母,一除就是帧率 */
  246. AVRational frameRate = pFormatCtx->streams[videoStreamIndex]->avg_frame_rate;
  247. m_fps = frameRate.num*1.0 / frameRate.den;
  248. // SPDLOG_DEBUG("分子:{} 分母:{} 帧率:{}",frameRate.num, frameRate.den, fps);
  249. }else
  250. {
  251. m_fps = 30;
  252. }
  253. avformat_close_input(&pFormatCtx);
  254. return m_fps;
  255. }
  256. /**
  257. * @brief 设置图像宽度和高度
  258. * 注意:目前不好用,建议使用Qt的图片缩放
  259. *
  260. * @param width
  261. * @param height
  262. */
  263. void DecodeVedio::setVideoSize(int width, int height)
  264. {
  265. m_width = width;
  266. m_height = height;
  267. if(m_sws_ctx != nullptr)
  268. {
  269. /* 先暂停解码器 */
  270. pauseDecode();
  271. sws_freeContext(m_sws_ctx);
  272. }
  273. /* 初始化Sws Context,这是转换规则,转换成RGB */
  274. m_sws_ctx = sws_getContext( m_pCodecCtx->width, m_pCodecCtx->height, m_pCodecCtx->pix_fmt, /* 原图像大小和格式 */
  275. m_width, m_height, AV_PIX_FMT_RGB24, /* 目标图像的大小和格式 */
  276. SWS_BICUBIC,
  277. nullptr,
  278. nullptr,
  279. nullptr);
  280. }
  281. /* 初始化函数 */
  282. void DecodeVedio::initFFmpeg(const QString& fileName)
  283. {
  284. if(m_initFFmpeg)
  285. {
  286. return;
  287. }
  288. m_fileName = fileName;
  289. /* 清空队列 */
  290. int size = m_queueImage.count();
  291. for(int i = 0; i < size; i++)
  292. {
  293. auto image = m_queueImage.dequeue();
  294. if (image)
  295. {
  296. delete image;
  297. }
  298. }
  299. m_queueImage.clear();
  300. SPDLOG_DEBUG("开始初始化FFMPEG");
  301. /* 注册所有的解码器,从4.0开始就不需要这个初始化了 */
  302. // av_register_all();
  303. /* 存储文件格式信息 */
  304. /* 打开文件,读取视频文件的头信息,放在第一个参数的结构体中 */
  305. int ret = avformat_open_input(&m_pFormatContext, m_fileName.toStdString().c_str(), nullptr, nullptr);
  306. if(ret != 0)
  307. {
  308. SPDLOG_WARN("打开视频文件错误,错误代码:{}",ret);
  309. return;
  310. }
  311. ret = 0;
  312. /* 检查视频容器内部的流信息,将流存储到了pFormatContext->streams中
  313. * 这个会补充avformat_open_input()没有获取到的信息 */
  314. ret = avformat_find_stream_info(m_pFormatContext, nullptr);
  315. if(ret < 0)
  316. {
  317. SPDLOG_WARN("获取视频流错误,错误代码:{}",ret);
  318. return;
  319. }
  320. m_duration = m_pFormatContext->duration;
  321. m_currentPos = 0;
  322. m_startPos = m_pFormatContext->start_time;
  323. // SPDLOG_DEBUG("开始时间:{} 时长:{}",m_startPos, m_duration);
  324. /* 一个调试函数,将流信息输出到控制台 */
  325. av_dump_format(m_pFormatContext, 0, m_fileName.toStdString().c_str(), 0);
  326. /************ 找到视频流 ************/
  327. int i = 0;
  328. AVCodecParameters *pCodecCtxOrig = nullptr;
  329. for(i = 0;i < m_pFormatContext->nb_streams; i++)
  330. {
  331. if(m_pFormatContext->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)
  332. {
  333. m_videoStream = i;
  334. break;
  335. }
  336. }
  337. if(m_videoStream == -1)
  338. {
  339. SPDLOG_WARN("没有找到视频流");
  340. return;
  341. }
  342. SPDLOG_INFO("找到视频流");
  343. pCodecCtxOrig = m_pFormatContext->streams[m_videoStream]->codecpar;
  344. SPDLOG_INFO("获取视频流参数成功!");
  345. /* 使用编码器指向流 */
  346. AVCodec *pCodec = nullptr;
  347. /* 找到解码器 */
  348. pCodec = avcodec_find_decoder(pCodecCtxOrig->codec_id);
  349. if(pCodec == nullptr)
  350. {
  351. SPDLOG_WARN("没有找到解码器");
  352. return;
  353. }
  354. SPDLOG_TRACE("找到解码器");
  355. /* 复制上下文,先分配空间,后面记得释放空间 */
  356. m_pCodecCtx = avcodec_alloc_context3(pCodec);
  357. SPDLOG_TRACE("分配空间成功!");
  358. /* 将视频流中的编码器参数拷贝下来,这个函数不是线程安全的 */
  359. if(avcodec_parameters_to_context(m_pCodecCtx, pCodecCtxOrig) != 0)
  360. {
  361. SPDLOG_WARN("复制上下文错误");
  362. return;
  363. }
  364. SPDLOG_INFO("复制上下文成功!");
  365. if(avcodec_open2(m_pCodecCtx, pCodec, nullptr) < 0)
  366. {
  367. SPDLOG_ERROR("打开解码器错误");
  368. return;
  369. }
  370. SPDLOG_TRACE("打开编码器成功!");
  371. /******** 读取数据(解码数据) ********/
  372. m_packet = av_packet_alloc();
  373. // av_init_packet(m_packet);
  374. av_new_packet(m_packet, m_pCodecCtx->width * m_pCodecCtx->height);
  375. /********* 创建两个pFrame,一个存放原始数据,一个存放转换后的RGB数据 **********/
  376. m_pFrame = av_frame_alloc();
  377. if(m_pFrame == nullptr)
  378. {
  379. SPDLOG_ERROR("创建pFrame错误");
  380. return;
  381. }
  382. m_pFrameRGB = av_frame_alloc();
  383. if(m_pFrameRGB == nullptr)
  384. {
  385. SPDLOG_ERROR("创建pFrameRGB错误");
  386. return;
  387. }
  388. int numBytes = 0;
  389. /* 初始化pFrameRGB */
  390. numBytes = av_image_get_buffer_size(AV_PIX_FMT_RGB24, m_pCodecCtx->width, m_pCodecCtx->height, 1);
  391. m_buffer = (uint8_t *)av_malloc(numBytes * sizeof(uint8_t));
  392. /* 获取视频相关信息 */
  393. m_width = m_srcWidth = m_pCodecCtx->width;
  394. m_height = m_srcHeight = m_pCodecCtx->height;
  395. m_frameCount = m_pFormatContext->streams[m_videoStream]->nb_frames;
  396. /* 这个函数的实际作用是将buffer设置给pFrameRGB作为原始数据的内存区域 */
  397. av_image_fill_arrays(m_pFrameRGB->data, m_pFrameRGB->linesize, m_buffer, AV_PIX_FMT_RGB24, m_pCodecCtx->width, m_pCodecCtx->height, 1);
  398. /********** 创建一个SwsContext结构体,主要为了格式转化的时候使用 ***********/
  399. /* 初始化Sws Context,这是转换规则,转换成RGB */
  400. m_sws_ctx = sws_getContext( m_pCodecCtx->width, m_pCodecCtx->height, m_pCodecCtx->pix_fmt, /* 原图像大小和格式 */
  401. m_srcWidth, m_srcHeight, AV_PIX_FMT_RGB24, /* 目标图像的大小和格式 */
  402. SWS_BILINEAR, /* 双线性 */
  403. nullptr,
  404. nullptr,
  405. nullptr);
  406. m_initFFmpeg = true;
  407. SPDLOG_INFO("FFMPEG初始化完成!");
  408. /* 这里不能释放,否则会出问题 */
  409. // avcodec_parameters_free(&pCodecCtxOrig);
  410. }
  411. /* 取消ffmpeg初始化函数 */
  412. void DecodeVedio::unInitFFmpeg()
  413. {
  414. stopDecodeVedio();
  415. }
  416. /* 取消ffmpeg初始化函数 */
  417. void DecodeVedio::freeFFmpeg()
  418. {
  419. /************** 清理缓冲区,释放空间 *********************/
  420. av_free(m_buffer);
  421. m_buffer = nullptr;
  422. av_free(m_pFrameRGB);
  423. m_pFrameRGB = nullptr;
  424. /* 释放pFrame */
  425. av_free(m_pFrame);
  426. m_pFrame = nullptr;
  427. av_packet_free(&m_packet);
  428. // sws_freeContext(m_sws_ctx);
  429. /* 关闭解码器 */
  430. avcodec_close(m_pCodecCtx);
  431. /* 关闭文件 */
  432. avformat_close_input(&m_pFormatContext);
  433. QString m_fileName = QString();
  434. int m_videoStream = -1; /* 记录视频流是第几个流 */
  435. m_initFFmpeg = false;
  436. }
  437. /**
  438. * @brief 进入之后就会一直解码,完成一个帧就会发送新一个信号
  439. *
  440. */
  441. void DecodeVedio::decodeVedio()
  442. {
  443. int ret = 0;
  444. int retFrame = 0;
  445. int retPacket = 0;
  446. m_pauseDecode = false;
  447. m_decodeStatus = true;
  448. while(m_isRunning)
  449. {
  450. /* 暂停解码 */
  451. while(m_pauseDecode)
  452. {
  453. m_decodeStatus = false;
  454. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  455. }
  456. m_decodeStatus = true;
  457. // std::chrono::steady_clock::time_point t1 = std::chrono::steady_clock::now();
  458. /* 读取一帧压缩码流,如果使用多线程解码,就从这里分发数据流的包 */
  459. retPacket = av_read_frame(m_pFormatContext, m_packet);
  460. // std::chrono::steady_clock::time_point t2 = std::chrono::steady_clock::now();
  461. // SPDLOG_TRACE("======================================= 读取数据包耗时(us):{}",std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count());
  462. if(retPacket == AVERROR_EOF)
  463. {
  464. SPDLOG_INFO("读取到文件末尾...");
  465. emit signal_playCompleted();
  466. /* 暂停解码 */
  467. m_pauseDecode = true;
  468. while(m_pauseDecode)
  469. {
  470. m_decodeStatus = false;
  471. std::this_thread::sleep_for(std::chrono::milliseconds(5));
  472. }
  473. }
  474. else if(retPacket < 0)
  475. {
  476. SPDLOG_WARN("读取帧错误...");
  477. break;
  478. }
  479. /* 判断是否是视频帧 */
  480. if(m_packet->stream_index == m_videoStream)
  481. {
  482. /* 现在的位置,转换成时间,乘上AV_TIME_BASE后,单位是微秒 */
  483. auto tmpPos = m_packet->pts * av_q2d(m_pFormatContext->streams[m_videoStream]->time_base) * AV_TIME_BASE;
  484. /* 判断是否在跳转状态,和设置的位置相距较远就跳过 */
  485. if(m_isSeek)
  486. {
  487. if( std::abs(m_currentPos - tmpPos) > AV_TIME_BASE)
  488. {
  489. SPDLOG_DEBUG("currentPos:{}, tmpPos:{}",m_currentPos,tmpPos);
  490. av_packet_unref(m_packet);
  491. continue;
  492. }
  493. }
  494. m_isSeek = false;
  495. m_currentPos = tmpPos;
  496. // SPDLOG_DEBUG("======== 当前位置:{} ========",m_currentPos);
  497. // std::chrono::steady_clock::time_point t3 = std::chrono::steady_clock::now();
  498. /* 解码视频帧,现在使用新的API */
  499. ret = avcodec_send_packet(m_pCodecCtx, m_packet);
  500. // std::chrono::steady_clock::time_point t4 = std::chrono::steady_clock::now();
  501. if(ret < 0)
  502. {
  503. SPDLOG_ERROR("发送数据包错误...");
  504. continue;
  505. }
  506. // SPDLOG_TRACE("======================================= 发送数据包耗时:{}",std::chrono::duration_cast<std::chrono::milliseconds>(t4 - t3).count());
  507. while (m_isRunning)
  508. {
  509. /* 从解码器接收解码后的帧的函数。*/
  510. retFrame = avcodec_receive_frame(m_pCodecCtx, m_pFrame);
  511. std::chrono::steady_clock::time_point t5 = std::chrono::steady_clock::now();
  512. // SPDLOG_TRACE("======================================= 接收帧耗时:{}",std::chrono::duration_cast<std::chrono::milliseconds>(t5 - t4).count());
  513. if (retFrame == 0)
  514. {
  515. /* 成功接收到一帧,处理解码后的帧 */
  516. // std::chrono::steady_clock::time_point t6 = std::chrono::steady_clock::now();
  517. /* 将帧转换为RGB */
  518. sws_scale(m_sws_ctx, (uint8_t const * const *)m_pFrame->data, m_pFrame->linesize, 0, m_pCodecCtx->height,
  519. m_pFrameRGB->data, m_pFrameRGB->linesize);
  520. // std::chrono::steady_clock::time_point t7 = std::chrono::steady_clock::now();
  521. // SPDLOG_TRACE("======================================= 转换RGB耗时:{}",std::chrono::duration_cast<std::chrono::milliseconds>(t7 - t6).count());
  522. /* 一帧图像入队 */
  523. auto image = new QImage(m_pFrameRGB->data[0], m_width, m_height, QImage::Format_RGB888);
  524. // SPDLOG_DEBUG("队列中图片个数:{} ",m_queueImage.count());
  525. m_mutexQueue.lock();
  526. if(m_queueImage.count() >= m_queueMaxNum)
  527. {
  528. // SPDLOG_TRACE("队列满了...");
  529. m_condQueueNoFull.wait(&m_mutexQueue);
  530. }
  531. m_queueImage.enqueue(image);
  532. m_mutexQueue.unlock();
  533. /* 队列有数据,唤醒阻塞函数 */
  534. m_condQueueNoEmpty.wakeAll();
  535. /* 同时发送信号 */
  536. emit signal_oneImage();
  537. retFrame = -1;
  538. }
  539. else if(retFrame < 0)
  540. {
  541. if( retFrame == AVERROR(EAGAIN) )
  542. {
  543. // SPDLOG_TRACE("在此状态下输出不可用-用户必须尝试发送新的输入...");
  544. }
  545. else if(retFrame == AVERROR_EOF)
  546. {
  547. // SPDLOG_TRACE("读取到文件末尾...");
  548. }else
  549. {
  550. // SPDLOG_TRACE("其他解码错误...{}",retFrame);
  551. }
  552. av_frame_unref(m_pFrame);
  553. break;
  554. }
  555. else
  556. {
  557. SPDLOG_TRACE("其他错误...{}",retFrame);
  558. }
  559. av_frame_unref(m_pFrame);
  560. }
  561. }else
  562. {
  563. // SPDLOG_TRACE("不是视频帧...");
  564. }
  565. // av_free_packet(AVPacket *pkt)
  566. av_packet_unref(m_packet);
  567. }
  568. freeFFmpeg();
  569. /* 清空队列 */
  570. m_mutexQueue.lock();
  571. int count = m_queueImage.count();
  572. for (int i = 0; i < count; i++)
  573. {
  574. auto image = m_queueImage.dequeue();
  575. if (image)
  576. {
  577. delete image;
  578. image = nullptr;
  579. }
  580. }
  581. m_mutexQueue.unlock();
  582. /* 线程已停止 */
  583. m_threadStopped = true;
  584. }
  585. /* 退出线程,将所有可能暂停线程运行的条件全部唤醒 */
  586. void DecodeVedio::exitThread()
  587. {
  588. if(m_isRunning)
  589. {
  590. m_isRunning = false;
  591. }
  592. m_pauseDecode = false;
  593. m_condQueueNoFull.wakeAll();
  594. }
  595. /* 暂停解码,会阻塞到线程暂停为止 */
  596. void DecodeVedio::pauseDecode()
  597. {
  598. m_pauseDecode = true;
  599. while (m_decodeStatus) {
  600. /* 需要唤醒阻塞住的线程 */
  601. m_condQueueNoFull.wakeAll();
  602. std::this_thread::sleep_for(std::chrono::milliseconds(1));
  603. }
  604. }
  605. /* 开启解码 */
  606. void DecodeVedio::do_startDecodeVedio()
  607. {
  608. SPDLOG_DEBUG("线程ID:{}",QThread::currentThreadId());
  609. // if(!m_initFFmpeg)
  610. // {
  611. // initFFmpeg();
  612. // }
  613. m_isRunning = true;
  614. m_threadStopped = false;
  615. m_pauseDecode = false;
  616. /* 进入解码,直到播放完成或者手动退出 */
  617. decodeVedio();
  618. SPDLOG_TRACE("解码线程退出。");
  619. }