当前位置: 首页 > news >正文

FFmpeg源码:url_find_protocol函数分析

一、url_find_protocol函数的定义

url_find_protocol函数定义在FFmpeg源码(本文演示用的FFmpeg源码版本为7.0.1)的源文件libavformat/avio.c中:

static const struct URLProtocol *url_find_protocol(const char *filename)
{const URLProtocol **protocols;char proto_str[128], proto_nested[128], *ptr;size_t proto_len = strspn(filename, URL_SCHEME_CHARS);int i;if (filename[proto_len] != ':' &&(strncmp(filename, "subfile,", 8) || !strchr(filename + proto_len + 1, ':')) ||is_dos_path(filename))strcpy(proto_str, "file");elseav_strlcpy(proto_str, filename,FFMIN(proto_len + 1, sizeof(proto_str)));av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));if ((ptr = strchr(proto_nested, '+')))*ptr = '\0';protocols = ffurl_get_protocols(NULL, NULL);if (!protocols)return NULL;for (i = 0; protocols[i]; i++) {const URLProtocol *up = protocols[i];if (!strcmp(proto_str, up->name)) {av_freep(&protocols);return up;}if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&!strcmp(proto_nested, up->name)) {av_freep(&protocols);return up;}}av_freep(&protocols);if (av_strstart(filename, "https:", NULL) || av_strstart(filename, "tls:", NULL))av_log(NULL, AV_LOG_WARNING, "https protocol not found, recompile FFmpeg with ""openssl, gnutls or securetransport enabled.\n");return NULL;
}

该函数在avformat_open_input函数内部被调用。其作用是:根据形参filename查找并返回匹配的流媒体协议类型 ;如果没有匹配的协议但filename符合“file”协议的url格式,那就返回“file”协议类型(在FFmpeg源码中,“文件”也被当作一种协议类型);如果都不符合则返回NULL。

形参filename:输入型参数,包含流媒体协议的名称信息。比如RTP对应的filename为:rtp://XXX,UDP对应的filename为:udp://XXX。

返回值:匹配到的流媒体协议类型,为URLProtocol类型。该结构体声明在libavformat/url.h中:

typedef struct URLProtocol {const char *name;int     (*url_open)( URLContext *h, const char *url, int flags);/*** This callback is to be used by protocols which open further nested* protocols. options are then to be passed to ffurl_open_whitelist()* or ffurl_connect() for those nested protocols.*/int     (*url_open2)(URLContext *h, const char *url, int flags, AVDictionary **options);int     (*url_accept)(URLContext *s, URLContext **c);int     (*url_handshake)(URLContext *c);/*** Read data from the protocol.* If data is immediately available (even less than size), EOF is* reached or an error occurs (including EINTR), return immediately.* Otherwise:* In non-blocking mode, return AVERROR(EAGAIN) immediately.* In blocking mode, wait for data/EOF/error with a short timeout (0.1s),* and return AVERROR(EAGAIN) on timeout.* Checking interrupt_callback, looping on EINTR and EAGAIN and until* enough data has been read is left to the calling function; see* retry_transfer_wrapper in avio.c.*/int     (*url_read)( URLContext *h, unsigned char *buf, int size);int     (*url_write)(URLContext *h, const unsigned char *buf, int size);int64_t (*url_seek)( URLContext *h, int64_t pos, int whence);int     (*url_close)(URLContext *h);int (*url_read_pause)(void *urlcontext, int pause);int64_t (*url_read_seek)(void *urlcontext, int stream_index,int64_t timestamp, int flags);int (*url_get_file_handle)(URLContext *h);int (*url_get_multi_file_handle)(URLContext *h, int **handles,int *numhandles);int (*url_get_short_seek)(URLContext *h);int (*url_shutdown)(URLContext *h, int flags);const AVClass *priv_data_class;int priv_data_size;int flags;int (*url_check)(URLContext *h, int mask);int (*url_open_dir)(URLContext *h);int (*url_read_dir)(URLContext *h, AVIODirEntry **next);int (*url_close_dir)(URLContext *h);int (*url_delete)(URLContext *h);int (*url_move)(URLContext *h_src, URLContext *h_dst);const char *default_whitelist;
} URLProtocol;

