forked from Thirdparty/wvp
1
0
Fork 0

修复回放、停止预览bug,增加ZLM hook监听,长时间无人观看,停止摄像头推流。需开启ZLM的hook并配置url

master
songww 2020-05-14 14:19:42 +08:00
parent 1b6f0137b9
commit 805fa42a2c
7 changed files with 257 additions and 106 deletions

View File

@ -123,7 +123,6 @@
<groupId>javax.sip</groupId>
<artifactId>jain-sip-ri</artifactId>
<version>1.3.0-91</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.dom4j</groupId>

View File

@ -22,7 +22,7 @@ public class SsrcUtil {
private static void init() {
SipConfig sipConfig = (SipConfig) SpringBeanFactory.getBean("sipConfig");
ssrcPrefix = sipConfig.getSipDomain().substring(4, 9);
ssrcPrefix = sipConfig.getSipDomain().substring(3, 8);
isUsed = new ArrayList<String>();
notUsed = new ArrayList<String>();
for (int i = 1; i < 10000; i++) {

View File

@ -24,7 +24,7 @@ import com.genersoft.iot.vmp.gb28181.bean.Device;
import com.genersoft.iot.vmp.gb28181.bean.Host;
/**
* @Description:TODO()
* @Description:request TODO
* @author: songww
* @date: 202056 9:29:02
*/
@ -72,50 +72,50 @@ public class SIPRequestHeaderProvider {
return request;
}
// public Request createInviteRequest(Device device, String content, String viaTag, String fromTag, String toTag) throws ParseException, InvalidArgumentException {
// Request request = null;
// Host host = device.getHost();
// //请求行
// SipURI requestLine = layer.getAddressFactory().createSipURI(device.getDeviceId(), host.getAddress());
// //via
// ArrayList<ViaHeader> viaHeaders = new ArrayList<ViaHeader>();
// ViaHeader viaHeader = layer.getHeaderFactory().createViaHeader(sipConfig.getSipIp(), sipConfig.getSipPort(), device.getTransport(), viaTag);
// viaHeader.setRPort();
// viaHeaders.add(viaHeader);
// //from
// SipURI fromSipURI = layer.getAddressFactory().createSipURI(device.getDeviceId(),sipConfig.getSipIp()+":"+sipConfig.getSipPort());
// Address fromAddress = layer.getAddressFactory().createAddress(fromSipURI);
// FromHeader fromHeader = layer.getHeaderFactory().createFromHeader(fromAddress, fromTag); //必须要有标记否则无法创建会话无法回应ack
// //to
// SipURI toSipURI = layer.getAddressFactory().createSipURI(device.getDeviceId(),host.getAddress());
// Address toAddress = layer.getAddressFactory().createAddress(toSipURI);
// ToHeader toHeader = layer.getHeaderFactory().createToHeader(toAddress,null);
//
// //callid
// CallIdHeader callIdHeader = null;
// if(device.getTransport().equals("TCP")) {
// callIdHeader = layer.getTcpSipProvider().getNewCallId();
// }
// if(device.getTransport().equals("UDP")) {
// callIdHeader = layer.getUdpSipProvider().getNewCallId();
// }
//
// //Forwards
// MaxForwardsHeader maxForwards = layer.getHeaderFactory().createMaxForwardsHeader(70);
//
// //ceq
// CSeqHeader cSeqHeader = layer.getHeaderFactory().createCSeqHeader(1L, Request.INVITE);
// request = layer.getMessageFactory().createRequest(requestLine, Request.INVITE, callIdHeader, cSeqHeader,fromHeader, toHeader, viaHeaders, maxForwards);
//
// Address concatAddress = layer.getAddressFactory().createAddress(layer.getAddressFactory().createSipURI(sipConfig.getSipId(), sipConfig.getSipIp()+":"+sipConfig.getSipPort()));
// request.addHeader(layer.getHeaderFactory().createContactHeader(concatAddress));
//
// ContentTypeHeader contentTypeHeader = layer.getHeaderFactory().createContentTypeHeader("Application", "SDP");
// request.setContent(content, contentTypeHeader);
// return request;
// }
public Request createInviteRequest(Device device, String channelId, String content, String viaTag, String fromTag, String toTag) throws ParseException, InvalidArgumentException {
Request request = null;
Host host = device.getHost();
//请求行
SipURI requestLine = layer.getAddressFactory().createSipURI(channelId, host.getAddress());
//via
ArrayList<ViaHeader> viaHeaders = new ArrayList<ViaHeader>();
ViaHeader viaHeader = layer.getHeaderFactory().createViaHeader(sipConfig.getSipIp(), sipConfig.getSipPort(), device.getTransport(), viaTag);
viaHeader.setRPort();
viaHeaders.add(viaHeader);
//from
SipURI fromSipURI = layer.getAddressFactory().createSipURI(sipConfig.getSipId(),sipConfig.getSipDomain());
Address fromAddress = layer.getAddressFactory().createAddress(fromSipURI);
FromHeader fromHeader = layer.getHeaderFactory().createFromHeader(fromAddress, fromTag); //必须要有标记否则无法创建会话无法回应ack
//to
SipURI toSipURI = layer.getAddressFactory().createSipURI(channelId,sipConfig.getSipDomain());
Address toAddress = layer.getAddressFactory().createAddress(toSipURI);
ToHeader toHeader = layer.getHeaderFactory().createToHeader(toAddress,null);
//callid
CallIdHeader callIdHeader = null;
if(device.getTransport().equals("TCP")) {
callIdHeader = layer.getTcpSipProvider().getNewCallId();
}
if(device.getTransport().equals("UDP")) {
callIdHeader = layer.getUdpSipProvider().getNewCallId();
}
//Forwards
MaxForwardsHeader maxForwards = layer.getHeaderFactory().createMaxForwardsHeader(70);
//ceq
CSeqHeader cSeqHeader = layer.getHeaderFactory().createCSeqHeader(1L, Request.INVITE);
request = layer.getMessageFactory().createRequest(requestLine, Request.INVITE, callIdHeader, cSeqHeader,fromHeader, toHeader, viaHeaders, maxForwards);
Address concatAddress = layer.getAddressFactory().createAddress(layer.getAddressFactory().createSipURI(sipConfig.getSipId(), sipConfig.getSipIp()+":"+sipConfig.getSipPort()));
request.addHeader(layer.getHeaderFactory().createContactHeader(concatAddress));
ContentTypeHeader contentTypeHeader = layer.getHeaderFactory().createContentTypeHeader("Application", "SDP");
request.setContent(content, contentTypeHeader);
return request;
}
public Request createInviteRequest(Device device, String content, String viaTag, String fromTag, String toTag) throws ParseException, InvalidArgumentException {
public Request createPlaybackInviteRequest(Device device, String channelId, String content, String viaTag, String fromTag, String toTag) throws ParseException, InvalidArgumentException {
Request request = null;
Host host = device.getHost();
//请求行
@ -130,7 +130,7 @@ public class SIPRequestHeaderProvider {
Address fromAddress = layer.getAddressFactory().createAddress(fromSipURI);
FromHeader fromHeader = layer.getHeaderFactory().createFromHeader(fromAddress, fromTag); //必须要有标记否则无法创建会话无法回应ack
//to
SipURI toSipURI = layer.getAddressFactory().createSipURI(device.getDeviceId(),sipConfig.getSipDomain());
SipURI toSipURI = layer.getAddressFactory().createSipURI(channelId,sipConfig.getSipDomain());
Address toAddress = layer.getAddressFactory().createAddress(toSipURI);
ToHeader toHeader = layer.getHeaderFactory().createToHeader(toAddress,null);

View File

@ -142,30 +142,31 @@ public class SIPCommander implements ISIPCommander {
try {
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 "+sipConfig.getMediaIp()+"\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")) {
if("TCP".equals(transport)) {
content.append("m=video "+sipConfig.getMediaPort()+" TCP/RTP/AVP 96 98 97\r\n");
}
if(device.getTransport().equals("UDP")) {
if("UDP".equals(transport)) {
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");
if(device.getTransport().equals("TCP")){
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, content.toString(), null, "live", null);
Request request = headerProvider.createInviteRequest(device, channelId, content.toString(), null, "live", null);
ClientTransaction transaction = transmitRequest(device, request);
streamSession.put(ssrc, transaction);
@ -192,9 +193,9 @@ public class SIPCommander implements ISIPCommander {
//
StringBuffer content = new StringBuffer(200);
content.append("v=0\r\n");
content.append("o="+device.getDeviceId()+" 0 0 IN IP4 "+sipConfig.getMediaIp()+"\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+":3\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")) {
@ -213,7 +214,7 @@ public class SIPCommander implements ISIPCommander {
}
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);
ClientTransaction transaction = transmitRequest(device, request);
streamSession.put(ssrc, transaction);
@ -245,7 +246,7 @@ public class SIPCommander implements ISIPCommander {
}
Request byeRequest = dialog.createRequest(Request.BYE);
ViaHeader viaHeader = (ViaHeader) byeRequest.getHeader(ViaHeader.NAME);
String protocol = viaHeader.getTransport();
String protocol = viaHeader.getTransport().toUpperCase();
ClientTransaction clientTransaction = null;
if("TCP".equals(protocol)) {
clientTransaction = sipLayer.getTcpSipProvider().getNewClientTransaction(byeRequest);

View File

@ -46,7 +46,7 @@ public class DateUtil {
Date date;
try {
date = format.parse(formatTime);
Long timestamp=date.getTime();
Long timestamp=date.getTime()/1000;
//转换为Date类
return timestamp;
} catch (ParseException e) {

View File

@ -1,12 +1,21 @@
package com.genersoft.iot.vmp.media.zlm;
import java.math.BigInteger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson.JSONObject;
import com.genersoft.iot.vmp.gb28181.transmit.cmd.impl.SIPCommander;
/**
* @Description: ZLMediaServerhook
* @author: songww
@ -18,135 +27,247 @@ public class ZLMHttpHookListener {
private final static Logger logger = LoggerFactory.getLogger(ZLMHttpHookListener.class);
@Autowired
private SIPCommander cmder;
/**
* general.flowThreshold
*
*/
@PostMapping("/on_flow_report")
public ResponseEntity onFlowReport(){
@ResponseBody
@PostMapping(value = "/on_flow_report", produces = "application/json;charset=UTF-8")
public ResponseEntity<String> onFlowReport(@RequestBody JSONObject json){
if (logger.isDebugEnabled()) {
logger.debug("ZLM HOOK on_flow_report API调用参数" + json.toString());
}
// TODO Auto-generated method stub
return null;
JSONObject ret = new JSONObject();
json.put("code", 0);
json.put("msg", "success");
return new ResponseEntity<String>(ret.toString(),HttpStatus.OK);
}
/**
* 访httphls
*
*/
@PostMapping("/on_http_access")
public ResponseEntity onHttpAccess(){
@ResponseBody
@PostMapping(value = "/on_http_access", produces = "application/json;charset=UTF-8")
public ResponseEntity<String> onHttpAccess(@RequestBody JSONObject json){
if (logger.isDebugEnabled()) {
logger.debug("ZLM HOOK on_http_access API 调用,参数:" + json.toString());
}
// TODO Auto-generated method stub
return null;
JSONObject ret = new JSONObject();
json.put("code", 0);
json.put("err", "");
json.put("path", "");
json.put("second", 600);
return new ResponseEntity<String>(ret.toString(),HttpStatus.OK);
}
/**
* rtsp/rtmp/http-flv/ws-flv/hls
*
*/
@PostMapping("/on_play")
public ResponseEntity onPlay(){
@ResponseBody
@PostMapping(value = "/on_play", produces = "application/json;charset=UTF-8")
public ResponseEntity<String> onPlay(@RequestBody JSONObject json){
if (logger.isDebugEnabled()) {
logger.debug("ZLM HOOK on_play API调用参数" + json.toString());
}
// TODO Auto-generated method stub
return null;
JSONObject ret = new JSONObject();
json.put("code", 0);
json.put("msg", "success");
return new ResponseEntity<String>(ret.toString(),HttpStatus.OK);
}
/**
* rtsp/rtmp/rtp
*
*/
@PostMapping("/on_publish")
public ResponseEntity onPublish(){
@ResponseBody
@PostMapping(value = "/on_publish", produces = "application/json;charset=UTF-8")
public ResponseEntity<String> onPublish(@RequestBody JSONObject json){
if (logger.isDebugEnabled()) {
logger.debug("ZLM HOOK on_publish API调用参数" + json.toString());
}
// TODO Auto-generated method stub
return null;
JSONObject ret = new JSONObject();
json.put("code", 0);
json.put("msg", "success");
json.put("enableHls", true);
json.put("enableMP4", false);
json.put("enableRtxp", true);
return new ResponseEntity<String>(ret.toString(),HttpStatus.OK);
}
/**
* mp4
*
*/
@PostMapping("/on_record_mp4")
public ResponseEntity onRecordMp4(){
@ResponseBody
@PostMapping(value = "/on_record_mp4", produces = "application/json;charset=UTF-8")
public ResponseEntity<String> onRecordMp4(@RequestBody JSONObject json){
if (logger.isDebugEnabled()) {
logger.debug("ZLM HOOK on_record_mp4 API调用参数" + json.toString());
}
// TODO Auto-generated method stub
return null;
}
/**
* rtsprtspon_rtsp_authrtspurl
*
*/
@PostMapping("/on_rtsp_auth")
public ResponseEntity onRtspAuth(){
// TODO Auto-generated method stub
return null;
JSONObject ret = new JSONObject();
json.put("code", 0);
json.put("msg", "success");
return new ResponseEntity<String>(ret.toString(),HttpStatus.OK);
}
/**
* rtspon_rtsp_realmon_rtsp_auth
*
*/
@PostMapping("/on_rtsp_realm")
public ResponseEntity onRtspRealm(){
@ResponseBody
@PostMapping(value = "/on_rtsp_realm", produces = "application/json;charset=UTF-8")
public ResponseEntity<String> onRtspRealm(@RequestBody JSONObject json){
if (logger.isDebugEnabled()) {
logger.debug("ZLM HOOK on_rtsp_realm API调用参数" + json.toString());
}
// TODO Auto-generated method stub
return null;
JSONObject ret = new JSONObject();
json.put("code", 0);
json.put("realm", "");
return new ResponseEntity<String>(ret.toString(),HttpStatus.OK);
}
/**
* rtsprtspon_rtsp_authrtspurl
*
*/
@ResponseBody
@PostMapping(value = "/on_rtsp_auth", produces = "application/json;charset=UTF-8")
public ResponseEntity<String> onRtspAuth(@RequestBody JSONObject json){
if (logger.isDebugEnabled()) {
logger.debug("ZLM HOOK on_rtsp_auth API调用参数" + json.toString());
}
// TODO Auto-generated method stub
JSONObject ret = new JSONObject();
json.put("code", 0);
json.put("encrypted", false);
json.put("passwd", "test");
return new ResponseEntity<String>(ret.toString(),HttpStatus.OK);
}
/**
* shellZLMediaKittelnet使telnet 127.0.0.1 9000MediaServershell
*
*/
@PostMapping("/on_shell_login")
public ResponseEntity onShellLogin(){
@ResponseBody
@PostMapping(value = "/on_shell_login", produces = "application/json;charset=UTF-8")
public ResponseEntity<String> onShellLogin(@RequestBody JSONObject json){
if (logger.isDebugEnabled()) {
logger.debug("ZLM HOOK on_shell_login API调用参数" + json.toString());
}
// TODO Auto-generated method stub
return null;
JSONObject ret = new JSONObject();
json.put("code", 0);
json.put("msg", "success");
return new ResponseEntity<String>(ret.toString(),HttpStatus.OK);
}
/**
* rtsp/rtmp
*
*/
@PostMapping("/on_stream_changed")
public ResponseEntity onStreamChanged(){
@ResponseBody
@PostMapping(value = "/on_stream_changed", produces = "application/json;charset=UTF-8")
public ResponseEntity<String> onStreamChanged(@RequestBody JSONObject json){
if (logger.isDebugEnabled()) {
logger.debug("ZLM HOOK on_stream_changed API调用参数" + json.toString());
}
// TODO Auto-generated method stub
return null;
JSONObject ret = new JSONObject();
json.put("code", 0);
json.put("msg", "success");
return new ResponseEntity<String>(ret.toString(),HttpStatus.OK);
}
/**
*
*
*/
@PostMapping("/on_stream_none_reader")
public ResponseEntity onStreamNoneReader(){
// TODO Auto-generated method stub
@ResponseBody
@PostMapping(value = "/on_stream_none_reader", produces = "application/json;charset=UTF-8")
public ResponseEntity<String> onStreamNoneReader(@RequestBody JSONObject json){
return null;
if (logger.isDebugEnabled()) {
logger.debug("ZLM HOOK on_stream_none_reader API调用参数" + json.toString());
}
BigInteger bigint=new BigInteger(json.getString("stream"), 16);
int numb=bigint.intValue();
String ssrc = String.format("%010d", numb);
cmder.streamByeCmd(ssrc);
JSONObject ret = new JSONObject();
json.put("code", 0);
json.put("close", true);
return new ResponseEntity<String>(ret.toString(),HttpStatus.OK);
}
/**
*
*
*/
@PostMapping("/on_stream_not_found")
public ResponseEntity onStreamNotFound(){
@ResponseBody
@PostMapping(value = "/on_stream_not_found", produces = "application/json;charset=UTF-8")
public ResponseEntity<String> onStreamNotFound(@RequestBody JSONObject json){
if (logger.isDebugEnabled()) {
logger.debug("ZLM HOOK on_stream_not_found API调用参数" + json.toString());
}
// TODO Auto-generated method stub
return null;
JSONObject ret = new JSONObject();
json.put("code", 0);
json.put("msg", "success");
return new ResponseEntity<String>(ret.toString(),HttpStatus.OK);
}
/**
*
*
*/
@PostMapping("/on_server_started")
public ResponseEntity onServerStarted(){
@ResponseBody
@PostMapping(value = "/on_server_started", produces = "application/json;charset=UTF-8")
public ResponseEntity<String> onServerStarted(@RequestBody JSONObject json){
if (logger.isDebugEnabled()) {
logger.debug("ZLM HOOK on_server_started API调用参数" + json.toString());
}
// TODO Auto-generated method stub
return null;
JSONObject ret = new JSONObject();
json.put("code", 0);
json.put("msg", "success");
return new ResponseEntity<String>(ret.toString(),HttpStatus.OK);
}
}

View File

@ -5,8 +5,10 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@ -30,12 +32,21 @@ public class PlaybackController {
@GetMapping("/playback/{deviceId}/{channelId}")
public ResponseEntity<String> play(@PathVariable String deviceId,@PathVariable String channelId, String startTime, String endTime){
if (logger.isDebugEnabled()) {
logger.debug(String.format("设备回放 API调用deviceId%s channelId%s",deviceId, channelId));
}
if (StringUtils.isEmpty(deviceId) || StringUtils.isEmpty(channelId)) {
String log = String.format("设备回放 API调用失败deviceId%s channelId%s",deviceId, channelId);
logger.warn(log);
return new ResponseEntity<String>(log,HttpStatus.BAD_REQUEST);
}
Device device = storager.queryVideoDevice(deviceId);
String ssrc = cmder.playbackStreamCmd(device, channelId, startTime, endTime);
if (logger.isDebugEnabled()) {
logger.debug(String.format("设备预览 API调用deviceId%s channelId%s",deviceId, channelId));
logger.debug("设备预览 API调用ssrc"+ssrc+",ZLMedia streamId:"+Integer.toHexString(Integer.parseInt(ssrc)));
logger.debug("设备回放 API调用ssrc"+ssrc+",ZLMedia streamId:"+Integer.toHexString(Integer.parseInt(ssrc)));
}
if(ssrc!=null) {
@ -43,7 +54,26 @@ public class PlaybackController {
json.put("ssrc", ssrc);
return new ResponseEntity<String>(json.toString(),HttpStatus.OK);
} else {
logger.warn("设备预览API调用失败");
logger.warn("设备回放API调用失败");
return new ResponseEntity<String>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@PostMapping("/playback/{ssrc}/stop")
public ResponseEntity<String> playStop(@PathVariable String ssrc){
cmder.streamByeCmd(ssrc);
if (logger.isDebugEnabled()) {
logger.debug(String.format("设备录像回放停止 API调用ssrc%s", ssrc));
}
if(ssrc!=null) {
JSONObject json = new JSONObject();
json.put("ssrc", ssrc);
return new ResponseEntity<String>(json.toString(),HttpStatus.OK);
} else {
logger.warn("设备录像回放停止API调用失败");
return new ResponseEntity<String>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}