From f458c9b79334b88683044ae7ccaab29504700e73 Mon Sep 17 00:00:00 2001
From: 648540858 <648540858@qq.com>
Date: 星期三, 10 四月 2024 22:38:12 +0800
Subject: [PATCH] 优化代码调用

---
 src/main/java/com/genersoft/iot/vmp/vmanager/rtp/RtpController.java |  149 ++++++++++++++-----------------------------------
 1 files changed, 42 insertions(+), 107 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 f42d153..e08f769 100755
--- a/src/main/java/com/genersoft/iot/vmp/vmanager/rtp/RtpController.java
+++ b/src/main/java/com/genersoft/iot/vmp/vmanager/rtp/RtpController.java
@@ -6,13 +6,14 @@
 import com.genersoft.iot.vmp.conf.UserSetting;
 import com.genersoft.iot.vmp.conf.exception.ControllerException;
 import com.genersoft.iot.vmp.conf.security.JwtUtils;
+import com.genersoft.iot.vmp.gb28181.bean.SendRtpItem;
 import com.genersoft.iot.vmp.media.event.hook.Hook;
 import com.genersoft.iot.vmp.media.event.hook.HookType;
 import com.genersoft.iot.vmp.media.zlm.SendRtpPortManager;
-import com.genersoft.iot.vmp.media.zlm.ZLMServerFactory;
 import com.genersoft.iot.vmp.media.event.hook.HookSubscribe;
 import com.genersoft.iot.vmp.media.bean.MediaServer;
 import com.genersoft.iot.vmp.media.service.IMediaServerService;
+import com.genersoft.iot.vmp.service.bean.SSRCInfo;
 import com.genersoft.iot.vmp.utils.redis.RedisUtil;
 import com.genersoft.iot.vmp.vmanager.bean.ErrorCode;
 import com.genersoft.iot.vmp.vmanager.bean.OtherRtpSendInfo;
