From b625dcff7dd18bcc6fb638ed7f65ef65d47c937e Mon Sep 17 00:00:00 2001 From: shikong <919411476@qq.com> Date: Sun, 10 Sep 2023 04:57:11 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A6=82=E6=9E=9C=20=E6=97=A0=E6=B3=95?= =?UTF-8?q?=E9=80=9A=E8=BF=87=20wvp-assist=20=E4=B8=8B=E8=BD=BD=E5=88=99?= =?UTF-8?q?=20=E4=BD=BF=E7=94=A8=20=E5=9B=9E=E6=94=BE=E5=AE=9E=E6=97=B6?= =?UTF-8?q?=E5=BD=95=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../gb28181/wvp/proxy/WvpProxyClient.java | 6 +++ .../wvp/service/video/RecordService.java | 2 +- .../gb28181/wvp/service/wvp/WvpService.java | 39 +++++++++++++++---- 3 files changed, 38 insertions(+), 9 deletions(-) diff --git a/gb28181-wvp-proxy-service/src/main/java/cn/skcks/docking/gb28181/wvp/proxy/WvpProxyClient.java b/gb28181-wvp-proxy-service/src/main/java/cn/skcks/docking/gb28181/wvp/proxy/WvpProxyClient.java index df54709..3c7b1e0 100644 --- a/gb28181-wvp-proxy-service/src/main/java/cn/skcks/docking/gb28181/wvp/proxy/WvpProxyClient.java +++ b/gb28181-wvp-proxy-service/src/main/java/cn/skcks/docking/gb28181/wvp/proxy/WvpProxyClient.java @@ -52,6 +52,12 @@ public interface WvpProxyClient { @PathVariable String channelId, @SpringQueryMap GeneralTimeReq req); + @GetMapping("/api/playback/stop/{deviceId}/{channelId}/{stream}") + JsonResponse playbackStop(@RequestHeader("access-token") String token, + @PathVariable String deviceId, + @PathVariable String channelId, + @PathVariable String stream); + // /api/playback/stop/44050100001180000001/44050100001310000001/44050100001180000001_44050100001310000001_20230908083001_20230908083501 @GetMapping("/api/gb_record/download/start/{deviceId}/{channelId}") JsonResponse downloadStart(@RequestHeader("access-token") String token, diff --git a/gb28181-wvp-proxy-service/src/main/java/cn/skcks/docking/gb28181/wvp/service/video/RecordService.java b/gb28181-wvp-proxy-service/src/main/java/cn/skcks/docking/gb28181/wvp/service/video/RecordService.java index 9beea30..4e23b8e 100644 --- a/gb28181-wvp-proxy-service/src/main/java/cn/skcks/docking/gb28181/wvp/service/video/RecordService.java +++ b/gb28181-wvp-proxy-service/src/main/java/cn/skcks/docking/gb28181/wvp/service/video/RecordService.java @@ -48,7 +48,7 @@ public class RecordService { } @SneakyThrows - private void record(ServletResponse response, String url, long time) { + public void record(ServletResponse response, String url, long time) { ByteArrayOutputStream stream = new ByteArrayOutputStream(); OutputStream outputStream = response.getOutputStream(); log.info("url {}", url); diff --git a/gb28181-wvp-proxy-service/src/main/java/cn/skcks/docking/gb28181/wvp/service/wvp/WvpService.java b/gb28181-wvp-proxy-service/src/main/java/cn/skcks/docking/gb28181/wvp/service/wvp/WvpService.java index 0e17c28..5683b5a 100644 --- a/gb28181-wvp-proxy-service/src/main/java/cn/skcks/docking/gb28181/wvp/service/wvp/WvpService.java +++ b/gb28181-wvp-proxy-service/src/main/java/cn/skcks/docking/gb28181/wvp/service/wvp/WvpService.java @@ -1,11 +1,13 @@ package cn.skcks.docking.gb28181.wvp.service.wvp; +import cn.hutool.core.date.DateUnit; import cn.hutool.core.date.DateUtil; import cn.hutool.core.io.IoUtil; import cn.hutool.crypto.digest.MD5; import cn.skcks.docking.gb28181.common.json.JsonException; import cn.skcks.docking.gb28181.common.json.JsonResponse; import cn.skcks.docking.gb28181.wvp.config.WvpProxyConfig; +import cn.skcks.docking.gb28181.wvp.dto.common.GeneralTimeReq; import cn.skcks.docking.gb28181.wvp.dto.device.DeviceChannel; import cn.skcks.docking.gb28181.wvp.dto.device.GetDeviceChannelsReq; import cn.skcks.docking.gb28181.wvp.dto.device.GetDeviceChannelsResp; @@ -22,8 +24,10 @@ import cn.skcks.docking.gb28181.wvp.orm.mybatis.dynamic.model.WvpProxyDevice; import cn.skcks.docking.gb28181.wvp.proxy.WvpProxyClient; import cn.skcks.docking.gb28181.wvp.service.device.DeviceService; import cn.skcks.docking.gb28181.wvp.service.download.DownloadService; +import cn.skcks.docking.gb28181.wvp.service.video.RecordService; import cn.skcks.docking.gb28181.wvp.utils.RetryUtil; import com.github.rholder.retry.*; +import io.swagger.v3.core.util.Json; import jakarta.servlet.AsyncContext; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; @@ -49,6 +53,7 @@ public class WvpService { private final WvpProxyConfig wvpProxyConfig; private final DeviceService deviceService; private final DownloadService downloadService; + private final RecordService recordService; @@ -111,7 +116,6 @@ public class WvpService { * @param endDateTime 结束时间 * @return JsonResponse 类型的执行结果 如果 为 null 或 code 不为 0 或 200 则视为执行失败 */ - @SuppressWarnings("UnstableApiUsage") @SneakyThrows public JsonResponse video(HttpServletResponse response, String deviceCode, String deviceId, String channelId, Date startDateTime, Date endDateTime) { String passwdMd5 = MD5.create().digestHex(wvpProxyConfig.getPasswd()); @@ -163,13 +167,37 @@ public class WvpService { log.debug("{}", record); }); + try{ + JsonResponse videoPathResponse = downloadFromWvpAssist(deviceCode, deviceId, channelId, startTime, endTime, token); + String videoUrl = videoPathResponse.getData(); + log.info("设备(deviceCode {}) (deviceId {}, channel{}) ({} ~ {}) 视频下载地址 {}", deviceCode, deviceId, channelId, startTime, endTime, videoUrl); + downloadService.download(response, videoUrl); + } catch (Exception e){ + Retryer> playBackRetryer = RetryUtil.getDefaultRetryer("通过回放获取实时视频流下载"); + JsonResponse videoStreamResponse = playBackRetryer.call(() -> wvpProxyClient.playbackStart(token, deviceId, channelId, new GeneralTimeReq(startTime, endTime))); + StreamContent streamContent = videoStreamResponse.getData(); + String stream = streamContent.getStream(); + String streamUrl = streamContent.getFlv(); + try { + recordService.record(response, streamUrl, DateUtil.between(startDateTime, endDateTime, DateUnit.SECOND)); + } finally { + wvpProxyClient.playbackStop(token, deviceId, channelId, stream); + } + } + + return login; + } + + @SuppressWarnings("UnstableApiUsage") + @SneakyThrows + private JsonResponse downloadFromWvpAssist(String deviceCode, String deviceId, String channelId, String startTime, String endTime, String token) { Retryer> downloadRetryer = RetryUtil.getDefaultRetryer("调用 wvp 设备历史视频下载 api"); - JsonResponse videoPathResponse = downloadRetryer.call(() -> { + return downloadRetryer.call(() -> { JsonResponse downloadStart = wvpProxyClient.downloadStart(token, deviceId, channelId, DownloadStartReq.builder() .startTime(startTime) .endTime(endTime) .build()); - if(downloadStart.getData() == null){ + if (downloadStart.getData() == null) { throw new JsonException(downloadStart.getMsg()); } StreamContent downloadStartData = downloadStart.getData(); @@ -232,10 +260,5 @@ public class WvpService { } }); }); - - String videoUrl = videoPathResponse.getData(); - log.info("设备(deviceCode {}) (deviceId {}, channel{}) ({} ~ {}) 视频下载地址 {}", deviceCode, deviceId, channelId, startTime, endTime, videoUrl); - downloadService.download(response, videoUrl); - return login; } }