RtpController.java 12 KB

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