ApiStreamController.java 11 KB

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