1. 程式人生 > 實用技巧 >流媒體伺服器之nginx的rtmp模組

流媒體伺服器之nginx的rtmp模組

1 簡介

RTMP是Real Time Messaging Protocol(實時訊息傳輸協議)的首字母縮寫。該協議基於TCP,是一個協議族,包括RTMP基本協議及RTMPT/RTMPS/RTMPE等多種變種。RTMP是一種設計用來進行實時資料通訊的網路協議,主要用來在Flash/AIR平臺和支援RTMP協議的流媒體/互動伺服器之間進行音視訊和資料通訊。支援該協議的軟體包括Adobe Media Server/Ultrant Media Server/red5等。

2 環境

2.1 環境資訊

IP Address=10.168.0.51

HostName=rtmp.cmdschool.org

OS=CentOS 7.3

2.2 yum源配置

yum-yinstallgccgcc-c++makeexpat-devel
yum-yinstallrpm-build

2.3 建立構建使用者

useradd -u 1001 builder

2.4 關閉selinux

setenforce0
sed-i's/SELINUX=enforcing/SELINUX=disabled/g'/etc/selinux/config

3 構建rpm包部分

3.1 下載安裝包

su-builder
cd~
wgethttp://nginx.org/packages/centos/7/SRPMS/nginx-1.8.1-1.el7.ngx.src.rpm
wget-Onginx-rtmp-module-master.ziphttps://codeload.github.com/arut/nginx-rtmp-module/zip/master

3.2 解壓並測試構建環境

3.2.1 構建環境和測試

rpm-ivhnginx-1.8.1-1.el7.ngx.src.rpm
rpmbuild-bbrpmbuild/SPECS/nginx.spec

3.2.2 異常情況

如果看到類似提示:

warning:bogusdatein%changelog:ThuAug52014SergeyBudnevitch<[email protected]>
warning:bogusdatein%changelog:ThuJul122014SergeyBudnevitch<[email protected]>
warning:bogusdatein%changelog:TueJul172013SergeyBudnevitch<
[email protected]
> warning:bogusdatein%changelog:TueMay62013SergeyBudnevitch<[email protected]> warning:bogusdatein%changelog:TueAug102011SergeyBudnevitch error:Failedbuilddependencies: openssl-devel>=1.0.1isneededbynginx-1:1.8.1-1.el7.centos.ngx.x86_64 zlib-develisneededbynginx-1:1.8.1-1.el7.centos.ngx.x86_64 pcre-develisneededbynginx-1:1.8.1-1.el7.centos.ngx.x86_64

請先解決包的依賴關係然後重試:

exit
yuminstall-yopenssl-develzlib-develpcre-devel

3.2.3 正常情況

如果末尾類似的提示表示構建環境測試通過或稱重新打包完成:

+umask022
+cd/home/builder/rpmbuild/BUILD
+cdnginx-1.8.1
+/bin/rm-rf/home/builder/rpmbuild/BUILDROOT/nginx-1.8.1-1.el6.ngx.x86_64
+exit0

3.3 配置增加模組的原始檔

unzipnginx-rtmp-module-master.zip
cp-anginx-rtmp-module-masternginx-rtmp-module
tarzcvfnginx-rtmp-module.tar.gznginx-rtmp-module
cpnginx-rtmp-module.tar.gzrpmbuild/SOURCES/

3.4 修改構建檔案

3.4.1 建立配置檔案副本

cprpmbuild/SPECS/nginx.specrpmbuild/SPECS/nginx.spec.default

3.4.2 修改配置檔案引數

vimrpmbuild/SPECS/nginx.spec

修改如下引數:

Source10:nginx.suse.logrotate
Source11:nginx-rtmp-module.tar.gz
[..]
%setup-q
%setup-T-D-b11

%build
[..]
--add-module=../nginx-rtmp-module
$*
make%{?_smp_mflags}

注:增加第2,5,9行

3.5 重新構建rpm包

rpmbuild-bbrpmbuild/SPECS/nginx.spec
exit

4 配置RTMP服務

4.1 安裝nginx包

cd/home/builder/rpmbuild/RPMS/x86_64/
yum-yinstallnginx-1.8.1-1.el7.centos.ngx.x86_64.rpm

注:以上包可拷貝到其他CentOS7.3的機器上安裝

4.2 確認模組啟用

nginx-V

顯示如下:

