From 468e2b2ffbbb4e15dea11d1f9c7451a8097a3ab8 Mon Sep 17 00:00:00 2001
From: xiaoQQya <xiaoQQya@126.com>
Date: 星期日, 09 七月 2023 20:22:40 +0800
Subject: [PATCH] fix(play): 修复作为上级平台点播截取快照问题

---
 src/main/java/com/genersoft/iot/vmp/vmanager/rtp/RtpController.java |   52 ++++++++++++++++++++++++----------------------------
 1 files changed, 24 insertions(+), 28 deletions(-)

diff --git a/src/main/java/com/genersoft/iot/vmp/vmanager/rtp/RtpController.java b/src/main/java/com/genersoft/iot/vmp/vmanager/rtp/RtpController.java
index 5c74fcc..2b061b4 100644
--- a/src/main/java/com/genersoft/iot/vmp/vmanager/rtp/RtpController.java
+++ b/src/main/java/com/genersoft/iot/vmp/vmanager/rtp/RtpController.java
@@ -7,11 +7,13 @@
 import com.genersoft.iot.vmp.conf.UserSetting;
 import com.genersoft.iot.vmp.conf.VersionInfo;
 import com.genersoft.iot.vmp.conf.exception.ControllerException;
-import com.genersoft.iot.vmp.media.zlm.ZLMRTPServerFactory;
+import com.genersoft.iot.vmp.media.zlm.SendRtpPortManager;
+import com.genersoft.iot.vmp.media.zlm.ZLMServerFactory;
 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.HookSubscribeForRtpServerTimeout;
 import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem;
+import com.genersoft.iot.vmp.media.zlm.dto.hook.OnRtpServerTimeoutHookParam;
 import com.genersoft.iot.vmp.service.IDeviceChannelService;
 import com.genersoft.iot.vmp.service.IDeviceService;
 import com.genersoft.iot.vmp.service.IMediaServerService;
@@ -28,14 +30,13 @@
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Value;
 import org.springframework.data.redis.core.RedisTemplate;
-import org.springframework.web.bind.annotation.GetMapping;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.ResponseBody;
-import org.springframework.web.bind.annotation.RestController;
+import org.springframework.util.ObjectUtils;
+import org.springframework.web.bind.annotation.*;
 
 import java.io.IOException;
 import java.util.HashMap;
 import java.util.Map;
+import java.util.concurrent.TimeUnit;
 
 @SuppressWarnings("rawtypes")
 @Tag(name = "绗笁鏂规湇鍔″鎺�")
