From 983c7c351f3790d0effe2894ce435552ff0f2feb Mon Sep 17 00:00:00 2001
From: 648540858 <648540858@qq.com>
Date: 星期四, 17 八月 2023 14:20:04 +0800
Subject: [PATCH] 优化对讲释放流程

---
 src/main/java/com/genersoft/iot/vmp/service/IPlatformService.java                                                             |    7 -
 src/main/java/com/genersoft/iot/vmp/service/impl/PlatformServiceImpl.java                                                     |   21 ++++++-
 src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/ByeRequestProcessor.java                              |   99 +++++++++++++++++++--------------
 src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/notify/cmd/BroadcastNotifyMessageHandler.java |    7 --
 4 files changed, 78 insertions(+), 56 deletions(-)

diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/ByeRequestProcessor.java b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/ByeRequestProcessor.java
index 304830e..34dce99 100644
--- a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/ByeRequestProcessor.java
+++ b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/ByeRequestProcessor.java
@@ -113,6 +113,7 @@
 		CallIdHeader callIdHeader = (CallIdHeader)evt.getRequest().getHeader(CallIdHeader.NAME);
 		SendRtpItem sendRtpItem =  redisCatchStorage.querySendRTPServer(null, null, null, callIdHeader.getCallId());
 
+		// 鏀舵祦绔彂閫佺殑鍋滄
 		if (sendRtpItem != null){
 			logger.info("[鏀跺埌bye] 鏉ヨ嚜{}锛屽仠姝㈤�氶亾锛歿}, 绫诲瀷锛� {}", sendRtpItem.getPlatformId(), sendRtpItem.getChannelId(), sendRtpItem.getPlayType());
 
@@ -139,6 +140,7 @@
 					logger.info("[涓婄骇骞冲彴鍋滄瑙傜湅] 鏈壘鍒板钩鍙皗}鐨勪俊鎭紝鍙戦�乺edis娑堟伅澶辫触", sendRtpItem.getPlatformId());
 				}
 			}
+
 			AudioBroadcastCatch audioBroadcastCatch = audioBroadcastManager.get(sendRtpItem.getDeviceId(), sendRtpItem.getChannelId());
 			if (audioBroadcastCatch != null && audioBroadcastCatch.getSipTransactionInfo().getCallId().equals(callIdHeader.getCallId())) {
 				// 鏉ヨ嚜涓婄骇骞冲彴鐨勫仠姝㈠璁�
@@ -165,54 +167,67 @@
 			}
 		}
 
