日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 运维知识 > Nginx >内容正文

Nginx

Nginx RTMP 功能研究

發(fā)布時間:2024/2/28 Nginx 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Nginx RTMP 功能研究 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

?Nginx-RTMP功能調(diào)研

1.?RTMP協(xié)議介紹...2

2.RTMP server.3

2.1當(dāng)前的流媒體server.3

2.2Wowza功能...3

3.Nginx-based RTMP server.5

3.1? Nginx rtmp 功能點...5

3.2編譯nginx rtmp模塊...6

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

3.4用nginx-rtmp-module搭建直播環(huán)境...8

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

?

1. RTMP協(xié)議介紹

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

它有三種變種:

1)工作在TCP之上的明文協(xié)議,使用端口1935

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

3)RTMPS類似RTMPT,但使用的是HTTPS連接;

?

RTMP協(xié)議就像一個用來裝數(shù)據(jù)包的容器,這些數(shù)據(jù)可以是AMF格式的數(shù)據(jù),也可以是FLV中的視/音頻數(shù)據(jù)。一個單一的連接可以通過不同的通道傳輸多路網(wǎng)絡(luò)流。這些通道中的包都是按照固定大小的包傳輸?shù)摹?/span>

?

?

? ?



? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?1???RTMP交互圖

?

更多協(xié)議的細(xì)節(jié)可以參見《rtmp specification 1.0

2. RTMP server

2.1當(dāng)前的流媒體server

?

現(xiàn)在主要有兩種rtmp server,商業(yè)的和開源的。商業(yè)的比開源的支持的功能多,個人根據(jù)需要選擇吧


? 商業(yè)的有FMS Wowza

? 開源RTMP server

? 1. ?red5?Java??有名

? 2. ?crtmpserver c++ 支持多種rtmp協(xié)議,移動設(shè)備以及IPTV相關(guān)網(wǎng)絡(luò)協(xié)議?http://www.rtmpd.com/?Erlyvideo erlong 有開源和商業(yè)版本 https//github.com/erlyvideo/erlyvideo h

?3. ?aXeVideo haXe 一個實驗性的,輕量級的服務(wù)器?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公司的產(chǎn)品,license非常昂貴。下面就wowza這個商業(yè)軟件講下流媒體Server,不僅僅是RTMP Server,在達(dá)到生產(chǎn)環(huán)境所需要的功能。wowza最突出的特定是多終端適應(yīng)性,這個在如今多媒體融合的網(wǎng)絡(luò)環(huán)境下有很強的實用意義。究其原理并無深入研究。國內(nèi)尚為視訊是其代理公司。

Wowza Media Server是一款高性能、多線程的流媒體服務(wù)器軟件。專為多種終端設(shè)備提供音視頻內(nèi)容播放服務(wù),支持iOS、windowsAndroid、BlackMerry等系統(tǒng)的終端設(shè)備,以HTTPRTSP/ RTPRTMP傳輸協(xié)議的流播放,還支持利用機頂盒進(jìn)行播放的IPTV。

?


通過對Wowza Media Server的深度定制開發(fā),尚為網(wǎng)絡(luò)視聽管理系統(tǒng)實現(xiàn)了對網(wǎng)絡(luò)音視頻內(nèi)容的采集、編碼、編輯、轉(zhuǎn)碼、審核、播出與監(jiān)控等完整的生命周期管理,突出了以下能力:

1)?自動化內(nèi)容收錄

  對網(wǎng)絡(luò)視頻的錄制,采用服務(wù)器端錄制模式,滿足從28Kbps10Mbps碼流范圍的H.264編碼流的自動化定時收錄。

2)?快速在線編輯

云計算架構(gòu)設(shè)計,服務(wù)器端處理。所有編輯任務(wù)都處在云端,對編輯機無要求,任務(wù)處理快捷簡單。

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

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

3)?云式轉(zhuǎn)碼

  采用云轉(zhuǎn)碼技術(shù)開發(fā)的音視頻文件轉(zhuǎn)碼、實時流轉(zhuǎn)碼,實現(xiàn)了不同格式、碼率、分辨率、幀數(shù)等參數(shù)的自由轉(zhuǎn)換,滿足不同終端的播放需求。

