ApiStreamController.java 11 KB

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