nginx+rtmp實現視頻直播

Nginx搭建rtmp流媒體服務器

 (20131020日星期日)

 

1       功能:... 1

2       代碼下載:... 1

3       編譯:... 2

3.1             Linux下:... 2

3.2           windows下:... 2

3.2.1         官網提供的編譯方法:... 2

3.2.1.1     預備:... 2

3.2.1.2     編譯步驟:... 2

3.2.2         Cygwin下:... 3

4       Windows下的限制:... 3

5       訪問URL格式:... 3

6       配置:... 3

6.1             點播配置:3

6.2             直播配置:... 5

 

1        功能:

1.1         視頻、音頻直播流(Live streaming of video/audio

1.2         Flvmp4視頻需求,從本地或HTTP播放(Video ondemand FLV/MP4, playing from local filesystem or HTTP

1.3         支持分佈式流中繼:推或拉模式(Stream relay support for distributed streaming: push & pullmodels

1.4         錄製FLVS流(Recording streams in multiple FLVs

1.5         支持H264/AAC

1.6         在線用ffmpeg轉碼(Online transcoding with FFmpeg

1.7         HLS (HTTP Live Streaming) 支持

1.8         基於http的操作(發佈、播放、錄製)(HTTP callbacks (publish/play/record/update etc)

1.9         在一定的事件中執行應用程序(Running external programs on certain events (exec)

1.10      HTTP control module forrecording audio/video and dropping clients

1.11      高級內存管理技術,快速流和低內存佔用(Advanced buffering techniques to keep memory allocations at aminimum level for faster streaming and low memory footprint

1.12      實踐證明可以在下列產品中工作: Wirecast,FMS,Wowza, JWPlayer,FlowPlayer,StrobeMediaPlayback,ffmpeg,avconv,rtmpdump,flvstreamer and many more

1.13      Statistics in XML/XSL in machine-& human- readable form

1.14      Linux/FreeBSD/MacOS/Windows

2        代碼下載:

Nginx:

官網:http://nginx.org/

下載:hg clone http://hg.nginx.org/nginx

nginx-rtmp-module:

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

下載:git clone https://github.com/arut/nginx-rtmp-module.git

3        編譯:

3.1         Linux下:

進入nginx源碼根目錄:

auto/configure--add-module=<path-to-nginx-rtmp-module>

make -f objs/Makefile

make -f objs/Makefile install

 

增加:http_ssl_module:

auto/configure--add-module=<path-to-nginx-rtmp-module> --with-http_ssl_module

3.2   windows下:

3.2.1 官網提供的編譯方法:

http://nginx.org/en/docs/howto_build_on_win32.html

3.2.1.1 預備:

l  Microsoft Visual C compiler.Microsoft Visual Studio® 8 and 10

l  Msys

l  Perl(如果要編譯OpenSSL並且nginx支持SLL),例如:ActivePerl 或 Strawberry Perl.

l  PCRE,zlib and OpenSSLlibraries sources.

3.2.1.2    編譯步驟:

l  設置MSYSPerlVC環境變量

運行vc設置環境變量腳本vcvarsall.bat(默認安裝路徑:C:\Program Files\MicrosoftVisual Studio 9.0\VC)

l  開始MSYS

l  建立編譯和庫目錄

mkdir objs

mkdir objs/lib

cd objs/lib

tar -xzf ../../pcre-8.32.tar.gz

tar -xzf ../../zlib-1.2.7.tar.gz

tar -xzf ../../openssl-1.0.1e.tar.gz

l  運行配置腳本:

auto/configure --with-cc=cl --builddir=objs--prefix= \

--conf-path=conf/nginx.conf--pid-path=logs/nginx.pid \

--http-log-path=logs/access.log--error-log-path=logs/error.log \

--sbin-path=nginx.exe--http-client-body-temp-path=temp/client_body_temp \

--http-proxy-temp-path=temp/proxy_temp \

--http-fastcgi-temp-path=temp/fastcgi_temp\

--with-cc-opt=-DFD_SETSIZE=1024--with-pcre=objs/lib/pcre-8.32 \

--with-zlib=objs/lib/zlib-1.2.7--with-openssl=objs/lib/openssl-1.0.1e \

--with-select_module --with-http_ssl_module--with-ipv6

運行腳本:

nmake –f objs/Makefile

3.2.2 Cygwin下:

linux

4        Windows下的限制:

Windows下下列功能不支持:

l  execs

l  static pulls

l  auto_push

 

windows下用的是select模式:

events{

    worker_connections  30; #默認爲(1024),windows默認爲64,所以需要修改此值小於64

}

 

5        訪問URL格式:

rtmp://rtmp.example.com/app[/name]

app-應該是配置文件中的application

name-可以爲空

6        配置:

在源碼要目下有個test目錄。其中有測試配置文件和腳本。

6.1         點播配置:

l  環境:

服務器IP爲:192.168.10.4

安裝根路徑:/usr/local/nginx

 

在你的配置時,需要把IP換成你的服務器IP或域名。

l  建立媒體文件夾:

mkdir /usr/local/nginx/vod/flvs

l  準備媒體文件:

把媒體文件 a.flv 複製到 /usr/local/nginx/vod/flvs目錄下。

l  準備網頁

下載jwplayer:http://www.longtailvideo.com/players/jw-flv-player/

下載後,解壓到:/usr/local/nginx/html/

建立測試頁面test.html,也放到上面目錄下。

<html>

         <head>

                   <scriptsrc="/jwplayer/jwplayer.js"></script>

         </head>

         <body>

                   <divid='my-video'></div>

                   <scripttype='text/javascript'>

                            jwplayer('my-video').setup({

                                     file:'rtmp://192.168.10.4/vod/a.flv',    

                                     width:'50%',

                                     aspectratio:'3:2',

                                     fallback:'false',

                                     primary:'flash'  

                                     });

                   </script>

         </body>

</html>

l  在nginx中配置http

#配置事件工作線程數

events {

   worker_connections  30; #默認爲(1024),windows默認爲64,所以需要修改此值小於64

}

#配置http

http {

   include       mime.types;

   default_type application/octet-stream;

 

    #配置訪問web

   server {

       listen       80;

       server_name  localhost;

 

       #charset koi8-r;

 

       #access_log logs/host.access.log  main;

 

       location / {

           root   html;

           index  index.html index.htm;

       }

 

       #error_page  404              /404.html;

 

       # redirect server error pages to the static page /50x.html

       #

       error_page   500 502 503 504  /50x.html;

       location = /50x.html {

           root   html;

       }

   }

  

   #配置rtmp狀態頁

  server {

       listen      8080;

 

       location /stat {

           rtmp_stat all;

           rtmp_stat_stylesheet stat.xsl;

       }

 

       location /stat.xsl {

           root /usr/local/nginx/nginx-rtmp-module/;  #nginx-rtmp-module源碼根目錄

       }

   }

}

#rtmp點播配置

rtmp {

   server {

        listen 1935;

        chunk_size 4000;

        application  vod {

                play /usr/local/nginx/vod/flvs;  #點播媒體文件存放目錄

        }

    }

}

l  啓動nginx

./nginx

l  從客戶端訪問網頁:

http://192.168.10.4/test.html

l  訪問rtmp狀態頁(非必須):

http://192.168.10.4:8080/stat

 

6.2         直播配置:

l   

l  在配置文件nginx.conf中加入

#rtmp直播配置

rtmp {

   server {

        listen 1935;

        chunk_size 4000;

        application  live {

                   live on;

        }

    }

}

 

#配置訪問web

http {

    include       mime.types;

   default_type application/octet-stream;

 

#配置訪問web

   server {

       listen       80;

       server_name  localhost;

 

       #charset koi8-r;

 

       #access_log logs/host.access.log  main;

 

       location / {

           root   html;

           index  index.html index.htm;

       }

 

       #error_page  404              /404.html;

 

       # redirect server error pages to the static page /50x.html

       #

       error_page   500 502 503 504  /50x.html;

        location = /50x.html {

           root   html;

       }

 

#配置rtmp狀態頁

   server {

       listen      8080;

 

       location /stat {

           rtmp_stat all;

           rtmp_stat_stylesheet stat.xsl;

       }

 

       location /stat.xsl {

           root /usr/local/nginx/nginx-rtmp-module/;  #nginx-rtmp-module源碼根目錄

       }

}

}

l  訪問rtmp狀態頁(非必須):

http://192.168.10.4:8080/stat

可以查看rtmp流的統計情況

l  準備網頁

下載jwplayer:http://www.longtailvideo.com/players/jw-flv-player/

下載後,解壓到:/usr/local/nginx/html/

建立測試頁面test.html,也放到上面目錄下。

<html>

         <head>

                   <scriptsrc="/jwplayer/jwplayer.js"></script>

         </head>

         <body>

                   <divid='my-video'></div>

                   <scripttype='text/javascript'>

                            jwplayer('my-video').setup({

                               file:'rtmp://192.168.0.128/live/test', #liveapplicatioin,test是直播緩存流文件

                                     width:'50%',

                                     aspectratio:'3:2',

                                     fallback:'false',

                                     primary:'flash'  

                                     });

                   </script>

         </body>

</html>

l  啓動服務

nginx

l  推流

ffmpeg產生一個模擬直播源,向rtmp服務器推送

ffmpeg –i/usr/local/nginx/vod/flvs/a.flv-strict -2 -c:v libx264 -c:a aac -f flv rtmp://192.168.10.4/live/test

注意,源文件必須是H.264+AAC編碼的。

192.168.10.4是運行nginx的服務器IPl

iveapplicatioin,

test是直播緩存流文件,需要與配置文件中的直播緩存文件名一樣。

 

l  從客戶端訪問網頁:

http://192.168.10.4/test.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章