From 3f43e40ed1a1e9f66b3ad2862da9dbc4b710c712 Mon Sep 17 00:00:00 2001
From: nishiwei <13126973358@163.com>
Date: Fri, 23 Oct 2026 14:09:47 +0800
Subject: [PATCH] 智云3.0机场1适配内容提交

---
 ard-sample/src/main/java/com/ard/sample/component/websocket/handler/LiveStreamHandler.java |   82 +++++++++++++++++++++++++++++-----------
 1 files changed, 59 insertions(+), 23 deletions(-)

diff --git a/ard-sample/src/main/java/com/ard/sample/component/websocket/handler/LiveStreamHandler.java b/ard-sample/src/main/java/com/ard/sample/component/websocket/handler/LiveStreamHandler.java
index 6468eaf..d8ea6e6 100644
--- a/ard-sample/src/main/java/com/ard/sample/component/websocket/handler/LiveStreamHandler.java
+++ b/ard-sample/src/main/java/com/ard/sample/component/websocket/handler/LiveStreamHandler.java
@@ -31,16 +31,22 @@
     /**
      * 开启直播
      */
-    public void StartLiveStreaming(String sn) {
+    public void StartLiveStreaming(String sn ,String userId) {
         //调用开始直播
         ResponseResult rlive = null;
         String resultLive = "";
         try {
             // 获取直播参数
             LiveTypeDTO param = getLiveParam(sn);
+            // 判断param是否为空,不为空则返回正确的直播参数,为null则未获取直播参数,不为空则继续操作
+            if (param == null) {
+                log.info("SN: {}-未获取到直播参数param为null,无法开启直播!", sn);
+                return;
+            }
             // 如果存在直播则读取直播地址返回
             if (RedisOpsUtils.checkExist(RedisConst.WEBSOCKET_VIDEO_ADDRESS_PREFIX + sn)) {
                 resultLive = String.valueOf(RedisOpsUtils.get(RedisConst.WEBSOCKET_VIDEO_ADDRESS_PREFIX + sn));
+                log.info("SN: {}-redis中存在相机的播放地址信息,直接返回地址!", sn);
             } else {
                 log.info("SN: {}-redis中未找到相机的播放地址信息!停止直播后再开启直播", sn);
                 StopLiveStreaming(sn);
@@ -53,35 +59,41 @@
                     rlive.setType(MessageType.LIVESTREAM.getType());
                     resultLive = _objectMapper.writeValueAsString(rlive);
                     RedisOpsUtils.set(RedisConst.WEBSOCKET_VIDEO_ADDRESS_PREFIX + sn, resultLive);
+                    LiveDTO live = (LiveDTO) rlive.getData();
+                    Boolean isWeb = _deviceService.updateWebrtcUrl(sn,live.getWebrtcUrl());
+                    if (isWeb) {
+                        log.info("SN: {}-更新webrtc地址成功!", sn);
+                    }
+                }
+                else {
+                    log.info("SN: {}-开启直播失败!返回值:{}", sn, rlive);
                 }
             }
+            _dockWebSocketHandler.sendMessageToSubscribers(sn, MessageType.LIVESTREAM.getType(), resultLive, userId);
+            log.info("SN: {}-直播地址:{}", sn, resultLive);
         } catch (Exception e) {
             log.info("SN: {}-开启直播失败!原因:{}", sn, e.getMessage());
-            String jsonString = getResult(sn, "开启直播失败!原因:" + e.getMessage());
-            _dockWebSocketHandler.sendMessageToSubscribers(sn, MessageType.LIVESTREAM.getType(), jsonString);
             e.printStackTrace();
         }
-
-        _dockWebSocketHandler.sendMessageToSubscribers(sn, MessageType.LIVESTREAM.getType(), resultLive);
-        log.info("SN: {}-直播地址:{}", sn, resultLive);
     }
 
     public void StopLiveStreaming(String sn) {
         ResponseResult rlive = null;
-//        String resultLive = "";
         try {
             // 获取直播参数
             LiveTypeDTO param = getLiveParam(sn);
             // 停止直播逻辑
             rlive = _liveStreamService.liveStop(param.getVideoId());
-//            rlive.setGateway(sn);
-//            rlive.setType(MessageType.LIVESTREAM.getType());
-//            resultLive = _objectMapper.writeValueAsString(rlive);
-            RedisOpsUtils.del(RedisConst.WEBSOCKET_VIDEO_ADDRESS_PREFIX + sn);
+            log.info("SN: {}-停止直播!返回值:{}", sn, rlive);
         } catch (Exception e) {
-            e.printStackTrace();
+            log.info("SN: {}-停止直播时发生异常,原因:{}", sn, e.getMessage(), e);
+        } finally {
+            // 无论停流是否成功都清理缓存地址:
+            // 设备离线等原因导致停流异常时,若不清 Redis,后续订阅会直接拿到失效地址,
+            // 页面持续黑屏,只有等设备离线清理缓存后才能恢复
+            RedisOpsUtils.del(RedisConst.WEBSOCKET_VIDEO_ADDRESS_PREFIX + sn);
+            log.info("SN: {}-已删除redis直播信息!", sn);
         }
-        //_dockWebSocketHandler.sendMessageToSubscribers(sn, MessageType.LIVESTREAM.getType(), resultLive);
     }
 
     public String getResult(String sn, String msg) {
@@ -117,28 +129,52 @@
         List<CapacityDeviceDTO> devicesList = _liveStreamService.getLiveCapacity();
         if (devicesList == null || devicesList.isEmpty()) {
             log.info("SN: {}-未获得设备直播能力!", sn);
+            return null;
             // 抛弃出异常,给调用者处理
-            throw new Exception("未获得设备直播能力!");
+            // throw new Exception("未获得设备直播能力!");
         }
         for (CapacityDeviceDTO device : devicesList) {
             if (!device.getSn().equals(sn)) {
                 continue;
             }
             List<CapacityCameraDTO> camerasList = device.getCamerasList();
+            if (camerasList == null || camerasList.isEmpty()) {
+                continue;
+            }
             for (CapacityCameraDTO camera : camerasList) {
-                if (camera.getName() == null || camera.getName().isEmpty()) {
+                List<CapacityVideoDTO> videosList = camera.getVideosList();
+                if (videosList == null || videosList.isEmpty()) {
                     continue;
                 }
-                String videoid = device.getSn() + "/" + camera.getIndex();
-                List<CapacityVideoDTO> videosList = camera.getVideosList();
+                // 机场1:M30 / M30T 相机索引为 52-x-x / 53-x-x,live_capacity 可能同时上报
+                // normal-0、zoom-0、ir-0 多条码流。默认优先选 normal 码流(可见光),
+                // 避免原来“循环取最后一条”时默认落到红外。其他机型保持原行为(取最后一条)。
+                boolean m30SeriesCamera = camera.getIndex() != null
+                        && (camera.getIndex().startsWith("52-") || camera.getIndex().startsWith("53-"));
+                CapacityVideoDTO selectedVideo = null;
                 for (CapacityVideoDTO video : videosList) {
-                    videoid = videoid + "/" + video.getIndex();
-                    param.setUrlType(1);
-                    param.setUrl("");
-                    param.setVideoId(videoid);
-                    param.setVideoQuality(1);
-                    param.setVideoType(video.getSwitchVideoTypes().get(0));
+                    selectedVideo = video;
+                    if (m30SeriesCamera && video.getIndex() != null
+                            && video.getIndex().startsWith("normal")) {
+                        break;
+                    }
                 }
+                if (selectedVideo == null) {
+                    continue;
+                }
+                List<String> switchVideoTypes = selectedVideo.getSwitchVideoTypes();
+                if (switchVideoTypes == null || switchVideoTypes.isEmpty()) {
+                    // 没有镜头类型信息时跳过该相机,避免 get(0) 越界
+                    continue;
+                }
+                // 机场1:video_id 格式固定为 {sn}/{camera_index}/{video_index}。
+                // 按选中的码流独立构造,单码流设备结果与原来完全一致。
+                String videoid = device.getSn() + "/" + camera.getIndex() + "/" + selectedVideo.getIndex();
+                param.setUrlType(1);
+                param.setUrl("");
+                param.setVideoId(videoid);
+                param.setVideoQuality(1);
+                param.setVideoType(switchVideoTypes.get(0));
             }
         }
         return param;

--
Gitblit v1.9.3