天天看點

基于 SRS+NG 搭建 HLS 直播流媒體伺服器

協定介紹

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。詳細參考

[[email protected] ~]$ git clone https://github.com/ossrs/srs
[[email protected] ~]$ cd srs/trunk
           

第二步,編譯 SRS。詳細參考

[[email protected] trunk]$ ./configure && make
           

第三步,編寫 SRS 配置檔案。詳細參考

将以下内容儲存為檔案

conf/srs.conf

,伺服器啟動時指定該配置檔案 ( SRS 的 conf 檔案夾有該檔案):

# 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。

[[email protected] 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 包
[[email protected] ~]$ wget http://nginx.org/download/nginx-1.12.2.tar.gz
           
  • 編譯安裝 NGINX
[[email protected] ~]$ tar -zxvf nginx-1.12.2.tar.gz
[[email protected] ~]$ cd nginx-1.12.2 && ./configure && make && make install 
           

第六步,編寫配置檔案、啟動 NGINX。

  • 編寫配置檔案
[[email protected] ~]$ 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
[[email protected] ~]$ ./nginx-1.12.2/objs/nginx -c ../conf/nginx.conf
           

第七步,啟動推流編碼器。

Linux 系統下可以使用 FFMPEG 進行推流;Windows/Ios 系統下可選擇 OBS 進行推流。(本文我們使用 FFMPEG 進行推流示範)

  • 擷取 FFMPEG
[[email protected] ~]$ git clone https://git.ffmpeg.org/ffmpeg.git ffmpeg
[[email protected] ~]$ cd ffmpeg
           
  • 編譯 FFMPEG
[[email protected] ffmpeg]$./configure && make
如果編譯失敗,請根據提示内容安裝依賴環境或忽略。
           
  • 使用 FFMPEG 推流
[[email protected] 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 線上播放器 播放。

繼續閱讀