前言:
由於個人工作的原因,公司採用了這個作爲點播直播服務器,採用這個的原因只有一個,因爲開源免費因爲不要錢!因爲不要錢!因爲不要錢!因爲很重要所以說三遍。如果不是這個原因,我也不會對此開源軟件相識,作爲創業公司當然是能省就省,買不起商業的點播直播軟件,沒人力做不了一個點播直播軟件已經用過大半年,各方面使用的情況來看還挺可以的,今日趁我寫博客的激情還在遂再寫一篇。
#本人的博客儘可能的多註釋,註釋不是給我看的,我都懂!沒錯,就是給不會的現在在看我的博客小白們看的!如果發現還有不懂的地方請留言哪裏不懂,然後我繼續改到讓螞蟻的小大腦都懂的地步?蛤,這麼多註釋下你還不明白?買塊豆腐就去了吧!!不攔你!!!!!
先wait下!!!!!此篇博客稍微寫下就作廢吧,因爲我上github後發現,此開源軟件已經死了,曾經的雄心壯志已經蕩然無存。。。。。。白研究那麼深了
官方已經跪了,源碼都清空了,我還往下寫不???????
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虛擬服務器、無中斷服務Reload、HTTP-FLV集羣、Kafka對接。此外,SRS還提供豐富的應用接口,包括HTTP回調、安全策略Security、HTTP API接口、RTMP測速。
2.SRS與其他媒體服務器比較
通過官方廣告的描述,總而言之一句話,SRS超級強,無所不能秒殺其他同類媒體服務器
Stream Delivery(流發送支持類型比較)
Feature | SRS | NGINX | CRTMPD | FMS | WOWZA |
---|---|---|---|---|---|
RTMP | Stable | Stable | Stable | Stable | Stable |
HLS | Stable | Stable | X | Stable | Stable |
HDS | Experiment | X | X | Stable | Stable |
HTTP FLV | Stable | X | X | X | X |
HLS(aonly) | Stable | X | X | Stable | Stable |
HTTP Server | Stable | Stable | X | X | Stable |
Cluster(集羣擴展支持比較)
Feature | SRS | NGINX | CRTMPD | FMS | WOWZA |
---|---|---|---|---|---|
RTMP Edge | Stable | X | X | Stable | X |
RTMP Backup | Stable | X | X | X | X |
VHOST | Stable | X | X | Stable | Stable |
Reload | Stable | X | X | X | X |
Forward | Stable | X | X | X | X |
ATC | Stable | X | X | X | X |
Stream Service(流服務功能支持比較)
Feature | SRS | NGINX | CRTMPD | FMS | WOWZA |
---|---|---|---|---|---|
DVR | Stable | Stable | X | X | Stable |
Transcode | Stable | X | X | X | Stable |
HTTP API | Stable | Stable | X | X | Stable |
HTTP hooks | Stable | X | X | X | X |
GopCache | Stable | X | X | Stable | X |
Security | Stable | Stable | X | X | Stable |
Token Traverse | Stable | X | X | Stable | X |
Efficiency(性能比較)
Feature | SRS | NGINX | CRTMPD | FMS | WOWZA |
---|---|---|---|---|---|
Concurrency(併發數) | 7.5k | 3k | 2k | 2k | 3k |
MultipleProcess(多線程) | Experiment | Stable | X | X | X |
RTMP Latency(rtmp延遲) | 0.1s | 3s | 3s | 3s | 3s |
HLS Latency(hls延遲) | 10s | 30s | X | 30s | 30s |
Stream Caster(流推送支持比較)
Feature | SRS | NGINX | CRTMPD | FMS | WOWZA |
---|---|---|---|---|---|
Ingest | Stable | X | X | X | X |
Push MPEGTS | Experiment | X | X | X | Stable |
Push RTSP | Experiment | X | Stable | X | Stable |
Push HTTP FLV | Experiment | X | X | X | X |
Debug System(debug系統支持比較)
Feature | SRS | NGINX | CRTMPD | FMS | WOWZA |
---|---|---|---|---|---|
BW check | Stable | X | X | X | X |
Tracable Log | Stable | X | X | X | X |
Docs(文檔比較,有中文wiki,因爲是國產開源軟件)
Feature | SRS | NGINX | CRTMPD | FMS | WOWZA |
---|---|---|---|---|---|
Demos | Stable | X | X | X | X |
WIKI(EN+CN) | Stable | EN only | X | X | Stable |
Others(其他比較)
Feature | SRS | NGINX | CRTMPD | FMS | WOWZA |
---|---|---|---|---|---|
ARM/MIPS | Stable | Stable | X | X | X |
Client Library | Stable | X | X | X | X |
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)軟件推流
我們公司採用的是購買的納加軟件進行多機位推流我去推一個試試,通過看目錄的流名和視頻播放可以看出已經成功了
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
生成的流地址爲:
RTMP流地址爲:
rtmp://192.168.0.20/live/livestream
HLS流地址爲:
http://192.168.0.20/live/livestream.m3u8
觀看的流地址也就是上面生成的流地址
好了,完結了。官方已跪讓我沒有想繼續寫下去的慾望了。。。。2333