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