您的位置:首页 > 运维架构 > Nginx

nginx + rtmp 搭建流媒体服务器

2015-07-19 18:08 471 查看

NginxRTMP功能研究

分类:视频流媒体nginx2012-12-2618:0931125人阅读评论(2)收藏举报目录(?)[+]Nginx-RTMP功能调研1.RTMP协议介绍...22.RTMPserver.32.1当前的流媒体server.32.2Wowza功能...33.Nginx-basedRTMPserver.53.1Nginxrtmp功能点...53.2编译nginxrtmp模块...63.3配置以及功能介绍...63.4用nginx-rtmp-module搭建直播环境...83.5Nginxrtmp对于HLS支持...13

1.RTMP协议介绍

RTMP(RealTimeMessagingProtocol)实时消息传送协议是AdobeSystems公司为Flash播放器和服务器之间音频、视频和数据传输开发的私有协议。它有三种变种:1)工作在TCP之上的明文协议,使用端口1935;2)RTMPT封装在HTTP请求之中,可穿越防火墙;3)RTMPS类似RTMPT,但使用的是HTTPS连接;RTMP协议就像一个用来装数据包的容器,这些数据可以是AMF格式的数据,也可以是FLV中的视/音频数据。一个单一的连接可以通过不同的通道传输多路网络流。这些通道中的包都是按照固定大小的包传输的。
图1RTMP交互图更多协议的细节可以参见《rtmpspecification1.0》

2.RTMPserver

2.1当前的流媒体server

现在主要有两种rtmpserver,商业的和开源的。商业的比开源的支持的功能多,个人根据需要选择吧商业的有FMSWowza开源RTMPserver1.red5java有名2.crtmpserverc++支持多种rtmp协议,移动设备以及IPTV相关网络协议http://www.rtmpd.com/Erlyvideoerlong有开源和商业版本https//github.com/erlyvideo/erlyvideoh3.aXeVideohaXe一个实验性的,轻量级的服务器http://code.google.com/p/haxevideo/4.FluorineFx.NetTobedefinedhttp://www/fluorinefx.com5.nginx-rtmpcnginx模块支持rtmp和HLShttps://github.com/arut/nginx-rtmp-module

2.2Wowza功能

FMS是Adobe公司的产品,license非常昂贵。下面就wowza这个商业软件讲下流媒体Server,不仅仅是RTMPServer,在达到生产环境所需要的功能。wowza最突出的特定是多终端适应性,这个在如今多媒体融合的网络环境下有很强的实用意义。究其原理并无深入研究。国内尚为视讯是其代理公司。WowzaMediaServer是一款高性能、多线程的流媒体服务器软件。专为多种终端设备提供音视频内容播放服务,支持iOS、windows、Android、BlackMerry等系统的终端设备,以HTTP、RTSP/RTP、RTMP传输协议的流播放,还支持利用机顶盒进行播放的IPTV。通过对WowzaMediaServer的深度定制开发,尚为网络视听管理系统实现了对网络音视频内容的采集、编码、编辑、转码、审核、播出与监控等完整的生命周期管理,突出了以下能力:1)自动化内容收录  对网络视频的录制,采用服务器端录制模式,满足从28Kbps到10Mbps码流范围的H.264编码流的自动化定时收录。2)快速在线编辑云计算架构设计,服务器端处理。所有编辑任务都处在云端,对编辑机无要求,任务处理快捷简单。  对视频的处理包括切条、合并、去除广告、添加logo等操作。  支持同一素材多格式文件的一次性编辑处理,提高工作效率。3)云式转码  采用云转码技术开发的音视频文件转码、实时流转码,实现了不同格式、码率、分辨率、帧数等参数的自由转换,满足不同终端的播放需求。4)多终端发布支持  结合WowzaMediaServer支持多协议多终端的特性,尚为网络视听系统一个平台即可支持多终端设备的视频访问,无论用任何设备都可访问所需的视频内容。  在对多终端内容统一管理的基础之上,还可实现了多终端视频的断点续播功能。用户的播放在一个终端上播放暂停后,用户的信息、节目信息以及断点信息立刻传送给其它终端,实现了不同终端间的断点续播。5)稳定的安全保障  全LINUX架构,保障系统的安全性。  自动支持负载均衡部署,可满足大并发用户的数据响应处理。扩展性好,可随需增减流媒体服务器来变更系统承载的并发用户数。除此之外,个人认为完善的DRM版权管理对于视频直播分发是非常重要的功能,服务器状态实时监控是对运营的高效稳定也有非常最要的功能。

3.Nginx-basedRTMPserver

3.1Nginxrtmp功能点

