diff --git a/gb28181-mocking-service/src/main/java/cn/skcks/docking/gb28181/mocking/core/sip/message/processor/invite/request/InviteRequestProcessor.java b/gb28181-mocking-service/src/main/java/cn/skcks/docking/gb28181/mocking/core/sip/message/processor/invite/request/InviteRequestProcessor.java index f082975..b866d3b 100644 --- a/gb28181-mocking-service/src/main/java/cn/skcks/docking/gb28181/mocking/core/sip/message/processor/invite/request/InviteRequestProcessor.java +++ b/gb28181-mocking-service/src/main/java/cn/skcks/docking/gb28181/mocking/core/sip/message/processor/invite/request/InviteRequestProcessor.java @@ -182,7 +182,7 @@ public class InviteRequestProcessor implements MessageProcessor { String senderIp = request.getLocalAddress().getHostAddress(); String transport = request.getTopmostViaHeader().getTransport(); - int taskNum = deviceProxyService.getTaskNum().get(); + int taskNum = DeviceProxyService.getTaskNum().get(); log.info("当前任务数 {}", taskNum); if(ffmpegConfig.getTask().getMax() > 0 && taskNum >= ffmpegConfig.getTask().getMax()){ log.warn("任务数过多 性能受限, 返回 486"); @@ -282,7 +282,6 @@ public class InviteRequestProcessor implements MessageProcessor { log.info("收到 ack 确认请求: {} 开始推流",key); // RTP 推流 deviceProxyService.proxyVideo2Rtp(request, callId, device, start, stop, address, port, deviceProxyService.downloadTask()); - onComplete(); } diff --git a/gb28181-mocking-service/src/main/java/cn/skcks/docking/gb28181/mocking/core/sip/request/SipRequestBuilder.java b/gb28181-mocking-service/src/main/java/cn/skcks/docking/gb28181/mocking/core/sip/request/SipRequestBuilder.java index 88b5068..a4a1cfd 100644 --- a/gb28181-mocking-service/src/main/java/cn/skcks/docking/gb28181/mocking/core/sip/request/SipRequestBuilder.java +++ b/gb28181-mocking-service/src/main/java/cn/skcks/docking/gb28181/mocking/core/sip/request/SipRequestBuilder.java @@ -21,6 +21,7 @@ import javax.sip.address.Address; import javax.sip.address.SipURI; import javax.sip.header.*; import javax.sip.message.Request; +import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.UUID; @@ -218,4 +219,41 @@ public class SipRequestBuilder implements ApplicationContextAware { request.setContent(content, contentTypeHeader); return request; } + + @SneakyThrows + public static Request createByeRequest(String ip, int port, long cSeq, String targetId, String fromTag, String toTag, String callId) { + Request request; + // 请求行 + String target = StringUtils.joinWith(":", ip, port); + SipURI requestLine = MessageHelper.createSipURI(targetId, target); + // via + ArrayList viaHeaders = new ArrayList(); + ViaHeader viaHeader = getSipFactory().createHeaderFactory().createViaHeader(ip, port, sipConfig.getTransport(), SipUtil.generateViaTag()); + viaHeaders.add(viaHeader); + // from + SipURI fromSipURI = MessageHelper.createSipURI(sipConfig.getId(), sipConfig.getDomain()); + Address fromAddress = MessageHelper.createAddress(fromSipURI); + FromHeader fromHeader = MessageHelper.createFromHeader(fromAddress, fromTag); + // to + SipURI toSipURI = MessageHelper.createSipURI(targetId, target); + Address toAddress = MessageHelper.createAddress(toSipURI); + ToHeader toHeader = MessageHelper.createToHeader(toAddress, toTag); + + // Forwards + MaxForwardsHeader maxForwards = getSipFactory().createHeaderFactory().createMaxForwardsHeader(70); + + // ceq + CSeqHeader cSeqHeader = getSipFactory().createHeaderFactory().createCSeqHeader(cSeq, Request.BYE); + CallIdHeader callIdHeader = getSipFactory().createHeaderFactory().createCallIdHeader(callId); + request = getSipFactory().createMessageFactory().createRequest(requestLine, Request.BYE, callIdHeader, cSeqHeader, fromHeader, toHeader, viaHeaders, maxForwards); + + request.addHeader(SipUtil.createUserAgentHeader()); + + Address concatAddress = MessageHelper.createAddress(MessageHelper.createSipURI(sipConfig.getId(), ip + ":" + port)); + request.addHeader(getSipFactory().createHeaderFactory().createContactHeader(concatAddress)); + request.addHeader(SipUtil.createUserAgentHeader()); + + return request; + } + } diff --git a/gb28181-mocking-service/src/main/java/cn/skcks/docking/gb28181/mocking/service/device/DeviceProxyService.java b/gb28181-mocking-service/src/main/java/cn/skcks/docking/gb28181/mocking/service/device/DeviceProxyService.java index 2bc34b3..6d8a068 100644 --- a/gb28181-mocking-service/src/main/java/cn/skcks/docking/gb28181/mocking/service/device/DeviceProxyService.java +++ b/gb28181-mocking-service/src/main/java/cn/skcks/docking/gb28181/mocking/service/device/DeviceProxyService.java @@ -21,13 +21,14 @@ import gov.nist.javax.sip.message.SIPRequest; import jakarta.annotation.PreDestroy; import lombok.*; import lombok.extern.slf4j.Slf4j; -import org.apache.commons.exec.DefaultExecuteResultHandler; import org.apache.commons.exec.ExecuteException; import org.apache.commons.exec.ExecuteResultHandler; import org.apache.commons.exec.Executor; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; +import javax.sip.SipProvider; +import javax.sip.address.SipURI; import javax.sip.header.CallIdHeader; import javax.sip.message.Request; import javax.sip.message.Response; @@ -92,6 +93,22 @@ public class DeviceProxyService { downloadTask.put(device.getDeviceCode(), executor); scheduledExecutorService.schedule(subscriber::onComplete, time + 60, TimeUnit.SECONDS); executeResultHandler.waitFor(); + + String ip = request.getLocalAddress().getHostAddress(); + SipURI targetUri = (SipURI) request.getFromHeader().getAddress().getURI(); + String targetId = targetUri.getUser(); + String targetIp = request.getRemoteAddress().getHostAddress(); + int targetPort = request.getTopmostViaHeader().getPort(); + String transport = request.getTopmostViaHeader().getTransport(); + long seqNumber = request.getCSeq().getSeqNumber() + 1; + SipProvider provider = sender.getProvider(transport, ip); + CallIdHeader newCallId = provider.getNewCallId(); + Request byeRequest = SipRequestBuilder.createByeRequest(targetIp, targetPort, seqNumber, targetId, SipUtil.generateFromTag(), null, newCallId.getCallId()); + try{ + provider.sendRequest(byeRequest); + }catch (Exception e){ + log.error("bye 请求发送失败 {}",e.getMessage()); + } }; }