123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195 |
- #include "demux.h"
- #include "packet.h"
- #ifndef RVC_MAX_BUFFER_LEN
- #define RVC_MAX_BUFFER_LEN 1024
- #endif
- static int decode_interrupt_cb(void *ctx)
- {
- player_stat_t *is = (player_stat_t*)ctx;
- //return (int)(is->bvideo_finished && is->baudio_finished);
- return (int)(is->buser_stop);
- }
- static int demux_init(player_stat_t *is)
- {
- int ret = -1;
- for (int index = 0; index < is->m_uFilesCount; index++)
- {
- AVFormatContext* p_fmt_ctx = NULL;
- int err = -1;
- int a_idx = -1;
- int v_idx = -1;
- p_fmt_ctx = avformat_alloc_context();
- if (!p_fmt_ctx){
- is->rvc_hostapi->Debug(MEDIA_LOG_ERROR, "Could not allocate context.");
- ret = AVERROR(ENOMEM);
- break;
- }
- // 中断回调机制。为底层I/O层提供一个处理接口,比如中止IO操作。
- p_fmt_ctx->interrupt_callback.callback = decode_interrupt_cb;
- p_fmt_ctx->interrupt_callback.opaque = is;
- // 1. 构建AVFormatContext
- // 1.1 打开视频文件:读取文件头,将文件格式信息存储在"fmt context"中
- err = avformat_open_input(&p_fmt_ctx, is->m_strPlayLists[index], NULL, NULL);
- if (err < 0){
- char buffer[RVC_MAX_BUFFER_LEN] = { 0 };
- av_strerror(err, buffer, RVC_MAX_BUFFER_LEN);
- is->rvc_hostapi->Debug(MEDIA_LOG_ERROR, "avformat_open_input file %s failed %d(%s).", is->m_strPlayLists[index], err, buffer);
- avformat_close_input(&p_fmt_ctx);
- avformat_free_context(p_fmt_ctx);
- ret = -1;
- break;
- }
- is->m_pfmt_ctx[index] = p_fmt_ctx;
- // 1.2 搜索流信息:读取一段视频文件数据,尝试解码,将取到的流信息填入p_fmt_ctx->streams
- // ic->streams是一个指针数组,数组大小是pFormatCtx->nb_streams
- err = avformat_find_stream_info(p_fmt_ctx, NULL);
- if (err < 0){
- char strbuffer[RVC_MAX_BUFFER_LEN] = { 0 };
- av_strerror(err, strbuffer, RVC_MAX_BUFFER_LEN);
- is->rvc_hostapi->Debug(MEDIA_LOG_ERROR, "%s avformat_find_stream_info() failed %d(%s).", is->m_strPlayLists[index], err, strbuffer);
- avformat_close_input(&p_fmt_ctx);
- avformat_free_context(p_fmt_ctx);
- ret = -1;
- break;
- }
- // 2. 查找第一个音频流/视频流
- for (int i = 0; i < (int)p_fmt_ctx->nb_streams; i++){
- if ((p_fmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) && (a_idx == -1)){
- a_idx = i;
- }
- if ((p_fmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) && (v_idx == -1)){
- v_idx = i;
- }
- if (a_idx != -1 && v_idx != -1){
- break;
- }
- }
- if ((a_idx == -1 && v_idx == -1) || (eVideo_Type == is->m_eMType && v_idx == -1)){
- is->rvc_hostapi->Debug(MEDIA_LOG_ERROR, "Invalid file %s can not find any audio/video stream.", is->m_strPlayLists[index]);
- ret = -1;
- if (NULL != p_fmt_ctx){
- avformat_close_input(&p_fmt_ctx);
- avformat_free_context(p_fmt_ctx);
- is->m_pfmt_ctx[index] = NULL;
- }
- break;
- }
- is->audio_idx[index] = a_idx;
- is->video_idx[index] = v_idx;
- is->m_paudio_stream[index] = p_fmt_ctx->streams[a_idx];
- is->m_pvideo_stream[index] = p_fmt_ctx->streams[v_idx];
-
- ret = 0;
- }
- return ret;
- }
- int demux_deinit()
- {
- return 0;
- }
- static int stream_has_enough_packets(AVStream *st, int stream_id, packet_queue_t *queue)
- {
- return stream_id < 0 ||
- queue->abort_flag ||
- (st->disposition & AV_DISPOSITION_ATTACHED_PIC) ||
- queue->nb_packets > MIN_FRAMES && (!queue->duration || av_q2d(st->time_base) * queue->duration > 1.0);
- }
- /* this thread gets the stream from the disk or the network */
- static int demux_thread(void *arg)
- {
- player_stat_t *is = (player_stat_t *)arg;
- int ret = 0;
-
- AVPacket pkt1, *pkt = &pkt1;
- SDL_mutex *wait_mutex = SDL_CreateMutex();
- // 4. 解复用处理
- while (false == is->buser_stop)
- {
- /* if the queue are full, no need to read more */
- if (is->audio_pkt_queue.packet_queue_size + is->video_pkt_queue.packet_queue_size > MAX_QUEUE_SIZE ||
- (stream_has_enough_packets(is->m_paudio_stream[is->m_icurrent_index], is->audio_idx[is->m_icurrent_index], &is->audio_pkt_queue) &&
- stream_has_enough_packets(is->m_pvideo_stream[is->m_icurrent_index], is->video_idx[is->m_icurrent_index], &is->video_pkt_queue)))
- {
- /* wait 10 ms */
- SDL_LockMutex(wait_mutex);
- SDL_CondWaitTimeout(is->m_continue_read_thread, wait_mutex, 10);
- SDL_UnlockMutex(wait_mutex);
- continue;
- }
- // 4.1 从输入文件中读取一个packet
- ret = av_read_frame(is->m_pfmt_ctx[is->m_icurrent_index], pkt);
- if (ret < 0)
- {
- if (AVERROR_EOF == ret)
- {
- // 输入文件已读完,则往packet队列中发送NULL packet,以冲洗(flush)解码器,否则解码器中缓存的帧取不出来
- if (is->video_idx[is->m_icurrent_index] >= 0){
- packet_queue_put_nullpacket(&is->video_pkt_queue, is->video_idx[is->m_icurrent_index], is->rvc_hostapi);
- }
- if (is->audio_idx[is->m_icurrent_index] >= 0){
- packet_queue_put_nullpacket(&is->audio_pkt_queue, is->audio_idx[is->m_icurrent_index], is->rvc_hostapi);
- }
- if (is->m_icurrent_index + 1 < is->m_uFilesCount){
- is->m_icurrent_index++;
- ret = 0;
- }
- else {
- break;
- }
- }
- SDL_LockMutex(wait_mutex);
- SDL_CondWaitTimeout(is->m_continue_read_thread, wait_mutex, 10);
- SDL_UnlockMutex(wait_mutex);
- continue;
- }
-
- // 4.3 根据当前packet类型(音频、视频、字幕),将其存入对应的packet队列
- if (pkt->stream_index == is->audio_idx[is->m_icurrent_index]){
- packet_queue_put(&is->audio_pkt_queue, pkt, is->rvc_hostapi);
- }
- else if (pkt->stream_index == is->video_idx[is->m_icurrent_index]){
- packet_queue_put(&is->video_pkt_queue, pkt, is->rvc_hostapi);
- }
- else{
- av_packet_unref(pkt);
- }
- }
- SDL_DestroyMutex(wait_mutex);
- return 0;
- }
- int open_demux(player_stat_t *is)
- {
- if (demux_init(is) != 0){
- is->rvc_hostapi->Debug(MEDIA_LOG_ERROR, "demux_init() failed.");
- return -1;
- }
- is->m_read_tid = SDL_CreateThread(demux_thread, "demux_thread", is);
- if (is->m_read_tid == NULL){
- is->rvc_hostapi->Debug(MEDIA_LOG_ERROR, "SDL_CreateThread() failed: %s.", SDL_GetError());
- return -1;
- }
- return 0;
- }
|