1.支持音视频直播2.支持flv/mp4视频格式,输入可以是文件或者HTTP流3.支持两种流的分发模式pushandpull4.可以将直播流录制成flv文件5.H264/AAC编码6.支持在线转码OnlinetranscodingwithFFmpeg7.支持HLS(HTTPLiveStreaming)需要libavformat(>=8.53.31.100)fromffmpeg(ffmpeg.org)8.HTTPcallbacks(publish/play/record/updateetc)9.支持外部程序(exec)10.HTTPcontrolmoduleforrecordingaudio/videoanddroppingclients11.先进内存控制技术,可以在使用少量内存的情况下完成流畅的直播功能。12.可以和以下协同工作。FMSserver(Wirecast,FMS,Wowza,)Player(JWPlayer,FlowPlayer,StrobeMediaPlayback,)外部程序(ffmpeg,avconv,rtmpdump,flvstreamer)13.StatisticsinXML/XSLinmachine-&human-readableform14.支持跨平台Linux/FreeBSD/MacOS项目地址https://github.com/arut/nginx-rtmp-modulenginx-rtmp-moduleDirectiveshttps://github.com/arut/nginx-rtmp-module/wiki/Directives总的来说Nginx-RTMP是个比较轻量的项目,可以满足直播的采集,编码和分发工作,该项目丰富的完善了nginx对视频的支持,特别的nginx-rtmp实现了对HLS的支持。

3.2编译nginxrtmp模块

