分佈式直播系統(九)【nginx-rtmp控制檯暫停、錄像控制】

轉載請註明出處:https://blog.csdn.net/impingo
項目地址:https://github.com/im-pingo/
項目官網:http://pingos.io

控制檯接口

一個成熟的媒體服務器需要有跟業系統對接的能力,例如允許開發者開發自己的控制檯系統,通過http接口控制當前直播流的暫停與播放,錄像功能的暫停與繼續等操作。
本項目支持多種控制檯控制接口和流信息獲取接口。

配置

配置項

配置項 參數類型 默認值 描述
rtmp_control 選項 all all: 開啓所有控制接口。record: 只開啓錄像控制接口。drop:只開啓關閉連接控制接口。redirect:只開啓重定向控制接口。pause:只開啓暫停接口。 resume:只開啓恢復接口

配置模板

user  root;
daemon on;
master_process on;
worker_processes  1;
#worker_rlimit 4g;
#working_directory /usr/local/openresty/nginx/logs;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
error_log  logs/error.log  info;

worker_rlimit_nofile 102400;
worker_rlimit_core   2G;
working_directory    /tmp;

#pid        logs/nginx.pid;

events {
    use epoll;
    worker_connections  1024;
    multi_listen unix:/tmp/http 80;
}

stream_zone buckets=1024 streams=4096;

rtmp {
    log_format log_json '{$remote_addr, [$time_local]}';
    access_log logs/rtmp.log trunc=2s;
    server {
        listen 1935;
        serverid 000;
        out_queue 2048;

        application live {
            rtmp_auto_pull on;
            rtmp_auto_pull_port unix:/tmp/rtmp;
#            live_record on;
#            live_record_path /tmp/record;
#            live_record_interval  1m;
            record all;
            record_path /tmp/record;

#            exec_publish_done bash -c  "ffmpeg -i /tmp/record/$name.flv -c copy /tmp/record/$basename.mp4";
#            exec_publish bash -c "ffmpeg -i rtmp://127.0.0.1/live/$name -c copy -movflags faststart /tmp/record/$name-$starttime.mp4";
            idle_streams on;
#
#            oclp_pull http://127.0.0.1/p1;

#            push rtmp://120.132.13.108/live;

            live on;
            hls on;
            hls_type live;
            hls_path /tmp/hls;
            hls_fragment 5000ms;
            hls_max_fragment 8000ms;
            hls_playlist_length 15000ms;

#            live_record on;
#            live_record_path /tmp/record;
#            live_record_interval 2m;
#            live_record_min_fragment 10s;
#            live_record_max_fragment 30s;

            hls2memory on;
            mpegts_cache_time 20s;

            hls2_fragment 1000ms;
            hls2_max_fragment 1300ms;
            hls2_playlist_length 3000ms;

            wait_key on;
            wait_video on;
            cache_time 3s;
            low_latency off;
            fix_timestamp 0s;
# h265 codecid, default 12
            hevc_codecid  12;
        }
    }
}

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

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_X-Forwarded-For" "$http_X-Real-IP" "$host"';


    access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #reset_server_name www.test1.com www.test2.com;
    #gzip  on;
    server {
         listen 80;
         location /ts {
             ts_live 1935 app=live;
         }
        location /control {
            rtmp_control all;
        }
        location /rtmp_stat {
            rtmp_stat all;
            rtmp_stat_stylesheet /stat.xsl;
        }

        location /xstat {
            rtmp_stat all;
        }

        location /sys_stat {
            sys_stat;
        }

     location /live {
            flv_live 1935;
         }
         
         location /files {
	         alias /tmp/record;
	        #Nginx日誌目錄
	         autoindex on;
	         #打開目錄瀏覽功能
	         autoindex_exact_size off;
	         #默認爲on,顯示出文件的確切大小,單位是bytes
	         #顯示出文件的大概大小,單位是kB或者MB或者GB
	         autoindex_localtime on;
	         #默認爲off,顯示的文件時間爲GMT時間。
	         #改爲on後,顯示的文件時間爲文件的服務器時間
	         add_header Cache-Control no-store;
         }
         location /hls {
            # Serve HLS fragments
             types {
                 application/vnd.apple.mpegurl m3u8;
                 video/mp2t ts;
             }
             root /tmp;
             expires -1;
             add_header Cache-Control no-cache;
         }

        location /hls2 {
             hls2_live 1935 app=live;
         }
         location / {
             chunked_transfer_encoding on;
             root html/;
         }

         location /hls0/ {
             types{
                 application/vnd.apple.mpegurl m3u8;
                 video/mp2t ts;
             }
             expires -1;
         }
    }
}

控制接口說明

接口名 請求 回覆 描述
暫停推流 /${location}/pause/publisher?srv=${serverid}&app=${app}&name=${name} 參考 http ack 服務器停止轉發收到的直播流
恢復推流 /${location}/resume/publisher?srv=${serverid}&app=${app}&name=${name} 參考 http ack 服務器恢復轉發收到的直播流
暫停錄像 /${location}/record/stop?srv=${serverid}&app=${app}&name=${name}&rec=${recorder} 參考 http ack 暫停某條流的錄像
恢復錄像 /${location}/record/start?srv=${serverid}&app=${app}&name=${name}&rec=${recorder} 參考 http ack 恢復某條流的錄像

