系统相关
首页 > 系统相关> > pingos(nginx-rtmp-module)获取实时直播数据

pingos(nginx-rtmp-module)获取实时直播数据

作者:互联网

pingos(nginx-rtmp-module)获取实时直播数据

转载请注明出处:https://blog.csdn.net/impingo
我的开源项目地址:https://github.com/pingostack/pingos
开源项目:https://pingos.io

QQ交流群:193611565

QQ交流二群:193611565
QQ交流一群(已满):697773082

描述

nginx-rtmp-module模块里有个rtmp_stat配置,该功能可以允许用户通过http接口获取单个worker进程上的正在直播的rtmp推拉流连接和对应的流量。

但是这个功能有很大的缺陷:

1. pingos中的解决方案

先看pingos中的配置:

location /sys_stat {
    sys_stat;
}

location /sys_stat_proxy/ {
        rewrite ^/proxy/(.*) /sys_stat break;
        proxy_pass http://$1:;
}

location /bs {
    broadcast unix:/tmp/http /sys_stat_proxy;
    broadcast_rewrite_prefix " " [;
    broadcast_suffix ];
}

2. APP列表和流列表

也就是说通过这个接口你将获取到一个数据,其中每个节点元素都是对应一个worker进程上的数据。

json最外层的整体结构例如:

[ {worker0}, {worker1}, {worker3} ... ]

2.1 worker 结构

{
    "ngx_worker": 0,
    "ngx_process_slot": 0,
    "ngx_pid": 29809,
    "live_stream": { ... }
}
字段类型描述
ngx_workerintworker进程的编号
ngx_process_slotintworker进程指针在master进程数组里的下标
ngx_pidintworker进程id
live_streamjson object直播流列表

2.2 live_stream 结构

{
    "server_version": "1.17.5",
    "compiler": "gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) ",
    "built": "Apr 11 2021",
    "pid": 29809,
    "timestamp": 1618637886,
    "naccepted": 178,
    "in": {
        "bw": 3764096,
        "bytes": 3421950282
    },
    "out": {
        "bw": 4984816,
        "bytes": 3643226942
    },
    "server_array": [ ... ]
}
字段类型描述
server_versionstring服务器版本
compilerstring编译器版本
builtstring编译日期
pidstring线程id
timestamptime当前请求时候的系统时间
nacceptedint当前进程接收到的请求总数
inobject当前worker进程的网络入流情况
outobject当前worker进程的网络出流情况
bwint当前worker进程的带宽(注意:是当前一个worker进程,而且全部worker进程;在in里就是入网带宽,在out里就是出网带宽)
bytesint从该worker进程开启到目前为止的流量总和
server_arrayjson array以serverid为索引的不同serverid下的流状态信息

2.3 server_array 结构

{
    "serverid": "112.121.178.154",
    "total_clients": 1,
    "rtmp_clients": 1,
    "flv_clients": 0,
    "hls_clients": 0,
    "ts_clients": 0,
    "stream_array": [ ... ]
}
字段类型描述
serveridstringserverid
total_clientsint当前时刻下的worker进程下的serverid下的所有客户端连接了
rtmp_clientsintrtmp客户端的连接量
flv_clientsinthttp-flv客户端的连接量
hls_clientsinthls+客户端的连接量(注意,只能统计hls+的连接量,普通hls无法被统计)
ts_clientsinthttp-ts客户端的连接量
stream_arrayjson array在线流列表

2.4 stream_array 结构

{
    "name": "112.121.178.154/live/111",
    "time": 102641,
    "in": {
        "bw": 3677064,
        "bytes": 30479426
    },
    "video_in": {
        "bw": 3295320,
        "bytes": 26164007
    },
    "audio_in": {
        "bw": 376040,
        "bytes": 4251276
    },
    "out": {
        "bw": 4836912,
        "bytes": 42234918
    },
    "meta": {
        "video": {
            "width": 1920,
            "height": 1080,
            "frame_rate": "24.0",
            "codec": "H264",
            "profile": "High",
            "compat": "4.0"
        },
        "audio": {
            "codec": "AAC",
            "profile": "LC",
            "channels": 6,
            "sample_rate": 48000
        }
    },
    "total_clients": 1,
    "rtmp_clients": 1,
    "flv_clients": 0,
    "hls_clients": 0,
    "ts_clients": 0,
    "active": true,
    "client_array": [ ... ]
}
字段类型描述
namestringserverid/app/流名 的组合
timeint流存在的时长
injson object这条流在这个worker进程上的入网网络情况
outjson object这条流在这个worker进程上的出网网络情况
video_injson object这条流的视频在这个worker进程上的入网网络情况
audio_injson object这条流的音频在这个worker进程上的入网网络情况
metajson object这条流的音视频信息
total_clientsint这条流在这个worker进程上的所有客户端连接量
rtmp_clientsint这条流在这个worker进程上的所有rtmp客户端连接量
flv_clientsint这条流在这个worker进程上的所有http-flv客户端连接量
hls_clientsint这条流在这个worker进程上的所有hls+客户端连接量
ts_clientsint这条流在这个worker进程上的所有http-ts客户端连接量
activebool这条流在这个worker进程上是否处于激活状态
client_arrayjson object这条流在这个worker进程上的所有客户端连接列表