4)?多終端發(fā)布支持

  結(jié)合Wowza Media Server支持多協(xié)議多終端的特性,尚為網(wǎng)絡(luò)視聽系統(tǒng)一個平臺即可支持多終端設(shè)備的視頻訪問,無論用任何設(shè)備都可訪問所需的視頻內(nèi)容。

  在對多終端內(nèi)容統(tǒng)一管理的基礎(chǔ)之上,還可實現(xiàn)了多終端視頻的斷點續(xù)播功能。用戶的播放在一個終端上播放暫停后,用戶的信息、節(jié)目信息以及斷點信息立刻傳送給其它終端,實現(xiàn)了不同終端間的斷點續(xù)播。

5)?穩(wěn)定的安全保障

  全LINUX架構(gòu),保障系統(tǒng)的安全性。

  自動支持負(fù)載均衡部署,可滿足大并發(fā)用戶的數(shù)據(jù)響應(yīng)處理。

擴(kuò)展性好,可隨需增減流媒體服務(wù)器來變更系統(tǒng)承載的并發(fā)用戶數(shù)。

除此之外,個人認(rèn)為完善的DRM版權(quán)管理對于視頻直播分發(fā)是非常重要的功能,服務(wù)器狀態(tài)實時監(jiān)控是對運營的高效穩(wěn)定也有非常最要的功能。

3. Nginx-basedRTMP server

3.1? Nginx rtmp?功能點

1.?? 支持音視頻直播

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

3.?? 支持兩種流的分發(fā)模式 pushand pull

4.?? 可以將直播流錄制成flv文件

5.?? H264/AAC編碼

6.?? 支持在線轉(zhuǎn)碼 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.? 先進(jìn)內(nèi)存控制技術(shù),可以在使用少量內(nèi)存的情況下完成流暢的直播功能。

12 .?可以和以下協(xié)同工作。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是個比較輕量的項目,可以滿足直播的采集,編碼和分發(fā)工作,該項目豐富的完善了nginx對視頻的支持,特別的nginx-rtmp實現(xiàn)了對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.????配置支持訪問控制,你可以指定允許發(fā)布的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的情況下,可以將流分發(fā)到多個進(jìn)程進(jìn)行處理,充分利用服務(wù)器資源。

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

?

4. 流轉(zhuǎn)碼的功能,下面的例子使用了exec這種使用外部程序ffmpeg的功能.? Ffmpeg是一個強大的媒體處理工具,幾乎可以做任意有關(guān)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協(xié)助的情況下,輸出流可以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搭建直播環(huán)境

1.??????需要準(zhǔn)備視頻源,這個可以使用ffmpeg模擬,或者通過FMS采集。使用ffmpeg比較方便,例如ffmpeg -re -i /root/test123.flv?-f flv rtmp://192.168.100.135/myapp/test,這樣一條命令就可以發(fā)布一個直播源。

?

2.??????配置nginx-rtmp,完成相關(guān)功能配置,可以參考以上描述。

?

3.??????搭建客戶端測試環(huán)境,本文測試播放器使用的是jwplayer

?

?

https://github.com/arut/nginx-rtmp-module項目里面test目錄下有相關(guān)的播放器設(shè)置和配置文件。

?

配置文件

?

