1. 程式人生 > >流媒體技術學習筆記之(三)Nginx-Rtmp-Module統計某頻道在線觀看流的客戶數

流媒體技術學習筆記之(三)Nginx-Rtmp-Module統計某頻道在線觀看流的客戶數

sele lec rest uri class origin 客戶 擴展 raw

獲得訂閱者人數,可以方便地顯示觀看流的客戶數。

查看已經安裝好的模塊

/usr/local/nginx/sbin/nginx -V

安裝從源編譯Nginx和Nginx-RTMP所需的工具

sudo apt-get install build-essential libpcre3 libpcre3-dev libssl-dev

下載Nginx和Nginx-RTMP源碼

wget http://nginx.org/download/nginx-1.7.5.tar.gz
wget https://github.com/arut/nginx-rtmp-module/archive/master.zip

提取Nginx和Nginx-RTMP源

tar -zxvf nginx-1.7.5.tar.gz
unzip master.zip

切換到Nginx目錄

cd nginx-1.7.5

添加Nginx將編譯的模塊,包括Nginx-RTMP

./configure --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_xslt_module --with-http_flv_module --with-debug --with-http_gzip_static_module --add-module=../nginx-rtmp-module-master

提示錯誤:

./configure: error: the HTTP XSLT module requires the libxml2/libxslt
libraries. You can either do not enable the module or install the libraries.

配置 --with-http_xslt_module 時提示 the HTTP XSLT module requires the libxml2/libxslt libraries:安裝一下擴展

sudo apt-get install libxml2 libxml2-dev libxslt-dev

編譯和安裝Nginx與Nginx-RTMP。

make
sudo make install

安裝Nginx初始化腳本

sudo wget https://raw.github.com/JasonGiedymin/nginx-init-ubuntu/master/nginx -O /etc/init.d/nginx
sudo chmod +x /etc/init.d/nginx
sudo update-rc.d nginx defaults

啟動和停止Nginx生成配置文件

sudo service nginx start
sudo service nginx stop

觀看安裝是否成功:

/usr/local/nginx/sbin/nginx -V

安裝結果

configure arguments: --prefix=/usr/local/nginx --user=www --group=www --with-http_ssl_module --with-http_xslt_module --with-http_flv_module --with-debug --with-http_gzip_static_module --add-module=../nginx-rtmp-module-master

修改Nginx 配置文件添加一下信息,在位置設置統計信息頁面

location /stat {
    rtmp_stat all;
    allow 127.0.0.1;
}

創建一個簡單的xsl樣式表nclients.xsl提取流用戶數

技術分享圖片
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="html"/>

<xsl:param name="app"/>
<xsl:param name="name"/>

<xsl:template match="/">
    <xsl:value-of select="count(//application[name=$app]/live/stream[name=$name]/client[not(publishing) and flashver])"/>
</xsl:template>

</xsl:stylesheet>
技術分享圖片

設置一個返回訂閱者數量的位置

location /nclients {
    proxy_pass http://127.0.0.1/stat;
    xslt_stylesheet /home/www/nclients.xsl app=‘$arg_app‘ name=‘$arg_name‘;
    add_header Refresh "3; $request_uri";
}

Nginx.config 完整配置:

技術分享圖片
user www www;
worker_processes  1;

error_log  logs/error.log  debug;

#pid        logs/nginx.pid;

events {
    worker_connections  65535;
}


http {
    include       mime.types;
    default_type  application/octet-stream;

    log_format  main  ‘[$time_local][$remote_addr][$http_x_forwarded_for] $status "$request" "$http_referer" "$http_user_agent"‘;

    access_log  logs/access.log  main;

    sendfile        on;
    keepalive_timeout  65;

    server {
        
        set  $wwwroot  /home/www/node/html;

        listen       80;
        server_name  localhost;
        index        index.html;
        root         $wwwroot;
        access_log   logs/node.access.log  main;
        error_log    logs/error.log debug;
        
        
        location /rtmp/stat {
            rtmp_stat all;
            rtmp_stat_stylesheet rtmpstat.xsl;
        }
        
        location /rtmpstat.xsl {
        }
        
        location /rtmp/control {
            rtmp_control all;
        }
    
        location /stat {
                rtmp_stat all;
                allow 127.0.0.1;
        }

        location /nclients {
            proxy_pass http://127.0.0.1/stat;
            xslt_stylesheet /home/www/nclients.xsl app=‘$arg_app‘ name=‘$arg_name‘;
            add_header Refresh "3; $request_uri";
        }
        
        location ~* /hls/.*\.m3u8$ {
            types {
                application/vnd.apple.mpegurl m3u8;
            }
            root /tmp;
            add_header Cache-Control no-cache;
            add_header Access-Control-Allow-Origin *;
        }

        location ~* /hls/.*\.ts$ {
            types {
                video/mp2t ts;
            }
            root /tmp;
            expires    1m;
            add_header Cache-Control public;
            add_header Access-Control-Allow-Origin *;
        }

        #error_page  404              /404.html;

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
  
        location / {
            
        }
        
    }

}

rtmp {
    server {
        listen 1935;
        chunk_size 4096;
        application live {
        live on;
        record off;    
      }    
    }
}
技術分享圖片

重啟Nginx

root@iZ239kcyg8rZ:/usr/local/nginx/conf# service nginx restart
 * Stopping Nginx Server...                                                                                                                                                           [ OK ] 
 * Starting Nginx Server...                                                                                                                                                           [ OK ] 
root@iZ239kcyg8rZ:/usr/local/nginx/conf# 

根據直播頻道訪問以下地址:

http://你的推流服務器IP/nclients?app=live&name=4001482820358

流媒體技術學習筆記之(三)Nginx-Rtmp-Module統計某頻道在線觀看流的客戶數