Commit df34954c9eee1e870248368dd77a441509ac1dc2

Authored by lawrencehj
1 parent b7857027

实现级联点播RTP推送

src/main/java/com/genersoft/iot/vmp/gb28181/bean/SendRtpItem.java
... ... @@ -22,7 +22,12 @@ public class SendRtpItem {
22 22 */
23 23 private String platformId;
24 24  
25   - /**
  25 + /**
  26 + * 对应设备id
  27 + */
  28 + private String deviceId;
  29 +
  30 + /**
26 31 * 通道id
27 32 */
28 33 private String channelId;
... ... @@ -92,6 +97,14 @@ public class SendRtpItem {
92 97 this.platformId = platformId;
93 98 }
94 99  
  100 + public String getDeviceId() {
  101 + return deviceId;
  102 + }
  103 +
  104 + public void setDeviceId(String deviceId) {
  105 + this.deviceId = deviceId;
  106 + }
  107 +
95 108 public String getChannelId() {
96 109 return channelId;
97 110 }
... ...
src/main/java/com/genersoft/iot/vmp/gb28181/transmit/SIPProcessorFactory.java
... ... @@ -4,25 +4,17 @@ import javax.sip.RequestEvent;
4 4 import javax.sip.ResponseEvent;
5 5 import javax.sip.SipProvider;
6 6 import javax.sip.header.CSeqHeader;
7   -import javax.sip.header.CallIdHeader;
8   -import javax.sip.header.Header;
9 7 import javax.sip.message.Request;
10 8 import javax.sip.message.Response;
11 9  
12 10 import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommanderFroPlatform;
13 11 import com.genersoft.iot.vmp.media.zlm.ZLMRTPServerFactory;
14 12 import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
15   -import com.alibaba.fastjson.JSON;
16 13 import com.genersoft.iot.vmp.gb28181.transmit.response.impl.*;
17   -import com.genersoft.iot.vmp.gb28181.transmit.response.impl.*;
18   -import com.genersoft.iot.vmp.gb28181.event.SipSubscribe;
19 14 import com.genersoft.iot.vmp.vmanager.service.IPlayService;
20   -import org.slf4j.Logger;
21   -import org.slf4j.LoggerFactory;
22 15 // import org.slf4j.Logger;
23 16 // import org.slf4j.LoggerFactory;
24 17 import org.springframework.beans.factory.annotation.Autowired;
25   -import org.springframework.beans.factory.annotation.Qualifier;
26 18 import org.springframework.context.annotation.Lazy;
27 19 import org.springframework.stereotype.Component;
28 20  
... ... @@ -59,7 +51,7 @@ import com.genersoft.iot.vmp.utils.redis.RedisUtil;
59 51 @Component
60 52 public class SIPProcessorFactory {
61 53  
62   - private final static Logger logger = LoggerFactory.getLogger(SIPProcessorFactory.class);
  54 + // private final static Logger logger = LoggerFactory.getLogger(SIPProcessorFactory.class);
63 55  
64 56 @Autowired
65 57 private SipConfig sipConfig;
... ... @@ -156,6 +148,8 @@ public class SIPProcessorFactory {
156 148 } else if (Request.ACK.equals(method)) {
157 149 AckRequestProcessor processor = new AckRequestProcessor();
158 150 processor.setRequestEvent(evt);
  151 + processor.setRedisCatchStorage(redisCatchStorage);
  152 + processor.setZlmrtpServerFactory(zlmrtpServerFactory);
159 153 return processor;
160 154 } else if (Request.BYE.equals(method)) {
161 155 ByeRequestProcessor processor = new ByeRequestProcessor();
... ...
src/main/java/com/genersoft/iot/vmp/gb28181/transmit/request/impl/AckRequestProcessor.java
1 1 package com.genersoft.iot.vmp.gb28181.transmit.request.impl;
2 2  
  3 +import java.util.HashMap;
  4 +import java.util.Map;
  5 +
3 6 import javax.sip.*;
4   -import javax.sip.message.Request;
  7 +//import javax.sip.message.Request;
5 8  
  9 +import com.genersoft.iot.vmp.common.StreamInfo;
  10 +import com.genersoft.iot.vmp.gb28181.bean.SendRtpItem;
6 11 import com.genersoft.iot.vmp.gb28181.transmit.request.SIPRequestAbstractProcessor;
  12 +import com.genersoft.iot.vmp.media.zlm.ZLMRTPServerFactory;
  13 +import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
7 14  
8   -import gov.nist.javax.sip.header.CSeq;
  15 +import org.springframework.stereotype.Component;
9 16  
10 17 /**
11 18 * @Description:ACK请求处理器
12 19 * @author: swwheihei
13 20 * @date: 2020年5月3日 下午5:31:45
14 21 */
  22 +@Component
15 23 public class AckRequestProcessor extends SIPRequestAbstractProcessor {
16   -
  24 +
  25 + //@Autowired
  26 + private IRedisCatchStorage redisCatchStorage;
  27 +
  28 + //@Autowired
  29 + private ZLMRTPServerFactory zlmrtpServerFactory;
  30 +
  31 +
17 32 /**
18 33 * 处理 ACK请求
19 34 *
... ... @@ -21,25 +36,77 @@ public class AckRequestProcessor extends SIPRequestAbstractProcessor {
21 36 */
22 37 @Override
23 38 public void process(RequestEvent evt) {
24   - Request request = evt.getRequest();
  39 + //Request request = evt.getRequest();
25 40 Dialog dialog = evt.getDialog();
26   - DialogState state = dialog.getState();
27 41 if (dialog == null) return;
28   - if (request.getMethod().equals(Request.INVITE) && dialog.getState()== DialogState.CONFIRMED) {
29   - // TODO 查询并开始推流
30   - }
31   - try {
32   - Request ackRequest = null;
33   - CSeq csReq = (CSeq) request.getHeader(CSeq.NAME);
34   - ackRequest = dialog.createAck(csReq.getSeqNumber());
35   - dialog.sendAck(ackRequest);
36   - System.out.println("send ack to callee:" + ackRequest.toString());
37   - } catch (SipException e) {
38   - e.printStackTrace();
39   - } catch (InvalidArgumentException e) {
40   - e.printStackTrace();
  42 + //DialogState state = dialog.getState();
  43 + if (/*request.getMethod().equals(Request.INVITE) &&*/ dialog.getState()== DialogState.CONFIRMED) {
  44 + String remoteUri = dialog.getRemoteParty().getURI().toString();
  45 + String localUri = dialog.getLocalParty().getURI().toString();
  46 + String platformGbId = remoteUri.substring(remoteUri.indexOf(":") + 1, remoteUri.indexOf("@"));
  47 + String channelId = localUri.substring(remoteUri.indexOf(":") + 1, remoteUri.indexOf("@"));
  48 + SendRtpItem sendRtpItem = redisCatchStorage.querySendRTPServer(platformGbId, channelId);
  49 + String is_Udp = sendRtpItem.isTcp() ? "0" : "1";
  50 + String deviceId = sendRtpItem.getDeviceId();
  51 + StreamInfo streamInfo = redisCatchStorage.queryPlayByDevice(deviceId, channelId);
  52 + System.out.println(platformGbId);
  53 + System.out.println(channelId);
  54 + Map<String, Object> param = new HashMap<>();
  55 + param.put("vhost","__defaultVhost__");
  56 + param.put("app","rtp");
  57 + param.put("stream",streamInfo.getStreamId());
  58 + param.put("ssrc", sendRtpItem.getSsrc());
  59 + param.put("dst_url",sendRtpItem.getIp());
  60 + param.put("dst_port", sendRtpItem.getPort());
  61 + param.put("is_udp", is_Udp);
  62 + //param.put ("src_port", sendRtpItem.getLocalPort());
  63 + // 设备推流查询,成功后才能转推
  64 + boolean rtpPushed = false;
  65 + long startTime = System.currentTimeMillis();
  66 + while (!rtpPushed) {
  67 + try {
  68 + if (System.currentTimeMillis() - startTime < 30 * 1000) {
  69 + if (zlmrtpServerFactory.isRtpReady(streamInfo.getStreamId())) {
  70 + rtpPushed = true;
  71 + zlmrtpServerFactory.startSendRtpStream(param);
  72 + } else {
  73 + Thread.sleep(2000);
  74 + continue;
  75 + }
  76 + }
  77 + } catch (InterruptedException e) {
  78 + e.printStackTrace();
  79 + }
  80 + }
41 81 }
  82 + // try {
  83 + // Request ackRequest = null;
  84 + // CSeq csReq = (CSeq) request.getHeader(CSeq.NAME);
  85 + // ackRequest = dialog.createAck(csReq.getSeqNumber());
  86 + // dialog.sendAck(ackRequest);
  87 + // System.out.println("send ack to callee:" + ackRequest.toString());
  88 + // } catch (SipException e) {
  89 + // e.printStackTrace();
  90 + // } catch (InvalidArgumentException e) {
  91 + // e.printStackTrace();
  92 + // }
42 93  
43 94 }
44 95  
  96 + public IRedisCatchStorage getRedisCatchStorage() {
  97 + return redisCatchStorage;
  98 + }
  99 +
  100 + public void setRedisCatchStorage(IRedisCatchStorage redisCatchStorage) {
  101 + this.redisCatchStorage = redisCatchStorage;
  102 + }
  103 +
  104 + public ZLMRTPServerFactory getZlmrtpServerFactory() {
  105 + return zlmrtpServerFactory;
  106 + }
  107 +
  108 + public void setZlmrtpServerFactory(ZLMRTPServerFactory zlmrtpServerFactory) {
  109 + this.zlmrtpServerFactory = zlmrtpServerFactory;
  110 + }
  111 +
45 112 }
... ...
src/main/java/com/genersoft/iot/vmp/gb28181/transmit/request/impl/InviteRequestProcessor.java
... ... @@ -7,14 +7,10 @@ import javax.sip.SipException;
7 7 import javax.sip.SipFactory;
8 8 import javax.sip.address.Address;
9 9 import javax.sip.address.SipURI;
10   -import javax.sip.header.ContentTypeHeader;
11   -import javax.sip.header.FromHeader;
12   -import javax.sip.header.HeaderFactory;
13   -import javax.sip.header.SubjectHeader;
  10 +import javax.sip.header.*;
14 11 import javax.sip.message.Request;
15 12 import javax.sip.message.Response;
16 13  
17   -import com.alibaba.fastjson.JSONObject;
18 14 import com.genersoft.iot.vmp.conf.MediaServerConfig;
19 15 import com.genersoft.iot.vmp.gb28181.bean.Device;
20 16 import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel;
... ... @@ -27,17 +23,12 @@ import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
27 23 import com.genersoft.iot.vmp.storager.IVideoManagerStorager;
28 24 import com.genersoft.iot.vmp.vmanager.play.bean.PlayResult;
29 25 import com.genersoft.iot.vmp.vmanager.service.IPlayService;
30   -import gov.nist.javax.sdp.fields.SDPFormat;
31 26 import gov.nist.javax.sip.address.AddressImpl;
32 27 import gov.nist.javax.sip.address.SipUri;
33 28 import org.slf4j.Logger;
34 29 import org.slf4j.LoggerFactory;
35   -import org.springframework.beans.factory.annotation.Autowired;
36 30  
37   -import java.io.IOException;
38 31 import java.text.ParseException;
39   -import java.util.List;
40   -import java.util.UUID;
41 32 import java.util.Vector;
42 33  
43 34 /**
... ... @@ -45,6 +36,7 @@ import java.util.Vector;
45 36 * @author: panll
46 37 * @date: 2021年1月14日
47 38 */
  39 +@SuppressWarnings("rawtypes")
48 40 public class InviteRequestProcessor extends SIPRequestAbstractProcessor {
49 41  
50 42 private final static Logger logger = LoggerFactory.getLogger(MessageRequestProcessor.class);
... ... @@ -122,8 +114,10 @@ public class InviteRequestProcessor extends SIPRequestAbstractProcessor {
122 114  
123 115 // jainSip不支持y=字段, 移除移除以解析。
124 116 int ssrcIndex = contentString.indexOf("y=");
125   - String ssrc = contentString.substring(ssrcIndex + 2, contentString.length())
126   - .replace("\r\n", "").replace("\n", "");
  117 + String ssrc = contentString.substring(ssrcIndex + 2, ssrcIndex + 12);
  118 + //ssrc规定长度为10字节,不取余下长度以避免后续还有“f=”字段
  119 + // String ssrc = contentString.substring(ssrcIndex + 2, contentString.length())
  120 + // .replace("\r\n", "").replace("\n", "");
127 121  
128 122 String substring = contentString.substring(0, contentString.indexOf("y="));
129 123 SessionDescription sdp = SdpFactory.getInstance().createSessionDescription(substring);
... ... @@ -131,9 +125,9 @@ public class InviteRequestProcessor extends SIPRequestAbstractProcessor {
131 125 // 获取支持的格式
132 126 Vector mediaDescriptions = sdp.getMediaDescriptions(true);
133 127 // 查看是否支持PS 负载96
134   - String ip = null;
  128 + //String ip = null;
135 129 int port = -1;
136   - boolean recvonly = false;
  130 + //boolean recvonly = false;
137 131 boolean mediaTransmissionTCP = false;
138 132 Boolean tcpActive = null;
139 133 for (int i = 0; i < mediaDescriptions.size(); i++) {
... ... @@ -143,7 +137,7 @@ public class InviteRequestProcessor extends SIPRequestAbstractProcessor {
143 137 Vector mediaFormats = media.getMediaFormats(false);
144 138 if (mediaFormats.contains("96")) {
145 139 port = media.getMediaPort();
146   - String mediaType = media.getMediaType();
  140 + //String mediaType = media.getMediaType();
147 141 String protocol = media.getProtocol();
148 142  
149 143 // 区分TCP发流还是udp, 当前默认udp
... ... @@ -169,7 +163,7 @@ public class InviteRequestProcessor extends SIPRequestAbstractProcessor {
169 163 }
170 164 String username = sdp.getOrigin().getUsername();
171 165 String addressStr = sdp.getOrigin().getAddress();
172   - String sessionName = sdp.getSessionName().getValue();
  166 + //String sessionName = sdp.getSessionName().getValue();
173 167 logger.info("[上级点播]用户:{}, 地址:{}:{}, ssrc:{}", username, addressStr, port, ssrc);
174 168  
175 169 Device device = storager.queryVideoDeviceByPlatformIdAndChannelId(platformId, channelId);
... ... @@ -178,7 +172,7 @@ public class InviteRequestProcessor extends SIPRequestAbstractProcessor {
178 172 responseAck(evt, Response.SERVER_INTERNAL_ERROR);
179 173 return;
180 174 }
181   - SendRtpItem sendRtpItem = zlmrtpServerFactory.createSendRtpItem(ip, port, platformId, ssrc, channelId,
  175 + SendRtpItem sendRtpItem = zlmrtpServerFactory.createSendRtpItem(addressStr, port, ssrc, platformId, device.getDeviceId(), channelId,
182 176 mediaTransmissionTCP);
183 177 if (tcpActive != null) {
184 178 sendRtpItem.setTcpActive(tcpActive);
... ...
src/main/java/com/genersoft/iot/vmp/gb28181/transmit/request/impl/MessageRequestProcessor.java
... ... @@ -370,31 +370,36 @@ public class MessageRequestProcessor extends SIPRequestAbstractProcessor {
370 370 private void processMessageDeviceInfo(RequestEvent evt) {
371 371 try {
372 372 Element rootElement = getRootElement(evt);
  373 + String requestName = rootElement.getName();
373 374 Element deviceIdElement = rootElement.element("DeviceID");
374 375 String deviceId = deviceIdElement.getTextTrim().toString();
375   -
376   - Device device = storager.queryVideoDevice(deviceId);
377   - if (device == null) {
378   - return;
379   - }
380   - device.setName(XmlUtil.getText(rootElement, "DeviceName"));
381   - device.setManufacturer(XmlUtil.getText(rootElement, "Manufacturer"));
382   - device.setModel(XmlUtil.getText(rootElement, "Model"));
383   - device.setFirmware(XmlUtil.getText(rootElement, "Firmware"));
384   - if (StringUtils.isEmpty(device.getStreamMode())) {
385   - device.setStreamMode("UDP");
386   - }
387   - storager.updateDevice(device);
388   -
389   - RequestMessage msg = new RequestMessage();
390   - msg.setDeviceId(deviceId);
391   - msg.setType(DeferredResultHolder.CALLBACK_CMD_DEVICEINFO);
392   - msg.setData(device);
393   - deferredResultHolder.invokeResult(msg);
394   - // 回复200 OK
395   - responseAck(evt);
396   - if (offLineDetector.isOnline(deviceId)) {
397   - publisher.onlineEventPublish(deviceId, VideoManagerConstants.EVENT_ONLINE_KEEPLIVE);
  376 + if (requestName.equals("Query")) {
  377 + // 回复200 OK
  378 + responseAck(evt);
  379 + } else {
  380 + Device device = storager.queryVideoDevice(deviceId);
  381 + if (device == null) {
  382 + return;
  383 + }
  384 + device.setName(XmlUtil.getText(rootElement, "DeviceName"));
  385 + device.setManufacturer(XmlUtil.getText(rootElement, "Manufacturer"));
  386 + device.setModel(XmlUtil.getText(rootElement, "Model"));
  387 + device.setFirmware(XmlUtil.getText(rootElement, "Firmware"));
  388 + if (StringUtils.isEmpty(device.getStreamMode())) {
  389 + device.setStreamMode("UDP");
  390 + }
  391 + storager.updateDevice(device);
  392 +
  393 + RequestMessage msg = new RequestMessage();
  394 + msg.setDeviceId(deviceId);
  395 + msg.setType(DeferredResultHolder.CALLBACK_CMD_DEVICEINFO);
  396 + msg.setData(device);
  397 + deferredResultHolder.invokeResult(msg);
  398 + // 回复200 OK
  399 + responseAck(evt);
  400 + if (offLineDetector.isOnline(deviceId)) {
  401 + publisher.onlineEventPublish(deviceId, VideoManagerConstants.EVENT_ONLINE_KEEPLIVE);
  402 + }
398 403 }
399 404 } catch (DocumentException | SipException | InvalidArgumentException | ParseException e) {
400 405 e.printStackTrace();
... ...
src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRESTfulUtils.java
... ... @@ -119,4 +119,8 @@ public class ZLMRESTfulUtils {
119 119 public JSONObject closeRtpServer(Map<String, Object> param) {
120 120 return sendPost("closeRtpServer",param);
121 121 }
  122 +
  123 + public JSONObject startSendRtp(Map<String, Object> param) {
  124 + return sendPost("startSendRtp",param);
  125 + }
122 126 }
... ...
src/main/java/com/genersoft/iot/vmp/media/zlm/ZLMRTPServerFactory.java
... ... @@ -105,7 +105,7 @@ public class ZLMRTPServerFactory {
105 105 * @param tcp 是否为tcp
106 106 * @return SendRtpItem
107 107 */
108   - public SendRtpItem createSendRtpItem(String ip, int port, String ssrc, String platformId, String channelId, boolean tcp){
  108 + public SendRtpItem createSendRtpItem(String ip, int port, String ssrc, String platformId, String deviceId, String channelId, boolean tcp){
109 109 String playSsrc = SsrcUtil.getPlaySsrc();
110 110 int localPort = createRTPServer(SsrcUtil.getPlaySsrc());
111 111 if (localPort != -1) {
... ... @@ -119,9 +119,54 @@ public class ZLMRTPServerFactory {
119 119 sendRtpItem.setPort(port);
120 120 sendRtpItem.setSsrc(ssrc);
121 121 sendRtpItem.setPlatformId(platformId);
  122 + sendRtpItem.setDeviceId(deviceId);
122 123 sendRtpItem.setChannelId(channelId);
123 124 sendRtpItem.setTcp(tcp);
124 125 sendRtpItem.setLocalPort(localPort);
125 126 return sendRtpItem;
126 127 }
  128 +
  129 + /**
  130 + *
  131 + */
  132 + public Boolean startSendRtpStream(Map<String, Object>param) {
  133 + Boolean result = false;
  134 + JSONObject jsonObject = zlmresTfulUtils.startSendRtp(param);
  135 + System.out.println(jsonObject);
  136 + if (jsonObject != null) {
  137 + switch (jsonObject.getInteger("code")){
  138 + case 0:
  139 + result= true;
  140 + logger.error("RTP推流请求成功,本地推流端口:" + jsonObject.getString("local_port"));
  141 + break;
  142 + // case -300: // id已经存在
  143 + // result = false;
  144 + // break;
  145 + // case -400: // 端口占用
  146 + // result= false;
  147 + // break;
  148 + default:
  149 + logger.error("RTP推流失败: " + jsonObject.getString("msg"));
  150 + break;
  151 + }
  152 + }else {
  153 + // 检查ZLM状态
  154 + logger.error("RTP推流失败: 请检查ZLM服务");
  155 + }
  156 + return result;
  157 + }
  158 +
  159 + /**
  160 + *
  161 + */
  162 + public Boolean isRtpReady(String streamId) {
  163 + JSONObject mediaInfo = zlmresTfulUtils.getMediaInfo("rtp", "rtmp", streamId);
  164 + if (mediaInfo.getInteger("code") == 0 && mediaInfo.getBoolean("online")) {
  165 + logger.info("设备RTP推流成功");
  166 + return true;
  167 + } else {
  168 + logger.info("设备RTP推流未完成");
  169 + return false;
  170 + }
  171 + }
127 172 }
... ...
src/main/java/com/genersoft/iot/vmp/storager/IRedisCatchStorage.java
... ... @@ -4,7 +4,6 @@ import com.genersoft.iot.vmp.common.StreamInfo;
4 4 import com.genersoft.iot.vmp.conf.MediaServerConfig;
5 5 import com.genersoft.iot.vmp.gb28181.bean.ParentPlatform;
6 6 import com.genersoft.iot.vmp.gb28181.bean.ParentPlatformCatch;
7   -import com.genersoft.iot.vmp.gb28181.bean.PlatformRegister;
8 7 import com.genersoft.iot.vmp.gb28181.bean.SendRtpItem;
9 8  
10 9 import java.util.Map;
... ... @@ -81,4 +80,13 @@ public interface IRedisCatchStorage {
81 80 void delPlatformRegisterInfo(String callId);
82 81  
83 82 void updateSendRTPSever(SendRtpItem sendRtpItem);
  83 +
  84 + /**
  85 + * 查询RTP推送信息缓存
  86 + * @param platformGbId
  87 + * @param channelId
  88 + * @return sendRtpItem
  89 + */
  90 + SendRtpItem querySendRTPServer(String platformGbId, String channelId);
  91 +
84 92 }
... ...
src/main/java/com/genersoft/iot/vmp/storager/impl/RedisCatchStorageImpl.java
... ... @@ -218,4 +218,11 @@ public class RedisCatchStorageImpl implements IRedisCatchStorage {
218 218 String key = VideoManagerConstants.PLATFORM_SEND_RTP_INFO_PREFIX + sendRtpItem.getPlatformId() + "_" + sendRtpItem.getChannelId();
219 219 redis.set(key, sendRtpItem);
220 220 }
  221 +
  222 + @Override
  223 + public SendRtpItem querySendRTPServer(String platformGbId, String channelId) {
  224 + String key = VideoManagerConstants.PLATFORM_SEND_RTP_INFO_PREFIX + platformGbId + "_" + channelId;
  225 + return (SendRtpItem)redis.get(key);
  226 + }
  227 +
221 228 }
... ...