[html]?view plaincopy
  • worker_processes??1;??
  • ??
  • error_log??logs/error.log?debug;??
  • #error_log??logs/error.log??notice;??
  • #error_log??logs/error.log??info;??
  • ??
  • #pid????????logs/nginx.pid;??
  • ??
  • ??
  • events?{??
  • ????worker_connections??1024;??
  • }??
  • ??
  • ??
  • rtmp?{??
  • ??
  • ????server?{??
  • ??
  • ????????listen?1935;??
  • ??
  • ????????chunk_size?128;??
  • ??
  • ????????publish_time_fix?off;??
  • ??
  • ????????application?myapp?{???
  • ??
  • ????????????live?on;??
  • ??
  • ????????????record?keyframes;??
  • ?????????record_path?/tmp;??
  • ??
  • ????????????record_max_size?128K;??
  • ????????????record_interval?30s;??
  • ??
  • ????????????record_suffix?.flv;??
  • ??
  • ????????????on_publish?http://localhost:8080/publish;??
  • ????????????on_play?http://localhost:8080/play;??
  • ????????????on_record_done?http://localhost:8080/record_done;??
  • ????????}??
  • ??
  • ????????application?myapp2?{??
  • ????????????live?on;??
  • ????????}??
  • ??
  • #????????application?mypull?{??
  • #????????????live?on;??
  • #????????????pull?myapp?mystream?localhost;??
  • #????????}??
  • ??
  • #????????application?mypush?{??
  • #????????????live?on;??
  • #????????????push?myapp?mystream?localhost;??
  • #????????????push?myapp2?mystream?localhost;??
  • #????????}??
  • ????}??
  • }??
  • ??
  • ??
  • http?{??
  • ??
  • ????server?{??
  • ??
  • ????????listen??????8080;??
  • ??
  • ????????location?/publish?{??
  • ????????????return?201;??
  • ????????}??
  • ??
  • ????????location?/play?{??
  • ????????????return?202;??
  • ????????}??
  • ??
  • ????????location?/record_done?{??
  • ????????????return?203;??
  • ????????}??
  • ??
  • ????????location?/stat?{??
  • ????????????rtmp_stat?all;??
  • ????????????rtmp_stat_stylesheet?stat.xsl;??
  • ????????}??
  • ??
  • ????????location?/stat.xsl?{??
  • ????????????root?/home/rarutyunyan/nginx-rtmp-module/;??
  • ????????}??
  • ??
  • ????????location?/rtmp-publisher?{??
  • ????????????root?/home/rarutyunyan/nginx-rtmp-module/test;??
  • ????????}??
  • location?/?{??
  • ????????????root?/home/rarutyunyan/nginx-rtmp-module/test/www;??
  • ????????}??
  • ??
  • ??
  • ????}??
  • }??
  • ??
  • ??????????????????
  • ?

    測試用例:

    /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的流媒體網(wǎng)絡(luò)傳輸協(xié)議。是蘋果公司QuickTime X和iPhone軟件系統(tǒng)的一部分。它的工作原理是把整個流分成一個個小的基于HTTP的文件來下載,每次只下載一些。當(dāng)媒體流正在播放時,客戶端可以選擇從許多不同的備用源中以不同的速率下載同樣的資源,允許流媒體會話適應(yīng)不同的數(shù)據(jù)速率。在開始一個流媒體會話時,客戶端會下載一個包含元數(shù)據(jù)的extended M3U (m3u8) playlist文件,用于尋找可用的媒體流。

    HLS只請求基本的HTTP報文,與實時傳輸協(xié)議(RTP)不同,HLS可以穿過任何允許HTTP數(shù)據(jù)通過的防火墻或者代理服務(wù)器。它也很容易使用內(nèi)容分發(fā)網(wǎng)絡(luò)來傳輸媒體流。

    此協(xié)議詳細(xì)內(nèi)容請參考apple官方網(wǎng)站: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

    利用其他商業(yè)軟件也可以比如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小文件并且產(chǎn)生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:首先轉(zhuǎn)化成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可以直接轉(zhuǎn)化

    參考鏈接: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相關(guān)配置如下?????

    [html]?view plaincopy
  • application?hls?{??
  • ??
  • ???????????????hls?on;??
  • ??
  • ???????????????hls_path?/tmp/app;??
  • ??
  • ???????????????hls_fragment?10s;??
  • ??
  • ??????}??
  • ??
  • ??????location?/hls?{??
  • ??????#?Serve?HLS?fragments??
  • ??????????alias?/tmp/app;??
  • ??
  • ??????}??


  • 使用vlc測試。

    ?

    測試成功。

    總結(jié)

    以上是生活随笔為你收集整理的Nginx RTMP 功能研究的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。