Nginx模块之———— RTMP 模块的在线统计功能 stat 数据流数据的获取(不同节点则获取的方式不同)

简介: 一、目前只有一个Live节点存在 单节点获取方式如下: public function getStreamByIp($outerIP, $streamName) { //查询录像模块的IP地址外网,根据这个可以查看到相应的流 $url = $outerIP .

一、目前只有一个Live节点存在 

单节点获取方式如下:

 public function getStreamByIp($outerIP, $streamName)
    {
        //查询录像模块的IP地址外网,根据这个可以查看到相应的流
        $url = $outerIP . "/rtmp/stat";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        $output = curl_exec($ch);
        curl_close($ch);
        //所有流的信息,解析Xml
        $outputs = $this->FromXml($output);
//        $streamInfo = $outputs['server']['application']['live']['stream'];
        $streamInfo = $outputs['server']['application']['live']['stream'];

        if (array_key_exists("name", $streamInfo)) {
            //判断该设备是否在这个数组中,真:获取这个设备的所有打流信息
            if ($streamName == $streamInfo['name']) {
                $totalInfo['status'] = 200;
                $totalInfo['message'] = 'The server is normal and is currently streaming';
                $totalInfo['dataList']['name'] = $streamInfo['name'];
                $totalInfo['dataList']['bw_in'] = $streamInfo['bw_in'];
                $totalInfo['dataList']['bw_out'] = $streamInfo['bw_out'];

            } else {
                $totalInfo['status'] = 500;
                $totalInfo['message'] = 'The server has a problem or is not currently streaming information 1';
                $totalInfo['dataList']['name'] = $streamName;
                $totalInfo['dataList']['bw_in'] = 0;
                $totalInfo['dataList']['bw_out'] = 0;
            }
        } else {
            //存放所有的设备号到一个数组中
            foreach ($streamInfo as $key => $val) {
                $deviceInfo[] = $val['name'];
            }
            //判断该设备是否在这个数组中,真:获取这个设备的所有打流信息
            if (in_array($streamName, $deviceInfo)) {
                $totalInfo['status'] = 200;
                $totalInfo['message'] = 'The server is normal and is currently streaming';
                foreach ($streamInfo as $val) {
                    if ($val['name'] == $streamName) {
                        $totalInfo['dataList']['name'] = $val['name'];
                        $totalInfo['dataList']['bw_in'] = $val['bw_in'];
                        $totalInfo['dataList']['bw_out'] = $val['bw_out'];
                    }
                }
            } else {
                $totalInfo['status'] = 500;
                $totalInfo['message'] = 'The server has a problem or is not currently streaming information 2';
                $totalInfo['dataList']['name'] = $streamName;
                $totalInfo['dataList']['bw_in'] = 0;
                $totalInfo['dataList']['bw_out'] = 0;
            }
        }
        return $totalInfo;
    }

二、目前有两个节点Live节点和Live2节点存在 

 多节点获取方式如下:

 public function getStreamByIp($outerIP, $streamName)
    {
        //查询录像模块的IP地址外网,根据这个可以查看到相应的流
        $url = $outerIP . "/rtmp/stat";
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        $output = curl_exec($ch);
        curl_close($ch);
        //所有流的信息,解析Xml
        $outputs = $this->FromXml($output);
        $streamInfo = $outputs['server']['application'][0]['live']['stream'];  //主要看这里哦!!!!!!!!!!!!!!!!这里获取的时候是一个二维数组

        if (array_key_exists("name", $streamInfo)) {
            //判断该设备是否在这个数组中,真:获取这个设备的所有打流信息
            if ($streamName == $streamInfo['name']) {
                $totalInfo['status'] = 200;
                $totalInfo['message'] = 'The server is normal and is currently streaming';
                $totalInfo['dataList']['name'] = $streamInfo['name'];
                $totalInfo['dataList']['bw_in'] = $streamInfo['bw_in'];
                $totalInfo['dataList']['bw_out'] = $streamInfo['bw_out'];

            } else {
                $totalInfo['status'] = 500;
                $totalInfo['message'] = 'The server has a problem or is not currently streaming information 1';
                $totalInfo['dataList']['name'] = $streamName;
                $totalInfo['dataList']['bw_in'] = 0;
                $totalInfo['dataList']['bw_out'] = 0;
            }
        } else {
            //存放所有的设备号到一个数组中
            foreach ($streamInfo as $key => $val) {
                $deviceInfo[] = $val['name'];
            }
            //判断该设备是否在这个数组中,真:获取这个设备的所有打流信息
            if (in_array($streamName, $deviceInfo)) {
                $totalInfo['status'] = 200;
                $totalInfo['message'] = 'The server is normal and is currently streaming';
                foreach ($streamInfo as $val) {
                    if ($val['name'] == $streamName) {
                        $totalInfo['dataList']['name'] = $val['name'];
                        $totalInfo['dataList']['bw_in'] = $val['bw_in'];
                        $totalInfo['dataList']['bw_out'] = $val['bw_out'];
                    }
                }
            } else {
                $totalInfo['status'] = 500;
                $totalInfo['message'] = 'The server has a problem or is not currently streaming information 2';
                $totalInfo['dataList']['name'] = $streamName;
                $totalInfo['dataList']['bw_in'] = 0;
                $totalInfo['dataList']['bw_out'] = 0;
            }
        }
        return $totalInfo;
    }

三、解析Xml

    public function FromXml($xml)
    {
        if (!$xml) {
            $totalInfo['status'] = 500;
            $totalInfo['message'] = '没有该设备的相应信息';
            $totalInfo['dataList'] = null;
            return $totalInfo;
        }
        //将XML转为array
        $values = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        return $values;
    }

以上为在录像时候遇到问题,已经解决!

 

目录
相关文章
|
2月前
|
负载均衡 安全 应用服务中间件
nginx的强大功能和如何使用?
nginx的强大功能和如何使用?
68 2
|
2月前
|
应用服务中间件 nginx C++
nginx的cgi模块
nginx的cgi模块
35 0
|
3月前
|
应用服务中间件 nginx
一文搞定Nginx配置RTMP!
一文搞定Nginx配置RTMP!
144 3
|
4月前
|
缓存 应用服务中间件 nginx
安装nginx-http-flv-module模块
本文介绍如何为Nginx安装`nginx-http-flv-module`模块。此模块基于`nginx-rtmp-module`二次开发,不仅具备原模块的所有功能,还支持HTTP-FLV播放、GOP缓存、虚拟主机等功能。安装步骤包括:确认Nginx版本、下载相应版本的Nginx与模块源码、重新编译Nginx并加入新模块、验证模块安装成功。特别注意,此模块已包含`nginx-rtmp-module`功能,无需重复编译安装。
189 1
|
4月前
|
负载均衡 应用服务中间件 Linux
在Linux中,常用的 Nginx 模块有哪些,常来做什么?
在Linux中,常用的 Nginx 模块有哪些,常来做什么?
|
4月前
|
应用服务中间件 Linux nginx
在Linux中,如何统计ip访问情况?分析 nginx 访问日志?如何找出访问页面数量在前十位的ip?
在Linux中,如何统计ip访问情况?分析 nginx 访问日志?如何找出访问页面数量在前十位的ip?
|
4月前
|
缓存 负载均衡 安全
介绍一下Nginx的反向代理功能吧
【8月更文挑战第22天】介绍一下Nginx的反向代理功能吧
74 0
|
4月前
|
缓存 安全 应用服务中间件
Nginx的反向代理功能有哪些应用场景呢
【8月更文挑战第22天】Nginx的反向代理功能有哪些应用场景呢
213 0
|
应用服务中间件 Linux 网络安全
虚拟机Centos下载安装Nginx并安装ssl模块——小白教程
虚拟机Centos下载安装Nginx并安装ssl模块——小白教程
409 0
|
应用服务中间件 nginx
安装nginx-rtmp-module模块与配置
安装nginx-rtmp-module模块与配置