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