编译安装,和其他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。
applicationlive{
liveon;
}
2.配置支持访问控制,你可以指定允许发布的IP以及允许收看的IP:
applicationlive{
liveon;
allowpublish127.0.0.1;
denypublishall;
allowplayall;
3.支持Multi-workerstreaming工作方式,需要指令rtmp_auto_pushon支持,这样nginx有多个worker的情况下,可以将流分发到多个进程进行处理,充分利用服务器资源。
rtmp_auto_pushon;
rtmp{
server{
listen1935;
chunk_size4000;
#TVmode:onepublisher,manysubscribers
applicationmytv{
liveon;
}
}
}
4.流转码的功能,下面的例子使用了exec这种使用外部程序ffmpeg的功能.Ffmpeg是一个强大的媒体处理工具,几乎可以做任意有关video/audio的处理。更多ffmpeg与RTMP流媒体连接用法可以参考以下链接。http://www.chinavideo.org/viewthread.php?tid=15423
#Transcoding(ffmpegneeded)
applicationbig{
liveon;
#Multipleexeclinescanbespecified.
exec/usr/bin/ffmpeg-re-irtmp://localhost:1935/$app/$name-vcodecflv-acodeccopy-s32x32-fflvrtmp://localhost:1935/small/${name};
}
applicationsmall{
liveon;
#Videowithreducedresolutioncomesherefromffmpeg
}
5.Push和pull模式,用在多个server协助的情况下,输出流可以push,输入流可以pull。
applicationmypush{
liveon;
#Everystreampublishedhere
#isautomaticallypushedto
#thesetwomachines
pushrtmp1.example.com;
pushrtmp2.example.com:1934;
}
applicationmypull{
liveon;
#Pullallstreamsfromremotemachine
#andplaylocally
pullrtmp://rtmp3.example.compageUrl=www.example.com/index.html;
}

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

1.需要准备视频源,这个可以使用ffmpeg模拟,或者通过FMS采集。使用ffmpeg比较方便,例如ffmpeg-re-i/root/test123.flv-fflvrtmp://192.168.100.135/myapp/test,这样一条命令就可以发布一个直播源。2.配置nginx-rtmp,完成相关功能配置,可以参考以上描述。3.搭建客户端测试环境,本文测试播放器使用的是jwplayerhttps://github.com/arut/nginx-rtmp-module项目里面test目录下有相关的播放器设置和配置文件。配置文件[html]viewplaincopyworker_processes1;error_loglogs/error.logdebug;#error_loglogs/error.lognotice;#error_loglogs/error.loginfo;#pidlogs/nginx.pid;events{worker_connections1024;}rtmp{server{listen1935;chunk_size128;publish_time_fixoff;applicationmyapp{liveon;recordkeyframes;record_path/tmp;record_max_size128K;record_interval30s;record_suffix.flv;on_publishhttp://localhost:8080/publish;on_playhttp://localhost:8080/play;on_record_donehttp://localhost:8080/record_done;}applicationmyapp2{liveon;}#applicationmypull{#liveon;#pullmyappmystreamlocalhost;#}#applicationmypush{#liveon;#pushmyappmystreamlocalhost;#pushmyapp2mystreamlocalhost;#}}}http{server{listen8080;location/publish{return201;}location/play{return202;}location/record_done{return203;}location/stat{rtmp_statall;rtmp_stat_stylesheetstat.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.htmlhttp://192.168.100.135:8080/rtmp-publisher/player.html效果:支持的配置指令有很多,没有一一研究。参见:https://github.com/arut/nginx-rtmp-module/wiki/Directives

max_streams

syntax:max_streamsvaluecontext:rtmp,serverSetsmaximumnumberofRTMPstreams.Datastreamsaremultiplexedintoasingledatastream.Differentchannelsareusedforsendingcommands,audio,videoetc.Defaultvalueis32whichisusuallyokformanycases.

exec

Syntax:execcommandarg*Context:rtmp,server,applicationSpecifiesexternalcommandwithargumentstobeexecutedoneverystreampublished.Whenpublishingstopstheprocessisterminated.Fullpathtobinaryshouldbespecifiedasthefirstargument.Therearenoassumptionsaboutwhatthisprocessshoulddo.Howeverthisfeatureisusefulwithffmpegforstreamtranscoding.FFmpegissupposedtoconnecttonginx-rtmpasaclientandoutputtranscodedstreambacktonginx-rtmpaspublisher.Substitutionsofform$var/${var}canbeusedwithincommandline:·$name-streamname·$app-applicationname·$addr-clientaddress·$flashver-clientflashversion·$swfurl-clientswfurl·$tcurl-clienttcurl·$pageurl-clientpageurlThefollowingffmpegcalltranscodesincomingstreamtoHLS-readystream(H264/AAC).FFmpegshouldbecompiledwithlibx264&libfaacsupportforthisexampletowork.

3.5Nginxrtmp对于HLS支持

HTTPLiveStreaming(缩写是HLS)是一个由苹果公司提出的基于HTTP的流媒体网络传输协议。是苹果公司QuickTimeX和iPhone软件系统的一部分。它的工作原理是把整个流分成一个个小的基于HTTP的文件来下载,每次只下载一些。当媒体流正在播放时,客户端可以选择从许多不同的备用源中以不同的速率下载同样的资源,允许流媒体会话适应不同的数据速率。在开始一个流媒体会话时,客户端会下载一个包含元数据的extendedM3U(m3u8)playlist文件,用于寻找可用的媒体流。HLS只请求基本的HTTP报文,与实时传输协议(RTP)不同,HLS可以穿过任何允许HTTP数据通过的防火墙或者代理服务器。它也很容易使用内容分发网络来传输媒体流。此协议详细内容请参考apple官方网站:https://developer.apple.com/resources/http-streaming/搭建HLSserver方式有以下三种:1.利用appleSDK,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直播的支持。HTTPLiveStreaming(HLS)isanHTTP-basedmediastreamingcommunicationsprotocolimplementedbyAppleInc.Ifyouareinterestedinanyofthefollowing:·StreamingaudioorvideotoiPhone,iPodtouch,iPad,orAppleTV·Streamingliveeventswithoutspecialserversoftware·Sendingvideoondemandwithencryptionandauthentication图5HLS(HTTPLiveStreaming)使用nginx-rtmp完成hls支持,需要额外的segmenter支持,该工具可以将文件分割成ts小文件并且产生m3u8列表。项目地址:https://github.com/johnf/m3u8-segmenter。下载编译只需要编译m3u8-segmenter.c即可,需要libc2.5以上支持。l流化可以支持以下两种。第一种尚未找到具体方法,下面的实例是采用第二种基于文件的。StreamSegmenterStreamSegmenterreadslivebroadcastfromnetwork(normallyudpprotocol)andpublishHTTPLiveStreamingintotheinternet.ItreadstheTransportStreamfromthenetworkanddividesitintoaseriesofsmallmediafilesofequalduration.Eventhougheachsegmentisinaseparatefile,videofilesaremadefromacontinuousstreamwhichcanbereconstructedseamlessly.Thesegmenteralsocreatesanindexfilecontainingreferencestotheindividualmediafiles.Eachtimethesegmentercompletesanewmediafile,theindexfileisupdated.Theindexisusedtotracktheavailabilityandlocationofthemediafiles.Mediasegmentsaresavedas.tsfiles(MPEG-2transportstreamfiles).Indexfilesaresavedas.M3U8playlists.FileSegmenterFileSegmenterallowsyoutousealibraryofexistingaudioandvideofilesforsendingvideoondemandviaHTTPLiveStreaming.TheFileSegmenterperformsthesametasksastheStreamSegmenter,butittakesfilesasinputinsteadofstreams.OurFileSegmentersupportsMP4,TS,MOV,FLVandsomeotherfileformats.Ifyoualreadyhaveamediafileencodedusingsupportedcodecs(H.264+AACorH.264+MP3),youneedn'ttore-encodeit,otherwise,youneedtore-encodethevideooraudio.TheFileSegmenterhastwoworkmode:re-encodeandno-re-encode.1.使用ffmpeg+segmenter:首先转化成ts文件,然后分割ffmpeg-loglevelquiet-icctv1.ts-fmpegts-|segmenter-i--d10-p/tmp/app/big_buck_bunny-m/tmp/app/big_buck.m3u8-uhttp://inodes.org/hls/2.高版本的ffmpeg可以直接转化参考链接:http://1.richitec.sinaapp.com/?p=64ffmpeg-itest456.mp4-fsegment-segment_time10-segment_formatmpegts-segment_listlist_file.m3u8-codeccopy-bsf:vh264_mp4toannexb-map0output_file-%d.tsHLS相关配置如下[html]viewplaincopyapplicationhls{hlson;hls_path/tmp/app;hls_fragment10s;}location/hls{#ServeHLSfragmentsalias/tmp/app;}使用vlc测试。测试成功。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: