From 2f165d595af3565bd17612ee0f5866ace8ab43f4 Mon Sep 17 00:00:00 2001
From: panlinlin <648540858@qq.com>
Date: 星期二, 02 三月 2021 18:23:06 +0800
Subject: [PATCH] 对级联点播信令进行处理

---
 src/main/java/com/genersoft/iot/vmp/gb28181/transmit/request/impl/InviteRequestProcessor.java |  256 ++++++++++++++++++--------------------------------
 1 files changed, 94 insertions(+), 162 deletions(-)

diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/request/impl/InviteRequestProcessor.java b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/request/impl/InviteRequestProcessor.java
index 7e9e626..ac19d6f 100644
--- a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/request/impl/InviteRequestProcessor.java
+++ b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/request/impl/InviteRequestProcessor.java
@@ -4,9 +4,12 @@
 import javax.sip.InvalidArgumentException;
 import javax.sip.RequestEvent;
 import javax.sip.SipException;
+import javax.sip.SipFactory;
+import javax.sip.address.Address;
 import javax.sip.address.SipURI;
 import javax.sip.header.ContentTypeHeader;
 import javax.sip.header.FromHeader;
+import javax.sip.header.HeaderFactory;
 import javax.sip.header.SubjectHeader;
 import javax.sip.message.Request;
 import javax.sip.message.Response;
@@ -15,6 +18,7 @@
 import com.genersoft.iot.vmp.conf.MediaServerConfig;
 import com.genersoft.iot.vmp.gb28181.bean.Device;
 import com.genersoft.iot.vmp.gb28181.bean.DeviceChannel;
+import com.genersoft.iot.vmp.gb28181.bean.SendRtpItem;
 import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommander;
 import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommanderFroPlatform;
 import com.genersoft.iot.vmp.gb28181.transmit.request.SIPRequestAbstractProcessor;
@@ -100,16 +104,18 @@
 			platformId = uri.getUser();
 
 			if (platformId == null || channelId == null) {
-				response400Ack(evt); // 鍙傛暟涓嶅叏锛� 鍙�400锛岃姹傞敊璇�
+				logger.info("鏃犳硶浠嶧romHeader鐨凙ddress涓幏鍙栧埌骞冲彴id锛岃繑鍥�404");
+				responseAck(evt, Response.BAD_REQUEST); // 鍙傛暟涓嶅叏锛� 鍙�400锛岃姹傞敊璇�
 				return;
 			}
 			// 鏌ヨ骞冲彴涓嬫槸鍚︽湁璇ラ�氶亾
 			DeviceChannel channel = storager.queryChannelInParentPlatform(platformId, channelId);
 			if (channel == null) {
-				response404Ack(evt); // 閫氶亾涓嶅瓨鍦紝鍙�404锛岃祫婧愪笉瀛樺湪
+				logger.info("閫氶亾涓嶅瓨鍦紝杩斿洖404");
+				responseAck(evt, Response.NOT_FOUND); // 閫氶亾涓嶅瓨鍦紝鍙�404锛岃祫婧愪笉瀛樺湪
 				return;
 			}else {
-				response100Ack(evt); // 閫氶亾瀛樺湪锛屽彂100锛宼rying
+				responseAck(evt, Response.TRYING); // 閫氶亾瀛樺湪锛屽彂100锛宼rying
 			}
 			// 瑙f瀽sdp娑堟伅, 浣跨敤jainsip 鑷甫鐨剆dp瑙f瀽鏂瑰紡
 			String contentString = new String(request.getRawContent());
@@ -152,107 +158,79 @@
 							}
 						}
 					}