URLProtocol是FFmpeg协议解析器的注册结构体。每个流媒体协议都有一个与之对应的URLProtocol结构,其定义了协议的名称、流媒体url的打开、读取、写入等操作的函数指针。

比如RTP这种流媒体协议,其对应的URLProtocol结构为:

const URLProtocol ff_rtp_protocol = {.name                      = "rtp",.url_open                  = rtp_open,.url_read                  = rtp_read,.url_write                 = rtp_write,.url_close                 = rtp_close,.url_get_file_handle       = rtp_get_file_handle,.url_get_multi_file_handle = rtp_get_multi_file_handle,.priv_data_size            = sizeof(RTPContext),.flags                     = URL_PROTOCOL_FLAG_NETWORK,.priv_data_class           = &rtp_class,
};

“file”协议对应的URLProtocol结构为:

const URLProtocol ff_file_protocol = {.name                = "file",.url_open            = file_open,.url_read            = file_read,.url_write           = file_write,.url_seek            = file_seek,.url_close           = file_close,.url_get_file_handle = file_get_handle,.url_check           = file_check,.url_delete          = file_delete,.url_move            = file_move,.priv_data_size      = sizeof(FileContext),.priv_data_class     = &file_class,.url_open_dir        = file_open_dir,.url_read_dir        = file_read_dir,.url_close_dir       = file_close_dir,.default_whitelist   = "file,crypto,data"
};

“rtmp”协议对应的URLProtocol结构为:

const URLProtocol ff_librtmp_protocol = {.name                = "rtmp",.url_open            = rtmp_open,.url_read            = rtmp_read,.url_write           = rtmp_write,.url_close           = rtmp_close,.url_read_pause      = rtmp_read_pause,.url_read_seek       = rtmp_read_seek,.url_get_file_handle = rtmp_get_file_handle,.priv_data_size      = sizeof(LibRTMPContext),.priv_data_class     = &librtmp_class,.flags               = URL_PROTOCOL_FLAG_NETWORK,
};

二、url_find_protocol函数的内部实现分析

url_find_protocol函数中首先通过下面代码块将形参filename中的流媒体协议名称拷贝到数组proto_str中。如果没有匹配的协议但filename符合“file”协议的url格式,那就拷贝“file”,表示是“文件”协议类型:

    if (filename[proto_len] != ':' &&(strncmp(filename, "subfile,", 8) || !strchr(filename + proto_len + 1, ':')) ||is_dos_path(filename))strcpy(proto_str, "file");elseav_strlcpy(proto_str, filename,FFMIN(proto_len + 1, sizeof(proto_str)));

拷贝流媒体协议名称到数组proto_nested中。关于av_strlcpy函数的用法可以参考:《FFmpeg源码:av_strlcpy函数分析》:

    av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));if ((ptr = strchr(proto_nested, '+')))*ptr = '\0';

返回指向全局数组url_protocols开头的二级指针:

    protocols = ffurl_get_protocols(NULL, NULL);if (!protocols)return NULL;

ffurl_get_protocols函数的定义如下。可以看到其内部遍历了全局数组url_protocols:

const URLProtocol **ffurl_get_protocols(const char *whitelist,const char *blacklist)
{const URLProtocol **ret;int i, ret_idx = 0;ret = av_calloc(FF_ARRAY_ELEMS(url_protocols), sizeof(*ret));if (!ret)return NULL;for (i = 0; url_protocols[i]; i++) {const URLProtocol *up = url_protocols[i];if (whitelist && *whitelist && !av_match_name(up->name, whitelist))continue;if (blacklist && *blacklist && av_match_name(up->name, blacklist))continue;ret[ret_idx++] = up;}return ret;
}

全局数组url_protocols定义在libavformat/protocol_list.c中,该数组中的每个元素都对应一种URLProtocol结构,也就是说数组中的每个元素都对应一种流媒体协议(RTP、FILE、RTMP等):

