ApiStreamController.java 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. package com.genersoft.iot.vmp.web;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.genersoft.iot.vmp.common.StreamInfo;
  5. import com.genersoft.iot.vmp.gb28181.bean.Device;
  6. import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel;
  7. import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommander;
  8. import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
  9. import com.genersoft.iot.vmp.storager.IVideoManagerStorager;
  10. import com.genersoft.iot.vmp.vmanager.gb28181.play.PlayController;
  11. import org.slf4j.Logger;
  12. import org.slf4j.LoggerFactory;
  13. import org.springframework.beans.factory.annotation.Autowired;
  14. import org.springframework.http.ResponseEntity;
  15. import org.springframework.web.bind.annotation.*;
  16. import org.springframework.web.context.request.async.DeferredResult;
  17. /**
  18. * 兼容LiveGBS的API:实时直播
  19. */
  20. @SuppressWarnings(value = {"rawtypes", "unchecked"})
  21. @CrossOrigin
  22. @RestController
  23. @RequestMapping(value = "/api/v1/stream")
  24. public class ApiStreamController {
  25. private final static Logger logger = LoggerFactory.getLogger(ApiStreamController.class);
  26. @Autowired
  27. private SIPCommander cmder;
  28. @Autowired
  29. private IVideoManagerStorager storager;
  30. @Autowired
  31. private IRedisCatchStorage redisCatchStorage;
  32. // @Autowired
  33. // private ZLMRESTfulUtils zlmresTfulUtils;
  34. @Autowired
  35. private PlayController playController;
  36. /**
  37. * 实时直播 - 开始直播
  38. * @param serial 设备编号
  39. * @param channel 通道序号 默认值: 1
  40. * @param code 通道编号,通过 /api/v1/device/channellist 获取的 ChannelList.ID, 该参数和 channel 二选一传递即可
  41. * @param cdn TODO 转推 CDN 地址, 形如: [rtmp|rtsp]://xxx, encodeURIComponent
  42. * @param audio TODO 是否开启音频, 默认 开启
  43. * @param transport 流传输模式, 默认 UDP
  44. * @param checkchannelstatus TODO 是否检查通道状态, 默认 false, 表示 拉流前不检查通道状态是否在线
  45. * @param transportmode TODO 当 transport=TCP 时有效, 指示流传输主被动模式, 默认被动
  46. * @param timeout TODO 拉流超时(秒),
  47. * @return
  48. */
  49. @RequestMapping(value = "/start")
  50. private DeferredResult<JSONObject> start(String serial ,
  51. @RequestParam(required = false)Integer channel ,
  52. @RequestParam(required = false)String code,
  53. @RequestParam(required = false)String cdn,
  54. @RequestParam(required = false)String audio,
  55. @RequestParam(required = false)String transport,
  56. @RequestParam(required = false)String checkchannelstatus ,
  57. @RequestParam(required = false)String transportmode,
  58. @RequestParam(required = false)String timeout
  59. ){
  60. DeferredResult<JSONObject> resultDeferredResult = new DeferredResult<JSONObject>();
  61. Device device = storager.queryVideoDevice(serial);
  62. if (device == null ) {
  63. JSONObject result = new JSONObject();
  64. result.put("error","device[ " + serial + " ]未找到");
  65. resultDeferredResult.setResult(result);
  66. }else if (device.getOnline() == 0) {
  67. JSONObject result = new JSONObject();
  68. result.put("error","device[ " + code + " ]offline");
  69. resultDeferredResult.setResult(result);
  70. }
  71. resultDeferredResult.onTimeout(()->{
  72. logger.info("播放等待超时");
  73. JSONObject result = new JSONObject();
  74. result.put("error","timeout");
  75. resultDeferredResult.setResult(result);
  76. // 清理RTP server
  77. });
  78. DeviceChannel deviceChannel = storager.queryChannel(serial, code);
  79. if (deviceChannel == null) {
  80. JSONObject result = new JSONObject();
  81. result.put("error","channel[ " + code + " ]未找到");
  82. resultDeferredResult.setResult(result);
  83. }else if (deviceChannel.getStatus() == 0) {
  84. JSONObject result = new JSONObject();
  85. result.put("error","channel[ " + code + " ]offline");
  86. resultDeferredResult.setResult(result);
  87. }
  88. DeferredResult<ResponseEntity<String>> play = playController.play(serial, code);
  89. play.setResultHandler((Object o)->{
  90. ResponseEntity<String> responseEntity = (ResponseEntity)o;
  91. StreamInfo streamInfo = JSON.parseObject(responseEntity.getBody(), StreamInfo.class);
  92. JSONObject result = new JSONObject();
  93. result.put("StreamID", streamInfo.getStreamId());
  94. result.put("DeviceID", device.getDeviceId());
  95. result.put("ChannelID", code);
  96. result.put("ChannelName", deviceChannel.getName());
  97. result.put("ChannelCustomName", "");
  98. result.put("FLV", streamInfo.getFlv());
  99. result.put("WS_FLV", streamInfo.getWs_flv());
  100. result.put("RTMP", streamInfo.getRtmp());
  101. result.put("HLS", streamInfo.getHls());
  102. result.put("RTSP", streamInfo.getRtsp());
  103. result.put("CDN", "");
  104. result.put("SnapURL", "");
  105. result.put("Transport", device.getTransport());
  106. result.put("StartAt", "");
  107. result.put("Duration", "");
  108. result.put("SourceVideoCodecName", "");
  109. result.put("SourceVideoWidth", "");
  110. result.put("SourceVideoHeight", "");
  111. result.put("SourceVideoFrameRate", "");
  112. result.put("SourceAudioCodecName", "");
  113. result.put("SourceAudioSampleRate", "");
  114. result.put("AudioEnable", "");
  115. result.put("Ondemand", "");
  116. result.put("InBytes", "");
  117. result.put("InBitRate", "");
  118. result.put("OutBytes", "");
  119. result.put("NumOutputs", "");
  120. result.put("CascadeSize", "");
  121. result.put("RelaySize", "");
  122. result.put("ChannelPTZType", 0);
  123. resultDeferredResult.setResult(result);
  124. });
  125. return resultDeferredResult;
  126. }
  127. /**
  128. * 实时直播 - 直播流停止
  129. * @param serial 设备编号
  130. * @param channel 通道序号
  131. * @param code 通道国标编号
  132. * @param check_outputs
  133. * @return
  134. */
  135. @RequestMapping(value = "/stop")
  136. @ResponseBody
  137. private JSONObject stop(String serial ,
  138. @RequestParam(required = false)Integer channel ,
  139. @RequestParam(required = false)String code,
  140. @RequestParam(required = false)String check_outputs
  141. ){
  142. StreamInfo streamInfo = redisCatchStorage.queryPlayByDevice(serial, code);
  143. if (streamInfo == null) {
  144. JSONObject result = new JSONObject();
  145. result.put("error","未找到流信息");
  146. return result;
  147. }
  148. cmder.streamByeCmd(streamInfo.getStreamId());
  149. redisCatchStorage.stopPlay(streamInfo);
  150. storager.stopPlay(streamInfo.getDeviceID(), streamInfo.getChannelId());
  151. return null;
  152. }
  153. /**
  154. * 实时直播 - 直播流保活
  155. * @param serial 设备编号
  156. * @param channel 通道序号
  157. * @param code 通道国标编号
  158. * @return
  159. */
  160. @RequestMapping(value = "/touch")
  161. @ResponseBody
  162. private JSONObject touch(String serial ,String t,
  163. @RequestParam(required = false)Integer channel ,
  164. @RequestParam(required = false)String code,
  165. @RequestParam(required = false)String autorestart,
  166. @RequestParam(required = false)String audio,
  167. @RequestParam(required = false)String cdn
  168. ){
  169. return null;
  170. }
  171. }