-//					Vector attributes = mediaDescription.getAttributes(false);
-//					for (Object attributeObj : attributes) {
-//						Attribute attribute = (Attribute)attributeObj;
-//						String name = attribute.getName();
-//						switch (name){
-//							case "recvonly":
-//								recvonly = true;
-//								break;
-//							case "rtpmap":
-//							case "connection":
-//								break;
-//							case "setup":
-//								mediaTransmissionTCP = true;
-//								if ("active".equals(attribute.getValue())) {  // tcp涓诲姩妯″紡
-//									tcpActive = true;
-//								}else if ("passive".equals(attribute.getValue())){ // tcp琚姩妯″紡
-//									tcpActive = false;
-//								}
-//								break;
-//
-//						}
-//						if ("recvonly".equals(name)) {
-//							recvonly = true;
-//						}
-//
-//						String value = attribute.getValue();
-//					}
 					break;
 				}
 			}
 			if (port == -1) {
+				logger.info("涓嶆敮鎸佺殑濯掍綋鏍煎紡锛岃繑鍥�415");
 				// 鍥炲涓嶆敮鎸佺殑鏍煎紡
-				response415Ack(evt); // 涓嶆敮鎸佺殑鏍煎紡锛屽彂415
+				responseAck(evt, Response.UNSUPPORTED_MEDIA_TYPE); // 涓嶆敮鎸佺殑鏍煎紡锛屽彂415
 				return;
 			}
 			String username = sdp.getOrigin().getUsername();
 			String addressStr = sdp.getOrigin().getAddress();
 			String sessionName = sdp.getSessionName().getValue();
 			logger.info("[涓婄骇鐐规挱]鐢ㄦ埛锛歿}锛� 鍦板潃锛歿}:{}锛� ssrc锛歿}", username, addressStr, port, ssrc);
-//
-//			Device device = storager.queryVideoDeviceByPlatformIdAndChannelId(platformId, channelId);
-//			if (device == null) {
-//				logger.warn("鐐规挱骞冲彴{}鐨勯�氶亾{}鏃舵湭鎵惧埌璁惧淇℃伅", platformId, channel);
-//				response500Ack(evt);
-//				return;
-//			}
-//
-//			// 閫氱煡涓嬬骇鎺ㄦ祦锛�
-//			PlayResult playResult = playService.play(device.getDeviceId(), channelId, (responseJSON)->{
-//				// 鏀跺埌鎺ㄦ祦锛� 鍥炲200OK
-//				UUID uuid = UUID.randomUUID();
-//				int rtpServer = zlmrtpServerFactory.createRTPServer(uuid.toString());
-//				if (rtpServer == -1) {
-//					logger.error("涓鸿幏鍙栧埌鍙敤绔彛");
-//					return;
-//				}else {
-//					zlmrtpServerFactory.closeRTPServer(uuid.toString());
-//				}
-//				// TODO 娣诲姞瀵箃cp鐨勬敮鎸�
-//				MediaServerConfig mediaInfo = redisCatchStorage.getMediaInfo();
-//				StringBuffer content = new StringBuffer(200);
-//				content.append("v=0\r\n");
-//				content.append("o="+"00000"+" 0 0 IN IP4 "+mediaInfo.getWanIp()+"\r\n");
-//				content.append("s=Play\r\n");
-//				content.append("c=IN IP4 "+mediaInfo.getWanIp()+"\r\n");
-//				content.append("t=0 0\r\n");
-//				content.append("m=video "+ rtpServer+" RTP/AVP 96\r\n");
-//				content.append("a=sendonly\r\n");
-//				content.append("a=rtpmap:96 PS/90000\r\n");
-//				content.append("y="+ ssrc + "\r\n");
-//				content.append("f=\r\n");
-//
-//				try {
-//					responseAck(evt, content.toString());
-//				} catch (SipException e) {
-//					e.printStackTrace();
-//				} catch (InvalidArgumentException e) {
-//					e.printStackTrace();
-//				} catch (ParseException e) {
-//					e.printStackTrace();
-//				}
-//
-//				// 鍐欏叆redis锛� 瓒呮椂鏃跺洖澶�
-////				redisCatchStorage.waiteAck()
-//			},(event -> {
-//				// 鏈煡閿欒銆傜洿鎺ヨ浆鍙戣澶囩偣鎾殑閿欒
-//				Response response = null;
-//				try {
-//					response = getMessageFactory().createResponse(event.getResponse().getStatusCode(), evt.getRequest());
-//					getServerTransaction(evt).sendResponse(response);
-//
-//				} catch (ParseException | SipException | InvalidArgumentException e) {
-//					e.printStackTrace();
-//				}
-//			}));
-//			playResult.getResult();
-			// 鏌ユ壘鍚堥�傜殑绔彛鎺ㄦ祦锛�
-			// 鏀跺埌ack鍚庤皟鐢ㄦ帹娴佹帴鍙�
 
