blob: 1dd4ca08aa95add4511e8585c95af6afe4bc6168 [file] [log] [blame]
Benny Prijono268ca612006-02-07 12:34:11 +00001/* $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 Prijono4f9f64e2006-02-27 00:00:30 +000019#include <pjsua-lib/pjsua.h>
Benny Prijono268ca612006-02-07 12:34:11 +000020
Benny Prijono84126ab2006-02-09 09:30:09 +000021/*
22 * pjsua_core.c
23 *
24 * Core application functionalities.
25 */
Benny Prijono268ca612006-02-07 12:34:11 +000026
Benny Prijono84126ab2006-02-09 09:30:09 +000027#define THIS_FILE "pjsua_core.c"
Benny Prijono268ca612006-02-07 12:34:11 +000028
29
Benny Prijono95196582006-02-09 00:13:40 +000030/*
Benny Prijono84126ab2006-02-09 09:30:09 +000031 * Global variable.
32 */
33struct pjsua pjsua;
34
35
36/*
37 * Default local URI, if none is specified in cmd-line
Benny Prijono95196582006-02-09 00:13:40 +000038 */
Benny Prijonoccf95622006-02-07 18:48:01 +000039#define PJSUA_LOCAL_URI "<sip:user@127.0.0.1>"
Benny Prijono268ca612006-02-07 12:34:11 +000040
Benny Prijono268ca612006-02-07 12:34:11 +000041
Benny Prijono268ca612006-02-07 12:34:11 +000042
43/*
44 * Init default application parameters.
45 */
46void pjsua_default(void)
47{
Benny Prijonoa91a0032006-02-26 21:23:45 +000048 unsigned i;
49
Benny Prijono268ca612006-02-07 12:34:11 +000050
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 Prijono268ca612006-02-07 12:34:11 +000058 pjsua.sip_port = 5060;
59
60
Benny Prijonocbf37402006-03-01 19:29:10 +000061 /* Default we start RTP at port 4000 */
62 pjsua.start_rtp_port = 4000;
63
64
Benny Prijono268ca612006-02-07 12:34:11 +000065 /* Default logging settings: */
Benny Prijono268ca612006-02-07 12:34:11 +000066 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 Prijono268ca612006-02-07 12:34:11 +000071
Benny Prijono105217f2006-03-06 16:25:59 +000072 /* Default call settings. */
73 pjsua.uas_refresh = -1;
74 pjsua.uas_duration = -1;
75
Benny Prijonoa91a0032006-02-26 21:23:45 +000076 /* Default: do not use STUN: */
Benny Prijono268ca612006-02-07 12:34:11 +000077 pjsua.stun_port1 = pjsua.stun_port2 = 0;
Benny Prijonoccf95622006-02-07 18:48:01 +000078
Benny Prijono1c2bf462006-03-05 11:54:02 +000079 /* Default for media: */
Benny Prijono08e0d062006-03-04 14:52:44 +000080 pjsua.clock_rate = 8000;
Benny Prijonoccb03fa2006-03-06 13:35:47 +000081 pjsua.complexity = -1;
Benny Prijono1c2bf462006-03-05 11:54:02 +000082 pjsua.quality = 4;
83
Benny Prijono08e0d062006-03-04 14:52:44 +000084
Benny Prijonoa91a0032006-02-26 21:23:45 +000085 /* Init accounts: */
86 pjsua.acc_cnt = 1;
87 for (i=0; i<PJ_ARRAY_SIZE(pjsua.acc); ++i) {
88 pjsua.acc[i].index = i;
89 pjsua.acc[i].local_uri = pj_str(PJSUA_LOCAL_URI);
90 pjsua.acc[i].reg_timeout = 55;
Benny Prijonoccb03fa2006-03-06 13:35:47 +000091 pjsua.acc[i].online_status = PJ_TRUE;
Benny Prijonoa91a0032006-02-26 21:23:45 +000092 pj_list_init(&pjsua.acc[i].route_set);
93 pj_list_init(&pjsua.acc[i].pres_srv_list);
94 }
Benny Prijonoccf95622006-02-07 18:48:01 +000095
Benny Prijonoa91a0032006-02-26 21:23:45 +000096 /* Init call array: */
Benny Prijono105217f2006-03-06 16:25:59 +000097 for (i=0; i<PJ_ARRAY_SIZE(pjsua.calls); ++i) {
Benny Prijonoa91a0032006-02-26 21:23:45 +000098 pjsua.calls[i].index = i;
Benny Prijono105217f2006-03-06 16:25:59 +000099 pjsua.calls[i].refresh_tm._timer_id = -1;
100 pjsua.calls[i].hangup_tm._timer_id = -1;
101 }
Benny Prijono95196582006-02-09 00:13:40 +0000102
Benny Prijonoa91a0032006-02-26 21:23:45 +0000103 /* Default max nb of calls. */
104 pjsua.max_calls = 4;
Benny Prijono834aee32006-02-19 01:38:06 +0000105
106 /* Init server presence subscription list: */
107
Benny Prijono834aee32006-02-19 01:38:06 +0000108
Benny Prijono268ca612006-02-07 12:34:11 +0000109}
110
111
Benny Prijono268ca612006-02-07 12:34:11 +0000112
113/*
114 * Handler for receiving incoming requests.
115 *
116 * This handler serves multiple purposes:
117 * - it receives requests outside dialogs.
118 * - it receives requests inside dialogs, when the requests are
119 * unhandled by other dialog usages. Example of these
120 * requests are: MESSAGE.
121 */
122static pj_bool_t mod_pjsua_on_rx_request(pjsip_rx_data *rdata)
123{
Benny Prijono38998232006-02-08 22:44:25 +0000124
Benny Prijono84126ab2006-02-09 09:30:09 +0000125 if (rdata->msg_info.msg->line.req.method.id == PJSIP_INVITE_METHOD) {
Benny Prijono38998232006-02-08 22:44:25 +0000126
Benny Prijonoa91a0032006-02-26 21:23:45 +0000127 return pjsua_call_on_incoming(rdata);
Benny Prijono38998232006-02-08 22:44:25 +0000128 }
129
Benny Prijono268ca612006-02-07 12:34:11 +0000130 return PJ_FALSE;
131}
132
133
134/*
135 * Handler for receiving incoming responses.
136 *
137 * This handler serves multiple purposes:
138 * - it receives strayed responses (i.e. outside any dialog and
139 * outside any transactions).
140 * - it receives responses coming to a transaction, when pjsua
141 * module is set as transaction user for the transaction.
142 * - it receives responses inside a dialog, when these responses
143 * are unhandled by other dialog usages.
144 */
145static pj_bool_t mod_pjsua_on_rx_response(pjsip_rx_data *rdata)
146{
147 PJ_UNUSED_ARG(rdata);
Benny Prijono268ca612006-02-07 12:34:11 +0000148 return PJ_FALSE;
149}
150
151
Benny Prijono268ca612006-02-07 12:34:11 +0000152/*
153 * Initialize sockets and optionally get the public address via STUN.
154 */
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000155static pj_status_t init_sockets(pj_bool_t sip,
156 pjmedia_sock_info *skinfo)
Benny Prijono268ca612006-02-07 12:34:11 +0000157{
158 enum {
Benny Prijonoa91a0032006-02-26 21:23:45 +0000159 RTP_RETRY = 100
Benny Prijono268ca612006-02-07 12:34:11 +0000160 };
161 enum {
162 SIP_SOCK,
163 RTP_SOCK,
164 RTCP_SOCK,
165 };
166 int i;
Benny Prijonocbf37402006-03-01 19:29:10 +0000167 static pj_uint16_t rtp_port;
Benny Prijono268ca612006-02-07 12:34:11 +0000168 pj_sock_t sock[3];
169 pj_sockaddr_in mapped_addr[3];
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000170 pj_status_t status = PJ_SUCCESS;
Benny Prijono268ca612006-02-07 12:34:11 +0000171
Benny Prijonocbf37402006-03-01 19:29:10 +0000172 if (rtp_port == 0)
173 rtp_port = (pj_uint16_t)pjsua.start_rtp_port;
174
Benny Prijono268ca612006-02-07 12:34:11 +0000175 for (i=0; i<3; ++i)
176 sock[i] = PJ_INVALID_SOCKET;
177
Benny Prijonofccab712006-02-22 22:23:22 +0000178 /* Create and bind SIP UDP socket. */
179 status = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, &sock[SIP_SOCK]);
180 if (status != PJ_SUCCESS) {
181 pjsua_perror(THIS_FILE, "socket() error", status);
182 goto on_error;
183 }
184
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000185 if (sip) {
Benny Prijonofccab712006-02-22 22:23:22 +0000186 status = pj_sock_bind_in(sock[SIP_SOCK], 0, pjsua.sip_port);
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000187 if (status != PJ_SUCCESS) {
Benny Prijonofccab712006-02-22 22:23:22 +0000188 pjsua_perror(THIS_FILE, "bind() error", status);
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000189 goto on_error;
190 }
Benny Prijonofccab712006-02-22 22:23:22 +0000191 } else {
192 status = pj_sock_bind_in(sock[SIP_SOCK], 0, 0);
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000193 if (status != PJ_SUCCESS) {
194 pjsua_perror(THIS_FILE, "bind() error", status);
195 goto on_error;
196 }
Benny Prijono268ca612006-02-07 12:34:11 +0000197 }
198
Benny Prijono268ca612006-02-07 12:34:11 +0000199
200 /* Loop retry to bind RTP and RTCP sockets. */
201 for (i=0; i<RTP_RETRY; ++i, rtp_port += 2) {
202
203 /* Create and bind RTP socket. */
204 status = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, &sock[RTP_SOCK]);
205 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000206 pjsua_perror(THIS_FILE, "socket() error", status);
Benny Prijono268ca612006-02-07 12:34:11 +0000207 goto on_error;
208 }
209
210 status = pj_sock_bind_in(sock[RTP_SOCK], 0, rtp_port);
211 if (status != PJ_SUCCESS) {
212 pj_sock_close(sock[RTP_SOCK]);
213 sock[RTP_SOCK] = PJ_INVALID_SOCKET;
214 continue;
215 }
216
217 /* Create and bind RTCP socket. */
218 status = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, &sock[RTCP_SOCK]);
219 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000220 pjsua_perror(THIS_FILE, "socket() error", status);
Benny Prijono268ca612006-02-07 12:34:11 +0000221 goto on_error;
222 }
223
224 status = pj_sock_bind_in(sock[RTCP_SOCK], 0, (pj_uint16_t)(rtp_port+1));
225 if (status != PJ_SUCCESS) {
226 pj_sock_close(sock[RTP_SOCK]);
227 sock[RTP_SOCK] = PJ_INVALID_SOCKET;
228
229 pj_sock_close(sock[RTCP_SOCK]);
230 sock[RTCP_SOCK] = PJ_INVALID_SOCKET;
231 continue;
232 }
233
234 /*
235 * If we're configured to use STUN, then find out the mapped address,
236 * and make sure that the mapped RTCP port is adjacent with the RTP.
237 */
238 if (pjsua.stun_port1 == 0) {
239 const pj_str_t *hostname;
240 pj_sockaddr_in addr;
241
242 /* Get local IP address. */
243 hostname = pj_gethostname();
244
245 pj_memset( &addr, 0, sizeof(addr));
246 addr.sin_family = PJ_AF_INET;
247 status = pj_sockaddr_in_set_str_addr( &addr, hostname);
248 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000249 pjsua_perror(THIS_FILE, "Unresolvable local hostname",
250 status);
Benny Prijono268ca612006-02-07 12:34:11 +0000251 goto on_error;
252 }
253
254 for (i=0; i<3; ++i)
255 pj_memcpy(&mapped_addr[i], &addr, sizeof(addr));
256
Benny Prijonoa91a0032006-02-26 21:23:45 +0000257 if (sip) {
258 mapped_addr[SIP_SOCK].sin_port =
259 pj_htons((pj_uint16_t)pjsua.sip_port);
260 }
261 mapped_addr[RTP_SOCK].sin_port=pj_htons((pj_uint16_t)rtp_port);
262 mapped_addr[RTCP_SOCK].sin_port=pj_htons((pj_uint16_t)(rtp_port+1));
Benny Prijono268ca612006-02-07 12:34:11 +0000263 break;
Benny Prijonoa91a0032006-02-26 21:23:45 +0000264
Benny Prijono268ca612006-02-07 12:34:11 +0000265 } else {
Benny Prijonoa91a0032006-02-26 21:23:45 +0000266 status=pj_stun_get_mapped_addr(&pjsua.cp.factory, 3, sock,
267 &pjsua.stun_srv1, pjsua.stun_port1,
268 &pjsua.stun_srv2, pjsua.stun_port2,
269 mapped_addr);
Benny Prijono268ca612006-02-07 12:34:11 +0000270 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000271 pjsua_perror(THIS_FILE, "STUN error", status);
Benny Prijono268ca612006-02-07 12:34:11 +0000272 goto on_error;
273 }
274
Benny Prijonoa91a0032006-02-26 21:23:45 +0000275 if (pj_ntohs(mapped_addr[2].sin_port) ==
276 pj_ntohs(mapped_addr[1].sin_port)+1)
277 {
Benny Prijono268ca612006-02-07 12:34:11 +0000278 break;
Benny Prijonoa91a0032006-02-26 21:23:45 +0000279 }
Benny Prijono268ca612006-02-07 12:34:11 +0000280
Benny Prijonoa91a0032006-02-26 21:23:45 +0000281 pj_sock_close(sock[RTP_SOCK]);
282 sock[RTP_SOCK] = PJ_INVALID_SOCKET;
283
284 pj_sock_close(sock[RTCP_SOCK]);
285 sock[RTCP_SOCK] = PJ_INVALID_SOCKET;
Benny Prijono268ca612006-02-07 12:34:11 +0000286 }
287 }
288
289 if (sock[RTP_SOCK] == PJ_INVALID_SOCKET) {
Benny Prijonoa91a0032006-02-26 21:23:45 +0000290 PJ_LOG(1,(THIS_FILE,
291 "Unable to find appropriate RTP/RTCP ports combination"));
Benny Prijono268ca612006-02-07 12:34:11 +0000292 goto on_error;
293 }
294
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000295 if (sip) {
296 pjsua.sip_sock = sock[SIP_SOCK];
Benny Prijonoa91a0032006-02-26 21:23:45 +0000297 pj_memcpy(&pjsua.sip_sock_name,
298 &mapped_addr[SIP_SOCK],
299 sizeof(pj_sockaddr_in));
Benny Prijonofccab712006-02-22 22:23:22 +0000300 } else {
301 pj_sock_close(sock[0]);
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000302 }
Benny Prijono95196582006-02-09 00:13:40 +0000303
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000304 skinfo->rtp_sock = sock[RTP_SOCK];
305 pj_memcpy(&skinfo->rtp_addr_name,
Benny Prijono95196582006-02-09 00:13:40 +0000306 &mapped_addr[RTP_SOCK], sizeof(pj_sockaddr_in));
307
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000308 skinfo->rtcp_sock = sock[RTCP_SOCK];
309 pj_memcpy(&skinfo->rtcp_addr_name,
Benny Prijono95196582006-02-09 00:13:40 +0000310 &mapped_addr[RTCP_SOCK], sizeof(pj_sockaddr_in));
Benny Prijono268ca612006-02-07 12:34:11 +0000311
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000312 if (sip) {
313 PJ_LOG(4,(THIS_FILE, "SIP UDP socket reachable at %s:%d",
314 pj_inet_ntoa(pjsua.sip_sock_name.sin_addr),
315 pj_ntohs(pjsua.sip_sock_name.sin_port)));
316 }
Benny Prijono268ca612006-02-07 12:34:11 +0000317 PJ_LOG(4,(THIS_FILE, "RTP socket reachable at %s:%d",
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000318 pj_inet_ntoa(skinfo->rtp_addr_name.sin_addr),
319 pj_ntohs(skinfo->rtp_addr_name.sin_port)));
Benny Prijono268ca612006-02-07 12:34:11 +0000320 PJ_LOG(4,(THIS_FILE, "RTCP UDP socket reachable at %s:%d",
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000321 pj_inet_ntoa(skinfo->rtcp_addr_name.sin_addr),
322 pj_ntohs(skinfo->rtcp_addr_name.sin_port)));
Benny Prijono268ca612006-02-07 12:34:11 +0000323
Benny Prijonofccab712006-02-22 22:23:22 +0000324 rtp_port += 2;
Benny Prijono268ca612006-02-07 12:34:11 +0000325 return PJ_SUCCESS;
326
327on_error:
328 for (i=0; i<3; ++i) {
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000329 if (sip && i==0)
330 continue;
Benny Prijono268ca612006-02-07 12:34:11 +0000331 if (sock[i] != PJ_INVALID_SOCKET)
332 pj_sock_close(sock[i]);
333 }
334 return status;
335}
336
337
338
339/*
340 * Initialize stack.
341 */
342static pj_status_t init_stack(void)
343{
344 pj_status_t status;
345
346 /* Create global endpoint: */
347
348 {
349 const pj_str_t *hostname;
350 const char *endpt_name;
351
352 /* Endpoint MUST be assigned a globally unique name.
353 * The name will be used as the hostname in Warning header.
354 */
355
356 /* For this implementation, we'll use hostname for simplicity */
357 hostname = pj_gethostname();
358 endpt_name = hostname->ptr;
359
360 /* Create the endpoint: */
361
362 status = pjsip_endpt_create(&pjsua.cp.factory, endpt_name,
363 &pjsua.endpt);
364 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000365 pjsua_perror(THIS_FILE, "Unable to create SIP endpoint", status);
Benny Prijono268ca612006-02-07 12:34:11 +0000366 return status;
367 }
368 }
369
370
371 /* Initialize transaction layer: */
372
Benny Prijono2f8992b2006-02-25 21:16:36 +0000373 status = pjsip_tsx_layer_init_module(pjsua.endpt);
Benny Prijono268ca612006-02-07 12:34:11 +0000374 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000375 pjsua_perror(THIS_FILE, "Transaction layer initialization error",
376 status);
Benny Prijono268ca612006-02-07 12:34:11 +0000377 goto on_error;
378 }
379
380 /* Initialize UA layer module: */
381
Benny Prijono2f8992b2006-02-25 21:16:36 +0000382 status = pjsip_ua_init_module( pjsua.endpt, NULL );
Benny Prijono268ca612006-02-07 12:34:11 +0000383 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000384 pjsua_perror(THIS_FILE, "UA layer initialization error", status);
Benny Prijono268ca612006-02-07 12:34:11 +0000385 goto on_error;
386 }
387
388 /* Initialize and register pjsua's application module: */
389
390 {
391 pjsip_module my_mod =
392 {
393 NULL, NULL, /* prev, next. */
394 { "mod-pjsua", 9 }, /* Name. */
395 -1, /* Id */
396 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
Benny Prijono268ca612006-02-07 12:34:11 +0000397 NULL, /* load() */
398 NULL, /* start() */
399 NULL, /* stop() */
400 NULL, /* unload() */
401 &mod_pjsua_on_rx_request, /* on_rx_request() */
402 &mod_pjsua_on_rx_response, /* on_rx_response() */
403 NULL, /* on_tx_request. */
404 NULL, /* on_tx_response() */
405 NULL, /* on_tsx_state() */
406 };
407
408 pjsua.mod = my_mod;
409
410 status = pjsip_endpt_register_module(pjsua.endpt, &pjsua.mod);
411 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000412 pjsua_perror(THIS_FILE, "Unable to register pjsua module",
413 status);
Benny Prijono268ca612006-02-07 12:34:11 +0000414 goto on_error;
415 }
416 }
417
418 /* Initialize invite session module: */
419
Benny Prijonoa91a0032006-02-26 21:23:45 +0000420 status = pjsua_call_init();
421 if (status != PJ_SUCCESS) {
422 pjsua_perror(THIS_FILE, "Invite usage initialization error",
423 status);
424 goto on_error;
Benny Prijono268ca612006-02-07 12:34:11 +0000425 }
426
Benny Prijonoccf95622006-02-07 18:48:01 +0000427 /* Done */
Benny Prijono268ca612006-02-07 12:34:11 +0000428
429 return PJ_SUCCESS;
430
431
432on_error:
433 pjsip_endpt_destroy(pjsua.endpt);
434 pjsua.endpt = NULL;
435 return status;
436}
437
438
Benny Prijono834aee32006-02-19 01:38:06 +0000439static int PJ_THREAD_FUNC pjsua_poll(void *arg)
Benny Prijono268ca612006-02-07 12:34:11 +0000440{
Benny Prijonof8baa872006-02-27 23:54:23 +0000441 pj_status_t last_err = 0;
442
Benny Prijono268ca612006-02-07 12:34:11 +0000443 PJ_UNUSED_ARG(arg);
444
Benny Prijono834aee32006-02-19 01:38:06 +0000445 do {
Benny Prijono268ca612006-02-07 12:34:11 +0000446 pj_time_val timeout = { 0, 10 };
Benny Prijonof8baa872006-02-27 23:54:23 +0000447 pj_status_t status;
448
449 status = pjsip_endpt_handle_events (pjsua.endpt, &timeout);
450 if (status != last_err) {
451 last_err = status;
452 pjsua_perror(THIS_FILE, "handle_events() returned error", status);
453 }
Benny Prijono834aee32006-02-19 01:38:06 +0000454 } while (!pjsua.quit_flag);
Benny Prijono268ca612006-02-07 12:34:11 +0000455
456 return 0;
457}
458
459/*
460 * Initialize pjsua application.
Benny Prijonoccf95622006-02-07 18:48:01 +0000461 * This will initialize all libraries, create endpoint instance, and register
462 * pjsip modules.
Benny Prijono268ca612006-02-07 12:34:11 +0000463 */
464pj_status_t pjsua_init(void)
465{
Benny Prijono268ca612006-02-07 12:34:11 +0000466 pj_status_t status;
467
468 /* Init PJLIB logging: */
469
470 pj_log_set_level(pjsua.log_level);
471 pj_log_set_decor(pjsua.log_decor);
472
473
474 /* Init PJLIB: */
475
476 status = pj_init();
477 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000478 pjsua_perror(THIS_FILE, "pj_init() error", status);
Benny Prijono268ca612006-02-07 12:34:11 +0000479 return status;
480 }
481
Benny Prijonofccab712006-02-22 22:23:22 +0000482 /* Init PJLIB-UTIL: */
483
484 status = pjlib_util_init();
485 if (status != PJ_SUCCESS) {
486 pjsua_perror(THIS_FILE, "pjlib_util_init() error", status);
487 return status;
488 }
489
Benny Prijono268ca612006-02-07 12:34:11 +0000490 /* Init memory pool: */
491
492 /* Init caching pool. */
493 pj_caching_pool_init(&pjsua.cp, &pj_pool_factory_default_policy, 0);
494
495 /* Create memory pool for application. */
496 pjsua.pool = pj_pool_create(&pjsua.cp.factory, "pjsua", 4000, 4000, NULL);
497
498
Benny Prijono834aee32006-02-19 01:38:06 +0000499 /* Init PJSIP : */
Benny Prijonoccf95622006-02-07 18:48:01 +0000500
501 status = init_stack();
502 if (status != PJ_SUCCESS) {
503 pj_caching_pool_destroy(&pjsua.cp);
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000504 pjsua_perror(THIS_FILE, "Stack initialization has returned error",
505 status);
Benny Prijonoccf95622006-02-07 18:48:01 +0000506 return status;
507 }
508
Benny Prijono95196582006-02-09 00:13:40 +0000509
Benny Prijono834aee32006-02-19 01:38:06 +0000510 /* Init core SIMPLE module : */
511
512 pjsip_evsub_init_module(pjsua.endpt);
513
514 /* Init presence module: */
515
516 pjsip_pres_init_module( pjsua.endpt, pjsip_evsub_instance());
517
Benny Prijono26ff9062006-02-21 23:47:00 +0000518 /* Init xfer/REFER module */
519
520 pjsip_xfer_init_module( pjsua.endpt );
Benny Prijono834aee32006-02-19 01:38:06 +0000521
522 /* Init pjsua presence handler: */
523
524 pjsua_pres_init();
525
Benny Prijonob0808372006-03-02 21:18:58 +0000526 /* Init out-of-dialog MESSAGE request handler. */
527
528 pjsua_im_init();
529
Benny Prijono834aee32006-02-19 01:38:06 +0000530
Benny Prijono95196582006-02-09 00:13:40 +0000531 /* Init media endpoint: */
532
533 status = pjmedia_endpt_create(&pjsua.cp.factory, &pjsua.med_endpt);
534 if (status != PJ_SUCCESS) {
535 pj_caching_pool_destroy(&pjsua.cp);
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000536 pjsua_perror(THIS_FILE,
537 "Media stack initialization has returned error",
538 status);
Benny Prijono95196582006-02-09 00:13:40 +0000539 return status;
540 }
541
Benny Prijonoccf95622006-02-07 18:48:01 +0000542 /* Done. */
543 return PJ_SUCCESS;
544}
545
546
Benny Prijonoa91a0032006-02-26 21:23:45 +0000547/*
548 * Find account for incoming request.
549 */
550int pjsua_find_account_for_incoming(pjsip_rx_data *rdata)
551{
552 pjsip_uri *uri;
553 pjsip_sip_uri *sip_uri;
554 int acc_index;
555
556 uri = rdata->msg_info.to->uri;
557
558 /* Just return account #0 if To URI is not SIP: */
559 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
560 !PJSIP_URI_SCHEME_IS_SIPS(uri))
561 {
562 return 0;
563 }
564
565
566 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
567
568 /* Find account which has matching username and domain. */
569 for (acc_index=0; acc_index < pjsua.acc_cnt; ++acc_index) {
570
571 pjsua_acc *acc = &pjsua.acc[acc_index];
572
573 if (pj_stricmp(&acc->user_part, &sip_uri->user)==0 &&
574 pj_stricmp(&acc->host_part, &sip_uri->host)==0)
575 {
576 /* Match ! */
577 return acc_index;
578 }
579 }
580
581 /* No matching, try match domain part only. */
582 for (acc_index=0; acc_index < pjsua.acc_cnt; ++acc_index) {
583
584 pjsua_acc *acc = &pjsua.acc[acc_index];
585
586 if (pj_stricmp(&acc->host_part, &sip_uri->host)==0) {
587 /* Match ! */
588 return acc_index;
589 }
590 }
591
592 /* Still no match, just return account #0 */
593 return 0;
594}
595
596
597/*
598 * Find account for outgoing request.
599 */
600int pjsua_find_account_for_outgoing(const pj_str_t *url)
601{
602 PJ_UNUSED_ARG(url);
603
604 /* Just use account #0 */
605 return 0;
606}
607
Benny Prijonoccf95622006-02-07 18:48:01 +0000608
609/*
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000610 * Init media.
Benny Prijonoccf95622006-02-07 18:48:01 +0000611 */
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000612static pj_status_t init_media(void)
Benny Prijonoccf95622006-02-07 18:48:01 +0000613{
Benny Prijono8e3344c2006-03-08 12:37:22 +0000614 unsigned options;
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000615 pj_status_t status;
616
617 /* If user doesn't specify any codecs, register all of them. */
618 if (pjsua.codec_cnt == 0) {
619
Benny Prijono4381efe2006-03-16 14:24:26 +0000620#if PJMEDIA_HAS_SPEEX_CODEC
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000621 unsigned option = PJMEDIA_SPEEX_NO_WB | PJMEDIA_SPEEX_NO_UWB;
622
623 /* Register speex. */
624 if (pjsua.clock_rate >= 16000)
625 option &= ~(PJMEDIA_SPEEX_NO_WB);
626 if (pjsua.clock_rate >= 32000)
627 option &= ~(PJMEDIA_SPEEX_NO_UWB);
628
Benny Prijono1c2bf462006-03-05 11:54:02 +0000629 status = pjmedia_codec_speex_init(pjsua.med_endpt, option,
630 pjsua.quality, pjsua.complexity );
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000631 if (status != PJ_SUCCESS) {
632 pjsua_perror(THIS_FILE, "Error initializing Speex codec",
633 status);
634 return status;
635 }
636
637 pjsua.codec_arg[pjsua.codec_cnt] = pj_str("speex");
638 pjsua.codec_deinit[pjsua.codec_cnt] = &pjmedia_codec_speex_deinit;
639 pjsua.codec_cnt++;
Benny Prijono4381efe2006-03-16 14:24:26 +0000640#endif /* PJMEDIA_HAS_SPEEX_CODEC */
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000641
Benny Prijono4381efe2006-03-16 14:24:26 +0000642#if PJMEDIA_HAS_GSM_CODEC
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000643 /* Register GSM */
644 status = pjmedia_codec_gsm_init(pjsua.med_endpt);
645 if (status != PJ_SUCCESS) {
646 pjsua_perror(THIS_FILE, "Error initializing GSM codec",
647 status);
648 return status;
649 }
650
651 pjsua.codec_arg[pjsua.codec_cnt] = pj_str("gsm");
652 pjsua.codec_deinit[pjsua.codec_cnt] = &pjmedia_codec_gsm_deinit;
653 pjsua.codec_cnt++;
Benny Prijono4381efe2006-03-16 14:24:26 +0000654#endif /* PJMEDIA_HAS_GSM_CODEC */
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000655
Benny Prijono4381efe2006-03-16 14:24:26 +0000656#if PJMEDIA_HAS_G711_CODEC
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000657 /* Register PCMA and PCMU */
658 status = pjmedia_codec_g711_init(pjsua.med_endpt);
659 if (status != PJ_SUCCESS) {
660 pjsua_perror(THIS_FILE, "Error initializing G711 codec",
661 status);
662 return status;
663 }
664
665 pjsua.codec_arg[pjsua.codec_cnt] = pj_str("pcmu");
666 pjsua.codec_deinit[pjsua.codec_cnt] = &pjmedia_codec_g711_deinit;
667 pjsua.codec_cnt++;
668 pjsua.codec_arg[pjsua.codec_cnt] = pj_str("pcma");
669 pjsua.codec_deinit[pjsua.codec_cnt] = &pjmedia_codec_g711_deinit;
670 pjsua.codec_cnt++;
Benny Prijono4381efe2006-03-16 14:24:26 +0000671#endif /* PJMEDIA_HAS_G711_CODEC */
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000672
673 } else {
674
675 /* If user specifies the exact codec to be used, then create only
676 * those codecs.
677 */
678 int i;
679
680 for (i=0; i<pjsua.codec_cnt; ++i) {
681
Benny Prijono4381efe2006-03-16 14:24:26 +0000682 if (0) {
683 /* Dummy */
684 }
685#if PJMEDIA_HAS_SPEEX_CODEC
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000686 /* Is it speex? */
Benny Prijono4381efe2006-03-16 14:24:26 +0000687 else if (!pj_stricmp2(&pjsua.codec_arg[i], "speex")) {
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000688
689 unsigned option = PJMEDIA_SPEEX_NO_WB | PJMEDIA_SPEEX_NO_UWB;
690
691 /* Register speex. */
692 if (pjsua.clock_rate >= 16000)
693 option &= ~(PJMEDIA_SPEEX_NO_WB);
694 if (pjsua.clock_rate >= 32000)
695 option &= ~(PJMEDIA_SPEEX_NO_UWB);
696
697 status = pjmedia_codec_speex_init(pjsua.med_endpt, option,
698 -1, -1);
699 if (status != PJ_SUCCESS) {
700 pjsua_perror(THIS_FILE, "Error initializing Speex codec",
701 status);
702 return status;
703 }
704
705 pjsua.codec_deinit[i] = &pjmedia_codec_speex_deinit;
706 }
Benny Prijono4381efe2006-03-16 14:24:26 +0000707#endif /* PJMEDIA_HAS_SPEEX_CODEC */
708
709#if PJMEDIA_HAS_GSM_CODEC
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000710 /* Is it gsm? */
711 else if (!pj_stricmp2(&pjsua.codec_arg[i], "gsm")) {
712
713 status = pjmedia_codec_gsm_init(pjsua.med_endpt);
714 if (status != PJ_SUCCESS) {
715 pjsua_perror(THIS_FILE, "Error initializing GSM codec",
716 status);
717 return status;
718 }
719
720 pjsua.codec_deinit[i] = &pjmedia_codec_gsm_deinit;
721
722 }
Benny Prijono4381efe2006-03-16 14:24:26 +0000723#endif /* PJMEDIA_HAS_GSM_CODEC */
724
725#if PJMEDIA_HAS_G711_CODEC
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000726 /* Is it pcma/pcmu? */
727 else if (!pj_stricmp2(&pjsua.codec_arg[i], "pcmu") ||
728 !pj_stricmp2(&pjsua.codec_arg[i], "pcma"))
729 {
730
731 status = pjmedia_codec_g711_init(pjsua.med_endpt);
732 if (status != PJ_SUCCESS) {
733 pjsua_perror(THIS_FILE, "Error initializing G711 codec",
734 status);
735 return status;
736 }
737
738 pjsua.codec_deinit[i] = &pjmedia_codec_g711_deinit;
739
740 }
Benny Prijono4381efe2006-03-16 14:24:26 +0000741#endif /* PJMEDIA_HAS_G711_CODEC */
742
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000743 /* Don't know about this codec... */
744 else {
745
746 PJ_LOG(1,(THIS_FILE, "Error: unsupported codecs %s",
747 pjsua.codec_arg[i].ptr));
748 return PJMEDIA_CODEC_EUNSUP;
749 }
750 }
751 }
Benny Prijonoccf95622006-02-07 18:48:01 +0000752
Benny Prijono8e3344c2006-03-08 12:37:22 +0000753 /* Init options for conference bridge. */
754 options = 0;
755 if (pjsua.no_mic)
756 options |= PJMEDIA_CONF_NO_MIC;
757
Benny Prijonof8baa872006-02-27 23:54:23 +0000758 /* Init conference bridge. */
759
760 status = pjmedia_conf_create(pjsua.pool,
761 pjsua.max_calls+PJSUA_CONF_MORE_PORTS,
Benny Prijono08e0d062006-03-04 14:52:44 +0000762 pjsua.clock_rate,
Benny Prijonod0659a32006-03-16 19:03:07 +0000763 1, /* mono */
Benny Prijono08e0d062006-03-04 14:52:44 +0000764 pjsua.clock_rate * 20 / 1000, 16,
Benny Prijono8e3344c2006-03-08 12:37:22 +0000765 options,
Benny Prijono08e0d062006-03-04 14:52:44 +0000766 &pjsua.mconf);
Benny Prijonof8baa872006-02-27 23:54:23 +0000767 if (status != PJ_SUCCESS) {
768 pj_caching_pool_destroy(&pjsua.cp);
769 pjsua_perror(THIS_FILE,
770 "Media stack initialization has returned error",
771 status);
772 return status;
773 }
774
Benny Prijonode380582006-03-15 19:32:41 +0000775 /* Add NULL port to the bridge. */
776 status = pjmedia_null_port_create( pjsua.pool, pjsua.clock_rate,
Benny Prijonod0659a32006-03-16 19:03:07 +0000777 1, /* mono */
Benny Prijonode380582006-03-15 19:32:41 +0000778 pjsua.clock_rate * 20 / 1000, 16,
779 &pjsua.null_port);
780 pjmedia_conf_add_port( pjsua.mconf, pjsua.pool, pjsua.null_port,
781 &pjsua.null_port->info.name, NULL );
782
Benny Prijono39879152006-02-23 02:09:10 +0000783 /* Create WAV file player if required: */
784
785 if (pjsua.wav_file) {
Benny Prijono39879152006-02-23 02:09:10 +0000786 pj_str_t port_name;
787
788 /* Create the file player port. */
789 status = pjmedia_file_player_port_create( pjsua.pool, pjsua.wav_file,
Benny Prijonode380582006-03-15 19:32:41 +0000790 0, -1, NULL,
791 &pjsua.file_port);
Benny Prijono39879152006-02-23 02:09:10 +0000792 if (status != PJ_SUCCESS) {
793 pjsua_perror(THIS_FILE,
794 "Error playing media file",
795 status);
796 return status;
797 }
798
799 /* Add port to conference bridge: */
Benny Prijonode380582006-03-15 19:32:41 +0000800 status = pjmedia_conf_add_port(pjsua.mconf, pjsua.pool,
801 pjsua.file_port,
Benny Prijono39879152006-02-23 02:09:10 +0000802 pj_cstr(&port_name, pjsua.wav_file),
803 &pjsua.wav_slot);
804 if (status != PJ_SUCCESS) {
805 pjsua_perror(THIS_FILE,
806 "Unable to add file player to conference bridge",
807 status);
808 return status;
809 }
810 }
811
812
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000813 return PJ_SUCCESS;
814}
815
816
817/*
818 * Start pjsua stack.
819 * This will start the registration process, if registration is configured.
820 */
821pj_status_t pjsua_start(void)
822{
823 int i; /* Must be signed */
824 pjsip_transport *udp_transport;
825 pj_status_t status = PJ_SUCCESS;
826
827 /*
828 * Init media subsystem (codecs, conference bridge, et all).
829 */
830 status = init_media();
831 if (status != PJ_SUCCESS)
832 return status;
833
Benny Prijono268ca612006-02-07 12:34:11 +0000834 /* Init sockets (STUN etc): */
Benny Prijonoa91a0032006-02-26 21:23:45 +0000835 for (i=0; i<(int)pjsua.max_calls; ++i) {
836 status = init_sockets(i==0, &pjsua.calls[i].skinfo);
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000837 if (status != PJ_SUCCESS) {
838 pjsua_perror(THIS_FILE, "init_sockets() has returned error",
839 status);
Benny Prijonoa91a0032006-02-26 21:23:45 +0000840 --i;
841 if (i >= 0)
842 pj_sock_close(pjsua.sip_sock);
843 while (i >= 0) {
844 pj_sock_close(pjsua.calls[i].skinfo.rtp_sock);
845 pj_sock_close(pjsua.calls[i].skinfo.rtcp_sock);
846 }
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000847 return status;
848 }
Benny Prijono268ca612006-02-07 12:34:11 +0000849 }
850
Benny Prijonoccf95622006-02-07 18:48:01 +0000851 /* Add UDP transport: */
Benny Prijono268ca612006-02-07 12:34:11 +0000852
Benny Prijonoccf95622006-02-07 18:48:01 +0000853 {
854 /* Init the published name for the transport.
855 * Depending whether STUN is used, this may be the STUN mapped
856 * address, or socket's bound address.
857 */
858 pjsip_host_port addr_name;
859
860 addr_name.host.ptr = pj_inet_ntoa(pjsua.sip_sock_name.sin_addr);
Benny Prijonof3195072006-02-14 21:15:30 +0000861 addr_name.host.slen = pj_ansi_strlen(addr_name.host.ptr);
Benny Prijonoccf95622006-02-07 18:48:01 +0000862 addr_name.port = pj_ntohs(pjsua.sip_sock_name.sin_port);
863
864 /* Create UDP transport from previously created UDP socket: */
865
866 status = pjsip_udp_transport_attach( pjsua.endpt, pjsua.sip_sock,
867 &addr_name, 1,
868 &udp_transport);
869 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000870 pjsua_perror(THIS_FILE, "Unable to start UDP transport",
871 status);
Benny Prijonoccf95622006-02-07 18:48:01 +0000872 return status;
873 }
Benny Prijono268ca612006-02-07 12:34:11 +0000874 }
875
Benny Prijonoccf95622006-02-07 18:48:01 +0000876 /* Initialize Contact URI, if one is not specified: */
Benny Prijonoa91a0032006-02-26 21:23:45 +0000877 for (i=0; i<pjsua.acc_cnt; ++i) {
Benny Prijonoccf95622006-02-07 18:48:01 +0000878
879 pjsip_uri *uri;
880 pjsip_sip_uri *sip_uri;
Benny Prijonoccf95622006-02-07 18:48:01 +0000881
Benny Prijonoccf95622006-02-07 18:48:01 +0000882 /* Need to parse local_uri to get the elements: */
883
Benny Prijonoa91a0032006-02-26 21:23:45 +0000884 uri = pjsip_parse_uri(pjsua.pool, pjsua.acc[i].local_uri.ptr,
885 pjsua.acc[i].local_uri.slen, 0);
Benny Prijonoccf95622006-02-07 18:48:01 +0000886 if (uri == NULL) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000887 pjsua_perror(THIS_FILE, "Invalid local URI",
888 PJSIP_EINVALIDURI);
Benny Prijonoccf95622006-02-07 18:48:01 +0000889 return PJSIP_EINVALIDURI;
890 }
891
Benny Prijonoccf95622006-02-07 18:48:01 +0000892 /* Local URI MUST be a SIP or SIPS: */
893
Benny Prijonoa91a0032006-02-26 21:23:45 +0000894 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
895 !PJSIP_URI_SCHEME_IS_SIPS(uri))
896 {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000897 pjsua_perror(THIS_FILE, "Invalid local URI",
898 PJSIP_EINVALIDSCHEME);
Benny Prijonoccf95622006-02-07 18:48:01 +0000899 return PJSIP_EINVALIDSCHEME;
900 }
901
902
903 /* Get the SIP URI object: */
904
905 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
906
Benny Prijonoa91a0032006-02-26 21:23:45 +0000907 pjsua.acc[i].user_part = sip_uri->user;
908 pjsua.acc[i].host_part = sip_uri->host;
Benny Prijonoccf95622006-02-07 18:48:01 +0000909
Benny Prijonoa91a0032006-02-26 21:23:45 +0000910 if (pjsua.acc[i].contact_uri.slen == 0 &&
911 pjsua.acc[i].local_uri.slen)
912 {
913 char contact[128];
914 int len;
Benny Prijonoccf95622006-02-07 18:48:01 +0000915
Benny Prijonoa91a0032006-02-26 21:23:45 +0000916 /* The local Contact is the username@ip-addr, where
917 * - username is taken from the local URI,
918 * - ip-addr in UDP transport's address name (which may have been
919 * resolved from STUN.
920 */
921
922 /* Build temporary contact string. */
Benny Prijonoccf95622006-02-07 18:48:01 +0000923
Benny Prijonoa91a0032006-02-26 21:23:45 +0000924 if (sip_uri->user.slen) {
Benny Prijonoccf95622006-02-07 18:48:01 +0000925
Benny Prijonoa91a0032006-02-26 21:23:45 +0000926 /* With the user part. */
Benny Prijonoed811d72006-03-10 12:57:12 +0000927 len = pj_ansi_snprintf(contact, sizeof(contact),
Benny Prijonoa91a0032006-02-26 21:23:45 +0000928 "<sip:%.*s@%.*s:%d>",
929 (int)sip_uri->user.slen,
930 sip_uri->user.ptr,
931 (int)udp_transport->local_name.host.slen,
932 udp_transport->local_name.host.ptr,
933 udp_transport->local_name.port);
934 } else {
935
936 /* Without user part */
937
Benny Prijonoed811d72006-03-10 12:57:12 +0000938 len = pj_ansi_snprintf(contact, sizeof(contact),
Benny Prijonoa91a0032006-02-26 21:23:45 +0000939 "<sip:%.*s:%d>",
940 (int)udp_transport->local_name.host.slen,
941 udp_transport->local_name.host.ptr,
942 udp_transport->local_name.port);
943 }
944
945 if (len < 1 || len >= sizeof(contact)) {
946 pjsua_perror(THIS_FILE, "Invalid Contact", PJSIP_EURITOOLONG);
947 return PJSIP_EURITOOLONG;
948 }
949
950 /* Duplicate Contact uri. */
951
952 pj_strdup2(pjsua.pool, &pjsua.acc[i].contact_uri, contact);
953
Benny Prijonoccf95622006-02-07 18:48:01 +0000954 }
Benny Prijonoccf95622006-02-07 18:48:01 +0000955 }
956
Benny Prijono95196582006-02-09 00:13:40 +0000957 /* If outbound_proxy is specified, put it in the route_set: */
Benny Prijonoccf95622006-02-07 18:48:01 +0000958
Benny Prijono95196582006-02-09 00:13:40 +0000959 if (pjsua.outbound_proxy.slen) {
960
961 pjsip_route_hdr *route;
962 const pj_str_t hname = { "Route", 5 };
963 int parsed_len;
964
965 route = pjsip_parse_hdr( pjsua.pool, &hname,
966 pjsua.outbound_proxy.ptr,
967 pjsua.outbound_proxy.slen,
968 &parsed_len);
969 if (route == NULL) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000970 pjsua_perror(THIS_FILE, "Invalid outbound proxy URL",
971 PJSIP_EINVALIDURI);
Benny Prijono95196582006-02-09 00:13:40 +0000972 return PJSIP_EINVALIDURI;
973 }
974
Benny Prijonoa91a0032006-02-26 21:23:45 +0000975 for (i=0; i<pjsua.acc_cnt; ++i) {
976 pj_list_push_front(&pjsua.acc[i].route_set, route);
977 }
Benny Prijono95196582006-02-09 00:13:40 +0000978 }
Benny Prijonoccf95622006-02-07 18:48:01 +0000979
980
Benny Prijono268ca612006-02-07 12:34:11 +0000981 /* Create worker thread(s), if required: */
982
983 for (i=0; i<pjsua.thread_cnt; ++i) {
Benny Prijono834aee32006-02-19 01:38:06 +0000984 status = pj_thread_create( pjsua.pool, "pjsua", &pjsua_poll,
Benny Prijono268ca612006-02-07 12:34:11 +0000985 NULL, 0, 0, &pjsua.threads[i]);
986 if (status != PJ_SUCCESS) {
987 pjsua.quit_flag = 1;
988 for (--i; i>=0; --i) {
989 pj_thread_join(pjsua.threads[i]);
990 pj_thread_destroy(pjsua.threads[i]);
991 }
Benny Prijono268ca612006-02-07 12:34:11 +0000992 return status;
993 }
994 }
995
Benny Prijonoccf95622006-02-07 18:48:01 +0000996 /* Start registration: */
997
998 /* Create client registration session: */
Benny Prijonoa91a0032006-02-26 21:23:45 +0000999 for (i=0; i<pjsua.acc_cnt; ++i) {
1000 status = pjsua_regc_init(i);
1001 if (status != PJ_SUCCESS)
1002 return status;
Benny Prijonoccf95622006-02-07 18:48:01 +00001003
Benny Prijonoa91a0032006-02-26 21:23:45 +00001004 /* Perform registration, if required. */
1005 if (pjsua.acc[i].regc) {
1006 pjsua_regc_update(i, 1);
1007 }
1008 }
Benny Prijonoccf95622006-02-07 18:48:01 +00001009
Benny Prijonoa91a0032006-02-26 21:23:45 +00001010
1011 /* Find account for outgoing preence subscription */
1012 for (i=0; i<pjsua.buddy_cnt; ++i) {
1013 pjsua.buddies[i].acc_index =
1014 pjsua_find_account_for_outgoing(&pjsua.buddies[i].uri);
Benny Prijonoccf95622006-02-07 18:48:01 +00001015 }
1016
1017
Benny Prijono834aee32006-02-19 01:38:06 +00001018 PJ_LOG(3,(THIS_FILE, "PJSUA version %s started", PJ_VERSION));
Benny Prijono268ca612006-02-07 12:34:11 +00001019 return PJ_SUCCESS;
1020}
1021
1022
Benny Prijono834aee32006-02-19 01:38:06 +00001023/* Sleep with polling */
1024static void busy_sleep(unsigned msec)
1025{
1026 pj_time_val timeout, now;
1027
1028 pj_gettimeofday(&timeout);
1029 timeout.msec += msec;
1030 pj_time_val_normalize(&timeout);
1031
1032 do {
1033 pjsua_poll(NULL);
1034 pj_gettimeofday(&now);
1035 } while (PJ_TIME_VAL_LT(now, timeout));
1036}
1037
Benny Prijono268ca612006-02-07 12:34:11 +00001038/*
1039 * Destroy pjsua.
1040 */
1041pj_status_t pjsua_destroy(void)
1042{
Benny Prijonoeb30bf52006-03-04 20:43:52 +00001043 int i; /* Must be signed */
Benny Prijono268ca612006-02-07 12:34:11 +00001044
1045 /* Signal threads to quit: */
Benny Prijono268ca612006-02-07 12:34:11 +00001046 pjsua.quit_flag = 1;
1047
Benny Prijonoa91a0032006-02-26 21:23:45 +00001048 /* Terminate all calls. */
Benny Prijono1a174142006-03-01 20:46:13 +00001049 pjsua_call_hangup_all();
Benny Prijonoa91a0032006-02-26 21:23:45 +00001050
1051 /* Terminate all presence subscriptions. */
1052 pjsua_pres_shutdown();
1053
1054 /* Unregister, if required: */
1055 for (i=0; i<pjsua.acc_cnt; ++i) {
1056 if (pjsua.acc[i].regc) {
1057 pjsua_regc_update(i, 0);
1058 }
1059 }
1060
Benny Prijono268ca612006-02-07 12:34:11 +00001061 /* Wait worker threads to quit: */
Benny Prijono268ca612006-02-07 12:34:11 +00001062 for (i=0; i<pjsua.thread_cnt; ++i) {
1063
Benny Prijonoe4f2abb2006-02-10 14:04:05 +00001064 if (pjsua.threads[i]) {
1065 pj_thread_join(pjsua.threads[i]);
1066 pj_thread_destroy(pjsua.threads[i]);
1067 pjsua.threads[i] = NULL;
1068 }
Benny Prijono268ca612006-02-07 12:34:11 +00001069 }
1070
Benny Prijono834aee32006-02-19 01:38:06 +00001071
Benny Prijono834aee32006-02-19 01:38:06 +00001072 /* Wait for some time to allow unregistration to complete: */
1073 PJ_LOG(4,(THIS_FILE, "Shutting down..."));
1074 busy_sleep(1000);
1075
Benny Prijono26ff9062006-02-21 23:47:00 +00001076 /* Destroy conference bridge. */
1077 if (pjsua.mconf)
1078 pjmedia_conf_destroy(pjsua.mconf);
1079
Benny Prijonode380582006-03-15 19:32:41 +00001080 /* Destroy file port */
Benny Prijono4381efe2006-03-16 14:24:26 +00001081 if (pjsua.file_port)
1082 pjmedia_port_destroy(pjsua.file_port);
Benny Prijonode380582006-03-15 19:32:41 +00001083
1084 /* Destroy null port. */
Benny Prijono4381efe2006-03-16 14:24:26 +00001085 if (pjsua.null_port)
1086 pjmedia_port_destroy(pjsua.null_port);
Benny Prijonode380582006-03-15 19:32:41 +00001087
1088
Benny Prijonoeb30bf52006-03-04 20:43:52 +00001089 /* Shutdown all codecs: */
1090 for (i = pjsua.codec_cnt-1; i >= 0; --i) {
1091 (*pjsua.codec_deinit[i])();
1092 }
1093
1094 /* Destroy media endpoint. */
1095
1096 pjmedia_endpt_destroy(pjsua.med_endpt);
1097
Benny Prijono268ca612006-02-07 12:34:11 +00001098 /* Destroy endpoint. */
Benny Prijono84126ab2006-02-09 09:30:09 +00001099
Benny Prijono268ca612006-02-07 12:34:11 +00001100 pjsip_endpt_destroy(pjsua.endpt);
1101 pjsua.endpt = NULL;
1102
1103 /* Destroy caching pool. */
Benny Prijono84126ab2006-02-09 09:30:09 +00001104
Benny Prijono268ca612006-02-07 12:34:11 +00001105 pj_caching_pool_destroy(&pjsua.cp);
1106
1107
1108 /* Done. */
1109
1110 return PJ_SUCCESS;
1111}
1112