blob: 2744f4eb4a2c785962cc93b2677f68cf7b5f6f56 [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 Prijono1c2bf462006-03-05 11:54:02 +000075 /* Default for media: */
Benny Prijono08e0d062006-03-04 14:52:44 +000076 pjsua.clock_rate = 8000;
Benny Prijono1c2bf462006-03-05 11:54:02 +000077 pjsua.complexity = 4;
78 pjsua.quality = 4;
79
Benny Prijono08e0d062006-03-04 14:52:44 +000080
Benny Prijonoa91a0032006-02-26 21:23:45 +000081 /* Init accounts: */
82 pjsua.acc_cnt = 1;
83 for (i=0; i<PJ_ARRAY_SIZE(pjsua.acc); ++i) {
84 pjsua.acc[i].index = i;
85 pjsua.acc[i].local_uri = pj_str(PJSUA_LOCAL_URI);
86 pjsua.acc[i].reg_timeout = 55;
87 pj_list_init(&pjsua.acc[i].route_set);
88 pj_list_init(&pjsua.acc[i].pres_srv_list);
89 }
Benny Prijonoccf95622006-02-07 18:48:01 +000090
Benny Prijonoa91a0032006-02-26 21:23:45 +000091 /* Init call array: */
92 for (i=0; i<PJ_ARRAY_SIZE(pjsua.calls); ++i)
93 pjsua.calls[i].index = i;
Benny Prijono95196582006-02-09 00:13:40 +000094
Benny Prijonoa91a0032006-02-26 21:23:45 +000095 /* Default max nb of calls. */
96 pjsua.max_calls = 4;
Benny Prijono834aee32006-02-19 01:38:06 +000097
98 /* Init server presence subscription list: */
99
Benny Prijono834aee32006-02-19 01:38:06 +0000100
Benny Prijono268ca612006-02-07 12:34:11 +0000101}
102
103
Benny Prijono268ca612006-02-07 12:34:11 +0000104
105/*
106 * Handler for receiving incoming requests.
107 *
108 * This handler serves multiple purposes:
109 * - it receives requests outside dialogs.
110 * - it receives requests inside dialogs, when the requests are
111 * unhandled by other dialog usages. Example of these
112 * requests are: MESSAGE.
113 */
114static pj_bool_t mod_pjsua_on_rx_request(pjsip_rx_data *rdata)
115{
Benny Prijono38998232006-02-08 22:44:25 +0000116
Benny Prijono84126ab2006-02-09 09:30:09 +0000117 if (rdata->msg_info.msg->line.req.method.id == PJSIP_INVITE_METHOD) {
Benny Prijono38998232006-02-08 22:44:25 +0000118
Benny Prijonoa91a0032006-02-26 21:23:45 +0000119 return pjsua_call_on_incoming(rdata);
Benny Prijono38998232006-02-08 22:44:25 +0000120 }
121
Benny Prijono268ca612006-02-07 12:34:11 +0000122 return PJ_FALSE;
123}
124
125
126/*
127 * Handler for receiving incoming responses.
128 *
129 * This handler serves multiple purposes:
130 * - it receives strayed responses (i.e. outside any dialog and
131 * outside any transactions).
132 * - it receives responses coming to a transaction, when pjsua
133 * module is set as transaction user for the transaction.
134 * - it receives responses inside a dialog, when these responses
135 * are unhandled by other dialog usages.
136 */
137static pj_bool_t mod_pjsua_on_rx_response(pjsip_rx_data *rdata)
138{
139 PJ_UNUSED_ARG(rdata);
Benny Prijono268ca612006-02-07 12:34:11 +0000140 return PJ_FALSE;
141}
142
143
Benny Prijono268ca612006-02-07 12:34:11 +0000144/*
145 * Initialize sockets and optionally get the public address via STUN.
146 */
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000147static pj_status_t init_sockets(pj_bool_t sip,
148 pjmedia_sock_info *skinfo)
Benny Prijono268ca612006-02-07 12:34:11 +0000149{
150 enum {
Benny Prijonoa91a0032006-02-26 21:23:45 +0000151 RTP_RETRY = 100
Benny Prijono268ca612006-02-07 12:34:11 +0000152 };
153 enum {
154 SIP_SOCK,
155 RTP_SOCK,
156 RTCP_SOCK,
157 };
158 int i;
Benny Prijonocbf37402006-03-01 19:29:10 +0000159 static pj_uint16_t rtp_port;
Benny Prijono268ca612006-02-07 12:34:11 +0000160 pj_sock_t sock[3];
161 pj_sockaddr_in mapped_addr[3];
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000162 pj_status_t status = PJ_SUCCESS;
Benny Prijono268ca612006-02-07 12:34:11 +0000163
Benny Prijonocbf37402006-03-01 19:29:10 +0000164 if (rtp_port == 0)
165 rtp_port = (pj_uint16_t)pjsua.start_rtp_port;
166
Benny Prijono268ca612006-02-07 12:34:11 +0000167 for (i=0; i<3; ++i)
168 sock[i] = PJ_INVALID_SOCKET;
169
Benny Prijonofccab712006-02-22 22:23:22 +0000170 /* Create and bind SIP UDP socket. */
171 status = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, &sock[SIP_SOCK]);
172 if (status != PJ_SUCCESS) {
173 pjsua_perror(THIS_FILE, "socket() error", status);
174 goto on_error;
175 }
176
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000177 if (sip) {
Benny Prijonofccab712006-02-22 22:23:22 +0000178 status = pj_sock_bind_in(sock[SIP_SOCK], 0, pjsua.sip_port);
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000179 if (status != PJ_SUCCESS) {
Benny Prijonofccab712006-02-22 22:23:22 +0000180 pjsua_perror(THIS_FILE, "bind() error", status);
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000181 goto on_error;
182 }
Benny Prijonofccab712006-02-22 22:23:22 +0000183 } else {
184 status = pj_sock_bind_in(sock[SIP_SOCK], 0, 0);
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000185 if (status != PJ_SUCCESS) {
186 pjsua_perror(THIS_FILE, "bind() error", status);
187 goto on_error;
188 }
Benny Prijono268ca612006-02-07 12:34:11 +0000189 }
190
Benny Prijono268ca612006-02-07 12:34:11 +0000191
192 /* Loop retry to bind RTP and RTCP sockets. */
193 for (i=0; i<RTP_RETRY; ++i, rtp_port += 2) {
194
195 /* Create and bind RTP socket. */
196 status = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, &sock[RTP_SOCK]);
197 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000198 pjsua_perror(THIS_FILE, "socket() error", status);
Benny Prijono268ca612006-02-07 12:34:11 +0000199 goto on_error;
200 }
201
202 status = pj_sock_bind_in(sock[RTP_SOCK], 0, rtp_port);
203 if (status != PJ_SUCCESS) {
204 pj_sock_close(sock[RTP_SOCK]);
205 sock[RTP_SOCK] = PJ_INVALID_SOCKET;
206 continue;
207 }
208
209 /* Create and bind RTCP socket. */
210 status = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, &sock[RTCP_SOCK]);
211 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000212 pjsua_perror(THIS_FILE, "socket() error", status);
Benny Prijono268ca612006-02-07 12:34:11 +0000213 goto on_error;
214 }
215
216 status = pj_sock_bind_in(sock[RTCP_SOCK], 0, (pj_uint16_t)(rtp_port+1));
217 if (status != PJ_SUCCESS) {
218 pj_sock_close(sock[RTP_SOCK]);
219 sock[RTP_SOCK] = PJ_INVALID_SOCKET;
220
221 pj_sock_close(sock[RTCP_SOCK]);
222 sock[RTCP_SOCK] = PJ_INVALID_SOCKET;
223 continue;
224 }
225
226 /*
227 * If we're configured to use STUN, then find out the mapped address,
228 * and make sure that the mapped RTCP port is adjacent with the RTP.
229 */
230 if (pjsua.stun_port1 == 0) {
231 const pj_str_t *hostname;
232 pj_sockaddr_in addr;
233
234 /* Get local IP address. */
235 hostname = pj_gethostname();
236
237 pj_memset( &addr, 0, sizeof(addr));
238 addr.sin_family = PJ_AF_INET;
239 status = pj_sockaddr_in_set_str_addr( &addr, hostname);
240 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000241 pjsua_perror(THIS_FILE, "Unresolvable local hostname",
242 status);
Benny Prijono268ca612006-02-07 12:34:11 +0000243 goto on_error;
244 }
245
246 for (i=0; i<3; ++i)
247 pj_memcpy(&mapped_addr[i], &addr, sizeof(addr));
248
Benny Prijonoa91a0032006-02-26 21:23:45 +0000249 if (sip) {
250 mapped_addr[SIP_SOCK].sin_port =
251 pj_htons((pj_uint16_t)pjsua.sip_port);
252 }
253 mapped_addr[RTP_SOCK].sin_port=pj_htons((pj_uint16_t)rtp_port);
254 mapped_addr[RTCP_SOCK].sin_port=pj_htons((pj_uint16_t)(rtp_port+1));
Benny Prijono268ca612006-02-07 12:34:11 +0000255 break;
Benny Prijonoa91a0032006-02-26 21:23:45 +0000256
Benny Prijono268ca612006-02-07 12:34:11 +0000257 } else {
Benny Prijonoa91a0032006-02-26 21:23:45 +0000258 status=pj_stun_get_mapped_addr(&pjsua.cp.factory, 3, sock,
259 &pjsua.stun_srv1, pjsua.stun_port1,
260 &pjsua.stun_srv2, pjsua.stun_port2,
261 mapped_addr);
Benny Prijono268ca612006-02-07 12:34:11 +0000262 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000263 pjsua_perror(THIS_FILE, "STUN error", status);
Benny Prijono268ca612006-02-07 12:34:11 +0000264 goto on_error;
265 }
266
Benny Prijonoa91a0032006-02-26 21:23:45 +0000267 if (pj_ntohs(mapped_addr[2].sin_port) ==
268 pj_ntohs(mapped_addr[1].sin_port)+1)
269 {
Benny Prijono268ca612006-02-07 12:34:11 +0000270 break;
Benny Prijonoa91a0032006-02-26 21:23:45 +0000271 }
Benny Prijono268ca612006-02-07 12:34:11 +0000272
Benny Prijonoa91a0032006-02-26 21:23:45 +0000273 pj_sock_close(sock[RTP_SOCK]);
274 sock[RTP_SOCK] = PJ_INVALID_SOCKET;
275
276 pj_sock_close(sock[RTCP_SOCK]);
277 sock[RTCP_SOCK] = PJ_INVALID_SOCKET;
Benny Prijono268ca612006-02-07 12:34:11 +0000278 }
279 }
280
281 if (sock[RTP_SOCK] == PJ_INVALID_SOCKET) {
Benny Prijonoa91a0032006-02-26 21:23:45 +0000282 PJ_LOG(1,(THIS_FILE,
283 "Unable to find appropriate RTP/RTCP ports combination"));
Benny Prijono268ca612006-02-07 12:34:11 +0000284 goto on_error;
285 }
286
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000287 if (sip) {
288 pjsua.sip_sock = sock[SIP_SOCK];
Benny Prijonoa91a0032006-02-26 21:23:45 +0000289 pj_memcpy(&pjsua.sip_sock_name,
290 &mapped_addr[SIP_SOCK],
291 sizeof(pj_sockaddr_in));
Benny Prijonofccab712006-02-22 22:23:22 +0000292 } else {
293 pj_sock_close(sock[0]);
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000294 }
Benny Prijono95196582006-02-09 00:13:40 +0000295
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000296 skinfo->rtp_sock = sock[RTP_SOCK];
297 pj_memcpy(&skinfo->rtp_addr_name,
Benny Prijono95196582006-02-09 00:13:40 +0000298 &mapped_addr[RTP_SOCK], sizeof(pj_sockaddr_in));
299
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000300 skinfo->rtcp_sock = sock[RTCP_SOCK];
301 pj_memcpy(&skinfo->rtcp_addr_name,
Benny Prijono95196582006-02-09 00:13:40 +0000302 &mapped_addr[RTCP_SOCK], sizeof(pj_sockaddr_in));
Benny Prijono268ca612006-02-07 12:34:11 +0000303
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000304 if (sip) {
305 PJ_LOG(4,(THIS_FILE, "SIP UDP socket reachable at %s:%d",
306 pj_inet_ntoa(pjsua.sip_sock_name.sin_addr),
307 pj_ntohs(pjsua.sip_sock_name.sin_port)));
308 }
Benny Prijono268ca612006-02-07 12:34:11 +0000309 PJ_LOG(4,(THIS_FILE, "RTP socket reachable at %s:%d",
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000310 pj_inet_ntoa(skinfo->rtp_addr_name.sin_addr),
311 pj_ntohs(skinfo->rtp_addr_name.sin_port)));
Benny Prijono268ca612006-02-07 12:34:11 +0000312 PJ_LOG(4,(THIS_FILE, "RTCP UDP socket reachable at %s:%d",
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000313 pj_inet_ntoa(skinfo->rtcp_addr_name.sin_addr),
314 pj_ntohs(skinfo->rtcp_addr_name.sin_port)));
Benny Prijono268ca612006-02-07 12:34:11 +0000315
Benny Prijonofccab712006-02-22 22:23:22 +0000316 rtp_port += 2;
Benny Prijono268ca612006-02-07 12:34:11 +0000317 return PJ_SUCCESS;
318
319on_error:
320 for (i=0; i<3; ++i) {
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000321 if (sip && i==0)
322 continue;
Benny Prijono268ca612006-02-07 12:34:11 +0000323 if (sock[i] != PJ_INVALID_SOCKET)
324 pj_sock_close(sock[i]);
325 }
326 return status;
327}
328
329
330
331/*
332 * Initialize stack.
333 */
334static pj_status_t init_stack(void)
335{
336 pj_status_t status;
337
338 /* Create global endpoint: */
339
340 {
341 const pj_str_t *hostname;
342 const char *endpt_name;
343
344 /* Endpoint MUST be assigned a globally unique name.
345 * The name will be used as the hostname in Warning header.
346 */
347
348 /* For this implementation, we'll use hostname for simplicity */
349 hostname = pj_gethostname();
350 endpt_name = hostname->ptr;
351
352 /* Create the endpoint: */
353
354 status = pjsip_endpt_create(&pjsua.cp.factory, endpt_name,
355 &pjsua.endpt);
356 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000357 pjsua_perror(THIS_FILE, "Unable to create SIP endpoint", status);
Benny Prijono268ca612006-02-07 12:34:11 +0000358 return status;
359 }
360 }
361
362
363 /* Initialize transaction layer: */
364
Benny Prijono2f8992b2006-02-25 21:16:36 +0000365 status = pjsip_tsx_layer_init_module(pjsua.endpt);
Benny Prijono268ca612006-02-07 12:34:11 +0000366 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000367 pjsua_perror(THIS_FILE, "Transaction layer initialization error",
368 status);
Benny Prijono268ca612006-02-07 12:34:11 +0000369 goto on_error;
370 }
371
372 /* Initialize UA layer module: */
373
Benny Prijono2f8992b2006-02-25 21:16:36 +0000374 status = pjsip_ua_init_module( pjsua.endpt, NULL );
Benny Prijono268ca612006-02-07 12:34:11 +0000375 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000376 pjsua_perror(THIS_FILE, "UA layer initialization error", status);
Benny Prijono268ca612006-02-07 12:34:11 +0000377 goto on_error;
378 }
379
380 /* Initialize and register pjsua's application module: */
381
382 {
383 pjsip_module my_mod =
384 {
385 NULL, NULL, /* prev, next. */
386 { "mod-pjsua", 9 }, /* Name. */
387 -1, /* Id */
388 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
Benny Prijono268ca612006-02-07 12:34:11 +0000389 NULL, /* load() */
390 NULL, /* start() */
391 NULL, /* stop() */
392 NULL, /* unload() */
393 &mod_pjsua_on_rx_request, /* on_rx_request() */
394 &mod_pjsua_on_rx_response, /* on_rx_response() */
395 NULL, /* on_tx_request. */
396 NULL, /* on_tx_response() */
397 NULL, /* on_tsx_state() */
398 };
399
400 pjsua.mod = my_mod;
401
402 status = pjsip_endpt_register_module(pjsua.endpt, &pjsua.mod);
403 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000404 pjsua_perror(THIS_FILE, "Unable to register pjsua module",
405 status);
Benny Prijono268ca612006-02-07 12:34:11 +0000406 goto on_error;
407 }
408 }
409
410 /* Initialize invite session module: */
411
Benny Prijonoa91a0032006-02-26 21:23:45 +0000412 status = pjsua_call_init();
413 if (status != PJ_SUCCESS) {
414 pjsua_perror(THIS_FILE, "Invite usage initialization error",
415 status);
416 goto on_error;
Benny Prijono268ca612006-02-07 12:34:11 +0000417 }
418
Benny Prijonoccf95622006-02-07 18:48:01 +0000419 /* Done */
Benny Prijono268ca612006-02-07 12:34:11 +0000420
421 return PJ_SUCCESS;
422
423
424on_error:
425 pjsip_endpt_destroy(pjsua.endpt);
426 pjsua.endpt = NULL;
427 return status;
428}
429
430
Benny Prijono834aee32006-02-19 01:38:06 +0000431static int PJ_THREAD_FUNC pjsua_poll(void *arg)
Benny Prijono268ca612006-02-07 12:34:11 +0000432{
Benny Prijonof8baa872006-02-27 23:54:23 +0000433 pj_status_t last_err = 0;
434
Benny Prijono268ca612006-02-07 12:34:11 +0000435 PJ_UNUSED_ARG(arg);
436
Benny Prijono834aee32006-02-19 01:38:06 +0000437 do {
Benny Prijono268ca612006-02-07 12:34:11 +0000438 pj_time_val timeout = { 0, 10 };
Benny Prijonof8baa872006-02-27 23:54:23 +0000439 pj_status_t status;
440
441 status = pjsip_endpt_handle_events (pjsua.endpt, &timeout);
442 if (status != last_err) {
443 last_err = status;
444 pjsua_perror(THIS_FILE, "handle_events() returned error", status);
445 }
Benny Prijono834aee32006-02-19 01:38:06 +0000446 } while (!pjsua.quit_flag);
Benny Prijono268ca612006-02-07 12:34:11 +0000447
448 return 0;
449}
450
451/*
452 * Initialize pjsua application.
Benny Prijonoccf95622006-02-07 18:48:01 +0000453 * This will initialize all libraries, create endpoint instance, and register
454 * pjsip modules.
Benny Prijono268ca612006-02-07 12:34:11 +0000455 */
456pj_status_t pjsua_init(void)
457{
Benny Prijono268ca612006-02-07 12:34:11 +0000458 pj_status_t status;
459
460 /* Init PJLIB logging: */
461
462 pj_log_set_level(pjsua.log_level);
463 pj_log_set_decor(pjsua.log_decor);
464
465
466 /* Init PJLIB: */
467
468 status = pj_init();
469 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000470 pjsua_perror(THIS_FILE, "pj_init() error", status);
Benny Prijono268ca612006-02-07 12:34:11 +0000471 return status;
472 }
473
Benny Prijonofccab712006-02-22 22:23:22 +0000474 /* Init PJLIB-UTIL: */
475
476 status = pjlib_util_init();
477 if (status != PJ_SUCCESS) {
478 pjsua_perror(THIS_FILE, "pjlib_util_init() error", status);
479 return status;
480 }
481
Benny Prijono268ca612006-02-07 12:34:11 +0000482 /* Init memory pool: */
483
484 /* Init caching pool. */
485 pj_caching_pool_init(&pjsua.cp, &pj_pool_factory_default_policy, 0);
486
487 /* Create memory pool for application. */
488 pjsua.pool = pj_pool_create(&pjsua.cp.factory, "pjsua", 4000, 4000, NULL);
489
490
Benny Prijono834aee32006-02-19 01:38:06 +0000491 /* Init PJSIP : */
Benny Prijonoccf95622006-02-07 18:48:01 +0000492
493 status = init_stack();
494 if (status != PJ_SUCCESS) {
495 pj_caching_pool_destroy(&pjsua.cp);
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000496 pjsua_perror(THIS_FILE, "Stack initialization has returned error",
497 status);
Benny Prijonoccf95622006-02-07 18:48:01 +0000498 return status;
499 }
500
Benny Prijono95196582006-02-09 00:13:40 +0000501
Benny Prijono834aee32006-02-19 01:38:06 +0000502 /* Init core SIMPLE module : */
503
504 pjsip_evsub_init_module(pjsua.endpt);
505
506 /* Init presence module: */
507
508 pjsip_pres_init_module( pjsua.endpt, pjsip_evsub_instance());
509
Benny Prijono26ff9062006-02-21 23:47:00 +0000510 /* Init xfer/REFER module */
511
512 pjsip_xfer_init_module( pjsua.endpt );
Benny Prijono834aee32006-02-19 01:38:06 +0000513
514 /* Init pjsua presence handler: */
515
516 pjsua_pres_init();
517
Benny Prijonob0808372006-03-02 21:18:58 +0000518 /* Init out-of-dialog MESSAGE request handler. */
519
520 pjsua_im_init();
521
Benny Prijono834aee32006-02-19 01:38:06 +0000522
Benny Prijono95196582006-02-09 00:13:40 +0000523 /* Init media endpoint: */
524
525 status = pjmedia_endpt_create(&pjsua.cp.factory, &pjsua.med_endpt);
526 if (status != PJ_SUCCESS) {
527 pj_caching_pool_destroy(&pjsua.cp);
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000528 pjsua_perror(THIS_FILE,
529 "Media stack initialization has returned error",
530 status);
Benny Prijono95196582006-02-09 00:13:40 +0000531 return status;
532 }
533
Benny Prijonoccf95622006-02-07 18:48:01 +0000534 /* Done. */
535 return PJ_SUCCESS;
536}
537
538
Benny Prijonoa91a0032006-02-26 21:23:45 +0000539/*
540 * Find account for incoming request.
541 */
542int pjsua_find_account_for_incoming(pjsip_rx_data *rdata)
543{
544 pjsip_uri *uri;
545 pjsip_sip_uri *sip_uri;
546 int acc_index;
547
548 uri = rdata->msg_info.to->uri;
549
550 /* Just return account #0 if To URI is not SIP: */
551 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
552 !PJSIP_URI_SCHEME_IS_SIPS(uri))
553 {
554 return 0;
555 }
556
557
558 sip_uri = (pjsip_sip_uri*)pjsip_uri_get_uri(uri);
559
560 /* Find account which has matching username and domain. */
561 for (acc_index=0; acc_index < pjsua.acc_cnt; ++acc_index) {
562
563 pjsua_acc *acc = &pjsua.acc[acc_index];
564
565 if (pj_stricmp(&acc->user_part, &sip_uri->user)==0 &&
566 pj_stricmp(&acc->host_part, &sip_uri->host)==0)
567 {
568 /* Match ! */
569 return acc_index;
570 }
571 }
572
573 /* No matching, try match domain part only. */
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->host_part, &sip_uri->host)==0) {
579 /* Match ! */
580 return acc_index;
581 }
582 }
583
584 /* Still no match, just return account #0 */
585 return 0;
586}
587
588
589/*
590 * Find account for outgoing request.
591 */
592int pjsua_find_account_for_outgoing(const pj_str_t *url)
593{
594 PJ_UNUSED_ARG(url);
595
596 /* Just use account #0 */
597 return 0;
598}
599
Benny Prijonoccf95622006-02-07 18:48:01 +0000600
601/*
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000602 * Init media.
Benny Prijonoccf95622006-02-07 18:48:01 +0000603 */
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000604static pj_status_t init_media(void)
Benny Prijonoccf95622006-02-07 18:48:01 +0000605{
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000606
607 pj_status_t status;
608
609 /* If user doesn't specify any codecs, register all of them. */
610 if (pjsua.codec_cnt == 0) {
611
612 unsigned option = PJMEDIA_SPEEX_NO_WB | PJMEDIA_SPEEX_NO_UWB;
613
614 /* Register speex. */
615 if (pjsua.clock_rate >= 16000)
616 option &= ~(PJMEDIA_SPEEX_NO_WB);
617 if (pjsua.clock_rate >= 32000)
618 option &= ~(PJMEDIA_SPEEX_NO_UWB);
619
Benny Prijono1c2bf462006-03-05 11:54:02 +0000620 status = pjmedia_codec_speex_init(pjsua.med_endpt, option,
621 pjsua.quality, pjsua.complexity );
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000622 if (status != PJ_SUCCESS) {
623 pjsua_perror(THIS_FILE, "Error initializing Speex codec",
624 status);
625 return status;
626 }
627
628 pjsua.codec_arg[pjsua.codec_cnt] = pj_str("speex");
629 pjsua.codec_deinit[pjsua.codec_cnt] = &pjmedia_codec_speex_deinit;
630 pjsua.codec_cnt++;
631
632 /* Register GSM */
633 status = pjmedia_codec_gsm_init(pjsua.med_endpt);
634 if (status != PJ_SUCCESS) {
635 pjsua_perror(THIS_FILE, "Error initializing GSM codec",
636 status);
637 return status;
638 }
639
640 pjsua.codec_arg[pjsua.codec_cnt] = pj_str("gsm");
641 pjsua.codec_deinit[pjsua.codec_cnt] = &pjmedia_codec_gsm_deinit;
642 pjsua.codec_cnt++;
643
644 /* Register PCMA and PCMU */
645 status = pjmedia_codec_g711_init(pjsua.med_endpt);
646 if (status != PJ_SUCCESS) {
647 pjsua_perror(THIS_FILE, "Error initializing G711 codec",
648 status);
649 return status;
650 }
651
652 pjsua.codec_arg[pjsua.codec_cnt] = pj_str("pcmu");
653 pjsua.codec_deinit[pjsua.codec_cnt] = &pjmedia_codec_g711_deinit;
654 pjsua.codec_cnt++;
655 pjsua.codec_arg[pjsua.codec_cnt] = pj_str("pcma");
656 pjsua.codec_deinit[pjsua.codec_cnt] = &pjmedia_codec_g711_deinit;
657 pjsua.codec_cnt++;
658
659 } else {
660
661 /* If user specifies the exact codec to be used, then create only
662 * those codecs.
663 */
664 int i;
665
666 for (i=0; i<pjsua.codec_cnt; ++i) {
667
668 /* Is it speex? */
669 if (!pj_stricmp2(&pjsua.codec_arg[i], "speex")) {
670
671 unsigned option = PJMEDIA_SPEEX_NO_WB | PJMEDIA_SPEEX_NO_UWB;
672
673 /* Register speex. */
674 if (pjsua.clock_rate >= 16000)
675 option &= ~(PJMEDIA_SPEEX_NO_WB);
676 if (pjsua.clock_rate >= 32000)
677 option &= ~(PJMEDIA_SPEEX_NO_UWB);
678
679 status = pjmedia_codec_speex_init(pjsua.med_endpt, option,
680 -1, -1);
681 if (status != PJ_SUCCESS) {
682 pjsua_perror(THIS_FILE, "Error initializing Speex codec",
683 status);
684 return status;
685 }
686
687 pjsua.codec_deinit[i] = &pjmedia_codec_speex_deinit;
688 }
689 /* Is it gsm? */
690 else if (!pj_stricmp2(&pjsua.codec_arg[i], "gsm")) {
691
692 status = pjmedia_codec_gsm_init(pjsua.med_endpt);
693 if (status != PJ_SUCCESS) {
694 pjsua_perror(THIS_FILE, "Error initializing GSM codec",
695 status);
696 return status;
697 }
698
699 pjsua.codec_deinit[i] = &pjmedia_codec_gsm_deinit;
700
701 }
702 /* Is it pcma/pcmu? */
703 else if (!pj_stricmp2(&pjsua.codec_arg[i], "pcmu") ||
704 !pj_stricmp2(&pjsua.codec_arg[i], "pcma"))
705 {
706
707 status = pjmedia_codec_g711_init(pjsua.med_endpt);
708 if (status != PJ_SUCCESS) {
709 pjsua_perror(THIS_FILE, "Error initializing G711 codec",
710 status);
711 return status;
712 }
713
714 pjsua.codec_deinit[i] = &pjmedia_codec_g711_deinit;
715
716 }
717 /* Don't know about this codec... */
718 else {
719
720 PJ_LOG(1,(THIS_FILE, "Error: unsupported codecs %s",
721 pjsua.codec_arg[i].ptr));
722 return PJMEDIA_CODEC_EUNSUP;
723 }
724 }
725 }
Benny Prijonoccf95622006-02-07 18:48:01 +0000726
Benny Prijonof8baa872006-02-27 23:54:23 +0000727 /* Init conference bridge. */
728
729 status = pjmedia_conf_create(pjsua.pool,
730 pjsua.max_calls+PJSUA_CONF_MORE_PORTS,
Benny Prijono08e0d062006-03-04 14:52:44 +0000731 pjsua.clock_rate,
732 pjsua.clock_rate * 20 / 1000, 16,
733 &pjsua.mconf);
Benny Prijonof8baa872006-02-27 23:54:23 +0000734 if (status != PJ_SUCCESS) {
735 pj_caching_pool_destroy(&pjsua.cp);
736 pjsua_perror(THIS_FILE,
737 "Media stack initialization has returned error",
738 status);
739 return status;
740 }
741
Benny Prijono39879152006-02-23 02:09:10 +0000742 /* Create WAV file player if required: */
743
744 if (pjsua.wav_file) {
745 pjmedia_port *port;
746 pj_str_t port_name;
747
748 /* Create the file player port. */
749 status = pjmedia_file_player_port_create( pjsua.pool, pjsua.wav_file,
750 0, -1, NULL, &port);
751 if (status != PJ_SUCCESS) {
752 pjsua_perror(THIS_FILE,
753 "Error playing media file",
754 status);
755 return status;
756 }
757
758 /* Add port to conference bridge: */
759 status = pjmedia_conf_add_port(pjsua.mconf, pjsua.pool, port,
760 pj_cstr(&port_name, pjsua.wav_file),
761 &pjsua.wav_slot);
762 if (status != PJ_SUCCESS) {
763 pjsua_perror(THIS_FILE,
764 "Unable to add file player to conference bridge",
765 status);
766 return status;
767 }
768 }
769
770
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000771 return PJ_SUCCESS;
772}
773
774
775/*
776 * Start pjsua stack.
777 * This will start the registration process, if registration is configured.
778 */
779pj_status_t pjsua_start(void)
780{
781 int i; /* Must be signed */
782 pjsip_transport *udp_transport;
783 pj_status_t status = PJ_SUCCESS;
784
785 /*
786 * Init media subsystem (codecs, conference bridge, et all).
787 */
788 status = init_media();
789 if (status != PJ_SUCCESS)
790 return status;
791
Benny Prijono268ca612006-02-07 12:34:11 +0000792 /* Init sockets (STUN etc): */
Benny Prijonoa91a0032006-02-26 21:23:45 +0000793 for (i=0; i<(int)pjsua.max_calls; ++i) {
794 status = init_sockets(i==0, &pjsua.calls[i].skinfo);
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000795 if (status != PJ_SUCCESS) {
796 pjsua_perror(THIS_FILE, "init_sockets() has returned error",
797 status);
Benny Prijonoa91a0032006-02-26 21:23:45 +0000798 --i;
799 if (i >= 0)
800 pj_sock_close(pjsua.sip_sock);
801 while (i >= 0) {
802 pj_sock_close(pjsua.calls[i].skinfo.rtp_sock);
803 pj_sock_close(pjsua.calls[i].skinfo.rtcp_sock);
804 }
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000805 return status;
806 }
Benny Prijono268ca612006-02-07 12:34:11 +0000807 }
808
Benny Prijonoccf95622006-02-07 18:48:01 +0000809 /* Add UDP transport: */
Benny Prijono268ca612006-02-07 12:34:11 +0000810
Benny Prijonoccf95622006-02-07 18:48:01 +0000811 {
812 /* Init the published name for the transport.
813 * Depending whether STUN is used, this may be the STUN mapped
814 * address, or socket's bound address.
815 */
816 pjsip_host_port addr_name;
817
818 addr_name.host.ptr = pj_inet_ntoa(pjsua.sip_sock_name.sin_addr);
Benny Prijonof3195072006-02-14 21:15:30 +0000819 addr_name.host.slen = pj_ansi_strlen(addr_name.host.ptr);
Benny Prijonoccf95622006-02-07 18:48:01 +0000820 addr_name.port = pj_ntohs(pjsua.sip_sock_name.sin_port);
821
822 /* Create UDP transport from previously created UDP socket: */
823
824 status = pjsip_udp_transport_attach( pjsua.endpt, pjsua.sip_sock,
825 &addr_name, 1,
826 &udp_transport);
827 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000828 pjsua_perror(THIS_FILE, "Unable to start UDP transport",
829 status);
Benny Prijonoccf95622006-02-07 18:48:01 +0000830 return status;
831 }
Benny Prijono268ca612006-02-07 12:34:11 +0000832 }
833
Benny Prijonoccf95622006-02-07 18:48:01 +0000834 /* Initialize Contact URI, if one is not specified: */
Benny Prijonoa91a0032006-02-26 21:23:45 +0000835 for (i=0; i<pjsua.acc_cnt; ++i) {
Benny Prijonoccf95622006-02-07 18:48:01 +0000836
837 pjsip_uri *uri;
838 pjsip_sip_uri *sip_uri;
Benny Prijonoccf95622006-02-07 18:48:01 +0000839
Benny Prijonoccf95622006-02-07 18:48:01 +0000840 /* Need to parse local_uri to get the elements: */
841
Benny Prijonoa91a0032006-02-26 21:23:45 +0000842 uri = pjsip_parse_uri(pjsua.pool, pjsua.acc[i].local_uri.ptr,
843 pjsua.acc[i].local_uri.slen, 0);
Benny Prijonoccf95622006-02-07 18:48:01 +0000844 if (uri == NULL) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000845 pjsua_perror(THIS_FILE, "Invalid local URI",
846 PJSIP_EINVALIDURI);
Benny Prijonoccf95622006-02-07 18:48:01 +0000847 return PJSIP_EINVALIDURI;
848 }
849
Benny Prijonoccf95622006-02-07 18:48:01 +0000850 /* Local URI MUST be a SIP or SIPS: */
851
Benny Prijonoa91a0032006-02-26 21:23:45 +0000852 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
853 !PJSIP_URI_SCHEME_IS_SIPS(uri))
854 {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000855 pjsua_perror(THIS_FILE, "Invalid local URI",
856 PJSIP_EINVALIDSCHEME);
Benny Prijonoccf95622006-02-07 18:48:01 +0000857 return PJSIP_EINVALIDSCHEME;
858 }
859
860
861 /* Get the SIP URI object: */
862
863 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
864
Benny Prijonoa91a0032006-02-26 21:23:45 +0000865 pjsua.acc[i].user_part = sip_uri->user;
866 pjsua.acc[i].host_part = sip_uri->host;
Benny Prijonoccf95622006-02-07 18:48:01 +0000867
Benny Prijonoa91a0032006-02-26 21:23:45 +0000868 if (pjsua.acc[i].contact_uri.slen == 0 &&
869 pjsua.acc[i].local_uri.slen)
870 {
871 char contact[128];
872 int len;
Benny Prijonoccf95622006-02-07 18:48:01 +0000873
Benny Prijonoa91a0032006-02-26 21:23:45 +0000874 /* The local Contact is the username@ip-addr, where
875 * - username is taken from the local URI,
876 * - ip-addr in UDP transport's address name (which may have been
877 * resolved from STUN.
878 */
879
880 /* Build temporary contact string. */
Benny Prijonoccf95622006-02-07 18:48:01 +0000881
Benny Prijonoa91a0032006-02-26 21:23:45 +0000882 if (sip_uri->user.slen) {
Benny Prijonoccf95622006-02-07 18:48:01 +0000883
Benny Prijonoa91a0032006-02-26 21:23:45 +0000884 /* With the user part. */
885 len = pj_snprintf(contact, sizeof(contact),
886 "<sip:%.*s@%.*s:%d>",
887 (int)sip_uri->user.slen,
888 sip_uri->user.ptr,
889 (int)udp_transport->local_name.host.slen,
890 udp_transport->local_name.host.ptr,
891 udp_transport->local_name.port);
892 } else {
893
894 /* Without user part */
895
896 len = pj_snprintf(contact, sizeof(contact),
897 "<sip:%.*s:%d>",
898 (int)udp_transport->local_name.host.slen,
899 udp_transport->local_name.host.ptr,
900 udp_transport->local_name.port);
901 }
902
903 if (len < 1 || len >= sizeof(contact)) {
904 pjsua_perror(THIS_FILE, "Invalid Contact", PJSIP_EURITOOLONG);
905 return PJSIP_EURITOOLONG;
906 }
907
908 /* Duplicate Contact uri. */
909
910 pj_strdup2(pjsua.pool, &pjsua.acc[i].contact_uri, contact);
911
Benny Prijonoccf95622006-02-07 18:48:01 +0000912 }
Benny Prijonoccf95622006-02-07 18:48:01 +0000913 }
914
Benny Prijono95196582006-02-09 00:13:40 +0000915 /* If outbound_proxy is specified, put it in the route_set: */
Benny Prijonoccf95622006-02-07 18:48:01 +0000916
Benny Prijono95196582006-02-09 00:13:40 +0000917 if (pjsua.outbound_proxy.slen) {
918
919 pjsip_route_hdr *route;
920 const pj_str_t hname = { "Route", 5 };
921 int parsed_len;
922
923 route = pjsip_parse_hdr( pjsua.pool, &hname,
924 pjsua.outbound_proxy.ptr,
925 pjsua.outbound_proxy.slen,
926 &parsed_len);
927 if (route == NULL) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000928 pjsua_perror(THIS_FILE, "Invalid outbound proxy URL",
929 PJSIP_EINVALIDURI);
Benny Prijono95196582006-02-09 00:13:40 +0000930 return PJSIP_EINVALIDURI;
931 }
932
Benny Prijonoa91a0032006-02-26 21:23:45 +0000933 for (i=0; i<pjsua.acc_cnt; ++i) {
934 pj_list_push_front(&pjsua.acc[i].route_set, route);
935 }
Benny Prijono95196582006-02-09 00:13:40 +0000936 }
Benny Prijonoccf95622006-02-07 18:48:01 +0000937
938
Benny Prijono268ca612006-02-07 12:34:11 +0000939 /* Create worker thread(s), if required: */
940
941 for (i=0; i<pjsua.thread_cnt; ++i) {
Benny Prijono834aee32006-02-19 01:38:06 +0000942 status = pj_thread_create( pjsua.pool, "pjsua", &pjsua_poll,
Benny Prijono268ca612006-02-07 12:34:11 +0000943 NULL, 0, 0, &pjsua.threads[i]);
944 if (status != PJ_SUCCESS) {
945 pjsua.quit_flag = 1;
946 for (--i; i>=0; --i) {
947 pj_thread_join(pjsua.threads[i]);
948 pj_thread_destroy(pjsua.threads[i]);
949 }
Benny Prijono268ca612006-02-07 12:34:11 +0000950 return status;
951 }
952 }
953
Benny Prijonoccf95622006-02-07 18:48:01 +0000954 /* Start registration: */
955
956 /* Create client registration session: */
Benny Prijonoa91a0032006-02-26 21:23:45 +0000957 for (i=0; i<pjsua.acc_cnt; ++i) {
958 status = pjsua_regc_init(i);
959 if (status != PJ_SUCCESS)
960 return status;
Benny Prijonoccf95622006-02-07 18:48:01 +0000961
Benny Prijonoa91a0032006-02-26 21:23:45 +0000962 /* Perform registration, if required. */
963 if (pjsua.acc[i].regc) {
964 pjsua_regc_update(i, 1);
965 }
966 }
Benny Prijonoccf95622006-02-07 18:48:01 +0000967
Benny Prijonoa91a0032006-02-26 21:23:45 +0000968
969 /* Find account for outgoing preence subscription */
970 for (i=0; i<pjsua.buddy_cnt; ++i) {
971 pjsua.buddies[i].acc_index =
972 pjsua_find_account_for_outgoing(&pjsua.buddies[i].uri);
Benny Prijonoccf95622006-02-07 18:48:01 +0000973 }
974
975
Benny Prijono834aee32006-02-19 01:38:06 +0000976 PJ_LOG(3,(THIS_FILE, "PJSUA version %s started", PJ_VERSION));
Benny Prijono268ca612006-02-07 12:34:11 +0000977 return PJ_SUCCESS;
978}
979
980
Benny Prijono834aee32006-02-19 01:38:06 +0000981/* Sleep with polling */
982static void busy_sleep(unsigned msec)
983{
984 pj_time_val timeout, now;
985
986 pj_gettimeofday(&timeout);
987 timeout.msec += msec;
988 pj_time_val_normalize(&timeout);
989
990 do {
991 pjsua_poll(NULL);
992 pj_gettimeofday(&now);
993 } while (PJ_TIME_VAL_LT(now, timeout));
994}
995
Benny Prijono268ca612006-02-07 12:34:11 +0000996/*
997 * Destroy pjsua.
998 */
999pj_status_t pjsua_destroy(void)
1000{
Benny Prijonoeb30bf52006-03-04 20:43:52 +00001001 int i; /* Must be signed */
Benny Prijono268ca612006-02-07 12:34:11 +00001002
1003 /* Signal threads to quit: */
Benny Prijono268ca612006-02-07 12:34:11 +00001004 pjsua.quit_flag = 1;
1005
Benny Prijonoa91a0032006-02-26 21:23:45 +00001006 /* Terminate all calls. */
Benny Prijono1a174142006-03-01 20:46:13 +00001007 pjsua_call_hangup_all();
Benny Prijonoa91a0032006-02-26 21:23:45 +00001008
1009 /* Terminate all presence subscriptions. */
1010 pjsua_pres_shutdown();
1011
1012 /* Unregister, if required: */
1013 for (i=0; i<pjsua.acc_cnt; ++i) {
1014 if (pjsua.acc[i].regc) {
1015 pjsua_regc_update(i, 0);
1016 }
1017 }
1018
Benny Prijono268ca612006-02-07 12:34:11 +00001019 /* Wait worker threads to quit: */
Benny Prijono268ca612006-02-07 12:34:11 +00001020 for (i=0; i<pjsua.thread_cnt; ++i) {
1021
Benny Prijonoe4f2abb2006-02-10 14:04:05 +00001022 if (pjsua.threads[i]) {
1023 pj_thread_join(pjsua.threads[i]);
1024 pj_thread_destroy(pjsua.threads[i]);
1025 pjsua.threads[i] = NULL;
1026 }
Benny Prijono268ca612006-02-07 12:34:11 +00001027 }
1028
Benny Prijono834aee32006-02-19 01:38:06 +00001029
Benny Prijono834aee32006-02-19 01:38:06 +00001030 /* Wait for some time to allow unregistration to complete: */
1031 PJ_LOG(4,(THIS_FILE, "Shutting down..."));
1032 busy_sleep(1000);
1033
Benny Prijono26ff9062006-02-21 23:47:00 +00001034 /* Destroy conference bridge. */
1035 if (pjsua.mconf)
1036 pjmedia_conf_destroy(pjsua.mconf);
1037
Benny Prijono834aee32006-02-19 01:38:06 +00001038 /* Destroy sound framework:
1039 * (this should be done in pjmedia_shutdown())
1040 */
1041 pj_snd_deinit();
1042
Benny Prijonoeb30bf52006-03-04 20:43:52 +00001043 /* Shutdown all codecs: */
1044 for (i = pjsua.codec_cnt-1; i >= 0; --i) {
1045 (*pjsua.codec_deinit[i])();
1046 }
1047
1048 /* Destroy media endpoint. */
1049
1050 pjmedia_endpt_destroy(pjsua.med_endpt);
1051
Benny Prijono268ca612006-02-07 12:34:11 +00001052 /* Destroy endpoint. */
Benny Prijono84126ab2006-02-09 09:30:09 +00001053
Benny Prijono268ca612006-02-07 12:34:11 +00001054 pjsip_endpt_destroy(pjsua.endpt);
1055 pjsua.endpt = NULL;
1056
1057 /* Destroy caching pool. */
Benny Prijono84126ab2006-02-09 09:30:09 +00001058
Benny Prijono268ca612006-02-07 12:34:11 +00001059 pj_caching_pool_destroy(&pjsua.cp);
1060
1061
1062 /* Done. */
1063
1064 return PJ_SUCCESS;
1065}
1066