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