From 421c2f56020bfe60b0e0c90e9d509abf03354dab Mon Sep 17 00:00:00 2001
From: panlinlin <648540858@qq.com>
Date: 星期一, 01 三月 2021 18:12:56 +0800
Subject: [PATCH] 使用jainsip的方式解析sdp

---
 src/main/java/com/genersoft/iot/vmp/gb28181/transmit/request/impl/InviteRequestProcessor.java |  107 +++++++++++++++++++++++++++++++++++++++--------------
 1 files changed, 79 insertions(+), 28 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 9d0a9a3..7e9e626 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
@@ -113,45 +113,84 @@
 			}
 			// 瑙f瀽sdp娑堟伅, 浣跨敤jainsip 鑷甫鐨剆dp瑙f瀽鏂瑰紡
 			String contentString = new String(request.getRawContent());
-			SessionDescription sdp = SdpFactory.getInstance().createSessionDescription(contentString);
 
-			// TODO 鍖哄垎TCP鍙戞祦杩樻槸udp锛� 褰撳墠榛樿udp
+			// jainSip涓嶆敮鎸亂=瀛楁锛� 绉婚櫎绉婚櫎浠ヨВ鏋愩��
+			int ssrcIndex = contentString.indexOf("y=");
+			String ssrc = contentString.substring(ssrcIndex + 2, contentString.length())
+					.replace("\r\n", "").replace("\n", "");
+
+			String substring = contentString.substring(0, contentString.indexOf("y="));
+			SessionDescription sdp = SdpFactory.getInstance().createSessionDescription(substring);
+
 			//  鑾峰彇鏀寔鐨勬牸寮�
 			Vector mediaDescriptions = sdp.getMediaDescriptions(true);
 			// 鏌ョ湅鏄惁鏀寔PS 璐熻浇96
 			String ip = null;
 			int port = -1;
+			boolean recvonly = false;
+			boolean mediaTransmissionTCP = false;
+			Boolean tcpActive = null;
 			for (int i = 0; i < mediaDescriptions.size(); i++) {
 				MediaDescription mediaDescription = (MediaDescription)mediaDescriptions.get(i);
 				Media media = mediaDescription.getMedia();
-				port = media.getMediaPort();
-			}
-//			for (MediaDescription mediaDescription : mediaDescriptions) {
+
+				Vector mediaFormats = media.getMediaFormats(false);
+				if (mediaFormats.contains("96")) {
+					port = media.getMediaPort();
+					String mediaType = media.getMediaType();
+					String protocol = media.getProtocol();
+
+					// 鍖哄垎TCP鍙戞祦杩樻槸udp锛� 褰撳墠榛樿udp
+					if ("TCP/RTP/AVP".equals(protocol)) {
+						String setup = mediaDescription.getAttribute("setup");
+						if (setup != null) {
+							mediaTransmissionTCP = true;
+							if ("active".equals(setup)) {
+								tcpActive = true;
+							}else if ("passive".equals(setup)) {
+								tcpActive = false;
+							}
+						}
+					}
+//					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;
 //
-//				List<Codec> codecs = mediaDescription.getCodecs();
-//				for (Codec codec : codecs) {
-//					if("96".equals(codec.getPayloadType()) || "PS".equals(codec.getName()) || "ps".equals(codec.getName())) {
-//						// TODO 杩欓噷寰堟參
-//						ip = mediaDescription.getIpAddress().getHostName();
-//						port = mediaDescription.getPort();
-//						break;
+//						}
+//						if ("recvonly".equals(name)) {
+//							recvonly = true;
+//						}
+//
+//						String value = attribute.getValue();
 //					}
-//				}
-//			}
-//			if (ip == null || port == -1) { // TODO 娌℃湁鍚堥�傜殑瑙嗛娴佹牸寮忥紝 鍙厤缃槸鍚︿娇鐢ㄧ涓�涓猰edia淇℃伅
-//				if (mediaDescriptions.size() > 0) {
-//					ip = mediaDescriptions.get(0).getIpAddress().getHostName();
-//					port = mediaDescriptions.get(0).getPort();
-//				}
-//			}
-//
-//			if (ip == null || port == -1) {
-//				response488Ack(evt);
-//				return;
-//			}
-//
-//
-//			String ssrc = sdp.getSsrc();
+					break;
+				}
+			}
+			if (port == -1) {
+				// 鍥炲涓嶆敮鎸佺殑鏍煎紡
+				response415Ack(evt); // 涓嶆敮鎸佺殑鏍煎紡锛屽彂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) {
@@ -278,6 +317,18 @@
 	}
 
 	/***
+	 * 鍥炲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

--
Gitblit v1.8.0