Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
Benny Prijono | a771a51 | 2007-02-19 01:13:53 +0000 | [diff] [blame] | 3 | * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org> |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
Benny Prijono | 4f9f64e | 2006-02-27 00:00:30 +0000 | [diff] [blame] | 19 | #include <pjsua-lib/pjsua.h> |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 20 | #include <pjsua-lib/pjsua_internal.h> |
| 21 | |
| 22 | |
| 23 | #define THIS_FILE "pjsua_call.c" |
| 24 | |
| 25 | |
| 26 | /* This callback receives notification from invite session when the |
| 27 | * session state has changed. |
| 28 | */ |
| 29 | static void pjsua_call_on_state_changed(pjsip_inv_session *inv, |
| 30 | pjsip_event *e); |
| 31 | |
| 32 | /* This callback is called by invite session framework when UAC session |
| 33 | * has forked. |
| 34 | */ |
| 35 | static void pjsua_call_on_forked( pjsip_inv_session *inv, |
| 36 | pjsip_event *e); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 37 | |
| 38 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 39 | * Callback to be called when SDP offer/answer negotiation has just completed |
| 40 | * in the session. This function will start/update media if negotiation |
| 41 | * has succeeded. |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 42 | */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 43 | static void pjsua_call_on_media_update(pjsip_inv_session *inv, |
| 44 | pj_status_t status); |
Benny Prijono | 105217f | 2006-03-06 16:25:59 +0000 | [diff] [blame] | 45 | |
| 46 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 47 | * Called when session received new offer. |
Benny Prijono | 105217f | 2006-03-06 16:25:59 +0000 | [diff] [blame] | 48 | */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 49 | static void pjsua_call_on_rx_offer(pjsip_inv_session *inv, |
| 50 | const pjmedia_sdp_session *offer); |
| 51 | |
| 52 | /* |
Benny Prijono | 77998ce | 2007-06-20 10:03:46 +0000 | [diff] [blame] | 53 | * Called to generate new offer. |
| 54 | */ |
| 55 | static void pjsua_call_on_create_offer(pjsip_inv_session *inv, |
| 56 | pjmedia_sdp_session **offer); |
| 57 | |
| 58 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 59 | * This callback is called when transaction state has changed in INVITE |
| 60 | * session. We use this to trap: |
| 61 | * - incoming REFER request. |
| 62 | * - incoming MESSAGE request. |
| 63 | */ |
| 64 | static void pjsua_call_on_tsx_state_changed(pjsip_inv_session *inv, |
| 65 | pjsip_transaction *tsx, |
| 66 | pjsip_event *e); |
| 67 | |
| 68 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 69 | |
| 70 | /* Create inactive SDP for call hold. */ |
| 71 | static pj_status_t create_inactive_sdp(pjsua_call *call, |
| 72 | pjmedia_sdp_session **p_answer); |
| 73 | |
Benny Prijono | 7129cc7 | 2007-11-05 05:54:25 +0000 | [diff] [blame] | 74 | /* Update SDP version in the offer */ |
| 75 | static void update_sdp_version(pjsua_call *call, |
| 76 | pjmedia_sdp_session *sdp) |
| 77 | { |
| 78 | const pjmedia_sdp_session *old_sdp = NULL; |
| 79 | pj_status_t status; |
| 80 | |
| 81 | status = pjmedia_sdp_neg_get_active_local(call->inv->neg, &old_sdp); |
| 82 | if (status != PJ_SUCCESS || old_sdp == NULL) |
| 83 | return; |
| 84 | |
| 85 | sdp->origin.version = old_sdp->origin.version + 1; |
| 86 | } |
| 87 | |
| 88 | |
Benny Prijono | d524e82 | 2006-09-22 12:48:18 +0000 | [diff] [blame] | 89 | /* |
| 90 | * Callback called by event framework when the xfer subscription state |
| 91 | * has changed. |
| 92 | */ |
Benny Prijono | 4ddad2c | 2006-10-18 17:16:34 +0000 | [diff] [blame] | 93 | static void xfer_client_on_evsub_state( pjsip_evsub *sub, pjsip_event *event); |
| 94 | static void xfer_server_on_evsub_state( pjsip_evsub *sub, pjsip_event *event); |
| 95 | |
| 96 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 97 | * Reset call descriptor. |
| 98 | */ |
| 99 | static void reset_call(pjsua_call_id id) |
Benny Prijono | 105217f | 2006-03-06 16:25:59 +0000 | [diff] [blame] | 100 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 101 | pjsua_call *call = &pjsua_var.calls[id]; |
Benny Prijono | 105217f | 2006-03-06 16:25:59 +0000 | [diff] [blame] | 102 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 103 | call->index = id; |
| 104 | call->inv = NULL; |
| 105 | call->user_data = NULL; |
| 106 | call->session = NULL; |
Benny Prijono | 8147f40 | 2007-11-21 14:50:07 +0000 | [diff] [blame] | 107 | call->ssrc = pj_rand(); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 108 | call->xfer_sub = NULL; |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 109 | call->last_code = (pjsip_status_code) 0; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 110 | call->conf_slot = PJSUA_INVALID_ID; |
| 111 | call->last_text.ptr = call->last_text_buf_; |
| 112 | call->last_text.slen = 0; |
Benny Prijono | 4be63b5 | 2006-11-25 14:50:25 +0000 | [diff] [blame] | 113 | call->conn_time.sec = 0; |
| 114 | call->conn_time.msec = 0; |
| 115 | call->res_time.sec = 0; |
| 116 | call->res_time.msec = 0; |
Benny Prijono | 91a6a17 | 2007-10-31 08:59:29 +0000 | [diff] [blame] | 117 | call->rem_nat_type = PJ_STUN_NAT_TYPE_UNKNOWN; |
Benny Prijono | 105217f | 2006-03-06 16:25:59 +0000 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | |
Benny Prijono | 275fd68 | 2006-03-22 11:59:11 +0000 | [diff] [blame] | 121 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 122 | * Init call subsystem. |
Benny Prijono | 275fd68 | 2006-03-22 11:59:11 +0000 | [diff] [blame] | 123 | */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 124 | pj_status_t pjsua_call_subsys_init(const pjsua_config *cfg) |
Benny Prijono | 275fd68 | 2006-03-22 11:59:11 +0000 | [diff] [blame] | 125 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 126 | pjsip_inv_callback inv_cb; |
| 127 | unsigned i; |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 128 | const pj_str_t str_norefersub = { "norefersub", 10 }; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 129 | pj_status_t status; |
Benny Prijono | 275fd68 | 2006-03-22 11:59:11 +0000 | [diff] [blame] | 130 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 131 | /* Init calls array. */ |
| 132 | for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.calls); ++i) |
| 133 | reset_call(i); |
Benny Prijono | 275fd68 | 2006-03-22 11:59:11 +0000 | [diff] [blame] | 134 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 135 | /* Copy config */ |
| 136 | pjsua_config_dup(pjsua_var.pool, &pjsua_var.ua_cfg, cfg); |
Benny Prijono | 275fd68 | 2006-03-22 11:59:11 +0000 | [diff] [blame] | 137 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 138 | /* Initialize invite session callback. */ |
Benny Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 139 | pj_bzero(&inv_cb, sizeof(inv_cb)); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 140 | inv_cb.on_state_changed = &pjsua_call_on_state_changed; |
| 141 | inv_cb.on_new_session = &pjsua_call_on_forked; |
| 142 | inv_cb.on_media_update = &pjsua_call_on_media_update; |
| 143 | inv_cb.on_rx_offer = &pjsua_call_on_rx_offer; |
Benny Prijono | 77998ce | 2007-06-20 10:03:46 +0000 | [diff] [blame] | 144 | inv_cb.on_create_offer = &pjsua_call_on_create_offer; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 145 | inv_cb.on_tsx_state_changed = &pjsua_call_on_tsx_state_changed; |
Benny Prijono | 275fd68 | 2006-03-22 11:59:11 +0000 | [diff] [blame] | 146 | |
Benny Prijono | 275fd68 | 2006-03-22 11:59:11 +0000 | [diff] [blame] | 147 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 148 | /* Initialize invite session module: */ |
| 149 | status = pjsip_inv_usage_init(pjsua_var.endpt, &inv_cb); |
| 150 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
| 151 | |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 152 | /* Add "norefersub" in Supported header */ |
| 153 | pjsip_endpt_add_capability(pjsua_var.endpt, NULL, PJSIP_H_SUPPORTED, |
| 154 | NULL, 1, &str_norefersub); |
| 155 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 156 | return status; |
| 157 | } |
| 158 | |
| 159 | |
| 160 | /* |
| 161 | * Start call subsystem. |
| 162 | */ |
| 163 | pj_status_t pjsua_call_subsys_start(void) |
| 164 | { |
| 165 | /* Nothing to do */ |
Benny Prijono | 275fd68 | 2006-03-22 11:59:11 +0000 | [diff] [blame] | 166 | return PJ_SUCCESS; |
| 167 | } |
| 168 | |
| 169 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 170 | /* |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 171 | * Get maximum number of calls configured in pjsua. |
| 172 | */ |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 173 | PJ_DEF(unsigned) pjsua_call_get_max_count(void) |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 174 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 175 | return pjsua_var.ua_cfg.max_calls; |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 179 | /* |
| 180 | * Get number of currently active calls. |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 181 | */ |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 182 | PJ_DEF(unsigned) pjsua_call_get_count(void) |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 183 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 184 | return pjsua_var.call_cnt; |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 188 | /* |
| 189 | * Enum calls. |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 190 | */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 191 | PJ_DEF(pj_status_t) pjsua_enum_calls( pjsua_call_id ids[], |
| 192 | unsigned *count) |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 193 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 194 | unsigned i, c; |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 195 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 196 | PJ_ASSERT_RETURN(ids && *count, PJ_EINVAL); |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 197 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 198 | PJSUA_LOCK(); |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 199 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 200 | for (i=0, c=0; c<*count && i<pjsua_var.ua_cfg.max_calls; ++i) { |
| 201 | if (!pjsua_var.calls[i].inv) |
| 202 | continue; |
| 203 | ids[c] = i; |
| 204 | ++c; |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 205 | } |
| 206 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 207 | *count = c; |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 208 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 209 | PJSUA_UNLOCK(); |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 210 | |
| 211 | return PJ_SUCCESS; |
| 212 | } |
| 213 | |
| 214 | |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 215 | #define LATE_SDP 0 |
| 216 | |
Benny Prijono | 5773cd6 | 2008-01-19 13:01:42 +0000 | [diff] [blame] | 217 | /* Allocate one call id */ |
| 218 | static pjsua_call_id alloc_call_id(void) |
| 219 | { |
| 220 | pjsua_call_id cid; |
| 221 | |
| 222 | #if 1 |
| 223 | /* New algorithm: round-robin */ |
| 224 | if (pjsua_var.next_call_id >= (int)pjsua_var.ua_cfg.max_calls || |
| 225 | pjsua_var.next_call_id < 0) |
| 226 | { |
| 227 | cid = 0; |
| 228 | } |
| 229 | |
| 230 | for (cid=pjsua_var.next_call_id; |
| 231 | cid<(int)pjsua_var.ua_cfg.max_calls; |
| 232 | ++cid) |
| 233 | { |
| 234 | if (pjsua_var.calls[cid].inv == NULL) { |
| 235 | ++pjsua_var.next_call_id; |
| 236 | return cid; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | for (cid=0; cid < pjsua_var.next_call_id; ++cid) { |
| 241 | if (pjsua_var.calls[cid].inv == NULL) { |
| 242 | ++pjsua_var.next_call_id; |
| 243 | return cid; |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | #else |
| 248 | /* Old algorithm */ |
| 249 | for (cid=0; cid<(int)pjsua_var.ua_cfg.max_calls; ++cid) { |
| 250 | if (pjsua_var.calls[cid].inv == NULL) |
| 251 | return cid; |
| 252 | } |
| 253 | #endif |
| 254 | |
| 255 | return PJSUA_INVALID_ID; |
| 256 | } |
| 257 | |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 258 | /* Get signaling secure level. |
| 259 | * Return: |
| 260 | * 0: if signaling is not secure |
| 261 | * 1: if TLS transport is used for immediate hop |
| 262 | * 2: if end-to-end signaling is secure. |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 263 | */ |
Benny Prijono | db844a4 | 2008-02-02 17:07:18 +0000 | [diff] [blame] | 264 | static int get_secure_level(pjsua_acc_id acc_id, const pj_str_t *dst_uri) |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 265 | { |
| 266 | const pj_str_t tls = pj_str(";transport=tls"); |
| 267 | const pj_str_t sips = pj_str("sips:"); |
Benny Prijono | db844a4 | 2008-02-02 17:07:18 +0000 | [diff] [blame] | 268 | pjsua_acc *acc = &pjsua_var.acc[acc_id]; |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 269 | |
| 270 | if (pj_stristr(dst_uri, &sips)) |
| 271 | return 2; |
Benny Prijono | db844a4 | 2008-02-02 17:07:18 +0000 | [diff] [blame] | 272 | |
| 273 | if (!pj_list_empty(&acc->route_set)) { |
| 274 | pjsip_route_hdr *r = acc->route_set.next; |
| 275 | pjsip_uri *uri = r->name_addr.uri; |
| 276 | pjsip_sip_uri *sip_uri; |
| 277 | |
| 278 | sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri); |
| 279 | if (pj_stricmp2(&sip_uri->transport_param, "tls")==0) |
| 280 | return 1; |
| 281 | |
| 282 | } else { |
| 283 | if (pj_stristr(dst_uri, &tls)) |
| 284 | return 1; |
| 285 | } |
| 286 | |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 287 | return 0; |
| 288 | } |
| 289 | |
Benny Prijono | db844a4 | 2008-02-02 17:07:18 +0000 | [diff] [blame] | 290 | static int call_get_secure_level(pjsua_call *call) |
| 291 | { |
| 292 | if (call->inv->dlg->secure) |
| 293 | return 2; |
| 294 | |
| 295 | if (!pj_list_empty(&call->inv->dlg->route_set)) { |
| 296 | pjsip_route_hdr *r = call->inv->dlg->route_set.next; |
| 297 | pjsip_uri *uri = r->name_addr.uri; |
| 298 | pjsip_sip_uri *sip_uri; |
| 299 | |
| 300 | sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri); |
| 301 | if (pj_stricmp2(&sip_uri->transport_param, "tls")==0) |
| 302 | return 1; |
| 303 | |
| 304 | } else { |
| 305 | pjsip_sip_uri *sip_uri; |
| 306 | |
| 307 | if (PJSIP_URI_SCHEME_IS_SIPS(call->inv->dlg->target)) |
| 308 | return 2; |
| 309 | if (!PJSIP_URI_SCHEME_IS_SIP(call->inv->dlg->target)) |
| 310 | return 0; |
| 311 | |
| 312 | sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(call->inv->dlg->target); |
| 313 | if (pj_stricmp2(&sip_uri->transport_param, "tls")==0) |
| 314 | return 1; |
| 315 | } |
| 316 | |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 321 | /* |
| 322 | * Make outgoing call to the specified URI using the specified account. |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 323 | */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 324 | PJ_DEF(pj_status_t) pjsua_call_make_call( pjsua_acc_id acc_id, |
| 325 | const pj_str_t *dest_uri, |
| 326 | unsigned options, |
| 327 | void *user_data, |
| 328 | const pjsua_msg_data *msg_data, |
| 329 | pjsua_call_id *p_call_id) |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 330 | { |
Benny Prijono | 1c2bf46 | 2006-03-05 11:54:02 +0000 | [diff] [blame] | 331 | pjsip_dialog *dlg = NULL; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 332 | pjmedia_sdp_session *offer; |
Benny Prijono | 1c2bf46 | 2006-03-05 11:54:02 +0000 | [diff] [blame] | 333 | pjsip_inv_session *inv = NULL; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 334 | pjsua_acc *acc; |
| 335 | pjsua_call *call; |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 336 | int call_id = -1; |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 337 | pj_str_t contact; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 338 | pjsip_tx_data *tdata; |
| 339 | pj_status_t status; |
| 340 | |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 341 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 342 | /* Check that account is valid */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 343 | PJ_ASSERT_RETURN(acc_id>=0 || acc_id<(int)PJ_ARRAY_SIZE(pjsua_var.acc), |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 344 | PJ_EINVAL); |
| 345 | |
Benny Prijono | 320fa4d | 2006-12-07 10:09:16 +0000 | [diff] [blame] | 346 | /* Check arguments */ |
| 347 | PJ_ASSERT_RETURN(dest_uri, PJ_EINVAL); |
| 348 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 349 | PJSUA_LOCK(); |
| 350 | |
| 351 | acc = &pjsua_var.acc[acc_id]; |
| 352 | if (!acc->valid) { |
| 353 | pjsua_perror(THIS_FILE, "Unable to make call because account " |
| 354 | "is not valid", PJ_EINVALIDOP); |
| 355 | PJSUA_UNLOCK(); |
| 356 | return PJ_EINVALIDOP; |
| 357 | } |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 358 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 359 | /* Find free call slot. */ |
Benny Prijono | 5773cd6 | 2008-01-19 13:01:42 +0000 | [diff] [blame] | 360 | call_id = alloc_call_id(); |
Benny Prijono | f04ffdd | 2006-02-21 00:11:18 +0000 | [diff] [blame] | 361 | |
Benny Prijono | 5773cd6 | 2008-01-19 13:01:42 +0000 | [diff] [blame] | 362 | if (call_id == PJSUA_INVALID_ID) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 363 | pjsua_perror(THIS_FILE, "Error making file", PJ_ETOOMANY); |
| 364 | PJSUA_UNLOCK(); |
Benny Prijono | f04ffdd | 2006-02-21 00:11:18 +0000 | [diff] [blame] | 365 | return PJ_ETOOMANY; |
| 366 | } |
| 367 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 368 | call = &pjsua_var.calls[call_id]; |
| 369 | |
Benny Prijono | 320fa4d | 2006-12-07 10:09:16 +0000 | [diff] [blame] | 370 | /* Verify that destination URI is valid before calling |
| 371 | * pjsua_acc_create_uac_contact, or otherwise there |
| 372 | * a misleading "Invalid Contact URI" error will be printed |
| 373 | * when pjsua_acc_create_uac_contact() fails. |
| 374 | */ |
| 375 | if (1) { |
| 376 | pj_pool_t *pool; |
| 377 | pjsip_uri *uri; |
| 378 | pj_str_t dup; |
| 379 | |
| 380 | pool = pjsua_pool_create("tmp-uri", 4000, 4000); |
| 381 | if (!pool) { |
| 382 | pjsua_perror(THIS_FILE, "Unable to create pool", PJ_ENOMEM); |
| 383 | PJSUA_UNLOCK(); |
| 384 | return PJ_ENOMEM; |
| 385 | } |
| 386 | |
| 387 | pj_strdup_with_null(pool, &dup, dest_uri); |
| 388 | uri = pjsip_parse_uri(pool, dup.ptr, dup.slen, 0); |
| 389 | pj_pool_release(pool); |
| 390 | |
| 391 | if (uri == NULL) { |
| 392 | pjsua_perror(THIS_FILE, "Unable to make call", |
| 393 | PJSIP_EINVALIDREQURI); |
| 394 | PJSUA_UNLOCK(); |
| 395 | return PJSIP_EINVALIDREQURI; |
| 396 | } |
| 397 | } |
| 398 | |
Benny Prijono | 093d302 | 2006-09-24 00:07:11 +0000 | [diff] [blame] | 399 | PJ_LOG(4,(THIS_FILE, "Making call with acc #%d to %.*s", acc_id, |
| 400 | (int)dest_uri->slen, dest_uri->ptr)); |
| 401 | |
Benny Prijono | e21e784 | 2006-04-09 16:46:05 +0000 | [diff] [blame] | 402 | /* Mark call start time. */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 403 | pj_gettimeofday(&call->start_time); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 404 | |
Benny Prijono | e21e784 | 2006-04-09 16:46:05 +0000 | [diff] [blame] | 405 | /* Reset first response time */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 406 | call->res_time.sec = 0; |
Benny Prijono | e21e784 | 2006-04-09 16:46:05 +0000 | [diff] [blame] | 407 | |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 408 | /* Create suitable Contact header */ |
| 409 | status = pjsua_acc_create_uac_contact(pjsua_var.pool, &contact, |
| 410 | acc_id, dest_uri); |
| 411 | if (status != PJ_SUCCESS) { |
| 412 | pjsua_perror(THIS_FILE, "Unable to generate Contact header", status); |
| 413 | PJSUA_UNLOCK(); |
| 414 | return status; |
| 415 | } |
| 416 | |
Benny Prijono | e21e784 | 2006-04-09 16:46:05 +0000 | [diff] [blame] | 417 | /* Create outgoing dialog: */ |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 418 | status = pjsip_dlg_create_uac( pjsip_ua_instance(), |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 419 | &acc->cfg.id, &contact, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 420 | dest_uri, dest_uri, &dlg); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 421 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 422 | pjsua_perror(THIS_FILE, "Dialog creation failed", status); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 423 | PJSUA_UNLOCK(); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 424 | return status; |
| 425 | } |
| 426 | |
Benny Prijono | db844a4 | 2008-02-02 17:07:18 +0000 | [diff] [blame] | 427 | /* Calculate call's secure level */ |
| 428 | call->secure_level = get_secure_level(acc_id, dest_uri); |
| 429 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 430 | /* Init media channel */ |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 431 | status = pjsua_media_channel_init(call->index, PJSIP_ROLE_UAC, |
Benny Prijono | db844a4 | 2008-02-02 17:07:18 +0000 | [diff] [blame] | 432 | call->secure_level, NULL); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 433 | if (status != PJ_SUCCESS) { |
| 434 | pjsua_perror(THIS_FILE, "Error initializing media channel", status); |
| 435 | goto on_error; |
| 436 | } |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 437 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 438 | /* Create SDP offer */ |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 439 | #if LATE_SDP |
| 440 | offer = NULL; |
| 441 | #else |
Benny Prijono | 25b2ea1 | 2008-01-24 19:20:54 +0000 | [diff] [blame] | 442 | status = pjsua_media_channel_create_sdp(call->index, dlg->pool, NULL, |
| 443 | &offer, NULL); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 444 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 445 | pjsua_perror(THIS_FILE, "pjmedia unable to create SDP", status); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 446 | goto on_error; |
| 447 | } |
Benny Prijono | 1f7767b | 2007-10-03 18:28:49 +0000 | [diff] [blame] | 448 | #endif |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 449 | |
| 450 | /* Create the INVITE session: */ |
Benny Prijono | dcfc0ba | 2007-09-30 16:50:27 +0000 | [diff] [blame] | 451 | options |= PJSIP_INV_SUPPORT_100REL; |
Benny Prijono | dcfc0ba | 2007-09-30 16:50:27 +0000 | [diff] [blame] | 452 | if (acc->cfg.require_100rel) |
| 453 | options |= PJSIP_INV_REQUIRE_100REL; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 454 | |
Benny Prijono | dcfc0ba | 2007-09-30 16:50:27 +0000 | [diff] [blame] | 455 | status = pjsip_inv_create_uac( dlg, offer, options, &inv); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 456 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 457 | pjsua_perror(THIS_FILE, "Invite session creation failed", status); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 458 | goto on_error; |
| 459 | } |
| 460 | |
| 461 | |
| 462 | /* Create and associate our data in the session. */ |
Benny Prijono | b8864a9 | 2007-07-20 08:37:29 +0000 | [diff] [blame] | 463 | call->acc_id = acc_id; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 464 | call->inv = inv; |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 465 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 466 | dlg->mod_data[pjsua_var.mod.id] = call; |
| 467 | inv->mod_data[pjsua_var.mod.id] = call; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 468 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 469 | /* Attach user data */ |
| 470 | call->user_data = user_data; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 471 | |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 472 | /* If account is locked to specific transport, then lock dialog |
| 473 | * to this transport too. |
| 474 | */ |
| 475 | if (acc->cfg.transport_id != PJSUA_INVALID_ID) { |
| 476 | pjsip_tpselector tp_sel; |
| 477 | |
| 478 | pjsua_init_tpselector(acc->cfg.transport_id, &tp_sel); |
| 479 | pjsip_dlg_set_transport(dlg, &tp_sel); |
| 480 | } |
| 481 | |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 482 | /* Set dialog Route-Set: */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 483 | if (!pj_list_empty(&acc->route_set)) |
| 484 | pjsip_dlg_set_route_set(dlg, &acc->route_set); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 485 | |
| 486 | |
| 487 | /* Set credentials: */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 488 | if (acc->cred_cnt) { |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 489 | pjsip_auth_clt_set_credentials( &dlg->auth_sess, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 490 | acc->cred_cnt, acc->cred); |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 491 | } |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 492 | |
Benny Prijono | 48ab2b7 | 2007-11-08 09:24:30 +0000 | [diff] [blame] | 493 | /* Set authentication preference */ |
| 494 | pjsip_auth_clt_set_prefs(&dlg->auth_sess, &acc->cfg.auth_pref); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 495 | |
| 496 | /* Create initial INVITE: */ |
| 497 | |
| 498 | status = pjsip_inv_invite(inv, &tdata); |
| 499 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 500 | pjsua_perror(THIS_FILE, "Unable to create initial INVITE request", |
| 501 | status); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 502 | goto on_error; |
| 503 | } |
| 504 | |
| 505 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 506 | /* Add additional headers etc */ |
| 507 | |
| 508 | pjsua_process_msg_data( tdata, msg_data); |
| 509 | |
Benny Prijono | 093d302 | 2006-09-24 00:07:11 +0000 | [diff] [blame] | 510 | /* Must increment call counter now */ |
| 511 | ++pjsua_var.call_cnt; |
| 512 | |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 513 | /* Send initial INVITE: */ |
| 514 | |
Benny Prijono | e8b0d3b | 2006-03-17 17:57:52 +0000 | [diff] [blame] | 515 | status = pjsip_inv_send_msg(inv, tdata); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 516 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 517 | pjsua_perror(THIS_FILE, "Unable to send initial INVITE request", |
| 518 | status); |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 519 | |
| 520 | /* Upon failure to send first request, both dialog and invite |
| 521 | * session would have been cleared. |
| 522 | */ |
| 523 | inv = NULL; |
| 524 | dlg = NULL; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 525 | goto on_error; |
| 526 | } |
| 527 | |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 528 | /* Done. */ |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 529 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 530 | if (p_call_id) |
| 531 | *p_call_id = call_id; |
| 532 | |
| 533 | PJSUA_UNLOCK(); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 534 | |
| 535 | return PJ_SUCCESS; |
| 536 | |
| 537 | |
| 538 | on_error: |
Benny Prijono | 1c2bf46 | 2006-03-05 11:54:02 +0000 | [diff] [blame] | 539 | if (inv != NULL) { |
| 540 | pjsip_inv_terminate(inv, PJSIP_SC_OK, PJ_FALSE); |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 541 | } else if (dlg) { |
Benny Prijono | 1c2bf46 | 2006-03-05 11:54:02 +0000 | [diff] [blame] | 542 | pjsip_dlg_terminate(dlg); |
| 543 | } |
| 544 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 545 | if (call_id != -1) { |
| 546 | reset_call(call_id); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 547 | pjsua_media_channel_deinit(call_id); |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 550 | PJSUA_UNLOCK(); |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 551 | return status; |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | |
Benny Prijono | 91a6a17 | 2007-10-31 08:59:29 +0000 | [diff] [blame] | 555 | /* Get the NAT type information in remote's SDP */ |
| 556 | static void update_remote_nat_type(pjsua_call *call, |
| 557 | const pjmedia_sdp_session *sdp) |
| 558 | { |
| 559 | const pjmedia_sdp_attr *xnat; |
| 560 | |
| 561 | xnat = pjmedia_sdp_attr_find2(sdp->attr_count, sdp->attr, "X-nat", NULL); |
| 562 | if (xnat) { |
| 563 | call->rem_nat_type = (pj_stun_nat_type) (xnat->value.ptr[0] - '0'); |
| 564 | } else { |
| 565 | call->rem_nat_type = PJ_STUN_NAT_TYPE_UNKNOWN; |
| 566 | } |
| 567 | |
| 568 | PJ_LOG(5,(THIS_FILE, "Call %d: remote NAT type is %d (%s)", call->index, |
| 569 | call->rem_nat_type, pj_stun_get_nat_name(call->rem_nat_type))); |
| 570 | } |
| 571 | |
| 572 | |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 573 | /** |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 574 | * Handle incoming INVITE request. |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 575 | * Called by pjsua_core.c |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 576 | */ |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 577 | pj_bool_t pjsua_call_on_incoming(pjsip_rx_data *rdata) |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 578 | { |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 579 | pj_str_t contact; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 580 | pjsip_dialog *dlg = pjsip_rdata_get_dlg(rdata); |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 581 | pjsip_dialog *replaced_dlg = NULL; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 582 | pjsip_transaction *tsx = pjsip_rdata_get_tsx(rdata); |
| 583 | pjsip_msg *msg = rdata->msg_info.msg; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 584 | pjsip_tx_data *response = NULL; |
| 585 | unsigned options = 0; |
Benny Prijono | 1c2bf46 | 2006-03-05 11:54:02 +0000 | [diff] [blame] | 586 | pjsip_inv_session *inv = NULL; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 587 | int acc_id; |
| 588 | pjsua_call *call; |
| 589 | int call_id = -1; |
Benny Prijono | db844a4 | 2008-02-02 17:07:18 +0000 | [diff] [blame] | 590 | int sip_err_code; |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 591 | pjmedia_sdp_session *offer, *answer; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 592 | pj_status_t status; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 593 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 594 | /* Don't want to handle anything but INVITE */ |
| 595 | if (msg->line.req.method.id != PJSIP_INVITE_METHOD) |
| 596 | return PJ_FALSE; |
| 597 | |
| 598 | /* Don't want to handle anything that's already associated with |
| 599 | * existing dialog or transaction. |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 600 | */ |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 601 | if (dlg || tsx) |
| 602 | return PJ_FALSE; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 603 | |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 604 | PJSUA_LOCK(); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 605 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 606 | /* Find free call slot. */ |
Benny Prijono | 5773cd6 | 2008-01-19 13:01:42 +0000 | [diff] [blame] | 607 | call_id = alloc_call_id(); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 608 | |
Benny Prijono | 5773cd6 | 2008-01-19 13:01:42 +0000 | [diff] [blame] | 609 | if (call_id == PJSUA_INVALID_ID) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 610 | pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 611 | PJSIP_SC_BUSY_HERE, NULL, |
| 612 | NULL, NULL); |
Benny Prijono | a38ada0 | 2006-07-02 14:22:35 +0000 | [diff] [blame] | 613 | PJ_LOG(2,(THIS_FILE, |
| 614 | "Unable to accept incoming call (too many calls)")); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 615 | PJSUA_UNLOCK(); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 616 | return PJ_TRUE; |
| 617 | } |
| 618 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 619 | /* Clear call descriptor */ |
| 620 | reset_call(call_id); |
Benny Prijono | e21e784 | 2006-04-09 16:46:05 +0000 | [diff] [blame] | 621 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 622 | call = &pjsua_var.calls[call_id]; |
| 623 | |
| 624 | /* Mark call start time. */ |
| 625 | pj_gettimeofday(&call->start_time); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 626 | |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 627 | /* Check INVITE request for Replaces header. If Replaces header is |
| 628 | * present, the function will make sure that we can handle the request. |
| 629 | */ |
| 630 | status = pjsip_replaces_verify_request(rdata, &replaced_dlg, PJ_FALSE, |
| 631 | &response); |
| 632 | if (status != PJ_SUCCESS) { |
| 633 | /* |
| 634 | * Something wrong with the Replaces header. |
| 635 | */ |
| 636 | if (response) { |
| 637 | pjsip_response_addr res_addr; |
| 638 | |
| 639 | pjsip_get_response_addr(response->pool, rdata, &res_addr); |
| 640 | pjsip_endpt_send_response(pjsua_var.endpt, &res_addr, response, |
| 641 | NULL, NULL); |
| 642 | |
| 643 | } else { |
| 644 | |
| 645 | /* Respond with 500 (Internal Server Error) */ |
| 646 | pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, 500, NULL, |
| 647 | NULL, NULL); |
| 648 | } |
| 649 | |
| 650 | PJSUA_UNLOCK(); |
| 651 | return PJ_TRUE; |
| 652 | } |
| 653 | |
| 654 | /* If this INVITE request contains Replaces header, notify application |
| 655 | * about the request so that application can do subsequent checking |
| 656 | * if it wants to. |
| 657 | */ |
| 658 | if (replaced_dlg != NULL && pjsua_var.ua_cfg.cb.on_call_replace_request) { |
| 659 | pjsua_call *replaced_call; |
| 660 | int st_code = 200; |
| 661 | pj_str_t st_text = { "OK", 2 }; |
| 662 | |
| 663 | /* Get the replaced call instance */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 664 | replaced_call = (pjsua_call*) replaced_dlg->mod_data[pjsua_var.mod.id]; |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 665 | |
| 666 | /* Notify application */ |
| 667 | pjsua_var.ua_cfg.cb.on_call_replace_request(replaced_call->index, |
| 668 | rdata, &st_code, &st_text); |
| 669 | |
| 670 | /* Must specify final response */ |
| 671 | PJ_ASSERT_ON_FAIL(st_code >= 200, st_code = 200); |
| 672 | |
| 673 | /* Check if application rejects this request. */ |
| 674 | if (st_code >= 300) { |
| 675 | |
| 676 | if (st_text.slen == 2) |
| 677 | st_text = *pjsip_get_status_text(st_code); |
| 678 | |
| 679 | pjsip_endpt_respond(pjsua_var.endpt, NULL, rdata, |
| 680 | st_code, &st_text, NULL, NULL, NULL); |
| 681 | PJSUA_UNLOCK(); |
| 682 | return PJ_TRUE; |
| 683 | } |
| 684 | } |
| 685 | |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 686 | /* |
| 687 | * Get which account is most likely to be associated with this incoming |
| 688 | * call. We need the account to find which contact URI to put for |
| 689 | * the call. |
| 690 | */ |
| 691 | acc_id = call->acc_id = pjsua_acc_find_for_incoming(rdata); |
| 692 | |
Benny Prijono | db844a4 | 2008-02-02 17:07:18 +0000 | [diff] [blame] | 693 | /* Get call's secure level */ |
| 694 | if (PJSIP_URI_SCHEME_IS_SIPS(rdata->msg_info.msg->line.req.uri)) |
| 695 | call->secure_level = 2; |
| 696 | else if (PJSIP_TRANSPORT_IS_SECURE(rdata->tp_info.transport)) |
| 697 | call->secure_level = 1; |
| 698 | else |
| 699 | call->secure_level = 0; |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 700 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 701 | /* Init media channel */ |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 702 | status = pjsua_media_channel_init(call->index, PJSIP_ROLE_UAS, |
Benny Prijono | db844a4 | 2008-02-02 17:07:18 +0000 | [diff] [blame] | 703 | call->secure_level, &sip_err_code); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 704 | if (status != PJ_SUCCESS) { |
Benny Prijono | 25b2ea1 | 2008-01-24 19:20:54 +0000 | [diff] [blame] | 705 | pjsua_perror(THIS_FILE, "Error initializing media channel", status); |
| 706 | pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, |
| 707 | sip_err_code, NULL, |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 708 | NULL, NULL); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 709 | PJSUA_UNLOCK(); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 710 | return PJ_TRUE; |
| 711 | } |
| 712 | |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 713 | /* Parse SDP from incoming request */ |
| 714 | if (rdata->msg_info.msg->body) { |
| 715 | status = pjmedia_sdp_parse(rdata->tp_info.pool, |
| 716 | rdata->msg_info.msg->body->data, |
| 717 | rdata->msg_info.msg->body->len, &offer); |
| 718 | if (status != PJ_SUCCESS) { |
| 719 | pjsua_perror(THIS_FILE, "Error parsing SDP in incoming INVITE", status); |
| 720 | pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, 400, NULL, |
| 721 | NULL, NULL); |
| 722 | pjsua_media_channel_deinit(call->index); |
| 723 | PJSUA_UNLOCK(); |
| 724 | return PJ_TRUE; |
| 725 | } |
| 726 | } else { |
| 727 | offer = NULL; |
| 728 | } |
Benny Prijono | 1d9b9a4 | 2006-09-25 13:40:12 +0000 | [diff] [blame] | 729 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 730 | /* Get media capability from media endpoint: */ |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 731 | status = pjsua_media_channel_create_sdp(call->index, rdata->tp_info.pool, |
Benny Prijono | 25b2ea1 | 2008-01-24 19:20:54 +0000 | [diff] [blame] | 732 | offer, &answer, &sip_err_code); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 733 | if (status != PJ_SUCCESS) { |
Benny Prijono | 25b2ea1 | 2008-01-24 19:20:54 +0000 | [diff] [blame] | 734 | pjsua_perror(THIS_FILE, "Error creating SDP answer", status); |
| 735 | pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, |
| 736 | sip_err_code, NULL, |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 737 | NULL, NULL); |
| 738 | pjsua_media_channel_deinit(call->index); |
| 739 | PJSUA_UNLOCK(); |
| 740 | return PJ_TRUE; |
| 741 | } |
| 742 | |
Benny Prijono | 1d9b9a4 | 2006-09-25 13:40:12 +0000 | [diff] [blame] | 743 | /* Verify that we can handle the request. */ |
Benny Prijono | dcfc0ba | 2007-09-30 16:50:27 +0000 | [diff] [blame] | 744 | options |= PJSIP_INV_SUPPORT_100REL; |
Benny Prijono | dcfc0ba | 2007-09-30 16:50:27 +0000 | [diff] [blame] | 745 | if (pjsua_var.acc[acc_id].cfg.require_100rel) |
| 746 | options |= PJSIP_INV_REQUIRE_100REL; |
| 747 | |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 748 | status = pjsip_inv_verify_request2(rdata, &options, offer, answer, NULL, |
| 749 | pjsua_var.endpt, &response); |
Benny Prijono | 1d9b9a4 | 2006-09-25 13:40:12 +0000 | [diff] [blame] | 750 | if (status != PJ_SUCCESS) { |
| 751 | |
| 752 | /* |
| 753 | * No we can't handle the incoming INVITE request. |
| 754 | */ |
Benny Prijono | 1d9b9a4 | 2006-09-25 13:40:12 +0000 | [diff] [blame] | 755 | if (response) { |
| 756 | pjsip_response_addr res_addr; |
| 757 | |
| 758 | pjsip_get_response_addr(response->pool, rdata, &res_addr); |
| 759 | pjsip_endpt_send_response(pjsua_var.endpt, &res_addr, response, |
| 760 | NULL, NULL); |
| 761 | |
| 762 | } else { |
| 763 | |
| 764 | /* Respond with 500 (Internal Server Error) */ |
| 765 | pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, 500, NULL, |
| 766 | NULL, NULL); |
| 767 | } |
| 768 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 769 | pjsua_media_channel_deinit(call->index); |
Benny Prijono | 1d9b9a4 | 2006-09-25 13:40:12 +0000 | [diff] [blame] | 770 | PJSUA_UNLOCK(); |
| 771 | return PJ_TRUE; |
| 772 | } |
| 773 | |
| 774 | |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 775 | /* Get suitable Contact header */ |
| 776 | status = pjsua_acc_create_uas_contact(rdata->tp_info.pool, &contact, |
| 777 | acc_id, rdata); |
| 778 | if (status != PJ_SUCCESS) { |
| 779 | pjsua_perror(THIS_FILE, "Unable to generate Contact header", status); |
| 780 | pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, 500, NULL, |
| 781 | NULL, NULL); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 782 | pjsua_media_channel_deinit(call->index); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 783 | PJSUA_UNLOCK(); |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 784 | return PJ_TRUE; |
| 785 | } |
| 786 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 787 | /* Create dialog: */ |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 788 | status = pjsip_dlg_create_uas( pjsip_ua_instance(), rdata, |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 789 | &contact, &dlg); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 790 | if (status != PJ_SUCCESS) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 791 | pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, 500, NULL, |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 792 | NULL, NULL); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 793 | pjsua_media_channel_deinit(call->index); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 794 | PJSUA_UNLOCK(); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 795 | return PJ_TRUE; |
| 796 | } |
| 797 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 798 | /* Set credentials */ |
| 799 | if (pjsua_var.acc[acc_id].cred_cnt) { |
| 800 | pjsip_auth_clt_set_credentials(&dlg->auth_sess, |
| 801 | pjsua_var.acc[acc_id].cred_cnt, |
| 802 | pjsua_var.acc[acc_id].cred); |
| 803 | } |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 804 | |
Benny Prijono | 48ab2b7 | 2007-11-08 09:24:30 +0000 | [diff] [blame] | 805 | /* Set preference */ |
| 806 | pjsip_auth_clt_set_prefs(&dlg->auth_sess, |
| 807 | &pjsua_var.acc[acc_id].cfg.auth_pref); |
| 808 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 809 | /* Create invite session: */ |
Benny Prijono | dcfc0ba | 2007-09-30 16:50:27 +0000 | [diff] [blame] | 810 | status = pjsip_inv_create_uas( dlg, rdata, answer, options, &inv); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 811 | if (status != PJ_SUCCESS) { |
Benny Prijono | a38ada0 | 2006-07-02 14:22:35 +0000 | [diff] [blame] | 812 | pjsip_hdr hdr_list; |
| 813 | pjsip_warning_hdr *w; |
| 814 | |
| 815 | w = pjsip_warning_hdr_create_from_status(dlg->pool, |
| 816 | pjsip_endpt_name(pjsua_var.endpt), |
| 817 | status); |
| 818 | pj_list_init(&hdr_list); |
| 819 | pj_list_push_back(&hdr_list, w); |
| 820 | |
| 821 | pjsip_dlg_respond(dlg, rdata, 500, NULL, &hdr_list, NULL); |
| 822 | |
| 823 | /* Can't terminate dialog because transaction is in progress. |
Benny Prijono | 1c2bf46 | 2006-03-05 11:54:02 +0000 | [diff] [blame] | 824 | pjsip_dlg_terminate(dlg); |
Benny Prijono | a38ada0 | 2006-07-02 14:22:35 +0000 | [diff] [blame] | 825 | */ |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 826 | pjsua_media_channel_deinit(call->index); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 827 | PJSUA_UNLOCK(); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 828 | return PJ_TRUE; |
| 829 | } |
| 830 | |
Benny Prijono | ea9fd39 | 2007-11-06 03:41:40 +0000 | [diff] [blame] | 831 | /* Update NAT type of remote endpoint, only when there is SDP in |
| 832 | * incoming INVITE! |
| 833 | */ |
| 834 | if (pjsua_var.ua_cfg.nat_type_in_sdp && |
| 835 | pjmedia_sdp_neg_get_state(inv->neg) > PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER) |
| 836 | { |
Benny Prijono | 91a6a17 | 2007-10-31 08:59:29 +0000 | [diff] [blame] | 837 | const pjmedia_sdp_session *remote_sdp; |
| 838 | |
| 839 | if (pjmedia_sdp_neg_get_neg_remote(inv->neg, &remote_sdp)==PJ_SUCCESS) |
| 840 | update_remote_nat_type(call, remote_sdp); |
| 841 | } |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 842 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 843 | /* Create and attach pjsua_var data to the dialog: */ |
| 844 | call->inv = inv; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 845 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 846 | dlg->mod_data[pjsua_var.mod.id] = call; |
| 847 | inv->mod_data[pjsua_var.mod.id] = call; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 848 | |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 849 | /* If account is locked to specific transport, then lock dialog |
| 850 | * to this transport too. |
| 851 | */ |
| 852 | if (pjsua_var.acc[acc_id].cfg.transport_id != PJSUA_INVALID_ID) { |
| 853 | pjsip_tpselector tp_sel; |
| 854 | |
| 855 | pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel); |
| 856 | pjsip_dlg_set_transport(dlg, &tp_sel); |
| 857 | } |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 858 | |
Benny Prijono | 64f851e | 2006-02-23 13:49:28 +0000 | [diff] [blame] | 859 | /* Must answer with some response to initial INVITE. |
Benny Prijono | 64f851e | 2006-02-23 13:49:28 +0000 | [diff] [blame] | 860 | */ |
Benny Prijono | 64f851e | 2006-02-23 13:49:28 +0000 | [diff] [blame] | 861 | status = pjsip_inv_initial_answer(inv, rdata, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 862 | 100, NULL, NULL, &response); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 863 | if (status != PJ_SUCCESS) { |
Benny Prijono | ccb03fa | 2006-03-06 13:35:47 +0000 | [diff] [blame] | 864 | pjsua_perror(THIS_FILE, "Unable to send answer to incoming INVITE", |
| 865 | status); |
| 866 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 867 | pjsip_dlg_respond(dlg, rdata, 500, NULL, NULL, NULL); |
| 868 | pjsip_inv_terminate(inv, 500, PJ_FALSE); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 869 | pjsua_media_channel_deinit(call->index); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 870 | PJSUA_UNLOCK(); |
Benny Prijono | ccb03fa | 2006-03-06 13:35:47 +0000 | [diff] [blame] | 871 | return PJ_TRUE; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 872 | |
| 873 | } else { |
Benny Prijono | e8b0d3b | 2006-03-17 17:57:52 +0000 | [diff] [blame] | 874 | status = pjsip_inv_send_msg(inv, response); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 875 | if (status != PJ_SUCCESS) { |
Benny Prijono | d2ae29d | 2006-02-22 15:42:31 +0000 | [diff] [blame] | 876 | pjsua_perror(THIS_FILE, "Unable to send 100 response", status); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 877 | } |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 878 | } |
| 879 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 880 | ++pjsua_var.call_cnt; |
Benny Prijono | 64f851e | 2006-02-23 13:49:28 +0000 | [diff] [blame] | 881 | |
Benny Prijono | 105217f | 2006-03-06 16:25:59 +0000 | [diff] [blame] | 882 | |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 883 | /* Check if this request should replace existing call */ |
| 884 | if (replaced_dlg) { |
| 885 | pjsip_inv_session *replaced_inv; |
| 886 | struct pjsua_call *replaced_call; |
| 887 | pjsip_tx_data *tdata; |
| 888 | |
| 889 | /* Get the invite session in the dialog */ |
| 890 | replaced_inv = pjsip_dlg_get_inv_session(replaced_dlg); |
| 891 | |
| 892 | /* Get the replaced call instance */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 893 | replaced_call = (pjsua_call*) replaced_dlg->mod_data[pjsua_var.mod.id]; |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 894 | |
| 895 | /* Notify application */ |
| 896 | if (pjsua_var.ua_cfg.cb.on_call_replaced) |
| 897 | pjsua_var.ua_cfg.cb.on_call_replaced(replaced_call->index, |
| 898 | call_id); |
| 899 | |
| 900 | PJ_LOG(4,(THIS_FILE, "Answering replacement call %d with 200/OK", |
| 901 | call_id)); |
| 902 | |
| 903 | /* Answer the new call with 200 response */ |
| 904 | status = pjsip_inv_answer(inv, 200, NULL, NULL, &tdata); |
| 905 | if (status == PJ_SUCCESS) |
| 906 | status = pjsip_inv_send_msg(inv, tdata); |
| 907 | |
| 908 | if (status != PJ_SUCCESS) |
| 909 | pjsua_perror(THIS_FILE, "Error answering session", status); |
| 910 | |
Benny Prijono | fed7f4c | 2007-03-27 11:01:45 +0000 | [diff] [blame] | 911 | /* Note that inv may be invalid if 200/OK has caused error in |
| 912 | * starting the media. |
| 913 | */ |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 914 | |
| 915 | PJ_LOG(4,(THIS_FILE, "Disconnecting replaced call %d", |
| 916 | replaced_call->index)); |
| 917 | |
| 918 | /* Disconnect replaced invite session */ |
| 919 | status = pjsip_inv_end_session(replaced_inv, PJSIP_SC_GONE, NULL, |
| 920 | &tdata); |
| 921 | if (status == PJ_SUCCESS && tdata) |
| 922 | status = pjsip_inv_send_msg(replaced_inv, tdata); |
| 923 | |
| 924 | if (status != PJ_SUCCESS) |
| 925 | pjsua_perror(THIS_FILE, "Error terminating session", status); |
| 926 | |
| 927 | |
| 928 | } else { |
| 929 | |
Benny Prijono | b5388cf | 2007-01-04 22:45:08 +0000 | [diff] [blame] | 930 | /* Notify application if on_incoming_call() is overriden, |
| 931 | * otherwise hangup the call with 480 |
| 932 | */ |
| 933 | if (pjsua_var.ua_cfg.cb.on_incoming_call) { |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 934 | pjsua_var.ua_cfg.cb.on_incoming_call(acc_id, call_id, rdata); |
Benny Prijono | b5388cf | 2007-01-04 22:45:08 +0000 | [diff] [blame] | 935 | } else { |
| 936 | pjsua_call_hangup(call_id, PJSIP_SC_TEMPORARILY_UNAVAILABLE, |
| 937 | NULL, NULL); |
| 938 | } |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 939 | } |
| 940 | |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 941 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 942 | /* This INVITE request has been handled. */ |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 943 | PJSUA_UNLOCK(); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 944 | return PJ_TRUE; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 945 | } |
| 946 | |
| 947 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 948 | |
| 949 | /* |
| 950 | * Check if the specified call has active INVITE session and the INVITE |
| 951 | * session has not been disconnected. |
| 952 | */ |
| 953 | PJ_DEF(pj_bool_t) pjsua_call_is_active(pjsua_call_id call_id) |
| 954 | { |
| 955 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 956 | PJ_EINVAL); |
| 957 | return pjsua_var.calls[call_id].inv != NULL && |
| 958 | pjsua_var.calls[call_id].inv->state != PJSIP_INV_STATE_DISCONNECTED; |
| 959 | } |
| 960 | |
| 961 | |
| 962 | /* |
| 963 | * Check if call has an active media session. |
| 964 | */ |
| 965 | PJ_DEF(pj_bool_t) pjsua_call_has_media(pjsua_call_id call_id) |
| 966 | { |
| 967 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 968 | PJ_EINVAL); |
| 969 | return pjsua_var.calls[call_id].session != NULL; |
| 970 | } |
| 971 | |
| 972 | |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 973 | /* Acquire lock to the specified call_id */ |
Benny Prijono | 627cbb4 | 2007-09-25 20:48:49 +0000 | [diff] [blame] | 974 | pj_status_t acquire_call(const char *title, |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 975 | pjsua_call_id call_id, |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 976 | pjsua_call **p_call, |
| 977 | pjsip_dialog **p_dlg) |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 978 | { |
| 979 | enum { MAX_RETRY=50 }; |
| 980 | unsigned retry; |
Benny Prijono | d524e82 | 2006-09-22 12:48:18 +0000 | [diff] [blame] | 981 | pjsua_call *call = NULL; |
| 982 | pj_bool_t has_pjsua_lock = PJ_FALSE; |
| 983 | pj_status_t status = PJ_SUCCESS; |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 984 | |
| 985 | for (retry=0; retry<MAX_RETRY; ++retry) { |
| 986 | |
| 987 | has_pjsua_lock = PJ_FALSE; |
| 988 | |
| 989 | status = PJSUA_TRY_LOCK(); |
| 990 | if (status != PJ_SUCCESS) { |
| 991 | pj_thread_sleep(retry/10); |
| 992 | continue; |
| 993 | } |
| 994 | |
| 995 | has_pjsua_lock = PJ_TRUE; |
| 996 | call = &pjsua_var.calls[call_id]; |
| 997 | |
| 998 | if (call->inv == NULL) { |
| 999 | PJSUA_UNLOCK(); |
| 1000 | PJ_LOG(3,(THIS_FILE, "Invalid call_id %d in %s", call_id, title)); |
| 1001 | return PJSIP_ESESSIONTERMINATED; |
| 1002 | } |
| 1003 | |
| 1004 | status = pjsip_dlg_try_inc_lock(call->inv->dlg); |
| 1005 | if (status != PJ_SUCCESS) { |
| 1006 | PJSUA_UNLOCK(); |
| 1007 | pj_thread_sleep(retry/10); |
| 1008 | continue; |
| 1009 | } |
| 1010 | |
| 1011 | PJSUA_UNLOCK(); |
| 1012 | |
| 1013 | break; |
| 1014 | } |
| 1015 | |
| 1016 | if (status != PJ_SUCCESS) { |
| 1017 | if (has_pjsua_lock == PJ_FALSE) |
| 1018 | PJ_LOG(1,(THIS_FILE, "Timed-out trying to acquire PJSUA mutex " |
| 1019 | "(possibly system has deadlocked) in %s", |
| 1020 | title)); |
| 1021 | else |
| 1022 | PJ_LOG(1,(THIS_FILE, "Timed-out trying to acquire dialog mutex " |
| 1023 | "(possibly system has deadlocked) in %s", |
| 1024 | title)); |
| 1025 | return PJ_ETIMEDOUT; |
| 1026 | } |
| 1027 | |
| 1028 | *p_call = call; |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1029 | *p_dlg = call->inv->dlg; |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1030 | |
| 1031 | return PJ_SUCCESS; |
| 1032 | } |
| 1033 | |
| 1034 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1035 | /* |
| 1036 | * Get the conference port identification associated with the call. |
| 1037 | */ |
| 1038 | PJ_DEF(pjsua_conf_port_id) pjsua_call_get_conf_port(pjsua_call_id call_id) |
| 1039 | { |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1040 | pjsua_call *call; |
| 1041 | pjsua_conf_port_id port_id; |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1042 | pjsip_dialog *dlg; |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1043 | pj_status_t status; |
| 1044 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1045 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1046 | PJ_EINVAL); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1047 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1048 | status = acquire_call("pjsua_call_get_conf_port()", call_id, &call, &dlg); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1049 | if (status != PJ_SUCCESS) |
Benny Prijono | d524e82 | 2006-09-22 12:48:18 +0000 | [diff] [blame] | 1050 | return PJSUA_INVALID_ID; |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1051 | |
| 1052 | port_id = call->conf_slot; |
| 1053 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1054 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1055 | |
| 1056 | return port_id; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1060 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1061 | /* |
| 1062 | * Obtain detail information about the specified call. |
| 1063 | */ |
| 1064 | PJ_DEF(pj_status_t) pjsua_call_get_info( pjsua_call_id call_id, |
| 1065 | pjsua_call_info *info) |
| 1066 | { |
| 1067 | pjsua_call *call; |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1068 | pjsip_dialog *dlg; |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1069 | pj_status_t status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1070 | |
| 1071 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1072 | PJ_EINVAL); |
| 1073 | |
Benny Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 1074 | pj_bzero(info, sizeof(*info)); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1075 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1076 | status = acquire_call("pjsua_call_get_info()", call_id, &call, &dlg); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1077 | if (status != PJ_SUCCESS) { |
| 1078 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1081 | /* id and role */ |
| 1082 | info->id = call_id; |
| 1083 | info->role = call->inv->role; |
Benny Prijono | 9031551 | 2006-09-14 16:05:16 +0000 | [diff] [blame] | 1084 | info->acc_id = call->acc_id; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1085 | |
| 1086 | /* local info */ |
| 1087 | info->local_info.ptr = info->buf_.local_info; |
| 1088 | pj_strncpy(&info->local_info, &call->inv->dlg->local.info_str, |
| 1089 | sizeof(info->buf_.local_info)); |
| 1090 | |
| 1091 | /* local contact */ |
| 1092 | info->local_contact.ptr = info->buf_.local_contact; |
| 1093 | info->local_contact.slen = pjsip_uri_print(PJSIP_URI_IN_CONTACT_HDR, |
| 1094 | call->inv->dlg->local.contact->uri, |
| 1095 | info->local_contact.ptr, |
| 1096 | sizeof(info->buf_.local_contact)); |
| 1097 | |
| 1098 | /* remote info */ |
| 1099 | info->remote_info.ptr = info->buf_.remote_info; |
| 1100 | pj_strncpy(&info->remote_info, &call->inv->dlg->remote.info_str, |
| 1101 | sizeof(info->buf_.remote_info)); |
| 1102 | |
| 1103 | /* remote contact */ |
| 1104 | if (call->inv->dlg->remote.contact) { |
| 1105 | int len; |
| 1106 | info->remote_contact.ptr = info->buf_.remote_contact; |
| 1107 | len = pjsip_uri_print(PJSIP_URI_IN_CONTACT_HDR, |
| 1108 | call->inv->dlg->remote.contact->uri, |
| 1109 | info->remote_contact.ptr, |
| 1110 | sizeof(info->buf_.remote_contact)); |
| 1111 | if (len < 0) len = 0; |
| 1112 | info->remote_contact.slen = len; |
| 1113 | } else { |
| 1114 | info->remote_contact.slen = 0; |
| 1115 | } |
| 1116 | |
| 1117 | /* call id */ |
| 1118 | info->call_id.ptr = info->buf_.call_id; |
| 1119 | pj_strncpy(&info->call_id, &call->inv->dlg->call_id->id, |
| 1120 | sizeof(info->buf_.call_id)); |
| 1121 | |
| 1122 | /* state, state_text */ |
| 1123 | info->state = call->inv->state; |
| 1124 | info->state_text = pj_str((char*)pjsip_inv_state_name(info->state)); |
| 1125 | |
| 1126 | /* If call is disconnected, set the last_status from the cause code */ |
| 1127 | if (call->inv->state >= PJSIP_INV_STATE_DISCONNECTED) { |
| 1128 | /* last_status, last_status_text */ |
| 1129 | info->last_status = call->inv->cause; |
| 1130 | |
| 1131 | info->last_status_text.ptr = info->buf_.last_status_text; |
| 1132 | pj_strncpy(&info->last_status_text, &call->inv->cause_text, |
| 1133 | sizeof(info->buf_.last_status_text)); |
| 1134 | } else { |
| 1135 | /* last_status, last_status_text */ |
| 1136 | info->last_status = call->last_code; |
| 1137 | |
| 1138 | info->last_status_text.ptr = info->buf_.last_status_text; |
| 1139 | pj_strncpy(&info->last_status_text, &call->last_text, |
| 1140 | sizeof(info->buf_.last_status_text)); |
| 1141 | } |
| 1142 | |
| 1143 | /* media status and dir */ |
| 1144 | info->media_status = call->media_st; |
| 1145 | info->media_dir = call->media_dir; |
| 1146 | |
| 1147 | |
| 1148 | /* conference slot number */ |
| 1149 | info->conf_slot = call->conf_slot; |
| 1150 | |
| 1151 | /* calculate duration */ |
| 1152 | if (info->state >= PJSIP_INV_STATE_DISCONNECTED) { |
| 1153 | |
| 1154 | info->total_duration = call->dis_time; |
| 1155 | PJ_TIME_VAL_SUB(info->total_duration, call->start_time); |
| 1156 | |
| 1157 | if (call->conn_time.sec) { |
| 1158 | info->connect_duration = call->dis_time; |
| 1159 | PJ_TIME_VAL_SUB(info->connect_duration, call->conn_time); |
| 1160 | } |
| 1161 | |
| 1162 | } else if (info->state == PJSIP_INV_STATE_CONFIRMED) { |
| 1163 | |
| 1164 | pj_gettimeofday(&info->total_duration); |
| 1165 | PJ_TIME_VAL_SUB(info->total_duration, call->start_time); |
| 1166 | |
| 1167 | pj_gettimeofday(&info->connect_duration); |
| 1168 | PJ_TIME_VAL_SUB(info->connect_duration, call->conn_time); |
| 1169 | |
| 1170 | } else { |
| 1171 | pj_gettimeofday(&info->total_duration); |
| 1172 | PJ_TIME_VAL_SUB(info->total_duration, call->start_time); |
| 1173 | } |
| 1174 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1175 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1176 | |
| 1177 | return PJ_SUCCESS; |
| 1178 | } |
| 1179 | |
| 1180 | |
| 1181 | /* |
| 1182 | * Attach application specific data to the call. |
| 1183 | */ |
| 1184 | PJ_DEF(pj_status_t) pjsua_call_set_user_data( pjsua_call_id call_id, |
| 1185 | void *user_data) |
| 1186 | { |
| 1187 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1188 | PJ_EINVAL); |
| 1189 | pjsua_var.calls[call_id].user_data = user_data; |
| 1190 | |
| 1191 | return PJ_SUCCESS; |
| 1192 | } |
| 1193 | |
| 1194 | |
| 1195 | /* |
| 1196 | * Get user data attached to the call. |
| 1197 | */ |
| 1198 | PJ_DEF(void*) pjsua_call_get_user_data(pjsua_call_id call_id) |
| 1199 | { |
| 1200 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1201 | NULL); |
| 1202 | return pjsua_var.calls[call_id].user_data; |
| 1203 | } |
| 1204 | |
| 1205 | |
| 1206 | /* |
Benny Prijono | 91a6a17 | 2007-10-31 08:59:29 +0000 | [diff] [blame] | 1207 | * Get remote's NAT type. |
| 1208 | */ |
| 1209 | PJ_DEF(pj_status_t) pjsua_call_get_rem_nat_type(pjsua_call_id call_id, |
| 1210 | pj_stun_nat_type *p_type) |
| 1211 | { |
| 1212 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1213 | PJ_EINVAL); |
| 1214 | PJ_ASSERT_RETURN(p_type != NULL, PJ_EINVAL); |
| 1215 | |
| 1216 | *p_type = pjsua_var.calls[call_id].rem_nat_type; |
| 1217 | return PJ_SUCCESS; |
| 1218 | } |
| 1219 | |
| 1220 | |
| 1221 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1222 | * Send response to incoming INVITE request. |
| 1223 | */ |
| 1224 | PJ_DEF(pj_status_t) pjsua_call_answer( pjsua_call_id call_id, |
| 1225 | unsigned code, |
| 1226 | const pj_str_t *reason, |
| 1227 | const pjsua_msg_data *msg_data) |
| 1228 | { |
| 1229 | pjsua_call *call; |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1230 | pjsip_dialog *dlg; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1231 | pjsip_tx_data *tdata; |
| 1232 | pj_status_t status; |
| 1233 | |
| 1234 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1235 | PJ_EINVAL); |
| 1236 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1237 | status = acquire_call("pjsua_call_answer()", call_id, &call, &dlg); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1238 | if (status != PJ_SUCCESS) |
| 1239 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1240 | |
Benny Prijono | 2e507c2 | 2006-06-23 15:04:11 +0000 | [diff] [blame] | 1241 | if (call->res_time.sec == 0) |
| 1242 | pj_gettimeofday(&call->res_time); |
| 1243 | |
Benny Prijono | ed7a5a7 | 2007-01-29 18:36:38 +0000 | [diff] [blame] | 1244 | if (reason && reason->slen == 0) |
| 1245 | reason = NULL; |
| 1246 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1247 | /* Create response message */ |
| 1248 | status = pjsip_inv_answer(call->inv, code, reason, NULL, &tdata); |
| 1249 | if (status != PJ_SUCCESS) { |
| 1250 | pjsua_perror(THIS_FILE, "Error creating response", |
| 1251 | status); |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1252 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1253 | return status; |
| 1254 | } |
| 1255 | |
Benny Prijono | fed7f4c | 2007-03-27 11:01:45 +0000 | [diff] [blame] | 1256 | /* Call might have been disconnected if application is answering with |
| 1257 | * 200/OK and the media failed to start. |
| 1258 | */ |
| 1259 | if (call->inv == NULL) { |
| 1260 | pjsip_dlg_dec_lock(dlg); |
| 1261 | return PJSIP_ESESSIONTERMINATED; |
| 1262 | } |
| 1263 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1264 | /* Add additional headers etc */ |
| 1265 | pjsua_process_msg_data( tdata, msg_data); |
| 1266 | |
| 1267 | /* Send the message */ |
| 1268 | status = pjsip_inv_send_msg(call->inv, tdata); |
| 1269 | if (status != PJ_SUCCESS) |
| 1270 | pjsua_perror(THIS_FILE, "Error sending response", |
| 1271 | status); |
| 1272 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1273 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1274 | |
| 1275 | return status; |
| 1276 | } |
| 1277 | |
| 1278 | |
| 1279 | /* |
| 1280 | * Hangup call by using method that is appropriate according to the |
| 1281 | * call state. |
| 1282 | */ |
| 1283 | PJ_DEF(pj_status_t) pjsua_call_hangup(pjsua_call_id call_id, |
| 1284 | unsigned code, |
| 1285 | const pj_str_t *reason, |
| 1286 | const pjsua_msg_data *msg_data) |
| 1287 | { |
| 1288 | pjsua_call *call; |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1289 | pjsip_dialog *dlg; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1290 | pj_status_t status; |
| 1291 | pjsip_tx_data *tdata; |
| 1292 | |
| 1293 | |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1294 | if (call_id<0 || call_id>=(int)pjsua_var.ua_cfg.max_calls) { |
| 1295 | PJ_LOG(1,(THIS_FILE, "pjsua_call_hangup(): invalid call id %d", |
| 1296 | call_id)); |
| 1297 | } |
| 1298 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1299 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1300 | PJ_EINVAL); |
| 1301 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1302 | status = acquire_call("pjsua_call_hangup()", call_id, &call, &dlg); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1303 | if (status != PJ_SUCCESS) |
| 1304 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1305 | |
| 1306 | if (code==0) { |
| 1307 | if (call->inv->state == PJSIP_INV_STATE_CONFIRMED) |
| 1308 | code = PJSIP_SC_OK; |
| 1309 | else if (call->inv->role == PJSIP_ROLE_UAS) |
| 1310 | code = PJSIP_SC_DECLINE; |
| 1311 | else |
| 1312 | code = PJSIP_SC_REQUEST_TERMINATED; |
| 1313 | } |
| 1314 | |
| 1315 | status = pjsip_inv_end_session(call->inv, code, reason, &tdata); |
| 1316 | if (status != PJ_SUCCESS) { |
| 1317 | pjsua_perror(THIS_FILE, |
| 1318 | "Failed to create end session message", |
| 1319 | status); |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1320 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1321 | return status; |
| 1322 | } |
| 1323 | |
| 1324 | /* pjsip_inv_end_session may return PJ_SUCCESS with NULL |
| 1325 | * as p_tdata when INVITE transaction has not been answered |
| 1326 | * with any provisional responses. |
| 1327 | */ |
| 1328 | if (tdata == NULL) { |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1329 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1330 | return PJ_SUCCESS; |
| 1331 | } |
| 1332 | |
| 1333 | /* Add additional headers etc */ |
| 1334 | pjsua_process_msg_data( tdata, msg_data); |
| 1335 | |
| 1336 | /* Send the message */ |
| 1337 | status = pjsip_inv_send_msg(call->inv, tdata); |
| 1338 | if (status != PJ_SUCCESS) { |
| 1339 | pjsua_perror(THIS_FILE, |
| 1340 | "Failed to send end session message", |
| 1341 | status); |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1342 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1343 | return status; |
| 1344 | } |
| 1345 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1346 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1347 | |
| 1348 | return PJ_SUCCESS; |
| 1349 | } |
| 1350 | |
| 1351 | |
| 1352 | /* |
| 1353 | * Put the specified call on hold. |
| 1354 | */ |
| 1355 | PJ_DEF(pj_status_t) pjsua_call_set_hold(pjsua_call_id call_id, |
| 1356 | const pjsua_msg_data *msg_data) |
| 1357 | { |
| 1358 | pjmedia_sdp_session *sdp; |
| 1359 | pjsua_call *call; |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1360 | pjsip_dialog *dlg; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1361 | pjsip_tx_data *tdata; |
| 1362 | pj_status_t status; |
| 1363 | |
| 1364 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1365 | PJ_EINVAL); |
| 1366 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1367 | status = acquire_call("pjsua_call_set_hold()", call_id, &call, &dlg); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1368 | if (status != PJ_SUCCESS) |
| 1369 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1370 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1371 | |
| 1372 | if (call->inv->state != PJSIP_INV_STATE_CONFIRMED) { |
| 1373 | PJ_LOG(3,(THIS_FILE, "Can not hold call that is not confirmed")); |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1374 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1375 | return PJSIP_ESESSIONSTATE; |
| 1376 | } |
| 1377 | |
| 1378 | status = create_inactive_sdp(call, &sdp); |
| 1379 | if (status != PJ_SUCCESS) { |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1380 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1381 | return status; |
| 1382 | } |
| 1383 | |
Benny Prijono | 7129cc7 | 2007-11-05 05:54:25 +0000 | [diff] [blame] | 1384 | update_sdp_version(call, sdp); |
| 1385 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1386 | /* Create re-INVITE with new offer */ |
| 1387 | status = pjsip_inv_reinvite( call->inv, NULL, sdp, &tdata); |
| 1388 | if (status != PJ_SUCCESS) { |
| 1389 | pjsua_perror(THIS_FILE, "Unable to create re-INVITE", status); |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1390 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1391 | return status; |
| 1392 | } |
| 1393 | |
| 1394 | /* Add additional headers etc */ |
| 1395 | pjsua_process_msg_data( tdata, msg_data); |
| 1396 | |
| 1397 | /* Send the request */ |
| 1398 | status = pjsip_inv_send_msg( call->inv, tdata); |
| 1399 | if (status != PJ_SUCCESS) { |
| 1400 | pjsua_perror(THIS_FILE, "Unable to send re-INVITE", status); |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1401 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1402 | return status; |
| 1403 | } |
| 1404 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1405 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1406 | |
| 1407 | return PJ_SUCCESS; |
| 1408 | } |
| 1409 | |
| 1410 | |
| 1411 | /* |
| 1412 | * Send re-INVITE (to release hold). |
| 1413 | */ |
| 1414 | PJ_DEF(pj_status_t) pjsua_call_reinvite( pjsua_call_id call_id, |
| 1415 | pj_bool_t unhold, |
| 1416 | const pjsua_msg_data *msg_data) |
| 1417 | { |
| 1418 | pjmedia_sdp_session *sdp; |
| 1419 | pjsip_tx_data *tdata; |
| 1420 | pjsua_call *call; |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1421 | pjsip_dialog *dlg; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1422 | pj_status_t status; |
| 1423 | |
| 1424 | |
| 1425 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1426 | PJ_EINVAL); |
| 1427 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1428 | status = acquire_call("pjsua_call_reinvite()", call_id, &call, &dlg); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1429 | if (status != PJ_SUCCESS) |
| 1430 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1431 | |
| 1432 | if (call->inv->state != PJSIP_INV_STATE_CONFIRMED) { |
| 1433 | PJ_LOG(3,(THIS_FILE, "Can not re-INVITE call that is not confirmed")); |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1434 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1435 | return PJSIP_ESESSIONSTATE; |
| 1436 | } |
| 1437 | |
Benny Prijono | db844a4 | 2008-02-02 17:07:18 +0000 | [diff] [blame] | 1438 | /* Update call secure level */ |
| 1439 | call->secure_level = call_get_secure_level(call); |
| 1440 | |
Benny Prijono | 667952e | 2007-04-02 19:27:54 +0000 | [diff] [blame] | 1441 | /* Init media channel */ |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 1442 | status = pjsua_media_channel_init(call->index, PJSIP_ROLE_UAC, |
Benny Prijono | db844a4 | 2008-02-02 17:07:18 +0000 | [diff] [blame] | 1443 | call->secure_level, NULL); |
Benny Prijono | 667952e | 2007-04-02 19:27:54 +0000 | [diff] [blame] | 1444 | if (status != PJ_SUCCESS) { |
| 1445 | pjsua_perror(THIS_FILE, "Error initializing media channel", status); |
| 1446 | pjsip_dlg_dec_lock(dlg); |
| 1447 | return PJSIP_ESESSIONSTATE; |
| 1448 | } |
| 1449 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1450 | /* Create SDP */ |
Benny Prijono | 00cae61 | 2006-07-31 15:19:36 +0000 | [diff] [blame] | 1451 | PJ_UNUSED_ARG(unhold); |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 1452 | PJ_TODO(create_active_inactive_sdp_based_on_unhold_arg); |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 1453 | status = pjsua_media_channel_create_sdp(call->index, call->inv->pool, |
Benny Prijono | 25b2ea1 | 2008-01-24 19:20:54 +0000 | [diff] [blame] | 1454 | NULL, &sdp, NULL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1455 | if (status != PJ_SUCCESS) { |
| 1456 | pjsua_perror(THIS_FILE, "Unable to get SDP from media endpoint", |
| 1457 | status); |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1458 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1459 | return status; |
| 1460 | } |
| 1461 | |
Benny Prijono | 7129cc7 | 2007-11-05 05:54:25 +0000 | [diff] [blame] | 1462 | update_sdp_version(call, sdp); |
| 1463 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1464 | /* Create re-INVITE with new offer */ |
| 1465 | status = pjsip_inv_reinvite( call->inv, NULL, sdp, &tdata); |
| 1466 | if (status != PJ_SUCCESS) { |
| 1467 | pjsua_perror(THIS_FILE, "Unable to create re-INVITE", status); |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1468 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1469 | return status; |
| 1470 | } |
| 1471 | |
| 1472 | /* Add additional headers etc */ |
| 1473 | pjsua_process_msg_data( tdata, msg_data); |
| 1474 | |
| 1475 | /* Send the request */ |
| 1476 | status = pjsip_inv_send_msg( call->inv, tdata); |
| 1477 | if (status != PJ_SUCCESS) { |
| 1478 | pjsua_perror(THIS_FILE, "Unable to send re-INVITE", status); |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1479 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1480 | return status; |
| 1481 | } |
| 1482 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1483 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1484 | |
| 1485 | return PJ_SUCCESS; |
| 1486 | } |
| 1487 | |
| 1488 | |
| 1489 | /* |
Benny Prijono | c08682e | 2007-10-04 06:17:58 +0000 | [diff] [blame] | 1490 | * Send UPDATE request. |
| 1491 | */ |
| 1492 | PJ_DEF(pj_status_t) pjsua_call_update( pjsua_call_id call_id, |
| 1493 | unsigned options, |
| 1494 | const pjsua_msg_data *msg_data) |
| 1495 | { |
| 1496 | pjmedia_sdp_session *sdp; |
| 1497 | pjsip_tx_data *tdata; |
| 1498 | pjsua_call *call; |
| 1499 | pjsip_dialog *dlg; |
| 1500 | pj_status_t status; |
| 1501 | |
| 1502 | PJ_UNUSED_ARG(options); |
| 1503 | |
| 1504 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1505 | PJ_EINVAL); |
| 1506 | |
| 1507 | status = acquire_call("pjsua_call_update()", call_id, &call, &dlg); |
| 1508 | if (status != PJ_SUCCESS) |
| 1509 | return status; |
| 1510 | |
Benny Prijono | db844a4 | 2008-02-02 17:07:18 +0000 | [diff] [blame] | 1511 | /* Update call's secure level */ |
| 1512 | call->secure_level = call_get_secure_level(call); |
| 1513 | |
Benny Prijono | c08682e | 2007-10-04 06:17:58 +0000 | [diff] [blame] | 1514 | /* Init media channel */ |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 1515 | status = pjsua_media_channel_init(call->index, PJSIP_ROLE_UAC, |
Benny Prijono | db844a4 | 2008-02-02 17:07:18 +0000 | [diff] [blame] | 1516 | call->secure_level, NULL); |
Benny Prijono | c08682e | 2007-10-04 06:17:58 +0000 | [diff] [blame] | 1517 | if (status != PJ_SUCCESS) { |
| 1518 | pjsua_perror(THIS_FILE, "Error initializing media channel", status); |
| 1519 | pjsip_dlg_dec_lock(dlg); |
| 1520 | return PJSIP_ESESSIONSTATE; |
| 1521 | } |
| 1522 | |
| 1523 | /* Create SDP */ |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 1524 | status = pjsua_media_channel_create_sdp(call->index, call->inv->pool, |
Benny Prijono | 25b2ea1 | 2008-01-24 19:20:54 +0000 | [diff] [blame] | 1525 | NULL, &sdp, NULL); |
Benny Prijono | c08682e | 2007-10-04 06:17:58 +0000 | [diff] [blame] | 1526 | if (status != PJ_SUCCESS) { |
| 1527 | pjsua_perror(THIS_FILE, "Unable to get SDP from media endpoint", |
| 1528 | status); |
| 1529 | pjsip_dlg_dec_lock(dlg); |
| 1530 | return status; |
| 1531 | } |
| 1532 | |
| 1533 | /* Create re-INVITE with new offer */ |
| 1534 | status = pjsip_inv_update(call->inv, NULL, sdp, &tdata); |
| 1535 | if (status != PJ_SUCCESS) { |
| 1536 | pjsua_perror(THIS_FILE, "Unable to create UPDATE request", status); |
| 1537 | pjsip_dlg_dec_lock(dlg); |
| 1538 | return status; |
| 1539 | } |
| 1540 | |
| 1541 | /* Add additional headers etc */ |
| 1542 | pjsua_process_msg_data( tdata, msg_data); |
| 1543 | |
| 1544 | /* Send the request */ |
| 1545 | status = pjsip_inv_send_msg( call->inv, tdata); |
| 1546 | if (status != PJ_SUCCESS) { |
| 1547 | pjsua_perror(THIS_FILE, "Unable to send UPDAT Erequest", status); |
| 1548 | pjsip_dlg_dec_lock(dlg); |
| 1549 | return status; |
| 1550 | } |
| 1551 | |
| 1552 | pjsip_dlg_dec_lock(dlg); |
| 1553 | |
| 1554 | return PJ_SUCCESS; |
| 1555 | } |
| 1556 | |
| 1557 | |
| 1558 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1559 | * Initiate call transfer to the specified address. |
| 1560 | */ |
| 1561 | PJ_DEF(pj_status_t) pjsua_call_xfer( pjsua_call_id call_id, |
| 1562 | const pj_str_t *dest, |
| 1563 | const pjsua_msg_data *msg_data) |
| 1564 | { |
| 1565 | pjsip_evsub *sub; |
| 1566 | pjsip_tx_data *tdata; |
| 1567 | pjsua_call *call; |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1568 | pjsip_dialog *dlg; |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 1569 | pjsip_generic_string_hdr *gs_hdr; |
| 1570 | const pj_str_t str_ref_by = { "Referred-By", 11 }; |
Benny Prijono | d524e82 | 2006-09-22 12:48:18 +0000 | [diff] [blame] | 1571 | struct pjsip_evsub_user xfer_cb; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1572 | pj_status_t status; |
| 1573 | |
| 1574 | |
| 1575 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1576 | PJ_EINVAL); |
| 1577 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1578 | status = acquire_call("pjsua_call_xfer()", call_id, &call, &dlg); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1579 | if (status != PJ_SUCCESS) |
| 1580 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1581 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1582 | |
Benny Prijono | d524e82 | 2006-09-22 12:48:18 +0000 | [diff] [blame] | 1583 | /* Create xfer client subscription. */ |
| 1584 | pj_bzero(&xfer_cb, sizeof(xfer_cb)); |
Benny Prijono | 4ddad2c | 2006-10-18 17:16:34 +0000 | [diff] [blame] | 1585 | xfer_cb.on_evsub_state = &xfer_client_on_evsub_state; |
Benny Prijono | d524e82 | 2006-09-22 12:48:18 +0000 | [diff] [blame] | 1586 | |
| 1587 | status = pjsip_xfer_create_uac(call->inv->dlg, &xfer_cb, &sub); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1588 | if (status != PJ_SUCCESS) { |
| 1589 | pjsua_perror(THIS_FILE, "Unable to create xfer", status); |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1590 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1591 | return status; |
| 1592 | } |
| 1593 | |
Benny Prijono | 4ddad2c | 2006-10-18 17:16:34 +0000 | [diff] [blame] | 1594 | /* Associate this call with the client subscription */ |
| 1595 | pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, call); |
| 1596 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1597 | /* |
| 1598 | * Create REFER request. |
| 1599 | */ |
| 1600 | status = pjsip_xfer_initiate(sub, dest, &tdata); |
| 1601 | if (status != PJ_SUCCESS) { |
| 1602 | pjsua_perror(THIS_FILE, "Unable to create REFER request", status); |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1603 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1604 | return status; |
| 1605 | } |
| 1606 | |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 1607 | /* Add Referred-By header */ |
| 1608 | gs_hdr = pjsip_generic_string_hdr_create(tdata->pool, &str_ref_by, |
| 1609 | &dlg->local.info_str); |
| 1610 | pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)gs_hdr); |
| 1611 | |
| 1612 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1613 | /* Add additional headers etc */ |
| 1614 | pjsua_process_msg_data( tdata, msg_data); |
| 1615 | |
| 1616 | /* Send. */ |
| 1617 | status = pjsip_xfer_send_request(sub, tdata); |
| 1618 | if (status != PJ_SUCCESS) { |
| 1619 | pjsua_perror(THIS_FILE, "Unable to send REFER request", status); |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1620 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1621 | return status; |
| 1622 | } |
| 1623 | |
| 1624 | /* For simplicity (that's what this program is intended to be!), |
| 1625 | * leave the original invite session as it is. More advanced application |
| 1626 | * may want to hold the INVITE, or terminate the invite, or whatever. |
| 1627 | */ |
| 1628 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1629 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1630 | |
| 1631 | return PJ_SUCCESS; |
| 1632 | |
| 1633 | } |
| 1634 | |
| 1635 | |
| 1636 | /* |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 1637 | * Initiate attended call transfer to the specified address. |
| 1638 | */ |
| 1639 | PJ_DEF(pj_status_t) pjsua_call_xfer_replaces( pjsua_call_id call_id, |
| 1640 | pjsua_call_id dest_call_id, |
| 1641 | unsigned options, |
| 1642 | const pjsua_msg_data *msg_data) |
| 1643 | { |
| 1644 | pjsua_call *dest_call; |
| 1645 | pjsip_dialog *dest_dlg; |
Benny Prijono | 38fb3ea | 2008-01-02 08:27:03 +0000 | [diff] [blame] | 1646 | char str_dest_buf[PJSIP_MAX_URL_SIZE*2]; |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 1647 | pj_str_t str_dest; |
| 1648 | int len; |
| 1649 | pjsip_uri *uri; |
| 1650 | pj_status_t status; |
| 1651 | |
| 1652 | |
| 1653 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1654 | PJ_EINVAL); |
| 1655 | PJ_ASSERT_RETURN(dest_call_id>=0 && |
| 1656 | dest_call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1657 | PJ_EINVAL); |
| 1658 | |
| 1659 | status = acquire_call("pjsua_call_xfer_replaces()", dest_call_id, |
| 1660 | &dest_call, &dest_dlg); |
| 1661 | if (status != PJ_SUCCESS) |
| 1662 | return status; |
| 1663 | |
| 1664 | /* |
| 1665 | * Create REFER destination URI with Replaces field. |
| 1666 | */ |
| 1667 | |
| 1668 | /* Make sure we have sufficient buffer's length */ |
| 1669 | PJ_ASSERT_RETURN( dest_dlg->remote.info_str.slen + |
| 1670 | dest_dlg->call_id->id.slen + |
| 1671 | dest_dlg->remote.info->tag.slen + |
| 1672 | dest_dlg->local.info->tag.slen + 32 |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1673 | < (long)sizeof(str_dest_buf), PJSIP_EURITOOLONG); |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 1674 | |
| 1675 | /* Print URI */ |
| 1676 | str_dest_buf[0] = '<'; |
| 1677 | str_dest.slen = 1; |
| 1678 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1679 | uri = (pjsip_uri*) pjsip_uri_get_uri(dest_dlg->remote.info->uri); |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 1680 | len = pjsip_uri_print(PJSIP_URI_IN_REQ_URI, uri, |
| 1681 | str_dest_buf+1, sizeof(str_dest_buf)-1); |
| 1682 | if (len < 0) |
| 1683 | return PJSIP_EURITOOLONG; |
| 1684 | |
| 1685 | str_dest.slen += len; |
| 1686 | |
| 1687 | |
| 1688 | /* Build the URI */ |
| 1689 | len = pj_ansi_snprintf(str_dest_buf + str_dest.slen, |
| 1690 | sizeof(str_dest_buf) - str_dest.slen, |
| 1691 | "?%s" |
| 1692 | "Replaces=%.*s" |
| 1693 | "%%3Bto-tag%%3D%.*s" |
| 1694 | "%%3Bfrom-tag%%3D%.*s>", |
| 1695 | ((options&PJSUA_XFER_NO_REQUIRE_REPLACES) ? |
| 1696 | "" : "Require=replaces&"), |
| 1697 | (int)dest_dlg->call_id->id.slen, |
| 1698 | dest_dlg->call_id->id.ptr, |
| 1699 | (int)dest_dlg->remote.info->tag.slen, |
| 1700 | dest_dlg->remote.info->tag.ptr, |
| 1701 | (int)dest_dlg->local.info->tag.slen, |
| 1702 | dest_dlg->local.info->tag.ptr); |
| 1703 | |
| 1704 | PJ_ASSERT_RETURN(len > 0 && len <= (int)sizeof(str_dest_buf)-str_dest.slen, |
| 1705 | PJSIP_EURITOOLONG); |
| 1706 | |
| 1707 | str_dest.ptr = str_dest_buf; |
| 1708 | str_dest.slen += len; |
| 1709 | |
| 1710 | pjsip_dlg_dec_lock(dest_dlg); |
| 1711 | |
| 1712 | return pjsua_call_xfer(call_id, &str_dest, msg_data); |
| 1713 | } |
| 1714 | |
| 1715 | |
| 1716 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1717 | * Send DTMF digits to remote using RFC 2833 payload formats. |
| 1718 | */ |
| 1719 | PJ_DEF(pj_status_t) pjsua_call_dial_dtmf( pjsua_call_id call_id, |
| 1720 | const pj_str_t *digits) |
| 1721 | { |
| 1722 | pjsua_call *call; |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1723 | pjsip_dialog *dlg; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1724 | pj_status_t status; |
| 1725 | |
| 1726 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1727 | PJ_EINVAL); |
| 1728 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1729 | status = acquire_call("pjsua_call_dial_dtmf()", call_id, &call, &dlg); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1730 | if (status != PJ_SUCCESS) |
| 1731 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1732 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1733 | if (!call->session) { |
| 1734 | PJ_LOG(3,(THIS_FILE, "Media is not established yet!")); |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1735 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1736 | return PJ_EINVALIDOP; |
| 1737 | } |
| 1738 | |
| 1739 | status = pjmedia_session_dial_dtmf( call->session, 0, digits); |
| 1740 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1741 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1742 | |
| 1743 | return status; |
| 1744 | } |
| 1745 | |
| 1746 | |
| 1747 | /** |
| 1748 | * Send instant messaging inside INVITE session. |
| 1749 | */ |
| 1750 | PJ_DEF(pj_status_t) pjsua_call_send_im( pjsua_call_id call_id, |
| 1751 | const pj_str_t *mime_type, |
| 1752 | const pj_str_t *content, |
| 1753 | const pjsua_msg_data *msg_data, |
| 1754 | void *user_data) |
| 1755 | { |
| 1756 | pjsua_call *call; |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1757 | pjsip_dialog *dlg; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1758 | const pj_str_t mime_text_plain = pj_str("text/plain"); |
| 1759 | pjsip_media_type ctype; |
| 1760 | pjsua_im_data *im_data; |
| 1761 | pjsip_tx_data *tdata; |
| 1762 | pj_status_t status; |
| 1763 | |
| 1764 | |
| 1765 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1766 | PJ_EINVAL); |
| 1767 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1768 | status = acquire_call("pjsua_call_send_im()", call_id, &call, &dlg); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1769 | if (status != PJ_SUCCESS) |
| 1770 | return status; |
| 1771 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1772 | /* Set default media type if none is specified */ |
| 1773 | if (mime_type == NULL) { |
| 1774 | mime_type = &mime_text_plain; |
| 1775 | } |
| 1776 | |
| 1777 | /* Create request message. */ |
| 1778 | status = pjsip_dlg_create_request( call->inv->dlg, &pjsip_message_method, |
| 1779 | -1, &tdata); |
| 1780 | if (status != PJ_SUCCESS) { |
| 1781 | pjsua_perror(THIS_FILE, "Unable to create MESSAGE request", status); |
| 1782 | goto on_return; |
| 1783 | } |
| 1784 | |
| 1785 | /* Add accept header. */ |
| 1786 | pjsip_msg_add_hdr( tdata->msg, |
| 1787 | (pjsip_hdr*)pjsua_im_create_accept(tdata->pool)); |
| 1788 | |
| 1789 | /* Parse MIME type */ |
| 1790 | pjsua_parse_media_type(tdata->pool, mime_type, &ctype); |
| 1791 | |
| 1792 | /* Create "text/plain" message body. */ |
| 1793 | tdata->msg->body = pjsip_msg_body_create( tdata->pool, &ctype.type, |
| 1794 | &ctype.subtype, content); |
| 1795 | if (tdata->msg->body == NULL) { |
| 1796 | pjsua_perror(THIS_FILE, "Unable to create msg body", PJ_ENOMEM); |
| 1797 | pjsip_tx_data_dec_ref(tdata); |
| 1798 | goto on_return; |
| 1799 | } |
| 1800 | |
| 1801 | /* Add additional headers etc */ |
| 1802 | pjsua_process_msg_data( tdata, msg_data); |
| 1803 | |
| 1804 | /* Create IM data and attach to the request. */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1805 | im_data = PJ_POOL_ZALLOC_T(tdata->pool, pjsua_im_data); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1806 | im_data->acc_id = call->acc_id; |
| 1807 | im_data->call_id = call_id; |
| 1808 | im_data->to = call->inv->dlg->remote.info_str; |
| 1809 | pj_strdup_with_null(tdata->pool, &im_data->body, content); |
| 1810 | im_data->user_data = user_data; |
| 1811 | |
| 1812 | |
| 1813 | /* Send the request. */ |
| 1814 | status = pjsip_dlg_send_request( call->inv->dlg, tdata, |
| 1815 | pjsua_var.mod.id, im_data); |
| 1816 | if (status != PJ_SUCCESS) { |
| 1817 | pjsua_perror(THIS_FILE, "Unable to send MESSAGE request", status); |
| 1818 | goto on_return; |
| 1819 | } |
| 1820 | |
| 1821 | on_return: |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1822 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1823 | return status; |
| 1824 | } |
| 1825 | |
| 1826 | |
| 1827 | /* |
| 1828 | * Send IM typing indication inside INVITE session. |
| 1829 | */ |
| 1830 | PJ_DEF(pj_status_t) pjsua_call_send_typing_ind( pjsua_call_id call_id, |
| 1831 | pj_bool_t is_typing, |
| 1832 | const pjsua_msg_data*msg_data) |
| 1833 | { |
| 1834 | pjsua_call *call; |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1835 | pjsip_dialog *dlg; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1836 | pjsip_tx_data *tdata; |
| 1837 | pj_status_t status; |
| 1838 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1839 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1840 | PJ_EINVAL); |
| 1841 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1842 | status = acquire_call("pjsua_call_send_typing_ind", call_id, &call, &dlg); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1843 | if (status != PJ_SUCCESS) |
| 1844 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1845 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1846 | /* Create request message. */ |
| 1847 | status = pjsip_dlg_create_request( call->inv->dlg, &pjsip_message_method, |
| 1848 | -1, &tdata); |
| 1849 | if (status != PJ_SUCCESS) { |
| 1850 | pjsua_perror(THIS_FILE, "Unable to create MESSAGE request", status); |
| 1851 | goto on_return; |
| 1852 | } |
| 1853 | |
| 1854 | /* Create "application/im-iscomposing+xml" msg body. */ |
| 1855 | tdata->msg->body = pjsip_iscomposing_create_body(tdata->pool, is_typing, |
| 1856 | NULL, NULL, -1); |
| 1857 | |
| 1858 | /* Add additional headers etc */ |
| 1859 | pjsua_process_msg_data( tdata, msg_data); |
| 1860 | |
| 1861 | /* Send the request. */ |
| 1862 | status = pjsip_dlg_send_request( call->inv->dlg, tdata, -1, NULL); |
| 1863 | if (status != PJ_SUCCESS) { |
| 1864 | pjsua_perror(THIS_FILE, "Unable to send MESSAGE request", status); |
| 1865 | goto on_return; |
| 1866 | } |
| 1867 | |
| 1868 | on_return: |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1869 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1870 | return status; |
| 1871 | } |
| 1872 | |
| 1873 | |
| 1874 | /* |
Benny Prijono | feb69f4 | 2007-10-05 09:12:26 +0000 | [diff] [blame] | 1875 | * Send arbitrary request. |
| 1876 | */ |
| 1877 | PJ_DEF(pj_status_t) pjsua_call_send_request(pjsua_call_id call_id, |
| 1878 | const pj_str_t *method_str, |
| 1879 | const pjsua_msg_data *msg_data) |
| 1880 | { |
| 1881 | pjsua_call *call; |
| 1882 | pjsip_dialog *dlg; |
| 1883 | pjsip_method method; |
| 1884 | pjsip_tx_data *tdata; |
| 1885 | pj_status_t status; |
| 1886 | |
| 1887 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1888 | PJ_EINVAL); |
| 1889 | |
| 1890 | status = acquire_call("pjsua_call_send_request", call_id, &call, &dlg); |
| 1891 | if (status != PJ_SUCCESS) |
| 1892 | return status; |
| 1893 | |
| 1894 | /* Init method */ |
| 1895 | pjsip_method_init_np(&method, (pj_str_t*)method_str); |
| 1896 | |
| 1897 | /* Create request message. */ |
| 1898 | status = pjsip_dlg_create_request( call->inv->dlg, &method, -1, &tdata); |
| 1899 | if (status != PJ_SUCCESS) { |
| 1900 | pjsua_perror(THIS_FILE, "Unable to create request", status); |
| 1901 | goto on_return; |
| 1902 | } |
| 1903 | |
| 1904 | /* Add additional headers etc */ |
| 1905 | pjsua_process_msg_data( tdata, msg_data); |
| 1906 | |
| 1907 | /* Send the request. */ |
| 1908 | status = pjsip_dlg_send_request( call->inv->dlg, tdata, -1, NULL); |
| 1909 | if (status != PJ_SUCCESS) { |
| 1910 | pjsua_perror(THIS_FILE, "Unable to send request", status); |
| 1911 | goto on_return; |
| 1912 | } |
| 1913 | |
| 1914 | on_return: |
| 1915 | pjsip_dlg_dec_lock(dlg); |
| 1916 | return status; |
| 1917 | } |
| 1918 | |
| 1919 | |
| 1920 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1921 | * Terminate all calls. |
| 1922 | */ |
| 1923 | PJ_DEF(void) pjsua_call_hangup_all(void) |
| 1924 | { |
| 1925 | unsigned i; |
| 1926 | |
| 1927 | PJSUA_LOCK(); |
| 1928 | |
| 1929 | for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) { |
| 1930 | if (pjsua_var.calls[i].inv) |
| 1931 | pjsua_call_hangup(i, 0, NULL, NULL); |
| 1932 | } |
| 1933 | |
| 1934 | PJSUA_UNLOCK(); |
| 1935 | } |
| 1936 | |
| 1937 | |
Benny Prijono | 627cbb4 | 2007-09-25 20:48:49 +0000 | [diff] [blame] | 1938 | const char *good_number(char *buf, pj_int32_t val) |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1939 | { |
| 1940 | if (val < 1000) { |
| 1941 | pj_ansi_sprintf(buf, "%d", val); |
| 1942 | } else if (val < 1000000) { |
| 1943 | pj_ansi_sprintf(buf, "%d.%dK", |
| 1944 | val / 1000, |
| 1945 | (val % 1000) / 100); |
| 1946 | } else { |
| 1947 | pj_ansi_sprintf(buf, "%d.%02dM", |
| 1948 | val / 1000000, |
| 1949 | (val % 1000000) / 10000); |
| 1950 | } |
| 1951 | |
| 1952 | return buf; |
| 1953 | } |
| 1954 | |
| 1955 | |
| 1956 | /* Dump media session */ |
| 1957 | static void dump_media_session(const char *indent, |
| 1958 | char *buf, unsigned maxlen, |
| 1959 | pjmedia_session *session) |
| 1960 | { |
| 1961 | unsigned i; |
| 1962 | char *p = buf, *end = buf+maxlen; |
| 1963 | int len; |
| 1964 | pjmedia_session_info info; |
| 1965 | |
| 1966 | pjmedia_session_get_info(session, &info); |
| 1967 | |
| 1968 | for (i=0; i<info.stream_cnt; ++i) { |
| 1969 | pjmedia_rtcp_stat stat; |
Benny Prijono | 5186eae | 2007-12-03 14:38:25 +0000 | [diff] [blame] | 1970 | char rem_addr_buf[80]; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1971 | const char *rem_addr; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1972 | const char *dir; |
Benny Prijono | e85bc41 | 2006-07-29 20:29:24 +0000 | [diff] [blame] | 1973 | char last_update[64]; |
Benny Prijono | 427af7d | 2007-05-11 10:36:40 +0000 | [diff] [blame] | 1974 | char packets[32], bytes[32], ipbytes[32], avg_bps[32], avg_ipbps[32]; |
Benny Prijono | 80019eb | 2006-08-07 13:22:23 +0000 | [diff] [blame] | 1975 | pj_time_val media_duration, now; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1976 | |
| 1977 | pjmedia_session_get_stream_stat(session, i, &stat); |
Benny Prijono | 5186eae | 2007-12-03 14:38:25 +0000 | [diff] [blame] | 1978 | rem_addr = pj_sockaddr_print(&info.stream_info[i].rem_addr, |
| 1979 | rem_addr_buf, sizeof(rem_addr_buf), 3); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1980 | |
| 1981 | if (info.stream_info[i].dir == PJMEDIA_DIR_ENCODING) |
| 1982 | dir = "sendonly"; |
| 1983 | else if (info.stream_info[i].dir == PJMEDIA_DIR_DECODING) |
| 1984 | dir = "recvonly"; |
| 1985 | else if (info.stream_info[i].dir == PJMEDIA_DIR_ENCODING_DECODING) |
| 1986 | dir = "sendrecv"; |
| 1987 | else |
| 1988 | dir = "inactive"; |
| 1989 | |
| 1990 | |
| 1991 | len = pj_ansi_snprintf(buf, end-p, |
Benny Prijono | 5186eae | 2007-12-03 14:38:25 +0000 | [diff] [blame] | 1992 | "%s #%d %.*s @%dKHz, %s, peer=%s", |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1993 | indent, i, |
Benny Prijono | 172cd73 | 2006-06-14 20:22:31 +0000 | [diff] [blame] | 1994 | (int)info.stream_info[i].fmt.encoding_name.slen, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1995 | info.stream_info[i].fmt.encoding_name.ptr, |
| 1996 | info.stream_info[i].fmt.clock_rate / 1000, |
| 1997 | dir, |
Benny Prijono | 5186eae | 2007-12-03 14:38:25 +0000 | [diff] [blame] | 1998 | rem_addr); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1999 | if (len < 1 || len > end-p) { |
| 2000 | *p = '\0'; |
| 2001 | return; |
| 2002 | } |
| 2003 | |
| 2004 | p += len; |
| 2005 | *p++ = '\n'; |
| 2006 | *p = '\0'; |
| 2007 | |
| 2008 | if (stat.rx.update_cnt == 0) |
| 2009 | strcpy(last_update, "never"); |
| 2010 | else { |
| 2011 | pj_gettimeofday(&now); |
| 2012 | PJ_TIME_VAL_SUB(now, stat.rx.update); |
| 2013 | sprintf(last_update, "%02ldh:%02ldm:%02ld.%03lds ago", |
| 2014 | now.sec / 3600, |
| 2015 | (now.sec % 3600) / 60, |
| 2016 | now.sec % 60, |
| 2017 | now.msec); |
| 2018 | } |
| 2019 | |
Benny Prijono | 80019eb | 2006-08-07 13:22:23 +0000 | [diff] [blame] | 2020 | pj_gettimeofday(&media_duration); |
| 2021 | PJ_TIME_VAL_SUB(media_duration, stat.start); |
| 2022 | if (PJ_TIME_VAL_MSEC(media_duration) == 0) |
| 2023 | media_duration.msec = 1; |
| 2024 | |
Benny Prijono | 1402a4a | 2008-01-08 23:41:22 +0000 | [diff] [blame] | 2025 | /* protect against division by zero */ |
| 2026 | if (stat.rx.pkt == 0) |
| 2027 | stat.rx.pkt = 1; |
| 2028 | if (stat.tx.pkt == 0) |
| 2029 | stat.tx.pkt = 1; |
| 2030 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2031 | len = pj_ansi_snprintf(p, end-p, |
| 2032 | "%s RX pt=%d, stat last update: %s\n" |
Benny Prijono | 427af7d | 2007-05-11 10:36:40 +0000 | [diff] [blame] | 2033 | "%s total %spkt %sB (%sB +IP hdr) @avg=%sbps/%sbps\n" |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2034 | "%s pkt loss=%d (%3.1f%%), dup=%d (%3.1f%%), reorder=%d (%3.1f%%)\n" |
| 2035 | "%s (msec) min avg max last\n" |
| 2036 | "%s loss period: %7.3f %7.3f %7.3f %7.3f\n" |
| 2037 | "%s jitter : %7.3f %7.3f %7.3f %7.3f%s", |
| 2038 | indent, info.stream_info[i].fmt.pt, |
| 2039 | last_update, |
| 2040 | indent, |
| 2041 | good_number(packets, stat.rx.pkt), |
| 2042 | good_number(bytes, stat.rx.bytes), |
Benny Prijono | 427af7d | 2007-05-11 10:36:40 +0000 | [diff] [blame] | 2043 | good_number(ipbytes, stat.rx.bytes + stat.rx.pkt * 40), |
Benny Prijono | e723b92 | 2008-01-09 22:11:24 +0000 | [diff] [blame] | 2044 | good_number(avg_bps, (pj_int32_t)((pj_int64_t)stat.rx.bytes * 8 * 1000 / PJ_TIME_VAL_MSEC(media_duration))), |
| 2045 | good_number(avg_ipbps, (pj_int32_t)(((pj_int64_t)stat.rx.bytes + stat.rx.pkt * 40) * 8 * 1000 / PJ_TIME_VAL_MSEC(media_duration))), |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2046 | indent, |
| 2047 | stat.rx.loss, |
Benny Prijono | f901651 | 2006-06-29 09:41:34 +0000 | [diff] [blame] | 2048 | stat.rx.loss * 100.0 / (stat.rx.pkt + stat.rx.loss), |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2049 | stat.rx.dup, |
Benny Prijono | b12106f | 2006-06-29 14:45:17 +0000 | [diff] [blame] | 2050 | stat.rx.dup * 100.0 / (stat.rx.pkt + stat.rx.loss), |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2051 | stat.rx.reorder, |
Benny Prijono | b12106f | 2006-06-29 14:45:17 +0000 | [diff] [blame] | 2052 | stat.rx.reorder * 100.0 / (stat.rx.pkt + stat.rx.loss), |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2053 | indent, indent, |
| 2054 | stat.rx.loss_period.min / 1000.0, |
| 2055 | stat.rx.loss_period.avg / 1000.0, |
| 2056 | stat.rx.loss_period.max / 1000.0, |
| 2057 | stat.rx.loss_period.last / 1000.0, |
| 2058 | indent, |
| 2059 | stat.rx.jitter.min / 1000.0, |
| 2060 | stat.rx.jitter.avg / 1000.0, |
| 2061 | stat.rx.jitter.max / 1000.0, |
| 2062 | stat.rx.jitter.last / 1000.0, |
| 2063 | "" |
| 2064 | ); |
| 2065 | |
| 2066 | if (len < 1 || len > end-p) { |
| 2067 | *p = '\0'; |
| 2068 | return; |
| 2069 | } |
| 2070 | |
| 2071 | p += len; |
| 2072 | *p++ = '\n'; |
| 2073 | *p = '\0'; |
| 2074 | |
| 2075 | if (stat.tx.update_cnt == 0) |
| 2076 | strcpy(last_update, "never"); |
| 2077 | else { |
| 2078 | pj_gettimeofday(&now); |
| 2079 | PJ_TIME_VAL_SUB(now, stat.tx.update); |
| 2080 | sprintf(last_update, "%02ldh:%02ldm:%02ld.%03lds ago", |
| 2081 | now.sec / 3600, |
| 2082 | (now.sec % 3600) / 60, |
| 2083 | now.sec % 60, |
| 2084 | now.msec); |
| 2085 | } |
| 2086 | |
| 2087 | len = pj_ansi_snprintf(p, end-p, |
| 2088 | "%s TX pt=%d, ptime=%dms, stat last update: %s\n" |
Benny Prijono | 427af7d | 2007-05-11 10:36:40 +0000 | [diff] [blame] | 2089 | "%s total %spkt %sB (%sB +IP hdr) @avg %sbps/%sbps\n" |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2090 | "%s pkt loss=%d (%3.1f%%), dup=%d (%3.1f%%), reorder=%d (%3.1f%%)\n" |
| 2091 | "%s (msec) min avg max last\n" |
| 2092 | "%s loss period: %7.3f %7.3f %7.3f %7.3f\n" |
| 2093 | "%s jitter : %7.3f %7.3f %7.3f %7.3f%s", |
| 2094 | indent, |
| 2095 | info.stream_info[i].tx_pt, |
| 2096 | info.stream_info[i].param->info.frm_ptime * |
| 2097 | info.stream_info[i].param->setting.frm_per_pkt, |
| 2098 | last_update, |
| 2099 | |
| 2100 | indent, |
| 2101 | good_number(packets, stat.tx.pkt), |
| 2102 | good_number(bytes, stat.tx.bytes), |
Benny Prijono | 427af7d | 2007-05-11 10:36:40 +0000 | [diff] [blame] | 2103 | good_number(ipbytes, stat.tx.bytes + stat.tx.pkt * 40), |
Benny Prijono | e723b92 | 2008-01-09 22:11:24 +0000 | [diff] [blame] | 2104 | good_number(avg_bps, (pj_int32_t)((pj_int64_t)stat.tx.bytes * 8 * 1000 / PJ_TIME_VAL_MSEC(media_duration))), |
| 2105 | good_number(avg_ipbps, (pj_int32_t)(((pj_int64_t)stat.tx.bytes + stat.tx.pkt * 40) * 8 * 1000 / PJ_TIME_VAL_MSEC(media_duration))), |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2106 | |
| 2107 | indent, |
| 2108 | stat.tx.loss, |
Benny Prijono | f901651 | 2006-06-29 09:41:34 +0000 | [diff] [blame] | 2109 | stat.tx.loss * 100.0 / (stat.tx.pkt + stat.tx.loss), |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2110 | stat.tx.dup, |
Benny Prijono | b12106f | 2006-06-29 14:45:17 +0000 | [diff] [blame] | 2111 | stat.tx.dup * 100.0 / (stat.tx.pkt + stat.tx.loss), |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2112 | stat.tx.reorder, |
Benny Prijono | b12106f | 2006-06-29 14:45:17 +0000 | [diff] [blame] | 2113 | stat.tx.reorder * 100.0 / (stat.tx.pkt + stat.tx.loss), |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2114 | |
| 2115 | indent, indent, |
| 2116 | stat.tx.loss_period.min / 1000.0, |
| 2117 | stat.tx.loss_period.avg / 1000.0, |
| 2118 | stat.tx.loss_period.max / 1000.0, |
| 2119 | stat.tx.loss_period.last / 1000.0, |
| 2120 | indent, |
| 2121 | stat.tx.jitter.min / 1000.0, |
| 2122 | stat.tx.jitter.avg / 1000.0, |
| 2123 | stat.tx.jitter.max / 1000.0, |
| 2124 | stat.tx.jitter.last / 1000.0, |
| 2125 | "" |
| 2126 | ); |
| 2127 | |
| 2128 | if (len < 1 || len > end-p) { |
| 2129 | *p = '\0'; |
| 2130 | return; |
| 2131 | } |
| 2132 | |
| 2133 | p += len; |
| 2134 | *p++ = '\n'; |
| 2135 | *p = '\0'; |
| 2136 | |
| 2137 | len = pj_ansi_snprintf(p, end-p, |
| 2138 | "%s RTT msec : %7.3f %7.3f %7.3f %7.3f", |
| 2139 | indent, |
| 2140 | stat.rtt.min / 1000.0, |
| 2141 | stat.rtt.avg / 1000.0, |
| 2142 | stat.rtt.max / 1000.0, |
| 2143 | stat.rtt.last / 1000.0 |
| 2144 | ); |
| 2145 | if (len < 1 || len > end-p) { |
| 2146 | *p = '\0'; |
| 2147 | return; |
| 2148 | } |
| 2149 | |
| 2150 | p += len; |
| 2151 | *p++ = '\n'; |
| 2152 | *p = '\0'; |
| 2153 | } |
| 2154 | } |
| 2155 | |
| 2156 | |
| 2157 | /* Print call info */ |
Benny Prijono | 627cbb4 | 2007-09-25 20:48:49 +0000 | [diff] [blame] | 2158 | void print_call(const char *title, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2159 | int call_id, |
| 2160 | char *buf, pj_size_t size) |
| 2161 | { |
| 2162 | int len; |
| 2163 | pjsip_inv_session *inv = pjsua_var.calls[call_id].inv; |
| 2164 | pjsip_dialog *dlg = inv->dlg; |
| 2165 | char userinfo[128]; |
| 2166 | |
| 2167 | /* Dump invite sesion info. */ |
| 2168 | |
| 2169 | len = pjsip_hdr_print_on(dlg->remote.info, userinfo, sizeof(userinfo)); |
| 2170 | if (len < 1) |
| 2171 | pj_ansi_strcpy(userinfo, "<--uri too long-->"); |
| 2172 | else |
| 2173 | userinfo[len] = '\0'; |
| 2174 | |
| 2175 | len = pj_ansi_snprintf(buf, size, "%s[%s] %s", |
| 2176 | title, |
| 2177 | pjsip_inv_state_name(inv->state), |
| 2178 | userinfo); |
| 2179 | if (len < 1 || len >= (int)size) { |
| 2180 | pj_ansi_strcpy(buf, "<--uri too long-->"); |
| 2181 | len = 18; |
| 2182 | } else |
| 2183 | buf[len] = '\0'; |
| 2184 | } |
| 2185 | |
| 2186 | |
| 2187 | /* |
| 2188 | * Dump call and media statistics to string. |
| 2189 | */ |
| 2190 | PJ_DEF(pj_status_t) pjsua_call_dump( pjsua_call_id call_id, |
| 2191 | pj_bool_t with_media, |
| 2192 | char *buffer, |
| 2193 | unsigned maxlen, |
| 2194 | const char *indent) |
| 2195 | { |
| 2196 | pjsua_call *call; |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 2197 | pjsip_dialog *dlg; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2198 | pj_time_val duration, res_delay, con_delay; |
| 2199 | char tmp[128]; |
| 2200 | char *p, *end; |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 2201 | pj_status_t status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2202 | int len; |
| 2203 | |
| 2204 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 2205 | PJ_EINVAL); |
| 2206 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 2207 | status = acquire_call("pjsua_call_dump()", call_id, &call, &dlg); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 2208 | if (status != PJ_SUCCESS) |
| 2209 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2210 | |
| 2211 | *buffer = '\0'; |
| 2212 | p = buffer; |
| 2213 | end = buffer + maxlen; |
| 2214 | len = 0; |
| 2215 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2216 | print_call(indent, call_id, tmp, sizeof(tmp)); |
| 2217 | |
| 2218 | len = pj_ansi_strlen(tmp); |
| 2219 | pj_ansi_strcpy(buffer, tmp); |
| 2220 | |
| 2221 | p += len; |
| 2222 | *p++ = '\r'; |
| 2223 | *p++ = '\n'; |
| 2224 | |
| 2225 | /* Calculate call duration */ |
Benny Prijono | 4be63b5 | 2006-11-25 14:50:25 +0000 | [diff] [blame] | 2226 | if (call->conn_time.sec != 0) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2227 | pj_gettimeofday(&duration); |
| 2228 | PJ_TIME_VAL_SUB(duration, call->conn_time); |
| 2229 | con_delay = call->conn_time; |
| 2230 | PJ_TIME_VAL_SUB(con_delay, call->start_time); |
| 2231 | } else { |
| 2232 | duration.sec = duration.msec = 0; |
| 2233 | con_delay.sec = con_delay.msec = 0; |
| 2234 | } |
| 2235 | |
| 2236 | /* Calculate first response delay */ |
Benny Prijono | 4be63b5 | 2006-11-25 14:50:25 +0000 | [diff] [blame] | 2237 | if (call->res_time.sec != 0) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2238 | res_delay = call->res_time; |
| 2239 | PJ_TIME_VAL_SUB(res_delay, call->start_time); |
| 2240 | } else { |
| 2241 | res_delay.sec = res_delay.msec = 0; |
| 2242 | } |
| 2243 | |
| 2244 | /* Print duration */ |
| 2245 | len = pj_ansi_snprintf(p, end-p, |
| 2246 | "%s Call time: %02dh:%02dm:%02ds, " |
| 2247 | "1st res in %d ms, conn in %dms", |
| 2248 | indent, |
Benny Prijono | 172cd73 | 2006-06-14 20:22:31 +0000 | [diff] [blame] | 2249 | (int)(duration.sec / 3600), |
| 2250 | (int)((duration.sec % 3600)/60), |
| 2251 | (int)(duration.sec % 60), |
| 2252 | (int)PJ_TIME_VAL_MSEC(res_delay), |
| 2253 | (int)PJ_TIME_VAL_MSEC(con_delay)); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2254 | |
| 2255 | if (len > 0 && len < end-p) { |
| 2256 | p += len; |
| 2257 | *p++ = '\n'; |
| 2258 | *p = '\0'; |
| 2259 | } |
| 2260 | |
| 2261 | /* Dump session statistics */ |
| 2262 | if (with_media && call->session) |
| 2263 | dump_media_session(indent, p, end-p, call->session); |
| 2264 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 2265 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2266 | |
| 2267 | return PJ_SUCCESS; |
| 2268 | } |
| 2269 | |
| 2270 | |
| 2271 | /* |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 2272 | * This callback receives notification from invite session when the |
| 2273 | * session state has changed. |
| 2274 | */ |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 2275 | static void pjsua_call_on_state_changed(pjsip_inv_session *inv, |
| 2276 | pjsip_event *e) |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 2277 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2278 | pjsua_call *call; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2279 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2280 | PJSUA_LOCK(); |
| 2281 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 2282 | call = (pjsua_call*) inv->dlg->mod_data[pjsua_var.mod.id]; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2283 | |
| 2284 | if (!call) { |
| 2285 | PJSUA_UNLOCK(); |
Benny Prijono | e21e784 | 2006-04-09 16:46:05 +0000 | [diff] [blame] | 2286 | return; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2287 | } |
| 2288 | |
Benny Prijono | e21e784 | 2006-04-09 16:46:05 +0000 | [diff] [blame] | 2289 | |
| 2290 | /* Get call times */ |
| 2291 | switch (inv->state) { |
| 2292 | case PJSIP_INV_STATE_EARLY: |
| 2293 | case PJSIP_INV_STATE_CONNECTING: |
| 2294 | if (call->res_time.sec == 0) |
| 2295 | pj_gettimeofday(&call->res_time); |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 2296 | call->last_code = (pjsip_status_code) |
| 2297 | e->body.tsx_state.tsx->status_code; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2298 | pj_strncpy(&call->last_text, |
| 2299 | &e->body.tsx_state.tsx->status_text, |
| 2300 | sizeof(call->last_text_buf_)); |
Benny Prijono | e21e784 | 2006-04-09 16:46:05 +0000 | [diff] [blame] | 2301 | break; |
| 2302 | case PJSIP_INV_STATE_CONFIRMED: |
| 2303 | pj_gettimeofday(&call->conn_time); |
| 2304 | break; |
| 2305 | case PJSIP_INV_STATE_DISCONNECTED: |
| 2306 | pj_gettimeofday(&call->dis_time); |
Benny Prijono | 2e507c2 | 2006-06-23 15:04:11 +0000 | [diff] [blame] | 2307 | if (call->res_time.sec == 0) |
| 2308 | pj_gettimeofday(&call->res_time); |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 2309 | if (e->body.tsx_state.tsx->status_code > call->last_code) { |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 2310 | call->last_code = (pjsip_status_code) |
| 2311 | e->body.tsx_state.tsx->status_code; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2312 | pj_strncpy(&call->last_text, |
| 2313 | &e->body.tsx_state.tsx->status_text, |
| 2314 | sizeof(call->last_text_buf_)); |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 2315 | } |
Benny Prijono | e21e784 | 2006-04-09 16:46:05 +0000 | [diff] [blame] | 2316 | break; |
Benny Prijono | 8befd9f | 2006-05-13 22:46:23 +0000 | [diff] [blame] | 2317 | default: |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 2318 | call->last_code = (pjsip_status_code) |
| 2319 | e->body.tsx_state.tsx->status_code; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2320 | pj_strncpy(&call->last_text, |
| 2321 | &e->body.tsx_state.tsx->status_text, |
| 2322 | sizeof(call->last_text_buf_)); |
Benny Prijono | 8befd9f | 2006-05-13 22:46:23 +0000 | [diff] [blame] | 2323 | break; |
Benny Prijono | e21e784 | 2006-04-09 16:46:05 +0000 | [diff] [blame] | 2324 | } |
| 2325 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2326 | /* If this is an outgoing INVITE that was created because of |
| 2327 | * REFER/transfer, send NOTIFY to transferer. |
| 2328 | */ |
Benny Prijono | e21e784 | 2006-04-09 16:46:05 +0000 | [diff] [blame] | 2329 | if (call->xfer_sub && e->type==PJSIP_EVENT_TSX_STATE) { |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2330 | int st_code = -1; |
| 2331 | pjsip_evsub_state ev_state = PJSIP_EVSUB_STATE_ACTIVE; |
| 2332 | |
| 2333 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 2334 | switch (call->inv->state) { |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2335 | case PJSIP_INV_STATE_NULL: |
| 2336 | case PJSIP_INV_STATE_CALLING: |
| 2337 | /* Do nothing */ |
| 2338 | break; |
| 2339 | |
| 2340 | case PJSIP_INV_STATE_EARLY: |
| 2341 | case PJSIP_INV_STATE_CONNECTING: |
| 2342 | st_code = e->body.tsx_state.tsx->status_code; |
| 2343 | ev_state = PJSIP_EVSUB_STATE_ACTIVE; |
| 2344 | break; |
| 2345 | |
| 2346 | case PJSIP_INV_STATE_CONFIRMED: |
| 2347 | /* When state is confirmed, send the final 200/OK and terminate |
| 2348 | * subscription. |
| 2349 | */ |
| 2350 | st_code = e->body.tsx_state.tsx->status_code; |
| 2351 | ev_state = PJSIP_EVSUB_STATE_TERMINATED; |
| 2352 | break; |
| 2353 | |
| 2354 | case PJSIP_INV_STATE_DISCONNECTED: |
| 2355 | st_code = e->body.tsx_state.tsx->status_code; |
| 2356 | ev_state = PJSIP_EVSUB_STATE_TERMINATED; |
| 2357 | break; |
Benny Prijono | 8befd9f | 2006-05-13 22:46:23 +0000 | [diff] [blame] | 2358 | |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 2359 | case PJSIP_INV_STATE_INCOMING: |
Benny Prijono | 8befd9f | 2006-05-13 22:46:23 +0000 | [diff] [blame] | 2360 | /* Nothing to do. Just to keep gcc from complaining about |
| 2361 | * unused enums. |
| 2362 | */ |
| 2363 | break; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2364 | } |
| 2365 | |
| 2366 | if (st_code != -1) { |
| 2367 | pjsip_tx_data *tdata; |
| 2368 | pj_status_t status; |
| 2369 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 2370 | status = pjsip_xfer_notify( call->xfer_sub, |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2371 | ev_state, st_code, |
| 2372 | NULL, &tdata); |
| 2373 | if (status != PJ_SUCCESS) { |
| 2374 | pjsua_perror(THIS_FILE, "Unable to create NOTIFY", status); |
| 2375 | } else { |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 2376 | status = pjsip_xfer_send_request(call->xfer_sub, tdata); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2377 | if (status != PJ_SUCCESS) { |
| 2378 | pjsua_perror(THIS_FILE, "Unable to send NOTIFY", status); |
| 2379 | } |
| 2380 | } |
| 2381 | } |
| 2382 | } |
| 2383 | |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 2384 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2385 | if (pjsua_var.ua_cfg.cb.on_call_state) |
| 2386 | (*pjsua_var.ua_cfg.cb.on_call_state)(call->index, e); |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 2387 | |
| 2388 | /* call->inv may be NULL now */ |
| 2389 | |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 2390 | /* Destroy media session when invite session is disconnected. */ |
| 2391 | if (inv->state == PJSIP_INV_STATE_DISCONNECTED) { |
Benny Prijono | 632ce71 | 2006-02-09 14:01:40 +0000 | [diff] [blame] | 2392 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 2393 | pj_assert(call != NULL); |
Benny Prijono | 632ce71 | 2006-02-09 14:01:40 +0000 | [diff] [blame] | 2394 | |
Benny Prijono | 275fd68 | 2006-03-22 11:59:11 +0000 | [diff] [blame] | 2395 | if (call) |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 2396 | pjsua_media_channel_deinit(call->index); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 2397 | |
Benny Prijono | 105217f | 2006-03-06 16:25:59 +0000 | [diff] [blame] | 2398 | /* Free call */ |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 2399 | call->inv = NULL; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2400 | --pjsua_var.call_cnt; |
Benny Prijono | 4be63b5 | 2006-11-25 14:50:25 +0000 | [diff] [blame] | 2401 | |
| 2402 | /* Reset call */ |
| 2403 | reset_call(call->index); |
| 2404 | |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 2405 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2406 | |
| 2407 | PJSUA_UNLOCK(); |
| 2408 | } |
| 2409 | |
| 2410 | /* |
| 2411 | * This callback is called by invite session framework when UAC session |
| 2412 | * has forked. |
| 2413 | */ |
| 2414 | static void pjsua_call_on_forked( pjsip_inv_session *inv, |
| 2415 | pjsip_event *e) |
| 2416 | { |
| 2417 | PJ_UNUSED_ARG(inv); |
| 2418 | PJ_UNUSED_ARG(e); |
| 2419 | |
| 2420 | PJ_TODO(HANDLE_FORKED_DIALOG); |
| 2421 | } |
| 2422 | |
| 2423 | |
| 2424 | /* |
Benny Prijono | a38ada0 | 2006-07-02 14:22:35 +0000 | [diff] [blame] | 2425 | * Disconnect call upon error. |
| 2426 | */ |
| 2427 | static void call_disconnect( pjsip_inv_session *inv, |
| 2428 | int code ) |
| 2429 | { |
Benny Prijono | 59b3aed | 2008-01-15 16:54:54 +0000 | [diff] [blame] | 2430 | pjsua_call *call; |
Benny Prijono | a38ada0 | 2006-07-02 14:22:35 +0000 | [diff] [blame] | 2431 | pjsip_tx_data *tdata; |
| 2432 | pj_status_t status; |
| 2433 | |
Benny Prijono | 59b3aed | 2008-01-15 16:54:54 +0000 | [diff] [blame] | 2434 | call = (pjsua_call*) inv->dlg->mod_data[pjsua_var.mod.id]; |
| 2435 | |
Benny Prijono | a38ada0 | 2006-07-02 14:22:35 +0000 | [diff] [blame] | 2436 | status = pjsip_inv_end_session(inv, code, NULL, &tdata); |
Benny Prijono | 59b3aed | 2008-01-15 16:54:54 +0000 | [diff] [blame] | 2437 | if (status != PJ_SUCCESS) |
| 2438 | return; |
| 2439 | |
| 2440 | /* Add SDP in 488 status */ |
| 2441 | if (call && call->med_tp && code==PJSIP_SC_NOT_ACCEPTABLE_HERE) { |
| 2442 | pjmedia_sdp_session *local_sdp; |
| 2443 | pjmedia_sock_info si; |
| 2444 | |
| 2445 | call->med_tp->op->get_info(call->med_tp, &si); |
| 2446 | status = pjmedia_endpt_create_sdp(pjsua_var.med_endpt, tdata->pool, |
| 2447 | 1, &si, &local_sdp); |
| 2448 | if (status == PJ_SUCCESS) { |
| 2449 | pjsip_create_sdp_body(tdata->pool, local_sdp, |
| 2450 | &tdata->msg->body); |
| 2451 | } |
| 2452 | } |
| 2453 | |
| 2454 | pjsip_inv_send_msg(inv, tdata); |
Benny Prijono | a38ada0 | 2006-07-02 14:22:35 +0000 | [diff] [blame] | 2455 | } |
| 2456 | |
| 2457 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2458 | * Callback to be called when SDP offer/answer negotiation has just completed |
| 2459 | * in the session. This function will start/update media if negotiation |
| 2460 | * has succeeded. |
| 2461 | */ |
| 2462 | static void pjsua_call_on_media_update(pjsip_inv_session *inv, |
| 2463 | pj_status_t status) |
| 2464 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2465 | pjsua_call *call; |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 2466 | const pjmedia_sdp_session *c_local; |
| 2467 | pjmedia_sdp_session *local_sdp; |
Benny Prijono | dbce2cf | 2007-03-28 16:24:00 +0000 | [diff] [blame] | 2468 | const pjmedia_sdp_session *remote_sdp; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2469 | |
| 2470 | PJSUA_LOCK(); |
| 2471 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 2472 | call = (pjsua_call*) inv->dlg->mod_data[pjsua_var.mod.id]; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2473 | |
| 2474 | if (status != PJ_SUCCESS) { |
| 2475 | |
| 2476 | pjsua_perror(THIS_FILE, "SDP negotiation has failed", status); |
| 2477 | |
Benny Prijono | 1d9b9a4 | 2006-09-25 13:40:12 +0000 | [diff] [blame] | 2478 | /* Stop/destroy media, if any */ |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 2479 | pjsua_media_channel_deinit(call->index); |
Benny Prijono | 1d9b9a4 | 2006-09-25 13:40:12 +0000 | [diff] [blame] | 2480 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2481 | /* Disconnect call if we're not in the middle of initializing an |
| 2482 | * UAS dialog and if this is not a re-INVITE |
| 2483 | */ |
| 2484 | if (inv->state != PJSIP_INV_STATE_NULL && |
| 2485 | inv->state != PJSIP_INV_STATE_CONFIRMED) |
| 2486 | { |
Benny Prijono | 2dbed82 | 2008-02-21 10:08:27 +0000 | [diff] [blame] | 2487 | call_disconnect(inv, PJSIP_SC_UNSUPPORTED_MEDIA_TYPE); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2488 | } |
| 2489 | |
| 2490 | PJSUA_UNLOCK(); |
| 2491 | return; |
| 2492 | } |
| 2493 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2494 | |
| 2495 | /* Get local and remote SDP */ |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 2496 | status = pjmedia_sdp_neg_get_active_local(call->inv->neg, &c_local); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2497 | if (status != PJ_SUCCESS) { |
| 2498 | pjsua_perror(THIS_FILE, |
| 2499 | "Unable to retrieve currently active local SDP", |
| 2500 | status); |
| 2501 | //call_disconnect(inv, PJSIP_SC_UNSUPPORTED_MEDIA_TYPE); |
| 2502 | PJSUA_UNLOCK(); |
| 2503 | return; |
| 2504 | } |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 2505 | local_sdp = (pjmedia_sdp_session*) c_local; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2506 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2507 | status = pjmedia_sdp_neg_get_active_remote(call->inv->neg, &remote_sdp); |
| 2508 | if (status != PJ_SUCCESS) { |
| 2509 | pjsua_perror(THIS_FILE, |
| 2510 | "Unable to retrieve currently active remote SDP", |
| 2511 | status); |
| 2512 | //call_disconnect(inv, PJSIP_SC_UNSUPPORTED_MEDIA_TYPE); |
| 2513 | PJSUA_UNLOCK(); |
| 2514 | return; |
| 2515 | } |
| 2516 | |
Benny Prijono | 91a6a17 | 2007-10-31 08:59:29 +0000 | [diff] [blame] | 2517 | /* Update remote's NAT type */ |
| 2518 | if (pjsua_var.ua_cfg.nat_type_in_sdp) { |
| 2519 | update_remote_nat_type(call, remote_sdp); |
| 2520 | } |
| 2521 | |
| 2522 | /* Update media channel with the new SDP */ |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 2523 | status = pjsua_media_channel_update(call->index, local_sdp, remote_sdp); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2524 | if (status != PJ_SUCCESS) { |
| 2525 | pjsua_perror(THIS_FILE, "Unable to create media session", |
| 2526 | status); |
Benny Prijono | 59b3aed | 2008-01-15 16:54:54 +0000 | [diff] [blame] | 2527 | call_disconnect(inv, PJSIP_SC_NOT_ACCEPTABLE_HERE); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 2528 | pjsua_media_channel_deinit(call->index); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2529 | PJSUA_UNLOCK(); |
| 2530 | return; |
| 2531 | } |
| 2532 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2533 | |
| 2534 | /* Call application callback, if any */ |
| 2535 | if (pjsua_var.ua_cfg.cb.on_call_media_state) |
| 2536 | pjsua_var.ua_cfg.cb.on_call_media_state(call->index); |
| 2537 | |
| 2538 | |
| 2539 | PJSUA_UNLOCK(); |
| 2540 | } |
| 2541 | |
| 2542 | |
| 2543 | /* |
| 2544 | * Create inactive SDP for call hold. |
| 2545 | */ |
| 2546 | static pj_status_t create_inactive_sdp(pjsua_call *call, |
| 2547 | pjmedia_sdp_session **p_answer) |
| 2548 | { |
| 2549 | pj_status_t status; |
| 2550 | pjmedia_sdp_conn *conn; |
| 2551 | pjmedia_sdp_attr *attr; |
Benny Prijono | 617c5bc | 2007-04-02 19:51:21 +0000 | [diff] [blame] | 2552 | pjmedia_sock_info skinfo; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2553 | pjmedia_sdp_session *sdp; |
| 2554 | |
Benny Prijono | 617c5bc | 2007-04-02 19:51:21 +0000 | [diff] [blame] | 2555 | /* Get media socket info */ |
| 2556 | pjmedia_transport_get_info(call->med_tp, &skinfo); |
| 2557 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2558 | /* Create new offer */ |
| 2559 | status = pjmedia_endpt_create_sdp(pjsua_var.med_endpt, pjsua_var.pool, 1, |
Benny Prijono | 617c5bc | 2007-04-02 19:51:21 +0000 | [diff] [blame] | 2560 | &skinfo, &sdp); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2561 | if (status != PJ_SUCCESS) { |
| 2562 | pjsua_perror(THIS_FILE, "Unable to create local SDP", status); |
| 2563 | return status; |
| 2564 | } |
| 2565 | |
| 2566 | /* Get SDP media connection line */ |
| 2567 | conn = sdp->media[0]->conn; |
| 2568 | if (!conn) |
| 2569 | conn = sdp->conn; |
| 2570 | |
| 2571 | /* Modify address */ |
| 2572 | conn->addr = pj_str("0.0.0.0"); |
| 2573 | |
| 2574 | /* Remove existing directions attributes */ |
| 2575 | pjmedia_sdp_media_remove_all_attr(sdp->media[0], "sendrecv"); |
| 2576 | pjmedia_sdp_media_remove_all_attr(sdp->media[0], "sendonly"); |
| 2577 | pjmedia_sdp_media_remove_all_attr(sdp->media[0], "recvonly"); |
| 2578 | pjmedia_sdp_media_remove_all_attr(sdp->media[0], "inactive"); |
| 2579 | |
| 2580 | /* Add inactive attribute */ |
| 2581 | attr = pjmedia_sdp_attr_create(pjsua_var.pool, "inactive", NULL); |
| 2582 | pjmedia_sdp_media_add_attr(sdp->media[0], attr); |
| 2583 | |
| 2584 | *p_answer = sdp; |
| 2585 | |
| 2586 | return status; |
| 2587 | } |
| 2588 | |
| 2589 | |
| 2590 | /* |
| 2591 | * Called when session received new offer. |
| 2592 | */ |
| 2593 | static void pjsua_call_on_rx_offer(pjsip_inv_session *inv, |
| 2594 | const pjmedia_sdp_session *offer) |
| 2595 | { |
| 2596 | const char *remote_state; |
| 2597 | pjsua_call *call; |
| 2598 | pjmedia_sdp_conn *conn; |
| 2599 | pjmedia_sdp_session *answer; |
| 2600 | pj_bool_t is_remote_active; |
| 2601 | pj_status_t status; |
| 2602 | |
| 2603 | PJSUA_LOCK(); |
| 2604 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 2605 | call = (pjsua_call*) inv->dlg->mod_data[pjsua_var.mod.id]; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2606 | |
| 2607 | /* |
| 2608 | * See if remote is offering active media (i.e. not on-hold) |
| 2609 | */ |
| 2610 | is_remote_active = PJ_TRUE; |
| 2611 | |
| 2612 | conn = offer->media[0]->conn; |
| 2613 | if (!conn) |
| 2614 | conn = offer->conn; |
| 2615 | |
| 2616 | if (pj_strcmp2(&conn->addr, "0.0.0.0")==0 || |
| 2617 | pj_strcmp2(&conn->addr, "0")==0) |
| 2618 | { |
| 2619 | is_remote_active = PJ_FALSE; |
| 2620 | |
| 2621 | } |
Benny Prijono | 8eb07bf | 2007-11-08 09:39:06 +0000 | [diff] [blame] | 2622 | else if (pjmedia_sdp_media_find_attr2(offer->media[0], "inactive", NULL) || |
| 2623 | pjmedia_sdp_media_find_attr2(offer->media[0], "sendonly", NULL)) |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2624 | { |
| 2625 | is_remote_active = PJ_FALSE; |
| 2626 | } |
| 2627 | |
| 2628 | remote_state = (is_remote_active ? "active" : "inactive"); |
| 2629 | |
| 2630 | /* Supply candidate answer */ |
| 2631 | if (call->media_st == PJSUA_CALL_MEDIA_LOCAL_HOLD || !is_remote_active) { |
| 2632 | PJ_LOG(4,(THIS_FILE, |
| 2633 | "Call %d: RX new media offer, creating inactive SDP " |
| 2634 | "(media in offer is %s)", call->index, remote_state)); |
| 2635 | status = create_inactive_sdp( call, &answer ); |
| 2636 | } else { |
| 2637 | PJ_LOG(4,(THIS_FILE, "Call %d: received updated media offer", |
| 2638 | call->index)); |
Benny Prijono | 667952e | 2007-04-02 19:27:54 +0000 | [diff] [blame] | 2639 | |
Benny Prijono | db844a4 | 2008-02-02 17:07:18 +0000 | [diff] [blame] | 2640 | /* Update call's secure level */ |
| 2641 | call->secure_level = call_get_secure_level(call); |
| 2642 | |
Benny Prijono | 667952e | 2007-04-02 19:27:54 +0000 | [diff] [blame] | 2643 | /* Init media channel */ |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 2644 | status = pjsua_media_channel_init(call->index, PJSIP_ROLE_UAS, |
Benny Prijono | db844a4 | 2008-02-02 17:07:18 +0000 | [diff] [blame] | 2645 | call->secure_level, NULL); |
Benny Prijono | 667952e | 2007-04-02 19:27:54 +0000 | [diff] [blame] | 2646 | if (status != PJ_SUCCESS) { |
| 2647 | pjsua_perror(THIS_FILE, "Error initializing media channel", status); |
| 2648 | PJSUA_UNLOCK(); |
| 2649 | return; |
| 2650 | } |
| 2651 | |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 2652 | status = pjsua_media_channel_create_sdp(call->index, call->inv->pool, |
Benny Prijono | 25b2ea1 | 2008-01-24 19:20:54 +0000 | [diff] [blame] | 2653 | offer, &answer, NULL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2654 | } |
| 2655 | |
| 2656 | if (status != PJ_SUCCESS) { |
| 2657 | pjsua_perror(THIS_FILE, "Unable to create local SDP", status); |
| 2658 | PJSUA_UNLOCK(); |
| 2659 | return; |
| 2660 | } |
| 2661 | |
| 2662 | status = pjsip_inv_set_sdp_answer(call->inv, answer); |
| 2663 | if (status != PJ_SUCCESS) { |
| 2664 | pjsua_perror(THIS_FILE, "Unable to set answer", status); |
| 2665 | PJSUA_UNLOCK(); |
| 2666 | return; |
| 2667 | } |
| 2668 | |
| 2669 | PJSUA_UNLOCK(); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 2670 | } |
| 2671 | |
| 2672 | |
| 2673 | /* |
Benny Prijono | 77998ce | 2007-06-20 10:03:46 +0000 | [diff] [blame] | 2674 | * Called to generate new offer. |
| 2675 | */ |
| 2676 | static void pjsua_call_on_create_offer(pjsip_inv_session *inv, |
| 2677 | pjmedia_sdp_session **offer) |
| 2678 | { |
| 2679 | pjsua_call *call; |
| 2680 | pj_status_t status; |
| 2681 | |
| 2682 | PJSUA_LOCK(); |
| 2683 | |
| 2684 | call = (pjsua_call*) inv->dlg->mod_data[pjsua_var.mod.id]; |
| 2685 | |
| 2686 | /* See if we've put call on hold. */ |
| 2687 | if (call->media_st == PJSUA_CALL_MEDIA_LOCAL_HOLD) { |
| 2688 | PJ_LOG(4,(THIS_FILE, |
| 2689 | "Call %d: call is on-hold locally, creating inactive SDP ", |
| 2690 | call->index)); |
| 2691 | status = create_inactive_sdp( call, offer ); |
| 2692 | } else { |
Benny Prijono | 77998ce | 2007-06-20 10:03:46 +0000 | [diff] [blame] | 2693 | PJ_LOG(4,(THIS_FILE, "Call %d: asked to send a new offer", |
| 2694 | call->index)); |
| 2695 | |
Benny Prijono | db844a4 | 2008-02-02 17:07:18 +0000 | [diff] [blame] | 2696 | /* Update call's secure level */ |
| 2697 | call->secure_level = call_get_secure_level(call); |
| 2698 | |
Benny Prijono | 77998ce | 2007-06-20 10:03:46 +0000 | [diff] [blame] | 2699 | /* Init media channel */ |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 2700 | status = pjsua_media_channel_init(call->index, PJSIP_ROLE_UAC, |
Benny Prijono | db844a4 | 2008-02-02 17:07:18 +0000 | [diff] [blame] | 2701 | call->secure_level, NULL); |
Benny Prijono | 77998ce | 2007-06-20 10:03:46 +0000 | [diff] [blame] | 2702 | if (status != PJ_SUCCESS) { |
| 2703 | pjsua_perror(THIS_FILE, "Error initializing media channel", status); |
| 2704 | PJSUA_UNLOCK(); |
| 2705 | return; |
| 2706 | } |
| 2707 | |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 2708 | status = pjsua_media_channel_create_sdp(call->index, call->inv->pool, |
Benny Prijono | 25b2ea1 | 2008-01-24 19:20:54 +0000 | [diff] [blame] | 2709 | NULL, offer, NULL); |
Benny Prijono | 77998ce | 2007-06-20 10:03:46 +0000 | [diff] [blame] | 2710 | } |
| 2711 | |
| 2712 | if (status != PJ_SUCCESS) { |
| 2713 | pjsua_perror(THIS_FILE, "Unable to create local SDP", status); |
| 2714 | PJSUA_UNLOCK(); |
| 2715 | return; |
| 2716 | } |
| 2717 | |
Benny Prijono | 7129cc7 | 2007-11-05 05:54:25 +0000 | [diff] [blame] | 2718 | update_sdp_version(call, *offer); |
Benny Prijono | 77998ce | 2007-06-20 10:03:46 +0000 | [diff] [blame] | 2719 | |
| 2720 | PJSUA_UNLOCK(); |
| 2721 | } |
| 2722 | |
| 2723 | |
| 2724 | /* |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2725 | * Callback called by event framework when the xfer subscription state |
| 2726 | * has changed. |
| 2727 | */ |
Benny Prijono | 4ddad2c | 2006-10-18 17:16:34 +0000 | [diff] [blame] | 2728 | static void xfer_client_on_evsub_state( pjsip_evsub *sub, pjsip_event *event) |
| 2729 | { |
| 2730 | |
| 2731 | PJ_UNUSED_ARG(event); |
| 2732 | |
| 2733 | /* |
| 2734 | * When subscription is accepted (got 200/OK to REFER), check if |
| 2735 | * subscription suppressed. |
| 2736 | */ |
| 2737 | if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_ACCEPTED) { |
| 2738 | |
| 2739 | pjsip_rx_data *rdata; |
| 2740 | pjsip_generic_string_hdr *refer_sub; |
| 2741 | const pj_str_t REFER_SUB = { "Refer-Sub", 9 }; |
| 2742 | pjsua_call *call; |
| 2743 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 2744 | call = (pjsua_call*) pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id); |
Benny Prijono | 4ddad2c | 2006-10-18 17:16:34 +0000 | [diff] [blame] | 2745 | |
| 2746 | /* Must be receipt of response message */ |
| 2747 | pj_assert(event->type == PJSIP_EVENT_TSX_STATE && |
| 2748 | event->body.tsx_state.type == PJSIP_EVENT_RX_MSG); |
| 2749 | rdata = event->body.tsx_state.src.rdata; |
| 2750 | |
| 2751 | /* Find Refer-Sub header */ |
| 2752 | refer_sub = (pjsip_generic_string_hdr*) |
| 2753 | pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, |
| 2754 | &REFER_SUB, NULL); |
| 2755 | |
| 2756 | /* Check if subscription is suppressed */ |
| 2757 | if (refer_sub && pj_stricmp2(&refer_sub->hvalue, "false")==0) { |
| 2758 | /* Since no subscription is desired, assume that call has been |
| 2759 | * transfered successfully. |
| 2760 | */ |
| 2761 | if (call && pjsua_var.ua_cfg.cb.on_call_transfer_status) { |
| 2762 | const pj_str_t ACCEPTED = { "Accepted", 8 }; |
| 2763 | pj_bool_t cont = PJ_FALSE; |
| 2764 | (*pjsua_var.ua_cfg.cb.on_call_transfer_status)(call->index, |
| 2765 | 200, |
| 2766 | &ACCEPTED, |
| 2767 | PJ_TRUE, |
| 2768 | &cont); |
| 2769 | } |
| 2770 | |
| 2771 | /* Yes, subscription is suppressed. |
| 2772 | * Terminate our subscription now. |
| 2773 | */ |
| 2774 | PJ_LOG(4,(THIS_FILE, "Xfer subscription suppressed, terminating " |
| 2775 | "event subcription...")); |
| 2776 | pjsip_evsub_terminate(sub, PJ_TRUE); |
| 2777 | |
| 2778 | } else { |
| 2779 | /* Notify application about call transfer progress. |
| 2780 | * Initially notify with 100/Accepted status. |
| 2781 | */ |
| 2782 | if (call && pjsua_var.ua_cfg.cb.on_call_transfer_status) { |
| 2783 | const pj_str_t ACCEPTED = { "Accepted", 8 }; |
| 2784 | pj_bool_t cont = PJ_FALSE; |
| 2785 | (*pjsua_var.ua_cfg.cb.on_call_transfer_status)(call->index, |
| 2786 | 100, |
| 2787 | &ACCEPTED, |
| 2788 | PJ_FALSE, |
| 2789 | &cont); |
| 2790 | } |
| 2791 | } |
| 2792 | } |
| 2793 | /* |
| 2794 | * On incoming NOTIFY, notify application about call transfer progress. |
| 2795 | */ |
| 2796 | else if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_ACTIVE || |
| 2797 | pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED) |
| 2798 | { |
| 2799 | pjsua_call *call; |
| 2800 | pjsip_msg *msg; |
| 2801 | pjsip_msg_body *body; |
| 2802 | pjsip_status_line status_line; |
| 2803 | pj_bool_t is_last; |
| 2804 | pj_bool_t cont; |
| 2805 | pj_status_t status; |
| 2806 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 2807 | call = (pjsua_call*) pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id); |
Benny Prijono | 4ddad2c | 2006-10-18 17:16:34 +0000 | [diff] [blame] | 2808 | |
| 2809 | /* When subscription is terminated, clear the xfer_sub member of |
| 2810 | * the inv_data. |
| 2811 | */ |
| 2812 | if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED) { |
| 2813 | pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, NULL); |
| 2814 | PJ_LOG(4,(THIS_FILE, "Xfer client subscription terminated")); |
| 2815 | |
| 2816 | } |
| 2817 | |
| 2818 | if (!call || !event || !pjsua_var.ua_cfg.cb.on_call_transfer_status) { |
| 2819 | /* Application is not interested with call progress status */ |
| 2820 | return; |
| 2821 | } |
| 2822 | |
| 2823 | /* This better be a NOTIFY request */ |
| 2824 | if (event->type == PJSIP_EVENT_TSX_STATE && |
| 2825 | event->body.tsx_state.type == PJSIP_EVENT_RX_MSG) |
| 2826 | { |
| 2827 | pjsip_rx_data *rdata; |
| 2828 | |
| 2829 | rdata = event->body.tsx_state.src.rdata; |
| 2830 | |
| 2831 | /* Check if there's body */ |
| 2832 | msg = rdata->msg_info.msg; |
| 2833 | body = msg->body; |
| 2834 | if (!body) { |
| 2835 | PJ_LOG(4,(THIS_FILE, |
| 2836 | "Warning: received NOTIFY without message body")); |
| 2837 | return; |
| 2838 | } |
| 2839 | |
| 2840 | /* Check for appropriate content */ |
| 2841 | if (pj_stricmp2(&body->content_type.type, "message") != 0 || |
| 2842 | pj_stricmp2(&body->content_type.subtype, "sipfrag") != 0) |
| 2843 | { |
| 2844 | PJ_LOG(4,(THIS_FILE, |
| 2845 | "Warning: received NOTIFY with non message/sipfrag " |
| 2846 | "content")); |
| 2847 | return; |
| 2848 | } |
| 2849 | |
| 2850 | /* Try to parse the content */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 2851 | status = pjsip_parse_status_line((char*)body->data, body->len, |
Benny Prijono | 4ddad2c | 2006-10-18 17:16:34 +0000 | [diff] [blame] | 2852 | &status_line); |
| 2853 | if (status != PJ_SUCCESS) { |
| 2854 | PJ_LOG(4,(THIS_FILE, |
| 2855 | "Warning: received NOTIFY with invalid " |
| 2856 | "message/sipfrag content")); |
| 2857 | return; |
| 2858 | } |
| 2859 | |
| 2860 | } else { |
| 2861 | status_line.code = 500; |
| 2862 | status_line.reason = *pjsip_get_status_text(500); |
| 2863 | } |
| 2864 | |
| 2865 | /* Notify application */ |
| 2866 | is_last = (pjsip_evsub_get_state(sub)==PJSIP_EVSUB_STATE_TERMINATED); |
| 2867 | cont = !is_last; |
| 2868 | (*pjsua_var.ua_cfg.cb.on_call_transfer_status)(call->index, |
| 2869 | status_line.code, |
| 2870 | &status_line.reason, |
| 2871 | is_last, &cont); |
| 2872 | |
| 2873 | if (!cont) { |
| 2874 | pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, NULL); |
| 2875 | } |
| 2876 | } |
| 2877 | } |
| 2878 | |
| 2879 | |
| 2880 | /* |
| 2881 | * Callback called by event framework when the xfer subscription state |
| 2882 | * has changed. |
| 2883 | */ |
| 2884 | static void xfer_server_on_evsub_state( pjsip_evsub *sub, pjsip_event *event) |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2885 | { |
| 2886 | |
| 2887 | PJ_UNUSED_ARG(event); |
| 2888 | |
| 2889 | /* |
Benny Prijono | d524e82 | 2006-09-22 12:48:18 +0000 | [diff] [blame] | 2890 | * When subscription is terminated, clear the xfer_sub member of |
| 2891 | * the inv_data. |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2892 | */ |
| 2893 | if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED) { |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 2894 | pjsua_call *call; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2895 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 2896 | call = (pjsua_call*) pjsip_evsub_get_mod_data(sub, pjsua_var.mod.id); |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 2897 | if (!call) |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2898 | return; |
| 2899 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2900 | pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, NULL); |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 2901 | call->xfer_sub = NULL; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2902 | |
Benny Prijono | 4ddad2c | 2006-10-18 17:16:34 +0000 | [diff] [blame] | 2903 | PJ_LOG(4,(THIS_FILE, "Xfer server subscription terminated")); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2904 | } |
| 2905 | } |
| 2906 | |
| 2907 | |
| 2908 | /* |
| 2909 | * Follow transfer (REFER) request. |
| 2910 | */ |
| 2911 | static void on_call_transfered( pjsip_inv_session *inv, |
| 2912 | pjsip_rx_data *rdata ) |
| 2913 | { |
| 2914 | pj_status_t status; |
| 2915 | pjsip_tx_data *tdata; |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 2916 | pjsua_call *existing_call; |
| 2917 | int new_call; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2918 | const pj_str_t str_refer_to = { "Refer-To", 8}; |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 2919 | const pj_str_t str_refer_sub = { "Refer-Sub", 9 }; |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 2920 | const pj_str_t str_ref_by = { "Referred-By", 11 }; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2921 | pjsip_generic_string_hdr *refer_to; |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 2922 | pjsip_generic_string_hdr *refer_sub; |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 2923 | pjsip_hdr *ref_by_hdr; |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 2924 | pj_bool_t no_refer_sub = PJ_FALSE; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2925 | char *uri; |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 2926 | pjsua_msg_data msg_data; |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 2927 | pj_str_t tmp; |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 2928 | pjsip_status_code code; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2929 | pjsip_evsub *sub; |
| 2930 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 2931 | existing_call = (pjsua_call*) inv->dlg->mod_data[pjsua_var.mod.id]; |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 2932 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2933 | /* Find the Refer-To header */ |
| 2934 | refer_to = (pjsip_generic_string_hdr*) |
| 2935 | pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_refer_to, NULL); |
| 2936 | |
| 2937 | if (refer_to == NULL) { |
| 2938 | /* Invalid Request. |
| 2939 | * No Refer-To header! |
| 2940 | */ |
| 2941 | PJ_LOG(4,(THIS_FILE, "Received REFER without Refer-To header!")); |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 2942 | pjsip_dlg_respond( inv->dlg, rdata, 400, NULL, NULL, NULL); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2943 | return; |
| 2944 | } |
| 2945 | |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 2946 | /* Find optional Refer-Sub header */ |
| 2947 | refer_sub = (pjsip_generic_string_hdr*) |
| 2948 | pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_refer_sub, NULL); |
| 2949 | |
| 2950 | if (refer_sub) { |
| 2951 | if (!pj_strnicmp2(&refer_sub->hvalue, "true", 4)==0) |
| 2952 | no_refer_sub = PJ_TRUE; |
| 2953 | } |
| 2954 | |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 2955 | /* Find optional Referred-By header (to be copied onto outgoing INVITE |
| 2956 | * request. |
| 2957 | */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 2958 | ref_by_hdr = (pjsip_hdr*) |
| 2959 | pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_ref_by, |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 2960 | NULL); |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 2961 | |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 2962 | /* Notify callback */ |
| 2963 | code = PJSIP_SC_OK; |
Benny Prijono | 4ddad2c | 2006-10-18 17:16:34 +0000 | [diff] [blame] | 2964 | if (pjsua_var.ua_cfg.cb.on_call_transfer_request) |
| 2965 | (*pjsua_var.ua_cfg.cb.on_call_transfer_request)(existing_call->index, |
| 2966 | &refer_to->hvalue, |
| 2967 | &code); |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 2968 | |
| 2969 | if (code < 200) |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 2970 | code = PJSIP_SC_OK; |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 2971 | if (code >= 300) { |
| 2972 | /* Application rejects call transfer request */ |
| 2973 | pjsip_dlg_respond( inv->dlg, rdata, code, NULL, NULL, NULL); |
| 2974 | return; |
| 2975 | } |
| 2976 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2977 | PJ_LOG(3,(THIS_FILE, "Call to %.*s is being transfered to %.*s", |
| 2978 | (int)inv->dlg->remote.info_str.slen, |
| 2979 | inv->dlg->remote.info_str.ptr, |
| 2980 | (int)refer_to->hvalue.slen, |
| 2981 | refer_to->hvalue.ptr)); |
| 2982 | |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 2983 | if (no_refer_sub) { |
| 2984 | /* |
| 2985 | * Always answer with 200. |
| 2986 | */ |
| 2987 | pjsip_tx_data *tdata; |
| 2988 | const pj_str_t str_false = { "false", 5}; |
| 2989 | pjsip_hdr *hdr; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2990 | |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 2991 | status = pjsip_dlg_create_response(inv->dlg, rdata, 200, NULL, &tdata); |
| 2992 | if (status != PJ_SUCCESS) { |
| 2993 | pjsua_perror(THIS_FILE, "Unable to create 200 response to REFER", |
| 2994 | status); |
| 2995 | return; |
| 2996 | } |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2997 | |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 2998 | /* Add Refer-Sub header */ |
| 2999 | hdr = (pjsip_hdr*) |
| 3000 | pjsip_generic_string_hdr_create(tdata->pool, &str_refer_sub, |
| 3001 | &str_false); |
| 3002 | pjsip_msg_add_hdr(tdata->msg, hdr); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3003 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3004 | |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 3005 | /* Send answer */ |
| 3006 | status = pjsip_dlg_send_response(inv->dlg, pjsip_rdata_get_tsx(rdata), |
| 3007 | tdata); |
| 3008 | if (status != PJ_SUCCESS) { |
| 3009 | pjsua_perror(THIS_FILE, "Unable to create 200 response to REFER", |
| 3010 | status); |
| 3011 | return; |
| 3012 | } |
| 3013 | |
| 3014 | /* Don't have subscription */ |
| 3015 | sub = NULL; |
| 3016 | |
| 3017 | } else { |
| 3018 | struct pjsip_evsub_user xfer_cb; |
| 3019 | pjsip_hdr hdr_list; |
| 3020 | |
| 3021 | /* Init callback */ |
| 3022 | pj_bzero(&xfer_cb, sizeof(xfer_cb)); |
Benny Prijono | 4ddad2c | 2006-10-18 17:16:34 +0000 | [diff] [blame] | 3023 | xfer_cb.on_evsub_state = &xfer_server_on_evsub_state; |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 3024 | |
| 3025 | /* Init additional header list to be sent with REFER response */ |
| 3026 | pj_list_init(&hdr_list); |
| 3027 | |
| 3028 | /* Create transferee event subscription */ |
| 3029 | status = pjsip_xfer_create_uas( inv->dlg, &xfer_cb, rdata, &sub); |
| 3030 | if (status != PJ_SUCCESS) { |
| 3031 | pjsua_perror(THIS_FILE, "Unable to create xfer uas", status); |
| 3032 | pjsip_dlg_respond( inv->dlg, rdata, 500, NULL, NULL, NULL); |
| 3033 | return; |
| 3034 | } |
| 3035 | |
| 3036 | /* If there's Refer-Sub header and the value is "true", send back |
| 3037 | * Refer-Sub in the response with value "true" too. |
| 3038 | */ |
| 3039 | if (refer_sub) { |
| 3040 | const pj_str_t str_true = { "true", 4 }; |
| 3041 | pjsip_hdr *hdr; |
| 3042 | |
| 3043 | hdr = (pjsip_hdr*) |
| 3044 | pjsip_generic_string_hdr_create(inv->dlg->pool, |
| 3045 | &str_refer_sub, |
| 3046 | &str_true); |
| 3047 | pj_list_push_back(&hdr_list, hdr); |
| 3048 | |
| 3049 | } |
| 3050 | |
| 3051 | /* Accept the REFER request, send 200 (OK). */ |
| 3052 | pjsip_xfer_accept(sub, rdata, code, &hdr_list); |
| 3053 | |
| 3054 | /* Create initial NOTIFY request */ |
| 3055 | status = pjsip_xfer_notify( sub, PJSIP_EVSUB_STATE_ACTIVE, |
| 3056 | 100, NULL, &tdata); |
| 3057 | if (status != PJ_SUCCESS) { |
| 3058 | pjsua_perror(THIS_FILE, "Unable to create NOTIFY to REFER", |
| 3059 | status); |
| 3060 | return; |
| 3061 | } |
| 3062 | |
| 3063 | /* Send initial NOTIFY request */ |
| 3064 | status = pjsip_xfer_send_request( sub, tdata); |
| 3065 | if (status != PJ_SUCCESS) { |
| 3066 | pjsua_perror(THIS_FILE, "Unable to send NOTIFY to REFER", status); |
| 3067 | return; |
| 3068 | } |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3069 | } |
| 3070 | |
| 3071 | /* We're cheating here. |
| 3072 | * We need to get a null terminated string from a pj_str_t. |
| 3073 | * So grab the pointer from the hvalue and NULL terminate it, knowing |
| 3074 | * that the NULL position will be occupied by a newline. |
| 3075 | */ |
| 3076 | uri = refer_to->hvalue.ptr; |
| 3077 | uri[refer_to->hvalue.slen] = '\0'; |
| 3078 | |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 3079 | /* Init msg_data */ |
| 3080 | pjsua_msg_data_init(&msg_data); |
| 3081 | |
| 3082 | /* If Referred-By header is present in the REFER request, copy this |
| 3083 | * to the outgoing INVITE request. |
| 3084 | */ |
| 3085 | if (ref_by_hdr != NULL) { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 3086 | pjsip_hdr *dup = (pjsip_hdr*) |
| 3087 | pjsip_hdr_clone(rdata->tp_info.pool, ref_by_hdr); |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 3088 | pj_list_push_back(&msg_data.hdr_list, dup); |
| 3089 | } |
| 3090 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3091 | /* Now make the outgoing call. */ |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 3092 | tmp = pj_str(uri); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3093 | status = pjsua_call_make_call(existing_call->acc_id, &tmp, 0, |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 3094 | existing_call->user_data, &msg_data, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3095 | &new_call); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3096 | if (status != PJ_SUCCESS) { |
| 3097 | |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 3098 | /* Notify xferer about the error (if we have subscription) */ |
| 3099 | if (sub) { |
| 3100 | status = pjsip_xfer_notify(sub, PJSIP_EVSUB_STATE_TERMINATED, |
| 3101 | 500, NULL, &tdata); |
| 3102 | if (status != PJ_SUCCESS) { |
| 3103 | pjsua_perror(THIS_FILE, "Unable to create NOTIFY to REFER", |
| 3104 | status); |
| 3105 | return; |
| 3106 | } |
| 3107 | status = pjsip_xfer_send_request(sub, tdata); |
| 3108 | if (status != PJ_SUCCESS) { |
| 3109 | pjsua_perror(THIS_FILE, "Unable to send NOTIFY to REFER", |
| 3110 | status); |
| 3111 | return; |
| 3112 | } |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3113 | } |
| 3114 | return; |
| 3115 | } |
| 3116 | |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 3117 | if (sub) { |
| 3118 | /* Put the server subscription in inv_data. |
| 3119 | * Subsequent state changed in pjsua_inv_on_state_changed() will be |
| 3120 | * reported back to the server subscription. |
| 3121 | */ |
| 3122 | pjsua_var.calls[new_call].xfer_sub = sub; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3123 | |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 3124 | /* Put the invite_data in the subscription. */ |
| 3125 | pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, |
| 3126 | &pjsua_var.calls[new_call]); |
| 3127 | } |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3128 | } |
| 3129 | |
| 3130 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3131 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3132 | /* |
| 3133 | * This callback is called when transaction state has changed in INVITE |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 3134 | * session. We use this to trap: |
| 3135 | * - incoming REFER request. |
| 3136 | * - incoming MESSAGE request. |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3137 | */ |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 3138 | static void pjsua_call_on_tsx_state_changed(pjsip_inv_session *inv, |
| 3139 | pjsip_transaction *tsx, |
| 3140 | pjsip_event *e) |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3141 | { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 3142 | pjsua_call *call = (pjsua_call*) inv->dlg->mod_data[pjsua_var.mod.id]; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3143 | |
| 3144 | PJSUA_LOCK(); |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 3145 | |
Benny Prijono | feb69f4 | 2007-10-05 09:12:26 +0000 | [diff] [blame] | 3146 | /* Notify application callback first */ |
| 3147 | if (pjsua_var.ua_cfg.cb.on_call_tsx_state) { |
| 3148 | (*pjsua_var.ua_cfg.cb.on_call_tsx_state)(call->index, tsx, e); |
| 3149 | } |
| 3150 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3151 | if (tsx->role==PJSIP_ROLE_UAS && |
| 3152 | tsx->state==PJSIP_TSX_STATE_TRYING && |
Benny Prijono | 1f61a8f | 2007-08-16 10:11:44 +0000 | [diff] [blame] | 3153 | pjsip_method_cmp(&tsx->method, pjsip_get_refer_method())==0) |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3154 | { |
| 3155 | /* |
| 3156 | * Incoming REFER request. |
| 3157 | */ |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 3158 | on_call_transfered(call->inv, e->body.tsx_state.src.rdata); |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 3159 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3160 | } |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 3161 | else if (tsx->role==PJSIP_ROLE_UAS && |
| 3162 | tsx->state==PJSIP_TSX_STATE_TRYING && |
| 3163 | pjsip_method_cmp(&tsx->method, &pjsip_message_method)==0) |
| 3164 | { |
| 3165 | /* |
| 3166 | * Incoming MESSAGE request! |
| 3167 | */ |
| 3168 | pjsip_rx_data *rdata; |
| 3169 | pjsip_msg *msg; |
| 3170 | pjsip_accept_hdr *accept_hdr; |
| 3171 | pj_status_t status; |
| 3172 | |
| 3173 | rdata = e->body.tsx_state.src.rdata; |
| 3174 | msg = rdata->msg_info.msg; |
| 3175 | |
| 3176 | /* Request MUST have message body, with Content-Type equal to |
| 3177 | * "text/plain". |
| 3178 | */ |
| 3179 | if (pjsua_im_accept_pager(rdata, &accept_hdr) == PJ_FALSE) { |
| 3180 | |
| 3181 | pjsip_hdr hdr_list; |
| 3182 | |
| 3183 | pj_list_init(&hdr_list); |
| 3184 | pj_list_push_back(&hdr_list, accept_hdr); |
| 3185 | |
| 3186 | pjsip_dlg_respond( inv->dlg, rdata, PJSIP_SC_NOT_ACCEPTABLE_HERE, |
| 3187 | NULL, &hdr_list, NULL ); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3188 | PJSUA_UNLOCK(); |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 3189 | return; |
| 3190 | } |
| 3191 | |
| 3192 | /* Respond with 200 first, so that remote doesn't retransmit in case |
| 3193 | * the UI takes too long to process the message. |
| 3194 | */ |
| 3195 | status = pjsip_dlg_respond( inv->dlg, rdata, 200, NULL, NULL, NULL); |
| 3196 | |
| 3197 | /* Process MESSAGE request */ |
| 3198 | pjsua_im_process_pager(call->index, &inv->dlg->remote.info_str, |
| 3199 | &inv->dlg->local.info_str, rdata); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3200 | |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 3201 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3202 | else if (tsx->role == PJSIP_ROLE_UAC && |
| 3203 | pjsip_method_cmp(&tsx->method, &pjsip_message_method)==0) |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3204 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3205 | /* Handle outgoing pager status */ |
| 3206 | if (tsx->status_code >= 200) { |
| 3207 | pjsua_im_data *im_data; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3208 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 3209 | im_data = (pjsua_im_data*) tsx->mod_data[pjsua_var.mod.id]; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3210 | /* im_data can be NULL if this is typing indication */ |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3211 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3212 | if (im_data && pjsua_var.ua_cfg.cb.on_pager_status) { |
| 3213 | pjsua_var.ua_cfg.cb.on_pager_status(im_data->call_id, |
| 3214 | &im_data->to, |
| 3215 | &im_data->body, |
| 3216 | im_data->user_data, |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 3217 | (pjsip_status_code) |
| 3218 | tsx->status_code, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3219 | &tsx->status_text); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3220 | } |
Benny Prijono | fccab71 | 2006-02-22 22:23:22 +0000 | [diff] [blame] | 3221 | } |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 3222 | } |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 3223 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3224 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3225 | PJSUA_UNLOCK(); |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 3226 | } |