This commit is contained in:
shikong 2023-12-14 15:48:44 +08:00
parent db58ea8b95
commit 9c5a2c7b26
2 changed files with 44 additions and 18 deletions

View File

@ -93,11 +93,11 @@ public class VideoController {
return JsonResponse.success(null); return JsonResponse.success(null);
} }
@Operation(summary = "关闭实时视频 60s 后关闭") @Operation(summary = "关闭实时视频")
@GetMapping(value = "/device/realtime/close") @GetMapping(value = "/device/realtime/close")
@ResponseBody @ResponseBody
public JsonResponse<Void> close(@ParameterObject RealtimeVideoReq req) { public JsonResponse<Void> close(@ParameterObject RealtimeVideoReq req) {
gb28181DownloadService.autoCloseReadtimeVideo(req.getDeviceCode()); gb28181DownloadService.closeRealtimeVideoNow(req.getDeviceCode());
return JsonResponse.success(null); return JsonResponse.success(null);
} }
} }

View File

@ -250,7 +250,7 @@ public class Gb28181DownloadService {
String cacheKey = CacheUtil.getKey(docking.getGbDeviceId(), device.getGbDeviceChannelId()); String cacheKey = CacheUtil.getKey(docking.getGbDeviceId(), device.getGbDeviceChannelId());
realtimeVideoInfoMap.put(cacheKey, videoInfo); realtimeVideoInfoMap.put(cacheKey, videoInfo);
String existCallId = RedisUtil.StringOps.get(cacheKey); String existCallId = RedisUtil.StringOps.get(cacheKey);
autoCloseReadtimeVideo(docking,device,videoInfo,cacheKey,existCallId); autoCloseRealtimeVideo(docking,device,videoInfo,cacheKey,existCallId);
String url = StringUtils.isNotBlank(proxySipConfig.getProxyMediaUrl()) ? String url = StringUtils.isNotBlank(proxySipConfig.getProxyMediaUrl()) ?
StringUtils.replace(videoInfo.getUrl(), zlmMediaConfig.getUrl(), proxySipConfig.getProxyMediaUrl()): StringUtils.replace(videoInfo.getUrl(), zlmMediaConfig.getUrl(), proxySipConfig.getProxyMediaUrl()):
videoInfo.getUrl(); videoInfo.getUrl();
@ -276,10 +276,10 @@ public class Gb28181DownloadService {
return null; return null;
}); });
autoCloseReadtimeVideo(deviceCode); autoCloseRealtimeVideo(deviceCode);
} }
public void autoCloseReadtimeVideo(String deviceCode){ public void autoCloseRealtimeVideo(String deviceCode){
WvpProxyDevice device = deviceService.getDeviceByDeviceCode(deviceCode).orElse(null); WvpProxyDevice device = deviceService.getDeviceByDeviceCode(deviceCode).orElse(null);
if(device == null){ if(device == null){
return; return;
@ -292,7 +292,7 @@ public class Gb28181DownloadService {
String cacheKey = CacheUtil.getKey(docking.getGbDeviceId(), device.getGbDeviceChannelId()); String cacheKey = CacheUtil.getKey(docking.getGbDeviceId(), device.getGbDeviceChannelId());
String existCallId = RedisUtil.StringOps.get(cacheKey); String existCallId = RedisUtil.StringOps.get(cacheKey);
realtimeVideoInfoMap.computeIfPresent(cacheKey, (key, videoInfo) -> { realtimeVideoInfoMap.computeIfPresent(cacheKey, (key, videoInfo) -> {
autoCloseReadtimeVideo(docking,device,videoInfo,cacheKey,existCallId); autoCloseRealtimeVideo(docking,device,videoInfo,cacheKey,existCallId);
return videoInfo; 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(() -> { ScheduledFuture<?> schedule = scheduledExecutorService.schedule(() -> {
log.info("结束实时视频 发送 bye 关闭 {} {}", videoInfo.getDevice().getGbDeviceChannelId(), videoInfo.getCallId()); closeRealtimeVideoNow(docking, device, videoInfo, cacheKey, existCallId);
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());
}, 60, TimeUnit.SECONDS); }, 60, TimeUnit.SECONDS);
realtimeMap.put(cacheKey,schedule); 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 @SneakyThrows
public void videoStream(HttpServletRequest request, HttpServletResponse response, String deviceCode, Date startTime, Date endTime) { public void videoStream(HttpServletRequest request, HttpServletResponse response, String deviceCode, Date startTime, Date endTime) {
AsyncContext asyncContext = request.startAsync(); AsyncContext asyncContext = request.startAsync();