From a2d93fce811acc83ad5ff0b4a93403db22795a10 Mon Sep 17 00:00:00 2001
From: winfed <chinesezwf@gmail.com>
Date: 星期五, 09 六月 2023 15:19:18 +0800
Subject: [PATCH] fix:修复拉流代理配置展示问题:音频、录制、无人观看
---
src/main/java/com/genersoft/iot/vmp/service/impl/StreamProxyServiceImpl.java | 125 +++++++++++++++++++++++++++++++++--------
1 files changed, 100 insertions(+), 25 deletions(-)
diff --git a/src/main/java/com/genersoft/iot/vmp/service/impl/StreamProxyServiceImpl.java b/src/main/java/com/genersoft/iot/vmp/service/impl/StreamProxyServiceImpl.java
index 02f4f91..9ac6ab9 100644
--- a/src/main/java/com/genersoft/iot/vmp/service/impl/StreamProxyServiceImpl.java
+++ b/src/main/java/com/genersoft/iot/vmp/service/impl/StreamProxyServiceImpl.java
@@ -2,12 +2,16 @@
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
+import com.genersoft.iot.vmp.common.GeneralCallback;
import com.genersoft.iot.vmp.common.StreamInfo;
import com.genersoft.iot.vmp.conf.UserSetting;
import com.genersoft.iot.vmp.conf.exception.ControllerException;
import com.genersoft.iot.vmp.gb28181.event.EventPublisher;
import com.genersoft.iot.vmp.gb28181.event.subscribe.catalog.CatalogEvent;
import com.genersoft.iot.vmp.media.zlm.ZLMRESTfulUtils;
+import com.genersoft.iot.vmp.media.zlm.ZlmHttpHookSubscribe;
+import com.genersoft.iot.vmp.media.zlm.dto.HookSubscribeFactory;
+import com.genersoft.iot.vmp.media.zlm.dto.HookSubscribeForStreamChange;
import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem;
import com.genersoft.iot.vmp.media.zlm.dto.StreamProxyItem;
import com.genersoft.iot.vmp.media.zlm.dto.hook.OnStreamChangedHookParam;
@@ -23,7 +27,7 @@
import com.genersoft.iot.vmp.storager.dao.StreamProxyMapper;
import com.genersoft.iot.vmp.utils.DateUtil;
import com.genersoft.iot.vmp.vmanager.bean.ErrorCode;
-import com.genersoft.iot.vmp.vmanager.bean.ResourceBaceInfo;
+import com.genersoft.iot.vmp.vmanager.bean.ResourceBaseInfo;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -86,6 +90,9 @@
private IMediaServerService mediaServerService;
@Autowired
+ private ZlmHttpHookSubscribe hookSubscribe;
+
+ @Autowired
DataSourceTransactionManager dataSourceTransactionManager;
@Autowired
@@ -93,7 +100,7 @@
@Override
- public StreamInfo save(StreamProxyItem param) {
+ public void save(StreamProxyItem param, GeneralCallback<StreamInfo> callback) {
MediaServerItem mediaInfo;
if (ObjectUtils.isEmpty(param.getMediaServerId()) || "auto".equals(param.getMediaServerId())){
mediaInfo = mediaServerService.getMediaServerForMinimumLoad(null);
@@ -104,10 +111,43 @@
logger.warn("淇濆瓨浠g悊鏈壘鍒板湪绾跨殑ZLM...");
throw new ControllerException(ErrorCode.ERROR100.getCode(), "淇濆瓨浠g悊鏈壘鍒板湪绾跨殑ZLM");
}
- String dstUrl = String.format("rtmp://%s:%s/%s/%s", "127.0.0.1", mediaInfo.getRtmpPort(), param.getApp(),
- param.getStream() );
- param.setDst_url(dstUrl);
- StringBuffer resultMsg = new StringBuffer();
+ String dstUrl;
+ if ("ffmpeg".equalsIgnoreCase(param.getType())) {
+ JSONObject jsonObject = zlmresTfulUtils.getMediaServerConfig(mediaInfo);
+ if (jsonObject.getInteger("code") != 0) {
+ throw new ControllerException(ErrorCode.ERROR100.getCode(), "鑾峰彇娴佸獟浣撻厤缃け璐�");
+ }
+ JSONArray dataArray = jsonObject.getJSONArray("data");
+ JSONObject mediaServerConfig = dataArray.getJSONObject(0);
+ String ffmpegCmd = mediaServerConfig.getString(param.getFfmpegCmdKey());
+ String schema = getSchemaFromFFmpegCmd(ffmpegCmd);
+ if (schema == null) {
+ throw new ControllerException(ErrorCode.ERROR100.getCode(), "ffmpeg鎷夋祦浠g悊鏃犳硶浠巉fmpeg cmd涓幏鍙栧埌杈撳嚭鏍煎紡");
+ }
+ int port;
+ String schemaForUri;
+ if (schema.equalsIgnoreCase("rtsp")) {
+ port = mediaInfo.getRtspPort();
+ schemaForUri = schema;
+ }else if (schema.equalsIgnoreCase("flv")) {
+ port = mediaInfo.getHttpPort();
+ schemaForUri = "http";
+ }else if (schema.equalsIgnoreCase("rtmp")) {
+ port = mediaInfo.getRtmpPort();
+ schemaForUri = schema;
+ }else {
+ port = mediaInfo.getRtmpPort();
+ schemaForUri = schema;
+ }
+
+ dstUrl = String.format("%s://%s:%s/%s/%s", schemaForUri, "127.0.0.1", port, param.getApp(),
+ param.getStream());
+ }else {
+ dstUrl = String.format("rtmp://%s:%s/%s/%s", "127.0.0.1", mediaInfo.getRtmpPort(), param.getApp(),
+ param.getStream());
+ }
+ param.setDstUrl(dstUrl);
+ logger.info("[鎷夋祦浠g悊] 杈撳嚭鍦板潃涓猴細{}", dstUrl);
param.setMediaServerId(mediaInfo.getId());
boolean saveResult;
// 鏇存柊
@@ -117,29 +157,60 @@
saveResult = addStreamProxy(param);
}
if (!saveResult) {
- throw new ControllerException(ErrorCode.ERROR100.getCode(),"淇濆瓨澶辫触");
+ callback.run(ErrorCode.ERROR100.getCode(), "淇濆瓨澶辫触", null);
+ return;
}
- StreamInfo resultForStreamInfo = null;
- resultMsg.append("淇濆瓨鎴愬姛");
+
+ HookSubscribeForStreamChange hookSubscribeForStreamChange = HookSubscribeFactory.on_stream_changed(param.getApp(), param.getStream(), true, "rtsp", mediaInfo.getId());
+ hookSubscribe.addSubscribe(hookSubscribeForStreamChange, (mediaServerItem, response) -> {
+ StreamInfo streamInfo = mediaService.getStreamInfoByAppAndStream(
+ mediaInfo, param.getApp(), param.getStream(), null, null);
+ callback.run(ErrorCode.SUCCESS.getCode(), ErrorCode.SUCCESS.getMsg(), streamInfo);
+ });
+
if (param.isEnable()) {
JSONObject jsonObject = addStreamProxyToZlm(param);
- if (jsonObject == null || jsonObject.getInteger("code") != 0) {
- resultMsg.append(", 浣嗘槸鍚敤澶辫触锛岃妫�鏌ユ祦鍦板潃鏄惁鍙敤");
+ if (jsonObject != null && jsonObject.getInteger("code") == 0) {
+ hookSubscribe.removeSubscribe(hookSubscribeForStreamChange);
+ StreamInfo streamInfo = mediaService.getStreamInfoByAppAndStream(
+ mediaInfo, param.getApp(), param.getStream(), null, null);
+ callback.run(ErrorCode.SUCCESS.getCode(), ErrorCode.SUCCESS.getMsg(), streamInfo);
+ }else {
param.setEnable(false);
// 鐩存帴绉婚櫎
- if (param.isEnable_remove_none_reader()) {
+ if (param.isEnableRemoveNoneReader()) {
del(param.getApp(), param.getStream());
}else {
updateStreamProxy(param);
}
+ if (jsonObject == null){
+ callback.run(ErrorCode.ERROR100.getCode(), "璁板綍宸蹭繚瀛橈紝鍚敤澶辫触", null);
+ return;
+ }else {
+ callback.run(ErrorCode.ERROR100.getCode(), jsonObject.getString("msg"), null);
+ return;
+ }
+ }
+ }
+ }
- }else {
- resultForStreamInfo = mediaService.getStreamInfoByAppAndStream(
- mediaInfo, param.getApp(), param.getStream(), null, null);
+ private String getSchemaFromFFmpegCmd(String ffmpegCmd) {
+ ffmpegCmd = ffmpegCmd.replaceAll(" + ", " ");
+ String[] paramArray = ffmpegCmd.split(" ");
+ if (paramArray.length == 0) {
+ return null;
+ }
+ for (int i = 0; i < paramArray.length; i++) {
+ if (paramArray[i].equalsIgnoreCase("-f")) {
+ if (i + 1 < paramArray.length - 1) {
+ return paramArray[i+1];
+ }else {
+ return null;
+ }
}
}
- return resultForStreamInfo;
+ return null;
}
/**
@@ -228,11 +299,11 @@
}
if ("default".equals(param.getType())){
result = zlmresTfulUtils.addStreamProxy(mediaServerItem, param.getApp(), param.getStream(), param.getUrl(),
- param.isEnable_audio(), param.isEnable_mp4(), param.getRtp_type());
+ param.isEnableAudio(), param.isEnableMp4(), param.getRtpType());
}else if ("ffmpeg".equals(param.getType())) {
- result = zlmresTfulUtils.addFFmpegSource(mediaServerItem, param.getSrc_url(), param.getDst_url(),
- param.getTimeout_ms() + "", param.isEnable_audio(), param.isEnable_mp4(),
- param.getFfmpeg_cmd_key());
+ result = zlmresTfulUtils.addFFmpegSource(mediaServerItem, param.getSrcUrl(), param.getDstUrl(),
+ param.getTimeoutMs() + "", param.isEnableAudio(), param.isEnableMp4(),
+ param.getFfmpegCmdKey());
}
return result;
}
@@ -286,7 +357,7 @@
updateStreamProxy(streamProxy);
}else {
logger.info("鍚敤浠g悊澶辫触锛� {}/{}->{}({})", app, stream, jsonObject.getString("msg"),
- streamProxy.getSrc_url() == null? streamProxy.getUrl():streamProxy.getSrc_url());
+ streamProxy.getSrcUrl() == null? streamProxy.getUrl():streamProxy.getSrcUrl());
}
}
return result;
@@ -332,7 +403,7 @@
@Override
public void zlmServerOnline(String mediaServerId) {
// 绉婚櫎寮�鍚簡鏃犱汉瑙傜湅鑷姩绉婚櫎鐨勬祦
- List<StreamProxyItem> streamProxyItemList = streamProxyMapper.selecAutoRemoveItemByMediaServerId(mediaServerId);
+ List<StreamProxyItem> streamProxyItemList = streamProxyMapper.selectAutoRemoveItemByMediaServerId(mediaServerId);
if (streamProxyItemList.size() > 0) {
gbStreamMapper.batchDel(streamProxyItemList);
}
@@ -360,7 +431,7 @@
@Override
public void zlmServerOffline(String mediaServerId) {
// 绉婚櫎寮�鍚簡鏃犱汉瑙傜湅鑷姩绉婚櫎鐨勬祦
- List<StreamProxyItem> streamProxyItemList = streamProxyMapper.selecAutoRemoveItemByMediaServerId(mediaServerId);
+ List<StreamProxyItem> streamProxyItemList = streamProxyMapper.selectAutoRemoveItemByMediaServerId(mediaServerId);
if (streamProxyItemList.size() > 0) {
gbStreamMapper.batchDel(streamProxyItemList);
}
@@ -438,7 +509,11 @@
}
@Override
- public ResourceBaceInfo getOverview() {
- return streamProxyMapper.getOverview();
+ public ResourceBaseInfo getOverview() {
+
+ int total = streamProxyMapper.getAllCount();
+ int online = streamProxyMapper.getOnline();
+
+ return new ResourceBaseInfo(total, online);
}
}
--
Gitblit v1.8.0