demux.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. #include "demux.h"
  2. #include "packet.h"
  3. #ifndef RVC_MAX_BUFFER_LEN
  4. #define RVC_MAX_BUFFER_LEN 1024
  5. #endif
  6. static int decode_interrupt_cb(void *ctx)
  7. {
  8. player_stat_t *is = (player_stat_t*)ctx;
  9. //return (int)(is->bvideo_finished && is->baudio_finished);
  10. return (int)(is->buser_stop);
  11. }
  12. static int demux_init(player_stat_t *is)
  13. {
  14. int ret = -1;
  15. for (int index = 0; index < is->m_uFilesCount; index++)
  16. {
  17. AVFormatContext* p_fmt_ctx = NULL;
  18. int err = -1;
  19. int a_idx = -1;
  20. int v_idx = -1;
  21. p_fmt_ctx = avformat_alloc_context();
  22. if (!p_fmt_ctx){
  23. is->rvc_hostapi->Debug(MEDIA_LOG_ERROR, "Could not allocate context.");
  24. ret = AVERROR(ENOMEM);
  25. break;
  26. }
  27. // 中断回调机制。为底层I/O层提供一个处理接口,比如中止IO操作。
  28. p_fmt_ctx->interrupt_callback.callback = decode_interrupt_cb;
  29. p_fmt_ctx->interrupt_callback.opaque = is;
  30. // 1. 构建AVFormatContext
  31. // 1.1 打开视频文件:读取文件头,将文件格式信息存储在"fmt context"中
  32. err = avformat_open_input(&p_fmt_ctx, is->m_strPlayLists[index], NULL, NULL);
  33. if (err < 0){
  34. char buffer[RVC_MAX_BUFFER_LEN] = { 0 };
  35. av_strerror(err, buffer, RVC_MAX_BUFFER_LEN);
  36. is->rvc_hostapi->Debug(MEDIA_LOG_ERROR, "avformat_open_input file %s failed %d(%s).", is->m_strPlayLists[index], err, buffer);
  37. avformat_close_input(&p_fmt_ctx);
  38. avformat_free_context(p_fmt_ctx);
  39. ret = -1;
  40. break;
  41. }
  42. is->m_pfmt_ctx[index] = p_fmt_ctx;
  43. is->rvc_hostapi->Debug(MEDIA_LOG_DEBUG, "is->p_fmt_ctx[%d] = 0x%08x, p_fmt_ctx = 0x%08x", index, is->m_pfmt_ctx[index], p_fmt_ctx);
  44. // 1.2 搜索流信息:读取一段视频文件数据,尝试解码,将取到的流信息填入p_fmt_ctx->streams
  45. // ic->streams是一个指针数组,数组大小是pFormatCtx->nb_streams
  46. err = avformat_find_stream_info(p_fmt_ctx, NULL);
  47. if (err < 0){
  48. char strbuffer[RVC_MAX_BUFFER_LEN] = { 0 };
  49. av_strerror(err, strbuffer, RVC_MAX_BUFFER_LEN);
  50. is->rvc_hostapi->Debug(MEDIA_LOG_ERROR, "%s avformat_find_stream_info() failed %d(%s).", is->m_strPlayLists[index], err, strbuffer);
  51. avformat_close_input(&p_fmt_ctx);
  52. avformat_free_context(p_fmt_ctx);
  53. ret = -1;
  54. break;
  55. }
  56. // 2. 查找第一个音频流/视频流
  57. for (int i = 0; i < (int)p_fmt_ctx->nb_streams; i++){
  58. if ((p_fmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) && (a_idx == -1)){
  59. a_idx = i;
  60. is->rvc_hostapi->Debug(MEDIA_LOG_DEBUG, "%s Find a audio stream, index %d, media type is %s.", is->m_strPlayLists[index], a_idx, Media_Type_Table[is->m_eMType]);
  61. }
  62. if ((p_fmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) && (v_idx == -1)){
  63. v_idx = i;
  64. is->rvc_hostapi->Debug(MEDIA_LOG_DEBUG, "%s Find a video stream, index %d, media type is %s.", is->m_strPlayLists[index], v_idx, Media_Type_Table[is->m_eMType]);
  65. }
  66. if (a_idx != -1 && v_idx != -1){
  67. break;
  68. }
  69. }
  70. if ((a_idx == -1 && v_idx == -1) || (eVideo_Type == is->m_eMType && v_idx == -1)){
  71. is->rvc_hostapi->Debug(MEDIA_LOG_ERROR, "Invalid file %s can not find any audio/video stream.", is->m_strPlayLists[index]);
  72. ret = -1;
  73. if (NULL != p_fmt_ctx){
  74. avformat_close_input(&p_fmt_ctx);
  75. avformat_free_context(p_fmt_ctx);
  76. is->m_pfmt_ctx[index] = NULL;
  77. }
  78. break;
  79. }
  80. is->audio_idx[index] = a_idx;
  81. is->video_idx[index] = v_idx;
  82. is->m_paudio_stream[index] = p_fmt_ctx->streams[a_idx];
  83. is->m_pvideo_stream[index] = p_fmt_ctx->streams[v_idx];
  84. ret = 0;
  85. }
  86. return ret;
  87. }
  88. int demux_deinit()
  89. {
  90. return 0;
  91. }
  92. static int stream_has_enough_packets(AVStream *st, int stream_id, packet_queue_t *queue)
  93. {
  94. return stream_id < 0 ||
  95. queue->abort_flag ||
  96. (st->disposition & AV_DISPOSITION_ATTACHED_PIC) ||
  97. queue->nb_packets > MIN_FRAMES && (!queue->duration || av_q2d(st->time_base) * queue->duration > 1.0);
  98. }
  99. /* this thread gets the stream from the disk or the network */
  100. static int demux_thread(void *arg)
  101. {
  102. player_stat_t *is = (player_stat_t *)arg;
  103. int ret = 0;
  104. AVPacket pkt1, *pkt = &pkt1;
  105. SDL_mutex *wait_mutex = SDL_CreateMutex();
  106. is->rvc_hostapi->Debug(MEDIA_LOG_DEBUG, "begin %s av_read_frame, nFilecount is %d, current index is %d.", is->m_strPlayLists[is->m_icurrent_index], is->m_uFilesCount, is->m_icurrent_index);
  107. // 4. 解复用处理
  108. while (false == is->buser_stop)
  109. {
  110. /* if the queue are full, no need to read more */
  111. if (is->audio_pkt_queue.packet_queue_size + is->video_pkt_queue.packet_queue_size > MAX_QUEUE_SIZE ||
  112. (stream_has_enough_packets(is->m_paudio_stream[is->m_icurrent_index], is->audio_idx[is->m_icurrent_index], &is->audio_pkt_queue) &&
  113. stream_has_enough_packets(is->m_pvideo_stream[is->m_icurrent_index], is->video_idx[is->m_icurrent_index], &is->video_pkt_queue)))
  114. {
  115. /* wait 10 ms */
  116. SDL_LockMutex(wait_mutex);
  117. SDL_CondWaitTimeout(is->m_continue_read_thread, wait_mutex, 10);
  118. SDL_UnlockMutex(wait_mutex);
  119. continue;
  120. }
  121. // 4.1 从输入文件中读取一个packet
  122. ret = av_read_frame(is->m_pfmt_ctx[is->m_icurrent_index], pkt);
  123. if (ret < 0)
  124. {
  125. if (AVERROR_EOF == ret)
  126. {
  127. // 输入文件已读完,则往packet队列中发送NULL packet,以冲洗(flush)解码器,否则解码器中缓存的帧取不出来
  128. if (is->video_idx[is->m_icurrent_index] >= 0){
  129. packet_queue_put_nullpacket(&is->video_pkt_queue, is->video_idx[is->m_icurrent_index], is->rvc_hostapi);
  130. }
  131. if (is->audio_idx[is->m_icurrent_index] >= 0){
  132. packet_queue_put_nullpacket(&is->audio_pkt_queue, is->audio_idx[is->m_icurrent_index], is->rvc_hostapi);
  133. }
  134. if (is->m_icurrent_index + 1 < is->m_uFilesCount){
  135. is->m_icurrent_index++;
  136. ret = 0;
  137. is->rvc_hostapi->Debug(MEDIA_LOG_DEBUG, "begin %s av_read_frame, nFilecount is %d, current is->index is %d.", is->m_strPlayLists[is->m_icurrent_index], is->m_uFilesCount, is->m_icurrent_index);
  138. }
  139. else {
  140. is->rvc_hostapi->Debug(MEDIA_LOG_DEBUG, "av_read_frame ret is AVERROR_EOF.");
  141. break;
  142. }
  143. }
  144. SDL_LockMutex(wait_mutex);
  145. SDL_CondWaitTimeout(is->m_continue_read_thread, wait_mutex, 10);
  146. SDL_UnlockMutex(wait_mutex);
  147. continue;
  148. }
  149. // 4.3 根据当前packet类型(音频、视频、字幕),将其存入对应的packet队列
  150. if (pkt->stream_index == is->audio_idx[is->m_icurrent_index]){
  151. packet_queue_put(&is->audio_pkt_queue, pkt, is->rvc_hostapi);
  152. }
  153. else if (pkt->stream_index == is->video_idx[is->m_icurrent_index]){
  154. packet_queue_put(&is->video_pkt_queue, pkt, is->rvc_hostapi);
  155. }
  156. else{
  157. av_packet_unref(pkt);
  158. }
  159. }
  160. SDL_DestroyMutex(wait_mutex);
  161. is->rvc_hostapi->Debug(MEDIA_LOG_DEBUG, "%s exit, thread id is %u, buser_stop flag is %s.",SDL_GetThreadName(is->m_read_tid), SDL_ThreadID(), is->buser_stop ? "true" : "false");
  162. return 0;
  163. }
  164. int open_demux(player_stat_t *is)
  165. {
  166. if (demux_init(is) != 0){
  167. is->rvc_hostapi->Debug(MEDIA_LOG_ERROR, "demux_init() failed.");
  168. return -1;
  169. }
  170. is->m_read_tid = SDL_CreateThread(demux_thread, "demux_thread", is);
  171. if (is->m_read_tid == NULL){
  172. is->rvc_hostapi->Debug(MEDIA_LOG_ERROR, "SDL_CreateThread() failed: %s.", SDL_GetError());
  173. return -1;
  174. }
  175. else {
  176. is->rvc_hostapi->Debug(MEDIA_LOG_DEBUG, "create %s success, and thread id is %u.", SDL_GetThreadName(is->m_read_tid), SDL_GetThreadID(is->m_read_tid));
  177. }
  178. return 0;
  179. }