+			Device device = storager.queryVideoDeviceByPlatformIdAndChannelId(platformId, channelId);
+			if (device == null) {
+				logger.warn("鐐规挱骞冲彴{}鐨勯�氶亾{}鏃舵湭鎵惧埌璁惧淇℃伅", platformId, channel);
+				responseAck(evt, Response.SERVER_INTERNAL_ERROR);
+				return;
+			}
+			SendRtpItem sendRtpItem = zlmrtpServerFactory.createSendRtpItem(ip, port, platformId, ssrc, channelId,
+					mediaTransmissionTCP);
+			if (tcpActive != null) {
+				sendRtpItem.setTcpActive(tcpActive);
+			}
+			if (sendRtpItem == null) {
+				logger.warn("鏈嶅姟鍣ㄧ鍙h祫婧愪笉瓒�");
+				responseAck(evt, Response.BUSY_HERE);
+				return;
+			}
 
+			// 鍐欏叆redis锛� 瓒呮椂鏃跺洖澶�
+			redisCatchStorage.updateSendRTPSever(sendRtpItem);
+			// 閫氱煡涓嬬骇鎺ㄦ祦锛�
+			PlayResult playResult = playService.play(device.getDeviceId(), channelId, (responseJSON)->{
+				// 鏀跺埌鎺ㄦ祦锛� 鍥炲200OK, 绛夊緟ack
+				sendRtpItem.setStatus(1);
+				redisCatchStorage.updateSendRTPSever(sendRtpItem);
+				// TODO 娣诲姞瀵箃cp鐨勬敮鎸�
+				MediaServerConfig mediaInfo = redisCatchStorage.getMediaInfo();
+				StringBuffer content = new StringBuffer(200);
+				content.append("v=0\r\n");
+				content.append("o="+"00000"+" 0 0 IN IP4 "+mediaInfo.getWanIp()+"\r\n");
+				content.append("s=Play\r\n");
+				content.append("c=IN IP4 "+mediaInfo.getWanIp()+"\r\n");
+				content.append("t=0 0\r\n");
+				content.append("m=video "+ sendRtpItem.getLocalPort()+" RTP/AVP 96\r\n");
+				content.append("a=sendonly\r\n");
+				content.append("a=rtpmap:96 PS/90000\r\n");
+				content.append("y="+ ssrc + "\r\n");
+				content.append("f=\r\n");
 
+				try {
+					responseAck(evt, content.toString());
+				} catch (SipException e) {
+					e.printStackTrace();
+				} catch (InvalidArgumentException e) {
+					e.printStackTrace();
+				} catch (ParseException e) {
+					e.printStackTrace();
+				}
+			},(event -> {
+				// 鏈煡閿欒銆傜洿鎺ヨ浆鍙戣澶囩偣鎾殑閿欒
+				Response response = null;
+				try {
+					response = getMessageFactory().createResponse(event.getResponse().getStatusCode(), evt.getRequest());
+					getServerTransaction(evt).sendResponse(response);
+
+				} catch (ParseException | SipException | InvalidArgumentException e) {
+					e.printStackTrace();
+				}
+			}));
+			playResult.getResult();
 
 		} catch (SipException | InvalidArgumentException | ParseException e) {
 			e.printStackTrace();
@@ -263,100 +241,46 @@
 		} catch (SdpException e) {
 			e.printStackTrace();
 		}
-
 	}
 
 	/***
-	 * 鍥炲100 trying
+	 * 鍥炲鐘舵�佺爜
+	 * 100 trying
+	 * 200 OK
+	 * 400
+	 * 404
 	 * @param evt
 	 * @throws SipException
 	 * @throws InvalidArgumentException
 	 * @throws ParseException
 	 */
