From 98d12ecd6bdc49e6bed86eda4f55ab75e940ca36 Mon Sep 17 00:00:00 2001
From: 648540858 <648540858@qq.com>
Date: 星期五, 11 三月 2022 09:49:48 +0800
Subject: [PATCH] Merge pull request #388 from AbnerSC/wvp-28181-2.0
---
src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommander.java | 606 ++++++++++++++++++++++++++++++++++++++++---------------
1 files changed, 440 insertions(+), 166 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 5c2fa32..b38a8c1 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,45 +1,47 @@
package com.genersoft.iot.vmp.gb28181.transmit.cmd.impl;
-import java.text.ParseException;
+import com.alibaba.fastjson.JSONObject;
+import com.genersoft.iot.vmp.common.StreamInfo;
+import com.genersoft.iot.vmp.conf.DynamicTask;
+import com.genersoft.iot.vmp.conf.SipConfig;
+import com.genersoft.iot.vmp.conf.UserSetup;
+import com.genersoft.iot.vmp.gb28181.bean.Device;
+import com.genersoft.iot.vmp.gb28181.bean.SsrcTransaction;
+import com.genersoft.iot.vmp.gb28181.event.SipSubscribe;
+import com.genersoft.iot.vmp.gb28181.session.VideoStreamSessionManager;
+import com.genersoft.iot.vmp.gb28181.transmit.cmd.ISIPCommander;
+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.media.zlm.ZLMHttpHookSubscribe;
+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 org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.context.annotation.DependsOn;
+import org.springframework.stereotype.Component;
+import org.springframework.util.StringUtils;
import javax.sip.*;
import javax.sip.address.SipURI;
import javax.sip.header.CallIdHeader;
import javax.sip.header.ViaHeader;
import javax.sip.message.Request;
-
-import com.alibaba.fastjson.JSONArray;
-import com.alibaba.fastjson.JSONObject;
-import com.genersoft.iot.vmp.common.StreamInfo;
-import com.genersoft.iot.vmp.conf.MediaConfig;
-import com.genersoft.iot.vmp.conf.UserSetup;
-import com.genersoft.iot.vmp.media.zlm.*;
-import com.genersoft.iot.vmp.gb28181.event.SipSubscribe;
-import com.genersoft.iot.vmp.media.zlm.dto.IMediaServerItem;
-import com.genersoft.iot.vmp.media.zlm.dto.MediaServerItem;
-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.context.annotation.DependsOn;
-import org.springframework.context.annotation.Lazy;
-import org.springframework.stereotype.Component;
-
-import com.genersoft.iot.vmp.conf.SipConfig;
-import com.genersoft.iot.vmp.gb28181.bean.Device;
-import com.genersoft.iot.vmp.gb28181.session.VideoStreamSessionManager;
-import com.genersoft.iot.vmp.gb28181.transmit.cmd.ISIPCommander;
-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;
+import java.lang.reflect.Field;
+import java.text.ParseException;
+import java.util.HashSet;
/**
- * @Description:璁惧鑳藉姏鎺ュ彛锛岀敤浜庡畾涔夎澶囩殑鎺у埗銆佹煡璇㈣兘鍔�
+ * @description:璁惧鑳藉姏鎺ュ彛锛岀敤浜庡畾涔夎澶囩殑鎺у埗銆佹煡璇㈣兘鍔�
* @author: swwheihei
* @date: 2020骞�5鏈�3鏃� 涓嬪崍9:22:48
*/
@@ -52,15 +54,13 @@
@Autowired
private SipConfig sipConfig;
- @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;
@@ -75,9 +75,6 @@
private IRedisCatchStorage redisCatchStorage;
@Autowired
- private ZLMRTPServerFactory zlmrtpServerFactory;
-
- @Autowired
private UserSetup userSetup;
@Autowired
@@ -86,9 +83,12 @@
@Autowired
private SipSubscribe sipSubscribe;
- public SipConfig getSipConfig() {
- return sipConfig;
- }
+ @Autowired
+ private IMediaServerService mediaServerService;
+
+ @Autowired
+ private DynamicTask dynamicTask;
+
/**
* 浜戝彴鏂瑰悜鏀炬帶鍒讹紝浣跨敤閰嶇疆鏂囦欢涓殑榛樿闀滃ご绉诲姩閫熷害
@@ -100,7 +100,7 @@
*/
@Override
public boolean ptzdirectCmd(Device device, String channelId, int leftRight, int upDown) {
- return ptzCmd(device, channelId, leftRight, upDown, 0, sipConfig.getSpeed(), 0);
+ return ptzCmd(device, channelId, leftRight, upDown, 0, sipConfig.getPtzSpeed(), 0);
}
/**
@@ -126,7 +126,7 @@
*/
@Override
public boolean ptzZoomCmd(Device device, String channelId, int inOut) {
- return ptzCmd(device, channelId, 0, 0, inOut, 0, sipConfig.getSpeed());
+ return ptzCmd(device, channelId, 0, 0, inOut, 0, sipConfig.getPtzSpeed());
}
/**
@@ -266,7 +266,7 @@
public boolean frontEndCmd(Device device, String channelId, int cmdCode, int parameter1, int parameter2, int combineCode2) {
try {
String cmdStr= frontEndCmdString(cmdCode, parameter1, parameter2, combineCode2);
- logger.info("鎺у埗瀛楃涓诧細" + cmdStr);
+ logger.debug("鎺у埗瀛楃涓诧細" + cmdStr);
StringBuffer ptzXml = new StringBuffer(200);
ptzXml.append("<?xml version=\"1.0\" ?>\r\n");
ptzXml.append("<Control>\r\n");
@@ -334,54 +334,42 @@
* @param errorEvent sip閿欒璁㈤槄
*/
@Override
- public void playStreamCmd(IMediaServerItem mediaServerItem, 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.getStream();
try {
if (device == null) return;
String streamMode = device.getStreamMode().toUpperCase();
- String ssrc = streamSession.createPlaySsrc();
- if (mediaServerItem.isRtpEnable()) {
- streamId = String.format("gb_play_%s_%s", device.getDeviceId(), channelId);
- }else {
- streamId = String.format("%08x", Integer.parseInt(ssrc)).toUpperCase();
- }
- Integer mediaPort = null;
- // 浣跨敤鍔ㄦ�乽dp绔彛
- if (mediaServerItem.isRtpEnable()) {
- mediaPort = zlmrtpServerFactory.createRTPServer(mediaServerItem, streamId);
- }else {
- mediaPort = mediaServerItem.getRtpProxyPort();
- }
- logger.info("{} 鍒嗛厤鐨刏LM涓�: {} [{}:{}]", streamId, mediaServerItem.getId(), mediaServerItem.getIp(), mediaPort);
+ 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);
+ subscribeKey.put("schema", "rtmp");
subscribeKey.put("mediaServerId", mediaServerItem.getId());
subscribe.addSubscribe(ZLMHttpHookSubscribe.HookType.on_stream_changed, subscribeKey,
- (IMediaServerItem mediaServerItemInUse, JSONObject json)->{
- if (userSetup.isWaitTrack() && json.getJSONArray("tracks") == null) return;
- event.response(mediaServerItemInUse, json);
- subscribe.removeSubscribe(ZLMHttpHookSubscribe.HookType.on_stream_changed, subscribeKey);
+ (MediaServerItem mediaServerItemInUse, JSONObject json)->{
+ if (event != null) {
+ event.response(mediaServerItemInUse, json);
+ }
});
//
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 "+ mediaServerItem.getSdpIp() +"\r\n");
+ content.append("o="+ sipConfig.getId()+" 0 0 IN IP4 "+ mediaServerItem.getSdpIp() +"\r\n");
content.append("s=Play\r\n");
content.append("c=IN IP4 "+ mediaServerItem.getSdpIp() +"\r\n");
content.append("t=0 0\r\n");
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");
@@ -402,11 +390,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");
@@ -421,20 +409,27 @@
}
}
- content.append("y="+ssrc+"\r\n");//ssrc
+ content.append("y="+ssrcInfo.getSsrc()+"\r\n");//ssrc
+ // f瀛楁:f= v/缂栫爜鏍煎紡/鍒嗚鲸鐜�/甯х巼/鐮佺巼绫诲瀷/鐮佺巼澶у皬a/缂栫爜鏍煎紡/鐮佺巼澶у皬/閲囨牱鐜�
+// content.append("f=v/2/5/25/1/4000a/1/8/1" + "\r\n"); // 鏈彂鐜版敮鎸佹鐗规�х殑璁惧
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 -> {
- streamSession.remove(device.getDeviceId(), channelId);
+ transmitRequest(device, request, (e -> {
+ streamSession.remove(device.getDeviceId(), channelId, ssrcInfo.getStream());
+ mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcInfo.getSsrc());
errorEvent.response(e);
- }));
- streamSession.put(device.getDeviceId(), channelId ,ssrc,streamId, transaction);
+ }), e ->{
+ // 杩欓噷涓轰緥閬垮厤涓�涓�氶亾鐨勭偣鎾彧鏈変竴涓猚allID杩欎釜鍙傛暟浣跨敤涓�涓浐瀹氬��
+ streamSession.put(device.getDeviceId(), channelId ,"play", streamId, ssrcInfo.getSsrc(), mediaServerItem.getId(), ((ResponseEvent)e.event).getClientTransaction());
+ streamSession.put(device.getDeviceId(), channelId ,"play", e.dialog);
+ });
+
} catch ( SipException | ParseException | InvalidArgumentException e) {
e.printStackTrace();
@@ -450,55 +445,45 @@
* @param endTime 缁撴潫鏃堕棿,鏍煎紡瑕佹眰锛歽yyy-MM-dd HH:mm:ss
*/
@Override
- public void playbackStreamCmd(IMediaServerItem mediaServerItem,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 {
- String ssrc = streamSession.createPlayBackSsrc();
- String streamId = String.format("%08x", Integer.parseInt(ssrc)).toUpperCase();
- Integer mediaPort = null;
- // 浣跨敤鍔ㄦ�乽dp绔彛
- if (mediaServerItem.isRtpEnable()) {
- mediaPort = zlmrtpServerFactory.createRTPServer(mediaServerItem, streamId);
- }else {
- mediaPort = mediaServerItem.getRtpProxyPort();
- }
- logger.info("{} 鍒嗛厤鐨刏LM涓�: {} [{}:{}]", streamId, mediaServerItem.getId(), mediaServerItem.getIp(), mediaPort);
+ logger.info("{} 鍒嗛厤鐨刏LM涓�: {} [{}:{}]", ssrcInfo.getStream(), mediaServerItem.getId(), mediaServerItem.getIp(), ssrcInfo.getPort());
// 娣诲姞璁㈤槄
JSONObject subscribeKey = new JSONObject();
subscribeKey.put("app", "rtp");
- subscribeKey.put("stream", streamId);
+ subscribeKey.put("stream", ssrcInfo.getStream());
subscribeKey.put("regist", true);
+ subscribeKey.put("schema", "rtmp");
subscribeKey.put("mediaServerId", mediaServerItem.getId());
logger.debug("褰曞儚鍥炴斁娣诲姞璁㈤槄锛岃闃呭唴瀹癸細" + subscribeKey.toString());
subscribe.addSubscribe(ZLMHttpHookSubscribe.HookType.on_stream_changed, subscribeKey,
- (IMediaServerItem mediaServerItemInUse, JSONObject json)->{
- if (userSetup.isWaitTrack() && json.getJSONArray("tracks") == null) return;
- event.response(mediaServerItemInUse, json);
- subscribe.removeSubscribe(ZLMHttpHookSubscribe.HookType.on_stream_changed, subscribeKey);
+ (MediaServerItem mediaServerItemInUse, JSONObject json)->{
+ if (event != null) {
+ event.response(mediaServerItemInUse, json);
+ }
});
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("o="+sipConfig.getId()+" 0 0 IN IP4 " + mediaServerItem.getSdpIp() + "\r\n");
content.append("s=Playback\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 "+ 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");
@@ -519,11 +504,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");
@@ -538,85 +523,178 @@
}
}
- 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);
+ Request request = headerProvider.createPlaybackInviteRequest(device, channelId, content.toString(), null, "fromplybck" + tm, null, callIdHeader, ssrcInfo.getSsrc());
+
+ transmitRequest(device, request, errorEvent, okEvent -> {
+ ResponseEvent responseEvent = (ResponseEvent) okEvent.event;
+ streamSession.put(device.getDeviceId(), channelId, callIdHeader.getCallId(), ssrcInfo.getStream(), ssrcInfo.getSsrc(), mediaServerItem.getId(), responseEvent.getClientTransaction());
+ streamSession.put(device.getDeviceId(), channelId, callIdHeader.getCallId(), okEvent.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.getStream(), mediaServerItem.getId(), mediaServerItem.getIp(), ssrcInfo.getPort());
+
+ // 娣诲姞璁㈤槄
+ JSONObject subscribeKey = new JSONObject();
+ subscribeKey.put("app", "rtp");
+ subscribeKey.put("stream", ssrcInfo.getStream());
+ 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)->{
+ 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.getId()+" 0 0 IN IP4 " + mediaServerItem.getSdpIp() + "\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());
+
+ CallIdHeader callIdHeader = device.getTransport().equals("TCP") ? tcpSipProvider.getNewCallId()
+ : udpSipProvider.getNewCallId();
+
+ Request request = headerProvider.createPlaybackInviteRequest(device, channelId, content.toString(), null, "fromplybck" + tm, null, callIdHeader, ssrcInfo.getSsrc());
ClientTransaction transaction = transmitRequest(device, request, errorEvent);
- streamSession.put(device.getDeviceId(), channelId, ssrc, streamId, transaction);
+ streamSession.put(device.getDeviceId(), channelId, callIdHeader.getCallId(), ssrcInfo.getStream(), ssrcInfo.getSsrc(), mediaServerItem.getId(), transaction);
+ streamSession.put(device.getDeviceId(), channelId, callIdHeader.getCallId(), ssrcInfo.getStream(), ssrcInfo.getSsrc(), mediaServerItem.getId(), transaction);
} catch ( SipException | ParseException | InvalidArgumentException e) {
e.printStackTrace();
}
}
-
-
/**
* 瑙嗛娴佸仠姝�, 涓嶄娇鐢ㄥ洖璋�
*/
@Override
- public void streamByeCmd(String deviceId, String channelId) {
- streamByeCmd(deviceId, channelId, null);
+ public void streamByeCmd(String deviceId, String channelId, String stream) {
+ streamByeCmd(deviceId, channelId, stream, null);
}
/**
* 瑙嗛娴佸仠姝�
*/
@Override
- public void streamByeCmd(String deviceId, String channelId, SipSubscribe.Event okEvent) {
- StreamInfo streamInfo = redisCatchStorage.queryPlayByDevice(deviceId, channelId);
+ public void streamByeCmd(String deviceId, String channelId, String stream, SipSubscribe.Event okEvent) {
try {
- ClientTransaction transaction = streamSession.getTransaction(deviceId, channelId);
- // 鏈嶅姟閲嶅惎鍚�, 鏃犳硶鐩存帴鍙戦�乥ye锛� 閫氳繃鎵嬪姩鏋勫缓鍙戦��
-// if (transaction == null) {
-//
-// if (streamInfo != null) {
-// MediaServerItem mediaServerItem = redisCatchStorage.getMediaInfo(streamInfo.getMediaServerId());
-// JSONObject mediaList = zlmresTfulUtils.getMediaList(mediaServerItem,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;
-// }
+ SsrcTransaction ssrcTransaction = streamSession.getSsrcTransaction(deviceId, channelId, null, stream);
+ ClientTransaction transaction = streamSession.getTransactionByStream(deviceId, channelId, stream);
if (transaction == null) {
logger.warn("[ {} -> {}]鍋滄瑙嗛娴佺殑鏃跺�欏彂鐜颁簨鍔″凡涓㈠け", deviceId, channelId);
+ SipSubscribe.EventResult<Object> eventResult = new SipSubscribe.EventResult<>();
+ if (okEvent != null) {
+ okEvent.response(eventResult);
+ }
return;
}
- Dialog dialog = transaction.getDialog();
+ SIPDialog dialog = streamSession.getDialogByStream(deviceId, channelId, stream);
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();
@@ -638,7 +716,12 @@
dialog.sendRequest(clientTransaction);
- streamSession.remove(deviceId, channelId);
+ if (ssrcTransaction != null) {
+ MediaServerItem mediaServerItem = mediaServerService.getOne(ssrcTransaction.getMediaServerId());
+ mediaServerService.releaseSsrc(mediaServerItem.getId(), ssrcTransaction.getSsrc());
+ mediaServerService.closeRTPServer(deviceId, channelId, ssrcTransaction.getStream());
+ streamSession.remove(deviceId, channelId, ssrcTransaction.getStream());
+ }
} catch (SipException | ParseException e) {
e.printStackTrace();
}
@@ -669,7 +752,7 @@
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("<SourceID>" + sipConfig.getId() + "</SourceID>\r\n");
broadcastXml.append("<TargetID>" + device.getDeviceId() + "</TargetID>\r\n");
broadcastXml.append("</Notify>\r\n");
@@ -694,7 +777,7 @@
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("<SourceID>" + sipConfig.getId() + "</SourceID>\r\n");
broadcastXml.append("<TargetID>" + device.getDeviceId() + "</TargetID>\r\n");
broadcastXml.append("</Notify>\r\n");
@@ -1086,8 +1169,6 @@
*/
@Override
public boolean catalogQuery(Device device, SipSubscribe.Event errorEvent) {
- // 娓呯┖閫氶亾
- storager.cleanChannelsForDevice(device.getDeviceId());
try {
StringBuffer catalogXml = new StringBuffer(200);
catalogXml.append("<?xml version=\"1.0\" encoding=\"GB2312\"?>\r\n");
@@ -1120,20 +1201,33 @@
* @param endTime 缁撴潫鏃堕棿,鏍煎紡瑕佹眰锛歽yyy-MM-dd HH:mm:ss
*/
@Override
- public boolean recordInfoQuery(Device device, String channelId, String startTime, String endTime) {
-
+ public boolean recordInfoQuery(Device device, String channelId, String startTime, String endTime, int sn, Integer secrecy, String type, SipSubscribe.Event okEvent, SipSubscribe.Event errorEvent) {
+ if (secrecy == null) {
+ secrecy = 0;
+ }
+ if (type == null) {
+ type = "all";
+ }
try {
StringBuffer recordInfoXml = new StringBuffer(200);
recordInfoXml.append("<?xml version=\"1.0\" encoding=\"GB2312\"?>\r\n");
recordInfoXml.append("<Query>\r\n");
recordInfoXml.append("<CmdType>RecordInfo</CmdType>\r\n");
- recordInfoXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>\r\n");
+ recordInfoXml.append("<SN>" + sn + "</SN>\r\n");
recordInfoXml.append("<DeviceID>" + channelId + "</DeviceID>\r\n");
- recordInfoXml.append("<StartTime>" + DateUtil.yyyy_MM_dd_HH_mm_ssToISO8601(startTime) + "</StartTime>\r\n");
- recordInfoXml.append("<EndTime>" + DateUtil.yyyy_MM_dd_HH_mm_ssToISO8601(endTime) + "</EndTime>\r\n");
- recordInfoXml.append("<Secrecy>0</Secrecy>\r\n");
- // 澶у崕NVR瑕佹眰蹇呴』澧炲姞涓�涓�间负all鐨勬枃鏈厓绱犺妭鐐筎ype
- recordInfoXml.append("<Type>all</Type>\r\n");
+ if (startTime != null) {
+ recordInfoXml.append("<StartTime>" + DateUtil.yyyy_MM_dd_HH_mm_ssToISO8601(startTime) + "</StartTime>\r\n");
+ }
+ if (endTime != null) {
+ recordInfoXml.append("<EndTime>" + DateUtil.yyyy_MM_dd_HH_mm_ssToISO8601(endTime) + "</EndTime>\r\n");
+ }
+ if (secrecy != null) {
+ recordInfoXml.append("<Secrecy> "+ secrecy + " </Secrecy>\r\n");
+ }
+ if (type != null) {
+ // 澶у崕NVR瑕佹眰蹇呴』澧炲姞涓�涓�间负all鐨勬枃鏈厓绱犺妭鐐筎ype
+ recordInfoXml.append("<Type>" + type+"</Type>\r\n");
+ }
recordInfoXml.append("</Query>\r\n");
String tm = Long.toString(System.currentTimeMillis());
@@ -1144,7 +1238,7 @@
Request request = headerProvider.createMessageRequest(device, recordInfoXml.toString(),
"z9hG4bK-ViaRecordInfo-" + tm, "fromRec" + tm, null, callIdHeader);
- transmitRequest(device, request);
+ transmitRequest(device, request, errorEvent, okEvent);
} catch (SipException | ParseException | InvalidArgumentException e) {
e.printStackTrace();
return false;
@@ -1406,6 +1500,64 @@
}
}
+ @Override
+ public boolean catalogSubscribe(Device device, SipSubscribe.Event okEvent, SipSubscribe.Event errorEvent) {
+ try {
+ StringBuffer cmdXml = new StringBuffer(200);
+ cmdXml.append("<?xml version=\"1.0\" encoding=\"GB2312\"?>\r\n");
+ cmdXml.append("<Query>\r\n");
+ cmdXml.append("<CmdType>Catalog</CmdType>\r\n");
+ cmdXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>\r\n");
+ cmdXml.append("<DeviceID>" + device.getDeviceId() + "</DeviceID>\r\n");
+ cmdXml.append("</Query>\r\n");
+
+ String tm = Long.toString(System.currentTimeMillis());
+
+ CallIdHeader callIdHeader = device.getTransport().equals("TCP") ? tcpSipProvider.getNewCallId()
+ : udpSipProvider.getNewCallId();
+
+ // 鏈夋晥鏃堕棿榛樿涓�60绉掍互涓�
+ Request request = headerProvider.createSubscribeRequest(device, cmdXml.toString(), "z9hG4bK-viaPos-" + tm,
+ "fromTagPos" + tm, null, device.getSubscribeCycleForCatalog(), "Catalog" ,
+ callIdHeader);
+ transmitRequest(device, request, errorEvent, okEvent);
+
+ return true;
+
+ } catch ( NumberFormatException | ParseException | InvalidArgumentException | SipException e) {
+ e.printStackTrace();
+ return false;
+ }
+ }
+
+ @Override
+ public boolean dragZoomCmd(Device device, String channelId, String cmdString) {
+ try {
+ StringBuffer dragXml = new StringBuffer(200);
+ dragXml.append("<?xml version=\"1.0\" ?>\r\n");
+ dragXml.append("<Control>\r\n");
+ dragXml.append("<CmdType>DeviceControl</CmdType>\r\n");
+ dragXml.append("<SN>" + (int) ((Math.random() * 9 + 1) * 100000) + "</SN>\r\n");
+ if (StringUtils.isEmpty(channelId)) {
+ dragXml.append("<DeviceID>" + device.getDeviceId() + "</DeviceID>\r\n");
+ } else {
+ dragXml.append("<DeviceID>" + channelId + "</DeviceID>\r\n");
+ }
+ dragXml.append(cmdString);
+ dragXml.append("</Control>\r\n");
+ String tm = Long.toString(System.currentTimeMillis());
+ CallIdHeader callIdHeader = device.getTransport().equals("TCP") ? tcpSipProvider.getNewCallId()
+ : udpSipProvider.getNewCallId();
+ Request request = headerProvider.createMessageRequest(device, dragXml.toString(), "z9hG4bK-ViaPtz-" + tm, "FromPtz" + tm, null, callIdHeader);
+ logger.debug("鎷夋淇′护锛� " + request.toString());
+ transmitRequest(device, request);
+ return true;
+ } catch (SipException | ParseException | InvalidArgumentException e) {
+ e.printStackTrace();
+ }
+ return false;
+ }
+
private ClientTransaction transmitRequest(Device device, Request request) throws SipException {
return transmitRequest(device, request, null, null);
@@ -1426,14 +1578,136 @@
CallIdHeader callIdHeader = (CallIdHeader)request.getHeader(CallIdHeader.NAME);
// 娣诲姞閿欒璁㈤槄
if (errorEvent != null) {
- sipSubscribe.addErrorSubscribe(callIdHeader.getCallId(), errorEvent);
+ sipSubscribe.addErrorSubscribe(callIdHeader.getCallId(), (eventResult -> {
+ errorEvent.response(eventResult);
+ sipSubscribe.removeErrorSubscribe(eventResult.callId);
+ }));
}
// 娣诲姞璁㈤槄
if (okEvent != null) {
- sipSubscribe.addOkSubscribe(callIdHeader.getCallId(), okEvent);
+ sipSubscribe.addOkSubscribe(callIdHeader.getCallId(), eventResult ->{
+ okEvent.response(eventResult);
+ sipSubscribe.removeOkSubscribe(eventResult.callId);
+ });
}
clientTransaction.sendRequest();
return clientTransaction;
}
+
+ /**
+ * 鍥炴斁鏆傚仠
+ */
+ @Override
+ public void playPauseCmd(Device device, StreamInfo streamInfo) {
+ try {
+ Long cseq = redisCatchStorage.getCSEQ(Request.INFO);
+ StringBuffer content = new StringBuffer(200);
+ content.append("PAUSE RTSP/1.0\r\n");
+ content.append("CSeq: " + cseq + "\r\n");
+ content.append("PauseTime: now\r\n");
+ Request request = headerProvider.createInfoRequest(device, streamInfo, content.toString(), cseq);
+ if (request == null) {
+ return;
+ }
+ logger.info(request.toString());
+ ClientTransaction clientTransaction = null;
+ if ("TCP".equals(device.getTransport())) {
+ clientTransaction = tcpSipProvider.getNewClientTransaction(request);
+ } else if ("UDP".equals(device.getTransport())) {
+ clientTransaction = udpSipProvider.getNewClientTransaction(request);
+ }
+ if (clientTransaction != null) {
+ clientTransaction.sendRequest();
+ }
+
+ } catch (SipException | ParseException | InvalidArgumentException e) {
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * 鍥炴斁鎭㈠
+ */
+ @Override
+ public void playResumeCmd(Device device, StreamInfo streamInfo) {
+ try {
+ Long cseq = redisCatchStorage.getCSEQ(Request.INFO);
+ StringBuffer content = new StringBuffer(200);
+ content.append("PLAY RTSP/1.0\r\n");
+ content.append("CSeq: " + cseq + "\r\n");
+ content.append("Range: npt=now-\r\n");
+ Request request = headerProvider.createInfoRequest(device, streamInfo, content.toString(), cseq);
+ if (request == null) return;
+ logger.info(request.toString());
+ ClientTransaction clientTransaction = null;
+ if ("TCP".equals(device.getTransport())) {
+ clientTransaction = tcpSipProvider.getNewClientTransaction(request);
+ } else if ("UDP".equals(device.getTransport())) {
+ clientTransaction = udpSipProvider.getNewClientTransaction(request);
+ }
+
+ clientTransaction.sendRequest();
+
+ } catch (SipException | ParseException | InvalidArgumentException e) {
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * 鍥炴斁鎷栧姩鎾斁
+ */
+ @Override
+ public void playSeekCmd(Device device, StreamInfo streamInfo, long seekTime) {
+ try {
+ Long cseq = redisCatchStorage.getCSEQ(Request.INFO);
+ StringBuffer content = new StringBuffer(200);
+ content.append("PLAY RTSP/1.0\r\n");
+ content.append("CSeq: " + cseq + "\r\n");
+ content.append("Range: npt=" + Math.abs(seekTime) + "-\r\n");
+
+ Request request = headerProvider.createInfoRequest(device, streamInfo, content.toString(), cseq);
+ if (request == null) return;
+ logger.info(request.toString());
+ ClientTransaction clientTransaction = null;
+ if ("TCP".equals(device.getTransport())) {
+ clientTransaction = tcpSipProvider.getNewClientTransaction(request);
+ } else if ("UDP".equals(device.getTransport())) {
+ clientTransaction = udpSipProvider.getNewClientTransaction(request);
+ }
+
+ clientTransaction.sendRequest();
+
+ } catch (SipException | ParseException | InvalidArgumentException e) {
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * 鍥炴斁鍊嶉�熸挱鏀�
+ */
+ @Override
+ public void playSpeedCmd(Device device, StreamInfo streamInfo, Double speed) {
+ try {
+ Long cseq = redisCatchStorage.getCSEQ(Request.INFO);
+ StringBuffer content = new StringBuffer(200);
+ content.append("PLAY RTSP/1.0\r\n");
+ content.append("CSeq: " + cseq + "\r\n");
+ content.append("Scale: " + String.format("%.1f",speed) + "\r\n");
+ Request request = headerProvider.createInfoRequest(device, streamInfo, content.toString(), cseq);
+ if (request == null) return;
+ logger.info(request.toString());
+ ClientTransaction clientTransaction = null;
+ if ("TCP".equals(device.getTransport())) {
+ clientTransaction = tcpSipProvider.getNewClientTransaction(request);
+ } else if ("UDP".equals(device.getTransport())) {
+ clientTransaction = udpSipProvider.getNewClientTransaction(request);
+ }
+
+ clientTransaction.sendRequest();
+
+ } catch (SipException | ParseException | InvalidArgumentException e) {
+ e.printStackTrace();
+ }
+ }
}
--
Gitblit v1.8.0