blob: 7ef91c7469862e7ae6e39df9d0d5a2116730a099 [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
620 unsigned option = PJMEDIA_SPEEX_NO_WB | PJMEDIA_SPEEX_NO_UWB;
621
622 /* Register speex. */
623 if (pjsua.clock_rate >= 16000)
624 option &= ~(PJMEDIA_SPEEX_NO_WB);
625 if (pjsua.clock_rate >= 32000)
626 option &= ~(PJMEDIA_SPEEX_NO_UWB);
627
Benny Prijono1c2bf462006-03-05 11:54:02 +0000628 status = pjmedia_codec_speex_init(pjsua.med_endpt, option,
629 pjsua.quality, pjsua.complexity );
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000630 if (status != PJ_SUCCESS) {
631 pjsua_perror(THIS_FILE, "Error initializing Speex codec",
632 status);
633 return status;
634 }
635
636 pjsua.codec_arg[pjsua.codec_cnt] = pj_str("speex");
637 pjsua.codec_deinit[pjsua.codec_cnt] = &pjmedia_codec_speex_deinit;
638 pjsua.codec_cnt++;
639
640 /* Register GSM */
641 status = pjmedia_codec_gsm_init(pjsua.med_endpt);
642 if (status != PJ_SUCCESS) {
643 pjsua_perror(THIS_FILE, "Error initializing GSM codec",
644 status);
645 return status;
646 }
647
648 pjsua.codec_arg[pjsua.codec_cnt] = pj_str("gsm");
649 pjsua.codec_deinit[pjsua.codec_cnt] = &pjmedia_codec_gsm_deinit;
650 pjsua.codec_cnt++;
651
652 /* Register PCMA and PCMU */
653 status = pjmedia_codec_g711_init(pjsua.med_endpt);
654 if (status != PJ_SUCCESS) {
655 pjsua_perror(THIS_FILE, "Error initializing G711 codec",
656 status);
657 return status;
658 }
659
660 pjsua.codec_arg[pjsua.codec_cnt] = pj_str("pcmu");
661 pjsua.codec_deinit[pjsua.codec_cnt] = &pjmedia_codec_g711_deinit;
662 pjsua.codec_cnt++;
663 pjsua.codec_arg[pjsua.codec_cnt] = pj_str("pcma");
664 pjsua.codec_deinit[pjsua.codec_cnt] = &pjmedia_codec_g711_deinit;
665 pjsua.codec_cnt++;
666
667 } else {
668
669 /* If user specifies the exact codec to be used, then create only
670 * those codecs.
671 */
672 int i;
673
674 for (i=0; i<pjsua.codec_cnt; ++i) {
675
676 /* Is it speex? */
677 if (!pj_stricmp2(&pjsua.codec_arg[i], "speex")) {
678
679 unsigned option = PJMEDIA_SPEEX_NO_WB | PJMEDIA_SPEEX_NO_UWB;
680
681 /* Register speex. */
682 if (pjsua.clock_rate >= 16000)
683 option &= ~(PJMEDIA_SPEEX_NO_WB);
684 if (pjsua.clock_rate >= 32000)
685 option &= ~(PJMEDIA_SPEEX_NO_UWB);
686
687 status = pjmedia_codec_speex_init(pjsua.med_endpt, option,
688 -1, -1);
689 if (status != PJ_SUCCESS) {
690 pjsua_perror(THIS_FILE, "Error initializing Speex codec",
691 status);
692 return status;
693 }
694
695 pjsua.codec_deinit[i] = &pjmedia_codec_speex_deinit;
696 }
697 /* Is it gsm? */
698 else if (!pj_stricmp2(&pjsua.codec_arg[i], "gsm")) {
699
700 status = pjmedia_codec_gsm_init(pjsua.med_endpt);
701 if (status != PJ_SUCCESS) {
702 pjsua_perror(THIS_FILE, "Error initializing GSM codec",
703 status);
704 return status;
705 }
706
707 pjsua.codec_deinit[i] = &pjmedia_codec_gsm_deinit;
708
709 }
710 /* Is it pcma/pcmu? */
711 else if (!pj_stricmp2(&pjsua.codec_arg[i], "pcmu") ||
712 !pj_stricmp2(&pjsua.codec_arg[i], "pcma"))
713 {
714
715 status = pjmedia_codec_g711_init(pjsua.med_endpt);
716 if (status != PJ_SUCCESS) {
717 pjsua_perror(THIS_FILE, "Error initializing G711 codec",
718 status);
719 return status;
720 }
721
722 pjsua.codec_deinit[i] = &pjmedia_codec_g711_deinit;
723
724 }
725 /* Don't know about this codec... */
726 else {
727
728 PJ_LOG(1,(THIS_FILE, "Error: unsupported codecs %s",
729 pjsua.codec_arg[i].ptr));
730 return PJMEDIA_CODEC_EUNSUP;
731 }
732 }
733 }
Benny Prijonoccf95622006-02-07 18:48:01 +0000734
Benny Prijono8e3344c2006-03-08 12:37:22 +0000735 /* Init options for conference bridge. */
736 options = 0;
737 if (pjsua.no_mic)
738 options |= PJMEDIA_CONF_NO_MIC;
739
Benny Prijonof8baa872006-02-27 23:54:23 +0000740 /* Init conference bridge. */
741
742 status = pjmedia_conf_create(pjsua.pool,
743 pjsua.max_calls+PJSUA_CONF_MORE_PORTS,
Benny Prijono08e0d062006-03-04 14:52:44 +0000744 pjsua.clock_rate,
745 pjsua.clock_rate * 20 / 1000, 16,
Benny Prijono8e3344c2006-03-08 12:37:22 +0000746 options,
Benny Prijono08e0d062006-03-04 14:52:44 +0000747 &pjsua.mconf);
Benny Prijonof8baa872006-02-27 23:54:23 +0000748 if (status != PJ_SUCCESS) {
749 pj_caching_pool_destroy(&pjsua.cp);
750 pjsua_perror(THIS_FILE,
751 "Media stack initialization has returned error",
752 status);
753 return status;
754 }
755
Benny Prijono39879152006-02-23 02:09:10 +0000756 /* Create WAV file player if required: */
757
758 if (pjsua.wav_file) {
759 pjmedia_port *port;
760 pj_str_t port_name;
761
762 /* Create the file player port. */
763 status = pjmedia_file_player_port_create( pjsua.pool, pjsua.wav_file,
764 0, -1, NULL, &port);
765 if (status != PJ_SUCCESS) {
766 pjsua_perror(THIS_FILE,
767 "Error playing media file",
768 status);
769 return status;
770 }
771
772 /* Add port to conference bridge: */
773 status = pjmedia_conf_add_port(pjsua.mconf, pjsua.pool, port,
774 pj_cstr(&port_name, pjsua.wav_file),
775 &pjsua.wav_slot);
776 if (status != PJ_SUCCESS) {
777 pjsua_perror(THIS_FILE,
778 "Unable to add file player to conference bridge",
779 status);
780 return status;
781 }
782 }
783
784
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000785 return PJ_SUCCESS;
786}
787
788
789/*
790 * Start pjsua stack.
791 * This will start the registration process, if registration is configured.
792 */
793pj_status_t pjsua_start(void)
794{
795 int i; /* Must be signed */
796 pjsip_transport *udp_transport;
797 pj_status_t status = PJ_SUCCESS;
798
799 /*
800 * Init media subsystem (codecs, conference bridge, et all).
801 */
802 status = init_media();
803 if (status != PJ_SUCCESS)
804 return status;
805
Benny Prijono268ca612006-02-07 12:34:11 +0000806 /* Init sockets (STUN etc): */
Benny Prijonoa91a0032006-02-26 21:23:45 +0000807 for (i=0; i<(int)pjsua.max_calls; ++i) {
808 status = init_sockets(i==0, &pjsua.calls[i].skinfo);
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000809 if (status != PJ_SUCCESS) {
810 pjsua_perror(THIS_FILE, "init_sockets() has returned error",
811 status);
Benny Prijonoa91a0032006-02-26 21:23:45 +0000812 --i;
813 if (i >= 0)
814 pj_sock_close(pjsua.sip_sock);
815 while (i >= 0) {
816 pj_sock_close(pjsua.calls[i].skinfo.rtp_sock);
817 pj_sock_close(pjsua.calls[i].skinfo.rtcp_sock);
818 }
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000819 return status;
820 }
Benny Prijono268ca612006-02-07 12:34:11 +0000821 }
822
Benny Prijonoccf95622006-02-07 18:48:01 +0000823 /* Add UDP transport: */
Benny Prijono268ca612006-02-07 12:34:11 +0000824
Benny Prijonoccf95622006-02-07 18:48:01 +0000825 {
826 /* Init the published name for the transport.
827 * Depending whether STUN is used, this may be the STUN mapped
828 * address, or socket's bound address.
829 */
830 pjsip_host_port addr_name;
831
832 addr_name.host.ptr = pj_inet_ntoa(pjsua.sip_sock_name.sin_addr);
Benny Prijonof3195072006-02-14 21:15:30 +0000833 addr_name.host.slen = pj_ansi_strlen(addr_name.host.ptr);
Benny Prijonoccf95622006-02-07 18:48:01 +0000834 addr_name.port = pj_ntohs(pjsua.sip_sock_name.sin_port);
835
836 /* Create UDP transport from previously created UDP socket: */
837
838 status = pjsip_udp_transport_attach( pjsua.endpt, pjsua.sip_sock,
839 &addr_name, 1,
840 &udp_transport);
841 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000842 pjsua_perror(THIS_FILE, "Unable to start UDP transport",
843 status);
Benny Prijonoccf95622006-02-07 18:48:01 +0000844 return status;
845 }
Benny Prijono268ca612006-02-07 12:34:11 +0000846 }
847
Benny Prijonoccf95622006-02-07 18:48:01 +0000848 /* Initialize Contact URI, if one is not specified: */
Benny Prijonoa91a0032006-02-26 21:23:45 +0000849 for (i=0; i<pjsua.acc_cnt; ++i) {
Benny Prijonoccf95622006-02-07 18:48:01 +0000850
851 pjsip_uri *uri;
852 pjsip_sip_uri *sip_uri;
Benny Prijonoccf95622006-02-07 18:48:01 +0000853
Benny Prijonoccf95622006-02-07 18:48:01 +0000854 /* Need to parse local_uri to get the elements: */
855
Benny Prijonoa91a0032006-02-26 21:23:45 +0000856 uri = pjsip_parse_uri(pjsua.pool, pjsua.acc[i].local_uri.ptr,
857 pjsua.acc[i].local_uri.slen, 0);
Benny Prijonoccf95622006-02-07 18:48:01 +0000858 if (uri == NULL) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000859 pjsua_perror(THIS_FILE, "Invalid local URI",
860 PJSIP_EINVALIDURI);
Benny Prijonoccf95622006-02-07 18:48:01 +0000861 return PJSIP_EINVALIDURI;
862 }
863
Benny Prijonoccf95622006-02-07 18:48:01 +0000864 /* Local URI MUST be a SIP or SIPS: */
865
Benny Prijonoa91a0032006-02-26 21:23:45 +0000866 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
867 !PJSIP_URI_SCHEME_IS_SIPS(uri))
868 {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000869 pjsua_perror(THIS_FILE, "Invalid local URI",
870 PJSIP_EINVALIDSCHEME);
Benny Prijonoccf95622006-02-07 18:48:01 +0000871 return PJSIP_EINVALIDSCHEME;
872 }
873
874
875 /* Get the SIP URI object: */
876
877 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
878
Benny Prijonoa91a0032006-02-26 21:23:45 +0000879 pjsua.acc[i].user_part = sip_uri->user;
880 pjsua.acc[i].host_part = sip_uri->host;
Benny Prijonoccf95622006-02-07 18:48:01 +0000881
Benny Prijonoa91a0032006-02-26 21:23:45 +0000882 if (pjsua.acc[i].contact_uri.slen == 0 &&
883 pjsua.acc[i].local_uri.slen)
884 {
885 char contact[128];
886 int len;
Benny Prijonoccf95622006-02-07 18:48:01 +0000887
Benny Prijonoa91a0032006-02-26 21:23:45 +0000888 /* The local Contact is the username@ip-addr, where
889 * - username is taken from the local URI,
890 * - ip-addr in UDP transport's address name (which may have been
891 * resolved from STUN.
892 */
893
894 /* Build temporary contact string. */
Benny Prijonoccf95622006-02-07 18:48:01 +0000895
Benny Prijonoa91a0032006-02-26 21:23:45 +0000896 if (sip_uri->user.slen) {
Benny Prijonoccf95622006-02-07 18:48:01 +0000897
Benny Prijonoa91a0032006-02-26 21:23:45 +0000898 /* With the user part. */
899 len = pj_snprintf(contact, sizeof(contact),
900 "<sip:%.*s@%.*s:%d>",
901 (int)sip_uri->user.slen,
902 sip_uri->user.ptr,
903 (int)udp_transport->local_name.host.slen,
904 udp_transport->local_name.host.ptr,
905 udp_transport->local_name.port);
906 } else {
907
908 /* Without user part */
909
910 len = pj_snprintf(contact, sizeof(contact),
911 "<sip:%.*s:%d>",
912 (int)udp_transport->local_name.host.slen,
913 udp_transport->local_name.host.ptr,
914 udp_transport->local_name.port);
915 }
916
917 if (len < 1 || len >= sizeof(contact)) {
918 pjsua_perror(THIS_FILE, "Invalid Contact", PJSIP_EURITOOLONG);
919 return PJSIP_EURITOOLONG;
920 }
921
922 /* Duplicate Contact uri. */
923
924 pj_strdup2(pjsua.pool, &pjsua.acc[i].contact_uri, contact);
925
Benny Prijonoccf95622006-02-07 18:48:01 +0000926 }
Benny Prijonoccf95622006-02-07 18:48:01 +0000927 }
928
Benny Prijono95196582006-02-09 00:13:40 +0000929 /* If outbound_proxy is specified, put it in the route_set: */
Benny Prijonoccf95622006-02-07 18:48:01 +0000930
Benny Prijono95196582006-02-09 00:13:40 +0000931 if (pjsua.outbound_proxy.slen) {
932
933 pjsip_route_hdr *route;
934 const pj_str_t hname = { "Route", 5 };
935 int parsed_len;
936
937 route = pjsip_parse_hdr( pjsua.pool, &hname,
938 pjsua.outbound_proxy.ptr,
939 pjsua.outbound_proxy.slen,
940 &parsed_len);
941 if (route == NULL) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000942 pjsua_perror(THIS_FILE, "Invalid outbound proxy URL",
943 PJSIP_EINVALIDURI);
Benny Prijono95196582006-02-09 00:13:40 +0000944 return PJSIP_EINVALIDURI;
945 }
946
Benny Prijonoa91a0032006-02-26 21:23:45 +0000947 for (i=0; i<pjsua.acc_cnt; ++i) {
948 pj_list_push_front(&pjsua.acc[i].route_set, route);
949 }
Benny Prijono95196582006-02-09 00:13:40 +0000950 }
Benny Prijonoccf95622006-02-07 18:48:01 +0000951
952
Benny Prijono268ca612006-02-07 12:34:11 +0000953 /* Create worker thread(s), if required: */
954
955 for (i=0; i<pjsua.thread_cnt; ++i) {
Benny Prijono834aee32006-02-19 01:38:06 +0000956 status = pj_thread_create( pjsua.pool, "pjsua", &pjsua_poll,
Benny Prijono268ca612006-02-07 12:34:11 +0000957 NULL, 0, 0, &pjsua.threads[i]);
958 if (status != PJ_SUCCESS) {
959 pjsua.quit_flag = 1;
960 for (--i; i>=0; --i) {
961 pj_thread_join(pjsua.threads[i]);
962 pj_thread_destroy(pjsua.threads[i]);
963 }
Benny Prijono268ca612006-02-07 12:34:11 +0000964 return status;
965 }
966 }
967
Benny Prijonoccf95622006-02-07 18:48:01 +0000968 /* Start registration: */
969
970 /* Create client registration session: */
Benny Prijonoa91a0032006-02-26 21:23:45 +0000971 for (i=0; i<pjsua.acc_cnt; ++i) {
972 status = pjsua_regc_init(i);
973 if (status != PJ_SUCCESS)
974 return status;
Benny Prijonoccf95622006-02-07 18:48:01 +0000975
Benny Prijonoa91a0032006-02-26 21:23:45 +0000976 /* Perform registration, if required. */
977 if (pjsua.acc[i].regc) {
978 pjsua_regc_update(i, 1);
979 }
980 }
Benny Prijonoccf95622006-02-07 18:48:01 +0000981
Benny Prijonoa91a0032006-02-26 21:23:45 +0000982
983 /* Find account for outgoing preence subscription */
984 for (i=0; i<pjsua.buddy_cnt; ++i) {
985 pjsua.buddies[i].acc_index =
986 pjsua_find_account_for_outgoing(&pjsua.buddies[i].uri);
Benny Prijonoccf95622006-02-07 18:48:01 +0000987 }
988
989
Benny Prijono834aee32006-02-19 01:38:06 +0000990 PJ_LOG(3,(THIS_FILE, "PJSUA version %s started", PJ_VERSION));
Benny Prijono268ca612006-02-07 12:34:11 +0000991 return PJ_SUCCESS;
992}
993
994
Benny Prijono834aee32006-02-19 01:38:06 +0000995/* Sleep with polling */
996static void busy_sleep(unsigned msec)
997{
998 pj_time_val timeout, now;
999
1000 pj_gettimeofday(&timeout);
1001 timeout.msec += msec;
1002 pj_time_val_normalize(&timeout);
1003
1004 do {
1005 pjsua_poll(NULL);
1006 pj_gettimeofday(&now);
1007 } while (PJ_TIME_VAL_LT(now, timeout));
1008}
1009
Benny Prijono268ca612006-02-07 12:34:11 +00001010/*
1011 * Destroy pjsua.
1012 */
1013pj_status_t pjsua_destroy(void)
1014{
Benny Prijonoeb30bf52006-03-04 20:43:52 +00001015 int i; /* Must be signed */
Benny Prijono268ca612006-02-07 12:34:11 +00001016
1017 /* Signal threads to quit: */
Benny Prijono268ca612006-02-07 12:34:11 +00001018 pjsua.quit_flag = 1;
1019
Benny Prijonoa91a0032006-02-26 21:23:45 +00001020 /* Terminate all calls. */
Benny Prijono1a174142006-03-01 20:46:13 +00001021 pjsua_call_hangup_all();
Benny Prijonoa91a0032006-02-26 21:23:45 +00001022
1023 /* Terminate all presence subscriptions. */
1024 pjsua_pres_shutdown();
1025
1026 /* Unregister, if required: */
1027 for (i=0; i<pjsua.acc_cnt; ++i) {
1028 if (pjsua.acc[i].regc) {
1029 pjsua_regc_update(i, 0);
1030 }
1031 }
1032
Benny Prijono268ca612006-02-07 12:34:11 +00001033 /* Wait worker threads to quit: */
Benny Prijono268ca612006-02-07 12:34:11 +00001034 for (i=0; i<pjsua.thread_cnt; ++i) {
1035
Benny Prijonoe4f2abb2006-02-10 14:04:05 +00001036 if (pjsua.threads[i]) {
1037 pj_thread_join(pjsua.threads[i]);
1038 pj_thread_destroy(pjsua.threads[i]);
1039 pjsua.threads[i] = NULL;
1040 }
Benny Prijono268ca612006-02-07 12:34:11 +00001041 }
1042
Benny Prijono834aee32006-02-19 01:38:06 +00001043
Benny Prijono834aee32006-02-19 01:38:06 +00001044 /* Wait for some time to allow unregistration to complete: */
1045 PJ_LOG(4,(THIS_FILE, "Shutting down..."));
1046 busy_sleep(1000);
1047
Benny Prijono26ff9062006-02-21 23:47:00 +00001048 /* Destroy conference bridge. */
1049 if (pjsua.mconf)
1050 pjmedia_conf_destroy(pjsua.mconf);
1051
Benny Prijono834aee32006-02-19 01:38:06 +00001052 /* Destroy sound framework:
1053 * (this should be done in pjmedia_shutdown())
1054 */
1055 pj_snd_deinit();
1056
Benny Prijonoeb30bf52006-03-04 20:43:52 +00001057 /* Shutdown all codecs: */
1058 for (i = pjsua.codec_cnt-1; i >= 0; --i) {
1059 (*pjsua.codec_deinit[i])();
1060 }
1061
1062 /* Destroy media endpoint. */
1063
1064 pjmedia_endpt_destroy(pjsua.med_endpt);
1065
Benny Prijono268ca612006-02-07 12:34:11 +00001066 /* Destroy endpoint. */
Benny Prijono84126ab2006-02-09 09:30:09 +00001067
Benny Prijono268ca612006-02-07 12:34:11 +00001068 pjsip_endpt_destroy(pjsua.endpt);
1069 pjsua.endpt = NULL;
1070
1071 /* Destroy caching pool. */
Benny Prijono84126ab2006-02-09 09:30:09 +00001072
Benny Prijono268ca612006-02-07 12:34:11 +00001073 pj_caching_pool_destroy(&pjsua.cp);
1074
1075
1076 /* Done. */
1077
1078 return PJ_SUCCESS;
1079}
1080