Streaming media technology study notes (3) Nginx-Rtmp-Module counts the number of customers watching a channel online

Get the number of subscribers, which can conveniently show the number of customers watching the stream.

View installed modules

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

Install the tools needed to compile Nginx and Nginx-RTMP from source

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

Download Nginx and Nginx-RTMP source code

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

Extract Nginx and Nginx-RTMP sources

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

Switch to the Nginx directory

cd nginx-1.7.5

Add modules that Nginx will compile, including 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

Prompt error:

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

When configuring --with-http_xslt_module, it prompts that the HTTP XSLT module requires the libxml2/libxslt libraries: install the extension

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

Compile and install Nginx and Nginx-RTMP.

make
sudo make install

Install the Nginx init script

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

Start and stop Nginx to generate configuration files

sudo service nginx start
sudo service nginx stop

To see if the installation was successful:

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

Installation result

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

Modify the Nginx configuration file to add some information, and set the statistics page in the location

location /stat {
    rtmp_stat all;
    allow 127.0.0.1;
}

Create a simple xsl stylesheet nclients.xsl to extract the number of stream users

copy code
<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>
copy code

Set a location that returns the number of subscribers

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 complete configuration:

copy code
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;    
      }    
    }
}
copy code

Restart Nginx

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

Visit the following address according to the live channel:

http://your push server IP/nclients?app=live&name=4001482820358

The client opens three playback tests:

Open 2 VLC playback streams:

 

 Check the number of people online:

 

 

 

 end.

 

from:https://www.cnblogs.com/tinywan/p/6226320.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325680816&siteId=291194637