From b92027f31ef24583e8b9d4c2dc44c7e9262a2023 Mon Sep 17 00:00:00 2001
From: panlinlin <648540858@qq.com>
Date: 星期一, 26 四月 2021 10:48:51 +0800
Subject: [PATCH] 修复注册是数据传输模式被回复默认
---
src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommander.java | 139 ++++++++++++++++++++++++++++------------------
1 files changed, 85 insertions(+), 54 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 27402ba..eb80955 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
@@ -10,20 +10,23 @@
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.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.storager.IRedisCatchStorage;
import com.genersoft.iot.vmp.storager.IVideoManagerStorager;
+import gov.nist.javax.sip.message.SIPRequest;
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.ComponentScan;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
@@ -76,6 +79,9 @@
@Autowired
private ZLMRTPServerFactory zlmrtpServerFactory;
+ @Autowired
+ private ZLMRESTfulUtils zlmresTfulUtils;
+
@Value("${media.rtp.enable}")
private boolean rtpEnable;
@@ -84,6 +90,9 @@
@Value("${media.autoApplyPlay}")
private boolean autoApplyPlay;
+
+ @Value("${userSettings.waitTrack}")
+ private boolean waitTrack;
@Autowired
private ZLMHttpHookSubscribe subscribe;
@@ -333,17 +342,17 @@
/**
* 璇锋眰棰勮瑙嗛娴�
- * @param device 瑙嗛璁惧
- * @param channelId 棰勮閫氶亾
- * @param event hook璁㈤槄
- * @param errorEvent sip閿欒璁㈤槄
- */
+ * @param device 瑙嗛璁惧
+ * @param channelId 棰勮閫氶亾
+ * @param event hook璁㈤槄
+ * @param errorEvent sip閿欒璁㈤槄
+ */
@Override
public void playStreamCmd(Device device, String channelId, ZLMHttpHookSubscribe.Event event, SipSubscribe.Event errorEvent) {
+ String streamId = null;
try {
if (device == null) return;
String ssrc = streamSession.createPlaySsrc();
- String streamId = null;
if (rtpEnable) {
streamId = String.format("gb_play_%s_%s", device.getDeviceId(), channelId);
}else {
@@ -366,13 +375,18 @@
// 娣诲姞璁㈤槄
JSONObject subscribeKey = new JSONObject();
subscribeKey.put("app", "rtp");
- subscribeKey.put("id", streamId);
+ subscribeKey.put("stream", streamId);
+ subscribeKey.put("regist", true);
- subscribe.addSubscribe(ZLMHttpHookSubscribe.HookType.on_publish, subscribeKey, event);
+ subscribe.addSubscribe(ZLMHttpHookSubscribe.HookType.on_stream_changed, subscribeKey, json->{
+ if (waitTrack && json.getJSONArray("tracks") == null) return;
+ event.response(json);
+ subscribe.removeSubscribe(ZLMHttpHookSubscribe.HookType.on_stream_changed, subscribeKey);
+ });
//
StringBuffer content = new StringBuffer(200);
content.append("v=0\r\n");
-// content.append("o="+channelId+" 0 0 IN IP4 "+mediaInfo.getWanIp()+"\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("s=Play\r\n");
content.append("c=IN IP4 "+mediaInfo.getWanIp()+"\r\n");
@@ -415,19 +429,12 @@
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");
}
}
@@ -440,9 +447,12 @@
Request request = headerProvider.createInviteRequest(device, channelId, content.toString(), null, "FromInvt" + tm, null, ssrc, callIdHeader);
- ClientTransaction transaction = transmitRequest(device, request, errorEvent);
- streamSession.put(streamId,ssrc, transaction);
-
+ ClientTransaction transaction = transmitRequest(device, request, (e -> {
+ streamSession.remove(device.getDeviceId(), channelId);
+ errorEvent.response(e);
+ }));
+ streamSession.put(device.getDeviceId(), channelId ,ssrc,streamId, transaction);
+
} catch ( SipException | ParseException | InvalidArgumentException e) {
e.printStackTrace();
}
@@ -466,9 +476,14 @@
// 娣诲姞璁㈤槄
JSONObject subscribeKey = new JSONObject();
subscribeKey.put("app", "rtp");
- subscribeKey.put("id", streamId);
+ subscribeKey.put("stream", streamId);
+ subscribeKey.put("regist", true);
- subscribe.addSubscribe(ZLMHttpHookSubscribe.HookType.on_publish, subscribeKey, event);
+ subscribe.addSubscribe(ZLMHttpHookSubscribe.HookType.on_stream_changed, subscribeKey, json->{
+ if (json.getJSONArray("tracks") == null) return;
+ event.response(json);
+ subscribe.removeSubscribe(ZLMHttpHookSubscribe.HookType.on_stream_changed, subscribeKey);
+ });
StringBuffer content = new StringBuffer(200);
content.append("v=0\r\n");
@@ -543,7 +558,7 @@
Request request = headerProvider.createPlaybackInviteRequest(device, channelId, content.toString(), null, "fromplybck" + tm, null, callIdHeader);
ClientTransaction transaction = transmitRequest(device, request, errorEvent);
- streamSession.put(streamId, ssrc, transaction);
+ streamSession.put(device.getDeviceId(), channelId, ssrc, streamId, transaction);
} catch ( SipException | ParseException | InvalidArgumentException e) {
e.printStackTrace();
@@ -557,19 +572,46 @@
*
*/
@Override
- public void streamByeCmd(String ssrc) {
- streamByeCmd(ssrc, null);
+ public void streamByeCmd(String deviceId, String channelId) {
+ streamByeCmd(deviceId, channelId, null);
}
@Override
- public void streamByeCmd(String streamId, SipSubscribe.Event okEvent) {
+ public void streamByeCmd(String deviceId, String channelId, SipSubscribe.Event okEvent) {
try {
- ClientTransaction transaction = streamSession.get(streamId);
- // 鏈嶅姟閲嶅惎鍚�
+ ClientTransaction transaction = streamSession.getTransaction(deviceId, channelId);
+ // 鏈嶅姟閲嶅惎鍚�, 鏃犳硶鐩存帴鍙戦�乥ye锛� 閫氳繃鎵嬪姩鏋勫缓鍙戦��
if (transaction == null) {
- StreamInfo streamInfo = redisCatchStorage.queryPlayByStreamId(streamId);
- if (streamInfo != 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);
}
return;
}
@@ -580,15 +622,9 @@
}
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()) {
- String ip = matcher.group(1);
- byeURI.setHost(ip);
- String port = matcher.group(2);
- byeURI.setPort(Integer.parseInt(port));
- }
+ SIPRequest request = (SIPRequest)transaction.getRequest();
+ byeURI.setHost(request.getRemoteAddress().getHostName());
+ byeURI.setPort(request.getRemotePort());
ViaHeader viaHeader = (ViaHeader) byeRequest.getHeader(ViaHeader.NAME);
String protocol = viaHeader.getTransport().toUpperCase();
ClientTransaction clientTransaction = null;
@@ -604,14 +640,9 @@
}
dialog.sendRequest(clientTransaction);
-
- streamSession.remove(streamId);
- zlmrtpServerFactory.closeRTPServer(streamId);
- } catch (TransactionDoesNotExistException e) {
- e.printStackTrace();
- } catch (SipException e) {
- e.printStackTrace();
- } catch (ParseException e) {
+ zlmrtpServerFactory.closeRTPServer(streamSession.getStreamId(deviceId, channelId));
+ streamSession.remove(deviceId, channelId);
+ } catch (SipException | ParseException e) {
e.printStackTrace();
}
}
@@ -632,7 +663,6 @@
* 璇煶骞挎挱
*
* @param device 瑙嗛璁惧
- * @param channelId 棰勮閫氶亾
*/
@Override
public boolean audioBroadcastCmd(Device device) {
@@ -1131,7 +1161,7 @@
* @param device 瑙嗛璁惧
* @param startPriority 鎶ヨ璧峰绾у埆锛堝彲閫夛級
* @param endPriority 鎶ヨ缁堟绾у埆锛堝彲閫夛級
- * @param alarmMethods 鎶ヨ鏂瑰紡鏉′欢锛堝彲閫夛級
+ * @param alarmMethod 鎶ヨ鏂瑰紡鏉′欢锛堝彲閫夛級
* @param alarmType 鎶ヨ绫诲瀷
* @param startTime 鎶ヨ鍙戠敓璧峰鏃堕棿锛堝彲閫夛級
* @param endTime 鎶ヨ鍙戠敓缁堟鏃堕棿锛堝彲閫夛級
@@ -1419,5 +1449,6 @@
String streamId = String.format("gb_play_%s_%s", device.getDeviceId(), channelId);
zlmrtpServerFactory.closeRTPServer(streamId);
}
+ streamSession.remove(device.getDeviceId(), channelId);
}
}
--
Gitblit v1.8.0