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