1 簡介html
RTMP是Real Time Messaging Protocol(實時消息傳輸協議)的首字母縮寫。該協議基於TCP,是一個協議族,包括RTMP基本協議及RTMPT/RTMPS/RTMPE等多種變種。RTMP是一種設計用來進行實時數據通訊的網絡協議,主要用來在Flash/AIR平臺和支持RTMP協議的流媒體/交互服務器之間進行音視頻和數據通訊。支持該協議的軟件包括Adobe Media Server/Ultrant Media Server/red5等。linux
2 環境nginx
2.1 環境信息c++
IP Address=10.168.0.51git
HostName=rtmp.cmdschool.orggithub
OS=CentOS 7.3vim
2.2 yum源配置centos
yum -y install gcc gcc-c++ make expat-devel yum -y install rpm-build
2.3 建立構建用戶bash
useradd -u 1001 builder服務器
2.4 關閉selinux
setenforce 0 sed -i 's/SELINUX=enforcing/SELINUX=disabled/g' /etc/selinux/config
3 構建rpm包部分
3.1 下載安裝包
su - builder cd ~ wget http://nginx.org/packages/centos/7/SRPMS/nginx-1.8.1-1.el7.ngx.src.rpm wget -O nginx-rtmp-module-master.zip https://codeload.github.com/arut/nginx-rtmp-module/zip/master
3.2 解壓並測試構建環境
3.2.1 構建環境和測試
rpm -ivh nginx-1.8.1-1.el7.ngx.src.rpm rpmbuild -bb rpmbuild/SPECS/nginx.spec
3.2.2 異常狀況
若是看到相似提示:
warning: bogus date in %changelog: Thu Aug 5 2014 Sergey Budnevitch <sb@nginx.com> warning: bogus date in %changelog: Thu Jul 12 2014 Sergey Budnevitch <sb@nginx.com> warning: bogus date in %changelog: Tue Jul 17 2013 Sergey Budnevitch <sb@nginx.com> warning: bogus date in %changelog: Tue May 6 2013 Sergey Budnevitch <sb@nginx.com> warning: bogus date in %changelog: Tue Aug 10 2011 Sergey Budnevitch error: Failed build dependencies: openssl-devel >= 1.0.1 is needed by nginx-1:1.8.1-1.el7.centos.ngx.x86_64 zlib-devel is needed by nginx-1:1.8.1-1.el7.centos.ngx.x86_64 pcre-devel is needed by nginx-1:1.8.1-1.el7.centos.ngx.x86_64
請先解決包的依賴關係而後重試:
exit yum install -y openssl-devel zlib-devel pcre-devel
3.2.3 正常狀況
若是末尾相似的提示表示構建環境測試經過或稱從新打包完成:
+ umask 022 + cd /home/builder/rpmbuild/BUILD + cd nginx-1.8.1 + /bin/rm -rf /home/builder/rpmbuild/BUILDROOT/nginx-1.8.1-1.el6.ngx.x86_64 + exit 0
3.3 配置增長模塊的源文件
unzip nginx-rtmp-module-master.zip cp -a nginx-rtmp-module-master nginx-rtmp-module tar zcvf nginx-rtmp-module.tar.gz nginx-rtmp-module cp nginx-rtmp-module.tar.gz rpmbuild/SOURCES/
3.4 修改構建文件
3.4.1 建立配置文件副本
cp rpmbuild/SPECS/nginx.spec rpmbuild/SPECS/nginx.spec.default
3.4.2 修改配置文件參數
vim rpmbuild/SPECS/nginx.spec
修改以下參數:
Source10: nginx.suse.logrotate Source11: nginx-rtmp-module.tar.gz [..] %setup -q %setup -T -D -b 11 %build [..] --add-module=../nginx-rtmp-module $* make %{?_smp_mflags}
注:增長第2,5,9行
3.5 從新構建rpm包
rpmbuild -bb rpmbuild/SPECS/nginx.spec exit
4 配置RTMP服務
4.1 安裝nginx包
cd /home/builder/rpmbuild/RPMS/x86_64/ yum -y install nginx-1.8.1-1.el7.centos.ngx.x86_64.rpm
注:以上包可拷貝到其餘CentOS7.3的機器上安裝
4.2 確認模塊啓用
nginx -V
顯示以下:
nginx version: nginx/1.8.1 built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC) built with OpenSSL 1.0.1e-fips 11 Feb 2013 TLS SNI support enabled configure arguments: --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 chown nginx:nginx /var/www/html chmod 700 /var/www/html
4.4 建立配置文件
vim /etc/nginx/nginx.conf
修改內容以下:
worker_processes 1; events { worker_connections 1024; } rtmp { server { listen 1935; chunk_size 4096; application myapp { # 定義一個頻道myapp live on; play /var/www/html; # 定義頻道的根目錄 } } } http { server { listen 80; location /stat { # 定義狀態的訪問URI rtmp_stat all; rtmp_stat_stylesheet stat.xsl; } location /stat.xsl { # 定義狀態文件路徑 root /var/www/html; } location / { # 定義播放器網頁訪問的URI和根目錄 root /var/www/html; } } }
4.5 重啓服務並配置服務默認啓動
systemctl restart nginx systemctl enable nginx
4.6 配置防火牆
firewall-cmd --permanent --add-port 1935/tcp firewall-cmd --permanent --add-service http 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 wget https://ssl.p.jwpcdn.com/player/download/jwplayer-7.10.7.zip unzip jwplayer-7.10.7.zip
注:下載前要先在官網註冊帳號
5.2 使用代碼加載播放器
vim /var/www/html/index.html
輸入以下內容:
<html> <head> <script src='./jwplayer-7.10.7/jwplayer.js'></script> <script>jwplayer.key='******XCUoBbOY0Xq+PLboip3jP7gEy4******==';</script> </head> <body> <div id="player">Loading the player...</div> <script> // Setup the player const player = jwplayer('player').setup({ file: 'rtmp://10.168.0.51/myapp/1431308733062.mp4' }); // Listen to an event player.on('pause', (event) => { alert('Why did my user pause their video instead of watching it?'); }); // Call the API const bumpIt = () => { const vol = player.getVolume(); player.setVolume(vol + 10); } bumpIt(); </script> </body> </html>
注:修改第3,4,11行,其中第3行是播放器的解壓位置,第4行是播放器的SN,第11行是流媒體的絕對路徑。
5.3 測試播放
http://10.168.0.51
5.4 監視播放狀態
http://10.168.0.51/stat
=====================================
參閱資料
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