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啥关系

相关推荐
今天也想MK代码1 小时前
WebRtc语音通话前置铃声处理
ffmpeg·webrtc
撬动未来的支点7 小时前
解读ffmpeg控制台输出
ffmpeg
戴草帽的大z11 小时前
使用V4L2工具验证RK3588平台视频设备节点数据有效性
ffmpeg·音视频·rk3588·nv12·v4l2-ctl
戴草帽的大z11 小时前
rk3588上用rk_mpi_vi_test与ffmpeg实战
ffmpeg·rk3588·mpi·rk_mpi_vi
笑虾11 小时前
bat 批处理实现 FFmpeg 命令压缩 MP4
ffmpeg
PenTablet11 小时前
用ffmpeg来压缩视频文件
ffmpeg
mortimer1 天前
一键实现人声伴奏分离:基于 `uv`, `FFmpeg` 和 `audio-separator` 的高效解决方案
python·ffmpeg·音视频开发
筏.k3 天前
WebRTC 项目中捕获 FFmpeg 底层源码日志(av_log)的完整方案
ffmpeg·webrtc
学习_学习_再学习3 天前
ffmpeg学习记录
学习·ffmpeg
我科绝伦(Huanhuan Zhou)4 天前
Oracle AWR管理与快照操作完整指南
数据库·oracle·ffmpeg