blob: 1f975142bdb5fec59ac6842a2ccca42c91893733 [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 Prijonoa91a0032006-02-26 21:23:45 +000072 /* Default: do not use STUN: */
Benny Prijono268ca612006-02-07 12:34:11 +000073 pjsua.stun_port1 = pjsua.stun_port2 = 0;
Benny Prijonoccf95622006-02-07 18:48:01 +000074
Benny Prijono08e0d062006-03-04 14:52:44 +000075 /* Default: sampling rate is 8000 */
76 pjsua.clock_rate = 8000;
77
Benny Prijonoa91a0032006-02-26 21:23:45 +000078 /* Init accounts: */
79 pjsua.acc_cnt = 1;
80 for (i=0; i<PJ_ARRAY_SIZE(pjsua.acc); ++i) {
81 pjsua.acc[i].index = i;
82 pjsua.acc[i].local_uri = pj_str(PJSUA_LOCAL_URI);
83 pjsua.acc[i].reg_timeout = 55;
84 pj_list_init(&pjsua.acc[i].route_set);
85 pj_list_init(&pjsua.acc[i].pres_srv_list);
86 }
Benny Prijonoccf95622006-02-07 18:48:01 +000087
Benny Prijonoa91a0032006-02-26 21:23:45 +000088 /* Init call array: */
89 for (i=0; i<PJ_ARRAY_SIZE(pjsua.calls); ++i)
90 pjsua.calls[i].index = i;
Benny Prijono95196582006-02-09 00:13:40 +000091
Benny Prijonoa91a0032006-02-26 21:23:45 +000092 /* Default max nb of calls. */
93 pjsua.max_calls = 4;
Benny Prijono834aee32006-02-19 01:38:06 +000094
95 /* Init server presence subscription list: */
96
Benny Prijono834aee32006-02-19 01:38:06 +000097
Benny Prijono268ca612006-02-07 12:34:11 +000098}
99
100
Benny Prijono268ca612006-02-07 12:34:11 +0000101
102/*
103 * Handler for receiving incoming requests.
104 *
105 * This handler serves multiple purposes:
106 * - it receives requests outside dialogs.
107 * - it receives requests inside dialogs, when the requests are
108 * unhandled by other dialog usages. Example of these
109 * requests are: MESSAGE.
110 */
111static pj_bool_t mod_pjsua_on_rx_request(pjsip_rx_data *rdata)
112{
Benny Prijono38998232006-02-08 22:44:25 +0000113
Benny Prijono84126ab2006-02-09 09:30:09 +0000114 if (rdata->msg_info.msg->line.req.method.id == PJSIP_INVITE_METHOD) {
Benny Prijono38998232006-02-08 22:44:25 +0000115
Benny Prijonoa91a0032006-02-26 21:23:45 +0000116 return pjsua_call_on_incoming(rdata);
Benny Prijono38998232006-02-08 22:44:25 +0000117 }
118
Benny Prijono268ca612006-02-07 12:34:11 +0000119 return PJ_FALSE;
120}
121
122
123/*
124 * Handler for receiving incoming responses.
125 *
126 * This handler serves multiple purposes:
127 * - it receives strayed responses (i.e. outside any dialog and
128 * outside any transactions).
129 * - it receives responses coming to a transaction, when pjsua
130 * module is set as transaction user for the transaction.
131 * - it receives responses inside a dialog, when these responses
132 * are unhandled by other dialog usages.
133 */
134static pj_bool_t mod_pjsua_on_rx_response(pjsip_rx_data *rdata)
135{
136 PJ_UNUSED_ARG(rdata);
Benny Prijono268ca612006-02-07 12:34:11 +0000137 return PJ_FALSE;
138}
139
140
Benny Prijono268ca612006-02-07 12:34:11 +0000141/*
142 * Initialize sockets and optionally get the public address via STUN.
143 */
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000144static pj_status_t init_sockets(pj_bool_t sip,
145 pjmedia_sock_info *skinfo)
Benny Prijono268ca612006-02-07 12:34:11 +0000146{
147 enum {
Benny Prijonoa91a0032006-02-26 21:23:45 +0000148 RTP_RETRY = 100
Benny Prijono268ca612006-02-07 12:34:11 +0000149 };
150 enum {
151 SIP_SOCK,
152 RTP_SOCK,
153 RTCP_SOCK,
154 };
155 int i;
Benny Prijonocbf37402006-03-01 19:29:10 +0000156 static pj_uint16_t rtp_port;
Benny Prijono268ca612006-02-07 12:34:11 +0000157 pj_sock_t sock[3];
158 pj_sockaddr_in mapped_addr[3];
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000159 pj_status_t status = PJ_SUCCESS;
Benny Prijono268ca612006-02-07 12:34:11 +0000160
Benny Prijonocbf37402006-03-01 19:29:10 +0000161 if (rtp_port == 0)
162 rtp_port = (pj_uint16_t)pjsua.start_rtp_port;
163
Benny Prijono268ca612006-02-07 12:34:11 +0000164 for (i=0; i<3; ++i)
165 sock[i] = PJ_INVALID_SOCKET;
166
Benny Prijonofccab712006-02-22 22:23:22 +0000167 /* Create and bind SIP UDP socket. */
168 status = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, &sock[SIP_SOCK]);
169 if (status != PJ_SUCCESS) {
170 pjsua_perror(THIS_FILE, "socket() error", status);
171 goto on_error;
172 }
173
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000174 if (sip) {
Benny Prijonofccab712006-02-22 22:23:22 +0000175 status = pj_sock_bind_in(sock[SIP_SOCK], 0, pjsua.sip_port);
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000176 if (status != PJ_SUCCESS) {
Benny Prijonofccab712006-02-22 22:23:22 +0000177 pjsua_perror(THIS_FILE, "bind() error", status);
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000178 goto on_error;
179 }
Benny Prijonofccab712006-02-22 22:23:22 +0000180 } else {
181 status = pj_sock_bind_in(sock[SIP_SOCK], 0, 0);
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000182 if (status != PJ_SUCCESS) {
183 pjsua_perror(THIS_FILE, "bind() error", status);
184 goto on_error;
185 }
Benny Prijono268ca612006-02-07 12:34:11 +0000186 }
187
Benny Prijono268ca612006-02-07 12:34:11 +0000188
189 /* Loop retry to bind RTP and RTCP sockets. */
190 for (i=0; i<RTP_RETRY; ++i, rtp_port += 2) {
191
192 /* Create and bind RTP socket. */
193 status = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, &sock[RTP_SOCK]);
194 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000195 pjsua_perror(THIS_FILE, "socket() error", status);
Benny Prijono268ca612006-02-07 12:34:11 +0000196 goto on_error;
197 }
198
199 status = pj_sock_bind_in(sock[RTP_SOCK], 0, rtp_port);
200 if (status != PJ_SUCCESS) {
201 pj_sock_close(sock[RTP_SOCK]);
202 sock[RTP_SOCK] = PJ_INVALID_SOCKET;
203 continue;
204 }
205
206 /* Create and bind RTCP socket. */
207 status = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, &sock[RTCP_SOCK]);
208 if (status != PJ_SUCCESS) {
Benny 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[RTCP_SOCK], 0, (pj_uint16_t)(rtp_port+1));
214 if (status != PJ_SUCCESS) {
215 pj_sock_close(sock[RTP_SOCK]);
216 sock[RTP_SOCK] = PJ_INVALID_SOCKET;
217
218 pj_sock_close(sock[RTCP_SOCK]);
219 sock[RTCP_SOCK] = PJ_INVALID_SOCKET;
220 continue;
221 }
222
223 /*
224 * If we're configured to use STUN, then find out the mapped address,
225 * and make sure that the mapped RTCP port is adjacent with the RTP.
226 */
227 if (pjsua.stun_port1 == 0) {
228 const pj_str_t *hostname;
229 pj_sockaddr_in addr;
230
231 /* Get local IP address. */
232 hostname = pj_gethostname();
233
234 pj_memset( &addr, 0, sizeof(addr));
235 addr.sin_family = PJ_AF_INET;
236 status = pj_sockaddr_in_set_str_addr( &addr, hostname);
237 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000238 pjsua_perror(THIS_FILE, "Unresolvable local hostname",
239 status);
Benny Prijono268ca612006-02-07 12:34:11 +0000240 goto on_error;
241 }
242
243 for (i=0; i<3; ++i)
244 pj_memcpy(&mapped_addr[i], &addr, sizeof(addr));
245
Benny Prijonoa91a0032006-02-26 21:23:45 +0000246 if (sip) {
247 mapped_addr[SIP_SOCK].sin_port =
248 pj_htons((pj_uint16_t)pjsua.sip_port);
249 }
250 mapped_addr[RTP_SOCK].sin_port=pj_htons((pj_uint16_t)rtp_port);
251 mapped_addr[RTCP_SOCK].sin_port=pj_htons((pj_uint16_t)(rtp_port+1));
Benny Prijono268ca612006-02-07 12:34:11 +0000252 break;
Benny Prijonoa91a0032006-02-26 21:23:45 +0000253
Benny Prijono268ca612006-02-07 12:34:11 +0000254 } else {
Benny Prijonoa91a0032006-02-26 21:23:45 +0000255 status=pj_stun_get_mapped_addr(&pjsua.cp.factory, 3, sock,
256 &pjsua.stun_srv1, pjsua.stun_port1,
257 &pjsua.stun_srv2, pjsua.stun_port2,
258 mapped_addr);
Benny Prijono268ca612006-02-07 12:34:11 +0000259 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000260 pjsua_perror(THIS_FILE, "STUN error", status);
Benny Prijono268ca612006-02-07 12:34:11 +0000261 goto on_error;
262 }
263
Benny Prijonoa91a0032006-02-26 21:23:45 +0000264 if (pj_ntohs(mapped_addr[2].sin_port) ==
265 pj_ntohs(mapped_addr[1].sin_port)+1)
266 {
Benny Prijono268ca612006-02-07 12:34:11 +0000267 break;
Benny Prijonoa91a0032006-02-26 21:23:45 +0000268 }
Benny Prijono268ca612006-02-07 12:34:11 +0000269
Benny Prijonoa91a0032006-02-26 21:23:45 +0000270 pj_sock_close(sock[RTP_SOCK]);
271 sock[RTP_SOCK] = PJ_INVALID_SOCKET;
272
273 pj_sock_close(sock[RTCP_SOCK]);
274 sock[RTCP_SOCK] = PJ_INVALID_SOCKET;
Benny Prijono268ca612006-02-07 12:34:11 +0000275 }
276 }
277
278 if (sock[RTP_SOCK] == PJ_INVALID_SOCKET) {
Benny Prijonoa91a0032006-02-26 21:23:45 +0000279 PJ_LOG(1,(THIS_FILE,
280 "Unable to find appropriate RTP/RTCP ports combination"));
Benny Prijono268ca612006-02-07 12:34:11 +0000281 goto on_error;
282 }
283
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000284 if (sip) {
285 pjsua.sip_sock = sock[SIP_SOCK];
Benny Prijonoa91a0032006-02-26 21:23:45 +0000286 pj_memcpy(&pjsua.sip_sock_name,
287 &mapped_addr[SIP_SOCK],
288 sizeof(pj_sockaddr_in));
Benny Prijonofccab712006-02-22 22:23:22 +0000289 } else {
290 pj_sock_close(sock[0]);
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000291 }
Benny Prijono95196582006-02-09 00:13:40 +0000292
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000293 skinfo->rtp_sock = sock[RTP_SOCK];
294 pj_memcpy(&skinfo->rtp_addr_name,
Benny Prijono95196582006-02-09 00:13:40 +0000295 &mapped_addr[RTP_SOCK], sizeof(pj_sockaddr_in));
296
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000297 skinfo->rtcp_sock = sock[RTCP_SOCK];
298 pj_memcpy(&skinfo->rtcp_addr_name,
Benny Prijono95196582006-02-09 00:13:40 +0000299 &mapped_addr[RTCP_SOCK], sizeof(pj_sockaddr_in));
Benny Prijono268ca612006-02-07 12:34:11 +0000300
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000301 if (sip) {
302 PJ_LOG(4,(THIS_FILE, "SIP UDP socket reachable at %s:%d",
303 pj_inet_ntoa(pjsua.sip_sock_name.sin_addr),
304 pj_ntohs(pjsua.sip_sock_name.sin_port)));
305 }
Benny Prijono268ca612006-02-07 12:34:11 +0000306 PJ_LOG(4,(THIS_FILE, "RTP socket reachable at %s:%d",
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000307 pj_inet_ntoa(skinfo->rtp_addr_name.sin_addr),
308 pj_ntohs(skinfo->rtp_addr_name.sin_port)));
Benny Prijono268ca612006-02-07 12:34:11 +0000309 PJ_LOG(4,(THIS_FILE, "RTCP UDP socket reachable at %s:%d",
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000310 pj_inet_ntoa(skinfo->rtcp_addr_name.sin_addr),
311 pj_ntohs(skinfo->rtcp_addr_name.sin_port)));
Benny Prijono268ca612006-02-07 12:34:11 +0000312
Benny Prijonofccab712006-02-22 22:23:22 +0000313 rtp_port += 2;
Benny Prijono268ca612006-02-07 12:34:11 +0000314 return PJ_SUCCESS;
315
316on_error:
317 for (i=0; i<3; ++i) {
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000318 if (sip && i==0)
319 continue;
Benny Prijono268ca612006-02-07 12:34:11 +0000320 if (sock[i] != PJ_INVALID_SOCKET)
321 pj_sock_close(sock[i]);
322 }
323 return status;
324}
325
326
327
328/*
329 * Initialize stack.
330 */
331static pj_status_t init_stack(void)
332{
333 pj_status_t status;
334
335 /* Create global endpoint: */
336
337 {
338 const pj_str_t *hostname;
339 const char *endpt_name;
340
341 /* Endpoint MUST be assigned a globally unique name.
342 * The name will be used as the hostname in Warning header.
343 */
344
345 /* For this implementation, we'll use hostname for simplicity */
346 hostname = pj_gethostname();
347 endpt_name = hostname->ptr;
348
349 /* Create the endpoint: */
350
351 status = pjsip_endpt_create(&pjsua.cp.factory, endpt_name,
352 &pjsua.endpt);
353 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000354 pjsua_perror(THIS_FILE, "Unable to create SIP endpoint", status);
Benny Prijono268ca612006-02-07 12:34:11 +0000355 return status;
356 }
357 }
358
359
360 /* Initialize transaction layer: */
361
Benny Prijono2f8992b2006-02-25 21:16:36 +0000362 status = pjsip_tsx_layer_init_module(pjsua.endpt);
Benny Prijono268ca612006-02-07 12:34:11 +0000363 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000364 pjsua_perror(THIS_FILE, "Transaction layer initialization error",
365 status);
Benny Prijono268ca612006-02-07 12:34:11 +0000366 goto on_error;
367 }
368
369 /* Initialize UA layer module: */
370
Benny Prijono2f8992b2006-02-25 21:16:36 +0000371 status = pjsip_ua_init_module( pjsua.endpt, NULL );
Benny Prijono268ca612006-02-07 12:34:11 +0000372 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000373 pjsua_perror(THIS_FILE, "UA layer initialization error", status);
Benny Prijono268ca612006-02-07 12:34:11 +0000374 goto on_error;
375 }
376
377 /* Initialize and register pjsua's application module: */
378
379 {
380 pjsip_module my_mod =
381 {
382 NULL, NULL, /* prev, next. */
383 { "mod-pjsua", 9 }, /* Name. */
384 -1, /* Id */
385 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
Benny Prijono268ca612006-02-07 12:34:11 +0000386 NULL, /* load() */
387 NULL, /* start() */
388 NULL, /* stop() */
389 NULL, /* unload() */
390 &mod_pjsua_on_rx_request, /* on_rx_request() */
391 &mod_pjsua_on_rx_response, /* on_rx_response() */
392 NULL, /* on_tx_request. */
393 NULL, /* on_tx_response() */
394 NULL, /* on_tsx_state() */
395 };
396
397 pjsua.mod = my_mod;
398
399 status = pjsip_endpt_register_module(pjsua.endpt, &pjsua.mod);
400 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000401 pjsua_perror(THIS_FILE, "Unable to register pjsua module",
402 status);
Benny Prijono268ca612006-02-07 12:34:11 +0000403 goto on_error;
404 }
405 }
406
407 /* Initialize invite session module: */
408
Benny Prijonoa91a0032006-02-26 21:23:45 +0000409 status = pjsua_call_init();
410 if (status != PJ_SUCCESS) {
411 pjsua_perror(THIS_FILE, "Invite usage initialization error",
412 status);
413 goto on_error;
Benny Prijono268ca612006-02-07 12:34:11 +0000414 }
415
Benny Prijonoccf95622006-02-07 18:48:01 +0000416 /* Done */
Benny Prijono268ca612006-02-07 12:34:11 +0000417
418 return PJ_SUCCESS;
419
420
421on_error:
422 pjsip_endpt_destroy(pjsua.endpt);
423 pjsua.endpt = NULL;
424 return status;
425}
426
427
Benny Prijono834aee32006-02-19 01:38:06 +0000428static int PJ_THREAD_FUNC pjsua_poll(void *arg)
Benny Prijono268ca612006-02-07 12:34:11 +0000429{
Benny Prijonof8baa872006-02-27 23:54:23 +0000430 pj_status_t last_err = 0;
431
Benny Prijono268ca612006-02-07 12:34:11 +0000432 PJ_UNUSED_ARG(arg);
433
Benny Prijono834aee32006-02-19 01:38:06 +0000434 do {
Benny Prijono268ca612006-02-07 12:34:11 +0000435 pj_time_val timeout = { 0, 10 };
Benny Prijonof8baa872006-02-27 23:54:23 +0000436 pj_status_t status;
437
438 status = pjsip_endpt_handle_events (pjsua.endpt, &timeout);
439 if (status != last_err) {
440 last_err = status;
441 pjsua_perror(THIS_FILE, "handle_events() returned error", status);
442 }
Benny Prijono834aee32006-02-19 01:38:06 +0000443 } while (!pjsua.quit_flag);
Benny Prijono268ca612006-02-07 12:34:11 +0000444
445 return 0;
446}
447
448/*
449 * Initialize pjsua application.
Benny Prijonoccf95622006-02-07 18:48:01 +0000450 * This will initialize all libraries, create endpoint instance, and register
451 * pjsip modules.
Benny Prijono268ca612006-02-07 12:34:11 +0000452 */
453pj_status_t pjsua_init(void)
454{
Benny Prijono268ca612006-02-07 12:34:11 +0000455 pj_status_t status;
456
457 /* Init PJLIB logging: */
458
459 pj_log_set_level(pjsua.log_level);
460 pj_log_set_decor(pjsua.log_decor);
461
462
463 /* Init PJLIB: */
464
465 status = pj_init();
466 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000467 pjsua_perror(THIS_FILE, "pj_init() error", status);
Benny Prijono268ca612006-02-07 12:34:11 +0000468 return status;
469 }
470
Benny Prijonofccab712006-02-22 22:23:22 +0000471 /* Init PJLIB-UTIL: */
472
473 status = pjlib_util_init();
474 if (status != PJ_SUCCESS) {
475 pjsua_perror(THIS_FILE, "pjlib_util_init() error", status);
476 return status;
477 }
478
Benny Prijono268ca612006-02-07 12:34:11 +0000479 /* Init memory pool: */
480
481 /* Init caching pool. */
482 pj_caching_pool_init(&pjsua.cp, &pj_pool_factory_default_policy, 0);
483
484 /* Create memory pool for application. */
485 pjsua.pool = pj_pool_create(&pjsua.cp.factory, "pjsua", 4000, 4000, NULL);
486
487
Benny Prijono834aee32006-02-19 01:38:06 +0000488 /* Init PJSIP : */
Benny Prijonoccf95622006-02-07 18:48:01 +0000489
490 status = init_stack();
491 if (status != PJ_SUCCESS) {
492 pj_caching_pool_destroy(&pjsua.cp);
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000493 pjsua_perror(THIS_FILE, "Stack initialization has returned error",
494 status);
Benny Prijonoccf95622006-02-07 18:48:01 +0000495 return status;
496 }
497
Benny Prijono95196582006-02-09 00:13:40 +0000498
Benny Prijono834aee32006-02-19 01:38:06 +0000499 /* Init core SIMPLE module : */
500
501 pjsip_evsub_init_module(pjsua.endpt);
502
503 /* Init presence module: */
504
505 pjsip_pres_init_module( pjsua.endpt, pjsip_evsub_instance());
506
Benny Prijono26ff9062006-02-21 23:47:00 +0000507 /* Init xfer/REFER module */
508
509 pjsip_xfer_init_module( pjsua.endpt );
Benny Prijono834aee32006-02-19 01:38:06 +0000510
511 /* Init pjsua presence handler: */
512
513 pjsua_pres_init();
514
Benny Prijonob0808372006-03-02 21:18:58 +0000515 /* Init out-of-dialog MESSAGE request handler. */
516
517 pjsua_im_init();
518
Benny Prijono834aee32006-02-19 01:38:06 +0000519
Benny Prijono95196582006-02-09 00:13:40 +0000520 /* Init media endpoint: */
521
522 status = pjmedia_endpt_create(&pjsua.cp.factory, &pjsua.med_endpt);
523 if (status != PJ_SUCCESS) {
524 pj_caching_pool_destroy(&pjsua.cp);
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000525 pjsua_perror(THIS_FILE,
526 "Media stack initialization has returned error",
527 status);
Benny Prijono95196582006-02-09 00:13:40 +0000528 return status;
529 }
530
Benny Prijonoccf95622006-02-07 18:48:01 +0000531 /* Done. */
532 return PJ_SUCCESS;
533}
534
535
Benny Prijonoa91a0032006-02-26 21:23:45 +0000536/*
537 * Find account for incoming request.
538 */
539int pjsua_find_account_for_incoming(pjsip_rx_data *rdata)
540{
541 pjsip_uri *uri;
542 pjsip_sip_uri *sip_uri;
543 int acc_index;
544
545 uri = rdata->msg_info.to->uri;
546
547 /* Just return account #0 if To URI is not SIP: */
548 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
549 !PJSIP_URI_SCHEME_IS_SIPS(uri))
550 {
551 return 0;
552 }
553
554
555 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
556
557 /* Find account which has matching username and domain. */
558 for (acc_index=0; acc_index < pjsua.acc_cnt; ++acc_index) {
559
560 pjsua_acc *acc = &pjsua.acc[acc_index];
561
562 if (pj_stricmp(&acc->user_part, &sip_uri->user)==0 &&
563 pj_stricmp(&acc->host_part, &sip_uri->host)==0)
564 {
565 /* Match ! */
566 return acc_index;
567 }
568 }
569
570 /* No matching, try match domain part only. */
571 for (acc_index=0; acc_index < pjsua.acc_cnt; ++acc_index) {
572
573 pjsua_acc *acc = &pjsua.acc[acc_index];
574
575 if (pj_stricmp(&acc->host_part, &sip_uri->host)==0) {
576 /* Match ! */
577 return acc_index;
578 }
579 }
580
581 /* Still no match, just return account #0 */
582 return 0;
583}
584
585
586/*
587 * Find account for outgoing request.
588 */
589int pjsua_find_account_for_outgoing(const pj_str_t *url)
590{
591 PJ_UNUSED_ARG(url);
592
593 /* Just use account #0 */
594 return 0;
595}
596
Benny Prijonoccf95622006-02-07 18:48:01 +0000597
598/*
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000599 * Init media.
Benny Prijonoccf95622006-02-07 18:48:01 +0000600 */
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000601static pj_status_t init_media(void)
Benny Prijonoccf95622006-02-07 18:48:01 +0000602{
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000603
604 pj_status_t status;
605
606 /* If user doesn't specify any codecs, register all of them. */
607 if (pjsua.codec_cnt == 0) {
608
609 unsigned option = PJMEDIA_SPEEX_NO_WB | PJMEDIA_SPEEX_NO_UWB;
610
611 /* Register speex. */
612 if (pjsua.clock_rate >= 16000)
613 option &= ~(PJMEDIA_SPEEX_NO_WB);
614 if (pjsua.clock_rate >= 32000)
615 option &= ~(PJMEDIA_SPEEX_NO_UWB);
616
617 status = pjmedia_codec_speex_init(pjsua.med_endpt, option, -1, -1);
618 if (status != PJ_SUCCESS) {
619 pjsua_perror(THIS_FILE, "Error initializing Speex codec",
620 status);
621 return status;
622 }
623
624 pjsua.codec_arg[pjsua.codec_cnt] = pj_str("speex");
625 pjsua.codec_deinit[pjsua.codec_cnt] = &pjmedia_codec_speex_deinit;
626 pjsua.codec_cnt++;
627
628 /* Register GSM */
629 status = pjmedia_codec_gsm_init(pjsua.med_endpt);
630 if (status != PJ_SUCCESS) {
631 pjsua_perror(THIS_FILE, "Error initializing GSM codec",
632 status);
633 return status;
634 }
635
636 pjsua.codec_arg[pjsua.codec_cnt] = pj_str("gsm");
637 pjsua.codec_deinit[pjsua.codec_cnt] = &pjmedia_codec_gsm_deinit;
638 pjsua.codec_cnt++;
639
640 /* Register PCMA and PCMU */
641 status = pjmedia_codec_g711_init(pjsua.med_endpt);
642 if (status != PJ_SUCCESS) {
643 pjsua_perror(THIS_FILE, "Error initializing G711 codec",
644 status);
645 return status;
646 }
647
648 pjsua.codec_arg[pjsua.codec_cnt] = pj_str("pcmu");
649 pjsua.codec_deinit[pjsua.codec_cnt] = &pjmedia_codec_g711_deinit;
650 pjsua.codec_cnt++;
651 pjsua.codec_arg[pjsua.codec_cnt] = pj_str("pcma");
652 pjsua.codec_deinit[pjsua.codec_cnt] = &pjmedia_codec_g711_deinit;
653 pjsua.codec_cnt++;
654
655 } else {
656
657 /* If user specifies the exact codec to be used, then create only
658 * those codecs.
659 */
660 int i;
661
662 for (i=0; i<pjsua.codec_cnt; ++i) {
663
664 /* Is it speex? */
665 if (!pj_stricmp2(&pjsua.codec_arg[i], "speex")) {
666
667 unsigned option = PJMEDIA_SPEEX_NO_WB | PJMEDIA_SPEEX_NO_UWB;
668
669 /* Register speex. */
670 if (pjsua.clock_rate >= 16000)
671 option &= ~(PJMEDIA_SPEEX_NO_WB);
672 if (pjsua.clock_rate >= 32000)
673 option &= ~(PJMEDIA_SPEEX_NO_UWB);
674
675 status = pjmedia_codec_speex_init(pjsua.med_endpt, option,
676 -1, -1);
677 if (status != PJ_SUCCESS) {
678 pjsua_perror(THIS_FILE, "Error initializing Speex codec",
679 status);
680 return status;
681 }
682
683 pjsua.codec_deinit[i] = &pjmedia_codec_speex_deinit;
684 }
685 /* Is it gsm? */
686 else if (!pj_stricmp2(&pjsua.codec_arg[i], "gsm")) {
687
688 status = pjmedia_codec_gsm_init(pjsua.med_endpt);
689 if (status != PJ_SUCCESS) {
690 pjsua_perror(THIS_FILE, "Error initializing GSM codec",
691 status);
692 return status;
693 }
694
695 pjsua.codec_deinit[i] = &pjmedia_codec_gsm_deinit;
696
697 }
698 /* Is it pcma/pcmu? */
699 else if (!pj_stricmp2(&pjsua.codec_arg[i], "pcmu") ||
700 !pj_stricmp2(&pjsua.codec_arg[i], "pcma"))
701 {
702
703 status = pjmedia_codec_g711_init(pjsua.med_endpt);
704 if (status != PJ_SUCCESS) {
705 pjsua_perror(THIS_FILE, "Error initializing G711 codec",
706 status);
707 return status;
708 }
709
710 pjsua.codec_deinit[i] = &pjmedia_codec_g711_deinit;
711
712 }
713 /* Don't know about this codec... */
714 else {
715
716 PJ_LOG(1,(THIS_FILE, "Error: unsupported codecs %s",
717 pjsua.codec_arg[i].ptr));
718 return PJMEDIA_CODEC_EUNSUP;
719 }
720 }
721 }
Benny Prijonoccf95622006-02-07 18:48:01 +0000722
Benny Prijonof8baa872006-02-27 23:54:23 +0000723 /* Init conference bridge. */
724
725 status = pjmedia_conf_create(pjsua.pool,
726 pjsua.max_calls+PJSUA_CONF_MORE_PORTS,
Benny Prijono08e0d062006-03-04 14:52:44 +0000727 pjsua.clock_rate,
728 pjsua.clock_rate * 20 / 1000, 16,
729 &pjsua.mconf);
Benny Prijonof8baa872006-02-27 23:54:23 +0000730 if (status != PJ_SUCCESS) {
731 pj_caching_pool_destroy(&pjsua.cp);
732 pjsua_perror(THIS_FILE,
733 "Media stack initialization has returned error",
734 status);
735 return status;
736 }
737
Benny Prijono39879152006-02-23 02:09:10 +0000738 /* Create WAV file player if required: */
739
740 if (pjsua.wav_file) {
741 pjmedia_port *port;
742 pj_str_t port_name;
743
744 /* Create the file player port. */
745 status = pjmedia_file_player_port_create( pjsua.pool, pjsua.wav_file,
746 0, -1, NULL, &port);
747 if (status != PJ_SUCCESS) {
748 pjsua_perror(THIS_FILE,
749 "Error playing media file",
750 status);
751 return status;
752 }
753
754 /* Add port to conference bridge: */
755 status = pjmedia_conf_add_port(pjsua.mconf, pjsua.pool, port,
756 pj_cstr(&port_name, pjsua.wav_file),
757 &pjsua.wav_slot);
758 if (status != PJ_SUCCESS) {
759 pjsua_perror(THIS_FILE,
760 "Unable to add file player to conference bridge",
761 status);
762 return status;
763 }
764 }
765
766
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000767 return PJ_SUCCESS;
768}
769
770
771/*
772 * Start pjsua stack.
773 * This will start the registration process, if registration is configured.
774 */
775pj_status_t pjsua_start(void)
776{
777 int i; /* Must be signed */
778 pjsip_transport *udp_transport;
779 pj_status_t status = PJ_SUCCESS;
780
781 /*
782 * Init media subsystem (codecs, conference bridge, et all).
783 */
784 status = init_media();
785 if (status != PJ_SUCCESS)
786 return status;
787
Benny Prijono268ca612006-02-07 12:34:11 +0000788 /* Init sockets (STUN etc): */
Benny Prijonoa91a0032006-02-26 21:23:45 +0000789 for (i=0; i<(int)pjsua.max_calls; ++i) {
790 status = init_sockets(i==0, &pjsua.calls[i].skinfo);
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000791 if (status != PJ_SUCCESS) {
792 pjsua_perror(THIS_FILE, "init_sockets() has returned error",
793 status);
Benny Prijonoa91a0032006-02-26 21:23:45 +0000794 --i;
795 if (i >= 0)
796 pj_sock_close(pjsua.sip_sock);
797 while (i >= 0) {
798 pj_sock_close(pjsua.calls[i].skinfo.rtp_sock);
799 pj_sock_close(pjsua.calls[i].skinfo.rtcp_sock);
800 }
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000801 return status;
802 }
Benny Prijono268ca612006-02-07 12:34:11 +0000803 }
804
Benny Prijonoccf95622006-02-07 18:48:01 +0000805 /* Add UDP transport: */
Benny Prijono268ca612006-02-07 12:34:11 +0000806
Benny Prijonoccf95622006-02-07 18:48:01 +0000807 {
808 /* Init the published name for the transport.
809 * Depending whether STUN is used, this may be the STUN mapped
810 * address, or socket's bound address.
811 */
812 pjsip_host_port addr_name;
813
814 addr_name.host.ptr = pj_inet_ntoa(pjsua.sip_sock_name.sin_addr);
Benny Prijonof3195072006-02-14 21:15:30 +0000815 addr_name.host.slen = pj_ansi_strlen(addr_name.host.ptr);
Benny Prijonoccf95622006-02-07 18:48:01 +0000816 addr_name.port = pj_ntohs(pjsua.sip_sock_name.sin_port);
817
818 /* Create UDP transport from previously created UDP socket: */
819
820 status = pjsip_udp_transport_attach( pjsua.endpt, pjsua.sip_sock,
821 &addr_name, 1,
822 &udp_transport);
823 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000824 pjsua_perror(THIS_FILE, "Unable to start UDP transport",
825 status);
Benny Prijonoccf95622006-02-07 18:48:01 +0000826 return status;
827 }
Benny Prijono268ca612006-02-07 12:34:11 +0000828 }
829
Benny Prijonoccf95622006-02-07 18:48:01 +0000830 /* Initialize Contact URI, if one is not specified: */
Benny Prijonoa91a0032006-02-26 21:23:45 +0000831 for (i=0; i<pjsua.acc_cnt; ++i) {
Benny Prijonoccf95622006-02-07 18:48:01 +0000832
833 pjsip_uri *uri;
834 pjsip_sip_uri *sip_uri;
Benny Prijonoccf95622006-02-07 18:48:01 +0000835
Benny Prijonoccf95622006-02-07 18:48:01 +0000836 /* Need to parse local_uri to get the elements: */
837
Benny Prijonoa91a0032006-02-26 21:23:45 +0000838 uri = pjsip_parse_uri(pjsua.pool, pjsua.acc[i].local_uri.ptr,
839 pjsua.acc[i].local_uri.slen, 0);
Benny Prijonoccf95622006-02-07 18:48:01 +0000840 if (uri == NULL) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000841 pjsua_perror(THIS_FILE, "Invalid local URI",
842 PJSIP_EINVALIDURI);
Benny Prijonoccf95622006-02-07 18:48:01 +0000843 return PJSIP_EINVALIDURI;
844 }
845
Benny Prijonoccf95622006-02-07 18:48:01 +0000846 /* Local URI MUST be a SIP or SIPS: */
847
Benny Prijonoa91a0032006-02-26 21:23:45 +0000848 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
849 !PJSIP_URI_SCHEME_IS_SIPS(uri))
850 {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000851 pjsua_perror(THIS_FILE, "Invalid local URI",
852 PJSIP_EINVALIDSCHEME);
Benny Prijonoccf95622006-02-07 18:48:01 +0000853 return PJSIP_EINVALIDSCHEME;
854 }
855
856
857 /* Get the SIP URI object: */
858
859 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
860
Benny Prijonoa91a0032006-02-26 21:23:45 +0000861 pjsua.acc[i].user_part = sip_uri->user;
862 pjsua.acc[i].host_part = sip_uri->host;
Benny Prijonoccf95622006-02-07 18:48:01 +0000863
Benny Prijonoa91a0032006-02-26 21:23:45 +0000864 if (pjsua.acc[i].contact_uri.slen == 0 &&
865 pjsua.acc[i].local_uri.slen)
866 {
867 char contact[128];
868 int len;
Benny Prijonoccf95622006-02-07 18:48:01 +0000869
Benny Prijonoa91a0032006-02-26 21:23:45 +0000870 /* The local Contact is the username@ip-addr, where
871 * - username is taken from the local URI,
872 * - ip-addr in UDP transport's address name (which may have been
873 * resolved from STUN.
874 */
875
876 /* Build temporary contact string. */
Benny Prijonoccf95622006-02-07 18:48:01 +0000877
Benny Prijonoa91a0032006-02-26 21:23:45 +0000878 if (sip_uri->user.slen) {
Benny Prijonoccf95622006-02-07 18:48:01 +0000879
Benny Prijonoa91a0032006-02-26 21:23:45 +0000880 /* With the user part. */
881 len = pj_snprintf(contact, sizeof(contact),
882 "<sip:%.*s@%.*s:%d>",
883 (int)sip_uri->user.slen,
884 sip_uri->user.ptr,
885 (int)udp_transport->local_name.host.slen,
886 udp_transport->local_name.host.ptr,
887 udp_transport->local_name.port);
888 } else {
889
890 /* Without user part */
891
892 len = pj_snprintf(contact, sizeof(contact),
893 "<sip:%.*s:%d>",
894 (int)udp_transport->local_name.host.slen,
895 udp_transport->local_name.host.ptr,
896 udp_transport->local_name.port);
897 }
898
899 if (len < 1 || len >= sizeof(contact)) {
900 pjsua_perror(THIS_FILE, "Invalid Contact", PJSIP_EURITOOLONG);
901 return PJSIP_EURITOOLONG;
902 }
903
904 /* Duplicate Contact uri. */
905
906 pj_strdup2(pjsua.pool, &pjsua.acc[i].contact_uri, contact);
907
Benny Prijonoccf95622006-02-07 18:48:01 +0000908 }
Benny Prijonoccf95622006-02-07 18:48:01 +0000909 }
910
Benny Prijono95196582006-02-09 00:13:40 +0000911 /* If outbound_proxy is specified, put it in the route_set: */
Benny Prijonoccf95622006-02-07 18:48:01 +0000912
Benny Prijono95196582006-02-09 00:13:40 +0000913 if (pjsua.outbound_proxy.slen) {
914
915 pjsip_route_hdr *route;
916 const pj_str_t hname = { "Route", 5 };
917 int parsed_len;
918
919 route = pjsip_parse_hdr( pjsua.pool, &hname,
920 pjsua.outbound_proxy.ptr,
921 pjsua.outbound_proxy.slen,
922 &parsed_len);
923 if (route == NULL) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000924 pjsua_perror(THIS_FILE, "Invalid outbound proxy URL",
925 PJSIP_EINVALIDURI);
Benny Prijono95196582006-02-09 00:13:40 +0000926 return PJSIP_EINVALIDURI;
927 }
928
Benny Prijonoa91a0032006-02-26 21:23:45 +0000929 for (i=0; i<pjsua.acc_cnt; ++i) {
930 pj_list_push_front(&pjsua.acc[i].route_set, route);
931 }
Benny Prijono95196582006-02-09 00:13:40 +0000932 }
Benny Prijonoccf95622006-02-07 18:48:01 +0000933
934
Benny Prijono268ca612006-02-07 12:34:11 +0000935 /* Create worker thread(s), if required: */
936
937 for (i=0; i<pjsua.thread_cnt; ++i) {
Benny Prijono834aee32006-02-19 01:38:06 +0000938 status = pj_thread_create( pjsua.pool, "pjsua", &pjsua_poll,
Benny Prijono268ca612006-02-07 12:34:11 +0000939 NULL, 0, 0, &pjsua.threads[i]);
940 if (status != PJ_SUCCESS) {
941 pjsua.quit_flag = 1;
942 for (--i; i>=0; --i) {
943 pj_thread_join(pjsua.threads[i]);
944 pj_thread_destroy(pjsua.threads[i]);
945 }
Benny Prijono268ca612006-02-07 12:34:11 +0000946 return status;
947 }
948 }
949
Benny Prijonoccf95622006-02-07 18:48:01 +0000950 /* Start registration: */
951
952 /* Create client registration session: */
Benny Prijonoa91a0032006-02-26 21:23:45 +0000953 for (i=0; i<pjsua.acc_cnt; ++i) {
954 status = pjsua_regc_init(i);
955 if (status != PJ_SUCCESS)
956 return status;
Benny Prijonoccf95622006-02-07 18:48:01 +0000957
Benny Prijonoa91a0032006-02-26 21:23:45 +0000958 /* Perform registration, if required. */
959 if (pjsua.acc[i].regc) {
960 pjsua_regc_update(i, 1);
961 }
962 }
Benny Prijonoccf95622006-02-07 18:48:01 +0000963
Benny Prijonoa91a0032006-02-26 21:23:45 +0000964
965 /* Find account for outgoing preence subscription */
966 for (i=0; i<pjsua.buddy_cnt; ++i) {
967 pjsua.buddies[i].acc_index =
968 pjsua_find_account_for_outgoing(&pjsua.buddies[i].uri);
Benny Prijonoccf95622006-02-07 18:48:01 +0000969 }
970
971
Benny Prijono834aee32006-02-19 01:38:06 +0000972 PJ_LOG(3,(THIS_FILE, "PJSUA version %s started", PJ_VERSION));
Benny Prijono268ca612006-02-07 12:34:11 +0000973 return PJ_SUCCESS;
974}
975
976
Benny Prijono834aee32006-02-19 01:38:06 +0000977/* Sleep with polling */
978static void busy_sleep(unsigned msec)
979{
980 pj_time_val timeout, now;
981
982 pj_gettimeofday(&timeout);
983 timeout.msec += msec;
984 pj_time_val_normalize(&timeout);
985
986 do {
987 pjsua_poll(NULL);
988 pj_gettimeofday(&now);
989 } while (PJ_TIME_VAL_LT(now, timeout));
990}
991
Benny Prijono268ca612006-02-07 12:34:11 +0000992/*
993 * Destroy pjsua.
994 */
995pj_status_t pjsua_destroy(void)
996{
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000997 int i; /* Must be signed */
Benny Prijono268ca612006-02-07 12:34:11 +0000998
999 /* Signal threads to quit: */
Benny Prijono268ca612006-02-07 12:34:11 +00001000 pjsua.quit_flag = 1;
1001
Benny Prijonoa91a0032006-02-26 21:23:45 +00001002 /* Terminate all calls. */
Benny Prijono1a174142006-03-01 20:46:13 +00001003 pjsua_call_hangup_all();
Benny Prijonoa91a0032006-02-26 21:23:45 +00001004
1005 /* Terminate all presence subscriptions. */
1006 pjsua_pres_shutdown();
1007
1008 /* Unregister, if required: */
1009 for (i=0; i<pjsua.acc_cnt; ++i) {
1010 if (pjsua.acc[i].regc) {
1011 pjsua_regc_update(i, 0);
1012 }
1013 }
1014
Benny Prijono268ca612006-02-07 12:34:11 +00001015 /* Wait worker threads to quit: */
Benny Prijono268ca612006-02-07 12:34:11 +00001016 for (i=0; i<pjsua.thread_cnt; ++i) {
1017
Benny Prijonoe4f2abb2006-02-10 14:04:05 +00001018 if (pjsua.threads[i]) {
1019 pj_thread_join(pjsua.threads[i]);
1020 pj_thread_destroy(pjsua.threads[i]);
1021 pjsua.threads[i] = NULL;
1022 }
Benny Prijono268ca612006-02-07 12:34:11 +00001023 }
1024
Benny Prijono834aee32006-02-19 01:38:06 +00001025
Benny Prijono834aee32006-02-19 01:38:06 +00001026 /* Wait for some time to allow unregistration to complete: */
1027 PJ_LOG(4,(THIS_FILE, "Shutting down..."));
1028 busy_sleep(1000);
1029
Benny Prijono26ff9062006-02-21 23:47:00 +00001030 /* Destroy conference bridge. */
1031 if (pjsua.mconf)
1032 pjmedia_conf_destroy(pjsua.mconf);
1033
Benny Prijono834aee32006-02-19 01:38:06 +00001034 /* Destroy sound framework:
1035 * (this should be done in pjmedia_shutdown())
1036 */
1037 pj_snd_deinit();
1038
Benny Prijonoeb30bf52006-03-04 20:43:52 +00001039 /* Shutdown all codecs: */
1040 for (i = pjsua.codec_cnt-1; i >= 0; --i) {
1041 (*pjsua.codec_deinit[i])();
1042 }
1043
1044 /* Destroy media endpoint. */
1045
1046 pjmedia_endpt_destroy(pjsua.med_endpt);
1047
Benny Prijono268ca612006-02-07 12:34:11 +00001048 /* Destroy endpoint. */
Benny Prijono84126ab2006-02-09 09:30:09 +00001049
Benny Prijono268ca612006-02-07 12:34:11 +00001050 pjsip_endpt_destroy(pjsua.endpt);
1051 pjsua.endpt = NULL;
1052
1053 /* Destroy caching pool. */
Benny Prijono84126ab2006-02-09 09:30:09 +00001054
Benny Prijono268ca612006-02-07 12:34:11 +00001055 pj_caching_pool_destroy(&pjsua.cp);
1056
1057
1058 /* Done. */
1059
1060 return PJ_SUCCESS;
1061}
1062