From 82adc0cb23f3ee47322e78889cdaba57e9309000 Mon Sep 17 00:00:00 2001
From: 648540858 <648540858@qq.com>
Date: 星期二, 21 三月 2023 15:55:24 +0800
Subject: [PATCH] 完善语音对讲级联

---
 src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMHttpHookListener.java |  177 ++++++++++++++++++++++++++++-------------------------------
 1 files changed, 84 insertions(+), 93 deletions(-)

diff --git a/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMHttpHookListener.java b/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMHttpHookListener.java
index a5f815a..984a377 100644
--- a/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMHttpHookListener.java
+++ b/src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMHttpHookListener.java
@@ -9,9 +9,9 @@
 import com.genersoft.iot.vmp.gb28181.event.EventPublisher;
 import com.genersoft.iot.vmp.gb28181.session.AudioBroadcastManager;
 import com.genersoft.iot.vmp.gb28181.session.VideoStreamSessionManager;
-import com.genersoft.iot.vmp.gb28181.transmit.cmd.ISIPCommanderForPlatform;
 import com.genersoft.iot.vmp.gb28181.transmit.callback.DeferredResultHolder;
 import com.genersoft.iot.vmp.gb28181.transmit.callback.RequestMessage;
+import com.genersoft.iot.vmp.gb28181.transmit.cmd.ISIPCommanderForPlatform;
 import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommander;
 import com.genersoft.iot.vmp.media.zlm.dto.HookType;
 import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem;
@@ -249,6 +249,7 @@
             String channelId = ssrcTransactionForAll.get(0).getChannelId();
             DeviceChannel deviceChannel = storager.queryChannel(deviceId, channelId);
             if (deviceChannel != null) {
+
                 result.setEnable_audio(deviceChannel.isHasAudio());
             }
             // 濡傛灉鏄綍鍍忎笅杞藉氨璁剧疆瑙嗛闂撮殧鍗佺
@@ -257,6 +258,11 @@
                 result.setEnable_audio(true);
                 result.setEnable_mp4(true);
             }
+            // 濡傛灉鏄痶alk瀵硅锛屽垯榛樿鑾峰彇澹伴煶
+            if (ssrcTransactionForAll.get(0).getType() == VideoStreamSessionManager.SessionType.talk) {
+                result.setEnable_audio(true);
+            }
+
         }
         return result;
     }
@@ -277,18 +283,17 @@
 		JSONObject ret = new JSONObject();
 		ret.put("code", 0);
 		ret.put("msg", "success");