nginxversion:nginx/1.8.1
builtbygcc4.8.520150623(RedHat4.8.5-11)(GCC)
builtwithOpenSSL1.0.1e-fips11Feb2013
TLSSNIsupportenabled
configurearguments:--prefix=/etc/nginx--sbin-path=/usr/sbin/nginx--conf-path=/etc/nginx/nginx.conf--error-log-path=/var/log/nginx/error.log--http-log-path=/var/log/nginx/access.log--pid-path=/var/run/nginx.pid--lock-path=/var/run/nginx.lock--http-client-body-temp-path=/var/cache/nginx/client_temp--http-proxy-temp-path=/var/cache/nginx/proxy_temp--http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp--http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp--http-scgi-temp-path=/var/cache/nginx/scgi_temp--user=nginx--group=nginx--add-module=../nginx-rtmp-module--with-http_ssl_module--with-http_realip_module--with-http_addition_module--with-http_sub_module--with-http_dav_module--with-http_flv_module--with-http_mp4_module--with-http_gunzip_module--with-http_gzip_static_module--with-http_random_index_module--with-http_secure_link_module--with-http_stub_status_module--with-http_auth_request_module--with-mail--with-mail_ssl_module--with-file-aio--with-ipv6--with-http_spdy_module--with-cc-opt='-O2-g-pipe-Wall-Wp,-D_FORTIFY_SOURCE=2-fexceptions-fstack-protector-strong--param=ssp-buffer-size=4-grecord-gcc-switches-m64-mtune=generic'

可見配置的引數後面有載入模組:

--add-module=../nginx-rtmp-module

4.3 程式資料目錄

mkdir-p/var/www/html
chownnginx:nginx/var/www/html
chmod700/var/www/html

4.4 建立配置檔案

vim/etc/nginx/nginx.conf

修改內容如下:

worker_processes1;

events{
worker_connections1024;
}

rtmp{
server{
listen1935;
chunk_size4096;
applicationmyapp{#定義一個頻道myapp
liveon;
play/var/www/html;#定義頻道的根目錄
}
}
}

http{
server{
listen80;
location/stat{#定義狀態的訪問URI
rtmp_statall;
rtmp_stat_stylesheetstat.xsl;
}

location/stat.xsl{#定義狀態檔案路徑
root/var/www/html;
}

location/{#定義播放器網頁訪問的URI和根目錄
root/var/www/html;
}

}
}

4.5 重啟服務並配置服務預設啟動

systemctlrestartnginx
systemctlenablenginx

4.6 配置防火牆

firewall-cmd--permanent--add-port1935/tcp
firewall-cmd--permanent--add-servicehttp
firewall-cmd--reload
firewall-cmd--list-all

4.7 上傳測試檔案

拍一個手機視訊(MP4檔案)上傳到頻道定義目錄“/var/www/html”用於測試(筆者上傳的是“1431308733062.mp4”)。

注:由於Nginx的rtmp模組一個簡單的流媒體伺服器,故對編碼格式支援有限,他需要“ffmpeg”來補充,由於本案例只是基於Nginx擴充套件模組實現流媒體伺服器,故這裡不詳述。

5 客戶端配置

5.1 下載客戶端並解壓

cd/var/www/html
wgethttps://ssl.p.jwpcdn.com/player/download/jwplayer-7.10.7.zip
unzipjwplayer-7.10.7.zip

注:下載前要先在官網註冊賬號

5.2 使用程式碼載入播放器

vim/var/www/html/index.html

輸入如下內容:

<html>
<head>
<scriptsrc='./jwplayer-7.10.7/jwplayer.js'></script>
<script>jwplayer.key='******XCUoBbOY0Xq+PLboip3jP7gEy4******==';</script>
</head>
<body>
<divid="player">Loadingtheplayer...</div>
<script>
//Setuptheplayer
constplayer=jwplayer('player').setup({
file:'rtmp://10.168.0.51/myapp/1431308733062.mp4'
});

//Listentoanevent
player.on('pause',(event)=>{
alert('Whydidmyuserpausetheirvideoinsteadofwatchingit?');
});

//CalltheAPI
constbumpIt=()=>{
constvol=player.getVolume();
player.setVolume(vol+10);
}
bumpIt();
</script>
</body>
</html>

注:修改第3,4,11行,其中第3行是播放器的解壓位置,第4行是播放器的SN,第11行是流媒體的絕對路徑。

5.3 測試播放

http://10.168.0.51

wKioL1kJjPSgfaREAAQpdY6gWqU499.png

5.4 監視播放狀態

http://10.168.0.51/stat

wKioL1kJjQbwxBukAABEvsumakQ658.png

=====================================

參閱資料

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

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

https://github.com/arut/nginx-rtmp-module/wiki/Directives

https://github.com/jwplayer/jwplayer

http://www.tuicool.com/articles/iauQNr

http://www.cuplayer.com/player/PlayerCode/Nginx/2016/0509/2375.html

https://wenku.baidu.com/view/1bb9e587ad51f01dc381f13b.html

https://wenku.baidu.com/view/1bb9e587ad51f01dc381f13b.html

http://blog.sina.com.cn/s/blog_704836f40102wlmh.html

http://www.linuxidc.com/Linux/2015-08/121324.htm

轉載於:https://blog.51cto.com/cmdschool/1921069