Nginx模块之———— RTMP 模块的在线统计功能 stat 数据流数据的获取(不同节点则获取的方式不同)
一、目前只有一个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; }
以上为在录像时候遇到问题,已经解决!
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构