【轉載】 Nginx-RTMP功能調研

 Nginx-RTMP功能調研php

轉自http://blog.csdn.net/cccallen/article/details/8440191/html

1. RTMP協議介紹...2java

2.RTMP server.3node

2.1當前的流媒體server.3nginx

2.2Wowza功能...3c++

3.Nginx-based RTMP server.5git

3.1  Nginx rtmp 功能點...5github

3.2編譯nginx rtmp模塊...6redis

3.3配置以及功能介紹...6windows

3.4用nginx-rtmp-module搭建直播環境...8

3.5Nginx rtmp對於HLS支持...13

 

1. RTMP協議介紹

RTMP(Real Time Messaging Protocol)實時消息傳送協議是Adobe Systems公司爲Flash播放器和服務器之間音頻、視頻和數據傳輸開發的私有協議。

它有三種變種:

1)工做在TCP之上的明文協議,使用端口1935;

2)RTMPT封裝HTTP請求之中,可穿越防火牆

3)RTMPS相似RTMPT,但使用的是HTTPS鏈接;

 

RTMP協議就像一個用來裝數據包的容器,這些數據能夠是AMF格式的數據,也能夠是FLV中的視/音頻數據。一個單一的鏈接能夠經過不一樣的通道傳輸多路網絡流。這些通道中的包都是按照固定大小的包傳輸的。

 

 

   

 

 

                                           圖1   RTMP交互圖

 

更多協議的細節能夠參見《rtmp specification 1.0》

2. RTMP server

2.1當前的流媒體server

 

如今主要有兩種rtmp server,商業的和開源的。商業的比開源的支持的功能多,我的根據須要選擇吧

 

  商業的有FMS Wowza

  開源RTMP server

  1.  red5 java  有名

  2.  crtmpserver c++ 支持多種rtmp協議,移動設備以及IPTV相關網絡協議 http://www.rtmpd.com/ Erlyvideo erlong 有開源和商業版本 https//github.com/erlyvideo/erlyvideo h

 3.  aXeVideo haXe 一個實驗性的,輕量級的服務器 http://code.google.com/p/haxevideo/ 

 4. FluorineFx .Net To be defined http://www/fluorinefx.com 

 5. nginx-rtmp c nginx模塊 支持rtmp和HLS https://github.com/arut/nginx-rtmp-module

2.2 Wowza功能

 

FMS是Adobe公司的產品,license很是昂貴。下面就wowza這個商業軟件講下流媒體Server,不只僅是RTMP Server,在達到生產環境所須要的功能。wowza最突出的特定是多終端適應性,這個在現在多媒體融合的網絡環境下有很強的實用意義。究其原理並沒有深刻研究。國內尚爲視訊是其代理公司。

Wowza Media Server是一款高性能、多線程的流媒體服務器軟件。專爲多種終端設備提供音視頻內容播放服務,支持iOS、windows、Android、BlackMerry等系統的終端設備,以HTTP、RTSP/ RTP、RTMP傳輸協議的流播放,還支持利用機頂盒進行播放的IPTV。

 

 

經過對Wowza Media Server的深度定製開發,尚爲網絡視聽管理系統實現了對網絡音視頻內容的採集、編碼、編輯、轉碼、審覈、播出與監控等完整的生命週期管理,突出瞭如下能力:

1) 自動化內容收錄

  對網絡視頻的錄製,採用服務器端錄製模式,知足從28Kbps到10Mbps碼流範圍的H.264編碼流的自動化定時收錄。

2) 快速在線編輯

雲計算架構設計,服務器端處理。全部編輯任務都處在雲端,對編輯機無要求,任務處理快捷簡單。

  對視頻的處理包括切條、合併、去除廣告、添加logo等操做。

  支持同一素材多格式文件的一次性編輯處理,提升工做效率。

3) 雲式轉碼

  採用雲轉碼技術開發的音視頻文件轉碼、實時流轉碼,實現了不一樣格式、碼率、分辨率、幀數等參數的自由轉換,知足不一樣終端的播放需求。

4) 多終端發佈支持

  結合Wowza Media Server支持多協議多終端的特性,尚爲網絡視聽系統一個平臺便可支持多終端設備的視頻訪問,不管用任何設備均可訪問所需的視頻內容。

  在對多終端內容統一管理的基礎之上,還可實現了多終端視頻的斷點續播功能。用戶的播放在一個終端上播放暫停後,用戶的信息、節目信息以及斷點信息馬上傳送給其它終端,實現了不一樣終端間的斷點續播。

5) 穩定的安全保障

  全LINUX架構,保障系統的安全性。

  自動支持負載均衡部署,可知足大併發用戶的數據響應處理。

