流媒体服务器之nginx的rtmp模块

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

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

相关文章
相关标签/搜索