diff --git a/gb28181-wvp-proxy-service/src/main/java/cn/skcks/docking/gb28181/wvp/service/video/VideoService.java b/gb28181-wvp-proxy-service/src/main/java/cn/skcks/docking/gb28181/wvp/service/video/VideoService.java index 7c07d5f..08b6f37 100644 --- a/gb28181-wvp-proxy-service/src/main/java/cn/skcks/docking/gb28181/wvp/service/video/VideoService.java +++ b/gb28181-wvp-proxy-service/src/main/java/cn/skcks/docking/gb28181/wvp/service/video/VideoService.java @@ -3,6 +3,7 @@ package cn.skcks.docking.gb28181.wvp.service.video; import cn.hutool.core.date.DateTime; import cn.hutool.core.date.DateUnit; import cn.hutool.core.date.DateUtil; +import cn.hutool.core.io.IoUtil; import cn.skcks.docking.gb28181.core.sip.utils.SipUtil; import cn.skcks.docking.gb28181.wvp.config.ProxySipConfig; import cn.skcks.docking.gb28181.wvp.config.WvpProxyConfig; @@ -31,9 +32,7 @@ import org.bytedeco.javacv.FFmpegFrameRecorder; import org.bytedeco.javacv.FrameGrabber; import org.springframework.stereotype.Service; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.OutputStream; +import java.io.*; import java.util.Optional; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; @@ -50,6 +49,8 @@ public class VideoService { private final ProxySipConfig proxySipConfig; private final DockingService dockingService; private final SipSender sender; + private final ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(); + /** * 写入 flv 响应头信息 * @param response HttpServletResponse 响应 @@ -230,8 +231,9 @@ public class VideoService { @SneakyThrows public void ffmpegRecord(ServletResponse response, String url, long time, WvpProxyDevice device,String callId){ ServletOutputStream outputStream = response.getOutputStream(); + PipedOutputStream pipedOutputStream = new PipedOutputStream(); ByteArrayOutputStream errorStream = new ByteArrayOutputStream(); - PumpStreamHandler streamHandler = new PumpStreamHandler(outputStream, errorStream); + PumpStreamHandler streamHandler = new PumpStreamHandler(pipedOutputStream, errorStream); DefaultExecuteResultHandler executeResultHandler = mediaStatus(device,callId); DateTime startTime = DateUtil.date(); Executor executor; @@ -241,13 +243,16 @@ public class VideoService { executor = ffmpegSupportService.downloadToStream(url, time, TimeUnit.SECONDS,streamHandler,executeResultHandler); } log.info("开始录制 {}", url); - ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(); ScheduledFuture schedule = scheduledExecutorService.schedule(() -> { log.info("到达结束时间, 结束录制 {}", url); executor.getWatchdog().destroyProcess(); log.info("结束录制 {}", url); }, time, TimeUnit.SECONDS); executeResultHandler.waitFor(); + + DataInputStream is = new DataInputStream(new PipedInputStream(pipedOutputStream)); + IoUtil.copy(is,outputStream); + schedule.cancel(true); DateTime endTime = DateUtil.date(); log.info("录制进程结束 {}, 录制耗时: {}", url, DateUtil.between(startTime,endTime, DateUnit.SECOND));