天天看點

流媒體伺服器之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源配置

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 &lt;[email protected]&gt;</code>

<code>warning: bogus </code><code>date</code> <code>in</code> <code>%changelog: Thu Jul 12 2014 Sergey Budnevitch &lt;[email protected]&gt;</code>

<code>warning: bogus </code><code>date</code> <code>in</code> <code>%changelog: Tue Jul 17 2013 Sergey Budnevitch &lt;[email protected]&gt;</code>

<code>warning: bogus </code><code>date</code> <code>in</code> <code>%changelog: Tue May  6 2013 Sergey Budnevitch &lt;[email protected]&gt;</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 &gt;= 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>&lt;html&gt;</code>

<code>    </code><code>&lt;</code><code>head</code><code>&gt;</code>

<code>        </code><code>&lt;script src=</code><code>'./jwplayer-7.10.7/jwplayer.js'</code><code>&gt;&lt;</code><code>/script</code><code>&gt;</code>

<code>        </code><code>&lt;script&gt;jwplayer.key=</code><code>'******XCUoBbOY0Xq+PLboip3jP7gEy4******=='</code><code>;&lt;</code><code>/script</code><code>&gt;</code>

<code>    </code><code>&lt;</code><code>/head</code><code>&gt;</code>

<code>    </code><code>&lt;body&gt;</code>

<code>        </code><code>&lt;div </code><code>id</code><code>=</code><code>"player"</code><code>&gt;Loading the player...&lt;</code><code>/div</code><code>&gt;</code>

<code>        </code><code>&lt;script&gt;</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) =&gt; {</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 = () =&gt; {</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>&lt;</code><code>/script</code><code>&gt;</code>

<code>    </code><code>&lt;</code><code>/body</code><code>&gt;</code>

<code>&lt;</code><code>/html</code><code>&gt;</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,如需轉載請自行聯系原作者

繼續閱讀