优化快照的存储与显示

This commit is contained in:
648540858
2022-06-02 10:55:41 +08:00
parent 936adf31fe
commit 9ae0691c80
5 changed files with 63 additions and 57 deletions

View File

@@ -91,10 +91,9 @@ public class ZLMHttpHookListener {
public ResponseEntity<String> onServerKeepalive(@RequestBody JSONObject json){
if (logger.isDebugEnabled()) {
logger.debug("[ ZLM HOOK ]on_server_keepalive API调用参数" + json.toString());
logger.debug("[ ZLM HOOK ] on_server_keepalive API调用参数" + json.toString());
}
String mediaServerId = json.getString("mediaServerId");
List<ZLMHttpHookSubscribe.Event> subscribes = this.subscribe.getSubscribes(ZLMHttpHookSubscribe.HookType.on_server_keepalive);
if (subscribes != null && subscribes.size() > 0) {
for (ZLMHttpHookSubscribe.Event subscribe : subscribes) {
@@ -164,7 +163,6 @@ public class ZLMHttpHookListener {
if (mediaInfo != null) {
subscribe.response(mediaInfo, json);
}
}
JSONObject ret = new JSONObject();
ret.put("code", 0);

View File

@@ -151,7 +151,7 @@ public class ZLMRESTfulUtils {
}
}
File snapFile = new File(targetPath + "/" + fileName);
File snapFile = new File(targetPath + File.separator + fileName);
FileOutputStream outStream = new FileOutputStream(snapFile);
outStream.write(Objects.requireNonNull(response.body()).bytes());

View File

@@ -123,36 +123,19 @@ public class PlayServiceImpl implements IPlayService {
result.onCompletion(()->{
// 点播结束时调用截图接口
// TODO 应该在上流时调用更好,结束也可能是错误结束
try {
String classPath = ResourceUtils.getURL("classpath:").getPath();
// 兼容打包为jar的class路径
if(classPath.contains("jar")) {
classPath = classPath.substring(0, classPath.lastIndexOf("."));
classPath = classPath.substring(0, classPath.lastIndexOf("/") + 1);
String path = "snap";
String fileName = deviceId + "_" + channelId + ".jpg";
ResponseEntity responseEntity = (ResponseEntity)result.getResult();
if (responseEntity != null && responseEntity.getStatusCode() == HttpStatus.OK) {
WVPResult wvpResult = (WVPResult)responseEntity.getBody();
if (Objects.requireNonNull(wvpResult).getCode() == 0) {
StreamInfo streamInfoForSuccess = (StreamInfo)wvpResult.getData();
MediaServerItem mediaInfo = mediaServerService.getOne(streamInfoForSuccess.getMediaServerId());
String streamUrl = streamInfoForSuccess.getFmp4();
// 请求截图
logger.info("[请求截图]: " + fileName);
zlmresTfulUtils.getSnap(mediaInfo, streamUrl, 15, 1, path, fileName);
}
if (classPath.startsWith("file:")) {
classPath = classPath.substring(classPath.indexOf(":") + 1);
}
String path = classPath + "static/static/snap/";
// 兼容Windows系统路径去除前面的“/”)
if(System.getProperty("os.name").contains("indows")) {
path = path.substring(1);
}
String fileName = deviceId + "_" + channelId + ".jpg";
ResponseEntity responseEntity = (ResponseEntity)result.getResult();
if (responseEntity != null && responseEntity.getStatusCode() == HttpStatus.OK) {
WVPResult wvpResult = (WVPResult)responseEntity.getBody();
if (Objects.requireNonNull(wvpResult).getCode() == 0) {
StreamInfo streamInfoForSuccess = (StreamInfo)wvpResult.getData();
MediaServerItem mediaInfo = mediaServerService.getOne(streamInfoForSuccess.getMediaServerId());
String streamUrl = streamInfoForSuccess.getFmp4();
// 请求截图
logger.info("[请求截图]: " + fileName);
zlmresTfulUtils.getSnap(mediaInfo, streamUrl, 15, 1, path, fileName);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
});
if (streamInfo != null) {

View File

@@ -21,16 +21,22 @@ import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.http.HttpResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.async.DeferredResult;
import javax.servlet.http.HttpServletResponse;
import javax.sip.DialogState;
import java.io.*;
import java.nio.file.Files;
import java.util.*;
@Api(tags = "国标设备查询", value = "国标设备查询")
@@ -456,4 +462,17 @@ public class DeviceQuery {
wvpResult.setData(dialogStateMap);
return wvpResult;
}
@GetMapping("/snap/{deviceId}/{channelId}")
@ApiOperation(value = "请求截图", notes = "请求截图")
public void getSnap(HttpServletResponse resp, @PathVariable String deviceId, @PathVariable String channelId) {
try {
final InputStream in = Files.newInputStream(new File("snap" + File.separator + deviceId + "_" + channelId + ".jpg").toPath());
resp.setContentType(MediaType.IMAGE_PNG_VALUE);
IOUtils.copy(in, resp.getOutputStream());
} catch (IOException e) {
resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
}
}
}