PlaybackController.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. package com.genersoft.iot.vmp.vmanager.playback;
  2. import com.genersoft.iot.vmp.common.StreamInfo;
  3. import com.genersoft.iot.vmp.gb28181.transmit.callback.DeferredResultHolder;
  4. import com.genersoft.iot.vmp.gb28181.transmit.callback.RequestMessage;
  5. //import com.genersoft.iot.vmp.media.zlm.ZLMRESTfulUtils;
  6. import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
  7. import com.genersoft.iot.vmp.service.IPlayService;
  8. import org.slf4j.Logger;
  9. import org.slf4j.LoggerFactory;
  10. import org.springframework.beans.factory.annotation.Autowired;
  11. import org.springframework.http.HttpStatus;
  12. import org.springframework.http.ResponseEntity;
  13. import org.springframework.web.bind.annotation.CrossOrigin;
  14. import org.springframework.web.bind.annotation.GetMapping;
  15. import org.springframework.web.bind.annotation.PathVariable;
  16. import org.springframework.web.bind.annotation.RequestMapping;
  17. import org.springframework.web.bind.annotation.RestController;
  18. import com.alibaba.fastjson.JSONObject;
  19. import com.genersoft.iot.vmp.gb28181.bean.Device;
  20. import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommander;
  21. import com.genersoft.iot.vmp.storager.IVideoManagerStorager;
  22. import org.springframework.web.context.request.async.DeferredResult;
  23. import javax.sip.message.Response;
  24. import java.util.UUID;
  25. @CrossOrigin
  26. @RestController
  27. @RequestMapping("/api")
  28. public class PlaybackController {
  29. private final static Logger logger = LoggerFactory.getLogger(PlaybackController.class);
  30. @Autowired
  31. private SIPCommander cmder;
  32. @Autowired
  33. private IVideoManagerStorager storager;
  34. @Autowired
  35. private IRedisCatchStorage redisCatchStorage;
  36. // @Autowired
  37. // private ZLMRESTfulUtils zlmresTfulUtils;
  38. @Autowired
  39. private IPlayService playService;
  40. @Autowired
  41. private DeferredResultHolder resultHolder;
  42. @GetMapping("/playback/{deviceId}/{channelId}")
  43. public DeferredResult<ResponseEntity<String>> play(@PathVariable String deviceId, @PathVariable String channelId, String startTime,
  44. String endTime) {
  45. if (logger.isDebugEnabled()) {
  46. logger.debug(String.format("设备回放 API调用,deviceId:%s ,channelId:%s", deviceId, channelId));
  47. }
  48. UUID uuid = UUID.randomUUID();
  49. DeferredResult<ResponseEntity<String>> result = new DeferredResult<ResponseEntity<String>>();
  50. // 超时处理
  51. result.onTimeout(()->{
  52. logger.warn(String.format("设备回放超时,deviceId:%s ,channelId:%s", deviceId, channelId));
  53. RequestMessage msg = new RequestMessage();
  54. msg.setId(DeferredResultHolder.CALLBACK_CMD_PlAY + uuid);
  55. msg.setData("Timeout");
  56. resultHolder.invokeResult(msg);
  57. });
  58. Device device = storager.queryVideoDevice(deviceId);
  59. StreamInfo streamInfo = redisCatchStorage.queryPlaybackByDevice(deviceId, channelId);
  60. if (streamInfo != null) {
  61. // 停止之前的回放
  62. cmder.streamByeCmd(streamInfo.getStreamId());
  63. }
  64. resultHolder.put(DeferredResultHolder.CALLBACK_CMD_PlAY + uuid, result);
  65. cmder.playbackStreamCmd(device, channelId, startTime, endTime, (JSONObject response) -> {
  66. logger.info("收到订阅消息: " + response.toJSONString());
  67. playService.onPublishHandlerForPlayBack(response, deviceId, channelId, uuid.toString());
  68. }, event -> {
  69. Response response = event.getResponse();
  70. RequestMessage msg = new RequestMessage();
  71. msg.setId(DeferredResultHolder.CALLBACK_CMD_PlAY + uuid);
  72. msg.setData(String.format("回放失败, 错误码: %s, %s", response.getStatusCode(), response.getReasonPhrase()));
  73. resultHolder.invokeResult(msg);
  74. });
  75. return result;
  76. }
  77. @RequestMapping("/playback/{ssrc}/stop")
  78. public ResponseEntity<String> playStop(@PathVariable String ssrc) {
  79. cmder.streamByeCmd(ssrc);
  80. if (logger.isDebugEnabled()) {
  81. logger.debug(String.format("设备录像回放停止 API调用,ssrc:%s", ssrc));
  82. }
  83. if (ssrc != null) {
  84. JSONObject json = new JSONObject();
  85. json.put("ssrc", ssrc);
  86. return new ResponseEntity<String>(json.toString(), HttpStatus.OK);
  87. } else {
  88. logger.warn("设备录像回放停止API调用失败!");
  89. return new ResponseEntity<String>(HttpStatus.INTERNAL_SERVER_ERROR);
  90. }
  91. }
  92. }