From f9d30bdfadd68e61f545e1c5f6260d4f58dca040 Mon Sep 17 00:00:00 2001
From: lawrencehj <1934378145@qq.com>
Date: 星期五, 26 三月 2021 20:15:12 +0800
Subject: [PATCH] 实现语音广播信令(web语音推流开发中)

---
 src/main/java/com/genersoft/iot/vmp/gb28181/transmit/request/impl/InviteRequestProcessor.java |  301 +++++++++++++++++++++++++++++++-------------------
 1 files changed, 187 insertions(+), 114 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 51ce21e..a7b3d94 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
@@ -14,6 +14,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.ParentPlatform;
 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;
@@ -74,144 +75,216 @@
 			Request request = evt.getRequest();
 			SipURI sipURI = (SipURI) request.getRequestURI();
 			String channelId = sipURI.getUser();
-			String platformId = null;
+			String requesterId = null;
 
 			FromHeader fromHeader = (FromHeader)request.getHeader(FromHeader.NAME);
 			AddressImpl address = (AddressImpl) fromHeader.getAddress();
 			SipUri uri = (SipUri) address.getURI();
-			platformId = uri.getUser();
+			requesterId = uri.getUser();
 
-			if (platformId == null || channelId == null) {
-				logger.info("鏃犳硶浠嶧romHeader鐨凙ddress涓幏鍙栧埌骞冲彴id锛岃繑鍥�404");
+			if (requesterId == null || channelId == null) {
+				logger.info("鏃犳硶浠嶧romHeader鐨凙ddress涓幏鍙栧埌骞冲彴id锛岃繑鍥�400");
 				responseAck(evt, Response.BAD_REQUEST); // 鍙傛暟涓嶅叏锛� 鍙�400锛岃姹傞敊璇�
 				return;
 			}
-			// 鏌ヨ骞冲彴涓嬫槸鍚︽湁璇ラ�氶亾
-			DeviceChannel channel = storager.queryChannelInParentPlatform(platformId, channelId);
-			if (channel == null) {
-				logger.info("閫氶亾涓嶅瓨鍦紝杩斿洖404");
-				responseAck(evt, Response.NOT_FOUND); // 閫氶亾涓嶅瓨鍦紝鍙�404锛岃祫婧愪笉瀛樺湪
-				return;
-			}else {
-				responseAck(evt, Response.TRYING); // 閫氶亾瀛樺湪锛屽彂100锛宼rying
-			}
-			// 瑙f瀽sdp娑堟伅, 浣跨敤jainsip 鑷甫鐨剆dp瑙f瀽鏂瑰紡
-			String contentString = new String(request.getRawContent());
 
-			// jainSip涓嶆敮鎸亂=瀛楁锛� 绉婚櫎绉婚櫎浠ヨВ鏋愩��
-			int ssrcIndex = contentString.indexOf("y=");
-			String ssrc = contentString.substring(ssrcIndex + 2, ssrcIndex + 12);
-			//ssrc瑙勫畾闀垮害涓�10瀛楄妭锛屼笉鍙栦綑涓嬮暱搴︿互閬垮厤鍚庣画杩樻湁鈥渇=鈥濆瓧娈�
-			// String ssrc = contentString.substring(ssrcIndex + 2, contentString.length())
-			// 		.replace("\r\n", "").replace("\n", "");
+			// 鏌ヨ璇锋眰鏂规槸鍚︿笂绾у钩鍙�
+			ParentPlatform platform = storager.queryParentPlatById(requesterId);
+			if (platform != null) {
+				// 鏌ヨ骞冲彴涓嬫槸鍚︽湁璇ラ�氶亾
+				DeviceChannel channel = storager.queryChannelInParentPlatform(requesterId, channelId);
+				if (channel == null) {
+					logger.info("閫氶亾涓嶅瓨鍦紝杩斿洖404");
+					responseAck(evt, Response.NOT_FOUND); // 閫氶亾涓嶅瓨鍦紝鍙�404锛岃祫婧愪笉瀛樺湪
+					return;
+				}else {
+					responseAck(evt, Response.CALL_IS_BEING_FORWARDED); // 閫氶亾瀛樺湪锛屽彂181锛屽懠鍙浆鎺ヤ腑
+				}
+				// 瑙f瀽sdp娑堟伅, 浣跨敤jainsip 鑷甫鐨剆dp瑙f瀽鏂瑰紡
+				String contentString = new String(request.getRawContent());
 
-			String substring = contentString.substring(0, contentString.indexOf("y="));
-			SessionDescription sdp = SdpFactory.getInstance().createSessionDescription(substring);
+				// jainSip涓嶆敮鎸亂=瀛楁锛� 绉婚櫎绉婚櫎浠ヨВ鏋愩��
+				int ssrcIndex = contentString.indexOf("y=");
+				//ssrc瑙勫畾闀垮害涓�10瀛楄妭锛屼笉鍙栦綑涓嬮暱搴︿互閬垮厤鍚庣画杩樻湁鈥渇=鈥濆瓧娈�
+				String ssrc = contentString.substring(ssrcIndex + 2, ssrcIndex + 12);
+				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();
+				//  鑾峰彇鏀寔鐨勬牸寮�
+				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();
 
-				Vector mediaFormats = media.getMediaFormats(false);
-				if (mediaFormats.contains("96")) {
-					port = media.getMediaPort();
-					//String mediaType = media.getMediaType();
-					String protocol = media.getProtocol();
+					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;
+						// 鍖哄垎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;
+								}
 							}
 						}
