Search code examples
sippjsip

pjsip (pjsua) notification when remote user answers the call


I am trying to make a simple SIP user agent using https://github.com/pjsip/pjproject. I can succesfully connect to a sip server (Twilio) and place calls to PSTN numbers using the pjsua_* interface. This works fine.

What I would like now is to get a notification (through a callback or such) from pjsip when the user that I am calling answers the call.

I am using on_call_state() to get updates on the invite, but this goes through the same states

CALLING -> CONNECTING -> CONFIRMED -> DISCONNCTD

even if the user rejects the call. So I guess I am not looking at the right callback for this.

How can I definitely tell if the user has answered or rejected the call?


Solution

  • for me it is working this way. in on_call_state callback:

    pjsua_call_info callInfo;
    pjsua_call_get_info(call_id, &callInfo);
    pjsip_inv_state state = callInfo.state;
    pjsip_status_code statusCode = callInfo.last_status;
    
    switch (state) {
    .....
    case PJSIP_INV_STATE_CONFIRMED:
        // remote party answered the call normally
        break;
    case PJSIP_INV_STATE_DISCONNECTED:
        if (statusCode == PJSIP_SC_BUSY_HERE) {
           // rejected
        } else {
           // finished ok
        }
        break;
    

    i reject call this way:

    pj_status_t state;
    int sendCode = PJSIP_SC_DECLINE;
    try {
       state = pjsua_call_answer((pjsua_call_id) call_id, sendCode, nullptr, nullptr);
    } catch (...) {
       return -1;
    }
    return state;