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);
}
@Operation(summary = "关闭实时视频 60s 后关闭")
@Operation(summary = "关闭实时视频")
@GetMapping(value = "/device/realtime/close")
@ResponseBody
public JsonResponse<Void> close(@ParameterObject RealtimeVideoReq req) {
gb28181DownloadService.autoCloseReadtimeVideo(req.getDeviceCode());
gb28181DownloadService.closeRealtimeVideoNow(req.getDeviceCode());
return JsonResponse.success(null);
}
}

View File

@ -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();