Nginx RTMP 功能研究

Nginx RTMP 功能研究
 
 
     php

Nginx-RTMP功能调研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测试。

 

 

测试成功。

相关文章
相关标签/搜索