前言:

由于个人工作的原因,公司采用了这个作为点播直播服务器,采用这个的原因只有一个,因为开源免费t_0033.gif因为不要钱!因为不要钱!因为不要钱!因为很重要所以说三遍。如果不是这个原因,我也不会对此开源软件相识,作为创业公司当然是能省就省,买不起商业的点播直播软件,没人力做不了一个点播直播软件t_0029.gif已经用过大半年,各方面使用的情况来看还挺可以的,今日趁我写博客的激情还在遂再写一篇。

#本人的博客尽可能的多注释,注释不是给我看的,我都懂!t_0015.gif没错,就是给不会的现在在看我的博客小白们看的!如果发现还有不懂的地方请留言哪里不懂,然后我继续改到让蚂蚁的小大脑都懂的地步?蛤,这么多注释下你还不明白?t_0004.gif买块豆腐就去了吧!!不拦你!!!!!


先wait下!!!!!j_0004.gif此篇博客稍微写下就作废吧,因为我上github后发现,此开源软件已经死了,曾经的雄心壮志已经荡然无存。。。。。。白研究那么深了t_0012.gif

官方已经跪了,源码都清空了,我还往下写不???????

wKioL1dD7tfBhBBqAABzJkQQSLs764.jpg


1.SRS(simple-rtmp-server)介绍

SRS定位是运营级的互联网直播服务器集群,追求更好的概念完整性和最简单实现的代码。

  • 运营级:商业运营追求极高的稳定性,良好的系统对接,以及错误排查和处理机制。譬如日志文件格式,reload,系统HTTP接口,提供init.d脚本,转发,转码,边缘回多源站,都是根据CDN运营经验作为判断这些功能作为核心的依据。

  • 互联网:互联网最大的特征是变化,唯一不变的就是不断变化的客户要求,唯一不变的是基础结构的概念完整性和简洁性。互联网还意味着参与性,听取用户的需求和变更,持续改进和维护。

  • 直播服务器:直播和点播这两种截然不同的业务类型,导致架构和目标完全不一致,从运营的设备组,应对的挑战都完全不同。两种都支持只能说明没有重心,或者低估了代价。

  • 集群:FMS(AMS)的集群还是很不错的,虽然在运营容错很差。SRS支持完善的直播集群,Vhost分为源站和边缘,容错支持多源站切换、测速、可追溯日志等。

  • 概念完整性:虽然代码甚至结构都在变化,但是结构的概念完整性是一直追求的目标。从SRS服务器,P2P,ARM监控产业,MIPS路由器,服务器监控管理,ARM智能手机,SRS的规模不再是一个服务器而已。

  • 简单实现:对于过于复杂的实现,宁可不加入这个功能,也不牺牲前面提到的要求。对于已经实现的功能的代码,总会在一个版本release前给予充分的时间来找出最简答案。不求最高性能,最优雅,最牛逼,但求最简单易懂。

备注:概念完整性可以参考Brooks的相关文献,在宏观方面他还是很有造诣

SRS提供了丰富的接入方案将RTMP流接入SRS,包括推送RTMP到SRS推送RTSP/UDP/FLV到SRS拉取流到SRS。SRS还支持将接入的RTMP流进行各种变换,譬如将RTMP流转码流截图转发给其他服务器转封装成HTTP-FLV流转封装成HLS转封装成HDS录制成FLV。SRS包含支大规模集群如CDN业务的关键特性,譬如RTMP多级集群VHOST虚拟服务器无中断服务ReloadHTTP-FLV集群Kafka对接。此外,SRS还提供丰富的应用接口,包括HTTP回调安全策略SecurityHTTP API接口RTMP测速


2.SRS与其他媒体服务器比较

通过官方广告的描述,总而言之一句话,SRS超级强,无所不能秒杀其他同类媒体服务器

至于你们信不信?t_0012.gif我反正是信了!

Stream Delivery(流发送支持类型比较)

FeatureSRSNGINXCRTMPDFMSWOWZA
RTMPStableStableStableStableStable
HLSStableStableXStableStable
HDSExperimentXXStableStable
HTTP FLVStableXXXX
HLS(aonly)StableXXStableStable
HTTP ServerStableStableXXStable

Cluster(集群扩展支持比较)

FeatureSRSNGINXCRTMPDFMSWOWZA
RTMP EdgeStableXXStableX
RTMP BackupStableXXXX
VHOSTStableXXStableStable
ReloadStableXXXX
ForwardStableXXXX
ATCStableXXXX

Stream Service(流服务功能支持比较)

FeatureSRSNGINXCRTMPDFMSWOWZA
DVRStableStableXXStable
TranscodeStableXXXStable
HTTP APIStableStableXXStable
HTTP hooksStableXXXX
GopCacheStableXXStableX
SecurityStableStableXXStable
Token TraverseStableXXStableX

Efficiency(性能比较)

FeatureSRSNGINXCRTMPDFMSWOWZA
Concurrency(并发数)7.5k3k2k2k3k
MultipleProcess(多线程)ExperimentStableXXX
RTMP Latency(rtmp延迟)0.1s3s3s3s3s
HLS Latency(hls延迟)10s30sX30s30s

Stream Caster(流推送支持比较)

FeatureSRSNGINXCRTMPDFMSWOWZA
IngestStableXXXX
Push MPEGTSExperimentXXXStable
Push RTSPExperimentXStableXStable
Push HTTP FLVExperimentXXXX

Debug System(debug系统支持比较)

FeatureSRSNGINXCRTMPDFMSWOWZA
BW checkStableXXXX
Tracable LogStableXXXX

Docs(文档比较,有中文wiki,因为是国产开源软件)

FeatureSRSNGINXCRTMPDFMSWOWZA
DemosStableXXXX
WIKI(EN+CN)StableEN onlyXXStable

Others(其他比较)

FeatureSRSNGINXCRTMPDFMSWOWZA
ARM/MIPSStableStableXXX
Client LibraryStableXXXX


3.SRS的架构体系

SRS always use the most simple architecture to support complex transaction.

  • System arch: the system structure and arch.

  • Modularity arch: the main modularity of SRS.

  • Stream arch: the stream dispatch arch of SRS.

  • RTMP cluster arch: the RTMP origin and edge cluster arch.

  • Multiple processes arch (by wenjie): the multiple process of SRS.

  • CLI arch: the cli arch for SRS, api to manage SRS.

  • Bandwidth specification: the bandwidth test specification of SRS.

System Architecture

+------------------------------------------------------+
|             SRS(Simple RTMP Server)                  |
+---------------+---------------+-----------+----------+
|   API/hook    |   Transcoder  |    HLS    |   RTMP   |
|  http-parser  |  FFMPEG/x264  |  NGINX/ts | protocol |
+---------------+---------------+-----------+----------+
|              Network(state-threads)                  |
+------------------------------------------------------+
|      All Linux(RHEL,CentOS,Ubuntu,Fedora...)         |
+------------------------------------------------------+

Modularity Architecture

+------------------------------------------------------+
|             Main(srs/bandwidth/librtmp)              |
+------------------------------------------------------+
|           App(Server/Client application)             |
+------------------------------------------------------+
|               RTMP(Protocol stack)                   |
+------------------------------------------------------+
|      Kernel(depends on Core, provides error/log)     |
+------------------------------------------------------+
|         Core(depends only on system apis)            |
+------------------------------------------------------+

Stream Architecture

                   +---------+              +----------+
                  + Publish +              +  Deliver |
                  +---|-----+              +----|-----+
+----------------------+-------------------------+----------------+
|     Input            | SRS(Simple RTMP Server) |     Output     |
+----------------------+-------------------------+----------------+
|    Encoder(1)        |   +-> RTMP protocol ----+-> Flash Player |
|  (FMLE,FFMPEG, -rtmp-+->-+-> HLS/NGINX --------+-> m3u8 player  |
|  Flash,XSPLIT,       |   +-> Fowarder ---------+-> RTMP Server  |
|  ......)             |   +-> Transcoder -------+-> RTMP Server  |
|                      |   +-> DVR --------------+-> FILE         |
|                      |   +-> BandwidthTest ----+-> Flash/StLoad |
+----------------------+                         |                |
|  MediaSource(2)      |                         |                |
|  (RTSP,FILE,         |                         |                |
|   HTTP,HLS,    ------+->-- Ingester ----(rtmp)-+-> SRS          |
|   Device,            |                         |                |
|   ......)            |                         |                |
+----------------------+-------------------------+----------------+

Remark:
(1) Encoder: encoder must push RTMP stream to SRS server.
(2) MediaSource: any media source, which can be ingest by ffmpeg.
(3) Ingester: SRS will fork a process to run ffmpeg(or your application)
to ingest any input to rtmp, push to SRS.


4.SRS实战(测试服务器的ip为192.168.0.20)

官方已经跪了,官方的github源码都清空了,我还往下写不???????

(1)部署SRS提供hls服务(因为懒所以我编译的时候把所有功能先全开启了,你可以编译的时候只开启一个hls的服务)

