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