From c1672728d373ff6e03b42d229a826d24270372ee Mon Sep 17 00:00:00 2001 From: lin <648540858@qq.com> Date: Tue, 16 Sep 2025 10:52:58 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96zlm=E7=BB=93=E6=9E=9C?= =?UTF-8?q?=E5=B0=81=E8=A3=85=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/impl/PlatformServiceImpl.java | 2 + .../iot/vmp/media/zlm/AssistRESTfulUtils.java | 2 +- .../media/zlm/ZLMMediaNodeServerService.java | 204 ++++++------- .../zlm/ZLMMediaServerStatusManager.java | 23 +- .../iot/vmp/media/zlm/ZLMRESTfulUtils.java | 283 ++++++++++++++---- .../iot/vmp/media/zlm/ZLMServerFactory.java | 74 ++--- .../iot/vmp/media/zlm/dto/ZLMResult.java | 28 +- 7 files changed, 389 insertions(+), 227 deletions(-) diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/service/impl/PlatformServiceImpl.java b/src/main/java/com/genersoft/iot/vmp/gb28181/service/impl/PlatformServiceImpl.java index aa3bf66fb..2c983773b 100755 --- a/src/main/java/com/genersoft/iot/vmp/gb28181/service/impl/PlatformServiceImpl.java +++ b/src/main/java/com/genersoft/iot/vmp/gb28181/service/impl/PlatformServiceImpl.java @@ -398,6 +398,8 @@ public class PlatformServiceImpl implements IPlatformService, CommandLineRunner SipTransactionInfo transactionInfo = statusTaskRunner.getRegisterTransactionInfo(platformInDb.getServerGBId()); // 注销后出发平台离线, 如果是启用的平台,那么下次丢失检测会检测到并重新注册上线 sendUnRegister(platformInDb, transactionInfo); + }else if (platform.isEnable()) { + sendRegister(platform, null); } return false; diff --git a/src/main/java/com/genersoft/iot/vmp/media/zlm/AssistRESTfulUtils.java b/src/main/java/com/genersoft/iot/vmp/media/zlm/AssistRESTfulUtils.java index 6a2b7d247..23b8b374e 100644 --- a/src/main/java/com/genersoft/iot/vmp/media/zlm/AssistRESTfulUtils.java +++ b/src/main/java/com/genersoft/iot/vmp/media/zlm/AssistRESTfulUtils.java @@ -209,7 +209,7 @@ public class AssistRESTfulUtils { if (response.isSuccessful()) { try { String responseStr = Objects.requireNonNull(response.body()).string(); - callback.run(JSON.parseObject(responseStr)); + callback.run(responseStr); } catch (IOException e) { log.error(String.format("[ %s ]请求失败: %s", url, e.getMessage())); } diff --git a/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMMediaNodeServerService.java b/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMMediaNodeServerService.java index 5d3b6fe87..3c0c12082 100644 --- a/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMMediaNodeServerService.java +++ b/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMMediaNodeServerService.java @@ -11,8 +11,7 @@ import com.genersoft.iot.vmp.gb28181.bean.SendRtpInfo; import com.genersoft.iot.vmp.media.bean.MediaInfo; import com.genersoft.iot.vmp.media.bean.MediaServer; import com.genersoft.iot.vmp.media.service.IMediaNodeServerService; -import com.genersoft.iot.vmp.media.zlm.dto.ZLMResult; -import com.genersoft.iot.vmp.media.zlm.dto.ZLMServerConfig; +import com.genersoft.iot.vmp.media.zlm.dto.*; import com.genersoft.iot.vmp.streamProxy.bean.StreamProxy; import com.genersoft.iot.vmp.vmanager.bean.ErrorCode; import com.genersoft.iot.vmp.vmanager.bean.WVPResult; @@ -72,10 +71,9 @@ public class ZLMMediaNodeServerService implements IMediaNodeServerService { if (mediaServer == null) { return false; } - ZLMResult zlmResult = zlmresTfulUtils.getMediaServerConfig(mediaServer); - if (zlmResult != null) { - zlmResult.getData() - JSONArray data = zlmResult.getJSONArray("data"); + ZLMResult> mediaServerConfig = zlmresTfulUtils.getMediaServerConfig(mediaServer); + if (mediaServerConfig != null) { + List data = mediaServerConfig.getData(); if (data != null && !data.isEmpty()) { ZLMServerConfig zlmServerConfig= JSON.parseObject(JSON.toJSONString(data.get(0)), ZLMServerConfig.class); return zlmServerConfig.getGeneralMediaServerId().equals(mediaServer.getId()); @@ -102,15 +100,15 @@ public class ZLMMediaNodeServerService implements IMediaNodeServerService { mediaServer.setFlvPort(port); mediaServer.setWsFlvPort(port); mediaServer.setSecret(secret); - JSONObject responseJSON = zlmresTfulUtils.getMediaServerConfig(mediaServer); - if (responseJSON == null) { + ZLMResult> mediaServerConfigResult = zlmresTfulUtils.getMediaServerConfig(mediaServer); + if (mediaServerConfigResult == null) { throw new ControllerException(ErrorCode.ERROR100.getCode(), "连接失败"); } - JSONArray data = responseJSON.getJSONArray("data"); - if (data == null) { + List configList = mediaServerConfigResult.getData(); + if (configList == null) { throw new ControllerException(ErrorCode.ERROR100.getCode(), "读取配置失败"); } - ZLMServerConfig zlmServerConfig = JSON.parseObject(JSON.toJSONString(data.get(0)), ZLMServerConfig.class); + ZLMServerConfig zlmServerConfig = JSON.parseObject(JSON.toJSONString(configList.get(0)), ZLMServerConfig.class); if (zlmServerConfig == null) { throw new ControllerException(ErrorCode.ERROR100.getCode(), "读取配置失败"); } @@ -140,12 +138,12 @@ public class ZLMMediaNodeServerService implements IMediaNodeServerService { if (!ObjectUtils.isEmpty(ssrc)) { param.put("ssrc", ssrc); } - JSONObject jsonObject = zlmresTfulUtils.stopSendRtp(mediaInfo, param); - if (jsonObject.getInteger("code") != null && jsonObject.getInteger("code") == 0) { + ZLMResult zlmResult = zlmresTfulUtils.stopSendRtp(mediaInfo, param); + if (zlmResult.getCode() == 0) { log.info("[停止发流] 成功: 参数:{}", JSON.toJSONString(param)); return true; }else { - log.info("停止发流结果: {}, 参数:{}", jsonObject.getString("msg"), JSON.toJSONString(param)); + log.info("停止发流结果: {}, 参数:{}", zlmResult.getMsg(), JSON.toJSONString(param)); return false; } } @@ -159,9 +157,9 @@ public class ZLMMediaNodeServerService implements IMediaNodeServerService { if (!ObjectUtils.isEmpty(ssrc)) { param.put("ssrc", ssrc); } - JSONObject jsonObject = zlmresTfulUtils.stopSendRtp(mediaInfo, param); - if (jsonObject == null || jsonObject.getInteger("code") != 0 ) { - log.error("停止发流失败: {}, 参数:{}", jsonObject.getString("msg"), JSON.toJSONString(param)); + ZLMResult zlmResult = zlmresTfulUtils.stopSendRtp(mediaInfo, param); + if (zlmResult.getCode() != 0 ) { + log.error("停止发流失败: {}, 参数:{}", zlmResult.getMsg(), JSON.toJSONString(param)); return false; } return true; @@ -170,12 +168,12 @@ public class ZLMMediaNodeServerService implements IMediaNodeServerService { @Override public boolean deleteRecordDirectory(MediaServer mediaServer, String app, String stream, String date, String fileName) { log.info("[zlm-deleteRecordDirectory] 删除磁盘文件, server: {} {}:{}->{}/{}", mediaServer.getId(), app, stream, date, fileName); - JSONObject jsonObject = zlmresTfulUtils.deleteRecordDirectory(mediaServer, app, + ZLMResult zlmResult = zlmresTfulUtils.deleteRecordDirectory(mediaServer, app, stream, date, fileName); - if (jsonObject.getInteger("code") == 0) { + if (zlmResult.getCode() == 0) { return true; }else { - log.info("[zlm-deleteRecordDirectory] 删除磁盘文件错误, server: {} {}:{}->{}/{}, 结果: {}", mediaServer.getId(), app, stream, date, fileName, jsonObject); + log.info("[zlm-deleteRecordDirectory] 删除磁盘文件错误, server: {} {}:{}->{}/{}, 结果: {}", mediaServer.getId(), app, stream, date, fileName, zlmResult); throw new ControllerException(ErrorCode.ERROR100.getCode(), "删除磁盘文件失败"); } } @@ -183,15 +181,14 @@ public class ZLMMediaNodeServerService implements IMediaNodeServerService { @Override public List getMediaList(MediaServer mediaServer, String app, String stream, String callId) { List streamInfoList = new ArrayList<>(); - ZLMResult zlmResult = zlmresTfulUtils.getMediaList(mediaServer, app, stream); + ZLMResult zlmResult = zlmresTfulUtils.getMediaList(mediaServer, app, stream); if (zlmResult != null) { if (zlmResult.getCode() == 0) { - ZLMResult result = (ZLMResult)zlmResult; - if (result.getData() == null) { + if (zlmResult.getData() == null) { return streamInfoList; } - for (int i = 0; i < result.getData().size(); i++) { - JSONObject mediaJSON = result.getData().getJSONObject(0); + for (int i = 0; i < zlmResult.getData().size(); i++) { + JSONObject mediaJSON = zlmResult.getData().getJSONObject(0); MediaInfo mediaInfo = MediaInfo.getInstance(mediaJSON, mediaServer, userSetting.getServerId()); StreamInfo streamInfo = getStreamInfoByAppAndStream(mediaServer, mediaInfo.getApp(), mediaInfo.getStream(), mediaInfo, callId, true); if (streamInfo != null) { @@ -251,9 +248,9 @@ public class ZLMMediaNodeServerService implements IMediaNodeServerService { @Override public Boolean connectRtpServer(MediaServer mediaServer, String address, int port, String stream) { - JSONObject jsonObject = zlmresTfulUtils.connectRtpServer(mediaServer, address, port, stream); - log.info("[TCP主动连接对方] 结果: {}", jsonObject); - return jsonObject.getInteger("code") == 0; + ZLMResult zlmResult = zlmresTfulUtils.connectRtpServer(mediaServer, address, port, stream); + log.info("[TCP主动连接对方] 结果: {}", zlmResult); + return zlmResult.getCode() == 0; } @Override @@ -269,34 +266,34 @@ public class ZLMMediaNodeServerService implements IMediaNodeServerService { @Override public MediaInfo getMediaInfo(MediaServer mediaServer, String app, String stream) { - JSONObject jsonObject = zlmresTfulUtils.getMediaInfo(mediaServer, app, "rtsp", stream); - if (jsonObject.getInteger("code") != 0) { + ZLMResult zlmResult = zlmresTfulUtils.getMediaInfo(mediaServer, app, "rtsp", stream); + if (zlmResult.getCode() != 0) { return null; } - return MediaInfo.getInstance(jsonObject, mediaServer, userSetting.getServerId()); + return MediaInfo.getInstance(zlmResult.getData(), mediaServer, userSetting.getServerId()); } @Override public Boolean pauseRtpCheck(MediaServer mediaServer, String streamKey) { - JSONObject jsonObject = zlmresTfulUtils.pauseRtpCheck(mediaServer, streamKey); - return jsonObject.getInteger("code") == 0; + ZLMResult zlmResult = zlmresTfulUtils.pauseRtpCheck(mediaServer, streamKey); + return zlmResult.getCode() == 0; } @Override public Boolean resumeRtpCheck(MediaServer mediaServer, String streamKey) { - JSONObject jsonObject = zlmresTfulUtils.resumeRtpCheck(mediaServer, streamKey); - return jsonObject.getInteger("code") == 0; + ZLMResult zlmResult = zlmresTfulUtils.resumeRtpCheck(mediaServer, streamKey); + return zlmResult.getCode() == 0; } @Override public String getFfmpegCmd(MediaServer mediaServer, String cmdKey) { - JSONObject jsonObject = zlmresTfulUtils.getMediaServerConfig(mediaServer); - if (jsonObject.getInteger("code") != 0) { + ZLMResult> mediaServerConfigResult = zlmresTfulUtils.getMediaServerConfig(mediaServer); + if (mediaServerConfigResult == null || mediaServerConfigResult.getCode() != 0) { log.warn("[getFfmpegCmd] 获取流媒体配置失败"); throw new ControllerException(ErrorCode.ERROR100.getCode(), "获取流媒体配置失败"); } - JSONArray dataArray = jsonObject.getJSONArray("data"); - JSONObject mediaServerConfig = dataArray.getJSONObject(0); + List data = mediaServerConfigResult.getData(); + JSONObject mediaServerConfig = data.get(0); if (ObjectUtils.isEmpty(cmdKey)) { cmdKey = "ffmpeg.cmd"; } @@ -306,42 +303,42 @@ public class ZLMMediaNodeServerService implements IMediaNodeServerService { @Override public WVPResult addStreamProxy(MediaServer mediaServer, String app, String stream, String url, boolean enableAudio, boolean enableMp4, String rtpType, Integer timeout) { - JSONObject jsonObject = zlmresTfulUtils.addStreamProxy(mediaServer, app, stream, url, enableAudio, enableMp4, rtpType, timeout); - if (jsonObject.getInteger("code") != 0) { + ZLMResult zlmResult = zlmresTfulUtils.addStreamProxy(mediaServer, app, stream, url, enableAudio, enableMp4, rtpType, timeout); + if (zlmResult.getCode() != 0) { return WVPResult.fail(ErrorCode.ERROR100.getCode(), "添加代理失败"); }else { - JSONObject data = jsonObject.getJSONObject("data"); + StreamProxyResult data = zlmResult.getData(); if (data == null) { - return WVPResult.fail(ErrorCode.ERROR100.getCode(), "代理结果异常: " + jsonObject); + return WVPResult.fail(ErrorCode.ERROR100.getCode(), "代理结果异常"); }else { - return WVPResult.success(data.getString("key")); + return WVPResult.success(data.getKey()); } } } @Override public Boolean delFFmpegSource(MediaServer mediaServer, String streamKey) { - JSONObject jsonObject = zlmresTfulUtils.delFFmpegSource(mediaServer, streamKey); - return jsonObject.getInteger("code") == 0; + ZLMResult flagDataZLMResult = zlmresTfulUtils.delFFmpegSource(mediaServer, streamKey); + return flagDataZLMResult != null && flagDataZLMResult.getCode() == 0; } @Override public Boolean delStreamProxy(MediaServer mediaServer, String streamKey) { - JSONObject jsonObject = zlmresTfulUtils.delStreamProxy(mediaServer, streamKey); - return jsonObject.getInteger("code") == 0; + ZLMResult flagDataZLMResult = zlmresTfulUtils.delStreamProxy(mediaServer, streamKey); + return flagDataZLMResult != null && flagDataZLMResult.getCode() == 0; } @Override public Map getFFmpegCMDs(MediaServer mediaServer) { Map result = new HashMap<>(); - JSONObject mediaServerConfigResuly = zlmresTfulUtils.getMediaServerConfig(mediaServer); - if (mediaServerConfigResuly != null && mediaServerConfigResuly.getInteger("code") == 0 - && mediaServerConfigResuly.getJSONArray("data").size() > 0){ - JSONObject mediaServerConfig = mediaServerConfigResuly.getJSONArray("data").getJSONObject(0); + ZLMResult> mediaServerConfigResult = zlmresTfulUtils.getMediaServerConfig(mediaServer); + if (mediaServerConfigResult != null && mediaServerConfigResult.getCode() == 0 + && !mediaServerConfigResult.getData().isEmpty()){ + JSONObject jsonObject = mediaServerConfigResult.getData().get(0); - for (String key : mediaServerConfig.keySet()) { + for (String key : jsonObject.keySet()) { if (key.startsWith("ffmpeg.cmd")){ - result.put(key, mediaServerConfig.getString(key)); + result.put(key, jsonObject.getString(key)); } } } @@ -374,15 +371,13 @@ public class ZLMMediaNodeServerService implements IMediaNodeServerService { param.put("dst_port", sendRtpItem.getPort()); } - JSONObject jsonObject = zlmServerFactory.startSendRtpPassive(mediaServer, param, null); - if (jsonObject == null || jsonObject.getInteger("code") != 0 ) { - log.error("启动监听TCP被动推流失败: {}, 参数:{}", jsonObject.getString("msg"), JSON.toJSONString(param)); - throw new ControllerException(jsonObject.getInteger("code"), jsonObject.getString("msg")); + ZLMResult zlmResult = zlmServerFactory.startSendRtpPassive(mediaServer, param, null); + if (zlmResult.getCode() != 0 ) { + log.error("启动监听TCP被动推流失败: {}, 参数:{}", zlmResult.getMsg(), JSON.toJSONString(param)); + throw new ControllerException(zlmResult.getCode(), zlmResult.getMsg()); } - log.info("调用ZLM-TCP被动推流接口, 结果: {}", jsonObject); - log.info("启动监听TCP被动推流成功[ {}/{} ],{}->{}:{}, " , sendRtpItem.getApp(), sendRtpItem.getStream(), - jsonObject.getString("local_port"), param.get("dst_url"), param.get("dst_port")); - return jsonObject.getInteger("local_port"); + log.info("调用ZLM-TCP被动推流接口成功: 本地端口: {}", zlmResult.getLocal_port()); + return zlmResult.getLocal_port(); } @Override @@ -404,13 +399,13 @@ public class ZLMMediaNodeServerService implements IMediaNodeServerService { } param.put("dst_url", sendRtpItem.getIp()); param.put("dst_port", sendRtpItem.getPort()); - JSONObject jsonObject = zlmresTfulUtils.startSendRtp(mediaServer, param); - if (jsonObject == null ) { + ZLMResult zlmResult = zlmresTfulUtils.startSendRtp(mediaServer, param); + if (zlmResult == null ) { throw new ControllerException(ErrorCode.ERROR100.getCode(), "连接zlm失败"); - }else if (jsonObject.getInteger("code") != 0) { - throw new ControllerException(jsonObject.getInteger("code"), jsonObject.getString("msg")); + }else if (zlmResult.getCode() != 0) { + throw new ControllerException(zlmResult.getCode(), zlmResult.getMsg()); } - log.info("[推流结果]:{} ,参数: {}",jsonObject, JSONObject.toJSONString(param)); + log.info("[推流结果]:{} ,参数: {}", zlmResult, JSONObject.toJSONString(param)); } @Override @@ -425,15 +420,13 @@ public class ZLMMediaNodeServerService implements IMediaNodeServerService { param.put("only_audio", sendRtpItem.isOnlyAudio() ? "1" : "0"); param.put("recv_stream_id", sendRtpItem.getReceiveStream()); param.put("enable_origin_recv_limit", "1"); - JSONObject jsonObject = zlmServerFactory.startSendRtpTalk(mediaServer, param, null); - if (jsonObject == null || jsonObject.getInteger("code") != 0 ) { - log.error("启动监听TCP被动推流失败: {}, 参数:{}", jsonObject.getString("msg"), JSON.toJSONString(param)); - throw new ControllerException(jsonObject.getInteger("code"), jsonObject.getString("msg")); + ZLMResult zlmResult = zlmServerFactory.startSendRtpTalk(mediaServer, param, null); + if (zlmResult.getCode() != 0 ) { + log.error("启动监听TCP被动推流失败: {}, 参数:{}", zlmResult.getMsg(), JSON.toJSONString(param)); + throw new ControllerException(zlmResult.getCode(), zlmResult.getMsg()); } - log.info("调用ZLM-TCP被动推流接口, 结果: {}", jsonObject); - log.info("启动监听TCP被动推流成功[ {}/{} ],{}->{}:{}, " , sendRtpItem.getApp(), sendRtpItem.getStream(), - jsonObject.getString("local_port"), param.get("dst_url"), param.get("dst_port")); - return jsonObject.getInteger("local_port"); + log.info("调用ZLM-TCP被动推流接口, 成功 本地端口: {}", zlmResult.getLocal_port()); + return zlmResult.getLocal_port(); } @Override @@ -488,28 +481,26 @@ public class ZLMMediaNodeServerService implements IMediaNodeServerService { closeStreams(mediaServer, streamProxy.getApp(), streamProxy.getStream()); } - JSONObject jsonObject = null; + ZLMResult zlmResult = null; if ("ffmpeg".equalsIgnoreCase(streamProxy.getType())){ if (streamProxy.getTimeout() == 0) { streamProxy.setTimeout(15); } - jsonObject = zlmresTfulUtils.addFFmpegSource(mediaServer, streamProxy.getSrcUrl().trim(), dstUrl, + zlmResult = zlmresTfulUtils.addFFmpegSource(mediaServer, streamProxy.getSrcUrl().trim(), dstUrl, streamProxy.getTimeout(), streamProxy.isEnableAudio(), streamProxy.isEnableMp4(), streamProxy.getFfmpegCmdKey()); }else { - jsonObject = zlmresTfulUtils.addStreamProxy(mediaServer, streamProxy.getApp(), streamProxy.getStream(), streamProxy.getSrcUrl().trim(), + zlmResult = zlmresTfulUtils.addStreamProxy(mediaServer, streamProxy.getApp(), streamProxy.getStream(), streamProxy.getSrcUrl().trim(), streamProxy.isEnableAudio(), streamProxy.isEnableMp4(), streamProxy.getRtspType(), streamProxy.getTimeout()); } - if (jsonObject == null) { - throw new ControllerException(ErrorCode.ERROR100.getCode(), "请求失败"); - }else if (jsonObject.getInteger("code") != 0) { - throw new ControllerException(jsonObject.getInteger("code"), jsonObject.getString("msg")); + if (zlmResult.getCode() != 0) { + throw new ControllerException(zlmResult.getCode(), zlmResult.getMsg()); }else { - JSONObject data = jsonObject.getJSONObject("data"); + StreamProxyResult data = zlmResult.getData(); if (data == null) { - throw new ControllerException(jsonObject.getInteger("code"), "代理结果异常: " + jsonObject); + throw new ControllerException(zlmResult.getCode(), "代理结果异常: " + zlmResult); }else { - return data.getString("key"); + return data.getKey(); } } } @@ -535,62 +526,61 @@ public class ZLMMediaNodeServerService implements IMediaNodeServerService { @Override public void stopProxy(MediaServer mediaServer, String streamKey, String type) { - JSONObject jsonObject = zlmresTfulUtils.delStreamProxy(mediaServer, streamKey); - if (jsonObject == null) { + ZLMResult zlmResult = zlmresTfulUtils.delStreamProxy(mediaServer, streamKey); + if (zlmResult == null) { throw new ControllerException(ErrorCode.ERROR100.getCode(), "请求失败"); - }else if (jsonObject.getInteger("code") != 0) { - throw new ControllerException(jsonObject.getInteger("code"), jsonObject.getString("msg")); + }else if (zlmResult.getCode() != 0) { + throw new ControllerException(zlmResult.getCode(), zlmResult.getMsg()); } } @Override public List listRtpServer(MediaServer mediaServer) { - JSONObject jsonObject = zlmresTfulUtils.listRtpServer(mediaServer); + ZLMResult> zlmResult = zlmresTfulUtils.listRtpServer(mediaServer); List result = new ArrayList<>(); - if (jsonObject == null || jsonObject.getInteger("code") != 0) { + if (zlmResult.getCode() != 0) { return result; } - JSONArray data = jsonObject.getJSONArray("data"); + List data = zlmResult.getData(); if (data == null || data.isEmpty()) { return result; } - for (int i = 0; i < data.size(); i++) { - JSONObject dataJSONObject = data.getJSONObject(i); - result.add(dataJSONObject.getString("stream_id")); + for (RtpServerResult datum : data) { + result.add(datum.getStream_id()); } return result; } @Override public void loadMP4File(MediaServer mediaServer, String app, String stream, String datePath) { - JSONObject jsonObject = zlmresTfulUtils.loadMP4File(mediaServer, app, stream, datePath); - if (jsonObject == null) { + ZLMResult zlmResult = zlmresTfulUtils.loadMP4File(mediaServer, app, stream, datePath); + if (zlmResult == null) { throw new ControllerException(ErrorCode.ERROR100.getCode(), "请求失败"); } - if (jsonObject.getInteger("code") != 0) { - throw new ControllerException(jsonObject.getInteger("code"), jsonObject.getString("msg")); + if (zlmResult.getCode() != 0) { + throw new ControllerException(zlmResult.getCode(), zlmResult.getMsg()); } } @Override public void seekRecordStamp(MediaServer mediaServer, String app, String stream, Double stamp, String schema) { - JSONObject jsonObject = zlmresTfulUtils.seekRecordStamp(mediaServer, app, stream, stamp, schema); - if (jsonObject == null) { + ZLMResult zlmResult = zlmresTfulUtils.seekRecordStamp(mediaServer, app, stream, stamp, schema); + if (zlmResult == null) { throw new ControllerException(ErrorCode.ERROR100.getCode(), "请求失败"); } - if (jsonObject.getInteger("code") != 0) { - throw new ControllerException(jsonObject.getInteger("code"), jsonObject.getString("msg")); + if (zlmResult.getCode() != 0) { + throw new ControllerException(zlmResult.getCode(), zlmResult.getMsg()); } } @Override public void setRecordSpeed(MediaServer mediaServer, String app, String stream, Integer speed, String schema) { - JSONObject jsonObject = zlmresTfulUtils.setRecordSpeed(mediaServer, app, stream, speed, schema); - if (jsonObject == null) { + ZLMResult zlmResult = zlmresTfulUtils.setRecordSpeed(mediaServer, app, stream, speed, schema); + if (zlmResult == null) { throw new ControllerException(ErrorCode.ERROR100.getCode(), "请求失败"); } - if (jsonObject.getInteger("code") != 0) { - throw new ControllerException(jsonObject.getInteger("code"), jsonObject.getString("msg")); + if (zlmResult.getCode() != 0) { + throw new ControllerException(zlmResult.getCode(), zlmResult.getMsg()); } } } diff --git a/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMMediaServerStatusManager.java b/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMMediaServerStatusManager.java index 39a9d974b..0281ffe45 100644 --- a/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMMediaServerStatusManager.java +++ b/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMMediaServerStatusManager.java @@ -1,7 +1,6 @@ package com.genersoft.iot.vmp.media.zlm; import com.alibaba.fastjson2.JSON; -import com.alibaba.fastjson2.JSONArray; import com.alibaba.fastjson2.JSONObject; import com.genersoft.iot.vmp.conf.DynamicTask; import com.genersoft.iot.vmp.gb28181.event.EventPublisher; @@ -9,6 +8,7 @@ import com.genersoft.iot.vmp.media.bean.MediaServer; import com.genersoft.iot.vmp.media.event.mediaServer.MediaServerChangeEvent; import com.genersoft.iot.vmp.media.event.mediaServer.MediaServerDeleteEvent; import com.genersoft.iot.vmp.media.service.IMediaServerService; +import com.genersoft.iot.vmp.media.zlm.dto.ZLMResult; import com.genersoft.iot.vmp.media.zlm.dto.ZLMServerConfig; import com.genersoft.iot.vmp.media.zlm.event.HookZlmServerKeepaliveEvent; import com.genersoft.iot.vmp.media.zlm.event.HookZlmServerStartEvent; @@ -23,6 +23,7 @@ import org.springframework.util.ObjectUtils; import java.io.File; import java.util.HashMap; +import java.util.List; import java.util.Map; import java.util.Objects; import java.util.concurrent.ConcurrentHashMap; @@ -136,13 +137,13 @@ public class ZLMMediaServerStatusManager { continue; } log.info("[ZLM-尝试连接] ID:{}, 地址: {}:{}", mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort()); - JSONObject responseJson = zlmresTfulUtils.getMediaServerConfig(mediaServerItem); + ZLMResult> mediaServerConfigResult = zlmresTfulUtils.getMediaServerConfig(mediaServerItem); ZLMServerConfig zlmServerConfig = null; - if (responseJson == null) { + if (mediaServerConfigResult == null) { log.info("[ZLM-尝试连接]失败, ID:{}, 地址: {}:{}", mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort()); continue; } - JSONArray data = responseJson.getJSONArray("data"); + List data = mediaServerConfigResult.getData(); if (data == null || data.isEmpty()) { log.info("[ZLM-尝试连接]失败, ID:{}, 地址: {}:{}", mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort()); }else { @@ -158,14 +159,14 @@ public class ZLMMediaServerStatusManager { continue; } log.info("[ZLM-尝试连接] ID:{}, 地址: {}:{}", mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort()); - JSONObject responseJson = zlmresTfulUtils.getMediaServerConfig(mediaServerItem); + ZLMResult> mediaServerConfig = zlmresTfulUtils.getMediaServerConfig(mediaServerItem); ZLMServerConfig zlmServerConfig = null; - if (responseJson == null) { + if (mediaServerConfig == null) { log.info("[ZLM-尝试连接]失败, ID:{}, 地址: {}:{}", mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort()); offlineZlmTimeMap.put(mediaServerItem.getId(), System.currentTimeMillis()); continue; } - JSONArray data = responseJson.getJSONArray("data"); + List data = mediaServerConfig.getData(); if (data == null || data.isEmpty()) { log.info("[ZLM-尝试连接]失败, ID:{}, 地址: {}:{}", mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort()); offlineZlmTimeMap.put(mediaServerItem.getId(), System.currentTimeMillis()); @@ -190,8 +191,8 @@ public class ZLMMediaServerStatusManager { eventPublisher.mediaServerOnlineEventPublish(mediaServerItem); if(mediaServerItem.isAutoConfig()) { if (config == null) { - JSONObject responseJSON = zlmresTfulUtils.getMediaServerConfig(mediaServerItem); - JSONArray data = responseJSON.getJSONArray("data"); + ZLMResult> mediaServerConfig = zlmresTfulUtils.getMediaServerConfig(mediaServerItem); + List data = mediaServerConfig.getData(); if (data != null && !data.isEmpty()) { config = JSON.parseObject(JSON.toJSONString(data.get(0)), ZLMServerConfig.class); } @@ -298,9 +299,9 @@ public class ZLMMediaServerStatusManager { param.put("record.appName", recordPathFile.getName()); } - JSONObject responseJSON = zlmresTfulUtils.setServerConfig(mediaServerItem, param); + ZLMResult zlmResult = zlmresTfulUtils.setServerConfig(mediaServerItem, param); - if (responseJSON != null && responseJSON.getInteger("code") == 0) { + if (zlmResult != null && zlmResult.getCode() == 0) { if (restart) { log.info("[媒体服务节点] 设置成功,开始重启以保证配置生效 {} -> {}:{}", mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort()); diff --git a/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRESTfulUtils.java b/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRESTfulUtils.java index 1b44a4e6a..a90e5732f 100755 --- a/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRESTfulUtils.java +++ b/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRESTfulUtils.java @@ -2,8 +2,8 @@ package com.genersoft.iot.vmp.media.zlm; import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSONArray; +import com.alibaba.fastjson2.JSONObject; import com.alibaba.fastjson2.TypeReference; -import com.genersoft.iot.vmp.media.bean.MediaInfo; import com.genersoft.iot.vmp.media.bean.MediaServer; import com.genersoft.iot.vmp.media.zlm.dto.*; import lombok.extern.slf4j.Slf4j; @@ -233,11 +233,16 @@ public class ZLMRESTfulUtils { if (response == null) { return ZLMResult.getFailForMediaServer(); }else { - return JSON.parseObject(response, ZLMResult.class); + ZLMResult zlmResult = JSON.parseObject(response, ZLMResult.class); + if (zlmResult == null) { + return ZLMResult.getFailForMediaServer(); + }else { + return zlmResult; + } } } - public ZLMResult getMediaList(MediaServer mediaServer, String app, String stream, String schema, RequestCallback callback){ + public ZLMResult getMediaList(MediaServer mediaServer, String app, String stream, String schema, ResultCallback callback){ Map param = new HashMap<>(); if (app != null) { param.put("app",app); @@ -249,19 +254,39 @@ public class ZLMRESTfulUtils { param.put("schema",schema); } param.put("vhost","__defaultVhost__"); - String response = sendPost(mediaServer, "getMediaList",param, callback); + String response = sendPost(mediaServer, "getMediaList",param, (responseStr -> { + if (callback == null) { + return; + } + if (responseStr == null) { + callback.run(ZLMResult.getFailForMediaServer()); + }else { + ZLMResult zlmResult = JSON.parseObject(responseStr, new TypeReference>() {}); + if (zlmResult == null) { + callback.run(ZLMResult.getFailForMediaServer()); + }else { + callback.run(zlmResult); + } + + } + })); if (response == null) { return ZLMResult.getFailForMediaServer(); }else { - return JSON.parseObject(response, new TypeReference>() {}); + ZLMResult zlmResult = JSON.parseObject(response, new TypeReference>() {}); + if (zlmResult == null) { + return ZLMResult.getFailForMediaServer(); + }else { + return zlmResult; + } } } - public ZLMResult getMediaList(MediaServer mediaServer, String app, String stream){ + public ZLMResult getMediaList(MediaServer mediaServer, String app, String stream){ return getMediaList(mediaServer, app, stream,null, null); } - public ZLMResult getMediaInfo(MediaServer mediaServer, String app, String schema, String stream){ + public ZLMResult getMediaInfo(MediaServer mediaServer, String app, String schema, String stream){ Map param = new HashMap<>(); param.put("app",app); param.put("schema",schema); @@ -272,7 +297,15 @@ public class ZLMRESTfulUtils { if (response == null) { return ZLMResult.getFailForMediaServer(); }else { - return JSON.parseObject(response, new TypeReference>() {}); + JSONObject jsonObject = JSON.parseObject(response); + if (jsonObject == null) { + return ZLMResult.getFailForMediaServer(); + }else { + ZLMResult zlmResult = new ZLMResult<>(); + zlmResult.setCode(0); + zlmResult.setData(jsonObject); + return zlmResult; + } } } @@ -283,11 +316,16 @@ public class ZLMRESTfulUtils { if (response == null) { return ZLMResult.getFailForMediaServer(); }else { - return JSON.parseObject(response, ZLMResult.class); + ZLMResult zlmResult = JSON.parseObject(response, ZLMResult.class); + if (zlmResult == null) { + return ZLMResult.getFailForMediaServer(); + }else { + return zlmResult; + } } } - public ZLMResult addFFmpegSource(MediaServer mediaServer, String src_url, String dst_url, Integer timeout_sec, + public ZLMResult addFFmpegSource(MediaServer mediaServer, String src_url, String dst_url, Integer timeout_sec, boolean enable_audio, boolean enable_mp4, String ffmpeg_cmd_key){ Map param = new HashMap<>(); param.put("src_url", src_url); @@ -300,11 +338,16 @@ public class ZLMRESTfulUtils { if (response == null) { return ZLMResult.getFailForMediaServer(); }else { - return JSON.parseObject(response, new TypeReference>() {}); + ZLMResult zlmResult = JSON.parseObject(response, new TypeReference>() {}); + if (zlmResult == null) { + return ZLMResult.getFailForMediaServer(); + }else { + return zlmResult; + } } } - public ZLMResult delFFmpegSource(MediaServer mediaServer, String key){ + public ZLMResult delFFmpegSource(MediaServer mediaServer, String key){ Map param = new HashMap<>(); param.put("key", key); @@ -312,28 +355,43 @@ public class ZLMRESTfulUtils { if (response == null) { return ZLMResult.getFailForMediaServer(); }else { - return JSON.parseObject(response, new TypeReference>() {}); + ZLMResult zlmResult = JSON.parseObject(response, new TypeReference>() {}); + if (zlmResult == null) { + return ZLMResult.getFailForMediaServer(); + }else { + return zlmResult; + } } } - public ZLMResult delStreamProxy(MediaServer mediaServer, String key){ + public ZLMResult delStreamProxy(MediaServer mediaServer, String key){ Map param = new HashMap<>(); param.put("key", key); String response = sendPost(mediaServer, "delStreamProxy",param, null); if (response == null) { return ZLMResult.getFailForMediaServer(); }else { - return JSON.parseObject(response, new TypeReference>() {}); + ZLMResult zlmResult = JSON.parseObject(response, new TypeReference>() {}); + if (zlmResult == null) { + return ZLMResult.getFailForMediaServer(); + }else { + return zlmResult; + } } } - public ZLMResult getMediaServerConfig(MediaServer mediaServer ){ + public ZLMResult> getMediaServerConfig(MediaServer mediaServer ){ String response = sendPost(mediaServer, "getServerConfig",null, null); if (response == null) { return ZLMResult.getFailForMediaServer(); }else { - return JSON.parseObject(response, new TypeReference>>() {}); + ZLMResult> zlmResult = JSON.parseObject(response, new TypeReference>>() {}); + if (zlmResult == null) { + return ZLMResult.getFailForMediaServer(); + }else { + return zlmResult; + } } } @@ -342,7 +400,12 @@ public class ZLMRESTfulUtils { if (response == null) { return ZLMResult.getFailForMediaServer(); }else { - return JSON.parseObject(response, ZLMResult.class); + ZLMResult zlmResult = JSON.parseObject(response, ZLMResult.class); + if (zlmResult == null) { + return ZLMResult.getFailForMediaServer(); + }else { + return zlmResult; + } } } @@ -351,7 +414,12 @@ public class ZLMRESTfulUtils { if (response == null) { return ZLMResult.getFailForMediaServer(); }else { - return JSON.parseObject(response, ZLMResult.class); + ZLMResult zlmResult = JSON.parseObject(response, ZLMResult.class); + if (zlmResult == null) { + return ZLMResult.getFailForMediaServer(); + }else { + return zlmResult; + } } } @@ -360,12 +428,20 @@ public class ZLMRESTfulUtils { if (response == null) { return ZLMResult.getFailForMediaServer(); }else { - return JSON.parseObject(response, ZLMResult.class); + ZLMResult zlmResult = JSON.parseObject(response, ZLMResult.class); + if (zlmResult == null) { + return ZLMResult.getFailForMediaServer(); + }else { + return zlmResult; + } } } public void closeRtpServer(MediaServer mediaServer, Map param, ResultCallback callback) { sendPost(mediaServer, "closeRtpServer",param, (response -> { + if (callback == null) { + return; + } if (response == null) { callback.run(ZLMResult.getFailForMediaServer()); }else { @@ -375,12 +451,17 @@ public class ZLMRESTfulUtils { } - public ZLMResult listRtpServer(MediaServer mediaServer) { + public ZLMResult> listRtpServer(MediaServer mediaServer) { String response = sendPost(mediaServer, "listRtpServer",null, null); if (response == null) { return ZLMResult.getFailForMediaServer(); }else { - return JSON.parseObject(response, new TypeReference>>() {}); + ZLMResult> zlmResult = JSON.parseObject(response, new TypeReference>>() {}); + if (zlmResult == null) { + return ZLMResult.getFailForMediaServer(); + }else { + return zlmResult; + } } } @@ -389,7 +470,12 @@ public class ZLMRESTfulUtils { if (response == null) { return ZLMResult.getFailForMediaServer(); }else { - return JSON.parseObject(response, ZLMResult.class); + ZLMResult zlmResult = JSON.parseObject(response, ZLMResult.class); + if (zlmResult == null) { + return ZLMResult.getFailForMediaServer(); + }else { + return zlmResult; + } } } @@ -398,12 +484,48 @@ public class ZLMRESTfulUtils { if (response == null) { return ZLMResult.getFailForMediaServer(); }else { - return JSON.parseObject(response, ZLMResult.class); + ZLMResult zlmResult = JSON.parseObject(response, ZLMResult.class); + if (zlmResult == null) { + return ZLMResult.getFailForMediaServer(); + }else { + return zlmResult; + } } } public ZLMResult startSendRtpPassive(MediaServer mediaServer, Map param, ResultCallback callback) { String response = sendPost(mediaServer, "startSendRtpPassive",param, (responseStr -> { + if (callback == null) { + return; + } + if (responseStr == null) { + callback.run(ZLMResult.getFailForMediaServer()); + }else { + ZLMResult zlmResult = JSON.parseObject(responseStr, ZLMResult.class); + if (zlmResult == null) { + callback.run(ZLMResult.getFailForMediaServer()); + }else { + callback.run(zlmResult); + } + } + })); + if (response == null) { + return ZLMResult.getFailForMediaServer(); + }else { + ZLMResult zlmResult = JSON.parseObject(response, ZLMResult.class); + if (zlmResult == null) { + return ZLMResult.getFailForMediaServer(); + }else { + return zlmResult; + } + } + } + + public ZLMResult startSendRtpTalk(MediaServer mediaServer, Map param, ResultCallback callback) { + String response = sendPost(mediaServer, "startSendRtpTalk",param, (responseStr -> { + if (callback == null) { + return; + } if (responseStr == null) { callback.run(ZLMResult.getFailForMediaServer()); }else { @@ -413,16 +535,12 @@ public class ZLMRESTfulUtils { if (response == null) { return ZLMResult.getFailForMediaServer(); }else { - return JSON.parseObject(response, ZLMResult.class); - } - } - - public ZLMResult startSendRtpTalk(MediaServer mediaServer, Map param, RequestCallback callback) { - String response = sendPost(mediaServer, "startSendRtpTalk",param, null); - if (response == null) { - return ZLMResult.getFailForMediaServer(); - }else { - return JSON.parseObject(response, ZLMResult.class); + ZLMResult zlmResult = JSON.parseObject(response, ZLMResult.class); + if (zlmResult == null) { + return ZLMResult.getFailForMediaServer(); + }else { + return zlmResult; + } } } @@ -431,7 +549,12 @@ public class ZLMRESTfulUtils { if (response == null) { return ZLMResult.getFailForMediaServer(); }else { - return JSON.parseObject(response, ZLMResult.class); + ZLMResult zlmResult = JSON.parseObject(response, ZLMResult.class); + if (zlmResult == null) { + return ZLMResult.getFailForMediaServer(); + }else { + return zlmResult; + } } } @@ -440,11 +563,16 @@ public class ZLMRESTfulUtils { if (response == null) { return ZLMResult.getFailForMediaServer(); }else { - return JSON.parseObject(response, ZLMResult.class); + ZLMResult zlmResult = JSON.parseObject(response, ZLMResult.class); + if (zlmResult == null) { + return ZLMResult.getFailForMediaServer(); + }else { + return zlmResult; + } } } - public ZLMResult addStreamProxy(MediaServer mediaServer, String app, String stream, String url, boolean enable_audio, boolean enable_mp4, String rtp_type, Integer timeOut) { + public ZLMResult addStreamProxy(MediaServer mediaServer, String app, String stream, String url, boolean enable_audio, boolean enable_mp4, String rtp_type, Integer timeOut) { Map param = new HashMap<>(); param.put("vhost", "__defaultVhost__"); param.put("app", app); @@ -461,11 +589,16 @@ public class ZLMRESTfulUtils { if (response == null) { return ZLMResult.getFailForMediaServer(); }else { - return JSON.parseObject(response, new TypeReference>() {}); + ZLMResult zlmResult = JSON.parseObject(response, new TypeReference>() {}); + if (zlmResult == null) { + return ZLMResult.getFailForMediaServer(); + }else { + return zlmResult; + } } } - public ZLMResult closeStreams(MediaServer mediaServer, String app, String stream) { + public ZLMResult closeStreams(MediaServer mediaServer, String app, String stream) { Map param = new HashMap<>(); param.put("vhost", "__defaultVhost__"); param.put("app", app); @@ -476,16 +609,26 @@ public class ZLMRESTfulUtils { if (response == null) { return ZLMResult.getFailForMediaServer(); }else { - return JSON.parseObject(response, new TypeReference>() {}); + ZLMResult zlmResult = JSON.parseObject(response, new TypeReference>() {}); + if (zlmResult == null) { + return ZLMResult.getFailForMediaServer(); + }else { + return zlmResult; + } } } - public ZLMResult getAllSession(MediaServer mediaServer) { + public ZLMResult> getAllSession(MediaServer mediaServer) { String response = sendPost(mediaServer, "getAllSession",null, null); if (response == null) { return ZLMResult.getFailForMediaServer(); }else { - return JSON.parseObject(response, new TypeReference>() {}); + ZLMResult> zlmResult = JSON.parseObject(response, new TypeReference>>() {}); + if (zlmResult == null) { + return ZLMResult.getFailForMediaServer(); + }else { + return zlmResult; + } } } @@ -511,7 +654,12 @@ public class ZLMRESTfulUtils { if (response == null) { return ZLMResult.getFailForMediaServer(); }else { - return JSON.parseObject(response, ZLMResult.class); + ZLMResult zlmResult = JSON.parseObject(response, ZLMResult.class); + if (zlmResult == null) { + return ZLMResult.getFailForMediaServer(); + }else { + return zlmResult; + } } } @@ -522,7 +670,12 @@ public class ZLMRESTfulUtils { if (response == null) { return ZLMResult.getFailForMediaServer(); }else { - return JSON.parseObject(response, ZLMResult.class); + ZLMResult zlmResult = JSON.parseObject(response, ZLMResult.class); + if (zlmResult == null) { + return ZLMResult.getFailForMediaServer(); + }else { + return zlmResult; + } } } @@ -535,7 +688,12 @@ public class ZLMRESTfulUtils { if (response == null) { return ZLMResult.getFailForMediaServer(); }else { - return JSON.parseObject(response, ZLMResult.class); + ZLMResult zlmResult = JSON.parseObject(response, ZLMResult.class); + if (zlmResult == null) { + return ZLMResult.getFailForMediaServer(); + }else { + return zlmResult; + } } } @@ -548,7 +706,12 @@ public class ZLMRESTfulUtils { if (response == null) { return ZLMResult.getFailForMediaServer(); }else { - return JSON.parseObject(response, ZLMResult.class); + ZLMResult zlmResult = JSON.parseObject(response, ZLMResult.class); + if (zlmResult == null) { + return ZLMResult.getFailForMediaServer(); + }else { + return zlmResult; + } } } @@ -563,7 +726,12 @@ public class ZLMRESTfulUtils { if (response == null) { return ZLMResult.getFailForMediaServer(); }else { - return JSON.parseObject(response, ZLMResult.class); + ZLMResult zlmResult = JSON.parseObject(response, ZLMResult.class); + if (zlmResult == null) { + return ZLMResult.getFailForMediaServer(); + }else { + return zlmResult; + } } } @@ -578,7 +746,12 @@ public class ZLMRESTfulUtils { if (response == null) { return ZLMResult.getFailForMediaServer(); }else { - return JSON.parseObject(response, ZLMResult.class); + ZLMResult zlmResult = JSON.parseObject(response, ZLMResult.class); + if (zlmResult == null) { + return ZLMResult.getFailForMediaServer(); + }else { + return zlmResult; + } } } @@ -593,7 +766,12 @@ public class ZLMRESTfulUtils { if (response == null) { return ZLMResult.getFailForMediaServer(); }else { - return JSON.parseObject(response, ZLMResult.class); + ZLMResult zlmResult = JSON.parseObject(response, ZLMResult.class); + if (zlmResult == null) { + return ZLMResult.getFailForMediaServer(); + }else { + return zlmResult; + } } } @@ -610,7 +788,12 @@ public class ZLMRESTfulUtils { if (response == null) { return ZLMResult.getFailForMediaServer(); }else { - return JSON.parseObject(response, ZLMResult.class); + ZLMResult zlmResult = JSON.parseObject(response, ZLMResult.class); + if (zlmResult == null) { + return ZLMResult.getFailForMediaServer(); + }else { + return zlmResult; + } } } } diff --git a/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMServerFactory.java b/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMServerFactory.java index b57563722..4349445d3 100755 --- a/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMServerFactory.java +++ b/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMServerFactory.java @@ -1,7 +1,6 @@ package com.genersoft.iot.vmp.media.zlm; import com.alibaba.fastjson2.JSONArray; -import com.alibaba.fastjson2.JSONObject; import com.genersoft.iot.vmp.common.CommonCallback; import com.genersoft.iot.vmp.gb28181.bean.SendRtpInfo; import com.genersoft.iot.vmp.media.bean.MediaServer; @@ -128,19 +127,14 @@ public class ZLMServerFactory { } Map param = new HashMap<>(); param.put("stream_id", streamId); - zlmresTfulUtils.closeRtpServer(serverItem, param, jsonObject -> { - if (jsonObject != null ) { - if (jsonObject.getInteger("code") == 0) { - if (callback != null) { - callback.run(jsonObject.getInteger("hit") == 1); - } - return; - }else { - log.error("关闭RTP Server 失败: " + jsonObject.getString("msg")); + zlmresTfulUtils.closeRtpServer(serverItem, param, zlmResult -> { + if (zlmResult.getCode() == 0) { + if (callback != null) { + callback.run(zlmResult.getHit() >= 1); } + return; }else { - // 检查ZLM状态 - log.error("关闭RTP Server 失败: 请检查ZLM服务"); + log.error("关闭RTP Server 失败: " + zlmResult.getMsg()); } if (callback != null) { callback.run(false); @@ -152,22 +146,22 @@ public class ZLMServerFactory { /** * 调用zlm RESTFUL API —— startSendRtp */ - public JSONObject startSendRtpStream(MediaServer mediaServerItem, Mapparam) { + public ZLMResult startSendRtpStream(MediaServer mediaServerItem, Mapparam) { return zlmresTfulUtils.startSendRtp(mediaServerItem, param); } /** * 调用zlm RESTFUL API —— startSendRtpPassive */ - public JSONObject startSendRtpPassive(MediaServer mediaServerItem, Mapparam) { + public ZLMResult startSendRtpPassive(MediaServer mediaServerItem, Mapparam) { return zlmresTfulUtils.startSendRtpPassive(mediaServerItem, param); } - public JSONObject startSendRtpPassive(MediaServer mediaServerItem, Map param, ZLMRESTfulUtils.RequestCallback callback) { + public ZLMResult startSendRtpPassive(MediaServer mediaServerItem, Map param, ZLMRESTfulUtils.ResultCallback callback) { return zlmresTfulUtils.startSendRtpPassive(mediaServerItem, param, callback); } - public JSONObject startSendRtpTalk(MediaServer mediaServer, Map param, ZLMRESTfulUtils.RequestCallback callback) { + public ZLMResult startSendRtpTalk(MediaServer mediaServer, Map param, ZLMRESTfulUtils.ResultCallback callback) { return zlmresTfulUtils.startSendRtpTalk(mediaServer, param, callback); } @@ -185,29 +179,7 @@ public class ZLMServerFactory { && !result.getData().isEmpty()); } - /** - * 查询转推的流是否有其它观看者 - * @param streamId - * @return - */ - public int totalReaderCount(MediaServer mediaServerItem, String app, String streamId) { - JSONObject mediaInfo = zlmresTfulUtils.getMediaInfo(mediaServerItem, app, "rtsp", streamId); - if (mediaInfo == null) { - return 0; - } - Integer code = mediaInfo.getInteger("code"); - if (code < 0) { - log.warn("查询流({}/{})是否有其它观看者时得到: {}", app, streamId, mediaInfo.getString("msg")); - return -1; - } - if ( code == 0 && mediaInfo.getBoolean("online") != null && ! mediaInfo.getBoolean("online")) { - log.warn("查询流({}/{})是否有其它观看者时得到: {}", app, streamId, mediaInfo.getString("msg")); - return -1; - } - return mediaInfo.getInteger("totalReaderCount"); - } - - public JSONObject startSendRtp(MediaServer mediaInfo, SendRtpInfo sendRtpItem) { + public ZLMResult startSendRtp(MediaServer mediaInfo, SendRtpInfo sendRtpItem) { String is_Udp = sendRtpItem.isTcp() ? "0" : "1"; log.info("rtp/{}开始推流, 目标={}:{},SSRC={}", sendRtpItem.getStream(), sendRtpItem.getIp(), sendRtpItem.getPort(), sendRtpItem.getSsrc()); Map param = new HashMap<>(12); @@ -228,45 +200,43 @@ public class ZLMServerFactory { return null; } // 如果是非严格模式,需要关闭端口占用 - JSONObject startSendRtpStreamResult = null; + ZLMResult zlmResult = null; if (sendRtpItem.getLocalPort() != 0) { if (sendRtpItem.isTcpActive()) { - startSendRtpStreamResult = startSendRtpPassive(mediaInfo, param); + zlmResult = startSendRtpPassive(mediaInfo, param); }else { param.put("is_udp", is_Udp); param.put("dst_url", sendRtpItem.getIp()); param.put("dst_port", sendRtpItem.getPort()); - startSendRtpStreamResult = startSendRtpStream(mediaInfo, param); + zlmResult = startSendRtpStream(mediaInfo, param); } }else { if (sendRtpItem.isTcpActive()) { - startSendRtpStreamResult = startSendRtpPassive(mediaInfo, param); + zlmResult = startSendRtpPassive(mediaInfo, param); }else { param.put("is_udp", is_Udp); param.put("dst_url", sendRtpItem.getIp()); param.put("dst_port", sendRtpItem.getPort()); - startSendRtpStreamResult = startSendRtpStream(mediaInfo, param); + zlmResult = startSendRtpStream(mediaInfo, param); } } - return startSendRtpStreamResult; + return zlmResult; } public Boolean updateRtpServerSSRC(MediaServer mediaServerItem, String streamId, String ssrc) { boolean result = false; - JSONObject jsonObject = zlmresTfulUtils.updateRtpServerSSRC(mediaServerItem, streamId, ssrc); - if (jsonObject == null) { - log.error("[更新RTPServer] 失败: 请检查ZLM服务"); - } else if (jsonObject.getInteger("code") == 0) { + ZLMResult zlmResult = zlmresTfulUtils.updateRtpServerSSRC(mediaServerItem, streamId, ssrc); + if (zlmResult.getCode() == 0) { result= true; log.info("[更新RTPServer] 成功"); } else { - log.error("[更新RTPServer] 失败: {}, streamId:{},ssrc:{}->\r\n{}",jsonObject.getString("msg"), - streamId, ssrc, jsonObject); + log.error("[更新RTPServer] 失败: {}, streamId:{},ssrc:{}", zlmResult.getMsg(), + streamId, ssrc); } return result; } - public JSONObject stopSendRtpStream(MediaServer mediaServerItem, SendRtpInfo sendRtpItem) { + public ZLMResult stopSendRtpStream(MediaServer mediaServerItem, SendRtpInfo sendRtpItem) { Map param = new HashMap<>(); param.put("vhost", "__defaultVhost__"); param.put("app", sendRtpItem.getApp()); diff --git a/src/main/java/com/genersoft/iot/vmp/media/zlm/dto/ZLMResult.java b/src/main/java/com/genersoft/iot/vmp/media/zlm/dto/ZLMResult.java index 03e8a8c34..5dd18386c 100644 --- a/src/main/java/com/genersoft/iot/vmp/media/zlm/dto/ZLMResult.java +++ b/src/main/java/com/genersoft/iot/vmp/media/zlm/dto/ZLMResult.java @@ -19,24 +19,40 @@ public class ZLMResult { private Integer port; private Integer hit; - public static ZLMResult getFailForMediaServer() { - ZLMResult zlmResult = new ZLMResult<>(); + public static ZLMResult getFailForMediaServer() { + ZLMResult zlmResult = new ZLMResult<>(); zlmResult.setCode(-2); zlmResult.setMsg("流媒体调用失败"); return zlmResult; } - public static ZLMResult getMediaServer(int code, String msg) { + public static ZLMResult getMediaServer(int code, String msg) { return getMediaServer(code, msg, null); } - public static ZLMResult getMediaServer(int code, String msg, Object data) { - ZLMResult zlmResult = new ZLMResult<>(); + public static ZLMResult getMediaServer(int code, String msg, T data) { + ZLMResult zlmResult = new ZLMResult<>(); zlmResult.setCode(code); zlmResult.setMsg(msg); zlmResult.setData(data); return zlmResult; } - + @Override + public String toString() { + return "ZLMResult{" + + "code=" + code + + ", msg='" + msg + '\'' + + ", data=" + data + + (online != null ? (", online=" + online) : "") + + (exist != null ? (", exist=" + exist) : "") + + (peer_ip != null ? (", peer_ip=" + peer_ip) : "") + + (peer_port != null ? (", peer_port=" + peer_port) : "") + + (local_ip != null ? (", local_ip=" + local_ip) : "") + + (local_port != null ? (", local_port=" + local_port) : "") + + (changed != null ? (", changed=" + changed) : "") + + (port != null ? (", port=" + port) : "") + + (hit != null ? (", hit=" + hit) : "") + + '}'; + } }