RtpController.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. package com.genersoft.iot.vmp.vmanager.rtp;
  2. import com.alibaba.fastjson2.JSONObject;
  3. import com.genersoft.iot.vmp.common.VideoManagerConstants;
  4. import com.genersoft.iot.vmp.conf.DynamicTask;
  5. import com.genersoft.iot.vmp.conf.SipConfig;
  6. import com.genersoft.iot.vmp.conf.UserSetting;
  7. import com.genersoft.iot.vmp.conf.VersionInfo;
  8. import com.genersoft.iot.vmp.conf.exception.ControllerException;
  9. import com.genersoft.iot.vmp.gb28181.bean.SendRtpItem;
  10. import com.genersoft.iot.vmp.media.zlm.ZLMServerFactory;
  11. import com.genersoft.iot.vmp.media.zlm.ZLMRTPServerFactory;
  12. import com.genersoft.iot.vmp.media.zlm.ZlmHttpHookSubscribe;
  13. import com.genersoft.iot.vmp.media.zlm.dto.HookSubscribeFactory;
  14. import com.genersoft.iot.vmp.media.zlm.dto.HookSubscribeForRtpServerTimeout;
  15. import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem;
  16. import com.genersoft.iot.vmp.service.IDeviceChannelService;
  17. import com.genersoft.iot.vmp.service.IDeviceService;
  18. import com.genersoft.iot.vmp.service.IMediaServerService;
  19. import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
  20. import com.genersoft.iot.vmp.vmanager.bean.ErrorCode;
  21. import com.genersoft.iot.vmp.vmanager.bean.OtherRtpSendInfo;
  22. import io.swagger.v3.oas.annotations.Operation;
  23. import io.swagger.v3.oas.annotations.Parameter;
  24. import io.swagger.v3.oas.annotations.tags.Tag;
  25. import okhttp3.OkHttpClient;
  26. import okhttp3.Request;
  27. import org.slf4j.Logger;
  28. import org.slf4j.LoggerFactory;
  29. import org.springframework.beans.factory.annotation.Autowired;
  30. import org.springframework.beans.factory.annotation.Value;
  31. import org.springframework.data.redis.core.RedisTemplate;
  32. import org.springframework.web.bind.annotation.GetMapping;
  33. import org.springframework.web.bind.annotation.RequestMapping;
  34. import org.springframework.web.bind.annotation.ResponseBody;
  35. import org.springframework.web.bind.annotation.RestController;
  36. import java.io.IOException;
  37. import java.util.HashMap;
  38. import java.util.Map;
  39. @SuppressWarnings("rawtypes")
  40. @Tag(name = "第三方服务对接")
  41. @RestController
  42. @RequestMapping("/api/rtp")
  43. public class RtpController {
  44. @Autowired
  45. private ZLMServerFactory zlmServerFactory;
  46. private final static Logger logger = LoggerFactory.getLogger(RtpController.class);
  47. @Autowired
  48. private ZlmHttpHookSubscribe hookSubscribe;
  49. @Autowired
  50. private IMediaServerService mediaServerService;
  51. @Autowired
  52. private VersionInfo versionInfo;
  53. @Autowired
  54. private SipConfig sipConfig;
  55. @Autowired
  56. private UserSetting userSetting;
  57. @Autowired
  58. private IDeviceService deviceService;
  59. @Autowired
  60. private IDeviceChannelService channelService;
  61. @Autowired
  62. private DynamicTask dynamicTask;
  63. @Autowired
  64. private RedisTemplate<Object, Object> redisTemplate;
  65. @Value("${server.port}")
  66. private int serverPort;
  67. @Autowired
  68. private IRedisCatchStorage redisCatchStorage;
  69. @GetMapping(value = "/receive/open")
  70. @ResponseBody
  71. @Operation(summary = "开启收流和获取发流信息")
  72. @Parameter(name = "isSend", description = "是否发送,false时只开启收流, true同时返回推流信息", required = true)
  73. @Parameter(name = "callId", description = "整个过程的唯一标识,为了与后续接口关联", required = true)
  74. @Parameter(name = "ssrc", description = "来源流的SSRC,不传则不校验来源ssrc", required = false)
  75. @Parameter(name = "stream", description = "形成的流的ID", required = true)
  76. @Parameter(name = "tcpMode", description = "收流模式, 0为UDP, 1为TCP被动", required = true)
  77. @Parameter(name = "callBack", description = "回调地址,如果收流超时会通道回调通知,回调为get请求,参数为callId", required = true)
  78. public OtherRtpSendInfo openRtpServer(Boolean isSend, String ssrc, String callId, String stream, Integer tcpMode, String callBack) {
  79. logger.info("[第三方服务对接->开启收流和获取发流信息] isSend->{}, ssrc->{}, callId->{}, stream->{}, tcpMode->{}, callBack->{}",
  80. isSend, ssrc, callId, stream, tcpMode==0?"UDP":"TCP被动", callBack);
  81. MediaServerItem mediaServerItem = mediaServerService.getDefaultMediaServer();
  82. if (mediaServerItem == null) {
  83. throw new ControllerException(ErrorCode.ERROR100.getCode(),"没有可用的MediaServer");
  84. }
  85. if (stream == null) {
  86. throw new ControllerException(ErrorCode.ERROR100.getCode(),"stream参数不可为空");
  87. }
  88. if (isSend != null && isSend && callId == null) {
  89. throw new ControllerException(ErrorCode.ERROR100.getCode(),"isSend为true时,CallID不能为空");
  90. }
  91. int ssrcInt = 0;
  92. if (ssrc != null) {
  93. try {
  94. ssrcInt = Integer.parseInt(ssrc);
  95. }catch (NumberFormatException e) {
  96. throw new ControllerException(ErrorCode.ERROR100.getCode(),"ssrc格式错误");
  97. }
  98. }
  99. int localPort = zlmServerFactory.createRTPServer(mediaServerItem, stream, ssrcInt, null, false, tcpMode);
  100. // 注册回调如果rtp收流超时则通过回调发送通知
  101. if (callBack != null) {
  102. HookSubscribeForRtpServerTimeout hookSubscribeForRtpServerTimeout = HookSubscribeFactory.on_rtp_server_timeout(ssrc, null, mediaServerItem.getId());
  103. // 订阅 zlm启动事件, 新的zlm也会从这里进入系统
  104. hookSubscribe.addSubscribe(hookSubscribeForRtpServerTimeout,
  105. (mediaServerItemInUse, response)->{
  106. if (stream.equals(response.getString("stream_id"))) {
  107. logger.info("[开启收流和获取发流信息] 等待收流超时 callId->{}, 发送回调", callId);
  108. OkHttpClient.Builder httpClientBuilder = new OkHttpClient.Builder();
  109. OkHttpClient client = httpClientBuilder.build();
  110. String url = callBack + "?callId=" + callId;
  111. Request request = new Request.Builder().get().url(url).build();
  112. try {
  113. client.newCall(request).execute();
  114. } catch (IOException e) {
  115. logger.error("[开启收流和获取发流信息] 等待收流超时 callId->{}, 发送回调失败", callId, e);
  116. }
  117. }
  118. });
  119. }
  120. OtherRtpSendInfo otherRtpSendInfo = new OtherRtpSendInfo();
  121. otherRtpSendInfo.setReceiveIp(mediaServerItem.getSdpIp());
  122. otherRtpSendInfo.setReceivePort(localPort);
  123. otherRtpSendInfo.setCallId(callId);
  124. otherRtpSendInfo.setStream(stream);
  125. if (isSend != null && isSend) {
  126. String key = VideoManagerConstants.WVP_OTHER_SEND_RTP_INFO + userSetting.getServerId() + callId;
  127. // 预创建发流信息
  128. int port = zlmServerFactory.keepPort(mediaServerItem, callId, 0, ssrc1 -> {
  129. return redisTemplate.opsForValue().get(key) != null;
  130. });
  131. // 将信息写入redis中,以备后用
  132. redisTemplate.opsForValue().set(key, otherRtpSendInfo);
  133. // 设置超时任务,超时未使用,则自动移除,并关闭端口保持, 默认五分钟
  134. dynamicTask.startDelay(key, ()->{
  135. logger.info("[第三方服务对接->开启收流和获取发流信息] 端口保持超时 callId->{}", callId);
  136. redisTemplate.delete(key);
  137. zlmServerFactory.releasePort(mediaServerItem, callId);
  138. }, 300000);
  139. otherRtpSendInfo.setIp(mediaServerItem.getSdpIp());
  140. otherRtpSendInfo.setPort(port);
  141. logger.info("[开启收流和获取发流信息] 结果,callId->{}, {}", callId, otherRtpSendInfo);
  142. }
  143. return otherRtpSendInfo;
  144. }
  145. @GetMapping(value = "/receive/close")
  146. @ResponseBody
  147. @Operation(summary = "关闭收流")
  148. @Parameter(name = "stream", description = "流的ID", required = true)
  149. public void closeRtpServer(String stream) {
  150. logger.info("[第三方服务对接->关闭收流] stream->{}", stream);
  151. MediaServerItem mediaServerItem = mediaServerService.getDefaultMediaServer();
  152. zlmServerFactory.closeRtpServer(mediaServerItem,stream);
  153. }
  154. @GetMapping(value = "/send/start")
  155. @ResponseBody
  156. @Operation(summary = "发送流")
  157. @Parameter(name = "ssrc", description = "发送流的SSRC", required = true)
  158. @Parameter(name = "ip", description = "目标IP", required = true)
  159. @Parameter(name = "port", description = "目标端口", required = true)
  160. @Parameter(name = "app", description = "待发送应用名", required = true)
  161. @Parameter(name = "stream", description = "待发送流Id", required = true)
  162. @Parameter(name = "callId", description = "整个过程的唯一标识,不传则使用随机端口发流", required = true)
  163. @Parameter(name = "onlyAudio", description = "是否只有音频", required = true)
  164. @Parameter(name = "isUdp", description = "是否为UDP", required = true)
  165. @Parameter(name = "streamType", description = "流类型,1为es流,2为ps流, 默认es流", required = false)
  166. public void sendRTP(String ssrc, String ip, Integer port, String app, String stream, String callId, Boolean onlyAudio, Boolean isUdp, Integer streamType) {
  167. logger.info("[第三方服务对接->发送流] ssrc->{}, ip->{}, port->{}, app->{}, stream->{}, callId->{}, onlyAudio->{}, streamType->{}",
  168. ssrc, ip, port, app, stream, callId, onlyAudio, streamType == 1? "ES":"PS");
  169. MediaServerItem mediaServerItem = mediaServerService.getDefaultMediaServer();
  170. String key = VideoManagerConstants.WVP_OTHER_SEND_RTP_INFO + userSetting.getServerId() + callId;
  171. OtherRtpSendInfo sendInfo = (OtherRtpSendInfo)redisTemplate.opsForValue().get(key);
  172. if (sendInfo != null) {
  173. zlmServerFactory.releasePort(mediaServerItem, sendInfo.getCallId());
  174. }else {
  175. sendInfo = new OtherRtpSendInfo();
  176. }
  177. sendInfo.setPushApp(app);
  178. sendInfo.setPushStream(stream);
  179. sendInfo.setPushSSRC(ssrc);
  180. Map<String, Object> param = new HashMap<>(12);
  181. param.put("vhost","__defaultVhost__");
  182. param.put("app",app);
  183. param.put("stream",stream);
  184. param.put("ssrc", ssrc);
  185. param.put("dst_url",ip);
  186. param.put("dst_port", port);
  187. String is_Udp = isUdp ? "1" : "0";
  188. param.put("is_udp", is_Udp);
  189. param.put("src_port", sendInfo.getPort());
  190. param.put("use_ps", streamType==2 ? "1" : "0");
  191. param.put("only_audio", onlyAudio ? "1" : "0");
  192. JSONObject jsonObject = zlmServerFactory.startSendRtpStream(mediaServerItem, param);
  193. if (jsonObject.getInteger("code") == 0) {
  194. logger.info("[第三方服务对接->发送流] 发流成功,callId->{}", callId);
  195. redisTemplate.opsForValue().set(key, sendInfo);
  196. }else {
  197. redisTemplate.delete(key);
  198. logger.info("[第三方服务对接->发送流] 发流失败,callId->{}, {}", callId, jsonObject.getString("msg"));
  199. throw new ControllerException(ErrorCode.ERROR100.getCode(), "[发流失败] " + jsonObject.getString("msg"));
  200. }
  201. }
  202. @GetMapping(value = "/send/stop")
  203. @ResponseBody
  204. @Operation(summary = "关闭发送流")
  205. @Parameter(name = "callId", description = "整个过程的唯一标识,不传则使用随机端口发流", required = true)
  206. public void closeSendRTP(String callId) {
  207. logger.info("[第三方服务对接->关闭发送流] callId->{}", callId);
  208. String key = VideoManagerConstants.WVP_OTHER_SEND_RTP_INFO + userSetting.getServerId() + callId;
  209. OtherRtpSendInfo sendInfo = (OtherRtpSendInfo)redisTemplate.opsForValue().get(key);
  210. if (sendInfo == null){
  211. throw new ControllerException(ErrorCode.ERROR100.getCode(), "未开启发流");
  212. }
  213. Map<String, Object> param = new HashMap<>();
  214. param.put("vhost","__defaultVhost__");
  215. param.put("app",sendInfo.getPushApp());
  216. param.put("stream",sendInfo.getPushStream());
  217. param.put("ssrc",sendInfo.getPushSSRC());
  218. MediaServerItem mediaServerItem = mediaServerService.getDefaultMediaServer();
  219. Boolean result = zlmServerFactory.stopSendRtpStream(mediaServerItem, param);
  220. if (!result) {
  221. logger.info("[第三方服务对接->关闭发送流] 失败 callId->{}", callId);
  222. throw new ControllerException(ErrorCode.ERROR100.getCode(), "停止发流失败");
  223. }else {
  224. logger.info("[第三方服务对接->关闭发送流] 成功 callId->{}", callId);
  225. }
  226. }
  227. }