#获取SRS
[root@ren tools]# git clone https://github.com/wenjiegit/srs
Initialized empty Git repository in /application/tools/srs/.git/
remote: Counting objects: 30921, done.
remote: Total 30921 (delta 0), reused 0 (delta 0), pack-reused 30921
Receiving objects: 100% (30921/30921), 156.81 MiB | 5.36 MiB/s, done.
Resolving deltas: 100% (23934/23934), done.
[root@ren tools]# ll
总用量 4
drwxr-xr-x 4 root root 4096 5月  24 15:21 srs
[root@ren tools]# cd srs/
[root@ren srs]# ll
总用量 96
-rw-r--r--  1 root root  1484 5月  24 15:21 AUTHORS.txt
-rw-r--r--  1 root root  1249 5月  24 15:21 DONATIONS.txt
-rw-r--r--  1 root root  1083 5月  24 15:21 LICENSE
-rwxr-xr-x  1 root root 81742 5月  24 15:21 README.md
drwxr-xr-x 12 root root  4096 5月  24 15:21 trunk
[root@ren srs]# cd trunk/
#进行编译安装
[root@ren trunk]# ./configure --prefix=/application/srs --full && make
#出了点小插曲,由于我没有事先创建这个我指定的根目录/application/srs,它默认给我装在了你编译文件目录下面。。233
##修改配置文件开启支持hls,想我这样写
[root@ren conf]# vim hls.conf 
# the config for srs to delivery hls
# @see https://github.com/o***s/srs/wiki/v1_CN_SampleHLS
# @see full.conf for detail config.

listen              1935;
max_connections     1000;
vhost __defaultVhost__ {
    hls {
        enabled         on;
        hls_fragment    10;
        hls_window      60;
        hls_path        /application/tools/srs/trunk/objs/nginx/html/;
        hls_m3u8_file   [app]/[stream].m3u8;
        hls_ts_file     [app]/[stream]/[04]:[05]-[seq].ts;
    }
}
#启动srs服务,首先启动srs里面已经内置的nginx服务
[root@ren ~]# /application/tools/srs/trunk/objs
/nginx/sbin/nginx
#指定刚才我们修改的配置文件进行启动真正的srs服务
[root@ren ~]# /application/tools/srs/trunk/objs/srs -c /application/tools/srs/trunk/conf/hls.conf 
[2016-05-24 15:58:15.730][trace][1909][0] XCORE-SRS/2.0.209(ZhouGuowen)
[2016-05-24 15:58:15.768][trace][1909][0] config parse complete
[2016-05-24 15:58:15.768][trace][1909][0] write log to file ./objs/srs.log
[2016-05-24 15:58:15.768][trace][1909][0] you can: tailf ./objs/srs.log
[2016-05-24 15:58:15.768][trace][1909][0] @see: 
####蛤蛤,出现了问题,你用绝对路径竟然无法启动程序!!!
#只能先cd进去然后启动程序。。。。。
[root@ren ~]# cd /application/tools/srs/trunk
[root@ren trunk]# ./objs/srs -c conf/hls.conf
[2016-05-24 16:00:41.955][trace][1926][0] XCORE-SRS/2.0.209(ZhouGuowen)
[2016-05-24 16:00:41.955][trace][1926][0] config parse complete
[2016-05-24 16:00:41.955][trace][1926][0] write log to file ./objs/srs.log
[2016-05-24 16:00:41.955][trace][1926][0] you can: tailf ./objs/srs.log
[2016-05-24 16:00:41.955][trace][1926][0] @see: https://github.com/o***s/srs/wiki/v1_CN_SrsLog
[root@ren trunk]# ps -ef|grep srs
root      1902     1  0 15:56 ?        00:00:00 nginx: master process /application/tools/srs/trunk/objs/nginx/sbin/nginx
root      1928     1  0 16:00 pts/0    00:00:00 ./objs/srs -c conf/hls.conf
root      1930  1885  0 16:00 pts/0    00:00:00 grep srs


已经部署成功,然后就可以推流上去进行测试了,如果看到这一步的还想往下做的人,请去弄一个推流的软件吧,省时省力,也可以用srs自带的推流软件进行推流。


1)软件推流

我们公司采用的是购买的纳加软件进行多机位推流j_0019.gif我去推一个试试,通过看目录的流名和视频播放可以看出已经成功了

wKiom1dD_zHgCb8gAACUlLIBtnA584.jpg

wKioL1dEACTBe5McAACRtZ7AXdU435.jpg

2)使用FFMPEG命令推流:

    for((;;)); do \
        ./objs/ffmpeg/bin/ffmpeg -re -i ./doc/source.200kbps.768x320.flv \
        -vcodec copy -acodec copy \
        -f flv -y rtmp://192.168.0.21/live/livestream; \
        sleep 1; \    done

生成的流地址为:

观看的流地址也就是上面生成的流地址


好了,完结了。官方已跪让我没有想继续写下去的欲望了。。。。2333

j_0008.gif