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 | |
| 32 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 33 | /* This callback receives notification from invite session when the |
| 34 | * session state has changed. |
| 35 | */ |
| 36 | static void pjsua_call_on_state_changed(pjsip_inv_session *inv, |
| 37 | pjsip_event *e); |
| 38 | |
| 39 | /* This callback is called by invite session framework when UAC session |
| 40 | * has forked. |
| 41 | */ |
| 42 | static void pjsua_call_on_forked( pjsip_inv_session *inv, |
| 43 | pjsip_event *e); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 44 | |
| 45 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 46 | * Callback to be called when SDP offer/answer negotiation has just completed |
| 47 | * in the session. This function will start/update media if negotiation |
| 48 | * has succeeded. |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 49 | */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 50 | static void pjsua_call_on_media_update(pjsip_inv_session *inv, |
| 51 | pj_status_t status); |
Benny Prijono | 105217f | 2006-03-06 16:25:59 +0000 | [diff] [blame] | 52 | |
| 53 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 54 | * Called when session received new offer. |
Benny Prijono | 105217f | 2006-03-06 16:25:59 +0000 | [diff] [blame] | 55 | */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 56 | static void pjsua_call_on_rx_offer(pjsip_inv_session *inv, |
| 57 | const pjmedia_sdp_session *offer); |
| 58 | |
| 59 | /* |
Benny Prijono | 77998ce | 2007-06-20 10:03:46 +0000 | [diff] [blame] | 60 | * Called to generate new offer. |
| 61 | */ |
| 62 | static void pjsua_call_on_create_offer(pjsip_inv_session *inv, |
| 63 | pjmedia_sdp_session **offer); |
| 64 | |
| 65 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 66 | * This callback is called when transaction state has changed in INVITE |
| 67 | * session. We use this to trap: |
| 68 | * - incoming REFER request. |
| 69 | * - incoming MESSAGE request. |
| 70 | */ |
| 71 | static void pjsua_call_on_tsx_state_changed(pjsip_inv_session *inv, |
| 72 | pjsip_transaction *tsx, |
| 73 | pjsip_event *e); |
| 74 | |
Benny Prijono | 5e51a4e | 2008-11-27 00:06:46 +0000 | [diff] [blame] | 75 | /* |
| 76 | * Redirection handler. |
| 77 | */ |
Benny Prijono | 08a48b8 | 2008-11-27 12:42:07 +0000 | [diff] [blame] | 78 | static pjsip_redirect_op pjsua_call_on_redirected(pjsip_inv_session *inv, |
| 79 | const pjsip_uri *target, |
| 80 | const pjsip_event *e); |
Benny Prijono | 5e51a4e | 2008-11-27 00:06:46 +0000 | [diff] [blame] | 81 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 82 | |
Nanang Izzuddin | 99d6952 | 2008-08-04 15:01:38 +0000 | [diff] [blame] | 83 | /* Create SDP for call hold. */ |
| 84 | static pj_status_t create_sdp_of_call_hold(pjsua_call *call, |
Benny Prijono | dd63b99 | 2010-10-01 02:03:42 +0000 | [diff] [blame^] | 85 | pjmedia_sdp_session **p_sdp); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 86 | |
Benny Prijono | d524e82 | 2006-09-22 12:48:18 +0000 | [diff] [blame] | 87 | /* |
| 88 | * Callback called by event framework when the xfer subscription state |
| 89 | * has changed. |
| 90 | */ |
Benny Prijono | 4ddad2c | 2006-10-18 17:16:34 +0000 | [diff] [blame] | 91 | static void xfer_client_on_evsub_state( pjsip_evsub *sub, pjsip_event *event); |
| 92 | static void xfer_server_on_evsub_state( pjsip_evsub *sub, pjsip_event *event); |
| 93 | |
| 94 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 95 | * Reset call descriptor. |
| 96 | */ |
| 97 | static void reset_call(pjsua_call_id id) |
Benny Prijono | 105217f | 2006-03-06 16:25:59 +0000 | [diff] [blame] | 98 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 99 | pjsua_call *call = &pjsua_var.calls[id]; |
Benny Prijono | 105217f | 2006-03-06 16:25:59 +0000 | [diff] [blame] | 100 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 101 | call->index = id; |
| 102 | call->inv = NULL; |
| 103 | call->user_data = NULL; |
| 104 | call->session = NULL; |
Benny Prijono | a310bd2 | 2008-06-27 21:19:44 +0000 | [diff] [blame] | 105 | call->audio_idx = -1; |
Benny Prijono | 8147f40 | 2007-11-21 14:50:07 +0000 | [diff] [blame] | 106 | call->ssrc = pj_rand(); |
Nanang Izzuddin | a815ceb | 2008-08-26 16:51:28 +0000 | [diff] [blame] | 107 | call->rtp_tx_seq = 0; |
| 108 | call->rtp_tx_ts = 0; |
| 109 | call->rtp_tx_seq_ts_set = 0; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 110 | call->xfer_sub = NULL; |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 111 | call->last_code = (pjsip_status_code) 0; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 112 | call->conf_slot = PJSUA_INVALID_ID; |
| 113 | call->last_text.ptr = call->last_text_buf_; |
| 114 | call->last_text.slen = 0; |
Benny Prijono | 4be63b5 | 2006-11-25 14:50:25 +0000 | [diff] [blame] | 115 | call->conn_time.sec = 0; |
| 116 | call->conn_time.msec = 0; |
| 117 | call->res_time.sec = 0; |
| 118 | call->res_time.msec = 0; |
Benny Prijono | 91a6a17 | 2007-10-31 08:59:29 +0000 | [diff] [blame] | 119 | call->rem_nat_type = PJ_STUN_NAT_TYPE_UNKNOWN; |
Nanang Izzuddin | 4375f90 | 2008-06-26 19:12:09 +0000 | [diff] [blame] | 120 | call->rem_srtp_use = PJMEDIA_SRTP_DISABLED; |
Nanang Izzuddin | 99d6952 | 2008-08-04 15:01:38 +0000 | [diff] [blame] | 121 | call->local_hold = PJ_FALSE; |
Benny Prijono | 105217f | 2006-03-06 16:25:59 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | |
Benny Prijono | 275fd68 | 2006-03-22 11:59:11 +0000 | [diff] [blame] | 125 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 126 | * Init call subsystem. |
Benny Prijono | 275fd68 | 2006-03-22 11:59:11 +0000 | [diff] [blame] | 127 | */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 128 | pj_status_t pjsua_call_subsys_init(const pjsua_config *cfg) |
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 | pjsip_inv_callback inv_cb; |
| 131 | unsigned i; |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 132 | const pj_str_t str_norefersub = { "norefersub", 10 }; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 133 | pj_status_t status; |
Benny Prijono | 275fd68 | 2006-03-22 11:59:11 +0000 | [diff] [blame] | 134 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 135 | /* Init calls array. */ |
| 136 | for (i=0; i<PJ_ARRAY_SIZE(pjsua_var.calls); ++i) |
| 137 | reset_call(i); |
Benny Prijono | 275fd68 | 2006-03-22 11:59:11 +0000 | [diff] [blame] | 138 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 139 | /* Copy config */ |
| 140 | pjsua_config_dup(pjsua_var.pool, &pjsua_var.ua_cfg, cfg); |
Benny Prijono | 275fd68 | 2006-03-22 11:59:11 +0000 | [diff] [blame] | 141 | |
Benny Prijono | 1cd713b | 2009-11-11 00:33:00 +0000 | [diff] [blame] | 142 | /* Verify settings */ |
| 143 | if (pjsua_var.ua_cfg.max_calls >= PJSUA_MAX_CALLS) { |
| 144 | pjsua_var.ua_cfg.max_calls = PJSUA_MAX_CALLS; |
| 145 | } |
| 146 | |
Benny Prijono | 91d06b6 | 2008-09-20 12:16:56 +0000 | [diff] [blame] | 147 | /* Check the route URI's and force loose route if required */ |
| 148 | for (i=0; i<pjsua_var.ua_cfg.outbound_proxy_cnt; ++i) { |
| 149 | status = normalize_route_uri(pjsua_var.pool, |
| 150 | &pjsua_var.ua_cfg.outbound_proxy[i]); |
| 151 | if (status != PJ_SUCCESS) |
| 152 | return status; |
| 153 | } |
| 154 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 155 | /* Initialize invite session callback. */ |
Benny Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 156 | pj_bzero(&inv_cb, sizeof(inv_cb)); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 157 | inv_cb.on_state_changed = &pjsua_call_on_state_changed; |
| 158 | inv_cb.on_new_session = &pjsua_call_on_forked; |
| 159 | inv_cb.on_media_update = &pjsua_call_on_media_update; |
| 160 | inv_cb.on_rx_offer = &pjsua_call_on_rx_offer; |
Benny Prijono | 77998ce | 2007-06-20 10:03:46 +0000 | [diff] [blame] | 161 | inv_cb.on_create_offer = &pjsua_call_on_create_offer; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 162 | inv_cb.on_tsx_state_changed = &pjsua_call_on_tsx_state_changed; |
Benny Prijono | 5e51a4e | 2008-11-27 00:06:46 +0000 | [diff] [blame] | 163 | inv_cb.on_redirected = &pjsua_call_on_redirected; |
Benny Prijono | 275fd68 | 2006-03-22 11:59:11 +0000 | [diff] [blame] | 164 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 165 | /* Initialize invite session module: */ |
| 166 | status = pjsip_inv_usage_init(pjsua_var.endpt, &inv_cb); |
| 167 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
| 168 | |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 169 | /* Add "norefersub" in Supported header */ |
| 170 | pjsip_endpt_add_capability(pjsua_var.endpt, NULL, PJSIP_H_SUPPORTED, |
| 171 | NULL, 1, &str_norefersub); |
| 172 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 173 | return status; |
| 174 | } |
| 175 | |
| 176 | |
| 177 | /* |
| 178 | * Start call subsystem. |
| 179 | */ |
| 180 | pj_status_t pjsua_call_subsys_start(void) |
| 181 | { |
| 182 | /* Nothing to do */ |
Benny Prijono | 275fd68 | 2006-03-22 11:59:11 +0000 | [diff] [blame] | 183 | return PJ_SUCCESS; |
| 184 | } |
| 185 | |
| 186 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 187 | /* |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 188 | * Get maximum number of calls configured in pjsua. |
| 189 | */ |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 190 | PJ_DEF(unsigned) pjsua_call_get_max_count(void) |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 191 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 192 | return pjsua_var.ua_cfg.max_calls; |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 196 | /* |
| 197 | * Get number of currently active calls. |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 198 | */ |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 199 | PJ_DEF(unsigned) pjsua_call_get_count(void) |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 200 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 201 | return pjsua_var.call_cnt; |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 205 | /* |
| 206 | * Enum calls. |
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_DEF(pj_status_t) pjsua_enum_calls( pjsua_call_id ids[], |
| 209 | unsigned *count) |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 210 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 211 | unsigned i, c; |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 212 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 213 | PJ_ASSERT_RETURN(ids && *count, PJ_EINVAL); |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 214 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 215 | PJSUA_LOCK(); |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 216 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 217 | for (i=0, c=0; c<*count && i<pjsua_var.ua_cfg.max_calls; ++i) { |
| 218 | if (!pjsua_var.calls[i].inv) |
| 219 | continue; |
| 220 | ids[c] = i; |
| 221 | ++c; |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 224 | *count = c; |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 225 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 226 | PJSUA_UNLOCK(); |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 227 | |
| 228 | return PJ_SUCCESS; |
| 229 | } |
| 230 | |
| 231 | |
Benny Prijono | 5773cd6 | 2008-01-19 13:01:42 +0000 | [diff] [blame] | 232 | /* Allocate one call id */ |
| 233 | static pjsua_call_id alloc_call_id(void) |
| 234 | { |
| 235 | pjsua_call_id cid; |
| 236 | |
| 237 | #if 1 |
| 238 | /* New algorithm: round-robin */ |
| 239 | if (pjsua_var.next_call_id >= (int)pjsua_var.ua_cfg.max_calls || |
| 240 | pjsua_var.next_call_id < 0) |
| 241 | { |
Benny Prijono | 5d177b8 | 2008-02-26 10:31:28 +0000 | [diff] [blame] | 242 | pjsua_var.next_call_id = 0; |
Benny Prijono | 5773cd6 | 2008-01-19 13:01:42 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | for (cid=pjsua_var.next_call_id; |
| 246 | cid<(int)pjsua_var.ua_cfg.max_calls; |
| 247 | ++cid) |
| 248 | { |
| 249 | if (pjsua_var.calls[cid].inv == NULL) { |
| 250 | ++pjsua_var.next_call_id; |
| 251 | return cid; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | for (cid=0; cid < pjsua_var.next_call_id; ++cid) { |
| 256 | if (pjsua_var.calls[cid].inv == NULL) { |
| 257 | ++pjsua_var.next_call_id; |
| 258 | return cid; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | #else |
| 263 | /* Old algorithm */ |
| 264 | for (cid=0; cid<(int)pjsua_var.ua_cfg.max_calls; ++cid) { |
| 265 | if (pjsua_var.calls[cid].inv == NULL) |
| 266 | return cid; |
| 267 | } |
| 268 | #endif |
| 269 | |
| 270 | return PJSUA_INVALID_ID; |
| 271 | } |
| 272 | |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 273 | /* Get signaling secure level. |
| 274 | * Return: |
| 275 | * 0: if signaling is not secure |
| 276 | * 1: if TLS transport is used for immediate hop |
| 277 | * 2: if end-to-end signaling is secure. |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 278 | */ |
Benny Prijono | db844a4 | 2008-02-02 17:07:18 +0000 | [diff] [blame] | 279 | 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] | 280 | { |
| 281 | const pj_str_t tls = pj_str(";transport=tls"); |
| 282 | const pj_str_t sips = pj_str("sips:"); |
Benny Prijono | db844a4 | 2008-02-02 17:07:18 +0000 | [diff] [blame] | 283 | pjsua_acc *acc = &pjsua_var.acc[acc_id]; |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 284 | |
| 285 | if (pj_stristr(dst_uri, &sips)) |
| 286 | return 2; |
Benny Prijono | db844a4 | 2008-02-02 17:07:18 +0000 | [diff] [blame] | 287 | |
| 288 | if (!pj_list_empty(&acc->route_set)) { |
| 289 | pjsip_route_hdr *r = acc->route_set.next; |
| 290 | pjsip_uri *uri = r->name_addr.uri; |
| 291 | pjsip_sip_uri *sip_uri; |
| 292 | |
| 293 | sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri); |
| 294 | if (pj_stricmp2(&sip_uri->transport_param, "tls")==0) |
| 295 | return 1; |
| 296 | |
| 297 | } else { |
| 298 | if (pj_stristr(dst_uri, &tls)) |
| 299 | return 1; |
| 300 | } |
| 301 | |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 302 | return 0; |
| 303 | } |
| 304 | |
Benny Prijono | 224b4e2 | 2008-06-19 14:10:28 +0000 | [diff] [blame] | 305 | /* |
Benny Prijono | db844a4 | 2008-02-02 17:07:18 +0000 | [diff] [blame] | 306 | static int call_get_secure_level(pjsua_call *call) |
| 307 | { |
| 308 | if (call->inv->dlg->secure) |
| 309 | return 2; |
| 310 | |
| 311 | if (!pj_list_empty(&call->inv->dlg->route_set)) { |
| 312 | pjsip_route_hdr *r = call->inv->dlg->route_set.next; |
| 313 | pjsip_uri *uri = r->name_addr.uri; |
| 314 | pjsip_sip_uri *sip_uri; |
| 315 | |
| 316 | sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri); |
| 317 | if (pj_stricmp2(&sip_uri->transport_param, "tls")==0) |
| 318 | return 1; |
| 319 | |
| 320 | } else { |
| 321 | pjsip_sip_uri *sip_uri; |
| 322 | |
| 323 | if (PJSIP_URI_SCHEME_IS_SIPS(call->inv->dlg->target)) |
| 324 | return 2; |
| 325 | if (!PJSIP_URI_SCHEME_IS_SIP(call->inv->dlg->target)) |
| 326 | return 0; |
| 327 | |
| 328 | sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(call->inv->dlg->target); |
| 329 | if (pj_stricmp2(&sip_uri->transport_param, "tls")==0) |
| 330 | return 1; |
| 331 | } |
| 332 | |
| 333 | return 0; |
| 334 | } |
Benny Prijono | 224b4e2 | 2008-06-19 14:10:28 +0000 | [diff] [blame] | 335 | */ |
Benny Prijono | db844a4 | 2008-02-02 17:07:18 +0000 | [diff] [blame] | 336 | |
| 337 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 338 | /* |
| 339 | * Make outgoing call to the specified URI using the specified account. |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 340 | */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 341 | PJ_DEF(pj_status_t) pjsua_call_make_call( pjsua_acc_id acc_id, |
| 342 | const pj_str_t *dest_uri, |
| 343 | unsigned options, |
| 344 | void *user_data, |
| 345 | const pjsua_msg_data *msg_data, |
| 346 | pjsua_call_id *p_call_id) |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 347 | { |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 348 | pj_pool_t *tmp_pool; |
Benny Prijono | 1c2bf46 | 2006-03-05 11:54:02 +0000 | [diff] [blame] | 349 | pjsip_dialog *dlg = NULL; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 350 | pjmedia_sdp_session *offer; |
Benny Prijono | 1c2bf46 | 2006-03-05 11:54:02 +0000 | [diff] [blame] | 351 | pjsip_inv_session *inv = NULL; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 352 | pjsua_acc *acc; |
| 353 | pjsua_call *call; |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 354 | int call_id = -1; |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 355 | pj_str_t contact; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 356 | pjsip_tx_data *tdata; |
| 357 | pj_status_t status; |
| 358 | |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 359 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 360 | /* Check that account is valid */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 361 | 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] | 362 | PJ_EINVAL); |
| 363 | |
Benny Prijono | 320fa4d | 2006-12-07 10:09:16 +0000 | [diff] [blame] | 364 | /* Check arguments */ |
| 365 | PJ_ASSERT_RETURN(dest_uri, PJ_EINVAL); |
| 366 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 367 | PJSUA_LOCK(); |
| 368 | |
Benny Prijono | f798e50 | 2009-03-09 13:08:16 +0000 | [diff] [blame] | 369 | /* Create sound port if none is instantiated, to check if sound device |
| 370 | * can be used. But only do this with the conference bridge, as with |
| 371 | * audio switchboard (i.e. APS-Direct), we can only open the sound |
| 372 | * device once the correct format has been known |
| 373 | */ |
| 374 | if (!pjsua_var.is_mswitch && pjsua_var.snd_port==NULL && |
| 375 | pjsua_var.null_snd==NULL && !pjsua_var.no_snd) |
Nanang Izzuddin | 148fd39 | 2008-06-16 09:52:50 +0000 | [diff] [blame] | 376 | { |
| 377 | pj_status_t status; |
| 378 | |
| 379 | status = pjsua_set_snd_dev(pjsua_var.cap_dev, pjsua_var.play_dev); |
| 380 | if (status != PJ_SUCCESS) { |
| 381 | PJSUA_UNLOCK(); |
| 382 | return status; |
| 383 | } |
| 384 | } |
| 385 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 386 | acc = &pjsua_var.acc[acc_id]; |
| 387 | if (!acc->valid) { |
| 388 | pjsua_perror(THIS_FILE, "Unable to make call because account " |
| 389 | "is not valid", PJ_EINVALIDOP); |
| 390 | PJSUA_UNLOCK(); |
| 391 | return PJ_EINVALIDOP; |
| 392 | } |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 393 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 394 | /* Find free call slot. */ |
Benny Prijono | 5773cd6 | 2008-01-19 13:01:42 +0000 | [diff] [blame] | 395 | call_id = alloc_call_id(); |
Benny Prijono | f04ffdd | 2006-02-21 00:11:18 +0000 | [diff] [blame] | 396 | |
Benny Prijono | 5773cd6 | 2008-01-19 13:01:42 +0000 | [diff] [blame] | 397 | if (call_id == PJSUA_INVALID_ID) { |
Nanang Izzuddin | 79f4f20 | 2009-10-20 13:14:40 +0000 | [diff] [blame] | 398 | pjsua_perror(THIS_FILE, "Error making call", PJ_ETOOMANY); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 399 | PJSUA_UNLOCK(); |
Benny Prijono | f04ffdd | 2006-02-21 00:11:18 +0000 | [diff] [blame] | 400 | return PJ_ETOOMANY; |
| 401 | } |
| 402 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 403 | call = &pjsua_var.calls[call_id]; |
| 404 | |
Nanang Izzuddin | 79f4f20 | 2009-10-20 13:14:40 +0000 | [diff] [blame] | 405 | /* Associate session with account */ |
| 406 | call->acc_id = acc_id; |
Benny Prijono | dd63b99 | 2010-10-01 02:03:42 +0000 | [diff] [blame^] | 407 | call->call_hold_type = acc->cfg.call_hold_type; |
Nanang Izzuddin | 79f4f20 | 2009-10-20 13:14:40 +0000 | [diff] [blame] | 408 | |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 409 | /* Create temporary pool */ |
| 410 | tmp_pool = pjsua_pool_create("tmpcall10", 512, 256); |
| 411 | |
Benny Prijono | 320fa4d | 2006-12-07 10:09:16 +0000 | [diff] [blame] | 412 | /* Verify that destination URI is valid before calling |
| 413 | * pjsua_acc_create_uac_contact, or otherwise there |
| 414 | * a misleading "Invalid Contact URI" error will be printed |
| 415 | * when pjsua_acc_create_uac_contact() fails. |
| 416 | */ |
| 417 | if (1) { |
Benny Prijono | 320fa4d | 2006-12-07 10:09:16 +0000 | [diff] [blame] | 418 | pjsip_uri *uri; |
| 419 | pj_str_t dup; |
| 420 | |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 421 | pj_strdup_with_null(tmp_pool, &dup, dest_uri); |
| 422 | uri = pjsip_parse_uri(tmp_pool, dup.ptr, dup.slen, 0); |
Benny Prijono | 320fa4d | 2006-12-07 10:09:16 +0000 | [diff] [blame] | 423 | |
| 424 | if (uri == NULL) { |
| 425 | pjsua_perror(THIS_FILE, "Unable to make call", |
| 426 | PJSIP_EINVALIDREQURI); |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 427 | pj_pool_release(tmp_pool); |
Benny Prijono | 320fa4d | 2006-12-07 10:09:16 +0000 | [diff] [blame] | 428 | PJSUA_UNLOCK(); |
| 429 | return PJSIP_EINVALIDREQURI; |
| 430 | } |
| 431 | } |
| 432 | |
Benny Prijono | 093d302 | 2006-09-24 00:07:11 +0000 | [diff] [blame] | 433 | PJ_LOG(4,(THIS_FILE, "Making call with acc #%d to %.*s", acc_id, |
| 434 | (int)dest_uri->slen, dest_uri->ptr)); |
| 435 | |
Benny Prijono | e21e784 | 2006-04-09 16:46:05 +0000 | [diff] [blame] | 436 | /* Mark call start time. */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 437 | pj_gettimeofday(&call->start_time); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 438 | |
Benny Prijono | e21e784 | 2006-04-09 16:46:05 +0000 | [diff] [blame] | 439 | /* Reset first response time */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 440 | call->res_time.sec = 0; |
Benny Prijono | e21e784 | 2006-04-09 16:46:05 +0000 | [diff] [blame] | 441 | |
Benny Prijono | ddaaf6a | 2008-04-15 10:37:19 +0000 | [diff] [blame] | 442 | /* Create suitable Contact header unless a Contact header has been |
| 443 | * set in the account. |
| 444 | */ |
| 445 | if (acc->contact.slen) { |
| 446 | contact = acc->contact; |
| 447 | } else { |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 448 | status = pjsua_acc_create_uac_contact(tmp_pool, &contact, |
Benny Prijono | ddaaf6a | 2008-04-15 10:37:19 +0000 | [diff] [blame] | 449 | acc_id, dest_uri); |
| 450 | if (status != PJ_SUCCESS) { |
| 451 | pjsua_perror(THIS_FILE, "Unable to generate Contact header", |
| 452 | status); |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 453 | pj_pool_release(tmp_pool); |
Benny Prijono | ddaaf6a | 2008-04-15 10:37:19 +0000 | [diff] [blame] | 454 | PJSUA_UNLOCK(); |
| 455 | return status; |
| 456 | } |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Benny Prijono | e21e784 | 2006-04-09 16:46:05 +0000 | [diff] [blame] | 459 | /* Create outgoing dialog: */ |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 460 | status = pjsip_dlg_create_uac( pjsip_ua_instance(), |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 461 | &acc->cfg.id, &contact, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 462 | dest_uri, dest_uri, &dlg); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 463 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 464 | pjsua_perror(THIS_FILE, "Dialog creation failed", status); |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 465 | pj_pool_release(tmp_pool); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 466 | PJSUA_UNLOCK(); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 467 | return status; |
| 468 | } |
| 469 | |
Benny Prijono | 0bd5eb9 | 2009-04-14 11:10:31 +0000 | [diff] [blame] | 470 | /* Increment the dialog's lock otherwise when invite session creation |
| 471 | * fails the dialog will be destroyed prematurely. |
| 472 | */ |
| 473 | pjsip_dlg_inc_lock(dlg); |
| 474 | |
Benny Prijono | db844a4 | 2008-02-02 17:07:18 +0000 | [diff] [blame] | 475 | /* Calculate call's secure level */ |
| 476 | call->secure_level = get_secure_level(acc_id, dest_uri); |
| 477 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 478 | /* Init media channel */ |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 479 | status = pjsua_media_channel_init(call->index, PJSIP_ROLE_UAC, |
Benny Prijono | 224b4e2 | 2008-06-19 14:10:28 +0000 | [diff] [blame] | 480 | call->secure_level, dlg->pool, |
| 481 | NULL, NULL); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 482 | if (status != PJ_SUCCESS) { |
| 483 | pjsua_perror(THIS_FILE, "Error initializing media channel", status); |
| 484 | goto on_error; |
| 485 | } |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 486 | |
Benny Prijono | 224b4e2 | 2008-06-19 14:10:28 +0000 | [diff] [blame] | 487 | /* Create offer */ |
| 488 | status = pjsua_media_channel_create_sdp(call->index, dlg->pool, NULL, |
Benny Prijono | 25b2ea1 | 2008-01-24 19:20:54 +0000 | [diff] [blame] | 489 | &offer, NULL); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 490 | if (status != PJ_SUCCESS) { |
Benny Prijono | 224b4e2 | 2008-06-19 14:10:28 +0000 | [diff] [blame] | 491 | pjsua_perror(THIS_FILE, "Error initializing media channel", status); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 492 | goto on_error; |
| 493 | } |
| 494 | |
| 495 | /* Create the INVITE session: */ |
Benny Prijono | dcfc0ba | 2007-09-30 16:50:27 +0000 | [diff] [blame] | 496 | options |= PJSIP_INV_SUPPORT_100REL; |
Benny Prijono | dcfc0ba | 2007-09-30 16:50:27 +0000 | [diff] [blame] | 497 | if (acc->cfg.require_100rel) |
| 498 | options |= PJSIP_INV_REQUIRE_100REL; |
Nanang Izzuddin | 742ef4b | 2010-09-07 09:36:15 +0000 | [diff] [blame] | 499 | if (acc->cfg.use_timer != PJSUA_SIP_TIMER_INACTIVE) { |
| 500 | options |= PJSIP_INV_SUPPORT_TIMER; |
| 501 | if (acc->cfg.use_timer == PJSUA_SIP_TIMER_REQUIRED) |
| 502 | options |= PJSIP_INV_REQUIRE_TIMER; |
| 503 | else if (acc->cfg.use_timer == PJSUA_SIP_TIMER_ALWAYS) |
| 504 | options |= PJSIP_INV_ALWAYS_USE_TIMER; |
| 505 | } |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 506 | |
Benny Prijono | dcfc0ba | 2007-09-30 16:50:27 +0000 | [diff] [blame] | 507 | status = pjsip_inv_create_uac( dlg, offer, options, &inv); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 508 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 509 | pjsua_perror(THIS_FILE, "Invite session creation failed", status); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 510 | goto on_error; |
| 511 | } |
| 512 | |
Nanang Izzuddin | 59dffb1 | 2009-08-11 12:42:38 +0000 | [diff] [blame] | 513 | /* Init Session Timers */ |
Nanang Izzuddin | 65add62 | 2009-08-11 16:26:20 +0000 | [diff] [blame] | 514 | status = pjsip_timer_init_session(inv, &acc->cfg.timer_setting); |
| 515 | if (status != PJ_SUCCESS) { |
| 516 | pjsua_perror(THIS_FILE, "Session Timer init failed", status); |
| 517 | goto on_error; |
Nanang Izzuddin | 59dffb1 | 2009-08-11 12:42:38 +0000 | [diff] [blame] | 518 | } |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 519 | |
| 520 | /* Create and associate our data in the session. */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 521 | call->inv = inv; |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 522 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 523 | dlg->mod_data[pjsua_var.mod.id] = call; |
| 524 | inv->mod_data[pjsua_var.mod.id] = call; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 525 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 526 | /* Attach user data */ |
| 527 | call->user_data = user_data; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 528 | |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 529 | /* If account is locked to specific transport, then lock dialog |
| 530 | * to this transport too. |
| 531 | */ |
| 532 | if (acc->cfg.transport_id != PJSUA_INVALID_ID) { |
| 533 | pjsip_tpselector tp_sel; |
| 534 | |
| 535 | pjsua_init_tpselector(acc->cfg.transport_id, &tp_sel); |
| 536 | pjsip_dlg_set_transport(dlg, &tp_sel); |
| 537 | } |
| 538 | |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 539 | /* Set dialog Route-Set: */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 540 | if (!pj_list_empty(&acc->route_set)) |
| 541 | pjsip_dlg_set_route_set(dlg, &acc->route_set); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 542 | |
| 543 | |
| 544 | /* Set credentials: */ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 545 | if (acc->cred_cnt) { |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 546 | pjsip_auth_clt_set_credentials( &dlg->auth_sess, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 547 | acc->cred_cnt, acc->cred); |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 548 | } |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 549 | |
Benny Prijono | 48ab2b7 | 2007-11-08 09:24:30 +0000 | [diff] [blame] | 550 | /* Set authentication preference */ |
| 551 | pjsip_auth_clt_set_prefs(&dlg->auth_sess, &acc->cfg.auth_pref); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 552 | |
| 553 | /* Create initial INVITE: */ |
| 554 | |
| 555 | status = pjsip_inv_invite(inv, &tdata); |
| 556 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 557 | pjsua_perror(THIS_FILE, "Unable to create initial INVITE request", |
| 558 | status); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 559 | goto on_error; |
| 560 | } |
| 561 | |
| 562 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 563 | /* Add additional headers etc */ |
| 564 | |
| 565 | pjsua_process_msg_data( tdata, msg_data); |
| 566 | |
Benny Prijono | 093d302 | 2006-09-24 00:07:11 +0000 | [diff] [blame] | 567 | /* Must increment call counter now */ |
| 568 | ++pjsua_var.call_cnt; |
| 569 | |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 570 | /* Send initial INVITE: */ |
| 571 | |
Benny Prijono | e8b0d3b | 2006-03-17 17:57:52 +0000 | [diff] [blame] | 572 | status = pjsip_inv_send_msg(inv, tdata); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 573 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 574 | pjsua_perror(THIS_FILE, "Unable to send initial INVITE request", |
| 575 | status); |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 576 | |
Benny Prijono | 0bd5eb9 | 2009-04-14 11:10:31 +0000 | [diff] [blame] | 577 | /* Upon failure to send first request, the invite |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 578 | * session would have been cleared. |
| 579 | */ |
| 580 | inv = NULL; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 581 | goto on_error; |
| 582 | } |
| 583 | |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 584 | /* Done. */ |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 585 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 586 | if (p_call_id) |
| 587 | *p_call_id = call_id; |
| 588 | |
Benny Prijono | 0bd5eb9 | 2009-04-14 11:10:31 +0000 | [diff] [blame] | 589 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 590 | pj_pool_release(tmp_pool); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 591 | PJSUA_UNLOCK(); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 592 | |
| 593 | return PJ_SUCCESS; |
| 594 | |
| 595 | |
| 596 | on_error: |
Benny Prijono | 0bd5eb9 | 2009-04-14 11:10:31 +0000 | [diff] [blame] | 597 | if (dlg) { |
| 598 | /* This may destroy the dialog */ |
| 599 | pjsip_dlg_dec_lock(dlg); |
| 600 | } |
| 601 | |
Benny Prijono | 1c2bf46 | 2006-03-05 11:54:02 +0000 | [diff] [blame] | 602 | if (inv != NULL) { |
| 603 | pjsip_inv_terminate(inv, PJSIP_SC_OK, PJ_FALSE); |
Benny Prijono | 1c2bf46 | 2006-03-05 11:54:02 +0000 | [diff] [blame] | 604 | } |
| 605 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 606 | if (call_id != -1) { |
| 607 | reset_call(call_id); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 608 | pjsua_media_channel_deinit(call_id); |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 609 | } |
| 610 | |
Benny Prijono | c91ed8d | 2008-07-13 12:24:55 +0000 | [diff] [blame] | 611 | pj_pool_release(tmp_pool); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 612 | PJSUA_UNLOCK(); |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 613 | return status; |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | |
Benny Prijono | 91a6a17 | 2007-10-31 08:59:29 +0000 | [diff] [blame] | 617 | /* Get the NAT type information in remote's SDP */ |
| 618 | static void update_remote_nat_type(pjsua_call *call, |
| 619 | const pjmedia_sdp_session *sdp) |
| 620 | { |
| 621 | const pjmedia_sdp_attr *xnat; |
| 622 | |
| 623 | xnat = pjmedia_sdp_attr_find2(sdp->attr_count, sdp->attr, "X-nat", NULL); |
| 624 | if (xnat) { |
| 625 | call->rem_nat_type = (pj_stun_nat_type) (xnat->value.ptr[0] - '0'); |
| 626 | } else { |
| 627 | call->rem_nat_type = PJ_STUN_NAT_TYPE_UNKNOWN; |
| 628 | } |
| 629 | |
| 630 | PJ_LOG(5,(THIS_FILE, "Call %d: remote NAT type is %d (%s)", call->index, |
| 631 | call->rem_nat_type, pj_stun_get_nat_name(call->rem_nat_type))); |
| 632 | } |
| 633 | |
| 634 | |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 635 | /** |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 636 | * Handle incoming INVITE request. |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 637 | * Called by pjsua_core.c |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 638 | */ |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 639 | pj_bool_t pjsua_call_on_incoming(pjsip_rx_data *rdata) |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 640 | { |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 641 | pj_str_t contact; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 642 | pjsip_dialog *dlg = pjsip_rdata_get_dlg(rdata); |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 643 | pjsip_dialog *replaced_dlg = NULL; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 644 | pjsip_transaction *tsx = pjsip_rdata_get_tsx(rdata); |
| 645 | pjsip_msg *msg = rdata->msg_info.msg; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 646 | pjsip_tx_data *response = NULL; |
| 647 | unsigned options = 0; |
Benny Prijono | 1c2bf46 | 2006-03-05 11:54:02 +0000 | [diff] [blame] | 648 | pjsip_inv_session *inv = NULL; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 649 | int acc_id; |
| 650 | pjsua_call *call; |
| 651 | int call_id = -1; |
Benny Prijono | db844a4 | 2008-02-02 17:07:18 +0000 | [diff] [blame] | 652 | int sip_err_code; |
Benny Prijono | 1c1d734 | 2010-08-01 09:48:51 +0000 | [diff] [blame] | 653 | pjmedia_sdp_session *offer=NULL, *answer; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 654 | pj_status_t status; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 655 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 656 | /* Don't want to handle anything but INVITE */ |
| 657 | if (msg->line.req.method.id != PJSIP_INVITE_METHOD) |
| 658 | return PJ_FALSE; |
| 659 | |
| 660 | /* Don't want to handle anything that's already associated with |
| 661 | * existing dialog or transaction. |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 662 | */ |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 663 | if (dlg || tsx) |
| 664 | return PJ_FALSE; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 665 | |
Benny Prijono | 384dab4 | 2009-10-14 01:58:04 +0000 | [diff] [blame] | 666 | /* Don't want to accept the call if shutdown is in progress */ |
| 667 | if (pjsua_var.thread_quit_flag) { |
| 668 | pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, |
| 669 | PJSIP_SC_TEMPORARILY_UNAVAILABLE, NULL, |
| 670 | NULL, NULL); |
| 671 | return PJ_TRUE; |
| 672 | } |
| 673 | |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 674 | PJSUA_LOCK(); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 675 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 676 | /* Find free call slot. */ |
Benny Prijono | 5773cd6 | 2008-01-19 13:01:42 +0000 | [diff] [blame] | 677 | call_id = alloc_call_id(); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 678 | |
Benny Prijono | 5773cd6 | 2008-01-19 13:01:42 +0000 | [diff] [blame] | 679 | if (call_id == PJSUA_INVALID_ID) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 680 | pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 681 | PJSIP_SC_BUSY_HERE, NULL, |
| 682 | NULL, NULL); |
Benny Prijono | a38ada0 | 2006-07-02 14:22:35 +0000 | [diff] [blame] | 683 | PJ_LOG(2,(THIS_FILE, |
| 684 | "Unable to accept incoming call (too many calls)")); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 685 | PJSUA_UNLOCK(); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 686 | return PJ_TRUE; |
| 687 | } |
| 688 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 689 | /* Clear call descriptor */ |
| 690 | reset_call(call_id); |
Benny Prijono | e21e784 | 2006-04-09 16:46:05 +0000 | [diff] [blame] | 691 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 692 | call = &pjsua_var.calls[call_id]; |
| 693 | |
| 694 | /* Mark call start time. */ |
| 695 | pj_gettimeofday(&call->start_time); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 696 | |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 697 | /* Check INVITE request for Replaces header. If Replaces header is |
| 698 | * present, the function will make sure that we can handle the request. |
| 699 | */ |
| 700 | status = pjsip_replaces_verify_request(rdata, &replaced_dlg, PJ_FALSE, |
| 701 | &response); |
| 702 | if (status != PJ_SUCCESS) { |
| 703 | /* |
| 704 | * Something wrong with the Replaces header. |
| 705 | */ |
| 706 | if (response) { |
| 707 | pjsip_response_addr res_addr; |
| 708 | |
| 709 | pjsip_get_response_addr(response->pool, rdata, &res_addr); |
| 710 | pjsip_endpt_send_response(pjsua_var.endpt, &res_addr, response, |
| 711 | NULL, NULL); |
| 712 | |
| 713 | } else { |
| 714 | |
| 715 | /* Respond with 500 (Internal Server Error) */ |
| 716 | pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, 500, NULL, |
| 717 | NULL, NULL); |
| 718 | } |
| 719 | |
| 720 | PJSUA_UNLOCK(); |
| 721 | return PJ_TRUE; |
| 722 | } |
| 723 | |
| 724 | /* If this INVITE request contains Replaces header, notify application |
| 725 | * about the request so that application can do subsequent checking |
| 726 | * if it wants to. |
| 727 | */ |
| 728 | if (replaced_dlg != NULL && pjsua_var.ua_cfg.cb.on_call_replace_request) { |
| 729 | pjsua_call *replaced_call; |
| 730 | int st_code = 200; |
| 731 | pj_str_t st_text = { "OK", 2 }; |
| 732 | |
| 733 | /* Get the replaced call instance */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 734 | replaced_call = (pjsua_call*) replaced_dlg->mod_data[pjsua_var.mod.id]; |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 735 | |
| 736 | /* Notify application */ |
| 737 | pjsua_var.ua_cfg.cb.on_call_replace_request(replaced_call->index, |
| 738 | rdata, &st_code, &st_text); |
| 739 | |
| 740 | /* Must specify final response */ |
| 741 | PJ_ASSERT_ON_FAIL(st_code >= 200, st_code = 200); |
| 742 | |
| 743 | /* Check if application rejects this request. */ |
| 744 | if (st_code >= 300) { |
| 745 | |
| 746 | if (st_text.slen == 2) |
| 747 | st_text = *pjsip_get_status_text(st_code); |
| 748 | |
| 749 | pjsip_endpt_respond(pjsua_var.endpt, NULL, rdata, |
| 750 | st_code, &st_text, NULL, NULL, NULL); |
| 751 | PJSUA_UNLOCK(); |
| 752 | return PJ_TRUE; |
| 753 | } |
| 754 | } |
| 755 | |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 756 | /* |
| 757 | * Get which account is most likely to be associated with this incoming |
| 758 | * call. We need the account to find which contact URI to put for |
| 759 | * the call. |
| 760 | */ |
| 761 | acc_id = call->acc_id = pjsua_acc_find_for_incoming(rdata); |
Benny Prijono | dd63b99 | 2010-10-01 02:03:42 +0000 | [diff] [blame^] | 762 | 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] | 763 | |
Benny Prijono | db844a4 | 2008-02-02 17:07:18 +0000 | [diff] [blame] | 764 | /* Get call's secure level */ |
| 765 | if (PJSIP_URI_SCHEME_IS_SIPS(rdata->msg_info.msg->line.req.uri)) |
| 766 | call->secure_level = 2; |
| 767 | else if (PJSIP_TRANSPORT_IS_SECURE(rdata->tp_info.transport)) |
| 768 | call->secure_level = 1; |
| 769 | else |
| 770 | call->secure_level = 0; |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 771 | |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 772 | /* Parse SDP from incoming request */ |
| 773 | if (rdata->msg_info.msg->body) { |
Benny Prijono | 1c1d734 | 2010-08-01 09:48:51 +0000 | [diff] [blame] | 774 | pjsip_rdata_sdp_info *sdp_info; |
| 775 | |
| 776 | sdp_info = pjsip_rdata_get_sdp_info(rdata); |
| 777 | offer = sdp_info->sdp; |
| 778 | |
| 779 | status = sdp_info->sdp_err; |
| 780 | if (status==PJ_SUCCESS && sdp_info->sdp==NULL) |
| 781 | status = PJSIP_ERRNO_FROM_SIP_STATUS(PJSIP_SC_NOT_ACCEPTABLE); |
Benny Prijono | 2c484e4 | 2008-06-26 19:47:23 +0000 | [diff] [blame] | 782 | |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 783 | if (status != PJ_SUCCESS) { |
Benny Prijono | 617b860 | 2008-04-07 10:10:31 +0000 | [diff] [blame] | 784 | const pj_str_t reason = pj_str("Bad SDP"); |
Benny Prijono | 2c484e4 | 2008-06-26 19:47:23 +0000 | [diff] [blame] | 785 | pjsip_hdr hdr_list; |
| 786 | pjsip_warning_hdr *w; |
| 787 | |
| 788 | pjsua_perror(THIS_FILE, "Bad SDP in incoming INVITE", |
Benny Prijono | 617b860 | 2008-04-07 10:10:31 +0000 | [diff] [blame] | 789 | status); |
Benny Prijono | 2c484e4 | 2008-06-26 19:47:23 +0000 | [diff] [blame] | 790 | |
| 791 | w = pjsip_warning_hdr_create_from_status(rdata->tp_info.pool, |
| 792 | pjsip_endpt_name(pjsua_var.endpt), |
| 793 | status); |
| 794 | pj_list_init(&hdr_list); |
| 795 | pj_list_push_back(&hdr_list, w); |
| 796 | |
Benny Prijono | 224b4e2 | 2008-06-19 14:10:28 +0000 | [diff] [blame] | 797 | pjsip_endpt_respond(pjsua_var.endpt, NULL, rdata, 400, |
Benny Prijono | 2c484e4 | 2008-06-26 19:47:23 +0000 | [diff] [blame] | 798 | &reason, &hdr_list, NULL, NULL); |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 799 | PJSUA_UNLOCK(); |
| 800 | return PJ_TRUE; |
| 801 | } |
Benny Prijono | 617b860 | 2008-04-07 10:10:31 +0000 | [diff] [blame] | 802 | |
| 803 | /* Do quick checks on SDP before passing it to transports. More elabore |
| 804 | * checks will be done in pjsip_inv_verify_request2() below. |
| 805 | */ |
| 806 | if (offer->media_count==0) { |
| 807 | const pj_str_t reason = pj_str("Missing media in SDP"); |
| 808 | pjsip_endpt_respond(pjsua_var.endpt, NULL, rdata, 400, &reason, |
| 809 | NULL, NULL, NULL); |
| 810 | PJSUA_UNLOCK(); |
| 811 | return PJ_TRUE; |
| 812 | } |
| 813 | |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 814 | } else { |
| 815 | offer = NULL; |
| 816 | } |
Benny Prijono | 1d9b9a4 | 2006-09-25 13:40:12 +0000 | [diff] [blame] | 817 | |
Benny Prijono | 224b4e2 | 2008-06-19 14:10:28 +0000 | [diff] [blame] | 818 | /* Init media channel */ |
| 819 | status = pjsua_media_channel_init(call->index, PJSIP_ROLE_UAS, |
| 820 | call->secure_level, |
| 821 | rdata->tp_info.pool, offer, |
| 822 | &sip_err_code); |
| 823 | if (status != PJ_SUCCESS) { |
| 824 | pjsua_perror(THIS_FILE, "Error initializing media channel", status); |
| 825 | pjsip_endpt_respond(pjsua_var.endpt, NULL, rdata, |
| 826 | sip_err_code, NULL, NULL, NULL, NULL); |
| 827 | PJSUA_UNLOCK(); |
| 828 | return PJ_TRUE; |
| 829 | } |
| 830 | |
| 831 | /* Create answer */ |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 832 | status = pjsua_media_channel_create_sdp(call->index, rdata->tp_info.pool, |
Benny Prijono | 25b2ea1 | 2008-01-24 19:20:54 +0000 | [diff] [blame] | 833 | offer, &answer, &sip_err_code); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 834 | if (status != PJ_SUCCESS) { |
Benny Prijono | 25b2ea1 | 2008-01-24 19:20:54 +0000 | [diff] [blame] | 835 | pjsua_perror(THIS_FILE, "Error creating SDP answer", status); |
Benny Prijono | 224b4e2 | 2008-06-19 14:10:28 +0000 | [diff] [blame] | 836 | pjsip_endpt_respond(pjsua_var.endpt, NULL, rdata, |
| 837 | sip_err_code, NULL, NULL, NULL, NULL); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 838 | PJSUA_UNLOCK(); |
| 839 | return PJ_TRUE; |
| 840 | } |
| 841 | |
Benny Prijono | 224b4e2 | 2008-06-19 14:10:28 +0000 | [diff] [blame] | 842 | |
Benny Prijono | 1d9b9a4 | 2006-09-25 13:40:12 +0000 | [diff] [blame] | 843 | /* Verify that we can handle the request. */ |
Benny Prijono | dcfc0ba | 2007-09-30 16:50:27 +0000 | [diff] [blame] | 844 | options |= PJSIP_INV_SUPPORT_100REL; |
Nanang Izzuddin | 59dffb1 | 2009-08-11 12:42:38 +0000 | [diff] [blame] | 845 | options |= PJSIP_INV_SUPPORT_TIMER; |
Benny Prijono | dcfc0ba | 2007-09-30 16:50:27 +0000 | [diff] [blame] | 846 | if (pjsua_var.acc[acc_id].cfg.require_100rel) |
| 847 | options |= PJSIP_INV_REQUIRE_100REL; |
Benny Prijono | 07fe230 | 2010-06-24 12:33:18 +0000 | [diff] [blame] | 848 | if (pjsua_var.media_cfg.enable_ice) |
| 849 | options |= PJSIP_INV_SUPPORT_ICE; |
Nanang Izzuddin | 742ef4b | 2010-09-07 09:36:15 +0000 | [diff] [blame] | 850 | if (pjsua_var.acc[acc_id].cfg.use_timer == PJSUA_SIP_TIMER_REQUIRED) |
| 851 | options |= PJSIP_INV_REQUIRE_TIMER; |
| 852 | else if (pjsua_var.acc[acc_id].cfg.use_timer == PJSUA_SIP_TIMER_ALWAYS) |
| 853 | options |= PJSIP_INV_ALWAYS_USE_TIMER; |
Benny Prijono | dcfc0ba | 2007-09-30 16:50:27 +0000 | [diff] [blame] | 854 | |
Benny Prijono | d817965 | 2008-01-23 20:39:07 +0000 | [diff] [blame] | 855 | status = pjsip_inv_verify_request2(rdata, &options, offer, answer, NULL, |
| 856 | pjsua_var.endpt, &response); |
Benny Prijono | 1d9b9a4 | 2006-09-25 13:40:12 +0000 | [diff] [blame] | 857 | if (status != PJ_SUCCESS) { |
| 858 | |
| 859 | /* |
| 860 | * No we can't handle the incoming INVITE request. |
| 861 | */ |
Benny Prijono | 1d9b9a4 | 2006-09-25 13:40:12 +0000 | [diff] [blame] | 862 | if (response) { |
| 863 | pjsip_response_addr res_addr; |
| 864 | |
| 865 | pjsip_get_response_addr(response->pool, rdata, &res_addr); |
| 866 | pjsip_endpt_send_response(pjsua_var.endpt, &res_addr, response, |
| 867 | NULL, NULL); |
| 868 | |
| 869 | } else { |
Benny Prijono | 1d9b9a4 | 2006-09-25 13:40:12 +0000 | [diff] [blame] | 870 | /* Respond with 500 (Internal Server Error) */ |
Benny Prijono | 224b4e2 | 2008-06-19 14:10:28 +0000 | [diff] [blame] | 871 | pjsip_endpt_respond(pjsua_var.endpt, NULL, rdata, 500, NULL, |
| 872 | NULL, NULL, NULL); |
Benny Prijono | 1d9b9a4 | 2006-09-25 13:40:12 +0000 | [diff] [blame] | 873 | } |
| 874 | |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 875 | pjsua_media_channel_deinit(call->index); |
Benny Prijono | 1d9b9a4 | 2006-09-25 13:40:12 +0000 | [diff] [blame] | 876 | PJSUA_UNLOCK(); |
| 877 | return PJ_TRUE; |
| 878 | } |
| 879 | |
| 880 | |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 881 | /* Get suitable Contact header */ |
Benny Prijono | ddaaf6a | 2008-04-15 10:37:19 +0000 | [diff] [blame] | 882 | if (pjsua_var.acc[acc_id].contact.slen) { |
| 883 | contact = pjsua_var.acc[acc_id].contact; |
| 884 | } else { |
| 885 | status = pjsua_acc_create_uas_contact(rdata->tp_info.pool, &contact, |
| 886 | acc_id, rdata); |
| 887 | if (status != PJ_SUCCESS) { |
| 888 | pjsua_perror(THIS_FILE, "Unable to generate Contact header", |
| 889 | status); |
| 890 | pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, 500, NULL, |
| 891 | NULL, NULL); |
| 892 | pjsua_media_channel_deinit(call->index); |
| 893 | PJSUA_UNLOCK(); |
| 894 | return PJ_TRUE; |
| 895 | } |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 896 | } |
| 897 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 898 | /* Create dialog: */ |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 899 | status = pjsip_dlg_create_uas( pjsip_ua_instance(), rdata, |
Benny Prijono | c570f2d | 2006-07-18 00:33:02 +0000 | [diff] [blame] | 900 | &contact, &dlg); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 901 | if (status != PJ_SUCCESS) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 902 | pjsip_endpt_respond_stateless(pjsua_var.endpt, rdata, 500, NULL, |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 903 | NULL, NULL); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 904 | pjsua_media_channel_deinit(call->index); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 905 | PJSUA_UNLOCK(); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 906 | return PJ_TRUE; |
| 907 | } |
| 908 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 909 | /* Set credentials */ |
| 910 | if (pjsua_var.acc[acc_id].cred_cnt) { |
| 911 | pjsip_auth_clt_set_credentials(&dlg->auth_sess, |
| 912 | pjsua_var.acc[acc_id].cred_cnt, |
| 913 | pjsua_var.acc[acc_id].cred); |
| 914 | } |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 915 | |
Benny Prijono | 48ab2b7 | 2007-11-08 09:24:30 +0000 | [diff] [blame] | 916 | /* Set preference */ |
| 917 | pjsip_auth_clt_set_prefs(&dlg->auth_sess, |
| 918 | &pjsua_var.acc[acc_id].cfg.auth_pref); |
| 919 | |
Nanang Izzuddin | 742ef4b | 2010-09-07 09:36:15 +0000 | [diff] [blame] | 920 | /* Disable Session Timers if not prefered and the incoming INVITE request |
| 921 | * did not require it. |
| 922 | */ |
| 923 | if (pjsua_var.acc[acc_id].cfg.use_timer == PJSUA_SIP_TIMER_INACTIVE && |
| 924 | (options & PJSIP_INV_REQUIRE_TIMER) == 0) |
| 925 | { |
| 926 | options &= ~(PJSIP_INV_SUPPORT_TIMER); |
| 927 | } |
| 928 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 929 | /* Create invite session: */ |
Benny Prijono | dcfc0ba | 2007-09-30 16:50:27 +0000 | [diff] [blame] | 930 | status = pjsip_inv_create_uas( dlg, rdata, answer, options, &inv); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 931 | if (status != PJ_SUCCESS) { |
Benny Prijono | a38ada0 | 2006-07-02 14:22:35 +0000 | [diff] [blame] | 932 | pjsip_hdr hdr_list; |
| 933 | pjsip_warning_hdr *w; |
| 934 | |
| 935 | w = pjsip_warning_hdr_create_from_status(dlg->pool, |
| 936 | pjsip_endpt_name(pjsua_var.endpt), |
| 937 | status); |
| 938 | pj_list_init(&hdr_list); |
| 939 | pj_list_push_back(&hdr_list, w); |
| 940 | |
| 941 | pjsip_dlg_respond(dlg, rdata, 500, NULL, &hdr_list, NULL); |
| 942 | |
| 943 | /* Can't terminate dialog because transaction is in progress. |
Benny Prijono | 1c2bf46 | 2006-03-05 11:54:02 +0000 | [diff] [blame] | 944 | pjsip_dlg_terminate(dlg); |
Benny Prijono | a38ada0 | 2006-07-02 14:22:35 +0000 | [diff] [blame] | 945 | */ |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 946 | pjsua_media_channel_deinit(call->index); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 947 | PJSUA_UNLOCK(); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 948 | return PJ_TRUE; |
| 949 | } |
| 950 | |
Nanang Izzuddin | 59dffb1 | 2009-08-11 12:42:38 +0000 | [diff] [blame] | 951 | /* Init Session Timers */ |
Nanang Izzuddin | 65add62 | 2009-08-11 16:26:20 +0000 | [diff] [blame] | 952 | status = pjsip_timer_init_session(inv, |
| 953 | &pjsua_var.acc[acc_id].cfg.timer_setting); |
| 954 | if (status != PJ_SUCCESS) { |
| 955 | pjsua_perror(THIS_FILE, "Session Timer init failed", status); |
| 956 | status = pjsip_inv_end_session(inv, PJSIP_SC_INTERNAL_SERVER_ERROR, |
| 957 | NULL, &response); |
| 958 | if (status == PJ_SUCCESS && response) |
| 959 | status = pjsip_inv_send_msg(inv, response); |
Nanang Izzuddin | 59dffb1 | 2009-08-11 12:42:38 +0000 | [diff] [blame] | 960 | |
Nanang Izzuddin | 65add62 | 2009-08-11 16:26:20 +0000 | [diff] [blame] | 961 | pjsua_media_channel_deinit(call->index); |
Nanang Izzuddin | 59dffb1 | 2009-08-11 12:42:38 +0000 | [diff] [blame] | 962 | |
Nanang Izzuddin | 65add62 | 2009-08-11 16:26:20 +0000 | [diff] [blame] | 963 | PJSUA_UNLOCK(); |
| 964 | return PJ_TRUE; |
Nanang Izzuddin | 59dffb1 | 2009-08-11 12:42:38 +0000 | [diff] [blame] | 965 | } |
| 966 | |
Benny Prijono | ea9fd39 | 2007-11-06 03:41:40 +0000 | [diff] [blame] | 967 | /* Update NAT type of remote endpoint, only when there is SDP in |
| 968 | * incoming INVITE! |
| 969 | */ |
| 970 | if (pjsua_var.ua_cfg.nat_type_in_sdp && |
| 971 | pjmedia_sdp_neg_get_state(inv->neg) > PJMEDIA_SDP_NEG_STATE_LOCAL_OFFER) |
| 972 | { |
Benny Prijono | 91a6a17 | 2007-10-31 08:59:29 +0000 | [diff] [blame] | 973 | const pjmedia_sdp_session *remote_sdp; |
| 974 | |
| 975 | if (pjmedia_sdp_neg_get_neg_remote(inv->neg, &remote_sdp)==PJ_SUCCESS) |
| 976 | update_remote_nat_type(call, remote_sdp); |
| 977 | } |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 978 | |
Benny Prijono | 62c5c5b | 2007-01-13 23:22:40 +0000 | [diff] [blame] | 979 | /* If account is locked to specific transport, then lock dialog |
| 980 | * to this transport too. |
| 981 | */ |
| 982 | if (pjsua_var.acc[acc_id].cfg.transport_id != PJSUA_INVALID_ID) { |
| 983 | pjsip_tpselector tp_sel; |
| 984 | |
| 985 | pjsua_init_tpselector(pjsua_var.acc[acc_id].cfg.transport_id, &tp_sel); |
| 986 | pjsip_dlg_set_transport(dlg, &tp_sel); |
| 987 | } |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 988 | |
Benny Prijono | 2285e7e | 2008-12-17 14:28:18 +0000 | [diff] [blame] | 989 | /* Must answer with some response to initial INVITE. We'll do this before |
| 990 | * attaching the call to the invite session/dialog, so that the application |
| 991 | * will not get notification about this event (on another scenario, it is |
| 992 | * also possible that inv_send_msg() fails and causes the invite session to |
| 993 | * be disconnected. If we have the call attached at this time, this will |
| 994 | * cause the disconnection callback to be called before on_incoming_call() |
| 995 | * callback is called, which is not right). |
Benny Prijono | 64f851e | 2006-02-23 13:49:28 +0000 | [diff] [blame] | 996 | */ |
Benny Prijono | 64f851e | 2006-02-23 13:49:28 +0000 | [diff] [blame] | 997 | status = pjsip_inv_initial_answer(inv, rdata, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 998 | 100, NULL, NULL, &response); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 999 | if (status != PJ_SUCCESS) { |
Nanang Izzuddin | 59dffb1 | 2009-08-11 12:42:38 +0000 | [diff] [blame] | 1000 | if (response == NULL) { |
| 1001 | pjsua_perror(THIS_FILE, "Unable to send answer to incoming INVITE", |
| 1002 | status); |
| 1003 | pjsip_dlg_respond(dlg, rdata, 500, NULL, NULL, NULL); |
| 1004 | pjsip_inv_terminate(inv, 500, PJ_FALSE); |
| 1005 | } else { |
| 1006 | pjsip_inv_send_msg(inv, response); |
Nanang Izzuddin | 65add62 | 2009-08-11 16:26:20 +0000 | [diff] [blame] | 1007 | pjsip_inv_terminate(inv, response->msg->line.status.code, |
Nanang Izzuddin | 59dffb1 | 2009-08-11 12:42:38 +0000 | [diff] [blame] | 1008 | PJ_FALSE); |
| 1009 | } |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1010 | pjsua_media_channel_deinit(call->index); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1011 | PJSUA_UNLOCK(); |
Benny Prijono | ccb03fa | 2006-03-06 13:35:47 +0000 | [diff] [blame] | 1012 | return PJ_TRUE; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1013 | |
| 1014 | } else { |
Benny Prijono | e8b0d3b | 2006-03-17 17:57:52 +0000 | [diff] [blame] | 1015 | status = pjsip_inv_send_msg(inv, response); |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1016 | if (status != PJ_SUCCESS) { |
Benny Prijono | d2ae29d | 2006-02-22 15:42:31 +0000 | [diff] [blame] | 1017 | pjsua_perror(THIS_FILE, "Unable to send 100 response", status); |
Benny Prijono | 2285e7e | 2008-12-17 14:28:18 +0000 | [diff] [blame] | 1018 | PJSUA_UNLOCK(); |
| 1019 | return PJ_TRUE; |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 1020 | } |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1021 | } |
| 1022 | |
Benny Prijono | 2285e7e | 2008-12-17 14:28:18 +0000 | [diff] [blame] | 1023 | /* Create and attach pjsua_var data to the dialog: */ |
| 1024 | call->inv = inv; |
| 1025 | |
| 1026 | dlg->mod_data[pjsua_var.mod.id] = call; |
| 1027 | inv->mod_data[pjsua_var.mod.id] = call; |
| 1028 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1029 | ++pjsua_var.call_cnt; |
Benny Prijono | 64f851e | 2006-02-23 13:49:28 +0000 | [diff] [blame] | 1030 | |
Benny Prijono | 105217f | 2006-03-06 16:25:59 +0000 | [diff] [blame] | 1031 | |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 1032 | /* Check if this request should replace existing call */ |
| 1033 | if (replaced_dlg) { |
| 1034 | pjsip_inv_session *replaced_inv; |
| 1035 | struct pjsua_call *replaced_call; |
| 1036 | pjsip_tx_data *tdata; |
| 1037 | |
| 1038 | /* Get the invite session in the dialog */ |
| 1039 | replaced_inv = pjsip_dlg_get_inv_session(replaced_dlg); |
| 1040 | |
| 1041 | /* Get the replaced call instance */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 1042 | replaced_call = (pjsua_call*) replaced_dlg->mod_data[pjsua_var.mod.id]; |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 1043 | |
| 1044 | /* Notify application */ |
| 1045 | if (pjsua_var.ua_cfg.cb.on_call_replaced) |
| 1046 | pjsua_var.ua_cfg.cb.on_call_replaced(replaced_call->index, |
| 1047 | call_id); |
| 1048 | |
| 1049 | PJ_LOG(4,(THIS_FILE, "Answering replacement call %d with 200/OK", |
| 1050 | call_id)); |
| 1051 | |
| 1052 | /* Answer the new call with 200 response */ |
| 1053 | status = pjsip_inv_answer(inv, 200, NULL, NULL, &tdata); |
| 1054 | if (status == PJ_SUCCESS) |
| 1055 | status = pjsip_inv_send_msg(inv, tdata); |
| 1056 | |
| 1057 | if (status != PJ_SUCCESS) |
| 1058 | pjsua_perror(THIS_FILE, "Error answering session", status); |
| 1059 | |
Benny Prijono | fed7f4c | 2007-03-27 11:01:45 +0000 | [diff] [blame] | 1060 | /* Note that inv may be invalid if 200/OK has caused error in |
| 1061 | * starting the media. |
| 1062 | */ |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 1063 | |
| 1064 | PJ_LOG(4,(THIS_FILE, "Disconnecting replaced call %d", |
| 1065 | replaced_call->index)); |
| 1066 | |
| 1067 | /* Disconnect replaced invite session */ |
| 1068 | status = pjsip_inv_end_session(replaced_inv, PJSIP_SC_GONE, NULL, |
| 1069 | &tdata); |
| 1070 | if (status == PJ_SUCCESS && tdata) |
| 1071 | status = pjsip_inv_send_msg(replaced_inv, tdata); |
| 1072 | |
| 1073 | if (status != PJ_SUCCESS) |
| 1074 | pjsua_perror(THIS_FILE, "Error terminating session", status); |
| 1075 | |
| 1076 | |
| 1077 | } else { |
| 1078 | |
Benny Prijono | b5388cf | 2007-01-04 22:45:08 +0000 | [diff] [blame] | 1079 | /* Notify application if on_incoming_call() is overriden, |
| 1080 | * otherwise hangup the call with 480 |
| 1081 | */ |
| 1082 | if (pjsua_var.ua_cfg.cb.on_incoming_call) { |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 1083 | 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] | 1084 | } else { |
| 1085 | pjsua_call_hangup(call_id, PJSIP_SC_TEMPORARILY_UNAVAILABLE, |
| 1086 | NULL, NULL); |
| 1087 | } |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 1088 | } |
| 1089 | |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 1090 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1091 | /* This INVITE request has been handled. */ |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1092 | PJSUA_UNLOCK(); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1093 | return PJ_TRUE; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 1094 | } |
| 1095 | |
| 1096 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1097 | |
| 1098 | /* |
| 1099 | * Check if the specified call has active INVITE session and the INVITE |
| 1100 | * session has not been disconnected. |
| 1101 | */ |
| 1102 | PJ_DEF(pj_bool_t) pjsua_call_is_active(pjsua_call_id call_id) |
| 1103 | { |
| 1104 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1105 | PJ_EINVAL); |
| 1106 | return pjsua_var.calls[call_id].inv != NULL && |
| 1107 | pjsua_var.calls[call_id].inv->state != PJSIP_INV_STATE_DISCONNECTED; |
| 1108 | } |
| 1109 | |
| 1110 | |
| 1111 | /* |
| 1112 | * Check if call has an active media session. |
| 1113 | */ |
| 1114 | PJ_DEF(pj_bool_t) pjsua_call_has_media(pjsua_call_id call_id) |
| 1115 | { |
| 1116 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1117 | PJ_EINVAL); |
| 1118 | return pjsua_var.calls[call_id].session != NULL; |
| 1119 | } |
| 1120 | |
| 1121 | |
Benny Prijono | cf986c4 | 2008-09-02 11:25:07 +0000 | [diff] [blame] | 1122 | /* |
| 1123 | * Retrieve the media session associated with this call. |
| 1124 | */ |
| 1125 | PJ_DEF(pjmedia_session*) pjsua_call_get_media_session(pjsua_call_id call_id) |
| 1126 | { |
| 1127 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1128 | NULL); |
| 1129 | return pjsua_var.calls[call_id].session; |
| 1130 | } |
| 1131 | |
| 1132 | |
| 1133 | /* |
| 1134 | * Retrieve the media transport instance that is used for this call. |
| 1135 | */ |
| 1136 | PJ_DEF(pjmedia_transport*) pjsua_call_get_media_transport(pjsua_call_id cid) |
| 1137 | { |
| 1138 | PJ_ASSERT_RETURN(cid>=0 && cid<(int)pjsua_var.ua_cfg.max_calls, |
| 1139 | NULL); |
| 1140 | return pjsua_var.calls[cid].med_tp; |
| 1141 | } |
| 1142 | |
| 1143 | |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1144 | /* Acquire lock to the specified call_id */ |
Benny Prijono | 627cbb4 | 2007-09-25 20:48:49 +0000 | [diff] [blame] | 1145 | pj_status_t acquire_call(const char *title, |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1146 | pjsua_call_id call_id, |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1147 | pjsua_call **p_call, |
| 1148 | pjsip_dialog **p_dlg) |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1149 | { |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1150 | unsigned retry; |
Benny Prijono | d524e82 | 2006-09-22 12:48:18 +0000 | [diff] [blame] | 1151 | pjsua_call *call = NULL; |
| 1152 | pj_bool_t has_pjsua_lock = PJ_FALSE; |
| 1153 | pj_status_t status = PJ_SUCCESS; |
Sauw Ming | 844c1c9 | 2010-09-07 05:12:02 +0000 | [diff] [blame] | 1154 | pj_time_val time_start, timeout; |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1155 | |
Sauw Ming | 844c1c9 | 2010-09-07 05:12:02 +0000 | [diff] [blame] | 1156 | pj_gettimeofday(&time_start); |
| 1157 | timeout.msec = PJSUA_ACQUIRE_CALL_TIMEOUT; |
| 1158 | pj_time_val_normalize(&timeout); |
| 1159 | |
| 1160 | for (retry=0; ; ++retry) { |
| 1161 | |
| 1162 | if (retry % 10 == 9) { |
| 1163 | pj_time_val dtime; |
| 1164 | |
| 1165 | pj_gettimeofday(&dtime); |
| 1166 | PJ_TIME_VAL_SUB(dtime, time_start); |
| 1167 | if (!PJ_TIME_VAL_LT(dtime, timeout)) |
| 1168 | break; |
| 1169 | } |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1170 | |
| 1171 | has_pjsua_lock = PJ_FALSE; |
| 1172 | |
| 1173 | status = PJSUA_TRY_LOCK(); |
| 1174 | if (status != PJ_SUCCESS) { |
| 1175 | pj_thread_sleep(retry/10); |
| 1176 | continue; |
| 1177 | } |
| 1178 | |
| 1179 | has_pjsua_lock = PJ_TRUE; |
| 1180 | call = &pjsua_var.calls[call_id]; |
| 1181 | |
| 1182 | if (call->inv == NULL) { |
| 1183 | PJSUA_UNLOCK(); |
| 1184 | PJ_LOG(3,(THIS_FILE, "Invalid call_id %d in %s", call_id, title)); |
| 1185 | return PJSIP_ESESSIONTERMINATED; |
| 1186 | } |
| 1187 | |
| 1188 | status = pjsip_dlg_try_inc_lock(call->inv->dlg); |
| 1189 | if (status != PJ_SUCCESS) { |
| 1190 | PJSUA_UNLOCK(); |
| 1191 | pj_thread_sleep(retry/10); |
| 1192 | continue; |
| 1193 | } |
| 1194 | |
| 1195 | PJSUA_UNLOCK(); |
| 1196 | |
| 1197 | break; |
| 1198 | } |
| 1199 | |
| 1200 | if (status != PJ_SUCCESS) { |
| 1201 | if (has_pjsua_lock == PJ_FALSE) |
| 1202 | PJ_LOG(1,(THIS_FILE, "Timed-out trying to acquire PJSUA mutex " |
| 1203 | "(possibly system has deadlocked) in %s", |
| 1204 | title)); |
| 1205 | else |
| 1206 | PJ_LOG(1,(THIS_FILE, "Timed-out trying to acquire dialog mutex " |
| 1207 | "(possibly system has deadlocked) in %s", |
| 1208 | title)); |
| 1209 | return PJ_ETIMEDOUT; |
| 1210 | } |
| 1211 | |
| 1212 | *p_call = call; |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1213 | *p_dlg = call->inv->dlg; |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1214 | |
| 1215 | return PJ_SUCCESS; |
| 1216 | } |
| 1217 | |
| 1218 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1219 | /* |
| 1220 | * Get the conference port identification associated with the call. |
| 1221 | */ |
| 1222 | PJ_DEF(pjsua_conf_port_id) pjsua_call_get_conf_port(pjsua_call_id call_id) |
| 1223 | { |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1224 | pjsua_call *call; |
| 1225 | pjsua_conf_port_id port_id; |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1226 | pjsip_dialog *dlg; |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1227 | pj_status_t status; |
| 1228 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1229 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1230 | PJ_EINVAL); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1231 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1232 | status = acquire_call("pjsua_call_get_conf_port()", call_id, &call, &dlg); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1233 | if (status != PJ_SUCCESS) |
Benny Prijono | d524e82 | 2006-09-22 12:48:18 +0000 | [diff] [blame] | 1234 | return PJSUA_INVALID_ID; |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1235 | |
| 1236 | port_id = call->conf_slot; |
| 1237 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1238 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1239 | |
| 1240 | return port_id; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1241 | } |
| 1242 | |
| 1243 | |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1244 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1245 | /* |
| 1246 | * Obtain detail information about the specified call. |
| 1247 | */ |
| 1248 | PJ_DEF(pj_status_t) pjsua_call_get_info( pjsua_call_id call_id, |
| 1249 | pjsua_call_info *info) |
| 1250 | { |
| 1251 | pjsua_call *call; |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1252 | pjsip_dialog *dlg; |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1253 | pj_status_t status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1254 | |
| 1255 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1256 | PJ_EINVAL); |
| 1257 | |
Benny Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 1258 | pj_bzero(info, sizeof(*info)); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1259 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1260 | status = acquire_call("pjsua_call_get_info()", call_id, &call, &dlg); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1261 | if (status != PJ_SUCCESS) { |
| 1262 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1263 | } |
| 1264 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1265 | /* id and role */ |
| 1266 | info->id = call_id; |
| 1267 | info->role = call->inv->role; |
Benny Prijono | 9031551 | 2006-09-14 16:05:16 +0000 | [diff] [blame] | 1268 | info->acc_id = call->acc_id; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1269 | |
| 1270 | /* local info */ |
| 1271 | info->local_info.ptr = info->buf_.local_info; |
| 1272 | pj_strncpy(&info->local_info, &call->inv->dlg->local.info_str, |
| 1273 | sizeof(info->buf_.local_info)); |
| 1274 | |
| 1275 | /* local contact */ |
| 1276 | info->local_contact.ptr = info->buf_.local_contact; |
| 1277 | info->local_contact.slen = pjsip_uri_print(PJSIP_URI_IN_CONTACT_HDR, |
| 1278 | call->inv->dlg->local.contact->uri, |
| 1279 | info->local_contact.ptr, |
| 1280 | sizeof(info->buf_.local_contact)); |
| 1281 | |
| 1282 | /* remote info */ |
| 1283 | info->remote_info.ptr = info->buf_.remote_info; |
| 1284 | pj_strncpy(&info->remote_info, &call->inv->dlg->remote.info_str, |
| 1285 | sizeof(info->buf_.remote_info)); |
| 1286 | |
| 1287 | /* remote contact */ |
| 1288 | if (call->inv->dlg->remote.contact) { |
| 1289 | int len; |
| 1290 | info->remote_contact.ptr = info->buf_.remote_contact; |
| 1291 | len = pjsip_uri_print(PJSIP_URI_IN_CONTACT_HDR, |
| 1292 | call->inv->dlg->remote.contact->uri, |
| 1293 | info->remote_contact.ptr, |
| 1294 | sizeof(info->buf_.remote_contact)); |
| 1295 | if (len < 0) len = 0; |
| 1296 | info->remote_contact.slen = len; |
| 1297 | } else { |
| 1298 | info->remote_contact.slen = 0; |
| 1299 | } |
| 1300 | |
| 1301 | /* call id */ |
| 1302 | info->call_id.ptr = info->buf_.call_id; |
| 1303 | pj_strncpy(&info->call_id, &call->inv->dlg->call_id->id, |
| 1304 | sizeof(info->buf_.call_id)); |
| 1305 | |
| 1306 | /* state, state_text */ |
| 1307 | info->state = call->inv->state; |
| 1308 | info->state_text = pj_str((char*)pjsip_inv_state_name(info->state)); |
| 1309 | |
| 1310 | /* If call is disconnected, set the last_status from the cause code */ |
| 1311 | if (call->inv->state >= PJSIP_INV_STATE_DISCONNECTED) { |
| 1312 | /* last_status, last_status_text */ |
| 1313 | info->last_status = call->inv->cause; |
| 1314 | |
| 1315 | info->last_status_text.ptr = info->buf_.last_status_text; |
| 1316 | pj_strncpy(&info->last_status_text, &call->inv->cause_text, |
| 1317 | sizeof(info->buf_.last_status_text)); |
| 1318 | } else { |
| 1319 | /* last_status, last_status_text */ |
| 1320 | info->last_status = call->last_code; |
| 1321 | |
| 1322 | info->last_status_text.ptr = info->buf_.last_status_text; |
| 1323 | pj_strncpy(&info->last_status_text, &call->last_text, |
| 1324 | sizeof(info->buf_.last_status_text)); |
| 1325 | } |
| 1326 | |
| 1327 | /* media status and dir */ |
| 1328 | info->media_status = call->media_st; |
| 1329 | info->media_dir = call->media_dir; |
| 1330 | |
| 1331 | |
| 1332 | /* conference slot number */ |
| 1333 | info->conf_slot = call->conf_slot; |
| 1334 | |
| 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 | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1957 | if (!call->session) { |
| 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 | |
| 1963 | status = pjmedia_session_dial_dtmf( call->session, 0, digits); |
| 1964 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1965 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1966 | |
| 1967 | return status; |
| 1968 | } |
| 1969 | |
| 1970 | |
| 1971 | /** |
| 1972 | * Send instant messaging inside INVITE session. |
| 1973 | */ |
| 1974 | PJ_DEF(pj_status_t) pjsua_call_send_im( pjsua_call_id call_id, |
| 1975 | const pj_str_t *mime_type, |
| 1976 | const pj_str_t *content, |
| 1977 | const pjsua_msg_data *msg_data, |
| 1978 | void *user_data) |
| 1979 | { |
| 1980 | pjsua_call *call; |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1981 | pjsip_dialog *dlg; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1982 | const pj_str_t mime_text_plain = pj_str("text/plain"); |
| 1983 | pjsip_media_type ctype; |
| 1984 | pjsua_im_data *im_data; |
| 1985 | pjsip_tx_data *tdata; |
| 1986 | pj_status_t status; |
| 1987 | |
| 1988 | |
| 1989 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 1990 | PJ_EINVAL); |
| 1991 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 1992 | status = acquire_call("pjsua_call_send_im()", call_id, &call, &dlg); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 1993 | if (status != PJ_SUCCESS) |
| 1994 | return status; |
| 1995 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 1996 | /* Set default media type if none is specified */ |
| 1997 | if (mime_type == NULL) { |
| 1998 | mime_type = &mime_text_plain; |
| 1999 | } |
| 2000 | |
| 2001 | /* Create request message. */ |
| 2002 | status = pjsip_dlg_create_request( call->inv->dlg, &pjsip_message_method, |
| 2003 | -1, &tdata); |
| 2004 | if (status != PJ_SUCCESS) { |
| 2005 | pjsua_perror(THIS_FILE, "Unable to create MESSAGE request", status); |
| 2006 | goto on_return; |
| 2007 | } |
| 2008 | |
| 2009 | /* Add accept header. */ |
| 2010 | pjsip_msg_add_hdr( tdata->msg, |
| 2011 | (pjsip_hdr*)pjsua_im_create_accept(tdata->pool)); |
| 2012 | |
| 2013 | /* Parse MIME type */ |
| 2014 | pjsua_parse_media_type(tdata->pool, mime_type, &ctype); |
| 2015 | |
| 2016 | /* Create "text/plain" message body. */ |
| 2017 | tdata->msg->body = pjsip_msg_body_create( tdata->pool, &ctype.type, |
| 2018 | &ctype.subtype, content); |
| 2019 | if (tdata->msg->body == NULL) { |
| 2020 | pjsua_perror(THIS_FILE, "Unable to create msg body", PJ_ENOMEM); |
| 2021 | pjsip_tx_data_dec_ref(tdata); |
| 2022 | goto on_return; |
| 2023 | } |
| 2024 | |
| 2025 | /* Add additional headers etc */ |
| 2026 | pjsua_process_msg_data( tdata, msg_data); |
| 2027 | |
| 2028 | /* Create IM data and attach to the request. */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 2029 | im_data = PJ_POOL_ZALLOC_T(tdata->pool, pjsua_im_data); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2030 | im_data->acc_id = call->acc_id; |
| 2031 | im_data->call_id = call_id; |
| 2032 | im_data->to = call->inv->dlg->remote.info_str; |
| 2033 | pj_strdup_with_null(tdata->pool, &im_data->body, content); |
| 2034 | im_data->user_data = user_data; |
| 2035 | |
| 2036 | |
| 2037 | /* Send the request. */ |
| 2038 | status = pjsip_dlg_send_request( call->inv->dlg, tdata, |
| 2039 | pjsua_var.mod.id, im_data); |
| 2040 | if (status != PJ_SUCCESS) { |
| 2041 | pjsua_perror(THIS_FILE, "Unable to send MESSAGE request", status); |
| 2042 | goto on_return; |
| 2043 | } |
| 2044 | |
| 2045 | on_return: |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 2046 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2047 | return status; |
| 2048 | } |
| 2049 | |
| 2050 | |
| 2051 | /* |
| 2052 | * Send IM typing indication inside INVITE session. |
| 2053 | */ |
| 2054 | PJ_DEF(pj_status_t) pjsua_call_send_typing_ind( pjsua_call_id call_id, |
| 2055 | pj_bool_t is_typing, |
| 2056 | const pjsua_msg_data*msg_data) |
| 2057 | { |
| 2058 | pjsua_call *call; |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 2059 | pjsip_dialog *dlg; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2060 | pjsip_tx_data *tdata; |
| 2061 | pj_status_t status; |
| 2062 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2063 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 2064 | PJ_EINVAL); |
| 2065 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 2066 | status = acquire_call("pjsua_call_send_typing_ind", call_id, &call, &dlg); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 2067 | if (status != PJ_SUCCESS) |
| 2068 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2069 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2070 | /* Create request message. */ |
| 2071 | status = pjsip_dlg_create_request( call->inv->dlg, &pjsip_message_method, |
| 2072 | -1, &tdata); |
| 2073 | if (status != PJ_SUCCESS) { |
| 2074 | pjsua_perror(THIS_FILE, "Unable to create MESSAGE request", status); |
| 2075 | goto on_return; |
| 2076 | } |
| 2077 | |
| 2078 | /* Create "application/im-iscomposing+xml" msg body. */ |
| 2079 | tdata->msg->body = pjsip_iscomposing_create_body(tdata->pool, is_typing, |
| 2080 | NULL, NULL, -1); |
| 2081 | |
| 2082 | /* Add additional headers etc */ |
| 2083 | pjsua_process_msg_data( tdata, msg_data); |
| 2084 | |
| 2085 | /* Send the request. */ |
| 2086 | status = pjsip_dlg_send_request( call->inv->dlg, tdata, -1, NULL); |
| 2087 | if (status != PJ_SUCCESS) { |
| 2088 | pjsua_perror(THIS_FILE, "Unable to send MESSAGE request", status); |
| 2089 | goto on_return; |
| 2090 | } |
| 2091 | |
| 2092 | on_return: |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 2093 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2094 | return status; |
| 2095 | } |
| 2096 | |
| 2097 | |
| 2098 | /* |
Benny Prijono | feb69f4 | 2007-10-05 09:12:26 +0000 | [diff] [blame] | 2099 | * Send arbitrary request. |
| 2100 | */ |
| 2101 | PJ_DEF(pj_status_t) pjsua_call_send_request(pjsua_call_id call_id, |
| 2102 | const pj_str_t *method_str, |
| 2103 | const pjsua_msg_data *msg_data) |
| 2104 | { |
| 2105 | pjsua_call *call; |
| 2106 | pjsip_dialog *dlg; |
| 2107 | pjsip_method method; |
| 2108 | pjsip_tx_data *tdata; |
| 2109 | pj_status_t status; |
| 2110 | |
| 2111 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 2112 | PJ_EINVAL); |
| 2113 | |
| 2114 | status = acquire_call("pjsua_call_send_request", call_id, &call, &dlg); |
| 2115 | if (status != PJ_SUCCESS) |
| 2116 | return status; |
| 2117 | |
| 2118 | /* Init method */ |
| 2119 | pjsip_method_init_np(&method, (pj_str_t*)method_str); |
| 2120 | |
| 2121 | /* Create request message. */ |
| 2122 | status = pjsip_dlg_create_request( call->inv->dlg, &method, -1, &tdata); |
| 2123 | if (status != PJ_SUCCESS) { |
| 2124 | pjsua_perror(THIS_FILE, "Unable to create request", status); |
| 2125 | goto on_return; |
| 2126 | } |
| 2127 | |
| 2128 | /* Add additional headers etc */ |
| 2129 | pjsua_process_msg_data( tdata, msg_data); |
| 2130 | |
| 2131 | /* Send the request. */ |
| 2132 | status = pjsip_dlg_send_request( call->inv->dlg, tdata, -1, NULL); |
| 2133 | if (status != PJ_SUCCESS) { |
| 2134 | pjsua_perror(THIS_FILE, "Unable to send request", status); |
| 2135 | goto on_return; |
| 2136 | } |
| 2137 | |
| 2138 | on_return: |
| 2139 | pjsip_dlg_dec_lock(dlg); |
| 2140 | return status; |
| 2141 | } |
| 2142 | |
| 2143 | |
| 2144 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2145 | * Terminate all calls. |
| 2146 | */ |
| 2147 | PJ_DEF(void) pjsua_call_hangup_all(void) |
| 2148 | { |
| 2149 | unsigned i; |
| 2150 | |
| 2151 | PJSUA_LOCK(); |
| 2152 | |
| 2153 | for (i=0; i<pjsua_var.ua_cfg.max_calls; ++i) { |
| 2154 | if (pjsua_var.calls[i].inv) |
| 2155 | pjsua_call_hangup(i, 0, NULL, NULL); |
| 2156 | } |
| 2157 | |
| 2158 | PJSUA_UNLOCK(); |
| 2159 | } |
| 2160 | |
| 2161 | |
Benny Prijono | 627cbb4 | 2007-09-25 20:48:49 +0000 | [diff] [blame] | 2162 | const char *good_number(char *buf, pj_int32_t val) |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2163 | { |
| 2164 | if (val < 1000) { |
| 2165 | pj_ansi_sprintf(buf, "%d", val); |
| 2166 | } else if (val < 1000000) { |
| 2167 | pj_ansi_sprintf(buf, "%d.%dK", |
| 2168 | val / 1000, |
| 2169 | (val % 1000) / 100); |
| 2170 | } else { |
| 2171 | pj_ansi_sprintf(buf, "%d.%02dM", |
| 2172 | val / 1000000, |
| 2173 | (val % 1000000) / 10000); |
| 2174 | } |
| 2175 | |
| 2176 | return buf; |
| 2177 | } |
| 2178 | |
| 2179 | |
| 2180 | /* Dump media session */ |
| 2181 | static void dump_media_session(const char *indent, |
| 2182 | char *buf, unsigned maxlen, |
Nanang Izzuddin | ff2b110 | 2008-09-17 15:32:06 +0000 | [diff] [blame] | 2183 | pjsua_call *call) |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2184 | { |
| 2185 | unsigned i; |
| 2186 | char *p = buf, *end = buf+maxlen; |
| 2187 | int len; |
| 2188 | pjmedia_session_info info; |
Nanang Izzuddin | ff2b110 | 2008-09-17 15:32:06 +0000 | [diff] [blame] | 2189 | pjmedia_session *session = call->session; |
| 2190 | pjmedia_transport_info tp_info; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2191 | |
Nanang Izzuddin | ff2b110 | 2008-09-17 15:32:06 +0000 | [diff] [blame] | 2192 | pjmedia_transport_info_init(&tp_info); |
| 2193 | |
| 2194 | pjmedia_transport_get_info(call->med_tp, &tp_info); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2195 | pjmedia_session_get_info(session, &info); |
| 2196 | |
| 2197 | for (i=0; i<info.stream_cnt; ++i) { |
| 2198 | pjmedia_rtcp_stat stat; |
Benny Prijono | 5186eae | 2007-12-03 14:38:25 +0000 | [diff] [blame] | 2199 | char rem_addr_buf[80]; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2200 | const char *rem_addr; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2201 | const char *dir; |
Benny Prijono | e85bc41 | 2006-07-29 20:29:24 +0000 | [diff] [blame] | 2202 | char last_update[64]; |
Benny Prijono | 427af7d | 2007-05-11 10:36:40 +0000 | [diff] [blame] | 2203 | char packets[32], bytes[32], ipbytes[32], avg_bps[32], avg_ipbps[32]; |
Benny Prijono | 80019eb | 2006-08-07 13:22:23 +0000 | [diff] [blame] | 2204 | pj_time_val media_duration, now; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2205 | |
| 2206 | pjmedia_session_get_stream_stat(session, i, &stat); |
Nanang Izzuddin | ff2b110 | 2008-09-17 15:32:06 +0000 | [diff] [blame] | 2207 | // rem_addr will contain actual address of RTP originator, instead of |
| 2208 | // remote RTP address specified by stream which is fetched from the SDP. |
| 2209 | // Please note that we are assuming only one stream per call. |
| 2210 | //rem_addr = pj_sockaddr_print(&info.stream_info[i].rem_addr, |
| 2211 | // rem_addr_buf, sizeof(rem_addr_buf), 3); |
Nanang Izzuddin | 4494a48 | 2008-09-18 12:58:33 +0000 | [diff] [blame] | 2212 | if (pj_sockaddr_has_addr(&tp_info.src_rtp_name)) { |
| 2213 | rem_addr = pj_sockaddr_print(&tp_info.src_rtp_name, rem_addr_buf, |
Nanang Izzuddin | ff2b110 | 2008-09-17 15:32:06 +0000 | [diff] [blame] | 2214 | sizeof(rem_addr_buf), 3); |
| 2215 | } else { |
| 2216 | pj_ansi_snprintf(rem_addr_buf, sizeof(rem_addr_buf), "-"); |
Nanang Izzuddin | 4494a48 | 2008-09-18 12:58:33 +0000 | [diff] [blame] | 2217 | rem_addr = rem_addr_buf; |
Nanang Izzuddin | ff2b110 | 2008-09-17 15:32:06 +0000 | [diff] [blame] | 2218 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2219 | |
Benny Prijono | 2dbf507 | 2010-06-23 12:38:28 +0000 | [diff] [blame] | 2220 | if (call->media_dir == PJMEDIA_DIR_NONE) { |
| 2221 | /* To handle when the stream that is currently being paused |
| 2222 | * (http://trac.pjsip.org/repos/ticket/1079) |
| 2223 | */ |
| 2224 | dir = "inactive"; |
| 2225 | } else if (info.stream_info[i].dir == PJMEDIA_DIR_ENCODING) |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2226 | dir = "sendonly"; |
| 2227 | else if (info.stream_info[i].dir == PJMEDIA_DIR_DECODING) |
| 2228 | dir = "recvonly"; |
| 2229 | else if (info.stream_info[i].dir == PJMEDIA_DIR_ENCODING_DECODING) |
| 2230 | dir = "sendrecv"; |
| 2231 | else |
| 2232 | dir = "inactive"; |
| 2233 | |
| 2234 | |
| 2235 | len = pj_ansi_snprintf(buf, end-p, |
Benny Prijono | 5186eae | 2007-12-03 14:38:25 +0000 | [diff] [blame] | 2236 | "%s #%d %.*s @%dKHz, %s, peer=%s", |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2237 | indent, i, |
Benny Prijono | 172cd73 | 2006-06-14 20:22:31 +0000 | [diff] [blame] | 2238 | (int)info.stream_info[i].fmt.encoding_name.slen, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2239 | info.stream_info[i].fmt.encoding_name.ptr, |
| 2240 | info.stream_info[i].fmt.clock_rate / 1000, |
| 2241 | dir, |
Benny Prijono | 5186eae | 2007-12-03 14:38:25 +0000 | [diff] [blame] | 2242 | rem_addr); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2243 | if (len < 1 || len > end-p) { |
| 2244 | *p = '\0'; |
| 2245 | return; |
| 2246 | } |
| 2247 | |
| 2248 | p += len; |
| 2249 | *p++ = '\n'; |
| 2250 | *p = '\0'; |
| 2251 | |
| 2252 | if (stat.rx.update_cnt == 0) |
| 2253 | strcpy(last_update, "never"); |
| 2254 | else { |
| 2255 | pj_gettimeofday(&now); |
| 2256 | PJ_TIME_VAL_SUB(now, stat.rx.update); |
| 2257 | sprintf(last_update, "%02ldh:%02ldm:%02ld.%03lds ago", |
| 2258 | now.sec / 3600, |
| 2259 | (now.sec % 3600) / 60, |
| 2260 | now.sec % 60, |
| 2261 | now.msec); |
| 2262 | } |
| 2263 | |
Benny Prijono | 80019eb | 2006-08-07 13:22:23 +0000 | [diff] [blame] | 2264 | pj_gettimeofday(&media_duration); |
| 2265 | PJ_TIME_VAL_SUB(media_duration, stat.start); |
| 2266 | if (PJ_TIME_VAL_MSEC(media_duration) == 0) |
| 2267 | media_duration.msec = 1; |
| 2268 | |
Benny Prijono | 1402a4a | 2008-01-08 23:41:22 +0000 | [diff] [blame] | 2269 | /* protect against division by zero */ |
| 2270 | if (stat.rx.pkt == 0) |
| 2271 | stat.rx.pkt = 1; |
| 2272 | if (stat.tx.pkt == 0) |
| 2273 | stat.tx.pkt = 1; |
| 2274 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2275 | len = pj_ansi_snprintf(p, end-p, |
| 2276 | "%s RX pt=%d, stat last update: %s\n" |
Benny Prijono | 427af7d | 2007-05-11 10:36:40 +0000 | [diff] [blame] | 2277 | "%s total %spkt %sB (%sB +IP hdr) @avg=%sbps/%sbps\n" |
Benny Prijono | 27c256a | 2008-09-08 21:31:36 +0000 | [diff] [blame] | 2278 | "%s pkt loss=%d (%3.1f%%), discrd=%d (%3.1f%%), dup=%d (%2.1f%%), reord=%d (%3.1f%%)\n" |
Nanang Izzuddin | 2d4ee7d | 2008-05-17 14:54:18 +0000 | [diff] [blame] | 2279 | "%s (msec) min avg max last dev\n" |
| 2280 | "%s loss period: %7.3f %7.3f %7.3f %7.3f %7.3f\n" |
Nanang Izzuddin | 78bec1a | 2010-07-15 14:45:47 +0000 | [diff] [blame] | 2281 | "%s jitter : %7.3f %7.3f %7.3f %7.3f %7.3f" |
| 2282 | #if defined(PJMEDIA_RTCP_STAT_HAS_RAW_JITTER) && PJMEDIA_RTCP_STAT_HAS_RAW_JITTER!=0 |
| 2283 | "\n" |
| 2284 | "%s raw jitter : %7.3f %7.3f %7.3f %7.3f %7.3f" |
| 2285 | #endif |
| 2286 | #if defined(PJMEDIA_RTCP_STAT_HAS_IPDV) && PJMEDIA_RTCP_STAT_HAS_IPDV!=0 |
| 2287 | "\n" |
| 2288 | "%s IPDV : %7.3f %7.3f %7.3f %7.3f %7.3f" |
| 2289 | #endif |
| 2290 | "%s", |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2291 | indent, info.stream_info[i].fmt.pt, |
| 2292 | last_update, |
| 2293 | indent, |
| 2294 | good_number(packets, stat.rx.pkt), |
| 2295 | good_number(bytes, stat.rx.bytes), |
Benny Prijono | 427af7d | 2007-05-11 10:36:40 +0000 | [diff] [blame] | 2296 | good_number(ipbytes, stat.rx.bytes + stat.rx.pkt * 40), |
Benny Prijono | e723b92 | 2008-01-09 22:11:24 +0000 | [diff] [blame] | 2297 | good_number(avg_bps, (pj_int32_t)((pj_int64_t)stat.rx.bytes * 8 * 1000 / PJ_TIME_VAL_MSEC(media_duration))), |
| 2298 | good_number(avg_ipbps, (pj_int32_t)(((pj_int64_t)stat.rx.bytes + stat.rx.pkt * 40) * 8 * 1000 / PJ_TIME_VAL_MSEC(media_duration))), |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2299 | indent, |
| 2300 | stat.rx.loss, |
Benny Prijono | f901651 | 2006-06-29 09:41:34 +0000 | [diff] [blame] | 2301 | stat.rx.loss * 100.0 / (stat.rx.pkt + stat.rx.loss), |
Benny Prijono | 27c256a | 2008-09-08 21:31:36 +0000 | [diff] [blame] | 2302 | stat.rx.discard, |
| 2303 | stat.rx.discard * 100.0 / (stat.rx.pkt + stat.rx.loss), |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2304 | stat.rx.dup, |
Benny Prijono | b12106f | 2006-06-29 14:45:17 +0000 | [diff] [blame] | 2305 | stat.rx.dup * 100.0 / (stat.rx.pkt + stat.rx.loss), |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2306 | stat.rx.reorder, |
Benny Prijono | b12106f | 2006-06-29 14:45:17 +0000 | [diff] [blame] | 2307 | stat.rx.reorder * 100.0 / (stat.rx.pkt + stat.rx.loss), |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2308 | indent, indent, |
| 2309 | stat.rx.loss_period.min / 1000.0, |
Nanang Izzuddin | 2d4ee7d | 2008-05-17 14:54:18 +0000 | [diff] [blame] | 2310 | stat.rx.loss_period.mean / 1000.0, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2311 | stat.rx.loss_period.max / 1000.0, |
| 2312 | stat.rx.loss_period.last / 1000.0, |
Nanang Izzuddin | 2d4ee7d | 2008-05-17 14:54:18 +0000 | [diff] [blame] | 2313 | pj_math_stat_get_stddev(&stat.rx.loss_period) / 1000.0, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2314 | indent, |
| 2315 | stat.rx.jitter.min / 1000.0, |
Nanang Izzuddin | 2d4ee7d | 2008-05-17 14:54:18 +0000 | [diff] [blame] | 2316 | stat.rx.jitter.mean / 1000.0, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2317 | stat.rx.jitter.max / 1000.0, |
| 2318 | stat.rx.jitter.last / 1000.0, |
Nanang Izzuddin | 2d4ee7d | 2008-05-17 14:54:18 +0000 | [diff] [blame] | 2319 | pj_math_stat_get_stddev(&stat.rx.jitter) / 1000.0, |
Nanang Izzuddin | 78bec1a | 2010-07-15 14:45:47 +0000 | [diff] [blame] | 2320 | #if defined(PJMEDIA_RTCP_STAT_HAS_RAW_JITTER) && PJMEDIA_RTCP_STAT_HAS_RAW_JITTER!=0 |
| 2321 | indent, |
| 2322 | stat.rx_raw_jitter.min / 1000.0, |
| 2323 | stat.rx_raw_jitter.mean / 1000.0, |
| 2324 | stat.rx_raw_jitter.max / 1000.0, |
| 2325 | stat.rx_raw_jitter.last / 1000.0, |
| 2326 | pj_math_stat_get_stddev(&stat.rx_raw_jitter) / 1000.0, |
| 2327 | #endif |
| 2328 | #if defined(PJMEDIA_RTCP_STAT_HAS_IPDV) && PJMEDIA_RTCP_STAT_HAS_IPDV!=0 |
| 2329 | indent, |
| 2330 | stat.rx_ipdv.min / 1000.0, |
| 2331 | stat.rx_ipdv.mean / 1000.0, |
| 2332 | stat.rx_ipdv.max / 1000.0, |
| 2333 | stat.rx_ipdv.last / 1000.0, |
| 2334 | pj_math_stat_get_stddev(&stat.rx_ipdv) / 1000.0, |
| 2335 | #endif |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2336 | "" |
| 2337 | ); |
| 2338 | |
| 2339 | if (len < 1 || len > end-p) { |
| 2340 | *p = '\0'; |
| 2341 | return; |
| 2342 | } |
| 2343 | |
| 2344 | p += len; |
| 2345 | *p++ = '\n'; |
| 2346 | *p = '\0'; |
| 2347 | |
| 2348 | if (stat.tx.update_cnt == 0) |
| 2349 | strcpy(last_update, "never"); |
| 2350 | else { |
| 2351 | pj_gettimeofday(&now); |
| 2352 | PJ_TIME_VAL_SUB(now, stat.tx.update); |
| 2353 | sprintf(last_update, "%02ldh:%02ldm:%02ld.%03lds ago", |
| 2354 | now.sec / 3600, |
| 2355 | (now.sec % 3600) / 60, |
| 2356 | now.sec % 60, |
| 2357 | now.msec); |
| 2358 | } |
| 2359 | |
| 2360 | len = pj_ansi_snprintf(p, end-p, |
| 2361 | "%s TX pt=%d, ptime=%dms, stat last update: %s\n" |
Benny Prijono | 427af7d | 2007-05-11 10:36:40 +0000 | [diff] [blame] | 2362 | "%s total %spkt %sB (%sB +IP hdr) @avg %sbps/%sbps\n" |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2363 | "%s pkt loss=%d (%3.1f%%), dup=%d (%3.1f%%), reorder=%d (%3.1f%%)\n" |
Nanang Izzuddin | 2d4ee7d | 2008-05-17 14:54:18 +0000 | [diff] [blame] | 2364 | "%s (msec) min avg max last dev \n" |
| 2365 | "%s loss period: %7.3f %7.3f %7.3f %7.3f %7.3f\n" |
| 2366 | "%s jitter : %7.3f %7.3f %7.3f %7.3f %7.3f%s", |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2367 | indent, |
| 2368 | info.stream_info[i].tx_pt, |
| 2369 | info.stream_info[i].param->info.frm_ptime * |
| 2370 | info.stream_info[i].param->setting.frm_per_pkt, |
| 2371 | last_update, |
| 2372 | |
| 2373 | indent, |
| 2374 | good_number(packets, stat.tx.pkt), |
| 2375 | good_number(bytes, stat.tx.bytes), |
Benny Prijono | 427af7d | 2007-05-11 10:36:40 +0000 | [diff] [blame] | 2376 | good_number(ipbytes, stat.tx.bytes + stat.tx.pkt * 40), |
Benny Prijono | e723b92 | 2008-01-09 22:11:24 +0000 | [diff] [blame] | 2377 | good_number(avg_bps, (pj_int32_t)((pj_int64_t)stat.tx.bytes * 8 * 1000 / PJ_TIME_VAL_MSEC(media_duration))), |
| 2378 | good_number(avg_ipbps, (pj_int32_t)(((pj_int64_t)stat.tx.bytes + stat.tx.pkt * 40) * 8 * 1000 / PJ_TIME_VAL_MSEC(media_duration))), |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2379 | |
| 2380 | indent, |
| 2381 | stat.tx.loss, |
Benny Prijono | f901651 | 2006-06-29 09:41:34 +0000 | [diff] [blame] | 2382 | stat.tx.loss * 100.0 / (stat.tx.pkt + stat.tx.loss), |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2383 | stat.tx.dup, |
Benny Prijono | b12106f | 2006-06-29 14:45:17 +0000 | [diff] [blame] | 2384 | stat.tx.dup * 100.0 / (stat.tx.pkt + stat.tx.loss), |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2385 | stat.tx.reorder, |
Benny Prijono | b12106f | 2006-06-29 14:45:17 +0000 | [diff] [blame] | 2386 | stat.tx.reorder * 100.0 / (stat.tx.pkt + stat.tx.loss), |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2387 | |
| 2388 | indent, indent, |
| 2389 | stat.tx.loss_period.min / 1000.0, |
Nanang Izzuddin | 2d4ee7d | 2008-05-17 14:54:18 +0000 | [diff] [blame] | 2390 | stat.tx.loss_period.mean / 1000.0, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2391 | stat.tx.loss_period.max / 1000.0, |
| 2392 | stat.tx.loss_period.last / 1000.0, |
Nanang Izzuddin | 2d4ee7d | 2008-05-17 14:54:18 +0000 | [diff] [blame] | 2393 | pj_math_stat_get_stddev(&stat.tx.loss_period) / 1000.0, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2394 | indent, |
| 2395 | stat.tx.jitter.min / 1000.0, |
Nanang Izzuddin | 2d4ee7d | 2008-05-17 14:54:18 +0000 | [diff] [blame] | 2396 | stat.tx.jitter.mean / 1000.0, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2397 | stat.tx.jitter.max / 1000.0, |
| 2398 | stat.tx.jitter.last / 1000.0, |
Nanang Izzuddin | 2d4ee7d | 2008-05-17 14:54:18 +0000 | [diff] [blame] | 2399 | pj_math_stat_get_stddev(&stat.tx.jitter) / 1000.0, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2400 | "" |
| 2401 | ); |
| 2402 | |
| 2403 | if (len < 1 || len > end-p) { |
| 2404 | *p = '\0'; |
| 2405 | return; |
| 2406 | } |
| 2407 | |
| 2408 | p += len; |
| 2409 | *p++ = '\n'; |
| 2410 | *p = '\0'; |
| 2411 | |
| 2412 | len = pj_ansi_snprintf(p, end-p, |
Nanang Izzuddin | 2d4ee7d | 2008-05-17 14:54:18 +0000 | [diff] [blame] | 2413 | "%s RTT msec : %7.3f %7.3f %7.3f %7.3f %7.3f", |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2414 | indent, |
| 2415 | stat.rtt.min / 1000.0, |
Nanang Izzuddin | 2d4ee7d | 2008-05-17 14:54:18 +0000 | [diff] [blame] | 2416 | stat.rtt.mean / 1000.0, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2417 | stat.rtt.max / 1000.0, |
Nanang Izzuddin | 2d4ee7d | 2008-05-17 14:54:18 +0000 | [diff] [blame] | 2418 | stat.rtt.last / 1000.0, |
| 2419 | pj_math_stat_get_stddev(&stat.rtt) / 1000.0 |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2420 | ); |
| 2421 | if (len < 1 || len > end-p) { |
| 2422 | *p = '\0'; |
| 2423 | return; |
| 2424 | } |
| 2425 | |
| 2426 | p += len; |
| 2427 | *p++ = '\n'; |
| 2428 | *p = '\0'; |
Nanang Izzuddin | 660eee8 | 2008-07-17 14:54:03 +0000 | [diff] [blame] | 2429 | |
| 2430 | #if defined(PJMEDIA_HAS_RTCP_XR) && (PJMEDIA_HAS_RTCP_XR != 0) |
| 2431 | # define SAMPLES_TO_USEC(usec, samples, clock_rate) \ |
| 2432 | do { \ |
| 2433 | if (samples <= 4294) \ |
| 2434 | usec = samples * 1000000 / clock_rate; \ |
| 2435 | else { \ |
| 2436 | usec = samples * 1000 / clock_rate; \ |
| 2437 | usec *= 1000; \ |
| 2438 | } \ |
| 2439 | } while(0) |
| 2440 | |
| 2441 | # define PRINT_VOIP_MTC_VAL(s, v) \ |
| 2442 | if (v == 127) \ |
| 2443 | sprintf(s, "(na)"); \ |
| 2444 | else \ |
| 2445 | sprintf(s, "%d", v) |
| 2446 | |
| 2447 | # define VALIDATE_PRINT_BUF() \ |
| 2448 | if (len < 1 || len > end-p) { *p = '\0'; return; } \ |
| 2449 | p += len; *p++ = '\n'; *p = '\0' |
| 2450 | |
| 2451 | |
| 2452 | do { |
| 2453 | char loss[16], dup[16]; |
| 2454 | char jitter[80]; |
| 2455 | char toh[80]; |
| 2456 | char plc[16], jba[16], jbr[16]; |
| 2457 | char signal_lvl[16], noise_lvl[16], rerl[16]; |
| 2458 | char r_factor[16], ext_r_factor[16], mos_lq[16], mos_cq[16]; |
| 2459 | pjmedia_rtcp_xr_stat xr_stat; |
| 2460 | unsigned clock_rate; |
| 2461 | |
| 2462 | if (pjmedia_session_get_stream_stat_xr(session, i, &xr_stat) != |
| 2463 | PJ_SUCCESS) |
| 2464 | { |
| 2465 | break; |
| 2466 | } |
| 2467 | |
| 2468 | clock_rate = info.stream_info[i].fmt.clock_rate; |
| 2469 | |
| 2470 | len = pj_ansi_snprintf(p, end-p, "\n%s Extended reports:", indent); |
| 2471 | VALIDATE_PRINT_BUF(); |
| 2472 | |
| 2473 | /* Statistics Summary */ |
| 2474 | len = pj_ansi_snprintf(p, end-p, "%s Statistics Summary", indent); |
| 2475 | VALIDATE_PRINT_BUF(); |
| 2476 | |
| 2477 | if (xr_stat.rx.stat_sum.l) |
| 2478 | sprintf(loss, "%d", xr_stat.rx.stat_sum.lost); |
| 2479 | else |
| 2480 | sprintf(loss, "(na)"); |
| 2481 | |
| 2482 | if (xr_stat.rx.stat_sum.d) |
| 2483 | sprintf(dup, "%d", xr_stat.rx.stat_sum.dup); |
| 2484 | else |
| 2485 | sprintf(dup, "(na)"); |
| 2486 | |
| 2487 | if (xr_stat.rx.stat_sum.j) { |
| 2488 | unsigned jmin, jmax, jmean, jdev; |
| 2489 | |
| 2490 | SAMPLES_TO_USEC(jmin, xr_stat.rx.stat_sum.jitter.min, |
| 2491 | clock_rate); |
| 2492 | SAMPLES_TO_USEC(jmax, xr_stat.rx.stat_sum.jitter.max, |
| 2493 | clock_rate); |
| 2494 | SAMPLES_TO_USEC(jmean, xr_stat.rx.stat_sum.jitter.mean, |
| 2495 | clock_rate); |
| 2496 | SAMPLES_TO_USEC(jdev, |
| 2497 | pj_math_stat_get_stddev(&xr_stat.rx.stat_sum.jitter), |
| 2498 | clock_rate); |
| 2499 | sprintf(jitter, "%7.3f %7.3f %7.3f %7.3f", |
| 2500 | jmin/1000.0, jmean/1000.0, jmax/1000.0, jdev/1000.0); |
| 2501 | } else |
| 2502 | sprintf(jitter, "(report not available)"); |
| 2503 | |
| 2504 | if (xr_stat.rx.stat_sum.t) { |
| 2505 | sprintf(toh, "%11d %11d %11d %11d", |
| 2506 | xr_stat.rx.stat_sum.toh.min, |
| 2507 | xr_stat.rx.stat_sum.toh.mean, |
| 2508 | xr_stat.rx.stat_sum.toh.max, |
| 2509 | pj_math_stat_get_stddev(&xr_stat.rx.stat_sum.toh)); |
| 2510 | } else |
| 2511 | sprintf(toh, "(report not available)"); |
| 2512 | |
| 2513 | if (xr_stat.rx.stat_sum.update.sec == 0) |
| 2514 | strcpy(last_update, "never"); |
| 2515 | else { |
| 2516 | pj_gettimeofday(&now); |
| 2517 | PJ_TIME_VAL_SUB(now, xr_stat.rx.stat_sum.update); |
| 2518 | sprintf(last_update, "%02ldh:%02ldm:%02ld.%03lds ago", |
| 2519 | now.sec / 3600, |
| 2520 | (now.sec % 3600) / 60, |
| 2521 | now.sec % 60, |
| 2522 | now.msec); |
| 2523 | } |
| 2524 | |
| 2525 | len = pj_ansi_snprintf(p, end-p, |
| 2526 | "%s RX last update: %s\n" |
| 2527 | "%s begin seq=%d, end seq=%d\n" |
| 2528 | "%s pkt loss=%s, dup=%s\n" |
| 2529 | "%s (msec) min avg max dev\n" |
| 2530 | "%s jitter : %s\n" |
| 2531 | "%s toh : %s", |
| 2532 | indent, last_update, |
| 2533 | indent, |
| 2534 | xr_stat.rx.stat_sum.begin_seq, xr_stat.rx.stat_sum.end_seq, |
| 2535 | indent, loss, dup, |
| 2536 | indent, |
| 2537 | indent, jitter, |
| 2538 | indent, toh |
| 2539 | ); |
| 2540 | VALIDATE_PRINT_BUF(); |
| 2541 | |
| 2542 | if (xr_stat.tx.stat_sum.l) |
| 2543 | sprintf(loss, "%d", xr_stat.tx.stat_sum.lost); |
| 2544 | else |
| 2545 | sprintf(loss, "(na)"); |
| 2546 | |
| 2547 | if (xr_stat.tx.stat_sum.d) |
| 2548 | sprintf(dup, "%d", xr_stat.tx.stat_sum.dup); |
| 2549 | else |
| 2550 | sprintf(dup, "(na)"); |
| 2551 | |
| 2552 | if (xr_stat.tx.stat_sum.j) { |
| 2553 | unsigned jmin, jmax, jmean, jdev; |
| 2554 | |
| 2555 | SAMPLES_TO_USEC(jmin, xr_stat.tx.stat_sum.jitter.min, |
| 2556 | clock_rate); |
| 2557 | SAMPLES_TO_USEC(jmax, xr_stat.tx.stat_sum.jitter.max, |
| 2558 | clock_rate); |
| 2559 | SAMPLES_TO_USEC(jmean, xr_stat.tx.stat_sum.jitter.mean, |
| 2560 | clock_rate); |
| 2561 | SAMPLES_TO_USEC(jdev, |
| 2562 | pj_math_stat_get_stddev(&xr_stat.tx.stat_sum.jitter), |
| 2563 | clock_rate); |
| 2564 | sprintf(jitter, "%7.3f %7.3f %7.3f %7.3f", |
| 2565 | jmin/1000.0, jmean/1000.0, jmax/1000.0, jdev/1000.0); |
| 2566 | } else |
| 2567 | sprintf(jitter, "(report not available)"); |
| 2568 | |
| 2569 | if (xr_stat.tx.stat_sum.t) { |
| 2570 | sprintf(toh, "%11d %11d %11d %11d", |
| 2571 | xr_stat.tx.stat_sum.toh.min, |
| 2572 | xr_stat.tx.stat_sum.toh.mean, |
| 2573 | xr_stat.tx.stat_sum.toh.max, |
| 2574 | pj_math_stat_get_stddev(&xr_stat.rx.stat_sum.toh)); |
| 2575 | } else |
| 2576 | sprintf(toh, "(report not available)"); |
| 2577 | |
| 2578 | if (xr_stat.tx.stat_sum.update.sec == 0) |
| 2579 | strcpy(last_update, "never"); |
| 2580 | else { |
| 2581 | pj_gettimeofday(&now); |
| 2582 | PJ_TIME_VAL_SUB(now, xr_stat.tx.stat_sum.update); |
| 2583 | sprintf(last_update, "%02ldh:%02ldm:%02ld.%03lds ago", |
| 2584 | now.sec / 3600, |
| 2585 | (now.sec % 3600) / 60, |
| 2586 | now.sec % 60, |
| 2587 | now.msec); |
| 2588 | } |
| 2589 | |
| 2590 | len = pj_ansi_snprintf(p, end-p, |
| 2591 | "%s TX last update: %s\n" |
| 2592 | "%s begin seq=%d, end seq=%d\n" |
| 2593 | "%s pkt loss=%s, dup=%s\n" |
| 2594 | "%s (msec) min avg max dev\n" |
| 2595 | "%s jitter : %s\n" |
| 2596 | "%s toh : %s", |
| 2597 | indent, last_update, |
| 2598 | indent, |
| 2599 | xr_stat.tx.stat_sum.begin_seq, xr_stat.tx.stat_sum.end_seq, |
| 2600 | indent, loss, dup, |
| 2601 | indent, |
| 2602 | indent, jitter, |
| 2603 | indent, toh |
| 2604 | ); |
| 2605 | VALIDATE_PRINT_BUF(); |
| 2606 | |
| 2607 | |
| 2608 | /* VoIP Metrics */ |
| 2609 | len = pj_ansi_snprintf(p, end-p, "%s VoIP Metrics", indent); |
| 2610 | VALIDATE_PRINT_BUF(); |
| 2611 | |
| 2612 | PRINT_VOIP_MTC_VAL(signal_lvl, xr_stat.rx.voip_mtc.signal_lvl); |
| 2613 | PRINT_VOIP_MTC_VAL(noise_lvl, xr_stat.rx.voip_mtc.noise_lvl); |
| 2614 | PRINT_VOIP_MTC_VAL(rerl, xr_stat.rx.voip_mtc.rerl); |
| 2615 | PRINT_VOIP_MTC_VAL(r_factor, xr_stat.rx.voip_mtc.r_factor); |
| 2616 | PRINT_VOIP_MTC_VAL(ext_r_factor, xr_stat.rx.voip_mtc.ext_r_factor); |
| 2617 | PRINT_VOIP_MTC_VAL(mos_lq, xr_stat.rx.voip_mtc.mos_lq); |
| 2618 | PRINT_VOIP_MTC_VAL(mos_cq, xr_stat.rx.voip_mtc.mos_cq); |
| 2619 | |
| 2620 | switch ((xr_stat.rx.voip_mtc.rx_config>>6) & 3) { |
| 2621 | case PJMEDIA_RTCP_XR_PLC_DIS: |
| 2622 | sprintf(plc, "DISABLED"); |
| 2623 | break; |
| 2624 | case PJMEDIA_RTCP_XR_PLC_ENH: |
| 2625 | sprintf(plc, "ENHANCED"); |
| 2626 | break; |
| 2627 | case PJMEDIA_RTCP_XR_PLC_STD: |
| 2628 | sprintf(plc, "STANDARD"); |
| 2629 | break; |
| 2630 | case PJMEDIA_RTCP_XR_PLC_UNK: |
| 2631 | default: |
| 2632 | sprintf(plc, "UNKNOWN"); |
| 2633 | break; |
| 2634 | } |
| 2635 | |
| 2636 | switch ((xr_stat.rx.voip_mtc.rx_config>>4) & 3) { |
| 2637 | case PJMEDIA_RTCP_XR_JB_FIXED: |
| 2638 | sprintf(jba, "FIXED"); |
| 2639 | break; |
| 2640 | case PJMEDIA_RTCP_XR_JB_ADAPTIVE: |
| 2641 | sprintf(jba, "ADAPTIVE"); |
| 2642 | break; |
| 2643 | default: |
| 2644 | sprintf(jba, "UNKNOWN"); |
| 2645 | break; |
| 2646 | } |
| 2647 | |
| 2648 | sprintf(jbr, "%d", xr_stat.rx.voip_mtc.rx_config & 0x0F); |
| 2649 | |
| 2650 | if (xr_stat.rx.voip_mtc.update.sec == 0) |
| 2651 | strcpy(last_update, "never"); |
| 2652 | else { |
| 2653 | pj_gettimeofday(&now); |
| 2654 | PJ_TIME_VAL_SUB(now, xr_stat.rx.voip_mtc.update); |
| 2655 | sprintf(last_update, "%02ldh:%02ldm:%02ld.%03lds ago", |
| 2656 | now.sec / 3600, |
| 2657 | (now.sec % 3600) / 60, |
| 2658 | now.sec % 60, |
| 2659 | now.msec); |
| 2660 | } |
| 2661 | |
| 2662 | len = pj_ansi_snprintf(p, end-p, |
| 2663 | "%s RX last update: %s\n" |
| 2664 | "%s packets : loss rate=%d (%.2f%%), discard rate=%d (%.2f%%)\n" |
| 2665 | "%s burst : density=%d (%.2f%%), duration=%d%s\n" |
| 2666 | "%s gap : density=%d (%.2f%%), duration=%d%s\n" |
| 2667 | "%s delay : round trip=%d%s, end system=%d%s\n" |
| 2668 | "%s level : signal=%s%s, noise=%s%s, RERL=%s%s\n" |
| 2669 | "%s quality : R factor=%s, ext R factor=%s\n" |
| 2670 | "%s MOS LQ=%s, MOS CQ=%s\n" |
| 2671 | "%s config : PLC=%s, JB=%s, JB rate=%s, Gmin=%d\n" |
| 2672 | "%s JB delay : cur=%d%s, max=%d%s, abs max=%d%s", |
| 2673 | indent, |
| 2674 | last_update, |
| 2675 | /* packets */ |
| 2676 | indent, |
| 2677 | xr_stat.rx.voip_mtc.loss_rate, xr_stat.rx.voip_mtc.loss_rate*100.0/256, |
| 2678 | xr_stat.rx.voip_mtc.discard_rate, xr_stat.rx.voip_mtc.discard_rate*100.0/256, |
| 2679 | /* burst */ |
| 2680 | indent, |
| 2681 | xr_stat.rx.voip_mtc.burst_den, xr_stat.rx.voip_mtc.burst_den*100.0/256, |
| 2682 | xr_stat.rx.voip_mtc.burst_dur, "ms", |
| 2683 | /* gap */ |
| 2684 | indent, |
| 2685 | xr_stat.rx.voip_mtc.gap_den, xr_stat.rx.voip_mtc.gap_den*100.0/256, |
| 2686 | xr_stat.rx.voip_mtc.gap_dur, "ms", |
| 2687 | /* delay */ |
| 2688 | indent, |
| 2689 | xr_stat.rx.voip_mtc.rnd_trip_delay, "ms", |
| 2690 | xr_stat.rx.voip_mtc.end_sys_delay, "ms", |
| 2691 | /* level */ |
| 2692 | indent, |
| 2693 | signal_lvl, "dB", |
| 2694 | noise_lvl, "dB", |
| 2695 | rerl, "", |
| 2696 | /* quality */ |
| 2697 | indent, |
| 2698 | r_factor, ext_r_factor, |
| 2699 | indent, |
| 2700 | mos_lq, mos_cq, |
| 2701 | /* config */ |
| 2702 | indent, |
| 2703 | plc, jba, jbr, xr_stat.rx.voip_mtc.gmin, |
| 2704 | /* JB delay */ |
| 2705 | indent, |
| 2706 | xr_stat.rx.voip_mtc.jb_nom, "ms", |
| 2707 | xr_stat.rx.voip_mtc.jb_max, "ms", |
| 2708 | xr_stat.rx.voip_mtc.jb_abs_max, "ms" |
| 2709 | ); |
| 2710 | VALIDATE_PRINT_BUF(); |
| 2711 | |
| 2712 | PRINT_VOIP_MTC_VAL(signal_lvl, xr_stat.tx.voip_mtc.signal_lvl); |
| 2713 | PRINT_VOIP_MTC_VAL(noise_lvl, xr_stat.tx.voip_mtc.noise_lvl); |
| 2714 | PRINT_VOIP_MTC_VAL(rerl, xr_stat.tx.voip_mtc.rerl); |
| 2715 | PRINT_VOIP_MTC_VAL(r_factor, xr_stat.tx.voip_mtc.r_factor); |
| 2716 | PRINT_VOIP_MTC_VAL(ext_r_factor, xr_stat.tx.voip_mtc.ext_r_factor); |
| 2717 | PRINT_VOIP_MTC_VAL(mos_lq, xr_stat.tx.voip_mtc.mos_lq); |
| 2718 | PRINT_VOIP_MTC_VAL(mos_cq, xr_stat.tx.voip_mtc.mos_cq); |
| 2719 | |
| 2720 | switch ((xr_stat.tx.voip_mtc.rx_config>>6) & 3) { |
| 2721 | case PJMEDIA_RTCP_XR_PLC_DIS: |
| 2722 | sprintf(plc, "DISABLED"); |
| 2723 | break; |
| 2724 | case PJMEDIA_RTCP_XR_PLC_ENH: |
| 2725 | sprintf(plc, "ENHANCED"); |
| 2726 | break; |
| 2727 | case PJMEDIA_RTCP_XR_PLC_STD: |
| 2728 | sprintf(plc, "STANDARD"); |
| 2729 | break; |
| 2730 | case PJMEDIA_RTCP_XR_PLC_UNK: |
| 2731 | default: |
| 2732 | sprintf(plc, "unknown"); |
| 2733 | break; |
| 2734 | } |
| 2735 | |
| 2736 | switch ((xr_stat.tx.voip_mtc.rx_config>>4) & 3) { |
| 2737 | case PJMEDIA_RTCP_XR_JB_FIXED: |
| 2738 | sprintf(jba, "FIXED"); |
| 2739 | break; |
| 2740 | case PJMEDIA_RTCP_XR_JB_ADAPTIVE: |
| 2741 | sprintf(jba, "ADAPTIVE"); |
| 2742 | break; |
| 2743 | default: |
| 2744 | sprintf(jba, "unknown"); |
| 2745 | break; |
| 2746 | } |
| 2747 | |
| 2748 | sprintf(jbr, "%d", xr_stat.tx.voip_mtc.rx_config & 0x0F); |
| 2749 | |
| 2750 | if (xr_stat.tx.voip_mtc.update.sec == 0) |
| 2751 | strcpy(last_update, "never"); |
| 2752 | else { |
| 2753 | pj_gettimeofday(&now); |
| 2754 | PJ_TIME_VAL_SUB(now, xr_stat.tx.voip_mtc.update); |
| 2755 | sprintf(last_update, "%02ldh:%02ldm:%02ld.%03lds ago", |
| 2756 | now.sec / 3600, |
| 2757 | (now.sec % 3600) / 60, |
| 2758 | now.sec % 60, |
| 2759 | now.msec); |
| 2760 | } |
| 2761 | |
| 2762 | len = pj_ansi_snprintf(p, end-p, |
| 2763 | "%s TX last update: %s\n" |
| 2764 | "%s packets : loss rate=%d (%.2f%%), discard rate=%d (%.2f%%)\n" |
| 2765 | "%s burst : density=%d (%.2f%%), duration=%d%s\n" |
| 2766 | "%s gap : density=%d (%.2f%%), duration=%d%s\n" |
| 2767 | "%s delay : round trip=%d%s, end system=%d%s\n" |
| 2768 | "%s level : signal=%s%s, noise=%s%s, RERL=%s%s\n" |
| 2769 | "%s quality : R factor=%s, ext R factor=%s\n" |
| 2770 | "%s MOS LQ=%s, MOS CQ=%s\n" |
| 2771 | "%s config : PLC=%s, JB=%s, JB rate=%s, Gmin=%d\n" |
| 2772 | "%s JB delay : cur=%d%s, max=%d%s, abs max=%d%s", |
| 2773 | indent, |
| 2774 | last_update, |
| 2775 | /* pakcets */ |
| 2776 | indent, |
| 2777 | xr_stat.tx.voip_mtc.loss_rate, xr_stat.tx.voip_mtc.loss_rate*100.0/256, |
| 2778 | xr_stat.tx.voip_mtc.discard_rate, xr_stat.tx.voip_mtc.discard_rate*100.0/256, |
| 2779 | /* burst */ |
| 2780 | indent, |
| 2781 | xr_stat.tx.voip_mtc.burst_den, xr_stat.tx.voip_mtc.burst_den*100.0/256, |
| 2782 | xr_stat.tx.voip_mtc.burst_dur, "ms", |
| 2783 | /* gap */ |
| 2784 | indent, |
| 2785 | xr_stat.tx.voip_mtc.gap_den, xr_stat.tx.voip_mtc.gap_den*100.0/256, |
| 2786 | xr_stat.tx.voip_mtc.gap_dur, "ms", |
| 2787 | /* delay */ |
| 2788 | indent, |
| 2789 | xr_stat.tx.voip_mtc.rnd_trip_delay, "ms", |
| 2790 | xr_stat.tx.voip_mtc.end_sys_delay, "ms", |
| 2791 | /* level */ |
| 2792 | indent, |
| 2793 | signal_lvl, "dB", |
| 2794 | noise_lvl, "dB", |
| 2795 | rerl, "", |
| 2796 | /* quality */ |
| 2797 | indent, |
| 2798 | r_factor, ext_r_factor, |
| 2799 | indent, |
| 2800 | mos_lq, mos_cq, |
| 2801 | /* config */ |
| 2802 | indent, |
| 2803 | plc, jba, jbr, xr_stat.tx.voip_mtc.gmin, |
| 2804 | /* JB delay */ |
| 2805 | indent, |
| 2806 | xr_stat.tx.voip_mtc.jb_nom, "ms", |
| 2807 | xr_stat.tx.voip_mtc.jb_max, "ms", |
| 2808 | xr_stat.tx.voip_mtc.jb_abs_max, "ms" |
| 2809 | ); |
| 2810 | VALIDATE_PRINT_BUF(); |
| 2811 | |
| 2812 | |
| 2813 | /* RTT delay (by receiver side) */ |
| 2814 | len = pj_ansi_snprintf(p, end-p, |
| 2815 | "%s RTT (from recv) min avg max last dev", |
| 2816 | indent); |
| 2817 | VALIDATE_PRINT_BUF(); |
| 2818 | len = pj_ansi_snprintf(p, end-p, |
| 2819 | "%s RTT msec : %7.3f %7.3f %7.3f %7.3f %7.3f", |
| 2820 | indent, |
| 2821 | xr_stat.rtt.min / 1000.0, |
| 2822 | xr_stat.rtt.mean / 1000.0, |
| 2823 | xr_stat.rtt.max / 1000.0, |
| 2824 | xr_stat.rtt.last / 1000.0, |
| 2825 | pj_math_stat_get_stddev(&xr_stat.rtt) / 1000.0 |
| 2826 | ); |
| 2827 | VALIDATE_PRINT_BUF(); |
| 2828 | } while(0); |
| 2829 | #endif |
| 2830 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2831 | } |
| 2832 | } |
| 2833 | |
| 2834 | |
| 2835 | /* Print call info */ |
Benny Prijono | 627cbb4 | 2007-09-25 20:48:49 +0000 | [diff] [blame] | 2836 | void print_call(const char *title, |
Nanang Izzuddin | ff2b110 | 2008-09-17 15:32:06 +0000 | [diff] [blame] | 2837 | int call_id, |
| 2838 | char *buf, pj_size_t size) |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2839 | { |
| 2840 | int len; |
| 2841 | pjsip_inv_session *inv = pjsua_var.calls[call_id].inv; |
| 2842 | pjsip_dialog *dlg = inv->dlg; |
| 2843 | char userinfo[128]; |
| 2844 | |
| 2845 | /* Dump invite sesion info. */ |
| 2846 | |
| 2847 | len = pjsip_hdr_print_on(dlg->remote.info, userinfo, sizeof(userinfo)); |
Benny Prijono | 329d638 | 2009-05-29 13:04:03 +0000 | [diff] [blame] | 2848 | if (len < 0) |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2849 | pj_ansi_strcpy(userinfo, "<--uri too long-->"); |
| 2850 | else |
| 2851 | userinfo[len] = '\0'; |
| 2852 | |
| 2853 | len = pj_ansi_snprintf(buf, size, "%s[%s] %s", |
| 2854 | title, |
| 2855 | pjsip_inv_state_name(inv->state), |
| 2856 | userinfo); |
| 2857 | if (len < 1 || len >= (int)size) { |
| 2858 | pj_ansi_strcpy(buf, "<--uri too long-->"); |
| 2859 | len = 18; |
| 2860 | } else |
| 2861 | buf[len] = '\0'; |
| 2862 | } |
| 2863 | |
| 2864 | |
| 2865 | /* |
| 2866 | * Dump call and media statistics to string. |
| 2867 | */ |
| 2868 | PJ_DEF(pj_status_t) pjsua_call_dump( pjsua_call_id call_id, |
| 2869 | pj_bool_t with_media, |
| 2870 | char *buffer, |
| 2871 | unsigned maxlen, |
| 2872 | const char *indent) |
| 2873 | { |
| 2874 | pjsua_call *call; |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 2875 | pjsip_dialog *dlg; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2876 | pj_time_val duration, res_delay, con_delay; |
| 2877 | char tmp[128]; |
| 2878 | char *p, *end; |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 2879 | pj_status_t status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2880 | int len; |
Benny Prijono | e1a5a85 | 2008-03-11 21:38:05 +0000 | [diff] [blame] | 2881 | pjmedia_transport_info tp_info; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2882 | |
| 2883 | PJ_ASSERT_RETURN(call_id>=0 && call_id<(int)pjsua_var.ua_cfg.max_calls, |
| 2884 | PJ_EINVAL); |
| 2885 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 2886 | status = acquire_call("pjsua_call_dump()", call_id, &call, &dlg); |
Benny Prijono | 148c9dd | 2006-09-19 13:37:53 +0000 | [diff] [blame] | 2887 | if (status != PJ_SUCCESS) |
| 2888 | return status; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2889 | |
| 2890 | *buffer = '\0'; |
| 2891 | p = buffer; |
| 2892 | end = buffer + maxlen; |
| 2893 | len = 0; |
| 2894 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2895 | print_call(indent, call_id, tmp, sizeof(tmp)); |
| 2896 | |
| 2897 | len = pj_ansi_strlen(tmp); |
| 2898 | pj_ansi_strcpy(buffer, tmp); |
| 2899 | |
| 2900 | p += len; |
| 2901 | *p++ = '\r'; |
| 2902 | *p++ = '\n'; |
| 2903 | |
| 2904 | /* Calculate call duration */ |
Benny Prijono | 4be63b5 | 2006-11-25 14:50:25 +0000 | [diff] [blame] | 2905 | if (call->conn_time.sec != 0) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2906 | pj_gettimeofday(&duration); |
| 2907 | PJ_TIME_VAL_SUB(duration, call->conn_time); |
| 2908 | con_delay = call->conn_time; |
| 2909 | PJ_TIME_VAL_SUB(con_delay, call->start_time); |
| 2910 | } else { |
| 2911 | duration.sec = duration.msec = 0; |
| 2912 | con_delay.sec = con_delay.msec = 0; |
| 2913 | } |
| 2914 | |
| 2915 | /* Calculate first response delay */ |
Benny Prijono | 4be63b5 | 2006-11-25 14:50:25 +0000 | [diff] [blame] | 2916 | if (call->res_time.sec != 0) { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2917 | res_delay = call->res_time; |
| 2918 | PJ_TIME_VAL_SUB(res_delay, call->start_time); |
| 2919 | } else { |
| 2920 | res_delay.sec = res_delay.msec = 0; |
| 2921 | } |
| 2922 | |
| 2923 | /* Print duration */ |
| 2924 | len = pj_ansi_snprintf(p, end-p, |
| 2925 | "%s Call time: %02dh:%02dm:%02ds, " |
| 2926 | "1st res in %d ms, conn in %dms", |
| 2927 | indent, |
Benny Prijono | 172cd73 | 2006-06-14 20:22:31 +0000 | [diff] [blame] | 2928 | (int)(duration.sec / 3600), |
| 2929 | (int)((duration.sec % 3600)/60), |
| 2930 | (int)(duration.sec % 60), |
| 2931 | (int)PJ_TIME_VAL_MSEC(res_delay), |
| 2932 | (int)PJ_TIME_VAL_MSEC(con_delay)); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2933 | |
| 2934 | if (len > 0 && len < end-p) { |
| 2935 | p += len; |
| 2936 | *p++ = '\n'; |
| 2937 | *p = '\0'; |
| 2938 | } |
| 2939 | |
Benny Prijono | f5d9f1f | 2009-10-14 13:13:18 +0000 | [diff] [blame] | 2940 | /* Get and ICE SRTP status */ |
Benny Prijono | 734fc2d | 2008-03-17 16:05:35 +0000 | [diff] [blame] | 2941 | pjmedia_transport_info_init(&tp_info); |
Benny Prijono | e1a5a85 | 2008-03-11 21:38:05 +0000 | [diff] [blame] | 2942 | pjmedia_transport_get_info(call->med_tp, &tp_info); |
| 2943 | if (tp_info.specific_info_cnt > 0) { |
Benny Prijono | f5d9f1f | 2009-10-14 13:13:18 +0000 | [diff] [blame] | 2944 | unsigned i; |
Benny Prijono | e1a5a85 | 2008-03-11 21:38:05 +0000 | [diff] [blame] | 2945 | for (i = 0; i < tp_info.specific_info_cnt; ++i) { |
| 2946 | if (tp_info.spc_info[i].type == PJMEDIA_TRANSPORT_TYPE_SRTP) |
| 2947 | { |
| 2948 | pjmedia_srtp_info *srtp_info = |
| 2949 | (pjmedia_srtp_info*) tp_info.spc_info[i].buffer; |
| 2950 | |
| 2951 | len = pj_ansi_snprintf(p, end-p, |
| 2952 | "%s SRTP status: %s Crypto-suite: %s", |
| 2953 | indent, |
| 2954 | (srtp_info->active?"Active":"Not active"), |
| 2955 | srtp_info->tx_policy.name.ptr); |
| 2956 | if (len > 0 && len < end-p) { |
| 2957 | p += len; |
| 2958 | *p++ = '\n'; |
| 2959 | *p = '\0'; |
| 2960 | } |
Benny Prijono | f5d9f1f | 2009-10-14 13:13:18 +0000 | [diff] [blame] | 2961 | } else if (tp_info.spc_info[i].type==PJMEDIA_TRANSPORT_TYPE_ICE) { |
| 2962 | const pjmedia_ice_transport_info *ii; |
| 2963 | |
| 2964 | ii = (const pjmedia_ice_transport_info*) |
| 2965 | tp_info.spc_info[i].buffer; |
| 2966 | |
| 2967 | len = pj_ansi_snprintf(p, end-p, |
| 2968 | "%s ICE role: %s, state: %s, comp_cnt: %u", |
| 2969 | indent, |
| 2970 | pj_ice_sess_role_name(ii->role), |
| 2971 | pj_ice_strans_state_name(ii->sess_state), |
| 2972 | ii->comp_cnt); |
| 2973 | if (len > 0 && len < end-p) { |
| 2974 | p += len; |
| 2975 | *p++ = '\n'; |
| 2976 | *p = '\0'; |
| 2977 | } |
Benny Prijono | e1a5a85 | 2008-03-11 21:38:05 +0000 | [diff] [blame] | 2978 | } |
| 2979 | } |
| 2980 | } |
| 2981 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2982 | /* Dump session statistics */ |
| 2983 | if (with_media && call->session) |
Nanang Izzuddin | ff2b110 | 2008-09-17 15:32:06 +0000 | [diff] [blame] | 2984 | dump_media_session(indent, p, end-p, call); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2985 | |
Benny Prijono | dc752ca | 2006-09-22 16:55:42 +0000 | [diff] [blame] | 2986 | pjsip_dlg_dec_lock(dlg); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 2987 | |
| 2988 | return PJ_SUCCESS; |
| 2989 | } |
| 2990 | |
| 2991 | |
Nanang Izzuddin | 93bacd0 | 2010-06-15 09:56:39 +0000 | [diff] [blame] | 2992 | /* Timer callback to close sound device */ |
| 2993 | static void reinv_timer_cb(pj_timer_heap_t *th, |
| 2994 | pj_timer_entry *entry) |
| 2995 | { |
| 2996 | pjsua_call_id call_id = (pjsua_call_id)(pj_size_t)entry->user_data; |
| 2997 | pjsip_dialog *dlg; |
| 2998 | pjsua_call *call; |
| 2999 | pjsip_tx_data *tdata; |
| 3000 | pj_status_t status; |
| 3001 | |
| 3002 | PJ_UNUSED_ARG(th); |
| 3003 | |
| 3004 | pjsua_var.calls[call_id].lock_codec.reinv_timer.id = PJ_FALSE; |
| 3005 | |
| 3006 | status = acquire_call("reinv_timer_cb()", call_id, &call, &dlg); |
| 3007 | if (status != PJ_SUCCESS) |
| 3008 | return; |
| 3009 | |
| 3010 | /* Verify if another SDP negotiation is in progress, e.g: session timer |
| 3011 | * or another re-INVITE. |
| 3012 | */ |
| 3013 | if (call->inv==NULL || call->inv->neg==NULL || |
| 3014 | pjmedia_sdp_neg_get_state(call->inv->neg)!=PJMEDIA_SDP_NEG_STATE_DONE) |
| 3015 | { |
| 3016 | goto on_return; |
| 3017 | } |
| 3018 | |
| 3019 | /* Verify if another SDP negotiation has been completed by comparing |
| 3020 | * the SDP version. |
| 3021 | */ |
| 3022 | { |
| 3023 | const pjmedia_sdp_session *sdp; |
| 3024 | |
| 3025 | status = pjmedia_sdp_neg_get_active_local(call->inv->neg, &sdp); |
| 3026 | if (status == PJ_SUCCESS && |
| 3027 | sdp->origin.version > call->lock_codec.new_sdp->origin.version) |
| 3028 | { |
| 3029 | goto on_return; |
| 3030 | } |
| 3031 | } |
| 3032 | |
| 3033 | /* Create re-INVITE with the new offer */ |
| 3034 | status = pjsip_inv_reinvite(call->inv, NULL, call->lock_codec.new_sdp, |
| 3035 | &tdata); |
| 3036 | if (status == PJ_EINVALIDOP) { |
| 3037 | /* Ups, let's reschedule again */ |
| 3038 | pj_time_val delay = {0, LOCK_CODEC_RETRY_INTERVAL}; |
| 3039 | call->lock_codec.reinv_timer.id = PJ_TRUE; |
| 3040 | pjsip_endpt_schedule_timer(pjsua_var.endpt, |
| 3041 | &call->lock_codec.reinv_timer, &delay); |
| 3042 | } else if (status != PJ_SUCCESS) { |
| 3043 | pjsua_perror(THIS_FILE, "Failed creating re-INVITE in lock codec", |
| 3044 | status); |
| 3045 | } |
| 3046 | |
| 3047 | /* Send the UPDATE/re-INVITE request */ |
| 3048 | status = pjsip_inv_send_msg(call->inv, tdata); |
| 3049 | if (status != PJ_SUCCESS) { |
| 3050 | pjsua_perror(THIS_FILE, "Failed sending re-INVITE in lock codec", |
| 3051 | status); |
| 3052 | } |
| 3053 | |
| 3054 | on_return: |
| 3055 | pjsip_dlg_dec_lock(dlg); |
| 3056 | } |
| 3057 | |
| 3058 | |
| 3059 | /* Check if the specified format can be skipped in counting codecs */ |
| 3060 | static pj_bool_t is_non_av_fmt(const pjmedia_sdp_media *m, |
| 3061 | const pj_str_t *fmt) |
| 3062 | { |
| 3063 | unsigned pt; |
| 3064 | |
| 3065 | pt = pj_strtoul(fmt); |
| 3066 | |
| 3067 | /* Check for comfort noise */ |
| 3068 | if (pt == PJMEDIA_RTP_PT_CN) |
| 3069 | return PJ_TRUE; |
| 3070 | |
| 3071 | /* Dynamic PT, check the format name */ |
| 3072 | if (pt >= 96) { |
| 3073 | pjmedia_sdp_attr *a; |
| 3074 | pjmedia_sdp_rtpmap rtpmap; |
| 3075 | |
| 3076 | /* Get the format name */ |
| 3077 | a = pjmedia_sdp_attr_find2(m->attr_count, m->attr, "rtpmap", fmt); |
| 3078 | if (a && pjmedia_sdp_attr_get_rtpmap(a, &rtpmap)==PJ_SUCCESS) { |
| 3079 | /* Check for telephone-event */ |
| 3080 | if (pj_stricmp2(&rtpmap.enc_name, "telephone-event")==0) |
| 3081 | return PJ_TRUE; |
| 3082 | } else { |
| 3083 | /* Invalid SDP, should not reach here */ |
| 3084 | pj_assert(!"SDP should have been validated!"); |
| 3085 | return PJ_TRUE; |
| 3086 | } |
| 3087 | } |
| 3088 | |
| 3089 | return PJ_FALSE; |
| 3090 | } |
| 3091 | |
| 3092 | |
| 3093 | /* Check if remote answerer has given us more than one codecs. If so, |
| 3094 | * create another offer with one codec only to lock down the codec. |
| 3095 | */ |
| 3096 | static pj_status_t lock_codec(pjsua_call *call) |
| 3097 | { |
| 3098 | const pj_str_t st_update = {"UPDATE", 6}; |
| 3099 | pjsip_inv_session *inv = call->inv; |
| 3100 | const pjmedia_sdp_session *local_sdp; |
| 3101 | const pjmedia_sdp_session *remote_sdp; |
| 3102 | const pjmedia_sdp_media *rem_m; |
| 3103 | pjmedia_sdp_session *new_sdp; |
| 3104 | pjmedia_sdp_media *m; |
| 3105 | pjsip_tx_data *tdata; |
| 3106 | unsigned i, codec_cnt = 0; |
| 3107 | pj_status_t status; |
| 3108 | |
| 3109 | if (!pjmedia_sdp_neg_was_answer_remote(inv->neg)) |
| 3110 | return PJ_SUCCESS; |
| 3111 | |
| 3112 | status = pjmedia_sdp_neg_get_active_local(call->inv->neg, &local_sdp); |
| 3113 | if (status != PJ_SUCCESS) |
| 3114 | return status; |
| 3115 | status = pjmedia_sdp_neg_get_active_remote(call->inv->neg, &remote_sdp); |
| 3116 | if (status != PJ_SUCCESS) |
| 3117 | return status; |
| 3118 | |
| 3119 | PJ_ASSERT_RETURN(call->audio_idx>=0 && |
| 3120 | call->audio_idx < (int)remote_sdp->media_count, |
| 3121 | PJ_EINVALIDOP); |
| 3122 | |
| 3123 | rem_m = remote_sdp->media[call->audio_idx]; |
| 3124 | |
| 3125 | /* Check if media is disabled or only one format in the answer. */ |
| 3126 | if (rem_m->desc.port==0 || rem_m->desc.fmt_count==1) |
| 3127 | return PJ_SUCCESS; |
| 3128 | |
| 3129 | /* Count the formats in the answer. */ |
| 3130 | for (i=0; i<rem_m->desc.fmt_count && codec_cnt <= 1; ++i) { |
| 3131 | if (!is_non_av_fmt(rem_m, &rem_m->desc.fmt[i])) |
| 3132 | ++codec_cnt; |
| 3133 | } |
| 3134 | |
| 3135 | if (codec_cnt <= 1) { |
| 3136 | /* Answer contains single codec. */ |
| 3137 | return PJ_SUCCESS; |
| 3138 | } |
| 3139 | |
| 3140 | PJ_LOG(3, (THIS_FILE, "Got answer with multiple codecs, start " |
| 3141 | "updating media session to use only one codec..")); |
| 3142 | |
| 3143 | /* Clone the offer */ |
| 3144 | new_sdp = pjmedia_sdp_session_clone(inv->pool_prov, local_sdp); |
| 3145 | /* Note that the usage of pool_prov above is risky when locking codec |
| 3146 | * delays the re-INVITE (using timer) and there are two SDP negotiations |
| 3147 | * done before the re-INVITE. |
| 3148 | */ |
| 3149 | |
| 3150 | /* Update the new offer so it contains only a codec. Note that formats |
| 3151 | * order in the offer should have been matched to the answer, so we can |
| 3152 | * just directly update the offer without looking-up the answer. |
| 3153 | */ |
| 3154 | m = new_sdp->media[call->audio_idx]; |
| 3155 | codec_cnt = 0; |
| 3156 | i = 0; |
| 3157 | while (i < m->desc.fmt_count) { |
| 3158 | pjmedia_sdp_attr *a; |
| 3159 | pj_str_t *fmt = &m->desc.fmt[i]; |
| 3160 | |
| 3161 | if (is_non_av_fmt(m, fmt) || (++codec_cnt == 1)) { |
| 3162 | ++i; |
| 3163 | continue; |
| 3164 | } |
| 3165 | |
| 3166 | /* Remove format */ |
| 3167 | a = pjmedia_sdp_attr_find2(m->attr_count, m->attr, "rtpmap", fmt); |
| 3168 | if (a) pjmedia_sdp_attr_remove(&m->attr_count, m->attr, a); |
| 3169 | a = pjmedia_sdp_attr_find2(m->attr_count, m->attr, "fmtp", fmt); |
| 3170 | if (a) pjmedia_sdp_attr_remove(&m->attr_count, m->attr, a); |
| 3171 | pj_array_erase(m->desc.fmt, sizeof(m->desc.fmt[0]), |
| 3172 | m->desc.fmt_count, i); |
| 3173 | --m->desc.fmt_count; |
| 3174 | } |
| 3175 | |
| 3176 | /* Send new SDP offer via UPDATE or re-INVITE */ |
| 3177 | if (pjsip_dlg_remote_has_cap(inv->dlg, PJSIP_H_ALLOW, NULL, &st_update)== |
| 3178 | PJSIP_DIALOG_CAP_SUPPORTED) |
| 3179 | { |
| 3180 | /* Create UPDATE with the new offer */ |
| 3181 | status = pjsip_inv_update(inv, NULL, new_sdp, &tdata); |
| 3182 | if (status != PJ_SUCCESS) |
| 3183 | return status; |
| 3184 | |
| 3185 | } else { |
| 3186 | /* Create re-INVITE with the new offer */ |
| 3187 | status = pjsip_inv_reinvite(inv, NULL, new_sdp, &tdata); |
| 3188 | if (status == PJ_EINVALIDOP) { |
| 3189 | /* Current INVITE transaction is pending, reschedule re-INVITE. */ |
| 3190 | pj_time_val delay = {0, LOCK_CODEC_RETRY_INTERVAL}; |
| 3191 | |
| 3192 | call->lock_codec.new_sdp = new_sdp; |
| 3193 | pj_timer_entry_init(&call->lock_codec.reinv_timer, PJ_TRUE, |
| 3194 | (void*)(pj_size_t)call->index, |
| 3195 | &reinv_timer_cb); |
| 3196 | pjsip_endpt_schedule_timer(pjsua_var.endpt, |
| 3197 | &call->lock_codec.reinv_timer, &delay); |
| 3198 | return PJ_SUCCESS; |
| 3199 | |
| 3200 | } else if (status != PJ_SUCCESS) |
| 3201 | return status; |
| 3202 | } |
| 3203 | |
| 3204 | /* Send the UPDATE/re-INVITE request */ |
| 3205 | status = pjsip_inv_send_msg(inv, tdata); |
| 3206 | if (status != PJ_SUCCESS) |
| 3207 | return status; |
| 3208 | |
| 3209 | return PJ_SUCCESS; |
| 3210 | } |
| 3211 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3212 | /* |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 3213 | * This callback receives notification from invite session when the |
| 3214 | * session state has changed. |
| 3215 | */ |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 3216 | static void pjsua_call_on_state_changed(pjsip_inv_session *inv, |
| 3217 | pjsip_event *e) |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 3218 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3219 | pjsua_call *call; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3220 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3221 | PJSUA_LOCK(); |
| 3222 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 3223 | call = (pjsua_call*) inv->dlg->mod_data[pjsua_var.mod.id]; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3224 | |
| 3225 | if (!call) { |
| 3226 | PJSUA_UNLOCK(); |
Benny Prijono | e21e784 | 2006-04-09 16:46:05 +0000 | [diff] [blame] | 3227 | return; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3228 | } |
| 3229 | |
Benny Prijono | e21e784 | 2006-04-09 16:46:05 +0000 | [diff] [blame] | 3230 | |
| 3231 | /* Get call times */ |
| 3232 | switch (inv->state) { |
| 3233 | case PJSIP_INV_STATE_EARLY: |
| 3234 | case PJSIP_INV_STATE_CONNECTING: |
| 3235 | if (call->res_time.sec == 0) |
| 3236 | pj_gettimeofday(&call->res_time); |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 3237 | call->last_code = (pjsip_status_code) |
| 3238 | e->body.tsx_state.tsx->status_code; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3239 | pj_strncpy(&call->last_text, |
| 3240 | &e->body.tsx_state.tsx->status_text, |
| 3241 | sizeof(call->last_text_buf_)); |
Benny Prijono | e21e784 | 2006-04-09 16:46:05 +0000 | [diff] [blame] | 3242 | break; |
| 3243 | case PJSIP_INV_STATE_CONFIRMED: |
| 3244 | pj_gettimeofday(&call->conn_time); |
Nanang Izzuddin | 93bacd0 | 2010-06-15 09:56:39 +0000 | [diff] [blame] | 3245 | |
| 3246 | /* Ticket #476, locking a codec in the media session. */ |
| 3247 | { |
| 3248 | pj_status_t status; |
| 3249 | status = lock_codec(call); |
| 3250 | if (status != PJ_SUCCESS) { |
| 3251 | pjsua_perror(THIS_FILE, "Unable to lock codec", status); |
| 3252 | } |
| 3253 | } |
| 3254 | |
Benny Prijono | e21e784 | 2006-04-09 16:46:05 +0000 | [diff] [blame] | 3255 | break; |
| 3256 | case PJSIP_INV_STATE_DISCONNECTED: |
| 3257 | pj_gettimeofday(&call->dis_time); |
Benny Prijono | 2e507c2 | 2006-06-23 15:04:11 +0000 | [diff] [blame] | 3258 | if (call->res_time.sec == 0) |
| 3259 | pj_gettimeofday(&call->res_time); |
Benny Prijono | 5e51a4e | 2008-11-27 00:06:46 +0000 | [diff] [blame] | 3260 | if (e->type == PJSIP_EVENT_TSX_STATE && |
| 3261 | e->body.tsx_state.tsx->status_code > call->last_code) |
| 3262 | { |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 3263 | call->last_code = (pjsip_status_code) |
| 3264 | e->body.tsx_state.tsx->status_code; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3265 | pj_strncpy(&call->last_text, |
| 3266 | &e->body.tsx_state.tsx->status_text, |
| 3267 | sizeof(call->last_text_buf_)); |
Benny Prijono | 5e51a4e | 2008-11-27 00:06:46 +0000 | [diff] [blame] | 3268 | } else { |
| 3269 | call->last_code = PJSIP_SC_REQUEST_TERMINATED; |
| 3270 | pj_strncpy(&call->last_text, |
| 3271 | pjsip_get_status_text(call->last_code), |
| 3272 | sizeof(call->last_text_buf_)); |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 3273 | } |
Nanang Izzuddin | 93bacd0 | 2010-06-15 09:56:39 +0000 | [diff] [blame] | 3274 | |
| 3275 | /* Stop lock codec timer, if it is active */ |
| 3276 | if (call->lock_codec.reinv_timer.id) { |
| 3277 | pjsip_endpt_cancel_timer(pjsua_var.endpt, |
| 3278 | &call->lock_codec.reinv_timer); |
| 3279 | call->lock_codec.reinv_timer.id = PJ_FALSE; |
| 3280 | } |
Benny Prijono | e21e784 | 2006-04-09 16:46:05 +0000 | [diff] [blame] | 3281 | break; |
Benny Prijono | 8befd9f | 2006-05-13 22:46:23 +0000 | [diff] [blame] | 3282 | default: |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 3283 | call->last_code = (pjsip_status_code) |
| 3284 | e->body.tsx_state.tsx->status_code; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3285 | pj_strncpy(&call->last_text, |
| 3286 | &e->body.tsx_state.tsx->status_text, |
| 3287 | sizeof(call->last_text_buf_)); |
Benny Prijono | 8befd9f | 2006-05-13 22:46:23 +0000 | [diff] [blame] | 3288 | break; |
Benny Prijono | e21e784 | 2006-04-09 16:46:05 +0000 | [diff] [blame] | 3289 | } |
| 3290 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3291 | /* If this is an outgoing INVITE that was created because of |
| 3292 | * REFER/transfer, send NOTIFY to transferer. |
| 3293 | */ |
Benny Prijono | e21e784 | 2006-04-09 16:46:05 +0000 | [diff] [blame] | 3294 | if (call->xfer_sub && e->type==PJSIP_EVENT_TSX_STATE) { |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3295 | int st_code = -1; |
| 3296 | pjsip_evsub_state ev_state = PJSIP_EVSUB_STATE_ACTIVE; |
| 3297 | |
| 3298 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 3299 | switch (call->inv->state) { |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3300 | case PJSIP_INV_STATE_NULL: |
| 3301 | case PJSIP_INV_STATE_CALLING: |
| 3302 | /* Do nothing */ |
| 3303 | break; |
| 3304 | |
| 3305 | case PJSIP_INV_STATE_EARLY: |
| 3306 | case PJSIP_INV_STATE_CONNECTING: |
| 3307 | st_code = e->body.tsx_state.tsx->status_code; |
Benny Prijono | 19eeb6e | 2009-06-04 22:16:47 +0000 | [diff] [blame] | 3308 | if (call->inv->state == PJSIP_INV_STATE_CONNECTING) |
| 3309 | ev_state = PJSIP_EVSUB_STATE_TERMINATED; |
| 3310 | else |
| 3311 | ev_state = PJSIP_EVSUB_STATE_ACTIVE; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3312 | break; |
| 3313 | |
Benny Prijono | 140beae | 2009-10-11 05:06:43 +0000 | [diff] [blame] | 3314 | case PJSIP_INV_STATE_CONFIRMED: |
Benny Prijono | 19eeb6e | 2009-06-04 22:16:47 +0000 | [diff] [blame] | 3315 | #if 0 |
| 3316 | /* We don't need this, as we've terminated the subscription in |
| 3317 | * CONNECTING state. |
| 3318 | */ |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3319 | /* When state is confirmed, send the final 200/OK and terminate |
| 3320 | * subscription. |
| 3321 | */ |
| 3322 | st_code = e->body.tsx_state.tsx->status_code; |
| 3323 | ev_state = PJSIP_EVSUB_STATE_TERMINATED; |
Benny Prijono | 19eeb6e | 2009-06-04 22:16:47 +0000 | [diff] [blame] | 3324 | #endif |
Benny Prijono | 140beae | 2009-10-11 05:06:43 +0000 | [diff] [blame] | 3325 | break; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3326 | |
| 3327 | case PJSIP_INV_STATE_DISCONNECTED: |
| 3328 | st_code = e->body.tsx_state.tsx->status_code; |
| 3329 | ev_state = PJSIP_EVSUB_STATE_TERMINATED; |
| 3330 | break; |
Benny Prijono | 8befd9f | 2006-05-13 22:46:23 +0000 | [diff] [blame] | 3331 | |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 3332 | case PJSIP_INV_STATE_INCOMING: |
Benny Prijono | 8befd9f | 2006-05-13 22:46:23 +0000 | [diff] [blame] | 3333 | /* Nothing to do. Just to keep gcc from complaining about |
| 3334 | * unused enums. |
| 3335 | */ |
| 3336 | break; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3337 | } |
| 3338 | |
| 3339 | if (st_code != -1) { |
| 3340 | pjsip_tx_data *tdata; |
| 3341 | pj_status_t status; |
| 3342 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 3343 | status = pjsip_xfer_notify( call->xfer_sub, |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3344 | ev_state, st_code, |
| 3345 | NULL, &tdata); |
| 3346 | if (status != PJ_SUCCESS) { |
| 3347 | pjsua_perror(THIS_FILE, "Unable to create NOTIFY", status); |
| 3348 | } else { |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 3349 | status = pjsip_xfer_send_request(call->xfer_sub, tdata); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3350 | if (status != PJ_SUCCESS) { |
| 3351 | pjsua_perror(THIS_FILE, "Unable to send NOTIFY", status); |
| 3352 | } |
| 3353 | } |
| 3354 | } |
| 3355 | } |
| 3356 | |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 3357 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3358 | if (pjsua_var.ua_cfg.cb.on_call_state) |
| 3359 | (*pjsua_var.ua_cfg.cb.on_call_state)(call->index, e); |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 3360 | |
| 3361 | /* call->inv may be NULL now */ |
| 3362 | |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 3363 | /* Destroy media session when invite session is disconnected. */ |
| 3364 | if (inv->state == PJSIP_INV_STATE_DISCONNECTED) { |
Benny Prijono | 632ce71 | 2006-02-09 14:01:40 +0000 | [diff] [blame] | 3365 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 3366 | pj_assert(call != NULL); |
Benny Prijono | 632ce71 | 2006-02-09 14:01:40 +0000 | [diff] [blame] | 3367 | |
Benny Prijono | 275fd68 | 2006-03-22 11:59:11 +0000 | [diff] [blame] | 3368 | if (call) |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 3369 | pjsua_media_channel_deinit(call->index); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 3370 | |
Benny Prijono | 105217f | 2006-03-06 16:25:59 +0000 | [diff] [blame] | 3371 | /* Free call */ |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 3372 | call->inv = NULL; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3373 | --pjsua_var.call_cnt; |
Benny Prijono | 4be63b5 | 2006-11-25 14:50:25 +0000 | [diff] [blame] | 3374 | |
| 3375 | /* Reset call */ |
| 3376 | reset_call(call->index); |
| 3377 | |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 3378 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3379 | |
| 3380 | PJSUA_UNLOCK(); |
| 3381 | } |
| 3382 | |
| 3383 | /* |
| 3384 | * This callback is called by invite session framework when UAC session |
| 3385 | * has forked. |
| 3386 | */ |
| 3387 | static void pjsua_call_on_forked( pjsip_inv_session *inv, |
| 3388 | pjsip_event *e) |
| 3389 | { |
| 3390 | PJ_UNUSED_ARG(inv); |
| 3391 | PJ_UNUSED_ARG(e); |
| 3392 | |
| 3393 | PJ_TODO(HANDLE_FORKED_DIALOG); |
| 3394 | } |
| 3395 | |
| 3396 | |
| 3397 | /* |
Benny Prijono | 3c5e28b | 2008-09-24 10:10:15 +0000 | [diff] [blame] | 3398 | * Callback from UA layer when forked dialog response is received. |
| 3399 | */ |
| 3400 | pjsip_dialog* on_dlg_forked(pjsip_dialog *dlg, pjsip_rx_data *res) |
| 3401 | { |
| 3402 | if (dlg->uac_has_2xx && |
| 3403 | res->msg_info.cseq->method.id == PJSIP_INVITE_METHOD && |
| 3404 | pjsip_rdata_get_tsx(res) == NULL && |
| 3405 | res->msg_info.msg->line.status.code/100 == 2) |
| 3406 | { |
| 3407 | pjsip_dialog *forked_dlg; |
| 3408 | pjsip_tx_data *bye; |
| 3409 | pj_status_t status; |
| 3410 | |
| 3411 | /* Create forked dialog */ |
| 3412 | status = pjsip_dlg_fork(dlg, res, &forked_dlg); |
| 3413 | if (status != PJ_SUCCESS) |
| 3414 | return NULL; |
| 3415 | |
| 3416 | pjsip_dlg_inc_lock(forked_dlg); |
| 3417 | |
| 3418 | /* Disconnect the call */ |
| 3419 | status = pjsip_dlg_create_request(forked_dlg, &pjsip_bye_method, |
| 3420 | -1, &bye); |
| 3421 | if (status == PJ_SUCCESS) { |
| 3422 | status = pjsip_dlg_send_request(forked_dlg, bye, -1, NULL); |
| 3423 | } |
| 3424 | |
| 3425 | pjsip_dlg_dec_lock(forked_dlg); |
| 3426 | |
| 3427 | if (status != PJ_SUCCESS) { |
| 3428 | return NULL; |
| 3429 | } |
| 3430 | |
| 3431 | return forked_dlg; |
| 3432 | |
| 3433 | } else { |
| 3434 | return dlg; |
| 3435 | } |
| 3436 | } |
| 3437 | |
| 3438 | /* |
Benny Prijono | a38ada0 | 2006-07-02 14:22:35 +0000 | [diff] [blame] | 3439 | * Disconnect call upon error. |
| 3440 | */ |
| 3441 | static void call_disconnect( pjsip_inv_session *inv, |
| 3442 | int code ) |
| 3443 | { |
Benny Prijono | 59b3aed | 2008-01-15 16:54:54 +0000 | [diff] [blame] | 3444 | pjsua_call *call; |
Benny Prijono | a38ada0 | 2006-07-02 14:22:35 +0000 | [diff] [blame] | 3445 | pjsip_tx_data *tdata; |
| 3446 | pj_status_t status; |
| 3447 | |
Benny Prijono | 59b3aed | 2008-01-15 16:54:54 +0000 | [diff] [blame] | 3448 | call = (pjsua_call*) inv->dlg->mod_data[pjsua_var.mod.id]; |
| 3449 | |
Benny Prijono | a38ada0 | 2006-07-02 14:22:35 +0000 | [diff] [blame] | 3450 | status = pjsip_inv_end_session(inv, code, NULL, &tdata); |
Benny Prijono | 59b3aed | 2008-01-15 16:54:54 +0000 | [diff] [blame] | 3451 | if (status != PJ_SUCCESS) |
| 3452 | return; |
| 3453 | |
| 3454 | /* Add SDP in 488 status */ |
Benny Prijono | 9963998 | 2008-03-07 14:39:52 +0000 | [diff] [blame] | 3455 | if (call && call->med_tp && tdata->msg->type==PJSIP_RESPONSE_MSG && |
| 3456 | code==PJSIP_SC_NOT_ACCEPTABLE_HERE) |
| 3457 | { |
Benny Prijono | 59b3aed | 2008-01-15 16:54:54 +0000 | [diff] [blame] | 3458 | pjmedia_sdp_session *local_sdp; |
Benny Prijono | e1a5a85 | 2008-03-11 21:38:05 +0000 | [diff] [blame] | 3459 | pjmedia_transport_info ti; |
Benny Prijono | 59b3aed | 2008-01-15 16:54:54 +0000 | [diff] [blame] | 3460 | |
Benny Prijono | 734fc2d | 2008-03-17 16:05:35 +0000 | [diff] [blame] | 3461 | pjmedia_transport_info_init(&ti); |
Benny Prijono | e1a5a85 | 2008-03-11 21:38:05 +0000 | [diff] [blame] | 3462 | pjmedia_transport_get_info(call->med_tp, &ti); |
Benny Prijono | 59b3aed | 2008-01-15 16:54:54 +0000 | [diff] [blame] | 3463 | status = pjmedia_endpt_create_sdp(pjsua_var.med_endpt, tdata->pool, |
Benny Prijono | e1a5a85 | 2008-03-11 21:38:05 +0000 | [diff] [blame] | 3464 | 1, &ti.sock_info, &local_sdp); |
Benny Prijono | 59b3aed | 2008-01-15 16:54:54 +0000 | [diff] [blame] | 3465 | if (status == PJ_SUCCESS) { |
| 3466 | pjsip_create_sdp_body(tdata->pool, local_sdp, |
| 3467 | &tdata->msg->body); |
| 3468 | } |
| 3469 | } |
| 3470 | |
| 3471 | pjsip_inv_send_msg(inv, tdata); |
Benny Prijono | a38ada0 | 2006-07-02 14:22:35 +0000 | [diff] [blame] | 3472 | } |
| 3473 | |
| 3474 | /* |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3475 | * Callback to be called when SDP offer/answer negotiation has just completed |
| 3476 | * in the session. This function will start/update media if negotiation |
| 3477 | * has succeeded. |
| 3478 | */ |
| 3479 | static void pjsua_call_on_media_update(pjsip_inv_session *inv, |
| 3480 | pj_status_t status) |
| 3481 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3482 | pjsua_call *call; |
Benny Prijono | 224b4e2 | 2008-06-19 14:10:28 +0000 | [diff] [blame] | 3483 | const pjmedia_sdp_session *local_sdp; |
Benny Prijono | dbce2cf | 2007-03-28 16:24:00 +0000 | [diff] [blame] | 3484 | const pjmedia_sdp_session *remote_sdp; |
Nanang Izzuddin | 93bacd0 | 2010-06-15 09:56:39 +0000 | [diff] [blame] | 3485 | const pj_str_t st_update = {"UPDATE", 6}; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3486 | |
| 3487 | PJSUA_LOCK(); |
| 3488 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 3489 | call = (pjsua_call*) inv->dlg->mod_data[pjsua_var.mod.id]; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3490 | |
| 3491 | if (status != PJ_SUCCESS) { |
| 3492 | |
| 3493 | pjsua_perror(THIS_FILE, "SDP negotiation has failed", status); |
| 3494 | |
Benny Prijono | 2331d20 | 2008-06-26 15:46:52 +0000 | [diff] [blame] | 3495 | /* Do not deinitialize media since this may be a re-INVITE or |
| 3496 | * UPDATE (which in this case the media should not get affected |
| 3497 | * by the failed re-INVITE/UPDATE). The media will be shutdown |
| 3498 | * when call is disconnected anyway. |
| 3499 | */ |
Benny Prijono | 1d9b9a4 | 2006-09-25 13:40:12 +0000 | [diff] [blame] | 3500 | /* Stop/destroy media, if any */ |
Benny Prijono | 2331d20 | 2008-06-26 15:46:52 +0000 | [diff] [blame] | 3501 | /*pjsua_media_channel_deinit(call->index);*/ |
Benny Prijono | 1d9b9a4 | 2006-09-25 13:40:12 +0000 | [diff] [blame] | 3502 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3503 | /* Disconnect call if we're not in the middle of initializing an |
| 3504 | * UAS dialog and if this is not a re-INVITE |
| 3505 | */ |
| 3506 | if (inv->state != PJSIP_INV_STATE_NULL && |
| 3507 | inv->state != PJSIP_INV_STATE_CONFIRMED) |
| 3508 | { |
Benny Prijono | 2dbed82 | 2008-02-21 10:08:27 +0000 | [diff] [blame] | 3509 | call_disconnect(inv, PJSIP_SC_UNSUPPORTED_MEDIA_TYPE); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3510 | } |
| 3511 | |
| 3512 | PJSUA_UNLOCK(); |
| 3513 | return; |
| 3514 | } |
| 3515 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3516 | |
| 3517 | /* Get local and remote SDP */ |
Benny Prijono | 224b4e2 | 2008-06-19 14:10:28 +0000 | [diff] [blame] | 3518 | status = pjmedia_sdp_neg_get_active_local(call->inv->neg, &local_sdp); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3519 | if (status != PJ_SUCCESS) { |
| 3520 | pjsua_perror(THIS_FILE, |
| 3521 | "Unable to retrieve currently active local SDP", |
| 3522 | status); |
| 3523 | //call_disconnect(inv, PJSIP_SC_UNSUPPORTED_MEDIA_TYPE); |
| 3524 | PJSUA_UNLOCK(); |
| 3525 | return; |
| 3526 | } |
| 3527 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3528 | status = pjmedia_sdp_neg_get_active_remote(call->inv->neg, &remote_sdp); |
| 3529 | if (status != PJ_SUCCESS) { |
| 3530 | pjsua_perror(THIS_FILE, |
| 3531 | "Unable to retrieve currently active remote SDP", |
| 3532 | status); |
| 3533 | //call_disconnect(inv, PJSIP_SC_UNSUPPORTED_MEDIA_TYPE); |
| 3534 | PJSUA_UNLOCK(); |
| 3535 | return; |
| 3536 | } |
| 3537 | |
Benny Prijono | 91a6a17 | 2007-10-31 08:59:29 +0000 | [diff] [blame] | 3538 | /* Update remote's NAT type */ |
| 3539 | if (pjsua_var.ua_cfg.nat_type_in_sdp) { |
| 3540 | update_remote_nat_type(call, remote_sdp); |
| 3541 | } |
| 3542 | |
| 3543 | /* Update media channel with the new SDP */ |
Benny Prijono | c97608e | 2007-03-23 16:34:20 +0000 | [diff] [blame] | 3544 | status = pjsua_media_channel_update(call->index, local_sdp, remote_sdp); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3545 | if (status != PJ_SUCCESS) { |
| 3546 | pjsua_perror(THIS_FILE, "Unable to create media session", |
| 3547 | status); |
Benny Prijono | 59b3aed | 2008-01-15 16:54:54 +0000 | [diff] [blame] | 3548 | call_disconnect(inv, PJSIP_SC_NOT_ACCEPTABLE_HERE); |
Benny Prijono | 2331d20 | 2008-06-26 15:46:52 +0000 | [diff] [blame] | 3549 | /* No need to deinitialize; media will be shutdown when call |
| 3550 | * state is disconnected anyway. |
| 3551 | */ |
| 3552 | /*pjsua_media_channel_deinit(call->index);*/ |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3553 | PJSUA_UNLOCK(); |
| 3554 | return; |
| 3555 | } |
| 3556 | |
Nanang Izzuddin | 93bacd0 | 2010-06-15 09:56:39 +0000 | [diff] [blame] | 3557 | /* Ticket #476, handle the case of early media and remote support UPDATE */ |
| 3558 | if (inv->state == PJSIP_INV_STATE_EARLY && |
| 3559 | pjmedia_sdp_neg_was_answer_remote(inv->neg) && |
| 3560 | pjsip_dlg_remote_has_cap(inv->dlg, PJSIP_H_ALLOW, NULL, &st_update)== |
| 3561 | PJSIP_DIALOG_CAP_SUPPORTED) |
| 3562 | { |
| 3563 | status = lock_codec(call); |
| 3564 | if (status != PJ_SUCCESS) { |
| 3565 | pjsua_perror(THIS_FILE, "Unable to lock codec", status); |
| 3566 | } |
| 3567 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3568 | |
| 3569 | /* Call application callback, if any */ |
| 3570 | if (pjsua_var.ua_cfg.cb.on_call_media_state) |
| 3571 | pjsua_var.ua_cfg.cb.on_call_media_state(call->index); |
| 3572 | |
| 3573 | |
| 3574 | PJSUA_UNLOCK(); |
| 3575 | } |
| 3576 | |
| 3577 | |
Benny Prijono | dd63b99 | 2010-10-01 02:03:42 +0000 | [diff] [blame^] | 3578 | /* Modify SDP for call hold. */ |
| 3579 | static pj_status_t modify_sdp_of_call_hold(pjsua_call *call, |
| 3580 | pj_pool_t *pool, |
| 3581 | pjmedia_sdp_session *sdp) |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3582 | { |
Nanang Izzuddin | 99d6952 | 2008-08-04 15:01:38 +0000 | [diff] [blame] | 3583 | /* Call-hold is done by set the media direction to 'sendonly' |
| 3584 | * (PJMEDIA_DIR_ENCODING), except when current media direction is |
| 3585 | * 'inactive' (PJMEDIA_DIR_NONE). |
| 3586 | * (See RFC 3264 Section 8.4 and RFC 4317 Section 3.1) |
| 3587 | */ |
Benny Prijono | e086013 | 2009-06-05 10:14:20 +0000 | [diff] [blame] | 3588 | /* http://trac.pjsip.org/repos/ticket/880 |
| 3589 | if (call->media_dir != PJMEDIA_DIR_ENCODING) { |
| 3590 | */ |
Benny Prijono | dd63b99 | 2010-10-01 02:03:42 +0000 | [diff] [blame^] | 3591 | /* https://trac.pjsip.org/repos/ticket/1142: |
| 3592 | * configuration to use c=0.0.0.0 for call hold. |
| 3593 | */ |
| 3594 | if (call->call_hold_type == PJSUA_CALL_HOLD_TYPE_RFC2543) { |
| 3595 | pjmedia_sdp_conn *conn; |
| 3596 | pjmedia_sdp_attr *attr; |
| 3597 | |
| 3598 | /* Get SDP media connection line */ |
| 3599 | conn = sdp->media[0]->conn; |
| 3600 | if (!conn) |
| 3601 | conn = sdp->conn; |
| 3602 | |
| 3603 | /* Modify address */ |
| 3604 | conn->addr = pj_str("0.0.0.0"); |
| 3605 | |
| 3606 | /* Remove existing directions attributes */ |
| 3607 | pjmedia_sdp_media_remove_all_attr(sdp->media[0], "sendrecv"); |
| 3608 | pjmedia_sdp_media_remove_all_attr(sdp->media[0], "sendonly"); |
| 3609 | pjmedia_sdp_media_remove_all_attr(sdp->media[0], "recvonly"); |
| 3610 | pjmedia_sdp_media_remove_all_attr(sdp->media[0], "inactive"); |
| 3611 | |
| 3612 | /* Add inactive attribute */ |
| 3613 | attr = pjmedia_sdp_attr_create(pool, "inactive", NULL); |
| 3614 | pjmedia_sdp_media_add_attr(sdp->media[0], attr); |
| 3615 | |
| 3616 | |
| 3617 | } else { |
Nanang Izzuddin | 99d6952 | 2008-08-04 15:01:38 +0000 | [diff] [blame] | 3618 | pjmedia_sdp_attr *attr; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3619 | |
Nanang Izzuddin | 99d6952 | 2008-08-04 15:01:38 +0000 | [diff] [blame] | 3620 | /* Remove existing directions attributes */ |
| 3621 | pjmedia_sdp_media_remove_all_attr(sdp->media[0], "sendrecv"); |
| 3622 | pjmedia_sdp_media_remove_all_attr(sdp->media[0], "sendonly"); |
| 3623 | pjmedia_sdp_media_remove_all_attr(sdp->media[0], "recvonly"); |
| 3624 | pjmedia_sdp_media_remove_all_attr(sdp->media[0], "inactive"); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3625 | |
Benny Prijono | e086013 | 2009-06-05 10:14:20 +0000 | [diff] [blame] | 3626 | if (call->media_dir & PJMEDIA_DIR_ENCODING) { |
Nanang Izzuddin | 99d6952 | 2008-08-04 15:01:38 +0000 | [diff] [blame] | 3627 | /* Add sendonly attribute */ |
| 3628 | attr = pjmedia_sdp_attr_create(pool, "sendonly", NULL); |
| 3629 | pjmedia_sdp_media_add_attr(sdp->media[0], attr); |
| 3630 | } else { |
| 3631 | /* Add inactive attribute */ |
| 3632 | attr = pjmedia_sdp_attr_create(pool, "inactive", NULL); |
| 3633 | pjmedia_sdp_media_add_attr(sdp->media[0], attr); |
| 3634 | } |
| 3635 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3636 | |
Benny Prijono | dd63b99 | 2010-10-01 02:03:42 +0000 | [diff] [blame^] | 3637 | return PJ_SUCCESS; |
| 3638 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3639 | |
Benny Prijono | dd63b99 | 2010-10-01 02:03:42 +0000 | [diff] [blame^] | 3640 | /* Create SDP for call hold. */ |
| 3641 | static pj_status_t create_sdp_of_call_hold(pjsua_call *call, |
| 3642 | pjmedia_sdp_session **p_sdp) |
| 3643 | { |
| 3644 | pj_status_t status; |
| 3645 | pj_pool_t *pool; |
| 3646 | pjmedia_sdp_session *sdp; |
| 3647 | |
| 3648 | /* Use call's provisional pool */ |
| 3649 | pool = call->inv->pool_prov; |
| 3650 | |
| 3651 | /* Create new offer */ |
| 3652 | status = pjsua_media_channel_create_sdp(call->index, pool, NULL, &sdp, |
| 3653 | NULL); |
| 3654 | if (status != PJ_SUCCESS) { |
| 3655 | pjsua_perror(THIS_FILE, "Unable to create local SDP", status); |
| 3656 | return status; |
| 3657 | } |
| 3658 | |
| 3659 | status = modify_sdp_of_call_hold(call, pool, sdp); |
| 3660 | if (status != PJ_SUCCESS) |
| 3661 | return status; |
| 3662 | |
| 3663 | *p_sdp = sdp; |
| 3664 | |
| 3665 | return PJ_SUCCESS; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3666 | } |
| 3667 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3668 | /* |
| 3669 | * Called when session received new offer. |
| 3670 | */ |
| 3671 | static void pjsua_call_on_rx_offer(pjsip_inv_session *inv, |
| 3672 | const pjmedia_sdp_session *offer) |
| 3673 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3674 | pjsua_call *call; |
| 3675 | pjmedia_sdp_conn *conn; |
| 3676 | pjmedia_sdp_session *answer; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3677 | pj_status_t status; |
| 3678 | |
| 3679 | PJSUA_LOCK(); |
| 3680 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 3681 | call = (pjsua_call*) inv->dlg->mod_data[pjsua_var.mod.id]; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3682 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3683 | conn = offer->media[0]->conn; |
| 3684 | if (!conn) |
| 3685 | conn = offer->conn; |
| 3686 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3687 | /* Supply candidate answer */ |
Nanang Izzuddin | 99d6952 | 2008-08-04 15:01:38 +0000 | [diff] [blame] | 3688 | PJ_LOG(4,(THIS_FILE, "Call %d: received updated media offer", |
| 3689 | call->index)); |
Benny Prijono | 667952e | 2007-04-02 19:27:54 +0000 | [diff] [blame] | 3690 | |
Benny Prijono | 40d62b6 | 2009-08-12 17:53:47 +0000 | [diff] [blame] | 3691 | status = pjsua_media_channel_create_sdp(call->index, |
| 3692 | call->inv->pool_prov, |
Nanang Izzuddin | 99d6952 | 2008-08-04 15:01:38 +0000 | [diff] [blame] | 3693 | offer, &answer, NULL); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3694 | if (status != PJ_SUCCESS) { |
| 3695 | pjsua_perror(THIS_FILE, "Unable to create local SDP", status); |
| 3696 | PJSUA_UNLOCK(); |
| 3697 | return; |
| 3698 | } |
| 3699 | |
Nanang Izzuddin | 99d6952 | 2008-08-04 15:01:38 +0000 | [diff] [blame] | 3700 | /* Check if offer's conn address is zero */ |
| 3701 | if (pj_strcmp2(&conn->addr, "0.0.0.0")==0 || |
| 3702 | pj_strcmp2(&conn->addr, "0")==0) |
| 3703 | { |
| 3704 | /* Modify address */ |
| 3705 | answer->conn->addr = pj_str("0.0.0.0"); |
| 3706 | } |
| 3707 | |
| 3708 | /* Check if call is on-hold */ |
| 3709 | if (call->local_hold) { |
Benny Prijono | dd63b99 | 2010-10-01 02:03:42 +0000 | [diff] [blame^] | 3710 | modify_sdp_of_call_hold(call, call->inv->pool_prov, answer); |
Nanang Izzuddin | 99d6952 | 2008-08-04 15:01:38 +0000 | [diff] [blame] | 3711 | } |
| 3712 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3713 | status = pjsip_inv_set_sdp_answer(call->inv, answer); |
| 3714 | if (status != PJ_SUCCESS) { |
| 3715 | pjsua_perror(THIS_FILE, "Unable to set answer", status); |
| 3716 | PJSUA_UNLOCK(); |
| 3717 | return; |
| 3718 | } |
| 3719 | |
| 3720 | PJSUA_UNLOCK(); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 3721 | } |
| 3722 | |
| 3723 | |
| 3724 | /* |
Benny Prijono | 77998ce | 2007-06-20 10:03:46 +0000 | [diff] [blame] | 3725 | * Called to generate new offer. |
| 3726 | */ |
| 3727 | static void pjsua_call_on_create_offer(pjsip_inv_session *inv, |
| 3728 | pjmedia_sdp_session **offer) |
| 3729 | { |
| 3730 | pjsua_call *call; |
| 3731 | pj_status_t status; |
| 3732 | |
| 3733 | PJSUA_LOCK(); |
| 3734 | |
| 3735 | call = (pjsua_call*) inv->dlg->mod_data[pjsua_var.mod.id]; |
| 3736 | |
| 3737 | /* See if we've put call on hold. */ |
Nanang Izzuddin | 99d6952 | 2008-08-04 15:01:38 +0000 | [diff] [blame] | 3738 | if (call->local_hold) { |
Benny Prijono | 77998ce | 2007-06-20 10:03:46 +0000 | [diff] [blame] | 3739 | PJ_LOG(4,(THIS_FILE, |
Nanang Izzuddin | 99d6952 | 2008-08-04 15:01:38 +0000 | [diff] [blame] | 3740 | "Call %d: call is on-hold locally, creating call-hold SDP ", |
Benny Prijono | 77998ce | 2007-06-20 10:03:46 +0000 | [diff] [blame] | 3741 | call->index)); |
Nanang Izzuddin | 99d6952 | 2008-08-04 15:01:38 +0000 | [diff] [blame] | 3742 | status = create_sdp_of_call_hold( call, offer ); |
Benny Prijono | 77998ce | 2007-06-20 10:03:46 +0000 | [diff] [blame] | 3743 | } else { |
Benny Prijono | 77998ce | 2007-06-20 10:03:46 +0000 | [diff] [blame] | 3744 | PJ_LOG(4,(THIS_FILE, "Call %d: asked to send a new offer", |
| 3745 | call->index)); |
| 3746 | |
Benny Prijono | 40d62b6 | 2009-08-12 17:53:47 +0000 | [diff] [blame] | 3747 | status = pjsua_media_channel_create_sdp(call->index, |
| 3748 | call->inv->pool_prov, |
Benny Prijono | 25b2ea1 | 2008-01-24 19:20:54 +0000 | [diff] [blame] | 3749 | NULL, offer, NULL); |
Benny Prijono | 77998ce | 2007-06-20 10:03:46 +0000 | [diff] [blame] | 3750 | } |
| 3751 | |
| 3752 | if (status != PJ_SUCCESS) { |
| 3753 | pjsua_perror(THIS_FILE, "Unable to create local SDP", status); |
| 3754 | PJSUA_UNLOCK(); |
| 3755 | return; |
| 3756 | } |
| 3757 | |
Benny Prijono | 77998ce | 2007-06-20 10:03:46 +0000 | [diff] [blame] | 3758 | PJSUA_UNLOCK(); |
| 3759 | } |
| 3760 | |
| 3761 | |
| 3762 | /* |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3763 | * Callback called by event framework when the xfer subscription state |
| 3764 | * has changed. |
| 3765 | */ |
Benny Prijono | 4ddad2c | 2006-10-18 17:16:34 +0000 | [diff] [blame] | 3766 | static void xfer_client_on_evsub_state( pjsip_evsub *sub, pjsip_event *event) |
| 3767 | { |
| 3768 | |
| 3769 | PJ_UNUSED_ARG(event); |
| 3770 | |
| 3771 | /* |
| 3772 | * When subscription is accepted (got 200/OK to REFER), check if |
| 3773 | * subscription suppressed. |
| 3774 | */ |
| 3775 | if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_ACCEPTED) { |
| 3776 | |
| 3777 | pjsip_rx_data *rdata; |
| 3778 | pjsip_generic_string_hdr *refer_sub; |
| 3779 | const pj_str_t REFER_SUB = { "Refer-Sub", 9 }; |
| 3780 | pjsua_call *call; |
| 3781 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 3782 | 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] | 3783 | |
| 3784 | /* Must be receipt of response message */ |
| 3785 | pj_assert(event->type == PJSIP_EVENT_TSX_STATE && |
| 3786 | event->body.tsx_state.type == PJSIP_EVENT_RX_MSG); |
| 3787 | rdata = event->body.tsx_state.src.rdata; |
| 3788 | |
| 3789 | /* Find Refer-Sub header */ |
| 3790 | refer_sub = (pjsip_generic_string_hdr*) |
| 3791 | pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, |
| 3792 | &REFER_SUB, NULL); |
| 3793 | |
| 3794 | /* Check if subscription is suppressed */ |
| 3795 | if (refer_sub && pj_stricmp2(&refer_sub->hvalue, "false")==0) { |
| 3796 | /* Since no subscription is desired, assume that call has been |
| 3797 | * transfered successfully. |
| 3798 | */ |
| 3799 | if (call && pjsua_var.ua_cfg.cb.on_call_transfer_status) { |
| 3800 | const pj_str_t ACCEPTED = { "Accepted", 8 }; |
| 3801 | pj_bool_t cont = PJ_FALSE; |
| 3802 | (*pjsua_var.ua_cfg.cb.on_call_transfer_status)(call->index, |
| 3803 | 200, |
| 3804 | &ACCEPTED, |
| 3805 | PJ_TRUE, |
| 3806 | &cont); |
| 3807 | } |
| 3808 | |
| 3809 | /* Yes, subscription is suppressed. |
| 3810 | * Terminate our subscription now. |
| 3811 | */ |
| 3812 | PJ_LOG(4,(THIS_FILE, "Xfer subscription suppressed, terminating " |
| 3813 | "event subcription...")); |
| 3814 | pjsip_evsub_terminate(sub, PJ_TRUE); |
| 3815 | |
| 3816 | } else { |
| 3817 | /* Notify application about call transfer progress. |
| 3818 | * Initially notify with 100/Accepted status. |
| 3819 | */ |
| 3820 | if (call && pjsua_var.ua_cfg.cb.on_call_transfer_status) { |
| 3821 | const pj_str_t ACCEPTED = { "Accepted", 8 }; |
| 3822 | pj_bool_t cont = PJ_FALSE; |
| 3823 | (*pjsua_var.ua_cfg.cb.on_call_transfer_status)(call->index, |
| 3824 | 100, |
| 3825 | &ACCEPTED, |
| 3826 | PJ_FALSE, |
| 3827 | &cont); |
| 3828 | } |
| 3829 | } |
| 3830 | } |
| 3831 | /* |
| 3832 | * On incoming NOTIFY, notify application about call transfer progress. |
| 3833 | */ |
| 3834 | else if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_ACTIVE || |
| 3835 | pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED) |
| 3836 | { |
| 3837 | pjsua_call *call; |
| 3838 | pjsip_msg *msg; |
| 3839 | pjsip_msg_body *body; |
| 3840 | pjsip_status_line status_line; |
| 3841 | pj_bool_t is_last; |
| 3842 | pj_bool_t cont; |
| 3843 | pj_status_t status; |
| 3844 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 3845 | 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] | 3846 | |
| 3847 | /* When subscription is terminated, clear the xfer_sub member of |
| 3848 | * the inv_data. |
| 3849 | */ |
| 3850 | if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED) { |
| 3851 | pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, NULL); |
| 3852 | PJ_LOG(4,(THIS_FILE, "Xfer client subscription terminated")); |
| 3853 | |
| 3854 | } |
| 3855 | |
| 3856 | if (!call || !event || !pjsua_var.ua_cfg.cb.on_call_transfer_status) { |
| 3857 | /* Application is not interested with call progress status */ |
| 3858 | return; |
| 3859 | } |
| 3860 | |
| 3861 | /* This better be a NOTIFY request */ |
| 3862 | if (event->type == PJSIP_EVENT_TSX_STATE && |
| 3863 | event->body.tsx_state.type == PJSIP_EVENT_RX_MSG) |
| 3864 | { |
| 3865 | pjsip_rx_data *rdata; |
| 3866 | |
| 3867 | rdata = event->body.tsx_state.src.rdata; |
| 3868 | |
| 3869 | /* Check if there's body */ |
| 3870 | msg = rdata->msg_info.msg; |
| 3871 | body = msg->body; |
| 3872 | if (!body) { |
Benny Prijono | c6ff4b8 | 2009-05-12 15:55:09 +0000 | [diff] [blame] | 3873 | PJ_LOG(2,(THIS_FILE, |
Benny Prijono | 4ddad2c | 2006-10-18 17:16:34 +0000 | [diff] [blame] | 3874 | "Warning: received NOTIFY without message body")); |
| 3875 | return; |
| 3876 | } |
| 3877 | |
| 3878 | /* Check for appropriate content */ |
| 3879 | if (pj_stricmp2(&body->content_type.type, "message") != 0 || |
| 3880 | pj_stricmp2(&body->content_type.subtype, "sipfrag") != 0) |
| 3881 | { |
Benny Prijono | c6ff4b8 | 2009-05-12 15:55:09 +0000 | [diff] [blame] | 3882 | PJ_LOG(2,(THIS_FILE, |
Benny Prijono | 4ddad2c | 2006-10-18 17:16:34 +0000 | [diff] [blame] | 3883 | "Warning: received NOTIFY with non message/sipfrag " |
| 3884 | "content")); |
| 3885 | return; |
| 3886 | } |
| 3887 | |
| 3888 | /* Try to parse the content */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 3889 | status = pjsip_parse_status_line((char*)body->data, body->len, |
Benny Prijono | 4ddad2c | 2006-10-18 17:16:34 +0000 | [diff] [blame] | 3890 | &status_line); |
| 3891 | if (status != PJ_SUCCESS) { |
Benny Prijono | c6ff4b8 | 2009-05-12 15:55:09 +0000 | [diff] [blame] | 3892 | PJ_LOG(2,(THIS_FILE, |
Benny Prijono | 4ddad2c | 2006-10-18 17:16:34 +0000 | [diff] [blame] | 3893 | "Warning: received NOTIFY with invalid " |
| 3894 | "message/sipfrag content")); |
| 3895 | return; |
| 3896 | } |
| 3897 | |
| 3898 | } else { |
| 3899 | status_line.code = 500; |
| 3900 | status_line.reason = *pjsip_get_status_text(500); |
| 3901 | } |
| 3902 | |
| 3903 | /* Notify application */ |
| 3904 | is_last = (pjsip_evsub_get_state(sub)==PJSIP_EVSUB_STATE_TERMINATED); |
| 3905 | cont = !is_last; |
| 3906 | (*pjsua_var.ua_cfg.cb.on_call_transfer_status)(call->index, |
| 3907 | status_line.code, |
| 3908 | &status_line.reason, |
| 3909 | is_last, &cont); |
| 3910 | |
| 3911 | if (!cont) { |
| 3912 | pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, NULL); |
| 3913 | } |
Benny Prijono | 19eeb6e | 2009-06-04 22:16:47 +0000 | [diff] [blame] | 3914 | |
| 3915 | /* If the call transfer has completed but the subscription is |
| 3916 | * not terminated, terminate it now. |
| 3917 | */ |
| 3918 | if (status_line.code/100 == 2 && !is_last) { |
| 3919 | pjsip_tx_data *tdata; |
| 3920 | |
| 3921 | status = pjsip_evsub_initiate(sub, &pjsip_subscribe_method, |
| 3922 | 0, &tdata); |
| 3923 | if (status == PJ_SUCCESS) |
| 3924 | status = pjsip_evsub_send_request(sub, tdata); |
| 3925 | } |
Benny Prijono | 4ddad2c | 2006-10-18 17:16:34 +0000 | [diff] [blame] | 3926 | } |
| 3927 | } |
| 3928 | |
| 3929 | |
| 3930 | /* |
| 3931 | * Callback called by event framework when the xfer subscription state |
| 3932 | * has changed. |
| 3933 | */ |
| 3934 | 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] | 3935 | { |
| 3936 | |
| 3937 | PJ_UNUSED_ARG(event); |
| 3938 | |
| 3939 | /* |
Benny Prijono | d524e82 | 2006-09-22 12:48:18 +0000 | [diff] [blame] | 3940 | * When subscription is terminated, clear the xfer_sub member of |
| 3941 | * the inv_data. |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3942 | */ |
| 3943 | if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED) { |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 3944 | pjsua_call *call; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3945 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 3946 | 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] | 3947 | if (!call) |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3948 | return; |
| 3949 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 3950 | pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, NULL); |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 3951 | call->xfer_sub = NULL; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3952 | |
Benny Prijono | 4ddad2c | 2006-10-18 17:16:34 +0000 | [diff] [blame] | 3953 | PJ_LOG(4,(THIS_FILE, "Xfer server subscription terminated")); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3954 | } |
| 3955 | } |
| 3956 | |
| 3957 | |
| 3958 | /* |
| 3959 | * Follow transfer (REFER) request. |
| 3960 | */ |
| 3961 | static void on_call_transfered( pjsip_inv_session *inv, |
| 3962 | pjsip_rx_data *rdata ) |
| 3963 | { |
| 3964 | pj_status_t status; |
| 3965 | pjsip_tx_data *tdata; |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 3966 | pjsua_call *existing_call; |
| 3967 | int new_call; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3968 | const pj_str_t str_refer_to = { "Refer-To", 8}; |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 3969 | const pj_str_t str_refer_sub = { "Refer-Sub", 9 }; |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 3970 | const pj_str_t str_ref_by = { "Referred-By", 11 }; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3971 | pjsip_generic_string_hdr *refer_to; |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 3972 | pjsip_generic_string_hdr *refer_sub; |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 3973 | pjsip_hdr *ref_by_hdr; |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 3974 | pj_bool_t no_refer_sub = PJ_FALSE; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3975 | char *uri; |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 3976 | pjsua_msg_data msg_data; |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 3977 | pj_str_t tmp; |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 3978 | pjsip_status_code code; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3979 | pjsip_evsub *sub; |
| 3980 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 3981 | existing_call = (pjsua_call*) inv->dlg->mod_data[pjsua_var.mod.id]; |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 3982 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3983 | /* Find the Refer-To header */ |
| 3984 | refer_to = (pjsip_generic_string_hdr*) |
| 3985 | pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_refer_to, NULL); |
| 3986 | |
| 3987 | if (refer_to == NULL) { |
| 3988 | /* Invalid Request. |
| 3989 | * No Refer-To header! |
| 3990 | */ |
| 3991 | PJ_LOG(4,(THIS_FILE, "Received REFER without Refer-To header!")); |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 3992 | pjsip_dlg_respond( inv->dlg, rdata, 400, NULL, NULL, NULL); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 3993 | return; |
| 3994 | } |
| 3995 | |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 3996 | /* Find optional Refer-Sub header */ |
| 3997 | refer_sub = (pjsip_generic_string_hdr*) |
| 3998 | pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_refer_sub, NULL); |
| 3999 | |
| 4000 | if (refer_sub) { |
| 4001 | if (!pj_strnicmp2(&refer_sub->hvalue, "true", 4)==0) |
| 4002 | no_refer_sub = PJ_TRUE; |
| 4003 | } |
| 4004 | |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 4005 | /* Find optional Referred-By header (to be copied onto outgoing INVITE |
| 4006 | * request. |
| 4007 | */ |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 4008 | ref_by_hdr = (pjsip_hdr*) |
| 4009 | 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] | 4010 | NULL); |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 4011 | |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 4012 | /* Notify callback */ |
Benny Prijono | c54dcb3 | 2008-04-08 23:33:15 +0000 | [diff] [blame] | 4013 | code = PJSIP_SC_ACCEPTED; |
Benny Prijono | 4ddad2c | 2006-10-18 17:16:34 +0000 | [diff] [blame] | 4014 | if (pjsua_var.ua_cfg.cb.on_call_transfer_request) |
| 4015 | (*pjsua_var.ua_cfg.cb.on_call_transfer_request)(existing_call->index, |
| 4016 | &refer_to->hvalue, |
| 4017 | &code); |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 4018 | |
| 4019 | if (code < 200) |
Benny Prijono | c54dcb3 | 2008-04-08 23:33:15 +0000 | [diff] [blame] | 4020 | code = PJSIP_SC_ACCEPTED; |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 4021 | if (code >= 300) { |
| 4022 | /* Application rejects call transfer request */ |
| 4023 | pjsip_dlg_respond( inv->dlg, rdata, code, NULL, NULL, NULL); |
| 4024 | return; |
| 4025 | } |
| 4026 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 4027 | PJ_LOG(3,(THIS_FILE, "Call to %.*s is being transfered to %.*s", |
| 4028 | (int)inv->dlg->remote.info_str.slen, |
| 4029 | inv->dlg->remote.info_str.ptr, |
| 4030 | (int)refer_to->hvalue.slen, |
| 4031 | refer_to->hvalue.ptr)); |
| 4032 | |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 4033 | if (no_refer_sub) { |
| 4034 | /* |
Benny Prijono | c54dcb3 | 2008-04-08 23:33:15 +0000 | [diff] [blame] | 4035 | * Always answer with 2xx. |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 4036 | */ |
| 4037 | pjsip_tx_data *tdata; |
| 4038 | const pj_str_t str_false = { "false", 5}; |
| 4039 | pjsip_hdr *hdr; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 4040 | |
Benny Prijono | c54dcb3 | 2008-04-08 23:33:15 +0000 | [diff] [blame] | 4041 | status = pjsip_dlg_create_response(inv->dlg, rdata, code, NULL, |
| 4042 | &tdata); |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 4043 | if (status != PJ_SUCCESS) { |
Benny Prijono | c54dcb3 | 2008-04-08 23:33:15 +0000 | [diff] [blame] | 4044 | pjsua_perror(THIS_FILE, "Unable to create 2xx response to REFER", |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 4045 | status); |
| 4046 | return; |
| 4047 | } |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 4048 | |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 4049 | /* Add Refer-Sub header */ |
| 4050 | hdr = (pjsip_hdr*) |
| 4051 | pjsip_generic_string_hdr_create(tdata->pool, &str_refer_sub, |
| 4052 | &str_false); |
| 4053 | pjsip_msg_add_hdr(tdata->msg, hdr); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 4054 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 4055 | |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 4056 | /* Send answer */ |
| 4057 | status = pjsip_dlg_send_response(inv->dlg, pjsip_rdata_get_tsx(rdata), |
| 4058 | tdata); |
| 4059 | if (status != PJ_SUCCESS) { |
Benny Prijono | c54dcb3 | 2008-04-08 23:33:15 +0000 | [diff] [blame] | 4060 | pjsua_perror(THIS_FILE, "Unable to create 2xx response to REFER", |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 4061 | status); |
| 4062 | return; |
| 4063 | } |
| 4064 | |
| 4065 | /* Don't have subscription */ |
| 4066 | sub = NULL; |
| 4067 | |
| 4068 | } else { |
| 4069 | struct pjsip_evsub_user xfer_cb; |
| 4070 | pjsip_hdr hdr_list; |
| 4071 | |
| 4072 | /* Init callback */ |
| 4073 | pj_bzero(&xfer_cb, sizeof(xfer_cb)); |
Benny Prijono | 4ddad2c | 2006-10-18 17:16:34 +0000 | [diff] [blame] | 4074 | xfer_cb.on_evsub_state = &xfer_server_on_evsub_state; |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 4075 | |
| 4076 | /* Init additional header list to be sent with REFER response */ |
| 4077 | pj_list_init(&hdr_list); |
| 4078 | |
| 4079 | /* Create transferee event subscription */ |
| 4080 | status = pjsip_xfer_create_uas( inv->dlg, &xfer_cb, rdata, &sub); |
| 4081 | if (status != PJ_SUCCESS) { |
| 4082 | pjsua_perror(THIS_FILE, "Unable to create xfer uas", status); |
| 4083 | pjsip_dlg_respond( inv->dlg, rdata, 500, NULL, NULL, NULL); |
| 4084 | return; |
| 4085 | } |
| 4086 | |
| 4087 | /* If there's Refer-Sub header and the value is "true", send back |
| 4088 | * Refer-Sub in the response with value "true" too. |
| 4089 | */ |
| 4090 | if (refer_sub) { |
| 4091 | const pj_str_t str_true = { "true", 4 }; |
| 4092 | pjsip_hdr *hdr; |
| 4093 | |
| 4094 | hdr = (pjsip_hdr*) |
| 4095 | pjsip_generic_string_hdr_create(inv->dlg->pool, |
| 4096 | &str_refer_sub, |
| 4097 | &str_true); |
| 4098 | pj_list_push_back(&hdr_list, hdr); |
| 4099 | |
| 4100 | } |
| 4101 | |
Benny Prijono | c54dcb3 | 2008-04-08 23:33:15 +0000 | [diff] [blame] | 4102 | /* Accept the REFER request, send 2xx. */ |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 4103 | pjsip_xfer_accept(sub, rdata, code, &hdr_list); |
| 4104 | |
| 4105 | /* Create initial NOTIFY request */ |
| 4106 | status = pjsip_xfer_notify( sub, PJSIP_EVSUB_STATE_ACTIVE, |
| 4107 | 100, NULL, &tdata); |
| 4108 | if (status != PJ_SUCCESS) { |
| 4109 | pjsua_perror(THIS_FILE, "Unable to create NOTIFY to REFER", |
| 4110 | status); |
| 4111 | return; |
| 4112 | } |
| 4113 | |
| 4114 | /* Send initial NOTIFY request */ |
| 4115 | status = pjsip_xfer_send_request( sub, tdata); |
| 4116 | if (status != PJ_SUCCESS) { |
| 4117 | pjsua_perror(THIS_FILE, "Unable to send NOTIFY to REFER", status); |
| 4118 | return; |
| 4119 | } |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 4120 | } |
| 4121 | |
| 4122 | /* We're cheating here. |
| 4123 | * We need to get a null terminated string from a pj_str_t. |
| 4124 | * So grab the pointer from the hvalue and NULL terminate it, knowing |
| 4125 | * that the NULL position will be occupied by a newline. |
| 4126 | */ |
| 4127 | uri = refer_to->hvalue.ptr; |
| 4128 | uri[refer_to->hvalue.slen] = '\0'; |
| 4129 | |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 4130 | /* Init msg_data */ |
| 4131 | pjsua_msg_data_init(&msg_data); |
| 4132 | |
| 4133 | /* If Referred-By header is present in the REFER request, copy this |
| 4134 | * to the outgoing INVITE request. |
| 4135 | */ |
| 4136 | if (ref_by_hdr != NULL) { |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 4137 | pjsip_hdr *dup = (pjsip_hdr*) |
| 4138 | pjsip_hdr_clone(rdata->tp_info.pool, ref_by_hdr); |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 4139 | pj_list_push_back(&msg_data.hdr_list, dup); |
| 4140 | } |
| 4141 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 4142 | /* Now make the outgoing call. */ |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 4143 | tmp = pj_str(uri); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 4144 | status = pjsua_call_make_call(existing_call->acc_id, &tmp, 0, |
Benny Prijono | 053f522 | 2006-11-11 16:16:04 +0000 | [diff] [blame] | 4145 | existing_call->user_data, &msg_data, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 4146 | &new_call); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 4147 | if (status != PJ_SUCCESS) { |
| 4148 | |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 4149 | /* Notify xferer about the error (if we have subscription) */ |
| 4150 | if (sub) { |
| 4151 | status = pjsip_xfer_notify(sub, PJSIP_EVSUB_STATE_TERMINATED, |
| 4152 | 500, NULL, &tdata); |
| 4153 | if (status != PJ_SUCCESS) { |
| 4154 | pjsua_perror(THIS_FILE, "Unable to create NOTIFY to REFER", |
| 4155 | status); |
| 4156 | return; |
| 4157 | } |
| 4158 | status = pjsip_xfer_send_request(sub, tdata); |
| 4159 | if (status != PJ_SUCCESS) { |
| 4160 | pjsua_perror(THIS_FILE, "Unable to send NOTIFY to REFER", |
| 4161 | status); |
| 4162 | return; |
| 4163 | } |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 4164 | } |
| 4165 | return; |
| 4166 | } |
| 4167 | |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 4168 | if (sub) { |
| 4169 | /* Put the server subscription in inv_data. |
| 4170 | * Subsequent state changed in pjsua_inv_on_state_changed() will be |
| 4171 | * reported back to the server subscription. |
| 4172 | */ |
| 4173 | pjsua_var.calls[new_call].xfer_sub = sub; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 4174 | |
Benny Prijono | c8141a8 | 2006-08-20 09:12:19 +0000 | [diff] [blame] | 4175 | /* Put the invite_data in the subscription. */ |
| 4176 | pjsip_evsub_set_mod_data(sub, pjsua_var.mod.id, |
| 4177 | &pjsua_var.calls[new_call]); |
| 4178 | } |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 4179 | } |
| 4180 | |
| 4181 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 4182 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 4183 | /* |
| 4184 | * This callback is called when transaction state has changed in INVITE |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 4185 | * session. We use this to trap: |
| 4186 | * - incoming REFER request. |
| 4187 | * - incoming MESSAGE request. |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 4188 | */ |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 4189 | static void pjsua_call_on_tsx_state_changed(pjsip_inv_session *inv, |
| 4190 | pjsip_transaction *tsx, |
| 4191 | pjsip_event *e) |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 4192 | { |
Benny Prijono | 2285e7e | 2008-12-17 14:28:18 +0000 | [diff] [blame] | 4193 | pjsua_call *call; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 4194 | |
| 4195 | PJSUA_LOCK(); |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 4196 | |
Benny Prijono | 2285e7e | 2008-12-17 14:28:18 +0000 | [diff] [blame] | 4197 | call = (pjsua_call*) inv->dlg->mod_data[pjsua_var.mod.id]; |
| 4198 | |
| 4199 | if (call == NULL) { |
| 4200 | PJSUA_UNLOCK(); |
| 4201 | return; |
| 4202 | } |
| 4203 | |
Benny Prijono | 19eeb6e | 2009-06-04 22:16:47 +0000 | [diff] [blame] | 4204 | if (call->inv == NULL) { |
| 4205 | /* Shouldn't happen. It happens only when we don't terminate the |
| 4206 | * server subscription caused by REFER after the call has been |
| 4207 | * transfered (and this call has been disconnected), and we |
| 4208 | * receive another REFER for this call. |
| 4209 | */ |
| 4210 | PJSUA_UNLOCK(); |
| 4211 | return; |
| 4212 | } |
| 4213 | |
Benny Prijono | feb69f4 | 2007-10-05 09:12:26 +0000 | [diff] [blame] | 4214 | /* Notify application callback first */ |
| 4215 | if (pjsua_var.ua_cfg.cb.on_call_tsx_state) { |
| 4216 | (*pjsua_var.ua_cfg.cb.on_call_tsx_state)(call->index, tsx, e); |
| 4217 | } |
| 4218 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 4219 | if (tsx->role==PJSIP_ROLE_UAS && |
| 4220 | tsx->state==PJSIP_TSX_STATE_TRYING && |
Benny Prijono | 1f61a8f | 2007-08-16 10:11:44 +0000 | [diff] [blame] | 4221 | pjsip_method_cmp(&tsx->method, pjsip_get_refer_method())==0) |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 4222 | { |
| 4223 | /* |
| 4224 | * Incoming REFER request. |
| 4225 | */ |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 4226 | on_call_transfered(call->inv, e->body.tsx_state.src.rdata); |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 4227 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 4228 | } |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 4229 | else if (tsx->role==PJSIP_ROLE_UAS && |
| 4230 | tsx->state==PJSIP_TSX_STATE_TRYING && |
| 4231 | pjsip_method_cmp(&tsx->method, &pjsip_message_method)==0) |
| 4232 | { |
| 4233 | /* |
| 4234 | * Incoming MESSAGE request! |
| 4235 | */ |
| 4236 | pjsip_rx_data *rdata; |
| 4237 | pjsip_msg *msg; |
| 4238 | pjsip_accept_hdr *accept_hdr; |
| 4239 | pj_status_t status; |
| 4240 | |
| 4241 | rdata = e->body.tsx_state.src.rdata; |
| 4242 | msg = rdata->msg_info.msg; |
| 4243 | |
| 4244 | /* Request MUST have message body, with Content-Type equal to |
| 4245 | * "text/plain". |
| 4246 | */ |
| 4247 | if (pjsua_im_accept_pager(rdata, &accept_hdr) == PJ_FALSE) { |
| 4248 | |
| 4249 | pjsip_hdr hdr_list; |
| 4250 | |
| 4251 | pj_list_init(&hdr_list); |
| 4252 | pj_list_push_back(&hdr_list, accept_hdr); |
| 4253 | |
| 4254 | pjsip_dlg_respond( inv->dlg, rdata, PJSIP_SC_NOT_ACCEPTABLE_HERE, |
| 4255 | NULL, &hdr_list, NULL ); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 4256 | PJSUA_UNLOCK(); |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 4257 | return; |
| 4258 | } |
| 4259 | |
| 4260 | /* Respond with 200 first, so that remote doesn't retransmit in case |
| 4261 | * the UI takes too long to process the message. |
| 4262 | */ |
| 4263 | status = pjsip_dlg_respond( inv->dlg, rdata, 200, NULL, NULL, NULL); |
| 4264 | |
| 4265 | /* Process MESSAGE request */ |
| 4266 | pjsua_im_process_pager(call->index, &inv->dlg->remote.info_str, |
| 4267 | &inv->dlg->local.info_str, rdata); |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 4268 | |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 4269 | } |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 4270 | else if (tsx->role == PJSIP_ROLE_UAC && |
| 4271 | pjsip_method_cmp(&tsx->method, &pjsip_message_method)==0) |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 4272 | { |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 4273 | /* Handle outgoing pager status */ |
| 4274 | if (tsx->status_code >= 200) { |
| 4275 | pjsua_im_data *im_data; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 4276 | |
Benny Prijono | a1e6968 | 2007-05-11 15:14:34 +0000 | [diff] [blame] | 4277 | im_data = (pjsua_im_data*) tsx->mod_data[pjsua_var.mod.id]; |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 4278 | /* im_data can be NULL if this is typing indication */ |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 4279 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 4280 | if (im_data && pjsua_var.ua_cfg.cb.on_pager_status) { |
| 4281 | pjsua_var.ua_cfg.cb.on_pager_status(im_data->call_id, |
| 4282 | &im_data->to, |
| 4283 | &im_data->body, |
| 4284 | im_data->user_data, |
Benny Prijono | ba5926a | 2007-05-02 11:29:37 +0000 | [diff] [blame] | 4285 | (pjsip_status_code) |
| 4286 | tsx->status_code, |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 4287 | &tsx->status_text); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 4288 | } |
Benny Prijono | fccab71 | 2006-02-22 22:23:22 +0000 | [diff] [blame] | 4289 | } |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 4290 | } |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 4291 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 4292 | |
Benny Prijono | eebe9af | 2006-06-13 22:57:13 +0000 | [diff] [blame] | 4293 | PJSUA_UNLOCK(); |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 4294 | } |
Benny Prijono | 5e51a4e | 2008-11-27 00:06:46 +0000 | [diff] [blame] | 4295 | |
| 4296 | |
| 4297 | /* Redirection handler */ |
Benny Prijono | 08a48b8 | 2008-11-27 12:42:07 +0000 | [diff] [blame] | 4298 | static pjsip_redirect_op pjsua_call_on_redirected(pjsip_inv_session *inv, |
| 4299 | const pjsip_uri *target, |
| 4300 | const pjsip_event *e) |
Benny Prijono | 5e51a4e | 2008-11-27 00:06:46 +0000 | [diff] [blame] | 4301 | { |
| 4302 | 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] | 4303 | pjsip_redirect_op op; |
Benny Prijono | 5e51a4e | 2008-11-27 00:06:46 +0000 | [diff] [blame] | 4304 | |
| 4305 | PJSUA_LOCK(); |
| 4306 | |
| 4307 | if (pjsua_var.ua_cfg.cb.on_call_redirected) { |
Benny Prijono | 08a48b8 | 2008-11-27 12:42:07 +0000 | [diff] [blame] | 4308 | op = (*pjsua_var.ua_cfg.cb.on_call_redirected)(call->index, |
| 4309 | target, e); |
Benny Prijono | 5e51a4e | 2008-11-27 00:06:46 +0000 | [diff] [blame] | 4310 | } else { |
| 4311 | PJ_LOG(4,(THIS_FILE, "Unhandled redirection for call %d " |
| 4312 | "(callback not implemented by application). Disconnecting " |
| 4313 | "call.", |
| 4314 | call->index)); |
Benny Prijono | 08a48b8 | 2008-11-27 12:42:07 +0000 | [diff] [blame] | 4315 | op = PJSIP_REDIRECT_STOP; |
Benny Prijono | 5e51a4e | 2008-11-27 00:06:46 +0000 | [diff] [blame] | 4316 | } |
| 4317 | |
| 4318 | PJSUA_UNLOCK(); |
Benny Prijono | 08a48b8 | 2008-11-27 12:42:07 +0000 | [diff] [blame] | 4319 | |
| 4320 | return op; |
Benny Prijono | 5e51a4e | 2008-11-27 00:06:46 +0000 | [diff] [blame] | 4321 | } |
| 4322 | |