read_thread.cpp 9.5 KB

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