From 118e42884e677a99dbdd20349eaad85c96bafeb4 Mon Sep 17 00:00:00 2001
From: panlinlin <648540858@qq.com>
Date: 星期四, 22 四月 2021 10:56:13 +0800
Subject: [PATCH] 优化拉流代理
---
src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommander.java | 137 +++++++++++++++++++++++++++++++++------------
1 files changed, 101 insertions(+), 36 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 abf9cf7..188676f 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
@@ -23,7 +23,6 @@
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;
@@ -47,8 +46,7 @@
public class SIPCommander implements ISIPCommander {
private final Logger logger = LoggerFactory.getLogger(SIPCommander.class);
-
-
+
@Autowired
private SipConfig sipConfig;
@@ -92,7 +90,9 @@
@Autowired
private SipSubscribe sipSubscribe;
-
+ public SipConfig getSipConfig() {
+ return sipConfig;
+ }
/**
* 浜戝彴鏂瑰悜鏀炬帶鍒讹紝浣跨敤閰嶇疆鏂囦欢涓殑榛樿闀滃ご绉诲姩閫熷害
@@ -332,17 +332,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 {
@@ -365,13 +365,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 (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");
@@ -439,9 +444,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();
}
@@ -465,9 +473,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");
@@ -542,7 +555,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();
@@ -556,20 +569,21 @@
*
*/
@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);
// 鏈嶅姟閲嶅惎鍚�
if (transaction == null) {
- StreamInfo streamInfo = redisCatchStorage.queryPlayByStreamId(streamId);
+ StreamInfo streamInfo = redisCatchStorage.queryPlayByDevice(deviceId, channelId);
if (streamInfo != null) {
-
+ redisCatchStorage.stopPlay(streamInfo);
}
+ okEvent.response(null);
return;
}
@@ -603,14 +617,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();
}
}
@@ -623,10 +632,65 @@
*/
@Override
public boolean audioBroadcastCmd(Device device, String channelId) {
- // TODO Auto-generated method stub
+ // 鏀逛负鏂扮殑瀹炵幇
return false;
}
+ /**
+ * 璇煶骞挎挱
+ *
+ * @param device 瑙嗛璁惧
+ */
+ @Override
+ public boolean audioBroadcastCmd(Device device) {
+ try {
+ StringBuffer broadcastXml = new StringBuffer(200);
+ broadcastXml.append("<?xml version=\"1.0\" ?>\r\n");
+ broadcastXml.append("<Notify>\r\n");
+ broadcastXml.append("<CmdType>Broadcast</CmdType>\r\n");
+ broadcastXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>\r\n");
+ broadcastXml.append("<SourceID>" + sipConfig.getSipId() + "</SourceID>\r\n");
+ broadcastXml.append("<TargetID>" + device.getDeviceId() + "</TargetID>\r\n");
+ broadcastXml.append("</Notify>\r\n");
+
+ String tm = Long.toString(System.currentTimeMillis());
+
+ CallIdHeader callIdHeader = device.getTransport().equals("TCP") ? tcpSipProvider.getNewCallId()
+ : udpSipProvider.getNewCallId();
+
+ Request request = headerProvider.createMessageRequest(device, broadcastXml.toString(), "z9hG4bK-ViaBcst-" + tm, "FromBcst" + tm, null, callIdHeader);
+ transmitRequest(device, request);
+ return true;
+ } catch (SipException | ParseException | InvalidArgumentException e) {
+ e.printStackTrace();
+ }
+ return false;
+ }
+ @Override
+ public void audioBroadcastCmd(Device device, SipSubscribe.Event errorEvent) {
+ try {
+ StringBuffer broadcastXml = new StringBuffer(200);
+ broadcastXml.append("<?xml version=\"1.0\" ?>\r\n");
+ broadcastXml.append("<Notify>\r\n");
+ broadcastXml.append("<CmdType>Broadcast</CmdType>\r\n");
+ broadcastXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>\r\n");
+ broadcastXml.append("<SourceID>" + sipConfig.getSipId() + "</SourceID>\r\n");
+ broadcastXml.append("<TargetID>" + device.getDeviceId() + "</TargetID>\r\n");
+ broadcastXml.append("</Notify>\r\n");
+
+ String tm = Long.toString(System.currentTimeMillis());
+
+ CallIdHeader callIdHeader = device.getTransport().equals("TCP") ? tcpSipProvider.getNewCallId()
+ : udpSipProvider.getNewCallId();
+
+ Request request = headerProvider.createMessageRequest(device, broadcastXml.toString(), "z9hG4bK-ViaBcst-" + tm, "FromBcst" + tm, null, callIdHeader);
+ transmitRequest(device, request, errorEvent);
+ } catch (SipException | ParseException | InvalidArgumentException e) {
+ e.printStackTrace();
+ }
+ }
+
+
/**
* 闊宠棰戝綍鍍忔帶鍒�
*
@@ -1074,7 +1138,7 @@
* @param device 瑙嗛璁惧
* @param startPriority 鎶ヨ璧峰绾у埆锛堝彲閫夛級
* @param endPriority 鎶ヨ缁堟绾у埆锛堝彲閫夛級
- * @param alarmMethods 鎶ヨ鏂瑰紡鏉′欢锛堝彲閫夛級
+ * @param alarmMethod 鎶ヨ鏂瑰紡鏉′欢锛堝彲閫夛級
* @param alarmType 鎶ヨ绫诲瀷
* @param startTime 鎶ヨ鍙戠敓璧峰鏃堕棿锛堝彲閫夛級
* @param endTime 鎶ヨ鍙戠敓缁堟鏃堕棿锛堝彲閫夛級
@@ -1362,5 +1426,6 @@
String streamId = String.format("gb_play_%s_%s", device.getDeviceId(), channelId);
zlmrtpServerFactory.closeRTPServer(streamId);
}
+ streamSession.remove(device.getDeviceId(), channelId);
}
}
--
Gitblit v1.8.0