From 667266f387375524a2d4a0b48ab13465dcf83dbb Mon Sep 17 00:00:00 2001
From: Lawrence <1934378145@qq.com>
Date: 星期三, 27 一月 2021 15:47:27 +0800
Subject: [PATCH] 增加移动位置查询和订阅功能,优化信令Tag的生成

---
 src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommander.java |  133 +++++++++++++++++++++++++++++++------------
 1 files changed, 95 insertions(+), 38 deletions(-)

diff --git a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommander.java b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommander.java
index dd6ac9d..b661c5e 100644
--- a/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommander.java
+++ b/src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommander.java
@@ -77,6 +77,9 @@
 	@Value("${media.seniorSdp}")
 	private boolean seniorSdp;
 
+	@Value("${media.autoApplyPlay}")
+	private boolean autoApplyPlay;
+
 	@Autowired
 	private ZLMHttpHookSubscribe subscribe;
 
@@ -182,11 +185,11 @@
    /**
 	* 浜戝彴鎸囦护鐮佽绠� 
 	*
-	* @param cmdCode		鎸囦护鐮�
-	* @param parameter1	鏁版嵁1
-	* @param parameter2	鏁版嵁2
-	* @param combineCode2	缁勫悎鐮�2
-    */
+	 * @param cmdCode 		鎸囦护鐮�
+	 * @param parameter1	鏁版嵁1
+	 * @param parameter2	鏁版嵁2
+	 * @param combineCode2	缁勫悎鐮�2
+	 */
     public static String frontEndCmdString(int cmdCode, int parameter1, int parameter2, int combineCode2) {
 		StringBuilder builder = new StringBuilder("A50F01");
 		String strTmp;
@@ -208,13 +211,13 @@
 	/**
 	 * 浜戝彴鎺у埗锛屾敮鎸佹柟鍚戜笌缂╂斁鎺у埗
 	 * 
-	 * @param device  鎺у埗璁惧
-	 * @param channelId  棰勮閫氶亾
-	 * @param leftRight  闀滃ご宸︾Щ鍙崇Щ 0:鍋滄 1:宸︾Щ 2:鍙崇Щ
-     * @param upDown     闀滃ご涓婄Щ涓嬬Щ 0:鍋滄 1:涓婄Щ 2:涓嬬Щ
-     * @param inOut      闀滃ご鏀惧ぇ缂╁皬 0:鍋滄 1:缂╁皬 2:鏀惧ぇ
-     * @param moveSpeed  闀滃ご绉诲姩閫熷害
-     * @param zoomSpeed  闀滃ご缂╂斁閫熷害
+	 * @param device  	鎺у埗璁惧
+	 * @param channelId	棰勮閫氶亾
+	 * @param leftRight	闀滃ご宸︾Щ鍙崇Щ 0:鍋滄 1:宸︾Щ 2:鍙崇Щ
+     * @param upDown	闀滃ご涓婄Щ涓嬬Щ 0:鍋滄 1:涓婄Щ 2:涓嬬Щ
+     * @param inOut		闀滃ご鏀惧ぇ缂╁皬 0:鍋滄 1:缂╁皬 2:鏀惧ぇ
+     * @param moveSpeed	闀滃ご绉诲姩閫熷害
+     * @param zoomSpeed	闀滃ご缂╂斁閫熷害
 	 */
 	@Override
 	public boolean ptzCmd(Device device, String channelId, int leftRight, int upDown, int inOut, int moveSpeed,
@@ -232,7 +235,8 @@
 			ptzXml.append("</Info>\r\n");
 			ptzXml.append("</Control>\r\n");
 			
-			Request request = headerProvider.createMessageRequest(device, ptzXml.toString(), "ViaPtzBranch", "FromPtzTag", null);
+			String tm = Long.toString(System.currentTimeMillis());
+			Request request = headerProvider.createMessageRequest(device, ptzXml.toString(), "ViaPtzBranch", "FromPtz" + tm, null);
 			
 			transmitRequest(device, request);
 			return true;
@@ -268,7 +272,8 @@
 			ptzXml.append("</Info>\r\n");
 			ptzXml.append("</Control>\r\n");
 			
-			Request request = headerProvider.createMessageRequest(device, ptzXml.toString(), "ViaPtzBranch", "FromPtzTag", null);
+			String tm = Long.toString(System.currentTimeMillis());
+			Request request = headerProvider.createMessageRequest(device, ptzXml.toString(), "ViaPtzBranch", "FromPtz" + tm, null);
 			transmitRequest(device, request);
 			return true;
 		} catch (SipException | ParseException | InvalidArgumentException e) {
@@ -326,13 +331,14 @@
 
 			if (seniorSdp) {
 				if("TCP-PASSIVE".equals(streamMode)) {
-					content.append("m=video "+ mediaPort +" TCP/RTP/AVP 126 125 99 34 98 97 96\r\n");
+					content.append("m=video "+ mediaPort +" TCP/RTP/AVP 96 126 125 99 34 98 97\r\n");
 				}else if ("TCP-ACTIVE".equals(streamMode)) {
-					content.append("m=video "+ mediaPort +" TCP/RTP/AVP 126 125 99 34 98 97 96\r\n");
+					content.append("m=video "+ mediaPort +" TCP/RTP/AVP 96 126 125 99 34 98 97\r\n");
 				}else if("UDP".equals(streamMode)) {
-					content.append("m=video "+ mediaPort +" RTP/AVP 126 125 99 34 98 97 96\r\n");
+					content.append("m=video "+ mediaPort +" RTP/AVP 96 126 125 99 34 98 97\r\n");
 				}
 				content.append("a=recvonly\r\n");
+				content.append("a=rtpmap:96 PS/90000\r\n");
 				content.append("a=fmtp:126 profile-level-id=42e01e\r\n");
 				content.append("a=rtpmap:126 H264/90000\r\n");
 				content.append("a=rtpmap:125 H264S/90000\r\n");
@@ -341,7 +347,6 @@
 				content.append("a=fmtp:99 profile-level-id=3\r\n");
 				content.append("a=rtpmap:98 H264/90000\r\n");
 				content.append("a=rtpmap:97 MPEG4/90000\r\n");
-				content.append("a=rtpmap:96 PS/90000\r\n");
 				if("TCP-PASSIVE".equals(streamMode)){ // tcp琚姩妯″紡
 					content.append("a=setup:passive\r\n");
 					content.append("a=connection:new\r\n");
@@ -380,10 +385,8 @@
 
 			content.append("y="+ssrc+"\r\n");//ssrc
 
-//			String fromTag = UUID.randomUUID().toString();
-//			Request request = headerProvider.createInviteRequest(device, channelId, content.toString(), null, fromTag, null, ssrc);
-
-			Request request = headerProvider.createInviteRequest(device, channelId, content.toString(), null, "live", null, ssrc);
+			String tm = Long.toString(System.currentTimeMillis());
+			Request request = headerProvider.createInviteRequest(device, channelId, content.toString(), null, "FromInvt" + tm, null, ssrc);
 
 			ClientTransaction transaction = transmitRequest(device, request, errorEvent);
 			streamSession.put(streamId, transaction);
@@ -417,7 +420,6 @@
 
 			subscribe.addSubscribe(ZLMHttpHookSubscribe.HookType.on_publish, subscribeKey, event);
 
-			//
 			StringBuffer content = new StringBuffer(200);
 	        content.append("v=0\r\n");
 	        content.append("o="+sipConfig.getSipId()+" 0 0 IN IP4 "+sipConfig.getSipIp()+"\r\n");
@@ -437,13 +439,14 @@
 
 			if (seniorSdp) {
 				if("TCP-PASSIVE".equals(streamMode)) {
-					content.append("m=video "+ mediaPort +" TCP/RTP/AVP 126 125 99 34 98 97 96\r\n");
+					content.append("m=video "+ mediaPort +" TCP/RTP/AVP 96 126 125 99 34 98 97\r\n");
 				}else if ("TCP-ACTIVE".equals(streamMode)) {
-					content.append("m=video "+ mediaPort +" TCP/RTP/AVP 126 125 99 34 98 97 96\r\n");
+					content.append("m=video "+ mediaPort +" TCP/RTP/AVP 96 126 125 99 34 98 97\r\n");
 				}else if("UDP".equals(streamMode)) {
-					content.append("m=video "+ mediaPort +" RTP/AVP 126 125 99 34 98 97 96\r\n");
+					content.append("m=video "+ mediaPort +" RTP/AVP 96 126 125 99 34 98 97\r\n");
 				}
 				content.append("a=recvonly\r\n");
+				content.append("a=rtpmap:96 PS/90000\r\n");
 				content.append("a=fmtp:126 profile-level-id=42e01e\r\n");
 				content.append("a=rtpmap:126 H264/90000\r\n");
 				content.append("a=rtpmap:125 H264S/90000\r\n");
@@ -452,7 +455,6 @@
 				content.append("a=fmtp:99 profile-level-id=3\r\n");
 				content.append("a=rtpmap:98 H264/90000\r\n");
 				content.append("a=rtpmap:97 MPEG4/90000\r\n");
-				content.append("a=rtpmap:96 PS/90000\r\n");
 				if("TCP-PASSIVE".equals(streamMode)){ // tcp琚姩妯″紡
 					content.append("a=setup:passive\r\n");
 					content.append("a=connection:new\r\n");
@@ -483,7 +485,8 @@
 
 	        content.append("y="+ssrc+"\r\n");//ssrc
 	        
-	        Request request = headerProvider.createPlaybackInviteRequest(device, channelId, content.toString(), null, "playback", null);
+			String tm = Long.toString(System.currentTimeMillis());
+	        Request request = headerProvider.createPlaybackInviteRequest(device, channelId, content.toString(), null, "fromplybck" + tm, null);
 
 	        ClientTransaction transaction = transmitRequest(device, request, errorEvent);
 	        streamSession.put(streamId, transaction);
@@ -521,16 +524,16 @@
 			if (dialog == null) {
 				return;
 			}
-
-
-
 			Request byeRequest = dialog.createRequest(Request.BYE);
 			SipURI byeURI = (SipURI) byeRequest.getRequestURI();
 			String vh = transaction.getRequest().getHeader(ViaHeader.NAME).toString();
 			Pattern p = Pattern.compile("(\\d+\\.\\d+\\.\\d+\\.\\d+)\\:(\\d+)");
 			Matcher matcher = p.matcher(vh);
 			if (matcher.find()) {
-				byeURI.setHost(matcher.group(1));
+				String ip = matcher.group(1);
+				byeURI.setHost(ip);
+				String port = matcher.group(2);
+				byeURI.setPort(Integer.parseInt(port));
 			}
 			ViaHeader viaHeader = (ViaHeader) byeRequest.getHeader(ViaHeader.NAME);
 			String protocol = viaHeader.getTransport().toUpperCase();
@@ -666,7 +669,8 @@
 			catalogXml.append("<DeviceID>" + device.getDeviceId() + "</DeviceID>\r\n");
 			catalogXml.append("</Query>\r\n");
 			
-			Request request = headerProvider.createMessageRequest(device, catalogXml.toString(), "ViaDeviceInfoBranch", "FromDeviceInfoTag", null);
+			String tm = Long.toString(System.currentTimeMillis());
+			Request request = headerProvider.createMessageRequest(device, catalogXml.toString(), "ViaDeviceInfoBranch", "FromDev" + tm, null);
 
 			transmitRequest(device, request);
 			
@@ -695,7 +699,8 @@
 			catalogXml.append("<DeviceID>" + device.getDeviceId() + "</DeviceID>\r\n");
 			catalogXml.append("</Query>\r\n");
 			
-			Request request = headerProvider.createMessageRequest(device, catalogXml.toString(), "ViaCatalogBranch", "FromCatalogTag", null);
+			String tm = Long.toString(System.currentTimeMillis());
+			Request request = headerProvider.createMessageRequest(device, catalogXml.toString(), "ViaCatalogBranch", "FromCat" + tm, null);
 
 			transmitRequest(device, request, errorEvent);
 		} catch (SipException | ParseException | InvalidArgumentException e) {
@@ -729,7 +734,8 @@
 			recordInfoXml.append("<Type>all</Type>\r\n");
 			recordInfoXml.append("</Query>\r\n");
 			
-			Request request = headerProvider.createMessageRequest(device, recordInfoXml.toString(), "ViaRecordInfoBranch", "FromRecordInfoTag", null);
+			String tm = Long.toString(System.currentTimeMillis());
+			Request request = headerProvider.createMessageRequest(device, recordInfoXml.toString(), "ViaRecordInfoBranch", "fromRec" + tm, null);
 
 			transmitRequest(device, request);
 		} catch (SipException | ParseException | InvalidArgumentException e) {
@@ -778,11 +784,62 @@
 	 * @param device 瑙嗛璁惧
 	 */  
 	@Override
-	public boolean mobilePostitionQuery(Device device) {
-		// TODO Auto-generated method stub
-		return false;
+	public boolean mobilePostitionQuery(Device device, SipSubscribe.Event errorEvent) {
+		try {
+			StringBuffer mobilePostitionXml = new StringBuffer(200);
+			mobilePostitionXml.append("<?xml version=\"1.0\" encoding=\"GB2312\"?>\r\n");
+			mobilePostitionXml.append("<Query>\r\n");
+			mobilePostitionXml.append("<CmdType>MobilePosition</CmdType>\r\n");
+			mobilePostitionXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>\r\n");
+			mobilePostitionXml.append("<DeviceID>" + device.getDeviceId() + "</DeviceID>\r\n");
+			mobilePostitionXml.append("<Interval>60</Interval>\r\n");
+			mobilePostitionXml.append("</Query>\r\n");
+			
+			String tm = Long.toString(System.currentTimeMillis());
+			Request request = headerProvider.createMessageRequest(device, mobilePostitionXml.toString(), "viaTagPos" + tm, "fromTagPos" + tm, null);
+
+			transmitRequest(device, request, errorEvent);
+			
+		} catch (SipException | ParseException | InvalidArgumentException e) {
+			e.printStackTrace();
+			return false;
+		}
+		return true;
 	}
 
+	/**
+	 * 璁㈤槄銆佸彇娑堣闃呯Щ鍔ㄤ綅缃�
+	 * 
+	 * @param device	瑙嗛璁惧
+	 * @param expires	璁㈤槄瓒呮椂鏃堕棿
+	 * @param interval	涓婃姤鏃堕棿闂撮殧
+	 */
+	public boolean mobilePositionSubscribe(Device device, int expires, int interval) {
+		try {
+			StringBuffer subscribePostitionXml = new StringBuffer(200);
+			subscribePostitionXml.append("<?xml version=\"1.0\" encoding=\"GB2312\"?>\r\n");
+			subscribePostitionXml.append("<Query>\r\n");
+			subscribePostitionXml.append("<CmdType>MobilePosition</CmdType>\r\n");
+			subscribePostitionXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>\r\n");
+			subscribePostitionXml.append("<DeviceID>" + device.getDeviceId() + "</DeviceID>\r\n");
+			if (expires > 0) {
+				subscribePostitionXml.append("<Interval>" + String.valueOf(interval) + "</Interval>\r\n");
+			}
+			subscribePostitionXml.append("</Query>\r\n");
+
+			String tm = Long.toString(System.currentTimeMillis());
+			Request request = headerProvider.createSubscribeRequest(device, subscribePostitionXml.toString(), "viaTagPos" + tm, "fromTagPos" + tm, null, expires, "Position;id=" + tm.substring(tm.length() - 4));
+			transmitRequest(device, request);
+
+			return true;
+
+		} catch ( NumberFormatException | ParseException | InvalidArgumentException	| SipException e) {
+			e.printStackTrace();
+			return false;
+		}
+	}
+
+
 	private ClientTransaction transmitRequest(Device device, Request request) throws SipException {
 		return transmitRequest(device, request, null, null);
 	}

--
Gitblit v1.8.0