From 00e61d9a80b5b013a75d1ce567dc997694577a45 Mon Sep 17 00:00:00 2001 From: panlinlin <648540858@qq.com> Date: 星期一, 28 十二月 2020 11:45:40 +0800 Subject: [PATCH] 增加对 需要扩展SDP的设备的单独设置 --- src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommander.java | 310 +++++++++++++++++++++++++++++++++++++-------------- 1 files changed, 225 insertions(+), 85 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 047918c..3f0adfd 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 @@ -4,23 +4,22 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; -import javax.sip.ClientTransaction; -import javax.sip.Dialog; -import javax.sip.InvalidArgumentException; -import javax.sip.SipException; -import javax.sip.SipFactory; -import javax.sip.SipProvider; -import javax.sip.TransactionDoesNotExistException; +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.media.zlm.ZLMUtils; +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.IVideoManagerStorager; +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; @@ -40,6 +39,8 @@ */ @Component public class SIPCommander implements ISIPCommander { + + private final Logger logger = LoggerFactory.getLogger(SIPCommander.class); @Autowired private SipConfig sipConfig; @@ -62,10 +63,19 @@ private SipProvider udpSipProvider; @Autowired - private ZLMUtils zlmUtils; + private ZLMRTPServerFactory zlmrtpServerFactory; @Value("${media.rtp.enable}") private boolean rtpEnable; + + @Value("${media.seniorSdp}") + private boolean seniorSdp; + + @Autowired + private ZLMHttpHookSubscribe subscribe; + + @Autowired + private SipSubscribe sipSubscribe; @@ -163,6 +173,32 @@ return builder.toString(); } + /** + * 浜戝彴鎸囦护鐮佽绠� + * + * @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 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(); + } /** * 浜戝彴鎺у埗锛屾敮鎸佹柟鍚戜笌缂╂斁鎺у埗 @@ -193,7 +229,7 @@ Request request = headerProvider.createMessageRequest(device, ptzXml.toString(), "ViaPtzBranch", "FromPtzTag", "ToPtzTag"); - transmitRequest(device, request); + transmitRequest(device, request, null); return true; } catch (SipException | ParseException | InvalidArgumentException e) { e.printStackTrace(); @@ -202,72 +238,126 @@ } /** - * 璇锋眰棰勮瑙嗛娴� + * 鍓嶇鎺у埗锛屽寘鎷琍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", "ToPtzTag"); + transmitRequest(device, request, null); + return true; + } catch (SipException | ParseException | InvalidArgumentException e) { + e.printStackTrace(); + } + return false; + } + + /** + * 璇锋眰棰勮瑙嗛娴� * @param device 瑙嗛璁惧 * @param channelId 棰勮閫氶亾 - */ + * @param event hook璁㈤槄 + * @param errorEvent sip閿欒璁㈤槄 + */ @Override - public StreamInfo playStreamCmd(Device device, String channelId) { + public void playStreamCmd(Device device, String channelId, ZLMHttpHookSubscribe.Event event, SipSubscribe.Event errorEvent) { try { 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 = storager.getMediaInfo(); + if (mediaInfo == null) { + logger.warn("鐐规挱鏃跺彂鐜癦LM灏氭湭杩炴帴..."); + return; + } String mediaPort = null; // 浣跨敤鍔ㄦ�乽dp绔彛 if (rtpEnable) { - mediaPort = zlmUtils.getNewRTPPort(ssrc) + ""; + 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 "+mediaInfo.getLocalIP()+"\r\n"); - content.append("s=Play\r\n"); - content.append("c=IN IP4 "+mediaInfo.getLocalIP()+"\r\n"); - content.append("t=0 0\r\n"); - if("TCP-PASSIVE".equals(streamMode)) { - content.append("m=video "+ mediaPort +" 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("TCP-PASSIVE".equals(streamMode)) { + content.append("m=video "+ mediaPort +" TCP/RTP/AVP 126 125 99 34 98 97 96\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 "+ mediaPort +" TCP/RTP/AVP 126 125 99 34 98 97 96\r\n"); }else if("UDP".equals(streamMode)) { - content.append("m=video "+ mediaPort +" RTP/AVP 96 98 97\r\n"); + content.append("m=video "+ mediaPort +" RTP/AVP 126 125 99 34 98 97 96\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=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"); + content.append("a=rtpmap:96 PS/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"); } - content.append("y="+ssrc+"\r\n");//ssrc - - Request request = headerProvider.createInviteRequest(device, channelId, content.toString(), null, "live", null); - - ClientTransaction transaction = transmitRequest(device, request); - streamSession.put(ssrc, transaction); + 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); DeviceChannel deviceChannel = storager.queryChannel(device.getDeviceId(), channelId); if (deviceChannel != null) { - deviceChannel.setSsrc(ssrc); + deviceChannel.setStreamId(streamId); storager.updateChannel(device.getDeviceId(), deviceChannel); } - StreamInfo streamInfo = new StreamInfo(); - streamInfo.setSsrc(ssrc); - streamInfo.setCahnnelId(channelId); - streamInfo.setDeviceID(device.getDeviceId()); - storager.startPlay(streamInfo); - return streamInfo; + } catch ( SipException | ParseException | InvalidArgumentException e) { e.printStackTrace(); - return null; - } + } } /** @@ -279,62 +369,92 @@ * @param endTime 缁撴潫鏃堕棿,鏍煎紡瑕佹眰锛歽yyy-MM-dd HH:mm:ss */ @Override - public StreamInfo playbackStreamCmd(Device device, String channelId, String startTime, String endTime) { + public void playbackStreamCmd(Device device, String channelId, String startTime, String endTime, ZLMHttpHookSubscribe.Event event + , SipSubscribe.Event errorEvent) { try { MediaServerConfig mediaInfo = storager.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="+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.getLocalIP()+"\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 = zlmUtils.getNewRTPPort(ssrc) + ""; + mediaPort = zlmrtpServerFactory.createRTPServer(streamId) + ""; }else { mediaPort = mediaInfo.getRtpProxyPort(); } String streamMode = device.getStreamMode().toUpperCase(); - 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"); + + if (seniorSdp) { + if("TCP-PASSIVE".equals(streamMode)) { + content.append("m=video "+ mediaPort +" TCP/RTP/AVP 126 125 99 34 98 97 96\r\n"); + }else if ("TCP-ACTIVE".equals(streamMode)) { + content.append("m=video "+ mediaPort +" TCP/RTP/AVP 126 125 99 34 98 97 96\r\n"); + }else if("UDP".equals(streamMode)) { + content.append("m=video "+ mediaPort +" RTP/AVP 126 125 99 34 98 97 96\r\n"); + } + content.append("a=recvonly\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"); + content.append("a=rtpmap:96 PS/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("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("y="+ssrc+"\r\n");//ssrc Request request = headerProvider.createPlaybackInviteRequest(device, channelId, content.toString(), null, "playback", null); - - ClientTransaction transaction = transmitRequest(device, request); - streamSession.put(ssrc, transaction); - StreamInfo streamInfo = new StreamInfo(); - streamInfo.setSsrc(ssrc); - streamInfo.setCahnnelId(channelId); - streamInfo.setDeviceID(device.getDeviceId()); - boolean b = storager.startPlayBlack(streamInfo); - return streamInfo; + ClientTransaction transaction = transmitRequest(device, request, errorEvent); + streamSession.put(streamId, transaction); } catch ( SipException | ParseException | InvalidArgumentException e) { e.printStackTrace(); - return null; } } @@ -343,10 +463,10 @@ * */ @Override - public void streamByeCmd(String ssrc) { + public void streamByeCmd(String streamId) { try { - ClientTransaction transaction = streamSession.get(ssrc); + ClientTransaction transaction = streamSession.get(streamId); if (transaction == null) { return; } @@ -372,6 +492,8 @@ clientTransaction = udpSipProvider.getNewClientTransaction(byeRequest); } dialog.sendRequest(clientTransaction); + streamSession.remove(streamId); + zlmrtpServerFactory.closeRTPServer(streamId); } catch (TransactionDoesNotExistException e) { e.printStackTrace(); } catch (SipException e) { @@ -489,7 +611,8 @@ catalogXml.append("</Query>\r\n"); Request request = headerProvider.createMessageRequest(device, catalogXml.toString(), "ViaDeviceInfoBranch", "FromDeviceInfoTag", "ToDeviceInfoTag"); - transmitRequest(device, request); + + transmitRequest(device, request, null); } catch (SipException | ParseException | InvalidArgumentException e) { e.printStackTrace(); @@ -504,7 +627,7 @@ * @param device 瑙嗛璁惧 */ @Override - public boolean catalogQuery(Device device) { + public boolean catalogQuery(Device device, SipSubscribe.Event errorEvent) { // 娓呯┖閫氶亾 storager.cleanChannelsForDevice(device.getDeviceId()); try { @@ -516,8 +639,9 @@ 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; @@ -549,8 +673,10 @@ recordInfoXml.append("<Type>all</Type>\r\n"); recordInfoXml.append("</Query>\r\n"); - Request request = headerProvider.createMessageRequest(device, recordInfoXml.toString(), "ViaRecordInfoBranch", "FromRecordInfoTag", "ToRecordInfoTag"); - transmitRequest(device, request); + Request request = headerProvider.createMessageRequest(device, recordInfoXml.toString(), "ViaRecordInfoBranch", "FromRecordInfoTag", null); + + + transmitRequest(device, request, null); } catch (SipException | ParseException | InvalidArgumentException e) { e.printStackTrace(); return false; @@ -602,16 +728,30 @@ return false; } - private ClientTransaction transmitRequest(Device device, Request request) throws SipException { + private ClientTransaction transmitRequest(Device device, Request request, SipSubscribe.Event errorEvent) throws SipException { ClientTransaction clientTransaction = null; if("TCP".equals(device.getTransport())) { clientTransaction = tcpSipProvider.getNewClientTransaction(request); } else if("UDP".equals(device.getTransport())) { clientTransaction = udpSipProvider.getNewClientTransaction(request); } + + // 娣诲姞璁㈤槄 + if (errorEvent != null) { + CallIdHeader callIdHeader = (CallIdHeader)request.getHeader(CallIdHeader.NAME); + sipSubscribe.addSubscribe(callIdHeader.getCallId(), errorEvent); + } + 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