blob: 034262abca2bb594db4825b8f0ff7af9e4793b69 [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 Prijonob8c25182006-04-29 08:31:09 +0000686 /* If user specifies the exact codec to be used, then disable all codecs
687 * and only enable those specific codecs.
688 */
689 if (pjsua.codec_cnt != 0) {
690 codec_id = pj_str("");
691 pjmedia_codec_mgr_set_codec_priority(
692 pjmedia_endpt_get_codec_mgr(pjsua.med_endpt),
693 &codec_id,
694 PJMEDIA_CODEC_PRIO_DISABLED);
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000695 }
Benny Prijonoccf95622006-02-07 18:48:01 +0000696
Benny Prijonob8c25182006-04-29 08:31:09 +0000697
698
699 for (i=0; i<pjsua.codec_cnt; ++i) {
700 pjmedia_codec_mgr_set_codec_priority(
701 pjmedia_endpt_get_codec_mgr(pjsua.med_endpt),
702 &pjsua.codec_arg[i],
703 PJMEDIA_CODEC_PRIO_NEXT_HIGHER);
704 }
705
706
Benny Prijono8e3344c2006-03-08 12:37:22 +0000707 /* Init options for conference bridge. */
708 options = 0;
709 if (pjsua.no_mic)
710 options |= PJMEDIA_CONF_NO_MIC;
711
Benny Prijonof8baa872006-02-27 23:54:23 +0000712 /* Init conference bridge. */
Benny Prijonob8c25182006-04-29 08:31:09 +0000713 clock_rate = pjsua.clock_rate ? pjsua.clock_rate : 16000;
714 samples_per_frame = clock_rate * 20 / 1000;
Benny Prijonof8baa872006-02-27 23:54:23 +0000715 status = pjmedia_conf_create(pjsua.pool,
716 pjsua.max_calls+PJSUA_CONF_MORE_PORTS,
Benny Prijonob8c25182006-04-29 08:31:09 +0000717 clock_rate,
Benny Prijonod0659a32006-03-16 19:03:07 +0000718 1, /* mono */
Benny Prijonob8c25182006-04-29 08:31:09 +0000719 samples_per_frame,
720 16,
Benny Prijono8e3344c2006-03-08 12:37:22 +0000721 options,
Benny Prijono08e0d062006-03-04 14:52:44 +0000722 &pjsua.mconf);
Benny Prijonof8baa872006-02-27 23:54:23 +0000723 if (status != PJ_SUCCESS) {
Benny Prijonof8baa872006-02-27 23:54:23 +0000724 pjsua_perror(THIS_FILE,
725 "Media stack initialization has returned error",
726 status);
727 return status;
728 }
729
Benny Prijonode380582006-03-15 19:32:41 +0000730 /* Add NULL port to the bridge. */
Benny Prijonob8c25182006-04-29 08:31:09 +0000731 status = pjmedia_null_port_create( pjsua.pool, clock_rate,
Benny Prijonod0659a32006-03-16 19:03:07 +0000732 1, /* mono */
Benny Prijonob8c25182006-04-29 08:31:09 +0000733 samples_per_frame, 16,
Benny Prijonode380582006-03-15 19:32:41 +0000734 &pjsua.null_port);
735 pjmedia_conf_add_port( pjsua.mconf, pjsua.pool, pjsua.null_port,
736 &pjsua.null_port->info.name, NULL );
737
Benny Prijono39879152006-02-23 02:09:10 +0000738 /* Create WAV file player if required: */
739
740 if (pjsua.wav_file) {
Benny Prijono39879152006-02-23 02:09:10 +0000741 pj_str_t port_name;
742
743 /* Create the file player port. */
Benny Prijono15953012006-04-27 22:37:08 +0000744 status = pjmedia_wav_player_port_create( pjsua.pool, pjsua.wav_file,
745 0, 0, -1, NULL,
Benny Prijonode380582006-03-15 19:32:41 +0000746 &pjsua.file_port);
Benny Prijono39879152006-02-23 02:09:10 +0000747 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: */
Benny Prijonode380582006-03-15 19:32:41 +0000755 status = pjmedia_conf_add_port(pjsua.mconf, pjsua.pool,
756 pjsua.file_port,
Benny Prijono39879152006-02-23 02:09:10 +0000757 pj_cstr(&port_name, pjsua.wav_file),
758 &pjsua.wav_slot);
759 if (status != PJ_SUCCESS) {
760 pjsua_perror(THIS_FILE,
761 "Unable to add file player to conference bridge",
762 status);
763 return status;
764 }
765 }
766
767
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000768 return PJ_SUCCESS;
769}
770
771
772/*
773 * Start pjsua stack.
774 * This will start the registration process, if registration is configured.
775 */
776pj_status_t pjsua_start(void)
777{
778 int i; /* Must be signed */
779 pjsip_transport *udp_transport;
780 pj_status_t status = PJ_SUCCESS;
781
782 /*
783 * Init media subsystem (codecs, conference bridge, et all).
784 */
785 status = init_media();
786 if (status != PJ_SUCCESS)
787 return status;
788
Benny Prijono268ca612006-02-07 12:34:11 +0000789 /* Init sockets (STUN etc): */
Benny Prijonoa91a0032006-02-26 21:23:45 +0000790 for (i=0; i<(int)pjsua.max_calls; ++i) {
791 status = init_sockets(i==0, &pjsua.calls[i].skinfo);
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000792 if (status != PJ_SUCCESS) {
793 pjsua_perror(THIS_FILE, "init_sockets() has returned error",
794 status);
Benny Prijonoa91a0032006-02-26 21:23:45 +0000795 --i;
796 if (i >= 0)
797 pj_sock_close(pjsua.sip_sock);
798 while (i >= 0) {
799 pj_sock_close(pjsua.calls[i].skinfo.rtp_sock);
800 pj_sock_close(pjsua.calls[i].skinfo.rtcp_sock);
801 }
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000802 return status;
803 }
Benny Prijono268ca612006-02-07 12:34:11 +0000804 }
805
Benny Prijonoccf95622006-02-07 18:48:01 +0000806 /* Add UDP transport: */
Benny Prijono268ca612006-02-07 12:34:11 +0000807
Benny Prijonoccf95622006-02-07 18:48:01 +0000808 {
809 /* Init the published name for the transport.
810 * Depending whether STUN is used, this may be the STUN mapped
811 * address, or socket's bound address.
812 */
813 pjsip_host_port addr_name;
814
815 addr_name.host.ptr = pj_inet_ntoa(pjsua.sip_sock_name.sin_addr);
Benny Prijonof3195072006-02-14 21:15:30 +0000816 addr_name.host.slen = pj_ansi_strlen(addr_name.host.ptr);
Benny Prijonoccf95622006-02-07 18:48:01 +0000817 addr_name.port = pj_ntohs(pjsua.sip_sock_name.sin_port);
818
819 /* Create UDP transport from previously created UDP socket: */
820
821 status = pjsip_udp_transport_attach( pjsua.endpt, pjsua.sip_sock,
822 &addr_name, 1,
823 &udp_transport);
824 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000825 pjsua_perror(THIS_FILE, "Unable to start UDP transport",
826 status);
Benny Prijonoccf95622006-02-07 18:48:01 +0000827 return status;
828 }
Benny Prijono268ca612006-02-07 12:34:11 +0000829 }
830
Benny Prijonoccf95622006-02-07 18:48:01 +0000831 /* Initialize Contact URI, if one is not specified: */
Benny Prijonoa91a0032006-02-26 21:23:45 +0000832 for (i=0; i<pjsua.acc_cnt; ++i) {
Benny Prijonoccf95622006-02-07 18:48:01 +0000833
834 pjsip_uri *uri;
835 pjsip_sip_uri *sip_uri;
Benny Prijonoccf95622006-02-07 18:48:01 +0000836
Benny Prijonoccf95622006-02-07 18:48:01 +0000837 /* Need to parse local_uri to get the elements: */
838
Benny Prijonoa91a0032006-02-26 21:23:45 +0000839 uri = pjsip_parse_uri(pjsua.pool, pjsua.acc[i].local_uri.ptr,
840 pjsua.acc[i].local_uri.slen, 0);
Benny Prijonoccf95622006-02-07 18:48:01 +0000841 if (uri == NULL) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000842 pjsua_perror(THIS_FILE, "Invalid local URI",
843 PJSIP_EINVALIDURI);
Benny Prijonoccf95622006-02-07 18:48:01 +0000844 return PJSIP_EINVALIDURI;
845 }
846
Benny Prijonoccf95622006-02-07 18:48:01 +0000847 /* Local URI MUST be a SIP or SIPS: */
848
Benny Prijonoa91a0032006-02-26 21:23:45 +0000849 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
850 !PJSIP_URI_SCHEME_IS_SIPS(uri))
851 {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000852 pjsua_perror(THIS_FILE, "Invalid local URI",
853 PJSIP_EINVALIDSCHEME);
Benny Prijonoccf95622006-02-07 18:48:01 +0000854 return PJSIP_EINVALIDSCHEME;
855 }
856
857
858 /* Get the SIP URI object: */
859
860 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
861
Benny Prijonoa91a0032006-02-26 21:23:45 +0000862 pjsua.acc[i].user_part = sip_uri->user;
863 pjsua.acc[i].host_part = sip_uri->host;
Benny Prijonoccf95622006-02-07 18:48:01 +0000864
Benny Prijonoa91a0032006-02-26 21:23:45 +0000865 if (pjsua.acc[i].contact_uri.slen == 0 &&
866 pjsua.acc[i].local_uri.slen)
867 {
868 char contact[128];
869 int len;
Benny Prijonoccf95622006-02-07 18:48:01 +0000870
Benny Prijonoa91a0032006-02-26 21:23:45 +0000871 /* The local Contact is the username@ip-addr, where
872 * - username is taken from the local URI,
873 * - ip-addr in UDP transport's address name (which may have been
874 * resolved from STUN.
875 */
876
877 /* Build temporary contact string. */
Benny Prijonoccf95622006-02-07 18:48:01 +0000878
Benny Prijonoa91a0032006-02-26 21:23:45 +0000879 if (sip_uri->user.slen) {
Benny Prijonoccf95622006-02-07 18:48:01 +0000880
Benny Prijonoa91a0032006-02-26 21:23:45 +0000881 /* With the user part. */
Benny Prijonoed811d72006-03-10 12:57:12 +0000882 len = pj_ansi_snprintf(contact, sizeof(contact),
Benny Prijonoa91a0032006-02-26 21:23:45 +0000883 "<sip:%.*s@%.*s:%d>",
884 (int)sip_uri->user.slen,
885 sip_uri->user.ptr,
886 (int)udp_transport->local_name.host.slen,
887 udp_transport->local_name.host.ptr,
888 udp_transport->local_name.port);
889 } else {
890
891 /* Without user part */
892
Benny Prijonoed811d72006-03-10 12:57:12 +0000893 len = pj_ansi_snprintf(contact, sizeof(contact),
Benny Prijonoa91a0032006-02-26 21:23:45 +0000894 "<sip:%.*s:%d>",
895 (int)udp_transport->local_name.host.slen,
896 udp_transport->local_name.host.ptr,
897 udp_transport->local_name.port);
898 }
899
900 if (len < 1 || len >= sizeof(contact)) {
901 pjsua_perror(THIS_FILE, "Invalid Contact", PJSIP_EURITOOLONG);
902 return PJSIP_EURITOOLONG;
903 }
904
905 /* Duplicate Contact uri. */
906
907 pj_strdup2(pjsua.pool, &pjsua.acc[i].contact_uri, contact);
908
Benny Prijonoccf95622006-02-07 18:48:01 +0000909 }
Benny Prijonoccf95622006-02-07 18:48:01 +0000910 }
911
Benny Prijono95196582006-02-09 00:13:40 +0000912 /* If outbound_proxy is specified, put it in the route_set: */
Benny Prijonoccf95622006-02-07 18:48:01 +0000913
Benny Prijono95196582006-02-09 00:13:40 +0000914 if (pjsua.outbound_proxy.slen) {
915
916 pjsip_route_hdr *route;
917 const pj_str_t hname = { "Route", 5 };
918 int parsed_len;
919
920 route = pjsip_parse_hdr( pjsua.pool, &hname,
921 pjsua.outbound_proxy.ptr,
922 pjsua.outbound_proxy.slen,
923 &parsed_len);
924 if (route == NULL) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000925 pjsua_perror(THIS_FILE, "Invalid outbound proxy URL",
926 PJSIP_EINVALIDURI);
Benny Prijono95196582006-02-09 00:13:40 +0000927 return PJSIP_EINVALIDURI;
928 }
929
Benny Prijonoa91a0032006-02-26 21:23:45 +0000930 for (i=0; i<pjsua.acc_cnt; ++i) {
931 pj_list_push_front(&pjsua.acc[i].route_set, route);
932 }
Benny Prijono95196582006-02-09 00:13:40 +0000933 }
Benny Prijonoccf95622006-02-07 18:48:01 +0000934
935
Benny Prijono268ca612006-02-07 12:34:11 +0000936 /* Create worker thread(s), if required: */
937
938 for (i=0; i<pjsua.thread_cnt; ++i) {
Benny Prijono834aee32006-02-19 01:38:06 +0000939 status = pj_thread_create( pjsua.pool, "pjsua", &pjsua_poll,
Benny Prijono268ca612006-02-07 12:34:11 +0000940 NULL, 0, 0, &pjsua.threads[i]);
941 if (status != PJ_SUCCESS) {
942 pjsua.quit_flag = 1;
943 for (--i; i>=0; --i) {
944 pj_thread_join(pjsua.threads[i]);
945 pj_thread_destroy(pjsua.threads[i]);
946 }
Benny Prijono268ca612006-02-07 12:34:11 +0000947 return status;
948 }
949 }
950
Benny Prijonoccf95622006-02-07 18:48:01 +0000951 /* Start registration: */
952
953 /* Create client registration session: */
Benny Prijonoa91a0032006-02-26 21:23:45 +0000954 for (i=0; i<pjsua.acc_cnt; ++i) {
955 status = pjsua_regc_init(i);
956 if (status != PJ_SUCCESS)
957 return status;
Benny Prijonoccf95622006-02-07 18:48:01 +0000958
Benny Prijonoa91a0032006-02-26 21:23:45 +0000959 /* Perform registration, if required. */
960 if (pjsua.acc[i].regc) {
961 pjsua_regc_update(i, 1);
962 }
963 }
Benny Prijonoccf95622006-02-07 18:48:01 +0000964
Benny Prijonoa91a0032006-02-26 21:23:45 +0000965
966 /* Find account for outgoing preence subscription */
967 for (i=0; i<pjsua.buddy_cnt; ++i) {
968 pjsua.buddies[i].acc_index =
969 pjsua_find_account_for_outgoing(&pjsua.buddies[i].uri);
Benny Prijonoccf95622006-02-07 18:48:01 +0000970 }
971
972
Benny Prijono834aee32006-02-19 01:38:06 +0000973 PJ_LOG(3,(THIS_FILE, "PJSUA version %s started", PJ_VERSION));
Benny Prijono268ca612006-02-07 12:34:11 +0000974 return PJ_SUCCESS;
975}
976
977
Benny Prijono834aee32006-02-19 01:38:06 +0000978/* Sleep with polling */
979static void busy_sleep(unsigned msec)
980{
981 pj_time_val timeout, now;
982
983 pj_gettimeofday(&timeout);
984 timeout.msec += msec;
985 pj_time_val_normalize(&timeout);
986
987 do {
988 pjsua_poll(NULL);
989 pj_gettimeofday(&now);
990 } while (PJ_TIME_VAL_LT(now, timeout));
991}
992
Benny Prijono268ca612006-02-07 12:34:11 +0000993/*
994 * Destroy pjsua.
995 */
996pj_status_t pjsua_destroy(void)
997{
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000998 int i; /* Must be signed */
Benny Prijono268ca612006-02-07 12:34:11 +0000999
1000 /* Signal threads to quit: */
Benny Prijono268ca612006-02-07 12:34:11 +00001001 pjsua.quit_flag = 1;
1002
Benny Prijonoa91a0032006-02-26 21:23:45 +00001003 /* Terminate all calls. */
Benny Prijono1a174142006-03-01 20:46:13 +00001004 pjsua_call_hangup_all();
Benny Prijonoa91a0032006-02-26 21:23:45 +00001005
1006 /* Terminate all presence subscriptions. */
1007 pjsua_pres_shutdown();
1008
1009 /* Unregister, if required: */
1010 for (i=0; i<pjsua.acc_cnt; ++i) {
1011 if (pjsua.acc[i].regc) {
1012 pjsua_regc_update(i, 0);
1013 }
1014 }
1015
Benny Prijono268ca612006-02-07 12:34:11 +00001016 /* Wait worker threads to quit: */
Benny Prijono268ca612006-02-07 12:34:11 +00001017 for (i=0; i<pjsua.thread_cnt; ++i) {
1018
Benny Prijonoe4f2abb2006-02-10 14:04:05 +00001019 if (pjsua.threads[i]) {
1020 pj_thread_join(pjsua.threads[i]);
1021 pj_thread_destroy(pjsua.threads[i]);
1022 pjsua.threads[i] = NULL;
1023 }
Benny Prijono268ca612006-02-07 12:34:11 +00001024 }
1025
Benny Prijono834aee32006-02-19 01:38:06 +00001026
Benny Prijono834aee32006-02-19 01:38:06 +00001027 /* Wait for some time to allow unregistration to complete: */
1028 PJ_LOG(4,(THIS_FILE, "Shutting down..."));
1029 busy_sleep(1000);
1030
Benny Prijono26ff9062006-02-21 23:47:00 +00001031 /* Destroy conference bridge. */
1032 if (pjsua.mconf)
1033 pjmedia_conf_destroy(pjsua.mconf);
1034
Benny Prijonode380582006-03-15 19:32:41 +00001035 /* Destroy file port */
Benny Prijono4381efe2006-03-16 14:24:26 +00001036 if (pjsua.file_port)
1037 pjmedia_port_destroy(pjsua.file_port);
Benny Prijonode380582006-03-15 19:32:41 +00001038
1039 /* Destroy null port. */
Benny Prijono4381efe2006-03-16 14:24:26 +00001040 if (pjsua.null_port)
1041 pjmedia_port_destroy(pjsua.null_port);
Benny Prijonode380582006-03-15 19:32:41 +00001042
1043
Benny Prijonoeb30bf52006-03-04 20:43:52 +00001044 /* Shutdown all codecs: */
Benny Prijonob8c25182006-04-29 08:31:09 +00001045#if PJMEDIA_HAS_SPEEX_CODEC
1046 pjmedia_codec_speex_deinit();
1047#endif /* PJMEDIA_HAS_SPEEX_CODEC */
1048
1049#if PJMEDIA_HAS_GSM_CODEC
1050 pjmedia_codec_gsm_deinit();
1051#endif /* PJMEDIA_HAS_GSM_CODEC */
1052
1053#if PJMEDIA_HAS_G711_CODEC
1054 pjmedia_codec_g711_deinit();
1055#endif /* PJMEDIA_HAS_G711_CODEC */
1056
1057#if PJMEDIA_HAS_L16_CODEC
1058 pjmedia_codec_l16_deinit();
1059#endif /* PJMEDIA_HAS_L16_CODEC */
Benny Prijonoeb30bf52006-03-04 20:43:52 +00001060
1061 /* Destroy media endpoint. */
1062
1063 pjmedia_endpt_destroy(pjsua.med_endpt);
1064
Benny Prijono268ca612006-02-07 12:34:11 +00001065 /* Destroy endpoint. */
Benny Prijono84126ab2006-02-09 09:30:09 +00001066
Benny Prijono268ca612006-02-07 12:34:11 +00001067 pjsip_endpt_destroy(pjsua.endpt);
1068 pjsua.endpt = NULL;
1069
1070 /* Destroy caching pool. */
Benny Prijono84126ab2006-02-09 09:30:09 +00001071
Benny Prijono268ca612006-02-07 12:34:11 +00001072 pj_caching_pool_destroy(&pjsua.cp);
1073
1074
1075 /* Done. */
1076
1077 return PJ_SUCCESS;
1078}
1079