背景
ffmpeg有自己的一套网络IO代码,但是相对比较简单,现在需要使用自研的网络库接管ffmpeg的网络IO功能。方法有两种:
- 在原本的url前面加127.0.0.1的方式,走本地代理的方式
- 新增自己的协议,替换原本的http协议 第一种方法比较简单,本文主要介绍第二种方法。
首先介绍下libavformat协议框架,然后在介绍下如何在ffmpeg中新增一个自定义的协议。
libavformat协议框架
相关文件
libavformat/protocol_list.c
libavformat/protocols.c
libavformat/avio.c
protocol_list.c是在执行 ./configure 根据参数而动态生成的代码。默认参数生成的代码如下:
static const URLProtocol *url_protocols[] = {
...
&ff_hls_protocol,
&ff_http_protocol,
&ff_httpproxy_protocol,
&ff_rtmp_protocol,
&ff_rtmpt_protocol,
&ff_rtp_protocol,
&ff_srtp_protocol,
&ff_tcp_protocol,
&ff_udp_protocol,
&ff_unix_protocol,
NULL };
url_protocols是一个静态数组,存储了http,tcp,udp,rtmp等常用协议的实现。
做一个与本文无关的友情链接 ffmpeg支持的所有协议说明
关键的URLProtocol现在出场
typedef struct URLProtocol {
const char *name;
int (*url_open2)(URLContext *h, const char *url, int flags, AVDictionary **options);
int (*url_read)( URLContext