From e46f1ae9150028b794a9519dda7d66bf77a486c2 Mon Sep 17 00:00:00 2001
From: xu-bin-bin <34916924+xu-bin-bin@users.noreply.github.com>
Date: 星期四, 31 八月 2023 16:51:06 +0800
Subject: [PATCH] Merge branch '648540858:wvp-28181-2.0' into wvp-28181-2.0
---
src/main/java/com/genersoft/iot/vmp/service/impl/StreamProxyServiceImpl.java | 114 +++++++++++++++++++++++++++++++++++++++++++++++++-------
1 files changed, 99 insertions(+), 15 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
old mode 100644
new mode 100755
index f73392f..d699bbc
--- 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,15 @@
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.DynamicTask;
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.MediaServerItem;
import com.genersoft.iot.vmp.media.zlm.dto.StreamProxyItem;
import com.genersoft.iot.vmp.media.zlm.dto.hook.OnStreamChangedHookParam;
@@ -37,6 +40,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.UUID;
/**
* 瑙嗛浠g悊涓氬姟
@@ -86,6 +90,12 @@
private IMediaServerService mediaServerService;
@Autowired
+ private ZlmHttpHookSubscribe hookSubscribe;
+
+ @Autowired
+ private DynamicTask dynamicTask;
+
+ @Autowired
DataSourceTransactionManager dataSourceTransactionManager;
@Autowired
@@ -93,7 +103,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 +114,40 @@
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() );
+ 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.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);
- StringBuffer resultMsg = new StringBuffer();
+ logger.info("[鎷夋祦浠g悊] 杈撳嚭鍦板潃涓猴細{}", dstUrl);
param.setMediaServerId(mediaInfo.getId());
boolean saveResult;
// 鏇存柊
@@ -117,14 +157,35 @@
saveResult = addStreamProxy(param);
}
if (!saveResult) {
- throw new ControllerException(ErrorCode.ERROR100.getCode(),"淇濆瓨澶辫触");
+ callback.run(ErrorCode.ERROR100.getCode(), "淇濆瓨澶辫触", null);
+ return;
}
- StreamInfo resultForStreamInfo = null;
- resultMsg.append("淇濆瓨鎴愬姛");
+
if (param.isEnable()) {
+ String talkKey = UUID.randomUUID().toString();
+ dynamicTask.startCron(talkKey, ()->{
+ StreamInfo streamInfo = mediaService.getStreamInfoByAppAndStreamWithCheck(param.getApp(), param.getStream(), mediaInfo.getId(), false);
+ if (streamInfo != null) {
+ callback.run(ErrorCode.SUCCESS.getCode(), ErrorCode.SUCCESS.getMsg(), streamInfo);
+ }
+ }, 1000);
+ String delayTalkKey = UUID.randomUUID().toString();
+ dynamicTask.startDelay(delayTalkKey, ()->{
+ StreamInfo streamInfo = mediaService.getStreamInfoByAppAndStreamWithCheck(param.getApp(), param.getStream(), mediaInfo.getId(), false);
+ if (streamInfo != null) {
+ callback.run(ErrorCode.SUCCESS.getCode(), ErrorCode.SUCCESS.getMsg(), streamInfo);
+ }else {
+ dynamicTask.stop(talkKey);
+ callback.run(ErrorCode.ERROR100.getCode(), "瓒呮椂", null);
+ }
+ }, 5000);
JSONObject jsonObject = addStreamProxyToZlm(param);
- if (jsonObject == null || jsonObject.getInteger("code") != 0) {
- resultMsg.append(", 浣嗘槸鍚敤澶辫触锛岃妫�鏌ユ祦鍦板潃鏄惁鍙敤");
+ if (jsonObject != null && jsonObject.getInteger("code") == 0) {
+ dynamicTask.stop(talkKey);
+ 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.isEnableRemoveNoneReader()) {
@@ -132,14 +193,37 @@
}else {
updateStreamProxy(param);
}
+ if (jsonObject == null){
+ callback.run(ErrorCode.ERROR100.getCode(), "璁板綍宸蹭繚瀛橈紝鍚敤澶辫触", null);
+ }else {
+ callback.run(ErrorCode.ERROR100.getCode(), jsonObject.getString("msg"), null);
+ }
+ }
+ }
+ else{
+ StreamInfo streamInfo = mediaService.getStreamInfoByAppAndStream(
+ mediaInfo, param.getApp(), param.getStream(), null, null);
+ callback.run(ErrorCode.SUCCESS.getCode(), ErrorCode.SUCCESS.getMsg(), streamInfo);
+ }
+ }
- }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;
}
/**
@@ -227,10 +311,10 @@
return null;
}
if ("default".equals(param.getType())){
- result = zlmresTfulUtils.addStreamProxy(mediaServerItem, param.getApp(), param.getStream(), param.getUrl(),
+ result = zlmresTfulUtils.addStreamProxy(mediaServerItem, param.getApp(), param.getStream(), param.getUrl().trim(),
param.isEnableAudio(), param.isEnableMp4(), param.getRtpType());
}else if ("ffmpeg".equals(param.getType())) {
- result = zlmresTfulUtils.addFFmpegSource(mediaServerItem, param.getSrcUrl(), param.getDstUrl(),
+ result = zlmresTfulUtils.addFFmpegSource(mediaServerItem, param.getSrcUrl().trim(), param.getDstUrl(),
param.getTimeoutMs() + "", param.isEnableAudio(), param.isEnableMp4(),
param.getFfmpegCmdKey());
}
--
Gitblit v1.8.0