Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +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 | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 20 | |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 21 | /* |
| 22 | * pjsua_core.c |
| 23 | * |
| 24 | * Core application functionalities. |
| 25 | */ |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 26 | |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 27 | #define THIS_FILE "pjsua_core.c" |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 28 | |
| 29 | |
Benny Prijono | 9519658 | 2006-02-09 00:13:40 +0000 | [diff] [blame] | 30 | /* |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 31 | * Global variable. |
| 32 | */ |
| 33 | struct pjsua pjsua; |
| 34 | |
| 35 | |
| 36 | /* |
| 37 | * Default local URI, if none is specified in cmd-line |
Benny Prijono | 9519658 | 2006-02-09 00:13:40 +0000 | [diff] [blame] | 38 | */ |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 39 | #define PJSUA_LOCAL_URI "<sip:user@127.0.0.1>" |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 40 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 41 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 42 | |
| 43 | /* |
| 44 | * Init default application parameters. |
| 45 | */ |
| 46 | void pjsua_default(void) |
| 47 | { |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 48 | unsigned i; |
| 49 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 50 | |
| 51 | /* Normally need another thread for console application, because main |
| 52 | * thread will be blocked in fgets(). |
| 53 | */ |
| 54 | pjsua.thread_cnt = 1; |
| 55 | |
| 56 | |
| 57 | /* Default transport settings: */ |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 58 | pjsua.sip_port = 5060; |
| 59 | |
| 60 | |
Benny Prijono | cbf3740 | 2006-03-01 19:29:10 +0000 | [diff] [blame] | 61 | /* Default we start RTP at port 4000 */ |
| 62 | pjsua.start_rtp_port = 4000; |
| 63 | |
| 64 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 65 | /* Default logging settings: */ |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 66 | pjsua.log_level = 5; |
| 67 | pjsua.app_log_level = 4; |
| 68 | pjsua.log_decor = PJ_LOG_HAS_SENDER | PJ_LOG_HAS_TIME | |
| 69 | PJ_LOG_HAS_MICRO_SEC | PJ_LOG_HAS_NEWLINE; |
| 70 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 71 | |
Benny Prijono | 105217f | 2006-03-06 16:25:59 +0000 | [diff] [blame] | 72 | /* Default call settings. */ |
| 73 | pjsua.uas_refresh = -1; |
| 74 | pjsua.uas_duration = -1; |
| 75 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 76 | /* Default: do not use STUN: */ |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 77 | pjsua.stun_port1 = pjsua.stun_port2 = 0; |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 78 | |
Benny Prijono | 1c2bf46 | 2006-03-05 11:54:02 +0000 | [diff] [blame] | 79 | /* Default for media: */ |
Benny Prijono | fa137ca | 2006-03-20 17:42:37 +0000 | [diff] [blame] | 80 | #if defined(PJ_DARWINOS) && PJ_DARWINOS!=0 |
| 81 | pjsua.clock_rate = 44100; |
| 82 | #else |
Benny Prijono | 08e0d06 | 2006-03-04 14:52:44 +0000 | [diff] [blame] | 83 | pjsua.clock_rate = 8000; |
Benny Prijono | fa137ca | 2006-03-20 17:42:37 +0000 | [diff] [blame] | 84 | #endif |
Benny Prijono | ccb03fa | 2006-03-06 13:35:47 +0000 | [diff] [blame] | 85 | pjsua.complexity = -1; |
Benny Prijono | 1c2bf46 | 2006-03-05 11:54:02 +0000 | [diff] [blame] | 86 | pjsua.quality = 4; |
Benny Prijono | fa137ca | 2006-03-20 17:42:37 +0000 | [diff] [blame] | 87 | pjsua.has_wb = 0; |
| 88 | pjsua.has_uwb = 0; |
Benny Prijono | 1c2bf46 | 2006-03-05 11:54:02 +0000 | [diff] [blame] | 89 | |
Benny Prijono | 08e0d06 | 2006-03-04 14:52:44 +0000 | [diff] [blame] | 90 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 91 | /* Init accounts: */ |
| 92 | pjsua.acc_cnt = 1; |
| 93 | for (i=0; i<PJ_ARRAY_SIZE(pjsua.acc); ++i) { |
| 94 | pjsua.acc[i].index = i; |
| 95 | pjsua.acc[i].local_uri = pj_str(PJSUA_LOCAL_URI); |
| 96 | pjsua.acc[i].reg_timeout = 55; |
Benny Prijono | ccb03fa | 2006-03-06 13:35:47 +0000 | [diff] [blame] | 97 | pjsua.acc[i].online_status = PJ_TRUE; |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 98 | pj_list_init(&pjsua.acc[i].route_set); |
| 99 | pj_list_init(&pjsua.acc[i].pres_srv_list); |
| 100 | } |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 101 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 102 | /* Init call array: */ |
Benny Prijono | 105217f | 2006-03-06 16:25:59 +0000 | [diff] [blame] | 103 | for (i=0; i<PJ_ARRAY_SIZE(pjsua.calls); ++i) { |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 104 | pjsua.calls[i].index = i; |
Benny Prijono | 105217f | 2006-03-06 16:25:59 +0000 | [diff] [blame] | 105 | pjsua.calls[i].refresh_tm._timer_id = -1; |
| 106 | pjsua.calls[i].hangup_tm._timer_id = -1; |
Benny Prijono | 275fd68 | 2006-03-22 11:59:11 +0000 | [diff] [blame] | 107 | pjsua.calls[i].conf_slot = 0; |
Benny Prijono | 105217f | 2006-03-06 16:25:59 +0000 | [diff] [blame] | 108 | } |
Benny Prijono | 9519658 | 2006-02-09 00:13:40 +0000 | [diff] [blame] | 109 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 110 | /* Default max nb of calls. */ |
| 111 | pjsua.max_calls = 4; |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 112 | |
| 113 | /* Init server presence subscription list: */ |
| 114 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 115 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 119 | |
| 120 | /* |
| 121 | * Handler for receiving incoming requests. |
| 122 | * |
| 123 | * This handler serves multiple purposes: |
| 124 | * - it receives requests outside dialogs. |
| 125 | * - it receives requests inside dialogs, when the requests are |
| 126 | * unhandled by other dialog usages. Example of these |
| 127 | * requests are: MESSAGE. |
| 128 | */ |
| 129 | static pj_bool_t mod_pjsua_on_rx_request(pjsip_rx_data *rdata) |
| 130 | { |
Benny Prijono | 3899823 | 2006-02-08 22:44:25 +0000 | [diff] [blame] | 131 | |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 132 | if (rdata->msg_info.msg->line.req.method.id == PJSIP_INVITE_METHOD) { |
Benny Prijono | 3899823 | 2006-02-08 22:44:25 +0000 | [diff] [blame] | 133 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 134 | return pjsua_call_on_incoming(rdata); |
Benny Prijono | 3899823 | 2006-02-08 22:44:25 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 137 | return PJ_FALSE; |
| 138 | } |
| 139 | |
| 140 | |
| 141 | /* |
| 142 | * Handler for receiving incoming responses. |
| 143 | * |
| 144 | * This handler serves multiple purposes: |
| 145 | * - it receives strayed responses (i.e. outside any dialog and |
| 146 | * outside any transactions). |
| 147 | * - it receives responses coming to a transaction, when pjsua |
| 148 | * module is set as transaction user for the transaction. |
| 149 | * - it receives responses inside a dialog, when these responses |
| 150 | * are unhandled by other dialog usages. |
| 151 | */ |
| 152 | static pj_bool_t mod_pjsua_on_rx_response(pjsip_rx_data *rdata) |
| 153 | { |
| 154 | PJ_UNUSED_ARG(rdata); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 155 | return PJ_FALSE; |
| 156 | } |
| 157 | |
| 158 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 159 | /* |
| 160 | * Initialize sockets and optionally get the public address via STUN. |
| 161 | */ |
Benny Prijono | f04ffdd | 2006-02-21 00:11:18 +0000 | [diff] [blame] | 162 | static pj_status_t init_sockets(pj_bool_t sip, |
| 163 | pjmedia_sock_info *skinfo) |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 164 | { |
| 165 | enum { |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 166 | RTP_RETRY = 100 |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 167 | }; |
| 168 | enum { |
| 169 | SIP_SOCK, |
| 170 | RTP_SOCK, |
| 171 | RTCP_SOCK, |
| 172 | }; |
| 173 | int i; |
Benny Prijono | cbf3740 | 2006-03-01 19:29:10 +0000 | [diff] [blame] | 174 | static pj_uint16_t rtp_port; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 175 | pj_sock_t sock[3]; |
| 176 | pj_sockaddr_in mapped_addr[3]; |
Benny Prijono | f04ffdd | 2006-02-21 00:11:18 +0000 | [diff] [blame] | 177 | pj_status_t status = PJ_SUCCESS; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 178 | |
Benny Prijono | cbf3740 | 2006-03-01 19:29:10 +0000 | [diff] [blame] | 179 | if (rtp_port == 0) |
| 180 | rtp_port = (pj_uint16_t)pjsua.start_rtp_port; |
| 181 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 182 | for (i=0; i<3; ++i) |
| 183 | sock[i] = PJ_INVALID_SOCKET; |
| 184 | |
Benny Prijono | fccab71 | 2006-02-22 22:23:22 +0000 | [diff] [blame] | 185 | /* Create and bind SIP UDP socket. */ |
| 186 | status = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, &sock[SIP_SOCK]); |
| 187 | if (status != PJ_SUCCESS) { |
| 188 | pjsua_perror(THIS_FILE, "socket() error", status); |
| 189 | goto on_error; |
| 190 | } |
| 191 | |
Benny Prijono | f04ffdd | 2006-02-21 00:11:18 +0000 | [diff] [blame] | 192 | if (sip) { |
Benny Prijono | fccab71 | 2006-02-22 22:23:22 +0000 | [diff] [blame] | 193 | status = pj_sock_bind_in(sock[SIP_SOCK], 0, pjsua.sip_port); |
Benny Prijono | f04ffdd | 2006-02-21 00:11:18 +0000 | [diff] [blame] | 194 | if (status != PJ_SUCCESS) { |
Benny Prijono | fccab71 | 2006-02-22 22:23:22 +0000 | [diff] [blame] | 195 | pjsua_perror(THIS_FILE, "bind() error", status); |
Benny Prijono | f04ffdd | 2006-02-21 00:11:18 +0000 | [diff] [blame] | 196 | goto on_error; |
| 197 | } |
Benny Prijono | fccab71 | 2006-02-22 22:23:22 +0000 | [diff] [blame] | 198 | } else { |
| 199 | status = pj_sock_bind_in(sock[SIP_SOCK], 0, 0); |
Benny Prijono | f04ffdd | 2006-02-21 00:11:18 +0000 | [diff] [blame] | 200 | if (status != PJ_SUCCESS) { |
| 201 | pjsua_perror(THIS_FILE, "bind() error", status); |
| 202 | goto on_error; |
| 203 | } |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 204 | } |
| 205 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 206 | |
| 207 | /* Loop retry to bind RTP and RTCP sockets. */ |
| 208 | for (i=0; i<RTP_RETRY; ++i, rtp_port += 2) { |
| 209 | |
| 210 | /* Create and bind RTP socket. */ |
| 211 | status = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, &sock[RTP_SOCK]); |
| 212 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 213 | pjsua_perror(THIS_FILE, "socket() error", status); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 214 | goto on_error; |
| 215 | } |
| 216 | |
| 217 | status = pj_sock_bind_in(sock[RTP_SOCK], 0, rtp_port); |
| 218 | if (status != PJ_SUCCESS) { |
| 219 | pj_sock_close(sock[RTP_SOCK]); |
| 220 | sock[RTP_SOCK] = PJ_INVALID_SOCKET; |
| 221 | continue; |
| 222 | } |
| 223 | |
| 224 | /* Create and bind RTCP socket. */ |
| 225 | status = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, &sock[RTCP_SOCK]); |
| 226 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 227 | pjsua_perror(THIS_FILE, "socket() error", status); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 228 | goto on_error; |
| 229 | } |
| 230 | |
| 231 | status = pj_sock_bind_in(sock[RTCP_SOCK], 0, (pj_uint16_t)(rtp_port+1)); |
| 232 | if (status != PJ_SUCCESS) { |
| 233 | pj_sock_close(sock[RTP_SOCK]); |
| 234 | sock[RTP_SOCK] = PJ_INVALID_SOCKET; |
| 235 | |
| 236 | pj_sock_close(sock[RTCP_SOCK]); |
| 237 | sock[RTCP_SOCK] = PJ_INVALID_SOCKET; |
| 238 | continue; |
| 239 | } |
| 240 | |
| 241 | /* |
| 242 | * If we're configured to use STUN, then find out the mapped address, |
| 243 | * and make sure that the mapped RTCP port is adjacent with the RTP. |
| 244 | */ |
| 245 | if (pjsua.stun_port1 == 0) { |
| 246 | const pj_str_t *hostname; |
| 247 | pj_sockaddr_in addr; |
| 248 | |
| 249 | /* Get local IP address. */ |
| 250 | hostname = pj_gethostname(); |
| 251 | |
| 252 | pj_memset( &addr, 0, sizeof(addr)); |
| 253 | addr.sin_family = PJ_AF_INET; |
| 254 | status = pj_sockaddr_in_set_str_addr( &addr, hostname); |
| 255 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 256 | pjsua_perror(THIS_FILE, "Unresolvable local hostname", |
| 257 | status); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 258 | goto on_error; |
| 259 | } |
| 260 | |
| 261 | for (i=0; i<3; ++i) |
| 262 | pj_memcpy(&mapped_addr[i], &addr, sizeof(addr)); |
| 263 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 264 | if (sip) { |
| 265 | mapped_addr[SIP_SOCK].sin_port = |
| 266 | pj_htons((pj_uint16_t)pjsua.sip_port); |
| 267 | } |
| 268 | mapped_addr[RTP_SOCK].sin_port=pj_htons((pj_uint16_t)rtp_port); |
| 269 | mapped_addr[RTCP_SOCK].sin_port=pj_htons((pj_uint16_t)(rtp_port+1)); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 270 | break; |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 271 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 272 | } else { |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 273 | status=pj_stun_get_mapped_addr(&pjsua.cp.factory, 3, sock, |
| 274 | &pjsua.stun_srv1, pjsua.stun_port1, |
| 275 | &pjsua.stun_srv2, pjsua.stun_port2, |
| 276 | mapped_addr); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 277 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 278 | pjsua_perror(THIS_FILE, "STUN error", status); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 279 | goto on_error; |
| 280 | } |
| 281 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 282 | if (pj_ntohs(mapped_addr[2].sin_port) == |
| 283 | pj_ntohs(mapped_addr[1].sin_port)+1) |
| 284 | { |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 285 | break; |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 286 | } |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 287 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 288 | pj_sock_close(sock[RTP_SOCK]); |
| 289 | sock[RTP_SOCK] = PJ_INVALID_SOCKET; |
| 290 | |
| 291 | pj_sock_close(sock[RTCP_SOCK]); |
| 292 | sock[RTCP_SOCK] = PJ_INVALID_SOCKET; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 293 | } |
| 294 | } |
| 295 | |
| 296 | if (sock[RTP_SOCK] == PJ_INVALID_SOCKET) { |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 297 | PJ_LOG(1,(THIS_FILE, |
| 298 | "Unable to find appropriate RTP/RTCP ports combination")); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 299 | goto on_error; |
| 300 | } |
| 301 | |
Benny Prijono | f04ffdd | 2006-02-21 00:11:18 +0000 | [diff] [blame] | 302 | if (sip) { |
| 303 | pjsua.sip_sock = sock[SIP_SOCK]; |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 304 | pj_memcpy(&pjsua.sip_sock_name, |
| 305 | &mapped_addr[SIP_SOCK], |
| 306 | sizeof(pj_sockaddr_in)); |
Benny Prijono | fccab71 | 2006-02-22 22:23:22 +0000 | [diff] [blame] | 307 | } else { |
| 308 | pj_sock_close(sock[0]); |
Benny Prijono | f04ffdd | 2006-02-21 00:11:18 +0000 | [diff] [blame] | 309 | } |
Benny Prijono | 9519658 | 2006-02-09 00:13:40 +0000 | [diff] [blame] | 310 | |
Benny Prijono | f04ffdd | 2006-02-21 00:11:18 +0000 | [diff] [blame] | 311 | skinfo->rtp_sock = sock[RTP_SOCK]; |
| 312 | pj_memcpy(&skinfo->rtp_addr_name, |
Benny Prijono | 9519658 | 2006-02-09 00:13:40 +0000 | [diff] [blame] | 313 | &mapped_addr[RTP_SOCK], sizeof(pj_sockaddr_in)); |
| 314 | |
Benny Prijono | f04ffdd | 2006-02-21 00:11:18 +0000 | [diff] [blame] | 315 | skinfo->rtcp_sock = sock[RTCP_SOCK]; |
| 316 | pj_memcpy(&skinfo->rtcp_addr_name, |
Benny Prijono | 9519658 | 2006-02-09 00:13:40 +0000 | [diff] [blame] | 317 | &mapped_addr[RTCP_SOCK], sizeof(pj_sockaddr_in)); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 318 | |
Benny Prijono | f04ffdd | 2006-02-21 00:11:18 +0000 | [diff] [blame] | 319 | if (sip) { |
| 320 | PJ_LOG(4,(THIS_FILE, "SIP UDP socket reachable at %s:%d", |
| 321 | pj_inet_ntoa(pjsua.sip_sock_name.sin_addr), |
| 322 | pj_ntohs(pjsua.sip_sock_name.sin_port))); |
| 323 | } |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 324 | PJ_LOG(4,(THIS_FILE, "RTP socket reachable at %s:%d", |
Benny Prijono | f04ffdd | 2006-02-21 00:11:18 +0000 | [diff] [blame] | 325 | pj_inet_ntoa(skinfo->rtp_addr_name.sin_addr), |
| 326 | pj_ntohs(skinfo->rtp_addr_name.sin_port))); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 327 | PJ_LOG(4,(THIS_FILE, "RTCP UDP socket reachable at %s:%d", |
Benny Prijono | f04ffdd | 2006-02-21 00:11:18 +0000 | [diff] [blame] | 328 | pj_inet_ntoa(skinfo->rtcp_addr_name.sin_addr), |
| 329 | pj_ntohs(skinfo->rtcp_addr_name.sin_port))); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 330 | |
Benny Prijono | fccab71 | 2006-02-22 22:23:22 +0000 | [diff] [blame] | 331 | rtp_port += 2; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 332 | return PJ_SUCCESS; |
| 333 | |
| 334 | on_error: |
| 335 | for (i=0; i<3; ++i) { |
Benny Prijono | f04ffdd | 2006-02-21 00:11:18 +0000 | [diff] [blame] | 336 | if (sip && i==0) |
| 337 | continue; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 338 | if (sock[i] != PJ_INVALID_SOCKET) |
| 339 | pj_sock_close(sock[i]); |
| 340 | } |
| 341 | return status; |
| 342 | } |
| 343 | |
| 344 | |
| 345 | |
| 346 | /* |
| 347 | * Initialize stack. |
| 348 | */ |
| 349 | static pj_status_t init_stack(void) |
| 350 | { |
| 351 | pj_status_t status; |
| 352 | |
| 353 | /* Create global endpoint: */ |
| 354 | |
| 355 | { |
| 356 | const pj_str_t *hostname; |
| 357 | const char *endpt_name; |
| 358 | |
| 359 | /* Endpoint MUST be assigned a globally unique name. |
| 360 | * The name will be used as the hostname in Warning header. |
| 361 | */ |
| 362 | |
| 363 | /* For this implementation, we'll use hostname for simplicity */ |
| 364 | hostname = pj_gethostname(); |
| 365 | endpt_name = hostname->ptr; |
| 366 | |
| 367 | /* Create the endpoint: */ |
| 368 | |
| 369 | status = pjsip_endpt_create(&pjsua.cp.factory, endpt_name, |
| 370 | &pjsua.endpt); |
| 371 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 372 | pjsua_perror(THIS_FILE, "Unable to create SIP endpoint", status); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 373 | return status; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | |
| 378 | /* Initialize transaction layer: */ |
| 379 | |
Benny Prijono | 2f8992b | 2006-02-25 21:16:36 +0000 | [diff] [blame] | 380 | status = pjsip_tsx_layer_init_module(pjsua.endpt); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 381 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 382 | pjsua_perror(THIS_FILE, "Transaction layer initialization error", |
| 383 | status); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 384 | goto on_error; |
| 385 | } |
| 386 | |
| 387 | /* Initialize UA layer module: */ |
| 388 | |
Benny Prijono | 2f8992b | 2006-02-25 21:16:36 +0000 | [diff] [blame] | 389 | status = pjsip_ua_init_module( pjsua.endpt, NULL ); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 390 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 391 | pjsua_perror(THIS_FILE, "UA layer initialization error", status); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 392 | goto on_error; |
| 393 | } |
| 394 | |
| 395 | /* Initialize and register pjsua's application module: */ |
| 396 | |
| 397 | { |
| 398 | pjsip_module my_mod = |
| 399 | { |
| 400 | NULL, NULL, /* prev, next. */ |
| 401 | { "mod-pjsua", 9 }, /* Name. */ |
| 402 | -1, /* Id */ |
| 403 | PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */ |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 404 | NULL, /* load() */ |
| 405 | NULL, /* start() */ |
| 406 | NULL, /* stop() */ |
| 407 | NULL, /* unload() */ |
| 408 | &mod_pjsua_on_rx_request, /* on_rx_request() */ |
| 409 | &mod_pjsua_on_rx_response, /* on_rx_response() */ |
| 410 | NULL, /* on_tx_request. */ |
| 411 | NULL, /* on_tx_response() */ |
| 412 | NULL, /* on_tsx_state() */ |
| 413 | }; |
| 414 | |
| 415 | pjsua.mod = my_mod; |
| 416 | |
| 417 | status = pjsip_endpt_register_module(pjsua.endpt, &pjsua.mod); |
| 418 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 419 | pjsua_perror(THIS_FILE, "Unable to register pjsua module", |
| 420 | status); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 421 | goto on_error; |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | /* Initialize invite session module: */ |
| 426 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 427 | status = pjsua_call_init(); |
| 428 | if (status != PJ_SUCCESS) { |
| 429 | pjsua_perror(THIS_FILE, "Invite usage initialization error", |
| 430 | status); |
| 431 | goto on_error; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 432 | } |
| 433 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 434 | /* Done */ |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 435 | |
| 436 | return PJ_SUCCESS; |
| 437 | |
| 438 | |
| 439 | on_error: |
| 440 | pjsip_endpt_destroy(pjsua.endpt); |
| 441 | pjsua.endpt = NULL; |
| 442 | return status; |
| 443 | } |
| 444 | |
| 445 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 446 | static int PJ_THREAD_FUNC pjsua_poll(void *arg) |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 447 | { |
Benny Prijono | f8baa87 | 2006-02-27 23:54:23 +0000 | [diff] [blame] | 448 | pj_status_t last_err = 0; |
| 449 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 450 | PJ_UNUSED_ARG(arg); |
| 451 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 452 | do { |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 453 | pj_time_val timeout = { 0, 10 }; |
Benny Prijono | f8baa87 | 2006-02-27 23:54:23 +0000 | [diff] [blame] | 454 | pj_status_t status; |
| 455 | |
| 456 | status = pjsip_endpt_handle_events (pjsua.endpt, &timeout); |
Benny Prijono | 080a2c4 | 2006-03-30 20:55:20 +0000 | [diff] [blame^] | 457 | if (status != PJ_SUCCESS && status != last_err) { |
Benny Prijono | f8baa87 | 2006-02-27 23:54:23 +0000 | [diff] [blame] | 458 | last_err = status; |
| 459 | pjsua_perror(THIS_FILE, "handle_events() returned error", status); |
| 460 | } |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 461 | } while (!pjsua.quit_flag); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 462 | |
| 463 | return 0; |
| 464 | } |
| 465 | |
| 466 | /* |
| 467 | * Initialize pjsua application. |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 468 | * This will initialize all libraries, create endpoint instance, and register |
| 469 | * pjsip modules. |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 470 | */ |
| 471 | pj_status_t pjsua_init(void) |
| 472 | { |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 473 | pj_status_t status; |
| 474 | |
| 475 | /* Init PJLIB logging: */ |
| 476 | |
| 477 | pj_log_set_level(pjsua.log_level); |
| 478 | pj_log_set_decor(pjsua.log_decor); |
| 479 | |
| 480 | |
| 481 | /* Init PJLIB: */ |
| 482 | |
| 483 | status = pj_init(); |
| 484 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 485 | pjsua_perror(THIS_FILE, "pj_init() error", status); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 486 | return status; |
| 487 | } |
| 488 | |
Benny Prijono | fccab71 | 2006-02-22 22:23:22 +0000 | [diff] [blame] | 489 | /* Init PJLIB-UTIL: */ |
| 490 | |
| 491 | status = pjlib_util_init(); |
| 492 | if (status != PJ_SUCCESS) { |
| 493 | pjsua_perror(THIS_FILE, "pjlib_util_init() error", status); |
| 494 | return status; |
| 495 | } |
| 496 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 497 | /* Init memory pool: */ |
| 498 | |
| 499 | /* Init caching pool. */ |
| 500 | pj_caching_pool_init(&pjsua.cp, &pj_pool_factory_default_policy, 0); |
| 501 | |
| 502 | /* Create memory pool for application. */ |
| 503 | pjsua.pool = pj_pool_create(&pjsua.cp.factory, "pjsua", 4000, 4000, NULL); |
| 504 | |
| 505 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 506 | /* Init PJSIP : */ |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 507 | |
| 508 | status = init_stack(); |
| 509 | if (status != PJ_SUCCESS) { |
| 510 | pj_caching_pool_destroy(&pjsua.cp); |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 511 | pjsua_perror(THIS_FILE, "Stack initialization has returned error", |
| 512 | status); |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 513 | return status; |
| 514 | } |
| 515 | |
Benny Prijono | 9519658 | 2006-02-09 00:13:40 +0000 | [diff] [blame] | 516 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 517 | /* Init core SIMPLE module : */ |
| 518 | |
| 519 | pjsip_evsub_init_module(pjsua.endpt); |
| 520 | |
| 521 | /* Init presence module: */ |
| 522 | |
| 523 | pjsip_pres_init_module( pjsua.endpt, pjsip_evsub_instance()); |
| 524 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 525 | /* Init xfer/REFER module */ |
| 526 | |
| 527 | pjsip_xfer_init_module( pjsua.endpt ); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 528 | |
| 529 | /* Init pjsua presence handler: */ |
| 530 | |
| 531 | pjsua_pres_init(); |
| 532 | |
Benny Prijono | b080837 | 2006-03-02 21:18:58 +0000 | [diff] [blame] | 533 | /* Init out-of-dialog MESSAGE request handler. */ |
| 534 | |
| 535 | pjsua_im_init(); |
| 536 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 537 | |
Benny Prijono | 9519658 | 2006-02-09 00:13:40 +0000 | [diff] [blame] | 538 | /* Init media endpoint: */ |
| 539 | |
Benny Prijono | 275fd68 | 2006-03-22 11:59:11 +0000 | [diff] [blame] | 540 | status = pjmedia_endpt_create(&pjsua.cp.factory, |
| 541 | pjsip_endpt_get_ioqueue(pjsua.endpt), 0, |
| 542 | &pjsua.med_endpt); |
Benny Prijono | 9519658 | 2006-02-09 00:13:40 +0000 | [diff] [blame] | 543 | if (status != PJ_SUCCESS) { |
| 544 | pj_caching_pool_destroy(&pjsua.cp); |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 545 | pjsua_perror(THIS_FILE, |
| 546 | "Media stack initialization has returned error", |
| 547 | status); |
Benny Prijono | 9519658 | 2006-02-09 00:13:40 +0000 | [diff] [blame] | 548 | return status; |
| 549 | } |
| 550 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 551 | /* Done. */ |
| 552 | return PJ_SUCCESS; |
| 553 | } |
| 554 | |
| 555 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 556 | /* |
| 557 | * Find account for incoming request. |
| 558 | */ |
| 559 | int pjsua_find_account_for_incoming(pjsip_rx_data *rdata) |
| 560 | { |
| 561 | pjsip_uri *uri; |
| 562 | pjsip_sip_uri *sip_uri; |
| 563 | int acc_index; |
| 564 | |
| 565 | uri = rdata->msg_info.to->uri; |
| 566 | |
| 567 | /* Just return account #0 if To URI is not SIP: */ |
| 568 | if (!PJSIP_URI_SCHEME_IS_SIP(uri) && |
| 569 | !PJSIP_URI_SCHEME_IS_SIPS(uri)) |
| 570 | { |
| 571 | return 0; |
| 572 | } |
| 573 | |
| 574 | |
| 575 | sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri); |
| 576 | |
| 577 | /* Find account which has matching username and domain. */ |
| 578 | for (acc_index=0; acc_index < pjsua.acc_cnt; ++acc_index) { |
| 579 | |
| 580 | pjsua_acc *acc = &pjsua.acc[acc_index]; |
| 581 | |
| 582 | if (pj_stricmp(&acc->user_part, &sip_uri->user)==0 && |
| 583 | pj_stricmp(&acc->host_part, &sip_uri->host)==0) |
| 584 | { |
| 585 | /* Match ! */ |
| 586 | return acc_index; |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | /* No matching, try match domain part only. */ |
| 591 | for (acc_index=0; acc_index < pjsua.acc_cnt; ++acc_index) { |
| 592 | |
| 593 | pjsua_acc *acc = &pjsua.acc[acc_index]; |
| 594 | |
| 595 | if (pj_stricmp(&acc->host_part, &sip_uri->host)==0) { |
| 596 | /* Match ! */ |
| 597 | return acc_index; |
| 598 | } |
| 599 | } |
| 600 | |
| 601 | /* Still no match, just return account #0 */ |
| 602 | return 0; |
| 603 | } |
| 604 | |
| 605 | |
| 606 | /* |
| 607 | * Find account for outgoing request. |
| 608 | */ |
| 609 | int pjsua_find_account_for_outgoing(const pj_str_t *url) |
| 610 | { |
| 611 | PJ_UNUSED_ARG(url); |
| 612 | |
| 613 | /* Just use account #0 */ |
| 614 | return 0; |
| 615 | } |
| 616 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 617 | |
| 618 | /* |
Benny Prijono | eb30bf5 | 2006-03-04 20:43:52 +0000 | [diff] [blame] | 619 | * Init media. |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 620 | */ |
Benny Prijono | eb30bf5 | 2006-03-04 20:43:52 +0000 | [diff] [blame] | 621 | static pj_status_t init_media(void) |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 622 | { |
Benny Prijono | 8e3344c | 2006-03-08 12:37:22 +0000 | [diff] [blame] | 623 | unsigned options; |
Benny Prijono | eb30bf5 | 2006-03-04 20:43:52 +0000 | [diff] [blame] | 624 | pj_status_t status; |
| 625 | |
| 626 | /* If user doesn't specify any codecs, register all of them. */ |
| 627 | if (pjsua.codec_cnt == 0) { |
| 628 | |
Benny Prijono | 4381efe | 2006-03-16 14:24:26 +0000 | [diff] [blame] | 629 | #if PJMEDIA_HAS_SPEEX_CODEC |
Benny Prijono | eb30bf5 | 2006-03-04 20:43:52 +0000 | [diff] [blame] | 630 | unsigned option = PJMEDIA_SPEEX_NO_WB | PJMEDIA_SPEEX_NO_UWB; |
| 631 | |
| 632 | /* Register speex. */ |
Benny Prijono | fa137ca | 2006-03-20 17:42:37 +0000 | [diff] [blame] | 633 | if (pjsua.has_wb) |
| 634 | option &= ~PJMEDIA_SPEEX_NO_WB; |
| 635 | if (pjsua.has_uwb) |
| 636 | option &= ~PJMEDIA_SPEEX_NO_UWB; |
Benny Prijono | eb30bf5 | 2006-03-04 20:43:52 +0000 | [diff] [blame] | 637 | |
Benny Prijono | 1c2bf46 | 2006-03-05 11:54:02 +0000 | [diff] [blame] | 638 | status = pjmedia_codec_speex_init(pjsua.med_endpt, option, |
| 639 | pjsua.quality, pjsua.complexity ); |
Benny Prijono | eb30bf5 | 2006-03-04 20:43:52 +0000 | [diff] [blame] | 640 | if (status != PJ_SUCCESS) { |
| 641 | pjsua_perror(THIS_FILE, "Error initializing Speex codec", |
| 642 | status); |
| 643 | return status; |
| 644 | } |
| 645 | |
| 646 | pjsua.codec_arg[pjsua.codec_cnt] = pj_str("speex"); |
| 647 | pjsua.codec_deinit[pjsua.codec_cnt] = &pjmedia_codec_speex_deinit; |
| 648 | pjsua.codec_cnt++; |
Benny Prijono | 4381efe | 2006-03-16 14:24:26 +0000 | [diff] [blame] | 649 | #endif /* PJMEDIA_HAS_SPEEX_CODEC */ |
Benny Prijono | eb30bf5 | 2006-03-04 20:43:52 +0000 | [diff] [blame] | 650 | |
Benny Prijono | 4381efe | 2006-03-16 14:24:26 +0000 | [diff] [blame] | 651 | #if PJMEDIA_HAS_GSM_CODEC |
Benny Prijono | eb30bf5 | 2006-03-04 20:43:52 +0000 | [diff] [blame] | 652 | /* Register GSM */ |
| 653 | status = pjmedia_codec_gsm_init(pjsua.med_endpt); |
| 654 | if (status != PJ_SUCCESS) { |
| 655 | pjsua_perror(THIS_FILE, "Error initializing GSM codec", |
| 656 | status); |
| 657 | return status; |
| 658 | } |
| 659 | |
| 660 | pjsua.codec_arg[pjsua.codec_cnt] = pj_str("gsm"); |
| 661 | pjsua.codec_deinit[pjsua.codec_cnt] = &pjmedia_codec_gsm_deinit; |
| 662 | pjsua.codec_cnt++; |
Benny Prijono | 4381efe | 2006-03-16 14:24:26 +0000 | [diff] [blame] | 663 | #endif /* PJMEDIA_HAS_GSM_CODEC */ |
Benny Prijono | eb30bf5 | 2006-03-04 20:43:52 +0000 | [diff] [blame] | 664 | |
Benny Prijono | 4381efe | 2006-03-16 14:24:26 +0000 | [diff] [blame] | 665 | #if PJMEDIA_HAS_G711_CODEC |
Benny Prijono | eb30bf5 | 2006-03-04 20:43:52 +0000 | [diff] [blame] | 666 | /* Register PCMA and PCMU */ |
| 667 | status = pjmedia_codec_g711_init(pjsua.med_endpt); |
| 668 | if (status != PJ_SUCCESS) { |
| 669 | pjsua_perror(THIS_FILE, "Error initializing G711 codec", |
| 670 | status); |
| 671 | return status; |
| 672 | } |
| 673 | |
| 674 | pjsua.codec_arg[pjsua.codec_cnt] = pj_str("pcmu"); |
| 675 | pjsua.codec_deinit[pjsua.codec_cnt] = &pjmedia_codec_g711_deinit; |
| 676 | pjsua.codec_cnt++; |
| 677 | pjsua.codec_arg[pjsua.codec_cnt] = pj_str("pcma"); |
| 678 | pjsua.codec_deinit[pjsua.codec_cnt] = &pjmedia_codec_g711_deinit; |
| 679 | pjsua.codec_cnt++; |
Benny Prijono | 4381efe | 2006-03-16 14:24:26 +0000 | [diff] [blame] | 680 | #endif /* PJMEDIA_HAS_G711_CODEC */ |
Benny Prijono | eb30bf5 | 2006-03-04 20:43:52 +0000 | [diff] [blame] | 681 | |
| 682 | } else { |
| 683 | |
| 684 | /* If user specifies the exact codec to be used, then create only |
| 685 | * those codecs. |
| 686 | */ |
| 687 | int i; |
| 688 | |
| 689 | for (i=0; i<pjsua.codec_cnt; ++i) { |
| 690 | |
Benny Prijono | 4381efe | 2006-03-16 14:24:26 +0000 | [diff] [blame] | 691 | if (0) { |
| 692 | /* Dummy */ |
| 693 | } |
| 694 | #if PJMEDIA_HAS_SPEEX_CODEC |
Benny Prijono | eb30bf5 | 2006-03-04 20:43:52 +0000 | [diff] [blame] | 695 | /* Is it speex? */ |
Benny Prijono | 4381efe | 2006-03-16 14:24:26 +0000 | [diff] [blame] | 696 | else if (!pj_stricmp2(&pjsua.codec_arg[i], "speex")) { |
Benny Prijono | eb30bf5 | 2006-03-04 20:43:52 +0000 | [diff] [blame] | 697 | |
| 698 | unsigned option = PJMEDIA_SPEEX_NO_WB | PJMEDIA_SPEEX_NO_UWB; |
| 699 | |
| 700 | /* Register speex. */ |
Benny Prijono | fa137ca | 2006-03-20 17:42:37 +0000 | [diff] [blame] | 701 | if (pjsua.has_wb) |
Benny Prijono | eb30bf5 | 2006-03-04 20:43:52 +0000 | [diff] [blame] | 702 | option &= ~(PJMEDIA_SPEEX_NO_WB); |
Benny Prijono | fa137ca | 2006-03-20 17:42:37 +0000 | [diff] [blame] | 703 | if (pjsua.has_uwb) |
Benny Prijono | eb30bf5 | 2006-03-04 20:43:52 +0000 | [diff] [blame] | 704 | option &= ~(PJMEDIA_SPEEX_NO_UWB); |
| 705 | |
| 706 | status = pjmedia_codec_speex_init(pjsua.med_endpt, option, |
| 707 | -1, -1); |
| 708 | if (status != PJ_SUCCESS) { |
| 709 | pjsua_perror(THIS_FILE, "Error initializing Speex codec", |
| 710 | status); |
| 711 | return status; |
| 712 | } |
| 713 | |
| 714 | pjsua.codec_deinit[i] = &pjmedia_codec_speex_deinit; |
| 715 | } |
Benny Prijono | 4381efe | 2006-03-16 14:24:26 +0000 | [diff] [blame] | 716 | #endif /* PJMEDIA_HAS_SPEEX_CODEC */ |
| 717 | |
| 718 | #if PJMEDIA_HAS_GSM_CODEC |
Benny Prijono | eb30bf5 | 2006-03-04 20:43:52 +0000 | [diff] [blame] | 719 | /* Is it gsm? */ |
| 720 | else if (!pj_stricmp2(&pjsua.codec_arg[i], "gsm")) { |
| 721 | |
| 722 | status = pjmedia_codec_gsm_init(pjsua.med_endpt); |
| 723 | if (status != PJ_SUCCESS) { |
| 724 | pjsua_perror(THIS_FILE, "Error initializing GSM codec", |
| 725 | status); |
| 726 | return status; |
| 727 | } |
| 728 | |
| 729 | pjsua.codec_deinit[i] = &pjmedia_codec_gsm_deinit; |
| 730 | |
| 731 | } |
Benny Prijono | 4381efe | 2006-03-16 14:24:26 +0000 | [diff] [blame] | 732 | #endif /* PJMEDIA_HAS_GSM_CODEC */ |
| 733 | |
| 734 | #if PJMEDIA_HAS_G711_CODEC |
Benny Prijono | eb30bf5 | 2006-03-04 20:43:52 +0000 | [diff] [blame] | 735 | /* Is it pcma/pcmu? */ |
| 736 | else if (!pj_stricmp2(&pjsua.codec_arg[i], "pcmu") || |
| 737 | !pj_stricmp2(&pjsua.codec_arg[i], "pcma")) |
| 738 | { |
| 739 | |
| 740 | status = pjmedia_codec_g711_init(pjsua.med_endpt); |
| 741 | if (status != PJ_SUCCESS) { |
| 742 | pjsua_perror(THIS_FILE, "Error initializing G711 codec", |
| 743 | status); |
| 744 | return status; |
| 745 | } |
| 746 | |
| 747 | pjsua.codec_deinit[i] = &pjmedia_codec_g711_deinit; |
| 748 | |
| 749 | } |
Benny Prijono | 4381efe | 2006-03-16 14:24:26 +0000 | [diff] [blame] | 750 | #endif /* PJMEDIA_HAS_G711_CODEC */ |
| 751 | |
Benny Prijono | eb30bf5 | 2006-03-04 20:43:52 +0000 | [diff] [blame] | 752 | /* Don't know about this codec... */ |
| 753 | else { |
| 754 | |
| 755 | PJ_LOG(1,(THIS_FILE, "Error: unsupported codecs %s", |
| 756 | pjsua.codec_arg[i].ptr)); |
| 757 | return PJMEDIA_CODEC_EUNSUP; |
| 758 | } |
| 759 | } |
| 760 | } |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 761 | |
Benny Prijono | 8e3344c | 2006-03-08 12:37:22 +0000 | [diff] [blame] | 762 | /* Init options for conference bridge. */ |
| 763 | options = 0; |
| 764 | if (pjsua.no_mic) |
| 765 | options |= PJMEDIA_CONF_NO_MIC; |
| 766 | |
Benny Prijono | f8baa87 | 2006-02-27 23:54:23 +0000 | [diff] [blame] | 767 | /* Init conference bridge. */ |
| 768 | |
| 769 | status = pjmedia_conf_create(pjsua.pool, |
| 770 | pjsua.max_calls+PJSUA_CONF_MORE_PORTS, |
Benny Prijono | 08e0d06 | 2006-03-04 14:52:44 +0000 | [diff] [blame] | 771 | pjsua.clock_rate, |
Benny Prijono | d0659a3 | 2006-03-16 19:03:07 +0000 | [diff] [blame] | 772 | 1, /* mono */ |
Benny Prijono | 08e0d06 | 2006-03-04 14:52:44 +0000 | [diff] [blame] | 773 | pjsua.clock_rate * 20 / 1000, 16, |
Benny Prijono | 8e3344c | 2006-03-08 12:37:22 +0000 | [diff] [blame] | 774 | options, |
Benny Prijono | 08e0d06 | 2006-03-04 14:52:44 +0000 | [diff] [blame] | 775 | &pjsua.mconf); |
Benny Prijono | f8baa87 | 2006-02-27 23:54:23 +0000 | [diff] [blame] | 776 | if (status != PJ_SUCCESS) { |
| 777 | pj_caching_pool_destroy(&pjsua.cp); |
| 778 | pjsua_perror(THIS_FILE, |
| 779 | "Media stack initialization has returned error", |
| 780 | status); |
| 781 | return status; |
| 782 | } |
| 783 | |
Benny Prijono | de38058 | 2006-03-15 19:32:41 +0000 | [diff] [blame] | 784 | /* Add NULL port to the bridge. */ |
| 785 | status = pjmedia_null_port_create( pjsua.pool, pjsua.clock_rate, |
Benny Prijono | d0659a3 | 2006-03-16 19:03:07 +0000 | [diff] [blame] | 786 | 1, /* mono */ |
Benny Prijono | de38058 | 2006-03-15 19:32:41 +0000 | [diff] [blame] | 787 | pjsua.clock_rate * 20 / 1000, 16, |
| 788 | &pjsua.null_port); |
| 789 | pjmedia_conf_add_port( pjsua.mconf, pjsua.pool, pjsua.null_port, |
| 790 | &pjsua.null_port->info.name, NULL ); |
| 791 | |
Benny Prijono | 3987915 | 2006-02-23 02:09:10 +0000 | [diff] [blame] | 792 | /* Create WAV file player if required: */ |
| 793 | |
| 794 | if (pjsua.wav_file) { |
Benny Prijono | 3987915 | 2006-02-23 02:09:10 +0000 | [diff] [blame] | 795 | pj_str_t port_name; |
| 796 | |
| 797 | /* Create the file player port. */ |
| 798 | status = pjmedia_file_player_port_create( pjsua.pool, pjsua.wav_file, |
Benny Prijono | de38058 | 2006-03-15 19:32:41 +0000 | [diff] [blame] | 799 | 0, -1, NULL, |
| 800 | &pjsua.file_port); |
Benny Prijono | 3987915 | 2006-02-23 02:09:10 +0000 | [diff] [blame] | 801 | if (status != PJ_SUCCESS) { |
| 802 | pjsua_perror(THIS_FILE, |
| 803 | "Error playing media file", |
| 804 | status); |
| 805 | return status; |
| 806 | } |
| 807 | |
| 808 | /* Add port to conference bridge: */ |
Benny Prijono | de38058 | 2006-03-15 19:32:41 +0000 | [diff] [blame] | 809 | status = pjmedia_conf_add_port(pjsua.mconf, pjsua.pool, |
| 810 | pjsua.file_port, |
Benny Prijono | 3987915 | 2006-02-23 02:09:10 +0000 | [diff] [blame] | 811 | pj_cstr(&port_name, pjsua.wav_file), |
| 812 | &pjsua.wav_slot); |
| 813 | if (status != PJ_SUCCESS) { |
| 814 | pjsua_perror(THIS_FILE, |
| 815 | "Unable to add file player to conference bridge", |
| 816 | status); |
| 817 | return status; |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | |
Benny Prijono | eb30bf5 | 2006-03-04 20:43:52 +0000 | [diff] [blame] | 822 | return PJ_SUCCESS; |
| 823 | } |
| 824 | |
| 825 | |
| 826 | /* |
| 827 | * Start pjsua stack. |
| 828 | * This will start the registration process, if registration is configured. |
| 829 | */ |
| 830 | pj_status_t pjsua_start(void) |
| 831 | { |
| 832 | int i; /* Must be signed */ |
| 833 | pjsip_transport *udp_transport; |
| 834 | pj_status_t status = PJ_SUCCESS; |
| 835 | |
| 836 | /* |
| 837 | * Init media subsystem (codecs, conference bridge, et all). |
| 838 | */ |
| 839 | status = init_media(); |
| 840 | if (status != PJ_SUCCESS) |
| 841 | return status; |
| 842 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 843 | /* Init sockets (STUN etc): */ |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 844 | for (i=0; i<(int)pjsua.max_calls; ++i) { |
| 845 | status = init_sockets(i==0, &pjsua.calls[i].skinfo); |
Benny Prijono | f04ffdd | 2006-02-21 00:11:18 +0000 | [diff] [blame] | 846 | if (status != PJ_SUCCESS) { |
| 847 | pjsua_perror(THIS_FILE, "init_sockets() has returned error", |
| 848 | status); |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 849 | --i; |
| 850 | if (i >= 0) |
| 851 | pj_sock_close(pjsua.sip_sock); |
| 852 | while (i >= 0) { |
| 853 | pj_sock_close(pjsua.calls[i].skinfo.rtp_sock); |
| 854 | pj_sock_close(pjsua.calls[i].skinfo.rtcp_sock); |
| 855 | } |
Benny Prijono | f04ffdd | 2006-02-21 00:11:18 +0000 | [diff] [blame] | 856 | return status; |
| 857 | } |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 858 | } |
| 859 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 860 | /* Add UDP transport: */ |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 861 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 862 | { |
| 863 | /* Init the published name for the transport. |
| 864 | * Depending whether STUN is used, this may be the STUN mapped |
| 865 | * address, or socket's bound address. |
| 866 | */ |
| 867 | pjsip_host_port addr_name; |
| 868 | |
| 869 | addr_name.host.ptr = pj_inet_ntoa(pjsua.sip_sock_name.sin_addr); |
Benny Prijono | f319507 | 2006-02-14 21:15:30 +0000 | [diff] [blame] | 870 | addr_name.host.slen = pj_ansi_strlen(addr_name.host.ptr); |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 871 | addr_name.port = pj_ntohs(pjsua.sip_sock_name.sin_port); |
| 872 | |
| 873 | /* Create UDP transport from previously created UDP socket: */ |
| 874 | |
| 875 | status = pjsip_udp_transport_attach( pjsua.endpt, pjsua.sip_sock, |
| 876 | &addr_name, 1, |
| 877 | &udp_transport); |
| 878 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 879 | pjsua_perror(THIS_FILE, "Unable to start UDP transport", |
| 880 | status); |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 881 | return status; |
| 882 | } |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 883 | } |
| 884 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 885 | /* Initialize Contact URI, if one is not specified: */ |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 886 | for (i=0; i<pjsua.acc_cnt; ++i) { |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 887 | |
| 888 | pjsip_uri *uri; |
| 889 | pjsip_sip_uri *sip_uri; |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 890 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 891 | /* Need to parse local_uri to get the elements: */ |
| 892 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 893 | uri = pjsip_parse_uri(pjsua.pool, pjsua.acc[i].local_uri.ptr, |
| 894 | pjsua.acc[i].local_uri.slen, 0); |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 895 | if (uri == NULL) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 896 | pjsua_perror(THIS_FILE, "Invalid local URI", |
| 897 | PJSIP_EINVALIDURI); |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 898 | return PJSIP_EINVALIDURI; |
| 899 | } |
| 900 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 901 | /* Local URI MUST be a SIP or SIPS: */ |
| 902 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 903 | if (!PJSIP_URI_SCHEME_IS_SIP(uri) && |
| 904 | !PJSIP_URI_SCHEME_IS_SIPS(uri)) |
| 905 | { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 906 | pjsua_perror(THIS_FILE, "Invalid local URI", |
| 907 | PJSIP_EINVALIDSCHEME); |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 908 | return PJSIP_EINVALIDSCHEME; |
| 909 | } |
| 910 | |
| 911 | |
| 912 | /* Get the SIP URI object: */ |
| 913 | |
| 914 | sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri); |
| 915 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 916 | pjsua.acc[i].user_part = sip_uri->user; |
| 917 | pjsua.acc[i].host_part = sip_uri->host; |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 918 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 919 | if (pjsua.acc[i].contact_uri.slen == 0 && |
| 920 | pjsua.acc[i].local_uri.slen) |
| 921 | { |
| 922 | char contact[128]; |
| 923 | int len; |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 924 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 925 | /* The local Contact is the username@ip-addr, where |
| 926 | * - username is taken from the local URI, |
| 927 | * - ip-addr in UDP transport's address name (which may have been |
| 928 | * resolved from STUN. |
| 929 | */ |
| 930 | |
| 931 | /* Build temporary contact string. */ |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 932 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 933 | if (sip_uri->user.slen) { |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 934 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 935 | /* With the user part. */ |
Benny Prijono | ed811d7 | 2006-03-10 12:57:12 +0000 | [diff] [blame] | 936 | len = pj_ansi_snprintf(contact, sizeof(contact), |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 937 | "<sip:%.*s@%.*s:%d>", |
| 938 | (int)sip_uri->user.slen, |
| 939 | sip_uri->user.ptr, |
| 940 | (int)udp_transport->local_name.host.slen, |
| 941 | udp_transport->local_name.host.ptr, |
| 942 | udp_transport->local_name.port); |
| 943 | } else { |
| 944 | |
| 945 | /* Without user part */ |
| 946 | |
Benny Prijono | ed811d7 | 2006-03-10 12:57:12 +0000 | [diff] [blame] | 947 | len = pj_ansi_snprintf(contact, sizeof(contact), |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 948 | "<sip:%.*s:%d>", |
| 949 | (int)udp_transport->local_name.host.slen, |
| 950 | udp_transport->local_name.host.ptr, |
| 951 | udp_transport->local_name.port); |
| 952 | } |
| 953 | |
| 954 | if (len < 1 || len >= sizeof(contact)) { |
| 955 | pjsua_perror(THIS_FILE, "Invalid Contact", PJSIP_EURITOOLONG); |
| 956 | return PJSIP_EURITOOLONG; |
| 957 | } |
| 958 | |
| 959 | /* Duplicate Contact uri. */ |
| 960 | |
| 961 | pj_strdup2(pjsua.pool, &pjsua.acc[i].contact_uri, contact); |
| 962 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 963 | } |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 964 | } |
| 965 | |
Benny Prijono | 9519658 | 2006-02-09 00:13:40 +0000 | [diff] [blame] | 966 | /* If outbound_proxy is specified, put it in the route_set: */ |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 967 | |
Benny Prijono | 9519658 | 2006-02-09 00:13:40 +0000 | [diff] [blame] | 968 | if (pjsua.outbound_proxy.slen) { |
| 969 | |
| 970 | pjsip_route_hdr *route; |
| 971 | const pj_str_t hname = { "Route", 5 }; |
| 972 | int parsed_len; |
| 973 | |
| 974 | route = pjsip_parse_hdr( pjsua.pool, &hname, |
| 975 | pjsua.outbound_proxy.ptr, |
| 976 | pjsua.outbound_proxy.slen, |
| 977 | &parsed_len); |
| 978 | if (route == NULL) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 979 | pjsua_perror(THIS_FILE, "Invalid outbound proxy URL", |
| 980 | PJSIP_EINVALIDURI); |
Benny Prijono | 9519658 | 2006-02-09 00:13:40 +0000 | [diff] [blame] | 981 | return PJSIP_EINVALIDURI; |
| 982 | } |
| 983 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 984 | for (i=0; i<pjsua.acc_cnt; ++i) { |
| 985 | pj_list_push_front(&pjsua.acc[i].route_set, route); |
| 986 | } |
Benny Prijono | 9519658 | 2006-02-09 00:13:40 +0000 | [diff] [blame] | 987 | } |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 988 | |
| 989 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 990 | /* Create worker thread(s), if required: */ |
| 991 | |
| 992 | for (i=0; i<pjsua.thread_cnt; ++i) { |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 993 | status = pj_thread_create( pjsua.pool, "pjsua", &pjsua_poll, |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 994 | NULL, 0, 0, &pjsua.threads[i]); |
| 995 | if (status != PJ_SUCCESS) { |
| 996 | pjsua.quit_flag = 1; |
| 997 | for (--i; i>=0; --i) { |
| 998 | pj_thread_join(pjsua.threads[i]); |
| 999 | pj_thread_destroy(pjsua.threads[i]); |
| 1000 | } |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1001 | return status; |
| 1002 | } |
| 1003 | } |
| 1004 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 1005 | /* Start registration: */ |
| 1006 | |
| 1007 | /* Create client registration session: */ |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1008 | for (i=0; i<pjsua.acc_cnt; ++i) { |
| 1009 | status = pjsua_regc_init(i); |
| 1010 | if (status != PJ_SUCCESS) |
| 1011 | return status; |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 1012 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1013 | /* Perform registration, if required. */ |
| 1014 | if (pjsua.acc[i].regc) { |
| 1015 | pjsua_regc_update(i, 1); |
| 1016 | } |
| 1017 | } |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 1018 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1019 | |
| 1020 | /* Find account for outgoing preence subscription */ |
| 1021 | for (i=0; i<pjsua.buddy_cnt; ++i) { |
| 1022 | pjsua.buddies[i].acc_index = |
| 1023 | pjsua_find_account_for_outgoing(&pjsua.buddies[i].uri); |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1027 | PJ_LOG(3,(THIS_FILE, "PJSUA version %s started", PJ_VERSION)); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1028 | return PJ_SUCCESS; |
| 1029 | } |
| 1030 | |
| 1031 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1032 | /* Sleep with polling */ |
| 1033 | static void busy_sleep(unsigned msec) |
| 1034 | { |
| 1035 | pj_time_val timeout, now; |
| 1036 | |
| 1037 | pj_gettimeofday(&timeout); |
| 1038 | timeout.msec += msec; |
| 1039 | pj_time_val_normalize(&timeout); |
| 1040 | |
| 1041 | do { |
| 1042 | pjsua_poll(NULL); |
| 1043 | pj_gettimeofday(&now); |
| 1044 | } while (PJ_TIME_VAL_LT(now, timeout)); |
| 1045 | } |
| 1046 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1047 | /* |
| 1048 | * Destroy pjsua. |
| 1049 | */ |
| 1050 | pj_status_t pjsua_destroy(void) |
| 1051 | { |
Benny Prijono | eb30bf5 | 2006-03-04 20:43:52 +0000 | [diff] [blame] | 1052 | int i; /* Must be signed */ |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1053 | |
| 1054 | /* Signal threads to quit: */ |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1055 | pjsua.quit_flag = 1; |
| 1056 | |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1057 | /* Terminate all calls. */ |
Benny Prijono | 1a17414 | 2006-03-01 20:46:13 +0000 | [diff] [blame] | 1058 | pjsua_call_hangup_all(); |
Benny Prijono | a91a003 | 2006-02-26 21:23:45 +0000 | [diff] [blame] | 1059 | |
| 1060 | /* Terminate all presence subscriptions. */ |
| 1061 | pjsua_pres_shutdown(); |
| 1062 | |
| 1063 | /* Unregister, if required: */ |
| 1064 | for (i=0; i<pjsua.acc_cnt; ++i) { |
| 1065 | if (pjsua.acc[i].regc) { |
| 1066 | pjsua_regc_update(i, 0); |
| 1067 | } |
| 1068 | } |
| 1069 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1070 | /* Wait worker threads to quit: */ |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1071 | for (i=0; i<pjsua.thread_cnt; ++i) { |
| 1072 | |
Benny Prijono | e4f2abb | 2006-02-10 14:04:05 +0000 | [diff] [blame] | 1073 | if (pjsua.threads[i]) { |
| 1074 | pj_thread_join(pjsua.threads[i]); |
| 1075 | pj_thread_destroy(pjsua.threads[i]); |
| 1076 | pjsua.threads[i] = NULL; |
| 1077 | } |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1078 | } |
| 1079 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1080 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 1081 | /* Wait for some time to allow unregistration to complete: */ |
| 1082 | PJ_LOG(4,(THIS_FILE, "Shutting down...")); |
| 1083 | busy_sleep(1000); |
| 1084 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 1085 | /* Destroy conference bridge. */ |
| 1086 | if (pjsua.mconf) |
| 1087 | pjmedia_conf_destroy(pjsua.mconf); |
| 1088 | |
Benny Prijono | de38058 | 2006-03-15 19:32:41 +0000 | [diff] [blame] | 1089 | /* Destroy file port */ |
Benny Prijono | 4381efe | 2006-03-16 14:24:26 +0000 | [diff] [blame] | 1090 | if (pjsua.file_port) |
| 1091 | pjmedia_port_destroy(pjsua.file_port); |
Benny Prijono | de38058 | 2006-03-15 19:32:41 +0000 | [diff] [blame] | 1092 | |
| 1093 | /* Destroy null port. */ |
Benny Prijono | 4381efe | 2006-03-16 14:24:26 +0000 | [diff] [blame] | 1094 | if (pjsua.null_port) |
| 1095 | pjmedia_port_destroy(pjsua.null_port); |
Benny Prijono | de38058 | 2006-03-15 19:32:41 +0000 | [diff] [blame] | 1096 | |
| 1097 | |
Benny Prijono | eb30bf5 | 2006-03-04 20:43:52 +0000 | [diff] [blame] | 1098 | /* Shutdown all codecs: */ |
| 1099 | for (i = pjsua.codec_cnt-1; i >= 0; --i) { |
| 1100 | (*pjsua.codec_deinit[i])(); |
| 1101 | } |
| 1102 | |
| 1103 | /* Destroy media endpoint. */ |
| 1104 | |
| 1105 | pjmedia_endpt_destroy(pjsua.med_endpt); |
| 1106 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1107 | /* Destroy endpoint. */ |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 1108 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1109 | pjsip_endpt_destroy(pjsua.endpt); |
| 1110 | pjsua.endpt = NULL; |
| 1111 | |
| 1112 | /* Destroy caching pool. */ |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 1113 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1114 | pj_caching_pool_destroy(&pjsua.cp); |
| 1115 | |
| 1116 | |
| 1117 | /* Done. */ |
| 1118 | |
| 1119 | return PJ_SUCCESS; |
| 1120 | } |
| 1121 | |