From 805fa42a2c010d92b1c8fc29a60d32fd99fb4ea3 Mon Sep 17 00:00:00 2001 From: songww <songww@inspur.com> Date: 星期四, 14 五月 2020 14:19:42 +0800 Subject: [PATCH] 修复回放、停止预览bug,增加ZLM hook监听,长时间无人观看,停止摄像头推流。需开启ZLM的hook并配置url --- src/main/java/com/genersoft/iot/vmp/gb28181/transmit/cmd/impl/SIPCommander.java | 163 ++++++++++++++++++++++++++++++++++++++++++----------- 1 files changed, 128 insertions(+), 35 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 630d44c..82dcee8 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,19 +1,22 @@ package com.genersoft.iot.vmp.gb28181.transmit.cmd.impl; import java.text.ParseException; -import java.util.Random; +import javax.sip.ClientTransaction; +import javax.sip.Dialog; import javax.sip.InvalidArgumentException; import javax.sip.SipException; +import javax.sip.TransactionDoesNotExistException; +import javax.sip.header.ViaHeader; import javax.sip.message.Request; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.ApplicationEventPublisher; 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; @@ -27,13 +30,16 @@ public class SIPCommander implements ISIPCommander { @Autowired - private SipConfig config; + private SipConfig sipConfig; @Autowired private SIPRequestHeaderProvider headerProvider; @Autowired private SipLayer sipLayer; + + @Autowired + private VideoStreamSessionManager streamSession; /** * 浜戝彴鏂瑰悜鏀炬帶鍒讹紝浣跨敤閰嶇疆鏂囦欢涓殑榛樿闀滃ご绉诲姩閫熷害 @@ -46,7 +52,7 @@ */ @Override public boolean ptzdirectCmd(Device device, String channelId, int leftRight, int upDown) { - return ptzCmd(device, channelId, leftRight, upDown, 0, config.getSpeed(), 0); + return ptzCmd(device, channelId, leftRight, upDown, 0, sipConfig.getSpeed(), 0); } /** @@ -72,7 +78,7 @@ */ @Override public boolean ptzZoomCmd(Device device, String channelId, int inOut) { - return ptzCmd(device, channelId, 0, 0, inOut, 0, config.getSpeed()); + return ptzCmd(device, channelId, 0, 0, inOut, 0, sipConfig.getSpeed()); } /** @@ -135,25 +141,70 @@ public String playStreamCmd(Device device, String channelId) { try { - //鐢熸垚ssrc鏍囪瘑鏁版嵁娴� 10浣嶆暟瀛� - String ssrc = ""; - Random random = new Random(); - // ZLMediaServer鏈�澶ц瘑鍒�7FFFFFFF鍗�2147483647锛屾墍浠ラ殢鏈烘暟涓嶈兘瓒呰繃杩欎釜鏁� - ssrc = String.valueOf(random.nextInt(2147483647)); + String ssrc = streamSession.createPlaySsrc(); + String transport = device.getTransport(); // StringBuffer content = new StringBuffer(200); content.append("v=0\r\n"); - content.append("o="+channelId+" 0 0 IN IP4 "+config.getSipIp()+"\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 "+config.getMediaIp()+"\r\n"); + content.append("c=IN IP4 "+sipConfig.getMediaIp()+"\r\n"); content.append("t=0 0\r\n"); + if("TCP".equals(transport)) { + content.append("m=video "+sipConfig.getMediaPort()+" TCP/RTP/AVP 96 98 97\r\n"); + } + if("UDP".equals(transport)) { + content.append("m=video "+sipConfig.getMediaPort()+" 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".equals(transport)){ + 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, channelId, content.toString(), null, "live", null); + + ClientTransaction transaction = transmitRequest(device, request); + streamSession.put(ssrc, transaction); + return ssrc; + } catch ( SipException | ParseException | InvalidArgumentException e) { + e.printStackTrace(); + return null; + } + } + + /** + * 璇锋眰鍥炴斁瑙嗛娴� + * + * @param device 瑙嗛璁惧 + * @param channelId 棰勮閫氶亾 + * @param startTime 寮�濮嬫椂闂�,鏍煎紡瑕佹眰锛歽yyy-MM-dd HH:mm:ss + * @param endTime 缁撴潫鏃堕棿,鏍煎紡瑕佹眰锛歽yyy-MM-dd HH:mm:ss + */ + @Override + public String playbackStreamCmd(Device device, String channelId, String startTime, String endTime) { + try { + + String ssrc = streamSession.createPlayBackSsrc(); + // + 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 "+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 "+config.getMediaPort()+" TCP/RTP/AVP 96 98 97\r\n"); + content.append("m=video "+sipConfig.getMediaPort()+" TCP/RTP/AVP 96 98 97\r\n"); } if(device.getTransport().equals("UDP")) { - content.append("m=video "+config.getMediaPort()+" RTP/AVP 96 98 97\r\n"); + content.append("m=video "+sipConfig.getMediaPort()+" RTP/AVP 96 98 97\r\n"); } - content.append("a=sendrecv\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"); @@ -163,14 +214,51 @@ } content.append("y="+ssrc+"\r\n");//ssrc - Request request = headerProvider.createInviteRequest(device, content.toString(), null, "live", null); + Request request = headerProvider.createPlaybackInviteRequest(device, channelId, content.toString(), null, "playback", null); - transmitRequest(device, request); + ClientTransaction transaction = transmitRequest(device, request); + streamSession.put(ssrc, transaction); return ssrc; } catch ( SipException | ParseException | InvalidArgumentException e) { e.printStackTrace(); return null; - } + } + } + + /** + * 瑙嗛娴佸仠姝� + * + * @param device 瑙嗛璁惧 + * @param channelId 棰勮閫氶亾 + */ + @Override + public void streamByeCmd(String ssrc) { + + try { + ClientTransaction transaction = streamSession.get(ssrc); + if (transaction == null) { + return; + } + + Dialog dialog = transaction.getDialog(); + if (dialog == null) { + return; + } + Request byeRequest = dialog.createRequest(Request.BYE); + ViaHeader viaHeader = (ViaHeader) byeRequest.getHeader(ViaHeader.NAME); + String protocol = viaHeader.getTransport().toUpperCase(); + ClientTransaction clientTransaction = null; + if("TCP".equals(protocol)) { + clientTransaction = sipLayer.getTcpSipProvider().getNewClientTransaction(byeRequest); + } else if("UDP".equals(protocol)) { + clientTransaction = sipLayer.getUdpSipProvider().getNewClientTransaction(byeRequest); + } + dialog.sendRequest(clientTransaction); + } catch (TransactionDoesNotExistException e) { + e.printStackTrace(); + } catch (SipException e) { + e.printStackTrace(); + } } /** @@ -323,22 +411,23 @@ * @param endTime 缁撴潫鏃堕棿,鏍煎紡瑕佹眰锛歽yyy-MM-dd HH:mm:ss */ @Override - public boolean recordInfoQuery(Device device, String startTime, String endTime) { + public boolean recordInfoQuery(Device device, String channelId, String startTime, String endTime) { try { - StringBuffer catalogXml = new StringBuffer(200); - catalogXml.append("<?xml version=\"1.0\" encoding=\"GB2312\"?>"); - catalogXml.append("<Query>"); - catalogXml.append("<CmdType>RecordInfo</CmdType>"); - catalogXml.append("<SN>" + (int)((Math.random()*9+1)*100000) + "</SN>"); - catalogXml.append("<DeviceID>" + device.getDeviceId() + "</DeviceID>"); - catalogXml.append("<StartTime>" + DateUtil.yyyy_MM_dd_HH_mm_ssToISO8601(startTime) + "</StartTime>"); - catalogXml.append("<EndTime>" + DateUtil.yyyy_MM_dd_HH_mm_ssToISO8601(endTime) + "</EndTime>"); + 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>"); // 澶у崕NVR瑕佹眰蹇呴』澧炲姞涓�涓�间负all鐨勬枃鏈厓绱犺妭鐐筎ype - catalogXml.append("<Type>all</Type>"); - catalogXml.append("</Query>"); + recordInfoXml.append("<Type>all</Type>"); + recordInfoXml.append("</Query>"); - Request request = headerProvider.createMessageRequest(device, catalogXml.toString(), "ViaRecordInfoBranch", "FromRecordInfoTag", "ToRecordInfoTag"); + Request request = headerProvider.createMessageRequest(device, recordInfoXml.toString(), "ViaRecordInfoBranch", "FromRecordInfoTag", "ToRecordInfoTag"); transmitRequest(device, request); } catch (SipException | ParseException | InvalidArgumentException e) { e.printStackTrace(); @@ -391,11 +480,15 @@ 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 { + ClientTransaction clientTransaction = null; + if("TCP".equals(device.getTransport())) { + clientTransaction = sipLayer.getTcpSipProvider().getNewClientTransaction(request); + } else if("UDP".equals(device.getTransport())) { + clientTransaction = sipLayer.getUdpSipProvider().getNewClientTransaction(request); } + clientTransaction.sendRequest(); + return clientTransaction; } + } -- Gitblit v1.8.0