支持全局异常和统一返回结果
This commit is contained in:
@@ -3,6 +3,7 @@ package com.genersoft.iot.vmp.conf;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.genersoft.iot.vmp.vmanager.bean.ErrorCode;
|
||||
import com.genersoft.iot.vmp.vmanager.bean.WVPResult;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.http.converter.HttpMessageConverter;
|
||||
@@ -13,20 +14,21 @@ import org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice;
|
||||
|
||||
/**
|
||||
* 全局统一返回结果
|
||||
* @author lin
|
||||
*/
|
||||
@RestControllerAdvice
|
||||
public class GlobalResponseAdvice implements ResponseBodyAdvice<Object> {
|
||||
|
||||
|
||||
@Override
|
||||
public boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType) {
|
||||
public boolean supports(@NotNull MethodParameter returnType, @NotNull Class<? extends HttpMessageConverter<?>> converterType) {
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object beforeBodyWrite(Object body, MethodParameter returnType, MediaType selectedContentType, Class<? extends HttpMessageConverter<?>> selectedConverterType, ServerHttpRequest request, ServerHttpResponse response) {
|
||||
public Object beforeBodyWrite(Object body, @NotNull MethodParameter returnType, @NotNull MediaType selectedContentType, @NotNull Class<? extends HttpMessageConverter<?>> selectedConverterType, @NotNull ServerHttpRequest request, @NotNull ServerHttpResponse response) {
|
||||
// 排除api文档的接口,这个接口不需要统一
|
||||
String[] excludePath = {"/v3/api-docs","/api/v1"};
|
||||
String[] excludePath = {"/v3/api-docs","/api/v1","/index/hook"};
|
||||
for (String path : excludePath) {
|
||||
if (request.getURI().getPath().startsWith(path)) {
|
||||
return body;
|
||||
@@ -43,7 +45,7 @@ public class GlobalResponseAdvice implements ResponseBodyAdvice<Object> {
|
||||
}
|
||||
|
||||
if (body instanceof String) {
|
||||
return JSON.toJSON(WVPResult.success(body));
|
||||
return JSON.toJSONString(WVPResult.success(body));
|
||||
}
|
||||
|
||||
return WVPResult.success(body);
|
||||
|
||||
@@ -4,14 +4,18 @@ import com.alibaba.fastjson.JSONObject;
|
||||
import com.genersoft.iot.vmp.gb28181.event.SipSubscribe;
|
||||
import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem;
|
||||
|
||||
import javax.sip.RequestEvent;
|
||||
import java.util.EventObject;
|
||||
|
||||
|
||||
/**
|
||||
* @author lin
|
||||
*/
|
||||
public class PlayBackResult<T> {
|
||||
private int code;
|
||||
private T data;
|
||||
private MediaServerItem mediaServerItem;
|
||||
private JSONObject response;
|
||||
private SipSubscribe.EventResult event;
|
||||
private SipSubscribe.EventResult<EventObject> event;
|
||||
|
||||
public int getCode() {
|
||||
return code;
|
||||
@@ -45,11 +49,11 @@ public class PlayBackResult<T> {
|
||||
this.response = response;
|
||||
}
|
||||
|
||||
public SipSubscribe.EventResult getEvent() {
|
||||
public SipSubscribe.EventResult<EventObject> getEvent() {
|
||||
return event;
|
||||
}
|
||||
|
||||
public void setEvent(SipSubscribe.EventResult event) {
|
||||
public void setEvent(SipSubscribe.EventResult<EventObject> event) {
|
||||
this.event = event;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,8 +12,6 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.context.request.async.DeferredResult;
|
||||
@@ -362,7 +360,7 @@ public class PlayServiceImpl implements IPlayService {
|
||||
resultHolder.invokeAllResult(msg);
|
||||
} else {
|
||||
logger.warn("设备预览API调用失败!");
|
||||
msg.setData("设备预览API调用失败!");
|
||||
msg.setData(WVPResult.fail(ErrorCode.ERROR100.getCode(), "设备预览API调用失败!"));
|
||||
resultHolder.invokeAllResult(msg);
|
||||
}
|
||||
}
|
||||
@@ -415,16 +413,15 @@ public class PlayServiceImpl implements IPlayService {
|
||||
}
|
||||
DeferredResult<String> result = new DeferredResult<>(30000L);
|
||||
resultHolder.put(DeferredResultHolder.CALLBACK_CMD_PLAYBACK + deviceId + channelId, uuid, result);
|
||||
RequestMessage msg = new RequestMessage();
|
||||
msg.setId(uuid);
|
||||
msg.setKey(key);
|
||||
RequestMessage requestMessage = new RequestMessage();
|
||||
requestMessage.setId(uuid);
|
||||
requestMessage.setKey(key);
|
||||
PlayBackResult<RequestMessage> playBackResult = new PlayBackResult<>();
|
||||
String playBackTimeOutTaskKey = UUID.randomUUID().toString();
|
||||
String playBackTimeOutTaskKey = UUID.randomUUID().toString();
|
||||
dynamicTask.startDelay(playBackTimeOutTaskKey, ()->{
|
||||
logger.warn(String.format("设备回放超时,deviceId:%s ,channelId:%s", deviceId, channelId));
|
||||
playBackResult.setCode(-1);
|
||||
playBackResult.setData(msg);
|
||||
playBackCallback.call(playBackResult);
|
||||
playBackResult.setData(requestMessage);
|
||||
SIPDialog dialog = streamSession.getDialogByStream(deviceId, channelId, ssrcInfo.getStream());
|
||||
// 点播超时回复BYE 同时释放ssrc以及此次点播的资源
|
||||
if (dialog != null) {
|
||||
@@ -447,24 +444,23 @@ public class PlayServiceImpl implements IPlayService {
|
||||
StreamInfo streamInfo = onPublishHandler(inviteStreamInfo.getMediaServerItem(), inviteStreamInfo.getResponse(), deviceId, channelId);
|
||||
if (streamInfo == null) {
|
||||
logger.warn("设备回放API调用失败!");
|
||||
msg.setData("设备回放API调用失败!");
|
||||
playBackResult.setCode(-1);
|
||||
playBackResult.setData(msg);
|
||||
playBackCallback.call(playBackResult);
|
||||
return;
|
||||
}
|
||||
redisCatchStorage.startPlayback(streamInfo, inviteStreamInfo.getCallId());
|
||||
msg.setData(JSON.toJSONString(streamInfo));
|
||||
WVPResult<StreamInfo> success = WVPResult.success(streamInfo);
|
||||
requestMessage.setData(success);
|
||||
playBackResult.setCode(0);
|
||||
playBackResult.setData(msg);
|
||||
playBackResult.setData(requestMessage);
|
||||
playBackResult.setMediaServerItem(inviteStreamInfo.getMediaServerItem());
|
||||
playBackResult.setResponse(inviteStreamInfo.getResponse());
|
||||
playBackCallback.call(playBackResult);
|
||||
}, event -> {
|
||||
dynamicTask.stop(playBackTimeOutTaskKey);
|
||||
msg.setData(String.format("回放失败, 错误码: %s, %s", event.statusCode, event.msg));
|
||||
requestMessage.setData(WVPResult.fail(ErrorCode.ERROR100.getCode(), String.format("回放失败, 错误码: %s, %s", event.statusCode, event.msg)));
|
||||
playBackResult.setCode(-1);
|
||||
playBackResult.setData(msg);
|
||||
playBackResult.setData(requestMessage);
|
||||
playBackResult.setEvent(event);
|
||||
playBackCallback.call(playBackResult);
|
||||
streamSession.remove(device.getDeviceId(), channelId, ssrcInfo.getStream());
|
||||
@@ -498,13 +494,13 @@ public class PlayServiceImpl implements IPlayService {
|
||||
}
|
||||
|
||||
resultHolder.put(key, uuid, result);
|
||||
RequestMessage msg = new RequestMessage();
|
||||
msg.setId(uuid);
|
||||
msg.setKey(key);
|
||||
RequestMessage requestMessage = new RequestMessage();
|
||||
requestMessage.setId(uuid);
|
||||
requestMessage.setKey(key);
|
||||
WVPResult<StreamInfo> wvpResult = new WVPResult<>();
|
||||
msg.setData(wvpResult);
|
||||
requestMessage.setData(wvpResult);
|
||||
PlayBackResult<RequestMessage> downloadResult = new PlayBackResult<>();
|
||||
downloadResult.setData(msg);
|
||||
downloadResult.setData(requestMessage);
|
||||
|
||||
String downLoadTimeOutTaskKey = UUID.randomUUID().toString();
|
||||
dynamicTask.startDelay(downLoadTimeOutTaskKey, ()->{
|
||||
@@ -606,7 +602,7 @@ public class PlayServiceImpl implements IPlayService {
|
||||
resultHolder.invokeResult(msg);
|
||||
} else {
|
||||
logger.warn("设备预览API调用失败!");
|
||||
msg.setData("设备预览API调用失败!");
|
||||
msg.setData(WVPResult.fail(ErrorCode.ERROR100.getCode(), "设备预览API调用失败!"));
|
||||
resultHolder.invokeResult(msg);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -24,8 +24,6 @@ import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
@@ -80,7 +78,7 @@ public class PlayController {
|
||||
@Parameter(name = "deviceId", description = "设备国标编号", required = true)
|
||||
@Parameter(name = "channelId", description = "通道国标编号", required = true)
|
||||
@GetMapping("/start/{deviceId}/{channelId}")
|
||||
public DeferredResult<String> play(@PathVariable String deviceId,
|
||||
public DeferredResult<WVPResult<String>> play(@PathVariable String deviceId,
|
||||
@PathVariable String channelId) {
|
||||
|
||||
// 获取可用的zlm
|
||||
@@ -96,72 +94,33 @@ public class PlayController {
|
||||
@Parameter(name = "deviceId", description = "设备国标编号", required = true)
|
||||
@Parameter(name = "channelId", description = "通道国标编号", required = true)
|
||||
@GetMapping("/stop/{deviceId}/{channelId}")
|
||||
public DeferredResult<String> playStop(@PathVariable String deviceId, @PathVariable String channelId) {
|
||||
public JSONObject playStop(@PathVariable String deviceId, @PathVariable String channelId) {
|
||||
|
||||
logger.debug(String.format("设备预览/回放停止API调用,streamId:%s_%s", deviceId, channelId ));
|
||||
|
||||
String uuid = UUID.randomUUID().toString();
|
||||
DeferredResult<String> result = new DeferredResult<>();
|
||||
if (deviceId == null || channelId == null) {
|
||||
throw new ControllerException(ErrorCode.ERROR400);
|
||||
}
|
||||
|
||||
// 录像查询以channelId作为deviceId查询
|
||||
String key = DeferredResultHolder.CALLBACK_CMD_STOP + deviceId + channelId;
|
||||
resultHolder.put(key, uuid, result);
|
||||
StreamInfo streamInfo = redisCatchStorage.queryPlayByDevice(deviceId, channelId);
|
||||
if (streamInfo == null) {
|
||||
RequestMessage msg = new RequestMessage();
|
||||
msg.setId(uuid);
|
||||
msg.setKey(key);
|
||||
msg.setData("点播未找到");
|
||||
resultHolder.invokeAllResult(msg);
|
||||
storager.stopPlay(deviceId, channelId);
|
||||
return result;
|
||||
}
|
||||
cmder.streamByeCmd(deviceId, channelId, streamInfo.getStream(), null, eventResult -> {
|
||||
redisCatchStorage.stopPlay(streamInfo);
|
||||
storager.stopPlay(streamInfo.getDeviceID(), streamInfo.getChannelId());
|
||||
RequestMessage msgForSuccess = new RequestMessage();
|
||||
msgForSuccess.setId(uuid);
|
||||
msgForSuccess.setKey(key);
|
||||
msgForSuccess.setData(String.format("success"));
|
||||
resultHolder.invokeAllResult(msgForSuccess);
|
||||
});
|
||||
|
||||
if (deviceId != null || channelId != null) {
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("deviceId", deviceId);
|
||||
json.put("channelId", channelId);
|
||||
RequestMessage msg = new RequestMessage();
|
||||
msg.setId(uuid);
|
||||
msg.setKey(key);
|
||||
msg.setData(json.toString());
|
||||
resultHolder.invokeAllResult(msg);
|
||||
} else {
|
||||
logger.warn("设备预览/回放停止API调用失败!");
|
||||
RequestMessage msg = new RequestMessage();
|
||||
msg.setId(uuid);
|
||||
msg.setKey(key);
|
||||
msg.setData("streamId null");
|
||||
resultHolder.invokeAllResult(msg);
|
||||
throw new ControllerException(ErrorCode.ERROR100.getCode(), "点播未找到");
|
||||
}
|
||||
|
||||
// 超时处理
|
||||
result.onTimeout(()->{
|
||||
logger.warn(String.format("设备预览/回放停止超时,deviceId/channelId:%s_%s ", deviceId, channelId));
|
||||
redisCatchStorage.stopPlay(streamInfo);
|
||||
storager.stopPlay(streamInfo.getDeviceID(), streamInfo.getChannelId());
|
||||
RequestMessage msg = new RequestMessage();
|
||||
msg.setId(uuid);
|
||||
msg.setKey(key);
|
||||
msg.setData("Timeout");
|
||||
resultHolder.invokeAllResult(msg);
|
||||
});
|
||||
return result;
|
||||
cmder.streamByeCmd(deviceId, channelId, streamInfo.getStream(), null, null);
|
||||
redisCatchStorage.stopPlay(streamInfo);
|
||||
|
||||
storager.stopPlay(streamInfo.getDeviceID(), streamInfo.getChannelId());
|
||||
JSONObject json = new JSONObject();
|
||||
json.put("deviceId", deviceId);
|
||||
json.put("channelId", channelId);
|
||||
return json;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 将不是h264的视频通过ffmpeg 转码为h264 + aac
|
||||
* @param streamId 流ID
|
||||
* @return
|
||||
*/
|
||||
@Operation(summary = "将不是h264的视频通过ffmpeg 转码为h264 + aac")
|
||||
@Parameter(name = "streamId", description = "视频流ID", required = true)
|
||||
@@ -205,8 +164,6 @@ public class PlayController {
|
||||
|
||||
/**
|
||||
* 结束转码
|
||||
* @param key
|
||||
* @return
|
||||
*/
|
||||
@Operation(summary = "结束转码")
|
||||
@Parameter(name = "key", description = "视频流key", required = true)
|
||||
@@ -278,7 +235,7 @@ public class PlayController {
|
||||
});
|
||||
|
||||
result.onTimeout(() -> {
|
||||
logger.warn(String.format("语音广播操作超时, 设备未返回应答指令"));
|
||||
logger.warn("语音广播操作超时, 设备未返回应答指令");
|
||||
RequestMessage msg = new RequestMessage();
|
||||
msg.setKey(key);
|
||||
msg.setId(uuid);
|
||||
|
||||
@@ -3,7 +3,6 @@ package com.genersoft.iot.vmp.vmanager.gb28181.playback;
|
||||
import com.genersoft.iot.vmp.common.StreamInfo;
|
||||
import com.genersoft.iot.vmp.conf.exception.ControllerException;
|
||||
import com.genersoft.iot.vmp.gb28181.transmit.callback.DeferredResultHolder;
|
||||
import com.genersoft.iot.vmp.service.IMediaServerService;
|
||||
import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
|
||||
import com.genersoft.iot.vmp.service.IPlayService;
|
||||
import com.genersoft.iot.vmp.vmanager.bean.ErrorCode;
|
||||
@@ -13,10 +12,7 @@ import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.util.ObjectUtils;
|
||||
import org.springframework.util.StringUtils;
|
||||
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
@@ -29,6 +25,9 @@ import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommander;
|
||||
import com.genersoft.iot.vmp.storager.IVideoManagerStorage;
|
||||
import org.springframework.web.context.request.async.DeferredResult;
|
||||
|
||||
/**
|
||||
* @author lin
|
||||
*/
|
||||
@Tag(name = "视频回放")
|
||||
@CrossOrigin
|
||||
@RestController
|
||||
@@ -65,11 +64,9 @@ public class PlaybackController {
|
||||
logger.debug(String.format("设备回放 API调用,deviceId:%s ,channelId:%s", deviceId, channelId));
|
||||
}
|
||||
|
||||
DeferredResult<String> result = playService.playBack(deviceId, channelId, startTime, endTime, null, wvpResult->{
|
||||
resultHolder.invokeResult(wvpResult.getData());
|
||||
});
|
||||
|
||||
return result;
|
||||
return playService.playBack(deviceId, channelId, startTime, endTime, null,
|
||||
playBackResult->resultHolder.invokeResult(playBackResult.getData()));
|
||||
}
|
||||
|
||||
|
||||
@@ -109,10 +106,8 @@ public class PlaybackController {
|
||||
@GetMapping("/resume/{streamId}")
|
||||
public void playResume(@PathVariable String streamId) {
|
||||
logger.info("playResume: "+streamId);
|
||||
JSONObject json = new JSONObject();
|
||||
StreamInfo streamInfo = redisCatchStorage.queryPlayback(null, null, streamId, null);
|
||||
if (null == streamInfo) {
|
||||
json.put("msg", "streamId不存在");
|
||||
logger.warn("streamId不存在!");
|
||||
throw new ControllerException(ErrorCode.ERROR400.getCode(), "streamId不存在");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user