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