From cb5afce55eec2badda5090c27564e43f201ac9af Mon Sep 17 00:00:00 2001
From: 648540858 <648540858@qq.com>
Date: 星期二, 12 三月 2024 23:55:00 +0800
Subject: [PATCH] 优化语音对讲定时任务的删除
---
src/main/java/com/genersoft/iot/vmp/service/impl/MediaServerServiceImpl.java | 132 +++++++++++++++++--------------------------
1 files changed, 52 insertions(+), 80 deletions(-)
diff --git a/src/main/java/com/genersoft/iot/vmp/service/impl/MediaServerServiceImpl.java b/src/main/java/com/genersoft/iot/vmp/service/impl/MediaServerServiceImpl.java
old mode 100644
new mode 100755
index 0597ea4..190d665
--- a/src/main/java/com/genersoft/iot/vmp/service/impl/MediaServerServiceImpl.java
+++ b/src/main/java/com/genersoft/iot/vmp/service/impl/MediaServerServiceImpl.java
@@ -3,6 +3,7 @@
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
+import com.baomidou.dynamic.datasource.annotation.DS;
import com.genersoft.iot.vmp.common.CommonCallback;
import com.genersoft.iot.vmp.common.VideoManagerConstants;
import com.genersoft.iot.vmp.conf.DynamicTask;
@@ -24,28 +25,36 @@
import com.genersoft.iot.vmp.utils.JsonUtil;
import com.genersoft.iot.vmp.utils.redis.RedisUtil;
import com.genersoft.iot.vmp.vmanager.bean.ErrorCode;
+import com.genersoft.iot.vmp.vmanager.bean.RecordFile;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
+import org.springframework.scheduling.annotation.Async;
+import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
+import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import java.io.File;
import java.time.LocalDateTime;
import java.util.*;
+import java.util.concurrent.CompletableFuture;
+import java.util.concurrent.ExecutionException;
/**
* 濯掍綋鏈嶅姟鍣ㄨ妭鐐圭鐞�
*/
@Service
+@DS("master")
public class MediaServerServiceImpl implements IMediaServerService {
private final static Logger logger = LoggerFactory.getLogger(MediaServerServiceImpl.class);
@@ -87,7 +96,7 @@
@Autowired
- private ZLMServerFactory ZLMServerFactory;
+ private ZLMServerFactory zlmServerFactory;
@Autowired
private EventPublisher publisher;
@@ -104,6 +113,11 @@
@Autowired
private RedisTemplate<Object, Object> redisTemplate;
+ @Qualifier("taskExecutor")
+ @Autowired
+ private ThreadPoolTaskExecutor taskExecutor;
+
+
@@ -118,36 +132,8 @@
continue;
}
// 鏇存柊
- if (ssrcFactory.hasMediaServerSSRC(mediaServerItem.getId())) {
+ if (!ssrcFactory.hasMediaServerSSRC(mediaServerItem.getId())) {
ssrcFactory.initMediaServerSSRC(mediaServerItem.getId(), null);
- }
- if (userSetting.getGbSendStreamStrict()) {
- int startPort = 50000;
- int endPort = 60000;
- String sendRtpPortRange = mediaServerItem.getSendRtpPortRange();
- if (sendRtpPortRange == null) {
- logger.warn("[zlm] ] 鏈厤缃彂娴佺鍙h寖鍥达紝榛樿浣跨敤50000鍒�60000");
- }else {
- String[] sendRtpPortRangeArray = sendRtpPortRange.trim().split(",");
- if (sendRtpPortRangeArray.length != 2) {
- logger.warn("[zlm] ] 鍙戞祦绔彛鑼冨洿閿欒锛岄粯璁や娇鐢�50000鍒�60000");
- }else {
- try {
- startPort = Integer.parseInt(sendRtpPortRangeArray[0]);
- endPort = Integer.parseInt(sendRtpPortRangeArray[1]);
- if (endPort <= startPort) {
- logger.warn("[zlm] ] 鍙戞祦绔彛鑼冨洿閿欒锛岀粨鏉熺鍙e簲澶т簬寮�濮嬬鍙�,浣跨敤榛樿绔彛");
- startPort = 50000;
- endPort = 60000;
- }
-
- }catch (NumberFormatException e) {
- logger.warn("[zlm] ] 鍙戞祦绔彛鑼冨洿閿欒锛岄粯璁や娇鐢�50000鍒�60000");
- }
- }
- }
- logger.info("[[zlm] ] 閰嶇疆鍙戞祦绔彛鑼冨洿锛寋}-{}", startPort, endPort);
- sendRtpPortManager.initServerPort(mediaServerItem.getId(), startPort, endPort);
}
// 鏌ヨredis鏄惁瀛樺湪姝ediaServer
String key = VideoManagerConstants.MEDIA_SERVER_PREFIX + userSetting.getServerId() + "_" + mediaServerItem.getId();
@@ -179,11 +165,15 @@
}
if (streamId == null) {
- streamId = String.format("%08x", Integer.parseInt(ssrc)).toUpperCase();
+ streamId = String.format("%08x", Long.parseLong(ssrc)).toUpperCase();
+ }
+ if (ssrcCheck && tcpMode > 0) {
+ // 鐩墠zlm涓嶆敮鎸� tcp妯″紡鏇存柊ssrc锛屾殏鏃跺叧闂璼src鏍¢獙
+ logger.warn("[openRTPServer] 骞冲彴瀵规帴鏃朵笅绾у彲鑳借嚜瀹氫箟ssrc锛屼絾鏄痶cp妯″紡zlm鏀舵祦鐩墠鏃犳硶鏇存柊ssrc锛屽彲鑳芥敹娴佽秴鏃讹紝姝ゆ椂璇蜂娇鐢╱dp鏀舵祦鎴栬�呭叧闂璼src鏍¢獙");
}
int rtpServerPort;
if (mediaServerItem.isRtpEnable()) {
- rtpServerPort = zlmrtpServerFactory.createRTPServer(mediaServerItem, streamId, ssrcCheck?Integer.parseInt(ssrc):0, port, onlyAuto, reUsePort, tcpMode);
+ rtpServerPort = zlmServerFactory.createRTPServer(mediaServerItem, streamId, ssrcCheck ? Long.parseLong(ssrc) : 0, port, onlyAuto, reUsePort, tcpMode);
} else {
rtpServerPort = mediaServerItem.getRtpProxyPort();
}
@@ -201,7 +191,7 @@
if (mediaServerItem == null) {
return;
}
- ZLMServerFactory.closeRtpServer(mediaServerItem, streamId);
+ zlmServerFactory.closeRtpServer(mediaServerItem, streamId);
}
@Override
@@ -210,18 +200,21 @@
callback.run(false);
return;
}
- ZLMServerFactory.closeRtpServer(mediaServerItem, streamId, callback);
+ zlmServerFactory.closeRtpServer(mediaServerItem, streamId, callback);
}
@Override
public void closeRTPServer(String mediaServerId, String streamId) {
MediaServerItem mediaServerItem = this.getOne(mediaServerId);
- closeRTPServer(mediaServerItem, streamId);
+ if (mediaServerItem != null && mediaServerItem.isRtpEnable()) {
+ closeRTPServer(mediaServerItem, streamId);
+ }
+ zlmresTfulUtils.closeStreams(mediaServerItem, "rtp", streamId);
}
@Override
public Boolean updateRtpServerSSRC(MediaServerItem mediaServerItem, String streamId, String ssrc) {
- return ZLMServerFactory.updateRtpServerSSRC(mediaServerItem, streamId, ssrc);
+ return zlmServerFactory.updateRtpServerSSRC(mediaServerItem, streamId, ssrc);
}
@Override
@@ -248,7 +241,7 @@
mediaServerMapper.update(mediaSerItem);
MediaServerItem mediaServerItemInRedis = getOne(mediaSerItem.getId());
MediaServerItem mediaServerItemInDataBase = mediaServerMapper.queryOne(mediaSerItem.getId());
- if (mediaServerItemInRedis == null || ssrcFactory.hasMediaServerSSRC(mediaSerItem.getId())) {
+ if (mediaServerItemInRedis == null || !ssrcFactory.hasMediaServerSSRC(mediaSerItem.getId())) {
ssrcFactory.initMediaServerSSRC(mediaServerItemInDataBase.getId(),null);
}
String key = VideoManagerConstants.MEDIA_SERVER_PREFIX + userSetting.getServerId() + "_" + mediaServerItemInDataBase.getId();
@@ -324,7 +317,6 @@
@Override
public MediaServerItem getDefaultMediaServer() {
-
return mediaServerMapper.queryDefault();
}
@@ -431,7 +423,7 @@
}
mediaServerMapper.update(serverItem);
String key = VideoManagerConstants.MEDIA_SERVER_PREFIX + userSetting.getServerId() + "_" + zlmServerConfig.getGeneralMediaServerId();
- if (ssrcFactory.hasMediaServerSSRC(serverItem.getId())) {
+ if (!ssrcFactory.hasMediaServerSSRC(serverItem.getId())) {
ssrcFactory.initMediaServerSSRC(zlmServerConfig.getGeneralMediaServerId(), null);
}
redisTemplate.opsForValue().set(key, serverItem);
@@ -439,17 +431,6 @@
if (serverItem.isAutoConfig()) {
- // 鏌ョ湅assist鏈嶅姟鐨勫綍鍍忚矾寰勯厤缃�
- if (serverItem.getRecordAssistPort() > 0 && userSetting.getRecordPath() == null) {
- JSONObject info = assistRESTfulUtils.getInfo(serverItem, null);
- if (info != null && info.getInteger("code") != null && info.getInteger("code") == 0 ) {
- JSONObject dataJson = info.getJSONObject("data");
- if (dataJson != null) {
- String recordPath = dataJson.getString("record");
- userSetting.setRecordPath(recordPath);
- }
- }
- }
setZLMConfig(serverItem, "0".equals(zlmServerConfig.getHookEnable()));
}
final String zlmKeepaliveKey = zlmKeepaliveKeyPrefix + serverItem.getId();
@@ -584,34 +565,30 @@
logger.info("[ZLM] 姝e湪璁剧疆 锛歿} -> {}:{}",
mediaServerItem.getId(), mediaServerItem.getIp(), mediaServerItem.getHttpPort());
String protocol = sslEnabled ? "https" : "http";
- String hookPrex = String.format("%s://%s:%s/index/hook", protocol, mediaServerItem.getHookIp(), serverPort);
+ String hookPrefix = String.format("%s://%s:%s/index/hook", protocol, mediaServerItem.getHookIp(), serverPort);
Map<String, Object> param = new HashMap<>();
param.put("api.secret",mediaServerItem.getSecret()); // -profile:v Baseline
if (mediaServerItem.getRtspPort() != 0) {
- param.put("ffmpeg.snap", "%s -rtsp_transport tcp -i %s -y -f mjpeg -t 0.001 %s");
+ param.put("ffmpeg.snap", "%s -rtsp_transport tcp -i %s -y -f mjpeg -frames:v 1 %s");
}
param.put("hook.enable","1");
param.put("hook.on_flow_report","");
- param.put("hook.on_play",String.format("%s/on_play", hookPrex));
+ param.put("hook.on_play",String.format("%s/on_play", hookPrefix));
param.put("hook.on_http_access","");
- param.put("hook.on_publish", String.format("%s/on_publish", hookPrex));
+ param.put("hook.on_publish", String.format("%s/on_publish", hookPrefix));
param.put("hook.on_record_ts","");
param.put("hook.on_rtsp_auth","");
param.put("hook.on_rtsp_realm","");
- param.put("hook.on_server_started",String.format("%s/on_server_started", hookPrex));
+ param.put("hook.on_server_started",String.format("%s/on_server_started", hookPrefix));
param.put("hook.on_shell_login","");
- param.put("hook.on_stream_changed",String.format("%s/on_stream_changed", hookPrex));
- param.put("hook.on_stream_none_reader",String.format("%s/on_stream_none_reader", hookPrex));
- param.put("hook.on_stream_not_found",String.format("%s/on_stream_not_found", hookPrex));
- param.put("hook.on_server_keepalive",String.format("%s/on_server_keepalive", hookPrex));
- param.put("hook.on_send_rtp_stopped",String.format("%s/on_send_rtp_stopped", hookPrex));
- param.put("hook.on_rtp_server_timeout",String.format("%s/on_rtp_server_timeout", hookPrex));
- if (mediaServerItem.getRecordAssistPort() > 0) {
- param.put("hook.on_record_mp4",String.format("http://127.0.0.1:%s/api/record/on_record_mp4", mediaServerItem.getRecordAssistPort()));
- }else {
- param.put("hook.on_record_mp4","");
- }
+ param.put("hook.on_stream_changed",String.format("%s/on_stream_changed", hookPrefix));
+ param.put("hook.on_stream_none_reader",String.format("%s/on_stream_none_reader", hookPrefix));
+ param.put("hook.on_stream_not_found",String.format("%s/on_stream_not_found", hookPrefix));
+ param.put("hook.on_server_keepalive",String.format("%s/on_server_keepalive", hookPrefix));
+ param.put("hook.on_send_rtp_stopped",String.format("%s/on_send_rtp_stopped", hookPrefix));
+ param.put("hook.on_rtp_server_timeout",String.format("%s/on_rtp_server_timeout", hookPrefix));
+ param.put("hook.on_record_mp4",String.format("%s/on_record_mp4", hookPrefix));
param.put("hook.timeoutSec","20");
// 鎺ㄦ祦鏂紑鍚庡彲浠ュ湪瓒呮椂鏃堕棿鍐呴噸鏂拌繛鎺ヤ笂缁х画鎺ㄦ祦锛岃繖鏍锋挱鏀惧櫒浼氭帴鐫�鎾斁銆�
// 缃�0鍏抽棴姝ょ壒鎬�(鎺ㄦ祦鏂紑浼氬鑷寸珛鍗虫柇寮�鎾斁鍣�)
@@ -620,15 +597,14 @@
param.put("protocol.continue_push_ms", "3000" );
// 鏈�澶氱瓑寰呮湭鍒濆鍖栫殑Track鏃堕棿锛屽崟浣嶆绉掞紝瓒呮椂涔嬪悗浼氬拷鐣ユ湭鍒濆鍖栫殑Track, 璁剧疆姝ら�夐」浼樺寲閭d簺闊抽閿欒鐨勪笉瑙勮寖娴侊紝
// 绛墇lm鏀寔缁欐瘡涓猺tpServer璁剧疆鍏抽棴闊抽鐨勬椂鍊欏彲浠ヤ笉璁剧疆姝ら�夐」
-// param.put("general.wait_track_ready_ms", "3000" );
if (mediaServerItem.isRtpEnable() && !ObjectUtils.isEmpty(mediaServerItem.getRtpPortRange())) {
param.put("rtp_proxy.port_range", mediaServerItem.getRtpPortRange().replace(",", "-"));
}
- if (userSetting.getRecordPath() != null) {
- File recordPathFile = new File(userSetting.getRecordPath());
- File mp4SavePathFile = recordPathFile.getParentFile().getAbsoluteFile();
- param.put("protocol.mp4_save_path", mp4SavePathFile.getAbsoluteFile());
+ if (!ObjectUtils.isEmpty(mediaServerItem.getRecordPath())) {
+ File recordPathFile = new File(mediaServerItem.getRecordPath());
+ param.put("protocol.mp4_save_path", recordPathFile.getParentFile().getPath());
+ param.put("protocol.downloadRoot", recordPathFile.getParentFile().getPath());
param.put("record.appName", recordPathFile.getName());
}
@@ -733,6 +709,7 @@
ssrcFactory.initMediaServerSSRC(mediaServerItem.getId(), null);
String key = VideoManagerConstants.MEDIA_SERVER_PREFIX + userSetting.getServerId() + "_" + mediaServerItem.getId();
redisTemplate.opsForValue().set(key, mediaServerItem);
+ resetOnlineServerItem(mediaServerItem);
clearRTPServer(mediaServerItem);
}
final String zlmKeepaliveKey = zlmKeepaliveKeyPrefix + mediaServerItem.getId();
@@ -761,15 +738,6 @@
}
@Override
- public boolean checkRtpServer(MediaServerItem mediaServerItem, String app, String stream) {
- JSONObject rtpInfo = zlmresTfulUtils.getRtpInfo(mediaServerItem, stream);
- if(rtpInfo.getInteger("code") == 0){
- return rtpInfo.getBoolean("exist");
- }
- return false;
- }
-
- @Override
public MediaServerLoad getLoad(MediaServerItem mediaServerItem) {
MediaServerLoad result = new MediaServerLoad();
result.setId(mediaServerItem.getId());
@@ -781,4 +749,8 @@
return result;
}
+ @Override
+ public List<MediaServerItem> getAllWithAssistPort() {
+ return mediaServerMapper.queryAllWithAssistPort();
+ }
}
--
Gitblit v1.8.0