先用 PipedInputStream 接收数据 再 写入 http 响应

This commit is contained in:
shikong 2024-03-13 16:14:50 +08:00
parent d4cf7e076a
commit ac10573dc8

View File

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