擴展性好,可隨需增減流媒體服務器來變動系統承載的併發用戶數。

除此以外,我的認爲完善的DRM版權管理對於視頻直播分發是很是重要的功能,服務器狀態實時監控是對運營的高效穩定也有很是最要的功能。

3. Nginx-basedRTMP server

3.1  Nginx rtmp 功能點

1.   支持音視頻直播

2.   支持flv/mp4視頻格式,輸入能夠是文件或者HTTP流

3.   支持兩種流的分發模式 pushand pull

4.   能夠將直播流錄製成flv文件

5.   H264/AAC編碼

6.   支持在線轉碼 Onlinetranscoding with FFmpeg

7.   支持HLS (HTTP LiveStreaming)須要 libavformat (>= 8. 53.31.100) from ffmpeg (ffmpeg.org)

8.   HTTPcallbacks (publish/play/record/update etc)

9.   支持外部程序(exec)

10.  HTTPcontrol module for recording audio/video and dropping clients

11.  先進內存控制技術,能夠在使用少許內存的狀況下完成流暢的直播功能。

12 . 能夠和如下協同工做。FMS server(Wirecast, FMS, Wowza,)Player(JWPlayer, FlowPlayer, StrobeMediaPlayback,)外部程序(ffmpeg,avconv,rtmpdump,flvstreamer )

13. Statistics in XML/XSL in machine- & human- readable form

14.  支持跨平臺 Linux/FreeBSD/MacOS

 

項目地址

https://github.com/arut/nginx-rtmp-module

 

nginx-rtmp-module Directives

https://github.com/arut/nginx-rtmp-module/wiki/Directives

 

總的來講Nginx-RTMP是個比較輕量的項目,能夠知足直播的採集,編碼和分發工做,該項目豐富的完善了nginx對視頻的支持,特別的nginx-rtmp實現了對HLS的支持。

3.2 編譯nginx rtmp模塊

編譯安裝,和其餘nginx模塊同樣

./configure--add-module=/path/to/nginx-rtmp-module

對於HLS的支持,須要額外的一個模塊 .

./configure--add-module=/path/to/nginx-rtmp-module--add-module=/path/to/nginx-rtmp-module/hls

 

 

3.3 配置以及功能介紹

RTMP 直播的通常格式是rtmp://youdomain.com/app/name,其中app的名字對於application的名字,

1.      下列是直播的配置,app是live。

application live {
 
    live on;
}
 
 
 

2.    配置支持訪問控制,你能夠指定容許發佈的IP以及容許收看的IP:

