利用nginx搭建http和rtmp協議的流媒體服務器javascript
實驗目的:讓Nginx支持flv和mp4格式文件,同時支持Rtmp協議;同時打開rtmp的hls功能
資料:
HTTPLive Streaming(縮寫是HLS)是一個由蘋果公司提出的基於HTTP的流媒體網絡傳輸協議。
HLS只請求基本的HTTP報文,與實時傳輸協議(RTP)不一樣,HLS能夠穿過任何容許HTTP數據經過的防火牆或者代理服務器。它也很容易使用內容分發網絡來傳輸媒體流。
使用ffmpeg來完成對flv、mp四、mp3等格式的轉化(點播實驗暫時不測試)
1、準備工做
模塊:nginx_mod_h264_streaming(支持h264編碼的視頻)
模塊:http_flv_module支持flv
模塊:http_mp4_module支持mp4
下載地址:
http://h264.code-shop.com/download/nginx_mod_h264_streaming-2.2.7.tar.gz
http://nginx.org
https://github.com/arut/nginx-rtmp-module
一、安裝依賴包:
#yum-y install gcc glibc glibc-devel make nasm pkgconfig lib-devel openssl-develexpat-devel gettext-devel libtool mhash.x86_64perl-Digest-SHA1.x86_64
二、安裝git工具:
#mkdirsoft-source
#cdsoft-source
#wgethttp://www.codemonkey.org.uk/projects/git-snapshots/git/git-latest.tar.gz
#tarxzvf git-latest.tar.gz
#cdgit-2013-02-04
#autoconf
#./configure
#make&& make install
# git--version
gitversion 1.8.1.GIT
#cd ..
三、安裝ffmpeg及其依賴包:
++++++++Yasm+++++++++++
#wgethttp://www.tortall.net/projects/yasm/releases/yasm-1.2.0.tar.gz
#tarxzvf yasm-1.2.0.tar.gz
#cdyasm-1.2.0
#./configure
#make
#makeinstall
#cd ..
++++++++x264+++++++++++
#gitclone git://git.videolan.org/x264
#cdx264
#./configure--enable-shared
#make
#makeinstall
#cd ..
++++++++LAME+++++++++++
#wgethttp://downloads.sourceforge.net/project/lame/lame/3.99/lame-3.99.5.tar.gz
#tarxzvf lame-3.99.5.tar.gz
#cdlame-3.99.5
#./configure--enable-nasm
#make
#makeinstall
#cd ..
++++++++libogg+++++++++++
#wgethttp://downloads.xiph.org/releases/ogg/libogg-1.3.0.tar.gz
#tarxzvf libogg-1.3.0.tar.gz
#cdlibogg-1.3.0
#./configure
#make
#makeinstall
#cd ..
++++++++libvorbis+++++++++++
#wgethttp://downloads.xiph.org/releases/vorbis/libvorbis-1.3.3.tar.gz
#tarxzvf libvorbis-1.3.3.tar.gz
#cdlibvorbis-1.3.3
#./configure
#make
#makeinstall
#cd ..
++++++++libvpx+++++++++++
#gitclone http://git.chromium.org/webm/libvpx.git
#cdlibvpx
#./configure --enable-shared
#make
#makeinstall
#cd ..
++++++++FAAD2+++++++++++
#wgethttp://downloads.sourceforge.net/project/faac/faad2-src/faad2-2.7/faad2-2.7.tar.gz
#tarzxvf faad2-2.7.tar.gz
#cdfaad2-2.7
#./configure
#make
#makeinstall
#cd ..
++++++++FAAC+++++++++++
#wgethttp://downloads.sourceforge.net/project/faac/faac-src/faac-1.28/faac-1.28.tar.gz
#tarzxvf faac-1.28.tar.gz
#cdfaac-1.28
#./configure
#make
#makeinstall
#cd ..css
編譯FAAC-1.28時遇到錯誤:html
mpeg4ip.h:126:error:new declaration ‘char*strcasestr(const char*, const char*)’java
解決方法:node
從123行開始修改此文件mpeg4ip.h,到129行結束。
修改前:
#ifdef __cplusplus
extern "C" {
#endif
char *strcasestr(const char *haystack, const char *needle);
#ifdef __cplusplus
}
#endif
修改後:
#ifdef __cplusplus
extern "C++" {
#endif
constchar *strcasestr(const char*haystack, const char *needle);
#ifdef __cplusplus
}
#endifnginx
++++++++Xvid+++++++++++
#wgethttp://downloads.xvid.org/downloads/xvidcore-1.3.2.tar.gz
#tarzxvf xvidcore-1.3.2.tar.gz
#cdxvidcore/build/generic
#./configure
#make
#makeinstall
cd ..
#gitclone git://source.ffmpeg.org/ffmpeg
#cdffmpeg
#./configure --prefix=/opt/ffmpeg/ --enable-version3 --enable-libvpx --enable-libfaac--enable-libmp3lame --enable-libvorbis --enable-libx264 --enable-libxvid--enable-shared --enable-gpl --enable-postproc --enable-nonfree --enable-avfilter --enable-pthreads
#make&& make install
#cd ..
修改/etc/ld.so.conf以下:
includeld.so.conf.d/*.conf
/lib
/lib64
/usr/lib
/usr/lib64
/usr/local/lib
/usr/local/lib64
/opt/ffmpeg/lib
#ldconfiggit
因爲暫時沒有找到合適版本rtmpmodel ,因此,這裏只是作了flv和MP4的,紅色部分暫時不用添加
2、安裝Nginx相關模塊
#tarzxvf nginx_mod_h264_streaming-2.2.7.tar.gz
#gitclone git://github.com/arut/nginx-rtmp-module.git
#tarzxvf pcre-8.12.tar.gz
#cdpcre-8.12
#./configure
#make&& make install
#tarzxvf nginx-1.2.6.tar.gz
#cdnginx-1.2.6
#./configure--prefix=/usr/local/nginx --add-module=../nginx_mod_h264_streaming-2.2.7--with-http_flv_module --with-http_gzip_static_module--with-http_stub_status_module --with-http_mp4_module --add-module=../nginx-rtmp-module--add-module=../nginx-rtmp-module/hls --with-cc-opt=-I/opt/ffmpeg/include--with-ld-opt='-L/opt/ffmpeg/lib -Wl,-rpath=/opt/ffmpeg/lib'
#make&& make install
3、修改nginx主配置文件,配置虛擬主機(這裏咱們暫時只配置和測試點播,直播請看借鑑地址):
user nobody nobody;
worker_processes4;
error_log logs/nginx_error.log info;
pid logs/nginx.pid;
worker_rlimit_nofile51200;
events
{
use epoll;
worker_connections 51200;
}
github
http
{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 50m ;
limit_conn_zone $binary_remote_addr zone=perip:256k;
limit_conn_log_level notice;
sendfile on;
tcp_nopush on;
keepalive_timeout 6000;#測試併發臨時調大
tcp_nodelay on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plainapplication/x-javascript text/css application/xml;
gzip_vary on;
#log format
log_format main '$remote_addr - $remote_user[$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
#支持flv
server
{
listen 80 ;
server_name 192.168.0.33;
root index.htmlweb
limit_rate_after5m; 服務器
limit_rate512k;
location ~ \.flv$ {
root /media/nginx; -視頻存放地點
flv;
}
location ~ \.mp4$ {
root /media/nginx;
flv;
}
access_log logs/nginxflv_access.log main;
}
下載並安裝yamdi
[root@flv ~]# wgethttp://cdnetworks-kr-2.dl.sourceforge.net/ \
>project/yamdi/yamdi/1.8/yamdi-1.8.tar.gz
[root@flv ~]# tar -zxvf yamdi-1.8.tar.gz
yamdi-1.8
yamdi-1.8/CHANGES
yamdi-1.8/LICENSE
yamdi-1.8/Makefile
yamdi-1.8/Makefile.mingw32
yamdi-1.8/README
yamdi-1.8/man1
yamdi-1.8/yamdi.c
yamdi-1.8/man1/yamdi.1
[root@ flv ~]# cd yamdi-1.8
[root@ flv yamdi-1.8]# make
gcc -O2 -Wall yamdi.c -o yamdi
[root@ flv yamdi-1.8]# make install
install -m 0755 -o root yamdi/usr/local/bin
使用yamdi
這裏咱們準備個文件,以便後面使用:
[root@flv html]# yamdi -i 62664.flv -o 7345.flv |
簡單的說一下,-i表示輸入文件,在這裏輸入文件爲62664.flv,即它是沒有添加過關鍵幀的文件;-o表示輸出文件,在這裏是7345.flv,它是添加過關鍵幀的,在對這兩文件的訪問中會發現,播放62664.flv是不能實現拖動操做的,而7345.flv則能夠。
下載並設置JW player
JW player是一個開源的FLV播放器,它如今也支持MP4。
[root@flv ~]# wget http://www.longtailvideo.com/jw/upload/mediaplayer-viral.zip [root@flv ~]# unzip mediaplayer-viral.zip Archive: mediaplayer-viral.zip creating: mediaplayer-5.7-viral/ inflating: mediaplayer-5.7-viral/JW Player Quick Start Guide.pdf inflating: mediaplayer-5.7-viral/jwplayer.js inflating: mediaplayer-5.7-viral/license.txt inflating: mediaplayer-5.7-viral/player.swf inflating: mediaplayer-5.7-viral/preview.jpg inflating: mediaplayer-5.7-viral/readme.html inflating: mediaplayer-5.7-viral/swfobject.js inflating: mediaplayer-5.7-viral/video.mp4 |
注意點
注意在解壓包中,player.swf和jwplayer.js是咱們須要的文件,將其拷貝到Nginx的web目錄下:
[root@flv ~]#cd mediaplayer-5.7-viral/ [root@mail mediaplayer-5.7-viral]# cp jwplayer.js player.swf /usr/local/nginx-1.0.2-flv/html/ |
到如今爲止,FLV服務器已經架設完畢。
咱們訪問測試一下看:
訪問方法:
http://flv.xx.com/player.swf?type=http&file=7345.flv
↑訪問協議 ↑FLV服務器地址 ↑播放器名稱↑http分發方式↑訪問的文件名
---------------------------------------------------------
實驗目的:讓Nginx支持flv和mp4格式文件,同時支持Rtmp協議;同時打開rtmp的hls功能
資料:
HTTP Live Streaming(縮寫是 HLS)是一個由蘋果公司提出的基於HTTP的流媒體 網絡傳輸協議。
HLS只請求基本的HTTP報文,與實時傳輸協議(RTP)不一樣,HLS能夠穿過任何容許HTTP數據經過的防火牆或者代理服務器。它也很容易使用內容分發網絡來傳輸媒體流。
使用ffmpeg來完成對flv、mp四、mp3等格式的轉化(點播實驗暫時不測試)
1、準備工做
模塊:nginx_mod_h264_streaming(支持h264編碼的視頻)
模塊:http_flv_module 支持flv
模塊:http_mp4_module 支持mp4
下載地址:
http://h264.code-shop.com/download/nginx_mod_h264_streaming-2.2.7.tar.gz
http://nginx.org
https://github.com/arut/nginx-rtmp-module
一、安裝依賴包:
#yum -y install gcc glibc glibc-devel make nasm pkgconfig lib-devel openssl-devel expat-devel gettext-devel libtool mhash.x86_64 perl-Digest-SHA1.x86_64
二、安裝git工具:
#mkdir soft-source
#cd soft-source
#wget http://www.codemonkey.org.uk/projects/git-snapshots/git/git-latest.tar.gz
#tar xzvf git-latest.tar.gz
#cd git-2013-02-04
#autoconf
#./configure
#make && make install
# git --version
git version 1.8.1.GIT
#cd ..
三、安裝ffmpeg及其依賴包:
++++++++Yasm+++++++++++
#wget http://www.tortall.net/projects/yasm/releases/yasm-1.2.0.tar.gz
#tar xzvf yasm-1.2.0.tar.gz
#cd yasm-1.2.0
#./configure
#make
#make install
#cd ..
++++++++x264+++++++++++
#git clone git://git.videolan.org/x264
#cd x264
#./configure --enable-shared
#make
#make install
#cd ..
++++++++LAME+++++++++++
#wget http://downloads.sourceforge.net/project/lame/lame/3.99/lame-3.99.5.tar.gz
#tar xzvf lame-3.99.5.tar.gz
#cd lame-3.99.5
#./configure --enable-nasm
#make
#make install
#cd ..
++++++++libogg+++++++++++
#wget http://downloads.xiph.org/releases/ogg/libogg-1.3.0.tar.gz
#tar xzvf libogg-1.3.0.tar.gz
#cd libogg-1.3.0
#./configure
#make
#make install
#cd ..
++++++++libvorbis+++++++++++
#wget http://downloads.xiph.org/releases/vorbis/libvorbis-1.3.3.tar.gz
#tar xzvf libvorbis-1.3.3.tar.gz
#cd libvorbis-1.3.3
#./configure
#make
#make install
#cd ..
++++++++libvpx+++++++++++
#git clone http://git.chromium.org/webm/libvpx.git
#cd libvpx
#./configure --enable-shared
#make
#make install
#cd ..
++++++++FAAD2+++++++++++
#wget http://downloads.sourceforge.net/project/faac/faad2-src/faad2-2.7/faad2-2.7.tar.gz
#tar zxvf faad2-2.7.tar.gz
#cd faad2-2.7
#./configure
#make
#make install
#cd ..
++++++++FAAC+++++++++++
#wget http://downloads.sourceforge.net/project/faac/faac-src/faac-1.28/faac-1.28.tar.gz
#tar zxvf faac-1.28.tar.gz
#cd faac-1.28
#./configure
#make
#make install
#cd ..
++++++++Xvid+++++++++++
#wget http://downloads.xvid.org/downloads/xvidcore-1.3.2.tar.gz
#tar zxvf xvidcore-1.3.2.tar.gz
#cd xvidcore/build/generic
#./configure
#make
#make install
cd ..
#git clone git://source.ffmpeg.org/ffmpeg
#cd ffmpeg
#./configure --prefix=/opt/ffmpeg/ --enable-version3 --enable-libvpx --enable-libfaac --enable-libmp3lame --enable-libvorbis --enable-libx264 --enable-libxvid --enable-shared --enable-gpl --enable-postproc --enable-nonfree --enable-avfilter --enable-pthreads
#make && make install
#cd ..
修改/etc/ld.so.conf以下:
include ld.so.conf.d/*.conf
/lib
/lib64
/usr/lib
/usr/lib64
/usr/local/lib
/usr/local/lib64
/opt/ffmpeg/lib
#ldconfig
2、安裝Nginx相關模塊
#tar zxvf nginx_mod_h264_streaming-2.2.7.tar.gz
#git clone git://github.com/arut/nginx-rtmp-module.git
#tar zxvf pcre-8.12.tar.gz
#cd pcre-8.12
#./configure
#make && make install
#tar zxvf nginx-1.2.6.tar.gz
#cd nginx-1.2.6
#./configure --prefix=/usr/local/nginx --add-module=../nginx_mod_h264_streaming-2.2.7 --with-http_flv_module --with-http_gzip_static_module --with-http_stub_status_module --with-http_mp4_module --add-module=../nginx-rtmp-module --add-module=../nginx-rtmp-module/hls --with-cc-opt=-I/opt/ffmpeg/include --with-ld-opt='-L/opt/ffmpeg/lib -Wl,-rpath=/opt/ffmpeg/lib'
#make && make install
3、修改nginx主配置文件,配置虛擬主機(這裏咱們暫時只配置和測試點播,直播請看借鑑地址):
user nobody nobody;
worker_processes 4;
error_log logs/nginx_error.log info;
pid logs/nginx.pid;
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 51200;
}
#rtmp_auto_push on;
rtmp {
server {
listen 1935;
application vod {
play /opt/media/nginxrtmp/flv;
}
}
}
http
{
include mime.types;
default_type application/octet-stream;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 50m ;
limit_conn_zone $binary_remote_addr zone=perip:256k;
limit_conn_log_level notice;
sendfile on;
tcp_nopush on;
keepalive_timeout 6000;#測試併發臨時調大
tcp_nodelay on;
gzip on;
gzip_min_length 1k;
gzip_buffers 4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types text/plain application/x-javascript text/css application/xml;
gzip_vary on;
#log format
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" $http_x_forwarded_for';
#支持flv
server
{
listen 8081;
server_name 192.168.0.33;
root /opt/pub/media/nginx; #http協議時候,flv視頻位置
location ~ .*.(flv|swf|mp4|wma|wmv)$ {
valid_referers none blocked *.xxxx.com http://localhost;
if ($invalid_referer) {
return 403;
}
}
location ~ \.flv$ {
flv;
limit_conn one 20;#限制客戶端併發鏈接數
limit_rate 200k;#限制每客戶端最大帶寬
}
location ~ \.mp4$ {
flv;
limit_conn one 20;
limit_rate 200k;
}
access_log logs/nginxflv_access.log main;
}
server
{
listen 8082;
server_name 192.168.0.33;
index index.html;
location / {
root /opt/pub/media/nginx-rtmp;
}
access_log logs/nginxrtmpflv_access.log main;
}
server {
listen 8080;
location /stat {
rtmp_stat all;
rtmp_stat_stylesheet stat.xsl;
}
location /stat.xsl {
root /opt/pub/soft/nginx-rtmp-module;
}
location / {
root /opt/pub/soft/nginx-rtmp-module/test/rtmp-publisher;
}
}
http://192.168.0.33:8080/stats 查看rtmp客戶請求信息
http://192.168.0.33:8081/index.html 查看nginx http協議時候的flv視頻
http://192.168.0.33:8082/index.html 查看nginx rtmp協議時候的flv視頻
4、基本功能測試:
播放器採用開源播放器jwplayer,將播放器放到index.html同級目錄下
NGINX-Http測試頁面index.html(切記http的時候須要給視頻添加關鍵幀)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test of nginx-http</title>
</head>
<body>
<div id="myElement"></div>
<object id="FlashID" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="480" height="360">
<param name="movie" value="player.swf" />
<param name="quality" value="high" />
<param name="wmode" value="opaque" />
<param name="swfversion" value="6.0.65.0" />
<param name="flashvars" value="type=http&file=http://192.168.0.33:8081/2.flv&autostart=true">
<EMBED SRC=\'#\'" WIDTH="480" HEIGHT="360" QUALITY="high" PLUGINSPAGE="http://www.macromedia.com/shockwave/download/i
ndex.cgi?P1_Prod_Version=ShockwaveFlash" flashvars="type=http&file=http://192.168.0.33:8081/2.flv&autostart=true" wmode="opaq
ue" />
</object>
</body>
</html>
NGINX-RTMP測試頁面index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>nginx-rtmp</title>
</head>
<body>
<div id="myElement"></div>
<object id="FlashID" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="480" height="360">
<param name="movie" value="player.swf" />
<param name="quality" value="high" />
<param name="wmode" value="opaque" />
<param name="swfversion" value="6.0.65.0" />
<param name="flashvars" value="streamer=rtmp://192.168.0.33:1935/vod&file=2.flv&autostart=true">
<EMBED SRC=\'#\'" WIDTH="480" HEIGHT="360" QUALITY="high" PLUGINSPAGE="http://www.macromedia.com/shockwave/download/i
ndex.cgi?P1_Prod_Version=ShockwaveFlash" flashvars="streamer=rtmp://192.168.0.33:1935/vod&file=2.flv&autostart=true" wmode="o
paque" />
</object>
</body>
</html>
頁面能打開正常播放就說明配置是正確的。
5、併發測試
NGINX-HTTP採用loadrunner測試(略)
NGINX-RTMP採用開源軟件flazr測試:
前提須要安裝jdk軟件
client.bat rtmp://192.168.0.33:1935/vod/2.flv -load 500
這裏的500是併發數
因爲局域網的限制,帶寬測試到必定大小的時候就上不去了,故此步截圖不忽略。
借鑑地址:
https://github.com/arut/nginx-rtmp-module/wiki/Examples
http://blog.sina.com.cn/s/blog_438308750100ez27.html
http://space.itpub.net/27043155/viewspace-735288
http://riches.blog.51cto.com/1167414/649598
https://github.com/arut/nginx-rtmp-module
http://www.yeeach.com