blob: 35309323dc4f6ccbf130f56188b5fcc29beb5a52 [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 Prijonofa137ca2006-03-20 17:42:37 +000080#if defined(PJ_DARWINOS) && PJ_DARWINOS!=0
81 pjsua.clock_rate = 44100;
Benny Prijonofa137ca2006-03-20 17:42:37 +000082#endif
Benny Prijonoccb03fa2006-03-06 13:35:47 +000083 pjsua.complexity = -1;
Benny Prijono1c2bf462006-03-05 11:54:02 +000084 pjsua.quality = 4;
85
Benny Prijono08e0d062006-03-04 14:52:44 +000086
Benny Prijonoa91a0032006-02-26 21:23:45 +000087 /* Init accounts: */
88 pjsua.acc_cnt = 1;
89 for (i=0; i<PJ_ARRAY_SIZE(pjsua.acc); ++i) {
90 pjsua.acc[i].index = i;
91 pjsua.acc[i].local_uri = pj_str(PJSUA_LOCAL_URI);
92 pjsua.acc[i].reg_timeout = 55;
Benny Prijonoccb03fa2006-03-06 13:35:47 +000093 pjsua.acc[i].online_status = PJ_TRUE;
Benny Prijonoa91a0032006-02-26 21:23:45 +000094 pj_list_init(&pjsua.acc[i].route_set);
95 pj_list_init(&pjsua.acc[i].pres_srv_list);
96 }
Benny Prijonoccf95622006-02-07 18:48:01 +000097
Benny Prijonoa91a0032006-02-26 21:23:45 +000098 /* Init call array: */
Benny Prijono105217f2006-03-06 16:25:59 +000099 for (i=0; i<PJ_ARRAY_SIZE(pjsua.calls); ++i) {
Benny Prijonoa91a0032006-02-26 21:23:45 +0000100 pjsua.calls[i].index = i;
Benny Prijono105217f2006-03-06 16:25:59 +0000101 pjsua.calls[i].refresh_tm._timer_id = -1;
102 pjsua.calls[i].hangup_tm._timer_id = -1;
Benny Prijono275fd682006-03-22 11:59:11 +0000103 pjsua.calls[i].conf_slot = 0;
Benny Prijono105217f2006-03-06 16:25:59 +0000104 }
Benny Prijono95196582006-02-09 00:13:40 +0000105
Benny Prijonoa91a0032006-02-26 21:23:45 +0000106 /* Default max nb of calls. */
107 pjsua.max_calls = 4;
Benny Prijono834aee32006-02-19 01:38:06 +0000108
109 /* Init server presence subscription list: */
110
Benny Prijono834aee32006-02-19 01:38:06 +0000111
Benny Prijono268ca612006-02-07 12:34:11 +0000112}
113
114
Benny Prijono268ca612006-02-07 12:34:11 +0000115
116/*
117 * Handler for receiving incoming requests.
118 *
119 * This handler serves multiple purposes:
120 * - it receives requests outside dialogs.
121 * - it receives requests inside dialogs, when the requests are
122 * unhandled by other dialog usages. Example of these
123 * requests are: MESSAGE.
124 */
125static pj_bool_t mod_pjsua_on_rx_request(pjsip_rx_data *rdata)
126{
Benny Prijono38998232006-02-08 22:44:25 +0000127
Benny Prijono84126ab2006-02-09 09:30:09 +0000128 if (rdata->msg_info.msg->line.req.method.id == PJSIP_INVITE_METHOD) {
Benny Prijono38998232006-02-08 22:44:25 +0000129
Benny Prijonoa91a0032006-02-26 21:23:45 +0000130 return pjsua_call_on_incoming(rdata);
Benny Prijono38998232006-02-08 22:44:25 +0000131 }
132
Benny Prijono268ca612006-02-07 12:34:11 +0000133 return PJ_FALSE;
134}
135
136
137/*
138 * Handler for receiving incoming responses.
139 *
140 * This handler serves multiple purposes:
141 * - it receives strayed responses (i.e. outside any dialog and
142 * outside any transactions).
143 * - it receives responses coming to a transaction, when pjsua
144 * module is set as transaction user for the transaction.
145 * - it receives responses inside a dialog, when these responses
146 * are unhandled by other dialog usages.
147 */
148static pj_bool_t mod_pjsua_on_rx_response(pjsip_rx_data *rdata)
149{
150 PJ_UNUSED_ARG(rdata);
Benny Prijono268ca612006-02-07 12:34:11 +0000151 return PJ_FALSE;
152}
153
154
Benny Prijono268ca612006-02-07 12:34:11 +0000155/*
156 * Initialize sockets and optionally get the public address via STUN.
157 */
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000158static pj_status_t init_sockets(pj_bool_t sip,
159 pjmedia_sock_info *skinfo)
Benny Prijono268ca612006-02-07 12:34:11 +0000160{
161 enum {
Benny Prijonoa91a0032006-02-26 21:23:45 +0000162 RTP_RETRY = 100
Benny Prijono268ca612006-02-07 12:34:11 +0000163 };
164 enum {
165 SIP_SOCK,
166 RTP_SOCK,
167 RTCP_SOCK,
168 };
169 int i;
Benny Prijonocbf37402006-03-01 19:29:10 +0000170 static pj_uint16_t rtp_port;
Benny Prijono268ca612006-02-07 12:34:11 +0000171 pj_sock_t sock[3];
172 pj_sockaddr_in mapped_addr[3];
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000173 pj_status_t status = PJ_SUCCESS;
Benny Prijono268ca612006-02-07 12:34:11 +0000174
Benny Prijonocbf37402006-03-01 19:29:10 +0000175 if (rtp_port == 0)
176 rtp_port = (pj_uint16_t)pjsua.start_rtp_port;
177
Benny Prijono268ca612006-02-07 12:34:11 +0000178 for (i=0; i<3; ++i)
179 sock[i] = PJ_INVALID_SOCKET;
180
Benny Prijonofccab712006-02-22 22:23:22 +0000181 /* Create and bind SIP UDP socket. */
182 status = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, &sock[SIP_SOCK]);
183 if (status != PJ_SUCCESS) {
184 pjsua_perror(THIS_FILE, "socket() error", status);
185 goto on_error;
186 }
187
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000188 if (sip) {
Benny Prijonofccab712006-02-22 22:23:22 +0000189 status = pj_sock_bind_in(sock[SIP_SOCK], 0, pjsua.sip_port);
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000190 if (status != PJ_SUCCESS) {
Benny Prijonofccab712006-02-22 22:23:22 +0000191 pjsua_perror(THIS_FILE, "bind() error", status);
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000192 goto on_error;
193 }
Benny Prijonofccab712006-02-22 22:23:22 +0000194 } else {
195 status = pj_sock_bind_in(sock[SIP_SOCK], 0, 0);
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000196 if (status != PJ_SUCCESS) {
197 pjsua_perror(THIS_FILE, "bind() error", status);
198 goto on_error;
199 }
Benny Prijono268ca612006-02-07 12:34:11 +0000200 }
201
Benny Prijono268ca612006-02-07 12:34:11 +0000202
203 /* Loop retry to bind RTP and RTCP sockets. */
204 for (i=0; i<RTP_RETRY; ++i, rtp_port += 2) {
205
206 /* Create and bind RTP socket. */
207 status = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, &sock[RTP_SOCK]);
208 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000209 pjsua_perror(THIS_FILE, "socket() error", status);
Benny Prijono268ca612006-02-07 12:34:11 +0000210 goto on_error;
211 }
212
213 status = pj_sock_bind_in(sock[RTP_SOCK], 0, rtp_port);
214 if (status != PJ_SUCCESS) {
215 pj_sock_close(sock[RTP_SOCK]);
216 sock[RTP_SOCK] = PJ_INVALID_SOCKET;
217 continue;
218 }
219
220 /* Create and bind RTCP socket. */
221 status = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, &sock[RTCP_SOCK]);
222 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000223 pjsua_perror(THIS_FILE, "socket() error", status);
Benny Prijono268ca612006-02-07 12:34:11 +0000224 goto on_error;
225 }
226
227 status = pj_sock_bind_in(sock[RTCP_SOCK], 0, (pj_uint16_t)(rtp_port+1));
228 if (status != PJ_SUCCESS) {
229 pj_sock_close(sock[RTP_SOCK]);
230 sock[RTP_SOCK] = PJ_INVALID_SOCKET;
231
232 pj_sock_close(sock[RTCP_SOCK]);
233 sock[RTCP_SOCK] = PJ_INVALID_SOCKET;
234 continue;
235 }
236
237 /*
238 * If we're configured to use STUN, then find out the mapped address,
239 * and make sure that the mapped RTCP port is adjacent with the RTP.
240 */
241 if (pjsua.stun_port1 == 0) {
242 const pj_str_t *hostname;
243 pj_sockaddr_in addr;
244
245 /* Get local IP address. */
246 hostname = pj_gethostname();
247
248 pj_memset( &addr, 0, sizeof(addr));
249 addr.sin_family = PJ_AF_INET;
250 status = pj_sockaddr_in_set_str_addr( &addr, hostname);
251 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000252 pjsua_perror(THIS_FILE, "Unresolvable local hostname",
253 status);
Benny Prijono268ca612006-02-07 12:34:11 +0000254 goto on_error;
255 }
256
257 for (i=0; i<3; ++i)
258 pj_memcpy(&mapped_addr[i], &addr, sizeof(addr));
259
Benny Prijonoa91a0032006-02-26 21:23:45 +0000260 if (sip) {
261 mapped_addr[SIP_SOCK].sin_port =
262 pj_htons((pj_uint16_t)pjsua.sip_port);
263 }
264 mapped_addr[RTP_SOCK].sin_port=pj_htons((pj_uint16_t)rtp_port);
265 mapped_addr[RTCP_SOCK].sin_port=pj_htons((pj_uint16_t)(rtp_port+1));
Benny Prijono268ca612006-02-07 12:34:11 +0000266 break;
Benny Prijonoa91a0032006-02-26 21:23:45 +0000267
Benny Prijono268ca612006-02-07 12:34:11 +0000268 } else {
Benny Prijonoa91a0032006-02-26 21:23:45 +0000269 status=pj_stun_get_mapped_addr(&pjsua.cp.factory, 3, sock,
270 &pjsua.stun_srv1, pjsua.stun_port1,
271 &pjsua.stun_srv2, pjsua.stun_port2,
272 mapped_addr);
Benny Prijono268ca612006-02-07 12:34:11 +0000273 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000274 pjsua_perror(THIS_FILE, "STUN error", status);
Benny Prijono268ca612006-02-07 12:34:11 +0000275 goto on_error;
276 }
277
Benny Prijonoa91a0032006-02-26 21:23:45 +0000278 if (pj_ntohs(mapped_addr[2].sin_port) ==
279 pj_ntohs(mapped_addr[1].sin_port)+1)
280 {
Benny Prijono268ca612006-02-07 12:34:11 +0000281 break;
Benny Prijonoa91a0032006-02-26 21:23:45 +0000282 }
Benny Prijono268ca612006-02-07 12:34:11 +0000283
Benny Prijonoa91a0032006-02-26 21:23:45 +0000284 pj_sock_close(sock[RTP_SOCK]);
285 sock[RTP_SOCK] = PJ_INVALID_SOCKET;
286
287 pj_sock_close(sock[RTCP_SOCK]);
288 sock[RTCP_SOCK] = PJ_INVALID_SOCKET;
Benny Prijono268ca612006-02-07 12:34:11 +0000289 }
290 }
291
292 if (sock[RTP_SOCK] == PJ_INVALID_SOCKET) {
Benny Prijonoa91a0032006-02-26 21:23:45 +0000293 PJ_LOG(1,(THIS_FILE,
294 "Unable to find appropriate RTP/RTCP ports combination"));
Benny Prijono268ca612006-02-07 12:34:11 +0000295 goto on_error;
296 }
297
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000298 if (sip) {
299 pjsua.sip_sock = sock[SIP_SOCK];
Benny Prijonoa91a0032006-02-26 21:23:45 +0000300 pj_memcpy(&pjsua.sip_sock_name,
301 &mapped_addr[SIP_SOCK],
302 sizeof(pj_sockaddr_in));
Benny Prijonofccab712006-02-22 22:23:22 +0000303 } else {
304 pj_sock_close(sock[0]);
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000305 }
Benny Prijono95196582006-02-09 00:13:40 +0000306
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000307 skinfo->rtp_sock = sock[RTP_SOCK];
308 pj_memcpy(&skinfo->rtp_addr_name,
Benny Prijono95196582006-02-09 00:13:40 +0000309 &mapped_addr[RTP_SOCK], sizeof(pj_sockaddr_in));
310
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000311 skinfo->rtcp_sock = sock[RTCP_SOCK];
312 pj_memcpy(&skinfo->rtcp_addr_name,
Benny Prijono95196582006-02-09 00:13:40 +0000313 &mapped_addr[RTCP_SOCK], sizeof(pj_sockaddr_in));
Benny Prijono268ca612006-02-07 12:34:11 +0000314
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000315 if (sip) {
316 PJ_LOG(4,(THIS_FILE, "SIP UDP socket reachable at %s:%d",
317 pj_inet_ntoa(pjsua.sip_sock_name.sin_addr),
318 pj_ntohs(pjsua.sip_sock_name.sin_port)));
319 }
Benny Prijono268ca612006-02-07 12:34:11 +0000320 PJ_LOG(4,(THIS_FILE, "RTP socket reachable at %s:%d",
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000321 pj_inet_ntoa(skinfo->rtp_addr_name.sin_addr),
322 pj_ntohs(skinfo->rtp_addr_name.sin_port)));
Benny Prijono268ca612006-02-07 12:34:11 +0000323 PJ_LOG(4,(THIS_FILE, "RTCP UDP socket reachable at %s:%d",
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000324 pj_inet_ntoa(skinfo->rtcp_addr_name.sin_addr),
325 pj_ntohs(skinfo->rtcp_addr_name.sin_port)));
Benny Prijono268ca612006-02-07 12:34:11 +0000326
Benny Prijonofccab712006-02-22 22:23:22 +0000327 rtp_port += 2;
Benny Prijono268ca612006-02-07 12:34:11 +0000328 return PJ_SUCCESS;
329
330on_error:
331 for (i=0; i<3; ++i) {
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000332 if (sip && i==0)
333 continue;
Benny Prijono268ca612006-02-07 12:34:11 +0000334 if (sock[i] != PJ_INVALID_SOCKET)
335 pj_sock_close(sock[i]);
336 }
337 return status;
338}
339
340
341
342/*
343 * Initialize stack.
344 */
345static pj_status_t init_stack(void)
346{
347 pj_status_t status;
348
349 /* Create global endpoint: */
350
351 {
352 const pj_str_t *hostname;
353 const char *endpt_name;
354
355 /* Endpoint MUST be assigned a globally unique name.
356 * The name will be used as the hostname in Warning header.
357 */
358
359 /* For this implementation, we'll use hostname for simplicity */
360 hostname = pj_gethostname();
361 endpt_name = hostname->ptr;
362
363 /* Create the endpoint: */
364
365 status = pjsip_endpt_create(&pjsua.cp.factory, endpt_name,
366 &pjsua.endpt);
367 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000368 pjsua_perror(THIS_FILE, "Unable to create SIP endpoint", status);
Benny Prijono268ca612006-02-07 12:34:11 +0000369 return status;
370 }
371 }
372
373
374 /* Initialize transaction layer: */
375
Benny Prijono2f8992b2006-02-25 21:16:36 +0000376 status = pjsip_tsx_layer_init_module(pjsua.endpt);
Benny Prijono268ca612006-02-07 12:34:11 +0000377 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000378 pjsua_perror(THIS_FILE, "Transaction layer initialization error",
379 status);
Benny Prijono268ca612006-02-07 12:34:11 +0000380 goto on_error;
381 }
382
383 /* Initialize UA layer module: */
384
Benny Prijono2f8992b2006-02-25 21:16:36 +0000385 status = pjsip_ua_init_module( pjsua.endpt, NULL );
Benny Prijono268ca612006-02-07 12:34:11 +0000386 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000387 pjsua_perror(THIS_FILE, "UA layer initialization error", status);
Benny Prijono268ca612006-02-07 12:34:11 +0000388 goto on_error;
389 }
390
391 /* Initialize and register pjsua's application module: */
392
393 {
394 pjsip_module my_mod =
395 {
396 NULL, NULL, /* prev, next. */
397 { "mod-pjsua", 9 }, /* Name. */
398 -1, /* Id */
399 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
Benny Prijono268ca612006-02-07 12:34:11 +0000400 NULL, /* load() */
401 NULL, /* start() */
402 NULL, /* stop() */
403 NULL, /* unload() */
404 &mod_pjsua_on_rx_request, /* on_rx_request() */
405 &mod_pjsua_on_rx_response, /* on_rx_response() */
406 NULL, /* on_tx_request. */
407 NULL, /* on_tx_response() */
408 NULL, /* on_tsx_state() */
409 };
410
411 pjsua.mod = my_mod;
412
413 status = pjsip_endpt_register_module(pjsua.endpt, &pjsua.mod);
414 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000415 pjsua_perror(THIS_FILE, "Unable to register pjsua module",
416 status);
Benny Prijono268ca612006-02-07 12:34:11 +0000417 goto on_error;
418 }
419 }
420
421 /* Initialize invite session module: */
422
Benny Prijonoa91a0032006-02-26 21:23:45 +0000423 status = pjsua_call_init();
424 if (status != PJ_SUCCESS) {
425 pjsua_perror(THIS_FILE, "Invite usage initialization error",
426 status);
427 goto on_error;
Benny Prijono268ca612006-02-07 12:34:11 +0000428 }
429
Benny Prijonoccf95622006-02-07 18:48:01 +0000430 /* Done */
Benny Prijono268ca612006-02-07 12:34:11 +0000431
432 return PJ_SUCCESS;
433
434
435on_error:
436 pjsip_endpt_destroy(pjsua.endpt);
437 pjsua.endpt = NULL;
438 return status;
439}
440
441
Benny Prijono834aee32006-02-19 01:38:06 +0000442static int PJ_THREAD_FUNC pjsua_poll(void *arg)
Benny Prijono268ca612006-02-07 12:34:11 +0000443{
Benny Prijonof8baa872006-02-27 23:54:23 +0000444 pj_status_t last_err = 0;
445
Benny Prijono268ca612006-02-07 12:34:11 +0000446 PJ_UNUSED_ARG(arg);
447
Benny Prijono834aee32006-02-19 01:38:06 +0000448 do {
Benny Prijono268ca612006-02-07 12:34:11 +0000449 pj_time_val timeout = { 0, 10 };
Benny Prijonof8baa872006-02-27 23:54:23 +0000450 pj_status_t status;
451
452 status = pjsip_endpt_handle_events (pjsua.endpt, &timeout);
Benny Prijono080a2c42006-03-30 20:55:20 +0000453 if (status != PJ_SUCCESS && status != last_err) {
Benny Prijonof8baa872006-02-27 23:54:23 +0000454 last_err = status;
455 pjsua_perror(THIS_FILE, "handle_events() returned error", status);
456 }
Benny Prijono834aee32006-02-19 01:38:06 +0000457 } while (!pjsua.quit_flag);
Benny Prijono268ca612006-02-07 12:34:11 +0000458
459 return 0;
460}
461
462/*
463 * Initialize pjsua application.
Benny Prijonoccf95622006-02-07 18:48:01 +0000464 * This will initialize all libraries, create endpoint instance, and register
465 * pjsip modules.
Benny Prijono268ca612006-02-07 12:34:11 +0000466 */
467pj_status_t pjsua_init(void)
468{
Benny Prijono268ca612006-02-07 12:34:11 +0000469 pj_status_t status;
470
471 /* Init PJLIB logging: */
472
473 pj_log_set_level(pjsua.log_level);
474 pj_log_set_decor(pjsua.log_decor);
475
476
477 /* Init PJLIB: */
478
479 status = pj_init();
480 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000481 pjsua_perror(THIS_FILE, "pj_init() error", status);
Benny Prijono268ca612006-02-07 12:34:11 +0000482 return status;
483 }
484
Benny Prijonofccab712006-02-22 22:23:22 +0000485 /* Init PJLIB-UTIL: */
486
487 status = pjlib_util_init();
488 if (status != PJ_SUCCESS) {
489 pjsua_perror(THIS_FILE, "pjlib_util_init() error", status);
490 return status;
491 }
492
Benny Prijono268ca612006-02-07 12:34:11 +0000493 /* Init memory pool: */
494
495 /* Init caching pool. */
496 pj_caching_pool_init(&pjsua.cp, &pj_pool_factory_default_policy, 0);
497
498 /* Create memory pool for application. */
499 pjsua.pool = pj_pool_create(&pjsua.cp.factory, "pjsua", 4000, 4000, NULL);
500
501
Benny Prijono834aee32006-02-19 01:38:06 +0000502 /* Init PJSIP : */
Benny Prijonoccf95622006-02-07 18:48:01 +0000503
504 status = init_stack();
505 if (status != PJ_SUCCESS) {
506 pj_caching_pool_destroy(&pjsua.cp);
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000507 pjsua_perror(THIS_FILE, "Stack initialization has returned error",
508 status);
Benny Prijonoccf95622006-02-07 18:48:01 +0000509 return status;
510 }
511
Benny Prijono95196582006-02-09 00:13:40 +0000512
Benny Prijono834aee32006-02-19 01:38:06 +0000513 /* Init core SIMPLE module : */
514
515 pjsip_evsub_init_module(pjsua.endpt);
516
517 /* Init presence module: */
518
519 pjsip_pres_init_module( pjsua.endpt, pjsip_evsub_instance());
520
Benny Prijono26ff9062006-02-21 23:47:00 +0000521 /* Init xfer/REFER module */
522
523 pjsip_xfer_init_module( pjsua.endpt );
Benny Prijono834aee32006-02-19 01:38:06 +0000524
525 /* Init pjsua presence handler: */
526
527 pjsua_pres_init();
528
Benny Prijonob0808372006-03-02 21:18:58 +0000529 /* Init out-of-dialog MESSAGE request handler. */
530
531 pjsua_im_init();
532
Benny Prijono834aee32006-02-19 01:38:06 +0000533
Benny Prijono95196582006-02-09 00:13:40 +0000534 /* Init media endpoint: */
535
Benny Prijono275fd682006-03-22 11:59:11 +0000536 status = pjmedia_endpt_create(&pjsua.cp.factory,
537 pjsip_endpt_get_ioqueue(pjsua.endpt), 0,
538 &pjsua.med_endpt);
Benny Prijono95196582006-02-09 00:13:40 +0000539 if (status != PJ_SUCCESS) {
540 pj_caching_pool_destroy(&pjsua.cp);
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000541 pjsua_perror(THIS_FILE,
542 "Media stack initialization has returned error",
543 status);
Benny Prijono95196582006-02-09 00:13:40 +0000544 return status;
545 }
546
Benny Prijonoccf95622006-02-07 18:48:01 +0000547 /* Done. */
548 return PJ_SUCCESS;
549}
550
551
Benny Prijonoa91a0032006-02-26 21:23:45 +0000552/*
553 * Find account for incoming request.
554 */
555int pjsua_find_account_for_incoming(pjsip_rx_data *rdata)
556{
557 pjsip_uri *uri;
558 pjsip_sip_uri *sip_uri;
559 int acc_index;
560
561 uri = rdata->msg_info.to->uri;
562
563 /* Just return account #0 if To URI is not SIP: */
564 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
565 !PJSIP_URI_SCHEME_IS_SIPS(uri))
566 {
567 return 0;
568 }
569
570
571 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
572
573 /* Find account which has matching username and domain. */
574 for (acc_index=0; acc_index < pjsua.acc_cnt; ++acc_index) {
575
576 pjsua_acc *acc = &pjsua.acc[acc_index];
577
578 if (pj_stricmp(&acc->user_part, &sip_uri->user)==0 &&
579 pj_stricmp(&acc->host_part, &sip_uri->host)==0)
580 {
581 /* Match ! */
582 return acc_index;
583 }
584 }
585
586 /* No matching, try match domain part only. */
587 for (acc_index=0; acc_index < pjsua.acc_cnt; ++acc_index) {
588
589 pjsua_acc *acc = &pjsua.acc[acc_index];
590
591 if (pj_stricmp(&acc->host_part, &sip_uri->host)==0) {
592 /* Match ! */
593 return acc_index;
594 }
595 }
596
597 /* Still no match, just return account #0 */
598 return 0;
599}
600
601
602/*
603 * Find account for outgoing request.
604 */
605int pjsua_find_account_for_outgoing(const pj_str_t *url)
606{
607 PJ_UNUSED_ARG(url);
608
609 /* Just use account #0 */
610 return 0;
611}
612
Benny Prijonoccf95622006-02-07 18:48:01 +0000613
614/*
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000615 * Init media.
Benny Prijonoccf95622006-02-07 18:48:01 +0000616 */
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000617static pj_status_t init_media(void)
Benny Prijonoccf95622006-02-07 18:48:01 +0000618{
Benny Prijonob8c25182006-04-29 08:31:09 +0000619 int i;
Benny Prijono8e3344c2006-03-08 12:37:22 +0000620 unsigned options;
Benny Prijonob8c25182006-04-29 08:31:09 +0000621 unsigned clock_rate;
622 unsigned samples_per_frame;
623 pj_str_t codec_id;
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000624 pj_status_t status;
625
Benny Prijonob8c25182006-04-29 08:31:09 +0000626 /* Register all codecs */
Benny Prijono4381efe2006-03-16 14:24:26 +0000627#if PJMEDIA_HAS_SPEEX_CODEC
Benny Prijonob8c25182006-04-29 08:31:09 +0000628 /* Register speex. */
629 status = pjmedia_codec_speex_init(pjsua.med_endpt,
630 PJMEDIA_SPEEX_NO_UWB,
631 pjsua.quality, pjsua.complexity );
632 if (status != PJ_SUCCESS) {
633 pjsua_perror(THIS_FILE, "Error initializing Speex codec",
634 status);
635 return status;
636 }
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000637
Benny Prijonob8c25182006-04-29 08:31:09 +0000638 /* Set "speex/16000/1" to have highest priority */
639 codec_id = pj_str("speex/16000/1");
640 pjmedia_codec_mgr_set_codec_priority(
641 pjmedia_endpt_get_codec_mgr(pjsua.med_endpt),
642 &codec_id,
643 PJMEDIA_CODEC_PRIO_HIGHEST);
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000644
Benny Prijono4381efe2006-03-16 14:24:26 +0000645#endif /* PJMEDIA_HAS_SPEEX_CODEC */
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000646
Benny Prijono4381efe2006-03-16 14:24:26 +0000647#if PJMEDIA_HAS_GSM_CODEC
Benny Prijonob8c25182006-04-29 08:31:09 +0000648 /* Register GSM */
649 status = pjmedia_codec_gsm_init(pjsua.med_endpt);
650 if (status != PJ_SUCCESS) {
651 pjsua_perror(THIS_FILE, "Error initializing GSM codec",
652 status);
653 return status;
654 }
Benny Prijono4381efe2006-03-16 14:24:26 +0000655#endif /* PJMEDIA_HAS_GSM_CODEC */
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000656
Benny Prijono4381efe2006-03-16 14:24:26 +0000657#if PJMEDIA_HAS_G711_CODEC
Benny Prijonob8c25182006-04-29 08:31:09 +0000658 /* Register PCMA and PCMU */
659 status = pjmedia_codec_g711_init(pjsua.med_endpt);
660 if (status != PJ_SUCCESS) {
661 pjsua_perror(THIS_FILE, "Error initializing G711 codec",
662 status);
663 return status;
664 }
Benny Prijono4381efe2006-03-16 14:24:26 +0000665#endif /* PJMEDIA_HAS_G711_CODEC */
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000666
Benny Prijono15953012006-04-27 22:37:08 +0000667#if PJMEDIA_HAS_L16_CODEC
Benny Prijonob8c25182006-04-29 08:31:09 +0000668 /* Register L16 family codecs, but disable all */
669 status = pjmedia_codec_l16_init(pjsua.med_endpt, 0);
670 if (status != PJ_SUCCESS) {
671 pjsua_perror(THIS_FILE, "Error initializing L16 codecs",
672 status);
673 return status;
674 }
Benny Prijono15953012006-04-27 22:37:08 +0000675
Benny Prijonob8c25182006-04-29 08:31:09 +0000676 /* Disable ALL L16 codecs */
677 codec_id = pj_str("L16");
678 pjmedia_codec_mgr_set_codec_priority(
679 pjmedia_endpt_get_codec_mgr(pjsua.med_endpt),
680 &codec_id,
681 PJMEDIA_CODEC_PRIO_DISABLED);
Benny Prijono15953012006-04-27 22:37:08 +0000682
Benny Prijono15953012006-04-27 22:37:08 +0000683#endif /* PJMEDIA_HAS_L16_CODEC */
684
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000685
Benny Prijono1d8d6082006-04-29 12:38:25 +0000686 /* Enable those codecs that user put with "--add-codec", and move
687 * the priority to top
Benny Prijonob8c25182006-04-29 08:31:09 +0000688 */
Benny Prijonob8c25182006-04-29 08:31:09 +0000689 for (i=0; i<pjsua.codec_cnt; ++i) {
690 pjmedia_codec_mgr_set_codec_priority(
691 pjmedia_endpt_get_codec_mgr(pjsua.med_endpt),
692 &pjsua.codec_arg[i],
693 PJMEDIA_CODEC_PRIO_NEXT_HIGHER);
694 }
695
696
Benny Prijono8e3344c2006-03-08 12:37:22 +0000697 /* Init options for conference bridge. */
698 options = 0;
699 if (pjsua.no_mic)
700 options |= PJMEDIA_CONF_NO_MIC;
701
Benny Prijonof8baa872006-02-27 23:54:23 +0000702 /* Init conference bridge. */
Benny Prijonob8c25182006-04-29 08:31:09 +0000703 clock_rate = pjsua.clock_rate ? pjsua.clock_rate : 16000;
704 samples_per_frame = clock_rate * 20 / 1000;
Benny Prijonof8baa872006-02-27 23:54:23 +0000705 status = pjmedia_conf_create(pjsua.pool,
706 pjsua.max_calls+PJSUA_CONF_MORE_PORTS,
Benny Prijonob8c25182006-04-29 08:31:09 +0000707 clock_rate,
Benny Prijonod0659a32006-03-16 19:03:07 +0000708 1, /* mono */
Benny Prijonob8c25182006-04-29 08:31:09 +0000709 samples_per_frame,
710 16,
Benny Prijono8e3344c2006-03-08 12:37:22 +0000711 options,
Benny Prijono08e0d062006-03-04 14:52:44 +0000712 &pjsua.mconf);
Benny Prijonof8baa872006-02-27 23:54:23 +0000713 if (status != PJ_SUCCESS) {
Benny Prijonof8baa872006-02-27 23:54:23 +0000714 pjsua_perror(THIS_FILE,
715 "Media stack initialization has returned error",
716 status);
717 return status;
718 }
719
Benny Prijonode380582006-03-15 19:32:41 +0000720 /* Add NULL port to the bridge. */
Benny Prijonob8c25182006-04-29 08:31:09 +0000721 status = pjmedia_null_port_create( pjsua.pool, clock_rate,
Benny Prijonod0659a32006-03-16 19:03:07 +0000722 1, /* mono */
Benny Prijonob8c25182006-04-29 08:31:09 +0000723 samples_per_frame, 16,
Benny Prijonode380582006-03-15 19:32:41 +0000724 &pjsua.null_port);
725 pjmedia_conf_add_port( pjsua.mconf, pjsua.pool, pjsua.null_port,
726 &pjsua.null_port->info.name, NULL );
727
Benny Prijono39879152006-02-23 02:09:10 +0000728 /* Create WAV file player if required: */
729
730 if (pjsua.wav_file) {
Benny Prijono39879152006-02-23 02:09:10 +0000731 pj_str_t port_name;
732
733 /* Create the file player port. */
Benny Prijono15953012006-04-27 22:37:08 +0000734 status = pjmedia_wav_player_port_create( pjsua.pool, pjsua.wav_file,
735 0, 0, -1, NULL,
Benny Prijonode380582006-03-15 19:32:41 +0000736 &pjsua.file_port);
Benny Prijono39879152006-02-23 02:09:10 +0000737 if (status != PJ_SUCCESS) {
738 pjsua_perror(THIS_FILE,
739 "Error playing media file",
740 status);
741 return status;
742 }
743
744 /* Add port to conference bridge: */
Benny Prijonode380582006-03-15 19:32:41 +0000745 status = pjmedia_conf_add_port(pjsua.mconf, pjsua.pool,
746 pjsua.file_port,
Benny Prijono39879152006-02-23 02:09:10 +0000747 pj_cstr(&port_name, pjsua.wav_file),
748 &pjsua.wav_slot);
749 if (status != PJ_SUCCESS) {
750 pjsua_perror(THIS_FILE,
751 "Unable to add file player to conference bridge",
752 status);
753 return status;
754 }
755 }
756
757
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000758 return PJ_SUCCESS;
759}
760
761
762/*
763 * Start pjsua stack.
764 * This will start the registration process, if registration is configured.
765 */
766pj_status_t pjsua_start(void)
767{
768 int i; /* Must be signed */
769 pjsip_transport *udp_transport;
770 pj_status_t status = PJ_SUCCESS;
771
772 /*
773 * Init media subsystem (codecs, conference bridge, et all).
774 */
775 status = init_media();
776 if (status != PJ_SUCCESS)
777 return status;
778
Benny Prijono268ca612006-02-07 12:34:11 +0000779 /* Init sockets (STUN etc): */
Benny Prijonoa91a0032006-02-26 21:23:45 +0000780 for (i=0; i<(int)pjsua.max_calls; ++i) {
781 status = init_sockets(i==0, &pjsua.calls[i].skinfo);
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000782 if (status != PJ_SUCCESS) {
783 pjsua_perror(THIS_FILE, "init_sockets() has returned error",
784 status);
Benny Prijonoa91a0032006-02-26 21:23:45 +0000785 --i;
786 if (i >= 0)
787 pj_sock_close(pjsua.sip_sock);
788 while (i >= 0) {
789 pj_sock_close(pjsua.calls[i].skinfo.rtp_sock);
790 pj_sock_close(pjsua.calls[i].skinfo.rtcp_sock);
791 }
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000792 return status;
793 }
Benny Prijono268ca612006-02-07 12:34:11 +0000794 }
795
Benny Prijonoccf95622006-02-07 18:48:01 +0000796 /* Add UDP transport: */
Benny Prijono268ca612006-02-07 12:34:11 +0000797
Benny Prijonoccf95622006-02-07 18:48:01 +0000798 {
799 /* Init the published name for the transport.
800 * Depending whether STUN is used, this may be the STUN mapped
801 * address, or socket's bound address.
802 */
803 pjsip_host_port addr_name;
804
805 addr_name.host.ptr = pj_inet_ntoa(pjsua.sip_sock_name.sin_addr);
Benny Prijonof3195072006-02-14 21:15:30 +0000806 addr_name.host.slen = pj_ansi_strlen(addr_name.host.ptr);
Benny Prijonoccf95622006-02-07 18:48:01 +0000807 addr_name.port = pj_ntohs(pjsua.sip_sock_name.sin_port);
808
809 /* Create UDP transport from previously created UDP socket: */
810
811 status = pjsip_udp_transport_attach( pjsua.endpt, pjsua.sip_sock,
812 &addr_name, 1,
813 &udp_transport);
814 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000815 pjsua_perror(THIS_FILE, "Unable to start UDP transport",
816 status);
Benny Prijonoccf95622006-02-07 18:48:01 +0000817 return status;
818 }
Benny Prijono268ca612006-02-07 12:34:11 +0000819 }
820
Benny Prijonoccf95622006-02-07 18:48:01 +0000821 /* Initialize Contact URI, if one is not specified: */
Benny Prijonoa91a0032006-02-26 21:23:45 +0000822 for (i=0; i<pjsua.acc_cnt; ++i) {
Benny Prijonoccf95622006-02-07 18:48:01 +0000823
824 pjsip_uri *uri;
825 pjsip_sip_uri *sip_uri;
Benny Prijonoccf95622006-02-07 18:48:01 +0000826
Benny Prijonoccf95622006-02-07 18:48:01 +0000827 /* Need to parse local_uri to get the elements: */
828
Benny Prijonoa91a0032006-02-26 21:23:45 +0000829 uri = pjsip_parse_uri(pjsua.pool, pjsua.acc[i].local_uri.ptr,
830 pjsua.acc[i].local_uri.slen, 0);
Benny Prijonoccf95622006-02-07 18:48:01 +0000831 if (uri == NULL) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000832 pjsua_perror(THIS_FILE, "Invalid local URI",
833 PJSIP_EINVALIDURI);
Benny Prijonoccf95622006-02-07 18:48:01 +0000834 return PJSIP_EINVALIDURI;
835 }
836
Benny Prijonoccf95622006-02-07 18:48:01 +0000837 /* Local URI MUST be a SIP or SIPS: */
838
Benny Prijonoa91a0032006-02-26 21:23:45 +0000839 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
840 !PJSIP_URI_SCHEME_IS_SIPS(uri))
841 {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000842 pjsua_perror(THIS_FILE, "Invalid local URI",
843 PJSIP_EINVALIDSCHEME);
Benny Prijonoccf95622006-02-07 18:48:01 +0000844 return PJSIP_EINVALIDSCHEME;
845 }
846
847
848 /* Get the SIP URI object: */
849
850 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
851
Benny Prijonoa91a0032006-02-26 21:23:45 +0000852 pjsua.acc[i].user_part = sip_uri->user;
853 pjsua.acc[i].host_part = sip_uri->host;
Benny Prijonoccf95622006-02-07 18:48:01 +0000854
Benny Prijonoa91a0032006-02-26 21:23:45 +0000855 if (pjsua.acc[i].contact_uri.slen == 0 &&
856 pjsua.acc[i].local_uri.slen)
857 {
858 char contact[128];
859 int len;
Benny Prijonoccf95622006-02-07 18:48:01 +0000860
Benny Prijonoa91a0032006-02-26 21:23:45 +0000861 /* The local Contact is the username@ip-addr, where
862 * - username is taken from the local URI,
863 * - ip-addr in UDP transport's address name (which may have been
864 * resolved from STUN.
865 */
866
867 /* Build temporary contact string. */
Benny Prijonoccf95622006-02-07 18:48:01 +0000868
Benny Prijonoa91a0032006-02-26 21:23:45 +0000869 if (sip_uri->user.slen) {
Benny Prijonoccf95622006-02-07 18:48:01 +0000870
Benny Prijonoa91a0032006-02-26 21:23:45 +0000871 /* With the user part. */
Benny Prijonoed811d72006-03-10 12:57:12 +0000872 len = pj_ansi_snprintf(contact, sizeof(contact),
Benny Prijonoa91a0032006-02-26 21:23:45 +0000873 "<sip:%.*s@%.*s:%d>",
874 (int)sip_uri->user.slen,
875 sip_uri->user.ptr,
876 (int)udp_transport->local_name.host.slen,
877 udp_transport->local_name.host.ptr,
878 udp_transport->local_name.port);
879 } else {
880
881 /* Without user part */
882
Benny Prijonoed811d72006-03-10 12:57:12 +0000883 len = pj_ansi_snprintf(contact, sizeof(contact),
Benny Prijonoa91a0032006-02-26 21:23:45 +0000884 "<sip:%.*s:%d>",
885 (int)udp_transport->local_name.host.slen,
886 udp_transport->local_name.host.ptr,
887 udp_transport->local_name.port);
888 }
889
890 if (len < 1 || len >= sizeof(contact)) {
891 pjsua_perror(THIS_FILE, "Invalid Contact", PJSIP_EURITOOLONG);
892 return PJSIP_EURITOOLONG;
893 }
894
895 /* Duplicate Contact uri. */
896
897 pj_strdup2(pjsua.pool, &pjsua.acc[i].contact_uri, contact);
898
Benny Prijonoccf95622006-02-07 18:48:01 +0000899 }
Benny Prijonoccf95622006-02-07 18:48:01 +0000900 }
901
Benny Prijono95196582006-02-09 00:13:40 +0000902 /* If outbound_proxy is specified, put it in the route_set: */
Benny Prijonoccf95622006-02-07 18:48:01 +0000903
Benny Prijono95196582006-02-09 00:13:40 +0000904 if (pjsua.outbound_proxy.slen) {
905
906 pjsip_route_hdr *route;
907 const pj_str_t hname = { "Route", 5 };
908 int parsed_len;
909
910 route = pjsip_parse_hdr( pjsua.pool, &hname,
911 pjsua.outbound_proxy.ptr,
912 pjsua.outbound_proxy.slen,
913 &parsed_len);
914 if (route == NULL) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000915 pjsua_perror(THIS_FILE, "Invalid outbound proxy URL",
916 PJSIP_EINVALIDURI);
Benny Prijono95196582006-02-09 00:13:40 +0000917 return PJSIP_EINVALIDURI;
918 }
919
Benny Prijonoa91a0032006-02-26 21:23:45 +0000920 for (i=0; i<pjsua.acc_cnt; ++i) {
921 pj_list_push_front(&pjsua.acc[i].route_set, route);
922 }
Benny Prijono95196582006-02-09 00:13:40 +0000923 }
Benny Prijonoccf95622006-02-07 18:48:01 +0000924
925
Benny Prijono268ca612006-02-07 12:34:11 +0000926 /* Create worker thread(s), if required: */
927
928 for (i=0; i<pjsua.thread_cnt; ++i) {
Benny Prijono834aee32006-02-19 01:38:06 +0000929 status = pj_thread_create( pjsua.pool, "pjsua", &pjsua_poll,
Benny Prijono268ca612006-02-07 12:34:11 +0000930 NULL, 0, 0, &pjsua.threads[i]);
931 if (status != PJ_SUCCESS) {
932 pjsua.quit_flag = 1;
933 for (--i; i>=0; --i) {
934 pj_thread_join(pjsua.threads[i]);
935 pj_thread_destroy(pjsua.threads[i]);
936 }
Benny Prijono268ca612006-02-07 12:34:11 +0000937 return status;
938 }
939 }
940
Benny Prijonoccf95622006-02-07 18:48:01 +0000941 /* Start registration: */
942
943 /* Create client registration session: */
Benny Prijonoa91a0032006-02-26 21:23:45 +0000944 for (i=0; i<pjsua.acc_cnt; ++i) {
945 status = pjsua_regc_init(i);
946 if (status != PJ_SUCCESS)
947 return status;
Benny Prijonoccf95622006-02-07 18:48:01 +0000948
Benny Prijonoa91a0032006-02-26 21:23:45 +0000949 /* Perform registration, if required. */
950 if (pjsua.acc[i].regc) {
951 pjsua_regc_update(i, 1);
952 }
953 }
Benny Prijonoccf95622006-02-07 18:48:01 +0000954
Benny Prijonoa91a0032006-02-26 21:23:45 +0000955
956 /* Find account for outgoing preence subscription */
957 for (i=0; i<pjsua.buddy_cnt; ++i) {
958 pjsua.buddies[i].acc_index =
959 pjsua_find_account_for_outgoing(&pjsua.buddies[i].uri);
Benny Prijonoccf95622006-02-07 18:48:01 +0000960 }
961
962
Benny Prijono834aee32006-02-19 01:38:06 +0000963 PJ_LOG(3,(THIS_FILE, "PJSUA version %s started", PJ_VERSION));
Benny Prijono268ca612006-02-07 12:34:11 +0000964 return PJ_SUCCESS;
965}
966
967
Benny Prijono834aee32006-02-19 01:38:06 +0000968/* Sleep with polling */
969static void busy_sleep(unsigned msec)
970{
971 pj_time_val timeout, now;
972
973 pj_gettimeofday(&timeout);
974 timeout.msec += msec;
975 pj_time_val_normalize(&timeout);
976
977 do {
978 pjsua_poll(NULL);
979 pj_gettimeofday(&now);
980 } while (PJ_TIME_VAL_LT(now, timeout));
981}
982
Benny Prijono268ca612006-02-07 12:34:11 +0000983/*
984 * Destroy pjsua.
985 */
986pj_status_t pjsua_destroy(void)
987{
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000988 int i; /* Must be signed */
Benny Prijono268ca612006-02-07 12:34:11 +0000989
990 /* Signal threads to quit: */
Benny Prijono268ca612006-02-07 12:34:11 +0000991 pjsua.quit_flag = 1;
992
Benny Prijonoa91a0032006-02-26 21:23:45 +0000993 /* Terminate all calls. */
Benny Prijono1a174142006-03-01 20:46:13 +0000994 pjsua_call_hangup_all();
Benny Prijonoa91a0032006-02-26 21:23:45 +0000995
996 /* Terminate all presence subscriptions. */
997 pjsua_pres_shutdown();
998
999 /* Unregister, if required: */
1000 for (i=0; i<pjsua.acc_cnt; ++i) {
1001 if (pjsua.acc[i].regc) {
1002 pjsua_regc_update(i, 0);
1003 }
1004 }
1005
Benny Prijono268ca612006-02-07 12:34:11 +00001006 /* Wait worker threads to quit: */
Benny Prijono268ca612006-02-07 12:34:11 +00001007 for (i=0; i<pjsua.thread_cnt; ++i) {
1008
Benny Prijonoe4f2abb2006-02-10 14:04:05 +00001009 if (pjsua.threads[i]) {
1010 pj_thread_join(pjsua.threads[i]);
1011 pj_thread_destroy(pjsua.threads[i]);
1012 pjsua.threads[i] = NULL;
1013 }
Benny Prijono268ca612006-02-07 12:34:11 +00001014 }
1015
Benny Prijono834aee32006-02-19 01:38:06 +00001016
Benny Prijono834aee32006-02-19 01:38:06 +00001017 /* Wait for some time to allow unregistration to complete: */
1018 PJ_LOG(4,(THIS_FILE, "Shutting down..."));
1019 busy_sleep(1000);
1020
Benny Prijono26ff9062006-02-21 23:47:00 +00001021 /* Destroy conference bridge. */
1022 if (pjsua.mconf)
1023 pjmedia_conf_destroy(pjsua.mconf);
1024
Benny Prijonode380582006-03-15 19:32:41 +00001025 /* Destroy file port */
Benny Prijono4381efe2006-03-16 14:24:26 +00001026 if (pjsua.file_port)
1027 pjmedia_port_destroy(pjsua.file_port);
Benny Prijonode380582006-03-15 19:32:41 +00001028
1029 /* Destroy null port. */
Benny Prijono4381efe2006-03-16 14:24:26 +00001030 if (pjsua.null_port)
1031 pjmedia_port_destroy(pjsua.null_port);
Benny Prijonode380582006-03-15 19:32:41 +00001032
1033
Benny Prijonoeb30bf52006-03-04 20:43:52 +00001034 /* Shutdown all codecs: */
Benny Prijonob8c25182006-04-29 08:31:09 +00001035#if PJMEDIA_HAS_SPEEX_CODEC
1036 pjmedia_codec_speex_deinit();
1037#endif /* PJMEDIA_HAS_SPEEX_CODEC */
1038
1039#if PJMEDIA_HAS_GSM_CODEC
1040 pjmedia_codec_gsm_deinit();
1041#endif /* PJMEDIA_HAS_GSM_CODEC */
1042
1043#if PJMEDIA_HAS_G711_CODEC
1044 pjmedia_codec_g711_deinit();
1045#endif /* PJMEDIA_HAS_G711_CODEC */
1046
1047#if PJMEDIA_HAS_L16_CODEC
1048 pjmedia_codec_l16_deinit();
1049#endif /* PJMEDIA_HAS_L16_CODEC */
Benny Prijonoeb30bf52006-03-04 20:43:52 +00001050
1051 /* Destroy media endpoint. */
1052
1053 pjmedia_endpt_destroy(pjsua.med_endpt);
1054
Benny Prijono268ca612006-02-07 12:34:11 +00001055 /* Destroy endpoint. */
Benny Prijono84126ab2006-02-09 09:30:09 +00001056
Benny Prijono268ca612006-02-07 12:34:11 +00001057 pjsip_endpt_destroy(pjsua.endpt);
1058 pjsua.endpt = NULL;
1059
1060 /* Destroy caching pool. */
Benny Prijono84126ab2006-02-09 09:30:09 +00001061
Benny Prijono268ca612006-02-07 12:34:11 +00001062 pj_caching_pool_destroy(&pjsua.cp);
1063
1064
1065 /* Done. */
1066
1067 return PJ_SUCCESS;
1068}
1069