Commit b55374c75a17c87b9ccb8d5c12abe188875af256

Authored by 648540858
1 parent 3ec3b884

支持使用udp端口段来启用高性能udp

src/main/java/com/genersoft/iot/vmp/gb28181/transmit/SIPProcessorFactory.java
... ... @@ -93,7 +93,7 @@ public class SIPProcessorFactory {
93 93 public ISIPRequestProcessor createRequestProcessor(RequestEvent evt) {
94 94 Request request = evt.getRequest();
95 95 String method = request.getMethod();
96   - logger.info("接收到消息:"+request.getMethod());
  96 +// logger.info("接收到消息:"+request.getMethod());
97 97 if (Request.INVITE.equals(method)) {
98 98 InviteRequestProcessor processor = new InviteRequestProcessor();
99 99 processor.setRequestEvent(evt);
... ...
src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommander.java
... ... @@ -19,6 +19,7 @@ import com.alibaba.fastjson.JSONObject;
19 19 import com.genersoft.iot.vmp.common.StreamInfo;
20 20 import com.genersoft.iot.vmp.conf.MediaServerConfig;
21 21 import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel;
  22 +import com.genersoft.iot.vmp.media.zlm.ZLMUtils;
22 23 import com.genersoft.iot.vmp.storager.IVideoManagerStorager;
23 24 import org.springframework.beans.factory.annotation.Autowired;
24 25 import org.springframework.beans.factory.annotation.Qualifier;
... ... @@ -60,6 +61,13 @@ public class SIPCommander implements ISIPCommander {
60 61 @Qualifier(value="udpSipProvider")
61 62 private SipProvider udpSipProvider;
62 63  
  64 + @Autowired
  65 + private ZLMUtils zlmUtils;
  66 +
  67 + @Value("${media.rtp.enable}")
  68 + private boolean rtpEnable;
  69 +
  70 +
63 71  
64 72 /**
65 73 * 云台方向放控制,使用配置文件中的默认镜头移动速度
... ... @@ -202,10 +210,17 @@ public class SIPCommander implements ISIPCommander {
202 210 @Override
203 211 public StreamInfo playStreamCmd(Device device, String channelId) {
204 212 try {
205   -
  213 +
206 214 String ssrc = streamSession.createPlaySsrc();
207 215 String transport = device.getTransport();
208 216 MediaServerConfig mediaInfo = storager.getMediaInfo();
  217 + String mediaPort = null;
  218 + // 使用动态udp端口
  219 + if (rtpEnable) {
  220 + mediaPort = zlmUtils.getNewRTPPort(ssrc) + "";
  221 + }else {
  222 + mediaPort = mediaInfo.getRtpProxyPort();
  223 + }
209 224 //
210 225 StringBuffer content = new StringBuffer(200);
211 226 content.append("v=0\r\n");
... ... @@ -214,10 +229,10 @@ public class SIPCommander implements ISIPCommander {
214 229 content.append("c=IN IP4 "+mediaInfo.getLocalIP()+"\r\n");
215 230 content.append("t=0 0\r\n");
216 231 if("TCP".equals(transport)) {
217   - content.append("m=video "+mediaInfo.getRtpProxyPort()+" TCP/RTP/AVP 96 98 97\r\n");
  232 + content.append("m=video "+ mediaPort +" TCP/RTP/AVP 96 98 97\r\n");
218 233 }
219 234 if("UDP".equals(transport)) {
220   - content.append("m=video "+mediaInfo.getRtpProxyPort()+" RTP/AVP 96 98 97\r\n");
  235 + content.append("m=video "+ mediaPort +" RTP/AVP 96 98 97\r\n");
221 236 }
222 237 content.append("a=recvonly\r\n");
223 238 content.append("a=rtpmap:96 PS/90000\r\n");
... ... @@ -575,4 +590,5 @@ public class SIPCommander implements ISIPCommander {
575 590 return clientTransaction;
576 591 }
577 592  
  593 +
578 594 }
... ...
src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRESTfulUtils.java
... ... @@ -83,4 +83,8 @@ public class ZLMRESTfulUtils {
83 83 public JSONObject setServerConfig(Map<String, Object> param){
84 84 return sendPost("setServerConfig",param);
85 85 }
  86 +
  87 + public JSONObject openRtpServer(Map<String, Object> param){
  88 + return sendPost("openRtpServer",param);
  89 + }
86 90 }
... ...
src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMUtils.java 0 → 100644
  1 +package com.genersoft.iot.vmp.media.zlm;
  2 +
  3 +import com.alibaba.fastjson.JSONObject;
  4 +import org.springframework.beans.factory.annotation.Autowired;
  5 +import org.springframework.beans.factory.annotation.Value;
  6 +import org.springframework.stereotype.Component;
  7 +
  8 +import java.util.HashMap;
  9 +import java.util.Map;
  10 +
  11 +@Component
  12 +public class ZLMUtils {
  13 +
  14 + @Value("${media.rtp.udpPortRange}")
  15 + private String udpPortRange;
  16 +
  17 + @Autowired
  18 + private ZLMRESTfulUtils zlmresTfulUtils;
  19 +
  20 + private int[] udpPortRangeArray = new int[2];
  21 +
  22 + private int currentPort = 0;
  23 +
  24 + public int getNewRTPPort(String ssrc) {
  25 + String streamId = String.format("%08x", Integer.parseInt(ssrc)).toUpperCase();
  26 + Map<String, Object> param = new HashMap<>();
  27 + int newPort = getPortFromUdpPortRange();
  28 + param.put("port", newPort);
  29 + param.put("enable_tcp", 0);
  30 + param.put("stream_id", streamId);
  31 + JSONObject jsonObject = zlmresTfulUtils.openRtpServer(param);
  32 + if (jsonObject.getInteger("code") == 0) {
  33 + System.out.println(11111111);
  34 + System.out.println(streamId);
  35 + System.out.println(ssrc);
  36 + System.out.println(newPort);
  37 + System.out.println(jsonObject.toJSONString());
  38 + return newPort;
  39 + }else {
  40 + return getNewRTPPort(streamId);
  41 + }
  42 + }
  43 +
  44 + private int getPortFromUdpPortRange() {
  45 + if (currentPort == 0) {
  46 + String[] udpPortRangeStrArray = udpPortRange.split(",");
  47 + udpPortRangeArray[0] = Integer.parseInt(udpPortRangeStrArray[0]);
  48 + udpPortRangeArray[1] = Integer.parseInt(udpPortRangeStrArray[1]);
  49 + }
  50 +
  51 + if (currentPort == 0 || currentPort ++ > udpPortRangeArray[1]) {
  52 + currentPort = udpPortRangeArray[0];
  53 + return udpPortRangeArray[0];
  54 + }else {
  55 + return currentPort ++;
  56 + }
  57 + }
  58 +}
... ...
src/main/java/com/genersoft/iot/vmp/vmanager/play/PlayController.java
... ... @@ -7,6 +7,7 @@ import com.genersoft.iot.vmp.media.zlm.ZLMRESTfulUtils;
7 7 import org.slf4j.Logger;
8 8 import org.slf4j.LoggerFactory;
9 9 import org.springframework.beans.factory.annotation.Autowired;
  10 +import org.springframework.beans.factory.annotation.Value;
10 11 import org.springframework.http.HttpStatus;
11 12 import org.springframework.http.ResponseEntity;
12 13 import org.springframework.web.bind.annotation.CrossOrigin;
... ... @@ -69,8 +70,8 @@ public class PlayController {
69 70 return new ResponseEntity<String>("timeout",HttpStatus.OK);
70 71 }else {
71 72 JSONObject rtpInfo = zlmresTfulUtils.getRtpInfo(streamId);
72   - Boolean exist = rtpInfo.getBoolean("exist");
73   - if (rtpInfo == null || !rtpInfo.getBoolean("exist") || streamInfo.getFlv() != null){
  73 + if (rtpInfo == null || !rtpInfo.getBoolean("exist") || storager.queryPlayByDevice(deviceId, channelId).getFlv() == null){
  74 + Thread.sleep(2000);
74 75 continue;
75 76 }else {
76 77 lockFlag = false;
... ... @@ -91,7 +92,6 @@ public class PlayController {
91 92 }
92 93 };
93 94 }
94   - Thread.sleep(200);
95 95 streamInfo = storager.queryPlayByDevice(deviceId, channelId);
96 96 } catch (InterruptedException e) {
97 97 e.printStackTrace();
... ...
src/main/resources/application.yml
... ... @@ -43,5 +43,9 @@ media: #zlm服务器的ip与http端口, 重点: 这是http端口
43 43 ip: 192.168.1.20
44 44 port: 9080
45 45 secret: 035c73f7-bb6b-4889-a715-d9eb2d1925cc
46   - streamNoneReaderDelayMS: 1800000 # 无人观看多久关闭流
  46 + streamNoneReaderDelayMS: 1800000 # 无人观看多久自动关闭流
  47 + rtp: # 启用udp多端口模式
  48 + enable: false
  49 + udpPortRange: 30000,300500 # 端口范围
  50 +
47 51  
... ...