From f58290cad307c9bb8018ae53cf2bf93bbe9d7bf5 Mon Sep 17 00:00:00 2001
From: 64850858 <648540858@qq.com>
Date: 星期一, 26 七月 2021 17:53:02 +0800
Subject: [PATCH] 更新版本信息

---
 src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommander.java |  422 ++++++++++++++++++++++++++++++----------------------
 1 files changed, 242 insertions(+), 180 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 529feaf..f1223d5 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
@@ -1,8 +1,8 @@
 package com.genersoft.iot.vmp.gb28181.transmit.cmd.impl;
 
+import java.lang.reflect.Field;
 import java.text.ParseException;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
+import java.util.HashSet;
 
 import javax.sip.*;
 import javax.sip.address.SipURI;
@@ -10,23 +10,25 @@
 import javax.sip.header.ViaHeader;
 import javax.sip.message.Request;
 
-import com.alibaba.fastjson.JSON;
-import com.alibaba.fastjson.JSONArray;
 import com.alibaba.fastjson.JSONObject;
-import com.genersoft.iot.vmp.common.StreamInfo;
-import com.genersoft.iot.vmp.conf.MediaServerConfig;
+import com.genersoft.iot.vmp.conf.UserSetup;
+import com.genersoft.iot.vmp.gb28181.bean.SsrcTransaction;
+import com.genersoft.iot.vmp.media.zlm.*;
 import com.genersoft.iot.vmp.gb28181.event.SipSubscribe;
-import com.genersoft.iot.vmp.media.zlm.ZLMHttpHookSubscribe;
-import com.genersoft.iot.vmp.media.zlm.ZLMRESTfulUtils;
-import com.genersoft.iot.vmp.media.zlm.ZLMRTPServerFactory;
+import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem;
+import com.genersoft.iot.vmp.service.IMediaServerService;
+import com.genersoft.iot.vmp.service.bean.SSRCInfo;
 import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
 import com.genersoft.iot.vmp.storager.IVideoManagerStorager;
+import gov.nist.javax.sip.SipProviderImpl;
+import gov.nist.javax.sip.SipStackImpl;
 import gov.nist.javax.sip.message.SIPRequest;
+import gov.nist.javax.sip.stack.SIPDialog;
+import gov.nist.javax.sip.stack.SIPTransaction;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Qualifier;
-import org.springframework.beans.factory.annotation.Value;
 import org.springframework.context.annotation.DependsOn;
 import org.springframework.context.annotation.Lazy;
 import org.springframework.stereotype.Component;
@@ -38,7 +40,7 @@
 import com.genersoft.iot.vmp.gb28181.transmit.cmd.SIPRequestHeaderProvider;
 import com.genersoft.iot.vmp.gb28181.utils.DateUtil;
 import com.genersoft.iot.vmp.gb28181.utils.NumericUtil;
-import com.genersoft.iot.vmp.gb28181.utils.XmlUtil;
+import org.springframework.util.StringUtils;
 
 /**    
  * @Description:璁惧鑳藉姏鎺ュ彛锛岀敤浜庡畾涔夎澶囩殑鎺у埗銆佹煡璇㈣兘鍔�   
@@ -57,12 +59,12 @@
 	@Lazy
 	@Autowired
 	@Qualifier(value="tcpSipProvider")
-	private SipProvider tcpSipProvider;
+	private SipProviderImpl tcpSipProvider;
 
 	@Lazy
 	@Autowired
 	@Qualifier(value="udpSipProvider")
-	private SipProvider udpSipProvider;
+	private SipProviderImpl udpSipProvider;
 
 	@Autowired
 	private SIPRequestHeaderProvider headerProvider;
@@ -77,25 +79,18 @@
 	private IRedisCatchStorage redisCatchStorage;
 
 	@Autowired
-	private ZLMRTPServerFactory zlmrtpServerFactory;
-
-	@Autowired
-	private ZLMRESTfulUtils zlmresTfulUtils;
-
-	@Value("${media.rtp.enable}")
-	private boolean rtpEnable;
-
-	@Value("${media.seniorSdp}")
-	private boolean seniorSdp;
-
-	@Value("${media.autoApplyPlay}")
-	private boolean autoApplyPlay;
+	private UserSetup userSetup;
 
 	@Autowired
 	private ZLMHttpHookSubscribe subscribe;
 
 	@Autowired
 	private SipSubscribe sipSubscribe;
+
+	@Autowired
+	private IMediaServerService mediaServerService;
+
+	private SIPDialog dialog;
 
 	public SipConfig getSipConfig() {
 		return sipConfig;
@@ -277,7 +272,7 @@
 	public boolean frontEndCmd(Device device, String channelId, int cmdCode, int parameter1, int parameter2, int combineCode2) {
 		try {
 			String cmdStr= frontEndCmdString(cmdCode, parameter1, parameter2, combineCode2);
-			System.out.println("鎺у埗瀛楃涓诧細" + cmdStr);
+			logger.info("鎺у埗瀛楃涓诧細" + cmdStr);
 			StringBuffer ptzXml = new StringBuffer(200);
 			ptzXml.append("<?xml version=\"1.0\" ?>\r\n");
 			ptzXml.append("<Control>\r\n");
@@ -345,57 +340,40 @@
 	  * @param errorEvent sip閿欒璁㈤槄
 	  */
 	@Override
