ApiStreamController.java 9.3 KB

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