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 | */ |
| 19 | #include "pjsua.h" |
| 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 | { |
| 48 | |
| 49 | /* Normally need another thread for console application, because main |
| 50 | * thread will be blocked in fgets(). |
| 51 | */ |
| 52 | pjsua.thread_cnt = 1; |
| 53 | |
| 54 | |
| 55 | /* Default transport settings: */ |
| 56 | |
| 57 | pjsua.sip_port = 5060; |
| 58 | |
| 59 | |
| 60 | /* Default logging settings: */ |
| 61 | |
| 62 | pjsua.log_level = 5; |
| 63 | pjsua.app_log_level = 4; |
| 64 | pjsua.log_decor = PJ_LOG_HAS_SENDER | PJ_LOG_HAS_TIME | |
| 65 | PJ_LOG_HAS_MICRO_SEC | PJ_LOG_HAS_NEWLINE; |
| 66 | |
| 67 | /* Default: do not use STUN: */ |
| 68 | |
| 69 | pjsua.stun_port1 = pjsua.stun_port2 = 0; |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 70 | |
| 71 | /* Default URIs: */ |
| 72 | |
| 73 | pjsua.local_uri = pj_str(PJSUA_LOCAL_URI); |
Benny Prijono | 9519658 | 2006-02-09 00:13:40 +0000 | [diff] [blame] | 74 | |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 75 | /* Default registration timeout: */ |
| 76 | |
| 77 | pjsua.reg_timeout = 55; |
| 78 | |
Benny Prijono | 9519658 | 2006-02-09 00:13:40 +0000 | [diff] [blame] | 79 | /* Init route set list: */ |
| 80 | |
| 81 | pj_list_init(&pjsua.route_set); |
Benny Prijono | 632ce71 | 2006-02-09 14:01:40 +0000 | [diff] [blame] | 82 | |
| 83 | /* Init invite session list: */ |
| 84 | |
| 85 | pj_list_init(&pjsua.inv_list); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 86 | |
| 87 | /* Init server presence subscription list: */ |
| 88 | |
| 89 | pj_list_init(&pjsua.pres_srv_list); |
| 90 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 94 | |
| 95 | /* |
| 96 | * Handler for receiving incoming requests. |
| 97 | * |
| 98 | * This handler serves multiple purposes: |
| 99 | * - it receives requests outside dialogs. |
| 100 | * - it receives requests inside dialogs, when the requests are |
| 101 | * unhandled by other dialog usages. Example of these |
| 102 | * requests are: MESSAGE. |
| 103 | */ |
| 104 | static pj_bool_t mod_pjsua_on_rx_request(pjsip_rx_data *rdata) |
| 105 | { |
Benny Prijono | 3899823 | 2006-02-08 22:44:25 +0000 | [diff] [blame] | 106 | |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 107 | if (rdata->msg_info.msg->line.req.method.id == PJSIP_INVITE_METHOD) { |
Benny Prijono | 3899823 | 2006-02-08 22:44:25 +0000 | [diff] [blame] | 108 | |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 109 | return pjsua_inv_on_incoming(rdata); |
Benny Prijono | 3899823 | 2006-02-08 22:44:25 +0000 | [diff] [blame] | 110 | |
Benny Prijono | 3899823 | 2006-02-08 22:44:25 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 113 | return PJ_FALSE; |
| 114 | } |
| 115 | |
| 116 | |
| 117 | /* |
| 118 | * Handler for receiving incoming responses. |
| 119 | * |
| 120 | * This handler serves multiple purposes: |
| 121 | * - it receives strayed responses (i.e. outside any dialog and |
| 122 | * outside any transactions). |
| 123 | * - it receives responses coming to a transaction, when pjsua |
| 124 | * module is set as transaction user for the transaction. |
| 125 | * - it receives responses inside a dialog, when these responses |
| 126 | * are unhandled by other dialog usages. |
| 127 | */ |
| 128 | static pj_bool_t mod_pjsua_on_rx_response(pjsip_rx_data *rdata) |
| 129 | { |
| 130 | PJ_UNUSED_ARG(rdata); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 131 | return PJ_FALSE; |
| 132 | } |
| 133 | |
| 134 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 135 | /* |
| 136 | * Initialize sockets and optionally get the public address via STUN. |
| 137 | */ |
Benny Prijono | f04ffdd | 2006-02-21 00:11:18 +0000 | [diff] [blame] | 138 | static pj_status_t init_sockets(pj_bool_t sip, |
| 139 | pjmedia_sock_info *skinfo) |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 140 | { |
| 141 | enum { |
| 142 | RTP_START_PORT = 4000, |
| 143 | RTP_RANDOM_START = 2, |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 144 | RTP_RETRY = 20 |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 145 | }; |
| 146 | enum { |
| 147 | SIP_SOCK, |
| 148 | RTP_SOCK, |
| 149 | RTCP_SOCK, |
| 150 | }; |
| 151 | int i; |
Benny Prijono | fccab71 | 2006-02-22 22:23:22 +0000 | [diff] [blame] | 152 | static pj_uint16_t rtp_port = RTP_START_PORT; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 153 | pj_sock_t sock[3]; |
| 154 | pj_sockaddr_in mapped_addr[3]; |
Benny Prijono | f04ffdd | 2006-02-21 00:11:18 +0000 | [diff] [blame] | 155 | pj_status_t status = PJ_SUCCESS; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 156 | |
| 157 | for (i=0; i<3; ++i) |
| 158 | sock[i] = PJ_INVALID_SOCKET; |
| 159 | |
Benny Prijono | fccab71 | 2006-02-22 22:23:22 +0000 | [diff] [blame] | 160 | /* Create and bind SIP UDP socket. */ |
| 161 | status = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, &sock[SIP_SOCK]); |
| 162 | if (status != PJ_SUCCESS) { |
| 163 | pjsua_perror(THIS_FILE, "socket() error", status); |
| 164 | goto on_error; |
| 165 | } |
| 166 | |
Benny Prijono | f04ffdd | 2006-02-21 00:11:18 +0000 | [diff] [blame] | 167 | if (sip) { |
Benny Prijono | fccab71 | 2006-02-22 22:23:22 +0000 | [diff] [blame] | 168 | status = pj_sock_bind_in(sock[SIP_SOCK], 0, pjsua.sip_port); |
Benny Prijono | f04ffdd | 2006-02-21 00:11:18 +0000 | [diff] [blame] | 169 | if (status != PJ_SUCCESS) { |
Benny Prijono | fccab71 | 2006-02-22 22:23:22 +0000 | [diff] [blame] | 170 | pjsua_perror(THIS_FILE, "bind() error", status); |
Benny Prijono | f04ffdd | 2006-02-21 00:11:18 +0000 | [diff] [blame] | 171 | goto on_error; |
| 172 | } |
Benny Prijono | fccab71 | 2006-02-22 22:23:22 +0000 | [diff] [blame] | 173 | } else { |
| 174 | status = pj_sock_bind_in(sock[SIP_SOCK], 0, 0); |
Benny Prijono | f04ffdd | 2006-02-21 00:11:18 +0000 | [diff] [blame] | 175 | if (status != PJ_SUCCESS) { |
| 176 | pjsua_perror(THIS_FILE, "bind() error", status); |
| 177 | goto on_error; |
| 178 | } |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 181 | |
| 182 | /* Loop retry to bind RTP and RTCP sockets. */ |
| 183 | for (i=0; i<RTP_RETRY; ++i, rtp_port += 2) { |
| 184 | |
| 185 | /* Create and bind RTP socket. */ |
| 186 | status = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, &sock[RTP_SOCK]); |
| 187 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 188 | pjsua_perror(THIS_FILE, "socket() error", status); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 189 | goto on_error; |
| 190 | } |
| 191 | |
| 192 | status = pj_sock_bind_in(sock[RTP_SOCK], 0, rtp_port); |
| 193 | if (status != PJ_SUCCESS) { |
| 194 | pj_sock_close(sock[RTP_SOCK]); |
| 195 | sock[RTP_SOCK] = PJ_INVALID_SOCKET; |
| 196 | continue; |
| 197 | } |
| 198 | |
| 199 | /* Create and bind RTCP socket. */ |
| 200 | status = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, &sock[RTCP_SOCK]); |
| 201 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 202 | pjsua_perror(THIS_FILE, "socket() error", status); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 203 | goto on_error; |
| 204 | } |
| 205 | |
| 206 | status = pj_sock_bind_in(sock[RTCP_SOCK], 0, (pj_uint16_t)(rtp_port+1)); |
| 207 | if (status != PJ_SUCCESS) { |
| 208 | pj_sock_close(sock[RTP_SOCK]); |
| 209 | sock[RTP_SOCK] = PJ_INVALID_SOCKET; |
| 210 | |
| 211 | pj_sock_close(sock[RTCP_SOCK]); |
| 212 | sock[RTCP_SOCK] = PJ_INVALID_SOCKET; |
| 213 | continue; |
| 214 | } |
| 215 | |
| 216 | /* |
| 217 | * If we're configured to use STUN, then find out the mapped address, |
| 218 | * and make sure that the mapped RTCP port is adjacent with the RTP. |
| 219 | */ |
| 220 | if (pjsua.stun_port1 == 0) { |
| 221 | const pj_str_t *hostname; |
| 222 | pj_sockaddr_in addr; |
| 223 | |
| 224 | /* Get local IP address. */ |
| 225 | hostname = pj_gethostname(); |
| 226 | |
| 227 | pj_memset( &addr, 0, sizeof(addr)); |
| 228 | addr.sin_family = PJ_AF_INET; |
| 229 | status = pj_sockaddr_in_set_str_addr( &addr, hostname); |
| 230 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 231 | pjsua_perror(THIS_FILE, "Unresolvable local hostname", |
| 232 | status); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 233 | goto on_error; |
| 234 | } |
| 235 | |
| 236 | for (i=0; i<3; ++i) |
| 237 | pj_memcpy(&mapped_addr[i], &addr, sizeof(addr)); |
| 238 | |
Benny Prijono | f04ffdd | 2006-02-21 00:11:18 +0000 | [diff] [blame] | 239 | if (sip) |
| 240 | mapped_addr[SIP_SOCK].sin_port = pj_htons((pj_uint16_t)pjsua.sip_port); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 241 | mapped_addr[RTP_SOCK].sin_port = pj_htons((pj_uint16_t)rtp_port); |
| 242 | mapped_addr[RTCP_SOCK].sin_port = pj_htons((pj_uint16_t)(rtp_port+1)); |
| 243 | break; |
| 244 | } else { |
| 245 | status = pj_stun_get_mapped_addr( &pjsua.cp.factory, 3, sock, |
| 246 | &pjsua.stun_srv1, pjsua.stun_port1, |
| 247 | &pjsua.stun_srv2, pjsua.stun_port2, |
| 248 | mapped_addr); |
| 249 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 250 | pjsua_perror(THIS_FILE, "STUN error", status); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 251 | goto on_error; |
| 252 | } |
| 253 | |
| 254 | if (pj_ntohs(mapped_addr[2].sin_port) == pj_ntohs(mapped_addr[1].sin_port)+1) |
| 255 | break; |
| 256 | |
| 257 | pj_sock_close(sock[RTP_SOCK]); sock[RTP_SOCK] = PJ_INVALID_SOCKET; |
| 258 | pj_sock_close(sock[RTCP_SOCK]); sock[RTCP_SOCK] = PJ_INVALID_SOCKET; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | if (sock[RTP_SOCK] == PJ_INVALID_SOCKET) { |
| 263 | PJ_LOG(1,(THIS_FILE, "Unable to find appropriate RTP/RTCP ports combination")); |
| 264 | goto on_error; |
| 265 | } |
| 266 | |
Benny Prijono | f04ffdd | 2006-02-21 00:11:18 +0000 | [diff] [blame] | 267 | if (sip) { |
| 268 | pjsua.sip_sock = sock[SIP_SOCK]; |
| 269 | pj_memcpy(&pjsua.sip_sock_name, &mapped_addr[SIP_SOCK], sizeof(pj_sockaddr_in)); |
Benny Prijono | fccab71 | 2006-02-22 22:23:22 +0000 | [diff] [blame] | 270 | } else { |
| 271 | pj_sock_close(sock[0]); |
Benny Prijono | f04ffdd | 2006-02-21 00:11:18 +0000 | [diff] [blame] | 272 | } |
Benny Prijono | 9519658 | 2006-02-09 00:13:40 +0000 | [diff] [blame] | 273 | |
Benny Prijono | f04ffdd | 2006-02-21 00:11:18 +0000 | [diff] [blame] | 274 | skinfo->rtp_sock = sock[RTP_SOCK]; |
| 275 | pj_memcpy(&skinfo->rtp_addr_name, |
Benny Prijono | 9519658 | 2006-02-09 00:13:40 +0000 | [diff] [blame] | 276 | &mapped_addr[RTP_SOCK], sizeof(pj_sockaddr_in)); |
| 277 | |
Benny Prijono | f04ffdd | 2006-02-21 00:11:18 +0000 | [diff] [blame] | 278 | skinfo->rtcp_sock = sock[RTCP_SOCK]; |
| 279 | pj_memcpy(&skinfo->rtcp_addr_name, |
Benny Prijono | 9519658 | 2006-02-09 00:13:40 +0000 | [diff] [blame] | 280 | &mapped_addr[RTCP_SOCK], sizeof(pj_sockaddr_in)); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 281 | |
Benny Prijono | f04ffdd | 2006-02-21 00:11:18 +0000 | [diff] [blame] | 282 | if (sip) { |
| 283 | PJ_LOG(4,(THIS_FILE, "SIP UDP socket reachable at %s:%d", |
| 284 | pj_inet_ntoa(pjsua.sip_sock_name.sin_addr), |
| 285 | pj_ntohs(pjsua.sip_sock_name.sin_port))); |
| 286 | } |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 287 | PJ_LOG(4,(THIS_FILE, "RTP socket reachable at %s:%d", |
Benny Prijono | f04ffdd | 2006-02-21 00:11:18 +0000 | [diff] [blame] | 288 | pj_inet_ntoa(skinfo->rtp_addr_name.sin_addr), |
| 289 | pj_ntohs(skinfo->rtp_addr_name.sin_port))); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 290 | PJ_LOG(4,(THIS_FILE, "RTCP UDP socket reachable at %s:%d", |
Benny Prijono | f04ffdd | 2006-02-21 00:11:18 +0000 | [diff] [blame] | 291 | pj_inet_ntoa(skinfo->rtcp_addr_name.sin_addr), |
| 292 | pj_ntohs(skinfo->rtcp_addr_name.sin_port))); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 293 | |
Benny Prijono | fccab71 | 2006-02-22 22:23:22 +0000 | [diff] [blame] | 294 | rtp_port += 2; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 295 | return PJ_SUCCESS; |
| 296 | |
| 297 | on_error: |
| 298 | for (i=0; i<3; ++i) { |
Benny Prijono | f04ffdd | 2006-02-21 00:11:18 +0000 | [diff] [blame] | 299 | if (sip && i==0) |
| 300 | continue; |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 301 | if (sock[i] != PJ_INVALID_SOCKET) |
| 302 | pj_sock_close(sock[i]); |
| 303 | } |
| 304 | return status; |
| 305 | } |
| 306 | |
| 307 | |
| 308 | |
| 309 | /* |
| 310 | * Initialize stack. |
| 311 | */ |
| 312 | static pj_status_t init_stack(void) |
| 313 | { |
| 314 | pj_status_t status; |
| 315 | |
| 316 | /* Create global endpoint: */ |
| 317 | |
| 318 | { |
| 319 | const pj_str_t *hostname; |
| 320 | const char *endpt_name; |
| 321 | |
| 322 | /* Endpoint MUST be assigned a globally unique name. |
| 323 | * The name will be used as the hostname in Warning header. |
| 324 | */ |
| 325 | |
| 326 | /* For this implementation, we'll use hostname for simplicity */ |
| 327 | hostname = pj_gethostname(); |
| 328 | endpt_name = hostname->ptr; |
| 329 | |
| 330 | /* Create the endpoint: */ |
| 331 | |
| 332 | status = pjsip_endpt_create(&pjsua.cp.factory, endpt_name, |
| 333 | &pjsua.endpt); |
| 334 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 335 | pjsua_perror(THIS_FILE, "Unable to create SIP endpoint", status); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 336 | return status; |
| 337 | } |
| 338 | } |
| 339 | |
| 340 | |
| 341 | /* Initialize transaction layer: */ |
| 342 | |
| 343 | status = pjsip_tsx_layer_init(pjsua.endpt); |
| 344 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 345 | pjsua_perror(THIS_FILE, "Transaction layer initialization error", |
| 346 | status); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 347 | goto on_error; |
| 348 | } |
| 349 | |
| 350 | /* Initialize UA layer module: */ |
| 351 | |
| 352 | status = pjsip_ua_init( pjsua.endpt, NULL ); |
| 353 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 354 | pjsua_perror(THIS_FILE, "UA layer initialization error", status); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 355 | goto on_error; |
| 356 | } |
| 357 | |
| 358 | /* Initialize and register pjsua's application module: */ |
| 359 | |
| 360 | { |
| 361 | pjsip_module my_mod = |
| 362 | { |
| 363 | NULL, NULL, /* prev, next. */ |
| 364 | { "mod-pjsua", 9 }, /* Name. */ |
| 365 | -1, /* Id */ |
| 366 | PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */ |
| 367 | NULL, /* User data. */ |
| 368 | NULL, /* load() */ |
| 369 | NULL, /* start() */ |
| 370 | NULL, /* stop() */ |
| 371 | NULL, /* unload() */ |
| 372 | &mod_pjsua_on_rx_request, /* on_rx_request() */ |
| 373 | &mod_pjsua_on_rx_response, /* on_rx_response() */ |
| 374 | NULL, /* on_tx_request. */ |
| 375 | NULL, /* on_tx_response() */ |
| 376 | NULL, /* on_tsx_state() */ |
| 377 | }; |
| 378 | |
| 379 | pjsua.mod = my_mod; |
| 380 | |
| 381 | status = pjsip_endpt_register_module(pjsua.endpt, &pjsua.mod); |
| 382 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 383 | pjsua_perror(THIS_FILE, "Unable to register pjsua module", |
| 384 | status); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 385 | goto on_error; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | /* Initialize invite session module: */ |
| 390 | |
| 391 | { |
| 392 | |
| 393 | /* Initialize invite session callback. */ |
| 394 | pjsip_inv_callback inv_cb; |
| 395 | |
| 396 | pj_memset(&inv_cb, 0, sizeof(inv_cb)); |
| 397 | inv_cb.on_state_changed = &pjsua_inv_on_state_changed; |
| 398 | inv_cb.on_new_session = &pjsua_inv_on_new_session; |
Benny Prijono | 9519658 | 2006-02-09 00:13:40 +0000 | [diff] [blame] | 399 | inv_cb.on_media_update = &pjsua_inv_on_media_update; |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 400 | inv_cb.on_rx_offer = &pjsua_inv_on_rx_offer; |
| 401 | inv_cb.on_tsx_state_changed = &pjsua_inv_on_tsx_state_changed; |
| 402 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 403 | |
| 404 | /* Initialize invite session module: */ |
| 405 | status = pjsip_inv_usage_init(pjsua.endpt, &pjsua.mod, &inv_cb); |
| 406 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 407 | pjsua_perror(THIS_FILE, "Invite usage initialization error", |
| 408 | status); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 409 | goto on_error; |
| 410 | } |
| 411 | |
| 412 | } |
| 413 | |
| 414 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 415 | /* Done */ |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 416 | |
| 417 | return PJ_SUCCESS; |
| 418 | |
| 419 | |
| 420 | on_error: |
| 421 | pjsip_endpt_destroy(pjsua.endpt); |
| 422 | pjsua.endpt = NULL; |
| 423 | return status; |
| 424 | } |
| 425 | |
| 426 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 427 | static int PJ_THREAD_FUNC pjsua_poll(void *arg) |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 428 | { |
| 429 | PJ_UNUSED_ARG(arg); |
| 430 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 431 | do { |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 432 | pj_time_val timeout = { 0, 10 }; |
| 433 | pjsip_endpt_handle_events (pjsua.endpt, &timeout); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 434 | } while (!pjsua.quit_flag); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 435 | |
| 436 | return 0; |
| 437 | } |
| 438 | |
| 439 | /* |
| 440 | * Initialize pjsua application. |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 441 | * This will initialize all libraries, create endpoint instance, and register |
| 442 | * pjsip modules. |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 443 | */ |
| 444 | pj_status_t pjsua_init(void) |
| 445 | { |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 446 | pj_status_t status; |
| 447 | |
| 448 | /* Init PJLIB logging: */ |
| 449 | |
| 450 | pj_log_set_level(pjsua.log_level); |
| 451 | pj_log_set_decor(pjsua.log_decor); |
| 452 | |
| 453 | |
| 454 | /* Init PJLIB: */ |
| 455 | |
| 456 | status = pj_init(); |
| 457 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 458 | pjsua_perror(THIS_FILE, "pj_init() error", status); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 459 | return status; |
| 460 | } |
| 461 | |
Benny Prijono | fccab71 | 2006-02-22 22:23:22 +0000 | [diff] [blame] | 462 | /* Init PJLIB-UTIL: */ |
| 463 | |
| 464 | status = pjlib_util_init(); |
| 465 | if (status != PJ_SUCCESS) { |
| 466 | pjsua_perror(THIS_FILE, "pjlib_util_init() error", status); |
| 467 | return status; |
| 468 | } |
| 469 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 470 | /* Init memory pool: */ |
| 471 | |
| 472 | /* Init caching pool. */ |
| 473 | pj_caching_pool_init(&pjsua.cp, &pj_pool_factory_default_policy, 0); |
| 474 | |
| 475 | /* Create memory pool for application. */ |
| 476 | pjsua.pool = pj_pool_create(&pjsua.cp.factory, "pjsua", 4000, 4000, NULL); |
| 477 | |
| 478 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 479 | /* Init PJSIP : */ |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 480 | |
| 481 | status = init_stack(); |
| 482 | if (status != PJ_SUCCESS) { |
| 483 | pj_caching_pool_destroy(&pjsua.cp); |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 484 | pjsua_perror(THIS_FILE, "Stack initialization has returned error", |
| 485 | status); |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 486 | return status; |
| 487 | } |
| 488 | |
Benny Prijono | 9519658 | 2006-02-09 00:13:40 +0000 | [diff] [blame] | 489 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 490 | /* Init core SIMPLE module : */ |
| 491 | |
| 492 | pjsip_evsub_init_module(pjsua.endpt); |
| 493 | |
| 494 | /* Init presence module: */ |
| 495 | |
| 496 | pjsip_pres_init_module( pjsua.endpt, pjsip_evsub_instance()); |
| 497 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 498 | /* Init xfer/REFER module */ |
| 499 | |
| 500 | pjsip_xfer_init_module( pjsua.endpt ); |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 501 | |
| 502 | /* Init pjsua presence handler: */ |
| 503 | |
| 504 | pjsua_pres_init(); |
| 505 | |
| 506 | |
Benny Prijono | 9519658 | 2006-02-09 00:13:40 +0000 | [diff] [blame] | 507 | /* Init media endpoint: */ |
| 508 | |
| 509 | status = pjmedia_endpt_create(&pjsua.cp.factory, &pjsua.med_endpt); |
| 510 | if (status != PJ_SUCCESS) { |
| 511 | pj_caching_pool_destroy(&pjsua.cp); |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 512 | pjsua_perror(THIS_FILE, |
| 513 | "Media stack initialization has returned error", |
| 514 | status); |
Benny Prijono | 9519658 | 2006-02-09 00:13:40 +0000 | [diff] [blame] | 515 | return status; |
| 516 | } |
| 517 | |
Benny Prijono | f04ffdd | 2006-02-21 00:11:18 +0000 | [diff] [blame] | 518 | /* Init conference bridge. */ |
| 519 | |
| 520 | status = pjmedia_conf_create(pjsua.pool, 8, 8000, 160, 16, &pjsua.mconf); |
| 521 | if (status != PJ_SUCCESS) { |
| 522 | pj_caching_pool_destroy(&pjsua.cp); |
| 523 | pjsua_perror(THIS_FILE, |
| 524 | "Media stack initialization has returned error", |
| 525 | status); |
| 526 | return status; |
| 527 | } |
| 528 | |
Benny Prijono | 1f9afba | 2006-02-10 15:57:32 +0000 | [diff] [blame] | 529 | /* Init pjmedia-codecs: */ |
| 530 | |
| 531 | status = pjmedia_codec_init(pjsua.med_endpt); |
| 532 | if (status != PJ_SUCCESS) { |
| 533 | pj_caching_pool_destroy(&pjsua.cp); |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 534 | pjsua_perror(THIS_FILE, |
| 535 | "Media codec initialization has returned error", |
| 536 | status); |
Benny Prijono | 1f9afba | 2006-02-10 15:57:32 +0000 | [diff] [blame] | 537 | return status; |
| 538 | } |
| 539 | |
| 540 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 541 | /* Done. */ |
| 542 | return PJ_SUCCESS; |
| 543 | } |
| 544 | |
| 545 | |
| 546 | |
| 547 | /* |
| 548 | * Start pjsua stack. |
| 549 | * This will start the registration process, if registration is configured. |
| 550 | */ |
| 551 | pj_status_t pjsua_start(void) |
| 552 | { |
| 553 | int i; /* Must be signed */ |
| 554 | pjsip_transport *udp_transport; |
Benny Prijono | f04ffdd | 2006-02-21 00:11:18 +0000 | [diff] [blame] | 555 | pj_status_t status = PJ_SUCCESS; |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 556 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 557 | /* Init sockets (STUN etc): */ |
Benny Prijono | f04ffdd | 2006-02-21 00:11:18 +0000 | [diff] [blame] | 558 | for (i=0; i<PJ_ARRAY_SIZE(pjsua.med_sock_info); ++i) { |
| 559 | status = init_sockets(i==0, &pjsua.med_sock_info[i]); |
| 560 | if (status != PJ_SUCCESS) { |
| 561 | pjsua_perror(THIS_FILE, "init_sockets() has returned error", |
| 562 | status); |
| 563 | return status; |
| 564 | } |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 565 | } |
| 566 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 567 | /* Add UDP transport: */ |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 568 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 569 | { |
| 570 | /* Init the published name for the transport. |
| 571 | * Depending whether STUN is used, this may be the STUN mapped |
| 572 | * address, or socket's bound address. |
| 573 | */ |
| 574 | pjsip_host_port addr_name; |
| 575 | |
| 576 | 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] | 577 | addr_name.host.slen = pj_ansi_strlen(addr_name.host.ptr); |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 578 | addr_name.port = pj_ntohs(pjsua.sip_sock_name.sin_port); |
| 579 | |
| 580 | /* Create UDP transport from previously created UDP socket: */ |
| 581 | |
| 582 | status = pjsip_udp_transport_attach( pjsua.endpt, pjsua.sip_sock, |
| 583 | &addr_name, 1, |
| 584 | &udp_transport); |
| 585 | if (status != PJ_SUCCESS) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 586 | pjsua_perror(THIS_FILE, "Unable to start UDP transport", |
| 587 | status); |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 588 | return status; |
| 589 | } |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 590 | } |
| 591 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 592 | /* Initialize Contact URI, if one is not specified: */ |
| 593 | |
| 594 | if (pjsua.contact_uri.slen == 0 && pjsua.local_uri.slen) { |
| 595 | |
| 596 | pjsip_uri *uri; |
| 597 | pjsip_sip_uri *sip_uri; |
| 598 | char contact[128]; |
| 599 | int len; |
| 600 | |
| 601 | /* The local Contact is the username@ip-addr, where |
| 602 | * - username is taken from the local URI, |
| 603 | * - ip-addr in UDP transport's address name (which may have been |
| 604 | * resolved from STUN. |
| 605 | */ |
| 606 | |
| 607 | /* Need to parse local_uri to get the elements: */ |
| 608 | |
| 609 | uri = pjsip_parse_uri(pjsua.pool, pjsua.local_uri.ptr, |
| 610 | pjsua.local_uri.slen, 0); |
| 611 | if (uri == NULL) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 612 | pjsua_perror(THIS_FILE, "Invalid local URI", |
| 613 | PJSIP_EINVALIDURI); |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 614 | return PJSIP_EINVALIDURI; |
| 615 | } |
| 616 | |
| 617 | |
| 618 | /* Local URI MUST be a SIP or SIPS: */ |
| 619 | |
| 620 | if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri)) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 621 | pjsua_perror(THIS_FILE, "Invalid local URI", |
| 622 | PJSIP_EINVALIDSCHEME); |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 623 | return PJSIP_EINVALIDSCHEME; |
| 624 | } |
| 625 | |
| 626 | |
| 627 | /* Get the SIP URI object: */ |
| 628 | |
| 629 | sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri); |
| 630 | |
| 631 | |
| 632 | /* Build temporary contact string. */ |
| 633 | |
| 634 | if (sip_uri->user.slen) { |
| 635 | |
| 636 | /* With the user part. */ |
| 637 | len = pj_snprintf(contact, sizeof(contact), |
| 638 | "<sip:%.*s@%.*s:%d>", |
Benny Prijono | 34a404e | 2006-02-09 14:38:30 +0000 | [diff] [blame] | 639 | (int)sip_uri->user.slen, |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 640 | sip_uri->user.ptr, |
Benny Prijono | 34a404e | 2006-02-09 14:38:30 +0000 | [diff] [blame] | 641 | (int)udp_transport->local_name.host.slen, |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 642 | udp_transport->local_name.host.ptr, |
| 643 | udp_transport->local_name.port); |
| 644 | } else { |
| 645 | |
| 646 | /* Without user part */ |
| 647 | |
| 648 | len = pj_snprintf(contact, sizeof(contact), |
| 649 | "<sip:%.*s:%d>", |
Benny Prijono | 34a404e | 2006-02-09 14:38:30 +0000 | [diff] [blame] | 650 | (int)udp_transport->local_name.host.slen, |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 651 | udp_transport->local_name.host.ptr, |
| 652 | udp_transport->local_name.port); |
| 653 | } |
| 654 | |
| 655 | if (len < 1 || len >= sizeof(contact)) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 656 | pjsua_perror(THIS_FILE, "Invalid Contact", PJSIP_EURITOOLONG); |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 657 | return PJSIP_EURITOOLONG; |
| 658 | } |
| 659 | |
| 660 | /* Duplicate Contact uri. */ |
| 661 | |
| 662 | pj_strdup2(pjsua.pool, &pjsua.contact_uri, contact); |
| 663 | |
| 664 | } |
| 665 | |
Benny Prijono | 9519658 | 2006-02-09 00:13:40 +0000 | [diff] [blame] | 666 | /* If outbound_proxy is specified, put it in the route_set: */ |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 667 | |
Benny Prijono | 9519658 | 2006-02-09 00:13:40 +0000 | [diff] [blame] | 668 | if (pjsua.outbound_proxy.slen) { |
| 669 | |
| 670 | pjsip_route_hdr *route; |
| 671 | const pj_str_t hname = { "Route", 5 }; |
| 672 | int parsed_len; |
| 673 | |
| 674 | route = pjsip_parse_hdr( pjsua.pool, &hname, |
| 675 | pjsua.outbound_proxy.ptr, |
| 676 | pjsua.outbound_proxy.slen, |
| 677 | &parsed_len); |
| 678 | if (route == NULL) { |
Benny Prijono | bcaed6c | 2006-02-19 15:37:19 +0000 | [diff] [blame] | 679 | pjsua_perror(THIS_FILE, "Invalid outbound proxy URL", |
| 680 | PJSIP_EINVALIDURI); |
Benny Prijono | 9519658 | 2006-02-09 00:13:40 +0000 | [diff] [blame] | 681 | return PJSIP_EINVALIDURI; |
| 682 | } |
| 683 | |
| 684 | pj_list_push_back(&pjsua.route_set, route); |
| 685 | } |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 686 | |
| 687 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 688 | /* Create worker thread(s), if required: */ |
| 689 | |
| 690 | for (i=0; i<pjsua.thread_cnt; ++i) { |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 691 | status = pj_thread_create( pjsua.pool, "pjsua", &pjsua_poll, |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 692 | NULL, 0, 0, &pjsua.threads[i]); |
| 693 | if (status != PJ_SUCCESS) { |
| 694 | pjsua.quit_flag = 1; |
| 695 | for (--i; i>=0; --i) { |
| 696 | pj_thread_join(pjsua.threads[i]); |
| 697 | pj_thread_destroy(pjsua.threads[i]); |
| 698 | } |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 699 | return status; |
| 700 | } |
| 701 | } |
| 702 | |
Benny Prijono | ccf9562 | 2006-02-07 18:48:01 +0000 | [diff] [blame] | 703 | /* Start registration: */ |
| 704 | |
| 705 | /* Create client registration session: */ |
| 706 | |
| 707 | status = pjsua_regc_init(); |
| 708 | if (status != PJ_SUCCESS) |
| 709 | return status; |
| 710 | |
| 711 | /* Perform registration, if required. */ |
| 712 | if (pjsua.regc) { |
| 713 | pjsua_regc_update(1); |
| 714 | } |
| 715 | |
| 716 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 717 | PJ_LOG(3,(THIS_FILE, "PJSUA version %s started", PJ_VERSION)); |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 718 | return PJ_SUCCESS; |
| 719 | } |
| 720 | |
| 721 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 722 | /* Sleep with polling */ |
| 723 | static void busy_sleep(unsigned msec) |
| 724 | { |
| 725 | pj_time_val timeout, now; |
| 726 | |
| 727 | pj_gettimeofday(&timeout); |
| 728 | timeout.msec += msec; |
| 729 | pj_time_val_normalize(&timeout); |
| 730 | |
| 731 | do { |
| 732 | pjsua_poll(NULL); |
| 733 | pj_gettimeofday(&now); |
| 734 | } while (PJ_TIME_VAL_LT(now, timeout)); |
| 735 | } |
| 736 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 737 | /* |
| 738 | * Destroy pjsua. |
| 739 | */ |
| 740 | pj_status_t pjsua_destroy(void) |
| 741 | { |
| 742 | int i; |
| 743 | |
| 744 | /* Signal threads to quit: */ |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 745 | pjsua.quit_flag = 1; |
| 746 | |
| 747 | /* Wait worker threads to quit: */ |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 748 | for (i=0; i<pjsua.thread_cnt; ++i) { |
| 749 | |
Benny Prijono | e4f2abb | 2006-02-10 14:04:05 +0000 | [diff] [blame] | 750 | if (pjsua.threads[i]) { |
| 751 | pj_thread_join(pjsua.threads[i]); |
| 752 | pj_thread_destroy(pjsua.threads[i]); |
| 753 | pjsua.threads[i] = NULL; |
| 754 | } |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 755 | } |
| 756 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 757 | |
| 758 | /* Terminate all calls. */ |
| 759 | pjsua_inv_shutdown(); |
| 760 | |
| 761 | /* Terminate all presence subscriptions. */ |
| 762 | pjsua_pres_shutdown(); |
| 763 | |
| 764 | /* Unregister, if required: */ |
| 765 | if (pjsua.regc) { |
| 766 | pjsua_regc_update(0); |
| 767 | } |
| 768 | |
| 769 | /* Wait for some time to allow unregistration to complete: */ |
| 770 | PJ_LOG(4,(THIS_FILE, "Shutting down...")); |
| 771 | busy_sleep(1000); |
| 772 | |
Benny Prijono | 26ff906 | 2006-02-21 23:47:00 +0000 | [diff] [blame] | 773 | /* Destroy conference bridge. */ |
| 774 | if (pjsua.mconf) |
| 775 | pjmedia_conf_destroy(pjsua.mconf); |
| 776 | |
Benny Prijono | 834aee3 | 2006-02-19 01:38:06 +0000 | [diff] [blame] | 777 | /* Shutdown pjmedia-codec: */ |
| 778 | pjmedia_codec_deinit(); |
| 779 | |
| 780 | /* Destroy sound framework: |
| 781 | * (this should be done in pjmedia_shutdown()) |
| 782 | */ |
| 783 | pj_snd_deinit(); |
| 784 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 785 | /* Destroy endpoint. */ |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 786 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 787 | pjsip_endpt_destroy(pjsua.endpt); |
| 788 | pjsua.endpt = NULL; |
| 789 | |
| 790 | /* Destroy caching pool. */ |
Benny Prijono | 84126ab | 2006-02-09 09:30:09 +0000 | [diff] [blame] | 791 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 792 | pj_caching_pool_destroy(&pjsua.cp); |
| 793 | |
| 794 | |
| 795 | /* Done. */ |
| 796 | |
| 797 | return PJ_SUCCESS; |
| 798 | } |
| 799 | |