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