From 32c73ff595c9939b342017ffbd8c86bc16877ba6 Mon Sep 17 00:00:00 2001
From: 648540858 <648540858@qq.com>
Date: 星期一, 17 七月 2023 16:56:58 +0800
Subject: [PATCH] Merge branch '2.6.8' into wvp-28181-2.0

---
 src/main/java/com/genersoft/iot/vmp/media/zlm/SendRtpPortManager.java |   63 ++++++++++++++++++++++++-------
 1 files changed, 49 insertions(+), 14 deletions(-)

diff --git a/src/main/java/com/genersoft/iot/vmp/media/zlm/SendRtpPortManager.java b/src/main/java/com/genersoft/iot/vmp/media/zlm/SendRtpPortManager.java
index 1b9d25e..893e52a 100644
--- a/src/main/java/com/genersoft/iot/vmp/media/zlm/SendRtpPortManager.java
+++ b/src/main/java/com/genersoft/iot/vmp/media/zlm/SendRtpPortManager.java
@@ -1,12 +1,19 @@
 package com.genersoft.iot.vmp.media.zlm;
 
+import com.genersoft.iot.vmp.common.VideoManagerConstants;
 import com.genersoft.iot.vmp.conf.UserSetting;
+import com.genersoft.iot.vmp.gb28181.bean.SendRtpItem;
 import com.genersoft.iot.vmp.media.zlm.dto.MediaSendRtpPortInfo;
+import com.genersoft.iot.vmp.utils.redis.RedisUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Component;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
 
 @Component
 public class SendRtpPortManager {
@@ -29,27 +36,55 @@
     }
 
     public int getNextPort(String mediaServerId) {
-        String key = KEY + userSetting.getServerId() + "_" +  mediaServerId;
-        MediaSendRtpPortInfo mediaSendRtpPortInfo = (MediaSendRtpPortInfo)redisTemplate.opsForValue().get(key);
+        String sendIndexKey = KEY + userSetting.getServerId() + "_" +  mediaServerId;
+        MediaSendRtpPortInfo mediaSendRtpPortInfo = (MediaSendRtpPortInfo)redisTemplate.opsForValue().get(sendIndexKey);
         if (mediaSendRtpPortInfo == null) {
             logger.warn("[鍙戦�佺鍙g鐞哴 鑾峰彇{}鐨勫彂閫佺鍙f椂鏈壘鍒扮鍙d俊鎭�", mediaServerId);
             return 0;
         }
-        int port;
-        if (mediaSendRtpPortInfo.getCurrent() %2 != 0) {
-            port = mediaSendRtpPortInfo.getCurrent() + 1;
-        }else {
-            port = mediaSendRtpPortInfo.getCurrent() + 2;
-        }
-        if (port > mediaSendRtpPortInfo.getEnd()) {
-            if (mediaSendRtpPortInfo.getStart() %2 != 0) {
-                port = mediaSendRtpPortInfo.getStart() + 1;
-            }else {
-                port = mediaSendRtpPortInfo.getStart();
+
+        String key = VideoManagerConstants.PLATFORM_SEND_RTP_INFO_PREFIX
+                + userSetting.getServerId() + "_*";
+        List<Object> queryResult = RedisUtil.scan(redisTemplate, key);
+        Map<Integer, SendRtpItem> sendRtpItemMap = new HashMap<>();
+
+        for (Object o : queryResult) {
+            SendRtpItem sendRtpItem = (SendRtpItem) redisTemplate.opsForValue().get(o);
+            if (sendRtpItem != null) {
+                sendRtpItemMap.put(sendRtpItem.getLocalPort(), sendRtpItem);
             }
         }
+
+        int port = getPort(mediaSendRtpPortInfo.getCurrent(),
+                mediaSendRtpPortInfo.getStart(),
+                mediaSendRtpPortInfo.getEnd(), checkPort -> sendRtpItemMap.get(checkPort) == null);
+
         mediaSendRtpPortInfo.setCurrent(port);
-        redisTemplate.opsForValue().set(key, mediaSendRtpPortInfo);
+        redisTemplate.opsForValue().set(sendIndexKey, mediaSendRtpPortInfo);
+        return port;
+    }
+
+    interface CheckPortCallback{
+        boolean check(int port);
+    }
+
+    private int getPort(int current, int start, int end, CheckPortCallback checkPortCallback) {
+        int port;
+        if (current %2 != 0) {
+            port = current + 1;
+        }else {
+            port = current + 2;
+        }
+        if (port > end) {
+            if (start %2 != 0) {
+                port = start + 1;
+            }else {
+                port = start;
+            }
+        }
+        if (!checkPortCallback.check(port)) {
+            return getPort(port, start, end, checkPortCallback);
+        }
         return port;
     }
 }

--
Gitblit v1.8.0