From f8f65d473bec182abeecd6fd17a9d4c4c4cfc7c5 Mon Sep 17 00:00:00 2001
From: 648540858 <648540858@qq.com>
Date: 星期二, 10 五月 2022 13:08:32 +0800
Subject: [PATCH] 优化语音广播流程

---
 src/main/java/com/genersoft/iot/vmp/service/impl/PlayServiceImpl.java |   54 +++++++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 43 insertions(+), 11 deletions(-)

diff --git a/src/main/java/com/genersoft/iot/vmp/service/impl/PlayServiceImpl.java b/src/main/java/com/genersoft/iot/vmp/service/impl/PlayServiceImpl.java
index a647c84..934745e 100644
--- a/src/main/java/com/genersoft/iot/vmp/service/impl/PlayServiceImpl.java
+++ b/src/main/java/com/genersoft/iot/vmp/service/impl/PlayServiceImpl.java
@@ -5,6 +5,7 @@
 import com.alibaba.fastjson.JSONObject;
 import com.genersoft.iot.vmp.common.StreamInfo;
 import com.genersoft.iot.vmp.conf.DynamicTask;
+import com.genersoft.iot.vmp.conf.SipConfig;
 import com.genersoft.iot.vmp.conf.UserSetting;
 import com.genersoft.iot.vmp.gb28181.bean.*;
 import com.genersoft.iot.vmp.gb28181.event.SipSubscribe;
@@ -44,9 +45,13 @@
 import org.springframework.web.context.request.async.DeferredResult;
 
 import javax.sip.ResponseEvent;
+import javax.sip.SipException;
 import java.io.FileNotFoundException;
 import java.math.BigDecimal;
+import java.text.ParseException;
 import java.util.*;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
 
 @SuppressWarnings(value = {"rawtypes", "unchecked"})
 @Service
@@ -92,6 +97,9 @@
 
     @Autowired
     private UserSetting userSetting;
+
+    @Autowired
+    private SipConfig sipConfig;
 
     @Autowired
     private DynamicTask dynamicTask;
@@ -641,16 +649,13 @@
         }
         // 鏌ヨ閫氶亾浣跨敤鐘舵��
         if (audioBroadcastManager.exit(device.getDeviceId(), channelId)) {
-            logger.warn("璇煶骞挎挱宸茬粡寮�鍚細 {}", channelId);
-            event.call("璇煶骞挎挱宸茬粡寮�鍚�");
-            return;
+            SendRtpItem sendRtpItem =  redisCatchStorage.querySendRTPServer(device.getDeviceId(), channelId, null, null);
+            if (sendRtpItem != null && sendRtpItem.isOnlyAudio()) {
+                logger.warn("璇煶骞挎挱宸茬粡寮�鍚細 {}", channelId);
+                event.call("璇煶骞挎挱宸茬粡寮�鍚�");
+                return;
+            }
         }
-        String timeOutTaskKey = "audio-broadcast-" + device.getDeviceId() + channelId;
-        dynamicTask.startDelay(timeOutTaskKey, ()->{
-            logger.error("璇煶骞挎挱鍙戦�佽秴鏃讹細 {}:{}", device.getDeviceId(), channelId);
-            event.call("璇煶骞挎挱鍙戦�佽秴鏃�");
-            audioBroadcastManager.del(device.getDeviceId(), channelId);
-        }, timeout * 1000);
 
         // 鍙戦�侀�氱煡
         cmder.audioBroadcastCmd(device, channelId, eventResultForOk -> {
@@ -658,11 +663,38 @@
             AudioBroadcastCatch audioBroadcastCatch = new AudioBroadcastCatch(device.getDeviceId(), channelId, AudioBroadcastCatchStatus.Ready);
             audioBroadcastManager.add(audioBroadcastCatch);
         }, eventResultForError -> {
-            dynamicTask.stop(timeOutTaskKey);
             // 鍙戦�佸け璐�
             logger.error("璇煶骞挎挱鍙戦�佸け璐ワ細 {}:{}", channelId, eventResultForError.msg);
             event.call("璇煶骞挎挱鍙戦�佸け璐�");
-            audioBroadcastManager.del(device.getDeviceId(), channelId);
+            stopAudioBroadcast(device.getDeviceId(), channelId);
         });
     }
+
+    @Override
+    public void stopAudioBroadcast(String deviceId, String channelId){
+        AudioBroadcastCatch audioBroadcastCatch = audioBroadcastManager.get(deviceId, channelId);
+        if (audioBroadcastCatch != null) {
+            audioBroadcastManager.del(deviceId, audioBroadcastCatch.getChannelId());
+        }
+        try {
+            SendRtpItem sendRtpItem =  redisCatchStorage.querySendRTPServer(deviceId, channelId, null, null);
+            if (sendRtpItem != null) {
+                redisCatchStorage.deleteSendRTPServer(deviceId, sendRtpItem.getChannelId(), null, null);
+                MediaServerItem mediaInfo = mediaServerService.getOne(sendRtpItem.getMediaServerId());
+                Map<String, Object> param = new HashMap<>();
+                param.put("vhost", "__defaultVhost__");
+                param.put("app", sendRtpItem.getApp());
+                param.put("stream", sendRtpItem.getStreamId());
+                zlmresTfulUtils.stopSendRtp(mediaInfo, param);
+            }
+            if (audioBroadcastCatch.getStatus() == AudioBroadcastCatchStatus.Ok) {
+                cmder.streamByeCmd(audioBroadcastCatch.getDialog(), audioBroadcastCatch.getRequest(), null);
+            }
+        } catch (SipException e) {
+            throw new RuntimeException(e);
+        } catch (ParseException e) {
+            throw new RuntimeException(e);
+        }
+
+    }
 }

--
Gitblit v1.8.0