From ad36354ef46a31f24b2583263f575d6736c0ad28 Mon Sep 17 00:00:00 2001
From: 648540858 <648540858@qq.com>
Date: 星期二, 05 十二月 2023 11:29:05 +0800
Subject: [PATCH] 优化国标录像服务端,使用zlm新接口实现功能

---
 src/main/java/com/genersoft/iot/vmp/service/impl/PlayServiceImpl.java |  209 ++++++++++++++++++++++++++++++++++++----------------
 1 files changed, 144 insertions(+), 65 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 0d1bad6..1e8dee7 100755
--- a/src/main/java/com/genersoft/iot/vmp/service/impl/PlayServiceImpl.java
+++ b/src/main/java/com/genersoft/iot/vmp/service/impl/PlayServiceImpl.java
@@ -1,5 +1,7 @@
 package com.genersoft.iot.vmp.service.impl;
 
+import com.alibaba.fastjson2.JSON;
+import com.alibaba.fastjson2.JSONArray;
 import com.alibaba.fastjson2.JSONObject;
 import com.genersoft.iot.vmp.common.InviteInfo;
 import com.genersoft.iot.vmp.common.InviteSessionStatus;
@@ -21,16 +23,12 @@
 import com.genersoft.iot.vmp.media.zlm.ZLMRESTfulUtils;
 import com.genersoft.iot.vmp.media.zlm.ZLMServerFactory;
 import com.genersoft.iot.vmp.media.zlm.ZlmHttpHookSubscribe;
-import com.genersoft.iot.vmp.media.zlm.dto.HookSubscribeFactory;
-import com.genersoft.iot.vmp.media.zlm.dto.HookSubscribeForStreamChange;
-import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem;
+import com.genersoft.iot.vmp.media.zlm.dto.*;
 import com.genersoft.iot.vmp.media.zlm.dto.hook.HookParam;
+import com.genersoft.iot.vmp.media.zlm.dto.hook.OnRecordMp4HookParam;
 import com.genersoft.iot.vmp.media.zlm.dto.hook.OnStreamChangedHookParam;
 import com.genersoft.iot.vmp.service.*;
-import com.genersoft.iot.vmp.service.bean.CloudRecordItem;
-import com.genersoft.iot.vmp.service.bean.ErrorCallback;
-import com.genersoft.iot.vmp.service.bean.InviteErrorCode;
-import com.genersoft.iot.vmp.service.bean.SSRCInfo;
+import com.genersoft.iot.vmp.service.bean.*;
 import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
 import com.genersoft.iot.vmp.storager.IVideoManagerStorage;
 import com.genersoft.iot.vmp.storager.dao.CloudRecordServiceMapper;
@@ -77,16 +75,13 @@
     private IInviteStreamService inviteStreamService;
 
     @Autowired
-    private DeferredResultHolder resultHolder;
+    private ZlmHttpHookSubscribe subscribe;
 
     @Autowired
     private ZLMRESTfulUtils zlmresTfulUtils;
 
     @Autowired
     private ZLMServerFactory zlmServerFactory;
-
-    @Autowired
-    private AssistRESTfulUtils assistRESTfulUtils;
 
     @Autowired
     private IMediaService mediaService;
@@ -105,9 +100,6 @@
 
     @Autowired
     private DynamicTask dynamicTask;
-
-    @Autowired
-    private ZlmHttpHookSubscribe subscribe;
 
     @Autowired
     private CloudRecordServiceMapper cloudRecordServiceMapper;
