ApiStreamController.java 8.7 KB

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