liusuyi
2026-05-06 99ab0d46d30b095d0e1c9d31349f42e0cefc9881
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package com.ard.gb28181.transmit.event.request.impl;
 
import com.ard.common.core.constant.SecurityConstants;
import com.ard.common.core.domain.RtpServerParam;
import com.ard.gb28181.api.domain.SsrcTransaction;
import com.ard.gb28181.session.SipInviteSessionManager;
import com.ard.gb28181.transmit.ISIPProcessorObserver;
import com.ard.gb28181.transmit.SIPSender;
import com.ard.gb28181.transmit.event.request.ISIPRequestProcessor;
import com.ard.gb28181.transmit.event.request.SIPRequestProcessorParent;
import com.ard.zlm.api.RemoteZlmService;
import gov.nist.javax.sip.message.SIPRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import javax.sip.RequestEvent;
import javax.sip.SipException;
import javax.sip.header.CallIdHeader;
import javax.sip.message.Response;
 
@Slf4j
@Component
public class ByeRequestProcessor extends SIPRequestProcessorParent implements InitializingBean, ISIPRequestProcessor {
 
    public final String method = "BYE";
 
    @Autowired
    private ISIPProcessorObserver sipProcessorObserver;
 
    @Autowired
    private SIPSender sipSender;
 
    @Autowired
    private SipInviteSessionManager sessionManager;
 
    @Autowired
    private RemoteZlmService remoteZlmService;
 
    @Override
    public void afterPropertiesSet() throws Exception {
        sipProcessorObserver.addRequestProcessor(method, this);
    }
 
    @Override
    public void process(RequestEvent evt) {
        SIPRequest request = (SIPRequest) evt.getRequest();
        CallIdHeader callIdHeader = request.getCallIdHeader();
        String callId = callIdHeader.getCallId();
 
        log.info("[收到 BYE 请求] callId: {}", callId);
 
        try {
            Response okResponse = getMessageFactory().createResponse(Response.OK, request);
            sipSender.transmitRequest(request.getLocalAddress().getHostAddress(), okResponse);
 
            SsrcTransaction ssrcTransaction = sessionManager.getSsrcTransactionByCallId(callId);
            if (ssrcTransaction != null) {
                log.info("[BYE 清理资源] deviceId: {}, channelId: {}, app: {}, stream: {}, ssrc: {}", 
                        ssrcTransaction.getDeviceId(), 
                        ssrcTransaction.getChannelId(), 
                        ssrcTransaction.getApp(), 
                        ssrcTransaction.getStream(), 
                        ssrcTransaction.getSsrc());
 
                sessionManager.removeByCallId(callId);
                remoteZlmService.releaseSsrc(ssrcTransaction.getMediaServerId(), ssrcTransaction.getSsrc(), SecurityConstants.INNER);
                
                RtpServerParam rtpServerParam = new RtpServerParam();
                rtpServerParam.setMediaServerId(ssrcTransaction.getMediaServerId());
                rtpServerParam.setApp(ssrcTransaction.getApp());
                rtpServerParam.setStream(ssrcTransaction.getStream());
                rtpServerParam.setSsrc(ssrcTransaction.getSsrc());
                rtpServerParam.setGbDeviceId(ssrcTransaction.getDeviceId());
                rtpServerParam.setGbChannelId(ssrcTransaction.getChannelId());
                remoteZlmService.closeRTPServer(ssrcTransaction.getMediaServerId(), rtpServerParam, SecurityConstants.INNER);
            }
        } catch (Exception e) {
            log.error("[BYE 处理异常]", e);
        }
    }
}