2.5 client_array 结构

[
    {
        "id": 430680,
        "address": "0.0.0.0:1935",
        "remote_address": "101.228.192.22",
        "time": 82155,
        "flashver": "LNX 9,0,124,2",
        "dropped": 0,
        "avsync": 4294967284,
        "timestamp": 0,
        "active": true,
        "publishing": false,
        "relay": false,
        "interprocess": false,
        "protocol": "rtmp"
    }, {
        "id": 0,
        "address": "0.0.0.0:1935",
        "remote_address": "",
        "time": 102638,
        "flashver": "FMLE/3.0 (compatible; Lavf58.77.100)",
        "dropped": 0,
        "avsync": 4294967284,
        "timestamp": 0,
        "active": true,
        "publishing": false,
        "relay": true,
        "interprocess": false,
        "protocol": "rtmp"
    }, {
        "id": 430653,
        "address": "0.0.0.0:1935",
        "remote_address": "101.228.192.22",
        "time": 102917,
        "flashver": "FMLE/3.0 (compatible; Lavf58.77.100)",
        "dropped": 0,
        "avsync": 4294967284,
        "timestamp": 102666,
        "active": true,
        "publishing": true,
        "relay": false,
        "interprocess": false,
        "protocol": "rtmp"
    }
]
字段类型描述
idint客户端id
addressstring该链接对应的服务器端监听的ip和端口
remote_addressstring客户端的公网ip
timeint客户端的连接时长
flashvarstring如果是rtmp连接,则展示客户端的版本
droppedint丢包量
avsyncint音视频时间戳差值(单位毫秒)
timestampint媒体数据时间戳(毫秒)
activebool客户端是否被激活
publishingbool是否为推流连接
relaybool是否为转发连接(如push、pull连接)
interprocessbool是否为进程间回源连接
protocolstring客户端使用的协议(rtmp、flv、ts、hls等)

2.6 完整的返回demo

[{
    "ngx_worker": 0,
    "ngx_process_slot": 0,
    "ngx_pid": 29809,
    "live_stream": {
        "server_version": "1.17.5",
        "compiler": "gcc 4.8.5 20150623 (Red Hat 4.8.5-44) (GCC) ",
        "built": "Apr 11 2021",
        "pid": 29809,
        "timestamp": 1618637886,
        "naccepted": 178,
        "in": {
            "bw": 3764096,
            "bytes": 3421950282
        },
        "out": {
            "bw": 4984816,
            "bytes": 3643226942
        },
        "server_array": [{
                "serverid": "112.121.178.154",
                "stream_array": [{
                        "name": "112.121.178.154/live/111",
                        "time": 102641,
                        "in": {
                            "bw": 3677064,
                            "bytes": 30479426
                        },
                        "video_in": {
                            "bw": 3295320,
                            "bytes": 26164007
                        },
                        "audio_in": {
                            "bw": 376040,
                            "bytes": 4251276
                        },
                        "out": {
                            "bw": 4836912,
                            "bytes": 42234918
                        },
                        "client_array": [{
                                "id": 430680,
                                "address": "0.0.0.0:1935",
                                "remote_address": "101.228.192.22",
                                "time": 82155,
                                "flashver": "LNX 9,0,124,2",
                                "dropped": 0,
                                "avsync": 4294967284,
                                "timestamp": 0,
                                "active": true,
                                "publishing": false,
                                "relay": false,
                                "interprocess": false,
                                "protocol": "rtmp"
                            }, {
                                "id": 0,
                                "address": "0.0.0.0:1935",
                                "remote_address": "",
                                "time": 102638,
                                "flashver": "FMLE/3.0 (compatible; Lavf58.77.100)",
                                "dropped": 0,
                                "avsync": 4294967284,
                                "timestamp": 0,
                                "active": true,
                                "publishing": false,
                                "relay": true,
                                "interprocess": false,
                                "protocol": "rtmp"
                            }, {
                                "id": 430653,
                                "address": "0.0.0.0:1935",
                                "remote_address": "101.228.192.22",
                                "time": 102917,
                                "flashver": "FMLE/3.0 (compatible; Lavf58.77.100)",
                                "dropped": 0,
                                "avsync": 4294967284,
                                "timestamp": 102666,
                                "active": true,
                                "publishing": true,
                                "relay": false,
                                "interprocess": false,
                                "protocol": "rtmp"
                            }],
                        "meta": {
                            "video": {
                                "width": 1920,
                                "height": 1080,
                                "frame_rate": "24.0",
                                "codec": "H264",
                                "profile": "High",
                                "compat": "4.0"
                            },
                            "audio": {
                                "codec": "AAC",
                                "profile": "LC",
                                "channels": 6,
                                "sample_rate": 48000
                            }
                        },
                        "total_clients": 1,
                        "rtmp_clients": 1,
                        "flv_clients": 0,
                        "hls_clients": 0,
                        "ts_clients": 0,
                        "active": true
                    }],
                "total_clients": 1,
                "rtmp_clients": 1,
                "flv_clients": 0,
                "hls_clients": 0,
                "ts_clients": 0
            }]
    }
}]

标签:clients,worker,module,pingos,nginx,rtmp,进程,array,客户端
来源: https://blog.csdn.net/impingo/article/details/115828424