-
+        MediaServerItem mediaInfo = mediaServerService.getOne(param.getMediaServerId());
         JSONObject json = (JSONObject) JSON.toJSON(param);
         taskExecutor.execute(() -> {
             ZlmHttpHookSubscribe.Event subscribe = this.subscribe.sendNotify(HookType.on_stream_changed, json);
             if (subscribe != null) {
-                MediaServerItem mediaInfo = mediaServerService.getOne(param.getMediaServerId());
+
                 if (mediaInfo != null) {
                     subscribe.response(mediaInfo, json);
                 }
             }
             // 娴佹秷澶辩Щ闄edis play
-            List<OnStreamChangedHookParam.MediaTrack> tracks = param.getTracks();
             if (param.isRegist()) {
                 if (param.getOriginType() == OriginType.RTMP_PUSH.ordinal()
                         || param.getOriginType() == OriginType.RTSP_PUSH.ordinal()
@@ -333,85 +338,60 @@
 				}
 			}else if ("broadcast".equals(param.getApp())){
 				// 璇煶瀵硅鎺ㄦ祦  stream闇�瑕佹弧瓒虫牸寮廳eviceId_channelId
-				if (param.isRegist() && param.getStream().indexOf("_") > 0) {
-					String[] streamArray = param.getStream().split("_");
-					if (streamArray.length == 2) {
-						String deviceId = streamArray[0];
-						String channelId = streamArray[1];
-						Device device = deviceService.getDevice(deviceId);
-						if (device != null) {
-							if (param.isRegist()) {
-								if (audioBroadcastManager.exit(deviceId, channelId)) {
-									// 鐩存帴鎺ㄦ祦
-									SendRtpItem sendRtpItem =  redisCatchStorage.querySendRTPServer(null, null, param.getStream(), null);
-									if (sendRtpItem == null) {
-										// TODO 鍙兘鏁版嵁閿欒锛岄噸鏂板紑鍚闊抽�氶亾
-									}else {
-										JSONObject jsonObject = zlmrtpServerFactory.startSendRtp(mediaInfo, sendRtpItem);
-										if (jsonObject != null && jsonObject.getInteger("code") == 0 ) {
-											logger.info("[璇煶鍠婅瘽] 鑷姩鎺ㄦ祦鎴愬姛, device: {}, channel: {}", device.getDeviceId(), channelId);
-										}else {
-											logger.info("[璇煶鍠婅瘽] 鎺ㄦ祦澶辫触, 缁撴灉锛� {}", jsonObject);
-										}
-
-									}
-								}else {
-									// 寮�鍚闊冲枈璇濋�氶亾
-									try {
-										playService.audioBroadcastCmd(device, channelId, mediaInfo, param.getApp(), param.getStream(),60, false, (msg)->{
-											logger.info("[璇煶鍠婅瘽] 閫氶亾寤虹珛鎴愬姛, device: {}, channel: {}", deviceId, channelId);
-										});
-									} catch (InvalidArgumentException | ParseException | SipException e) {
-										logger.error("[鍛戒护鍙戦�佸け璐 璇煶鍠婅瘽: {}", e.getMessage());
-									}
-								}
-							}else {
-								// 娴佹敞閿�
-								playService.stopAudioBroadcast(deviceId, channelId);
-							}
-						} else{
-							logger.info("[璇煶瀵硅] 鏈壘鍒拌澶囷細{}", deviceId);
-						}
-					}else {
-						logger.info("[璇煶鍠婅瘽] 鎺ㄦ祦鏍煎紡鏈夎, 鏍煎紡涓猴細 broadcast/璁惧缂栧彿_閫氶亾缂栧彿 ");
-					}
-				}
+                if (param.getStream().indexOf("_") > 0) {
+                    String[] streamArray = param.getStream().split("_");
+                    if (streamArray.length == 2) {
+                        String deviceId = streamArray[0];
+                        String channelId = streamArray[1];
+                        Device device = deviceService.getDevice(deviceId);
+                        if (device != null) {
+                            if (param.isRegist()) {
+                                if (audioBroadcastManager.exit(deviceId, channelId)) {
+                                    playService.stopAudioBroadcast(deviceId, channelId);
+                                }
+                                // 寮�鍚闊冲璁查�氶亾
+                                try {
+                                    playService.audioBroadcastCmd(device, channelId, mediaInfo, param.getApp(), param.getStream(), 60, false, (msg)->{
+                                        logger.info("[璇煶瀵硅] 閫氶亾寤虹珛鎴愬姛, device: {}, channel: {}", deviceId, channelId);
+                                    });
+                                } catch (InvalidArgumentException | ParseException | SipException e) {
+                                    logger.error("[鍛戒护鍙戦�佸け璐 璇煶瀵硅: {}", e.getMessage());
+                                }
+                            }else {
+                                // 娴佹敞閿�
+                                playService.stopAudioBroadcast(deviceId, channelId);
+                            }
+                        } else{
+                            logger.info("[璇煶瀵硅] 鏈壘鍒拌澶囷細{}", deviceId);
+                        }
+                    }
+                }
 			}else if ("talk".equals(param.getApp())){
-				// 璇煶鍠婅瘽鎺ㄦ祦  stream闇�瑕佹弧瓒虫牸寮廳eviceId_channelId
-				if (param.isRegist() && param.getStream().indexOf("_") > 0) {
-					String[] streamArray = param.getStream().split("_");
-					if (streamArray.length == 2) {
-						String deviceId = streamArray[0];
-						String channelId = streamArray[1];
-						Device device = deviceService.getDevice(deviceId);
-						if (device != null) {
-							DeviceChannel deviceChannel = storager.queryChannel(deviceId, channelId);
-							if (deviceChannel != null) {
-								if (audioBroadcastManager.exit(deviceId, channelId)) {
-									// 鐩存帴鎺ㄦ祦
-									SendRtpItem sendRtpItem =  redisCatchStorage.querySendRTPServer(null, null, param.getStream(), null);
-									if (sendRtpItem == null) {
-										// TODO 鍙兘鏁版嵁閿欒锛岄噸鏂板紑鍚闊抽�氶亾
-									}else {
-										logger.info("rtp/{}寮�濮嬪悜涓婄骇鎺ㄦ祦, 鐩爣={}:{}锛孲SRC={}", sendRtpItem.getStreamId(), sendRtpItem.getIp(), sendRtpItem.getPort(), sendRtpItem.getSsrc());
-										zlmrtpServerFactory.startSendRtp(mediaInfo, sendRtpItem);
-									}
-								}else {
-									// 寮�鍚闊冲枈璇濋�氶亾
-									MediaServerItem mediaServerItem = mediaServerService.getOne(param.getMediaServerId());
-									playService.talk(mediaServerItem, device, channelId, (mediaServer, jsonObject)->{
-										System.out.println("寮�濮嬫帹娴�");
-									}, eventResult -> {
-										System.out.println(eventResult.msg);
-									}, ()->{
-										System.out.println("瓒呮椂");
-									});
-								}
-
-							}
-						}
-					}
-				}
+				// 璇煶瀵硅鎺ㄦ祦  stream闇�瑕佹弧瓒虫牸寮廳eviceId_channelId
+                if (param.getStream().indexOf("_") > 0) {
+                    String[] streamArray = param.getStream().split("_");
+                    if (streamArray.length == 2) {
+                        String deviceId = streamArray[0];
+                        String channelId = streamArray[1];
+                        Device device = deviceService.getDevice(deviceId);
+                        if (device != null) {
+                            if (param.isRegist()) {
+                                if (audioBroadcastManager.exit(deviceId, channelId)) {
+                                    playService.stopAudioBroadcast(deviceId, channelId);
+                                }
+                                // 寮�鍚闊冲璁查�氶亾
+                                playService.talkCmd(device, channelId, mediaInfo, param.getStream(), (msg)->{
+                                    logger.info("[璇煶瀵硅] 閫氶亾寤虹珛鎴愬姛, device: {}, channel: {}", deviceId, channelId);
+                                });
+                            }else {
+                                // 娴佹敞閿�
+                                playService.stopTalk(device, channelId, param.isRegist());
+                            }
+                        } else{
+                            logger.info("[璇煶瀵硅] 鏈壘鍒拌澶囷細{}", deviceId);
+                        }
+                    }
+                }
 
 			}else{
 				if (!"rtp".equals(param.getApp())){
@@ -471,16 +451,21 @@
                                 ParentPlatform platform = storager.queryParentPlatByServerGBId(platformId);
                                 Device device = deviceService.getDevice(platformId);
 
-                                try {
+
                                     if (platform != null) {
-                                        commanderFroPlatform.streamByeCmd(platform, sendRtpItem);
+                                        try {
+                                            commanderFroPlatform.streamByeCmd(platform, sendRtpItem);
+                                        } catch (SipException | InvalidArgumentException | ParseException e) {
+                                            logger.error("[鍛戒护鍙戦�佸け璐 鍥芥爣绾ц仈 鍙戦�丅YE: {}", e.getMessage());
+                                        }
                                     } else {
-                                        cmder.streamByeCmd(device, sendRtpItem.getChannelId(), param.getStream(), sendRtpItem.getCallId());
+                                        try {
+                                            cmder.streamByeCmd(device, sendRtpItem.getChannelId(), param.getStream(), sendRtpItem.getCallId());
+                                        } catch (SipException | InvalidArgumentException | ParseException |
+                                                 SsrcTransactionNotFoundException e) {
+                                            logger.error("[鍛戒护鍙戦�佸け璐 鍙戦�丅YE: {}", e.getMessage());
+                                        }
                                     }
-                                } catch (SipException | InvalidArgumentException | ParseException |
-                                         SsrcTransactionNotFoundException e) {
-                                    logger.error("[鍛戒护鍙戦�佸け璐 鍥芥爣绾ц仈 鍙戦�丅YE: {}", e.getMessage());
-                                }
                             }
                         }
                     }
@@ -523,7 +508,7 @@
 								logger.error("[鍛戒护鍙戦�佸け璐 鍥芥爣绾ц仈 鍙戦�丅YE: {}", e.getMessage());
 							}
 							redisCatchStorage.deleteSendRTPServer(parentPlatform.getServerGBId(), sendRtpItem.getChannelId(),
-									sendRtpItem.getCallId(), sendRtpItem.getStreamId());
+									sendRtpItem.getCallId(), sendRtpItem.getStream());
 						}
 					}
 				}
