ffmpeg面向对象——rtsp拉流探索(1)

目录

标准rtsp协议的基石是tcp,本节探索下ffmpeg的rtsp拉流协议tcp的socket创建及链接。

1.tcp创建及链接的流程图及对象图

tcp创建及链接的流程图,如下:

tcp创建及链接的对象图,如下:

2.解析

ffmpeg拉流,从avformat_open_input接口开始,去除与rtsp拉流无关的代码后,如下:

c 复制代码
int avformat_open_input(AVFormatContext **ps, const char *filename,
                        const AVInputFormat *fmt, AVDictionary **options)
{
    AVFormatContext *s = *ps;
    FFFormatContext *si;
    AVDictionary *tmp = NULL;
    int ret = 0;

    if (!s && !(s = avformat_alloc_context()))
        return AVERROR(ENOMEM);
    si = ffformatcontext(s);
    if (!s->av_class) {
        av_log(NULL, AV_LOG_ERROR, "Input context has not been properly allocated by avformat_alloc_context() and is not NULL either\n");
        return AVERROR(EINVAL);
    }

    if (options)
        av_dict_copy(&tmp, *options, 0);

    if ((ret = av_opt_set_dict(s, &tmp)) < 0)
        goto fail;

    if (!(s->url = av_strdup(filename ? filename : ""))) {
        ret = AVERROR(ENOMEM);
        goto fail;
    }

    if ((ret = init_input(s, filename, &tmp)) < 0)
        goto fail;
    s->probe_score = ret;


    if (s->format_whitelist && av_match_list(s->iformat->name, s->format_whitelist, ',') <= 0) 
    {
        av_log(s, AV_LOG_ERROR, "Format not on whitelist \'%s\'\n", s->format_whitelist);
        ret = AVERROR(EINVAL);
        goto fail;
    }

    /* Check filename in case an image number is expected. */
    if (s->iformat->flags & AVFMT_NEEDNUMBER) {
        if (!av_filename_number_test(filename)) {
            ret = AVERROR(EINVAL);
            goto fail;
        }
    }

    s->duration = s->start_time = AV_NOPTS_VALUE;

    /* Allocate private data. */
    if (s->iformat->priv_data_size > 0) 
    {
        if (!(s->priv_data = av_mallocz(s->iformat->priv_data_size))) {
            ret = AVERROR(ENOMEM);
            goto fail;
        }
        if (s->iformat->priv_class) 
        {
            *(const AVClass **) s->priv_data = s->iformat->priv_class;
            av_opt_set_defaults(s->priv_data);
            if ((ret = av_opt_set_dict(s->priv_data, &tmp)) < 0)
                goto fail;
        }
    }

    if (s->iformat->read_header)
    {
        if ((ret = s->iformat->read_header(s)) < 0) {
            if (s->iformat->flags_internal & FF_FMT_INIT_CLEANUP)
                goto close;
            goto fail;
        }
    }


    if ((ret = avformat_queue_attached_pictures(s)) < 0)
        goto close;

    si->raw_packet_buffer_size = 0;

    update_stream_avctx(s);

    if (options) {
        av_dict_free(options);
        *options = tmp;
    }
    *ps = s;
    return 0;

close:
    if (s->iformat->read_close)
        s->iformat->read_close(s);
fail:
    av_dict_free(&tmp);
    avformat_free_context(s);
    *ps = NULL;
    return ret;
}

其中留下了字典参数配置流程,因为基本会有所设置,参数配置参见《ffmpeg面向对象------参数配置机制及其设计模式探索》。

流程图中,输入格式匹配参见《ffmpeg面向对象-rtsp拉流相关对象》的第2节。

协议匹配机制参见《ffmpeg面向对象------拉流协议匹配机制探索》。

输入格式类与协议类什么关系参见《ffmpeg面向对象------AVInputFormat与URLProtocol啥关系

相关推荐
jndingxin1 小时前
瑞芯微算法环境搭建(1)------编译ffmpeg
ffmpeg
彷徨而立1 天前
【FFmpeg】销毁解码器时,必须清理剩余帧吗?
ffmpeg
骄傲的心别枯萎1 天前
项目1:FFMPEG推流器讲解(五):FFMPEG时间戳、时间基、时间转换的讲解
ffmpeg·音视频·视频编解码·时间戳·rv1126
彷徨而立1 天前
【FFmpeg】HW 解码器销毁时,资源回收顺序
ffmpeg
彷徨而立2 天前
【FFmpeg】如何判断 HW解码器输出的是 硬件帧?
ffmpeg
派阿喵搞电子2 天前
基于ffmpeg库,在AGX上编译jetsonFFmpeg库带有硬件加速的h264_nvmpi视频编解码器
ffmpeg·视频编解码
彷徨而立2 天前
【FFmpeg】HW解码器输出 硬件帧 or 软件帧
ffmpeg
长沙红胖子Qt2 天前
FFmpeg开发笔记(十三):ffmpeg采集麦克风音频pcm重采样为aac录音为AAC文件
笔记·ffmpeg·音视频
feiyangqingyun2 天前
全网首发/Qt结合ffmpeg实现rist推拉流/可信赖的互联网流媒体协议/跨平台支持各个系统
qt·ffmpeg·rist推拉流
shenhuxi_yu3 天前
ffmpeg avio使用示例
ffmpeg