@@ -42,9 +43,6 @@
 @RestController
 @RequestMapping("/api/rtp")
 public class RtpController {
-
-    @Autowired
-    private ZLMServerFactory zlmServerFactory;
 
     @Autowired
     private SendRtpPortManager sendRtpPortManager;
@@ -81,8 +79,8 @@
         logger.info("[绗笁鏂规湇鍔″鎺�->寮�鍚敹娴佸拰鑾峰彇鍙戞祦淇℃伅] isSend->{}, ssrc->{}, callId->{}, stream->{}, tcpMode->{}, callBack->{}",
                 isSend, ssrc, callId, stream, tcpMode==0?"UDP":"TCP琚姩", callBack);
 
-        MediaServer mediaServerItem = mediaServerService.getDefaultMediaServer();
-        if (mediaServerItem == null) {
+        MediaServer mediaServer = mediaServerService.getDefaultMediaServer();
+        if (mediaServer == null) {
             throw new ControllerException(ErrorCode.ERROR100.getCode(),"娌℃湁鍙敤鐨凪ediaServer");
         }
         if (stream == null) {
@@ -100,14 +98,14 @@
             }
         }
         String receiveKey = VideoManagerConstants.WVP_OTHER_RECEIVE_RTP_INFO + userSetting.getServerId() + "_" + callId + "_"  + stream;
-        int localPortForVideo = zlmServerFactory.createRTPServer(mediaServerItem, stream, ssrcInt, null, false, false, tcpMode);
-        int localPortForAudio = zlmServerFactory.createRTPServer(mediaServerItem, stream + "_a" , ssrcInt, null, false, false, tcpMode);
-        if (localPortForVideo == 0 || localPortForAudio == 0) {
+        SSRCInfo ssrcInfoForVideo =  mediaServerService.openRTPServer(mediaServer, stream, ssrcInt + "",false,false, null, false, false, false, tcpMode);
+        SSRCInfo ssrcInfoForAudio =  mediaServerService.openRTPServer(mediaServer, stream + "_a", ssrcInt + "", false, false, null, false,false,false, tcpMode);
+        if (ssrcInfoForVideo.getPort() == 0 || ssrcInfoForAudio.getPort() == 0) {
             throw new ControllerException(ErrorCode.ERROR100.getCode(), "鑾峰彇绔彛澶辫触");
         }
         // 娉ㄥ唽鍥炶皟濡傛灉rtp鏀舵祦瓒呮椂鍒欓�氳繃鍥炶皟鍙戦�侀�氱煡
         if (callBack != null) {
-            Hook hook = Hook.getInstance(HookType.on_rtp_server_timeout, "rtp", stream, mediaServerItem.getId());
+            Hook hook = Hook.getInstance(HookType.on_rtp_server_timeout, "rtp", stream, mediaServer.getId());
             // 璁㈤槄 zlm鍚姩浜嬩欢, 鏂扮殑zlm涔熶細浠庤繖閲岃繘鍏ョ郴缁�
             hookSubscribe.addSubscribe(hook,
                     (hookData)->{
@@ -128,9 +126,9 @@
         }
         String key = VideoManagerConstants.WVP_OTHER_SEND_RTP_INFO + userSetting.getServerId() + "_"  + callId;
         OtherRtpSendInfo otherRtpSendInfo = new OtherRtpSendInfo();
-        otherRtpSendInfo.setReceiveIp(mediaServerItem.getSdpIp());
-        otherRtpSendInfo.setReceivePortForVideo(localPortForVideo);
-        otherRtpSendInfo.setReceivePortForAudio(localPortForAudio);
+        otherRtpSendInfo.setReceiveIp(mediaServer.getSdpIp());
+        otherRtpSendInfo.setReceivePortForVideo(ssrcInfoForVideo.getPort());
+        otherRtpSendInfo.setReceivePortForAudio(ssrcInfoForAudio.getPort());
         otherRtpSendInfo.setCallId(callId);
         otherRtpSendInfo.setStream(stream);
 
@@ -138,10 +136,10 @@
         redisTemplate.opsForValue().set(receiveKey, otherRtpSendInfo);
         if (isSend != null && isSend) {
             // 棰勫垱寤哄彂娴佷俊鎭�
-            int portForVideo = sendRtpPortManager.getNextPort(mediaServerItem);
-            int portForAudio = sendRtpPortManager.getNextPort(mediaServerItem);
+            int portForVideo = sendRtpPortManager.getNextPort(mediaServer);
+            int portForAudio = sendRtpPortManager.getNextPort(mediaServer);
 
-            otherRtpSendInfo.setSendLocalIp(mediaServerItem.getSdpIp());
+            otherRtpSendInfo.setSendLocalIp(mediaServer.getSdpIp());
             otherRtpSendInfo.setSendLocalPortForVideo(portForVideo);
             otherRtpSendInfo.setSendLocalPortForAudio(portForAudio);
             // 灏嗕俊鎭啓鍏edis涓紝浠ュ鍚庣敤
@@ -160,8 +158,8 @@
     public void closeRtpServer(String stream) {
         logger.info("[绗笁鏂规湇鍔″鎺�->鍏抽棴鏀舵祦] stream->{}", stream);
         MediaServer mediaServerItem = mediaServerService.getDefaultMediaServer();
-        zlmServerFactory.closeRtpServer(mediaServerItem,stream);
-        zlmServerFactory.closeRtpServer(mediaServerItem,stream + "_a");
+        mediaServerService.closeRTPServer(mediaServerItem, stream);
+        mediaServerService.closeRTPServer(mediaServerItem, stream+ "_a");
         String receiveKey = VideoManagerConstants.WVP_OTHER_RECEIVE_RTP_INFO + userSetting.getServerId() + "_*_"  + stream;
         List<Object> scan = RedisUtil.scan(redisTemplate, receiveKey);
         if (scan.size() > 0) {
@@ -232,72 +230,31 @@
         sendInfo.setPushStream(stream);
         sendInfo.setPushSSRC(ssrc);
 
-        Map<String, Object> paramForAudio;
-        Map<String, Object> paramForVideo;
+
+        SendRtpItem sendRtpItemForVideo;
+        SendRtpItem sendRtpItemForAudio;
         if (!ObjectUtils.isEmpty(dstIpForAudio) && dstPortForAudio > 0) {
-            paramForAudio = new HashMap<>();
-            paramForAudio.put("vhost","__defaultVhost__");
-            paramForAudio.put("app",app);
-            paramForAudio.put("stream",stream);
-            paramForAudio.put("ssrc", ssrc);
-
-            paramForAudio.put("dst_url", dstIpForAudio);
-            paramForAudio.put("dst_port", dstPortForAudio);
-            String is_Udp = isUdp ? "1" : "0";
-            paramForAudio.put("is_udp", is_Udp);
-            paramForAudio.put("src_port", sendInfo.getSendLocalPortForAudio());
-            paramForAudio.put("only_audio", "1");
-            if (ptForAudio != null) {
-                paramForAudio.put("pt", ptForAudio);
-            }
-
+            sendRtpItemForAudio = SendRtpItem.getInstance(app, stream, ssrc, dstIpForAudio, dstPortForAudio, !isUdp, sendInfo.getSendLocalPortForAudio(), ptForAudio);
         } else {
-            paramForAudio = null;
+            sendRtpItemForAudio = null;
         }
         if (!ObjectUtils.isEmpty(dstIpForVideo) && dstPortForVideo > 0) {
-            paramForVideo = new HashMap<>();
-            paramForVideo.put("vhost","__defaultVhost__");
-            paramForVideo.put("app",app);
-            paramForVideo.put("stream",stream);
-            paramForVideo.put("ssrc", ssrc);
-
-            paramForVideo.put("dst_url", dstIpForVideo);
-            paramForVideo.put("dst_port", dstPortForVideo);
-            String is_Udp = isUdp ? "1" : "0";
-            paramForVideo.put("is_udp", is_Udp);
-            paramForVideo.put("src_port", sendInfo.getSendLocalPortForVideo());
-            paramForVideo.put("only_audio", "0");
-            if (ptForVideo != null) {
-                paramForVideo.put("pt", ptForVideo);
-            }
-
+            sendRtpItemForVideo = SendRtpItem.getInstance(app, stream, ssrc, dstIpForAudio, dstPortForAudio, !isUdp, sendInfo.getSendLocalPortForVideo(), ptForVideo);
         } else {
-            paramForVideo = null;
+            sendRtpItemForVideo = null;
         }
 
         Boolean streamReady = mediaServerService.isStreamReady(mediaServer, app, stream);
         if (streamReady) {
-            if (paramForVideo != null) {
-                JSONObject jsonObject = zlmServerFactory.startSendRtpStream(mediaServer, paramForVideo);
-                if (jsonObject.getInteger("code") == 0) {
-                    logger.info("[绗笁鏂规湇鍔″鎺�->鍙戦�佹祦] 瑙嗛娴佸彂娴佹垚鍔燂紝callId->{}锛宲aram->{}", callId, paramForVideo);
-                    redisTemplate.opsForValue().set(key, sendInfo);
-                }else {
-                    redisTemplate.delete(key);
-                    logger.info("[绗笁鏂规湇鍔″鎺�->鍙戦�佹祦] 瑙嗛娴佸彂娴佸け璐ワ紝callId->{}, {}", callId, jsonObject.getString("msg"));
-                    throw new ControllerException(ErrorCode.ERROR100.getCode(), "[瑙嗛娴佸彂娴佸け璐 " + jsonObject.getString("msg"));
-                }
+            if (sendRtpItemForVideo != null) {
+                mediaServerService.startSendRtp(mediaServer, null,  sendRtpItemForVideo);
+                logger.info("[绗笁鏂规湇鍔″鎺�->鍙戦�佹祦] 瑙嗛娴佸彂娴佹垚鍔燂紝callId->{}锛宲aram->{}", callId, sendRtpItemForVideo);
+                redisTemplate.opsForValue().set(key, sendInfo);
             }
-            if(paramForAudio != null) {
-                JSONObject jsonObject = zlmServerFactory.startSendRtpStream(mediaServer, paramForAudio);
-                if (jsonObject.getInteger("code") == 0) {
-                    logger.info("[绗笁鏂规湇鍔″鎺�->鍙戦�佹祦] 闊抽娴佸彂娴佹垚鍔燂紝callId->{}锛宲aram->{}", callId, paramForAudio);
-                    redisTemplate.opsForValue().set(key, sendInfo);
-                }else {
-                    redisTemplate.delete(key);
-                    logger.info("[绗笁鏂规湇鍔″鎺�->鍙戦�佹祦] 闊抽娴佸彂娴佸け璐ワ紝callId->{}, {}", callId, jsonObject.getString("msg"));
-                    throw new ControllerException(ErrorCode.ERROR100.getCode(), "[闊抽娴佸彂娴佸け璐 " + jsonObject.getString("msg"));
-                }
+            if(sendRtpItemForAudio != null) {
+                mediaServerService.startSendRtp(mediaServer, null,  sendRtpItemForAudio);
+                logger.info("[绗笁鏂规湇鍔″鎺�->鍙戦�佹祦] 闊抽娴佸彂娴佹垚鍔燂紝callId->{}锛宲aram->{}", callId, sendRtpItemForAudio);
+                redisTemplate.opsForValue().set(key, sendInfo);
             }
         }else {
             logger.info("[绗笁鏂规湇鍔″鎺�->鍙戦�佹祦] 娴佷笉瀛樺湪锛岀瓑寰呮祦涓婄嚎锛宑allId->{}", callId);
@@ -310,8 +267,8 @@
             }, 10000);
 
             // 璁㈤槄 zlm鍚姩浜嬩欢, 鏂扮殑zlm涔熶細浠庤繖閲岃繘鍏ョ郴缁�
-            OtherRtpSendInfo finalSendInfo = sendInfo;
             hookSubscribe.removeSubscribe(hook);
+            OtherRtpSendInfo finalSendInfo = sendInfo;
             hookSubscribe.addSubscribe(hook,
                     (hookData)->{
                         dynamicTask.stop(uuid);
@@ -321,27 +278,15 @@
                         } catch (InterruptedException e) {
                             throw new RuntimeException(e);
                         }
-                        if (paramForVideo != null) {
-                            JSONObject jsonObject = zlmServerFactory.startSendRtpStream(mediaServer, paramForVideo);
-                            if (jsonObject.getInteger("code") == 0) {
-                                logger.info("[绗笁鏂规湇鍔″鎺�->鍙戦�佹祦] 瑙嗛娴佸彂娴佹垚鍔燂紝callId->{}锛宲aram->{}", callId, paramForVideo);
-                                redisTemplate.opsForValue().set(key, finalSendInfo);
-                            }else {
-                                redisTemplate.delete(key);
-                                logger.info("[绗笁鏂规湇鍔″鎺�->鍙戦�佹祦] 瑙嗛娴佸彂娴佸け璐ワ紝callId->{}, {}", callId, jsonObject.getString("msg"));
-                                throw new ControllerException(ErrorCode.ERROR100.getCode(), "[瑙嗛娴佸彂娴佸け璐 " + jsonObject.getString("msg"));
-                            }
+                        if (sendRtpItemForVideo != null) {
+                            mediaServerService.startSendRtp(mediaServer, null,  sendRtpItemForVideo);
+                            logger.info("[绗笁鏂规湇鍔″鎺�->鍙戦�佹祦] 瑙嗛娴佸彂娴佹垚鍔燂紝callId->{}锛宲aram->{}", callId, sendRtpItemForVideo);
+                            redisTemplate.opsForValue().set(key, finalSendInfo);
                         }
-                        if(paramForAudio != null) {
-                            JSONObject jsonObject = zlmServerFactory.startSendRtpStream(mediaServer, paramForAudio);
-                            if (jsonObject.getInteger("code") == 0) {
-                                logger.info("[绗笁鏂规湇鍔″鎺�->鍙戦�佹祦] 闊抽娴佸彂娴佹垚鍔燂紝callId->{}锛宲aram->{}", callId, paramForAudio);
-                                redisTemplate.opsForValue().set(key, finalSendInfo);
-                            }else {
-                                redisTemplate.delete(key);
-                                logger.info("[绗笁鏂规湇鍔″鎺�->鍙戦�佹祦] 闊抽娴佸彂娴佸け璐ワ紝callId->{}, {}", callId, jsonObject.getString("msg"));
-                                throw new ControllerException(ErrorCode.ERROR100.getCode(), "[闊抽娴佸彂娴佸け璐 " + jsonObject.getString("msg"));
-                            }
+                        if(sendRtpItemForAudio != null) {
+                            mediaServerService.startSendRtp(mediaServer, null,  sendRtpItemForAudio);
+                            logger.info("[绗笁鏂规湇鍔″鎺�->鍙戦�佹祦] 闊抽娴佸彂娴佹垚鍔燂紝callId->{}锛宲aram->{}", callId, sendRtpItemForAudio);
+                            redisTemplate.opsForValue().set(key, finalSendInfo);
                         }
                         hookSubscribe.removeSubscribe(hook);
                     });
@@ -359,19 +304,9 @@
         if (sendInfo == null){
             throw new ControllerException(ErrorCode.ERROR100.getCode(), "鏈紑鍚彂娴�");
         }
-        Map<String, Object> param = new HashMap<>();
-        param.put("vhost","__defaultVhost__");
-        param.put("app",sendInfo.getPushApp());
-        param.put("stream",sendInfo.getPushStream());
-        param.put("ssrc",sendInfo.getPushSSRC());
         MediaServer mediaServerItem = mediaServerService.getDefaultMediaServer();
-        Boolean result = zlmServerFactory.stopSendRtpStream(mediaServerItem, param);
-        if (!result) {
-            logger.info("[绗笁鏂规湇鍔″鎺�->鍏抽棴鍙戦�佹祦] 澶辫触 callId->{}", callId);
-            throw new ControllerException(ErrorCode.ERROR100.getCode(), "鍋滄鍙戞祦澶辫触");
-        }else {
-            logger.info("[绗笁鏂规湇鍔″鎺�->鍏抽棴鍙戦�佹祦] 鎴愬姛 callId->{}", callId);
-        }
+        mediaServerService.stopSendRtp(mediaServerItem, sendInfo.getPushApp(), sendInfo.getPushStream(), sendInfo.getPushSSRC());
+        logger.info("[绗笁鏂规湇鍔″鎺�->鍏抽棴鍙戦�佹祦] 鎴愬姛 callId->{}", callId);
         redisTemplate.delete(key);
     }
 

--
Gitblit v1.8.0