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

相关推荐
shenhuxi_yu17 小时前
ffmpeg avio使用示例
ffmpeg
aqi002 天前
FFmpeg开发笔记(八十二)使用国产直播服务器smart_rtmpd执行推流操作
ffmpeg·音视频·直播·流媒体
陈增林2 天前
用 PyQt5 + FFmpeg 打造批量视频音频提取器
qt·ffmpeg·音视频
西瓜er2 天前
JAVA:Spring Boot 集成 FFmpeg 实现多媒体处理
java·spring boot·ffmpeg
QMCY_jason3 天前
ubuntu 24.04 FFmpeg编译 带Nvidia 加速记录
linux·ubuntu·ffmpeg
eqwaak03 天前
动态图表导出与视频生成:精通Matplotlib Animation与FFmpeg
开发语言·python·ffmpeg·音视频·matplotlib
执尺量北斗3 天前
LinkMate 智能会议室系统:基于 Qt / QML / WebRTC / FFmpeg / Whisper / OpenGL 的实时音视频会议平台
qt·ffmpeg·webrtc
月起星九4 天前
为什么ffmpeg进行视频合成有时长误差
ffmpeg·音视频
长沙红胖子Qt5 天前
FFmpeg开发笔记(十二):ffmpeg音频处理、采集麦克风音频录音为WAV
ffmpeg·pcm·wav·录音·麦克风
aqi006 天前
FFmpeg开发笔记(八十一)FFmpeg代码对RTSP和RTMP的推流区别
ffmpeg·音视频·直播·流媒体