1. 协议的注册av_register_all
-
void av_register_all(void)
-
{
-
REGISTER_PROTOCOL(FILE, file);
-
REGISTER_PROTOCOL(FTP, ftp);
-
REGISTER_PROTOCOL(HTTP, http);
-
REGISTER_PROTOCOL(RTP, rtp);
-
REGISTER_PROTOCOL(SCTP, sctp);
-
...
- }
FILE FTP HTTP RTP等协议都属于protocol
2. 协议的初始化过程
-
avformat_open_input(&ic, is->filename, is->iformat, &format_opts);
-
{
-
1.avformat_alloc_context
-
--> avformat_get_context_defaults
-
:: s->io_open = io_open_default; //初始化了AVFormatContext的io_open
-
2. init_input(s, filename, &tmp);
-
--> av_probe_input_format2 //没有打开媒体文件之前,先去probe input的类型,这儿一般是失败的
-
-->s->io_open //先probe协议类型,然后用probe到的协议类型去打开媒体文件
- -->io_open_default //options.c
- -->ffio_open_whitelist //aviobuf.c
- -->ffurl_open_whitelist //avio.c
- --> ffurl_alloc //avio.c
- --> ffurl_connect //avio.c
- }
-->url_find_protocol //解析filename,并返回查找到的协议类型
-->url_alloc_for_protocol //为这个协议类型分配内存,并初始化
-
static struct URLProtocol *url_find_protocol(const char *filename)
-
{
-
URLProtocol *up = NULL;
-
char proto_str[128], proto_nested[128], *ptr;
-
size_t proto_len = strspn(filename, URL_SCHEME_CHARS);
- //如果文件名中没有:则协议类型的名字是file,若有则把:之前的当做协议类型
- //例如: /tmp/friends.mkv则协议类型为name=file
- // ,则协议类型的name=http
-
if (filename[proto_len] != ':' &&
-
(strncmp(filename, "subfile,", 8) || !strchr(filename + proto_len + 1, ':')) ||
-
is_dos_path(filename))
-
strcpy(proto_str, "file");
-
else
- av_strlcpy(proto_str, filename, FFMIN(proto_len + 1, sizeof(proto_str)));
-
-
if ((ptr = strchr(proto_str, ',')))
-
*ptr = '\0';
-
av_strlcpy(proto_nested, proto_str, sizeof(proto_nested));
-
if ((ptr = strchr(proto_nested, '+')))
-
*ptr = '\0';
- //然后按名字查找协议类型,把查找到的返回
-
while (up = ffurl_protocol_next(up)) {
-
if (!strcmp(proto_str, up->name))
-
break;
-
if (up->flags & URL_PROTOCOL_FLAG_NESTED_SCHEME &&
-
!strcmp(proto_nested, up->name))
-
break;
- }
-
return up;
- }
ffurl_connect //如果protocol中没有url_open2则调用url_open
::err = uc->prot->url_open2 ? uc->prot->url_open2(uc, uc->filename, uc->flags,options) :
uc->prot->url_open(uc, uc->filename, uc->flags);
这儿以file的protocol为例说明
在file中其url_open就是file_open
.url_open = file_open,
-
static int file_open(URLContext *h, const char *filename, int flags)
-
{
-
FileContext *c = h->priv_data;
-
int access;
-
int fd;
-
struct stat st;
- fd = avpriv_open(filename, access, 0666); //这个就是调用系统调用open
-
if (fd == -1)
-
return AVERROR(errno);
-
c->fd = fd;
-
-
h->is_streamed = !fstat(fd, &st) && S_ISFIFO(st.st_mode);
-
-
return 0;
- }
二.协议层的操作
具体的AVInputFormat对文件的操作都是直接调用aviobuf层的接口来实现的
2.1 函数指针的初始化
ffio_open_whitelist
ffio_fdopen
-->avio_alloc_context
::分别初始化如下
write_packet= ffurl_write
read_packet= ffurl_read
seek = ffurl_seek
2.2 avio_size的调用
-
avio_size
-
--> s->seek(s->opaque, 0, AVSEEK_SIZE);
-
::ffurl_seek();
-
-
int64_t ffurl_seek(URLContext *h, int64_t pos, int whence)
-
{
-
int64_t ret;
-
-
if (!h->prot->url_seek)
-
return AVERROR(ENOSYS);
-
ret = h->prot->url_seek(h, pos, whence & ~AVSEEK_FORCE);
-
return ret;
- }
-
.url_seek = file_seek,
-
static int64_t file_seek(URLContext *h, int64_t pos, int whence)
-
{
-
FileContext *c = h->priv_data;
-
int64_t ret;
-
//如果whence是AVSEEK_SIZE,则返回长度
-
if (whence == AVSEEK_SIZE) {
-
struct stat st;
-
ret = fstat(c->fd, &st);
- return ret < 0 ? AVERROR(errno) : (S_ISFIFO(st.st_mode) ? 0 : st.st_size);
-
}
- //否则就调用lseek
-
ret = lseek(c->fd, pos, whence);
- return ret < 0 ? AVERROR(errno) : ret;
- }