@@ -44,10 +45,13 @@
 @RequestMapping("/api/rtp")
 public class RtpController {
 
-    private final static Logger logger = LoggerFactory.getLogger(RtpController.class);
+    @Autowired
+    private ZLMServerFactory zlmServerFactory;
 
     @Autowired
-    private ZLMRTPServerFactory zlmServerFactory;
+    private SendRtpPortManager sendRtpPortManager;
+
+    private final static Logger logger = LoggerFactory.getLogger(RtpController.class);
 
     @Autowired
     private ZlmHttpHookSubscribe hookSubscribe;
@@ -95,7 +99,7 @@
     @Parameter(name = "stream", description = "褰㈡垚鐨勬祦鐨処D", required = true)
     @Parameter(name = "tcpMode", description = "鏀舵祦妯″紡锛� 0涓篣DP锛� 1涓篢CP琚姩", required = true)
     @Parameter(name = "callBack", description = "鍥炶皟鍦板潃锛屽鏋滄敹娴佽秴鏃朵細閫氶亾鍥炶皟閫氱煡锛屽洖璋冧负get璇锋眰锛屽弬鏁颁负callId", required = true)
-    public OtherRtpSendInfo openRtpServer(Boolean isSend, String ssrc, String callId, String stream, Integer tcpMode, String callBack) {
+    public OtherRtpSendInfo openRtpServer(Boolean isSend, @RequestParam(required = false)String ssrc, String callId, String stream, Integer tcpMode, String callBack) {
 
         logger.info("[绗笁鏂规湇鍔″鎺�->寮�鍚敹娴佸拰鑾峰彇鍙戞祦淇℃伅] isSend->{}, ssrc->{}, callId->{}, stream->{}, tcpMode->{}, callBack->{}",
                 isSend, ssrc, callId, stream, tcpMode==0?"UDP":"TCP琚姩", callBack);
@@ -125,8 +129,9 @@
             HookSubscribeForRtpServerTimeout hookSubscribeForRtpServerTimeout = HookSubscribeFactory.on_rtp_server_timeout(ssrc, null, mediaServerItem.getId());
             // 璁㈤槄 zlm鍚姩浜嬩欢, 鏂扮殑zlm涔熶細浠庤繖閲岃繘鍏ョ郴缁�
             hookSubscribe.addSubscribe(hookSubscribeForRtpServerTimeout,
-                    (mediaServerItemInUse, response)->{
-                        if (stream.equals(response.getString("stream_id"))) {
+                    (mediaServerItemInUse, hookParam)->{
+                        OnRtpServerTimeoutHookParam serverTimeoutHookParam = (OnRtpServerTimeoutHookParam) hookParam;
+                        if (stream.equals(serverTimeoutHookParam.getStream_id())) {
                             logger.info("[寮�鍚敹娴佸拰鑾峰彇鍙戞祦淇℃伅] 绛夊緟鏀舵祦瓒呮椂 callId->{}, 鍙戦�佸洖璋�", callId);
                             OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
                             OkHttpClient client = httpClientBuilder.build();
@@ -140,30 +145,20 @@
                         }
                     });
         }
+        String key = VideoManagerConstants.WVP_OTHER_SEND_RTP_INFO + userSetting.getServerId() + callId;
         OtherRtpSendInfo otherRtpSendInfo = new OtherRtpSendInfo();
         otherRtpSendInfo.setReceiveIp(mediaServerItem.getSdpIp());
         otherRtpSendInfo.setReceivePort(localPort);
         otherRtpSendInfo.setCallId(callId);
         otherRtpSendInfo.setStream(stream);
         if (isSend != null && isSend) {
-            String key = VideoManagerConstants.WVP_OTHER_SEND_RTP_INFO + userSetting.getServerId() + callId;
-            // 棰勫垱寤哄彂娴佷俊鎭�
-            int port = zlmServerFactory.keepPort(mediaServerItem, callId, 0, ssrc1 -> {
-                return redisTemplate.opsForValue().get(key) != null;
-            });
-
-            // 灏嗕俊鎭啓鍏edis涓紝浠ュ鍚庣敤
-            redisTemplate.opsForValue().set(key, otherRtpSendInfo);
-            // 璁剧疆瓒呮椂浠诲姟锛岃秴鏃舵湭浣跨敤锛屽垯鑷姩绉婚櫎锛屽苟鍏抽棴绔彛淇濇寔, 榛樿浜斿垎閽�
-            dynamicTask.startDelay(key, ()->{
-                logger.info("[绗笁鏂规湇鍔″鎺�->寮�鍚敹娴佸拰鑾峰彇鍙戞祦淇℃伅] 绔彛淇濇寔瓒呮椂 callId->{}", callId);
-                redisTemplate.delete(key);
-                zlmServerFactory.releasePort(mediaServerItem, callId);
-            }, 300000);
+            int port = sendRtpPortManager.getNextPort(mediaServerItem.getId());
             otherRtpSendInfo.setIp(mediaServerItem.getSdpIp());
             otherRtpSendInfo.setPort(port);
             logger.info("[寮�鍚敹娴佸拰鑾峰彇鍙戞祦淇℃伅] 缁撴灉锛宑allId->{}锛� {}", callId, otherRtpSendInfo);
         }
+        // 灏嗕俊鎭啓鍏edis涓紝浠ュ鍚庣敤
+        redisTemplate.opsForValue().set(key, otherRtpSendInfo, 300, TimeUnit.SECONDS);
         return otherRtpSendInfo;
     }
 
@@ -189,15 +184,16 @@
     @Parameter(name = "onlyAudio", description = "鏄惁鍙湁闊抽", required = true)
     @Parameter(name = "isUdp", description = "鏄惁涓篣DP", required = true)
     @Parameter(name = "streamType", description = "娴佺被鍨嬶紝1涓篹s娴侊紝2涓簆s娴侊紝 榛樿es娴�", required = false)
-    public void sendRTP(String ssrc, String ip, Integer port, String app, String stream, String callId, Boolean onlyAudio, Boolean isUdp, Integer streamType) {
+    public void sendRTP(String ssrc, String ip, Integer port, String app, String stream, String callId, Boolean onlyAudio, Boolean isUdp, @RequestParam(required = false)Integer streamType) {
         logger.info("[绗笁鏂规湇鍔″鎺�->鍙戦�佹祦] ssrc->{}, ip->{}, port->{}, app->{}, stream->{}, callId->{}, onlyAudio->{}, streamType->{}",
                 ssrc, ip, port, app, stream, callId, onlyAudio, streamType == 1? "ES":"PS");
+        if (ObjectUtils.isEmpty(streamType)) {
+            streamType = 1;
+        }
         MediaServerItem mediaServerItem = mediaServerService.getDefaultMediaServer();
         String key = VideoManagerConstants.WVP_OTHER_SEND_RTP_INFO + userSetting.getServerId() + callId;
         OtherRtpSendInfo sendInfo = (OtherRtpSendInfo)redisTemplate.opsForValue().get(key);
-        if (sendInfo != null) {
-            zlmServerFactory.releasePort(mediaServerItem, sendInfo.getCallId());
-        }else {
+        if (sendInfo == null) {
             sendInfo = new OtherRtpSendInfo();
         }
         sendInfo.setPushApp(app);

--
Gitblit v1.8.0