static const URLProtocol * const url_protocols[] = {&ff_async_protocol,&ff_cache_protocol,&ff_concat_protocol,&ff_concatf_protocol,&ff_crypto_protocol,&ff_data_protocol,&ff_fd_protocol,&ff_ffrtmphttp_protocol,&ff_file_protocol,&ff_ftp_protocol,&ff_gopher_protocol,&ff_hls_protocol,&ff_http_protocol,&ff_httpproxy_protocol,&ff_icecast_protocol,&ff_mmsh_protocol,&ff_mmst_protocol,&ff_md5_protocol,&ff_pipe_protocol,&ff_prompeg_protocol,&ff_rtmp_protocol,&ff_rtmpt_protocol,&ff_rtp_protocol,&ff_srtp_protocol,&ff_subfile_protocol,&ff_tee_protocol,&ff_tcp_protocol,&ff_udp_protocol,&ff_udplite_protocol,&ff_unix_protocol,NULL };

url_find_protocol函数中,通过遍历全局数组url_protocols,比较协议名称,查找出该流媒体协议匹配的URLProtocol结构。如果查找到了,那就赋值给指针up,作为url_find_protocol函数的返回值返回:

    for (i = 0; protocols[i]; i++) {const URLProtocol *up = protocols[i];if (!strcmp(proto_str, up->name)) {av_freep(&protocols);return up;}if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&!strcmp(proto_nested, up->name)) {av_freep(&protocols);return up;}}

如果查找不到匹配的流媒体协议,那就打印警报日志,返回NULL:

    av_freep(&protocols);if (av_strstart(filename, "https:", NULL) || av_strstart(filename, "tls:", NULL))av_log(NULL, AV_LOG_WARNING, "https protocol not found, recompile FFmpeg with ""openssl, gnutls or securetransport enabled.\n");return NULL;

相关文章:

FFmpeg源码:url_find_protocol函数分析

