织梦CMS - 轻松建站从此开始!

罗索

ffmpeg中的http协议相关代码阅读笔记

jackyhwei 发布于 2011-10-14 10:08 点击:次 
通过看ffmpeg的代码,总结了下,其实ffmpeg本身并不是很神秘,他本身只是提供了一个很好的很平等并可以无线扩展的架构。而在ffmpeg这个架构上,才能有很多codec, decodec和mutex, io, protocol很好的共存处理。
TAG:

今天闲来无事,尝试看了下ffmpeg中的相关http协议传输处理代码

先简单说下这个代码在整个仓库里面的位置:

ffmpeg/libavformat/http.h

ffmpeg/libavformat/http.c

avoi.h中的函数调用分析

avoi.h是ffmpeg中libavformat/目录下的一个重要的头文件,这个文件主要处理了一些传输协议的传输封装。他的封装过程是通过函数指针来实现的。

可以先看http.c文件中的最后一个结构体:

  1. URLProtocol http_protocol = { 
  2.     "http"
  3.     http_open, 
  4.     http_read, 
  5.     http_write, 
  6.     http_seek, 
  7.     http_close, 
  8.     .url_get_file_handle = http_get_file_handle, 
  9.     .priv_data_size = sizeof(HTTPContext), 
  10.     .priv_data_class = &httpcontext_class, 
  11. }; 

URLProtocol这个结构是在avoi.h中一个很重要的结构,他的存在的意义就是为了统一接口,可以在avoi.h中查到他的定义:

  1. typedef struct URLProtocol { 
  2.     const char *name; 
  3.     int (*url_open)(URLContext *h, const char *url, int flags); 
  4.     int (*url_read)(URLContext *h, unsigned char *buf, int size); 
  5.     int (*url_write)(URLContext *h, const unsigned char *buf, int size); 
  6.     int64_t (*url_seek)(URLContext *h, int64_t pos, int whence); 
  7.     int (*url_close)(URLContext *h); 
  8.     struct URLProtocol *next; 
  9.     int (*url_read_pause)(URLContext *h, int pause); 
  10.     int64_t (*url_read_seek)(URLContext *h, int stream_index
  11. ,int64_t timestamp, int flags); 
  12.     int (*url_get_file_handle)(URLContext *h); 
  13.     int priv_data_size; 
  14.     const AVClass *priv_data_class; 
  15. } URLProtocol; 

针对http.c文件处理的http协议,最主要的就是实现URLProtocol中的函数指针,至于每个函数的具体意义,相信看命名就可以知道。

那么avoi.h中的函数又是怎么使用到这个内容的呢?这就可以从avoi.h中的几个函数看出来:

int url_open();

int url_read();

int url_write();

int64_t url_seek(URLContext *h, int64_t pos, int whence);
int url_close(URLContext *h);
找其中的一个分析,url_open:
  1. int url_open(URLContext **puc, const char *filename, int flags) 
  2.     int ret = url_alloc(puc, filename, flags); 
  3.     if (ret) 
  4.         return ret; 
  5.     ret = url_connect(*puc); 
  6.     if (!ret) 
  7.         return 0; 
  8.     url_close(*puc); 
  9.     *puc = NULL; 
  10.     return ret; 
url_open函数中使用到了url_alloc, url_connect, url_close,折伞个函数可能会在不同的情况下进行使用,由于代码太多,就不贴了,仅记录如下
url_alloc函数:
  • 处理协议探测,如果url参数中没有正常的协议,就默认填上"file://"然后作为文件协议处理
  • 处理探测的过程是在所有的已经注册上的协议链表中搜索对应名称的协议,这个名称,就是URLProtocol.name
  • 如果探测成功会把puc,即URLContext结构体也帮忙分配好,并作为参数out

url_connect:

  • 和url_open函数一样,url_connect会调用协议中的http_open函数进行http请求初始化: uc->prot->url_open()
  • url_connect返回非零值代表失败

url_close:

  • 如果url_connect失败,返回是非0值,就会调用url_close,并返回整个url_open函数失败

http.c文件的一些关键点分析

http_open函数

http_open函数会尝试调用其中的http_open_cnx,然后http_open_cnx会调用静态成员函数http_connect来进行http请求,其中所有的请求发送都是通过http_write函数来进行的。

http_read函数

相关的函数:

http_read, http_getline, http_getc

http_getc会有个1024的缓冲区,如果发现缓冲区满了才会继续读取,如果没有慢就每次读缓冲区里面的一个字符

  1. static int http_getc(HTTPContext *s) 
  2.     int len; 
  3.     if (s->buf_ptr >= s->buf_end) { 
  4.         len = url_read(s->hd, s->buffer, BUFFER_SIZE); 
  5.         if (len < 0) { 
  6.             return AVERROR(EIO); 
  7.         } else if (len == 0) { 
  8.             return -1; 
  9.         } else { 
  10.             s->buf_ptr = s->buffer; 
  11.             s->buf_end = s->buffer + len; 
  12.         } 
  13.     } 
  14.     return *s->buf_ptr++; 

HTTP协议中数据发送和接受的处理

http.c函数的URLProtocol结构体中有个PrivateData的指针,这个指针存储了http协议中的一些内部使用到的数据变量,内部处理的数据变量结构体如下:

  1. typedef struct { 
  2.     const AVClass *class
  3.     URLContext *hd; 
  4.     unsigned char buffer[BUFFER_SIZE], *buf_ptr, *buf_end; 
  5.     int line_count; 
  6.     int http_code; 
  7.     int64_t chunksize;/**< Used if "Transfer-Encoding: chunked" otherwise -1. */ 
  8.     int64_t off, filesize; 
  9.     char location[MAX_URL_SIZE]; 
  10.     HTTPAuthState auth_state; 
  11.     unsigned char headers[BUFFER_SIZE]; 
  12.     int willclose;
  13. /**< Set if the server correctly handles Connection: close and will 
  14. close the connection after feeding us the content. */ 
  15. } HTTPContext; 

可以看到一个比较重要的信息是:该私有的内部数据中竟 然也有一个URLContext,那这个是什么呢?这个URLContext的协议又是什么呢?我通过阅读代码发现这个URLContext就是指向的 tcp协议,用来通过tcp读取和返回数据。很感慨阿。这样很简单的就可以同时支持http,tcp协议,而且把tcp,udp协议封装起来以后,基本上 其他的像rtsp, mms等协议也可以通过制定他们自己的内在协议来处理了。

在http.c中的http_open_cnx函数中有一句代码:

ff_url_join(buf, sizeof(buf), "tcp", NULL, hostname, port, NULL);

这样就很好的把http的底层tcp协议比较好的串接起来

总结

通过看ffmpeg的代码,总结了下,其实ffmpeg本身并不是很神秘,他本身只是提供了一个很好的很平等并可以无线扩展的架构。而在ffmpeg这个架构上,才能有很多codec, decodec和mutex, io, protocol很好的共存处理。

很欣赏,能作这个完美架构的大师,只有这种完美的架构才能吸引更多的开源爱好着去研究ffmpeg的代码,并不断的丰富它!

(allen_young_yang)
本站文章除注明转载外,均为本站原创或编译欢迎任何形式的转载,但请务必注明出处,尊重他人劳动,同学习共成长。转载请注明:文章转载自:罗索实验室 [http://www.rosoo.net/a/201110/15135.html]
本文出处:blog.csdn.net/allen_young_yang 作者:allen_young_yang
顶一下
(2)
100%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片
栏目列表
将本文分享到微信
织梦二维码生成器
推荐内容