From 9c5a2c7b26a4b46b7d7da2e8e1419c5c8a98fedc Mon Sep 17 00:00:00 2001 From: shikong <919411476@qq.com> Date: Thu, 14 Dec 2023 15:48:44 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../wvp/api/video/VideoController.java | 4 +- .../gb28181/Gb28181DownloadService.java | 58 ++++++++++++++----- 2 files changed, 44 insertions(+), 18 deletions(-) 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 802c8d2..ad7eb18 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 @@ -93,11 +93,11 @@ public class VideoController { return JsonResponse.success(null); } - @Operation(summary = "关闭实时视频 60s 后关闭") + @Operation(summary = "关闭实时视频") @GetMapping(value = "/device/realtime/close") @ResponseBody public JsonResponse close(@ParameterObject RealtimeVideoReq req) { - gb28181DownloadService.autoCloseReadtimeVideo(req.getDeviceCode()); + gb28181DownloadService.closeRealtimeVideoNow(req.getDeviceCode()); return JsonResponse.success(null); } } 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 eb9adb2..4db8996 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 @@ -250,7 +250,7 @@ public class Gb28181DownloadService { String cacheKey = CacheUtil.getKey(docking.getGbDeviceId(), device.getGbDeviceChannelId()); realtimeVideoInfoMap.put(cacheKey, videoInfo); String existCallId = RedisUtil.StringOps.get(cacheKey); - autoCloseReadtimeVideo(docking,device,videoInfo,cacheKey,existCallId); + autoCloseRealtimeVideo(docking,device,videoInfo,cacheKey,existCallId); String url = StringUtils.isNotBlank(proxySipConfig.getProxyMediaUrl()) ? StringUtils.replace(videoInfo.getUrl(), zlmMediaConfig.getUrl(), proxySipConfig.getProxyMediaUrl()): videoInfo.getUrl(); @@ -276,10 +276,10 @@ public class Gb28181DownloadService { return null; }); - autoCloseReadtimeVideo(deviceCode); + autoCloseRealtimeVideo(deviceCode); } - public void autoCloseReadtimeVideo(String deviceCode){ + public void autoCloseRealtimeVideo(String deviceCode){ WvpProxyDevice device = deviceService.getDeviceByDeviceCode(deviceCode).orElse(null); if(device == null){ return; @@ -292,7 +292,7 @@ public class Gb28181DownloadService { String cacheKey = CacheUtil.getKey(docking.getGbDeviceId(), device.getGbDeviceChannelId()); String existCallId = RedisUtil.StringOps.get(cacheKey); realtimeVideoInfoMap.computeIfPresent(cacheKey, (key, videoInfo) -> { - autoCloseReadtimeVideo(docking,device,videoInfo,cacheKey,existCallId); + autoCloseRealtimeVideo(docking,device,videoInfo,cacheKey,existCallId); return videoInfo; }); } @@ -315,24 +315,50 @@ public class Gb28181DownloadService { }); } - public void autoCloseReadtimeVideo(WvpProxyDocking docking,WvpProxyDevice device,Gb28181DownloadService.VideoInfo videoInfo,String cacheKey, String existCallId){ + public void autoCloseRealtimeVideo(WvpProxyDocking docking, WvpProxyDevice device, Gb28181DownloadService.VideoInfo videoInfo, String cacheKey, String existCallId){ ScheduledFuture schedule = scheduledExecutorService.schedule(() -> { - log.info("结束实时视频 发送 bye 关闭 {} {}", videoInfo.getDevice().getGbDeviceChannelId(), videoInfo.getCallId()); - String deviceIp = docking.getIp(); - int devicePort = Integer.parseInt(docking.getPort()); - if (StringUtils.isNotBlank(existCallId)) { - sender.sendRequest((provider, localIp, localPort) -> - SipRequestBuilder.createByeRequest(deviceIp, devicePort, device.getGbDeviceChannelId(), SipUtil.generateFromTag(), null, existCallId)); - } - RedisUtil.KeyOps.delete(cacheKey); - zlmMediaService.closeRtpServer(CloseRtpServer.builder() - .streamId(videoInfo.streamId) - .build()); + closeRealtimeVideoNow(docking, device, videoInfo, cacheKey, existCallId); }, 60, TimeUnit.SECONDS); realtimeMap.put(cacheKey,schedule); } + public void closeRealtimeVideoNow(String deviceCode){ + WvpProxyDevice device = deviceService.getDeviceByDeviceCode(deviceCode).orElse(null); + if(device == null){ + return; + } + WvpProxyDocking docking = dockingService.getDeviceByDeviceCode(device.getGbDeviceId()).orElse(null); + if(docking == null){ + return; + } + + String cacheKey = CacheUtil.getKey(docking.getGbDeviceId(), device.getGbDeviceChannelId()); + String existCallId = RedisUtil.StringOps.get(cacheKey); + realtimeVideoInfoMap.computeIfPresent(cacheKey, (key, videoInfo) -> { + closeRealtimeVideoNow(docking,device,videoInfo,cacheKey,existCallId); + realtimeMap.computeIfPresent(cacheKey,(k, scheduledFuture)->{ + scheduledFuture.cancel(true); + return null; + }); + return null; + }); + } + + public void closeRealtimeVideoNow(WvpProxyDocking docking, WvpProxyDevice device, Gb28181DownloadService.VideoInfo videoInfo, String cacheKey, String existCallId){ + log.info("结束实时视频 发送 bye 关闭 {} {}", videoInfo.getDevice().getGbDeviceChannelId(), videoInfo.getCallId()); + String deviceIp = docking.getIp(); + int devicePort = Integer.parseInt(docking.getPort()); + if (StringUtils.isNotBlank(existCallId)) { + sender.sendRequest((provider, localIp, localPort) -> + SipRequestBuilder.createByeRequest(deviceIp, devicePort, device.getGbDeviceChannelId(), SipUtil.generateFromTag(), null, existCallId)); + } + RedisUtil.KeyOps.delete(cacheKey); + zlmMediaService.closeRtpServer(CloseRtpServer.builder() + .streamId(videoInfo.streamId) + .build()); + } + @SneakyThrows public void videoStream(HttpServletRequest request, HttpServletResponse response, String deviceCode, Date startTime, Date endTime) { AsyncContext asyncContext = request.startAsync();