read_thread.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // ***********************************************************/
  2. // read_thread.cpp
  3. //
  4. // Copy Right @ Steven Huang. All rights reserved.
  5. //
  6. // read packets thread.
  7. // ***********************************************************/
  8. #include "read_thread.h"
  9. #include "AVPlayer2/playercontroller.h"
  10. #include <QLoggingCategory>
  11. Q_LOGGING_CATEGORY(playerControllerReadThread, "player.controller.ReadThread")
  12. extern int infinite_buffer;
  13. extern int64_t start_time;
  14. static int64_t duration = AV_NOPTS_VALUE;
  15. ReadThread::ReadThread(VideoState* pState)
  16. : m_pPlayData(pState)
  17. {
  18. qCDebug(playerControllerReadThread) << "[ReadThread] constructed, pState:" << (void*) pState;
  19. }
  20. ReadThread::~ReadThread()
  21. {
  22. qCDebug(playerControllerReadThread) << "[ReadThread] destructed.";
  23. stop();
  24. join();
  25. }
  26. void ReadThread::set_video_state(VideoState* pState)
  27. {
  28. assert(pState);
  29. m_pPlayData = pState;
  30. }
  31. int ReadThread::loop_read()
  32. {
  33. int ret = -1;
  34. VideoState* is = m_pPlayData;
  35. AVPacket* pkt = nullptr;
  36. int pkt_in_play_range = 0;
  37. int64_t stream_start_time = 0;
  38. int64_t pkt_ts = 0;
  39. if (!is)
  40. return ret;
  41. pkt = av_packet_alloc();
  42. if (!pkt) {
  43. av_log(nullptr, AV_LOG_FATAL, "Could not allocate packet.\n");
  44. ret = AVERROR(ENOMEM);
  45. qCWarning(playerControllerReadThread) << "[ReadThread] av_packet_alloc failed!";
  46. return ret;
  47. }
  48. qCDebug(playerControllerReadThread) << "[ReadThread] loop_read start, file:"
  49. << (is && is->ic ? is->ic->url : "null");
  50. is->read_thread_exit = 0;
  51. for (;;) {
  52. if (isExit()) {
  53. qCDebug(playerControllerReadThread) << "[ReadThread] m_exit set, break.";
  54. break;
  55. }
  56. if (is->abort_request) {
  57. qCDebug(playerControllerReadThread) << "[ReadThread] abort_request set, break.";
  58. break;
  59. }
  60. if (is->paused != is->last_paused) {
  61. is->last_paused = is->paused;
  62. if (is->paused)
  63. is->read_pause_return = av_read_pause(is->ic);
  64. else
  65. av_read_play(is->ic);
  66. }
  67. if (is->seek_req) {
  68. qCDebug(playerControllerReadThread)
  69. << "[ReadThread] seek_req, seek_pos:" << is->seek_pos
  70. << ", seek_rel:" << is->seek_rel << ", seek_flags:" << is->seek_flags;
  71. int64_t seek_target = is->seek_pos;
  72. int64_t seek_min = is->seek_rel > 0 ? seek_target - is->seek_rel + 2 : INT64_MIN;
  73. int64_t seek_max = is->seek_rel < 0 ? seek_target - is->seek_rel - 2 : INT64_MAX;
  74. ret = avformat_seek_file(is->ic, -1, seek_min, seek_target, seek_max, is->seek_flags);
  75. if (ret < 0) {
  76. av_log(nullptr, AV_LOG_ERROR, "%s: error while seeking\n", is->ic->url);
  77. qCWarning(playerControllerReadThread)
  78. << "[ReadThread] avformat_seek_file failed, ret:" << ret;
  79. } else {
  80. qCDebug(playerControllerReadThread)
  81. << "[ReadThread] avformat_seek_file success, flush queues.";
  82. if (is->audio_stream >= 0)
  83. packet_queue_flush(&is->audioq);
  84. if (is->subtitle_stream >= 0)
  85. packet_queue_flush(&is->subtitleq);
  86. if (is->video_stream >= 0)
  87. packet_queue_flush(&is->videoq);
  88. if (is->seek_flags & AVSEEK_FLAG_BYTE) {
  89. set_clock(&is->extclk, NAN, 0);
  90. } else {
  91. set_clock(&is->extclk, seek_target / (double) AV_TIME_BASE, 0);
  92. }
  93. }
  94. is->seek_req = 0;
  95. is->queue_attachments_req = 1;
  96. is->eof = 0;
  97. if (is->paused)
  98. step_to_next_frame(is);
  99. }
  100. if (is->queue_attachments_req) {
  101. qCDebug(playerControllerReadThread)
  102. << "[ReadThread] queue_attachments_req, video_st:" << (void*) is->video_st;
  103. if (is->video_st && is->video_st->disposition & AV_DISPOSITION_ATTACHED_PIC) {
  104. ret = av_packet_ref(pkt, &is->video_st->attached_pic);
  105. if (ret < 0)
  106. break;
  107. packet_queue_put(&is->videoq, pkt);
  108. packet_queue_put_nullpacket(&is->videoq, pkt, is->video_stream);
  109. }
  110. is->queue_attachments_req = 0;
  111. }
  112. /* if the queue are full, no need to read more */
  113. if (infinite_buffer < 1
  114. && (is->audioq.size + is->videoq.size + is->subtitleq.size > MAX_QUEUE_SIZE
  115. || (stream_has_enough_packets(is->audio_st, is->audio_stream, &is->audioq)
  116. && stream_has_enough_packets(is->video_st, is->video_stream, &is->videoq)
  117. && stream_has_enough_packets(is->subtitle_st,
  118. is->subtitle_stream,
  119. &is->subtitleq)))) {
  120. qCDebug(playerControllerReadThread)
  121. << "[ReadThread] queues full, waiting... audioq:" << is->audioq.size
  122. << ", videoq:" << is->videoq.size << ", subtitleq:" << is->subtitleq.size;
  123. std::unique_lock<std::mutex> lock(m_mutex);
  124. m_cv.wait_for(lock, std::chrono::milliseconds(10), [this, is] { return isExit() || is->abort_request; });
  125. continue;
  126. }
  127. ret = av_read_frame(is->ic, pkt);
  128. if (ret < 0) {
  129. char buf[256] = {0};
  130. av_strerror(ret, buf, 256);
  131. qCWarning(playerControllerReadThread)
  132. << "[ReadThread] av_read_frame failed, ret:" << ret << ", eof:" << is->eof
  133. << ", pb_error:" << (is->ic->pb ? is->ic->pb->error : 0)
  134. << "error:" << QString::fromUtf8(buf);
  135. if ((ret == AVERROR_EOF || avio_feof(is->ic->pb)) && !is->eof) {
  136. qCDebug(playerControllerReadThread) << "[ReadThread] EOF reached, send null packets.";
  137. if (is->video_stream >= 0)
  138. packet_queue_put_nullpacket(&is->videoq, pkt, is->video_stream);
  139. if (is->audio_stream >= 0)
  140. packet_queue_put_nullpacket(&is->audioq, pkt, is->audio_stream);
  141. if (is->subtitle_stream >= 0)
  142. packet_queue_put_nullpacket(&is->subtitleq, pkt, is->subtitle_stream);
  143. if (is->loop) {
  144. stream_seek(is, 0, 0, 0);
  145. } else {
  146. is->eof = 1;
  147. break; // added for auto exit read thread
  148. }
  149. }
  150. if (is->ic->pb && is->ic->pb->error) {
  151. qCWarning(playerControllerReadThread) << "[ReadThread] IO error detected.";
  152. break;
  153. }
  154. std::unique_lock<std::mutex> lock(m_mutex);
  155. m_cv.wait_for(lock, std::chrono::milliseconds(10), [this, is] { return isExit() || is->abort_request; });
  156. continue;
  157. } else {
  158. is->eof = 0;
  159. qCDebug(playerControllerReadThread)
  160. << "[ReadThread] av_read_frame success, stream_index:" << pkt->stream_index
  161. << ", pts:" << pkt->pts;
  162. }
  163. /* check if packet is in play range specified by user, then queue, otherwise discard */
  164. stream_start_time = is->ic->streams[pkt->stream_index]->start_time;
  165. pkt_ts = pkt->pts == AV_NOPTS_VALUE ? pkt->dts : pkt->pts;
  166. pkt_in_play_range = duration == AV_NOPTS_VALUE
  167. || (pkt_ts
  168. - (stream_start_time != AV_NOPTS_VALUE ? stream_start_time : 0))
  169. * av_q2d(is->ic->streams[pkt->stream_index]->time_base)
  170. - (double) (start_time != AV_NOPTS_VALUE ? start_time : 0)
  171. / 1000000
  172. <= ((double) duration / 1000000);
  173. if (pkt->stream_index == is->audio_stream && pkt_in_play_range) {
  174. qCDebug(playerControllerReadThread) << "[ReadThread] put audio packet, pts:" << pkt->pts;
  175. packet_queue_put(&is->audioq, pkt);
  176. } else if (pkt->stream_index == is->video_stream && pkt_in_play_range
  177. && !(is->video_st->disposition & AV_DISPOSITION_ATTACHED_PIC)) {
  178. qCDebug(playerControllerReadThread) << "[ReadThread] put video packet, pts:" << pkt->pts;
  179. packet_queue_put(&is->videoq, pkt);
  180. } else if (pkt->stream_index == is->subtitle_stream && pkt_in_play_range) {
  181. qCDebug(playerControllerReadThread) << "[ReadThread] put subtitle packet, pts:" << pkt->pts;
  182. packet_queue_put(&is->subtitleq, pkt);
  183. } else {
  184. qCDebug(playerControllerReadThread)
  185. << "[ReadThread] drop packet, stream_index:" << pkt->stream_index;
  186. av_packet_unref(pkt);
  187. }
  188. }
  189. qCWarning(playerControllerReadThread)
  190. << "[ReadThread] loop_read about to exit, eof:" << is->eof
  191. << ", audioq.size:" << is->audioq.size << ", videoq.size:" << is->videoq.size
  192. << ", subtitleq.size:" << is->subtitleq.size << ", abort_request:" << is->abort_request
  193. << ", m_exit:" << (m_exit ? m_exit->load() : -1);
  194. qCDebug(playerControllerReadThread) << "[ReadThread] loop_read exit, set read_thread_exit = -1";
  195. is->read_thread_exit = -1;
  196. av_packet_free(&pkt);
  197. return 0;
  198. }
  199. void ReadThread::run()
  200. {
  201. qCDebug(playerControllerReadThread) << "[ReadThread] run start";
  202. int ret = loop_read();
  203. qCDebug(playerControllerReadThread) << "[ReadThread] run end, loop_read ret:" << ret;
  204. // 可加日志输出
  205. }