application live {
 
    live on;
 
    allow publish 127.0.0.1;
    deny publish all;
    allow play all;

3.   支持Multi-worker streaming工做方式,須要指令rtmp_auto_push on支持,這樣nginx有多個worker的狀況下,能夠將流分發到多個進程進行處理,充分利用服務器資源。

rtmp_auto_push on;
 
rtmp {
 
    server {
 
        listen 1935;
 
        chunk_size 4000;
 
        # TV mode: one publisher, many subscribers
        application mytv {
            live on;
        }
    }
}

 

4. 流轉碼的功能,下面的例子使用了exec這種使用外部程序ffmpeg的功能.  Ffmpeg是一個強大的媒體處理工具,幾乎能夠作任意有關video/audio的處理。更多ffmpeg與RTMP流媒體鏈接用法能夠參考如下連接。

http://www.chinavideo.org/viewthread.php?tid=15423

 

# Transcoding (ffmpeg needed)
application big {
            live on;
            # Multiple exec lines can be specified.
            exec /usr/bin/ffmpeg -re -i rtmp://localhost:1935/$app/$name -vcodec flv -acodec copy -s 32x32 -f flv rtmp://localhost:1935/small/${name};
}
application small {
            live on;
            # Video with reduced resolution comes here from ffmpeg
}
 

 

5.Push和pull模式,用在多個server協助的狀況下,輸出流能夠push,輸入流能夠pull。

 

 
        application mypush {
            live on;
 
            # Every stream published here
            # is automatically pushed to
            # these two machines
            push rtmp1.example.com;
            push rtmp2.example.com:1934;
        }
 
        application mypull {
            live on;
 
            # Pull all streams from remote machine
            # and play locally
            pull rtmp://rtmp3.example.com pageUrl=www.example.com/index.html;
        }
 

 

3.4 用nginx-rtmp-module搭建直播環境

1.      須要準備視頻源,這個可使用ffmpeg模擬,或者經過FMS採集。使用ffmpeg比較方便,例如ffmpeg -re -i /root/test123.flv -f flv rtmp://192.168.100.135/myapp/test,這樣一條命令就能夠發佈一個直播源。

 

2.      配置nginx-rtmp,完成相關功能配置,能夠參考以上描述。

 

3.      搭建客戶端測試環境,本文測試播放器使用的是jwplayer

 

 

https://github.com/arut/nginx-rtmp-module項目裏面test目錄下有相關的播放器設置和配置文件。

 

配置文件

 

[html]  view plain copy
 
  1. worker_processes  1;  
  2.   
  3. error_log  logs/error.log debug;  
  4. #error_log  logs/error.log  notice;  
  5. #error_log  logs/error.log  info;  
  6.   
  7. #pid        logs/nginx.pid;  
  8.   
  9.   
  10. events {  
  11.     worker_connections  1024;  
  12. }  
  13.   
  14.   
  15. rtmp {  
  16.   
  17.     server {  
  18.   
  19.         listen 1935;  
  20.   
  21.         chunk_size 128;  
  22.   
  23.         publish_time_fix off;  
  24.   
  25.         application myapp {   
  26.   
  27.             live on;  
  28.   
  29.             record keyframes;  
  30.          record_path /tmp;  
  31.   
  32.             record_max_size 128K;  
  33.             record_interval 30s;  
  34.   
  35.             record_suffix .flv;  
  36.   
  37.             on_publish http://localhost:8080/publish;  
  38.             on_play http://localhost:8080/play;  
  39.             on_record_done http://localhost:8080/record_done;  
  40.         }  
  41.   
  42.         application myapp2 {  
  43.             live on;  
  44.         }  
  45.   
  46. #        application mypull {  
  47. #            live on;  
  48. #            pull myapp mystream localhost;  
  49. #        }  
  50.   
  51. #        application mypush {  
  52. #            live on;  
  53. #            push myapp mystream localhost;  
  54. #            push myapp2 mystream localhost;  
  55. #        }  
  56.     }  
  57. }  
  58.   
  59.   
  60. http {  
  61.   
  62.     server {  
  63.   
  64.         listen      8080;  
  65.   
  66.         location /publish {  
  67.             return 201;  
  68.         }  
  69.   
  70.         location /play {  
  71.             return 202;  
  72.         }  
  73.   
  74.         location /record_done {  
  75.             return 203;  
  76.         }  
  77.   
  78.         location /stat {  
  79.             rtmp_stat all;  
  80.             rtmp_stat_stylesheet stat.xsl;  
  81.         }  
  82.   
  83.         location /stat.xsl {  
  84.             root /home/rarutyunyan/nginx-rtmp-module/;  
  85.         }  
  86.   
  87.         location /rtmp-publisher {  
  88.             root /home/rarutyunyan/nginx-rtmp-module/test;  
  89.         }  
  90. location / {  
  91.             root /home/rarutyunyan/nginx-rtmp-module/test/www;  
  92.         }  
  93.   
  94.   
  95.     }  
  96. }  
  97.   
  98.                   

 

 

測試用例:

/home/rarutyunyan/nginx-rtmp-module/test/rtmp-publisher

/home/rarutyunyan/nginx-rtmp-module/test/www

 

測試URL:

http://192.168.100.135:8080/index.html

http://192.168.100.135:8080/rtmp-publisher/player.html

 

 

 

 

 

支持的配置指令有不少,沒有一一研究。參見:

https://github.com/arut/nginx-rtmp-module/wiki/Directives

max_streams

syntax: max_streams value
context: rtmp, server

Setsmaximum number of RTMP streams. Data streams are multiplexed into a single datastream. Different channels are used for sending commands, audio, video etc.Default value is 32 which is usually ok for many cases.

exec

Syntax: exec command arg*
Context: rtmp, server, application

Specifiesexternal command with arguments to be executed on every stream published. Whenpublishing stops the process is terminated. Full path to binary should bespecified as the first argument. There are no assumptions about what thisprocess should do. However this feature is useful with ffmpeg for streamtranscoding. FFmpeg is supposed to connect to nginx-rtmp as a client and outputtranscoded stream back to nginx-rtmp as publisher. Substitutions of form$var/${var} can be used within command line:

·        $name - stream name

·        $app - application name

·        $addr - client address

·        $flashver - client flashversion

·        $swfurl - client swf url

·        $tcurl - client tc url

·        $pageurl - client page url

Thefollowing ffmpeg call transcodes incoming stream to HLS-ready stream(H264/AAC). FFmpeg should be compiled with libx264 & libfaac support forthis example to work.

 

3.5 Nginx rtmp對於HLS支持

HTTP Live Streaming(縮寫是 HLS)是一個由蘋果公司提出的基於HTTP的流媒體網絡傳輸協議。是蘋果公司QuickTime X和iPhone軟件系統的一部分。它的工做原理是把整個流分紅一個個小的基於HTTP的文件來下載,每次只下載一些。當媒體流正在播放時,客戶端能夠選擇從許多不一樣的備用源中以不一樣的速率下載一樣的資源,容許流媒體會話適應不一樣的數據速率。在開始一個流媒體會話時,客戶端會下載一個包含元數據的extended M3U (m3u8) playlist文件,用於尋找可用的媒體流。

HLS只請求基本的HTTP報文,與實時傳輸協議(RTP)不一樣,HLS能夠穿過任何容許HTTP數據經過的防火牆或者代理服務器。它也很容易使用內容分發網絡來傳輸媒體流。

此協議詳細內容請參考apple官方網站:https://developer.apple.com/resources/http-streaming/

 

搭建HLS server方式有如下三種:

1.      利用apple SDK,

2.      利用adobe 的fms,4.5版本支持hls,能夠參考,

http://www.adobe.com/products/flash-media-streaming/features._sl_id-contentfilter_sl_featuredisplaytypes_sl_new.html

利用其餘商業軟件也能夠好比wowza。

3.    一種是利用opensouce.Nginx-rtmp。

下面主要是Nginx-RTMP對HLS直播的支持。

 

HTTP Live Streaming (HLS)is an HTTP-based media streaming communications protocol implemented by AppleInc.
If you are interested in any of the following:

·        Streaming audio or video toiPhone, iPod touch, iPad, or Apple TV

·        Streaming live events withoutspecial server software

·        Sending video on demand withencryption and authentication

圖5 HLS(HTTP Live Streaming)

使用nginx-rtmp完成hls支持,須要額外的segmenter支持,該工具能夠將文件分割成 ts小文件而且產生m3u8列表。項目地址:https://github.com/johnf/m3u8-segmenter

 

下載編譯只須要編譯m3u8-segmenter.c便可,須要libc2.5以上支持。l

流化能夠支持如下兩種。第一種還沒有找到具體方法,下面的實例是採用第二種基於文件的。

Stream Segmenter

Stream Segmenter reads live broadcast from network(normally udpprotocol) and publish HTTP Live Streaming into the internet. It reads theTransport Stream from the network and divides it into a series of small mediafiles of equal duration. Even though each segment is in a separate file, videofiles are made from a continuous stream which can be reconstructed seamlessly.

The segmenter also creates an index file containing references tothe individual media files. Each time the segmenter completes a new media file,the index file is updated. The index is used to track the availability andlocation of the media files. 
Media segments are saved as .ts files (MPEG-2 transport stream files). Indexfiles are saved as .M3U8 playlists.

File Segmenter

File Segmenter allows you to use a library of existing audio andvideo files for sending video on demand via HTTP Live Streaming. The FileSegmenter performs the same tasks as the Stream Segmenter, but it takes filesas input instead of streams.

Our File Segmenter supports MP4,TS, MOV, FLV and some otherfile formats. If you already have a media file encoded using supportedcodecs(H.264 + AAC or H.264 + MP3), you needn't to re-encode it, otherwise, youneed to re-encode the video or audio. The File Segmenter has two work mode:re-encode and no-re-encode.

 

 

1.      使用ffmpeg + segmenter:首先轉化成ts文件,而後分割

ffmpeg -loglevel quiet  -i cctv1.ts -f mpegts - | segmenter -i - -d 10 -p /tmp/app/big_buck_bunny -m/tmp/app/big_buck.m3u8 -u http://inodes.org/hls/

 

2.       高版本的ffmpeg能夠直接轉化

參考連接:http://1.richitec.sinaapp.com/?p=64

 

ffmpeg -i test456.mp4 -f  segment -segment_time 10  -segment_format mpegts -segment_listlist_file.m3u8 -codec copy -bsf:v h264_mp4toannexb -map 0 output_file-%d.ts

 

HLS相關配置以下     

 

[html]  view plain copy
 
  1. application hls {  
  2.   
  3.                hls on;  
  4.   
  5.                hls_path /tmp/app;  
  6.   
  7.                hls_fragment 10s;  
  8.   
  9.       }  
  10.   
  11.       location /hls {  
  12.       # Serve HLS fragments  
  13.           alias /tmp/app;  
  14.   
  15.       }  



 

使用vlc測試。

 

 

測試成功。

相關文章
相關標籤/搜索