优化zlm结果封装完成

This commit is contained in:
lin 2025-09-16 10:52:58 +08:00
parent 576feec514
commit c1672728d3
7 changed files with 389 additions and 227 deletions

View File

@ -398,6 +398,8 @@ public class PlatformServiceImpl implements IPlatformService, CommandLineRunner
SipTransactionInfo transactionInfo = statusTaskRunner.getRegisterTransactionInfo(platformInDb.getServerGBId()); SipTransactionInfo transactionInfo = statusTaskRunner.getRegisterTransactionInfo(platformInDb.getServerGBId());
// 注销后出发平台离线 如果是启用的平台那么下次丢失检测会检测到并重新注册上线 // 注销后出发平台离线 如果是启用的平台那么下次丢失检测会检测到并重新注册上线
sendUnRegister(platformInDb, transactionInfo); sendUnRegister(platformInDb, transactionInfo);
}else if (platform.isEnable()) {
sendRegister(platform, null);
} }
return false; return false;

View File

@ -209,7 +209,7 @@ public class AssistRESTfulUtils {
if (response.isSuccessful()) { if (response.isSuccessful()) {
try { try {
String responseStr = Objects.requireNonNull(response.body()).string(); String responseStr = Objects.requireNonNull(response.body()).string();
callback.run(JSON.parseObject(responseStr)); callback.run(responseStr);
} catch (IOException e) { } catch (IOException e) {
log.error(String.format("[ %s ]请求失败: %s", url, e.getMessage())); log.error(String.format("[ %s ]请求失败: %s", url, e.getMessage()));
} }

View File

@ -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.MediaInfo;
import com.genersoft.iot.vmp.media.bean.MediaServer; import com.genersoft.iot.vmp.media.bean.MediaServer;
import com.genersoft.iot.vmp.media.service.IMediaNodeServerService; 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.*;
import com.genersoft.iot.vmp.media.zlm.dto.ZLMServerConfig;
import com.genersoft.iot.vmp.streamProxy.bean.StreamProxy; import com.genersoft.iot.vmp.streamProxy.bean.StreamProxy;
import com.genersoft.iot.vmp.vmanager.bean.ErrorCode; import com.genersoft.iot.vmp.vmanager.bean.ErrorCode;
import com.genersoft.iot.vmp.vmanager.bean.WVPResult; import com.genersoft.iot.vmp.vmanager.bean.WVPResult;
@ -72,10 +71,9 @@ public class ZLMMediaNodeServerService implements IMediaNodeServerService {
if (mediaServer == null) { if (mediaServer == null) {
return false; return false;
} }
ZLMResult zlmResult = zlmresTfulUtils.getMediaServerConfig(mediaServer); ZLMResult<List<JSONObject>> mediaServerConfig = zlmresTfulUtils.getMediaServerConfig(mediaServer);
if (zlmResult != null) { if (mediaServerConfig != null) {
zlmResult.getData() List<JSONObject> data = mediaServerConfig.getData();
JSONArray data = zlmResult.getJSONArray("data");
if (data != null && !data.isEmpty()) { if (data != null && !data.isEmpty()) {
ZLMServerConfig zlmServerConfig= JSON.parseObject(JSON.toJSONString(data.get(0)), ZLMServerConfig.class); ZLMServerConfig zlmServerConfig= JSON.parseObject(JSON.toJSONString(data.get(0)), ZLMServerConfig.class);
return zlmServerConfig.getGeneralMediaServerId().equals(mediaServer.getId()); return zlmServerConfig.getGeneralMediaServerId().equals(mediaServer.getId());
@ -102,15 +100,15 @@ public class ZLMMediaNodeServerService implements IMediaNodeServerService {
mediaServer.setFlvPort(port); mediaServer.setFlvPort(port);
mediaServer.setWsFlvPort(port); mediaServer.setWsFlvPort(port);
mediaServer.setSecret(secret); mediaServer.setSecret(secret);
JSONObject responseJSON = zlmresTfulUtils.getMediaServerConfig(mediaServer); ZLMResult<List<JSONObject>> mediaServerConfigResult = zlmresTfulUtils.getMediaServerConfig(mediaServer);
if (responseJSON == null) { if (mediaServerConfigResult == null) {
throw new ControllerException(ErrorCode.ERROR100.getCode(), "连接失败"); throw new ControllerException(ErrorCode.ERROR100.getCode(), "连接失败");
} }
JSONArray data = responseJSON.getJSONArray("data"); List<JSONObject> configList = mediaServerConfigResult.getData();
if (data == null) { if (configList == null) {
throw new ControllerException(ErrorCode.ERROR100.getCode(), "读取配置失败"); 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) { if (zlmServerConfig == null) {
throw new ControllerException(ErrorCode.ERROR100.getCode(), "读取配置失败"); throw new ControllerException(ErrorCode.ERROR100.getCode(), "读取配置失败");
} }
@ -140,12 +138,12 @@ public class ZLMMediaNodeServerService implements IMediaNodeServerService {
if (!ObjectUtils.isEmpty(ssrc)) { if (!ObjectUtils.isEmpty(ssrc)) {
param.put("ssrc", ssrc); param.put("ssrc", ssrc);
} }
JSONObject jsonObject = zlmresTfulUtils.stopSendRtp(mediaInfo, param); ZLMResult<?> zlmResult = zlmresTfulUtils.stopSendRtp(mediaInfo, param);
if (jsonObject.getInteger("code") != null && jsonObject.getInteger("code") == 0) { if (zlmResult.getCode() == 0) {
log.info("[停止发流] 成功: 参数:{}", JSON.toJSONString(param)); log.info("[停止发流] 成功: 参数:{}", JSON.toJSONString(param));
return true; return true;
}else { }else {
log.info("停止发流结果: {}, 参数:{}", jsonObject.getString("msg"), JSON.toJSONString(param)); log.info("停止发流结果: {}, 参数:{}", zlmResult.getMsg(), JSON.toJSONString(param));
return false; return false;
} }
} }
@ -159,9 +157,9 @@ public class ZLMMediaNodeServerService implements IMediaNodeServerService {
if (!ObjectUtils.isEmpty(ssrc)) { if (!ObjectUtils.isEmpty(ssrc)) {
param.put("ssrc", ssrc); param.put("ssrc", ssrc);
} }
JSONObject jsonObject = zlmresTfulUtils.stopSendRtp(mediaInfo, param); ZLMResult<?> zlmResult = zlmresTfulUtils.stopSendRtp(mediaInfo, param);
if (jsonObject == null || jsonObject.getInteger("code") != 0 ) { if (zlmResult.getCode() != 0 ) {
log.error("停止发流失败: {}, 参数:{}", jsonObject.getString("msg"), JSON.toJSONString(param)); log.error("停止发流失败: {}, 参数:{}", zlmResult.getMsg(), JSON.toJSONString(param));
return false; return false;
} }
return true; return true;
@ -170,12 +168,12 @@ public class ZLMMediaNodeServerService implements IMediaNodeServerService {
@Override @Override
public boolean deleteRecordDirectory(MediaServer mediaServer, String app, String stream, String date, String fileName) { public boolean deleteRecordDirectory(MediaServer mediaServer, String app, String stream, String date, String fileName) {
log.info("[zlm-deleteRecordDirectory] 删除磁盘文件, server: {} {}:{}->{}/{}", mediaServer.getId(), app, stream, date, 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); stream, date, fileName);
if (jsonObject.getInteger("code") == 0) { if (zlmResult.getCode() == 0) {
return true; return true;
}else { }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(), "删除磁盘文件失败"); throw new ControllerException(ErrorCode.ERROR100.getCode(), "删除磁盘文件失败");
} }
} }
@ -183,15 +181,14 @@ public class ZLMMediaNodeServerService implements IMediaNodeServerService {
@Override @Override
public List<StreamInfo> getMediaList(MediaServer mediaServer, String app, String stream, String callId) { public List<StreamInfo> getMediaList(MediaServer mediaServer, String app, String stream, String callId) {
List<StreamInfo> streamInfoList = new ArrayList<>(); List<StreamInfo> streamInfoList = new ArrayList<>();
ZLMResult<?> zlmResult = zlmresTfulUtils.getMediaList(mediaServer, app, stream); ZLMResult<JSONArray> zlmResult = zlmresTfulUtils.getMediaList(mediaServer, app, stream);
if (zlmResult != null) { if (zlmResult != null) {
if (zlmResult.getCode() == 0) { if (zlmResult.getCode() == 0) {
ZLMResult<JSONArray> result = (ZLMResult<JSONArray>)zlmResult; if (zlmResult.getData() == null) {
if (result.getData() == null) {
return streamInfoList; return streamInfoList;
} }
for (int i = 0; i < result.getData().size(); i++) { for (int i = 0; i < zlmResult.getData().size(); i++) {
JSONObject mediaJSON = result.getData().getJSONObject(0); JSONObject mediaJSON = zlmResult.getData().getJSONObject(0);
MediaInfo mediaInfo = MediaInfo.getInstance(mediaJSON, mediaServer, userSetting.getServerId()); MediaInfo mediaInfo = MediaInfo.getInstance(mediaJSON, mediaServer, userSetting.getServerId());
StreamInfo streamInfo = getStreamInfoByAppAndStream(mediaServer, mediaInfo.getApp(), mediaInfo.getStream(), mediaInfo, callId, true); StreamInfo streamInfo = getStreamInfoByAppAndStream(mediaServer, mediaInfo.getApp(), mediaInfo.getStream(), mediaInfo, callId, true);
if (streamInfo != null) { if (streamInfo != null) {
@ -251,9 +248,9 @@ public class ZLMMediaNodeServerService implements IMediaNodeServerService {
@Override @Override
public Boolean connectRtpServer(MediaServer mediaServer, String address, int port, String stream) { public Boolean connectRtpServer(MediaServer mediaServer, String address, int port, String stream) {
JSONObject jsonObject = zlmresTfulUtils.connectRtpServer(mediaServer, address, port, stream); ZLMResult<?> zlmResult = zlmresTfulUtils.connectRtpServer(mediaServer, address, port, stream);
log.info("[TCP主动连接对方] 结果: {}", jsonObject); log.info("[TCP主动连接对方] 结果: {}", zlmResult);
return jsonObject.getInteger("code") == 0; return zlmResult.getCode() == 0;
} }
@Override @Override
@ -269,34 +266,34 @@ public class ZLMMediaNodeServerService implements IMediaNodeServerService {
@Override @Override
public MediaInfo getMediaInfo(MediaServer mediaServer, String app, String stream) { public MediaInfo getMediaInfo(MediaServer mediaServer, String app, String stream) {
JSONObject jsonObject = zlmresTfulUtils.getMediaInfo(mediaServer, app, "rtsp", stream); ZLMResult<JSONObject> zlmResult = zlmresTfulUtils.getMediaInfo(mediaServer, app, "rtsp", stream);
if (jsonObject.getInteger("code") != 0) { if (zlmResult.getCode() != 0) {
return null; return null;
} }
return MediaInfo.getInstance(jsonObject, mediaServer, userSetting.getServerId()); return MediaInfo.getInstance(zlmResult.getData(), mediaServer, userSetting.getServerId());
} }
@Override @Override
public Boolean pauseRtpCheck(MediaServer mediaServer, String streamKey) { public Boolean pauseRtpCheck(MediaServer mediaServer, String streamKey) {
JSONObject jsonObject = zlmresTfulUtils.pauseRtpCheck(mediaServer, streamKey); ZLMResult<?> zlmResult = zlmresTfulUtils.pauseRtpCheck(mediaServer, streamKey);
return jsonObject.getInteger("code") == 0; return zlmResult.getCode() == 0;
} }
@Override @Override
public Boolean resumeRtpCheck(MediaServer mediaServer, String streamKey) { public Boolean resumeRtpCheck(MediaServer mediaServer, String streamKey) {
JSONObject jsonObject = zlmresTfulUtils.resumeRtpCheck(mediaServer, streamKey); ZLMResult<?> zlmResult = zlmresTfulUtils.resumeRtpCheck(mediaServer, streamKey);
return jsonObject.getInteger("code") == 0; return zlmResult.getCode() == 0;
} }
@Override @Override
public String getFfmpegCmd(MediaServer mediaServer, String cmdKey) { public String getFfmpegCmd(MediaServer mediaServer, String cmdKey) {
JSONObject jsonObject = zlmresTfulUtils.getMediaServerConfig(mediaServer); ZLMResult<List<JSONObject>> mediaServerConfigResult = zlmresTfulUtils.getMediaServerConfig(mediaServer);
if (jsonObject.getInteger("code") != 0) { if (mediaServerConfigResult == null || mediaServerConfigResult.getCode() != 0) {
log.warn("[getFfmpegCmd] 获取流媒体配置失败"); log.warn("[getFfmpegCmd] 获取流媒体配置失败");
throw new ControllerException(ErrorCode.ERROR100.getCode(), "获取流媒体配置失败"); throw new ControllerException(ErrorCode.ERROR100.getCode(), "获取流媒体配置失败");
} }
JSONArray dataArray = jsonObject.getJSONArray("data"); List<JSONObject> data = mediaServerConfigResult.getData();
JSONObject mediaServerConfig = dataArray.getJSONObject(0); JSONObject mediaServerConfig = data.get(0);
if (ObjectUtils.isEmpty(cmdKey)) { if (ObjectUtils.isEmpty(cmdKey)) {
cmdKey = "ffmpeg.cmd"; cmdKey = "ffmpeg.cmd";
} }
@ -306,42 +303,42 @@ public class ZLMMediaNodeServerService implements IMediaNodeServerService {
@Override @Override
public WVPResult<String> addStreamProxy(MediaServer mediaServer, String app, String stream, String url, public WVPResult<String> addStreamProxy(MediaServer mediaServer, String app, String stream, String url,
boolean enableAudio, boolean enableMp4, String rtpType, Integer timeout) { boolean enableAudio, boolean enableMp4, String rtpType, Integer timeout) {
JSONObject jsonObject = zlmresTfulUtils.addStreamProxy(mediaServer, app, stream, url, enableAudio, enableMp4, rtpType, timeout); ZLMResult<StreamProxyResult> zlmResult = zlmresTfulUtils.addStreamProxy(mediaServer, app, stream, url, enableAudio, enableMp4, rtpType, timeout);
if (jsonObject.getInteger("code") != 0) { if (zlmResult.getCode() != 0) {
return WVPResult.fail(ErrorCode.ERROR100.getCode(), "添加代理失败"); return WVPResult.fail(ErrorCode.ERROR100.getCode(), "添加代理失败");
}else { }else {
JSONObject data = jsonObject.getJSONObject("data"); StreamProxyResult data = zlmResult.getData();
if (data == null) { if (data == null) {
return WVPResult.fail(ErrorCode.ERROR100.getCode(), "代理结果异常 " + jsonObject); return WVPResult.fail(ErrorCode.ERROR100.getCode(), "代理结果异常");
}else { }else {
return WVPResult.success(data.getString("key")); return WVPResult.success(data.getKey());
} }
} }
} }
@Override @Override
public Boolean delFFmpegSource(MediaServer mediaServer, String streamKey) { public Boolean delFFmpegSource(MediaServer mediaServer, String streamKey) {
JSONObject jsonObject = zlmresTfulUtils.delFFmpegSource(mediaServer, streamKey); ZLMResult<FlagData> flagDataZLMResult = zlmresTfulUtils.delFFmpegSource(mediaServer, streamKey);
return jsonObject.getInteger("code") == 0; return flagDataZLMResult != null && flagDataZLMResult.getCode() == 0;
} }
@Override @Override
public Boolean delStreamProxy(MediaServer mediaServer, String streamKey) { public Boolean delStreamProxy(MediaServer mediaServer, String streamKey) {
JSONObject jsonObject = zlmresTfulUtils.delStreamProxy(mediaServer, streamKey); ZLMResult<FlagData> flagDataZLMResult = zlmresTfulUtils.delStreamProxy(mediaServer, streamKey);
return jsonObject.getInteger("code") == 0; return flagDataZLMResult != null && flagDataZLMResult.getCode() == 0;
} }
@Override @Override
public Map<String, String> getFFmpegCMDs(MediaServer mediaServer) { public Map<String, String> getFFmpegCMDs(MediaServer mediaServer) {
Map<String, String> result = new HashMap<>(); Map<String, String> result = new HashMap<>();
JSONObject mediaServerConfigResuly = zlmresTfulUtils.getMediaServerConfig(mediaServer); ZLMResult<List<JSONObject>> mediaServerConfigResult = zlmresTfulUtils.getMediaServerConfig(mediaServer);
if (mediaServerConfigResuly != null && mediaServerConfigResuly.getInteger("code") == 0 if (mediaServerConfigResult != null && mediaServerConfigResult.getCode() == 0
&& mediaServerConfigResuly.getJSONArray("data").size() > 0){ && !mediaServerConfigResult.getData().isEmpty()){
JSONObject mediaServerConfig = mediaServerConfigResuly.getJSONArray("data").getJSONObject(0); JSONObject jsonObject = mediaServerConfigResult.getData().get(0);
for (String key : mediaServerConfig.keySet()) { for (String key : jsonObject.keySet()) {
if (key.startsWith("ffmpeg.cmd")){ 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()); param.put("dst_port", sendRtpItem.getPort());
} }
JSONObject jsonObject = zlmServerFactory.startSendRtpPassive(mediaServer, param, null); ZLMResult<?> zlmResult = zlmServerFactory.startSendRtpPassive(mediaServer, param, null);
if (jsonObject == null || jsonObject.getInteger("code") != 0 ) { if (zlmResult.getCode() != 0 ) {
log.error("启动监听TCP被动推流失败: {}, 参数:{}", jsonObject.getString("msg"), JSON.toJSONString(param)); log.error("启动监听TCP被动推流失败: {}, 参数:{}", zlmResult.getMsg(), JSON.toJSONString(param));
throw new ControllerException(jsonObject.getInteger("code"), jsonObject.getString("msg")); throw new ControllerException(zlmResult.getCode(), zlmResult.getMsg());
} }
log.info("调用ZLM-TCP被动推流接口, 结果: {}", jsonObject); log.info("调用ZLM-TCP被动推流接口成功 本地端口: {}", zlmResult.getLocal_port());
log.info("启动监听TCP被动推流成功[ {}/{} ]{}->{}:{}, " , sendRtpItem.getApp(), sendRtpItem.getStream(), return zlmResult.getLocal_port();
jsonObject.getString("local_port"), param.get("dst_url"), param.get("dst_port"));
return jsonObject.getInteger("local_port");
} }
@Override @Override
@ -404,13 +399,13 @@ public class ZLMMediaNodeServerService implements IMediaNodeServerService {
} }
param.put("dst_url", sendRtpItem.getIp()); param.put("dst_url", sendRtpItem.getIp());
param.put("dst_port", sendRtpItem.getPort()); param.put("dst_port", sendRtpItem.getPort());
JSONObject jsonObject = zlmresTfulUtils.startSendRtp(mediaServer, param); ZLMResult<?> zlmResult = zlmresTfulUtils.startSendRtp(mediaServer, param);
if (jsonObject == null ) { if (zlmResult == null ) {
throw new ControllerException(ErrorCode.ERROR100.getCode(), "连接zlm失败"); throw new ControllerException(ErrorCode.ERROR100.getCode(), "连接zlm失败");
}else if (jsonObject.getInteger("code") != 0) { }else if (zlmResult.getCode() != 0) {
throw new ControllerException(jsonObject.getInteger("code"), jsonObject.getString("msg")); throw new ControllerException(zlmResult.getCode(), zlmResult.getMsg());
} }
log.info("[推流结果]{} ,参数: {}",jsonObject, JSONObject.toJSONString(param)); log.info("[推流结果]{} ,参数: {}", zlmResult, JSONObject.toJSONString(param));
} }
@Override @Override
@ -425,15 +420,13 @@ public class ZLMMediaNodeServerService implements IMediaNodeServerService {
param.put("only_audio", sendRtpItem.isOnlyAudio() ? "1" : "0"); param.put("only_audio", sendRtpItem.isOnlyAudio() ? "1" : "0");
param.put("recv_stream_id", sendRtpItem.getReceiveStream()); param.put("recv_stream_id", sendRtpItem.getReceiveStream());
param.put("enable_origin_recv_limit", "1"); param.put("enable_origin_recv_limit", "1");
JSONObject jsonObject = zlmServerFactory.startSendRtpTalk(mediaServer, param, null); ZLMResult<?> zlmResult = zlmServerFactory.startSendRtpTalk(mediaServer, param, null);
if (jsonObject == null || jsonObject.getInteger("code") != 0 ) { if (zlmResult.getCode() != 0 ) {
log.error("启动监听TCP被动推流失败: {}, 参数:{}", jsonObject.getString("msg"), JSON.toJSONString(param)); log.error("启动监听TCP被动推流失败: {}, 参数:{}", zlmResult.getMsg(), JSON.toJSONString(param));
throw new ControllerException(jsonObject.getInteger("code"), jsonObject.getString("msg")); throw new ControllerException(zlmResult.getCode(), zlmResult.getMsg());
} }
log.info("调用ZLM-TCP被动推流接口, 结果: {}", jsonObject); log.info("调用ZLM-TCP被动推流接口, 成功 本地端口: {}", zlmResult.getLocal_port());
log.info("启动监听TCP被动推流成功[ {}/{} ]{}->{}:{}, " , sendRtpItem.getApp(), sendRtpItem.getStream(), return zlmResult.getLocal_port();
jsonObject.getString("local_port"), param.get("dst_url"), param.get("dst_port"));
return jsonObject.getInteger("local_port");
} }
@Override @Override
@ -488,28 +481,26 @@ public class ZLMMediaNodeServerService implements IMediaNodeServerService {
closeStreams(mediaServer, streamProxy.getApp(), streamProxy.getStream()); closeStreams(mediaServer, streamProxy.getApp(), streamProxy.getStream());
} }
JSONObject jsonObject = null; ZLMResult<StreamProxyResult> zlmResult = null;
if ("ffmpeg".equalsIgnoreCase(streamProxy.getType())){ if ("ffmpeg".equalsIgnoreCase(streamProxy.getType())){
if (streamProxy.getTimeout() == 0) { if (streamProxy.getTimeout() == 0) {
streamProxy.setTimeout(15); 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.getTimeout(), streamProxy.isEnableAudio(), streamProxy.isEnableMp4(),
streamProxy.getFfmpegCmdKey()); streamProxy.getFfmpegCmdKey());
}else { }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()); streamProxy.isEnableAudio(), streamProxy.isEnableMp4(), streamProxy.getRtspType(), streamProxy.getTimeout());
} }
if (jsonObject == null) { if (zlmResult.getCode() != 0) {
throw new ControllerException(ErrorCode.ERROR100.getCode(), "请求失败"); throw new ControllerException(zlmResult.getCode(), zlmResult.getMsg());
}else if (jsonObject.getInteger("code") != 0) {
throw new ControllerException(jsonObject.getInteger("code"), jsonObject.getString("msg"));
}else { }else {
JSONObject data = jsonObject.getJSONObject("data"); StreamProxyResult data = zlmResult.getData();
if (data == null) { if (data == null) {
throw new ControllerException(jsonObject.getInteger("code"), "代理结果异常: " + jsonObject); throw new ControllerException(zlmResult.getCode(), "代理结果异常: " + zlmResult);
}else { }else {
return data.getString("key"); return data.getKey();
} }
} }
} }
@ -535,62 +526,61 @@ public class ZLMMediaNodeServerService implements IMediaNodeServerService {
@Override @Override
public void stopProxy(MediaServer mediaServer, String streamKey, String type) { public void stopProxy(MediaServer mediaServer, String streamKey, String type) {
JSONObject jsonObject = zlmresTfulUtils.delStreamProxy(mediaServer, streamKey); ZLMResult<FlagData> zlmResult = zlmresTfulUtils.delStreamProxy(mediaServer, streamKey);
if (jsonObject == null) { if (zlmResult == null) {
throw new ControllerException(ErrorCode.ERROR100.getCode(), "请求失败"); throw new ControllerException(ErrorCode.ERROR100.getCode(), "请求失败");
}else if (jsonObject.getInteger("code") != 0) { }else if (zlmResult.getCode() != 0) {
throw new ControllerException(jsonObject.getInteger("code"), jsonObject.getString("msg")); throw new ControllerException(zlmResult.getCode(), zlmResult.getMsg());
} }
} }
@Override @Override
public List<String> listRtpServer(MediaServer mediaServer) { public List<String> listRtpServer(MediaServer mediaServer) {
JSONObject jsonObject = zlmresTfulUtils.listRtpServer(mediaServer); ZLMResult<List<RtpServerResult>> zlmResult = zlmresTfulUtils.listRtpServer(mediaServer);
List<String> result = new ArrayList<>(); List<String> result = new ArrayList<>();
if (jsonObject == null || jsonObject.getInteger("code") != 0) { if (zlmResult.getCode() != 0) {
return result; return result;
} }
JSONArray data = jsonObject.getJSONArray("data"); List<RtpServerResult> data = zlmResult.getData();
if (data == null || data.isEmpty()) { if (data == null || data.isEmpty()) {
return result; return result;
} }
for (int i = 0; i < data.size(); i++) { for (RtpServerResult datum : data) {
JSONObject dataJSONObject = data.getJSONObject(i); result.add(datum.getStream_id());
result.add(dataJSONObject.getString("stream_id"));
} }
return result; return result;
} }
@Override @Override
public void loadMP4File(MediaServer mediaServer, String app, String stream, String datePath) { public void loadMP4File(MediaServer mediaServer, String app, String stream, String datePath) {
JSONObject jsonObject = zlmresTfulUtils.loadMP4File(mediaServer, app, stream, datePath); ZLMResult<?> zlmResult = zlmresTfulUtils.loadMP4File(mediaServer, app, stream, datePath);
if (jsonObject == null) { if (zlmResult == null) {
throw new ControllerException(ErrorCode.ERROR100.getCode(), "请求失败"); throw new ControllerException(ErrorCode.ERROR100.getCode(), "请求失败");
} }
if (jsonObject.getInteger("code") != 0) { if (zlmResult.getCode() != 0) {
throw new ControllerException(jsonObject.getInteger("code"), jsonObject.getString("msg")); throw new ControllerException(zlmResult.getCode(), zlmResult.getMsg());
} }
} }
@Override @Override
public void seekRecordStamp(MediaServer mediaServer, String app, String stream, Double stamp, String schema) { public void seekRecordStamp(MediaServer mediaServer, String app, String stream, Double stamp, String schema) {
JSONObject jsonObject = zlmresTfulUtils.seekRecordStamp(mediaServer, app, stream, stamp, schema); ZLMResult<?> zlmResult = zlmresTfulUtils.seekRecordStamp(mediaServer, app, stream, stamp, schema);
if (jsonObject == null) { if (zlmResult == null) {
throw new ControllerException(ErrorCode.ERROR100.getCode(), "请求失败"); throw new ControllerException(ErrorCode.ERROR100.getCode(), "请求失败");
} }
if (jsonObject.getInteger("code") != 0) { if (zlmResult.getCode() != 0) {
throw new ControllerException(jsonObject.getInteger("code"), jsonObject.getString("msg")); throw new ControllerException(zlmResult.getCode(), zlmResult.getMsg());
} }
} }
@Override @Override
public void setRecordSpeed(MediaServer mediaServer, String app, String stream, Integer speed, String schema) { public void setRecordSpeed(MediaServer mediaServer, String app, String stream, Integer speed, String schema) {
JSONObject jsonObject = zlmresTfulUtils.setRecordSpeed(mediaServer, app, stream, speed, schema); ZLMResult<?> zlmResult = zlmresTfulUtils.setRecordSpeed(mediaServer, app, stream, speed, schema);
if (jsonObject == null) { if (zlmResult == null) {
throw new ControllerException(ErrorCode.ERROR100.getCode(), "请求失败"); throw new ControllerException(ErrorCode.ERROR100.getCode(), "请求失败");
} }
if (jsonObject.getInteger("code") != 0) { if (zlmResult.getCode() != 0) {
throw new ControllerException(jsonObject.getInteger("code"), jsonObject.getString("msg")); throw new ControllerException(zlmResult.getCode(), zlmResult.getMsg());
} }
} }
} }

View File

@ -1,7 +1,6 @@
package com.genersoft.iot.vmp.media.zlm; package com.genersoft.iot.vmp.media.zlm;
import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject; import com.alibaba.fastjson2.JSONObject;
import com.genersoft.iot.vmp.conf.DynamicTask; import com.genersoft.iot.vmp.conf.DynamicTask;
import com.genersoft.iot.vmp.gb28181.event.EventPublisher; 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.MediaServerChangeEvent;
import com.genersoft.iot.vmp.media.event.mediaServer.MediaServerDeleteEvent; import com.genersoft.iot.vmp.media.event.mediaServer.MediaServerDeleteEvent;
import com.genersoft.iot.vmp.media.service.IMediaServerService; 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.dto.ZLMServerConfig;
import com.genersoft.iot.vmp.media.zlm.event.HookZlmServerKeepaliveEvent; import com.genersoft.iot.vmp.media.zlm.event.HookZlmServerKeepaliveEvent;
import com.genersoft.iot.vmp.media.zlm.event.HookZlmServerStartEvent; import com.genersoft.iot.vmp.media.zlm.event.HookZlmServerStartEvent;
@ -23,6 +23,7 @@ import org.springframework.util.ObjectUtils;
import java.io.File; import java.io.File;
import java.util.HashMap; import java.util.HashMap;
import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
@ -136,13 +137,13 @@ public class ZLMMediaServerStatusManager {
continue; continue;
} }
log.info("[ZLM-尝试连接] ID{}, 地址: {}:{}", mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort()); log.info("[ZLM-尝试连接] ID{}, 地址: {}:{}", mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort());
JSONObject responseJson = zlmresTfulUtils.getMediaServerConfig(mediaServerItem); ZLMResult<List<JSONObject>> mediaServerConfigResult = zlmresTfulUtils.getMediaServerConfig(mediaServerItem);
ZLMServerConfig zlmServerConfig = null; ZLMServerConfig zlmServerConfig = null;
if (responseJson == null) { if (mediaServerConfigResult == null) {
log.info("[ZLM-尝试连接]失败, ID{}, 地址: {}:{}", mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort()); log.info("[ZLM-尝试连接]失败, ID{}, 地址: {}:{}", mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort());
continue; continue;
} }
JSONArray data = responseJson.getJSONArray("data"); List<JSONObject> data = mediaServerConfigResult.getData();
if (data == null || data.isEmpty()) { if (data == null || data.isEmpty()) {
log.info("[ZLM-尝试连接]失败, ID{}, 地址: {}:{}", mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort()); log.info("[ZLM-尝试连接]失败, ID{}, 地址: {}:{}", mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort());
}else { }else {
@ -158,14 +159,14 @@ public class ZLMMediaServerStatusManager {
continue; continue;
} }
log.info("[ZLM-尝试连接] ID{}, 地址: {}:{}", mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort()); log.info("[ZLM-尝试连接] ID{}, 地址: {}:{}", mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort());
JSONObject responseJson = zlmresTfulUtils.getMediaServerConfig(mediaServerItem); ZLMResult<List<JSONObject>> mediaServerConfig = zlmresTfulUtils.getMediaServerConfig(mediaServerItem);
ZLMServerConfig zlmServerConfig = null; ZLMServerConfig zlmServerConfig = null;
if (responseJson == null) { if (mediaServerConfig == null) {
log.info("[ZLM-尝试连接]失败, ID{}, 地址: {}:{}", mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort()); log.info("[ZLM-尝试连接]失败, ID{}, 地址: {}:{}", mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort());
offlineZlmTimeMap.put(mediaServerItem.getId(), System.currentTimeMillis()); offlineZlmTimeMap.put(mediaServerItem.getId(), System.currentTimeMillis());
continue; continue;
} }
JSONArray data = responseJson.getJSONArray("data"); List<JSONObject> data = mediaServerConfig.getData();
if (data == null || data.isEmpty()) { if (data == null || data.isEmpty()) {
log.info("[ZLM-尝试连接]失败, ID{}, 地址: {}:{}", mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort()); log.info("[ZLM-尝试连接]失败, ID{}, 地址: {}:{}", mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort());
offlineZlmTimeMap.put(mediaServerItem.getId(), System.currentTimeMillis()); offlineZlmTimeMap.put(mediaServerItem.getId(), System.currentTimeMillis());
@ -190,8 +191,8 @@ public class ZLMMediaServerStatusManager {
eventPublisher.mediaServerOnlineEventPublish(mediaServerItem); eventPublisher.mediaServerOnlineEventPublish(mediaServerItem);
if(mediaServerItem.isAutoConfig()) { if(mediaServerItem.isAutoConfig()) {
if (config == null) { if (config == null) {
JSONObject responseJSON = zlmresTfulUtils.getMediaServerConfig(mediaServerItem); ZLMResult<List<JSONObject>> mediaServerConfig = zlmresTfulUtils.getMediaServerConfig(mediaServerItem);
JSONArray data = responseJSON.getJSONArray("data"); List<JSONObject> data = mediaServerConfig.getData();
if (data != null && !data.isEmpty()) { if (data != null && !data.isEmpty()) {
config = JSON.parseObject(JSON.toJSONString(data.get(0)), ZLMServerConfig.class); config = JSON.parseObject(JSON.toJSONString(data.get(0)), ZLMServerConfig.class);
} }
@ -298,9 +299,9 @@ public class ZLMMediaServerStatusManager {
param.put("record.appName", recordPathFile.getName()); 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) { if (restart) {
log.info("[媒体服务节点] 设置成功,开始重启以保证配置生效 {} -> {}:{}", log.info("[媒体服务节点] 设置成功,开始重启以保证配置生效 {} -> {}:{}",
mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort()); mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort());

View File

@ -2,8 +2,8 @@ package com.genersoft.iot.vmp.media.zlm;
import com.alibaba.fastjson2.JSON; import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray; import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.TypeReference; 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.bean.MediaServer;
import com.genersoft.iot.vmp.media.zlm.dto.*; import com.genersoft.iot.vmp.media.zlm.dto.*;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
@ -233,11 +233,16 @@ public class ZLMRESTfulUtils {
if (response == null) { if (response == null) {
return ZLMResult.getFailForMediaServer(); return ZLMResult.getFailForMediaServer();
}else { }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<JSONArray> getMediaList(MediaServer mediaServer, String app, String stream, String schema, ResultCallback callback){
Map<String, Object> param = new HashMap<>(); Map<String, Object> param = new HashMap<>();
if (app != null) { if (app != null) {
param.put("app",app); param.put("app",app);
@ -249,19 +254,39 @@ public class ZLMRESTfulUtils {
param.put("schema",schema); param.put("schema",schema);
} }
param.put("vhost","__defaultVhost__"); 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<JSONArray> zlmResult = JSON.parseObject(responseStr, new TypeReference<ZLMResult<JSONArray>>() {});
if (zlmResult == null) {
callback.run(ZLMResult.getFailForMediaServer());
}else {
callback.run(zlmResult);
}
}
}));
if (response == null) { if (response == null) {
return ZLMResult.getFailForMediaServer(); return ZLMResult.getFailForMediaServer();
}else { }else {
return JSON.parseObject(response, new TypeReference<ZLMResult<JSONArray>>() {}); ZLMResult<JSONArray> zlmResult = JSON.parseObject(response, new TypeReference<ZLMResult<JSONArray>>() {});
if (zlmResult == null) {
return ZLMResult.getFailForMediaServer();
}else {
return zlmResult;
}
} }
} }
public ZLMResult<?> getMediaList(MediaServer mediaServer, String app, String stream){ public ZLMResult<JSONArray> getMediaList(MediaServer mediaServer, String app, String stream){
return getMediaList(mediaServer, app, stream,null, null); return getMediaList(mediaServer, app, stream,null, null);
} }
public ZLMResult<?> getMediaInfo(MediaServer mediaServer, String app, String schema, String stream){ public ZLMResult<JSONObject> getMediaInfo(MediaServer mediaServer, String app, String schema, String stream){
Map<String, Object> param = new HashMap<>(); Map<String, Object> param = new HashMap<>();
param.put("app",app); param.put("app",app);
param.put("schema",schema); param.put("schema",schema);
@ -272,7 +297,15 @@ public class ZLMRESTfulUtils {
if (response == null) { if (response == null) {
return ZLMResult.getFailForMediaServer(); return ZLMResult.getFailForMediaServer();
}else { }else {
return JSON.parseObject(response, new TypeReference<ZLMResult<MediaInfo>>() {}); JSONObject jsonObject = JSON.parseObject(response);
if (jsonObject == null) {
return ZLMResult.getFailForMediaServer();
}else {
ZLMResult<JSONObject> zlmResult = new ZLMResult<>();
zlmResult.setCode(0);
zlmResult.setData(jsonObject);
return zlmResult;
}
} }
} }
@ -283,11 +316,16 @@ public class ZLMRESTfulUtils {
if (response == null) { if (response == null) {
return ZLMResult.getFailForMediaServer(); return ZLMResult.getFailForMediaServer();
}else { }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<StreamProxyResult> addFFmpegSource(MediaServer mediaServer, String src_url, String dst_url, Integer timeout_sec,
boolean enable_audio, boolean enable_mp4, String ffmpeg_cmd_key){ boolean enable_audio, boolean enable_mp4, String ffmpeg_cmd_key){
Map<String, Object> param = new HashMap<>(); Map<String, Object> param = new HashMap<>();
param.put("src_url", src_url); param.put("src_url", src_url);
@ -300,11 +338,16 @@ public class ZLMRESTfulUtils {
if (response == null) { if (response == null) {
return ZLMResult.getFailForMediaServer(); return ZLMResult.getFailForMediaServer();
}else { }else {
return JSON.parseObject(response, new TypeReference<ZLMResult<StreamProxyResult>>() {}); ZLMResult<StreamProxyResult> zlmResult = JSON.parseObject(response, new TypeReference<ZLMResult<StreamProxyResult>>() {});
if (zlmResult == null) {
return ZLMResult.getFailForMediaServer();
}else {
return zlmResult;
}
} }
} }
public ZLMResult<?> delFFmpegSource(MediaServer mediaServer, String key){ public ZLMResult<FlagData> delFFmpegSource(MediaServer mediaServer, String key){
Map<String, Object> param = new HashMap<>(); Map<String, Object> param = new HashMap<>();
param.put("key", key); param.put("key", key);
@ -312,28 +355,43 @@ public class ZLMRESTfulUtils {
if (response == null) { if (response == null) {
return ZLMResult.getFailForMediaServer(); return ZLMResult.getFailForMediaServer();
}else { }else {
return JSON.parseObject(response, new TypeReference<ZLMResult<FlagData>>() {}); ZLMResult<FlagData> zlmResult = JSON.parseObject(response, new TypeReference<ZLMResult<FlagData>>() {});
if (zlmResult == null) {
return ZLMResult.getFailForMediaServer();
}else {
return zlmResult;
}
} }
} }
public ZLMResult<?> delStreamProxy(MediaServer mediaServer, String key){ public ZLMResult<FlagData> delStreamProxy(MediaServer mediaServer, String key){
Map<String, Object> param = new HashMap<>(); Map<String, Object> param = new HashMap<>();
param.put("key", key); param.put("key", key);
String response = sendPost(mediaServer, "delStreamProxy",param, null); String response = sendPost(mediaServer, "delStreamProxy",param, null);
if (response == null) { if (response == null) {
return ZLMResult.getFailForMediaServer(); return ZLMResult.getFailForMediaServer();
}else { }else {
return JSON.parseObject(response, new TypeReference<ZLMResult<FlagData>>() {}); ZLMResult<FlagData> zlmResult = JSON.parseObject(response, new TypeReference<ZLMResult<FlagData>>() {});
if (zlmResult == null) {
return ZLMResult.getFailForMediaServer();
}else {
return zlmResult;
}
} }
} }
public ZLMResult<?> getMediaServerConfig(MediaServer mediaServer ){ public ZLMResult<List<JSONObject>> getMediaServerConfig(MediaServer mediaServer ){
String response = sendPost(mediaServer, "getServerConfig",null, null); String response = sendPost(mediaServer, "getServerConfig",null, null);
if (response == null) { if (response == null) {
return ZLMResult.getFailForMediaServer(); return ZLMResult.getFailForMediaServer();
}else { }else {
return JSON.parseObject(response, new TypeReference<ZLMResult<List<ZLMServerConfig>>>() {}); ZLMResult<List<JSONObject>> zlmResult = JSON.parseObject(response, new TypeReference<ZLMResult<List<JSONObject>>>() {});
if (zlmResult == null) {
return ZLMResult.getFailForMediaServer();
}else {
return zlmResult;
}
} }
} }
@ -342,7 +400,12 @@ public class ZLMRESTfulUtils {
if (response == null) { if (response == null) {
return ZLMResult.getFailForMediaServer(); return ZLMResult.getFailForMediaServer();
}else { }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) { if (response == null) {
return ZLMResult.getFailForMediaServer(); return ZLMResult.getFailForMediaServer();
}else { }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) { if (response == null) {
return ZLMResult.getFailForMediaServer(); return ZLMResult.getFailForMediaServer();
}else { }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<String, Object> param, ResultCallback callback) { public void closeRtpServer(MediaServer mediaServer, Map<String, Object> param, ResultCallback callback) {
sendPost(mediaServer, "closeRtpServer",param, (response -> { sendPost(mediaServer, "closeRtpServer",param, (response -> {
if (callback == null) {
return;
}
if (response == null) { if (response == null) {
callback.run(ZLMResult.getFailForMediaServer()); callback.run(ZLMResult.getFailForMediaServer());
}else { }else {
@ -375,12 +451,17 @@ public class ZLMRESTfulUtils {
} }
public ZLMResult<?> listRtpServer(MediaServer mediaServer) { public ZLMResult<List<RtpServerResult>> listRtpServer(MediaServer mediaServer) {
String response = sendPost(mediaServer, "listRtpServer",null, null); String response = sendPost(mediaServer, "listRtpServer",null, null);
if (response == null) { if (response == null) {
return ZLMResult.getFailForMediaServer(); return ZLMResult.getFailForMediaServer();
}else { }else {
return JSON.parseObject(response, new TypeReference<ZLMResult<List<RtpServerResult>>>() {}); ZLMResult<List<RtpServerResult>> zlmResult = JSON.parseObject(response, new TypeReference<ZLMResult<List<RtpServerResult>>>() {});
if (zlmResult == null) {
return ZLMResult.getFailForMediaServer();
}else {
return zlmResult;
}
} }
} }
@ -389,7 +470,12 @@ public class ZLMRESTfulUtils {
if (response == null) { if (response == null) {
return ZLMResult.getFailForMediaServer(); return ZLMResult.getFailForMediaServer();
}else { }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) { if (response == null) {
return ZLMResult.getFailForMediaServer(); return ZLMResult.getFailForMediaServer();
}else { }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<String, Object> param, ResultCallback callback) { public ZLMResult<?> startSendRtpPassive(MediaServer mediaServer, Map<String, Object> param, ResultCallback callback) {
String response = sendPost(mediaServer, "startSendRtpPassive",param, (responseStr -> { 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<String, Object> param, ResultCallback callback) {
String response = sendPost(mediaServer, "startSendRtpTalk",param, (responseStr -> {
if (callback == null) {
return;
}
if (responseStr == null) { if (responseStr == null) {
callback.run(ZLMResult.getFailForMediaServer()); callback.run(ZLMResult.getFailForMediaServer());
}else { }else {
@ -413,16 +535,12 @@ public class ZLMRESTfulUtils {
if (response == null) { if (response == null) {
return ZLMResult.getFailForMediaServer(); return ZLMResult.getFailForMediaServer();
}else { }else {
return JSON.parseObject(response, ZLMResult.class); ZLMResult<?> zlmResult = JSON.parseObject(response, ZLMResult.class);
} if (zlmResult == null) {
} return ZLMResult.getFailForMediaServer();
}else {
public ZLMResult<?> startSendRtpTalk(MediaServer mediaServer, Map<String, Object> param, RequestCallback callback) { return zlmResult;
String response = sendPost(mediaServer, "startSendRtpTalk",param, null); }
if (response == null) {
return ZLMResult.getFailForMediaServer();
}else {
return JSON.parseObject(response, ZLMResult.class);
} }
} }
@ -431,7 +549,12 @@ public class ZLMRESTfulUtils {
if (response == null) { if (response == null) {
return ZLMResult.getFailForMediaServer(); return ZLMResult.getFailForMediaServer();
}else { }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) { if (response == null) {
return ZLMResult.getFailForMediaServer(); return ZLMResult.getFailForMediaServer();
}else { }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<StreamProxyResult> addStreamProxy(MediaServer mediaServer, String app, String stream, String url, boolean enable_audio, boolean enable_mp4, String rtp_type, Integer timeOut) {
Map<String, Object> param = new HashMap<>(); Map<String, Object> param = new HashMap<>();
param.put("vhost", "__defaultVhost__"); param.put("vhost", "__defaultVhost__");
param.put("app", app); param.put("app", app);
@ -461,11 +589,16 @@ public class ZLMRESTfulUtils {
if (response == null) { if (response == null) {
return ZLMResult.getFailForMediaServer(); return ZLMResult.getFailForMediaServer();
}else { }else {
return JSON.parseObject(response, new TypeReference<ZLMResult<StreamProxyResult>>() {}); ZLMResult<StreamProxyResult> zlmResult = JSON.parseObject(response, new TypeReference<ZLMResult<StreamProxyResult>>() {});
if (zlmResult == null) {
return ZLMResult.getFailForMediaServer();
}else {
return zlmResult;
}
} }
} }
public ZLMResult<?> closeStreams(MediaServer mediaServer, String app, String stream) { public ZLMResult<FlagData> closeStreams(MediaServer mediaServer, String app, String stream) {
Map<String, Object> param = new HashMap<>(); Map<String, Object> param = new HashMap<>();
param.put("vhost", "__defaultVhost__"); param.put("vhost", "__defaultVhost__");
param.put("app", app); param.put("app", app);
@ -476,16 +609,26 @@ public class ZLMRESTfulUtils {
if (response == null) { if (response == null) {
return ZLMResult.getFailForMediaServer(); return ZLMResult.getFailForMediaServer();
}else { }else {
return JSON.parseObject(response, new TypeReference<ZLMResult<FlagData>>() {}); ZLMResult<FlagData> zlmResult = JSON.parseObject(response, new TypeReference<ZLMResult<FlagData>>() {});
if (zlmResult == null) {
return ZLMResult.getFailForMediaServer();
}else {
return zlmResult;
}
} }
} }
public ZLMResult<?> getAllSession(MediaServer mediaServer) { public ZLMResult<List<SessionData>> getAllSession(MediaServer mediaServer) {
String response = sendPost(mediaServer, "getAllSession",null, null); String response = sendPost(mediaServer, "getAllSession",null, null);
if (response == null) { if (response == null) {
return ZLMResult.getFailForMediaServer(); return ZLMResult.getFailForMediaServer();
}else { }else {
return JSON.parseObject(response, new TypeReference<ZLMResult<SessionData>>() {}); ZLMResult<List<SessionData>> zlmResult = JSON.parseObject(response, new TypeReference<ZLMResult<List<SessionData>>>() {});
if (zlmResult == null) {
return ZLMResult.getFailForMediaServer();
}else {
return zlmResult;
}
} }
} }
@ -511,7 +654,12 @@ public class ZLMRESTfulUtils {
if (response == null) { if (response == null) {
return ZLMResult.getFailForMediaServer(); return ZLMResult.getFailForMediaServer();
}else { }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) { if (response == null) {
return ZLMResult.getFailForMediaServer(); return ZLMResult.getFailForMediaServer();
}else { }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) { if (response == null) {
return ZLMResult.getFailForMediaServer(); return ZLMResult.getFailForMediaServer();
}else { }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) { if (response == null) {
return ZLMResult.getFailForMediaServer(); return ZLMResult.getFailForMediaServer();
}else { }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) { if (response == null) {
return ZLMResult.getFailForMediaServer(); return ZLMResult.getFailForMediaServer();
}else { }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) { if (response == null) {
return ZLMResult.getFailForMediaServer(); return ZLMResult.getFailForMediaServer();
}else { }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) { if (response == null) {
return ZLMResult.getFailForMediaServer(); return ZLMResult.getFailForMediaServer();
}else { }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) { if (response == null) {
return ZLMResult.getFailForMediaServer(); return ZLMResult.getFailForMediaServer();
}else { }else {
return JSON.parseObject(response, ZLMResult.class); ZLMResult<?> zlmResult = JSON.parseObject(response, ZLMResult.class);
if (zlmResult == null) {
return ZLMResult.getFailForMediaServer();
}else {
return zlmResult;
}
} }
} }
} }

View File

@ -1,7 +1,6 @@
package com.genersoft.iot.vmp.media.zlm; package com.genersoft.iot.vmp.media.zlm;
import com.alibaba.fastjson2.JSONArray; import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.genersoft.iot.vmp.common.CommonCallback; import com.genersoft.iot.vmp.common.CommonCallback;
import com.genersoft.iot.vmp.gb28181.bean.SendRtpInfo; import com.genersoft.iot.vmp.gb28181.bean.SendRtpInfo;
import com.genersoft.iot.vmp.media.bean.MediaServer; import com.genersoft.iot.vmp.media.bean.MediaServer;
@ -128,19 +127,14 @@ public class ZLMServerFactory {
} }
Map<String, Object> param = new HashMap<>(); Map<String, Object> param = new HashMap<>();
param.put("stream_id", streamId); param.put("stream_id", streamId);
zlmresTfulUtils.closeRtpServer(serverItem, param, jsonObject -> { zlmresTfulUtils.closeRtpServer(serverItem, param, zlmResult -> {
if (jsonObject != null ) { if (zlmResult.getCode() == 0) {
if (jsonObject.getInteger("code") == 0) { if (callback != null) {
if (callback != null) { callback.run(zlmResult.getHit() >= 1);
callback.run(jsonObject.getInteger("hit") == 1);
}
return;
}else {
log.error("关闭RTP Server 失败: " + jsonObject.getString("msg"));
} }
return;
}else { }else {
// 检查ZLM状态 log.error("关闭RTP Server 失败: " + zlmResult.getMsg());
log.error("关闭RTP Server 失败: 请检查ZLM服务");
} }
if (callback != null) { if (callback != null) {
callback.run(false); callback.run(false);
@ -152,22 +146,22 @@ public class ZLMServerFactory {
/** /**
* 调用zlm RESTFUL API startSendRtp * 调用zlm RESTFUL API startSendRtp
*/ */
public JSONObject startSendRtpStream(MediaServer mediaServerItem, Map<String, Object>param) { public ZLMResult<?> startSendRtpStream(MediaServer mediaServerItem, Map<String, Object>param) {
return zlmresTfulUtils.startSendRtp(mediaServerItem, param); return zlmresTfulUtils.startSendRtp(mediaServerItem, param);
} }
/** /**
* 调用zlm RESTFUL API startSendRtpPassive * 调用zlm RESTFUL API startSendRtpPassive
*/ */
public JSONObject startSendRtpPassive(MediaServer mediaServerItem, Map<String, Object>param) { public ZLMResult<?> startSendRtpPassive(MediaServer mediaServerItem, Map<String, Object>param) {
return zlmresTfulUtils.startSendRtpPassive(mediaServerItem, param); return zlmresTfulUtils.startSendRtpPassive(mediaServerItem, param);
} }
public JSONObject startSendRtpPassive(MediaServer mediaServerItem, Map<String, Object> param, ZLMRESTfulUtils.RequestCallback callback) { public ZLMResult<?> startSendRtpPassive(MediaServer mediaServerItem, Map<String, Object> param, ZLMRESTfulUtils.ResultCallback callback) {
return zlmresTfulUtils.startSendRtpPassive(mediaServerItem, param, callback); return zlmresTfulUtils.startSendRtpPassive(mediaServerItem, param, callback);
} }
public JSONObject startSendRtpTalk(MediaServer mediaServer, Map<String, Object> param, ZLMRESTfulUtils.RequestCallback callback) { public ZLMResult<?> startSendRtpTalk(MediaServer mediaServer, Map<String, Object> param, ZLMRESTfulUtils.ResultCallback callback) {
return zlmresTfulUtils.startSendRtpTalk(mediaServer, param, callback); return zlmresTfulUtils.startSendRtpTalk(mediaServer, param, callback);
} }
@ -185,29 +179,7 @@ public class ZLMServerFactory {
&& !result.getData().isEmpty()); && !result.getData().isEmpty());
} }
/** public ZLMResult<?> startSendRtp(MediaServer mediaInfo, SendRtpInfo sendRtpItem) {
* 查询转推的流是否有其它观看者
* @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) {
String is_Udp = sendRtpItem.isTcp() ? "0" : "1"; String is_Udp = sendRtpItem.isTcp() ? "0" : "1";
log.info("rtp/{}开始推流, 目标={}:{}SSRC={}", sendRtpItem.getStream(), sendRtpItem.getIp(), sendRtpItem.getPort(), sendRtpItem.getSsrc()); log.info("rtp/{}开始推流, 目标={}:{}SSRC={}", sendRtpItem.getStream(), sendRtpItem.getIp(), sendRtpItem.getPort(), sendRtpItem.getSsrc());
Map<String, Object> param = new HashMap<>(12); Map<String, Object> param = new HashMap<>(12);
@ -228,45 +200,43 @@ public class ZLMServerFactory {
return null; return null;
} }
// 如果是非严格模式需要关闭端口占用 // 如果是非严格模式需要关闭端口占用
JSONObject startSendRtpStreamResult = null; ZLMResult<?> zlmResult = null;
if (sendRtpItem.getLocalPort() != 0) { if (sendRtpItem.getLocalPort() != 0) {
if (sendRtpItem.isTcpActive()) { if (sendRtpItem.isTcpActive()) {
startSendRtpStreamResult = startSendRtpPassive(mediaInfo, param); zlmResult = startSendRtpPassive(mediaInfo, param);
}else { }else {
param.put("is_udp", is_Udp); param.put("is_udp", is_Udp);
param.put("dst_url", sendRtpItem.getIp()); param.put("dst_url", sendRtpItem.getIp());
param.put("dst_port", sendRtpItem.getPort()); param.put("dst_port", sendRtpItem.getPort());
startSendRtpStreamResult = startSendRtpStream(mediaInfo, param); zlmResult = startSendRtpStream(mediaInfo, param);
} }
}else { }else {
if (sendRtpItem.isTcpActive()) { if (sendRtpItem.isTcpActive()) {
startSendRtpStreamResult = startSendRtpPassive(mediaInfo, param); zlmResult = startSendRtpPassive(mediaInfo, param);
}else { }else {
param.put("is_udp", is_Udp); param.put("is_udp", is_Udp);
param.put("dst_url", sendRtpItem.getIp()); param.put("dst_url", sendRtpItem.getIp());
param.put("dst_port", sendRtpItem.getPort()); 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) { public Boolean updateRtpServerSSRC(MediaServer mediaServerItem, String streamId, String ssrc) {
boolean result = false; boolean result = false;
JSONObject jsonObject = zlmresTfulUtils.updateRtpServerSSRC(mediaServerItem, streamId, ssrc); ZLMResult<?> zlmResult = zlmresTfulUtils.updateRtpServerSSRC(mediaServerItem, streamId, ssrc);
if (jsonObject == null) { if (zlmResult.getCode() == 0) {
log.error("[更新RTPServer] 失败: 请检查ZLM服务");
} else if (jsonObject.getInteger("code") == 0) {
result= true; result= true;
log.info("[更新RTPServer] 成功"); log.info("[更新RTPServer] 成功");
} else { } else {
log.error("[更新RTPServer] 失败: {}, streamId{}ssrc{}->\r\n{}",jsonObject.getString("msg"), log.error("[更新RTPServer] 失败: {}, streamId{}ssrc{}", zlmResult.getMsg(),
streamId, ssrc, jsonObject); streamId, ssrc);
} }
return result; return result;
} }
public JSONObject stopSendRtpStream(MediaServer mediaServerItem, SendRtpInfo sendRtpItem) { public ZLMResult<?> stopSendRtpStream(MediaServer mediaServerItem, SendRtpInfo sendRtpItem) {
Map<String, Object> param = new HashMap<>(); Map<String, Object> param = new HashMap<>();
param.put("vhost", "__defaultVhost__"); param.put("vhost", "__defaultVhost__");
param.put("app", sendRtpItem.getApp()); param.put("app", sendRtpItem.getApp());

View File

@ -19,24 +19,40 @@ public class ZLMResult<T> {
private Integer port; private Integer port;
private Integer hit; private Integer hit;
public static ZLMResult<?> getFailForMediaServer() { public static <T> ZLMResult<T> getFailForMediaServer() {
ZLMResult<?> zlmResult = new ZLMResult<>(); ZLMResult<T> zlmResult = new ZLMResult<>();
zlmResult.setCode(-2); zlmResult.setCode(-2);
zlmResult.setMsg("流媒体调用失败"); zlmResult.setMsg("流媒体调用失败");
return zlmResult; return zlmResult;
} }
public static ZLMResult<?> getMediaServer(int code, String msg) { public static <T> ZLMResult<T> getMediaServer(int code, String msg) {
return getMediaServer(code, msg, null); return getMediaServer(code, msg, null);
} }
public static ZLMResult<?> getMediaServer(int code, String msg, Object data) { public static <T> ZLMResult<T> getMediaServer(int code, String msg, T data) {
ZLMResult<Object> zlmResult = new ZLMResult<>(); ZLMResult<T> zlmResult = new ZLMResult<>();
zlmResult.setCode(code); zlmResult.setCode(code);
zlmResult.setMsg(msg); zlmResult.setMsg(msg);
zlmResult.setData(data); zlmResult.setData(data);
return zlmResult; 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) : "") +
'}';
}
} }