-	public void playStreamCmd(Device device, String channelId, ZLMHttpHookSubscribe.Event event, SipSubscribe.Event errorEvent) {
-		String streamId = null;
+	public void playStreamCmd(MediaServerItem mediaServerItem, SSRCInfo ssrcInfo, Device device, String channelId, ZLMHttpHookSubscribe.Event event, SipSubscribe.Event errorEvent) {
+		String streamId = ssrcInfo.getStreamId();
 		try {
 			if (device == null) return;
-			String ssrc = streamSession.createPlaySsrc();
-			if (rtpEnable) {
-				streamId = String.format("gb_play_%s_%s", device.getDeviceId(), channelId);
-			}else {
-				streamId = String.format("%08x", Integer.parseInt(ssrc)).toUpperCase();
-			}
 			String streamMode = device.getStreamMode().toUpperCase();
-			MediaServerConfig mediaInfo = redisCatchStorage.getMediaInfo();
-			if (mediaInfo == null) {
-				logger.warn("鐐规挱鏃跺彂鐜癦LM灏氭湭杩炴帴...");
-				return;
-			}
-			String mediaPort = null;
-			// 浣跨敤鍔ㄦ�乽dp绔彛
-			if (rtpEnable) {
-				mediaPort = zlmrtpServerFactory.createRTPServer(streamId) + "";
-			}else {
-				mediaPort = mediaInfo.getRtpProxyPort();
-			}
 
+			logger.info("{} 鍒嗛厤鐨刏LM涓�: {} [{}:{}]", streamId, mediaServerItem.getId(), mediaServerItem.getIp(), ssrcInfo.getPort());
 			// 娣诲姞璁㈤槄
 			JSONObject subscribeKey = new JSONObject();
 			subscribeKey.put("app", "rtp");
 			subscribeKey.put("stream", streamId);
 			subscribeKey.put("regist", true);
-
-			subscribe.addSubscribe(ZLMHttpHookSubscribe.HookType.on_stream_changed, subscribeKey, json->{
-				if (json.getJSONArray("tracks") == null) return;
-				event.response(json);
+			subscribeKey.put("mediaServerId", mediaServerItem.getId());
+			subscribe.addSubscribe(ZLMHttpHookSubscribe.HookType.on_stream_changed, subscribeKey,
+					(MediaServerItem mediaServerItemInUse, JSONObject json)->{
+				if (userSetup.isWaitTrack() && json.getJSONArray("tracks") == null) return;
+				event.response(mediaServerItemInUse, json);
 				subscribe.removeSubscribe(ZLMHttpHookSubscribe.HookType.on_stream_changed, subscribeKey);
 			});
 			//
 			StringBuffer content = new StringBuffer(200);
 			content.append("v=0\r\n");
-//			content.append("o=" + sipConfig.getSipId() + " 0 0 IN IP4 "+mediaInfo.getWanIp()+"\r\n");
-			content.append("o="+"00000"+" 0 0 IN IP4 "+mediaInfo.getWanIp()+"\r\n");
+			content.append("o="+"00000"+" 0 0 IN IP4 "+ mediaServerItem.getSdpIp() +"\r\n");
 			content.append("s=Play\r\n");
-			content.append("c=IN IP4 "+mediaInfo.getWanIp()+"\r\n");
+			content.append("c=IN IP4 "+ mediaServerItem.getSdpIp() +"\r\n");
 			content.append("t=0 0\r\n");
 
-			if (seniorSdp) {
+			if (userSetup.isSeniorSdp()) {
 				if("TCP-PASSIVE".equals(streamMode)) {
-					content.append("m=video "+ mediaPort +" TCP/RTP/AVP 96 126 125 99 34 98 97\r\n");
+					content.append("m=video "+ ssrcInfo.getPort() +" 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 96 126 125 99 34 98 97\r\n");
+					content.append("m=video "+ ssrcInfo.getPort() +" TCP/RTP/AVP 96 126 125 99 34 98 97\r\n");
 				}else if("UDP".equals(streamMode)) {
-					content.append("m=video "+ mediaPort +" RTP/AVP 96 126 125 99 34 98 97\r\n");
+					content.append("m=video "+ ssrcInfo.getPort() +" 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");
@@ -416,46 +394,44 @@
 				}
 			}else {
 				if("TCP-PASSIVE".equals(streamMode)) {
-					content.append("m=video "+ mediaPort +" TCP/RTP/AVP 96 98 97\r\n");
+					content.append("m=video "+ ssrcInfo.getPort() +" TCP/RTP/AVP 96 98 97\r\n");
 				}else if ("TCP-ACTIVE".equals(streamMode)) {
-					content.append("m=video "+ mediaPort +" TCP/RTP/AVP 96 98 97\r\n");
+					content.append("m=video "+ ssrcInfo.getPort() +" TCP/RTP/AVP 96 98 97\r\n");
 				}else if("UDP".equals(streamMode)) {
-					content.append("m=video "+ mediaPort +" RTP/AVP 96 98 97\r\n");
+					content.append("m=video "+ ssrcInfo.getPort() +" RTP/AVP 96 98 97\r\n");
 				}
 				content.append("a=recvonly\r\n");
 				content.append("a=rtpmap:96 PS/90000\r\n");
 				content.append("a=rtpmap:98 H264/90000\r\n");
 				content.append("a=rtpmap:97 MPEG4/90000\r\n");
-				if("TCP-PASSIVE".equals(streamMode)) { // tcp琚姩妯″紡
+				if ("TCP-PASSIVE".equals(streamMode)) { // tcp琚姩妯″紡
 					content.append("a=setup:passive\r\n");
-					content.append("a=recvonly\r\n");
-					content.append("a=rtpmap:96 PS/90000\r\n");
-					content.append("a=rtpmap:98 H264/90000\r\n");
-					content.append("a=rtpmap:97 MPEG4/90000\r\n");
-					if ("TCP-PASSIVE".equals(streamMode)) { // tcp琚姩妯″紡
-						content.append("a=setup:passive\r\n");
-						content.append("a=connection:new\r\n");
-					} else if ("TCP-ACTIVE".equals(streamMode)) { // tcp涓诲姩妯″紡
-						content.append("a=setup:active\r\n");
-						content.append("a=connection:new\r\n");
-					}
+					content.append("a=connection:new\r\n");
+				} else if ("TCP-ACTIVE".equals(streamMode)) { // tcp涓诲姩妯″紡
+					content.append("a=setup:active\r\n");
+					content.append("a=connection:new\r\n");
 				}
 			}
 
-			content.append("y="+ssrc+"\r\n");//ssrc
+			content.append("y="+ssrcInfo.getSsrc()+"\r\n");//ssrc
 
 			String tm = Long.toString(System.currentTimeMillis());
 
 			CallIdHeader callIdHeader = device.getTransport().equals("TCP") ? tcpSipProvider.getNewCallId()
 					: udpSipProvider.getNewCallId();
 
-			Request request = headerProvider.createInviteRequest(device, channelId, content.toString(), null, "FromInvt" + tm, null, ssrc, callIdHeader);
+			Request request = headerProvider.createInviteRequest(device, channelId, content.toString(), null, "FromInvt" + tm, null, ssrcInfo.getSsrc(), callIdHeader);
 
-			ClientTransaction transaction = transmitRequest(device, request, (e -> {
+			String finalStreamId = streamId;
+			transmitRequest(device, request, (e -> {
 				streamSession.remove(device.getDeviceId(), channelId);
+				mediaServerService.releaseSsrc(mediaServerItem, ssrcInfo.getSsrc());
 				errorEvent.response(e);
-			}));
-			streamSession.put(device.getDeviceId(), channelId ,ssrc,streamId, transaction);
+			}), e ->{
+				streamSession.put(device.getDeviceId(), channelId ,ssrcInfo.getSsrc(), finalStreamId, mediaServerItem.getId(),e.getClientTransaction());
+				streamSession.put(device.getDeviceId(), channelId , e.getDialog());
+			});
+
 			
 		} catch ( SipException | ParseException | InvalidArgumentException e) {
 			e.printStackTrace();
@@ -471,21 +447,23 @@
 	 * @param endTime 缁撴潫鏃堕棿,鏍煎紡瑕佹眰锛歽yyy-MM-dd HH:mm:ss
 	 */ 
 	@Override
-	public void playbackStreamCmd(Device device, String channelId, String startTime, String endTime, ZLMHttpHookSubscribe.Event event
+	public void playbackStreamCmd(MediaServerItem mediaServerItem, SSRCInfo ssrcInfo, Device device, String channelId, String startTime, String endTime, ZLMHttpHookSubscribe.Event event
 			, SipSubscribe.Event errorEvent) {
 		try {
-			MediaServerConfig mediaInfo = redisCatchStorage.getMediaInfo();
-			String ssrc = streamSession.createPlayBackSsrc();
-			String streamId = String.format("%08x", Integer.parseInt(ssrc)).toUpperCase();
+
+			logger.info("{} 鍒嗛厤鐨刏LM涓�: {} [{}:{}]", ssrcInfo.getStreamId(), mediaServerItem.getId(), mediaServerItem.getIp(), ssrcInfo.getPort());
+
 			// 娣诲姞璁㈤槄
 			JSONObject subscribeKey = new JSONObject();
 			subscribeKey.put("app", "rtp");
-			subscribeKey.put("stream", streamId);
+			subscribeKey.put("stream", ssrcInfo.getStreamId());
 			subscribeKey.put("regist", true);
-
-			subscribe.addSubscribe(ZLMHttpHookSubscribe.HookType.on_stream_changed, subscribeKey, json->{
-				if (json.getJSONArray("tracks") == null) return;
-				event.response(json);
+			subscribeKey.put("mediaServerId", mediaServerItem.getId());
+			logger.debug("褰曞儚鍥炴斁娣诲姞璁㈤槄锛岃闃呭唴瀹癸細" + subscribeKey.toString());
+			subscribe.addSubscribe(ZLMHttpHookSubscribe.HookType.on_stream_changed, subscribeKey,
+					(MediaServerItem mediaServerItemInUse, JSONObject json)->{
+				if (userSetup.isWaitTrack() && json.getJSONArray("tracks") == null) return;
+				event.response(mediaServerItemInUse, json);
 				subscribe.removeSubscribe(ZLMHttpHookSubscribe.HookType.on_stream_changed, subscribeKey);
 			});
 
@@ -494,25 +472,21 @@
 	        content.append("o="+sipConfig.getSipId()+" 0 0 IN IP4 "+sipConfig.getSipIp()+"\r\n");
 	        content.append("s=Playback\r\n");
 	        content.append("u="+channelId+":0\r\n");
-	        content.append("c=IN IP4 "+mediaInfo.getWanIp()+"\r\n");
+	        content.append("c=IN IP4 "+mediaServerItem.getSdpIp()+"\r\n");
 	        content.append("t="+DateUtil.yyyy_MM_dd_HH_mm_ssToTimestamp(startTime)+" "
 					+DateUtil.yyyy_MM_dd_HH_mm_ssToTimestamp(endTime) +"\r\n");
-			String mediaPort = null;
-			// 浣跨敤鍔ㄦ�乽dp绔彛
-			if (rtpEnable) {
-				mediaPort = zlmrtpServerFactory.createRTPServer(streamId) + "";
-			}else {
-				mediaPort = mediaInfo.getRtpProxyPort();
-			}
+
+
+
 			String streamMode = device.getStreamMode().toUpperCase();
 
-			if (seniorSdp) {
+			if (userSetup.isSeniorSdp()) {
 				if("TCP-PASSIVE".equals(streamMode)) {
-					content.append("m=video "+ mediaPort +" TCP/RTP/AVP 96 126 125 99 34 98 97\r\n");
+					content.append("m=video "+ ssrcInfo.getPort() +" 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 96 126 125 99 34 98 97\r\n");
+					content.append("m=video "+ ssrcInfo.getPort() +" TCP/RTP/AVP 96 126 125 99 34 98 97\r\n");
 				}else if("UDP".equals(streamMode)) {
-					content.append("m=video "+ mediaPort +" RTP/AVP 96 126 125 99 34 98 97\r\n");
+					content.append("m=video "+ ssrcInfo.getPort() +" 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");
@@ -533,11 +507,11 @@
 				}
 			}else {
 				if("TCP-PASSIVE".equals(streamMode)) {
-					content.append("m=video "+ mediaPort +" TCP/RTP/AVP 96 98 97\r\n");
+					content.append("m=video "+ ssrcInfo.getPort() +" TCP/RTP/AVP 96 98 97\r\n");
 				}else if ("TCP-ACTIVE".equals(streamMode)) {
-					content.append("m=video "+ mediaPort +" TCP/RTP/AVP 96 98 97\r\n");
+					content.append("m=video "+ ssrcInfo.getPort() +" TCP/RTP/AVP 96 98 97\r\n");
 				}else if("UDP".equals(streamMode)) {
-					content.append("m=video "+ mediaPort +" RTP/AVP 96 98 97\r\n");
+					content.append("m=video "+ ssrcInfo.getPort() +" RTP/AVP 96 98 97\r\n");
 				}
 				content.append("a=recvonly\r\n");
 				content.append("a=rtpmap:96 PS/90000\r\n");
@@ -552,7 +526,115 @@
 				}
 			}
 
-	        content.append("y="+ssrc+"\r\n");//ssrc
+	        content.append("y=" + ssrcInfo.getSsrc() + "\r\n");//ssrc
+	        
+			String tm = Long.toString(System.currentTimeMillis());
+
+			CallIdHeader callIdHeader = device.getTransport().equals("TCP") ? tcpSipProvider.getNewCallId()
+					: udpSipProvider.getNewCallId();
+
+	        Request request = headerProvider.createPlaybackInviteRequest(device, channelId, content.toString(), null, "fromplybck" + tm, null, callIdHeader);
+
+	        transmitRequest(device, request, errorEvent, okEvent -> {
+				Dialog dialog = okEvent.getClientTransaction().getDialog();
+	        	streamSession.put(device.getDeviceId(), channelId, ssrcInfo.getSsrc(), ssrcInfo.getStreamId(), mediaServerItem.getId(), okEvent.getClientTransaction());
+				streamSession.put(device.getDeviceId(), channelId, dialog);
+			});
+		} catch ( SipException | ParseException | InvalidArgumentException e) {
+			e.printStackTrace();
+		}
+	}
+
+	/**
+	 * 璇锋眰鍘嗗彶濯掍綋涓嬭浇
+	 * 
+	 * @param device  瑙嗛璁惧
+	 * @param channelId  棰勮閫氶亾
+	 * @param startTime 寮�濮嬫椂闂�,鏍煎紡瑕佹眰锛歽yyy-MM-dd HH:mm:ss
+	 * @param endTime 缁撴潫鏃堕棿,鏍煎紡瑕佹眰锛歽yyy-MM-dd HH:mm:ss
+	 * @param downloadSpeed 涓嬭浇鍊嶉�熷弬鏁�
+	 */ 
+	@Override
+	public void downloadStreamCmd(MediaServerItem mediaServerItem, SSRCInfo ssrcInfo, Device device, String channelId, String startTime, String endTime, String downloadSpeed, ZLMHttpHookSubscribe.Event event
+			, SipSubscribe.Event errorEvent) {
+		try {
+			logger.info("{} 鍒嗛厤鐨刏LM涓�: {} [{}:{}]", ssrcInfo.getStreamId(), mediaServerItem.getId(), mediaServerItem.getIp(), ssrcInfo.getPort());
+
+			// 娣诲姞璁㈤槄
+			JSONObject subscribeKey = new JSONObject();
+			subscribeKey.put("app", "rtp");
+			subscribeKey.put("stream", ssrcInfo.getStreamId());
+			subscribeKey.put("regist", true);
+			subscribeKey.put("mediaServerId", mediaServerItem.getId());
+			logger.debug("褰曞儚鍥炴斁娣诲姞璁㈤槄锛岃闃呭唴瀹癸細" + subscribeKey.toString());
+			subscribe.addSubscribe(ZLMHttpHookSubscribe.HookType.on_stream_changed, subscribeKey,
+					(MediaServerItem mediaServerItemInUse, JSONObject json)->{
+				if (userSetup.isWaitTrack() && json.getJSONArray("tracks") == null) return;
+				event.response(mediaServerItemInUse, json);
+				subscribe.removeSubscribe(ZLMHttpHookSubscribe.HookType.on_stream_changed, subscribeKey);
+			});
+
+			StringBuffer content = new StringBuffer(200);
+	        content.append("v=0\r\n");
+	        content.append("o="+sipConfig.getSipId()+" 0 0 IN IP4 "+sipConfig.getSipIp()+"\r\n");
+	        content.append("s=Download\r\n");
+	        content.append("u="+channelId+":0\r\n");
+	        content.append("c=IN IP4 "+mediaServerItem.getSdpIp()+"\r\n");
+	        content.append("t="+DateUtil.yyyy_MM_dd_HH_mm_ssToTimestamp(startTime)+" "
+					+DateUtil.yyyy_MM_dd_HH_mm_ssToTimestamp(endTime) +"\r\n");
+
+
+
+			String streamMode = device.getStreamMode().toUpperCase();
+
+			if (userSetup.isSeniorSdp()) {
+				if("TCP-PASSIVE".equals(streamMode)) {
+					content.append("m=video "+ ssrcInfo.getPort() +" TCP/RTP/AVP 96 126 125 99 34 98 97\r\n");
+				}else if ("TCP-ACTIVE".equals(streamMode)) {
+					content.append("m=video "+ ssrcInfo.getPort() +" TCP/RTP/AVP 96 126 125 99 34 98 97\r\n");
+				}else if("UDP".equals(streamMode)) {
+					content.append("m=video "+ ssrcInfo.getPort() +" 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");
+				content.append("a=fmtp:125 profile-level-id=42e01e\r\n");
+				content.append("a=rtpmap:99 MP4V-ES/90000\r\n");
+				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");
+				if("TCP-PASSIVE".equals(streamMode)){ // tcp琚姩妯″紡
+					content.append("a=setup:passive\r\n");
+					content.append("a=connection:new\r\n");
+				}else if ("TCP-ACTIVE".equals(streamMode)) { // tcp涓诲姩妯″紡
+					content.append("a=setup:active\r\n");
+					content.append("a=connection:new\r\n");
+				}
+			}else {
+				if("TCP-PASSIVE".equals(streamMode)) {
+					content.append("m=video "+ ssrcInfo.getPort() +" TCP/RTP/AVP 96 98 97\r\n");
+				}else if ("TCP-ACTIVE".equals(streamMode)) {
+					content.append("m=video "+ ssrcInfo.getPort() +" TCP/RTP/AVP 96 98 97\r\n");
+				}else if("UDP".equals(streamMode)) {
+					content.append("m=video "+ ssrcInfo.getPort() +" RTP/AVP 96 98 97\r\n");
+				}
+				content.append("a=recvonly\r\n");
+				content.append("a=rtpmap:96 PS/90000\r\n");
+				content.append("a=rtpmap:98 H264/90000\r\n");
+				content.append("a=rtpmap:97 MPEG4/90000\r\n");
+				if("TCP-PASSIVE".equals(streamMode)){ // tcp琚姩妯″紡
+					content.append("a=setup:passive\r\n");
+					content.append("a=connection:new\r\n");
+				}else if ("TCP-ACTIVE".equals(streamMode)) { // tcp涓诲姩妯″紡
+					content.append("a=setup:active\r\n");
+					content.append("a=connection:new\r\n");
+				}
+			}
+			content.append("a=downloadspeed:" + downloadSpeed + "\r\n");
+
+	        content.append("y=" + ssrcInfo.getSsrc() + "\r\n");//ssrc
 	        
 			String tm = Long.toString(System.currentTimeMillis());
 
@@ -562,68 +644,55 @@
 	        Request request = headerProvider.createPlaybackInviteRequest(device, channelId, content.toString(), null, "fromplybck" + tm, null, callIdHeader);
 
 	        ClientTransaction transaction = transmitRequest(device, request, errorEvent);
-	        streamSession.put(device.getDeviceId(), channelId, ssrc, streamId, transaction);
+	        streamSession.put(device.getDeviceId(), channelId, ssrcInfo.getSsrc(), ssrcInfo.getStreamId(), mediaServerItem.getId(), transaction);
 
 		} catch ( SipException | ParseException | InvalidArgumentException e) {
 			e.printStackTrace();
 		}
 	}
 
-
-
 	/**
-	 * 瑙嗛娴佸仠姝�
-	 * 
+	 * 瑙嗛娴佸仠姝�, 涓嶄娇鐢ㄥ洖璋�
 	 */
 	@Override
 	public void streamByeCmd(String deviceId, String channelId) {
 		streamByeCmd(deviceId, channelId, null);
 	}
+
+	/**
+	 * 瑙嗛娴佸仠姝�
+	 */
 	@Override
 	public void streamByeCmd(String deviceId, String channelId, SipSubscribe.Event okEvent) {
-		
 		try {
 			ClientTransaction transaction = streamSession.getTransaction(deviceId, channelId);
-			// 鏈嶅姟閲嶅惎鍚�, 鏃犳硶鐩存帴鍙戦�乥ye锛� 閫氳繃鎵嬪姩鏋勫缓鍙戦��
 			if (transaction == null) {
-
-				StreamInfo streamInfo = redisCatchStorage.queryPlayByDevice(deviceId, channelId);
-				if (streamInfo != null) {
-					JSONObject mediaList = zlmresTfulUtils.getMediaList(streamInfo.getApp(), streamInfo.getStreamId());
-					if (mediaList != null) { // 浠嶅湪鎺ㄦ祦鎵嶅彂閫�
-						if (mediaList.getInteger("code") == 0) {
-							JSONArray data = mediaList.getJSONArray("data");
-							if (data != null && data.size() > 0) {
-								Device device = storager.queryVideoDevice(deviceId);
-								if (device != null) {
-									StreamInfo.TransactionInfo transactionInfo = streamInfo.getTransactionInfo();
-									try {
-										Request byteRequest = headerProvider.createByteRequest(device, channelId,
-												transactionInfo.branch,
-												transactionInfo.localTag,
-												transactionInfo.remoteTag,
-												transactionInfo.callId);
-										transmitRequest(device, byteRequest);
-									} catch (InvalidArgumentException e) {
-										e.printStackTrace();
-									}
-								}
-							}
-						}
-					}
-					redisCatchStorage.stopPlay(streamInfo);
-				}
-
-				if (okEvent != null) {
-					okEvent.response(null);
-				}
+				logger.warn("[ {} -> {}]鍋滄瑙嗛娴佺殑鏃跺�欏彂鐜颁簨鍔″凡涓㈠け", deviceId, channelId);
 				return;
 			}
-			
-			Dialog dialog = transaction.getDialog();
+			SIPDialog dialog = streamSession.getDialog(deviceId, channelId);
 			if (dialog == null) {
+				logger.warn("[ {} -> {}]鍋滄瑙嗛娴佺殑鏃跺�欏彂鐜板璇濆凡涓㈠け", deviceId, channelId);
 				return;
 			}
+			SipStack sipStack = udpSipProvider.getSipStack();
+			SIPDialog sipDialog = ((SipStackImpl) sipStack).putDialog(dialog);
+			if (dialog != sipDialog) {
+				dialog = sipDialog;
+			}else {
+				dialog.setSipProvider(udpSipProvider);
+				try {
+					Field sipStackField = SIPDialog.class.getDeclaredField("sipStack");
+					sipStackField.setAccessible(true);
+					sipStackField.set(dialog, sipStack);
+					Field eventListenersField = SIPDialog.class.getDeclaredField("eventListeners");
+					eventListenersField.setAccessible(true);
+					eventListenersField.set(dialog, new HashSet<>());
+				} catch (NoSuchFieldException | IllegalAccessException e) {
+					e.printStackTrace();
+				}
+			}
+
 			Request byeRequest = dialog.createRequest(Request.BYE);
 			SipURI byeURI = (SipURI) byeRequest.getRequestURI();
 			SIPRequest request = (SIPRequest)transaction.getRequest();
@@ -644,8 +713,13 @@
 			}
 
 			dialog.sendRequest(clientTransaction);
-			zlmrtpServerFactory.closeRTPServer(streamSession.getStreamId(deviceId, channelId));
-			streamSession.remove(deviceId, channelId);
+
+			SsrcTransaction ssrcTransaction = streamSession.getSsrcTransaction(deviceId, channelId);
+			if (ssrcTransaction != null) {
+				MediaServerItem mediaServerItem = mediaServerService.getOne(ssrcTransaction.getMediaServerId());
+				mediaServerService.releaseSsrc(mediaServerItem, ssrcTransaction.getSsrc());
+				streamSession.remove(deviceId, channelId);
+			}
 		} catch (SipException | ParseException e) {
 			e.printStackTrace();
 		}
@@ -733,7 +807,7 @@
 			cmdXml.append("<Control>\r\n");
 			cmdXml.append("<CmdType>DeviceControl</CmdType>\r\n");
 			cmdXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>\r\n");
-			if (XmlUtil.isEmpty(channelId)) {
+			if (StringUtils.isEmpty(channelId)) {
 				cmdXml.append("<DeviceID>" + device.getDeviceId() + "</DeviceID>\r\n");
 			} else {
 				cmdXml.append("<DeviceID>" + channelId + "</DeviceID>\r\n");
@@ -833,16 +907,16 @@
 			cmdXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>\r\n");
 			cmdXml.append("<DeviceID>" + device.getDeviceId() + "</DeviceID>\r\n");
 			cmdXml.append("<AlarmCmd>ResetAlarm</AlarmCmd>\r\n");
-			if (!XmlUtil.isEmpty(alarmMethod) || !XmlUtil.isEmpty(alarmType)) {
+			if (!StringUtils.isEmpty(alarmMethod) || !StringUtils.isEmpty(alarmType)) {
 				cmdXml.append("<Info>\r\n");
 			}
-			if (!XmlUtil.isEmpty(alarmMethod)) {
+			if (!StringUtils.isEmpty(alarmMethod)) {
 				cmdXml.append("<AlarmMethod>" + alarmMethod + "</AlarmMethod>\r\n");
 			}
-			if (!XmlUtil.isEmpty(alarmType)) {
+			if (!StringUtils.isEmpty(alarmType)) {
 				cmdXml.append("<AlarmType>" + alarmType + "</AlarmType>\r\n");
 			}
-			if (!XmlUtil.isEmpty(alarmMethod) || !XmlUtil.isEmpty(alarmType)) {
+			if (!StringUtils.isEmpty(alarmMethod) || !StringUtils.isEmpty(alarmType)) {
 				cmdXml.append("</Info>\r\n");
 			}
 			cmdXml.append("</Control>\r\n");
@@ -875,7 +949,7 @@
 			cmdXml.append("<Control>\r\n");
 			cmdXml.append("<CmdType>DeviceControl</CmdType>\r\n");
 			cmdXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>\r\n");
-			if (XmlUtil.isEmpty(channelId)) {
+			if (StringUtils.isEmpty(channelId)) {
 				cmdXml.append("<DeviceID>" + device.getDeviceId() + "</DeviceID>\r\n");
 			} else {
 				cmdXml.append("<DeviceID>" + channelId + "</DeviceID>\r\n");
@@ -913,7 +987,7 @@
 			cmdXml.append("<Control>\r\n");
 			cmdXml.append("<CmdType>DeviceControl</CmdType>\r\n");
 			cmdXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>\r\n");
-			if (XmlUtil.isEmpty(channelId)) {
+			if (StringUtils.isEmpty(channelId)) {
 				cmdXml.append("<DeviceID>" + device.getDeviceId() + "</DeviceID>\r\n");
 			} else {
 				cmdXml.append("<DeviceID>" + channelId + "</DeviceID>\r\n");
@@ -981,13 +1055,13 @@
 			cmdXml.append("<Control>\r\n");
 			cmdXml.append("<CmdType>DeviceConfig</CmdType>\r\n");
 			cmdXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>\r\n");
-			if (XmlUtil.isEmpty(channelId)) {
+			if (StringUtils.isEmpty(channelId)) {
 				cmdXml.append("<DeviceID>" + device.getDeviceId() + "</DeviceID>\r\n");
 			} else {
 				cmdXml.append("<DeviceID>" + channelId + "</DeviceID>\r\n");
 			}
 			cmdXml.append("<BasicParam>\r\n");
-			if (!XmlUtil.isEmpty(name)) {
+			if (!StringUtils.isEmpty(name)) {
 				cmdXml.append("<Name>" + name + "</Name>\r\n");
 			}
 			if (NumericUtil.isInteger(expiration)) {
@@ -1181,22 +1255,22 @@
 			cmdXml.append("<CmdType>Alarm</CmdType>\r\n");
 			cmdXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>\r\n");
 			cmdXml.append("<DeviceID>" + device.getDeviceId() + "</DeviceID>\r\n");
-			if (!XmlUtil.isEmpty(startPriority)) {
+			if (!StringUtils.isEmpty(startPriority)) {
 				cmdXml.append("<StartAlarmPriority>" + startPriority + "</StartAlarmPriority>\r\n");
 			}
-			if (!XmlUtil.isEmpty(endPriority)) {
+			if (!StringUtils.isEmpty(endPriority)) {
 				cmdXml.append("<EndAlarmPriority>" + endPriority + "</EndAlarmPriority>\r\n");
 			}
-			if (!XmlUtil.isEmpty(alarmMethod)) {
+			if (!StringUtils.isEmpty(alarmMethod)) {
 				cmdXml.append("<AlarmMethod>" + alarmMethod + "</AlarmMethod>\r\n");
 			}
-			if (!XmlUtil.isEmpty(alarmType)) {
+			if (!StringUtils.isEmpty(alarmType)) {
 				cmdXml.append("<AlarmType>" + alarmType + "</AlarmType>\r\n");
 			}
-			if (!XmlUtil.isEmpty(startTime)) {
+			if (!StringUtils.isEmpty(startTime)) {
 				cmdXml.append("<StartAlarmTime>" + startTime + "</StartAlarmTime>\r\n");
 			}
-			if (!XmlUtil.isEmpty(endTime)) {
+			if (!StringUtils.isEmpty(endTime)) {
 				cmdXml.append("<EndAlarmTime>" + endTime + "</EndAlarmTime>\r\n");
 			}
 			cmdXml.append("</Query>\r\n");
@@ -1230,7 +1304,7 @@
 			cmdXml.append("<Query>\r\n");
 			cmdXml.append("<CmdType>ConfigDownload</CmdType>\r\n");
 			cmdXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>\r\n");
-			if (XmlUtil.isEmpty(channelId)) {
+			if (StringUtils.isEmpty(channelId)) {
 				cmdXml.append("<DeviceID>" + device.getDeviceId() + "</DeviceID>\r\n");
 			} else {
 				cmdXml.append("<DeviceID>" + channelId + "</DeviceID>\r\n");
@@ -1265,7 +1339,7 @@
 			cmdXml.append("<Query>\r\n");
 			cmdXml.append("<CmdType>PresetQuery</CmdType>\r\n");
 			cmdXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>\r\n");
-			if (XmlUtil.isEmpty(channelId)) {
+			if (StringUtils.isEmpty(channelId)) {
 				cmdXml.append("<DeviceID>" + device.getDeviceId() + "</DeviceID>\r\n");
 			} else {
 				cmdXml.append("<DeviceID>" + channelId + "</DeviceID>\r\n");
@@ -1377,22 +1451,22 @@
 			cmdXml.append("<CmdType>Alarm</CmdType>\r\n");
 			cmdXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>\r\n");
 			cmdXml.append("<DeviceID>" + device.getDeviceId() + "</DeviceID>\r\n");
-			if (!XmlUtil.isEmpty(startPriority)) {
+			if (!StringUtils.isEmpty(startPriority)) {
 				cmdXml.append("<StartAlarmPriority>" + startPriority + "</StartAlarmPriority>\r\n");
 			}
-			if (!XmlUtil.isEmpty(endPriority)) {
+			if (!StringUtils.isEmpty(endPriority)) {
 				cmdXml.append("<EndAlarmPriority>" + endPriority + "</EndAlarmPriority>\r\n");
 			}
-			if (!XmlUtil.isEmpty(alarmMethod)) {
+			if (!StringUtils.isEmpty(alarmMethod)) {
 				cmdXml.append("<AlarmMethod>" + alarmMethod + "</AlarmMethod>\r\n");
 			}
-			if (!XmlUtil.isEmpty(alarmType)) {
+			if (!StringUtils.isEmpty(alarmType)) {
 				cmdXml.append("<AlarmType>" + alarmType + "</AlarmType>\r\n");
 			}
-			if (!XmlUtil.isEmpty(startTime)) {
+			if (!StringUtils.isEmpty(startTime)) {
 				cmdXml.append("<StartAlarmTime>" + startTime + "</StartAlarmTime>\r\n");
 			}
-			if (!XmlUtil.isEmpty(endTime)) {
+			if (!StringUtils.isEmpty(endTime)) {
 				cmdXml.append("<EndAlarmTime>" + endTime + "</EndAlarmTime>\r\n");
 			}
 			cmdXml.append("</Query>\r\n");
@@ -1442,17 +1516,5 @@
 
 		clientTransaction.sendRequest();
 		return clientTransaction;
-	}
-
-
-
-
-	@Override
-	public void closeRTPServer(Device device, String channelId) {
-		if (rtpEnable) {
-			String streamId = String.format("gb_play_%s_%s", device.getDeviceId(), channelId);
-			zlmrtpServerFactory.closeRTPServer(streamId);
-		}
-		streamSession.remove(device.getDeviceId(), channelId);
 	}
 }

--
Gitblit v1.8.0