Commit 9fc3db1f5e381378fd54818e0ba017df6be96fa9

Authored by 648540858
1 parent f525b557

增加发流音视频分开发送

src/main/java/com/genersoft/iot/vmp/media/zlm/dto/HookType.java
... ... @@ -21,5 +21,6 @@ public enum HookType {
21 21 on_server_started,
22 22  
23 23 on_rtp_server_timeout,
24   - on_server_keepalive
  24 + on_server_keepalive,
  25 + on_send_rtp_stopped
25 26 }
... ...
src/main/java/com/genersoft/iot/vmp/vmanager/bean/OtherRtpSendInfo.java
... ... @@ -5,12 +5,17 @@ public class OtherRtpSendInfo {
5 5 /**
6 6 * 发流IP
7 7 */
8   - private String ip;
  8 + private String sendLocalIp;
9 9  
10 10 /**
11   - * 发流端口
  11 + * 音频发流端口
12 12 */
13   - private int port;
  13 + private int sendLocalPortForAudio;
  14 +
  15 + /**
  16 + * 视频发流端口
  17 + */
  18 + private int sendLocalPortForVideo;
14 19  
15 20 /**
16 21 * 收流IP
... ... @@ -18,9 +23,14 @@ public class OtherRtpSendInfo {
18 23 private String receiveIp;
19 24  
20 25 /**
21   - * 收流端口
  26 + * 音频收流端口
  27 + */
  28 + private int receivePortForAudio;
  29 +
  30 + /**
  31 + * 视频收流端口
22 32 */
23   - private int receivePort;
  33 + private int receivePortForVideo;
24 34  
25 35 /**
26 36 * 会话ID
... ... @@ -48,23 +58,6 @@ public class OtherRtpSendInfo {
48 58 private String pushSSRC;
49 59  
50 60  
51   -
52   - public String getIp() {
53   - return ip;
54   - }
55   -
56   - public void setIp(String ip) {
57   - this.ip = ip;
58   - }
59   -
60   - public int getPort() {
61   - return port;
62   - }
63   -
64   - public void setPort(int port) {
65   - this.port = port;
66   - }
67   -
68 61 public String getReceiveIp() {
69 62 return receiveIp;
70 63 }
... ... @@ -73,12 +66,20 @@ public class OtherRtpSendInfo {
73 66 this.receiveIp = receiveIp;
74 67 }
75 68  
76   - public int getReceivePort() {
77   - return receivePort;
  69 + public int getReceivePortForAudio() {
  70 + return receivePortForAudio;
  71 + }
  72 +
  73 + public void setReceivePortForAudio(int receivePortForAudio) {
  74 + this.receivePortForAudio = receivePortForAudio;
  75 + }
  76 +
  77 + public int getReceivePortForVideo() {
  78 + return receivePortForVideo;
78 79 }
79 80  
80   - public void setReceivePort(int receivePort) {
81   - this.receivePort = receivePort;
  81 + public void setReceivePortForVideo(int receivePortForVideo) {
  82 + this.receivePortForVideo = receivePortForVideo;
82 83 }
83 84  
84 85 public String getCallId() {
... ... @@ -121,15 +122,45 @@ public class OtherRtpSendInfo {
121 122 this.pushSSRC = pushSSRC;
122 123 }
123 124  
  125 +
  126 + public String getSendLocalIp() {
  127 + return sendLocalIp;
  128 + }
  129 +
  130 + public void setSendLocalIp(String sendLocalIp) {
  131 + this.sendLocalIp = sendLocalIp;
  132 + }
  133 +
  134 + public int getSendLocalPortForAudio() {
  135 + return sendLocalPortForAudio;
  136 + }
  137 +
  138 + public void setSendLocalPortForAudio(int sendLocalPortForAudio) {
  139 + this.sendLocalPortForAudio = sendLocalPortForAudio;
  140 + }
  141 +
  142 + public int getSendLocalPortForVideo() {
  143 + return sendLocalPortForVideo;
  144 + }
  145 +
  146 + public void setSendLocalPortForVideo(int sendLocalPortForVideo) {
  147 + this.sendLocalPortForVideo = sendLocalPortForVideo;
  148 + }
  149 +
124 150 @Override
125 151 public String toString() {
126 152 return "OtherRtpSendInfo{" +
127   - " ip='" + ip + '\'' +
128   - ", port=" + port +
  153 + "sendLocalIp='" + sendLocalIp + '\'' +
  154 + ", sendLocalPortForAudio=" + sendLocalPortForAudio +
  155 + ", sendLocalPortForVideo=" + sendLocalPortForVideo +
129 156 ", receiveIp='" + receiveIp + '\'' +
130   - ", receivePort=" + receivePort +
  157 + ", receivePortForAudio=" + receivePortForAudio +
  158 + ", receivePortForVideo=" + receivePortForVideo +
131 159 ", callId='" + callId + '\'' +
132 160 ", stream='" + stream + '\'' +
  161 + ", pushApp='" + pushApp + '\'' +
  162 + ", pushStream='" + pushStream + '\'' +
  163 + ", pushSSRC='" + pushSSRC + '\'' +
133 164 '}';
134 165 }
135 166 }
... ...
src/main/java/com/genersoft/iot/vmp/vmanager/rtp/RtpController.java
... ... @@ -98,10 +98,13 @@ public class RtpController {
98 98 }catch (NumberFormatException e) {
99 99 throw new ControllerException(ErrorCode.ERROR100.getCode(),"ssrc格式错误");
100 100 }
101   -
102 101 }
103 102 String receiveKey = VideoManagerConstants.WVP_OTHER_RECEIVE_RTP_INFO + userSetting.getServerId() + "_" + callId + "_" + stream;
104   - int localPort = zlmServerFactory.createRTPServer(mediaServerItem, stream, ssrcInt, null, false, tcpMode);
  103 + int localPortForVideo = zlmServerFactory.createRTPServer(mediaServerItem, stream, ssrcInt, null, false, tcpMode);
  104 + int localPortForAudio = zlmServerFactory.createRTPServer(mediaServerItem, stream + "_a" , ssrcInt, null, false, tcpMode);
  105 + if (localPortForVideo == 0 || localPortForAudio == 0) {
  106 + throw new ControllerException(ErrorCode.ERROR100.getCode(), "获取端口失败");
  107 + }
105 108 // 注册回调如果rtp收流超时则通过回调发送通知
106 109 if (callBack != null) {
107 110 HookSubscribeForRtpServerTimeout hookSubscribeForRtpServerTimeout = HookSubscribeFactory.on_rtp_server_timeout(stream, String.valueOf(ssrcInt), mediaServerItem.getId());
... ... @@ -121,12 +124,14 @@ public class RtpController {
121 124 } catch (IOException e) {
122 125 logger.error("[第三方服务对接->开启收流和获取发流信息] 等待收流超时 callId->{}, 发送回调失败", callId, e);
123 126 }
  127 + hookSubscribe.removeSubscribe(hookSubscribeForRtpServerTimeout);
124 128 }
125 129 });
126 130 }
127 131 OtherRtpSendInfo otherRtpSendInfo = new OtherRtpSendInfo();
128 132 otherRtpSendInfo.setReceiveIp(mediaServerItem.getSdpIp());
129   - otherRtpSendInfo.setReceivePort(localPort);
  133 + otherRtpSendInfo.setReceivePortForVideo(localPortForVideo);
  134 + otherRtpSendInfo.setReceivePortForAudio(localPortForAudio);
130 135 otherRtpSendInfo.setCallId(callId);
131 136 otherRtpSendInfo.setStream(stream);
132 137  
... ... @@ -135,11 +140,13 @@ public class RtpController {
135 140 if (isSend != null && isSend) {
136 141 String key = VideoManagerConstants.WVP_OTHER_SEND_RTP_INFO + userSetting.getServerId() + "_" + callId;
137 142 // 预创建发流信息
138   - int port = sendRtpPortManager.getNextPort(mediaServerItem.getId());
  143 + int portForVideo = sendRtpPortManager.getNextPort(mediaServerItem.getId());
  144 + int portForAudio = sendRtpPortManager.getNextPort(mediaServerItem.getId());
139 145 // 将信息写入redis中,以备后用
140 146 redisTemplate.opsForValue().set(key, otherRtpSendInfo, 300, TimeUnit.SECONDS);
141   - otherRtpSendInfo.setIp(mediaServerItem.getSdpIp());
142   - otherRtpSendInfo.setPort(port);
  147 + otherRtpSendInfo.setSendLocalIp(mediaServerItem.getSdpIp());
  148 + otherRtpSendInfo.setSendLocalPortForVideo(portForVideo);
  149 + otherRtpSendInfo.setSendLocalPortForAudio(portForAudio);
143 150 logger.info("[第三方服务对接->开启收流和获取发流信息] 结果,callId->{}, {}", callId, otherRtpSendInfo);
144 151 }
145 152 return otherRtpSendInfo;
... ... @@ -153,6 +160,7 @@ public class RtpController {
153 160 logger.info("[第三方服务对接->关闭收流] stream->{}", stream);
154 161 MediaServerItem mediaServerItem = mediaServerService.getDefaultMediaServer();
155 162 zlmServerFactory.closeRtpServer(mediaServerItem,stream);
  163 + zlmServerFactory.closeRtpServer(mediaServerItem,stream + "_a");
156 164 String receiveKey = VideoManagerConstants.WVP_OTHER_RECEIVE_RTP_INFO + userSetting.getServerId() + "_*_" + stream;
157 165 List<Object> scan = RedisUtil.scan(redisTemplate, receiveKey);
158 166 if (scan.size() > 0) {
... ... @@ -167,20 +175,51 @@ public class RtpController {
167 175 @ResponseBody
168 176 @Operation(summary = "发送流")
169 177 @Parameter(name = "ssrc", description = "发送流的SSRC", required = true)
170   - @Parameter(name = "ip", description = "目标IP", required = true)
171   - @Parameter(name = "port", description = "目标端口", required = true)
  178 + @Parameter(name = "dstIpForAudio", description = "目标音频收流IP", required = false)
  179 + @Parameter(name = "dstIpForVideo", description = "目标视频收流IP", required = false)
  180 + @Parameter(name = "dstPortForAudio", description = "目标音频收流端口", required = false)
  181 + @Parameter(name = "dstPortForVideo", description = "目标视频收流端口", required = false)
172 182 @Parameter(name = "app", description = "待发送应用名", required = true)
173 183 @Parameter(name = "stream", description = "待发送流Id", required = true)
174 184 @Parameter(name = "callId", description = "整个过程的唯一标识,不传则使用随机端口发流", required = true)
175   - @Parameter(name = "onlyAudio", description = "是否只有音频", required = true)
176 185 @Parameter(name = "isUdp", description = "是否为UDP", required = true)
177   - @Parameter(name = "streamType", description = "流类型,1为es流,2为ps流, 默认es流", required = false)
178   - @Parameter(name = "pt", description = "rtp的pt", required = true)
179   - public void sendRTP(String ssrc, String ip, Integer port, String app, String stream, String callId, Boolean onlyAudio, Boolean isUdp, @RequestParam(required = false)Integer streamType, Integer pt) {
180   - logger.info("[第三方服务对接->发送流] ssrc->{}, ip->{}, port->{}, app->{}, stream->{}, callId->{}, onlyAudio->{}, streamType->{}, pt->{}",
181   - ssrc, ip, port, app, stream, callId, onlyAudio, streamType == 1? "ES":"PS", pt);
182   - if (ObjectUtils.isEmpty(streamType)) {
183   - streamType = 1;
  186 + @Parameter(name = "ptForAudio", description = "rtp的音频pt", required = false)
  187 + @Parameter(name = "ptForVideo", description = "rtp的视频pt", required = false)
  188 + public void sendRTP(String ssrc,
  189 + @RequestParam(required = false)String dstIpForAudio,
  190 + @RequestParam(required = false)String dstIpForVideo,
  191 + @RequestParam(required = false)Integer dstPortForAudio,
  192 + @RequestParam(required = false)Integer dstPortForVideo,
  193 + String app,
  194 + String stream,
  195 + String callId,
  196 + Boolean isUdp,
  197 + @RequestParam(required = false)Integer ptForAudio,
  198 + @RequestParam(required = false)Integer ptForVideo
  199 + ) {
  200 + logger.info("[第三方服务对接->发送流] " +
  201 + "ssrc->{}, \r\n" +
  202 + "dstIpForAudio->{}, \n" +
  203 + "dstIpForAudio->{}, \n" +
  204 + "dstPortForAudio->{}, \n" +
  205 + "dstPortForVideo->{}, \n" +
  206 + "app->{}, \n" +
  207 + "stream->{}, \n" +
  208 + "callId->{}, \n" +
  209 + "ptForAudio->{}, \n" +
  210 + "ptForVideo->{}",
  211 + ssrc,
  212 + dstIpForAudio,
  213 + dstIpForVideo,
  214 + dstPortForAudio,
  215 + dstPortForVideo,
  216 + app,
  217 + stream,
  218 + callId,
  219 + ptForAudio,
  220 + ptForVideo);
  221 + if (!((dstPortForAudio > 0 && !ObjectUtils.isEmpty(dstPortForAudio) || (dstPortForVideo > 0 && !ObjectUtils.isEmpty(dstIpForVideo))))) {
  222 + throw new ControllerException(ErrorCode.ERROR400.getCode(), "至少应该存在一组音频或视频发送参数");
184 223 }
185 224 MediaServerItem mediaServerItem = mediaServerService.getDefaultMediaServer();
186 225 String key = VideoManagerConstants.WVP_OTHER_SEND_RTP_INFO + userSetting.getServerId() + "_" + callId;
... ... @@ -192,32 +231,74 @@ public class RtpController {
192 231 sendInfo.setPushStream(stream);
193 232 sendInfo.setPushSSRC(ssrc);
194 233  
195   - Map<String, Object> param = new HashMap<>(12);
196   - param.put("vhost","__defaultVhost__");
197   - param.put("app",app);
198   - param.put("stream",stream);
199   - param.put("ssrc", ssrc);
  234 + Map<String, Object> paramForAudio;
  235 + Map<String, Object> paramForVideo;
  236 + if (!ObjectUtils.isEmpty(dstIpForAudio) && dstPortForAudio > 0) {
  237 + paramForAudio = new HashMap<>();
  238 + paramForAudio.put("vhost","__defaultVhost__");
  239 + paramForAudio.put("app",app);
  240 + paramForAudio.put("stream",stream);
  241 + paramForAudio.put("ssrc", ssrc);
  242 +
  243 + paramForAudio.put("dst_url", dstIpForAudio);
  244 + paramForAudio.put("dst_port", dstPortForAudio);
  245 + String is_Udp = isUdp ? "1" : "0";
  246 + paramForAudio.put("is_udp", is_Udp);
  247 + paramForAudio.put("src_port", sendInfo.getSendLocalPortForAudio());
  248 + paramForAudio.put("use_ps", "0");
  249 + paramForAudio.put("only_audio", "1");
  250 + if (ptForAudio != null) {
  251 + paramForAudio.put("pt", ptForAudio);
  252 + }
  253 +
  254 + } else {
  255 + paramForAudio = null;
  256 + }
  257 + if (!ObjectUtils.isEmpty(dstIpForVideo) && dstPortForVideo > 0) {
  258 + paramForVideo = new HashMap<>();
  259 + paramForVideo.put("vhost","__defaultVhost__");
  260 + paramForVideo.put("app",app);
  261 + paramForVideo.put("stream",stream);
  262 + paramForVideo.put("ssrc", ssrc);
  263 +
  264 + paramForVideo.put("dst_url", dstIpForVideo);
  265 + paramForVideo.put("dst_port", dstPortForVideo);
  266 + String is_Udp = isUdp ? "1" : "0";
  267 + paramForVideo.put("is_udp", is_Udp);
  268 + paramForVideo.put("src_port", sendInfo.getSendLocalPortForVideo());
  269 + paramForVideo.put("use_ps", "0");
  270 + paramForVideo.put("only_audio", "0");
  271 + if (ptForVideo != null) {
  272 + paramForVideo.put("pt", ptForVideo);
  273 + }
200 274  
201   - param.put("dst_url",ip);
202   - param.put("dst_port", port);
203   - String is_Udp = isUdp ? "1" : "0";
204   - param.put("is_udp", is_Udp);
205   - param.put("src_port", sendInfo.getPort());
206   - param.put("use_ps", streamType==2 ? "1" : "0");
207   - param.put("only_audio", onlyAudio ? "1" : "0");
208   - param.put("pt", pt);
  275 + } else {
  276 + paramForVideo = null;
  277 + }
209 278  
210 279 Boolean streamReady = zlmServerFactory.isStreamReady(mediaServerItem, app, stream);
211 280 if (streamReady) {
212   - logger.info("[第三方服务对接->发送流] 流存在,开始发流,callId->{}", callId);
213   - JSONObject jsonObject = zlmServerFactory.startSendRtpStream(mediaServerItem, param);
214   - if (jsonObject.getInteger("code") == 0) {
215   - logger.info("[第三方服务对接->发送流] 发流成功,callId->{}", callId);
216   - redisTemplate.opsForValue().set(key, sendInfo);
217   - }else {
218   - redisTemplate.delete(key);
219   - logger.info("[第三方服务对接->发送流] 发流失败,callId->{}, {}", callId, jsonObject.getString("msg"));
220   - throw new ControllerException(ErrorCode.ERROR100.getCode(), "[发流失败] " + jsonObject.getString("msg"));
  281 + if (paramForVideo != null) {
  282 + JSONObject jsonObject = zlmServerFactory.startSendRtpStream(mediaServerItem, paramForVideo);
  283 + if (jsonObject.getInteger("code") == 0) {
  284 + logger.info("[第三方服务对接->发送流] 视频流发流成功,callId->{},param->{}", callId, paramForVideo);
  285 + redisTemplate.opsForValue().set(key, sendInfo);
  286 + }else {
  287 + redisTemplate.delete(key);
  288 + logger.info("[第三方服务对接->发送流] 视频流发流失败,callId->{}, {}", callId, jsonObject.getString("msg"));
  289 + throw new ControllerException(ErrorCode.ERROR100.getCode(), "[视频流发流失败] " + jsonObject.getString("msg"));
  290 + }
  291 + }
  292 + if(paramForAudio != null) {
  293 + JSONObject jsonObject = zlmServerFactory.startSendRtpStream(mediaServerItem, paramForAudio);
  294 + if (jsonObject.getInteger("code") == 0) {
  295 + logger.info("[第三方服务对接->发送流] 音频流发流成功,callId->{},param->{}", callId, paramForAudio);
  296 + redisTemplate.opsForValue().set(key, sendInfo);
  297 + }else {
  298 + redisTemplate.delete(key);
  299 + logger.info("[第三方服务对接->发送流] 音频流发流失败,callId->{}, {}", callId, jsonObject.getString("msg"));
  300 + throw new ControllerException(ErrorCode.ERROR100.getCode(), "[音频流发流失败] " + jsonObject.getString("msg"));
  301 + }
221 302 }
222 303 }else {
223 304 logger.info("[第三方服务对接->发送流] 流不存在,等待流上线,callId->{}", callId);
... ... @@ -231,21 +312,39 @@ public class RtpController {
231 312  
232 313 // 订阅 zlm启动事件, 新的zlm也会从这里进入系统
233 314 OtherRtpSendInfo finalSendInfo = sendInfo;
  315 + hookSubscribe.removeSubscribe(hookSubscribeForStreamChange);
234 316 hookSubscribe.addSubscribe(hookSubscribeForStreamChange,
235 317 (mediaServerItemInUse, response)->{
236 318 dynamicTask.stop(uuid);
237   - logger.info("[第三方服务对接->发送流] 流上线,开始发流 callId->{},param->{}", callId, JSONObject.toJSONString(param));
238   - JSONObject jsonObject = zlmServerFactory.startSendRtpStream(mediaServerItem, param);
239   - System.out.println("========发流结果==========");
240   - System.out.println(jsonObject);
241   - if (jsonObject.getInteger("code") == 0) {
242   - logger.info("[第三方服务对接->发送流] 发流成功,callId->{}", callId);
243   - redisTemplate.opsForValue().set(key, finalSendInfo);
244   - }else {
245   - redisTemplate.delete(key);
246   - logger.info("[第三方服务对接->发送流] 发流失败,callId->{}, {}", callId, jsonObject.getString("msg"));
247   - throw new ControllerException(ErrorCode.ERROR100.getCode(), "[发流失败] " + jsonObject.getString("msg"));
  319 + logger.info("[第三方服务对接->发送流] 流上线,开始发流 callId->{}", callId);
  320 + try {
  321 + Thread.sleep(400);
  322 + } catch (InterruptedException e) {
  323 + throw new RuntimeException(e);
  324 + }
  325 + if (paramForVideo != null) {
  326 + JSONObject jsonObject = zlmServerFactory.startSendRtpStream(mediaServerItem, paramForVideo);
  327 + if (jsonObject.getInteger("code") == 0) {
  328 + logger.info("[第三方服务对接->发送流] 视频流发流成功,callId->{},param->{}", callId, paramForVideo);
  329 + redisTemplate.opsForValue().set(key, finalSendInfo);
  330 + }else {
  331 + redisTemplate.delete(key);
  332 + logger.info("[第三方服务对接->发送流] 视频流发流失败,callId->{}, {}", callId, jsonObject.getString("msg"));
  333 + throw new ControllerException(ErrorCode.ERROR100.getCode(), "[视频流发流失败] " + jsonObject.getString("msg"));
  334 + }
  335 + }
  336 + if(paramForAudio != null) {
  337 + JSONObject jsonObject = zlmServerFactory.startSendRtpStream(mediaServerItem, paramForAudio);
  338 + if (jsonObject.getInteger("code") == 0) {
  339 + logger.info("[第三方服务对接->发送流] 音频流发流成功,callId->{},param->{}", callId, paramForAudio);
  340 + redisTemplate.opsForValue().set(key, finalSendInfo);
  341 + }else {
  342 + redisTemplate.delete(key);
  343 + logger.info("[第三方服务对接->发送流] 音频流发流失败,callId->{}, {}", callId, jsonObject.getString("msg"));
  344 + throw new ControllerException(ErrorCode.ERROR100.getCode(), "[音频流发流失败] " + jsonObject.getString("msg"));
  345 + }
248 346 }
  347 + hookSubscribe.removeSubscribe(hookSubscribeForStreamChange);
249 348 });
250 349 }
251 350 }
... ... @@ -274,6 +373,7 @@ public class RtpController {
274 373 }else {
275 374 logger.info("[第三方服务对接->关闭发送流] 成功 callId->{}", callId);
276 375 }
  376 + redisTemplate.delete(key);
277 377 }
278 378  
279 379 }
... ...