@@ -741,60 +733,147 @@
     @Override
     public StreamInfo getDownLoadInfo(String deviceId, String channelId, String stream) {
         InviteInfo inviteInfo = inviteStreamService.getInviteInfo(InviteSessionType.DOWNLOAD, deviceId, channelId, stream);
+        if (inviteInfo == null || inviteInfo.getStreamInfo() == null) {
+            logger.warn("[鑾峰彇涓嬭浇杩涘害] 鏈煡璇㈠埌褰曞儚涓嬭浇鐨勪俊鎭�");
+            return null;
+        }
 
-        if (inviteInfo != null && inviteInfo.getStreamInfo() != null) {
-            if (inviteInfo.getStreamInfo().getProgress() == 1) {
-                return inviteInfo.getStreamInfo();
-            }
-
-            // 鑾峰彇褰撳墠宸蹭笅杞芥椂闀�
-            String mediaServerId = inviteInfo.getStreamInfo().getMediaServerId();
-            MediaServerItem mediaServerItem = mediaServerService.getOne(mediaServerId);
-            if (mediaServerItem == null) {
-                logger.warn("鏌ヨ褰曞儚淇℃伅鏃跺彂鐜拌妭鐐瑰凡绂荤嚎");
-                return null;
-            }
-            if (mediaServerItem.getRecordAssistPort() == 0) {
-                throw new ControllerException(ErrorCode.ERROR100.getCode(), "鏈厤缃瓵ssist鏈嶅姟锛屾棤娉曞畬鎴愬綍鍍忎笅杞�");
-            }
-            SsrcTransaction ssrcTransaction = streamSession.getSsrcTransaction(deviceId, channelId, null, stream);
-
-            if (ssrcTransaction == null) {
-                logger.warn("[鑾峰彇涓嬭浇杩涘害]锛屾湭鎵惧埌涓嬭浇浜嬪姟淇℃伅");
-                return null;
-            }
-
-            // 涓轰簡鏀寔澶氫釜鏁版嵁搴擄紝杩欓噷涓嶈兘浣跨敤姹傚拰鍑芥暟鏉ョ洿鎺ヨ幏鍙栨�绘暟浜�
-            List<CloudRecordItem> cloudRecordItemList = cloudRecordServiceMapper.getList(null, "rtp", inviteInfo.getStream(), null, null, ssrcTransaction.getCallId(), null);
-
-            if (cloudRecordItemList.isEmpty()) {
-                logger.warn("[鑾峰彇涓嬭浇杩涘害]锛屾湭鎵惧埌涓嬭浇瑙嗛淇℃伅");
-                return null;
-            }
-            long duration = 0;
-            for (CloudRecordItem cloudRecordItem : cloudRecordItemList) {
-                duration += cloudRecordItem.getTimeLen();
-            }
-            if (duration == 0) {
-                inviteInfo.getStreamInfo().setProgress(0);
-            } else {
-                String startTime = inviteInfo.getStreamInfo().getStartTime();
-                String endTime = inviteInfo.getStreamInfo().getEndTime();
-                // 姝ゆ椂start鍜宔nd鍗曚綅鏄
-                long start = DateUtil.yyyy_MM_dd_HH_mm_ssToTimestamp(startTime);
-                long end = DateUtil.yyyy_MM_dd_HH_mm_ssToTimestamp(endTime);
-
-                BigDecimal currentCount = new BigDecimal(duration);
-                BigDecimal totalCount = new BigDecimal((end - start) * 1000);
-                BigDecimal divide = currentCount.divide(totalCount, 2, RoundingMode.HALF_UP);
-                double process = divide.doubleValue();
-                inviteInfo.getStreamInfo().setProgress(process);
-            }
-            inviteStreamService.updateInviteInfo(inviteInfo);
-
+        if (inviteInfo.getStreamInfo().getProgress() == 1) {
             return inviteInfo.getStreamInfo();
         }
-        return null;
+
+        // 鑾峰彇褰撳墠宸蹭笅杞芥椂闀�
+        String mediaServerId = inviteInfo.getStreamInfo().getMediaServerId();
+        MediaServerItem mediaServerItem = mediaServerService.getOne(mediaServerId);
+        if (mediaServerItem == null) {
+            logger.warn("[鑾峰彇涓嬭浇杩涘害] 鏌ヨ褰曞儚淇℃伅鏃跺彂鐜拌妭鐐逛笉瀛樺湪");
+            return null;
+        }
+        SsrcTransaction ssrcTransaction = streamSession.getSsrcTransaction(deviceId, channelId, null, stream);
+
+        if (ssrcTransaction == null) {
+            logger.warn("[鑾峰彇涓嬭浇杩涘害] 涓嬭浇宸茬粨鏉�");
+            return null;
+        }
+
+        JSONObject mediaListJson= zlmresTfulUtils.getMediaList(mediaServerItem, "rtp", stream);
+        if (mediaListJson == null) {
+            logger.warn("[鑾峰彇涓嬭浇杩涘害] 浠巣lm鏌ヨ杩涘害澶辫触");
+            return null;
+        }
+        if (mediaListJson.getInteger("code") != 0) {
+            logger.warn("[鑾峰彇涓嬭浇杩涘害] 浠巣lm鏌ヨ杩涘害鍑虹幇閿欒锛� {}", mediaListJson.getString("msg"));
+            return null;
+        }
+        JSONArray data = mediaListJson.getJSONArray("data");
+        if (data == null) {
+            logger.warn("[鑾峰彇涓嬭浇杩涘害] 浠巣lm鏌ヨ杩涘害鏃舵湭杩斿洖鏁版嵁");
+            return null;
+        }
+        JSONObject mediaJSON = data.getJSONObject(0);
+        JSONArray tracks = mediaJSON.getJSONArray("tracks");
+        if (tracks.isEmpty()) {
+            logger.warn("[鑾峰彇涓嬭浇杩涘害] 浠巣lm鏌ヨ杩涘害鏃舵湭杩斿洖鏁版嵁");
+            return null;
+        }
+        JSONObject jsonObject = tracks.getJSONObject(0);
+        long duration = jsonObject.getLongValue("duration");
+        if (duration == 0) {
+            inviteInfo.getStreamInfo().setProgress(0);
+        } else {
+            String startTime = inviteInfo.getStreamInfo().getStartTime();
+            String endTime = inviteInfo.getStreamInfo().getEndTime();
+            // 姝ゆ椂start鍜宔nd鍗曚綅鏄
+            long start = DateUtil.yyyy_MM_dd_HH_mm_ssToTimestamp(startTime);
+            long end = DateUtil.yyyy_MM_dd_HH_mm_ssToTimestamp(endTime);
+
+            BigDecimal currentCount = new BigDecimal(duration);
+            BigDecimal totalCount = new BigDecimal((end - start) * 1000);
+            BigDecimal divide = currentCount.divide(totalCount, 2, RoundingMode.HALF_UP);
+            double process = divide.doubleValue();
+            inviteInfo.getStreamInfo().setProgress(process);
+        }
+        inviteStreamService.updateInviteInfo(inviteInfo);
+        return inviteInfo.getStreamInfo();
+    }
+
+    @Override
+    public void getFilePath(String deviceId, String channelId, String stream, ErrorCallback<DownloadFileInfo> callback) {
+        InviteInfo inviteInfo = inviteStreamService.getInviteInfo(InviteSessionType.DOWNLOAD, deviceId, channelId, stream);
+        if (inviteInfo == null || inviteInfo.getStreamInfo() == null) {
+            logger.warn("[鑾峰彇褰曞儚涓嬭浇鏂囦欢鍦板潃] 鏈煡璇㈠埌褰曞儚涓嬭浇鐨勪俊鎭紝 {}/{}-{}", deviceId, channelId, stream);
+            callback.run(ErrorCode.ERROR100.getCode(), "鏈煡璇㈠埌褰曞儚涓嬭浇鐨勪俊鎭�", null);
+            return ;
+        }
+
+        if (!ObjectUtils.isEmpty(inviteInfo.getStreamInfo().getDownLoadFilePath())) {
+            callback.run(ErrorCode.SUCCESS.getCode(), ErrorCode.SUCCESS.getMsg(),
+                    inviteInfo.getStreamInfo().getDownLoadFilePath());
+            return;
+        }
+
+        StreamAuthorityInfo streamAuthorityInfo = redisCatchStorage.getStreamAuthorityInfo("rtp", stream);
+        if (streamAuthorityInfo == null) {
+            logger.warn("[鑾峰彇褰曞儚涓嬭浇鏂囦欢鍦板潃] 鏈煡璇㈠埌褰曞儚鐨勮棰戜俊鎭紝 {}/{}-{}", deviceId, channelId, stream);
+            callback.run(ErrorCode.ERROR100.getCode(), "鏈煡璇㈠埌褰曞儚鐨勮棰戜俊鎭�", null);
+            return ;
+        }
+
+        // 鑾峰彇褰撳墠宸蹭笅杞芥椂闀�
+        String mediaServerId = inviteInfo.getStreamInfo().getMediaServerId();
+        MediaServerItem mediaServerItem = mediaServerService.getOne(mediaServerId);
+        if (mediaServerItem == null) {
+            logger.warn("[鑾峰彇褰曞儚涓嬭浇鏂囦欢鍦板潃] 鏌ヨ褰曞儚淇℃伅鏃跺彂鐜拌妭鐐逛笉瀛樺湪锛� {}/{}-{}", deviceId, channelId, stream);
+            callback.run(ErrorCode.ERROR100.getCode(), "鏌ヨ褰曞儚淇℃伅鏃跺彂鐜拌妭鐐逛笉瀛樺湪", null);
+            return ;
+        }
+
+        List<CloudRecordItem> cloudRecordItemList =  cloudRecordServiceMapper.getListByCallId(streamAuthorityInfo.getCallId());
+        if (!cloudRecordItemList.isEmpty()) {
+            String filePath = cloudRecordItemList.get(0).getFilePath();
+
+            DownloadFileInfo downloadFileInfo = getDownloadFilePath(mediaServerItem, filePath);
+            inviteInfo.getStreamInfo().setDownLoadFilePath(downloadFileInfo);
+            inviteStreamService.updateInviteInfo(inviteInfo);
+            callback.run(ErrorCode.SUCCESS.getCode(), ErrorCode.SUCCESS.getMsg(), downloadFileInfo);
+        }else {
+            // 鍙兘灏氭湭鐢熸垚锛岄偅灏辩洃鍚琱ook绛夌潃鏀跺埌瀵瑰簲鐨勫綍鍍忛�氱煡
+            ZlmHttpHookSubscribe.Event hookEvent = (mediaServerItemInuse, hookParam) -> {
+                logger.info("[褰曞儚涓嬭浇]鏀跺埌璁㈤槄娑堟伅锛� 锛� {}/{}-{}", deviceId, channelId, stream);
+                logger.info("[褰曞儚涓嬭浇]鏀跺埌璁㈤槄娑堟伅鍐呭锛� " + hookParam);
+                dynamicTask.stop(streamAuthorityInfo.getCallId());
+                OnRecordMp4HookParam recordMp4HookParam = (OnRecordMp4HookParam)hookParam;
+                String filePath = recordMp4HookParam.getFile_path();
+                DownloadFileInfo downloadFileInfo = getDownloadFilePath(mediaServerItem, filePath);
+                inviteInfo.getStreamInfo().setDownLoadFilePath(downloadFileInfo);
+                inviteStreamService.updateInviteInfo(inviteInfo);
+                callback.run(ErrorCode.SUCCESS.getCode(), ErrorCode.SUCCESS.getMsg(), downloadFileInfo);
+            };
+            HookSubscribeForRecordMp4 hookSubscribe = HookSubscribeFactory.on_record_mp4(mediaServerId, "rtp", stream);
+            subscribe.addSubscribe(hookSubscribe, hookEvent);
+
+            // 璁剧疆瓒呮椂锛岃秴鏃剁粨鏉熺洃鍚�
+            dynamicTask.startDelay(streamAuthorityInfo.getCallId(), ()->{
+                logger.info("[褰曞儚涓嬭浇] 鎺ユ敹hook瓒呮椂锛� {}/{}-{}", deviceId, channelId, stream);
+                subscribe.removeSubscribe(hookSubscribe);
+                callback.run(ErrorCode.ERROR100.getCode(), "鎺ユ敹hook瓒呮椂", null);
+            }, 10000);
+        }
+    }
+
+    private DownloadFileInfo getDownloadFilePath(MediaServerItem mediaServerItem, String filePath) {
+        DownloadFileInfo downloadFileInfo = new DownloadFileInfo();
+
+        String pathTemplate = "%s://%s:%s/index/api/downloadFile?file_path=" + filePath;
+
+        downloadFileInfo.setHttpPath(String.format(pathTemplate, "http", mediaServerItem.getStreamIp(),
+                mediaServerItem.getHttpPort()));
+
+        if (mediaServerItem.getHttpSSlPort() > 0) {
+            downloadFileInfo.setHttpsPath(String.format(pathTemplate, "https", mediaServerItem.getStreamIp(),
+                    mediaServerItem.getHttpSSlPort()));
+        }
+        return downloadFileInfo;
     }
 
     private StreamInfo onPublishHandlerForDownload(MediaServerItem mediaServerItemInuse, HookParam hookParam, String deviceId, String channelId, String startTime, String endTime) {

--
Gitblit v1.8.0