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源配置
1
2
<code>yum -y </code><code>install</code> <code>gcc gcc-c++ </code><code>make</code> <code>expat-devel </code>
<code>yum -y </code><code>install</code> <code>rpm-build</code>
2.3 创建构建用户
useradd -u 1001 builder
2.4 关闭selinux
<code>setenforce 0</code>
<code>sed</code> <code>-i </code><code>'s/SELINUX=enforcing/SELINUX=disabled/g'</code> <code>/etc/selinux/config</code>
3 构建rpm包部分
3.1 下载安装包
3
4
<code>su</code> <code>- builder</code>
<code>cd</code> <code>~</code>
<code>wget http:</code><code>//nginx</code><code>.org</code><code>/packages/centos/7/SRPMS/nginx-1</code><code>.8.1-1.el7.ngx.src.rpm</code>
<code>wget -O nginx-rtmp-module-master.zip https:</code><code>//codeload</code><code>.github.com</code><code>/arut/nginx-rtmp-module/zip/master</code>
3.2 解压并测试构建环境
3.2.1 构建环境和测试
<code>rpm -ivh nginx-1.8.1-1.el7.ngx.src.rpm</code>
<code>rpmbuild -bb rpmbuild</code><code>/SPECS/nginx</code><code>.spec</code>
3.2.2 异常情况
如果看到类似提示:
5
6
7
8
9
<code>warning: bogus </code><code>date</code> <code>in</code> <code>%changelog: Thu Aug 5 2014 Sergey Budnevitch <[email protected]></code>
<code>warning: bogus </code><code>date</code> <code>in</code> <code>%changelog: Thu Jul 12 2014 Sergey Budnevitch <[email protected]></code>
<code>warning: bogus </code><code>date</code> <code>in</code> <code>%changelog: Tue Jul 17 2013 Sergey Budnevitch <[email protected]></code>
<code>warning: bogus </code><code>date</code> <code>in</code> <code>%changelog: Tue May 6 2013 Sergey Budnevitch <[email protected]></code>
<code>warning: bogus </code><code>date</code> <code>in</code> <code>%changelog: Tue Aug 10 2011 Sergey Budnevitch</code>
<code>error: Failed build dependencies:</code>
<code> </code><code>openssl-devel >= 1.0.1 is needed by nginx-1:1.8.1-1.el7.centos.ngx.x86_64</code>
<code> </code><code>zlib-devel is needed by nginx-1:1.8.1-1.el7.centos.ngx.x86_64</code>
<code> </code><code>pcre-devel is needed by nginx-1:1.8.1-1.el7.centos.ngx.x86_64</code>
请先解决包的依赖关系然后重试:
<code>exit</code>
<code>yum </code><code>install</code> <code>-y openssl-devel zlib-devel pcre-devel</code>
3.2.3 正常情况
如果末尾类似的提示表示构建环境测试通过或称重新打包完成:
<code>+ </code><code>umask</code> <code>022</code>
<code>+ </code><code>cd</code> <code>/home/builder/rpmbuild/BUILD</code>
<code>+ </code><code>cd</code> <code>nginx-1.8.1</code>
<code>+ </code><code>/bin/rm</code> <code>-rf </code><code>/home/builder/rpmbuild/BUILDROOT/nginx-1</code><code>.8.1-1.el6.ngx.x86_64</code>
<code>+ </code><code>exit</code> <code>0</code>
3.3 配置增加模块的源文件
<code>unzip nginx-rtmp-module-master.zip</code>
<code>cp</code> <code>-a nginx-rtmp-module-master nginx-rtmp-module</code>
<code>tar</code> <code>zcvf nginx-rtmp-module.</code><code>tar</code><code>.gz nginx-rtmp-module</code>
<code>cp</code> <code>nginx-rtmp-module.</code><code>tar</code><code>.gz rpmbuild</code><code>/SOURCES/</code>
3.4 修改构建文件
3.4.1 创建配置文件副本
<code>cp</code> <code>rpmbuild</code><code>/SPECS/nginx</code><code>.spec rpmbuild</code><code>/SPECS/nginx</code><code>.spec.default</code>
3.4.2 修改配置文件参数
<code>vim rpmbuild</code><code>/SPECS/nginx</code><code>.spec</code>
修改如下参数:
10
11
<code>Source10: nginx.suse.logrotate</code>
<code>Source11: nginx-rtmp-module.</code><code>tar</code><code>.gz</code>
<code>[..]</code>
<code>%setup -q</code>
<code>%setup -T -D -b 11</code>
<code>%build</code>
<code> </code><code>--add-module=..</code><code>/nginx-rtmp-module</code>
<code> </code><code>$*</code>
<code>make</code> <code>%{?_smp_mflags}</code>
注:增加第2,5,9行
3.5 重新构建rpm包
4 配置RTMP服务
4.1 安装nginx包
<code>cd</code> <code>/home/builder/rpmbuild/RPMS/x86_64/</code>
<code>yum -y </code><code>install</code> <code>nginx-1.8.1-1.el7.centos.ngx.x86_64.rpm</code>
注:以上包可拷贝到其他CentOS7.3的机器上安装
4.2 确认模块启用
<code>nginx -V</code>
显示如下:
<code>nginx version: nginx</code><code>/1</code><code>.8.1</code>
<code>built by gcc 4.8.5 20150623 (Red Hat 4.8.5-11) (GCC)</code>
<code>built with OpenSSL 1.0.1e-fips 11 Feb 2013</code>
<code>TLS SNI support enabled</code>
<code>configure arguments: --prefix=</code><code>/etc/nginx</code> <code>--sbin-path=</code><code>/usr/sbin/nginx</code> <code>--conf-path=</code><code>/etc/nginx/nginx</code><code>.conf --error-log-path=</code><code>/var/log/nginx/error</code><code>.log --http-log-path=</code><code>/var/log/nginx/access</code><code>.log --pid-path=</code><code>/var/run/nginx</code><code>.pid --lock-path=</code><code>/var/run/nginx</code><code>.lock --http-client-body-temp-path=</code><code>/var/cache/nginx/client_temp</code> <code>--http-proxy-temp-path=</code><code>/var/cache/nginx/proxy_temp</code> <code>--http-fastcgi-temp-path=</code><code>/var/cache/nginx/fastcgi_temp</code> <code>--http-uwsgi-temp-path=</code><code>/var/cache/nginx/uwsgi_temp</code> <code>--http-scgi-temp-path=</code><code>/var/cache/nginx/scgi_temp</code> <code>--user=nginx --group=nginx --add-module=..</code><code>/nginx-rtmp-module</code> <code>--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-</code><code>file</code><code>-aio --with-ipv6 --with-http_spdy_module --with-cc-opt=</code><code>'-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic'</code>
可见配置的参数后面有加载模块:
<code>--add-module=..</code><code>/nginx-rtmp-module</code>
4.3 程序数据目录
<code>mkdir</code> <code>-p </code><code>/var/www/html</code>
<code>chown</code> <code>nginx:nginx </code><code>/var/www/html</code>
<code>chmod</code> <code>700 </code><code>/var/www/html</code>
4.4 创建配置文件
<code>vim </code><code>/etc/nginx/nginx</code><code>.conf</code>
修改内容如下:
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<code>worker_processes 1;</code>
<code>events {</code>
<code> </code><code>worker_connections 1024;</code>
<code>}</code>
<code>rtmp {</code>
<code> </code><code>server {</code>
<code> </code><code>listen 1935;</code>
<code> </code><code>chunk_size 4096;</code>
<code> </code><code>application myapp { </code><code># 定义一个频道myapp</code>
<code> </code><code>live on;</code>
<code> </code><code>play </code><code>/var/www/html</code><code>; </code><code># 定义频道的根目录</code>
<code> </code><code>}</code>
<code> </code><code>}</code>
<code>http {</code>
<code> </code><code>server {</code>
<code> </code><code>listen 80;</code>
<code> </code><code>location </code><code>/stat</code> <code>{ </code><code># 定义状态的访问URI</code>
<code> </code><code>rtmp_stat all;</code>
<code> </code><code>rtmp_stat_stylesheet stat.xsl;</code>
<code> </code><code>}</code>
<code> </code><code>location </code><code>/stat</code><code>.xsl { </code><code># 定义状态文件路径</code>
<code> </code><code>root </code><code>/var/www/html</code><code>;</code>
<code> </code>
<code> </code><code>location / { </code><code># 定义播放器网页访问的URI和根目录</code>
<code> </code><code>}</code>
4.5 重启服务并配置服务默认启动
<code>systemctl restart nginx</code>
<code>systemctl </code><code>enable</code> <code>nginx</code>
4.6 配置防火墙
<code>firewall-cmd --permanent --add-port 1935</code><code>/tcp</code>
<code>firewall-cmd --permanent --add-service http</code>
<code>firewall-cmd --reload</code>
<code>firewall-cmd --list-all</code>
4.7 上传测试文件
拍一个手机视频(MP4文件)上传到频道定义目录“/var/www/html”用于测试(笔者上传的是“1431308733062.mp4”)。
注:由于Nginx的rtmp模块一个简单的流媒体服务器,故对编码格式支持有限,他需要“ffmpeg”来补充,由于本案例只是基于Nginx扩展模块实现流媒体服务器,故这里不详述。
5 客户端配置
5.1 下载客户端并解压
<code>cd</code> <code>/var/www/html</code>
<code>wget https:</code><code>//ssl</code><code>.p.jwpcdn.com</code><code>/player/download/jwplayer-7</code><code>.10.7.zip</code>
<code>unzip jwplayer-7.10.7.zip</code>
注:下载前要先在官网注册账号
5.2 使用代码加载播放器
<code>vim </code><code>/var/www/html/index</code><code>.html</code>
输入如下内容:
<code><html></code>
<code> </code><code><</code><code>head</code><code>></code>
<code> </code><code><script src=</code><code>'./jwplayer-7.10.7/jwplayer.js'</code><code>><</code><code>/script</code><code>></code>
<code> </code><code><script>jwplayer.key=</code><code>'******XCUoBbOY0Xq+PLboip3jP7gEy4******=='</code><code>;<</code><code>/script</code><code>></code>
<code> </code><code><</code><code>/head</code><code>></code>
<code> </code><code><body></code>
<code> </code><code><div </code><code>id</code><code>=</code><code>"player"</code><code>>Loading the player...<</code><code>/div</code><code>></code>
<code> </code><code><script></code>
<code> </code><code>//</code> <code>Setup the player</code>
<code> </code><code>const player = jwplayer(</code><code>'player'</code><code>).setup({</code>
<code> </code><code>file</code><code>: </code><code>'rtmp://10.168.0.51/myapp/1431308733062.mp4'</code>
<code> </code><code>});</code>
<code> </code><code>//</code> <code>Listen to an event</code>
<code> </code><code>player.on(</code><code>'pause'</code><code>, (event) => {</code>
<code> </code><code>alert(</code><code>'Why did my user pause their video instead of watching it?'</code><code>);</code>
<code> </code><code>});</code>
<code> </code><code>//</code> <code>Call the API</code>
<code> </code><code>const bumpIt = () => {</code>
<code> </code><code>const vol = player.getVolume();</code>
<code> </code><code>player.setVolume(vol + 10);</code>
<code> </code><code>}</code>
<code> </code><code>bumpIt();</code>
<code> </code><code><</code><code>/script</code><code>></code>
<code> </code><code><</code><code>/body</code><code>></code>
<code><</code><code>/html</code><code>></code>
注:修改第3,4,11行,其中第3行是播放器的解压位置,第4行是播放器的SN,第11行是流媒体的绝对路径。
5.3 测试播放
<code>http:</code><code>//10</code><code>.168.0.51</code>
<a href="https://s5.51cto.com/wyfs02/M02/93/3E/wKioL1kJjPSgfaREAAQpdY6gWqU499.png" target="_blank"></a>
5.4 监视播放状态
<code>http:</code><code>//10</code><code>.168.0.51</code><code>/stat</code>
<a href="https://s3.51cto.com/wyfs02/M00/93/3E/wKioL1kJjQbwxBukAABEvsumakQ658.png" target="_blank"></a>
本文转自 tanzhenchao 51CTO博客,原文链接:http://blog.51cto.com/cmdschool/1921069,如需转载请自行联系原作者