blob: dd71f135181df24eda23204ad48e716e7d5a3106 [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
Benny Prijono4381efe2006-03-16 14:24:26 +0000620#if PJMEDIA_HAS_SPEEX_CODEC
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000621 unsigned option = PJMEDIA_SPEEX_NO_WB | PJMEDIA_SPEEX_NO_UWB;
622
623 /* Register speex. */
624 if (pjsua.clock_rate >= 16000)
625 option &= ~(PJMEDIA_SPEEX_NO_WB);
626 if (pjsua.clock_rate >= 32000)
627 option &= ~(PJMEDIA_SPEEX_NO_UWB);
628
Benny Prijono1c2bf462006-03-05 11:54:02 +0000629 status = pjmedia_codec_speex_init(pjsua.med_endpt, option,
630 pjsua.quality, pjsua.complexity );
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000631 if (status != PJ_SUCCESS) {
632 pjsua_perror(THIS_FILE, "Error initializing Speex codec",
633 status);
634 return status;
635 }
636
637 pjsua.codec_arg[pjsua.codec_cnt] = pj_str("speex");
638 pjsua.codec_deinit[pjsua.codec_cnt] = &pjmedia_codec_speex_deinit;
639 pjsua.codec_cnt++;
Benny Prijono4381efe2006-03-16 14:24:26 +0000640#endif /* PJMEDIA_HAS_SPEEX_CODEC */
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000641
Benny Prijono4381efe2006-03-16 14:24:26 +0000642#if PJMEDIA_HAS_GSM_CODEC
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000643 /* Register GSM */
644 status = pjmedia_codec_gsm_init(pjsua.med_endpt);
645 if (status != PJ_SUCCESS) {
646 pjsua_perror(THIS_FILE, "Error initializing GSM codec",
647 status);
648 return status;
649 }
650
651 pjsua.codec_arg[pjsua.codec_cnt] = pj_str("gsm");
652 pjsua.codec_deinit[pjsua.codec_cnt] = &pjmedia_codec_gsm_deinit;
653 pjsua.codec_cnt++;
Benny Prijono4381efe2006-03-16 14:24:26 +0000654#endif /* PJMEDIA_HAS_GSM_CODEC */
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000655
Benny Prijono4381efe2006-03-16 14:24:26 +0000656#if PJMEDIA_HAS_G711_CODEC
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000657 /* Register PCMA and PCMU */
658 status = pjmedia_codec_g711_init(pjsua.med_endpt);
659 if (status != PJ_SUCCESS) {
660 pjsua_perror(THIS_FILE, "Error initializing G711 codec",
661 status);
662 return status;
663 }
664
665 pjsua.codec_arg[pjsua.codec_cnt] = pj_str("pcmu");
666 pjsua.codec_deinit[pjsua.codec_cnt] = &pjmedia_codec_g711_deinit;
667 pjsua.codec_cnt++;
668 pjsua.codec_arg[pjsua.codec_cnt] = pj_str("pcma");
669 pjsua.codec_deinit[pjsua.codec_cnt] = &pjmedia_codec_g711_deinit;
670 pjsua.codec_cnt++;
Benny Prijono4381efe2006-03-16 14:24:26 +0000671#endif /* PJMEDIA_HAS_G711_CODEC */
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000672
673 } else {
674
675 /* If user specifies the exact codec to be used, then create only
676 * those codecs.
677 */
678 int i;
679
680 for (i=0; i<pjsua.codec_cnt; ++i) {
681
Benny Prijono4381efe2006-03-16 14:24:26 +0000682 if (0) {
683 /* Dummy */
684 }
685#if PJMEDIA_HAS_SPEEX_CODEC
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000686 /* Is it speex? */
Benny Prijono4381efe2006-03-16 14:24:26 +0000687 else if (!pj_stricmp2(&pjsua.codec_arg[i], "speex")) {
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000688
689 unsigned option = PJMEDIA_SPEEX_NO_WB | PJMEDIA_SPEEX_NO_UWB;
690
691 /* Register speex. */
692 if (pjsua.clock_rate >= 16000)
693 option &= ~(PJMEDIA_SPEEX_NO_WB);
694 if (pjsua.clock_rate >= 32000)
695 option &= ~(PJMEDIA_SPEEX_NO_UWB);
696
697 status = pjmedia_codec_speex_init(pjsua.med_endpt, option,
698 -1, -1);
699 if (status != PJ_SUCCESS) {
700 pjsua_perror(THIS_FILE, "Error initializing Speex codec",
701 status);
702 return status;
703 }
704
705 pjsua.codec_deinit[i] = &pjmedia_codec_speex_deinit;
706 }
Benny Prijono4381efe2006-03-16 14:24:26 +0000707#endif /* PJMEDIA_HAS_SPEEX_CODEC */
708
709#if PJMEDIA_HAS_GSM_CODEC
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000710 /* Is it gsm? */
711 else if (!pj_stricmp2(&pjsua.codec_arg[i], "gsm")) {
712
713 status = pjmedia_codec_gsm_init(pjsua.med_endpt);
714 if (status != PJ_SUCCESS) {
715 pjsua_perror(THIS_FILE, "Error initializing GSM codec",
716 status);
717 return status;
718 }
719
720 pjsua.codec_deinit[i] = &pjmedia_codec_gsm_deinit;
721
722 }
Benny Prijono4381efe2006-03-16 14:24:26 +0000723#endif /* PJMEDIA_HAS_GSM_CODEC */
724
725#if PJMEDIA_HAS_G711_CODEC
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000726 /* Is it pcma/pcmu? */
727 else if (!pj_stricmp2(&pjsua.codec_arg[i], "pcmu") ||
728 !pj_stricmp2(&pjsua.codec_arg[i], "pcma"))
729 {
730
731 status = pjmedia_codec_g711_init(pjsua.med_endpt);
732 if (status != PJ_SUCCESS) {
733 pjsua_perror(THIS_FILE, "Error initializing G711 codec",
734 status);
735 return status;
736 }
737
738 pjsua.codec_deinit[i] = &pjmedia_codec_g711_deinit;
739
740 }
Benny Prijono4381efe2006-03-16 14:24:26 +0000741#endif /* PJMEDIA_HAS_G711_CODEC */
742
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000743 /* Don't know about this codec... */
744 else {
745
746 PJ_LOG(1,(THIS_FILE, "Error: unsupported codecs %s",
747 pjsua.codec_arg[i].ptr));
748 return PJMEDIA_CODEC_EUNSUP;
749 }
750 }
751 }
Benny Prijonoccf95622006-02-07 18:48:01 +0000752
Benny Prijono8e3344c2006-03-08 12:37:22 +0000753 /* Init options for conference bridge. */
754 options = 0;
755 if (pjsua.no_mic)
756 options |= PJMEDIA_CONF_NO_MIC;
757
Benny Prijonof8baa872006-02-27 23:54:23 +0000758 /* Init conference bridge. */
759
760 status = pjmedia_conf_create(pjsua.pool,
761 pjsua.max_calls+PJSUA_CONF_MORE_PORTS,
Benny Prijono08e0d062006-03-04 14:52:44 +0000762 pjsua.clock_rate,
763 pjsua.clock_rate * 20 / 1000, 16,
Benny Prijono8e3344c2006-03-08 12:37:22 +0000764 options,
Benny Prijono08e0d062006-03-04 14:52:44 +0000765 &pjsua.mconf);
Benny Prijonof8baa872006-02-27 23:54:23 +0000766 if (status != PJ_SUCCESS) {
767 pj_caching_pool_destroy(&pjsua.cp);
768 pjsua_perror(THIS_FILE,
769 "Media stack initialization has returned error",
770 status);
771 return status;
772 }
773
Benny Prijonode380582006-03-15 19:32:41 +0000774 /* Add NULL port to the bridge. */
775 status = pjmedia_null_port_create( pjsua.pool, pjsua.clock_rate,
776 pjsua.clock_rate * 20 / 1000, 16,
777 &pjsua.null_port);
778 pjmedia_conf_add_port( pjsua.mconf, pjsua.pool, pjsua.null_port,
779 &pjsua.null_port->info.name, NULL );
780
Benny Prijono39879152006-02-23 02:09:10 +0000781 /* Create WAV file player if required: */
782
783 if (pjsua.wav_file) {
Benny Prijono39879152006-02-23 02:09:10 +0000784 pj_str_t port_name;
785
786 /* Create the file player port. */
787 status = pjmedia_file_player_port_create( pjsua.pool, pjsua.wav_file,
Benny Prijonode380582006-03-15 19:32:41 +0000788 0, -1, NULL,
789 &pjsua.file_port);
Benny Prijono39879152006-02-23 02:09:10 +0000790 if (status != PJ_SUCCESS) {
791 pjsua_perror(THIS_FILE,
792 "Error playing media file",
793 status);
794 return status;
795 }
796
797 /* Add port to conference bridge: */
Benny Prijonode380582006-03-15 19:32:41 +0000798 status = pjmedia_conf_add_port(pjsua.mconf, pjsua.pool,
799 pjsua.file_port,
Benny Prijono39879152006-02-23 02:09:10 +0000800 pj_cstr(&port_name, pjsua.wav_file),
801 &pjsua.wav_slot);
802 if (status != PJ_SUCCESS) {
803 pjsua_perror(THIS_FILE,
804 "Unable to add file player to conference bridge",
805 status);
806 return status;
807 }
808 }
809
810
Benny Prijonoeb30bf52006-03-04 20:43:52 +0000811 return PJ_SUCCESS;
812}
813
814
815/*
816 * Start pjsua stack.
817 * This will start the registration process, if registration is configured.
818 */
819pj_status_t pjsua_start(void)
820{
821 int i; /* Must be signed */
822 pjsip_transport *udp_transport;
823 pj_status_t status = PJ_SUCCESS;
824
825 /*
826 * Init media subsystem (codecs, conference bridge, et all).
827 */
828 status = init_media();
829 if (status != PJ_SUCCESS)
830 return status;
831
Benny Prijono268ca612006-02-07 12:34:11 +0000832 /* Init sockets (STUN etc): */
Benny Prijonoa91a0032006-02-26 21:23:45 +0000833 for (i=0; i<(int)pjsua.max_calls; ++i) {
834 status = init_sockets(i==0, &pjsua.calls[i].skinfo);
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000835 if (status != PJ_SUCCESS) {
836 pjsua_perror(THIS_FILE, "init_sockets() has returned error",
837 status);
Benny Prijonoa91a0032006-02-26 21:23:45 +0000838 --i;
839 if (i >= 0)
840 pj_sock_close(pjsua.sip_sock);
841 while (i >= 0) {
842 pj_sock_close(pjsua.calls[i].skinfo.rtp_sock);
843 pj_sock_close(pjsua.calls[i].skinfo.rtcp_sock);
844 }
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000845 return status;
846 }
Benny Prijono268ca612006-02-07 12:34:11 +0000847 }
848
Benny Prijonoccf95622006-02-07 18:48:01 +0000849 /* Add UDP transport: */
Benny Prijono268ca612006-02-07 12:34:11 +0000850
Benny Prijonoccf95622006-02-07 18:48:01 +0000851 {
852 /* Init the published name for the transport.
853 * Depending whether STUN is used, this may be the STUN mapped
854 * address, or socket's bound address.
855 */
856 pjsip_host_port addr_name;
857
858 addr_name.host.ptr = pj_inet_ntoa(pjsua.sip_sock_name.sin_addr);
Benny Prijonof3195072006-02-14 21:15:30 +0000859 addr_name.host.slen = pj_ansi_strlen(addr_name.host.ptr);
Benny Prijonoccf95622006-02-07 18:48:01 +0000860 addr_name.port = pj_ntohs(pjsua.sip_sock_name.sin_port);
861
862 /* Create UDP transport from previously created UDP socket: */
863
864 status = pjsip_udp_transport_attach( pjsua.endpt, pjsua.sip_sock,
865 &addr_name, 1,
866 &udp_transport);
867 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000868 pjsua_perror(THIS_FILE, "Unable to start UDP transport",
869 status);
Benny Prijonoccf95622006-02-07 18:48:01 +0000870 return status;
871 }
Benny Prijono268ca612006-02-07 12:34:11 +0000872 }
873
Benny Prijonoccf95622006-02-07 18:48:01 +0000874 /* Initialize Contact URI, if one is not specified: */
Benny Prijonoa91a0032006-02-26 21:23:45 +0000875 for (i=0; i<pjsua.acc_cnt; ++i) {
Benny Prijonoccf95622006-02-07 18:48:01 +0000876
877 pjsip_uri *uri;
878 pjsip_sip_uri *sip_uri;
Benny Prijonoccf95622006-02-07 18:48:01 +0000879
Benny Prijonoccf95622006-02-07 18:48:01 +0000880 /* Need to parse local_uri to get the elements: */
881
Benny Prijonoa91a0032006-02-26 21:23:45 +0000882 uri = pjsip_parse_uri(pjsua.pool, pjsua.acc[i].local_uri.ptr,
883 pjsua.acc[i].local_uri.slen, 0);
Benny Prijonoccf95622006-02-07 18:48:01 +0000884 if (uri == NULL) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000885 pjsua_perror(THIS_FILE, "Invalid local URI",
886 PJSIP_EINVALIDURI);
Benny Prijonoccf95622006-02-07 18:48:01 +0000887 return PJSIP_EINVALIDURI;
888 }
889
Benny Prijonoccf95622006-02-07 18:48:01 +0000890 /* Local URI MUST be a SIP or SIPS: */
891
Benny Prijonoa91a0032006-02-26 21:23:45 +0000892 if (!PJSIP_URI_SCHEME_IS_SIP(uri) &&
893 !PJSIP_URI_SCHEME_IS_SIPS(uri))
894 {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000895 pjsua_perror(THIS_FILE, "Invalid local URI",
896 PJSIP_EINVALIDSCHEME);
Benny Prijonoccf95622006-02-07 18:48:01 +0000897 return PJSIP_EINVALIDSCHEME;
898 }
899
900
901 /* Get the SIP URI object: */
902
903 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
904
Benny Prijonoa91a0032006-02-26 21:23:45 +0000905 pjsua.acc[i].user_part = sip_uri->user;
906 pjsua.acc[i].host_part = sip_uri->host;
Benny Prijonoccf95622006-02-07 18:48:01 +0000907
Benny Prijonoa91a0032006-02-26 21:23:45 +0000908 if (pjsua.acc[i].contact_uri.slen == 0 &&
909 pjsua.acc[i].local_uri.slen)
910 {
911 char contact[128];
912 int len;
Benny Prijonoccf95622006-02-07 18:48:01 +0000913
Benny Prijonoa91a0032006-02-26 21:23:45 +0000914 /* The local Contact is the username@ip-addr, where
915 * - username is taken from the local URI,
916 * - ip-addr in UDP transport's address name (which may have been
917 * resolved from STUN.
918 */
919
920 /* Build temporary contact string. */
Benny Prijonoccf95622006-02-07 18:48:01 +0000921
Benny Prijonoa91a0032006-02-26 21:23:45 +0000922 if (sip_uri->user.slen) {
Benny Prijonoccf95622006-02-07 18:48:01 +0000923
Benny Prijonoa91a0032006-02-26 21:23:45 +0000924 /* With the user part. */
Benny Prijonoed811d72006-03-10 12:57:12 +0000925 len = pj_ansi_snprintf(contact, sizeof(contact),
Benny Prijonoa91a0032006-02-26 21:23:45 +0000926 "<sip:%.*s@%.*s:%d>",
927 (int)sip_uri->user.slen,
928 sip_uri->user.ptr,
929 (int)udp_transport->local_name.host.slen,
930 udp_transport->local_name.host.ptr,
931 udp_transport->local_name.port);
932 } else {
933
934 /* Without user part */
935
Benny Prijonoed811d72006-03-10 12:57:12 +0000936 len = pj_ansi_snprintf(contact, sizeof(contact),
Benny Prijonoa91a0032006-02-26 21:23:45 +0000937 "<sip:%.*s:%d>",
938 (int)udp_transport->local_name.host.slen,
939 udp_transport->local_name.host.ptr,
940 udp_transport->local_name.port);
941 }
942
943 if (len < 1 || len >= sizeof(contact)) {
944 pjsua_perror(THIS_FILE, "Invalid Contact", PJSIP_EURITOOLONG);
945 return PJSIP_EURITOOLONG;
946 }
947
948 /* Duplicate Contact uri. */
949
950 pj_strdup2(pjsua.pool, &pjsua.acc[i].contact_uri, contact);
951
Benny Prijonoccf95622006-02-07 18:48:01 +0000952 }
Benny Prijonoccf95622006-02-07 18:48:01 +0000953 }
954
Benny Prijono95196582006-02-09 00:13:40 +0000955 /* If outbound_proxy is specified, put it in the route_set: */
Benny Prijonoccf95622006-02-07 18:48:01 +0000956
Benny Prijono95196582006-02-09 00:13:40 +0000957 if (pjsua.outbound_proxy.slen) {
958
959 pjsip_route_hdr *route;
960 const pj_str_t hname = { "Route", 5 };
961 int parsed_len;
962
963 route = pjsip_parse_hdr( pjsua.pool, &hname,
964 pjsua.outbound_proxy.ptr,
965 pjsua.outbound_proxy.slen,
966 &parsed_len);
967 if (route == NULL) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000968 pjsua_perror(THIS_FILE, "Invalid outbound proxy URL",
969 PJSIP_EINVALIDURI);
Benny Prijono95196582006-02-09 00:13:40 +0000970 return PJSIP_EINVALIDURI;
971 }
972
Benny Prijonoa91a0032006-02-26 21:23:45 +0000973 for (i=0; i<pjsua.acc_cnt; ++i) {
974 pj_list_push_front(&pjsua.acc[i].route_set, route);
975 }
Benny Prijono95196582006-02-09 00:13:40 +0000976 }
Benny Prijonoccf95622006-02-07 18:48:01 +0000977
978
Benny Prijono268ca612006-02-07 12:34:11 +0000979 /* Create worker thread(s), if required: */
980
981 for (i=0; i<pjsua.thread_cnt; ++i) {
Benny Prijono834aee32006-02-19 01:38:06 +0000982 status = pj_thread_create( pjsua.pool, "pjsua", &pjsua_poll,
Benny Prijono268ca612006-02-07 12:34:11 +0000983 NULL, 0, 0, &pjsua.threads[i]);
984 if (status != PJ_SUCCESS) {
985 pjsua.quit_flag = 1;
986 for (--i; i>=0; --i) {
987 pj_thread_join(pjsua.threads[i]);
988 pj_thread_destroy(pjsua.threads[i]);
989 }
Benny Prijono268ca612006-02-07 12:34:11 +0000990 return status;
991 }
992 }
993
Benny Prijonoccf95622006-02-07 18:48:01 +0000994 /* Start registration: */
995
996 /* Create client registration session: */
Benny Prijonoa91a0032006-02-26 21:23:45 +0000997 for (i=0; i<pjsua.acc_cnt; ++i) {
998 status = pjsua_regc_init(i);
999 if (status != PJ_SUCCESS)
1000 return status;
Benny Prijonoccf95622006-02-07 18:48:01 +00001001
Benny Prijonoa91a0032006-02-26 21:23:45 +00001002 /* Perform registration, if required. */
1003 if (pjsua.acc[i].regc) {
1004 pjsua_regc_update(i, 1);
1005 }
1006 }
Benny Prijonoccf95622006-02-07 18:48:01 +00001007
Benny Prijonoa91a0032006-02-26 21:23:45 +00001008
1009 /* Find account for outgoing preence subscription */
1010 for (i=0; i<pjsua.buddy_cnt; ++i) {
1011 pjsua.buddies[i].acc_index =
1012 pjsua_find_account_for_outgoing(&pjsua.buddies[i].uri);
Benny Prijonoccf95622006-02-07 18:48:01 +00001013 }
1014
1015
Benny Prijono834aee32006-02-19 01:38:06 +00001016 PJ_LOG(3,(THIS_FILE, "PJSUA version %s started", PJ_VERSION));
Benny Prijono268ca612006-02-07 12:34:11 +00001017 return PJ_SUCCESS;
1018}
1019
1020
Benny Prijono834aee32006-02-19 01:38:06 +00001021/* Sleep with polling */
1022static void busy_sleep(unsigned msec)
1023{
1024 pj_time_val timeout, now;
1025
1026 pj_gettimeofday(&timeout);
1027 timeout.msec += msec;
1028 pj_time_val_normalize(&timeout);
1029
1030 do {
1031 pjsua_poll(NULL);
1032 pj_gettimeofday(&now);
1033 } while (PJ_TIME_VAL_LT(now, timeout));
1034}
1035
Benny Prijono268ca612006-02-07 12:34:11 +00001036/*
1037 * Destroy pjsua.
1038 */
1039pj_status_t pjsua_destroy(void)
1040{
Benny Prijonoeb30bf52006-03-04 20:43:52 +00001041 int i; /* Must be signed */
Benny Prijono268ca612006-02-07 12:34:11 +00001042
1043 /* Signal threads to quit: */
Benny Prijono268ca612006-02-07 12:34:11 +00001044 pjsua.quit_flag = 1;
1045
Benny Prijonoa91a0032006-02-26 21:23:45 +00001046 /* Terminate all calls. */
Benny Prijono1a174142006-03-01 20:46:13 +00001047 pjsua_call_hangup_all();
Benny Prijonoa91a0032006-02-26 21:23:45 +00001048
1049 /* Terminate all presence subscriptions. */
1050 pjsua_pres_shutdown();
1051
1052 /* Unregister, if required: */
1053 for (i=0; i<pjsua.acc_cnt; ++i) {
1054 if (pjsua.acc[i].regc) {
1055 pjsua_regc_update(i, 0);
1056 }
1057 }
1058
Benny Prijono268ca612006-02-07 12:34:11 +00001059 /* Wait worker threads to quit: */
Benny Prijono268ca612006-02-07 12:34:11 +00001060 for (i=0; i<pjsua.thread_cnt; ++i) {
1061
Benny Prijonoe4f2abb2006-02-10 14:04:05 +00001062 if (pjsua.threads[i]) {
1063 pj_thread_join(pjsua.threads[i]);
1064 pj_thread_destroy(pjsua.threads[i]);
1065 pjsua.threads[i] = NULL;
1066 }
Benny Prijono268ca612006-02-07 12:34:11 +00001067 }
1068
Benny Prijono834aee32006-02-19 01:38:06 +00001069
Benny Prijono834aee32006-02-19 01:38:06 +00001070 /* Wait for some time to allow unregistration to complete: */
1071 PJ_LOG(4,(THIS_FILE, "Shutting down..."));
1072 busy_sleep(1000);
1073
Benny Prijono26ff9062006-02-21 23:47:00 +00001074 /* Destroy conference bridge. */
1075 if (pjsua.mconf)
1076 pjmedia_conf_destroy(pjsua.mconf);
1077
Benny Prijonode380582006-03-15 19:32:41 +00001078 /* Destroy file port */
Benny Prijono4381efe2006-03-16 14:24:26 +00001079 if (pjsua.file_port)
1080 pjmedia_port_destroy(pjsua.file_port);
Benny Prijonode380582006-03-15 19:32:41 +00001081
1082 /* Destroy null port. */
Benny Prijono4381efe2006-03-16 14:24:26 +00001083 if (pjsua.null_port)
1084 pjmedia_port_destroy(pjsua.null_port);
Benny Prijonode380582006-03-15 19:32:41 +00001085
1086
Benny Prijono834aee32006-02-19 01:38:06 +00001087 /* Destroy sound framework:
1088 * (this should be done in pjmedia_shutdown())
1089 */
1090 pj_snd_deinit();
1091
Benny Prijonoeb30bf52006-03-04 20:43:52 +00001092 /* Shutdown all codecs: */
1093 for (i = pjsua.codec_cnt-1; i >= 0; --i) {
1094 (*pjsua.codec_deinit[i])();
1095 }
1096
1097 /* Destroy media endpoint. */
1098
1099 pjmedia_endpt_destroy(pjsua.med_endpt);
1100
Benny Prijono268ca612006-02-07 12:34:11 +00001101 /* Destroy endpoint. */
Benny Prijono84126ab2006-02-09 09:30:09 +00001102
Benny Prijono268ca612006-02-07 12:34:11 +00001103 pjsip_endpt_destroy(pjsua.endpt);
1104 pjsua.endpt = NULL;
1105
1106 /* Destroy caching pool. */
Benny Prijono84126ab2006-02-09 09:30:09 +00001107
Benny Prijono268ca612006-02-07 12:34:11 +00001108 pj_caching_pool_destroy(&pjsua.cp);
1109
1110
1111 /* Done. */
1112
1113 return PJ_SUCCESS;
1114}
1115