Compare commits

..

1 Commits

Author SHA1 Message Date
阿斌
c39d33897d
Pre Merge pull request !41 from 阿斌/N/A 2025-09-12 02:35:52 +00:00
11 changed files with 289 additions and 698 deletions

View File

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

View File

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

View File

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

View File

@ -1,6 +1,7 @@
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;
@ -8,7 +9,6 @@ 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,7 +23,6 @@ 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;
@ -137,13 +136,13 @@ public class ZLMMediaServerStatusManager {
continue;
}
log.info("[ZLM-尝试连接] ID{}, 地址: {}:{}", mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort());
ZLMResult<List<JSONObject>> mediaServerConfigResult = zlmresTfulUtils.getMediaServerConfig(mediaServerItem);
JSONObject responseJson = zlmresTfulUtils.getMediaServerConfig(mediaServerItem);
ZLMServerConfig zlmServerConfig = null;
if (mediaServerConfigResult == null) {
if (responseJson == null) {
log.info("[ZLM-尝试连接]失败, ID{}, 地址: {}:{}", mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort());
continue;
}
List<JSONObject> data = mediaServerConfigResult.getData();
JSONArray data = responseJson.getJSONArray("data");
if (data == null || data.isEmpty()) {
log.info("[ZLM-尝试连接]失败, ID{}, 地址: {}:{}", mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort());
}else {
@ -159,14 +158,14 @@ public class ZLMMediaServerStatusManager {
continue;
}
log.info("[ZLM-尝试连接] ID{}, 地址: {}:{}", mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort());
ZLMResult<List<JSONObject>> mediaServerConfig = zlmresTfulUtils.getMediaServerConfig(mediaServerItem);
JSONObject responseJson = zlmresTfulUtils.getMediaServerConfig(mediaServerItem);
ZLMServerConfig zlmServerConfig = null;
if (mediaServerConfig == null) {
if (responseJson == null) {
log.info("[ZLM-尝试连接]失败, ID{}, 地址: {}:{}", mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort());
offlineZlmTimeMap.put(mediaServerItem.getId(), System.currentTimeMillis());
continue;
}
List<JSONObject> data = mediaServerConfig.getData();
JSONArray data = responseJson.getJSONArray("data");
if (data == null || data.isEmpty()) {
log.info("[ZLM-尝试连接]失败, ID{}, 地址: {}:{}", mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort());
offlineZlmTimeMap.put(mediaServerItem.getId(), System.currentTimeMillis());
@ -191,8 +190,8 @@ public class ZLMMediaServerStatusManager {
eventPublisher.mediaServerOnlineEventPublish(mediaServerItem);
if(mediaServerItem.isAutoConfig()) {
if (config == null) {
ZLMResult<List<JSONObject>> mediaServerConfig = zlmresTfulUtils.getMediaServerConfig(mediaServerItem);
List<JSONObject> data = mediaServerConfig.getData();
JSONObject responseJSON = zlmresTfulUtils.getMediaServerConfig(mediaServerItem);
JSONArray data = responseJSON.getJSONArray("data");
if (data != null && !data.isEmpty()) {
config = JSON.parseObject(JSON.toJSONString(data.get(0)), ZLMServerConfig.class);
}
@ -299,9 +298,9 @@ public class ZLMMediaServerStatusManager {
param.put("record.appName", recordPathFile.getName());
}
ZLMResult<?> zlmResult = zlmresTfulUtils.setServerConfig(mediaServerItem, param);
JSONObject responseJSON = zlmresTfulUtils.setServerConfig(mediaServerItem, param);
if (zlmResult != null && zlmResult.getCode() == 0) {
if (responseJSON != null && responseJSON.getInteger("code") == 0) {
if (restart) {
log.info("[媒体服务节点] 设置成功,开始重启以保证配置生效 {} -> {}:{}",
mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort());

View File

@ -1,11 +1,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.MediaServer;
import com.genersoft.iot.vmp.media.zlm.dto.*;
import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import okhttp3.logging.HttpLoggingInterceptor;
@ -19,7 +16,6 @@ import java.math.BigDecimal;
import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
@ -32,10 +28,7 @@ public class ZLMRESTfulUtils {
public interface RequestCallback{
void run(String response);
}
public interface ResultCallback{
void run(ZLMResult<?> response);
void run(JSONObject response);
}
private OkHttpClient getClient(){
@ -69,22 +62,26 @@ public class ZLMRESTfulUtils {
}
public String sendPost(MediaServer mediaServer, String api, Map<String, Object> param, RequestCallback callback) {
return sendPost(mediaServer, api, param, callback, null);
public JSONObject sendPost(MediaServer mediaServerItem, String api, Map<String, Object> param, RequestCallback callback) {
return sendPost(mediaServerItem, api, param, callback, null);
}
public String sendPost(MediaServer mediaServer, String api, Map<String, Object> param, RequestCallback callback, Integer readTimeOut) {
public JSONObject sendPost(MediaServer mediaServerItem, String api, Map<String, Object> param, RequestCallback callback, Integer readTimeOut) {
OkHttpClient client = getClient(readTimeOut);
if (mediaServer == null) {
if (mediaServerItem == null) {
return null;
}
String url = String.format("http://%s:%s/index/api/%s", mediaServer.getIp(), mediaServer.getHttpPort(), api);
String result = null;
String url = String.format("http://%s:%s/index/api/%s", mediaServerItem.getIp(), mediaServerItem.getHttpPort(), api);
JSONObject responseJSON = new JSONObject();
//-2自定义流媒体 调用错误码
responseJSON.put("code",-2);
responseJSON.put("msg","流媒体调用失败");
FormBody.Builder builder = new FormBody.Builder();
builder.add("secret",mediaServer.getSecret());
if (param != null && !param.isEmpty()) {
builder.add("secret",mediaServerItem.getSecret());
if (param != null && param.keySet().size() > 0) {
for (String key : param.keySet()){
if (param.get(key) != null) {
builder.add(key, param.get(key).toString());
@ -104,7 +101,8 @@ public class ZLMRESTfulUtils {
if (response.isSuccessful()) {
ResponseBody responseBody = response.body();
if (responseBody != null) {
result = responseBody.string();
String responseStr = responseBody.string();
responseJSON = JSON.parseObject(responseStr);
}
}else {
response.close();
@ -133,7 +131,7 @@ public class ZLMRESTfulUtils {
if (response.isSuccessful()) {
try {
String responseStr = Objects.requireNonNull(response.body()).string();
callback.run(responseStr);
callback.run(JSON.parseObject(responseStr));
} catch (IOException e) {
log.error(String.format("[ %s ]请求失败: %s", url, e.getMessage()));
}
@ -160,18 +158,20 @@ public class ZLMRESTfulUtils {
});
}
return result;
return responseJSON;
}
public void sendGetForImg(MediaServer mediaServer, String api, Map<String, Object> params, String targetPath, String fileName) {
String url = String.format("http://%s:%s/index/api/%s", mediaServer.getIp(), mediaServer.getHttpPort(), api);
public void sendGetForImg(MediaServer mediaServerItem, String api, Map<String, Object> params, String targetPath, String fileName) {
String url = String.format("http://%s:%s/index/api/%s", mediaServerItem.getIp(), mediaServerItem.getHttpPort(), api);
HttpUrl parseUrl = HttpUrl.parse(url);
if (parseUrl == null) {
return;
}
HttpUrl.Builder httpBuilder = parseUrl.newBuilder();
httpBuilder.addQueryParameter("secret", mediaServer.getSecret());
httpBuilder.addQueryParameter("secret", mediaServerItem.getSecret());
if (params != null) {
for (Map.Entry<String, Object> param : params.entrySet()) {
httpBuilder.addQueryParameter(param.getKey(), param.getValue().toString());
@ -217,7 +217,7 @@ public class ZLMRESTfulUtils {
}
}
public ZLMResult<?> isMediaOnline(MediaServer mediaServer, String app, String stream, String schema){
public JSONObject isMediaOnline(MediaServer mediaServerItem, String app, String stream, String schema){
Map<String, Object> param = new HashMap<>();
if (app != null) {
param.put("app",app);
@ -229,20 +229,10 @@ public class ZLMRESTfulUtils {
param.put("schema",schema);
}
param.put("vhost","__defaultVhost__");
String response = sendPost(mediaServer, "isMediaOnline", param, null);
if (response == null) {
return ZLMResult.getFailForMediaServer();
}else {
ZLMResult<?> zlmResult = JSON.parseObject(response, ZLMResult.class);
if (zlmResult == null) {
return ZLMResult.getFailForMediaServer();
}else {
return zlmResult;
}
}
return sendPost(mediaServerItem, "isMediaOnline", param, null);
}
public ZLMResult<JSONArray> getMediaList(MediaServer mediaServer, String app, String stream, String schema, ResultCallback callback){
public JSONObject getMediaList(MediaServer mediaServerItem, String app, String stream, String schema, RequestCallback callback){
Map<String, Object> param = new HashMap<>();
if (app != null) {
param.put("app",app);
@ -254,325 +244,106 @@ public class ZLMRESTfulUtils {
param.put("schema",schema);
}
param.put("vhost","__defaultVhost__");
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) {
return ZLMResult.getFailForMediaServer();
}else {
ZLMResult<JSONArray> zlmResult = JSON.parseObject(response, new TypeReference<ZLMResult<JSONArray>>() {});
if (zlmResult == null) {
return ZLMResult.getFailForMediaServer();
}else {
return zlmResult;
}
}
return sendPost(mediaServerItem, "getMediaList",param, callback);
}
public ZLMResult<JSONArray> getMediaList(MediaServer mediaServer, String app, String stream){
return getMediaList(mediaServer, app, stream,null, null);
public JSONObject getMediaList(MediaServer mediaServerItem, String app, String stream){
return getMediaList(mediaServerItem, app, stream,null, null);
}
public ZLMResult<JSONObject> getMediaInfo(MediaServer mediaServer, String app, String schema, String stream){
public JSONObject getMediaList(MediaServer mediaServerItem, RequestCallback callback){
return sendPost(mediaServerItem, "getMediaList",null, callback);
}
public JSONObject getMediaInfo(MediaServer mediaServerItem, String app, String schema, String stream){
Map<String, Object> param = new HashMap<>();
param.put("app",app);
param.put("schema",schema);
param.put("stream",stream);
param.put("vhost","__defaultVhost__");
String response = sendPost(mediaServer, "getMediaInfo",param, null);
if (response == null) {
return ZLMResult.getFailForMediaServer();
}else {
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;
}
}
return sendPost(mediaServerItem, "getMediaInfo",param, null);
}
public ZLMResult<?> getRtpInfo(MediaServer mediaServer, String stream_id){
public JSONObject getRtpInfo(MediaServer mediaServerItem, String stream_id){
Map<String, Object> param = new HashMap<>();
param.put("stream_id",stream_id);
String response = sendPost(mediaServer, "getRtpInfo",param, null);
if (response == null) {
return ZLMResult.getFailForMediaServer();
}else {
ZLMResult<?> zlmResult = JSON.parseObject(response, ZLMResult.class);
if (zlmResult == null) {
return ZLMResult.getFailForMediaServer();
}else {
return zlmResult;
}
}
return sendPost(mediaServerItem, "getRtpInfo",param, null);
}
public ZLMResult<StreamProxyResult> addFFmpegSource(MediaServer mediaServer, String src_url, String dst_url, Integer timeout_sec,
public JSONObject addFFmpegSource(MediaServer mediaServerItem, String src_url, String dst_url, Integer timeout_sec,
boolean enable_audio, boolean enable_mp4, String ffmpeg_cmd_key){
log.info(src_url);
log.info(dst_url);
Map<String, Object> param = new HashMap<>();
param.put("src_url", src_url);
param.put("dst_url", dst_url);
param.put("timeout_ms", timeout_sec*1000);
param.put("enable_mp4", enable_mp4);
param.put("ffmpeg_cmd_key", ffmpeg_cmd_key);
String response = sendPost(mediaServer, "addFFmpegSource",param, null);
if (response == null) {
return ZLMResult.getFailForMediaServer();
}else {
ZLMResult<StreamProxyResult> zlmResult = JSON.parseObject(response, new TypeReference<ZLMResult<StreamProxyResult>>() {});
if (zlmResult == null) {
return ZLMResult.getFailForMediaServer();
}else {
return zlmResult;
}
}
return sendPost(mediaServerItem, "addFFmpegSource",param, null);
}
public ZLMResult<FlagData> delFFmpegSource(MediaServer mediaServer, String key){
public JSONObject delFFmpegSource(MediaServer mediaServerItem, String key){
Map<String, Object> param = new HashMap<>();
param.put("key", key);
String response = sendPost(mediaServer, "delFFmpegSource",param, null);
if (response == null) {
return ZLMResult.getFailForMediaServer();
}else {
ZLMResult<FlagData> zlmResult = JSON.parseObject(response, new TypeReference<ZLMResult<FlagData>>() {});
if (zlmResult == null) {
return ZLMResult.getFailForMediaServer();
}else {
return zlmResult;
}
}
return sendPost(mediaServerItem, "delFFmpegSource",param, null);
}
public ZLMResult<FlagData> delStreamProxy(MediaServer mediaServer, String key){
public JSONObject delStreamProxy(MediaServer mediaServerItem, String key){
Map<String, Object> param = new HashMap<>();
param.put("key", key);
String response = sendPost(mediaServer, "delStreamProxy",param, null);
if (response == null) {
return ZLMResult.getFailForMediaServer();
}else {
ZLMResult<FlagData> zlmResult = JSON.parseObject(response, new TypeReference<ZLMResult<FlagData>>() {});
if (zlmResult == null) {
return ZLMResult.getFailForMediaServer();
}else {
return zlmResult;
}
}
return sendPost(mediaServerItem, "delStreamProxy",param, null);
}
public ZLMResult<List<JSONObject>> getMediaServerConfig(MediaServer mediaServer ){
String response = sendPost(mediaServer, "getServerConfig",null, null);
if (response == null) {
return ZLMResult.getFailForMediaServer();
}else {
ZLMResult<List<JSONObject>> zlmResult = JSON.parseObject(response, new TypeReference<ZLMResult<List<JSONObject>>>() {});
if (zlmResult == null) {
return ZLMResult.getFailForMediaServer();
}else {
return zlmResult;
}
}
public JSONObject getMediaServerConfig(MediaServer mediaServerItem){
return sendPost(mediaServerItem, "getServerConfig",null, null);
}
public ZLMResult<?> setServerConfig(MediaServer mediaServer, Map<String, Object> param){
String response = sendPost(mediaServer, "setServerConfig",param, null);
if (response == null) {
return ZLMResult.getFailForMediaServer();
}else {
ZLMResult<?> zlmResult = JSON.parseObject(response, ZLMResult.class);
if (zlmResult == null) {
return ZLMResult.getFailForMediaServer();
}else {
return zlmResult;
}
}
public JSONObject setServerConfig(MediaServer mediaServerItem, Map<String, Object> param){
return sendPost(mediaServerItem,"setServerConfig",param, null);
}
public ZLMResult<?> openRtpServer(MediaServer mediaServer, Map<String, Object> param){
String response = sendPost(mediaServer, "openRtpServer",param, null);
if (response == null) {
return ZLMResult.getFailForMediaServer();
}else {
ZLMResult<?> zlmResult = JSON.parseObject(response, ZLMResult.class);
if (zlmResult == null) {
return ZLMResult.getFailForMediaServer();
}else {
return zlmResult;
}
}
public JSONObject openRtpServer(MediaServer mediaServerItem, Map<String, Object> param){
return sendPost(mediaServerItem, "openRtpServer",param, null);
}
public ZLMResult<?> closeRtpServer(MediaServer mediaServer, Map<String, Object> param) {
String response = sendPost(mediaServer, "closeRtpServer",param, null);
if (response == null) {
return ZLMResult.getFailForMediaServer();
}else {
ZLMResult<?> zlmResult = JSON.parseObject(response, ZLMResult.class);
if (zlmResult == null) {
return ZLMResult.getFailForMediaServer();
}else {
return zlmResult;
}
}
public JSONObject closeRtpServer(MediaServer mediaServerItem, Map<String, Object> param) {
return sendPost(mediaServerItem, "closeRtpServer",param, null);
}
public void closeRtpServer(MediaServer mediaServer, Map<String, Object> param, ResultCallback callback) {
sendPost(mediaServer, "closeRtpServer",param, (response -> {
if (callback == null) {
return;
}
if (response == null) {
callback.run(ZLMResult.getFailForMediaServer());
}else {
callback.run(JSON.parseObject(response, ZLMResult.class));
}
}));
public void closeRtpServer(MediaServer mediaServerItem, Map<String, Object> param, RequestCallback callback) {
sendPost(mediaServerItem, "closeRtpServer",param, callback);
}
public ZLMResult<List<RtpServerResult>> listRtpServer(MediaServer mediaServer) {
String response = sendPost(mediaServer, "listRtpServer",null, null);
if (response == null) {
return ZLMResult.getFailForMediaServer();
}else {
ZLMResult<List<RtpServerResult>> zlmResult = JSON.parseObject(response, new TypeReference<ZLMResult<List<RtpServerResult>>>() {});
if (zlmResult == null) {
return ZLMResult.getFailForMediaServer();
}else {
return zlmResult;
}
}
public JSONObject listRtpServer(MediaServer mediaServerItem) {
return sendPost(mediaServerItem, "listRtpServer",null, null);
}
public ZLMResult<?> startSendRtp(MediaServer mediaServer, Map<String, Object> param) {
String response = sendPost(mediaServer, "startSendRtp",param, null);
if (response == null) {
return ZLMResult.getFailForMediaServer();
}else {
ZLMResult<?> zlmResult = JSON.parseObject(response, ZLMResult.class);
if (zlmResult == null) {
return ZLMResult.getFailForMediaServer();
}else {
return zlmResult;
}
}
public JSONObject startSendRtp(MediaServer mediaServerItem, Map<String, Object> param) {
return sendPost(mediaServerItem, "startSendRtp",param, null);
}
public ZLMResult<?> startSendRtpPassive(MediaServer mediaServer, Map<String, Object> param) {
String response = sendPost(mediaServer, "startSendRtpPassive",param, null);
if (response == null) {
return ZLMResult.getFailForMediaServer();
}else {
ZLMResult<?> zlmResult = JSON.parseObject(response, ZLMResult.class);
if (zlmResult == null) {
return ZLMResult.getFailForMediaServer();
}else {
return zlmResult;
}
}
public JSONObject startSendRtpPassive(MediaServer mediaServerItem, Map<String, Object> param) {
return sendPost(mediaServerItem, "startSendRtpPassive",param, null);
}
public ZLMResult<?> startSendRtpPassive(MediaServer mediaServer, Map<String, Object> 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 JSONObject startSendRtpPassive(MediaServer mediaServerItem, Map<String, Object> param, RequestCallback callback) {
return sendPost(mediaServerItem, "startSendRtpPassive",param, callback);
}
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) {
callback.run(ZLMResult.getFailForMediaServer());
}else {
callback.run(JSON.parseObject(responseStr, ZLMResult.class));
}
}));
if (response == null) {
return ZLMResult.getFailForMediaServer();
}else {
ZLMResult<?> zlmResult = JSON.parseObject(response, ZLMResult.class);
if (zlmResult == null) {
return ZLMResult.getFailForMediaServer();
}else {
return zlmResult;
}
}
public JSONObject startSendRtpTalk(MediaServer mediaServerItem, Map<String, Object> param, RequestCallback callback) {
return sendPost(mediaServerItem, "startSendRtpTalk",param, callback);
}
public ZLMResult<?> stopSendRtp(MediaServer mediaServer, Map<String, Object> param) {
String response = sendPost(mediaServer, "stopSendRtp",param, null);
if (response == null) {
return ZLMResult.getFailForMediaServer();
}else {
ZLMResult<?> zlmResult = JSON.parseObject(response, ZLMResult.class);
if (zlmResult == null) {
return ZLMResult.getFailForMediaServer();
}else {
return zlmResult;
}
}
public JSONObject stopSendRtp(MediaServer mediaServerItem, Map<String, Object> param) {
return sendPost(mediaServerItem, "stopSendRtp",param, null);
}
public ZLMResult<?> restartServer(MediaServer mediaServer) {
String response = sendPost(mediaServer, "restartServer",null, null);
if (response == null) {
return ZLMResult.getFailForMediaServer();
}else {
ZLMResult<?> zlmResult = JSON.parseObject(response, ZLMResult.class);
if (zlmResult == null) {
return ZLMResult.getFailForMediaServer();
}else {
return zlmResult;
}
}
public JSONObject restartServer(MediaServer mediaServerItem) {
return sendPost(mediaServerItem, "restartServer",null, null);
}
public ZLMResult<StreamProxyResult> addStreamProxy(MediaServer mediaServer, String app, String stream, String url, boolean enable_audio, boolean enable_mp4, String rtp_type, Integer timeOut) {
public JSONObject addStreamProxy(MediaServer mediaServerItem, String app, String stream, String url, boolean enable_audio, boolean enable_mp4, String rtp_type, Integer timeOut) {
Map<String, Object> param = new HashMap<>();
param.put("vhost", "__defaultVhost__");
param.put("app", app);
@ -584,198 +355,95 @@ public class ZLMRESTfulUtils {
param.put("timeout_sec", timeOut);
// 拉流重试次数,默认为3
param.put("retry_count", 3);
String response = sendPost(mediaServer, "addStreamProxy",param, null);
if (response == null) {
return ZLMResult.getFailForMediaServer();
}else {
ZLMResult<StreamProxyResult> zlmResult = JSON.parseObject(response, new TypeReference<ZLMResult<StreamProxyResult>>() {});
if (zlmResult == null) {
return ZLMResult.getFailForMediaServer();
}else {
return zlmResult;
}
}
return sendPost(mediaServerItem, "addStreamProxy",param, null, 20);
}
public ZLMResult<FlagData> closeStreams(MediaServer mediaServer, String app, String stream) {
public JSONObject closeStreams(MediaServer mediaServerItem, String app, String stream) {
Map<String, Object> param = new HashMap<>();
param.put("vhost", "__defaultVhost__");
param.put("app", app);
param.put("stream", stream);
param.put("force", 1);
String response = sendPost(mediaServer, "close_streams",param, null);
if (response == null) {
return ZLMResult.getFailForMediaServer();
}else {
ZLMResult<FlagData> zlmResult = JSON.parseObject(response, new TypeReference<ZLMResult<FlagData>>() {});
if (zlmResult == null) {
return ZLMResult.getFailForMediaServer();
}else {
return zlmResult;
}
}
return sendPost(mediaServerItem, "close_streams",param, null);
}
public ZLMResult<List<SessionData>> getAllSession(MediaServer mediaServer) {
String response = sendPost(mediaServer, "getAllSession",null, null);
if (response == null) {
return ZLMResult.getFailForMediaServer();
}else {
ZLMResult<List<SessionData>> zlmResult = JSON.parseObject(response, new TypeReference<ZLMResult<List<SessionData>>>() {});
if (zlmResult == null) {
return ZLMResult.getFailForMediaServer();
}else {
return zlmResult;
}
}
public JSONObject getAllSession(MediaServer mediaServerItem) {
return sendPost(mediaServerItem, "getAllSession",null, null);
}
public void kickSessions(MediaServer mediaServer, String localPortSStr) {
public void kickSessions(MediaServer mediaServerItem, String localPortSStr) {
Map<String, Object> param = new HashMap<>();
param.put("local_port", localPortSStr);
sendPost(mediaServer, "kick_sessions",param, null);
sendPost(mediaServerItem, "kick_sessions",param, null);
}
public void getSnap(MediaServer mediaServer, String streamUrl, int timeout_sec, int expire_sec, String targetPath, String fileName) {
public void getSnap(MediaServer mediaServerItem, String streamUrl, int timeout_sec, int expire_sec, String targetPath, String fileName) {
Map<String, Object> param = new HashMap<>(3);
param.put("url", streamUrl);
param.put("timeout_sec", timeout_sec);
param.put("expire_sec", expire_sec);
param.put("async", 1);
sendGetForImg(mediaServer, "getSnap", param, targetPath, fileName);
sendGetForImg(mediaServerItem, "getSnap", param, targetPath, fileName);
}
public ZLMResult<?> pauseRtpCheck(MediaServer mediaServer, String streamId) {
public JSONObject pauseRtpCheck(MediaServer mediaServerItem, String streamId) {
Map<String, Object> param = new HashMap<>(1);
param.put("stream_id", streamId);
String response = sendPost(mediaServer, "pauseRtpCheck", param, null);
if (response == null) {
return ZLMResult.getFailForMediaServer();
}else {
ZLMResult<?> zlmResult = JSON.parseObject(response, ZLMResult.class);
if (zlmResult == null) {
return ZLMResult.getFailForMediaServer();
}else {
return zlmResult;
}
}
return sendPost(mediaServerItem, "pauseRtpCheck",param, null);
}
public ZLMResult<?> resumeRtpCheck(MediaServer mediaServer, String streamId) {
public JSONObject resumeRtpCheck(MediaServer mediaServerItem, String streamId) {
Map<String, Object> param = new HashMap<>(1);
param.put("stream_id", streamId);
String response = sendPost(mediaServer, "resumeRtpCheck", param, null);
if (response == null) {
return ZLMResult.getFailForMediaServer();
}else {
ZLMResult<?> zlmResult = JSON.parseObject(response, ZLMResult.class);
if (zlmResult == null) {
return ZLMResult.getFailForMediaServer();
}else {
return zlmResult;
}
}
return sendPost(mediaServerItem, "resumeRtpCheck",param, null);
}
public ZLMResult<?> connectRtpServer(MediaServer mediaServer, String dst_url, int dst_port, String stream_id) {
public JSONObject connectRtpServer(MediaServer mediaServerItem, String dst_url, int dst_port, String stream_id) {
Map<String, Object> param = new HashMap<>(1);
param.put("dst_url", dst_url);
param.put("dst_port", dst_port);
param.put("stream_id", stream_id);
String response = sendPost(mediaServer, "connectRtpServer", param, null);
if (response == null) {
return ZLMResult.getFailForMediaServer();
}else {
ZLMResult<?> zlmResult = JSON.parseObject(response, ZLMResult.class);
if (zlmResult == null) {
return ZLMResult.getFailForMediaServer();
}else {
return zlmResult;
}
}
return sendPost(mediaServerItem, "connectRtpServer",param, null);
}
public ZLMResult<?> updateRtpServerSSRC(MediaServer mediaServer, String streamId, String ssrc) {
public JSONObject updateRtpServerSSRC(MediaServer mediaServerItem, String streamId, String ssrc) {
Map<String, Object> param = new HashMap<>(1);
param.put("ssrc", ssrc);
param.put("stream_id", streamId);
String response = sendPost(mediaServer, "updateRtpServerSSRC", param, null);
if (response == null) {
return ZLMResult.getFailForMediaServer();
}else {
ZLMResult<?> zlmResult = JSON.parseObject(response, ZLMResult.class);
if (zlmResult == null) {
return ZLMResult.getFailForMediaServer();
}else {
return zlmResult;
}
}
return sendPost(mediaServerItem, "updateRtpServerSSRC",param, null);
}
public ZLMResult<?> deleteRecordDirectory(MediaServer mediaServer, String app, String stream, String date, String fileName) {
public JSONObject deleteRecordDirectory(MediaServer mediaServerItem, String app, String stream, String date, String fileName) {
Map<String, Object> param = new HashMap<>(1);
param.put("vhost", "__defaultVhost__");
param.put("app", app);
param.put("stream", stream);
param.put("period", date);
param.put("name", fileName);
String response = sendPost(mediaServer, "deleteRecordDirectory", param, null);
if (response == null) {
return ZLMResult.getFailForMediaServer();
}else {
ZLMResult<?> zlmResult = JSON.parseObject(response, ZLMResult.class);
if (zlmResult == null) {
return ZLMResult.getFailForMediaServer();
}else {
return zlmResult;
}
}
return sendPost(mediaServerItem, "deleteRecordDirectory",param, null);
}
public ZLMResult<?> loadMP4File(MediaServer mediaServer, String app, String stream, String datePath) {
public JSONObject loadMP4File(MediaServer mediaServer, String app, String stream, String datePath) {
Map<String, Object> param = new HashMap<>(1);
param.put("vhost", "__defaultVhost__");
param.put("app", app);
param.put("stream", stream);
param.put("file_path", datePath);
param.put("file_repeat", "0");
String response = sendPost(mediaServer, "loadMP4File", param, null);
if (response == null) {
return ZLMResult.getFailForMediaServer();
}else {
ZLMResult<?> zlmResult = JSON.parseObject(response, ZLMResult.class);
if (zlmResult == null) {
return ZLMResult.getFailForMediaServer();
}else {
return zlmResult;
}
}
return sendPost(mediaServer, "loadMP4File",param, null);
}
public ZLMResult<?> setRecordSpeed(MediaServer mediaServer, String app, String stream, int speed, String schema) {
public JSONObject setRecordSpeed(MediaServer mediaServer, String app, String stream, int speed, String schema) {
Map<String, Object> param = new HashMap<>(1);
param.put("vhost", "__defaultVhost__");
param.put("app", app);
param.put("stream", stream);
param.put("speed", speed);
param.put("schema", schema);
String response = sendPost(mediaServer, "setRecordSpeed", param, null);
if (response == null) {
return ZLMResult.getFailForMediaServer();
}else {
ZLMResult<?> zlmResult = JSON.parseObject(response, ZLMResult.class);
if (zlmResult == null) {
return ZLMResult.getFailForMediaServer();
}else {
return zlmResult;
}
}
return sendPost(mediaServer, "setRecordSpeed",param, null);
}
public ZLMResult<?> seekRecordStamp(MediaServer mediaServer, String app, String stream, Double stamp, String schema) {
public JSONObject seekRecordStamp(MediaServer mediaServer, String app, String stream, Double stamp, String schema) {
Map<String, Object> param = new HashMap<>(1);
param.put("vhost", "__defaultVhost__");
param.put("app", app);
@ -783,17 +451,6 @@ public class ZLMRESTfulUtils {
BigDecimal bigDecimal = new BigDecimal(stamp);
param.put("stamp", bigDecimal);
param.put("schema", schema);
String response = sendPost(mediaServer, "seekRecordStamp", param, null);
if (response == null) {
return ZLMResult.getFailForMediaServer();
}else {
ZLMResult<?> zlmResult = JSON.parseObject(response, ZLMResult.class);
if (zlmResult == null) {
return ZLMResult.getFailForMediaServer();
}else {
return zlmResult;
}
}
return sendPost(mediaServer, "seekRecordStamp",param, null);
}
}

View File

@ -1,10 +1,9 @@
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;
import com.genersoft.iot.vmp.media.zlm.dto.ZLMResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@ -32,18 +31,18 @@ public class ZLMServerFactory {
public int createRTPServer(MediaServer mediaServerItem, String app, String streamId, long ssrc, Integer port, Boolean onlyAuto, Boolean disableAudio, Boolean reUsePort, Integer tcpMode) {
int result = -1;
// 查询此rtp server 是否已经存在
ZLMResult<?> rtpInfoResult = zlmresTfulUtils.getRtpInfo(mediaServerItem, streamId);
if(rtpInfoResult.getCode() == 0){
if (rtpInfoResult.getExist() != null && rtpInfoResult.getExist()) {
result = rtpInfoResult.getLocal_port();
JSONObject rtpInfo = zlmresTfulUtils.getRtpInfo(mediaServerItem, streamId);
if(rtpInfo.getInteger("code") == 0){
if (rtpInfo.getBoolean("exist")) {
result = rtpInfo.getInteger("local_port");
if (result == 0) {
// 此时说明rtpServer已经创建但是流还没有推上来
// 此时重新打开rtpServer
Map<String, Object> param = new HashMap<>();
param.put("stream_id", streamId);
ZLMResult<?> zlmResult = zlmresTfulUtils.closeRtpServer(mediaServerItem, param);
if (zlmResult != null ) {
if (zlmResult.getCode() == 0) {
JSONObject jsonObject = zlmresTfulUtils.closeRtpServer(mediaServerItem, param);
if (jsonObject != null ) {
if (jsonObject.getInteger("code") == 0) {
return createRTPServer(mediaServerItem, streamId, app, ssrc, port,onlyAuto, reUsePort,disableAudio, tcpMode);
}else {
log.warn("[开启rtpServer], 重启RtpServer错误");
@ -52,7 +51,7 @@ public class ZLMServerFactory {
}
return result;
}
}else if(rtpInfoResult.getCode() == -2){
}else if(rtpInfo.getInteger("code") == -2){
return result;
}
@ -84,12 +83,12 @@ public class ZLMServerFactory {
param.put("ssrc", ssrc);
}
ZLMResult<?> zlmResult = zlmresTfulUtils.openRtpServer(mediaServerItem, param);
if (zlmResult != null) {
if (zlmResult.getCode() == 0) {
result= zlmResult.getPort();
JSONObject openRtpServerResultJson = zlmresTfulUtils.openRtpServer(mediaServerItem, param);
if (openRtpServerResultJson != null) {
if (openRtpServerResultJson.getInteger("code") == 0) {
result= openRtpServerResultJson.getInteger("port");
}else {
log.error("创建RTP Server 失败 {}: ", zlmResult.getMsg());
log.error("创建RTP Server 失败 {}: ", openRtpServerResultJson.getString("msg"));
}
}else {
// 检查ZLM状态
@ -103,12 +102,13 @@ public class ZLMServerFactory {
if (serverItem !=null){
Map<String, Object> param = new HashMap<>();
param.put("stream_id", streamId);
ZLMResult<?> zlmResult = zlmresTfulUtils.closeRtpServer(serverItem, param);
if (zlmResult != null ) {
if (zlmResult.getCode() == 0) {
result = zlmResult.getHit() >= 1;
JSONObject jsonObject = zlmresTfulUtils.closeRtpServer(serverItem, param);
log.info("关闭RTP Server " + jsonObject);
if (jsonObject != null ) {
if (jsonObject.getInteger("code") == 0) {
result = jsonObject.getInteger("hit") >= 1;
}else {
log.error("关闭RTP Server 失败: " + zlmResult.getMsg());
log.error("关闭RTP Server 失败: " + jsonObject.getString("msg"));
}
}else {
// 检查ZLM状态
@ -127,14 +127,19 @@ public class ZLMServerFactory {
}
Map<String, Object> param = new HashMap<>();
param.put("stream_id", streamId);
zlmresTfulUtils.closeRtpServer(serverItem, param, zlmResult -> {
if (zlmResult.getCode() == 0) {
if (callback != null) {
callback.run(zlmResult.getHit() >= 1);
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"));
}
return;
}else {
log.error("关闭RTP Server 失败: " + zlmResult.getMsg());
// 检查ZLM状态
log.error("关闭RTP Server 失败: 请检查ZLM服务");
}
if (callback != null) {
callback.run(false);
@ -146,22 +151,22 @@ public class ZLMServerFactory {
/**
* 调用zlm RESTFUL API startSendRtp
*/
public ZLMResult<?> startSendRtpStream(MediaServer mediaServerItem, Map<String, Object>param) {
public JSONObject startSendRtpStream(MediaServer mediaServerItem, Map<String, Object>param) {
return zlmresTfulUtils.startSendRtp(mediaServerItem, param);
}
/**
* 调用zlm RESTFUL API startSendRtpPassive
*/
public ZLMResult<?> startSendRtpPassive(MediaServer mediaServerItem, Map<String, Object>param) {
public JSONObject startSendRtpPassive(MediaServer mediaServerItem, Map<String, Object>param) {
return zlmresTfulUtils.startSendRtpPassive(mediaServerItem, param);
}
public ZLMResult<?> startSendRtpPassive(MediaServer mediaServerItem, Map<String, Object> param, ZLMRESTfulUtils.ResultCallback callback) {
public JSONObject startSendRtpPassive(MediaServer mediaServerItem, Map<String, Object> param, ZLMRESTfulUtils.RequestCallback callback) {
return zlmresTfulUtils.startSendRtpPassive(mediaServerItem, param, callback);
}
public ZLMResult<?> startSendRtpTalk(MediaServer mediaServer, Map<String, Object> param, ZLMRESTfulUtils.ResultCallback callback) {
public JSONObject startSendRtpTalk(MediaServer mediaServer, Map<String, Object> param, ZLMRESTfulUtils.RequestCallback callback) {
return zlmresTfulUtils.startSendRtpTalk(mediaServer, param, callback);
}
@ -169,17 +174,38 @@ public class ZLMServerFactory {
* 查询待转推的流是否就绪
*/
public Boolean isStreamReady(MediaServer mediaServerItem, String app, String streamId) {
ZLMResult<?> zlmResult = zlmresTfulUtils.getMediaList(mediaServerItem, app, streamId);
if (zlmResult == null || zlmResult.getCode() == -2) {
JSONObject mediaInfo = zlmresTfulUtils.getMediaList(mediaServerItem, app, streamId);
if (mediaInfo == null || (mediaInfo.getInteger("code") == -2)) {
return null;
}
ZLMResult<JSONArray> result = (ZLMResult<JSONArray>) zlmResult;
return (result.getCode() == 0
&& result.getData() != null
&& !result.getData().isEmpty());
return (mediaInfo.getInteger("code") == 0
&& mediaInfo.getJSONArray("data") != null
&& mediaInfo.getJSONArray("data").size() > 0);
}
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";
log.info("rtp/{}开始推流, 目标={}:{}SSRC={}", sendRtpItem.getStream(), sendRtpItem.getIp(), sendRtpItem.getPort(), sendRtpItem.getSsrc());
Map<String, Object> param = new HashMap<>(12);
@ -200,43 +226,45 @@ public class ZLMServerFactory {
return null;
}
// 如果是非严格模式需要关闭端口占用
ZLMResult<?> zlmResult = null;
JSONObject startSendRtpStreamResult = null;
if (sendRtpItem.getLocalPort() != 0) {
if (sendRtpItem.isTcpActive()) {
zlmResult = startSendRtpPassive(mediaInfo, param);
startSendRtpStreamResult = startSendRtpPassive(mediaInfo, param);
}else {
param.put("is_udp", is_Udp);
param.put("dst_url", sendRtpItem.getIp());
param.put("dst_port", sendRtpItem.getPort());
zlmResult = startSendRtpStream(mediaInfo, param);
startSendRtpStreamResult = startSendRtpStream(mediaInfo, param);
}
}else {
if (sendRtpItem.isTcpActive()) {
zlmResult = startSendRtpPassive(mediaInfo, param);
startSendRtpStreamResult = startSendRtpPassive(mediaInfo, param);
}else {
param.put("is_udp", is_Udp);
param.put("dst_url", sendRtpItem.getIp());
param.put("dst_port", sendRtpItem.getPort());
zlmResult = startSendRtpStream(mediaInfo, param);
startSendRtpStreamResult = startSendRtpStream(mediaInfo, param);
}
}
return zlmResult;
return startSendRtpStreamResult;
}
public Boolean updateRtpServerSSRC(MediaServer mediaServerItem, String streamId, String ssrc) {
boolean result = false;
ZLMResult<?> zlmResult = zlmresTfulUtils.updateRtpServerSSRC(mediaServerItem, streamId, ssrc);
if (zlmResult.getCode() == 0) {
JSONObject jsonObject = zlmresTfulUtils.updateRtpServerSSRC(mediaServerItem, streamId, ssrc);
if (jsonObject == null) {
log.error("[更新RTPServer] 失败: 请检查ZLM服务");
} else if (jsonObject.getInteger("code") == 0) {
result= true;
log.info("[更新RTPServer] 成功");
} else {
log.error("[更新RTPServer] 失败: {}, streamId{}ssrc{}", zlmResult.getMsg(),
streamId, ssrc);
log.error("[更新RTPServer] 失败: {}, streamId{}ssrc{}->\r\n{}",jsonObject.getString("msg"),
streamId, ssrc, jsonObject);
}
return result;
}
public ZLMResult<?> stopSendRtpStream(MediaServer mediaServerItem, SendRtpInfo sendRtpItem) {
public JSONObject stopSendRtpStream(MediaServer mediaServerItem, SendRtpInfo sendRtpItem) {
Map<String, Object> param = new HashMap<>();
param.put("vhost", "__defaultVhost__");
param.put("app", sendRtpItem.getApp());

View File

@ -1,8 +0,0 @@
package com.genersoft.iot.vmp.media.zlm.dto;
import lombok.Data;
@Data
public class FlagData {
private boolean flag;
}

View File

@ -1,10 +0,0 @@
package com.genersoft.iot.vmp.media.zlm.dto;
import lombok.Data;
@Data
public class RtpServerResult {
private Integer port;
private String stream_id;
}

View File

@ -1,14 +0,0 @@
package com.genersoft.iot.vmp.media.zlm.dto;
import lombok.Data;
@Data
public class SessionData {
private String id;
private String local_ip;
private Integer local_port;
private String peer_ip;
private Integer peer_port;
private String typeid;
}

View File

@ -1,9 +0,0 @@
package com.genersoft.iot.vmp.media.zlm.dto;
import lombok.Data;
@Data
public class StreamProxyResult {
private String key;
}

View File

@ -1,58 +0,0 @@
package com.genersoft.iot.vmp.media.zlm.dto;
import lombok.Data;
@Data
public class ZLMResult<T> {
private int code;
private String msg;
private T data;
private Boolean online;
private Boolean exist;
private String peer_ip;
private Integer peer_port;
private String local_ip;
private Integer local_port;
private Integer changed;
private Integer port;
private Integer hit;
public static <T> ZLMResult<T> getFailForMediaServer() {
ZLMResult<T> zlmResult = new ZLMResult<>();
zlmResult.setCode(-2);
zlmResult.setMsg("流媒体调用失败");
return zlmResult;
}
public static <T> ZLMResult<T> getMediaServer(int code, String msg) {
return getMediaServer(code, msg, null);
}
public static <T> ZLMResult<T> getMediaServer(int code, String msg, T data) {
ZLMResult<T> 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) : "") +
'}';
}
}