From 5a35e376875a5117d4caacc5a3b4138d6948b6a3 Mon Sep 17 00:00:00 2001 From: shikong <919411476@qq.com> Date: Tue, 10 Oct 2023 10:05:30 +0800 Subject: [PATCH] =?UTF-8?q?/device/video.mp4=20contentType=20=E6=94=B9?= =?UTF-8?q?=E4=B8=BA=20application/octet-stream?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wvp/api/video/VideoController.java | 2 +- .../gb28181/Gb28181DownloadService.java | 33 +++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/gb28181-wvp-proxy-api/src/main/java/cn/skcks/docking/gb28181/wvp/api/video/VideoController.java b/gb28181-wvp-proxy-api/src/main/java/cn/skcks/docking/gb28181/wvp/api/video/VideoController.java index 3391134..dfe487f 100644 --- a/gb28181-wvp-proxy-api/src/main/java/cn/skcks/docking/gb28181/wvp/api/video/VideoController.java +++ b/gb28181-wvp-proxy-api/src/main/java/cn/skcks/docking/gb28181/wvp/api/video/VideoController.java @@ -72,6 +72,6 @@ public class VideoController { Date endTime = DateUtil.offsetMillisecond(reqEndTime, (int) back); log.info("请求的时间范围 {} ~ {}", reqStartTime, reqEndTime); log.info("偏移后的时间范围 {} ~ {}", startTime, endTime); - gb28181DownloadService.video(request,response,req.getDeviceCode(), startTime, endTime, req.getFileHeader()); + gb28181DownloadService.videoStream(request,response,req.getDeviceCode(), startTime, endTime); } } diff --git a/gb28181-wvp-proxy-service/src/main/java/cn/skcks/docking/gb28181/wvp/service/gb28181/Gb28181DownloadService.java b/gb28181-wvp-proxy-service/src/main/java/cn/skcks/docking/gb28181/wvp/service/gb28181/Gb28181DownloadService.java index 81b7bd0..58e97cb 100644 --- a/gb28181-wvp-proxy-service/src/main/java/cn/skcks/docking/gb28181/wvp/service/gb28181/Gb28181DownloadService.java +++ b/gb28181-wvp-proxy-service/src/main/java/cn/skcks/docking/gb28181/wvp/service/gb28181/Gb28181DownloadService.java @@ -93,6 +93,12 @@ public class Gb28181DownloadService { response.setHeader("Connection", "close"); } + public void streamHeader(HttpServletResponse response) { + response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE); + response.setHeader("Accept-Ranges", "none"); + response.setHeader("Connection", "close"); + } + public void header(HttpServletResponse response, String fileName) { header(response); response.setHeader("Content-Disposition", @@ -158,6 +164,33 @@ public class Gb28181DownloadService { }); } + @SneakyThrows + public void videoStream(HttpServletRequest request, HttpServletResponse response, String deviceCode, Date startTime, Date endTime) { + AsyncContext asyncContext = request.startAsync(); + asyncContext.setTimeout(0); + asyncContext.start(()->{ + HttpServletResponse asyncResponse = (HttpServletResponse)asyncContext.getResponse(); + try{ + download(deviceCode, startTime,endTime).whenComplete((videoInfo, e)->{ + streamHeader(asyncResponse); + if(e != null){ + writeErrorToResponse(asyncResponse, JsonResponse.error(e.getMessage())); + } else if(videoInfo == null){ + writeErrorToResponse(asyncResponse, JsonResponse.error("下载失败")); + } else if(wvpProxyConfig.getUseFfmpeg()){ + videoService.ffmpegRecord(asyncResponse, videoInfo.getUrl(), DateUtil.between(startTime,endTime,DateUnit.SECOND) + 60,videoInfo.getDevice(),videoInfo.getCallId()); + } else { + videoService.javaCVrecord(asyncResponse, videoInfo.getUrl(), DateUtil.between(startTime,endTime,DateUnit.SECOND) + 60); + } + asyncContext.complete(); + }); + } catch(Exception e) { + writeErrorToResponse(asyncResponse, JsonResponse.error(e.getMessage())); + asyncContext.complete(); + } + }); + } + private void writeFileHeader(HttpServletResponse response, String deviceCode, Date startTime, Date endTime, Boolean fileHeader){ if(fileHeader){ header(response, StringUtils.joinWith("_",