ApiStreamController.java 12 KB

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