协议介绍
SRS 支持 HLS/RTMP 两种成熟而且广泛应用的流媒体分发方式。
- RTMP 指 Adobe 的 RTMP ( Realtime Message Protocol ),广泛应用于低延时直播,也是编码器和服务器对接的实际标准协议,在 PC(Flash)上有最佳观看体验和最佳稳定性。
- HLS 指 Apple 的 HLS ( Http Live Streaming ),本身就是 Live(直播)的,不过 Vod(点播)也能支持。HLS 是 Apple 平台的标准流媒体协议,和 RTMP 在 PC 上一样支持得天衣无缝。
HLS 和 RTMP 两种分发方式,就可以支持所有的终端。RTMP 分发参考 基于 SRS 搭建 RTMP 直播流媒体服务器。
使用场景
- 简单:HLS 作为流媒体协议非常简单,Apple 支持得也很完善。Android 对 HLS 的支持也会越来越完善。
- 跨平台:PC 主要的直播方案是 RTMP,也有一些库能播放 HLS,譬如 JWPlayer,基于 OSMF 的 HLS 插件也一大堆。所以实际上如果选一种协议能跨 PC/Android/IOS,那就是 HLS。
- 友好的 CDN 分发方式:目前 CDN 对于 RTMP 也是基本协议,但是 HLS 分发的基础是 HTTP,所以 CDN 的接入和分发会比 RTMP 更加完善。能在各种 CDN 之间切换,RTMP 也能,只是可能需要对接测试。
- IOS 上苛刻的稳定性要求:IOS 上最稳定的当然是 HLS,稳定性不差于 RTMP 在 PC-flash 上的表现。
总之,SRS 支持 HLS 主要是作为输出的分发协议,直播以 RTMP+HLS 分发,满总各种应用场景;点播以 HLS 为主。
协议对比
分发 | 平台 | 协议 | 公司 | 说明 |
---|---|---|---|---|
RTMP | Windows Flash | RTMP | Adobe | 主流的低延时分发方式,Adobe对RTMP是Flash原生支持方式,FMS(Adobe Media Server前身),就是Flash Media Server的简写,可见Flash播放RTMP是多么“原生”,就像浏览器打开http网页一样“原生”,经测试,Flash播放RTMP流可以10天以上不间断播放。 |
HLS | Apple/Android | HTTP | Apple/Google | 延时一个切片以上(一般10秒以上),Apple平台上HLS的效果比PC的RTMP还要好,而且Apple所有设备都支持,Android最初不支持HLS,后来也支持了,但测试发现支持得还不如Apple,不过观看是没有问题,稳定性稍差,所以有些公司专门做Android上的流媒体播放器。 |
HDS | – | HTTP | Adobe | Adobe自己的HLS,协议方面做得是复杂而且没有什么好处,国内没有什么应用,传说国外有,SRS2已经支持。 |
dash | – | HTTP | – | Dynamic Adaptive Streaming over HTTP (DASH),为了对业界存在的多种自适应流技术进行规范,MEPG推出MEPG-DASH标准。旨在为动态自适应流媒体技术创造一种同一的协议标准,nginx-rtmp已经支持。 |
部署
基于滴滴云 DC2(IP:116.85.57.94)进行软件部署,使用 SRS 切片,使用 NGINX 分发 HLS。
第一步,获取 SRS。详细参考
1 2 3 |
[dc2-user@10-254-81-196 ~]$ git clone https://github.com/ossrs/srs [dc2-user@10-254-81-196 ~]$ cd srs/trunk |
第二步,编译 SRS。详细参考
1 2 |
[dc2-user@10-254-81-196 trunk]$ ./configure && make |
第三步,编写 SRS 配置文件。详细参考
将以下内容保存为文件 conf/srs.conf
,服务器启动时指定该配置文件 ( SRS 的 conf 文件夹有该文件):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
# conf/srs.conf listen 888; #默认端口为1935,由于滴滴云DC2安全组策略问题,选择已开放的端口888测试 max_connections 1000; srs_log_file ./objs/srs.log; vhost stream.didi.com { hls { enabled on; #是否开启HLS hls_path ./objs/nginx/html; hls_m3u8_file [app]/[stream].m3u8; hls_fragment 3; #指定ts切片的最小长度(单位:秒) hls_window 3; #指定HLS大小,即m3u8中ts文件的时长之和 } } |
第四步,启动 SRS。
1 2 3 4 5 6 7 |
[root@10-254-81-196 trunk]# ./objs/srs -c conf/srs.conf [2018-12-11 14:34:43.454][trace][16007][0] XCORE-SRS/2.0.258(ZhouGuowen) [2018-12-11 14:34:43.454][trace][16007][0] config parse complete [2018-12-11 14:34:43.454][trace][16007][0] write log to file ./objs/srs.log [2018-12-11 14:34:43.454][trace][16007][0] you can: tailf ./objs/srs.log [2018-12-11 14:34:43.454][trace][16007][0] @see: https://github.com/ossrs/srs/wiki/v1_CN_SrsLog |
第五步,获取、编译 NGINX。
- 获取 NGINX 包
1 2 |
[dc2-user@10-254-81-196 ~]$ wget http://nginx.org/download/nginx-1.12.2.tar.gz |
- 编译安装 NGINX
1 2 3 |
[dc2-user@10-254-81-196 ~]$ tar -zxvf nginx-1.12.2.tar.gz [dc2-user@10-254-81-196 ~]$ cd nginx-1.12.2 && ./configure && make && make install |
第六步,编写配置文件、启动 NGINX。
- 编写配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
[dc2-user@10-254-81-196 ~]$ vim nginx-1.12.2/conf/nginx.conf user root; worker_processes 1; error_log logs/error.log error; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; log_format main '$remote_addr - $remote_user [$time_local] "$request" ' access_log logs/access.log main; sendfile on; keepalive_timeout 65; server { listen 80; server_name localhost; location / { root html; index index.html index.htm; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } location ~* \.m3u8 { root /home/dc2-user/srs/trunk/objs/nginx/html/; add_header Access-Control-Allow-Origin *; } location ~* \.ts { root /home/dc2-user/srs/trunk/objs/nginx/html; add_header Access-Control-Allow-Origin *; } location ~* crossdomain.xml { root /usr/local/nginx/html/; } } } |
- 启动 NGINX
1 2 |
[dc2-user@10-254-81-196 ~]$ ./nginx-1.12.2/objs/nginx -c ../conf/nginx.conf |
第七步,启动推流编码器。
Linux 系统下可以使用 FFMPEG 进行推流;Windows/Ios 系统下可选择 OBS 进行推流。(本文我们使用 FFMPEG 进行推流演示)
- 获取 FFMPEG
1 2 3 |
[dc2-user@10-254-81-196 ~]$ git clone https://git.ffmpeg.org/ffmpeg.git ffmpeg [dc2-user@10-254-81-196 ~]$ cd ffmpeg |
- 编译 FFMPEG
1 2 3 |
[dc2-user@10-254-81-196 ffmpeg]$./configure && make 如果编译失败,请根据提示内容安装依赖环境或忽略。 |
- 使用 FFMPEG 推流
1 2 3 |
[dc2-user@10-254-81-196 ffmpeg]$ ./ffmpeg -re -i ../test.mp4 -f flv -vcodec copy -acodec copy -y rtmp://116.85.57.94:888/live?vhost=stream.didi.com/teststream |
第八步,观看 HLS 直播流。
HLS 播放地址为:http://116.85.57.94/live/teststream.m3u8
- 可使用软件 VLC 播放。
- 可使用 CUTV 在线播放器 播放。
本文作者:李洋