当客户端推流RTMP数据发到SRS流媒体服务器,如果正确配置SRS流媒体服务器,可以输出HTTP-FLV的码流,拉流端就可以成功拉取到,那这个详细过程是怎样呢?本篇文章就来详细分析 。先回顾下整体的框架:
RTMP推流端-----》SRS流媒体服务器(建立SOURCE->生成Consumer->指定封装格式endoder=FLV) 《《--------------拉流客户端拉取HTTP-FLV
1.简述http-flv技术
(1)在http协议中有个content-length字段,指的是http的body的长度 。服务器在恢复客户端请求时,如果没有这个字段,客户端就一直接收数据,直到服务器与客户端的socket连接断开 。如果有这个字段,客户端接收这个长度的数据后,就认为数据传输完毕 。
http-flv直播就是利?了这个原理,服务器回复客户端请求的时候不加content-length字段,回复了http内容之后,紧接着发送flv数据,客户端就?直接收数据了 。客户端就会认为一直有数据接收 。
客户端发起请求,SRS流媒体服务器返回的是:
0 SrsLiveStream::SrsLiveStream (this=0xa3da40, s=0xa3bbd0, r=0xa3ad40, c=0xa3d520)at src/App/srs_app_http_stream.cpp:5141 0x00000000005010bb in SrsHttpStreamServer::http_mount (this=0xa11fd0, s=0xa3bbd0,r=0xa3ad40) at src/app/srs_app_http_stream.cpp:9122 0x00000000005620f5 in SrsHttpServer::http_mount (this=0xa11e00, s=0xa3bbd0,r=0xa3ad40) at src/app/srs_app_http_conn.cpp:3083 0x00000000004cd3cc in SrsServer::on_publish (this=0xa11ea0, s=0xa3bbd0, r=0xa3ad40)at src/app/srs_app_server.cpp:16084 0x00000000004e6a9b in SrsSource::on_publish (this=0xa3bbd0) at src/app/srs_app_source.cpp:24665 0x00000000004d89f2 in SrsRtmpConn::acquire_publish (this=0xa30d00,source=0xa3bbd0) at src/app/srs_app_rtmp_conn.cpp:9406 0x00000000004d7a74 in SrsRtmpConn::publishing (this=0xa30d00, source=0xa3bbd0) at src/app/srs_app_rtmp_conn.cpp:822#7 0x00000000004d5229 in SrsRtmpConn::stream_service_cycle (this=0xa30d00) atsrc/app/srs_app_rtmp_conn.cpp:534#8 0x00000000004d4141 in SrsRtmpConn::service_cycle (this=0xa30d00) atsrc/app/srs_app_rtmp_conn.cpp:388#9 0x00000000004d2f09 in SrsRtmpConn::do_cycle (this=0xa30d00) atsrc/app/srs_app_rtmp_conn.cpp:209#10 0x00000000004d10fb in SrsConnection::cycle (this=0xa30d78) atsrc/app/srs_app_conn.cpp:171#11 0x0000000000509c88 in SrsSTCoroutine::cycle (this=0xa30f90) atsrc/app/srs_app_st.cpp:198#12 0x0000000000509cfd in SrsSTCoroutine::pfn (arg=0xa30f90) atsrc/app/srs_app_st.cpp:213#13 0x00000000005bdd9d in _st_thread_main () at sched.c:337#14 0x00000000005be515 in st_thread_create (start=0x5bd719 <_st_vp_schedule+170>,arg=0x700000001, joinable=1,stk_size=1) at sched.c:616【SRS流媒体服务器之HTTP-FLV框架分析】(2)配置文件
主要分为两部分:
(1)配置http服务
(2)配置http-flv服务
配置?件如下所示:
listen 1935;max_connections 1000; #srs_log_tank file; #srs_log_file ./objs/srs.log; # 前台运? daemon off; # 打印到终端控制台 srs_log_tank console; http_api {enabled on;listen 1985; }http_server {enabled on;listen 8081;# http监听端? (1)配置的http服务器,注意端?,如果是云服务器?定要注意开 放相应端?dir ./objs/Nginx/html; }stats {network 0;disk sda sdb xvda xvdb;}vhost __defaultVhost__ {# 使?默认的vhost# hlshls {enabled on;hls_path ./objs/nginx/html;hls_fragment 10;hls_window 60;}# 使用http-flv要配置http_remux {enabled on;mount [vhost]/[app]/[stream].flv; # ?持flv的使?,flv拉流的地址hstrs on;}}
(3)测试准备
在客户端使用ffmpeg推rtmp流,其中xxx.xxx.xxx.xxx表示IP地址,根据实际环境的ip地址去配置,命令如下:
ffmpeg -re -i xxx.flv -vcodec copy -acodec copy -f flv -y rtmp://xxx.xxx.xxx.xxx/live/livestream在客户端拉取rtmp和http流,命令如下:
ffplay http://xxx.xxx.xxx.xxx:8081/live/livestream.flvffplay rtmp://xxx.xxx.xxx.xxx/live/livestream
2.SRS流媒体rtmp推流时的函数调用关系
RTMP推流的时候根据url,创建对应的handler,拉流的时候根据url,找到对应处理的handler 。即url和handler是一一对应关系 。以下流程,在RTMP推流时,创建了一个HTTP-FLV的SOURCE(函数调用关系是从下至上,即数字14到0),关于SOURCE的详细分析,前面文章也分析过 。
0 SrsLiveStream::SrsLiveStream (this=0xa3da40, s=0xa3bbd0, r=0xa3ad40, c=0xa3d520)at src/app/srs_app_http_stream.cpp:5141 0x00000000005010bb in SrsHttpStreamServer::http_mount (this=0xa11fd0, s=0xa3bbd0,r=0xa3ad40) at src/app/srs_app_http_stream.cpp:9122 0x00000000005620f5 in SrsHttpServer::http_mount (this=0xa11e00, s=0xa3bbd0,r=0xa3ad40) at src/app/srs_app_http_conn.cpp:3083 0x00000000004cd3cc in SrsServer::on_publish (this=0xa11ea0, s=0xa3bbd0, r=0xa3ad40)at src/app/srs_app_server.cpp:16084 0x00000000004e6a9b in SrsSource::on_publish (this=0xa3bbd0) at src/app/srs_app_source.cpp:24665 0x00000000004d89f2 in SrsRtmpConn::acquire_publish (this=0xa30d00,source=0xa3bbd0) at src/app/srs_app_rtmp_conn.cpp:9406 0x00000000004d7a74 in SrsRtmpConn::publishing (this=0xa30d00, source=0xa3bbd0) at src/app/srs_app_rtmp_conn.cpp:822#7 0x00000000004d5229 in SrsRtmpConn::stream_service_cycle (this=0xa30d00) atsrc/app/srs_app_rtmp_conn.cpp:534#8 0x00000000004d4141 in SrsRtmpConn::service_cycle (this=0xa30d00) atsrc/app/srs_app_rtmp_conn.cpp:388#9 0x00000000004d2f09 in SrsRtmpConn::do_cycle (this=0xa30d00) atsrc/app/srs_app_rtmp_conn.cpp:209#10 0x00000000004d10fb in SrsConnection::cycle (this=0xa30d78) atsrc/app/srs_app_conn.cpp:171#11 0x0000000000509c88 in SrsSTCoroutine::cycle (this=0xa30f90) atsrc/app/srs_app_st.cpp:198#12 0x0000000000509cfd in SrsSTCoroutine::pfn (arg=0xa30f90) atsrc/app/srs_app_st.cpp:213#13 0x00000000005bdd9d in _st_thread_main () at sched.c:337#14 0x00000000005be515 in st_thread_create (start=0x5bd719 <_st_vp_schedule+170>,arg=0x700000001, joinable=1,stk_size=1) at sched.c:616
推荐阅读
- 建立数据中转服务器的详细方法
- 阿里云、腾讯云、等云服务器有什么区别
- 网站服务器遇到CC攻击时可能发生的两大误区
- frp远程办公,访问内网服务器
- 互联网云服务器JVM监控——JDK自带工具
- 4 个打造多媒体和共享服务器的开源工具
- Linux服务器集群系统——LVS集群中的IP负载均衡技术
- CentOS系统下PXE服务器的搭建与部署
- 5分钟搭建公网https网页文件服务器,免费权威TLS证书
- 一个比 Nginx 功能更强大的 Web 服务器