-			// 鍙兘鏄澶囧彂閫佺殑鍋滄
-			SsrcTransaction ssrcTransaction = streamSession.getSsrcTransaction(null, null, callIdHeader.getCallId(), null);
-			if (ssrcTransaction == null && sendRtpItem == null) {
-				logger.info("[鏀跺埌bye] 浣嗘槸鏃犳硶鑾峰彇鎺ㄦ祦淇℃伅鍜屽彂娴佷俊鎭紝蹇界暐姝よ姹�");
-				logger.info(request.toString());
-				return;
-			}
-			if (ssrcTransaction != null) {
-				logger.info("[鏀跺埌bye] 鏉ヨ嚜璁惧锛歿}, 閫氶亾宸插仠姝㈡帹娴�: {}", ssrcTransaction.getDeviceId(), ssrcTransaction.getChannelId());
+		// 鍙戞祦绔彂閫佺殑鍋滄
+		SsrcTransaction ssrcTransaction = streamSession.getSsrcTransaction(null, null, callIdHeader.getCallId(), null);
+		if (ssrcTransaction == null ) {
+			logger.info("[鏀跺埌bye] 浣嗘槸鏃犳硶鑾峰彇鎺ㄦ祦淇℃伅鍜屽彂娴佷俊鎭紝蹇界暐姝よ姹�");
+			logger.info(request.toString());
+			return;
+		}
 
-				Device device = deviceService.getDevice(ssrcTransaction.getDeviceId());
-				if (device == null) {
-					logger.info("[鏀跺埌bye] 鏈壘鍒拌澶囷細{} ", ssrcTransaction.getDeviceId());
-					return;
-				}
-				DeviceChannel channel = channelService.getOne(ssrcTransaction.getDeviceId(), ssrcTransaction.getChannelId());
+
+		ParentPlatform platform = platformService.queryPlatformByServerGBId(ssrcTransaction.getDeviceId());
+		if (platform != null ) {
+			if (ssrcTransaction.getType().equals(InviteSessionType.BROADCAST)) {
+				logger.info("[鏀跺埌bye] 涓婄骇鍋滄璇煶瀵硅锛屾潵鑷細{}, 閫氶亾宸插仠姝㈡帹娴�: {}", ssrcTransaction.getDeviceId(), ssrcTransaction.getChannelId());
+				DeviceChannel channel = storager.queryChannelInParentPlatform(ssrcTransaction.getDeviceId(), ssrcTransaction.getChannelId());
 				if (channel == null) {
 					logger.info("[鏀跺埌bye] 鏈壘鍒伴�氶亾锛岃澶囷細{}锛� 閫氶亾锛歿}", ssrcTransaction.getDeviceId(), ssrcTransaction.getChannelId());
 					return;
 				}
-				storager.stopPlay(device.getDeviceId(), channel.getChannelId());
-				InviteInfo inviteInfo = inviteStreamService.getInviteInfoByDeviceAndChannel(InviteSessionType.PLAY, device.getDeviceId(), channel.getChannelId());
-				if (inviteInfo != null) {
-					inviteStreamService.removeInviteInfo(inviteInfo);
-					if (inviteInfo.getStreamInfo() != null) {
-						mediaServerService.closeRTPServer(inviteInfo.getStreamInfo().getMediaServerId(), inviteInfo.getStreamInfo().getStream());
-					}
-				}
-				// 閲婃斁ssrc
-				MediaServerItem mediaServerItem = mediaServerService.getOne(ssrcTransaction.getMediaServerId());
-				if (mediaServerItem != null) {
-					mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcTransaction.getSsrc());
-				}
-				streamSession.remove(device.getDeviceId(), channel.getChannelId(), ssrcTransaction.getStream());
-				if (ssrcTransaction.getType() == InviteSessionType.BROADCAST) {
-					// 鏌ユ壘鏉ユ簮鐨勫璁茶澶囷紝鍙戦�佸仠姝�
-					Device sourceDevice = storager.queryVideoDeviceByPlatformIdAndChannelId(ssrcTransaction.getDeviceId(), ssrcTransaction.getChannelId());
-					if (sourceDevice != null) {
-						playService.stopAudioBroadcast(sourceDevice.getDeviceId(), channel.getChannelId());
-					}
-				}
-				AudioBroadcastCatch audioBroadcastCatch = audioBroadcastManager.get(ssrcTransaction.getDeviceId(), channel.getChannelId());
-				if (audioBroadcastCatch != null) {
-					// 鏉ヨ嚜涓婄骇骞冲彴鐨勫仠姝㈠璁�
-					logger.info("[鍋滄瀵硅] 鏉ヨ嚜涓婄骇锛屽钩鍙帮細{}, 閫氶亾锛歿}", ssrcTransaction.getDeviceId(), channel.getChannelId());
-					audioBroadcastManager.del(ssrcTransaction.getDeviceId(), channel.getChannelId());
-				}
+				String mediaServerId = ssrcTransaction.getMediaServerId();
+				platformService.stopBroadcast(platform, channel, ssrcTransaction.getStream(), false,
+						mediaServerService.getOne(mediaServerId));
 			}
 
+		}else {
+			Device device = deviceService.getDevice(ssrcTransaction.getDeviceId());
+			if (device == null) {
+				logger.info("[鏀跺埌bye] 鏈壘鍒拌澶囷細{} ", ssrcTransaction.getDeviceId());
+				return;
+			}
+			DeviceChannel channel = channelService.getOne(ssrcTransaction.getDeviceId(), ssrcTransaction.getChannelId());
+			if (channel == null) {
+				logger.info("[鏀跺埌bye] 鏈壘鍒伴�氶亾锛岃澶囷細{}锛� 閫氶亾锛歿}", ssrcTransaction.getDeviceId(), ssrcTransaction.getChannelId());
+				return;
+			}
+			storager.stopPlay(device.getDeviceId(), channel.getChannelId());
+			InviteInfo inviteInfo = inviteStreamService.getInviteInfoByDeviceAndChannel(InviteSessionType.PLAY, device.getDeviceId(), channel.getChannelId());
+			if (inviteInfo != null) {
+				inviteStreamService.removeInviteInfo(inviteInfo);
+				if (inviteInfo.getStreamInfo() != null) {
+					mediaServerService.closeRTPServer(inviteInfo.getStreamInfo().getMediaServerId(), inviteInfo.getStreamInfo().getStream());
+				}
+			}
+			// 閲婃斁ssrc
+			MediaServerItem mediaServerItem = mediaServerService.getOne(ssrcTransaction.getMediaServerId());
+			if (mediaServerItem != null) {
+				mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcTransaction.getSsrc());
+			}
+			streamSession.remove(device.getDeviceId(), channel.getChannelId(), ssrcTransaction.getStream());
+			if (ssrcTransaction.getType() == InviteSessionType.BROADCAST) {
+				// 鏌ユ壘鏉ユ簮鐨勫璁茶澶囷紝鍙戦�佸仠姝�
+				Device sourceDevice = storager.queryVideoDeviceByPlatformIdAndChannelId(ssrcTransaction.getDeviceId(), ssrcTransaction.getChannelId());
+				if (sourceDevice != null) {
+					playService.stopAudioBroadcast(sourceDevice.getDeviceId(), channel.getChannelId());
+				}
+			}
+			AudioBroadcastCatch audioBroadcastCatch = audioBroadcastManager.get(ssrcTransaction.getDeviceId(), channel.getChannelId());
+			if (audioBroadcastCatch != null) {
+				// 鏉ヨ嚜涓婄骇骞冲彴鐨勫仠姝㈠璁�
+				logger.info("[鍋滄瀵硅] 鏉ヨ嚜涓婄骇锛屽钩鍙帮細{}, 閫氶亾锛歿}", ssrcTransaction.getDeviceId(), channel.getChannelId());
+				audioBroadcastManager.del(ssrcTransaction.getDeviceId(), channel.getChannelId());
+			}
+		}
 	}
 }
diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/notify/cmd/BroadcastNotifyMessageHandler.java b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/notify/cmd/BroadcastNotifyMessageHandler.java
index 7700dd3..a05847a 100644
--- a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/notify/cmd/BroadcastNotifyMessageHandler.java
+++ b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/notify/cmd/BroadcastNotifyMessageHandler.java
@@ -137,12 +137,7 @@
                                 logger.info("[鍥芥爣绾ц仈] 璇煶鍠婅瘽 璁惧姝e湪浣跨敤涓� platform锛� {}锛� channel: {}",
                                         platform.getServerGBId(), deviceChannel.getChannelId());
                                 //  鏌ョ湅璇煶閫氶亾宸茬粡寤虹珛涓斿凡缁忓崰鐢� 鍥炲BYE
-                                try {
-                                    platformService.stopBroadcast(platform, deviceChannel.getChannelId(), streamChangedHookParam.getStream());
-                                } catch (InvalidArgumentException | ParseException | SsrcTransactionNotFoundException |
-                                         SipException e) {
-                                    logger.info("[娑堟伅鍙戦�佸け璐 鍥芥爣绾ц仈 璇煶鍠婅瘽 platform锛� {}锛� channel: {}", platform.getServerGBId(), deviceChannel.getChannelId());
-                                }
+                                platformService.stopBroadcast(platform, deviceChannel, streamChangedHookParam.getStream(),  true, mediaServerItem);
                             }else {
                                 // 鏌ョ湅璇煶閫氶亾宸茬粡寤虹珛浣嗘槸鏈崰鐢�
                                 broadcastCatch.setApp(streamChangedHookParam.getApp());
diff --git a/src/main/java/com/genersoft/iot/vmp/service/IPlatformService.java b/src/main/java/com/genersoft/iot/vmp/service/IPlatformService.java
index f9bb94b..519a7ad 100644
--- a/src/main/java/com/genersoft/iot/vmp/service/IPlatformService.java
+++ b/src/main/java/com/genersoft/iot/vmp/service/IPlatformService.java
@@ -1,6 +1,6 @@
 package com.genersoft.iot.vmp.service;
 
-import com.genersoft.iot.vmp.conf.exception.SsrcTransactionNotFoundException;
+import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel;
 import com.genersoft.iot.vmp.gb28181.bean.ParentPlatform;
 import com.genersoft.iot.vmp.gb28181.event.SipSubscribe;
 import com.genersoft.iot.vmp.media.zlm.ZlmHttpHookSubscribe;
@@ -78,9 +78,6 @@
 
     /**
      * 璇煶鍠婅瘽鍥炲BYE
-     * @param platform 骞冲彴
-     * @param channelId 閫氶亾
-     * @param stream 娴佷俊鎭�
      */
-    void stopBroadcast(ParentPlatform platform, String channelId, String stream )throws InvalidArgumentException, ParseException, SsrcTransactionNotFoundException, SipException;
+    void stopBroadcast(ParentPlatform platform, DeviceChannel channel, String stream,boolean sendBye, MediaServerItem mediaServerItem);
 }
