视频下载结束时主动发送 bye

This commit is contained in:
shikong 2023-09-25 11:16:00 +08:00
parent ffbb2fff54
commit 91218eb59d
3 changed files with 57 additions and 3 deletions

View File

@ -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();
}

View File

@ -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<ViaHeader> viaHeaders = new ArrayList<ViaHeader>();
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;
}
}

View File

@ -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());
}
};
}