From 26bdf2e7768ee5dfc400c3970a5aa129fed49453 Mon Sep 17 00:00:00 2001
From: 648540858 <648540858@qq.com>
Date: 星期一, 20 三月 2023 15:31:43 +0800
Subject: [PATCH] Merge branch '级联' into main-dev

---
 src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/notify/cmd/BroadcastNotifyMessageHandler.java |  200 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 200 insertions(+), 0 deletions(-)

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
new file mode 100644
index 0000000..240f24f
--- /dev/null
+++ b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/event/request/impl/message/notify/cmd/BroadcastNotifyMessageHandler.java
@@ -0,0 +1,200 @@
+package com.genersoft.iot.vmp.gb28181.transmit.event.request.impl.message.notify.cmd;
+
+import com.alibaba.fastjson2.JSONObject;
+import com.genersoft.iot.vmp.conf.exception.SsrcTransactionNotFoundException;
+import com.genersoft.iot.vmp.gb28181.bean.*;
+import com.genersoft.iot.vmp.gb28181.session.AudioBroadcastManager;
+import com.genersoft.iot.vmp.gb28181.transmit.cmd.ISIPCommanderForPlatform;
+import com.genersoft.iot.vmp.gb28181.transmit.event.request.SIPRequestProcessorParent;
+import com.genersoft.iot.vmp.gb28181.transmit.event.request.impl.message.IMessageHandler;
+import com.genersoft.iot.vmp.gb28181.transmit.event.request.impl.message.notify.NotifyMessageHandler;
+import com.genersoft.iot.vmp.media.zlm.ZLMRTPServerFactory;
+import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem;
+import com.genersoft.iot.vmp.service.IDeviceService;
+import com.genersoft.iot.vmp.service.IMediaServerService;
+import com.genersoft.iot.vmp.service.IPlatformService;
+import com.genersoft.iot.vmp.service.IPlayService;
+import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
+import com.genersoft.iot.vmp.storager.IVideoManagerStorage;
+import gov.nist.javax.sip.message.SIPRequest;
+import org.dom4j.Element;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.InitializingBean;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import javax.sip.InvalidArgumentException;
+import javax.sip.RequestEvent;
+import javax.sip.SipException;
+import javax.sip.message.Response;
+import java.text.ParseException;
+
+/**
+ * 鐘舵�佷俊鎭�(蹇冭烦)鎶ラ��
+ */
+@Component
+public class BroadcastNotifyMessageHandler extends SIPRequestProcessorParent implements InitializingBean, IMessageHandler {
+
+    private Logger logger = LoggerFactory.getLogger(BroadcastNotifyMessageHandler.class);
+    private final static String cmdType = "Broadcast";
+
+    @Autowired
+    private NotifyMessageHandler notifyMessageHandler;
+
+    @Autowired
+    private IVideoManagerStorage storage;
+
+    @Autowired
+    private ISIPCommanderForPlatform commanderForPlatform;
+
+    @Autowired
+    private IMediaServerService mediaServerService;
+
+    @Autowired
+    private IPlayService playService;
+
+    @Autowired
+    private IDeviceService deviceService;
+
+    @Autowired
+    private IPlatformService platformService;
+
+    @Autowired
+    private AudioBroadcastManager audioBroadcastManager;
+
+    @Autowired
+    private ZLMRTPServerFactory zlmrtpServerFactory;
+
+    @Autowired
+    private IRedisCatchStorage redisCatchStorage;
+
+    @Override
+    public void afterPropertiesSet() throws Exception {
+        notifyMessageHandler.addHandler(cmdType, this);
+    }
+
+    @Override
+    public void handForDevice(RequestEvent evt, Device device, Element element) {
+
+    }
+
+    @Override
+    public void handForPlatform(RequestEvent evt, ParentPlatform platform, Element rootElement) {
+        // 鏉ヨ嚜涓婄骇骞冲彴鐨勮闊冲枈璇濊姹�
+        SIPRequest request = (SIPRequest) evt.getRequest();
+        try {
+            Element snElement = rootElement.element("SN");
+            if (snElement == null) {
+                responseAck(request, Response.BAD_REQUEST, "sn must not null");
+                return;
+            }
+            String sn = snElement.getText();
+            Element targetIDElement = rootElement.element("TargetID");
+            if (targetIDElement == null) {
+                responseAck(request, Response.BAD_REQUEST, "TargetID must not null");
+                return;
+            }
+            String targetId = targetIDElement.getText();
+
+
+            logger.info("[鍥芥爣绾ц仈 璇煶鍠婅瘽] platform: {}, channel: {}", platform.getServerGBId(), targetId);
+
+            DeviceChannel deviceChannel = storage.queryChannelInParentPlatform(platform.getServerGBId(), targetId);
+            if (deviceChannel == null) {
+                responseAck(request, Response.NOT_FOUND, "TargetID not found");
+                return;
+            }
+            // 鍚戜笅绾у彂閫佽闊崇殑鍠婅瘽璇锋眰
+            Device device = deviceService.getDevice(deviceChannel.getDeviceId());
+            if (device == null) {
+                responseAck(request, Response.NOT_FOUND, "device not found");
+                return;
+            }
+            responseAck(request, Response.OK);
+
+            // 鏌ョ湅璇煶閫氶亾鏄惁宸茬粡寤虹珛骞朵笖宸茬粡鍦ㄤ娇鐢�
+            if (playService.audioBroadcastInUse(device, targetId)) {
+                commanderForPlatform.broadcastResultCmd(platform, deviceChannel, sn, false,null, null);
+                return;
+            }
+
+            MediaServerItem mediaServerForMinimumLoad = mediaServerService.getMediaServerForMinimumLoad(null);
+            commanderForPlatform.broadcastResultCmd(platform, deviceChannel, sn, true,  eventResult->{
+                logger.info("[鍥芥爣绾ц仈] 璇煶鍠婅瘽 鍥炲澶辫触 platform锛� {}锛� 閿欒锛歿}/{}", platform.getServerGBId(), eventResult.statusCode, eventResult.msg);
+            }, eventResult->{
+                // 娑堟伅鍙戦�佹垚鍔燂紝 鍚戜笂绾у彂閫乮nvite锛岃幏鍙栨帹娴�
+                try {
+                    platformService.broadcastInvite(platform, deviceChannel.getChannelId(), mediaServerForMinimumLoad,  (mediaServerItem, response)->{
+                        // 涓婄骇骞冲彴鎺ㄦ祦鎴愬姛
+                        String app = response.getString("app");
+                        String stream = response.getString("stream");
+                        AudioBroadcastCatch broadcastCatch = audioBroadcastManager.get(device.getDeviceId(), targetId);
+                        if (broadcastCatch != null ) {
+                            if (playService.audioBroadcastInUse(device, targetId)) {
+                                logger.info("[鍥芥爣绾ц仈] 璇煶鍠婅瘽 璁惧姝f鍦ㄤ娇鐢ㄤ腑 platform锛� {}锛� channel: {}",
+                                        platform.getServerGBId(), deviceChannel.getChannelId());
+                                //  鏌ョ湅璇煶閫氶亾宸茬粡寤虹珛涓斿凡缁忓崰鐢� 鍥炲BYE
+                                try {
+                                    platformService.stopBroadcast(platform, deviceChannel.getChannelId(), stream);
+                                } catch (InvalidArgumentException | ParseException | SsrcTransactionNotFoundException |
+                                         SipException e) {
+                                    logger.info("[娑堟伅鍙戦�佸け璐 鍥芥爣绾ц仈 璇煶鍠婅瘽 platform锛� {}锛� channel: {}", platform.getServerGBId(), deviceChannel.getChannelId());
+                                }
+                            }else {
+                                // 鏌ョ湅璇煶閫氶亾宸茬粡寤虹珛浣嗘槸鏈崰鐢�
+                                broadcastCatch.setApp(app);
+                                broadcastCatch.setStream(stream);
+                                broadcastCatch.setMediaServerItem(mediaServerItem);
+                                audioBroadcastManager.update(broadcastCatch);
+                                // 鎺ㄦ祦鍒拌澶�
+                                SendRtpItem sendRtpItem = redisCatchStorage.querySendRTPServer(null, targetId, stream, null);
+                                if (sendRtpItem == null) {
+                                    logger.warn("[鍥芥爣绾ц仈] 璇煶鍠婅瘽 寮傚父锛屾湭鎵惧埌鍙戞祦淇℃伅锛� channelId: {}, stream: {}", targetId, stream);
+                                    logger.info("[鍥芥爣绾ц仈] 璇煶鍠婅瘽 閲嶆柊寮�濮嬶紝channelId: {}, stream: {}", targetId, stream);
+                                    try {
+                                        playService.audioBroadcastCmd(device, targetId, mediaServerItem, app, stream, 60, true, msg -> {
+                                            logger.info("[璇煶鍠婅瘽] 閫氶亾寤虹珛鎴愬姛, device: {}, channel: {}", device.getDeviceId(), targetId);
+                                        });
+                                    } catch (SipException | InvalidArgumentException | ParseException e) {
+                                        logger.info("[娑堟伅鍙戦�佸け璐 鍥芥爣绾ц仈 璇煶鍠婅瘽 platform锛� {}", platform.getServerGBId());
+                                    }
+                                }else {
+                                    // 鍙戞祦
+                                    JSONObject jsonObject = zlmrtpServerFactory.startSendRtp(mediaServerItem, sendRtpItem);
+                                    if (jsonObject != null && jsonObject.getInteger("code") == 0 ) {
+                                        logger.info("[璇煶鍠婅瘽] 鑷姩鎺ㄦ祦鎴愬姛, device: {}, channel: {}", device.getDeviceId(), targetId);
+                                    }else {
+                                        logger.info("[璇煶鍠婅瘽] 鎺ㄦ祦澶辫触, 缁撴灉锛� {}", jsonObject);
+                                    }
+                                }
+                            }
+                        }else {
+                            try {
+                                playService.audioBroadcastCmd(device, targetId, mediaServerItem, app, stream, 60, true, msg -> {
+                                    logger.info("[璇煶鍠婅瘽] 閫氶亾寤虹珛鎴愬姛, device: {}, channel: {}", device.getDeviceId(), targetId);
+                                });
+                            } catch (SipException | InvalidArgumentException | ParseException e) {
+                                logger.info("[娑堟伅鍙戦�佸け璐 鍥芥爣绾ц仈 璇煶鍠婅瘽 platform锛� {}", platform.getServerGBId());
+                            }
+                        }
+
+                    }, eventResultForBroadcastInvite -> {
+                        // 鏀跺埌閿欒
+                        logger.info("[鍥芥爣绾ц仈-璇煶鍠婅瘽] 涓庝笅绾ч�氶亾寤虹珛澶辫触 device: {}, channel: {}锛� 閿欒锛歿}/{}", device.getDeviceId(),
+                                targetId, eventResultForBroadcastInvite.statusCode, eventResultForBroadcastInvite.msg);
+                    }, (code, msg)->{
+                        // 瓒呮椂
+                        logger.info("[鍥芥爣绾ц仈-璇煶鍠婅瘽] 涓庝笅绾ч�氶亾寤虹珛瓒呮椂 device: {}, channel: {}锛� 閿欒锛歿}/{}", device.getDeviceId(),
+                                targetId, code, msg);
+                    });
+                } catch (SipException | InvalidArgumentException | ParseException e) {
+                    logger.info("[娑堟伅鍙戦�佸け璐 鍥芥爣绾ц仈 璇煶鍠婅瘽 invite娑堟伅 platform锛� {}", platform.getServerGBId());
+                }
+            });
+        } catch (SipException | InvalidArgumentException | ParseException e) {
+            logger.info("[娑堟伅鍙戦�佸け璐 鍥芥爣绾ц仈 璇煶鍠婅瘽 platform锛� {}", platform.getServerGBId());
+        }
+
+    }
+}

--
Gitblit v1.8.0