+						break;
 					}
-					break;
 				}
-			}
-			if (port == -1) {
-				logger.info("涓嶆敮鎸佺殑濯掍綋鏍煎紡锛岃繑鍥�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);
+				if (port == -1) {
+					logger.info("涓嶆敮鎸佺殑濯掍綋鏍煎紡锛岃繑鍥�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);
-				responseAck(evt, Response.SERVER_INTERNAL_ERROR);
-				return;
-			}
-			SendRtpItem sendRtpItem = zlmrtpServerFactory.createSendRtpItem(addressStr, port, ssrc, platformId, device.getDeviceId(), channelId,
-					mediaTransmissionTCP);
-			if (tcpActive != null) {
-				sendRtpItem.setTcpActive(tcpActive);
-			}
-			if (sendRtpItem == null) {
-				logger.warn("鏈嶅姟鍣ㄧ鍙h祫婧愪笉瓒�");
-				responseAck(evt, Response.BUSY_HERE);
-				return;
-			}
+				Device device = storager.queryVideoDeviceByPlatformIdAndChannelId(requesterId, channelId);
+				if (device == null) {
+					logger.warn("鐐规挱骞冲彴{}鐨勯�氶亾{}鏃舵湭鎵惧埌璁惧淇℃伅", requesterId, channel);
+					responseAck(evt, Response.SERVER_INTERNAL_ERROR);
+					return;
+				}
+				SendRtpItem sendRtpItem = zlmrtpServerFactory.createSendRtpItem(addressStr, port, ssrc, requesterId, device.getDeviceId(), 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);
+				// 鍐欏叆redis锛� 瓒呮椂鏃跺洖澶�
 				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");
+				// 閫氱煡涓嬬骇鎺ㄦ祦锛�
+				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();
+					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();
+					}
+				}));
+				if (logger.isDebugEnabled()) {
+					logger.debug(playResult.getResult().toString());
 				}
-			},(event -> {
-				// 鏈煡閿欒銆傜洿鎺ヨ浆鍙戣澶囩偣鎾殑閿欒
-				Response response = null;
-				try {
-					response = getMessageFactory().createResponse(event.getResponse().getStatusCode(), evt.getRequest());
-					getServerTransaction(evt).sendResponse(response);
+			} else {
+				// 闈炰笂绾у钩鍙拌姹傦紝鏌ヨ鏄惁璁惧璇锋眰锛堥�氬父涓烘帴鏀惰闊冲箍鎾殑璁惧锛�
+				Device device = storager.queryVideoDevice(requesterId);
+				if (device != null) {
+					logger.info("鏀跺埌璁惧" + requesterId + "鐨勮闊冲箍鎾璉nvite璇锋眰");
+					responseAck(evt, Response.TRYING);
 
-				} catch (ParseException | SipException | InvalidArgumentException e) {
-					e.printStackTrace();
+					String contentString = new String(request.getRawContent());
+					// jainSip涓嶆敮鎸亂=瀛楁锛� 绉婚櫎绉婚櫎浠ヨВ鏋愩��
+					String substring = contentString;
+					String ssrc = "0000000404";
+					int ssrcIndex = contentString.indexOf("y=");
+					if (ssrcIndex > 0) {
+						substring = contentString.substring(0, ssrcIndex);
+						ssrc = contentString.substring(ssrcIndex + 2, ssrcIndex + 12);
+					}
+					ssrcIndex = substring.indexOf("f=");
+					if (ssrcIndex > 0) {
+						substring = contentString.substring(0, ssrcIndex);
+					}
+					SessionDescription sdp = SdpFactory.getInstance().createSessionDescription(substring);
+
+					//  鑾峰彇鏀寔鐨勬牸寮�
+					Vector mediaDescriptions = sdp.getMediaDescriptions(true);
+					// 鏌ョ湅鏄惁鏀寔PS 璐熻浇96
+					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();
+
+						Vector mediaFormats = media.getMediaFormats(false);
+						if (mediaFormats.contains("8")) {
+							port = media.getMediaPort();
+							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;
+									}
+								}
+							}
+							break;
+						}
+					}
+					if (port == -1) {
+						logger.info("涓嶆敮鎸佺殑濯掍綋鏍煎紡锛岃繑鍥�415");
+						// 鍥炲涓嶆敮鎸佺殑鏍煎紡
+						responseAck(evt, Response.UNSUPPORTED_MEDIA_TYPE); // 涓嶆敮鎸佺殑鏍煎紡锛屽彂415
+						return;
+					}
+					String username = sdp.getOrigin().getUsername();
+					String addressStr = sdp.getOrigin().getAddress();
+					logger.info("璁惧{}璇锋眰璇煶娴侊紝鍦板潃锛歿}:{}锛宻src锛歿}", username, addressStr, port, ssrc);
+					
+
+
+
+
+
+				} else {
+					logger.warn("鏉ヨ嚜鏃犳晥璁惧/骞冲彴鐨勮姹�");
+					responseAck(evt, Response.BAD_REQUEST);
 				}
-			}));
-			if (logger.isDebugEnabled()) {
-				logger.debug(playResult.getResult().toString());
 			}
 
 		} catch (SipException | InvalidArgumentException | ParseException e) {

--
Gitblit v1.8.0