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 | { |
Benny Prijono | 5d177b8 | 2008-02-26 10:31:28 +0000 | [diff] [blame] | 227 | pjsua_var.next_call_id = 0; |
Benny Prijono | 5773cd6 | 2008-01-19 13:01:42 +0000 | [diff] [blame] | 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) { |
Benny Prijono | 617b860 | 2008-04-07 10:10:31 +0000 | [diff] [blame] | 719 | const pj_str_t reason = pj_str("Bad SDP"); |
| 720 | pjsua_perror(THIS_FILE, "Error parsing SDP in incoming INVITE", |
| 721 | status); |
| 722 | pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, 400, &reason, |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 723 | NULL, NULL); |
| 724 | pjsua_media_channel_deinit(call->index); |
| 725 | PJSUA_UNLOCK(); |
| 726 | return PJ_TRUE; |
| 727 | } |
Benny Prijono | 617b860 | 2008-04-07 10:10:31 +0000 | [diff] [blame] | 728 | |
| 729 | /* Do quick checks on SDP before passing it to transports. More elabore |
| 730 | * checks will be done in pjsip_inv_verify_request2() below. |
| 731 | */ |
| 732 | if (offer->media_count==0) { |
| 733 | const pj_str_t reason = pj_str("Missing media in SDP"); |
| 734 | pjsip_endpt_respond(pjsua_var.endpt, NULL, rdata, 400, &reason, |
| 735 | NULL, NULL, NULL); |
| 736 | PJSUA_UNLOCK(); |
| 737 | return PJ_TRUE; |
| 738 | } |
| 739 | |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 740 | } else { |
| 741 | offer = NULL; |
| 742 | } |
Benny Prijono | 1d9b9a4 | 2006-09-25 13:40:12 +0000 | [diff] [blame] | 743 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 744 | /* Get media capability from media endpoint: */ |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 745 | status = pjsua_media_channel_create_sdp(call->index, rdata->tp_info.pool, |
Benny Prijono | 25b2ea1 | 2008-01-24 19:20:54 +0000 | [diff] [blame] | 746 | offer, &answer, &sip_err_code); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 747 | if (status != PJ_SUCCESS) { |
Benny Prijono | 25b2ea1 | 2008-01-24 19:20:54 +0000 | [diff] [blame] | 748 | pjsua_perror(THIS_FILE, "Error creating SDP answer", status); |
| 749 | pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, |
| 750 | sip_err_code, NULL, |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 751 | NULL, NULL); |
| 752 | pjsua_media_channel_deinit(call->index); |
| 753 | PJSUA_UNLOCK(); |
| 754 | return PJ_TRUE; |
| 755 | } |
| 756 | |
Benny Prijono | 1d9b9a4 | 2006-09-25 13:40:12 +0000 | [diff] [blame] | 757 | /* Verify that we can handle the request. */ |
Benny Prijono | dcfc0ba | 2007-09-30 16:50:27 +0000 | [diff] [blame] | 758 | options |= PJSIP_INV_SUPPORT_100REL; |
Benny Prijono | dcfc0ba | 2007-09-30 16:50:27 +0000 | [diff] [blame] | 759 | if (pjsua_var.acc[acc_id].cfg.require_100rel) |
| 760 | options |= PJSIP_INV_REQUIRE_100REL; |
| 761 | |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 762 | status = pjsip_inv_verify_request2(rdata, &options, offer, answer, NULL, |
| 763 | pjsua_var.endpt, &response); |
Benny Prijono | 1d9b9a4 | 2006-09-25 13:40:12 +0000 | [diff] [blame] | 764 | if (status != PJ_SUCCESS) { |
| 765 | |
| 766 | /* |
| 767 | * No we can't handle the incoming INVITE request. |
| 768 | */ |
Benny Prijono | 1d9b9a4 | 2006-09-25 13:40:12 +0000 | [diff] [blame] | 769 | if (response) { |
| 770 | pjsip_response_addr res_addr; |
| 771 | |
| 772 | pjsip_get_response_addr(response->pool, rdata, &res_addr); |
| 773 | pjsip_endpt_send_response(pjsua_var.endpt, &res_addr, response, |
| 774 | NULL, NULL); |
| 775 | |
| 776 | } else { |
| 777 | |
| 778 | /* Respond with 500 (Internal Server Error) */ |
| 779 | pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, 500, NULL, |
| 780 | NULL, NULL); |
| 781 | } |
| 782 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 783 | pjsua_media_channel_deinit(call->index); |
Benny Prijono | 1d9b9a4 | 2006-09-25 13:40:12 +0000 | [diff] [blame] | 784 | PJSUA_UNLOCK(); |
| 785 | return PJ_TRUE; |
| 786 | } |
| 787 | |
| 788 | |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 789 | /* Get suitable Contact header */ |
| 790 | status = pjsua_acc_create_uas_contact(rdata->tp_info.pool, &contact, |
| 791 | acc_id, rdata); |
| 792 | if (status != PJ_SUCCESS) { |
| 793 | pjsua_perror(THIS_FILE, "Unable to generate Contact header", status); |
| 794 | pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, 500, NULL, |
| 795 | NULL, NULL); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 796 | pjsua_media_channel_deinit(call->index); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 797 | PJSUA_UNLOCK(); |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 798 | return PJ_TRUE; |
| 799 | } |
| 800 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 801 | /* Create dialog: */ |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 802 | status = pjsip_dlg_create_uas( pjsip_ua_instance(), rdata, |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 803 | &contact, &dlg); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 804 | if (status != PJ_SUCCESS) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 805 | pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, 500, NULL, |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 806 | NULL, NULL); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 807 | pjsua_media_channel_deinit(call->index); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 808 | PJSUA_UNLOCK(); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 809 | return PJ_TRUE; |
| 810 | } |
| 811 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 812 | /* Set credentials */ |
| 813 | if (pjsua_var.acc[acc_id].cred_cnt) { |
| 814 | pjsip_auth_clt_set_credentials(&dlg->auth_sess, |
| 815 | pjsua_var.acc[acc_id].cred_cnt, |
| 816 | pjsua_var.acc[acc_id].cred); |
| 817 | } |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 818 | |
Benny Prijono | 48ab2b7 | 2007-11-08 09:24:30 +0000 | [diff] [blame] | 819 | /* Set preference */ |
| 820 | pjsip_auth_clt_set_prefs(&dlg->auth_sess, |
| 821 | &pjsua_var.acc[acc_id].cfg.auth_pref); |
| 822 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 823 | /* Create invite session: */ |
Benny Prijono | dcfc0ba | 2007-09-30 16:50:27 +0000 | [diff] [blame] | 824 | status = pjsip_inv_create_uas( dlg, rdata, answer, options, &inv); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 825 | if (status != PJ_SUCCESS) { |
Benny Prijono | a38ada0 | 2006-07-02 14:22:35 +0000 | [diff] [blame] | 826 | pjsip_hdr hdr_list; |
| 827 | pjsip_warning_hdr *w; |
| 828 | |
| 829 | w = pjsip_warning_hdr_create_from_status(dlg->pool, |
| 830 | pjsip_endpt_name(pjsua_var.endpt), |
| 831 | status); |
| 832 | pj_list_init(&hdr_list); |
| 833 | pj_list_push_back(&hdr_list, w); |
| 834 | |
| 835 | pjsip_dlg_respond(dlg, rdata, 500, NULL, &hdr_list, NULL); |
| 836 | |
| 837 | /* Can't terminate dialog because transaction is in progress. |
Benny Prijono | 1c2bf46 | 2006-03-05 11:54:02 +0000 | [diff] [blame] | 838 | pjsip_dlg_terminate(dlg); |
Benny Prijono | a38ada0 | 2006-07-02 14:22:35 +0000 | [diff] [blame] | 839 | */ |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 840 | pjsua_media_channel_deinit(call->index); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 841 | PJSUA_UNLOCK(); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 842 | return PJ_TRUE; |
| 843 | } |
| 844 | |
Benny Prijono | ea9fd39 | 2007-11-06 03:41:40 +0000 | [diff] [blame] | 845 | /* Update NAT type of remote endpoint, only when there is SDP in |
| 846 | * incoming INVITE! |
| 847 | */ |
| 848 | if (pjsua_var.ua_cfg.nat_type_in_sdp && |
| 849 | pjmedia_sdp_neg_get_state(inv->neg) > PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER) |
| 850 | { |
Benny Prijono | 91a6a17 | 2007-10-31 08:59:29 +0000 | [diff] [blame] | 851 | const pjmedia_sdp_session *remote_sdp; |
| 852 | |
| 853 | if (pjmedia_sdp_neg_get_neg_remote(inv->neg, &remote_sdp)==PJ_SUCCESS) |
| 854 | update_remote_nat_type(call, remote_sdp); |
| 855 | } |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 856 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 857 | /* Create and attach pjsua_var data to the dialog: */ |
| 858 | call->inv = inv; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 859 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 860 | dlg->mod_data[pjsua_var.mod.id] = call; |
| 861 | inv->mod_data[pjsua_var.mod.id] = call; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 862 | |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 863 | /* If account is locked to specific transport, then lock dialog |
| 864 | * to this transport too. |
| 865 | */ |
| 866 | if (pjsua_var.acc[acc_id].cfg.transport_id != PJSUA_INVALID_ID) { |
| 867 | pjsip_tpselector tp_sel; |
| 868 | |
| 869 | pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel); |
| 870 | pjsip_dlg_set_transport(dlg, &tp_sel); |
| 871 | } |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 872 | |
Benny Prijono | 64f851e | 2006-02-23 13:49:28 +0000 | [diff] [blame] | 873 | /* Must answer with some response to initial INVITE. |
Benny Prijono | 64f851e | 2006-02-23 13:49:28 +0000 | [diff] [blame] | 874 | */ |
Benny Prijono | 64f851e | 2006-02-23 13:49:28 +0000 | [diff] [blame] | 875 | status = pjsip_inv_initial_answer(inv, rdata, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 876 | 100, NULL, NULL, &response); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 877 | if (status != PJ_SUCCESS) { |
Benny Prijono | ccb03fa | 2006-03-06 13:35:47 +0000 | [diff] [blame] | 878 | pjsua_perror(THIS_FILE, "Unable to send answer to incoming INVITE", |
| 879 | status); |
| 880 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 881 | pjsip_dlg_respond(dlg, rdata, 500, NULL, NULL, NULL); |
| 882 | pjsip_inv_terminate(inv, 500, PJ_FALSE); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 883 | pjsua_media_channel_deinit(call->index); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 884 | PJSUA_UNLOCK(); |
Benny Prijono | ccb03fa | 2006-03-06 13:35:47 +0000 | [diff] [blame] | 885 | return PJ_TRUE; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 886 | |
| 887 | } else { |
Benny Prijono | e8b0d3b | 2006-03-17 17:57:52 +0000 | [diff] [blame] | 888 | status = pjsip_inv_send_msg(inv, response); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 889 | if (status != PJ_SUCCESS) { |
Benny Prijono | d2ae29d | 2006-02-22 15:42:31 +0000 | [diff] [blame] | 890 | pjsua_perror(THIS_FILE, "Unable to send 100 response", status); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 891 | } |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 892 | } |
| 893 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 894 | ++pjsua_var.call_cnt; |
Benny Prijono | 64f851e | 2006-02-23 13:49:28 +0000 | [diff] [blame] | 895 | |
Benny Prijono | 105217f | 2006-03-06 16:25:59 +0000 | [diff] [blame] | 896 | |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 897 | /* Check if this request should replace existing call */ |
| 898 | if (replaced_dlg) { |
| 899 | pjsip_inv_session *replaced_inv; |
| 900 | struct pjsua_call *replaced_call; |
| 901 | pjsip_tx_data *tdata; |
| 902 | |
| 903 | /* Get the invite session in the dialog */ |
| 904 | replaced_inv = pjsip_dlg_get_inv_session(replaced_dlg); |
| 905 | |
| 906 | /* Get the replaced call instance */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 907 | replaced_call = (pjsua_call*) replaced_dlg->mod_data[pjsua_var.mod.id]; |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 908 | |
| 909 | /* Notify application */ |
| 910 | if (pjsua_var.ua_cfg.cb.on_call_replaced) |
| 911 | pjsua_var.ua_cfg.cb.on_call_replaced(replaced_call->index, |
| 912 | call_id); |
| 913 | |
| 914 | PJ_LOG(4,(THIS_FILE, "Answering replacement call %d with 200/OK", |
| 915 | call_id)); |
| 916 | |
| 917 | /* Answer the new call with 200 response */ |
| 918 | status = pjsip_inv_answer(inv, 200, NULL, NULL, &tdata); |
| 919 | if (status == PJ_SUCCESS) |
| 920 | status = pjsip_inv_send_msg(inv, tdata); |
| 921 | |
| 922 | if (status != PJ_SUCCESS) |
| 923 | pjsua_perror(THIS_FILE, "Error answering session", status); |
| 924 | |
Benny Prijono | fed7f4c | 2007-03-27 11:01:45 +0000 | [diff] [blame] | 925 | /* Note that inv may be invalid if 200/OK has caused error in |
| 926 | * starting the media. |
| 927 | */ |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 928 | |
| 929 | PJ_LOG(4,(THIS_FILE, "Disconnecting replaced call %d", |
| 930 | replaced_call->index)); |
| 931 | |
| 932 | /* Disconnect replaced invite session */ |
| 933 | status = pjsip_inv_end_session(replaced_inv, PJSIP_SC_GONE, NULL, |
| 934 | &tdata); |
| 935 | if (status == PJ_SUCCESS && tdata) |
| 936 | status = pjsip_inv_send_msg(replaced_inv, tdata); |
| 937 | |
| 938 | if (status != PJ_SUCCESS) |
| 939 | pjsua_perror(THIS_FILE, "Error terminating session", status); |
| 940 | |
| 941 | |
| 942 | } else { |
| 943 | |
Benny Prijono | b5388cf | 2007-01-04 22:45:08 +0000 | [diff] [blame] | 944 | /* Notify application if on_incoming_call() is overriden, |
| 945 | * otherwise hangup the call with 480 |
| 946 | */ |
| 947 | if (pjsua_var.ua_cfg.cb.on_incoming_call) { |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 948 | 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] | 949 | } else { |
| 950 | pjsua_call_hangup(call_id, PJSIP_SC_TEMPORARILY_UNAVAILABLE, |
| 951 | NULL, NULL); |
| 952 | } |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 953 | } |
| 954 | |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 955 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 956 | /* This INVITE request has been handled. */ |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 957 | PJSUA_UNLOCK(); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 958 | return PJ_TRUE; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 962 | |
| 963 | /* |
| 964 | * Check if the specified call has active INVITE session and the INVITE |
| 965 | * session has not been disconnected. |
| 966 | */ |
| 967 | PJ_DEF(pj_bool_t) pjsua_call_is_active(pjsua_call_id call_id) |
| 968 | { |
| 969 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 970 | PJ_EINVAL); |
| 971 | return pjsua_var.calls[call_id].inv != NULL && |
| 972 | pjsua_var.calls[call_id].inv->state != PJSIP_INV_STATE_DISCONNECTED; |
| 973 | } |
| 974 | |
| 975 | |
| 976 | /* |
| 977 | * Check if call has an active media session. |
| 978 | */ |
| 979 | PJ_DEF(pj_bool_t) pjsua_call_has_media(pjsua_call_id call_id) |
| 980 | { |
| 981 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 982 | PJ_EINVAL); |
| 983 | return pjsua_var.calls[call_id].session != NULL; |
| 984 | } |
| 985 | |
| 986 | |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 987 | /* Acquire lock to the specified call_id */ |
Benny Prijono | 627cbb4 | 2007-09-25 20:48:49 +0000 | [diff] [blame] | 988 | pj_status_t acquire_call(const char *title, |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 989 | pjsua_call_id call_id, |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 990 | pjsua_call **p_call, |
| 991 | pjsip_dialog **p_dlg) |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 992 | { |
| 993 | enum { MAX_RETRY=50 }; |
| 994 | unsigned retry; |
Benny Prijono | d524e82 | 2006-09-22 12:48:18 +0000 | [diff] [blame] | 995 | pjsua_call *call = NULL; |
| 996 | pj_bool_t has_pjsua_lock = PJ_FALSE; |
| 997 | pj_status_t status = PJ_SUCCESS; |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 998 | |
| 999 | for (retry=0; retry<MAX_RETRY; ++retry) { |
| 1000 | |
| 1001 | has_pjsua_lock = PJ_FALSE; |
| 1002 | |
| 1003 | status = PJSUA_TRY_LOCK(); |
| 1004 | if (status != PJ_SUCCESS) { |
| 1005 | pj_thread_sleep(retry/10); |
| 1006 | continue; |
| 1007 | } |
| 1008 | |
| 1009 | has_pjsua_lock = PJ_TRUE; |
| 1010 | call = &pjsua_var.calls[call_id]; |
| 1011 | |
| 1012 | if (call->inv == NULL) { |
| 1013 | PJSUA_UNLOCK(); |
| 1014 | PJ_LOG(3,(THIS_FILE, "Invalid call_id %d in %s", call_id, title)); |
| 1015 | return PJSIP_ESESSIONTERMINATED; |
| 1016 | } |
| 1017 | |
| 1018 | status = pjsip_dlg_try_inc_lock(call->inv->dlg); |
| 1019 | if (status != PJ_SUCCESS) { |
| 1020 | PJSUA_UNLOCK(); |
| 1021 | pj_thread_sleep(retry/10); |
| 1022 | continue; |
| 1023 | } |
| 1024 | |
| 1025 | PJSUA_UNLOCK(); |
| 1026 | |
| 1027 | break; |
| 1028 | } |
| 1029 | |
| 1030 | if (status != PJ_SUCCESS) { |
| 1031 | if (has_pjsua_lock == PJ_FALSE) |
| 1032 | PJ_LOG(1,(THIS_FILE, "Timed-out trying to acquire PJSUA mutex " |
| 1033 | "(possibly system has deadlocked) in %s", |
| 1034 | title)); |
| 1035 | else |
| 1036 | PJ_LOG(1,(THIS_FILE, "Timed-out trying to acquire dialog mutex " |
| 1037 | "(possibly system has deadlocked) in %s", |
| 1038 | title)); |
| 1039 | return PJ_ETIMEDOUT; |
| 1040 | } |
| 1041 | |
| 1042 | *p_call = call; |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1043 | *p_dlg = call->inv->dlg; |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1044 | |
| 1045 | return PJ_SUCCESS; |
| 1046 | } |
| 1047 | |
| 1048 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1049 | /* |
| 1050 | * Get the conference port identification associated with the call. |
| 1051 | */ |
| 1052 | PJ_DEF(pjsua_conf_port_id) pjsua_call_get_conf_port(pjsua_call_id call_id) |
| 1053 | { |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1054 | pjsua_call *call; |
| 1055 | pjsua_conf_port_id port_id; |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1056 | pjsip_dialog *dlg; |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1057 | pj_status_t status; |
| 1058 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1059 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1060 | PJ_EINVAL); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1061 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1062 | status = acquire_call("pjsua_call_get_conf_port()", call_id, &call, &dlg); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1063 | if (status != PJ_SUCCESS) |
Benny Prijono | d524e82 | 2006-09-22 12:48:18 +0000 | [diff] [blame] | 1064 | return PJSUA_INVALID_ID; |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1065 | |
| 1066 | port_id = call->conf_slot; |
| 1067 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1068 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1069 | |
| 1070 | return port_id; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1071 | } |
| 1072 | |
| 1073 | |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1074 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1075 | /* |
| 1076 | * Obtain detail information about the specified call. |
| 1077 | */ |
| 1078 | PJ_DEF(pj_status_t) pjsua_call_get_info( pjsua_call_id call_id, |
| 1079 | pjsua_call_info *info) |
| 1080 | { |
| 1081 | pjsua_call *call; |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1082 | pjsip_dialog *dlg; |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1083 | pj_status_t status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1084 | |
| 1085 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1086 | PJ_EINVAL); |
| 1087 | |
Benny Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 1088 | pj_bzero(info, sizeof(*info)); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1089 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1090 | status = acquire_call("pjsua_call_get_info()", call_id, &call, &dlg); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1091 | if (status != PJ_SUCCESS) { |
| 1092 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1093 | } |
| 1094 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1095 | /* id and role */ |
| 1096 | info->id = call_id; |
| 1097 | info->role = call->inv->role; |
Benny Prijono | 9031551 | 2006-09-14 16:05:16 +0000 | [diff] [blame] | 1098 | info->acc_id = call->acc_id; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1099 | |
| 1100 | /* local info */ |
| 1101 | info->local_info.ptr = info->buf_.local_info; |
| 1102 | pj_strncpy(&info->local_info, &call->inv->dlg->local.info_str, |
| 1103 | sizeof(info->buf_.local_info)); |
| 1104 | |
| 1105 | /* local contact */ |
| 1106 | info->local_contact.ptr = info->buf_.local_contact; |
| 1107 | info->local_contact.slen = pjsip_uri_print(PJSIP_URI_IN_CONTACT_HDR, |
| 1108 | call->inv->dlg->local.contact->uri, |
| 1109 | info->local_contact.ptr, |
| 1110 | sizeof(info->buf_.local_contact)); |
| 1111 | |
| 1112 | /* remote info */ |
| 1113 | info->remote_info.ptr = info->buf_.remote_info; |
| 1114 | pj_strncpy(&info->remote_info, &call->inv->dlg->remote.info_str, |
| 1115 | sizeof(info->buf_.remote_info)); |
| 1116 | |
| 1117 | /* remote contact */ |
| 1118 | if (call->inv->dlg->remote.contact) { |
| 1119 | int len; |
| 1120 | info->remote_contact.ptr = info->buf_.remote_contact; |
| 1121 | len = pjsip_uri_print(PJSIP_URI_IN_CONTACT_HDR, |
| 1122 | call->inv->dlg->remote.contact->uri, |
| 1123 | info->remote_contact.ptr, |
| 1124 | sizeof(info->buf_.remote_contact)); |
| 1125 | if (len < 0) len = 0; |
| 1126 | info->remote_contact.slen = len; |
| 1127 | } else { |
| 1128 | info->remote_contact.slen = 0; |
| 1129 | } |
| 1130 | |
| 1131 | /* call id */ |
| 1132 | info->call_id.ptr = info->buf_.call_id; |
| 1133 | pj_strncpy(&info->call_id, &call->inv->dlg->call_id->id, |
| 1134 | sizeof(info->buf_.call_id)); |
| 1135 | |
| 1136 | /* state, state_text */ |
| 1137 | info->state = call->inv->state; |
| 1138 | info->state_text = pj_str((char*)pjsip_inv_state_name(info->state)); |
| 1139 | |
| 1140 | /* If call is disconnected, set the last_status from the cause code */ |
| 1141 | if (call->inv->state >= PJSIP_INV_STATE_DISCONNECTED) { |
| 1142 | /* last_status, last_status_text */ |
| 1143 | info->last_status = call->inv->cause; |
| 1144 | |
| 1145 | info->last_status_text.ptr = info->buf_.last_status_text; |
| 1146 | pj_strncpy(&info->last_status_text, &call->inv->cause_text, |
| 1147 | sizeof(info->buf_.last_status_text)); |
| 1148 | } else { |
| 1149 | /* last_status, last_status_text */ |
| 1150 | info->last_status = call->last_code; |
| 1151 | |
| 1152 | info->last_status_text.ptr = info->buf_.last_status_text; |
| 1153 | pj_strncpy(&info->last_status_text, &call->last_text, |
| 1154 | sizeof(info->buf_.last_status_text)); |
| 1155 | } |
| 1156 | |
| 1157 | /* media status and dir */ |
| 1158 | info->media_status = call->media_st; |
| 1159 | info->media_dir = call->media_dir; |
| 1160 | |
| 1161 | |
| 1162 | /* conference slot number */ |
| 1163 | info->conf_slot = call->conf_slot; |
| 1164 | |
| 1165 | /* calculate duration */ |
| 1166 | if (info->state >= PJSIP_INV_STATE_DISCONNECTED) { |
| 1167 | |
| 1168 | info->total_duration = call->dis_time; |
| 1169 | PJ_TIME_VAL_SUB(info->total_duration, call->start_time); |
| 1170 | |
| 1171 | if (call->conn_time.sec) { |
| 1172 | info->connect_duration = call->dis_time; |
| 1173 | PJ_TIME_VAL_SUB(info->connect_duration, call->conn_time); |
| 1174 | } |
| 1175 | |
| 1176 | } else if (info->state == PJSIP_INV_STATE_CONFIRMED) { |
| 1177 | |
| 1178 | pj_gettimeofday(&info->total_duration); |
| 1179 | PJ_TIME_VAL_SUB(info->total_duration, call->start_time); |
| 1180 | |
| 1181 | pj_gettimeofday(&info->connect_duration); |
| 1182 | PJ_TIME_VAL_SUB(info->connect_duration, call->conn_time); |
| 1183 | |
| 1184 | } else { |
| 1185 | pj_gettimeofday(&info->total_duration); |
| 1186 | PJ_TIME_VAL_SUB(info->total_duration, call->start_time); |
| 1187 | } |
| 1188 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1189 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1190 | |
| 1191 | return PJ_SUCCESS; |
| 1192 | } |
| 1193 | |
| 1194 | |
| 1195 | /* |
| 1196 | * Attach application specific data to the call. |
| 1197 | */ |
| 1198 | PJ_DEF(pj_status_t) pjsua_call_set_user_data( pjsua_call_id call_id, |
| 1199 | void *user_data) |
| 1200 | { |
| 1201 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1202 | PJ_EINVAL); |
| 1203 | pjsua_var.calls[call_id].user_data = user_data; |
| 1204 | |
| 1205 | return PJ_SUCCESS; |
| 1206 | } |
| 1207 | |
| 1208 | |
| 1209 | /* |
| 1210 | * Get user data attached to the call. |
| 1211 | */ |
| 1212 | PJ_DEF(void*) pjsua_call_get_user_data(pjsua_call_id call_id) |
| 1213 | { |
| 1214 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1215 | NULL); |
| 1216 | return pjsua_var.calls[call_id].user_data; |
| 1217 | } |
| 1218 | |
| 1219 | |
| 1220 | /* |
Benny Prijono | 91a6a17 | 2007-10-31 08:59:29 +0000 | [diff] [blame] | 1221 | * Get remote's NAT type. |
| 1222 | */ |
| 1223 | PJ_DEF(pj_status_t) pjsua_call_get_rem_nat_type(pjsua_call_id call_id, |
| 1224 | pj_stun_nat_type *p_type) |
| 1225 | { |
| 1226 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1227 | PJ_EINVAL); |
| 1228 | PJ_ASSERT_RETURN(p_type != NULL, PJ_EINVAL); |
| 1229 | |
| 1230 | *p_type = pjsua_var.calls[call_id].rem_nat_type; |
| 1231 | return PJ_SUCCESS; |
| 1232 | } |
| 1233 | |
| 1234 | |
| 1235 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1236 | * Send response to incoming INVITE request. |
| 1237 | */ |
| 1238 | PJ_DEF(pj_status_t) pjsua_call_answer( pjsua_call_id call_id, |
| 1239 | unsigned code, |
| 1240 | const pj_str_t *reason, |
| 1241 | const pjsua_msg_data *msg_data) |
| 1242 | { |
| 1243 | pjsua_call *call; |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1244 | pjsip_dialog *dlg; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1245 | pjsip_tx_data *tdata; |
| 1246 | pj_status_t status; |
| 1247 | |
| 1248 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1249 | PJ_EINVAL); |
| 1250 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1251 | status = acquire_call("pjsua_call_answer()", call_id, &call, &dlg); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1252 | if (status != PJ_SUCCESS) |
| 1253 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1254 | |
Benny Prijono | 2e507c2 | 2006-06-23 15:04:11 +0000 | [diff] [blame] | 1255 | if (call->res_time.sec == 0) |
| 1256 | pj_gettimeofday(&call->res_time); |
| 1257 | |
Benny Prijono | ed7a5a7 | 2007-01-29 18:36:38 +0000 | [diff] [blame] | 1258 | if (reason && reason->slen == 0) |
| 1259 | reason = NULL; |
| 1260 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1261 | /* Create response message */ |
| 1262 | status = pjsip_inv_answer(call->inv, code, reason, NULL, &tdata); |
| 1263 | if (status != PJ_SUCCESS) { |
| 1264 | pjsua_perror(THIS_FILE, "Error creating response", |
| 1265 | status); |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1266 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1267 | return status; |
| 1268 | } |
| 1269 | |
Benny Prijono | fed7f4c | 2007-03-27 11:01:45 +0000 | [diff] [blame] | 1270 | /* Call might have been disconnected if application is answering with |
| 1271 | * 200/OK and the media failed to start. |
| 1272 | */ |
| 1273 | if (call->inv == NULL) { |
| 1274 | pjsip_dlg_dec_lock(dlg); |
| 1275 | return PJSIP_ESESSIONTERMINATED; |
| 1276 | } |
| 1277 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1278 | /* Add additional headers etc */ |
| 1279 | pjsua_process_msg_data( tdata, msg_data); |
| 1280 | |
| 1281 | /* Send the message */ |
| 1282 | status = pjsip_inv_send_msg(call->inv, tdata); |
| 1283 | if (status != PJ_SUCCESS) |
| 1284 | pjsua_perror(THIS_FILE, "Error sending response", |
| 1285 | status); |
| 1286 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1287 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1288 | |
| 1289 | return status; |
| 1290 | } |
| 1291 | |
| 1292 | |
| 1293 | /* |
| 1294 | * Hangup call by using method that is appropriate according to the |
| 1295 | * call state. |
| 1296 | */ |
| 1297 | PJ_DEF(pj_status_t) pjsua_call_hangup(pjsua_call_id call_id, |
| 1298 | unsigned code, |
| 1299 | const pj_str_t *reason, |
| 1300 | const pjsua_msg_data *msg_data) |
| 1301 | { |
| 1302 | pjsua_call *call; |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1303 | pjsip_dialog *dlg; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1304 | pj_status_t status; |
| 1305 | pjsip_tx_data *tdata; |
| 1306 | |
| 1307 | |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1308 | if (call_id<0 || call_id>=(int)pjsua_var.ua_cfg.max_calls) { |
| 1309 | PJ_LOG(1,(THIS_FILE, "pjsua_call_hangup(): invalid call id %d", |
| 1310 | call_id)); |
| 1311 | } |
| 1312 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1313 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1314 | PJ_EINVAL); |
| 1315 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1316 | status = acquire_call("pjsua_call_hangup()", call_id, &call, &dlg); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1317 | if (status != PJ_SUCCESS) |
| 1318 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1319 | |
| 1320 | if (code==0) { |
| 1321 | if (call->inv->state == PJSIP_INV_STATE_CONFIRMED) |
| 1322 | code = PJSIP_SC_OK; |
| 1323 | else if (call->inv->role == PJSIP_ROLE_UAS) |
| 1324 | code = PJSIP_SC_DECLINE; |
| 1325 | else |
| 1326 | code = PJSIP_SC_REQUEST_TERMINATED; |
| 1327 | } |
| 1328 | |
| 1329 | status = pjsip_inv_end_session(call->inv, code, reason, &tdata); |
| 1330 | if (status != PJ_SUCCESS) { |
| 1331 | pjsua_perror(THIS_FILE, |
| 1332 | "Failed to create end session message", |
| 1333 | status); |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1334 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1335 | return status; |
| 1336 | } |
| 1337 | |
| 1338 | /* pjsip_inv_end_session may return PJ_SUCCESS with NULL |
| 1339 | * as p_tdata when INVITE transaction has not been answered |
| 1340 | * with any provisional responses. |
| 1341 | */ |
| 1342 | if (tdata == NULL) { |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1343 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1344 | return PJ_SUCCESS; |
| 1345 | } |
| 1346 | |
| 1347 | /* Add additional headers etc */ |
| 1348 | pjsua_process_msg_data( tdata, msg_data); |
| 1349 | |
| 1350 | /* Send the message */ |
| 1351 | status = pjsip_inv_send_msg(call->inv, tdata); |
| 1352 | if (status != PJ_SUCCESS) { |
| 1353 | pjsua_perror(THIS_FILE, |
| 1354 | "Failed to send end session message", |
| 1355 | status); |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1356 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1357 | return status; |
| 1358 | } |
| 1359 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1360 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1361 | |
| 1362 | return PJ_SUCCESS; |
| 1363 | } |
| 1364 | |
| 1365 | |
| 1366 | /* |
| 1367 | * Put the specified call on hold. |
| 1368 | */ |
| 1369 | PJ_DEF(pj_status_t) pjsua_call_set_hold(pjsua_call_id call_id, |
| 1370 | const pjsua_msg_data *msg_data) |
| 1371 | { |
| 1372 | pjmedia_sdp_session *sdp; |
| 1373 | pjsua_call *call; |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1374 | pjsip_dialog *dlg; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1375 | pjsip_tx_data *tdata; |
| 1376 | pj_status_t status; |
| 1377 | |
| 1378 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1379 | PJ_EINVAL); |
| 1380 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1381 | status = acquire_call("pjsua_call_set_hold()", call_id, &call, &dlg); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1382 | if (status != PJ_SUCCESS) |
| 1383 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1384 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1385 | |
| 1386 | if (call->inv->state != PJSIP_INV_STATE_CONFIRMED) { |
| 1387 | 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] | 1388 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1389 | return PJSIP_ESESSIONSTATE; |
| 1390 | } |
| 1391 | |
| 1392 | status = create_inactive_sdp(call, &sdp); |
| 1393 | if (status != PJ_SUCCESS) { |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1394 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1395 | return status; |
| 1396 | } |
| 1397 | |
Benny Prijono | 7129cc7 | 2007-11-05 05:54:25 +0000 | [diff] [blame] | 1398 | update_sdp_version(call, sdp); |
| 1399 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1400 | /* Create re-INVITE with new offer */ |
| 1401 | status = pjsip_inv_reinvite( call->inv, NULL, sdp, &tdata); |
| 1402 | if (status != PJ_SUCCESS) { |
| 1403 | pjsua_perror(THIS_FILE, "Unable to create re-INVITE", status); |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1404 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1405 | return status; |
| 1406 | } |
| 1407 | |
| 1408 | /* Add additional headers etc */ |
| 1409 | pjsua_process_msg_data( tdata, msg_data); |
| 1410 | |
| 1411 | /* Send the request */ |
| 1412 | status = pjsip_inv_send_msg( call->inv, tdata); |
| 1413 | if (status != PJ_SUCCESS) { |
| 1414 | pjsua_perror(THIS_FILE, "Unable to send re-INVITE", status); |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1415 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1416 | return status; |
| 1417 | } |
| 1418 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1419 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1420 | |
| 1421 | return PJ_SUCCESS; |
| 1422 | } |
| 1423 | |
| 1424 | |
| 1425 | /* |
| 1426 | * Send re-INVITE (to release hold). |
| 1427 | */ |
| 1428 | PJ_DEF(pj_status_t) pjsua_call_reinvite( pjsua_call_id call_id, |
| 1429 | pj_bool_t unhold, |
| 1430 | const pjsua_msg_data *msg_data) |
| 1431 | { |
| 1432 | pjmedia_sdp_session *sdp; |
| 1433 | pjsip_tx_data *tdata; |
| 1434 | pjsua_call *call; |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1435 | pjsip_dialog *dlg; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1436 | pj_status_t status; |
| 1437 | |
| 1438 | |
| 1439 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1440 | PJ_EINVAL); |
| 1441 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1442 | status = acquire_call("pjsua_call_reinvite()", call_id, &call, &dlg); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1443 | if (status != PJ_SUCCESS) |
| 1444 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1445 | |
| 1446 | if (call->inv->state != PJSIP_INV_STATE_CONFIRMED) { |
| 1447 | 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] | 1448 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1449 | return PJSIP_ESESSIONSTATE; |
| 1450 | } |
| 1451 | |
Benny Prijono | db844a4 | 2008-02-02 17:07:18 +0000 | [diff] [blame] | 1452 | /* Update call secure level */ |
| 1453 | call->secure_level = call_get_secure_level(call); |
| 1454 | |
Benny Prijono | 667952e | 2007-04-02 19:27:54 +0000 | [diff] [blame] | 1455 | /* Init media channel */ |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 1456 | status = pjsua_media_channel_init(call->index, PJSIP_ROLE_UAC, |
Benny Prijono | db844a4 | 2008-02-02 17:07:18 +0000 | [diff] [blame] | 1457 | call->secure_level, NULL); |
Benny Prijono | 667952e | 2007-04-02 19:27:54 +0000 | [diff] [blame] | 1458 | if (status != PJ_SUCCESS) { |
| 1459 | pjsua_perror(THIS_FILE, "Error initializing media channel", status); |
| 1460 | pjsip_dlg_dec_lock(dlg); |
| 1461 | return PJSIP_ESESSIONSTATE; |
| 1462 | } |
| 1463 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1464 | /* Create SDP */ |
Benny Prijono | 00cae61 | 2006-07-31 15:19:36 +0000 | [diff] [blame] | 1465 | PJ_UNUSED_ARG(unhold); |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 1466 | PJ_TODO(create_active_inactive_sdp_based_on_unhold_arg); |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 1467 | status = pjsua_media_channel_create_sdp(call->index, call->inv->pool, |
Benny Prijono | 25b2ea1 | 2008-01-24 19:20:54 +0000 | [diff] [blame] | 1468 | NULL, &sdp, NULL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1469 | if (status != PJ_SUCCESS) { |
| 1470 | pjsua_perror(THIS_FILE, "Unable to get SDP from media endpoint", |
| 1471 | status); |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1472 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1473 | return status; |
| 1474 | } |
| 1475 | |
Benny Prijono | 7129cc7 | 2007-11-05 05:54:25 +0000 | [diff] [blame] | 1476 | update_sdp_version(call, sdp); |
| 1477 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1478 | /* Create re-INVITE with new offer */ |
| 1479 | status = pjsip_inv_reinvite( call->inv, NULL, sdp, &tdata); |
| 1480 | if (status != PJ_SUCCESS) { |
| 1481 | pjsua_perror(THIS_FILE, "Unable to create re-INVITE", status); |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1482 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1483 | return status; |
| 1484 | } |
| 1485 | |
| 1486 | /* Add additional headers etc */ |
| 1487 | pjsua_process_msg_data( tdata, msg_data); |
| 1488 | |
| 1489 | /* Send the request */ |
| 1490 | status = pjsip_inv_send_msg( call->inv, tdata); |
| 1491 | if (status != PJ_SUCCESS) { |
| 1492 | pjsua_perror(THIS_FILE, "Unable to send re-INVITE", status); |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1493 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1494 | return status; |
| 1495 | } |
| 1496 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1497 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1498 | |
| 1499 | return PJ_SUCCESS; |
| 1500 | } |
| 1501 | |
| 1502 | |
| 1503 | /* |
Benny Prijono | c08682e | 2007-10-04 06:17:58 +0000 | [diff] [blame] | 1504 | * Send UPDATE request. |
| 1505 | */ |
| 1506 | PJ_DEF(pj_status_t) pjsua_call_update( pjsua_call_id call_id, |
| 1507 | unsigned options, |
| 1508 | const pjsua_msg_data *msg_data) |
| 1509 | { |
| 1510 | pjmedia_sdp_session *sdp; |
| 1511 | pjsip_tx_data *tdata; |
| 1512 | pjsua_call *call; |
| 1513 | pjsip_dialog *dlg; |
| 1514 | pj_status_t status; |
| 1515 | |
| 1516 | PJ_UNUSED_ARG(options); |
| 1517 | |
| 1518 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1519 | PJ_EINVAL); |
| 1520 | |
| 1521 | status = acquire_call("pjsua_call_update()", call_id, &call, &dlg); |
| 1522 | if (status != PJ_SUCCESS) |
| 1523 | return status; |
| 1524 | |
Benny Prijono | db844a4 | 2008-02-02 17:07:18 +0000 | [diff] [blame] | 1525 | /* Update call's secure level */ |
| 1526 | call->secure_level = call_get_secure_level(call); |
| 1527 | |
Benny Prijono | c08682e | 2007-10-04 06:17:58 +0000 | [diff] [blame] | 1528 | /* Init media channel */ |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 1529 | status = pjsua_media_channel_init(call->index, PJSIP_ROLE_UAC, |
Benny Prijono | db844a4 | 2008-02-02 17:07:18 +0000 | [diff] [blame] | 1530 | call->secure_level, NULL); |
Benny Prijono | c08682e | 2007-10-04 06:17:58 +0000 | [diff] [blame] | 1531 | if (status != PJ_SUCCESS) { |
| 1532 | pjsua_perror(THIS_FILE, "Error initializing media channel", status); |
| 1533 | pjsip_dlg_dec_lock(dlg); |
| 1534 | return PJSIP_ESESSIONSTATE; |
| 1535 | } |
| 1536 | |
| 1537 | /* Create SDP */ |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 1538 | status = pjsua_media_channel_create_sdp(call->index, call->inv->pool, |
Benny Prijono | 25b2ea1 | 2008-01-24 19:20:54 +0000 | [diff] [blame] | 1539 | NULL, &sdp, NULL); |
Benny Prijono | c08682e | 2007-10-04 06:17:58 +0000 | [diff] [blame] | 1540 | if (status != PJ_SUCCESS) { |
| 1541 | pjsua_perror(THIS_FILE, "Unable to get SDP from media endpoint", |
| 1542 | status); |
| 1543 | pjsip_dlg_dec_lock(dlg); |
| 1544 | return status; |
| 1545 | } |
| 1546 | |
| 1547 | /* Create re-INVITE with new offer */ |
| 1548 | status = pjsip_inv_update(call->inv, NULL, sdp, &tdata); |
| 1549 | if (status != PJ_SUCCESS) { |
| 1550 | pjsua_perror(THIS_FILE, "Unable to create UPDATE request", status); |
| 1551 | pjsip_dlg_dec_lock(dlg); |
| 1552 | return status; |
| 1553 | } |
| 1554 | |
| 1555 | /* Add additional headers etc */ |
| 1556 | pjsua_process_msg_data( tdata, msg_data); |
| 1557 | |
| 1558 | /* Send the request */ |
| 1559 | status = pjsip_inv_send_msg( call->inv, tdata); |
| 1560 | if (status != PJ_SUCCESS) { |
| 1561 | pjsua_perror(THIS_FILE, "Unable to send UPDAT Erequest", status); |
| 1562 | pjsip_dlg_dec_lock(dlg); |
| 1563 | return status; |
| 1564 | } |
| 1565 | |
| 1566 | pjsip_dlg_dec_lock(dlg); |
| 1567 | |
| 1568 | return PJ_SUCCESS; |
| 1569 | } |
| 1570 | |
| 1571 | |
| 1572 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1573 | * Initiate call transfer to the specified address. |
| 1574 | */ |
| 1575 | PJ_DEF(pj_status_t) pjsua_call_xfer( pjsua_call_id call_id, |
| 1576 | const pj_str_t *dest, |
| 1577 | const pjsua_msg_data *msg_data) |
| 1578 | { |
| 1579 | pjsip_evsub *sub; |
| 1580 | pjsip_tx_data *tdata; |
| 1581 | pjsua_call *call; |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1582 | pjsip_dialog *dlg; |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 1583 | pjsip_generic_string_hdr *gs_hdr; |
| 1584 | const pj_str_t str_ref_by = { "Referred-By", 11 }; |
Benny Prijono | d524e82 | 2006-09-22 12:48:18 +0000 | [diff] [blame] | 1585 | struct pjsip_evsub_user xfer_cb; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1586 | pj_status_t status; |
| 1587 | |
| 1588 | |
| 1589 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1590 | PJ_EINVAL); |
| 1591 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1592 | status = acquire_call("pjsua_call_xfer()", call_id, &call, &dlg); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1593 | if (status != PJ_SUCCESS) |
| 1594 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1595 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1596 | |
Benny Prijono | d524e82 | 2006-09-22 12:48:18 +0000 | [diff] [blame] | 1597 | /* Create xfer client subscription. */ |
| 1598 | pj_bzero(&xfer_cb, sizeof(xfer_cb)); |
Benny Prijono | 4ddad2c | 2006-10-18 17:16:34 +0000 | [diff] [blame] | 1599 | xfer_cb.on_evsub_state = &xfer_client_on_evsub_state; |
Benny Prijono | d524e82 | 2006-09-22 12:48:18 +0000 | [diff] [blame] | 1600 | |
| 1601 | status = pjsip_xfer_create_uac(call->inv->dlg, &xfer_cb, &sub); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1602 | if (status != PJ_SUCCESS) { |
| 1603 | pjsua_perror(THIS_FILE, "Unable to create xfer", status); |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1604 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1605 | return status; |
| 1606 | } |
| 1607 | |
Benny Prijono | 4ddad2c | 2006-10-18 17:16:34 +0000 | [diff] [blame] | 1608 | /* Associate this call with the client subscription */ |
| 1609 | pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, call); |
| 1610 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1611 | /* |
| 1612 | * Create REFER request. |
| 1613 | */ |
| 1614 | status = pjsip_xfer_initiate(sub, dest, &tdata); |
| 1615 | if (status != PJ_SUCCESS) { |
| 1616 | pjsua_perror(THIS_FILE, "Unable to create REFER request", status); |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1617 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1618 | return status; |
| 1619 | } |
| 1620 | |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 1621 | /* Add Referred-By header */ |
| 1622 | gs_hdr = pjsip_generic_string_hdr_create(tdata->pool, &str_ref_by, |
| 1623 | &dlg->local.info_str); |
| 1624 | pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)gs_hdr); |
| 1625 | |
| 1626 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1627 | /* Add additional headers etc */ |
| 1628 | pjsua_process_msg_data( tdata, msg_data); |
| 1629 | |
| 1630 | /* Send. */ |
| 1631 | status = pjsip_xfer_send_request(sub, tdata); |
| 1632 | if (status != PJ_SUCCESS) { |
| 1633 | pjsua_perror(THIS_FILE, "Unable to send REFER request", status); |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1634 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1635 | return status; |
| 1636 | } |
| 1637 | |
| 1638 | /* For simplicity (that's what this program is intended to be!), |
| 1639 | * leave the original invite session as it is. More advanced application |
| 1640 | * may want to hold the INVITE, or terminate the invite, or whatever. |
| 1641 | */ |
| 1642 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1643 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1644 | |
| 1645 | return PJ_SUCCESS; |
| 1646 | |
| 1647 | } |
| 1648 | |
| 1649 | |
| 1650 | /* |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 1651 | * Initiate attended call transfer to the specified address. |
| 1652 | */ |
| 1653 | PJ_DEF(pj_status_t) pjsua_call_xfer_replaces( pjsua_call_id call_id, |
| 1654 | pjsua_call_id dest_call_id, |
| 1655 | unsigned options, |
| 1656 | const pjsua_msg_data *msg_data) |
| 1657 | { |
| 1658 | pjsua_call *dest_call; |
| 1659 | pjsip_dialog *dest_dlg; |
Benny Prijono | 38fb3ea | 2008-01-02 08:27:03 +0000 | [diff] [blame] | 1660 | char str_dest_buf[PJSIP_MAX_URL_SIZE*2]; |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 1661 | pj_str_t str_dest; |
| 1662 | int len; |
| 1663 | pjsip_uri *uri; |
| 1664 | pj_status_t status; |
| 1665 | |
| 1666 | |
| 1667 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1668 | PJ_EINVAL); |
| 1669 | PJ_ASSERT_RETURN(dest_call_id>=0 && |
| 1670 | dest_call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1671 | PJ_EINVAL); |
| 1672 | |
| 1673 | status = acquire_call("pjsua_call_xfer_replaces()", dest_call_id, |
| 1674 | &dest_call, &dest_dlg); |
| 1675 | if (status != PJ_SUCCESS) |
| 1676 | return status; |
| 1677 | |
| 1678 | /* |
| 1679 | * Create REFER destination URI with Replaces field. |
| 1680 | */ |
| 1681 | |
| 1682 | /* Make sure we have sufficient buffer's length */ |
| 1683 | PJ_ASSERT_RETURN( dest_dlg->remote.info_str.slen + |
| 1684 | dest_dlg->call_id->id.slen + |
| 1685 | dest_dlg->remote.info->tag.slen + |
| 1686 | dest_dlg->local.info->tag.slen + 32 |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1687 | < (long)sizeof(str_dest_buf), PJSIP_EURITOOLONG); |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 1688 | |
| 1689 | /* Print URI */ |
| 1690 | str_dest_buf[0] = '<'; |
| 1691 | str_dest.slen = 1; |
| 1692 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1693 | uri = (pjsip_uri*) pjsip_uri_get_uri(dest_dlg->remote.info->uri); |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 1694 | len = pjsip_uri_print(PJSIP_URI_IN_REQ_URI, uri, |
| 1695 | str_dest_buf+1, sizeof(str_dest_buf)-1); |
| 1696 | if (len < 0) |
| 1697 | return PJSIP_EURITOOLONG; |
| 1698 | |
| 1699 | str_dest.slen += len; |
| 1700 | |
| 1701 | |
| 1702 | /* Build the URI */ |
| 1703 | len = pj_ansi_snprintf(str_dest_buf + str_dest.slen, |
| 1704 | sizeof(str_dest_buf) - str_dest.slen, |
| 1705 | "?%s" |
| 1706 | "Replaces=%.*s" |
| 1707 | "%%3Bto-tag%%3D%.*s" |
| 1708 | "%%3Bfrom-tag%%3D%.*s>", |
| 1709 | ((options&PJSUA_XFER_NO_REQUIRE_REPLACES) ? |
| 1710 | "" : "Require=replaces&"), |
| 1711 | (int)dest_dlg->call_id->id.slen, |
| 1712 | dest_dlg->call_id->id.ptr, |
| 1713 | (int)dest_dlg->remote.info->tag.slen, |
| 1714 | dest_dlg->remote.info->tag.ptr, |
| 1715 | (int)dest_dlg->local.info->tag.slen, |
| 1716 | dest_dlg->local.info->tag.ptr); |
| 1717 | |
| 1718 | PJ_ASSERT_RETURN(len > 0 && len <= (int)sizeof(str_dest_buf)-str_dest.slen, |
| 1719 | PJSIP_EURITOOLONG); |
| 1720 | |
| 1721 | str_dest.ptr = str_dest_buf; |
| 1722 | str_dest.slen += len; |
| 1723 | |
| 1724 | pjsip_dlg_dec_lock(dest_dlg); |
| 1725 | |
| 1726 | return pjsua_call_xfer(call_id, &str_dest, msg_data); |
| 1727 | } |
| 1728 | |
| 1729 | |
| 1730 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1731 | * Send DTMF digits to remote using RFC 2833 payload formats. |
| 1732 | */ |
| 1733 | PJ_DEF(pj_status_t) pjsua_call_dial_dtmf( pjsua_call_id call_id, |
| 1734 | const pj_str_t *digits) |
| 1735 | { |
| 1736 | pjsua_call *call; |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1737 | pjsip_dialog *dlg; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1738 | pj_status_t status; |
| 1739 | |
| 1740 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1741 | PJ_EINVAL); |
| 1742 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1743 | status = acquire_call("pjsua_call_dial_dtmf()", call_id, &call, &dlg); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1744 | if (status != PJ_SUCCESS) |
| 1745 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1746 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1747 | if (!call->session) { |
| 1748 | PJ_LOG(3,(THIS_FILE, "Media is not established yet!")); |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1749 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1750 | return PJ_EINVALIDOP; |
| 1751 | } |
| 1752 | |
| 1753 | status = pjmedia_session_dial_dtmf( call->session, 0, digits); |
| 1754 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1755 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1756 | |
| 1757 | return status; |
| 1758 | } |
| 1759 | |
| 1760 | |
| 1761 | /** |
| 1762 | * Send instant messaging inside INVITE session. |
| 1763 | */ |
| 1764 | PJ_DEF(pj_status_t) pjsua_call_send_im( pjsua_call_id call_id, |
| 1765 | const pj_str_t *mime_type, |
| 1766 | const pj_str_t *content, |
| 1767 | const pjsua_msg_data *msg_data, |
| 1768 | void *user_data) |
| 1769 | { |
| 1770 | pjsua_call *call; |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1771 | pjsip_dialog *dlg; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1772 | const pj_str_t mime_text_plain = pj_str("text/plain"); |
| 1773 | pjsip_media_type ctype; |
| 1774 | pjsua_im_data *im_data; |
| 1775 | pjsip_tx_data *tdata; |
| 1776 | pj_status_t status; |
| 1777 | |
| 1778 | |
| 1779 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1780 | PJ_EINVAL); |
| 1781 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1782 | status = acquire_call("pjsua_call_send_im()", call_id, &call, &dlg); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1783 | if (status != PJ_SUCCESS) |
| 1784 | return status; |
| 1785 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1786 | /* Set default media type if none is specified */ |
| 1787 | if (mime_type == NULL) { |
| 1788 | mime_type = &mime_text_plain; |
| 1789 | } |
| 1790 | |
| 1791 | /* Create request message. */ |
| 1792 | status = pjsip_dlg_create_request( call->inv->dlg, &pjsip_message_method, |
| 1793 | -1, &tdata); |
| 1794 | if (status != PJ_SUCCESS) { |
| 1795 | pjsua_perror(THIS_FILE, "Unable to create MESSAGE request", status); |
| 1796 | goto on_return; |
| 1797 | } |
| 1798 | |
| 1799 | /* Add accept header. */ |
| 1800 | pjsip_msg_add_hdr( tdata->msg, |
| 1801 | (pjsip_hdr*)pjsua_im_create_accept(tdata->pool)); |
| 1802 | |
| 1803 | /* Parse MIME type */ |
| 1804 | pjsua_parse_media_type(tdata->pool, mime_type, &ctype); |
| 1805 | |
| 1806 | /* Create "text/plain" message body. */ |
| 1807 | tdata->msg->body = pjsip_msg_body_create( tdata->pool, &ctype.type, |
| 1808 | &ctype.subtype, content); |
| 1809 | if (tdata->msg->body == NULL) { |
| 1810 | pjsua_perror(THIS_FILE, "Unable to create msg body", PJ_ENOMEM); |
| 1811 | pjsip_tx_data_dec_ref(tdata); |
| 1812 | goto on_return; |
| 1813 | } |
| 1814 | |
| 1815 | /* Add additional headers etc */ |
| 1816 | pjsua_process_msg_data( tdata, msg_data); |
| 1817 | |
| 1818 | /* Create IM data and attach to the request. */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1819 | im_data = PJ_POOL_ZALLOC_T(tdata->pool, pjsua_im_data); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1820 | im_data->acc_id = call->acc_id; |
| 1821 | im_data->call_id = call_id; |
| 1822 | im_data->to = call->inv->dlg->remote.info_str; |
| 1823 | pj_strdup_with_null(tdata->pool, &im_data->body, content); |
| 1824 | im_data->user_data = user_data; |
| 1825 | |
| 1826 | |
| 1827 | /* Send the request. */ |
| 1828 | status = pjsip_dlg_send_request( call->inv->dlg, tdata, |
| 1829 | pjsua_var.mod.id, im_data); |
| 1830 | if (status != PJ_SUCCESS) { |
| 1831 | pjsua_perror(THIS_FILE, "Unable to send MESSAGE request", status); |
| 1832 | goto on_return; |
| 1833 | } |
| 1834 | |
| 1835 | on_return: |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1836 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1837 | return status; |
| 1838 | } |
| 1839 | |
| 1840 | |
| 1841 | /* |
| 1842 | * Send IM typing indication inside INVITE session. |
| 1843 | */ |
| 1844 | PJ_DEF(pj_status_t) pjsua_call_send_typing_ind( pjsua_call_id call_id, |
| 1845 | pj_bool_t is_typing, |
| 1846 | const pjsua_msg_data*msg_data) |
| 1847 | { |
| 1848 | pjsua_call *call; |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1849 | pjsip_dialog *dlg; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1850 | pjsip_tx_data *tdata; |
| 1851 | pj_status_t status; |
| 1852 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1853 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1854 | PJ_EINVAL); |
| 1855 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1856 | status = acquire_call("pjsua_call_send_typing_ind", call_id, &call, &dlg); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1857 | if (status != PJ_SUCCESS) |
| 1858 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1859 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1860 | /* Create request message. */ |
| 1861 | status = pjsip_dlg_create_request( call->inv->dlg, &pjsip_message_method, |
| 1862 | -1, &tdata); |
| 1863 | if (status != PJ_SUCCESS) { |
| 1864 | pjsua_perror(THIS_FILE, "Unable to create MESSAGE request", status); |
| 1865 | goto on_return; |
| 1866 | } |
| 1867 | |
| 1868 | /* Create "application/im-iscomposing+xml" msg body. */ |
| 1869 | tdata->msg->body = pjsip_iscomposing_create_body(tdata->pool, is_typing, |
| 1870 | NULL, NULL, -1); |
| 1871 | |
| 1872 | /* Add additional headers etc */ |
| 1873 | pjsua_process_msg_data( tdata, msg_data); |
| 1874 | |
| 1875 | /* Send the request. */ |
| 1876 | status = pjsip_dlg_send_request( call->inv->dlg, tdata, -1, NULL); |
| 1877 | if (status != PJ_SUCCESS) { |
| 1878 | pjsua_perror(THIS_FILE, "Unable to send MESSAGE request", status); |
| 1879 | goto on_return; |
| 1880 | } |
| 1881 | |
| 1882 | on_return: |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1883 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1884 | return status; |
| 1885 | } |
| 1886 | |
| 1887 | |
| 1888 | /* |
Benny Prijono | feb69f4 | 2007-10-05 09:12:26 +0000 | [diff] [blame] | 1889 | * Send arbitrary request. |
| 1890 | */ |
| 1891 | PJ_DEF(pj_status_t) pjsua_call_send_request(pjsua_call_id call_id, |
| 1892 | const pj_str_t *method_str, |
| 1893 | const pjsua_msg_data *msg_data) |
| 1894 | { |
| 1895 | pjsua_call *call; |
| 1896 | pjsip_dialog *dlg; |
| 1897 | pjsip_method method; |
| 1898 | pjsip_tx_data *tdata; |
| 1899 | pj_status_t status; |
| 1900 | |
| 1901 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1902 | PJ_EINVAL); |
| 1903 | |
| 1904 | status = acquire_call("pjsua_call_send_request", call_id, &call, &dlg); |
| 1905 | if (status != PJ_SUCCESS) |
| 1906 | return status; |
| 1907 | |
| 1908 | /* Init method */ |
| 1909 | pjsip_method_init_np(&method, (pj_str_t*)method_str); |
| 1910 | |
| 1911 | /* Create request message. */ |
| 1912 | status = pjsip_dlg_create_request( call->inv->dlg, &method, -1, &tdata); |
| 1913 | if (status != PJ_SUCCESS) { |
| 1914 | pjsua_perror(THIS_FILE, "Unable to create request", status); |
| 1915 | goto on_return; |
| 1916 | } |
| 1917 | |
| 1918 | /* Add additional headers etc */ |
| 1919 | pjsua_process_msg_data( tdata, msg_data); |
| 1920 | |
| 1921 | /* Send the request. */ |
| 1922 | status = pjsip_dlg_send_request( call->inv->dlg, tdata, -1, NULL); |
| 1923 | if (status != PJ_SUCCESS) { |
| 1924 | pjsua_perror(THIS_FILE, "Unable to send request", status); |
| 1925 | goto on_return; |
| 1926 | } |
| 1927 | |
| 1928 | on_return: |
| 1929 | pjsip_dlg_dec_lock(dlg); |
| 1930 | return status; |
| 1931 | } |
| 1932 | |
| 1933 | |
| 1934 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1935 | * Terminate all calls. |
| 1936 | */ |
| 1937 | PJ_DEF(void) pjsua_call_hangup_all(void) |
| 1938 | { |
| 1939 | unsigned i; |
| 1940 | |
| 1941 | PJSUA_LOCK(); |
| 1942 | |
| 1943 | for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) { |
| 1944 | if (pjsua_var.calls[i].inv) |
| 1945 | pjsua_call_hangup(i, 0, NULL, NULL); |
| 1946 | } |
| 1947 | |
| 1948 | PJSUA_UNLOCK(); |
| 1949 | } |
| 1950 | |
| 1951 | |
Benny Prijono | 627cbb4 | 2007-09-25 20:48:49 +0000 | [diff] [blame] | 1952 | const char *good_number(char *buf, pj_int32_t val) |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1953 | { |
| 1954 | if (val < 1000) { |
| 1955 | pj_ansi_sprintf(buf, "%d", val); |
| 1956 | } else if (val < 1000000) { |
| 1957 | pj_ansi_sprintf(buf, "%d.%dK", |
| 1958 | val / 1000, |
| 1959 | (val % 1000) / 100); |
| 1960 | } else { |
| 1961 | pj_ansi_sprintf(buf, "%d.%02dM", |
| 1962 | val / 1000000, |
| 1963 | (val % 1000000) / 10000); |
| 1964 | } |
| 1965 | |
| 1966 | return buf; |
| 1967 | } |
| 1968 | |
| 1969 | |
| 1970 | /* Dump media session */ |
| 1971 | static void dump_media_session(const char *indent, |
| 1972 | char *buf, unsigned maxlen, |
| 1973 | pjmedia_session *session) |
| 1974 | { |
| 1975 | unsigned i; |
| 1976 | char *p = buf, *end = buf+maxlen; |
| 1977 | int len; |
| 1978 | pjmedia_session_info info; |
| 1979 | |
| 1980 | pjmedia_session_get_info(session, &info); |
| 1981 | |
| 1982 | for (i=0; i<info.stream_cnt; ++i) { |
| 1983 | pjmedia_rtcp_stat stat; |
Benny Prijono | 5186eae | 2007-12-03 14:38:25 +0000 | [diff] [blame] | 1984 | char rem_addr_buf[80]; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1985 | const char *rem_addr; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1986 | const char *dir; |
Benny Prijono | e85bc41 | 2006-07-29 20:29:24 +0000 | [diff] [blame] | 1987 | char last_update[64]; |
Benny Prijono | 427af7d | 2007-05-11 10:36:40 +0000 | [diff] [blame] | 1988 | 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] | 1989 | pj_time_val media_duration, now; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1990 | |
| 1991 | pjmedia_session_get_stream_stat(session, i, &stat); |
Benny Prijono | 5186eae | 2007-12-03 14:38:25 +0000 | [diff] [blame] | 1992 | rem_addr = pj_sockaddr_print(&info.stream_info[i].rem_addr, |
| 1993 | rem_addr_buf, sizeof(rem_addr_buf), 3); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1994 | |
| 1995 | if (info.stream_info[i].dir == PJMEDIA_DIR_ENCODING) |
| 1996 | dir = "sendonly"; |
| 1997 | else if (info.stream_info[i].dir == PJMEDIA_DIR_DECODING) |
| 1998 | dir = "recvonly"; |
| 1999 | else if (info.stream_info[i].dir == PJMEDIA_DIR_ENCODING_DECODING) |
| 2000 | dir = "sendrecv"; |
| 2001 | else |
| 2002 | dir = "inactive"; |
| 2003 | |
| 2004 | |
| 2005 | len = pj_ansi_snprintf(buf, end-p, |
Benny Prijono | 5186eae | 2007-12-03 14:38:25 +0000 | [diff] [blame] | 2006 | "%s #%d %.*s @%dKHz, %s, peer=%s", |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2007 | indent, i, |
Benny Prijono | 172cd73 | 2006-06-14 20:22:31 +0000 | [diff] [blame] | 2008 | (int)info.stream_info[i].fmt.encoding_name.slen, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2009 | info.stream_info[i].fmt.encoding_name.ptr, |
| 2010 | info.stream_info[i].fmt.clock_rate / 1000, |
| 2011 | dir, |
Benny Prijono | 5186eae | 2007-12-03 14:38:25 +0000 | [diff] [blame] | 2012 | rem_addr); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2013 | if (len < 1 || len > end-p) { |
| 2014 | *p = '\0'; |
| 2015 | return; |
| 2016 | } |
| 2017 | |
| 2018 | p += len; |
| 2019 | *p++ = '\n'; |
| 2020 | *p = '\0'; |
| 2021 | |
| 2022 | if (stat.rx.update_cnt == 0) |
| 2023 | strcpy(last_update, "never"); |
| 2024 | else { |
| 2025 | pj_gettimeofday(&now); |
| 2026 | PJ_TIME_VAL_SUB(now, stat.rx.update); |
| 2027 | sprintf(last_update, "%02ldh:%02ldm:%02ld.%03lds ago", |
| 2028 | now.sec / 3600, |
| 2029 | (now.sec % 3600) / 60, |
| 2030 | now.sec % 60, |
| 2031 | now.msec); |
| 2032 | } |
| 2033 | |
Benny Prijono | 80019eb | 2006-08-07 13:22:23 +0000 | [diff] [blame] | 2034 | pj_gettimeofday(&media_duration); |
| 2035 | PJ_TIME_VAL_SUB(media_duration, stat.start); |
| 2036 | if (PJ_TIME_VAL_MSEC(media_duration) == 0) |
| 2037 | media_duration.msec = 1; |
| 2038 | |
Benny Prijono | 1402a4a | 2008-01-08 23:41:22 +0000 | [diff] [blame] | 2039 | /* protect against division by zero */ |
| 2040 | if (stat.rx.pkt == 0) |
| 2041 | stat.rx.pkt = 1; |
| 2042 | if (stat.tx.pkt == 0) |
| 2043 | stat.tx.pkt = 1; |
| 2044 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2045 | len = pj_ansi_snprintf(p, end-p, |
| 2046 | "%s RX pt=%d, stat last update: %s\n" |
Benny Prijono | 427af7d | 2007-05-11 10:36:40 +0000 | [diff] [blame] | 2047 | "%s total %spkt %sB (%sB +IP hdr) @avg=%sbps/%sbps\n" |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2048 | "%s pkt loss=%d (%3.1f%%), dup=%d (%3.1f%%), reorder=%d (%3.1f%%)\n" |
| 2049 | "%s (msec) min avg max last\n" |
| 2050 | "%s loss period: %7.3f %7.3f %7.3f %7.3f\n" |
| 2051 | "%s jitter : %7.3f %7.3f %7.3f %7.3f%s", |
| 2052 | indent, info.stream_info[i].fmt.pt, |
| 2053 | last_update, |
| 2054 | indent, |
| 2055 | good_number(packets, stat.rx.pkt), |
| 2056 | good_number(bytes, stat.rx.bytes), |
Benny Prijono | 427af7d | 2007-05-11 10:36:40 +0000 | [diff] [blame] | 2057 | good_number(ipbytes, stat.rx.bytes + stat.rx.pkt * 40), |
Benny Prijono | e723b92 | 2008-01-09 22:11:24 +0000 | [diff] [blame] | 2058 | good_number(avg_bps, (pj_int32_t)((pj_int64_t)stat.rx.bytes * 8 * 1000 / PJ_TIME_VAL_MSEC(media_duration))), |
| 2059 | 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] | 2060 | indent, |
| 2061 | stat.rx.loss, |
Benny Prijono | f901651 | 2006-06-29 09:41:34 +0000 | [diff] [blame] | 2062 | stat.rx.loss * 100.0 / (stat.rx.pkt + stat.rx.loss), |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2063 | stat.rx.dup, |
Benny Prijono | b12106f | 2006-06-29 14:45:17 +0000 | [diff] [blame] | 2064 | stat.rx.dup * 100.0 / (stat.rx.pkt + stat.rx.loss), |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2065 | stat.rx.reorder, |
Benny Prijono | b12106f | 2006-06-29 14:45:17 +0000 | [diff] [blame] | 2066 | stat.rx.reorder * 100.0 / (stat.rx.pkt + stat.rx.loss), |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2067 | indent, indent, |
| 2068 | stat.rx.loss_period.min / 1000.0, |
| 2069 | stat.rx.loss_period.avg / 1000.0, |
| 2070 | stat.rx.loss_period.max / 1000.0, |
| 2071 | stat.rx.loss_period.last / 1000.0, |
| 2072 | indent, |
| 2073 | stat.rx.jitter.min / 1000.0, |
| 2074 | stat.rx.jitter.avg / 1000.0, |
| 2075 | stat.rx.jitter.max / 1000.0, |
| 2076 | stat.rx.jitter.last / 1000.0, |
| 2077 | "" |
| 2078 | ); |
| 2079 | |
| 2080 | if (len < 1 || len > end-p) { |
| 2081 | *p = '\0'; |
| 2082 | return; |
| 2083 | } |
| 2084 | |
| 2085 | p += len; |
| 2086 | *p++ = '\n'; |
| 2087 | *p = '\0'; |
| 2088 | |
| 2089 | if (stat.tx.update_cnt == 0) |
| 2090 | strcpy(last_update, "never"); |
| 2091 | else { |
| 2092 | pj_gettimeofday(&now); |
| 2093 | PJ_TIME_VAL_SUB(now, stat.tx.update); |
| 2094 | sprintf(last_update, "%02ldh:%02ldm:%02ld.%03lds ago", |
| 2095 | now.sec / 3600, |
| 2096 | (now.sec % 3600) / 60, |
| 2097 | now.sec % 60, |
| 2098 | now.msec); |
| 2099 | } |
| 2100 | |
| 2101 | len = pj_ansi_snprintf(p, end-p, |
| 2102 | "%s TX pt=%d, ptime=%dms, stat last update: %s\n" |
Benny Prijono | 427af7d | 2007-05-11 10:36:40 +0000 | [diff] [blame] | 2103 | "%s total %spkt %sB (%sB +IP hdr) @avg %sbps/%sbps\n" |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2104 | "%s pkt loss=%d (%3.1f%%), dup=%d (%3.1f%%), reorder=%d (%3.1f%%)\n" |
| 2105 | "%s (msec) min avg max last\n" |
| 2106 | "%s loss period: %7.3f %7.3f %7.3f %7.3f\n" |
| 2107 | "%s jitter : %7.3f %7.3f %7.3f %7.3f%s", |
| 2108 | indent, |
| 2109 | info.stream_info[i].tx_pt, |
| 2110 | info.stream_info[i].param->info.frm_ptime * |
| 2111 | info.stream_info[i].param->setting.frm_per_pkt, |
| 2112 | last_update, |
| 2113 | |
| 2114 | indent, |
| 2115 | good_number(packets, stat.tx.pkt), |
| 2116 | good_number(bytes, stat.tx.bytes), |
Benny Prijono | 427af7d | 2007-05-11 10:36:40 +0000 | [diff] [blame] | 2117 | good_number(ipbytes, stat.tx.bytes + stat.tx.pkt * 40), |
Benny Prijono | e723b92 | 2008-01-09 22:11:24 +0000 | [diff] [blame] | 2118 | good_number(avg_bps, (pj_int32_t)((pj_int64_t)stat.tx.bytes * 8 * 1000 / PJ_TIME_VAL_MSEC(media_duration))), |
| 2119 | 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] | 2120 | |
| 2121 | indent, |
| 2122 | stat.tx.loss, |
Benny Prijono | f901651 | 2006-06-29 09:41:34 +0000 | [diff] [blame] | 2123 | stat.tx.loss * 100.0 / (stat.tx.pkt + stat.tx.loss), |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2124 | stat.tx.dup, |
Benny Prijono | b12106f | 2006-06-29 14:45:17 +0000 | [diff] [blame] | 2125 | stat.tx.dup * 100.0 / (stat.tx.pkt + stat.tx.loss), |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2126 | stat.tx.reorder, |
Benny Prijono | b12106f | 2006-06-29 14:45:17 +0000 | [diff] [blame] | 2127 | stat.tx.reorder * 100.0 / (stat.tx.pkt + stat.tx.loss), |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2128 | |
| 2129 | indent, indent, |
| 2130 | stat.tx.loss_period.min / 1000.0, |
| 2131 | stat.tx.loss_period.avg / 1000.0, |
| 2132 | stat.tx.loss_period.max / 1000.0, |
| 2133 | stat.tx.loss_period.last / 1000.0, |
| 2134 | indent, |
| 2135 | stat.tx.jitter.min / 1000.0, |
| 2136 | stat.tx.jitter.avg / 1000.0, |
| 2137 | stat.tx.jitter.max / 1000.0, |
| 2138 | stat.tx.jitter.last / 1000.0, |
| 2139 | "" |
| 2140 | ); |
| 2141 | |
| 2142 | if (len < 1 || len > end-p) { |
| 2143 | *p = '\0'; |
| 2144 | return; |
| 2145 | } |
| 2146 | |
| 2147 | p += len; |
| 2148 | *p++ = '\n'; |
| 2149 | *p = '\0'; |
| 2150 | |
| 2151 | len = pj_ansi_snprintf(p, end-p, |
| 2152 | "%s RTT msec : %7.3f %7.3f %7.3f %7.3f", |
| 2153 | indent, |
| 2154 | stat.rtt.min / 1000.0, |
| 2155 | stat.rtt.avg / 1000.0, |
| 2156 | stat.rtt.max / 1000.0, |
| 2157 | stat.rtt.last / 1000.0 |
| 2158 | ); |
| 2159 | if (len < 1 || len > end-p) { |
| 2160 | *p = '\0'; |
| 2161 | return; |
| 2162 | } |
| 2163 | |
| 2164 | p += len; |
| 2165 | *p++ = '\n'; |
| 2166 | *p = '\0'; |
| 2167 | } |
| 2168 | } |
| 2169 | |
| 2170 | |
| 2171 | /* Print call info */ |
Benny Prijono | 627cbb4 | 2007-09-25 20:48:49 +0000 | [diff] [blame] | 2172 | void print_call(const char *title, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2173 | int call_id, |
| 2174 | char *buf, pj_size_t size) |
| 2175 | { |
| 2176 | int len; |
| 2177 | pjsip_inv_session *inv = pjsua_var.calls[call_id].inv; |
| 2178 | pjsip_dialog *dlg = inv->dlg; |
| 2179 | char userinfo[128]; |
| 2180 | |
| 2181 | /* Dump invite sesion info. */ |
| 2182 | |
| 2183 | len = pjsip_hdr_print_on(dlg->remote.info, userinfo, sizeof(userinfo)); |
| 2184 | if (len < 1) |
| 2185 | pj_ansi_strcpy(userinfo, "<--uri too long-->"); |
| 2186 | else |
| 2187 | userinfo[len] = '\0'; |
| 2188 | |
| 2189 | len = pj_ansi_snprintf(buf, size, "%s[%s] %s", |
| 2190 | title, |
| 2191 | pjsip_inv_state_name(inv->state), |
| 2192 | userinfo); |
| 2193 | if (len < 1 || len >= (int)size) { |
| 2194 | pj_ansi_strcpy(buf, "<--uri too long-->"); |
| 2195 | len = 18; |
| 2196 | } else |
| 2197 | buf[len] = '\0'; |
| 2198 | } |
| 2199 | |
| 2200 | |
| 2201 | /* |
| 2202 | * Dump call and media statistics to string. |
| 2203 | */ |
| 2204 | PJ_DEF(pj_status_t) pjsua_call_dump( pjsua_call_id call_id, |
| 2205 | pj_bool_t with_media, |
| 2206 | char *buffer, |
| 2207 | unsigned maxlen, |
| 2208 | const char *indent) |
| 2209 | { |
| 2210 | pjsua_call *call; |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 2211 | pjsip_dialog *dlg; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2212 | pj_time_val duration, res_delay, con_delay; |
| 2213 | char tmp[128]; |
| 2214 | char *p, *end; |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 2215 | pj_status_t status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2216 | int len; |
Benny Prijono | e1a5a85 | 2008-03-11 21:38:05 +0000 | [diff] [blame] | 2217 | pjmedia_transport_info tp_info; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2218 | |
| 2219 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 2220 | PJ_EINVAL); |
| 2221 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 2222 | status = acquire_call("pjsua_call_dump()", call_id, &call, &dlg); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 2223 | if (status != PJ_SUCCESS) |
| 2224 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2225 | |
| 2226 | *buffer = '\0'; |
| 2227 | p = buffer; |
| 2228 | end = buffer + maxlen; |
| 2229 | len = 0; |
| 2230 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2231 | print_call(indent, call_id, tmp, sizeof(tmp)); |
| 2232 | |
| 2233 | len = pj_ansi_strlen(tmp); |
| 2234 | pj_ansi_strcpy(buffer, tmp); |
| 2235 | |
| 2236 | p += len; |
| 2237 | *p++ = '\r'; |
| 2238 | *p++ = '\n'; |
| 2239 | |
| 2240 | /* Calculate call duration */ |
Benny Prijono | 4be63b5 | 2006-11-25 14:50:25 +0000 | [diff] [blame] | 2241 | if (call->conn_time.sec != 0) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2242 | pj_gettimeofday(&duration); |
| 2243 | PJ_TIME_VAL_SUB(duration, call->conn_time); |
| 2244 | con_delay = call->conn_time; |
| 2245 | PJ_TIME_VAL_SUB(con_delay, call->start_time); |
| 2246 | } else { |
| 2247 | duration.sec = duration.msec = 0; |
| 2248 | con_delay.sec = con_delay.msec = 0; |
| 2249 | } |
| 2250 | |
| 2251 | /* Calculate first response delay */ |
Benny Prijono | 4be63b5 | 2006-11-25 14:50:25 +0000 | [diff] [blame] | 2252 | if (call->res_time.sec != 0) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2253 | res_delay = call->res_time; |
| 2254 | PJ_TIME_VAL_SUB(res_delay, call->start_time); |
| 2255 | } else { |
| 2256 | res_delay.sec = res_delay.msec = 0; |
| 2257 | } |
| 2258 | |
| 2259 | /* Print duration */ |
| 2260 | len = pj_ansi_snprintf(p, end-p, |
| 2261 | "%s Call time: %02dh:%02dm:%02ds, " |
| 2262 | "1st res in %d ms, conn in %dms", |
| 2263 | indent, |
Benny Prijono | 172cd73 | 2006-06-14 20:22:31 +0000 | [diff] [blame] | 2264 | (int)(duration.sec / 3600), |
| 2265 | (int)((duration.sec % 3600)/60), |
| 2266 | (int)(duration.sec % 60), |
| 2267 | (int)PJ_TIME_VAL_MSEC(res_delay), |
| 2268 | (int)PJ_TIME_VAL_MSEC(con_delay)); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2269 | |
| 2270 | if (len > 0 && len < end-p) { |
| 2271 | p += len; |
| 2272 | *p++ = '\n'; |
| 2273 | *p = '\0'; |
| 2274 | } |
| 2275 | |
Benny Prijono | e1a5a85 | 2008-03-11 21:38:05 +0000 | [diff] [blame] | 2276 | /* Get SRTP status */ |
Benny Prijono | 734fc2d | 2008-03-17 16:05:35 +0000 | [diff] [blame] | 2277 | pjmedia_transport_info_init(&tp_info); |
Benny Prijono | e1a5a85 | 2008-03-11 21:38:05 +0000 | [diff] [blame] | 2278 | pjmedia_transport_get_info(call->med_tp, &tp_info); |
| 2279 | if (tp_info.specific_info_cnt > 0) { |
| 2280 | int i; |
| 2281 | for (i = 0; i < tp_info.specific_info_cnt; ++i) { |
| 2282 | if (tp_info.spc_info[i].type == PJMEDIA_TRANSPORT_TYPE_SRTP) |
| 2283 | { |
| 2284 | pjmedia_srtp_info *srtp_info = |
| 2285 | (pjmedia_srtp_info*) tp_info.spc_info[i].buffer; |
| 2286 | |
| 2287 | len = pj_ansi_snprintf(p, end-p, |
| 2288 | "%s SRTP status: %s Crypto-suite: %s", |
| 2289 | indent, |
| 2290 | (srtp_info->active?"Active":"Not active"), |
| 2291 | srtp_info->tx_policy.name.ptr); |
| 2292 | if (len > 0 && len < end-p) { |
| 2293 | p += len; |
| 2294 | *p++ = '\n'; |
| 2295 | *p = '\0'; |
| 2296 | } |
| 2297 | break; |
| 2298 | } |
| 2299 | } |
| 2300 | } |
| 2301 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2302 | /* Dump session statistics */ |
| 2303 | if (with_media && call->session) |
| 2304 | dump_media_session(indent, p, end-p, call->session); |
| 2305 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 2306 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2307 | |
| 2308 | return PJ_SUCCESS; |
| 2309 | } |
| 2310 | |
| 2311 | |
| 2312 | /* |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 2313 | * This callback receives notification from invite session when the |
| 2314 | * session state has changed. |
| 2315 | */ |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 2316 | static void pjsua_call_on_state_changed(pjsip_inv_session *inv, |
| 2317 | pjsip_event *e) |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 2318 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2319 | pjsua_call *call; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2320 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2321 | PJSUA_LOCK(); |
| 2322 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 2323 | call = (pjsua_call*) inv->dlg->mod_data[pjsua_var.mod.id]; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2324 | |
| 2325 | if (!call) { |
| 2326 | PJSUA_UNLOCK(); |
Benny Prijono | e21e784 | 2006-04-09 16:46:05 +0000 | [diff] [blame] | 2327 | return; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2328 | } |
| 2329 | |
Benny Prijono | e21e784 | 2006-04-09 16:46:05 +0000 | [diff] [blame] | 2330 | |
| 2331 | /* Get call times */ |
| 2332 | switch (inv->state) { |
| 2333 | case PJSIP_INV_STATE_EARLY: |
| 2334 | case PJSIP_INV_STATE_CONNECTING: |
| 2335 | if (call->res_time.sec == 0) |
| 2336 | pj_gettimeofday(&call->res_time); |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 2337 | call->last_code = (pjsip_status_code) |
| 2338 | e->body.tsx_state.tsx->status_code; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2339 | pj_strncpy(&call->last_text, |
| 2340 | &e->body.tsx_state.tsx->status_text, |
| 2341 | sizeof(call->last_text_buf_)); |
Benny Prijono | e21e784 | 2006-04-09 16:46:05 +0000 | [diff] [blame] | 2342 | break; |
| 2343 | case PJSIP_INV_STATE_CONFIRMED: |
| 2344 | pj_gettimeofday(&call->conn_time); |
| 2345 | break; |
| 2346 | case PJSIP_INV_STATE_DISCONNECTED: |
| 2347 | pj_gettimeofday(&call->dis_time); |
Benny Prijono | 2e507c2 | 2006-06-23 15:04:11 +0000 | [diff] [blame] | 2348 | if (call->res_time.sec == 0) |
| 2349 | pj_gettimeofday(&call->res_time); |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 2350 | if (e->body.tsx_state.tsx->status_code > call->last_code) { |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 2351 | call->last_code = (pjsip_status_code) |
| 2352 | e->body.tsx_state.tsx->status_code; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2353 | pj_strncpy(&call->last_text, |
| 2354 | &e->body.tsx_state.tsx->status_text, |
| 2355 | sizeof(call->last_text_buf_)); |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 2356 | } |
Benny Prijono | e21e784 | 2006-04-09 16:46:05 +0000 | [diff] [blame] | 2357 | break; |
Benny Prijono | 8befd9f | 2006-05-13 22:46:23 +0000 | [diff] [blame] | 2358 | default: |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 2359 | call->last_code = (pjsip_status_code) |
| 2360 | e->body.tsx_state.tsx->status_code; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2361 | pj_strncpy(&call->last_text, |
| 2362 | &e->body.tsx_state.tsx->status_text, |
| 2363 | sizeof(call->last_text_buf_)); |
Benny Prijono | 8befd9f | 2006-05-13 22:46:23 +0000 | [diff] [blame] | 2364 | break; |
Benny Prijono | e21e784 | 2006-04-09 16:46:05 +0000 | [diff] [blame] | 2365 | } |
| 2366 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2367 | /* If this is an outgoing INVITE that was created because of |
| 2368 | * REFER/transfer, send NOTIFY to transferer. |
| 2369 | */ |
Benny Prijono | e21e784 | 2006-04-09 16:46:05 +0000 | [diff] [blame] | 2370 | if (call->xfer_sub && e->type==PJSIP_EVENT_TSX_STATE) { |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2371 | int st_code = -1; |
| 2372 | pjsip_evsub_state ev_state = PJSIP_EVSUB_STATE_ACTIVE; |
| 2373 | |
| 2374 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 2375 | switch (call->inv->state) { |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2376 | case PJSIP_INV_STATE_NULL: |
| 2377 | case PJSIP_INV_STATE_CALLING: |
| 2378 | /* Do nothing */ |
| 2379 | break; |
| 2380 | |
| 2381 | case PJSIP_INV_STATE_EARLY: |
| 2382 | case PJSIP_INV_STATE_CONNECTING: |
| 2383 | st_code = e->body.tsx_state.tsx->status_code; |
| 2384 | ev_state = PJSIP_EVSUB_STATE_ACTIVE; |
| 2385 | break; |
| 2386 | |
| 2387 | case PJSIP_INV_STATE_CONFIRMED: |
| 2388 | /* When state is confirmed, send the final 200/OK and terminate |
| 2389 | * subscription. |
| 2390 | */ |
| 2391 | st_code = e->body.tsx_state.tsx->status_code; |
| 2392 | ev_state = PJSIP_EVSUB_STATE_TERMINATED; |
| 2393 | break; |
| 2394 | |
| 2395 | case PJSIP_INV_STATE_DISCONNECTED: |
| 2396 | st_code = e->body.tsx_state.tsx->status_code; |
| 2397 | ev_state = PJSIP_EVSUB_STATE_TERMINATED; |
| 2398 | break; |
Benny Prijono | 8befd9f | 2006-05-13 22:46:23 +0000 | [diff] [blame] | 2399 | |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 2400 | case PJSIP_INV_STATE_INCOMING: |
Benny Prijono | 8befd9f | 2006-05-13 22:46:23 +0000 | [diff] [blame] | 2401 | /* Nothing to do. Just to keep gcc from complaining about |
| 2402 | * unused enums. |
| 2403 | */ |
| 2404 | break; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2405 | } |
| 2406 | |
| 2407 | if (st_code != -1) { |
| 2408 | pjsip_tx_data *tdata; |
| 2409 | pj_status_t status; |
| 2410 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 2411 | status = pjsip_xfer_notify( call->xfer_sub, |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2412 | ev_state, st_code, |
| 2413 | NULL, &tdata); |
| 2414 | if (status != PJ_SUCCESS) { |
| 2415 | pjsua_perror(THIS_FILE, "Unable to create NOTIFY", status); |
| 2416 | } else { |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 2417 | status = pjsip_xfer_send_request(call->xfer_sub, tdata); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2418 | if (status != PJ_SUCCESS) { |
| 2419 | pjsua_perror(THIS_FILE, "Unable to send NOTIFY", status); |
| 2420 | } |
| 2421 | } |
| 2422 | } |
| 2423 | } |
| 2424 | |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 2425 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2426 | if (pjsua_var.ua_cfg.cb.on_call_state) |
| 2427 | (*pjsua_var.ua_cfg.cb.on_call_state)(call->index, e); |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 2428 | |
| 2429 | /* call->inv may be NULL now */ |
| 2430 | |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 2431 | /* Destroy media session when invite session is disconnected. */ |
| 2432 | if (inv->state == PJSIP_INV_STATE_DISCONNECTED) { |
Benny Prijono | 632ce71 | 2006-02-09 14:01:40 +0000 | [diff] [blame] | 2433 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 2434 | pj_assert(call != NULL); |
Benny Prijono | 632ce71 | 2006-02-09 14:01:40 +0000 | [diff] [blame] | 2435 | |
Benny Prijono | 275fd68 | 2006-03-22 11:59:11 +0000 | [diff] [blame] | 2436 | if (call) |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 2437 | pjsua_media_channel_deinit(call->index); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 2438 | |
Benny Prijono | 105217f | 2006-03-06 16:25:59 +0000 | [diff] [blame] | 2439 | /* Free call */ |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 2440 | call->inv = NULL; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2441 | --pjsua_var.call_cnt; |
Benny Prijono | 4be63b5 | 2006-11-25 14:50:25 +0000 | [diff] [blame] | 2442 | |
| 2443 | /* Reset call */ |
| 2444 | reset_call(call->index); |
| 2445 | |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 2446 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2447 | |
| 2448 | PJSUA_UNLOCK(); |
| 2449 | } |
| 2450 | |
| 2451 | /* |
| 2452 | * This callback is called by invite session framework when UAC session |
| 2453 | * has forked. |
| 2454 | */ |
| 2455 | static void pjsua_call_on_forked( pjsip_inv_session *inv, |
| 2456 | pjsip_event *e) |
| 2457 | { |
| 2458 | PJ_UNUSED_ARG(inv); |
| 2459 | PJ_UNUSED_ARG(e); |
| 2460 | |
| 2461 | PJ_TODO(HANDLE_FORKED_DIALOG); |
| 2462 | } |
| 2463 | |
| 2464 | |
| 2465 | /* |
Benny Prijono | a38ada0 | 2006-07-02 14:22:35 +0000 | [diff] [blame] | 2466 | * Disconnect call upon error. |
| 2467 | */ |
| 2468 | static void call_disconnect( pjsip_inv_session *inv, |
| 2469 | int code ) |
| 2470 | { |
Benny Prijono | 59b3aed | 2008-01-15 16:54:54 +0000 | [diff] [blame] | 2471 | pjsua_call *call; |
Benny Prijono | a38ada0 | 2006-07-02 14:22:35 +0000 | [diff] [blame] | 2472 | pjsip_tx_data *tdata; |
| 2473 | pj_status_t status; |
| 2474 | |
Benny Prijono | 59b3aed | 2008-01-15 16:54:54 +0000 | [diff] [blame] | 2475 | call = (pjsua_call*) inv->dlg->mod_data[pjsua_var.mod.id]; |
| 2476 | |
Benny Prijono | a38ada0 | 2006-07-02 14:22:35 +0000 | [diff] [blame] | 2477 | status = pjsip_inv_end_session(inv, code, NULL, &tdata); |
Benny Prijono | 59b3aed | 2008-01-15 16:54:54 +0000 | [diff] [blame] | 2478 | if (status != PJ_SUCCESS) |
| 2479 | return; |
| 2480 | |
| 2481 | /* Add SDP in 488 status */ |
Benny Prijono | 9963998 | 2008-03-07 14:39:52 +0000 | [diff] [blame] | 2482 | if (call && call->med_tp && tdata->msg->type==PJSIP_RESPONSE_MSG && |
| 2483 | code==PJSIP_SC_NOT_ACCEPTABLE_HERE) |
| 2484 | { |
Benny Prijono | 59b3aed | 2008-01-15 16:54:54 +0000 | [diff] [blame] | 2485 | pjmedia_sdp_session *local_sdp; |
Benny Prijono | e1a5a85 | 2008-03-11 21:38:05 +0000 | [diff] [blame] | 2486 | pjmedia_transport_info ti; |
Benny Prijono | 59b3aed | 2008-01-15 16:54:54 +0000 | [diff] [blame] | 2487 | |
Benny Prijono | 734fc2d | 2008-03-17 16:05:35 +0000 | [diff] [blame] | 2488 | pjmedia_transport_info_init(&ti); |
Benny Prijono | e1a5a85 | 2008-03-11 21:38:05 +0000 | [diff] [blame] | 2489 | pjmedia_transport_get_info(call->med_tp, &ti); |
Benny Prijono | 59b3aed | 2008-01-15 16:54:54 +0000 | [diff] [blame] | 2490 | status = pjmedia_endpt_create_sdp(pjsua_var.med_endpt, tdata->pool, |
Benny Prijono | e1a5a85 | 2008-03-11 21:38:05 +0000 | [diff] [blame] | 2491 | 1, &ti.sock_info, &local_sdp); |
Benny Prijono | 59b3aed | 2008-01-15 16:54:54 +0000 | [diff] [blame] | 2492 | if (status == PJ_SUCCESS) { |
| 2493 | pjsip_create_sdp_body(tdata->pool, local_sdp, |
| 2494 | &tdata->msg->body); |
| 2495 | } |
| 2496 | } |
| 2497 | |
| 2498 | pjsip_inv_send_msg(inv, tdata); |
Benny Prijono | a38ada0 | 2006-07-02 14:22:35 +0000 | [diff] [blame] | 2499 | } |
| 2500 | |
| 2501 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2502 | * Callback to be called when SDP offer/answer negotiation has just completed |
| 2503 | * in the session. This function will start/update media if negotiation |
| 2504 | * has succeeded. |
| 2505 | */ |
| 2506 | static void pjsua_call_on_media_update(pjsip_inv_session *inv, |
| 2507 | pj_status_t status) |
| 2508 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2509 | pjsua_call *call; |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 2510 | const pjmedia_sdp_session *c_local; |
| 2511 | pjmedia_sdp_session *local_sdp; |
Benny Prijono | dbce2cf | 2007-03-28 16:24:00 +0000 | [diff] [blame] | 2512 | const pjmedia_sdp_session *remote_sdp; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2513 | |
| 2514 | PJSUA_LOCK(); |
| 2515 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 2516 | call = (pjsua_call*) inv->dlg->mod_data[pjsua_var.mod.id]; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2517 | |
| 2518 | if (status != PJ_SUCCESS) { |
| 2519 | |
| 2520 | pjsua_perror(THIS_FILE, "SDP negotiation has failed", status); |
| 2521 | |
Benny Prijono | 1d9b9a4 | 2006-09-25 13:40:12 +0000 | [diff] [blame] | 2522 | /* Stop/destroy media, if any */ |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 2523 | pjsua_media_channel_deinit(call->index); |
Benny Prijono | 1d9b9a4 | 2006-09-25 13:40:12 +0000 | [diff] [blame] | 2524 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2525 | /* Disconnect call if we're not in the middle of initializing an |
| 2526 | * UAS dialog and if this is not a re-INVITE |
| 2527 | */ |
| 2528 | if (inv->state != PJSIP_INV_STATE_NULL && |
| 2529 | inv->state != PJSIP_INV_STATE_CONFIRMED) |
| 2530 | { |
Benny Prijono | 2dbed82 | 2008-02-21 10:08:27 +0000 | [diff] [blame] | 2531 | call_disconnect(inv, PJSIP_SC_UNSUPPORTED_MEDIA_TYPE); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2532 | } |
| 2533 | |
| 2534 | PJSUA_UNLOCK(); |
| 2535 | return; |
| 2536 | } |
| 2537 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2538 | |
| 2539 | /* Get local and remote SDP */ |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 2540 | status = pjmedia_sdp_neg_get_active_local(call->inv->neg, &c_local); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2541 | if (status != PJ_SUCCESS) { |
| 2542 | pjsua_perror(THIS_FILE, |
| 2543 | "Unable to retrieve currently active local SDP", |
| 2544 | status); |
| 2545 | //call_disconnect(inv, PJSIP_SC_UNSUPPORTED_MEDIA_TYPE); |
| 2546 | PJSUA_UNLOCK(); |
| 2547 | return; |
| 2548 | } |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 2549 | local_sdp = (pjmedia_sdp_session*) c_local; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2550 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2551 | status = pjmedia_sdp_neg_get_active_remote(call->inv->neg, &remote_sdp); |
| 2552 | if (status != PJ_SUCCESS) { |
| 2553 | pjsua_perror(THIS_FILE, |
| 2554 | "Unable to retrieve currently active remote SDP", |
| 2555 | status); |
| 2556 | //call_disconnect(inv, PJSIP_SC_UNSUPPORTED_MEDIA_TYPE); |
| 2557 | PJSUA_UNLOCK(); |
| 2558 | return; |
| 2559 | } |
| 2560 | |
Benny Prijono | 91a6a17 | 2007-10-31 08:59:29 +0000 | [diff] [blame] | 2561 | /* Update remote's NAT type */ |
| 2562 | if (pjsua_var.ua_cfg.nat_type_in_sdp) { |
| 2563 | update_remote_nat_type(call, remote_sdp); |
| 2564 | } |
| 2565 | |
| 2566 | /* Update media channel with the new SDP */ |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 2567 | status = pjsua_media_channel_update(call->index, local_sdp, remote_sdp); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2568 | if (status != PJ_SUCCESS) { |
| 2569 | pjsua_perror(THIS_FILE, "Unable to create media session", |
| 2570 | status); |
Benny Prijono | 59b3aed | 2008-01-15 16:54:54 +0000 | [diff] [blame] | 2571 | call_disconnect(inv, PJSIP_SC_NOT_ACCEPTABLE_HERE); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 2572 | pjsua_media_channel_deinit(call->index); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2573 | PJSUA_UNLOCK(); |
| 2574 | return; |
| 2575 | } |
| 2576 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2577 | |
| 2578 | /* Call application callback, if any */ |
| 2579 | if (pjsua_var.ua_cfg.cb.on_call_media_state) |
| 2580 | pjsua_var.ua_cfg.cb.on_call_media_state(call->index); |
| 2581 | |
| 2582 | |
| 2583 | PJSUA_UNLOCK(); |
| 2584 | } |
| 2585 | |
| 2586 | |
| 2587 | /* |
| 2588 | * Create inactive SDP for call hold. |
| 2589 | */ |
| 2590 | static pj_status_t create_inactive_sdp(pjsua_call *call, |
| 2591 | pjmedia_sdp_session **p_answer) |
| 2592 | { |
| 2593 | pj_status_t status; |
| 2594 | pjmedia_sdp_conn *conn; |
| 2595 | pjmedia_sdp_attr *attr; |
Benny Prijono | e1a5a85 | 2008-03-11 21:38:05 +0000 | [diff] [blame] | 2596 | pjmedia_transport_info tp_info; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2597 | pjmedia_sdp_session *sdp; |
| 2598 | |
Benny Prijono | 617c5bc | 2007-04-02 19:51:21 +0000 | [diff] [blame] | 2599 | /* Get media socket info */ |
Benny Prijono | 734fc2d | 2008-03-17 16:05:35 +0000 | [diff] [blame] | 2600 | pjmedia_transport_info_init(&tp_info); |
Benny Prijono | e1a5a85 | 2008-03-11 21:38:05 +0000 | [diff] [blame] | 2601 | pjmedia_transport_get_info(call->med_tp, &tp_info); |
Benny Prijono | 617c5bc | 2007-04-02 19:51:21 +0000 | [diff] [blame] | 2602 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2603 | /* Create new offer */ |
| 2604 | status = pjmedia_endpt_create_sdp(pjsua_var.med_endpt, pjsua_var.pool, 1, |
Benny Prijono | e1a5a85 | 2008-03-11 21:38:05 +0000 | [diff] [blame] | 2605 | &tp_info.sock_info, &sdp); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2606 | if (status != PJ_SUCCESS) { |
| 2607 | pjsua_perror(THIS_FILE, "Unable to create local SDP", status); |
| 2608 | return status; |
| 2609 | } |
| 2610 | |
| 2611 | /* Get SDP media connection line */ |
| 2612 | conn = sdp->media[0]->conn; |
| 2613 | if (!conn) |
| 2614 | conn = sdp->conn; |
| 2615 | |
| 2616 | /* Modify address */ |
| 2617 | conn->addr = pj_str("0.0.0.0"); |
| 2618 | |
| 2619 | /* Remove existing directions attributes */ |
| 2620 | pjmedia_sdp_media_remove_all_attr(sdp->media[0], "sendrecv"); |
| 2621 | pjmedia_sdp_media_remove_all_attr(sdp->media[0], "sendonly"); |
| 2622 | pjmedia_sdp_media_remove_all_attr(sdp->media[0], "recvonly"); |
| 2623 | pjmedia_sdp_media_remove_all_attr(sdp->media[0], "inactive"); |
| 2624 | |
| 2625 | /* Add inactive attribute */ |
| 2626 | attr = pjmedia_sdp_attr_create(pjsua_var.pool, "inactive", NULL); |
| 2627 | pjmedia_sdp_media_add_attr(sdp->media[0], attr); |
| 2628 | |
| 2629 | *p_answer = sdp; |
| 2630 | |
| 2631 | return status; |
| 2632 | } |
| 2633 | |
| 2634 | |
| 2635 | /* |
| 2636 | * Called when session received new offer. |
| 2637 | */ |
| 2638 | static void pjsua_call_on_rx_offer(pjsip_inv_session *inv, |
| 2639 | const pjmedia_sdp_session *offer) |
| 2640 | { |
| 2641 | const char *remote_state; |
| 2642 | pjsua_call *call; |
| 2643 | pjmedia_sdp_conn *conn; |
| 2644 | pjmedia_sdp_session *answer; |
| 2645 | pj_bool_t is_remote_active; |
| 2646 | pj_status_t status; |
| 2647 | |
| 2648 | PJSUA_LOCK(); |
| 2649 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 2650 | call = (pjsua_call*) inv->dlg->mod_data[pjsua_var.mod.id]; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2651 | |
| 2652 | /* |
| 2653 | * See if remote is offering active media (i.e. not on-hold) |
| 2654 | */ |
| 2655 | is_remote_active = PJ_TRUE; |
| 2656 | |
| 2657 | conn = offer->media[0]->conn; |
| 2658 | if (!conn) |
| 2659 | conn = offer->conn; |
| 2660 | |
| 2661 | if (pj_strcmp2(&conn->addr, "0.0.0.0")==0 || |
| 2662 | pj_strcmp2(&conn->addr, "0")==0) |
| 2663 | { |
| 2664 | is_remote_active = PJ_FALSE; |
| 2665 | |
| 2666 | } |
Benny Prijono | 8eb07bf | 2007-11-08 09:39:06 +0000 | [diff] [blame] | 2667 | else if (pjmedia_sdp_media_find_attr2(offer->media[0], "inactive", NULL) || |
| 2668 | pjmedia_sdp_media_find_attr2(offer->media[0], "sendonly", NULL)) |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2669 | { |
| 2670 | is_remote_active = PJ_FALSE; |
| 2671 | } |
| 2672 | |
| 2673 | remote_state = (is_remote_active ? "active" : "inactive"); |
| 2674 | |
| 2675 | /* Supply candidate answer */ |
| 2676 | if (call->media_st == PJSUA_CALL_MEDIA_LOCAL_HOLD || !is_remote_active) { |
| 2677 | PJ_LOG(4,(THIS_FILE, |
| 2678 | "Call %d: RX new media offer, creating inactive SDP " |
| 2679 | "(media in offer is %s)", call->index, remote_state)); |
| 2680 | status = create_inactive_sdp( call, &answer ); |
| 2681 | } else { |
| 2682 | PJ_LOG(4,(THIS_FILE, "Call %d: received updated media offer", |
| 2683 | call->index)); |
Benny Prijono | 667952e | 2007-04-02 19:27:54 +0000 | [diff] [blame] | 2684 | |
Benny Prijono | db844a4 | 2008-02-02 17:07:18 +0000 | [diff] [blame] | 2685 | /* Update call's secure level */ |
| 2686 | call->secure_level = call_get_secure_level(call); |
| 2687 | |
Benny Prijono | 667952e | 2007-04-02 19:27:54 +0000 | [diff] [blame] | 2688 | /* Init media channel */ |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 2689 | status = pjsua_media_channel_init(call->index, PJSIP_ROLE_UAS, |
Benny Prijono | db844a4 | 2008-02-02 17:07:18 +0000 | [diff] [blame] | 2690 | call->secure_level, NULL); |
Benny Prijono | 667952e | 2007-04-02 19:27:54 +0000 | [diff] [blame] | 2691 | if (status != PJ_SUCCESS) { |
| 2692 | pjsua_perror(THIS_FILE, "Error initializing media channel", status); |
| 2693 | PJSUA_UNLOCK(); |
| 2694 | return; |
| 2695 | } |
| 2696 | |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 2697 | status = pjsua_media_channel_create_sdp(call->index, call->inv->pool, |
Benny Prijono | 25b2ea1 | 2008-01-24 19:20:54 +0000 | [diff] [blame] | 2698 | offer, &answer, NULL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2699 | } |
| 2700 | |
| 2701 | if (status != PJ_SUCCESS) { |
| 2702 | pjsua_perror(THIS_FILE, "Unable to create local SDP", status); |
| 2703 | PJSUA_UNLOCK(); |
| 2704 | return; |
| 2705 | } |
| 2706 | |
| 2707 | status = pjsip_inv_set_sdp_answer(call->inv, answer); |
| 2708 | if (status != PJ_SUCCESS) { |
| 2709 | pjsua_perror(THIS_FILE, "Unable to set answer", status); |
| 2710 | PJSUA_UNLOCK(); |
| 2711 | return; |
| 2712 | } |
| 2713 | |
| 2714 | PJSUA_UNLOCK(); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 2715 | } |
| 2716 | |
| 2717 | |
| 2718 | /* |
Benny Prijono | 77998ce | 2007-06-20 10:03:46 +0000 | [diff] [blame] | 2719 | * Called to generate new offer. |
| 2720 | */ |
| 2721 | static void pjsua_call_on_create_offer(pjsip_inv_session *inv, |
| 2722 | pjmedia_sdp_session **offer) |
| 2723 | { |
| 2724 | pjsua_call *call; |
| 2725 | pj_status_t status; |
| 2726 | |
| 2727 | PJSUA_LOCK(); |
| 2728 | |
| 2729 | call = (pjsua_call*) inv->dlg->mod_data[pjsua_var.mod.id]; |
| 2730 | |
| 2731 | /* See if we've put call on hold. */ |
| 2732 | if (call->media_st == PJSUA_CALL_MEDIA_LOCAL_HOLD) { |
| 2733 | PJ_LOG(4,(THIS_FILE, |
| 2734 | "Call %d: call is on-hold locally, creating inactive SDP ", |
| 2735 | call->index)); |
| 2736 | status = create_inactive_sdp( call, offer ); |
| 2737 | } else { |
Benny Prijono | 77998ce | 2007-06-20 10:03:46 +0000 | [diff] [blame] | 2738 | PJ_LOG(4,(THIS_FILE, "Call %d: asked to send a new offer", |
| 2739 | call->index)); |
| 2740 | |
Benny Prijono | db844a4 | 2008-02-02 17:07:18 +0000 | [diff] [blame] | 2741 | /* Update call's secure level */ |
| 2742 | call->secure_level = call_get_secure_level(call); |
| 2743 | |
Benny Prijono | 77998ce | 2007-06-20 10:03:46 +0000 | [diff] [blame] | 2744 | /* Init media channel */ |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 2745 | status = pjsua_media_channel_init(call->index, PJSIP_ROLE_UAC, |
Benny Prijono | db844a4 | 2008-02-02 17:07:18 +0000 | [diff] [blame] | 2746 | call->secure_level, NULL); |
Benny Prijono | 77998ce | 2007-06-20 10:03:46 +0000 | [diff] [blame] | 2747 | if (status != PJ_SUCCESS) { |
| 2748 | pjsua_perror(THIS_FILE, "Error initializing media channel", status); |
| 2749 | PJSUA_UNLOCK(); |
| 2750 | return; |
| 2751 | } |
| 2752 | |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 2753 | status = pjsua_media_channel_create_sdp(call->index, call->inv->pool, |
Benny Prijono | 25b2ea1 | 2008-01-24 19:20:54 +0000 | [diff] [blame] | 2754 | NULL, offer, NULL); |
Benny Prijono | 77998ce | 2007-06-20 10:03:46 +0000 | [diff] [blame] | 2755 | } |
| 2756 | |
| 2757 | if (status != PJ_SUCCESS) { |
| 2758 | pjsua_perror(THIS_FILE, "Unable to create local SDP", status); |
| 2759 | PJSUA_UNLOCK(); |
| 2760 | return; |
| 2761 | } |
| 2762 | |
Benny Prijono | 7129cc7 | 2007-11-05 05:54:25 +0000 | [diff] [blame] | 2763 | update_sdp_version(call, *offer); |
Benny Prijono | 77998ce | 2007-06-20 10:03:46 +0000 | [diff] [blame] | 2764 | |
| 2765 | PJSUA_UNLOCK(); |
| 2766 | } |
| 2767 | |
| 2768 | |
| 2769 | /* |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2770 | * Callback called by event framework when the xfer subscription state |
| 2771 | * has changed. |
| 2772 | */ |
Benny Prijono | 4ddad2c | 2006-10-18 17:16:34 +0000 | [diff] [blame] | 2773 | static void xfer_client_on_evsub_state( pjsip_evsub *sub, pjsip_event *event) |
| 2774 | { |
| 2775 | |
| 2776 | PJ_UNUSED_ARG(event); |
| 2777 | |
| 2778 | /* |
| 2779 | * When subscription is accepted (got 200/OK to REFER), check if |
| 2780 | * subscription suppressed. |
| 2781 | */ |
| 2782 | if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_ACCEPTED) { |
| 2783 | |
| 2784 | pjsip_rx_data *rdata; |
| 2785 | pjsip_generic_string_hdr *refer_sub; |
| 2786 | const pj_str_t REFER_SUB = { "Refer-Sub", 9 }; |
| 2787 | pjsua_call *call; |
| 2788 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 2789 | 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] | 2790 | |
| 2791 | /* Must be receipt of response message */ |
| 2792 | pj_assert(event->type == PJSIP_EVENT_TSX_STATE && |
| 2793 | event->body.tsx_state.type == PJSIP_EVENT_RX_MSG); |
| 2794 | rdata = event->body.tsx_state.src.rdata; |
| 2795 | |
| 2796 | /* Find Refer-Sub header */ |
| 2797 | refer_sub = (pjsip_generic_string_hdr*) |
| 2798 | pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, |
| 2799 | &REFER_SUB, NULL); |
| 2800 | |
| 2801 | /* Check if subscription is suppressed */ |
| 2802 | if (refer_sub && pj_stricmp2(&refer_sub->hvalue, "false")==0) { |
| 2803 | /* Since no subscription is desired, assume that call has been |
| 2804 | * transfered successfully. |
| 2805 | */ |
| 2806 | if (call && pjsua_var.ua_cfg.cb.on_call_transfer_status) { |
| 2807 | const pj_str_t ACCEPTED = { "Accepted", 8 }; |
| 2808 | pj_bool_t cont = PJ_FALSE; |
| 2809 | (*pjsua_var.ua_cfg.cb.on_call_transfer_status)(call->index, |
| 2810 | 200, |
| 2811 | &ACCEPTED, |
| 2812 | PJ_TRUE, |
| 2813 | &cont); |
| 2814 | } |
| 2815 | |
| 2816 | /* Yes, subscription is suppressed. |
| 2817 | * Terminate our subscription now. |
| 2818 | */ |
| 2819 | PJ_LOG(4,(THIS_FILE, "Xfer subscription suppressed, terminating " |
| 2820 | "event subcription...")); |
| 2821 | pjsip_evsub_terminate(sub, PJ_TRUE); |
| 2822 | |
| 2823 | } else { |
| 2824 | /* Notify application about call transfer progress. |
| 2825 | * Initially notify with 100/Accepted status. |
| 2826 | */ |
| 2827 | if (call && pjsua_var.ua_cfg.cb.on_call_transfer_status) { |
| 2828 | const pj_str_t ACCEPTED = { "Accepted", 8 }; |
| 2829 | pj_bool_t cont = PJ_FALSE; |
| 2830 | (*pjsua_var.ua_cfg.cb.on_call_transfer_status)(call->index, |
| 2831 | 100, |
| 2832 | &ACCEPTED, |
| 2833 | PJ_FALSE, |
| 2834 | &cont); |
| 2835 | } |
| 2836 | } |
| 2837 | } |
| 2838 | /* |
| 2839 | * On incoming NOTIFY, notify application about call transfer progress. |
| 2840 | */ |
| 2841 | else if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_ACTIVE || |
| 2842 | pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED) |
| 2843 | { |
| 2844 | pjsua_call *call; |
| 2845 | pjsip_msg *msg; |
| 2846 | pjsip_msg_body *body; |
| 2847 | pjsip_status_line status_line; |
| 2848 | pj_bool_t is_last; |
| 2849 | pj_bool_t cont; |
| 2850 | pj_status_t status; |
| 2851 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 2852 | 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] | 2853 | |
| 2854 | /* When subscription is terminated, clear the xfer_sub member of |
| 2855 | * the inv_data. |
| 2856 | */ |
| 2857 | if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED) { |
| 2858 | pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, NULL); |
| 2859 | PJ_LOG(4,(THIS_FILE, "Xfer client subscription terminated")); |
| 2860 | |
| 2861 | } |
| 2862 | |
| 2863 | if (!call || !event || !pjsua_var.ua_cfg.cb.on_call_transfer_status) { |
| 2864 | /* Application is not interested with call progress status */ |
| 2865 | return; |
| 2866 | } |
| 2867 | |
| 2868 | /* This better be a NOTIFY request */ |
| 2869 | if (event->type == PJSIP_EVENT_TSX_STATE && |
| 2870 | event->body.tsx_state.type == PJSIP_EVENT_RX_MSG) |
| 2871 | { |
| 2872 | pjsip_rx_data *rdata; |
| 2873 | |
| 2874 | rdata = event->body.tsx_state.src.rdata; |
| 2875 | |
| 2876 | /* Check if there's body */ |
| 2877 | msg = rdata->msg_info.msg; |
| 2878 | body = msg->body; |
| 2879 | if (!body) { |
| 2880 | PJ_LOG(4,(THIS_FILE, |
| 2881 | "Warning: received NOTIFY without message body")); |
| 2882 | return; |
| 2883 | } |
| 2884 | |
| 2885 | /* Check for appropriate content */ |
| 2886 | if (pj_stricmp2(&body->content_type.type, "message") != 0 || |
| 2887 | pj_stricmp2(&body->content_type.subtype, "sipfrag") != 0) |
| 2888 | { |
| 2889 | PJ_LOG(4,(THIS_FILE, |
| 2890 | "Warning: received NOTIFY with non message/sipfrag " |
| 2891 | "content")); |
| 2892 | return; |
| 2893 | } |
| 2894 | |
| 2895 | /* Try to parse the content */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 2896 | status = pjsip_parse_status_line((char*)body->data, body->len, |
Benny Prijono | 4ddad2c | 2006-10-18 17:16:34 +0000 | [diff] [blame] | 2897 | &status_line); |
| 2898 | if (status != PJ_SUCCESS) { |
| 2899 | PJ_LOG(4,(THIS_FILE, |
| 2900 | "Warning: received NOTIFY with invalid " |
| 2901 | "message/sipfrag content")); |
| 2902 | return; |
| 2903 | } |
| 2904 | |
| 2905 | } else { |
| 2906 | status_line.code = 500; |
| 2907 | status_line.reason = *pjsip_get_status_text(500); |
| 2908 | } |
| 2909 | |
| 2910 | /* Notify application */ |
| 2911 | is_last = (pjsip_evsub_get_state(sub)==PJSIP_EVSUB_STATE_TERMINATED); |
| 2912 | cont = !is_last; |
| 2913 | (*pjsua_var.ua_cfg.cb.on_call_transfer_status)(call->index, |
| 2914 | status_line.code, |
| 2915 | &status_line.reason, |
| 2916 | is_last, &cont); |
| 2917 | |
| 2918 | if (!cont) { |
| 2919 | pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, NULL); |
| 2920 | } |
| 2921 | } |
| 2922 | } |
| 2923 | |
| 2924 | |
| 2925 | /* |
| 2926 | * Callback called by event framework when the xfer subscription state |
| 2927 | * has changed. |
| 2928 | */ |
| 2929 | 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] | 2930 | { |
| 2931 | |
| 2932 | PJ_UNUSED_ARG(event); |
| 2933 | |
| 2934 | /* |
Benny Prijono | d524e82 | 2006-09-22 12:48:18 +0000 | [diff] [blame] | 2935 | * When subscription is terminated, clear the xfer_sub member of |
| 2936 | * the inv_data. |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2937 | */ |
| 2938 | if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED) { |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 2939 | pjsua_call *call; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2940 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 2941 | 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] | 2942 | if (!call) |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2943 | return; |
| 2944 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2945 | pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, NULL); |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 2946 | call->xfer_sub = NULL; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2947 | |
Benny Prijono | 4ddad2c | 2006-10-18 17:16:34 +0000 | [diff] [blame] | 2948 | PJ_LOG(4,(THIS_FILE, "Xfer server subscription terminated")); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2949 | } |
| 2950 | } |
| 2951 | |
| 2952 | |
| 2953 | /* |
| 2954 | * Follow transfer (REFER) request. |
| 2955 | */ |
| 2956 | static void on_call_transfered( pjsip_inv_session *inv, |
| 2957 | pjsip_rx_data *rdata ) |
| 2958 | { |
| 2959 | pj_status_t status; |
| 2960 | pjsip_tx_data *tdata; |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 2961 | pjsua_call *existing_call; |
| 2962 | int new_call; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2963 | const pj_str_t str_refer_to = { "Refer-To", 8}; |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 2964 | const pj_str_t str_refer_sub = { "Refer-Sub", 9 }; |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 2965 | const pj_str_t str_ref_by = { "Referred-By", 11 }; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2966 | pjsip_generic_string_hdr *refer_to; |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 2967 | pjsip_generic_string_hdr *refer_sub; |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 2968 | pjsip_hdr *ref_by_hdr; |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 2969 | pj_bool_t no_refer_sub = PJ_FALSE; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2970 | char *uri; |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 2971 | pjsua_msg_data msg_data; |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 2972 | pj_str_t tmp; |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 2973 | pjsip_status_code code; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2974 | pjsip_evsub *sub; |
| 2975 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 2976 | existing_call = (pjsua_call*) inv->dlg->mod_data[pjsua_var.mod.id]; |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 2977 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2978 | /* Find the Refer-To header */ |
| 2979 | refer_to = (pjsip_generic_string_hdr*) |
| 2980 | pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_refer_to, NULL); |
| 2981 | |
| 2982 | if (refer_to == NULL) { |
| 2983 | /* Invalid Request. |
| 2984 | * No Refer-To header! |
| 2985 | */ |
| 2986 | PJ_LOG(4,(THIS_FILE, "Received REFER without Refer-To header!")); |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 2987 | pjsip_dlg_respond( inv->dlg, rdata, 400, NULL, NULL, NULL); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 2988 | return; |
| 2989 | } |
| 2990 | |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 2991 | /* Find optional Refer-Sub header */ |
| 2992 | refer_sub = (pjsip_generic_string_hdr*) |
| 2993 | pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_refer_sub, NULL); |
| 2994 | |
| 2995 | if (refer_sub) { |
| 2996 | if (!pj_strnicmp2(&refer_sub->hvalue, "true", 4)==0) |
| 2997 | no_refer_sub = PJ_TRUE; |
| 2998 | } |
| 2999 | |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 3000 | /* Find optional Referred-By header (to be copied onto outgoing INVITE |
| 3001 | * request. |
| 3002 | */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 3003 | ref_by_hdr = (pjsip_hdr*) |
| 3004 | 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] | 3005 | NULL); |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 3006 | |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 3007 | /* Notify callback */ |
Benny Prijono | c54dcb3 | 2008-04-08 23:33:15 +0000 | [diff] [blame^] | 3008 | code = PJSIP_SC_ACCEPTED; |
Benny Prijono | 4ddad2c | 2006-10-18 17:16:34 +0000 | [diff] [blame] | 3009 | if (pjsua_var.ua_cfg.cb.on_call_transfer_request) |
| 3010 | (*pjsua_var.ua_cfg.cb.on_call_transfer_request)(existing_call->index, |
| 3011 | &refer_to->hvalue, |
| 3012 | &code); |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 3013 | |
| 3014 | if (code < 200) |
Benny Prijono | c54dcb3 | 2008-04-08 23:33:15 +0000 | [diff] [blame^] | 3015 | code = PJSIP_SC_ACCEPTED; |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 3016 | if (code >= 300) { |
| 3017 | /* Application rejects call transfer request */ |
| 3018 | pjsip_dlg_respond( inv->dlg, rdata, code, NULL, NULL, NULL); |
| 3019 | return; |
| 3020 | } |
| 3021 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3022 | PJ_LOG(3,(THIS_FILE, "Call to %.*s is being transfered to %.*s", |
| 3023 | (int)inv->dlg->remote.info_str.slen, |
| 3024 | inv->dlg->remote.info_str.ptr, |
| 3025 | (int)refer_to->hvalue.slen, |
| 3026 | refer_to->hvalue.ptr)); |
| 3027 | |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 3028 | if (no_refer_sub) { |
| 3029 | /* |
Benny Prijono | c54dcb3 | 2008-04-08 23:33:15 +0000 | [diff] [blame^] | 3030 | * Always answer with 2xx. |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 3031 | */ |
| 3032 | pjsip_tx_data *tdata; |
| 3033 | const pj_str_t str_false = { "false", 5}; |
| 3034 | pjsip_hdr *hdr; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3035 | |
Benny Prijono | c54dcb3 | 2008-04-08 23:33:15 +0000 | [diff] [blame^] | 3036 | status = pjsip_dlg_create_response(inv->dlg, rdata, code, NULL, |
| 3037 | &tdata); |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 3038 | if (status != PJ_SUCCESS) { |
Benny Prijono | c54dcb3 | 2008-04-08 23:33:15 +0000 | [diff] [blame^] | 3039 | pjsua_perror(THIS_FILE, "Unable to create 2xx response to REFER", |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 3040 | status); |
| 3041 | return; |
| 3042 | } |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3043 | |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 3044 | /* Add Refer-Sub header */ |
| 3045 | hdr = (pjsip_hdr*) |
| 3046 | pjsip_generic_string_hdr_create(tdata->pool, &str_refer_sub, |
| 3047 | &str_false); |
| 3048 | pjsip_msg_add_hdr(tdata->msg, hdr); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3049 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3050 | |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 3051 | /* Send answer */ |
| 3052 | status = pjsip_dlg_send_response(inv->dlg, pjsip_rdata_get_tsx(rdata), |
| 3053 | tdata); |
| 3054 | if (status != PJ_SUCCESS) { |
Benny Prijono | c54dcb3 | 2008-04-08 23:33:15 +0000 | [diff] [blame^] | 3055 | pjsua_perror(THIS_FILE, "Unable to create 2xx response to REFER", |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 3056 | status); |
| 3057 | return; |
| 3058 | } |
| 3059 | |
| 3060 | /* Don't have subscription */ |
| 3061 | sub = NULL; |
| 3062 | |
| 3063 | } else { |
| 3064 | struct pjsip_evsub_user xfer_cb; |
| 3065 | pjsip_hdr hdr_list; |
| 3066 | |
| 3067 | /* Init callback */ |
| 3068 | pj_bzero(&xfer_cb, sizeof(xfer_cb)); |
Benny Prijono | 4ddad2c | 2006-10-18 17:16:34 +0000 | [diff] [blame] | 3069 | xfer_cb.on_evsub_state = &xfer_server_on_evsub_state; |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 3070 | |
| 3071 | /* Init additional header list to be sent with REFER response */ |
| 3072 | pj_list_init(&hdr_list); |
| 3073 | |
| 3074 | /* Create transferee event subscription */ |
| 3075 | status = pjsip_xfer_create_uas( inv->dlg, &xfer_cb, rdata, &sub); |
| 3076 | if (status != PJ_SUCCESS) { |
| 3077 | pjsua_perror(THIS_FILE, "Unable to create xfer uas", status); |
| 3078 | pjsip_dlg_respond( inv->dlg, rdata, 500, NULL, NULL, NULL); |
| 3079 | return; |
| 3080 | } |
| 3081 | |
| 3082 | /* If there's Refer-Sub header and the value is "true", send back |
| 3083 | * Refer-Sub in the response with value "true" too. |
| 3084 | */ |
| 3085 | if (refer_sub) { |
| 3086 | const pj_str_t str_true = { "true", 4 }; |
| 3087 | pjsip_hdr *hdr; |
| 3088 | |
| 3089 | hdr = (pjsip_hdr*) |
| 3090 | pjsip_generic_string_hdr_create(inv->dlg->pool, |
| 3091 | &str_refer_sub, |
| 3092 | &str_true); |
| 3093 | pj_list_push_back(&hdr_list, hdr); |
| 3094 | |
| 3095 | } |
| 3096 | |
Benny Prijono | c54dcb3 | 2008-04-08 23:33:15 +0000 | [diff] [blame^] | 3097 | /* Accept the REFER request, send 2xx. */ |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 3098 | pjsip_xfer_accept(sub, rdata, code, &hdr_list); |
| 3099 | |
| 3100 | /* Create initial NOTIFY request */ |
| 3101 | status = pjsip_xfer_notify( sub, PJSIP_EVSUB_STATE_ACTIVE, |
| 3102 | 100, NULL, &tdata); |
| 3103 | if (status != PJ_SUCCESS) { |
| 3104 | pjsua_perror(THIS_FILE, "Unable to create NOTIFY to REFER", |
| 3105 | status); |
| 3106 | return; |
| 3107 | } |
| 3108 | |
| 3109 | /* Send initial NOTIFY request */ |
| 3110 | status = pjsip_xfer_send_request( sub, tdata); |
| 3111 | if (status != PJ_SUCCESS) { |
| 3112 | pjsua_perror(THIS_FILE, "Unable to send NOTIFY to REFER", status); |
| 3113 | return; |
| 3114 | } |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3115 | } |
| 3116 | |
| 3117 | /* We're cheating here. |
| 3118 | * We need to get a null terminated string from a pj_str_t. |
| 3119 | * So grab the pointer from the hvalue and NULL terminate it, knowing |
| 3120 | * that the NULL position will be occupied by a newline. |
| 3121 | */ |
| 3122 | uri = refer_to->hvalue.ptr; |
| 3123 | uri[refer_to->hvalue.slen] = '\0'; |
| 3124 | |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 3125 | /* Init msg_data */ |
| 3126 | pjsua_msg_data_init(&msg_data); |
| 3127 | |
| 3128 | /* If Referred-By header is present in the REFER request, copy this |
| 3129 | * to the outgoing INVITE request. |
| 3130 | */ |
| 3131 | if (ref_by_hdr != NULL) { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 3132 | pjsip_hdr *dup = (pjsip_hdr*) |
| 3133 | pjsip_hdr_clone(rdata->tp_info.pool, ref_by_hdr); |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 3134 | pj_list_push_back(&msg_data.hdr_list, dup); |
| 3135 | } |
| 3136 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3137 | /* Now make the outgoing call. */ |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 3138 | tmp = pj_str(uri); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3139 | status = pjsua_call_make_call(existing_call->acc_id, &tmp, 0, |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 3140 | existing_call->user_data, &msg_data, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3141 | &new_call); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3142 | if (status != PJ_SUCCESS) { |
| 3143 | |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 3144 | /* Notify xferer about the error (if we have subscription) */ |
| 3145 | if (sub) { |
| 3146 | status = pjsip_xfer_notify(sub, PJSIP_EVSUB_STATE_TERMINATED, |
| 3147 | 500, NULL, &tdata); |
| 3148 | if (status != PJ_SUCCESS) { |
| 3149 | pjsua_perror(THIS_FILE, "Unable to create NOTIFY to REFER", |
| 3150 | status); |
| 3151 | return; |
| 3152 | } |
| 3153 | status = pjsip_xfer_send_request(sub, tdata); |
| 3154 | if (status != PJ_SUCCESS) { |
| 3155 | pjsua_perror(THIS_FILE, "Unable to send NOTIFY to REFER", |
| 3156 | status); |
| 3157 | return; |
| 3158 | } |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3159 | } |
| 3160 | return; |
| 3161 | } |
| 3162 | |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 3163 | if (sub) { |
| 3164 | /* Put the server subscription in inv_data. |
| 3165 | * Subsequent state changed in pjsua_inv_on_state_changed() will be |
| 3166 | * reported back to the server subscription. |
| 3167 | */ |
| 3168 | pjsua_var.calls[new_call].xfer_sub = sub; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3169 | |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 3170 | /* Put the invite_data in the subscription. */ |
| 3171 | pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, |
| 3172 | &pjsua_var.calls[new_call]); |
| 3173 | } |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3174 | } |
| 3175 | |
| 3176 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3177 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3178 | /* |
| 3179 | * This callback is called when transaction state has changed in INVITE |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 3180 | * session. We use this to trap: |
| 3181 | * - incoming REFER request. |
| 3182 | * - incoming MESSAGE request. |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3183 | */ |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 3184 | static void pjsua_call_on_tsx_state_changed(pjsip_inv_session *inv, |
| 3185 | pjsip_transaction *tsx, |
| 3186 | pjsip_event *e) |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3187 | { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 3188 | 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] | 3189 | |
| 3190 | PJSUA_LOCK(); |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 3191 | |
Benny Prijono | feb69f4 | 2007-10-05 09:12:26 +0000 | [diff] [blame] | 3192 | /* Notify application callback first */ |
| 3193 | if (pjsua_var.ua_cfg.cb.on_call_tsx_state) { |
| 3194 | (*pjsua_var.ua_cfg.cb.on_call_tsx_state)(call->index, tsx, e); |
| 3195 | } |
| 3196 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3197 | if (tsx->role==PJSIP_ROLE_UAS && |
| 3198 | tsx->state==PJSIP_TSX_STATE_TRYING && |
Benny Prijono | 1f61a8f | 2007-08-16 10:11:44 +0000 | [diff] [blame] | 3199 | pjsip_method_cmp(&tsx->method, pjsip_get_refer_method())==0) |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3200 | { |
| 3201 | /* |
| 3202 | * Incoming REFER request. |
| 3203 | */ |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 3204 | on_call_transfered(call->inv, e->body.tsx_state.src.rdata); |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 3205 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3206 | } |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 3207 | else if (tsx->role==PJSIP_ROLE_UAS && |
| 3208 | tsx->state==PJSIP_TSX_STATE_TRYING && |
| 3209 | pjsip_method_cmp(&tsx->method, &pjsip_message_method)==0) |
| 3210 | { |
| 3211 | /* |
| 3212 | * Incoming MESSAGE request! |
| 3213 | */ |
| 3214 | pjsip_rx_data *rdata; |
| 3215 | pjsip_msg *msg; |
| 3216 | pjsip_accept_hdr *accept_hdr; |
| 3217 | pj_status_t status; |
| 3218 | |
| 3219 | rdata = e->body.tsx_state.src.rdata; |
| 3220 | msg = rdata->msg_info.msg; |
| 3221 | |
| 3222 | /* Request MUST have message body, with Content-Type equal to |
| 3223 | * "text/plain". |
| 3224 | */ |
| 3225 | if (pjsua_im_accept_pager(rdata, &accept_hdr) == PJ_FALSE) { |
| 3226 | |
| 3227 | pjsip_hdr hdr_list; |
| 3228 | |
| 3229 | pj_list_init(&hdr_list); |
| 3230 | pj_list_push_back(&hdr_list, accept_hdr); |
| 3231 | |
| 3232 | pjsip_dlg_respond( inv->dlg, rdata, PJSIP_SC_NOT_ACCEPTABLE_HERE, |
| 3233 | NULL, &hdr_list, NULL ); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3234 | PJSUA_UNLOCK(); |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 3235 | return; |
| 3236 | } |
| 3237 | |
| 3238 | /* Respond with 200 first, so that remote doesn't retransmit in case |
| 3239 | * the UI takes too long to process the message. |
| 3240 | */ |
| 3241 | status = pjsip_dlg_respond( inv->dlg, rdata, 200, NULL, NULL, NULL); |
| 3242 | |
| 3243 | /* Process MESSAGE request */ |
| 3244 | pjsua_im_process_pager(call->index, &inv->dlg->remote.info_str, |
| 3245 | &inv->dlg->local.info_str, rdata); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3246 | |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 3247 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3248 | else if (tsx->role == PJSIP_ROLE_UAC && |
| 3249 | pjsip_method_cmp(&tsx->method, &pjsip_message_method)==0) |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3250 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3251 | /* Handle outgoing pager status */ |
| 3252 | if (tsx->status_code >= 200) { |
| 3253 | pjsua_im_data *im_data; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3254 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 3255 | im_data = (pjsua_im_data*) tsx->mod_data[pjsua_var.mod.id]; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3256 | /* im_data can be NULL if this is typing indication */ |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3257 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3258 | if (im_data && pjsua_var.ua_cfg.cb.on_pager_status) { |
| 3259 | pjsua_var.ua_cfg.cb.on_pager_status(im_data->call_id, |
| 3260 | &im_data->to, |
| 3261 | &im_data->body, |
| 3262 | im_data->user_data, |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 3263 | (pjsip_status_code) |
| 3264 | tsx->status_code, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3265 | &tsx->status_text); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3266 | } |
Benny Prijono | fccab71 | 2006-02-22 22:23:22 +0000 | [diff] [blame] | 3267 | } |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 3268 | } |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 3269 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3270 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3271 | PJSUA_UNLOCK(); |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 3272 | } |