如何在 Java 中自动接听所有 SIP 呼叫

How to answer all SIP Calls automatically in Java

我想自动接听所有 sip 电话。 当我以尝试或振铃回应时,交易处于进行中状态, 但是当我发送 OK 响应时,事务处于终止状态。 这是我的代码:

public void processRequest(RequestEvent requestReceivedEvent)
{
    Address contactAddress = myAddressFactory.createAddress("sip:" + myIP + ":" + myPort);
    myContactHeader = myHeaderFactory.createContactHeader(contactAddress);
    Request req = requestReceivedEvent.getRequest();
    myGUI.display("<<< " + req.toString());
    String method = req.getMethod(); //bad request type.                 
    FromHeader from = (FromHeader) req.getHeader("From");
    Response response = null;
    try
    { //Reply with OK                 
        response = myMessageFactory.createResponse(200, req);
        ToHeader toHeader = (ToHeader) response.getHeader(ToHeader.NAME);
        toHeader.setTag("888"); //Identifier, specific to your application                 
        ServerTransaction st = mySipProvider.getNewServerTransaction(req);
        response.addHeader(myContactHeader);
        st.sendResponse(response);
        System.out.println("Ok response: " + st.getState());

    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
}

提前致谢。

INVITE 服务器事务在 200 OK 后进入终止状态。这是正常的。您的电话已接听,您不必担心。

(参见 RFC 中的状态机图)

                           |INVITE
                           |pass INV to TU
        INVITE             V send 100 if TU won't in 200ms
        send response+-----------+
            +--------|           |--------+101-199 from TU
            |        | Proceeding|        |send response
            +------->|           |<-------+
                     |           |          Transport Err.
                     |           |          Inform TU
                     |           |--------------->+
                     +-----------+                |
        300-699 from TU |     |2xx from TU        |
        send response   |     |send response      |
                        |     +------------------>+
                        |                         |
        INVITE          V          Timer G fires  |
        send response+-----------+ send response  |
            +--------|           |--------+       |
            |        | Completed |        |       |
            +------->|           |<-------+       |
                     +-----------+                |
                        |     |                   |
                    ACK |     |                   |
                    -   |     +------------------>+
                        |        Timer H fires    |
                        V        or Transport Err.|
                     +-----------+  Inform TU     |
                     |           |                |
                     | Confirmed |                |
                     |           |                |
                     +-----------+                |
                           |                      |
                           |Timer I fires         |
                           |-                     |
                           |                      |
                           V                      |
                     +-----------+                |
                     |           |                |
                     | Terminated|<---------------+
                     |           |
                     +-----------+

          Figure 7: INVITE server transaction