diff --git a/src/main/java/com/genersoft/iot/vmp/service/impl/PlatformServiceImpl.java b/src/main/java/com/genersoft/iot/vmp/service/impl/PlatformServiceImpl.java
index 0e6ca5f..dbde373 100644
--- a/src/main/java/com/genersoft/iot/vmp/service/impl/PlatformServiceImpl.java
+++ b/src/main/java/com/genersoft/iot/vmp/service/impl/PlatformServiceImpl.java
@@ -422,7 +422,6 @@
         }
         InviteInfo inviteInfo = inviteStreamService.getInviteInfoByDeviceAndChannel(InviteSessionType.PLAY, platform.getServerGBId(), channelId);
 
-
         if (inviteInfo != null && inviteInfo.getStreamInfo() != null) {
             // 濡傛灉zlm涓嶅瓨鍦ㄨ繖涓祦锛屽垯鍒犻櫎鏁版嵁鍗冲彲
             MediaServerItem mediaServerItemForStreamInfo = mediaServerService.getOne(inviteInfo.getStreamInfo().getMediaServerId());
@@ -547,7 +546,23 @@
     }
 
     @Override
-    public void stopBroadcast(ParentPlatform platform, String channelId, String stream) throws InvalidArgumentException, ParseException, SsrcTransactionNotFoundException, SipException {
-        commanderForPlatform.streamByeCmd(platform, channelId, stream, null, null);
+    public void stopBroadcast(ParentPlatform platform, DeviceChannel channel, String stream, boolean sendBye, MediaServerItem mediaServerItem) {
+
+        try {
+            if (sendBye) {
+                commanderForPlatform.streamByeCmd(platform, channel.getChannelId(), stream, null, null);
+            }
+        } catch (InvalidArgumentException | SipException | ParseException | SsrcTransactionNotFoundException e) {
+            logger.warn("[娑堟伅鍙戦�佸け璐 鍋滄璇煶瀵硅锛� 骞冲彴锛歿}锛岄�氶亾锛歿}", platform.getId(), channel.getChannelId() );
+        } finally {
+            mediaServerService.closeRTPServer(mediaServerItem, stream);
+            InviteInfo inviteInfo = inviteStreamService.getInviteInfo(null, platform.getServerGBId(), channel.getChannelId(), stream);
+            if (inviteInfo != null) {
+                // 閲婃斁ssrc
+                mediaServerService.releaseSsrc(mediaServerItem.getId(), inviteInfo.getSsrcInfo().getSsrc());
+                inviteStreamService.removeInviteInfo(inviteInfo);
+            }
+            streamSession.remove(platform.getServerGBId(), channel.getChannelId(), stream);
+        }
     }
 }

--
Gitblit v1.8.0