@@ -552,8 +537,7 @@
                         try {
                             cmder.streamByeCmd(device, streamInfoForPlayBackCatch.getChannelId(),
                                     streamInfoForPlayBackCatch.getStream(), null);
-                        } catch (InvalidArgumentException | ParseException | SipException |
-                                 SsrcTransactionNotFoundException e) {
+                        } catch (InvalidArgumentException | ParseException | SipException | SsrcTransactionNotFoundException e) {
                             logger.error("[鏃犱汉瑙傜湅]鍥炴斁锛� 鍙戦�丅YE澶辫触 {}", e.getMessage());
                         }
                     }
@@ -569,6 +553,13 @@
                 ret.put("close", false);
                 return ret;
             }
+            SendRtpItem sendRtpItem = redisCatchStorage.querySendRTPServer(null, null, param.getStream(), null);
+            if ("talk".equals(sendRtpItem.getApp())){
+                ret.put("close", false);
+                return ret;
+            }
+        }else if ("talk".equals(param.getApp()) || "broadcast".equals(param.getApp())){
+            ret.put("close", false);
         } else {
             // 闈炲浗鏍囨祦 鎺ㄦ祦/鎷夋祦浠g悊
             // 鎷夋祦浠g悊
@@ -730,7 +721,7 @@
                         logger.error("[鍛戒护鍙戦�佸け璐 鍥芥爣绾ц仈 鍙戦�丅YE: {}", e.getMessage());
                     }
                     redisCatchStorage.deleteSendRTPServer(parentPlatform.getServerGBId(), sendRtpItem.getChannelId(),
-                            sendRtpItem.getCallId(), sendRtpItem.getStreamId());
+                            sendRtpItem.getCallId(), sendRtpItem.getStream());
                 }
             }
         });

--
Gitblit v1.8.0