JS控制檯示例

爲了更好地幫助大家理解上述接口的使用方法,我專門寫了一個簡單的js控制檯,僅僅實現了暫停、播放、錄像和停止錄像的基本控制。
在這裏插入圖片描述
源碼如下:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<script>
    function pause() {
		var name=document.getElementById('value').value;
		var ip=document.getElementById('ip').value;
        var url = 'http://'+ ip + '/control/pause/publisher?srv=000&app=live&name='+name;
        var request = new XMLHttpRequest();
        request.open("Get", url, true);
        request.send()
    }

    function resume() {
		var name=document.getElementById('value').value;
		var ip=document.getElementById('ip').value;
        var url = 'http://' + ip + '/control/resume/publisher?srv=000&app=live&name='+name;
        var request = new XMLHttpRequest();
        request.open("Get", url, true);
        request.send()
    }

    function pauseRecord(){
		var name=document.getElementById('value').value;
		var ip=document.getElementById('ip').value;
        var url = 'http://' + ip + '/control/record/stop?srv=000&app=live&name='+name+'&rec=r1';
        var request = new XMLHttpRequest();
        request.open("Get", url, true);
        request.send()
    }

    function resumeRecord(){
		var name=document.getElementById('value').value;
		var ip=document.getElementById('ip').value;
        var url = 'http://'+ip+'/control/record/start?srv=000&app=live&name='+name+'&rec=r1';
        var request = new XMLHttpRequest();
        request.open("Get", url, true);
        request.send()
    }

    function finalize(){
		var name=document.getElementById('value').value;
		var ip=document.getElementById('ip').value;
        var url = 'http://'+ip+'/control/drop/publisher?srv=000&app=live&name='+name;
        var request = new XMLHttpRequest();
        request.open("Get", url, true);
        request.send()
    }
</script>

    <span id="car_control" class="car">
		server ip:
        <input type="text" id="ip" placeholder=""/><br/>
		stream name:
        <input type="text" id="value" placeholder=""/><br/>
        <button class="top" onclick="pause()">暫停直播</button>
        <button class="left" onclick="resume()">繼續直播</button>
        <button class="right" onclick="pauseRecord()">暫停錄像</button>
        <button class="bottom" onclick="resumeRecord()">繼續錄像</button>
        <button class="bottom" onclick="finalize()">結束直播</button>
    </span>
</body>
</html>

錄像文件查看接口

生成的錄像文件會存在固定目錄下,通過http接口(http://ip/files/)可以在瀏覽器中看到其文件列表。
在這裏插入圖片描述

獲取流信息

通過接口(http://ip/xstat)可請求到所有在線流的信息,內容如下

<?xml version="1.0" encoding="utf-8" ?>
<rtmp>
    <nginx_version>1.17.1</nginx_version>
    <nginx_rtmp_version>1.1.4</nginx_rtmp_version>
    <compiler>gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC) </compiler>
    <built>Oct 24 2019 16:26:06</built>
    <pid>20853</pid>
    <uptime>176258</uptime>
    <naccepted>3</naccepted>
    <bw_in>0</bw_in>
    <bytes_in>171403</bytes_in>
    <bw_out>0</bw_out>
    <bytes_out>172549</bytes_out>
    <server>
        <serverid>000</serverid>
        <live>
            <stream>
                <name>0</name>
                <time>3225</time>
                <bw_in>0</bw_in>
                <bytes_in>168524</bytes_in>
                <bw_out>0</bw_out>
                <bytes_out>165725</bytes_out>
                <bw_audio>0</bw_audio>
                <bw_video>0</bw_video>
                <client>
                    <id>455</id>
                    <address>0.0.0.0:1935</address>
                    <remote_address>127.0.0.1</remote_address>
                    <time>3056</time>
                    <flashver>LNX 9,0,124,2</flashver>
                    <dropped>0</dropped>
                    <avsync>0</avsync>
                    <timestamp>0</timestamp>
                    <active/>
                </client>
                <client>
                    <id>454</id>
                    <address>0.0.0.0:1935</address>
                    <remote_address>127.0.0.1</remote_address>
                    <time>3413</time>
                    <flashver>FMLE/3.0 (compatible; Lavf58.26.101)</flashver>
                    <dropped>0</dropped>
                    <avsync>11</avsync>
                    <timestamp>3219</timestamp>
                    <publishing/>
                    <active/>
                </client>
                <meta>
                    <video>
                        <width>1920</width>
                        <height>1080</height>
                        <frame_rate>24</frame_rate>
                        <codec>H264</codec>
                        <profile>High</profile>
                        <compat>0</compat>
                        <level>4.0</level>
                    </video>
                    <audio>
                        <codec>AAC</codec>
                        <profile>LC</profile>
                        <channels>6</channels>
                        <sample_rate>48000</sample_rate>
                    </audio>
                </meta>
                <nclients>2</nclients>
                <publishing/>
                <active/>
            </stream>
            <nclients>2</nclients>
        </live>
    </server>
</rtmp>

本項目支持的接口遠不止此,更多控制接口請繼續關注後續的文章或者進羣交流。

QQ交流羣:697773082

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章