1)avformat_open_input函数的核心调用是init_input和read_header

init_input主要是根据传递的文件名称遍历搜索到匹配的解复用器(文件名称不能很好描述如何遍历过程的,参考相关博客有详细说明)

read_header顾名思义就是读取文件名称的头部信息,当前以RTSP URL为例,可以看到该函数主要是连接RTSP服务器,进行RTSP交互,然后取流

 

2)针对RTSP URL初始化过程代码展示

    AVFormatContext *ic = avformat_alloc_context();

     AVInputFormat* pAVInputFormat = av_find_input_format("rtsp");
     is->iformat = pAVInputFormat;//手动指定解复用器
    err = avformat_open_input(&ic, is->filename, is->iformat, NULL);//is->filename指向URL

 

3)针对内存中的视音频数据,目前以H264裸流数据作为例子说明

//创建一个I/O读写上下文,I/O读写上下文如果要读取数据,就调用ReadSteamData获取数据,保存到pbBuffer
const int nBufferSize = 1024*500
uint8_t* pbBuffer= (uint8_t*)av_mallocz(sizeof(uint8_t)*nBufferSize);
AVIOContext *pbAVIOContext   = avio_alloc_context(pbBuffer , nBufferSize, 0, this, ReadStreamData, NULL, NULL);

//avio_alloc_context设置了read_packet指向ReadStreamData

 AVInputFormat * pAVInputFormat   = NULL;

//在这里就是不断读取ReadStreamData中的数据,探测码流的解复用器

av_probe_input_buffer(pbAVIOContext, &pAVInputFormat, "", NULL, 0, 0);

//保存I/O上下文

 AVFormatContext* pAVFormatContext =  avformat_alloc_context();

 pAVFormatContext->pb = pbAVIOContext  ;