PlayController.java 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package com.genersoft.iot.vmp.vmanager.play;
  2. import com.alibaba.fastjson.JSON;
  3. import com.genersoft.iot.vmp.common.StreamInfo;
  4. import org.slf4j.Logger;
  5. import org.slf4j.LoggerFactory;
  6. import org.springframework.beans.factory.annotation.Autowired;
  7. import org.springframework.http.HttpStatus;
  8. import org.springframework.http.ResponseEntity;
  9. import org.springframework.web.bind.annotation.CrossOrigin;
  10. import org.springframework.web.bind.annotation.GetMapping;
  11. import org.springframework.web.bind.annotation.PathVariable;
  12. import org.springframework.web.bind.annotation.PostMapping;
  13. import org.springframework.web.bind.annotation.RequestMapping;
  14. import org.springframework.web.bind.annotation.RestController;
  15. import com.alibaba.fastjson.JSONObject;
  16. import com.genersoft.iot.vmp.gb28181.bean.Device;
  17. import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommander;
  18. import com.genersoft.iot.vmp.storager.IVideoManagerStorager;
  19. @CrossOrigin
  20. @RestController
  21. @RequestMapping("/api")
  22. public class PlayController {
  23. private final static Logger logger = LoggerFactory.getLogger(PlayController.class);
  24. @Autowired
  25. private SIPCommander cmder;
  26. @Autowired
  27. private IVideoManagerStorager storager;
  28. @GetMapping("/play/{deviceId}/{channelId}")
  29. public ResponseEntity<String> play(@PathVariable String deviceId,@PathVariable String channelId){
  30. Device device = storager.queryVideoDevice(deviceId);
  31. StreamInfo streamInfo = cmder.playStreamCmd(device, channelId);
  32. // 等待推流, TODO 默认超时15s
  33. long startTime = System.currentTimeMillis();
  34. while (storager.queryPlay(streamInfo) == null || storager.queryPlay(streamInfo).getFlv() == null) {
  35. try {
  36. if (System.currentTimeMillis() - startTime > 15 * 1000)
  37. Thread.sleep(200);
  38. } catch (InterruptedException e) {
  39. e.printStackTrace();
  40. }
  41. }
  42. streamInfo = storager.queryPlay(streamInfo);
  43. if (logger.isDebugEnabled()) {
  44. logger.debug(String.format("设备预览 API调用,deviceId:%s ,channelId:%s",deviceId, channelId));
  45. logger.debug("设备预览 API调用,ssrc:"+streamInfo.getSsrc()+",ZLMedia streamId:"+Integer.toHexString(Integer.parseInt(streamInfo.getSsrc())));
  46. }
  47. if(streamInfo!=null) {
  48. return new ResponseEntity<String>(JSON.toJSONString(streamInfo),HttpStatus.OK);
  49. } else {
  50. logger.warn("设备预览API调用失败!");
  51. return new ResponseEntity<String>(HttpStatus.INTERNAL_SERVER_ERROR);
  52. }
  53. }
  54. @PostMapping("/play/{ssrc}/stop")
  55. public ResponseEntity<String> playStop(@PathVariable String ssrc){
  56. cmder.streamByeCmd(ssrc);
  57. StreamInfo streamInfo = storager.queryPlayBySSRC(ssrc);
  58. storager.stopPlay(streamInfo);
  59. if (logger.isDebugEnabled()) {
  60. logger.debug(String.format("设备预览停止API调用,ssrc:%s", ssrc));
  61. }
  62. if(ssrc!=null) {
  63. JSONObject json = new JSONObject();
  64. json.put("ssrc", ssrc);
  65. return new ResponseEntity<String>(json.toString(),HttpStatus.OK);
  66. } else {
  67. logger.warn("设备预览停止API调用失败!");
  68. return new ResponseEntity<String>(HttpStatus.INTERNAL_SERVER_ERROR);
  69. }
  70. }
  71. }