-	private void response100Ack(RequestEvent evt) throws SipException, InvalidArgumentException, ParseException {
+	private void responseAck(RequestEvent evt, int statusCode) throws SipException, InvalidArgumentException, ParseException {
 		Response response = getMessageFactory().createResponse(Response.TRYING, evt.getRequest());
 		getServerTransaction(evt).sendResponse(response);
 	}
 
-	/***
-	 * 鍥炲200 OK
+	/**
+	 * 鍥炲甯dp鐨�200
 	 * @param evt
+	 * @param sdp
 	 * @throws SipException
 	 * @throws InvalidArgumentException
 	 * @throws ParseException
 	 */
 	private void responseAck(RequestEvent evt, String sdp) throws SipException, InvalidArgumentException, ParseException {
 		Response response = getMessageFactory().createResponse(Response.OK, evt.getRequest());
-		ContentTypeHeader contentTypeHeader = getHeaderFactory().createContentTypeHeader("APPLICATION", "SDP");
+		SipFactory sipFactory = SipFactory.getInstance();
+		ContentTypeHeader contentTypeHeader = sipFactory.createHeaderFactory().createContentTypeHeader("APPLICATION", "SDP");
 		response.setContent(sdp, contentTypeHeader);
+
+		SipURI sipURI = (SipURI)evt.getRequest().getRequestURI();
+
+		Address concatAddress = sipFactory.createAddressFactory().createAddress(
+				sipFactory.createAddressFactory().createSipURI(sipURI.getUser(),  sipURI.getHost()+":"+sipURI.getPort()
+				));
+		response.addHeader(sipFactory.createHeaderFactory().createContactHeader(concatAddress));
 		getServerTransaction(evt).sendResponse(response);
 	}
-
-	/***
-	 * 鍥炲400
-	 * @param evt
-	 * @throws SipException
-	 * @throws InvalidArgumentException
-	 * @throws ParseException
-	 */
-	private void response400Ack(RequestEvent evt) throws SipException, InvalidArgumentException, ParseException {
-		Response response = getMessageFactory().createResponse(Response.BAD_REQUEST, evt.getRequest());
-		getServerTransaction(evt).sendResponse(response);
-	}
-
-	/***
-	 * 鍥炲404
-	 * @param evt
-	 * @throws SipException
-	 * @throws InvalidArgumentException
-	 * @throws ParseException
-	 */
-	private void response404Ack(RequestEvent evt) throws SipException, InvalidArgumentException, ParseException {
-		Response response = getMessageFactory().createResponse(Response.NOT_FOUND, evt.getRequest());
-		getServerTransaction(evt).sendResponse(response);
-	}
-
-	/***
-	 * 鍥炲415 涓嶆敮鎸佺殑濯掍綋绫诲瀷
-	 * @param evt
-	 * @throws SipException
-	 * @throws InvalidArgumentException
-	 * @throws ParseException
-	 */
-	private void response415Ack(RequestEvent evt) throws SipException, InvalidArgumentException, ParseException {
-		Response response = getMessageFactory().createResponse(Response.UNSUPPORTED_MEDIA_TYPE, evt.getRequest());
-		getServerTransaction(evt).sendResponse(response);
-	}
-
-	/***
-	 * 鍥炲488
-	 * @param evt
-	 * @throws SipException
-	 * @throws InvalidArgumentException
-	 * @throws ParseException
-	 */
-	private void response488Ack(RequestEvent evt) throws SipException, InvalidArgumentException, ParseException {
-		Response response = getMessageFactory().createResponse(Response.NOT_ACCEPTABLE_HERE, evt.getRequest());
-		getServerTransaction(evt).sendResponse(response);
-	}
-
-	/***
-	 * 鍥炲500
-	 * @param evt
-	 * @throws SipException
-	 * @throws InvalidArgumentException
-	 * @throws ParseException
-	 */
-	private void response500Ack(RequestEvent evt) throws SipException, InvalidArgumentException, ParseException {
-		Response response = getMessageFactory().createResponse(Response.SERVER_INTERNAL_ERROR, evt.getRequest());
-		getServerTransaction(evt).sendResponse(response);
-	}
-
-
-
-
-
-
 
 
 
@@ -394,4 +318,12 @@
 	public void setPlayService(IPlayService playService) {
 		this.playService = playService;
 	}
+
+	public IRedisCatchStorage getRedisCatchStorage() {
+		return redisCatchStorage;
+	}
+
+	public void setRedisCatchStorage(IRedisCatchStorage redisCatchStorage) {
+		this.redisCatchStorage = redisCatchStorage;
+	}
 }

--
Gitblit v1.8.0