ApiStreamController.java 8.6 KB

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