read_thread.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. extern int infinite_buffer;
  10. extern int64_t start_time;
  11. static int64_t duration = AV_NOPTS_VALUE;
  12. ReadThread::ReadThread(VideoState* pState)
  13. : m_pPlayData(pState)
  14. {
  15. }
  16. ReadThread::~ReadThread()
  17. {
  18. stop();
  19. join();
  20. }
  21. void ReadThread::set_video_state(VideoState* pState)
  22. {
  23. assert(pState);
  24. m_pPlayData = pState;
  25. }
  26. void ReadThread::stop()
  27. {
  28. m_exit = true;
  29. m_cv.notify_all();
  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. {
  44. av_log(nullptr, AV_LOG_FATAL, "Could not allocate packet.\n");
  45. ret = AVERROR(ENOMEM);
  46. return ret;
  47. }
  48. is->read_thread_exit = 0;
  49. for (;;)
  50. {
  51. if (m_exit)
  52. break;
  53. if (is->abort_request)
  54. break;
  55. if (is->paused != is->last_paused)
  56. {
  57. is->last_paused = is->paused;
  58. if (is->paused)
  59. is->read_pause_return = av_read_pause(is->ic);
  60. else
  61. av_read_play(is->ic);
  62. }
  63. if (is->seek_req)
  64. {
  65. int64_t seek_target = is->seek_pos;
  66. int64_t seek_min =
  67. is->seek_rel > 0 ? seek_target - is->seek_rel + 2 : INT64_MIN;
  68. int64_t seek_max =
  69. is->seek_rel < 0 ? seek_target - is->seek_rel - 2 : INT64_MAX;
  70. ret = avformat_seek_file(is->ic, -1, seek_min, seek_target, seek_max,
  71. is->seek_flags);
  72. if (ret < 0)
  73. {
  74. av_log(nullptr, AV_LOG_ERROR, "%s: error while seeking\n", is->ic->url);
  75. }
  76. else
  77. {
  78. if (is->audio_stream >= 0)
  79. packet_queue_flush(&is->audioq);
  80. if (is->subtitle_stream >= 0)
  81. packet_queue_flush(&is->subtitleq);
  82. if (is->video_stream >= 0)
  83. packet_queue_flush(&is->videoq);
  84. if (is->seek_flags & AVSEEK_FLAG_BYTE)
  85. {
  86. set_clock(&is->extclk, NAN, 0);
  87. }
  88. else
  89. {
  90. set_clock(&is->extclk, seek_target / (double)AV_TIME_BASE, 0);
  91. }
  92. }
  93. is->seek_req = 0;
  94. is->queue_attachments_req = 1;
  95. is->eof = 0;
  96. if (is->paused)
  97. step_to_next_frame(is);
  98. }
  99. if (is->queue_attachments_req)
  100. {
  101. if (is->video_st &&
  102. is->video_st->disposition & AV_DISPOSITION_ATTACHED_PIC)
  103. {
  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 >
  115. MAX_QUEUE_SIZE ||
  116. (stream_has_enough_packets(is->audio_st, is->audio_stream,
  117. &is->audioq) &&
  118. stream_has_enough_packets(is->video_st, is->video_stream,
  119. &is->videoq) &&
  120. stream_has_enough_packets(is->subtitle_st, is->subtitle_stream,
  121. &is->subtitleq))))
  122. {
  123. std::unique_lock<std::mutex> lock(m_mutex);
  124. m_cv.wait_for(lock, std::chrono::milliseconds(10), [this]{ return m_exit; });
  125. continue;
  126. }
  127. ret = av_read_frame(is->ic, pkt);
  128. if (ret < 0)
  129. {
  130. if ((ret == AVERROR_EOF || avio_feof(is->ic->pb)) && !is->eof)
  131. {
  132. if (is->video_stream >= 0)
  133. packet_queue_put_nullpacket(&is->videoq, pkt, is->video_stream);
  134. if (is->audio_stream >= 0)
  135. packet_queue_put_nullpacket(&is->audioq, pkt, is->audio_stream);
  136. if (is->subtitle_stream >= 0)
  137. packet_queue_put_nullpacket(&is->subtitleq, pkt, is->subtitle_stream);
  138. if (is->loop)
  139. {
  140. stream_seek(is, 0, 0, 0);
  141. }
  142. else
  143. {
  144. is->eof = 1;
  145. break; // added for auto exit read thread
  146. }
  147. }
  148. if (is->ic->pb && is->ic->pb->error)
  149. {
  150. break;
  151. }
  152. std::unique_lock<std::mutex> lock(m_mutex);
  153. m_cv.wait_for(lock, std::chrono::milliseconds(10), [this]{ return m_exit; });
  154. continue;
  155. }
  156. else
  157. {
  158. is->eof = 0;
  159. }
  160. /* check if packet is in play range specified by user, then queue, otherwise
  161. * discard */
  162. stream_start_time = is->ic->streams[pkt->stream_index]->start_time;
  163. pkt_ts = pkt->pts == AV_NOPTS_VALUE ? pkt->dts : pkt->pts;
  164. pkt_in_play_range =
  165. duration == AV_NOPTS_VALUE ||
  166. (pkt_ts -
  167. (stream_start_time != AV_NOPTS_VALUE ? stream_start_time : 0)) *
  168. av_q2d(is->ic->streams[pkt->stream_index]->time_base) -
  169. (double)(start_time != AV_NOPTS_VALUE ? start_time : 0) /
  170. 1000000 <=
  171. ((double)duration / 1000000);
  172. if (pkt->stream_index == is->audio_stream && pkt_in_play_range)
  173. {
  174. packet_queue_put(&is->audioq, pkt);
  175. }
  176. else if (pkt->stream_index == is->video_stream && pkt_in_play_range &&
  177. !(is->video_st->disposition & AV_DISPOSITION_ATTACHED_PIC))
  178. {
  179. packet_queue_put(&is->videoq, pkt);
  180. }
  181. else if (pkt->stream_index == is->subtitle_stream && pkt_in_play_range)
  182. {
  183. packet_queue_put(&is->subtitleq, pkt);
  184. }
  185. else
  186. {
  187. av_packet_unref(pkt);
  188. }
  189. }
  190. is->read_thread_exit = -1;
  191. av_packet_free(&pkt);
  192. return 0;
  193. }
  194. void ReadThread::run()
  195. {
  196. int ret = loop_read();
  197. // 可加日志输出
  198. }