demux.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. // 1.2 搜索流信息:读取一段视频文件数据,尝试解码,将取到的流信息填入p_fmt_ctx->streams
  44. // ic->streams是一个指针数组,数组大小是pFormatCtx->nb_streams
  45. err = avformat_find_stream_info(p_fmt_ctx, NULL);
  46. if (err < 0){
  47. char strbuffer[RVC_MAX_BUFFER_LEN] = { 0 };
  48. av_strerror(err, strbuffer, RVC_MAX_BUFFER_LEN);
  49. is->rvc_hostapi->Debug(MEDIA_LOG_ERROR, "%s avformat_find_stream_info() failed %d(%s).", is->m_strPlayLists[index], err, strbuffer);
  50. avformat_close_input(&p_fmt_ctx);
  51. avformat_free_context(p_fmt_ctx);
  52. ret = -1;
  53. break;
  54. }
  55. // 2. 查找第一个音频流/视频流
  56. for (int i = 0; i < (int)p_fmt_ctx->nb_streams; i++){
  57. if ((p_fmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) && (a_idx == -1)){
  58. a_idx = i;
  59. }
  60. if ((p_fmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) && (v_idx == -1)){
  61. v_idx = i;
  62. }
  63. if (a_idx != -1 && v_idx != -1){
  64. break;
  65. }
  66. }
  67. if ((a_idx == -1 && v_idx == -1) || (eVideo_Type == is->m_eMType && v_idx == -1)){
  68. is->rvc_hostapi->Debug(MEDIA_LOG_ERROR, "Invalid file %s can not find any audio/video stream.", is->m_strPlayLists[index]);
  69. ret = -1;
  70. if (NULL != p_fmt_ctx){
  71. avformat_close_input(&p_fmt_ctx);
  72. avformat_free_context(p_fmt_ctx);
  73. is->m_pfmt_ctx[index] = NULL;
  74. }
  75. break;
  76. }
  77. is->audio_idx[index] = a_idx;
  78. is->video_idx[index] = v_idx;
  79. is->m_paudio_stream[index] = p_fmt_ctx->streams[a_idx];
  80. is->m_pvideo_stream[index] = p_fmt_ctx->streams[v_idx];
  81. ret = 0;
  82. }
  83. return ret;
  84. }
  85. int demux_deinit()
  86. {
  87. return 0;
  88. }
  89. static int stream_has_enough_packets(AVStream *st, int stream_id, packet_queue_t *queue)
  90. {
  91. return stream_id < 0 ||
  92. queue->abort_flag ||
  93. (st->disposition & AV_DISPOSITION_ATTACHED_PIC) ||
  94. queue->nb_packets > MIN_FRAMES && (!queue->duration || av_q2d(st->time_base) * queue->duration > 1.0);
  95. }
  96. /* this thread gets the stream from the disk or the network */
  97. static int demux_thread(void *arg)
  98. {
  99. player_stat_t *is = (player_stat_t *)arg;
  100. int ret = 0;
  101. AVPacket pkt1, *pkt = &pkt1;
  102. SDL_mutex *wait_mutex = SDL_CreateMutex();
  103. // 4. 解复用处理
  104. while (false == is->buser_stop)
  105. {
  106. /* if the queue are full, no need to read more */
  107. if (is->audio_pkt_queue.packet_queue_size + is->video_pkt_queue.packet_queue_size > MAX_QUEUE_SIZE ||
  108. (stream_has_enough_packets(is->m_paudio_stream[is->m_icurrent_index], is->audio_idx[is->m_icurrent_index], &is->audio_pkt_queue) &&
  109. stream_has_enough_packets(is->m_pvideo_stream[is->m_icurrent_index], is->video_idx[is->m_icurrent_index], &is->video_pkt_queue)))
  110. {
  111. /* wait 10 ms */
  112. SDL_LockMutex(wait_mutex);
  113. SDL_CondWaitTimeout(is->m_continue_read_thread, wait_mutex, 10);
  114. SDL_UnlockMutex(wait_mutex);
  115. continue;
  116. }
  117. // 4.1 从输入文件中读取一个packet
  118. ret = av_read_frame(is->m_pfmt_ctx[is->m_icurrent_index], pkt);
  119. if (ret < 0)
  120. {
  121. if (AVERROR_EOF == ret)
  122. {
  123. // 输入文件已读完,则往packet队列中发送NULL packet,以冲洗(flush)解码器,否则解码器中缓存的帧取不出来
  124. if (is->video_idx[is->m_icurrent_index] >= 0){
  125. packet_queue_put_nullpacket(&is->video_pkt_queue, is->video_idx[is->m_icurrent_index], is->rvc_hostapi);
  126. }
  127. if (is->audio_idx[is->m_icurrent_index] >= 0){
  128. packet_queue_put_nullpacket(&is->audio_pkt_queue, is->audio_idx[is->m_icurrent_index], is->rvc_hostapi);
  129. }
  130. if (is->m_icurrent_index + 1 < is->m_uFilesCount){
  131. is->m_icurrent_index++;
  132. ret = 0;
  133. }
  134. else {
  135. break;
  136. }
  137. }
  138. SDL_LockMutex(wait_mutex);
  139. SDL_CondWaitTimeout(is->m_continue_read_thread, wait_mutex, 10);
  140. SDL_UnlockMutex(wait_mutex);
  141. continue;
  142. }
  143. // 4.3 根据当前packet类型(音频、视频、字幕),将其存入对应的packet队列
  144. if (pkt->stream_index == is->audio_idx[is->m_icurrent_index]){
  145. packet_queue_put(&is->audio_pkt_queue, pkt, is->rvc_hostapi);
  146. }
  147. else if (pkt->stream_index == is->video_idx[is->m_icurrent_index]){
  148. packet_queue_put(&is->video_pkt_queue, pkt, is->rvc_hostapi);
  149. }
  150. else{
  151. av_packet_unref(pkt);
  152. }
  153. }
  154. SDL_DestroyMutex(wait_mutex);
  155. return 0;
  156. }
  157. int open_demux(player_stat_t *is)
  158. {
  159. if (demux_init(is) != 0){
  160. is->rvc_hostapi->Debug(MEDIA_LOG_ERROR, "demux_init() failed.");
  161. return -1;
  162. }
  163. is->m_read_tid = SDL_CreateThread(demux_thread, "demux_thread", is);
  164. if (is->m_read_tid == NULL){
  165. is->rvc_hostapi->Debug(MEDIA_LOG_ERROR, "SDL_CreateThread() failed: %s.", SDL_GetError());
  166. return -1;
  167. }
  168. return 0;
  169. }