demux.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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->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->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->strPlayLists[index], err, buffer);
  37. ret = -1;
  38. break;
  39. }
  40. is->p_fmt_ctx[index] = p_fmt_ctx;
  41. is->rvc_hostapi->Debug(MEDIA_LOG_DEBUG, "is->p_fmt_ctx[%d] = 0x%08x, p_fmt_ctx = 0x%08x", index, is->p_fmt_ctx[index], p_fmt_ctx);
  42. // 1.2 搜索流信息:读取一段视频文件数据,尝试解码,将取到的流信息填入p_fmt_ctx->streams
  43. // ic->streams是一个指针数组,数组大小是pFormatCtx->nb_streams
  44. err = avformat_find_stream_info(p_fmt_ctx, NULL);
  45. if (err < 0){
  46. char strbuffer[RVC_MAX_BUFFER_LEN] = { 0 };
  47. av_strerror(err, strbuffer, RVC_MAX_BUFFER_LEN);
  48. is->rvc_hostapi->Debug(MEDIA_LOG_ERROR, "%s avformat_find_stream_info() failed %d(%s).", is->strPlayLists[index], err, strbuffer);
  49. ret = -1;
  50. break;
  51. }
  52. // 2. 查找第一个音频流/视频流
  53. for (int i = 0; i < (int)p_fmt_ctx->nb_streams; i++){
  54. if ((p_fmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) && (a_idx == -1)){
  55. a_idx = i;
  56. is->rvc_hostapi->Debug(MEDIA_LOG_DEBUG, "%s Find a audio stream, index %d, media type is %s.", is->strPlayLists[index], a_idx, Media_Type_Table[is->eMType]);
  57. }
  58. if ((p_fmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) && (v_idx == -1)){
  59. v_idx = i;
  60. is->rvc_hostapi->Debug(MEDIA_LOG_DEBUG, "%s Find a video stream, index %d, media type is %s.", is->strPlayLists[index], v_idx, Media_Type_Table[is->eMType]);
  61. }
  62. if (a_idx != -1 && v_idx != -1){
  63. break;
  64. }
  65. }
  66. if ((a_idx == -1 && v_idx == -1) || (eVideo_Type == is->eMType && v_idx == -1)){
  67. is->rvc_hostapi->Debug(MEDIA_LOG_ERROR, "Invalid file %s can not find any audio/video stream.", is->strPlayLists[index]);
  68. ret = -1;
  69. if (NULL != p_fmt_ctx){
  70. avformat_close_input(&p_fmt_ctx);
  71. is->p_fmt_ctx[index] = NULL;
  72. }
  73. break;
  74. }
  75. is->audio_idx[index] = a_idx;
  76. is->video_idx[index] = v_idx;
  77. is->p_audio_stream[index] = p_fmt_ctx->streams[a_idx];
  78. is->p_video_stream[index] = p_fmt_ctx->streams[v_idx];
  79. ret = 0;
  80. }
  81. return ret;
  82. }
  83. int demux_deinit()
  84. {
  85. return 0;
  86. }
  87. static int stream_has_enough_packets(AVStream *st, int stream_id, packet_queue_t *queue)
  88. {
  89. return stream_id < 0 ||
  90. queue->abort_flag ||
  91. (st->disposition & AV_DISPOSITION_ATTACHED_PIC) ||
  92. queue->nb_packets > MIN_FRAMES && (!queue->duration || av_q2d(st->time_base) * queue->duration > 1.0);
  93. }
  94. /* this thread gets the stream from the disk or the network */
  95. static int demux_thread(void *arg)
  96. {
  97. player_stat_t *is = (player_stat_t *)arg;
  98. //AVFormatContext *p_fmt_ctx = is->p_fmt_ctx;
  99. int ret = 0;
  100. AVPacket pkt1, *pkt = &pkt1;
  101. SDL_mutex *wait_mutex = SDL_CreateMutex();
  102. is->rvc_hostapi->Debug(MEDIA_LOG_DEBUG, "begin %s av_read_frame, nFilecount is %d. current index is %d.", is->strPlayLists[is->icurrent_index], is->uFilesCount, is->icurrent_index);
  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->p_audio_stream[is->icurrent_index], is->audio_idx[is->icurrent_index], &is->audio_pkt_queue) &&
  109. stream_has_enough_packets(is->p_video_stream[is->icurrent_index], is->video_idx[is->icurrent_index], &is->video_pkt_queue)))
  110. {
  111. /* wait 10 ms */
  112. SDL_LockMutex(wait_mutex);
  113. SDL_CondWaitTimeout(is->continue_read_thread, wait_mutex, 10);
  114. SDL_UnlockMutex(wait_mutex);
  115. continue;
  116. }
  117. // 4.1 从输入文件中读取一个packet
  118. ret = av_read_frame(is->p_fmt_ctx[is->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->icurrent_index] >= 0){
  125. packet_queue_put_nullpacket(&is->video_pkt_queue, is->video_idx[is->icurrent_index], is->rvc_hostapi);
  126. }
  127. if (is->audio_idx[is->icurrent_index] >= 0){
  128. packet_queue_put_nullpacket(&is->audio_pkt_queue, is->audio_idx[is->icurrent_index], is->rvc_hostapi);
  129. }
  130. if (is->icurrent_index + 1 < is->uFilesCount){
  131. is->icurrent_index++;
  132. ret = 0;
  133. is->rvc_hostapi->Debug(MEDIA_LOG_DEBUG, "begin %s av_read_frame, nFilecount is %d, current is->index is %d.", is->strPlayLists[is->icurrent_index], is->uFilesCount, is->icurrent_index);
  134. }
  135. else {
  136. is->rvc_hostapi->Debug(MEDIA_LOG_DEBUG, "av_read_frame ret is AVERROR_EOF.");
  137. break;
  138. }
  139. }
  140. SDL_LockMutex(wait_mutex);
  141. SDL_CondWaitTimeout(is->continue_read_thread, wait_mutex, 10);
  142. SDL_UnlockMutex(wait_mutex);
  143. continue;
  144. }
  145. // 4.3 根据当前packet类型(音频、视频、字幕),将其存入对应的packet队列
  146. if (pkt->stream_index == is->audio_idx[is->icurrent_index]){
  147. packet_queue_put(&is->audio_pkt_queue, pkt, is->rvc_hostapi);
  148. }
  149. else if (pkt->stream_index == is->video_idx[is->icurrent_index]){
  150. packet_queue_put(&is->video_pkt_queue, pkt, is->rvc_hostapi);
  151. }
  152. else{
  153. av_packet_unref(pkt);
  154. }
  155. }
  156. SDL_DestroyMutex(wait_mutex);
  157. is->rvc_hostapi->Debug(MEDIA_LOG_DEBUG, "%s exit, thread id is %u, buser_stop flag is %s.",SDL_GetThreadName(is->read_tid), SDL_ThreadID(), is->buser_stop ? "true" : "false");
  158. return 0;
  159. }
  160. int open_demux(player_stat_t *is)
  161. {
  162. if (demux_init(is) != 0){
  163. is->rvc_hostapi->Debug(MEDIA_LOG_ERROR, "demux_init() failed.");
  164. return -1;
  165. }
  166. is->read_tid = SDL_CreateThread(demux_thread, "demux_thread", is);
  167. if (is->read_tid == NULL){
  168. is->rvc_hostapi->Debug(MEDIA_LOG_ERROR, "SDL_CreateThread() failed: %s.", SDL_GetError());
  169. return -1;
  170. }
  171. else {
  172. is->rvc_hostapi->Debug(MEDIA_LOG_DEBUG, "create %s success, and thread id is %u.", SDL_GetThreadName(is->read_tid), SDL_GetThreadID(is->read_tid));
  173. }
  174. return 0;
  175. }