一、url_find_protocol函数的定义 url_find_protocol函数定义在FFmpeg源码(本文演示用的FFmpeg源码版本为7.0.1)的源文件libavformat/avio.c中: static const struct URLProtocol *url_find_protocol(const char *filename) {const URLProt…...

3D与2D机器视觉机械臂引导的区别

3D与2D机器视觉在机械臂引导中的主要区别如下: 数据维度 2D视觉:仅处理平面图像,提供X、Y坐标信息,无法获取深度(Z轴)数据。 3D视觉:处理三维空间数据,提供X、Y、Z坐标及物体的姿态…...

C# 添加图标

一、前言 为应用程序添加图标是优化用户界面、提升应用辨识度的重要操作。合适的图标能帮助用户快速识别和区分不同应用,增强应用的易用性和专业性。 本指南旨在为你提供详细、易懂的步骤,教你如何为应用程序的窗体添加图标。从图标素材的获取到具体的…...

基于 Python 和 Django 的北极星招聘数据可视化系统(附源码,部署)

博主介绍:✌程序员徐师兄、7年大厂程序员经历。全网粉丝12w、csdn博客专家、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java技术领域和毕业项目实战✌ 🍅文末获取源码联系🍅 👇🏻 精彩专栏推荐订阅👇…...

基于STM32、HAL库、MB85RC16PNF(I2C接口)驱动程序设计

一、概述: MB85RC16PNF 是富士通推出的16Kbit(2K x 8位)FRAM(铁电随机存取存储器),具有非易失性、高读写速度和低功耗特性,常用于数据存储。 二、硬件连接: MB85RC16PNF通过I2C接口与STM32L4XX通信,典型连接如下: VDD:3.3V VSS:GND SDA:I2C数据线 SCL:I2C时钟线…...

【产品推介】可驱动5A负载的降压型DC/DC转换器XBL1663

一、产品简介 采用ESOP-8封装的XBL1663最大可输出5A电流 芯伯乐XBL1663是一款专为降压型DC/DC转换器设计的单片集成电路,具有高转换效率、恒定开关频率工作的特点。内置功率 MOSFET可在 4.5 V-40V 输入电源上实现 5A 峰值输出电流,并具有出色的负载和线…...

20.【线性代数】——坐标系中,平行四边形面积=矩阵的行列式

三 坐标系中,平行四边形面积矩阵的行列式 定理验证 定理 在坐标系中,由向量(a,b)和向量(c,d)组成平行四边形的面积 矩阵 [ a b c d ] \begin{bmatrix} a&b\\ c&d \end{bmatrix} [ac​bd​]的行列式,即&#x…...

数据库知识速记:事物隔离级别

数据库知识速记:事物隔离级别 一、什么是事物隔离级别? 事物隔离级别(Transaction Isolation Levels)指的是在数据库管理系统中,不同事物之间在访问共享数据时的隔离程度。隔离级别不仅影响数据的读取和写入行为&…...

重构测试项目为spring+springMVC+Mybatis框架

重构测试项目为springspringMVCMybatis框架 背景 继上次将自动化测试时的医药管理信息系统项目用idea运行成功后,由于项目结构有些乱,一部分代码好像也重复,于是打算重新重构以下该项目,这次先使用springspringMVCMybatis框架 …...

如何使用OPENAI的Whisper功能进行音频字母提取功能

首先你可以使用 Python 中的 requests 库来下载该音频文件,然后通过 open() 打开该文件并传递给 OpenAI Whisper API。 完整代码如下: 安装需要的库: pip install openai requests Python 代码: OPENAI_API_KEY "your o…...

DFS算法篇:理解递归,熟悉递归,成为递归

1.DFS原理 那么dfs就是大家熟知的一个深度优先搜索,那么听起来很高大尚的一个名字,但是实际上dfs的本质就是一个递归,而且是一个带路径的递归,那么递归大家一定很熟悉了,大学c语言课程里面就介绍过递归,我…...

2025java常见面试题第一弹

1. Java中的HashMap和Hashtable有什么区别? 答案: 线程安全性: HashMap是线程不安全的,适合单线程环境。如果在多线程环境下使用,可能会出现数据不一致的问题。 Hashtable是线程安全的,内部方法通过synch…...

JMeter工具介绍、元件和组件的介绍

Jmeter功能概要 JDK常用文件目录介绍 Bin目录:存放可执行文件和配置文件 Docs目录:是Jmeter的API文档,用于开发扩展组件 printable_docs目录:用户帮助手册 lib目录:存放JMeter依赖的jar包和用户扩展所依赖的Jar包…...

机舱卫生和空气质量改善

公共卫生挑战:在密闭空间内控制病原体 由于公共交通等密闭空间内的人员密度很高,因此保持良好的空气质量至关重要。有效的通风系统在循环新鲜空气和降低空气中病原体和污染物的浓度方面起着关键作用。使用高效微粒空气 (HEPA) 过滤…...

springBoot之环境变量

springboot 在new SpringBootApplication()时, 会扫描所有的spring.factory; 它会给每个接口当做group,所有实现类为List当做value,形成map; group -> List 系统属性 java的相关属性 系统环境属性,指的是操作系统相关的配置 每个配置对应一个contro…...

萨班斯-奥克斯利法案(Sarbanes-Oxley Act, SOX):公司财务透明度的守护者(中英双语)

萨班斯-奥克斯利法案(Sarbanes-Oxley Act):公司财务透明度的守护者 在2001年安然(Enron)和世通(WorldCom)等公司财务造假丑闻爆发后,美国政府迅速出台了《萨班斯-奥克斯利法案》&am…...

iOS 中使用 FFmpeg 的高级功能 - 滤镜(Filters)

FFmpeg 提供了强大的滤镜功能,可以对音视频进行各种处理,例如裁剪、缩放、添加水印、调整颜色、添加特效等。 1. FFmpeg 滤镜基础知识 1.1 什么是滤镜(Filters)? 滤镜是 FFmpeg 提供的一种功能,用于对音视频流进行处理。滤镜链(Filter Chain)是多个滤镜的组合,按顺序…...

tomcat html乱码

web tomcat html中文乱码 将html文件改成jsp <% page language"java" contentType"text/html; charsetUTF-8" pageEncoding"UTF-8"%>添加 <meta charset"UTF-8">...

kubectl exec 实现的原理

kubectl exec 是 Kubernetes 提供的一个命令&#xff0c;它允许你在指定的 Pod 中执行命令&#xff0c;类似于在容器中打开一个终端会话。这个功能对于调试、监控和管理容器化应用非常有用。kubectl exec 的实现涉及到多个 Kubernetes 组件和机制&#xff0c;包括 API Server、…...

Unity中可靠的UDP实现

可靠 UDP&#xff08;Reliable UDP&#xff09;是一种在用户数据报协议&#xff08;UDP&#xff09;基础上&#xff0c;通过添加额外机制来实现可靠数据传输的技术。与传统 UDP 相比&#xff0c;它克服了 UDP 本身不保证数据可靠性、顺序性以及可能丢失数据的缺点&#xff0c;同…...

AI-调查研究-01-正念冥想有用吗?对健康的影响及科学指南

点一下关注吧&#xff01;&#xff01;&#xff01;非常感谢&#xff01;&#xff01;持续更新&#xff01;&#xff01;&#xff01; &#x1f680; AI篇持续更新中&#xff01;&#xff08;长期更新&#xff09; 目前2025年06月05日更新到&#xff1a; AI炼丹日志-28 - Aud…...

基于大模型的 UI 自动化系统

基于大模型的 UI 自动化系统 下面是一个完整的 Python 系统,利用大模型实现智能 UI 自动化,结合计算机视觉和自然语言处理技术,实现"看屏操作"的能力。 系统架构设计 #mermaid-svg-2gn2GRvh5WCP2ktF {font-family:"trebuchet ms",verdana,arial,sans-…...

Qt Widget类解析与代码注释

#include "widget.h" #include "ui_widget.h"Widget::Widget(QWidget *parent): QWidget(parent), ui(new Ui::Widget) {ui->setupUi(this); }Widget::~Widget() {delete ui; }//解释这串代码&#xff0c;写上注释 当然可以&#xff01;这段代码是 Qt …...

C# 求圆面积的程序(Program to find area of a circle)

给定半径r&#xff0c;求圆的面积。圆的面积应精确到小数点后5位。 例子&#xff1a; 输入&#xff1a;r 5 输出&#xff1a;78.53982 解释&#xff1a;由于面积 PI * r * r 3.14159265358979323846 * 5 * 5 78.53982&#xff0c;因为我们只保留小数点后 5 位数字。 输…...

在Ubuntu24上采用Wine打开SourceInsight

1. 安装wine sudo apt install wine 2. 安装32位库支持,SourceInsight是32位程序 sudo dpkg --add-architecture i386 sudo apt update sudo apt install wine32:i386 3. 验证安装 wine --version 4. 安装必要的字体和库(解决显示问题) sudo apt install fonts-wqy…...

return this;返回的是谁

一个审批系统的示例来演示责任链模式的实现。假设公司需要处理不同金额的采购申请&#xff0c;不同级别的经理有不同的审批权限&#xff1a; // 抽象处理者&#xff1a;审批者 abstract class Approver {protected Approver successor; // 下一个处理者// 设置下一个处理者pub…...

C++ 设计模式 《小明的奶茶加料风波》

&#x1f468;‍&#x1f393; 模式名称&#xff1a;装饰器模式&#xff08;Decorator Pattern&#xff09; &#x1f466; 小明最近上线了校园奶茶配送功能&#xff0c;业务火爆&#xff0c;大家都在加料&#xff1a; 有的同学要加波霸 &#x1f7e4;&#xff0c;有的要加椰果…...

前端中slice和splic的区别

1. slice slice 用于从数组中提取一部分元素&#xff0c;返回一个新的数组。 特点&#xff1a; 不修改原数组&#xff1a;slice 不会改变原数组&#xff0c;而是返回一个新的数组。提取数组的部分&#xff1a;slice 会根据指定的开始索引和结束索引提取数组的一部分。不包含…...

【51单片机】4. 模块化编程与LCD1602Debug

1. 什么是模块化编程 传统编程会将所有函数放在main.c中&#xff0c;如果使用的模块多&#xff0c;一个文件内会有很多代码&#xff0c;不利于组织和管理 模块化编程则是将各个模块的代码放在不同的.c文件里&#xff0c;在.h文件里提供外部可调用函数声明&#xff0c;其他.c文…...

ArcGIS Pro+ArcGIS给你的地图加上北回归线!

今天来看ArcGIS Pro和ArcGIS中如何给制作的中国地图或者其他大范围地图加上北回归线。 我们将在ArcGIS Pro和ArcGIS中一同介绍。 1 ArcGIS Pro中设置北回归线 1、在ArcGIS Pro中初步设置好经纬格网等&#xff0c;设置经线、纬线都以10间隔显示。 2、需要插入背会归线&#xf…...