From 8da6906e40c8a96a94624b468833bebc2c3a3d5c Mon Sep 17 00:00:00 2001
From: Lawrence <1934378145@qq.com>
Date: 星期三, 20 一月 2021 20:43:40 +0800
Subject: [PATCH] 修改功能说明
---
src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommander.java | 614 +++++++++++++++++++++++++++++++++++++++++++++----------
1 files changed, 495 insertions(+), 119 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 b012f9a..7ff5d14 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,33 +1,49 @@
package com.genersoft.iot.vmp.gb28181.transmit.cmd.impl;
import java.text.ParseException;
-import java.util.Random;
+import java.util.UUID;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
-import javax.sip.InvalidArgumentException;
-import javax.sip.SipException;
+import javax.sip.*;
+import javax.sip.address.SipURI;
+import javax.sip.header.CallIdHeader;
+import javax.sip.header.Header;
+import javax.sip.header.ViaHeader;
import javax.sip.message.Request;
+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.bean.DeviceChannel;
+import com.genersoft.iot.vmp.gb28181.event.SipSubscribe;
+import com.genersoft.iot.vmp.media.zlm.ZLMHttpHookSubscribe;
+import com.genersoft.iot.vmp.media.zlm.ZLMRTPServerFactory;
+import com.genersoft.iot.vmp.storager.IRedisCatchStorage;
+import com.genersoft.iot.vmp.storager.IVideoManagerStorager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.context.ApplicationEventPublisher;
+import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import com.genersoft.iot.vmp.conf.SipConfig;
-import com.genersoft.iot.vmp.gb28181.SipLayer;
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.SsrcUtil;
-
-import tk.mybatis.mapper.util.StringUtil;
/**
* @Description:璁惧鑳藉姏鎺ュ彛锛岀敤浜庡畾涔夎澶囩殑鎺у埗銆佹煡璇㈣兘鍔�
- * @author: songww
+ * @author: swwheihei
* @date: 2020骞�5鏈�3鏃� 涓嬪崍9:22:48
*/
@Component
public class SIPCommander implements ISIPCommander {
+
+ private final Logger logger = LoggerFactory.getLogger(SIPCommander.class);
@Autowired
private SipConfig sipConfig;
@@ -36,8 +52,42 @@
private SIPRequestHeaderProvider headerProvider;
@Autowired
- private SipLayer sipLayer;
+ private VideoStreamSessionManager streamSession;
+
+ @Autowired
+ private IVideoManagerStorager storager;
+
+ @Autowired
+ private IRedisCatchStorage redisCatchStorage;
+ @Autowired
+ @Qualifier(value="tcpSipProvider")
+ private SipProvider tcpSipProvider;
+
+ @Autowired
+ @Qualifier(value="udpSipProvider")
+ private SipProvider udpSipProvider;
+
+ @Autowired
+ private ZLMRTPServerFactory zlmrtpServerFactory;
+
+ @Value("${media.rtp.enable}")
+ private boolean rtpEnable;
+
+ @Value("${media.seniorSdp}")
+ private boolean seniorSdp;
+
+ @Value("${media.autoApplyPlay}")
+ private boolean autoApplyPlay;
+
+ @Autowired
+ private ZLMHttpHookSubscribe subscribe;
+
+ @Autowired
+ private SipSubscribe sipSubscribe;
+
+
+
/**
* 浜戝彴鏂瑰悜鏀炬帶鍒讹紝浣跨敤閰嶇疆鏂囦欢涓殑榛樿闀滃ご绉诲姩閫熷害
*
@@ -45,7 +95,6 @@
* @param channelId 棰勮閫氶亾
* @param leftRight 闀滃ご宸︾Щ鍙崇Щ 0:鍋滄 1:宸︾Щ 2:鍙崇Щ
* @param upDown 闀滃ご涓婄Щ涓嬬Щ 0:鍋滄 1:涓婄Щ 2:涓嬬Щ
- * @param moveSpeed 闀滃ご绉诲姩閫熷害
*/
@Override
public boolean ptzdirectCmd(Device device, String channelId, int leftRight, int upDown) {
@@ -91,36 +140,104 @@
return ptzCmd(device, channelId, 0, 0, inOut, 0, zoomSpeed);
}
+ /**
+ * 浜戝彴鎸囦护鐮佽绠�
+ *
+ * @param leftRight 闀滃ご宸︾Щ鍙崇Щ 0:鍋滄 1:宸︾Щ 2:鍙崇Щ
+ * @param upDown 闀滃ご涓婄Щ涓嬬Щ 0:鍋滄 1:涓婄Щ 2:涓嬬Щ
+ * @param inOut 闀滃ご鏀惧ぇ缂╁皬 0:鍋滄 1:缂╁皬 2:鏀惧ぇ
+ * @param moveSpeed 闀滃ご绉诲姩閫熷害 榛樿 0XFF (0-255)
+ * @param zoomSpeed 闀滃ご缂╂斁閫熷害 榛樿 0X1 (0-255)
+ */
+ public static String cmdString(int leftRight, int upDown, int inOut, int moveSpeed, int zoomSpeed) {
+ int cmdCode = 0;
+ if (leftRight == 2) {
+ cmdCode|=0x01; // 鍙崇Щ
+ } else if(leftRight == 1) {
+ cmdCode|=0x02; // 宸︾Щ
+ }
+ if (upDown == 2) {
+ cmdCode|=0x04; // 涓嬬Щ
+ } else if(upDown == 1) {
+ cmdCode|=0x08; // 涓婄Щ
+ }
+ if (inOut == 2) {
+ cmdCode |= 0x10; // 鏀惧ぇ
+ } else if(inOut == 1) {
+ cmdCode |= 0x20; // 缂╁皬
+ }
+ StringBuilder builder = new StringBuilder("A50F01");
+ String strTmp;
+ strTmp = String.format("%02X", cmdCode);
+ builder.append(strTmp, 0, 2);
+ strTmp = String.format("%02X", moveSpeed);
+ builder.append(strTmp, 0, 2);
+ builder.append(strTmp, 0, 2);
+ strTmp = String.format("%X", zoomSpeed);
+ builder.append(strTmp, 0, 1).append("0");
+ //璁$畻鏍¢獙鐮�
+ int checkCode = (0XA5 + 0X0F + 0X01 + cmdCode + moveSpeed + moveSpeed + (zoomSpeed /*<< 4*/ & 0XF0)) % 0X100;
+ strTmp = String.format("%02X", checkCode);
+ builder.append(strTmp, 0, 2);
+ return builder.toString();
+}
+
+ /**
+ * 浜戝彴鎸囦护鐮佽绠�
+ *
+ * @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;
+ strTmp = String.format("%02X", cmdCode);
+ builder.append(strTmp, 0, 2);
+ strTmp = String.format("%02X", parameter1);
+ builder.append(strTmp, 0, 2);
+ strTmp = String.format("%02X", parameter2);
+ builder.append(strTmp, 0, 2);
+ strTmp = String.format("%X", combineCode2);
+ builder.append(strTmp, 0, 1).append("0");
+ //璁$畻鏍¢獙鐮�
+ int checkCode = (0XA5 + 0X0F + 0X01 + cmdCode + parameter1 + parameter2 + (combineCode2 & 0XF0)) % 0X100;
+ strTmp = String.format("%02X", checkCode);
+ builder.append(strTmp, 0, 2);
+ return builder.toString();
+ }
+
/**
* 浜戝彴鎺у埗锛屾敮鎸佹柟鍚戜笌缂╂斁鎺у埗
*
- * @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,
int zoomSpeed) {
try {
+ String cmdStr= cmdString(leftRight, upDown, inOut, moveSpeed, zoomSpeed);
StringBuffer ptzXml = new StringBuffer(200);
- ptzXml.append("<?xml version=\"1.0\" ?>");
- ptzXml.append("<Control>");
- ptzXml.append("<CmdType>DeviceControl</CmdType>");
- ptzXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>");
- ptzXml.append("<DeviceID>" + channelId + "</DeviceID>");
- ptzXml.append("<PTZCmd>" + PtzCmdHelper.create(leftRight, upDown, inOut, moveSpeed, zoomSpeed) + "</PTZCmd>");
- ptzXml.append("<Info>");
- ptzXml.append("</Info>");
- ptzXml.append("</Control>");
+ ptzXml.append("<?xml version=\"1.0\" ?>\r\n");
+ ptzXml.append("<Control>\r\n");
+ ptzXml.append("<CmdType>DeviceControl</CmdType>\r\n");
+ ptzXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>\r\n");
+ ptzXml.append("<DeviceID>" + channelId + "</DeviceID>\r\n");
+ ptzXml.append("<PTZCmd>" + cmdStr + "</PTZCmd>\r\n");
+ ptzXml.append("<Info>\r\n");
+ ptzXml.append("</Info>\r\n");
+ ptzXml.append("</Control>\r\n");
- Request request = headerProvider.createMessageRequest(device, ptzXml.toString(), "ViaPtzBranch", "FromPtzTag", "ToPtzTag");
+ Request request = headerProvider.createMessageRequest(device, ptzXml.toString(), "ViaPtzBranch", "FromPtzTag", null);
transmitRequest(device, request);
-
return true;
} catch (SipException | ParseException | InvalidArgumentException e) {
e.printStackTrace();
@@ -129,47 +246,153 @@
}
/**
- * 璇锋眰棰勮瑙嗛娴�
+ * 鍓嶇鎺у埗锛屽寘鎷琍TZ鎸囦护銆丗I鎸囦护銆侀缃綅鎸囦护銆佸贰鑸寚浠ゃ�佹壂鎻忔寚浠ゅ拰杈呭姪寮�鍏虫寚浠�
*
+ * @param device 鎺у埗璁惧
+ * @param channelId 棰勮閫氶亾
+ * @param cmdCode 鎸囦护鐮�
+ * @param parameter1 鏁版嵁1
+ * @param parameter2 鏁版嵁2
+ * @param combineCode2 缁勫悎鐮�2
+ */
+ @Override
+ 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);
+ StringBuffer ptzXml = new StringBuffer(200);
+ ptzXml.append("<?xml version=\"1.0\" ?>\r\n");
+ ptzXml.append("<Control>\r\n");
+ ptzXml.append("<CmdType>DeviceControl</CmdType>\r\n");
+ ptzXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>\r\n");
+ ptzXml.append("<DeviceID>" + channelId + "</DeviceID>\r\n");
+ ptzXml.append("<PTZCmd>" + cmdStr + "</PTZCmd>\r\n");
+ ptzXml.append("<Info>\r\n");
+ ptzXml.append("</Info>\r\n");
+ ptzXml.append("</Control>\r\n");
+
+ Request request = headerProvider.createMessageRequest(device, ptzXml.toString(), "ViaPtzBranch", "FromPtzTag", null);
+ transmitRequest(device, request);
+ return true;
+ } catch (SipException | ParseException | InvalidArgumentException e) {
+ e.printStackTrace();
+ }
+ return false;
+ }
+
+ /**
+ * 璇锋眰棰勮瑙嗛娴�
* @param device 瑙嗛璁惧
* @param channelId 棰勮閫氶亾
- */
+ * @param event hook璁㈤槄
+ * @param errorEvent sip閿欒璁㈤槄
+ */
@Override
- public String playStreamCmd(Device device, String channelId) {
+ public void playStreamCmd(Device device, String channelId, ZLMHttpHookSubscribe.Event event, SipSubscribe.Event errorEvent) {
try {
-
- String ssrc = SsrcUtil.getPlaySsrc();
+
+ String ssrc = streamSession.createPlaySsrc();
+ String streamId = null;
+ 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();
+ }
+
+ // 娣诲姞璁㈤槄
+ JSONObject subscribeKey = new JSONObject();
+ subscribeKey.put("app", "rtp");
+ subscribeKey.put("id", streamId);
+
+ subscribe.addSubscribe(ZLMHttpHookSubscribe.HookType.on_publish, subscribeKey, event);
//
StringBuffer content = new StringBuffer(200);
- content.append("v=0\r\n");
- content.append("o="+channelId+" 0 0 IN IP4 "+sipConfig.getSipIp()+"\r\n");
- content.append("s=Play\r\n");
- content.append("c=IN IP4 "+sipConfig.getMediaIp()+"\r\n");
- content.append("t=0 0\r\n");
- if(device.getTransport().equals("TCP")) {
- content.append("m=video "+sipConfig.getMediaPort()+" TCP/RTP/AVP 96 98 97\r\n");
+ content.append("v=0\r\n");
+// content.append("o="+channelId+" 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");
+ content.append("t=0 0\r\n");
+
+ if (seniorSdp) {
+ if("TCP-PASSIVE".equals(streamMode)) {
+ 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 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("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 "+ mediaPort +" 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");
+ }else if("UDP".equals(streamMode)) {
+ content.append("m=video "+ mediaPort +" 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=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涓诲姩妯″紡
+ } else if ("TCP-ACTIVE".equals(streamMode)) { // tcp涓诲姩妯″紡
+ content.append("a=setup:active\r\n");
+ content.append("a=connection:new\r\n");
+ }
+ }
}
- if(device.getTransport().equals("UDP")) {
- content.append("m=video "+sipConfig.getMediaPort()+" RTP/AVP 96 98 97\r\n");
- }
- content.append("a=sendrecv\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(device.getTransport().equals("TCP")){
- content.append("a=setup:passive\r\n");
- content.append("a=connection:new\r\n");
- }
- content.append("y="+ssrc+"\r\n");//ssrc
-
- Request request = headerProvider.createInviteRequest(device, content.toString(), null, "live", null);
-
- transmitRequest(device, request);
- return ssrc;
+
+ content.append("y="+ssrc+"\r\n");//ssrc
+
+ Request request = headerProvider.createInviteRequest(device, channelId, content.toString(), null, "live", null, ssrc);
+
+ ClientTransaction transaction = transmitRequest(device, request, errorEvent);
+ streamSession.put(streamId, transaction);
+
+
+
} catch ( SipException | ParseException | InvalidArgumentException e) {
e.printStackTrace();
- return null;
- }
+ }
}
/**
@@ -181,41 +404,157 @@
* @param endTime 缁撴潫鏃堕棿,鏍煎紡瑕佹眰锛歽yyy-MM-dd HH:mm:ss
*/
@Override
- public String playbackStreamCmd(Device device, String channelId, String recordId, String startTime, String endTime) {
+ public void playbackStreamCmd(Device device, String channelId, String startTime, String endTime, ZLMHttpHookSubscribe.Event event
+ , SipSubscribe.Event errorEvent) {
try {
-
- String ssrc = SsrcUtil.getPlayBackSsrc();
- //
+ MediaServerConfig mediaInfo = redisCatchStorage.getMediaInfo();
+ String ssrc = streamSession.createPlayBackSsrc();
+ String streamId = String.format("%08x", Integer.parseInt(ssrc)).toUpperCase();
+ // 娣诲姞璁㈤槄
+ JSONObject subscribeKey = new JSONObject();
+ subscribeKey.put("app", "rtp");
+ subscribeKey.put("id", streamId);
+
+ subscribe.addSubscribe(ZLMHttpHookSubscribe.HookType.on_publish, subscribeKey, event);
+
StringBuffer content = new StringBuffer(200);
content.append("v=0\r\n");
- content.append("o="+channelId+" 0 0 IN IP4 "+sipConfig.getSipIp()+"\r\n");
+ content.append("o="+sipConfig.getSipId()+" 0 0 IN IP4 "+sipConfig.getSipIp()+"\r\n");
content.append("s=Playback\r\n");
- content.append("u="+recordId+":3\r\n");
- content.append("c=IN IP4 "+sipConfig.getMediaIp()+"\r\n");
- content.append("t="+DateUtil.yyyy_MM_dd_HH_mm_ssToTimestamp(startTime)+" "+DateUtil.yyyy_MM_dd_HH_mm_ssToTimestamp(endTime) +"\r\n");
- if(device.getTransport().equals("TCP")) {
- content.append("m=video "+sipConfig.getMediaPort()+" TCP/RTP/AVP 96 98 97\r\n");
+ content.append("u="+channelId+":0\r\n");
+ content.append("c=IN IP4 "+mediaInfo.getWanIp()+"\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();
}
- if(device.getTransport().equals("UDP")) {
- content.append("m=video "+sipConfig.getMediaPort()+" RTP/AVP 96 98 97\r\n");
+ String streamMode = device.getStreamMode().toUpperCase();
+
+ if (seniorSdp) {
+ if("TCP-PASSIVE".equals(streamMode)) {
+ 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 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("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 "+ mediaPort +" 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");
+ }else if("UDP".equals(streamMode)) {
+ content.append("m=video "+ mediaPort +" 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=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(device.getTransport().equals("TCP")){
- content.append("a=setup:passive\r\n");
- content.append("a=connection:new\r\n");
- }
+
content.append("y="+ssrc+"\r\n");//ssrc
- Request request = headerProvider.createInviteRequest(device, content.toString(), null, "live", null);
-
- transmitRequest(device, request);
- return ssrc;
+ Request request = headerProvider.createPlaybackInviteRequest(device, channelId, content.toString(), null, "playback", null);
+
+ ClientTransaction transaction = transmitRequest(device, request, errorEvent);
+ streamSession.put(streamId, transaction);
+
} catch ( SipException | ParseException | InvalidArgumentException e) {
e.printStackTrace();
- return null;
+ }
+ }
+
+
+
+ /**
+ * 瑙嗛娴佸仠姝�
+ *
+ */
+ @Override
+ public void streamByeCmd(String ssrc) {
+ streamByeCmd(ssrc, null);
+ }
+ @Override
+ public void streamByeCmd(String streamId, SipSubscribe.Event okEvent) {
+
+ try {
+ ClientTransaction transaction = streamSession.get(streamId);
+ // 鏈嶅姟閲嶅惎鍚�
+ if (transaction == null) {
+ StreamInfo streamInfo = redisCatchStorage.queryPlayByStreamId(streamId);
+ if (streamInfo != null) {
+
+ }
+ return;
+ }
+
+ Dialog dialog = transaction.getDialog();
+ 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()) {
+ 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();
+ ClientTransaction clientTransaction = null;
+ if("TCP".equals(protocol)) {
+ clientTransaction = tcpSipProvider.getNewClientTransaction(byeRequest);
+ } else if("UDP".equals(protocol)) {
+ clientTransaction = udpSipProvider.getNewClientTransaction(byeRequest);
+ }
+
+ CallIdHeader callIdHeader = (CallIdHeader) byeRequest.getHeader(CallIdHeader.NAME);
+ if (okEvent != null) {
+ sipSubscribe.addOkSubscribe(callIdHeader.getCallId(), okEvent);
+ }
+
+ dialog.sendRequest(clientTransaction);
+
+ streamSession.remove(streamId);
+ zlmrtpServerFactory.closeRTPServer(streamId);
+ } catch (TransactionDoesNotExistException e) {
+ e.printStackTrace();
+ } catch (SipException e) {
+ e.printStackTrace();
+ } catch (ParseException e) {
+ e.printStackTrace();
}
}
@@ -319,14 +658,15 @@
public boolean deviceInfoQuery(Device device) {
try {
StringBuffer catalogXml = new StringBuffer(200);
- catalogXml.append("<?xml version=\"1.0\" encoding=\"GB2312\"?>");
- catalogXml.append("<Query>");
- catalogXml.append("<CmdType>DeviceInfo</CmdType>");
- catalogXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>");
- catalogXml.append("<DeviceID>" + device.getDeviceId() + "</DeviceID>");
- catalogXml.append("</Query>");
+ catalogXml.append("<?xml version=\"1.0\" encoding=\"GB2312\"?>\r\n");
+ catalogXml.append("<Query>\r\n");
+ catalogXml.append("<CmdType>DeviceInfo</CmdType>\r\n");
+ catalogXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>\r\n");
+ catalogXml.append("<DeviceID>" + device.getDeviceId() + "</DeviceID>\r\n");
+ catalogXml.append("</Query>\r\n");
- Request request = headerProvider.createMessageRequest(device, catalogXml.toString(), "ViaDeviceInfoBranch", "FromDeviceInfoTag", "ToDeviceInfoTag");
+ Request request = headerProvider.createMessageRequest(device, catalogXml.toString(), "ViaDeviceInfoBranch", "FromDeviceInfoTag", null);
+
transmitRequest(device, request);
} catch (SipException | ParseException | InvalidArgumentException e) {
@@ -342,18 +682,21 @@
* @param device 瑙嗛璁惧
*/
@Override
- public boolean catalogQuery(Device device) {
+ 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\"?>");
- catalogXml.append("<Query>");
- catalogXml.append("<CmdType>Catalog</CmdType>");
- catalogXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>");
- catalogXml.append("<DeviceID>" + device.getDeviceId() + "</DeviceID>");
- catalogXml.append("</Query>");
+ catalogXml.append("<?xml version=\"1.0\" encoding=\"GB2312\"?>\r\n");
+ catalogXml.append("<Query>\r\n");
+ catalogXml.append("<CmdType>Catalog</CmdType>\r\n");
+ catalogXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>\r\n");
+ catalogXml.append("<DeviceID>" + device.getDeviceId() + "</DeviceID>\r\n");
+ catalogXml.append("</Query>\r\n");
- Request request = headerProvider.createMessageRequest(device, catalogXml.toString(), "ViaCatalogBranch", "FromCatalogTag", "ToCatalogTag");
- transmitRequest(device, request);
+ Request request = headerProvider.createMessageRequest(device, catalogXml.toString(), "ViaCatalogBranch", "FromCatalogTag", null);
+
+ transmitRequest(device, request, errorEvent);
} catch (SipException | ParseException | InvalidArgumentException e) {
e.printStackTrace();
return false;
@@ -373,19 +716,20 @@
try {
StringBuffer recordInfoXml = new StringBuffer(200);
- recordInfoXml.append("<?xml version=\"1.0\" encoding=\"GB2312\"?>");
- recordInfoXml.append("<Query>");
- recordInfoXml.append("<CmdType>RecordInfo</CmdType>");
- recordInfoXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>");
- recordInfoXml.append("<DeviceID>" + channelId + "</DeviceID>");
- recordInfoXml.append("<StartTime>" + DateUtil.yyyy_MM_dd_HH_mm_ssToISO8601(startTime) + "</StartTime>");
- recordInfoXml.append("<EndTime>" + DateUtil.yyyy_MM_dd_HH_mm_ssToISO8601(endTime) + "</EndTime>");
- recordInfoXml.append("<Secrecy>0</Secrecy>");
+ 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("<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>");
- recordInfoXml.append("</Query>");
+ recordInfoXml.append("<Type>all</Type>\r\n");
+ recordInfoXml.append("</Query>\r\n");
- Request request = headerProvider.createMessageRequest(device, recordInfoXml.toString(), "ViaRecordInfoBranch", "FromRecordInfoTag", "ToRecordInfoTag");
+ Request request = headerProvider.createMessageRequest(device, recordInfoXml.toString(), "ViaRecordInfoBranch", "FromRecordInfoTag", null);
+
transmitRequest(device, request);
} catch (SipException | ParseException | InvalidArgumentException e) {
e.printStackTrace();
@@ -437,13 +781,45 @@
// TODO Auto-generated method stub
return false;
}
-
- private void transmitRequest(Device device, Request request) throws SipException {
- if(device.getTransport().equals("TCP")) {
- sipLayer.getTcpSipProvider().sendRequest(request);
- } else if(device.getTransport().equals("UDP")) {
- sipLayer.getUdpSipProvider().sendRequest(request);
- }
+
+ private ClientTransaction transmitRequest(Device device, Request request) throws SipException {
+ return transmitRequest(device, request, null, null);
}
+ private ClientTransaction transmitRequest(Device device, Request request, SipSubscribe.Event errorEvent) throws SipException {
+ return transmitRequest(device, request, errorEvent, null);
+ }
+
+ private ClientTransaction transmitRequest(Device device, Request request, SipSubscribe.Event errorEvent , SipSubscribe.Event okEvent) throws SipException {
+ ClientTransaction clientTransaction = null;
+ if("TCP".equals(device.getTransport())) {
+ clientTransaction = tcpSipProvider.getNewClientTransaction(request);
+ } else if("UDP".equals(device.getTransport())) {
+ clientTransaction = udpSipProvider.getNewClientTransaction(request);
+ }
+
+ CallIdHeader callIdHeader = (CallIdHeader)request.getHeader(CallIdHeader.NAME);
+ // 娣诲姞閿欒璁㈤槄
+ if (errorEvent != null) {
+ sipSubscribe.addErrorSubscribe(callIdHeader.getCallId(), errorEvent);
+ }
+ // 娣诲姞璁㈤槄
+ if (okEvent != null) {
+ sipSubscribe.addOkSubscribe(callIdHeader.getCallId(), okEvent);
+ }
+
+ 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);
+ }
+ }
}
--
Gitblit v1.8.0