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 | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 20 | #include <pj/log.h> |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 21 | #include "pjsua_imp.h" |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 22 | |
| 23 | /* |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 24 | * pjsua_call.c |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 25 | * |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 26 | * Call (INVITE) related stuffs. |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 27 | */ |
| 28 | |
Benny Prijono | 8befd9f | 2006-05-13 22:46:23 +0000 | [diff] [blame] | 29 | #define THIS_FILE "pjsua_call.c" |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 30 | |
| 31 | |
Benny Prijono | 105217f | 2006-03-06 16:25:59 +0000 | [diff] [blame] | 32 | #define REFRESH_CALL_TIMER 0x63 |
| 33 | #define HANGUP_CALL_TIMER 0x64 |
| 34 | |
| 35 | /* Proto */ |
| 36 | static void schedule_call_timer( pjsua_call *call, pj_timer_entry *e, |
| 37 | int timer_type, int duration ); |
| 38 | |
| 39 | /* |
| 40 | * Timer callback when UAS needs to send re-INVITE to see if remote |
| 41 | * is still there. |
| 42 | */ |
| 43 | static void call_on_timer(pj_timer_heap_t *ht, pj_timer_entry *e) |
| 44 | { |
| 45 | pjsua_call *call = e->user_data; |
| 46 | |
| 47 | PJ_UNUSED_ARG(ht); |
| 48 | |
| 49 | if (e->id == REFRESH_CALL_TIMER) { |
| 50 | |
| 51 | /* If call is still not connected, hangup. */ |
| 52 | if (call->inv->state != PJSIP_INV_STATE_CONFIRMED) { |
| 53 | PJ_LOG(3,(THIS_FILE, "Refresh call timer is called when " |
| 54 | "invite is still not confirmed. Call %d will " |
| 55 | "disconnect.", call->index)); |
| 56 | pjsua_call_hangup(call->index); |
| 57 | } else { |
| 58 | PJ_LOG(3,(THIS_FILE, "Refreshing call %d", call->index)); |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 59 | schedule_call_timer(call,e,REFRESH_CALL_TIMER, |
| 60 | pjsua.config.uas_refresh); |
Benny Prijono | 105217f | 2006-03-06 16:25:59 +0000 | [diff] [blame] | 61 | pjsua_call_reinvite(call->index); |
| 62 | } |
| 63 | |
| 64 | } else if (e->id == HANGUP_CALL_TIMER) { |
| 65 | PJ_LOG(3,(THIS_FILE, "Call %d duration exceeded, disconnecting call", |
| 66 | call->index)); |
| 67 | pjsua_call_hangup(call->index); |
| 68 | |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /* |
| 73 | * Schedule call timer. |
| 74 | */ |
| 75 | static void schedule_call_timer( pjsua_call *call, pj_timer_entry *e, |
| 76 | int timer_type, int duration ) |
| 77 | { |
| 78 | pj_time_val timeout; |
| 79 | |
| 80 | if (duration == 0) { |
| 81 | /* Cancel timer. */ |
| 82 | if (e->id != 0) { |
| 83 | pjsip_endpt_cancel_timer(pjsua.endpt, e); |
| 84 | e->id = 0; |
| 85 | } |
| 86 | |
| 87 | } else { |
| 88 | /* Schedule timer. */ |
| 89 | timeout.sec = duration; |
| 90 | timeout.msec = 0; |
| 91 | |
| 92 | e->cb = &call_on_timer; |
| 93 | e->id = timer_type; |
| 94 | e->user_data = call; |
| 95 | |
| 96 | pjsip_endpt_schedule_timer( pjsua.endpt, e, &timeout); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | |
Benny Prijono | 275fd68 | 2006-03-22 11:59:11 +0000 | [diff] [blame] | 101 | /* |
| 102 | * Destroy the call's media |
| 103 | */ |
| 104 | static pj_status_t call_destroy_media(int call_index) |
| 105 | { |
| 106 | pjsua_call *call = &pjsua.calls[call_index]; |
| 107 | |
| 108 | if (call->conf_slot > 0) { |
| 109 | pjmedia_conf_remove_port(pjsua.mconf, call->conf_slot); |
| 110 | call->conf_slot = 0; |
| 111 | } |
| 112 | |
| 113 | if (call->session) { |
Benny Prijono | 46ecff8 | 2006-03-30 16:46:36 +0000 | [diff] [blame] | 114 | /* Destroy session (this will also close RTP/RTCP sockets). */ |
| 115 | pjmedia_session_destroy(call->session); |
Benny Prijono | 275fd68 | 2006-03-22 11:59:11 +0000 | [diff] [blame] | 116 | call->session = NULL; |
| 117 | |
Benny Prijono | 8befd9f | 2006-05-13 22:46:23 +0000 | [diff] [blame] | 118 | PJ_LOG(3,(THIS_FILE, "Media session for call %d is destroyed", |
| 119 | call_index)); |
Benny Prijono | 275fd68 | 2006-03-22 11:59:11 +0000 | [diff] [blame] | 120 | |
Benny Prijono | 8befd9f | 2006-05-13 22:46:23 +0000 | [diff] [blame] | 121 | } |
Benny Prijono | 275fd68 | 2006-03-22 11:59:11 +0000 | [diff] [blame] | 122 | |
| 123 | return PJ_SUCCESS; |
| 124 | } |
| 125 | |
| 126 | |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 127 | /** |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 128 | * Get maximum number of calls configured in pjsua. |
| 129 | */ |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 130 | PJ_DEF(unsigned) pjsua_call_get_max_count(void) |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 131 | { |
| 132 | return pjsua.config.max_calls; |
| 133 | } |
| 134 | |
| 135 | |
| 136 | /** |
| 137 | * Get current number of active calls. |
| 138 | */ |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 139 | PJ_DEF(unsigned) pjsua_call_get_count(void) |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 140 | { |
| 141 | return pjsua.call_cnt; |
| 142 | } |
| 143 | |
| 144 | |
| 145 | /** |
| 146 | * Check if the specified call is active. |
| 147 | */ |
| 148 | PJ_DEF(pj_bool_t) pjsua_call_is_active(unsigned call_index) |
| 149 | { |
| 150 | PJ_ASSERT_RETURN(call_index < pjsua.config.max_calls, |
| 151 | PJ_EINVAL); |
| 152 | return pjsua.calls[call_index].inv != NULL && |
| 153 | pjsua.calls[call_index].inv->state != PJSIP_INV_STATE_DISCONNECTED; |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * Check if call has a media session. |
| 158 | */ |
| 159 | PJ_DEF(pj_bool_t) pjsua_call_has_media(unsigned call_index) |
| 160 | { |
| 161 | PJ_ASSERT_RETURN(call_index < pjsua.config.max_calls, PJ_EINVAL); |
| 162 | return pjsua.calls[call_index].session != NULL; |
| 163 | } |
| 164 | |
| 165 | |
| 166 | /** |
| 167 | * Get call info. |
| 168 | */ |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 169 | PJ_DEF(pj_status_t) pjsua_call_get_info( unsigned call_index, |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 170 | pjsua_call_info *info) |
| 171 | { |
| 172 | pjsua_call *call; |
| 173 | |
| 174 | PJ_ASSERT_RETURN(call_index < pjsua.config.max_calls, |
| 175 | PJ_EINVAL); |
| 176 | |
| 177 | pj_memset(info, 0, sizeof(pjsua_call_info)); |
| 178 | |
| 179 | call = &pjsua.calls[call_index]; |
| 180 | info->active = pjsua_call_is_active(call_index); |
| 181 | |
| 182 | if (call->inv == NULL) |
| 183 | return PJ_SUCCESS; |
| 184 | |
| 185 | info->index = call_index; |
| 186 | info->role = call->inv->role; |
| 187 | info->local_info = call->inv->dlg->local.info_str; |
| 188 | info->remote_info = call->inv->dlg->remote.info_str; |
| 189 | info->state = call->inv->state; |
| 190 | info->state_text = pj_str((char*)pjsip_inv_state_name(info->state)); |
| 191 | |
| 192 | if (info->state >= PJSIP_INV_STATE_DISCONNECTED) { |
| 193 | |
| 194 | info->total_duration = call->dis_time; |
| 195 | PJ_TIME_VAL_SUB(info->total_duration, call->start_time); |
| 196 | |
| 197 | if (call->conn_time.sec) { |
| 198 | info->connect_duration = call->dis_time; |
| 199 | PJ_TIME_VAL_SUB(info->total_duration, call->conn_time); |
| 200 | } |
| 201 | |
| 202 | } else if (info->state == PJSIP_INV_STATE_CONFIRMED) { |
| 203 | |
| 204 | pj_gettimeofday(&info->total_duration); |
| 205 | PJ_TIME_VAL_SUB(info->total_duration, call->start_time); |
| 206 | |
| 207 | pj_gettimeofday(&info->connect_duration); |
| 208 | PJ_TIME_VAL_SUB(info->connect_duration, call->conn_time); |
| 209 | |
| 210 | } else { |
| 211 | pj_gettimeofday(&info->total_duration); |
| 212 | PJ_TIME_VAL_SUB(info->total_duration, call->start_time); |
| 213 | } |
| 214 | |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 215 | info->last_status = call->last_code; |
| 216 | info->last_status_text = *pjsip_get_status_text(info->last_status); |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 217 | |
| 218 | info->has_media = (call->session != NULL); |
| 219 | info->conf_slot = call->conf_slot; |
| 220 | |
| 221 | return PJ_SUCCESS; |
| 222 | } |
| 223 | |
| 224 | |
| 225 | /** |
| 226 | * Duplicate call info. |
| 227 | */ |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 228 | PJ_DEF(void) pjsua_call_info_dup( pj_pool_t *pool, |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 229 | pjsua_call_info *dst_info, |
| 230 | const pjsua_call_info *src_info) |
| 231 | { |
| 232 | PJ_ASSERT_ON_FAIL(pool && dst_info && src_info, return); |
| 233 | |
| 234 | pj_memcpy(dst_info, src_info, sizeof(pjsua_call_info)); |
| 235 | |
| 236 | pj_strdup(pool, &dst_info->local_info, &src_info->local_info); |
| 237 | pj_strdup(pool, &dst_info->remote_info, &src_info->remote_info); |
| 238 | |
| 239 | /* state_text and cause_text belong to pjsip, so don't need to be |
| 240 | * duplicated because they'll always be available. |
| 241 | */ |
| 242 | } |
| 243 | |
| 244 | |
| 245 | /** |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 246 | * Make outgoing call. |
| 247 | */ |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 248 | PJ_DEF(pj_status_t) pjsua_call_make_call(unsigned acc_index, |
| 249 | const pj_str_t *dest_uri, |
| 250 | int *p_call_index) |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 251 | { |
Benny Prijono | 1c2bf46 | 2006-03-05 11:54:02 +0000 | [diff] [blame] | 252 | pjsip_dialog *dlg = NULL; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 253 | pjmedia_sdp_session *offer; |
Benny Prijono | 1c2bf46 | 2006-03-05 11:54:02 +0000 | [diff] [blame] | 254 | pjsip_inv_session *inv = NULL; |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 255 | unsigned call_index; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 256 | pjsip_tx_data *tdata; |
| 257 | pj_status_t status; |
| 258 | |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 259 | |
| 260 | PJ_ASSERT_RETURN(acc_index==0 || acc_index < pjsua.config.acc_cnt, |
| 261 | PJ_EINVAL); |
| 262 | |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 263 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 264 | /* Find free call slot. */ |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 265 | for (call_index=0; call_index<pjsua.config.max_calls; ++call_index) { |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 266 | if (pjsua.calls[call_index].inv == NULL) |
Benny Prijono | f04ffdd | 2006-02-21 00:11:18 +0000 | [diff] [blame] | 267 | break; |
| 268 | } |
| 269 | |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 270 | if (call_index == pjsua.config.max_calls) { |
Benny Prijono | f04ffdd | 2006-02-21 00:11:18 +0000 | [diff] [blame] | 271 | PJ_LOG(3,(THIS_FILE, "Error: too many calls!")); |
| 272 | return PJ_ETOOMANY; |
| 273 | } |
| 274 | |
Benny Prijono | e21e784 | 2006-04-09 16:46:05 +0000 | [diff] [blame] | 275 | /* Mark call start time. */ |
| 276 | pj_gettimeofday(&pjsua.calls[call_index].start_time); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 277 | |
Benny Prijono | e21e784 | 2006-04-09 16:46:05 +0000 | [diff] [blame] | 278 | /* Reset first response time */ |
| 279 | pjsua.calls[call_index].res_time.sec = 0; |
| 280 | |
| 281 | /* Create outgoing dialog: */ |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 282 | status = pjsip_dlg_create_uac( pjsip_ua_instance(), |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 283 | &pjsua.config.acc_config[acc_index].id, |
| 284 | &pjsua.config.acc_config[acc_index].contact, |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 285 | dest_uri, dest_uri, |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 286 | &dlg); |
| 287 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 288 | pjsua_perror(THIS_FILE, "Dialog creation failed", status); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 289 | return status; |
| 290 | } |
| 291 | |
| 292 | /* Get media capability from media endpoint: */ |
| 293 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 294 | status = pjmedia_endpt_create_sdp( pjsua.med_endpt, dlg->pool, 1, |
| 295 | &pjsua.calls[call_index].skinfo, |
Benny Prijono | f04ffdd | 2006-02-21 00:11:18 +0000 | [diff] [blame] | 296 | &offer); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 297 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 298 | pjsua_perror(THIS_FILE, "pjmedia unable to create SDP", status); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 299 | goto on_error; |
| 300 | } |
| 301 | |
| 302 | /* Create the INVITE session: */ |
| 303 | |
| 304 | status = pjsip_inv_create_uac( dlg, offer, 0, &inv); |
| 305 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 306 | pjsua_perror(THIS_FILE, "Invite session creation failed", status); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 307 | goto on_error; |
| 308 | } |
| 309 | |
| 310 | |
| 311 | /* Create and associate our data in the session. */ |
| 312 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 313 | pjsua.calls[call_index].inv = inv; |
| 314 | |
| 315 | dlg->mod_data[pjsua.mod.id] = &pjsua.calls[call_index]; |
| 316 | inv->mod_data[pjsua.mod.id] = &pjsua.calls[call_index]; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 317 | |
| 318 | |
| 319 | /* Set dialog Route-Set: */ |
| 320 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 321 | if (!pj_list_empty(&pjsua.acc[acc_index].route_set)) |
| 322 | pjsip_dlg_set_route_set(dlg, &pjsua.acc[acc_index].route_set); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 323 | |
| 324 | |
| 325 | /* Set credentials: */ |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 326 | if (pjsua.config.acc_config[acc_index].cred_count) { |
| 327 | pjsua_acc_config *acc_cfg = &pjsua.config.acc_config[acc_index]; |
| 328 | pjsip_auth_clt_set_credentials( &dlg->auth_sess, |
| 329 | acc_cfg->cred_count, |
| 330 | acc_cfg->cred_info); |
| 331 | } |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 332 | |
| 333 | |
| 334 | /* Create initial INVITE: */ |
| 335 | |
| 336 | status = pjsip_inv_invite(inv, &tdata); |
| 337 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 338 | pjsua_perror(THIS_FILE, "Unable to create initial INVITE request", |
| 339 | status); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 340 | goto on_error; |
| 341 | } |
| 342 | |
| 343 | |
| 344 | /* Send initial INVITE: */ |
| 345 | |
Benny Prijono | e8b0d3b | 2006-03-17 17:57:52 +0000 | [diff] [blame] | 346 | status = pjsip_inv_send_msg(inv, tdata); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 347 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 348 | pjsua_perror(THIS_FILE, "Unable to send initial INVITE request", |
| 349 | status); |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 350 | |
| 351 | /* Upon failure to send first request, both dialog and invite |
| 352 | * session would have been cleared. |
| 353 | */ |
| 354 | inv = NULL; |
| 355 | dlg = NULL; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 356 | goto on_error; |
| 357 | } |
| 358 | |
| 359 | |
| 360 | /* Done. */ |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 361 | |
| 362 | ++pjsua.call_cnt; |
| 363 | |
| 364 | if (p_call_index) |
| 365 | *p_call_index = call_index; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 366 | |
| 367 | return PJ_SUCCESS; |
| 368 | |
| 369 | |
| 370 | on_error: |
Benny Prijono | 1c2bf46 | 2006-03-05 11:54:02 +0000 | [diff] [blame] | 371 | if (inv != NULL) { |
| 372 | pjsip_inv_terminate(inv, PJSIP_SC_OK, PJ_FALSE); |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 373 | } else if (dlg) { |
Benny Prijono | 1c2bf46 | 2006-03-05 11:54:02 +0000 | [diff] [blame] | 374 | pjsip_dlg_terminate(dlg); |
| 375 | } |
| 376 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 377 | if (call_index != -1) { |
| 378 | pjsua.calls[call_index].inv = NULL; |
| 379 | } |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 380 | return status; |
| 381 | } |
| 382 | |
| 383 | |
| 384 | /** |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 385 | * Answer call. |
| 386 | */ |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 387 | PJ_DEF(pj_status_t) pjsua_call_answer(int call_index, int code) |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 388 | { |
| 389 | pjsip_tx_data *tdata; |
| 390 | pj_status_t status; |
| 391 | |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 392 | PJ_ASSERT_RETURN( call_index >= 0 && |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 393 | call_index < (int)pjsua.config.max_calls, |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 394 | PJ_EINVAL); |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 395 | |
| 396 | if (pjsua.calls[call_index].inv == NULL) { |
| 397 | PJ_LOG(3,(THIS_FILE, "Call %d already disconnected")); |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 398 | return PJSIP_ESESSIONTERMINATED; |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 399 | } |
| 400 | |
| 401 | status = pjsip_inv_answer(pjsua.calls[call_index].inv, |
| 402 | code, NULL, NULL, &tdata); |
| 403 | if (status == PJ_SUCCESS) |
| 404 | status = pjsip_inv_send_msg(pjsua.calls[call_index].inv, |
| 405 | tdata); |
| 406 | |
| 407 | if (status != PJ_SUCCESS) |
| 408 | pjsua_perror(THIS_FILE, "Unable to create/send response", |
| 409 | status); |
| 410 | |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 411 | return status; |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 412 | } |
| 413 | |
| 414 | |
| 415 | /** |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 416 | * Handle incoming INVITE request. |
| 417 | */ |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 418 | pj_bool_t pjsua_call_on_incoming(pjsip_rx_data *rdata) |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 419 | { |
| 420 | pjsip_dialog *dlg = pjsip_rdata_get_dlg(rdata); |
| 421 | pjsip_transaction *tsx = pjsip_rdata_get_tsx(rdata); |
| 422 | pjsip_msg *msg = rdata->msg_info.msg; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 423 | pjsip_tx_data *response = NULL; |
| 424 | unsigned options = 0; |
Benny Prijono | 1c2bf46 | 2006-03-05 11:54:02 +0000 | [diff] [blame] | 425 | pjsip_inv_session *inv = NULL; |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 426 | int acc_index; |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 427 | unsigned call_index; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 428 | pjmedia_sdp_session *answer; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 429 | pj_status_t status; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 430 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 431 | /* Don't want to handle anything but INVITE */ |
| 432 | if (msg->line.req.method.id != PJSIP_INVITE_METHOD) |
| 433 | return PJ_FALSE; |
| 434 | |
| 435 | /* Don't want to handle anything that's already associated with |
| 436 | * existing dialog or transaction. |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 437 | */ |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 438 | if (dlg || tsx) |
| 439 | return PJ_FALSE; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 440 | |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 441 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 442 | /* Verify that we can handle the request. */ |
| 443 | status = pjsip_inv_verify_request(rdata, &options, NULL, NULL, |
| 444 | pjsua.endpt, &response); |
| 445 | if (status != PJ_SUCCESS) { |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 446 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 447 | /* |
| 448 | * No we can't handle the incoming INVITE request. |
| 449 | */ |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 450 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 451 | if (response) { |
| 452 | pjsip_response_addr res_addr; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 453 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 454 | pjsip_get_response_addr(response->pool, rdata, &res_addr); |
| 455 | pjsip_endpt_send_response(pjsua.endpt, &res_addr, response, |
| 456 | NULL, NULL); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 457 | |
| 458 | } else { |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 459 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 460 | /* Respond with 500 (Internal Server Error) */ |
| 461 | pjsip_endpt_respond_stateless(pjsua.endpt, rdata, 500, NULL, |
| 462 | NULL, NULL); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 465 | return PJ_TRUE; |
| 466 | } |
| 467 | |
| 468 | |
| 469 | /* |
| 470 | * Yes we can handle the incoming INVITE request. |
| 471 | */ |
| 472 | |
| 473 | /* Find free call slot. */ |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 474 | for (call_index=0; call_index < pjsua.config.max_calls; ++call_index) { |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 475 | if (pjsua.calls[call_index].inv == NULL) |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 476 | break; |
| 477 | } |
| 478 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 479 | if (call_index == PJSUA_MAX_CALLS) { |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 480 | pjsip_endpt_respond_stateless(pjsua.endpt, rdata, |
| 481 | PJSIP_SC_BUSY_HERE, NULL, |
| 482 | NULL, NULL); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 483 | return PJ_TRUE; |
| 484 | } |
| 485 | |
Benny Prijono | e21e784 | 2006-04-09 16:46:05 +0000 | [diff] [blame] | 486 | /* Mark call start time. */ |
| 487 | pj_gettimeofday(&pjsua.calls[call_index].start_time); |
| 488 | |
| 489 | /* Reset first response time */ |
| 490 | pjsua.calls[call_index].res_time.sec = 0; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 491 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 492 | /* Get media capability from media endpoint: */ |
| 493 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 494 | status = pjmedia_endpt_create_sdp( pjsua.med_endpt, rdata->tp_info.pool, 1, |
| 495 | &pjsua.calls[call_index].skinfo, |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 496 | &answer ); |
| 497 | if (status != PJ_SUCCESS) { |
| 498 | pjsip_endpt_respond_stateless(pjsua.endpt, rdata, 500, NULL, |
| 499 | NULL, NULL); |
| 500 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 501 | return PJ_TRUE; |
| 502 | } |
| 503 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 504 | /* TODO: |
| 505 | * |
| 506 | * Get which account is most likely to be associated with this incoming |
| 507 | * call. We need the account to find which contact URI to put for |
| 508 | * the call. |
| 509 | */ |
| 510 | acc_index = 0; |
| 511 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 512 | /* Create dialog: */ |
| 513 | |
| 514 | status = pjsip_dlg_create_uas( pjsip_ua_instance(), rdata, |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 515 | &pjsua.config.acc_config[acc_index].contact, |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 516 | &dlg); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 517 | if (status != PJ_SUCCESS) { |
| 518 | pjsip_endpt_respond_stateless(pjsua.endpt, rdata, 500, NULL, |
| 519 | NULL, NULL); |
| 520 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 521 | return PJ_TRUE; |
| 522 | } |
| 523 | |
| 524 | |
| 525 | /* Create invite session: */ |
| 526 | |
| 527 | status = pjsip_inv_create_uas( dlg, rdata, answer, 0, &inv); |
| 528 | if (status != PJ_SUCCESS) { |
| 529 | |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 530 | pjsip_dlg_respond(dlg, rdata, 500, NULL, NULL, NULL); |
Benny Prijono | 1c2bf46 | 2006-03-05 11:54:02 +0000 | [diff] [blame] | 531 | pjsip_dlg_terminate(dlg); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 532 | return PJ_TRUE; |
| 533 | } |
| 534 | |
| 535 | |
| 536 | /* Create and attach pjsua data to the dialog: */ |
| 537 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 538 | pjsua.calls[call_index].inv = inv; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 539 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 540 | dlg->mod_data[pjsua.mod.id] = &pjsua.calls[call_index]; |
| 541 | inv->mod_data[pjsua.mod.id] = &pjsua.calls[call_index]; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 542 | |
| 543 | |
Benny Prijono | 64f851e | 2006-02-23 13:49:28 +0000 | [diff] [blame] | 544 | /* Must answer with some response to initial INVITE. |
| 545 | * If auto-answer flag is set, send 200 straight away, otherwise send 100. |
| 546 | */ |
| 547 | |
| 548 | status = pjsip_inv_initial_answer(inv, rdata, |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 549 | (pjsua.config.auto_answer ? |
| 550 | pjsua.config.auto_answer : 100), |
Benny Prijono | 64f851e | 2006-02-23 13:49:28 +0000 | [diff] [blame] | 551 | NULL, NULL, &response); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 552 | if (status != PJ_SUCCESS) { |
| 553 | |
Benny Prijono | ccb03fa | 2006-03-06 13:35:47 +0000 | [diff] [blame] | 554 | int st_code; |
Benny Prijono | d2ae29d | 2006-02-22 15:42:31 +0000 | [diff] [blame] | 555 | |
Benny Prijono | ccb03fa | 2006-03-06 13:35:47 +0000 | [diff] [blame] | 556 | pjsua_perror(THIS_FILE, "Unable to send answer to incoming INVITE", |
| 557 | status); |
| 558 | |
| 559 | /* If failed to send 2xx response, there's a good chance that it is |
| 560 | * because SDP negotiation has failed. |
| 561 | */ |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 562 | if (pjsua.config.auto_answer/100 == 2) |
Benny Prijono | ccb03fa | 2006-03-06 13:35:47 +0000 | [diff] [blame] | 563 | st_code = PJSIP_SC_UNSUPPORTED_MEDIA_TYPE; |
| 564 | else |
| 565 | st_code = 500; |
| 566 | |
| 567 | pjsip_dlg_respond(dlg, rdata, st_code, NULL, NULL, NULL); |
| 568 | pjsip_inv_terminate(inv, st_code, PJ_FALSE); |
| 569 | return PJ_TRUE; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 570 | |
| 571 | } else { |
Benny Prijono | e8b0d3b | 2006-03-17 17:57:52 +0000 | [diff] [blame] | 572 | status = pjsip_inv_send_msg(inv, response); |
Benny Prijono | d2ae29d | 2006-02-22 15:42:31 +0000 | [diff] [blame] | 573 | if (status != PJ_SUCCESS) |
| 574 | pjsua_perror(THIS_FILE, "Unable to send 100 response", status); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 575 | } |
| 576 | |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 577 | if (pjsua.config.auto_answer < 200) { |
Benny Prijono | 64f851e | 2006-02-23 13:49:28 +0000 | [diff] [blame] | 578 | PJ_LOG(3,(THIS_FILE, |
| 579 | "\nIncoming call!!\n" |
| 580 | "From: %.*s\n" |
| 581 | "To: %.*s\n" |
| 582 | "(press 'a' to answer, 'h' to decline)", |
| 583 | (int)dlg->remote.info_str.slen, |
| 584 | dlg->remote.info_str.ptr, |
| 585 | (int)dlg->local.info_str.slen, |
| 586 | dlg->local.info_str.ptr)); |
| 587 | } else { |
| 588 | PJ_LOG(3,(THIS_FILE, |
| 589 | "Call From:%.*s To:%.*s was answered with %d (%s)", |
| 590 | (int)dlg->remote.info_str.slen, |
| 591 | dlg->remote.info_str.ptr, |
| 592 | (int)dlg->local.info_str.slen, |
| 593 | dlg->local.info_str.ptr, |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 594 | pjsua.config.auto_answer, |
| 595 | pjsip_get_status_text(pjsua.config.auto_answer)->ptr )); |
Benny Prijono | 64f851e | 2006-02-23 13:49:28 +0000 | [diff] [blame] | 596 | } |
| 597 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 598 | ++pjsua.call_cnt; |
| 599 | |
Benny Prijono | 105217f | 2006-03-06 16:25:59 +0000 | [diff] [blame] | 600 | /* Schedule timer to refresh. */ |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 601 | if (pjsua.config.uas_refresh > 0) { |
Benny Prijono | 105217f | 2006-03-06 16:25:59 +0000 | [diff] [blame] | 602 | schedule_call_timer( &pjsua.calls[call_index], |
| 603 | &pjsua.calls[call_index].refresh_tm, |
| 604 | REFRESH_CALL_TIMER, |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 605 | pjsua.config.uas_refresh); |
Benny Prijono | 105217f | 2006-03-06 16:25:59 +0000 | [diff] [blame] | 606 | } |
| 607 | |
| 608 | /* Schedule timer to hangup call. */ |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 609 | if (pjsua.config.uas_duration > 0) { |
Benny Prijono | 105217f | 2006-03-06 16:25:59 +0000 | [diff] [blame] | 610 | schedule_call_timer( &pjsua.calls[call_index], |
| 611 | &pjsua.calls[call_index].hangup_tm, |
| 612 | HANGUP_CALL_TIMER, |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 613 | pjsua.config.uas_duration); |
Benny Prijono | 105217f | 2006-03-06 16:25:59 +0000 | [diff] [blame] | 614 | } |
| 615 | |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 616 | /* Notify application */ |
| 617 | if (pjsua.cb.on_incoming_call) |
| 618 | pjsua.cb.on_incoming_call(acc_index, call_index, rdata); |
| 619 | |
| 620 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 621 | /* This INVITE request has been handled. */ |
| 622 | return PJ_TRUE; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 623 | } |
| 624 | |
| 625 | |
| 626 | /* |
| 627 | * This callback receives notification from invite session when the |
| 628 | * session state has changed. |
| 629 | */ |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 630 | static void pjsua_call_on_state_changed(pjsip_inv_session *inv, |
| 631 | pjsip_event *e) |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 632 | { |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 633 | pjsua_call *call = inv->dlg->mod_data[pjsua.mod.id]; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 634 | |
Benny Prijono | e21e784 | 2006-04-09 16:46:05 +0000 | [diff] [blame] | 635 | if (!call) |
| 636 | return; |
| 637 | |
| 638 | /* Get call times */ |
| 639 | switch (inv->state) { |
| 640 | case PJSIP_INV_STATE_EARLY: |
| 641 | case PJSIP_INV_STATE_CONNECTING: |
| 642 | if (call->res_time.sec == 0) |
| 643 | pj_gettimeofday(&call->res_time); |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 644 | call->last_code = e->body.tsx_state.tsx->status_code; |
Benny Prijono | e21e784 | 2006-04-09 16:46:05 +0000 | [diff] [blame] | 645 | break; |
| 646 | case PJSIP_INV_STATE_CONFIRMED: |
| 647 | pj_gettimeofday(&call->conn_time); |
| 648 | break; |
| 649 | case PJSIP_INV_STATE_DISCONNECTED: |
| 650 | pj_gettimeofday(&call->dis_time); |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 651 | if (e->body.tsx_state.tsx->status_code > call->last_code) { |
| 652 | call->last_code = e->body.tsx_state.tsx->status_code; |
| 653 | } |
Benny Prijono | e21e784 | 2006-04-09 16:46:05 +0000 | [diff] [blame] | 654 | break; |
Benny Prijono | 8befd9f | 2006-05-13 22:46:23 +0000 | [diff] [blame] | 655 | default: |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 656 | call->last_code = e->body.tsx_state.tsx->status_code; |
Benny Prijono | 8befd9f | 2006-05-13 22:46:23 +0000 | [diff] [blame] | 657 | break; |
Benny Prijono | e21e784 | 2006-04-09 16:46:05 +0000 | [diff] [blame] | 658 | } |
| 659 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 660 | /* If this is an outgoing INVITE that was created because of |
| 661 | * REFER/transfer, send NOTIFY to transferer. |
| 662 | */ |
Benny Prijono | e21e784 | 2006-04-09 16:46:05 +0000 | [diff] [blame] | 663 | if (call->xfer_sub && e->type==PJSIP_EVENT_TSX_STATE) { |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 664 | int st_code = -1; |
| 665 | pjsip_evsub_state ev_state = PJSIP_EVSUB_STATE_ACTIVE; |
| 666 | |
| 667 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 668 | switch (call->inv->state) { |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 669 | case PJSIP_INV_STATE_NULL: |
| 670 | case PJSIP_INV_STATE_CALLING: |
| 671 | /* Do nothing */ |
| 672 | break; |
| 673 | |
| 674 | case PJSIP_INV_STATE_EARLY: |
| 675 | case PJSIP_INV_STATE_CONNECTING: |
| 676 | st_code = e->body.tsx_state.tsx->status_code; |
| 677 | ev_state = PJSIP_EVSUB_STATE_ACTIVE; |
| 678 | break; |
| 679 | |
| 680 | case PJSIP_INV_STATE_CONFIRMED: |
| 681 | /* When state is confirmed, send the final 200/OK and terminate |
| 682 | * subscription. |
| 683 | */ |
| 684 | st_code = e->body.tsx_state.tsx->status_code; |
| 685 | ev_state = PJSIP_EVSUB_STATE_TERMINATED; |
| 686 | break; |
| 687 | |
| 688 | case PJSIP_INV_STATE_DISCONNECTED: |
| 689 | st_code = e->body.tsx_state.tsx->status_code; |
| 690 | ev_state = PJSIP_EVSUB_STATE_TERMINATED; |
| 691 | break; |
Benny Prijono | 8befd9f | 2006-05-13 22:46:23 +0000 | [diff] [blame] | 692 | |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 693 | case PJSIP_INV_STATE_INCOMING: |
Benny Prijono | 8befd9f | 2006-05-13 22:46:23 +0000 | [diff] [blame] | 694 | /* Nothing to do. Just to keep gcc from complaining about |
| 695 | * unused enums. |
| 696 | */ |
| 697 | break; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | if (st_code != -1) { |
| 701 | pjsip_tx_data *tdata; |
| 702 | pj_status_t status; |
| 703 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 704 | status = pjsip_xfer_notify( call->xfer_sub, |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 705 | ev_state, st_code, |
| 706 | NULL, &tdata); |
| 707 | if (status != PJ_SUCCESS) { |
| 708 | pjsua_perror(THIS_FILE, "Unable to create NOTIFY", status); |
| 709 | } else { |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 710 | status = pjsip_xfer_send_request(call->xfer_sub, tdata); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 711 | if (status != PJ_SUCCESS) { |
| 712 | pjsua_perror(THIS_FILE, "Unable to send NOTIFY", status); |
| 713 | } |
| 714 | } |
| 715 | } |
| 716 | } |
| 717 | |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 718 | |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 719 | if (pjsua.cb.on_call_state) |
| 720 | (*pjsua.cb.on_call_state)(call->index, e); |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 721 | |
| 722 | /* call->inv may be NULL now */ |
| 723 | |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 724 | /* Destroy media session when invite session is disconnected. */ |
| 725 | if (inv->state == PJSIP_INV_STATE_DISCONNECTED) { |
Benny Prijono | 632ce71 | 2006-02-09 14:01:40 +0000 | [diff] [blame] | 726 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 727 | pj_assert(call != NULL); |
Benny Prijono | 632ce71 | 2006-02-09 14:01:40 +0000 | [diff] [blame] | 728 | |
Benny Prijono | 275fd68 | 2006-03-22 11:59:11 +0000 | [diff] [blame] | 729 | if (call) |
| 730 | call_destroy_media(call->index); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 731 | |
Benny Prijono | 105217f | 2006-03-06 16:25:59 +0000 | [diff] [blame] | 732 | /* Remove timers. */ |
| 733 | schedule_call_timer(call, &call->refresh_tm, REFRESH_CALL_TIMER, 0); |
| 734 | schedule_call_timer(call, &call->hangup_tm, HANGUP_CALL_TIMER, 0); |
| 735 | |
| 736 | /* Free call */ |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 737 | call->inv = NULL; |
| 738 | --pjsua.call_cnt; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 739 | } |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 740 | } |
| 741 | |
| 742 | |
| 743 | /* |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 744 | * Callback called by event framework when the xfer subscription state |
| 745 | * has changed. |
| 746 | */ |
| 747 | static void xfer_on_evsub_state( pjsip_evsub *sub, pjsip_event *event) |
| 748 | { |
| 749 | |
| 750 | PJ_UNUSED_ARG(event); |
| 751 | |
| 752 | /* |
| 753 | * We're only interested when subscription is terminated, to |
| 754 | * clear the xfer_sub member of the inv_data. |
| 755 | */ |
| 756 | if (pjsip_evsub_get_state(sub) == PJSIP_EVSUB_STATE_TERMINATED) { |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 757 | pjsua_call *call; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 758 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 759 | call = pjsip_evsub_get_mod_data(sub, pjsua.mod.id); |
| 760 | if (!call) |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 761 | return; |
| 762 | |
| 763 | pjsip_evsub_set_mod_data(sub, pjsua.mod.id, NULL); |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 764 | call->xfer_sub = NULL; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 765 | |
| 766 | PJ_LOG(3,(THIS_FILE, "Xfer subscription terminated")); |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | |
| 771 | /* |
| 772 | * Follow transfer (REFER) request. |
| 773 | */ |
| 774 | static void on_call_transfered( pjsip_inv_session *inv, |
| 775 | pjsip_rx_data *rdata ) |
| 776 | { |
| 777 | pj_status_t status; |
| 778 | pjsip_tx_data *tdata; |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 779 | pjsua_call *existing_call; |
| 780 | int new_call; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 781 | const pj_str_t str_refer_to = { "Refer-To", 8}; |
| 782 | pjsip_generic_string_hdr *refer_to; |
| 783 | char *uri; |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 784 | pj_str_t tmp; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 785 | struct pjsip_evsub_user xfer_cb; |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 786 | pjsip_status_code code; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 787 | pjsip_evsub *sub; |
| 788 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 789 | existing_call = inv->dlg->mod_data[pjsua.mod.id]; |
| 790 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 791 | /* Find the Refer-To header */ |
| 792 | refer_to = (pjsip_generic_string_hdr*) |
| 793 | pjsip_msg_find_hdr_by_name(rdata->msg_info.msg, &str_refer_to, NULL); |
| 794 | |
| 795 | if (refer_to == NULL) { |
| 796 | /* Invalid Request. |
| 797 | * No Refer-To header! |
| 798 | */ |
| 799 | PJ_LOG(4,(THIS_FILE, "Received REFER without Refer-To header!")); |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 800 | pjsip_dlg_respond( inv->dlg, rdata, 400, NULL, NULL, NULL); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 801 | return; |
| 802 | } |
| 803 | |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 804 | /* Notify callback */ |
| 805 | code = PJSIP_SC_OK; |
| 806 | if (pjsua.cb.on_call_transfered) |
| 807 | (*pjsua.cb.on_call_transfered)(existing_call->index, |
| 808 | &refer_to->hvalue, &code); |
| 809 | |
| 810 | if (code < 200) |
| 811 | code = 200; |
| 812 | if (code >= 300) { |
| 813 | /* Application rejects call transfer request */ |
| 814 | pjsip_dlg_respond( inv->dlg, rdata, code, NULL, NULL, NULL); |
| 815 | return; |
| 816 | } |
| 817 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 818 | PJ_LOG(3,(THIS_FILE, "Call to %.*s is being transfered to %.*s", |
| 819 | (int)inv->dlg->remote.info_str.slen, |
| 820 | inv->dlg->remote.info_str.ptr, |
| 821 | (int)refer_to->hvalue.slen, |
| 822 | refer_to->hvalue.ptr)); |
| 823 | |
| 824 | /* Init callback */ |
| 825 | pj_memset(&xfer_cb, 0, sizeof(xfer_cb)); |
| 826 | xfer_cb.on_evsub_state = &xfer_on_evsub_state; |
| 827 | |
| 828 | /* Create transferee event subscription */ |
| 829 | status = pjsip_xfer_create_uas( inv->dlg, &xfer_cb, rdata, &sub); |
| 830 | if (status != PJ_SUCCESS) { |
| 831 | pjsua_perror(THIS_FILE, "Unable to create xfer uas", status); |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 832 | pjsip_dlg_respond( inv->dlg, rdata, 500, NULL, NULL, NULL); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 833 | return; |
| 834 | } |
| 835 | |
| 836 | /* Accept the REFER request, send 200 (OK). */ |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 837 | pjsip_xfer_accept(sub, rdata, code, NULL); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 838 | |
| 839 | /* Create initial NOTIFY request */ |
| 840 | status = pjsip_xfer_notify( sub, PJSIP_EVSUB_STATE_ACTIVE, |
| 841 | 100, NULL, &tdata); |
| 842 | if (status != PJ_SUCCESS) { |
| 843 | pjsua_perror(THIS_FILE, "Unable to create NOTIFY to REFER", status); |
| 844 | return; |
| 845 | } |
| 846 | |
| 847 | /* Send initial NOTIFY request */ |
| 848 | status = pjsip_xfer_send_request( sub, tdata); |
| 849 | if (status != PJ_SUCCESS) { |
| 850 | pjsua_perror(THIS_FILE, "Unable to send NOTIFY to REFER", status); |
| 851 | return; |
| 852 | } |
| 853 | |
| 854 | /* We're cheating here. |
| 855 | * We need to get a null terminated string from a pj_str_t. |
| 856 | * So grab the pointer from the hvalue and NULL terminate it, knowing |
| 857 | * that the NULL position will be occupied by a newline. |
| 858 | */ |
| 859 | uri = refer_to->hvalue.ptr; |
| 860 | uri[refer_to->hvalue.slen] = '\0'; |
| 861 | |
| 862 | /* Now make the outgoing call. */ |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 863 | tmp = pj_str(uri); |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 864 | status = pjsua_call_make_call(existing_call->acc_index, &tmp, &new_call); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 865 | if (status != PJ_SUCCESS) { |
| 866 | |
| 867 | /* Notify xferer about the error */ |
| 868 | status = pjsip_xfer_notify(sub, PJSIP_EVSUB_STATE_TERMINATED, |
| 869 | 500, NULL, &tdata); |
| 870 | if (status != PJ_SUCCESS) { |
| 871 | pjsua_perror(THIS_FILE, "Unable to create NOTIFY to REFER", |
| 872 | status); |
| 873 | return; |
| 874 | } |
| 875 | status = pjsip_xfer_send_request(sub, tdata); |
| 876 | if (status != PJ_SUCCESS) { |
| 877 | pjsua_perror(THIS_FILE, "Unable to send NOTIFY to REFER", |
| 878 | status); |
| 879 | return; |
| 880 | } |
| 881 | return; |
| 882 | } |
| 883 | |
| 884 | /* Put the server subscription in inv_data. |
| 885 | * Subsequent state changed in pjsua_inv_on_state_changed() will be |
| 886 | * reported back to the server subscription. |
| 887 | */ |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 888 | pjsua.calls[new_call].xfer_sub = sub; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 889 | |
| 890 | /* Put the invite_data in the subscription. */ |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 891 | pjsip_evsub_set_mod_data(sub, pjsua.mod.id, &pjsua.calls[new_call]); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 892 | } |
| 893 | |
| 894 | |
| 895 | /* |
| 896 | * This callback is called when transaction state has changed in INVITE |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 897 | * session. We use this to trap: |
| 898 | * - incoming REFER request. |
| 899 | * - incoming MESSAGE request. |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 900 | */ |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 901 | static void pjsua_call_on_tsx_state_changed(pjsip_inv_session *inv, |
| 902 | pjsip_transaction *tsx, |
| 903 | pjsip_event *e) |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 904 | { |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 905 | pjsua_call *call = inv->dlg->mod_data[pjsua.mod.id]; |
| 906 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 907 | if (tsx->role==PJSIP_ROLE_UAS && |
| 908 | tsx->state==PJSIP_TSX_STATE_TRYING && |
| 909 | pjsip_method_cmp(&tsx->method, &pjsip_refer_method)==0) |
| 910 | { |
| 911 | /* |
| 912 | * Incoming REFER request. |
| 913 | */ |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 914 | on_call_transfered(call->inv, e->body.tsx_state.src.rdata); |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 915 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 916 | } |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 917 | else if (tsx->role==PJSIP_ROLE_UAS && |
| 918 | tsx->state==PJSIP_TSX_STATE_TRYING && |
| 919 | pjsip_method_cmp(&tsx->method, &pjsip_message_method)==0) |
| 920 | { |
| 921 | /* |
| 922 | * Incoming MESSAGE request! |
| 923 | */ |
| 924 | pjsip_rx_data *rdata; |
| 925 | pjsip_msg *msg; |
| 926 | pjsip_accept_hdr *accept_hdr; |
| 927 | pj_status_t status; |
| 928 | |
| 929 | rdata = e->body.tsx_state.src.rdata; |
| 930 | msg = rdata->msg_info.msg; |
| 931 | |
| 932 | /* Request MUST have message body, with Content-Type equal to |
| 933 | * "text/plain". |
| 934 | */ |
| 935 | if (pjsua_im_accept_pager(rdata, &accept_hdr) == PJ_FALSE) { |
| 936 | |
| 937 | pjsip_hdr hdr_list; |
| 938 | |
| 939 | pj_list_init(&hdr_list); |
| 940 | pj_list_push_back(&hdr_list, accept_hdr); |
| 941 | |
| 942 | pjsip_dlg_respond( inv->dlg, rdata, PJSIP_SC_NOT_ACCEPTABLE_HERE, |
| 943 | NULL, &hdr_list, NULL ); |
| 944 | return; |
| 945 | } |
| 946 | |
| 947 | /* Respond with 200 first, so that remote doesn't retransmit in case |
| 948 | * the UI takes too long to process the message. |
| 949 | */ |
| 950 | status = pjsip_dlg_respond( inv->dlg, rdata, 200, NULL, NULL, NULL); |
| 951 | |
| 952 | /* Process MESSAGE request */ |
| 953 | pjsua_im_process_pager(call->index, &inv->dlg->remote.info_str, |
| 954 | &inv->dlg->local.info_str, rdata); |
| 955 | } |
| 956 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 957 | } |
| 958 | |
| 959 | |
| 960 | /* |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 961 | * This callback is called by invite session framework when UAC session |
| 962 | * has forked. |
| 963 | */ |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 964 | static void pjsua_call_on_forked( pjsip_inv_session *inv, |
| 965 | pjsip_event *e) |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 966 | { |
| 967 | PJ_UNUSED_ARG(inv); |
| 968 | PJ_UNUSED_ARG(e); |
| 969 | |
| 970 | PJ_TODO(HANDLE_FORKED_DIALOG); |
| 971 | } |
| 972 | |
| 973 | |
| 974 | /* |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 975 | * Create inactive SDP for call hold. |
| 976 | */ |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 977 | static pj_status_t create_inactive_sdp(pjsua_call *call, |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 978 | pjmedia_sdp_session **p_answer) |
| 979 | { |
| 980 | pj_status_t status; |
| 981 | pjmedia_sdp_conn *conn; |
| 982 | pjmedia_sdp_attr *attr; |
| 983 | pjmedia_sdp_session *sdp; |
| 984 | |
| 985 | /* Create new offer */ |
| 986 | status = pjmedia_endpt_create_sdp(pjsua.med_endpt, pjsua.pool, 1, |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 987 | &call->skinfo, &sdp); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 988 | if (status != PJ_SUCCESS) { |
| 989 | pjsua_perror(THIS_FILE, "Unable to create local SDP", status); |
| 990 | return status; |
| 991 | } |
| 992 | |
| 993 | /* Get SDP media connection line */ |
| 994 | conn = sdp->media[0]->conn; |
| 995 | if (!conn) |
| 996 | conn = sdp->conn; |
| 997 | |
| 998 | /* Modify address */ |
| 999 | conn->addr = pj_str("0.0.0.0"); |
| 1000 | |
| 1001 | /* Remove existing directions attributes */ |
| 1002 | pjmedia_sdp_media_remove_all_attr(sdp->media[0], "sendrecv"); |
| 1003 | pjmedia_sdp_media_remove_all_attr(sdp->media[0], "sendonly"); |
| 1004 | pjmedia_sdp_media_remove_all_attr(sdp->media[0], "recvonly"); |
| 1005 | pjmedia_sdp_media_remove_all_attr(sdp->media[0], "inactive"); |
| 1006 | |
| 1007 | /* Add inactive attribute */ |
| 1008 | attr = pjmedia_sdp_attr_create(pjsua.pool, "inactive", NULL); |
| 1009 | pjmedia_sdp_media_add_attr(sdp->media[0], attr); |
| 1010 | |
| 1011 | *p_answer = sdp; |
| 1012 | |
| 1013 | return status; |
| 1014 | } |
| 1015 | |
| 1016 | /* |
| 1017 | * Called when session received new offer. |
| 1018 | */ |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1019 | static void pjsua_call_on_rx_offer(pjsip_inv_session *inv, |
| 1020 | const pjmedia_sdp_session *offer) |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1021 | { |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1022 | pjsua_call *call; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1023 | pjmedia_sdp_conn *conn; |
| 1024 | pjmedia_sdp_session *answer; |
| 1025 | pj_bool_t is_remote_active; |
| 1026 | pj_status_t status; |
| 1027 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1028 | call = inv->dlg->mod_data[pjsua.mod.id]; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1029 | |
| 1030 | /* |
| 1031 | * See if remote is offering active media (i.e. not on-hold) |
| 1032 | */ |
| 1033 | is_remote_active = PJ_TRUE; |
| 1034 | |
| 1035 | conn = offer->media[0]->conn; |
| 1036 | if (!conn) |
| 1037 | conn = offer->conn; |
| 1038 | |
| 1039 | if (pj_strcmp2(&conn->addr, "0.0.0.0")==0 || |
| 1040 | pj_strcmp2(&conn->addr, "0")==0) |
| 1041 | { |
| 1042 | is_remote_active = PJ_FALSE; |
| 1043 | |
| 1044 | } |
| 1045 | else if (pjmedia_sdp_media_find_attr2(offer->media[0], "inactive", NULL)) |
| 1046 | { |
| 1047 | is_remote_active = PJ_FALSE; |
| 1048 | } |
| 1049 | |
| 1050 | PJ_LOG(4,(THIS_FILE, "Received SDP offer, remote media is %s", |
| 1051 | (is_remote_active ? "active" : "inactive"))); |
| 1052 | |
| 1053 | /* Supply candidate answer */ |
| 1054 | if (is_remote_active) { |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1055 | status = pjmedia_endpt_create_sdp( pjsua.med_endpt, call->inv->pool, 1, |
| 1056 | &call->skinfo, &answer); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1057 | } else { |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1058 | status = create_inactive_sdp( call, &answer ); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
| 1061 | if (status != PJ_SUCCESS) { |
| 1062 | pjsua_perror(THIS_FILE, "Unable to create local SDP", status); |
| 1063 | return; |
| 1064 | } |
| 1065 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1066 | status = pjsip_inv_set_sdp_answer(call->inv, answer); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1067 | if (status != PJ_SUCCESS) { |
| 1068 | pjsua_perror(THIS_FILE, "Unable to set answer", status); |
| 1069 | return; |
| 1070 | } |
| 1071 | |
| 1072 | } |
| 1073 | |
Benny Prijono | 8befd9f | 2006-05-13 22:46:23 +0000 | [diff] [blame] | 1074 | #if 0 |
Benny Prijono | 1c2bf46 | 2006-03-05 11:54:02 +0000 | [diff] [blame] | 1075 | /* Disconnect call */ |
| 1076 | static void call_disconnect(pjsip_inv_session *inv, |
| 1077 | int st_code) |
| 1078 | { |
| 1079 | pjsip_tx_data *tdata; |
| 1080 | pj_status_t status; |
| 1081 | |
| 1082 | status = pjsip_inv_end_session(inv, st_code, NULL, &tdata); |
| 1083 | if (status == PJ_SUCCESS) |
Benny Prijono | e8b0d3b | 2006-03-17 17:57:52 +0000 | [diff] [blame] | 1084 | status = pjsip_inv_send_msg(inv, tdata); |
Benny Prijono | 1c2bf46 | 2006-03-05 11:54:02 +0000 | [diff] [blame] | 1085 | |
| 1086 | if (status != PJ_SUCCESS) { |
| 1087 | pjsua_perror(THIS_FILE, "Unable to disconnect call", status); |
| 1088 | } |
| 1089 | } |
Benny Prijono | 8befd9f | 2006-05-13 22:46:23 +0000 | [diff] [blame] | 1090 | #endif |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1091 | |
| 1092 | /* |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 1093 | * Callback to be called when SDP offer/answer negotiation has just completed |
| 1094 | * in the session. This function will start/update media if negotiation |
| 1095 | * has succeeded. |
| 1096 | */ |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1097 | static void pjsua_call_on_media_update(pjsip_inv_session *inv, |
| 1098 | pj_status_t status) |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 1099 | { |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1100 | pjsua_call *call; |
Benny Prijono | 8befd9f | 2006-05-13 22:46:23 +0000 | [diff] [blame] | 1101 | pjmedia_session_info sess_info; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 1102 | const pjmedia_sdp_session *local_sdp; |
| 1103 | const pjmedia_sdp_session *remote_sdp; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1104 | pjmedia_port *media_port; |
| 1105 | pj_str_t port_name; |
| 1106 | char tmp[PJSIP_MAX_URL_SIZE]; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 1107 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1108 | call = inv->dlg->mod_data[pjsua.mod.id]; |
| 1109 | |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 1110 | if (status != PJ_SUCCESS) { |
| 1111 | |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 1112 | pjsua_perror(THIS_FILE, "SDP negotiation has failed", status); |
Benny Prijono | 1c2bf46 | 2006-03-05 11:54:02 +0000 | [diff] [blame] | 1113 | |
Benny Prijono | ccb03fa | 2006-03-06 13:35:47 +0000 | [diff] [blame] | 1114 | /* Disconnect call if we're not in the middle of initializing an |
| 1115 | * UAS dialog and if this is not a re-INVITE |
| 1116 | */ |
| 1117 | if (inv->state != PJSIP_INV_STATE_NULL && |
| 1118 | inv->state != PJSIP_INV_STATE_CONFIRMED) |
| 1119 | { |
Benny Prijono | e8b0d3b | 2006-03-17 17:57:52 +0000 | [diff] [blame] | 1120 | //call_disconnect(inv, PJSIP_SC_UNSUPPORTED_MEDIA_TYPE); |
Benny Prijono | 1c2bf46 | 2006-03-05 11:54:02 +0000 | [diff] [blame] | 1121 | } |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 1122 | return; |
| 1123 | |
| 1124 | } |
| 1125 | |
| 1126 | /* Destroy existing media session, if any. */ |
| 1127 | |
Benny Prijono | 275fd68 | 2006-03-22 11:59:11 +0000 | [diff] [blame] | 1128 | if (call) |
| 1129 | call_destroy_media(call->index); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 1130 | |
| 1131 | /* Get local and remote SDP */ |
| 1132 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1133 | status = pjmedia_sdp_neg_get_active_local(call->inv->neg, &local_sdp); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 1134 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 1135 | pjsua_perror(THIS_FILE, |
| 1136 | "Unable to retrieve currently active local SDP", |
| 1137 | status); |
Benny Prijono | e8b0d3b | 2006-03-17 17:57:52 +0000 | [diff] [blame] | 1138 | //call_disconnect(inv, PJSIP_SC_UNSUPPORTED_MEDIA_TYPE); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 1139 | return; |
| 1140 | } |
| 1141 | |
| 1142 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1143 | status = pjmedia_sdp_neg_get_active_remote(call->inv->neg, &remote_sdp); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 1144 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 1145 | pjsua_perror(THIS_FILE, |
| 1146 | "Unable to retrieve currently active remote SDP", |
| 1147 | status); |
Benny Prijono | e8b0d3b | 2006-03-17 17:57:52 +0000 | [diff] [blame] | 1148 | //call_disconnect(inv, PJSIP_SC_UNSUPPORTED_MEDIA_TYPE); |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 1149 | return; |
| 1150 | } |
| 1151 | |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 1152 | if (pjsua.config.null_audio) |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1153 | return; |
Benny Prijono | 8befd9f | 2006-05-13 22:46:23 +0000 | [diff] [blame] | 1154 | |
| 1155 | /* Create media session info based on SDP parameters. |
| 1156 | * We only support one stream per session at the moment |
| 1157 | */ |
| 1158 | status = pjmedia_session_info_from_sdp( call->inv->dlg->pool, |
Benny Prijono | b04c9e0 | 2006-05-17 17:17:39 +0000 | [diff] [blame] | 1159 | pjsua.med_endpt, |
| 1160 | 1,&sess_info, |
Benny Prijono | 8befd9f | 2006-05-13 22:46:23 +0000 | [diff] [blame] | 1161 | local_sdp, remote_sdp); |
| 1162 | if (status != PJ_SUCCESS) { |
| 1163 | pjsua_perror(THIS_FILE, "Unable to create media session", |
| 1164 | status); |
| 1165 | //call_disconnect(inv, PJSIP_SC_UNSUPPORTED_MEDIA_TYPE); |
| 1166 | return; |
| 1167 | } |
| 1168 | |
| 1169 | /* Override ptime, if this option is specified. */ |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 1170 | if (pjsua.config.ptime) { |
Benny Prijono | 8befd9f | 2006-05-13 22:46:23 +0000 | [diff] [blame] | 1171 | sess_info.stream_info[0].param->setting.frm_per_pkt = (pj_uint8_t) |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 1172 | (pjsua.config.ptime / |
| 1173 | sess_info.stream_info[0].param->info.frm_ptime); |
Benny Prijono | 8befd9f | 2006-05-13 22:46:23 +0000 | [diff] [blame] | 1174 | if (sess_info.stream_info[0].param->setting.frm_per_pkt==0) |
| 1175 | sess_info.stream_info[0].param->setting.frm_per_pkt = 1; |
| 1176 | } |
| 1177 | |
| 1178 | /* Optionally, application may modify other stream settings here |
| 1179 | * (such as jitter buffer parameters, codec ptime, etc.) |
| 1180 | */ |
| 1181 | |
| 1182 | /* Create session based on session info. */ |
| 1183 | status = pjmedia_session_create( pjsua.med_endpt, &sess_info, |
Benny Prijono | b04c9e0 | 2006-05-17 17:17:39 +0000 | [diff] [blame] | 1184 | &call->med_tp, |
Benny Prijono | 8befd9f | 2006-05-13 22:46:23 +0000 | [diff] [blame] | 1185 | call, &call->session ); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1186 | if (status != PJ_SUCCESS) { |
| 1187 | pjsua_perror(THIS_FILE, "Unable to create media session", |
| 1188 | status); |
Benny Prijono | e8b0d3b | 2006-03-17 17:57:52 +0000 | [diff] [blame] | 1189 | //call_disconnect(inv, PJSIP_SC_UNSUPPORTED_MEDIA_TYPE); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1190 | return; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 1191 | } |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1192 | |
| 1193 | |
| 1194 | /* Get the port interface of the first stream in the session. |
| 1195 | * We need the port interface to add to the conference bridge. |
| 1196 | */ |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1197 | pjmedia_session_get_port(call->session, 0, &media_port); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1198 | |
| 1199 | |
| 1200 | /* |
| 1201 | * Add the call to conference bridge. |
| 1202 | */ |
| 1203 | port_name.ptr = tmp; |
| 1204 | port_name.slen = pjsip_uri_print(PJSIP_URI_IN_REQ_URI, |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1205 | call->inv->dlg->remote.info->uri, |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1206 | tmp, sizeof(tmp)); |
| 1207 | if (port_name.slen < 1) { |
| 1208 | port_name = pj_str("call"); |
| 1209 | } |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1210 | status = pjmedia_conf_add_port( pjsua.mconf, call->inv->pool, |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1211 | media_port, |
| 1212 | &port_name, |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1213 | &call->conf_slot); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1214 | if (status != PJ_SUCCESS) { |
| 1215 | pjsua_perror(THIS_FILE, "Unable to create conference slot", |
| 1216 | status); |
Benny Prijono | 275fd68 | 2006-03-22 11:59:11 +0000 | [diff] [blame] | 1217 | call_destroy_media(call->index); |
Benny Prijono | e8b0d3b | 2006-03-17 17:57:52 +0000 | [diff] [blame] | 1218 | //call_disconnect(inv, PJSIP_SC_INTERNAL_SERVER_ERROR); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1219 | return; |
| 1220 | } |
| 1221 | |
Benny Prijono | 64f851e | 2006-02-23 13:49:28 +0000 | [diff] [blame] | 1222 | /* If auto-play is configured, connect the call to the file player |
| 1223 | * port |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1224 | */ |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 1225 | if (pjsua.config.auto_play && pjsua.config.wav_file.slen && |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1226 | call->inv->role == PJSIP_ROLE_UAS) |
| 1227 | { |
Benny Prijono | 64f851e | 2006-02-23 13:49:28 +0000 | [diff] [blame] | 1228 | |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 1229 | pjmedia_conf_connect_port( pjsua.mconf, pjsua.player[0].slot, |
Benny Prijono | b100d69 | 2006-03-17 00:16:01 +0000 | [diff] [blame] | 1230 | call->conf_slot, 0); |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1231 | |
Benny Prijono | b8c2518 | 2006-04-29 08:31:09 +0000 | [diff] [blame] | 1232 | } |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 1233 | if (pjsua.config.auto_loop && call->inv->role == PJSIP_ROLE_UAS) { |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1234 | |
| 1235 | pjmedia_conf_connect_port( pjsua.mconf, call->conf_slot, |
Benny Prijono | b100d69 | 2006-03-17 00:16:01 +0000 | [diff] [blame] | 1236 | call->conf_slot, 0); |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1237 | |
Benny Prijono | b8c2518 | 2006-04-29 08:31:09 +0000 | [diff] [blame] | 1238 | } |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 1239 | if (pjsua.config.auto_conf) { |
| 1240 | unsigned i; |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1241 | |
Benny Prijono | b100d69 | 2006-03-17 00:16:01 +0000 | [diff] [blame] | 1242 | pjmedia_conf_connect_port( pjsua.mconf, 0, call->conf_slot, 0); |
| 1243 | pjmedia_conf_connect_port( pjsua.mconf, call->conf_slot, 0, 0); |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1244 | |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 1245 | for (i=0; i < pjsua.config.max_calls; ++i) { |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1246 | |
| 1247 | if (!pjsua.calls[i].session) |
| 1248 | continue; |
| 1249 | |
| 1250 | pjmedia_conf_connect_port( pjsua.mconf, call->conf_slot, |
Benny Prijono | b100d69 | 2006-03-17 00:16:01 +0000 | [diff] [blame] | 1251 | pjsua.calls[i].conf_slot, 0); |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1252 | pjmedia_conf_connect_port( pjsua.mconf, pjsua.calls[i].conf_slot, |
Benny Prijono | b100d69 | 2006-03-17 00:16:01 +0000 | [diff] [blame] | 1253 | call->conf_slot, 0); |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1254 | } |
Benny Prijono | 64f851e | 2006-02-23 13:49:28 +0000 | [diff] [blame] | 1255 | |
Benny Prijono | b8c2518 | 2006-04-29 08:31:09 +0000 | [diff] [blame] | 1256 | } |
| 1257 | |
| 1258 | /* Normal operation: if no auto_xx is given, connect new call to |
| 1259 | * the sound device port (port zero) in the main conference bridge. |
| 1260 | */ |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 1261 | if (pjsua.config.auto_play == 0 && pjsua.config.auto_loop == 0 && |
| 1262 | pjsua.config.auto_conf == 0) |
Benny Prijono | b8c2518 | 2006-04-29 08:31:09 +0000 | [diff] [blame] | 1263 | { |
Benny Prijono | b100d69 | 2006-03-17 00:16:01 +0000 | [diff] [blame] | 1264 | pjmedia_conf_connect_port( pjsua.mconf, 0, call->conf_slot, 0); |
| 1265 | pjmedia_conf_connect_port( pjsua.mconf, call->conf_slot, 0, 0); |
Benny Prijono | 64f851e | 2006-02-23 13:49:28 +0000 | [diff] [blame] | 1266 | } |
| 1267 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1268 | |
| 1269 | /* Done. */ |
| 1270 | { |
| 1271 | struct pjmedia_session_info sess_info; |
| 1272 | char info[80]; |
| 1273 | int info_len = 0; |
| 1274 | unsigned i; |
| 1275 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1276 | pjmedia_session_get_info(call->session, &sess_info); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1277 | for (i=0; i<sess_info.stream_cnt; ++i) { |
| 1278 | int len; |
| 1279 | const char *dir; |
| 1280 | pjmedia_stream_info *strm_info = &sess_info.stream_info[i]; |
| 1281 | |
| 1282 | switch (strm_info->dir) { |
| 1283 | case PJMEDIA_DIR_NONE: |
| 1284 | dir = "inactive"; |
| 1285 | break; |
| 1286 | case PJMEDIA_DIR_ENCODING: |
| 1287 | dir = "sendonly"; |
| 1288 | break; |
| 1289 | case PJMEDIA_DIR_DECODING: |
| 1290 | dir = "recvonly"; |
| 1291 | break; |
| 1292 | case PJMEDIA_DIR_ENCODING_DECODING: |
| 1293 | dir = "sendrecv"; |
| 1294 | break; |
| 1295 | default: |
| 1296 | dir = "unknown"; |
| 1297 | break; |
| 1298 | } |
| 1299 | len = pj_ansi_sprintf( info+info_len, |
| 1300 | ", stream #%d: %.*s (%s)", i, |
| 1301 | (int)strm_info->fmt.encoding_name.slen, |
Benny Prijono | ab7399b | 2006-02-27 00:40:31 +0000 | [diff] [blame] | 1302 | strm_info->fmt.encoding_name.ptr, |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1303 | dir); |
| 1304 | if (len > 0) |
| 1305 | info_len += len; |
| 1306 | } |
| 1307 | PJ_LOG(3,(THIS_FILE,"Media started%s", info)); |
| 1308 | } |
| 1309 | } |
| 1310 | |
| 1311 | |
| 1312 | /* |
| 1313 | * Hangup call. |
| 1314 | */ |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 1315 | PJ_DEF(void) pjsua_call_hangup(int call_index) |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1316 | { |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1317 | pjsua_call *call; |
Benny Prijono | 1c2bf46 | 2006-03-05 11:54:02 +0000 | [diff] [blame] | 1318 | int code; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1319 | pj_status_t status; |
| 1320 | pjsip_tx_data *tdata; |
| 1321 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1322 | |
| 1323 | call = &pjsua.calls[call_index]; |
| 1324 | |
| 1325 | if (!call->inv) { |
| 1326 | PJ_LOG(3,(THIS_FILE,"Call has been disconnected")); |
| 1327 | return; |
| 1328 | } |
| 1329 | |
Benny Prijono | 1c2bf46 | 2006-03-05 11:54:02 +0000 | [diff] [blame] | 1330 | if (call->inv->state == PJSIP_INV_STATE_CONFIRMED) |
| 1331 | code = PJSIP_SC_OK; |
| 1332 | else if (call->inv->role == PJSIP_ROLE_UAS) |
| 1333 | code = PJSIP_SC_DECLINE; |
| 1334 | else |
| 1335 | code = PJSIP_SC_REQUEST_TERMINATED; |
| 1336 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1337 | status = pjsip_inv_end_session(call->inv, code, NULL, &tdata); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1338 | if (status != PJ_SUCCESS) { |
| 1339 | pjsua_perror(THIS_FILE, |
| 1340 | "Failed to create end session message", |
| 1341 | status); |
| 1342 | return; |
| 1343 | } |
| 1344 | |
Benny Prijono | fccab71 | 2006-02-22 22:23:22 +0000 | [diff] [blame] | 1345 | /* pjsip_inv_end_session may return PJ_SUCCESS with NULL |
| 1346 | * as p_tdata when INVITE transaction has not been answered |
| 1347 | * with any provisional responses. |
| 1348 | */ |
| 1349 | if (tdata == NULL) |
| 1350 | return; |
| 1351 | |
Benny Prijono | e8b0d3b | 2006-03-17 17:57:52 +0000 | [diff] [blame] | 1352 | status = pjsip_inv_send_msg(call->inv, tdata); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1353 | if (status != PJ_SUCCESS) { |
| 1354 | pjsua_perror(THIS_FILE, |
| 1355 | "Failed to send end session message", |
| 1356 | status); |
| 1357 | return; |
| 1358 | } |
| 1359 | } |
| 1360 | |
| 1361 | |
| 1362 | /* |
| 1363 | * Put call on-Hold. |
| 1364 | */ |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 1365 | PJ_DEF(pj_status_t) pjsua_call_set_hold(int call_index) |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1366 | { |
| 1367 | pjmedia_sdp_session *sdp; |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1368 | pjsua_call *call; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1369 | pjsip_tx_data *tdata; |
| 1370 | pj_status_t status; |
| 1371 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1372 | call = &pjsua.calls[call_index]; |
| 1373 | |
| 1374 | if (!call->inv) { |
| 1375 | PJ_LOG(3,(THIS_FILE,"Call has been disconnected")); |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 1376 | return PJSIP_ESESSIONTERMINATED; |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1377 | } |
| 1378 | |
| 1379 | if (call->inv->state != PJSIP_INV_STATE_CONFIRMED) { |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1380 | PJ_LOG(3,(THIS_FILE, "Can not hold call that is not confirmed")); |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 1381 | return PJSIP_ESESSIONSTATE; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1382 | } |
| 1383 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1384 | status = create_inactive_sdp(call, &sdp); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1385 | if (status != PJ_SUCCESS) |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 1386 | return status; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1387 | |
| 1388 | /* Send re-INVITE with new offer */ |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1389 | status = pjsip_inv_reinvite( call->inv, NULL, sdp, &tdata); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1390 | if (status != PJ_SUCCESS) { |
| 1391 | pjsua_perror(THIS_FILE, "Unable to create re-INVITE", status); |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 1392 | return status; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1393 | } |
| 1394 | |
Benny Prijono | e8b0d3b | 2006-03-17 17:57:52 +0000 | [diff] [blame] | 1395 | status = pjsip_inv_send_msg( call->inv, tdata); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1396 | if (status != PJ_SUCCESS) { |
| 1397 | pjsua_perror(THIS_FILE, "Unable to send re-INVITE", status); |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 1398 | return status; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1399 | } |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 1400 | |
| 1401 | return PJ_SUCCESS; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1402 | } |
| 1403 | |
| 1404 | |
| 1405 | /* |
| 1406 | * re-INVITE. |
| 1407 | */ |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 1408 | PJ_DEF(pj_status_t) pjsua_call_reinvite(int call_index) |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1409 | { |
| 1410 | pjmedia_sdp_session *sdp; |
| 1411 | pjsip_tx_data *tdata; |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1412 | pjsua_call *call; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1413 | pj_status_t status; |
| 1414 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1415 | call = &pjsua.calls[call_index]; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1416 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1417 | if (!call->inv) { |
| 1418 | PJ_LOG(3,(THIS_FILE,"Call has been disconnected")); |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 1419 | return PJSIP_ESESSIONTERMINATED; |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1420 | } |
| 1421 | |
| 1422 | |
| 1423 | if (call->inv->state != PJSIP_INV_STATE_CONFIRMED) { |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1424 | PJ_LOG(3,(THIS_FILE, "Can not re-INVITE call that is not confirmed")); |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 1425 | return PJSIP_ESESSIONSTATE; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1426 | } |
| 1427 | |
| 1428 | /* Create SDP */ |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1429 | status = pjmedia_endpt_create_sdp( pjsua.med_endpt, call->inv->pool, 1, |
| 1430 | &call->skinfo, &sdp); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1431 | if (status != PJ_SUCCESS) { |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1432 | pjsua_perror(THIS_FILE, "Unable to get SDP from media endpoint", |
| 1433 | status); |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 1434 | return status; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1435 | } |
| 1436 | |
| 1437 | /* Send re-INVITE with new offer */ |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1438 | status = pjsip_inv_reinvite( call->inv, NULL, sdp, &tdata); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1439 | if (status != PJ_SUCCESS) { |
| 1440 | pjsua_perror(THIS_FILE, "Unable to create re-INVITE", status); |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 1441 | return status; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1442 | } |
| 1443 | |
Benny Prijono | e8b0d3b | 2006-03-17 17:57:52 +0000 | [diff] [blame] | 1444 | status = pjsip_inv_send_msg( call->inv, tdata); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1445 | if (status != PJ_SUCCESS) { |
| 1446 | pjsua_perror(THIS_FILE, "Unable to send re-INVITE", status); |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 1447 | return status; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1448 | } |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 1449 | |
| 1450 | return PJ_SUCCESS; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1451 | } |
| 1452 | |
| 1453 | |
| 1454 | /* |
| 1455 | * Transfer call. |
| 1456 | */ |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 1457 | PJ_DEF(pj_status_t) pjsua_call_xfer(unsigned call_index, const pj_str_t *dest) |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1458 | { |
| 1459 | pjsip_evsub *sub; |
| 1460 | pjsip_tx_data *tdata; |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1461 | pjsua_call *call; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1462 | pj_status_t status; |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1463 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1464 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1465 | call = &pjsua.calls[call_index]; |
| 1466 | |
| 1467 | if (!call->inv) { |
| 1468 | PJ_LOG(3,(THIS_FILE,"Call has been disconnected")); |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 1469 | return PJSIP_ESESSIONTERMINATED; |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1470 | } |
| 1471 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1472 | /* Create xfer client subscription. |
| 1473 | * We're not interested in knowing the transfer result, so we |
| 1474 | * put NULL as the callback. |
| 1475 | */ |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1476 | status = pjsip_xfer_create_uac(call->inv->dlg, NULL, &sub); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1477 | if (status != PJ_SUCCESS) { |
| 1478 | pjsua_perror(THIS_FILE, "Unable to create xfer", status); |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 1479 | return status; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1480 | } |
| 1481 | |
| 1482 | /* |
| 1483 | * Create REFER request. |
| 1484 | */ |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 1485 | status = pjsip_xfer_initiate(sub, dest, &tdata); |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1486 | if (status != PJ_SUCCESS) { |
| 1487 | pjsua_perror(THIS_FILE, "Unable to create REFER request", status); |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 1488 | return status; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1489 | } |
| 1490 | |
| 1491 | /* Send. */ |
| 1492 | status = pjsip_xfer_send_request(sub, tdata); |
| 1493 | if (status != PJ_SUCCESS) { |
| 1494 | pjsua_perror(THIS_FILE, "Unable to send REFER request", status); |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 1495 | return status; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1496 | } |
| 1497 | |
| 1498 | /* For simplicity (that's what this program is intended to be!), |
| 1499 | * leave the original invite session as it is. More advanced application |
| 1500 | * may want to hold the INVITE, or terminate the invite, or whatever. |
| 1501 | */ |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 1502 | |
| 1503 | return PJ_SUCCESS; |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 1504 | } |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1505 | |
| 1506 | |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 1507 | /** |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 1508 | * Dial DTMF. |
| 1509 | */ |
| 1510 | PJ_DEF(pj_status_t) pjsua_call_dial_dtmf( unsigned call_index, |
| 1511 | const pj_str_t *digits) |
| 1512 | { |
| 1513 | pjsua_call *call = &pjsua.calls[call_index]; |
| 1514 | |
| 1515 | PJ_ASSERT_RETURN(call_index < pjsua.config.max_calls, PJ_EINVAL); |
| 1516 | |
| 1517 | if (!call->session) { |
| 1518 | PJ_LOG(3,(THIS_FILE, "Media is not established yet!")); |
| 1519 | return -1; |
| 1520 | } |
| 1521 | |
| 1522 | return pjmedia_session_dial_dtmf( call->session, 0, digits); |
| 1523 | } |
| 1524 | |
| 1525 | |
| 1526 | /** |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 1527 | * Send instant messaging inside INVITE session. |
| 1528 | */ |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 1529 | PJ_DEF(pj_status_t) pjsua_call_send_im(int call_index, const pj_str_t *str) |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 1530 | { |
| 1531 | pjsua_call *call; |
| 1532 | const pj_str_t mime_text = pj_str("text"); |
| 1533 | const pj_str_t mime_plain = pj_str("plain"); |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 1534 | pjsip_tx_data *tdata; |
| 1535 | pj_status_t status; |
| 1536 | |
| 1537 | call = &pjsua.calls[call_index]; |
| 1538 | |
| 1539 | if (!call->inv) { |
| 1540 | PJ_LOG(3,(THIS_FILE,"Call has been disconnected")); |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 1541 | return PJSIP_ESESSIONTERMINATED; |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 1542 | } |
| 1543 | |
| 1544 | /* Lock dialog. */ |
| 1545 | pjsip_dlg_inc_lock(call->inv->dlg); |
| 1546 | |
| 1547 | /* Create request message. */ |
| 1548 | status = pjsip_dlg_create_request( call->inv->dlg, &pjsip_message_method, |
| 1549 | -1, &tdata); |
| 1550 | if (status != PJ_SUCCESS) { |
| 1551 | pjsua_perror(THIS_FILE, "Unable to create MESSAGE request", status); |
| 1552 | goto on_return; |
| 1553 | } |
| 1554 | |
| 1555 | /* Add accept header. */ |
| 1556 | pjsip_msg_add_hdr( tdata->msg, |
| 1557 | (pjsip_hdr*)pjsua_im_create_accept(tdata->pool)); |
| 1558 | |
| 1559 | /* Create "text/plain" message body. */ |
| 1560 | tdata->msg->body = pjsip_msg_body_create( tdata->pool, &mime_text, |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 1561 | &mime_plain, str); |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 1562 | if (tdata->msg->body == NULL) { |
| 1563 | pjsua_perror(THIS_FILE, "Unable to create msg body", PJ_ENOMEM); |
| 1564 | pjsip_tx_data_dec_ref(tdata); |
| 1565 | goto on_return; |
| 1566 | } |
| 1567 | |
| 1568 | /* Send the request. */ |
Benny Prijono | 64158af | 2006-04-04 11:06:34 +0000 | [diff] [blame] | 1569 | status = pjsip_dlg_send_request( call->inv->dlg, tdata, -1, NULL); |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 1570 | if (status != PJ_SUCCESS) { |
| 1571 | pjsua_perror(THIS_FILE, "Unable to send MESSAGE request", status); |
| 1572 | goto on_return; |
| 1573 | } |
| 1574 | |
| 1575 | on_return: |
| 1576 | pjsip_dlg_dec_lock(call->inv->dlg); |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 1577 | return status; |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 1578 | } |
| 1579 | |
| 1580 | |
| 1581 | /** |
| 1582 | * Send IM typing indication inside INVITE session. |
| 1583 | */ |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 1584 | PJ_DEF(pj_status_t) pjsua_call_send_typing_ind(int call_index, |
| 1585 | pj_bool_t is_typing) |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 1586 | { |
| 1587 | pjsua_call *call; |
| 1588 | pjsip_tx_data *tdata; |
| 1589 | pj_status_t status; |
| 1590 | |
| 1591 | call = &pjsua.calls[call_index]; |
| 1592 | |
| 1593 | if (!call->inv) { |
| 1594 | PJ_LOG(3,(THIS_FILE,"Call has been disconnected")); |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 1595 | return PJSIP_ESESSIONTERMINATED; |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 1596 | } |
| 1597 | |
| 1598 | /* Lock dialog. */ |
| 1599 | pjsip_dlg_inc_lock(call->inv->dlg); |
| 1600 | |
| 1601 | /* Create request message. */ |
| 1602 | status = pjsip_dlg_create_request( call->inv->dlg, &pjsip_message_method, |
| 1603 | -1, &tdata); |
| 1604 | if (status != PJ_SUCCESS) { |
| 1605 | pjsua_perror(THIS_FILE, "Unable to create MESSAGE request", status); |
| 1606 | goto on_return; |
| 1607 | } |
| 1608 | |
| 1609 | /* Create "application/im-iscomposing+xml" msg body. */ |
| 1610 | tdata->msg->body = pjsip_iscomposing_create_body(tdata->pool, is_typing, |
| 1611 | NULL, NULL, -1); |
| 1612 | |
| 1613 | /* Send the request. */ |
Benny Prijono | 64158af | 2006-04-04 11:06:34 +0000 | [diff] [blame] | 1614 | status = pjsip_dlg_send_request( call->inv->dlg, tdata, -1, NULL); |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 1615 | if (status != PJ_SUCCESS) { |
| 1616 | pjsua_perror(THIS_FILE, "Unable to send MESSAGE request", status); |
| 1617 | goto on_return; |
| 1618 | } |
| 1619 | |
| 1620 | on_return: |
Benny Prijono | 8b1889b | 2006-06-06 18:40:40 +0000 | [diff] [blame] | 1621 | pjsip_dlg_dec_lock(call->inv->dlg); |
| 1622 | return status; |
| 1623 | } |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 1624 | |
| 1625 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1626 | /* |
| 1627 | * Terminate all calls. |
| 1628 | */ |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 1629 | PJ_DEF(void) pjsua_call_hangup_all(void) |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1630 | { |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 1631 | unsigned i; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1632 | |
Benny Prijono | dc39fe8 | 2006-05-26 12:17:46 +0000 | [diff] [blame] | 1633 | for (i=0; i<pjsua.config.max_calls; ++i) { |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1634 | pjsip_tx_data *tdata; |
Benny Prijono | 1c2bf46 | 2006-03-05 11:54:02 +0000 | [diff] [blame] | 1635 | int st_code; |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1636 | pjsua_call *call; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1637 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1638 | if (pjsua.calls[i].inv == NULL) |
| 1639 | continue; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1640 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1641 | call = &pjsua.calls[i]; |
| 1642 | |
Benny Prijono | 1c2bf46 | 2006-03-05 11:54:02 +0000 | [diff] [blame] | 1643 | if (call->inv->state == PJSIP_INV_STATE_CONFIRMED) { |
| 1644 | st_code = 200; |
| 1645 | } else { |
| 1646 | st_code = PJSIP_SC_GONE; |
| 1647 | } |
| 1648 | |
| 1649 | if (pjsip_inv_end_session(call->inv, st_code, NULL, &tdata)==0) { |
Benny Prijono | fccab71 | 2006-02-22 22:23:22 +0000 | [diff] [blame] | 1650 | if (tdata) |
Benny Prijono | e8b0d3b | 2006-03-17 17:57:52 +0000 | [diff] [blame] | 1651 | pjsip_inv_send_msg(call->inv, tdata); |
Benny Prijono | fccab71 | 2006-02-22 22:23:22 +0000 | [diff] [blame] | 1652 | } |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1653 | } |
| 1654 | } |
| 1655 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1656 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1657 | pj_status_t pjsua_call_init(void) |
| 1658 | { |
| 1659 | /* Initialize invite session callback. */ |
| 1660 | pjsip_inv_callback inv_cb; |
| 1661 | pj_status_t status; |
| 1662 | |
| 1663 | pj_memset(&inv_cb, 0, sizeof(inv_cb)); |
| 1664 | inv_cb.on_state_changed = &pjsua_call_on_state_changed; |
| 1665 | inv_cb.on_new_session = &pjsua_call_on_forked; |
| 1666 | inv_cb.on_media_update = &pjsua_call_on_media_update; |
| 1667 | inv_cb.on_rx_offer = &pjsua_call_on_rx_offer; |
| 1668 | inv_cb.on_tsx_state_changed = &pjsua_call_on_tsx_state_changed; |
| 1669 | |
| 1670 | |
| 1671 | /* Initialize invite session module: */ |
Benny Prijono | e8b0d3b | 2006-03-17 17:57:52 +0000 | [diff] [blame] | 1672 | status = pjsip_inv_usage_init(pjsua.endpt, &inv_cb); |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1673 | |
| 1674 | return status; |
| 1675 | } |
Benny Prijono | 9fc735d | 2006-05-28 14:58:12 +0000 | [diff] [blame] | 1676 | |
| 1677 | /** |
| 1678 | * Replace media transport. |
| 1679 | */ |
| 1680 | PJ_DEF(pj_status_t) pjsua_set_call_media_transport( unsigned call_index, |
| 1681 | const pjmedia_sock_info *i, |
| 1682 | pjmedia_transport *tp) |
| 1683 | { |
| 1684 | pjsua_call *call = &pjsua.calls[call_index]; |
| 1685 | |
| 1686 | if (i) |
| 1687 | pj_memcpy(&call->skinfo, i, sizeof(pjmedia_sock_info)); |
| 1688 | |
| 1689 | if (call->med_tp) |
| 1690 | (*call->med_tp->op->destroy)(call->med_tp); |
| 1691 | |
| 1692 | call->med_tp = tp; |
| 1693 | return PJ_SUCCESS; |
| 1694 | } |