PlaybackController.java 3.8 KB

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