read_thread.cpp 9.3 KB

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