blob: 77b6631abae80e40a99b09b3d1663eb18c655440 [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 */
19#include "pjsua.h"
20
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{
48
49 /* Normally need another thread for console application, because main
50 * thread will be blocked in fgets().
51 */
52 pjsua.thread_cnt = 1;
53
54
55 /* Default transport settings: */
56
57 pjsua.sip_port = 5060;
58
59
60 /* Default logging settings: */
61
62 pjsua.log_level = 5;
63 pjsua.app_log_level = 4;
64 pjsua.log_decor = PJ_LOG_HAS_SENDER | PJ_LOG_HAS_TIME |
65 PJ_LOG_HAS_MICRO_SEC | PJ_LOG_HAS_NEWLINE;
66
67 /* Default: do not use STUN: */
68
69 pjsua.stun_port1 = pjsua.stun_port2 = 0;
Benny Prijonoccf95622006-02-07 18:48:01 +000070
71 /* Default URIs: */
72
73 pjsua.local_uri = pj_str(PJSUA_LOCAL_URI);
Benny Prijono95196582006-02-09 00:13:40 +000074
Benny Prijonobcaed6c2006-02-19 15:37:19 +000075 /* Default registration timeout: */
76
77 pjsua.reg_timeout = 55;
78
Benny Prijono95196582006-02-09 00:13:40 +000079 /* Init route set list: */
80
81 pj_list_init(&pjsua.route_set);
Benny Prijono632ce712006-02-09 14:01:40 +000082
83 /* Init invite session list: */
84
85 pj_list_init(&pjsua.inv_list);
Benny Prijono834aee32006-02-19 01:38:06 +000086
87 /* Init server presence subscription list: */
88
89 pj_list_init(&pjsua.pres_srv_list);
90
Benny Prijono268ca612006-02-07 12:34:11 +000091}
92
93
Benny Prijono268ca612006-02-07 12:34:11 +000094
95/*
96 * Handler for receiving incoming requests.
97 *
98 * This handler serves multiple purposes:
99 * - it receives requests outside dialogs.
100 * - it receives requests inside dialogs, when the requests are
101 * unhandled by other dialog usages. Example of these
102 * requests are: MESSAGE.
103 */
104static pj_bool_t mod_pjsua_on_rx_request(pjsip_rx_data *rdata)
105{
Benny Prijono38998232006-02-08 22:44:25 +0000106
Benny Prijono84126ab2006-02-09 09:30:09 +0000107 if (rdata->msg_info.msg->line.req.method.id == PJSIP_INVITE_METHOD) {
Benny Prijono38998232006-02-08 22:44:25 +0000108
Benny Prijono84126ab2006-02-09 09:30:09 +0000109 return pjsua_inv_on_incoming(rdata);
Benny Prijono38998232006-02-08 22:44:25 +0000110
Benny Prijono38998232006-02-08 22:44:25 +0000111 }
112
Benny Prijono268ca612006-02-07 12:34:11 +0000113 return PJ_FALSE;
114}
115
116
117/*
118 * Handler for receiving incoming responses.
119 *
120 * This handler serves multiple purposes:
121 * - it receives strayed responses (i.e. outside any dialog and
122 * outside any transactions).
123 * - it receives responses coming to a transaction, when pjsua
124 * module is set as transaction user for the transaction.
125 * - it receives responses inside a dialog, when these responses
126 * are unhandled by other dialog usages.
127 */
128static pj_bool_t mod_pjsua_on_rx_response(pjsip_rx_data *rdata)
129{
130 PJ_UNUSED_ARG(rdata);
Benny Prijono268ca612006-02-07 12:34:11 +0000131 return PJ_FALSE;
132}
133
134
Benny Prijono268ca612006-02-07 12:34:11 +0000135/*
136 * Initialize sockets and optionally get the public address via STUN.
137 */
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000138static pj_status_t init_sockets(pj_bool_t sip,
139 pjmedia_sock_info *skinfo)
Benny Prijono268ca612006-02-07 12:34:11 +0000140{
141 enum {
142 RTP_START_PORT = 4000,
143 RTP_RANDOM_START = 2,
Benny Prijono26ff9062006-02-21 23:47:00 +0000144 RTP_RETRY = 20
Benny Prijono268ca612006-02-07 12:34:11 +0000145 };
146 enum {
147 SIP_SOCK,
148 RTP_SOCK,
149 RTCP_SOCK,
150 };
151 int i;
Benny Prijonofccab712006-02-22 22:23:22 +0000152 static pj_uint16_t rtp_port = RTP_START_PORT;
Benny Prijono268ca612006-02-07 12:34:11 +0000153 pj_sock_t sock[3];
154 pj_sockaddr_in mapped_addr[3];
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000155 pj_status_t status = PJ_SUCCESS;
Benny Prijono268ca612006-02-07 12:34:11 +0000156
157 for (i=0; i<3; ++i)
158 sock[i] = PJ_INVALID_SOCKET;
159
Benny Prijonofccab712006-02-22 22:23:22 +0000160 /* Create and bind SIP UDP socket. */
161 status = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, &sock[SIP_SOCK]);
162 if (status != PJ_SUCCESS) {
163 pjsua_perror(THIS_FILE, "socket() error", status);
164 goto on_error;
165 }
166
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000167 if (sip) {
Benny Prijonofccab712006-02-22 22:23:22 +0000168 status = pj_sock_bind_in(sock[SIP_SOCK], 0, pjsua.sip_port);
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000169 if (status != PJ_SUCCESS) {
Benny Prijonofccab712006-02-22 22:23:22 +0000170 pjsua_perror(THIS_FILE, "bind() error", status);
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000171 goto on_error;
172 }
Benny Prijonofccab712006-02-22 22:23:22 +0000173 } else {
174 status = pj_sock_bind_in(sock[SIP_SOCK], 0, 0);
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000175 if (status != PJ_SUCCESS) {
176 pjsua_perror(THIS_FILE, "bind() error", status);
177 goto on_error;
178 }
Benny Prijono268ca612006-02-07 12:34:11 +0000179 }
180
Benny Prijono268ca612006-02-07 12:34:11 +0000181
182 /* Loop retry to bind RTP and RTCP sockets. */
183 for (i=0; i<RTP_RETRY; ++i, rtp_port += 2) {
184
185 /* Create and bind RTP socket. */
186 status = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, &sock[RTP_SOCK]);
187 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000188 pjsua_perror(THIS_FILE, "socket() error", status);
Benny Prijono268ca612006-02-07 12:34:11 +0000189 goto on_error;
190 }
191
192 status = pj_sock_bind_in(sock[RTP_SOCK], 0, rtp_port);
193 if (status != PJ_SUCCESS) {
194 pj_sock_close(sock[RTP_SOCK]);
195 sock[RTP_SOCK] = PJ_INVALID_SOCKET;
196 continue;
197 }
198
199 /* Create and bind RTCP socket. */
200 status = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, &sock[RTCP_SOCK]);
201 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000202 pjsua_perror(THIS_FILE, "socket() error", status);
Benny Prijono268ca612006-02-07 12:34:11 +0000203 goto on_error;
204 }
205
206 status = pj_sock_bind_in(sock[RTCP_SOCK], 0, (pj_uint16_t)(rtp_port+1));
207 if (status != PJ_SUCCESS) {
208 pj_sock_close(sock[RTP_SOCK]);
209 sock[RTP_SOCK] = PJ_INVALID_SOCKET;
210
211 pj_sock_close(sock[RTCP_SOCK]);
212 sock[RTCP_SOCK] = PJ_INVALID_SOCKET;
213 continue;
214 }
215
216 /*
217 * If we're configured to use STUN, then find out the mapped address,
218 * and make sure that the mapped RTCP port is adjacent with the RTP.
219 */
220 if (pjsua.stun_port1 == 0) {
221 const pj_str_t *hostname;
222 pj_sockaddr_in addr;
223
224 /* Get local IP address. */
225 hostname = pj_gethostname();
226
227 pj_memset( &addr, 0, sizeof(addr));
228 addr.sin_family = PJ_AF_INET;
229 status = pj_sockaddr_in_set_str_addr( &addr, hostname);
230 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000231 pjsua_perror(THIS_FILE, "Unresolvable local hostname",
232 status);
Benny Prijono268ca612006-02-07 12:34:11 +0000233 goto on_error;
234 }
235
236 for (i=0; i<3; ++i)
237 pj_memcpy(&mapped_addr[i], &addr, sizeof(addr));
238
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000239 if (sip)
240 mapped_addr[SIP_SOCK].sin_port = pj_htons((pj_uint16_t)pjsua.sip_port);
Benny Prijono268ca612006-02-07 12:34:11 +0000241 mapped_addr[RTP_SOCK].sin_port = pj_htons((pj_uint16_t)rtp_port);
242 mapped_addr[RTCP_SOCK].sin_port = pj_htons((pj_uint16_t)(rtp_port+1));
243 break;
244 } else {
245 status = pj_stun_get_mapped_addr( &pjsua.cp.factory, 3, sock,
246 &pjsua.stun_srv1, pjsua.stun_port1,
247 &pjsua.stun_srv2, pjsua.stun_port2,
248 mapped_addr);
249 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000250 pjsua_perror(THIS_FILE, "STUN error", status);
Benny Prijono268ca612006-02-07 12:34:11 +0000251 goto on_error;
252 }
253
254 if (pj_ntohs(mapped_addr[2].sin_port) == pj_ntohs(mapped_addr[1].sin_port)+1)
255 break;
256
257 pj_sock_close(sock[RTP_SOCK]); sock[RTP_SOCK] = PJ_INVALID_SOCKET;
258 pj_sock_close(sock[RTCP_SOCK]); sock[RTCP_SOCK] = PJ_INVALID_SOCKET;
259 }
260 }
261
262 if (sock[RTP_SOCK] == PJ_INVALID_SOCKET) {
263 PJ_LOG(1,(THIS_FILE, "Unable to find appropriate RTP/RTCP ports combination"));
264 goto on_error;
265 }
266
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000267 if (sip) {
268 pjsua.sip_sock = sock[SIP_SOCK];
269 pj_memcpy(&pjsua.sip_sock_name, &mapped_addr[SIP_SOCK], sizeof(pj_sockaddr_in));
Benny Prijonofccab712006-02-22 22:23:22 +0000270 } else {
271 pj_sock_close(sock[0]);
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000272 }
Benny Prijono95196582006-02-09 00:13:40 +0000273
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000274 skinfo->rtp_sock = sock[RTP_SOCK];
275 pj_memcpy(&skinfo->rtp_addr_name,
Benny Prijono95196582006-02-09 00:13:40 +0000276 &mapped_addr[RTP_SOCK], sizeof(pj_sockaddr_in));
277
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000278 skinfo->rtcp_sock = sock[RTCP_SOCK];
279 pj_memcpy(&skinfo->rtcp_addr_name,
Benny Prijono95196582006-02-09 00:13:40 +0000280 &mapped_addr[RTCP_SOCK], sizeof(pj_sockaddr_in));
Benny Prijono268ca612006-02-07 12:34:11 +0000281
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000282 if (sip) {
283 PJ_LOG(4,(THIS_FILE, "SIP UDP socket reachable at %s:%d",
284 pj_inet_ntoa(pjsua.sip_sock_name.sin_addr),
285 pj_ntohs(pjsua.sip_sock_name.sin_port)));
286 }
Benny Prijono268ca612006-02-07 12:34:11 +0000287 PJ_LOG(4,(THIS_FILE, "RTP socket reachable at %s:%d",
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000288 pj_inet_ntoa(skinfo->rtp_addr_name.sin_addr),
289 pj_ntohs(skinfo->rtp_addr_name.sin_port)));
Benny Prijono268ca612006-02-07 12:34:11 +0000290 PJ_LOG(4,(THIS_FILE, "RTCP UDP socket reachable at %s:%d",
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000291 pj_inet_ntoa(skinfo->rtcp_addr_name.sin_addr),
292 pj_ntohs(skinfo->rtcp_addr_name.sin_port)));
Benny Prijono268ca612006-02-07 12:34:11 +0000293
Benny Prijonofccab712006-02-22 22:23:22 +0000294 rtp_port += 2;
Benny Prijono268ca612006-02-07 12:34:11 +0000295 return PJ_SUCCESS;
296
297on_error:
298 for (i=0; i<3; ++i) {
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000299 if (sip && i==0)
300 continue;
Benny Prijono268ca612006-02-07 12:34:11 +0000301 if (sock[i] != PJ_INVALID_SOCKET)
302 pj_sock_close(sock[i]);
303 }
304 return status;
305}
306
307
308
309/*
310 * Initialize stack.
311 */
312static pj_status_t init_stack(void)
313{
314 pj_status_t status;
315
316 /* Create global endpoint: */
317
318 {
319 const pj_str_t *hostname;
320 const char *endpt_name;
321
322 /* Endpoint MUST be assigned a globally unique name.
323 * The name will be used as the hostname in Warning header.
324 */
325
326 /* For this implementation, we'll use hostname for simplicity */
327 hostname = pj_gethostname();
328 endpt_name = hostname->ptr;
329
330 /* Create the endpoint: */
331
332 status = pjsip_endpt_create(&pjsua.cp.factory, endpt_name,
333 &pjsua.endpt);
334 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000335 pjsua_perror(THIS_FILE, "Unable to create SIP endpoint", status);
Benny Prijono268ca612006-02-07 12:34:11 +0000336 return status;
337 }
338 }
339
340
341 /* Initialize transaction layer: */
342
343 status = pjsip_tsx_layer_init(pjsua.endpt);
344 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000345 pjsua_perror(THIS_FILE, "Transaction layer initialization error",
346 status);
Benny Prijono268ca612006-02-07 12:34:11 +0000347 goto on_error;
348 }
349
350 /* Initialize UA layer module: */
351
352 status = pjsip_ua_init( pjsua.endpt, NULL );
353 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000354 pjsua_perror(THIS_FILE, "UA layer initialization error", status);
Benny Prijono268ca612006-02-07 12:34:11 +0000355 goto on_error;
356 }
357
358 /* Initialize and register pjsua's application module: */
359
360 {
361 pjsip_module my_mod =
362 {
363 NULL, NULL, /* prev, next. */
364 { "mod-pjsua", 9 }, /* Name. */
365 -1, /* Id */
366 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
367 NULL, /* User data. */
368 NULL, /* load() */
369 NULL, /* start() */
370 NULL, /* stop() */
371 NULL, /* unload() */
372 &mod_pjsua_on_rx_request, /* on_rx_request() */
373 &mod_pjsua_on_rx_response, /* on_rx_response() */
374 NULL, /* on_tx_request. */
375 NULL, /* on_tx_response() */
376 NULL, /* on_tsx_state() */
377 };
378
379 pjsua.mod = my_mod;
380
381 status = pjsip_endpt_register_module(pjsua.endpt, &pjsua.mod);
382 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000383 pjsua_perror(THIS_FILE, "Unable to register pjsua module",
384 status);
Benny Prijono268ca612006-02-07 12:34:11 +0000385 goto on_error;
386 }
387 }
388
389 /* Initialize invite session module: */
390
391 {
392
393 /* Initialize invite session callback. */
394 pjsip_inv_callback inv_cb;
395
396 pj_memset(&inv_cb, 0, sizeof(inv_cb));
397 inv_cb.on_state_changed = &pjsua_inv_on_state_changed;
398 inv_cb.on_new_session = &pjsua_inv_on_new_session;
Benny Prijono95196582006-02-09 00:13:40 +0000399 inv_cb.on_media_update = &pjsua_inv_on_media_update;
Benny Prijono26ff9062006-02-21 23:47:00 +0000400 inv_cb.on_rx_offer = &pjsua_inv_on_rx_offer;
401 inv_cb.on_tsx_state_changed = &pjsua_inv_on_tsx_state_changed;
402
Benny Prijono268ca612006-02-07 12:34:11 +0000403
404 /* Initialize invite session module: */
405 status = pjsip_inv_usage_init(pjsua.endpt, &pjsua.mod, &inv_cb);
406 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000407 pjsua_perror(THIS_FILE, "Invite usage initialization error",
408 status);
Benny Prijono268ca612006-02-07 12:34:11 +0000409 goto on_error;
410 }
411
412 }
413
414
Benny Prijonoccf95622006-02-07 18:48:01 +0000415 /* Done */
Benny Prijono268ca612006-02-07 12:34:11 +0000416
417 return PJ_SUCCESS;
418
419
420on_error:
421 pjsip_endpt_destroy(pjsua.endpt);
422 pjsua.endpt = NULL;
423 return status;
424}
425
426
Benny Prijono834aee32006-02-19 01:38:06 +0000427static int PJ_THREAD_FUNC pjsua_poll(void *arg)
Benny Prijono268ca612006-02-07 12:34:11 +0000428{
429 PJ_UNUSED_ARG(arg);
430
Benny Prijono834aee32006-02-19 01:38:06 +0000431 do {
Benny Prijono268ca612006-02-07 12:34:11 +0000432 pj_time_val timeout = { 0, 10 };
433 pjsip_endpt_handle_events (pjsua.endpt, &timeout);
Benny Prijono834aee32006-02-19 01:38:06 +0000434 } while (!pjsua.quit_flag);
Benny Prijono268ca612006-02-07 12:34:11 +0000435
436 return 0;
437}
438
439/*
440 * Initialize pjsua application.
Benny Prijonoccf95622006-02-07 18:48:01 +0000441 * This will initialize all libraries, create endpoint instance, and register
442 * pjsip modules.
Benny Prijono268ca612006-02-07 12:34:11 +0000443 */
444pj_status_t pjsua_init(void)
445{
Benny Prijono268ca612006-02-07 12:34:11 +0000446 pj_status_t status;
447
448 /* Init PJLIB logging: */
449
450 pj_log_set_level(pjsua.log_level);
451 pj_log_set_decor(pjsua.log_decor);
452
453
454 /* Init PJLIB: */
455
456 status = pj_init();
457 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000458 pjsua_perror(THIS_FILE, "pj_init() error", status);
Benny Prijono268ca612006-02-07 12:34:11 +0000459 return status;
460 }
461
Benny Prijonofccab712006-02-22 22:23:22 +0000462 /* Init PJLIB-UTIL: */
463
464 status = pjlib_util_init();
465 if (status != PJ_SUCCESS) {
466 pjsua_perror(THIS_FILE, "pjlib_util_init() error", status);
467 return status;
468 }
469
Benny Prijono268ca612006-02-07 12:34:11 +0000470 /* Init memory pool: */
471
472 /* Init caching pool. */
473 pj_caching_pool_init(&pjsua.cp, &pj_pool_factory_default_policy, 0);
474
475 /* Create memory pool for application. */
476 pjsua.pool = pj_pool_create(&pjsua.cp.factory, "pjsua", 4000, 4000, NULL);
477
478
Benny Prijono834aee32006-02-19 01:38:06 +0000479 /* Init PJSIP : */
Benny Prijonoccf95622006-02-07 18:48:01 +0000480
481 status = init_stack();
482 if (status != PJ_SUCCESS) {
483 pj_caching_pool_destroy(&pjsua.cp);
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000484 pjsua_perror(THIS_FILE, "Stack initialization has returned error",
485 status);
Benny Prijonoccf95622006-02-07 18:48:01 +0000486 return status;
487 }
488
Benny Prijono95196582006-02-09 00:13:40 +0000489
Benny Prijono834aee32006-02-19 01:38:06 +0000490 /* Init core SIMPLE module : */
491
492 pjsip_evsub_init_module(pjsua.endpt);
493
494 /* Init presence module: */
495
496 pjsip_pres_init_module( pjsua.endpt, pjsip_evsub_instance());
497
Benny Prijono26ff9062006-02-21 23:47:00 +0000498 /* Init xfer/REFER module */
499
500 pjsip_xfer_init_module( pjsua.endpt );
Benny Prijono834aee32006-02-19 01:38:06 +0000501
502 /* Init pjsua presence handler: */
503
504 pjsua_pres_init();
505
506
Benny Prijono95196582006-02-09 00:13:40 +0000507 /* Init media endpoint: */
508
509 status = pjmedia_endpt_create(&pjsua.cp.factory, &pjsua.med_endpt);
510 if (status != PJ_SUCCESS) {
511 pj_caching_pool_destroy(&pjsua.cp);
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000512 pjsua_perror(THIS_FILE,
513 "Media stack initialization has returned error",
514 status);
Benny Prijono95196582006-02-09 00:13:40 +0000515 return status;
516 }
517
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000518 /* Init conference bridge. */
519
520 status = pjmedia_conf_create(pjsua.pool, 8, 8000, 160, 16, &pjsua.mconf);
521 if (status != PJ_SUCCESS) {
522 pj_caching_pool_destroy(&pjsua.cp);
523 pjsua_perror(THIS_FILE,
524 "Media stack initialization has returned error",
525 status);
526 return status;
527 }
528
Benny Prijono1f9afba2006-02-10 15:57:32 +0000529 /* Init pjmedia-codecs: */
530
531 status = pjmedia_codec_init(pjsua.med_endpt);
532 if (status != PJ_SUCCESS) {
533 pj_caching_pool_destroy(&pjsua.cp);
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000534 pjsua_perror(THIS_FILE,
535 "Media codec initialization has returned error",
536 status);
Benny Prijono1f9afba2006-02-10 15:57:32 +0000537 return status;
538 }
539
540
Benny Prijonoccf95622006-02-07 18:48:01 +0000541 /* Done. */
542 return PJ_SUCCESS;
543}
544
545
546
547/*
548 * Start pjsua stack.
549 * This will start the registration process, if registration is configured.
550 */
551pj_status_t pjsua_start(void)
552{
553 int i; /* Must be signed */
554 pjsip_transport *udp_transport;
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000555 pj_status_t status = PJ_SUCCESS;
Benny Prijonoccf95622006-02-07 18:48:01 +0000556
Benny Prijono268ca612006-02-07 12:34:11 +0000557 /* Init sockets (STUN etc): */
Benny Prijonof04ffdd2006-02-21 00:11:18 +0000558 for (i=0; i<PJ_ARRAY_SIZE(pjsua.med_sock_info); ++i) {
559 status = init_sockets(i==0, &pjsua.med_sock_info[i]);
560 if (status != PJ_SUCCESS) {
561 pjsua_perror(THIS_FILE, "init_sockets() has returned error",
562 status);
563 return status;
564 }
Benny Prijono268ca612006-02-07 12:34:11 +0000565 }
566
Benny Prijonoccf95622006-02-07 18:48:01 +0000567 /* Add UDP transport: */
Benny Prijono268ca612006-02-07 12:34:11 +0000568
Benny Prijonoccf95622006-02-07 18:48:01 +0000569 {
570 /* Init the published name for the transport.
571 * Depending whether STUN is used, this may be the STUN mapped
572 * address, or socket's bound address.
573 */
574 pjsip_host_port addr_name;
575
576 addr_name.host.ptr = pj_inet_ntoa(pjsua.sip_sock_name.sin_addr);
Benny Prijonof3195072006-02-14 21:15:30 +0000577 addr_name.host.slen = pj_ansi_strlen(addr_name.host.ptr);
Benny Prijonoccf95622006-02-07 18:48:01 +0000578 addr_name.port = pj_ntohs(pjsua.sip_sock_name.sin_port);
579
580 /* Create UDP transport from previously created UDP socket: */
581
582 status = pjsip_udp_transport_attach( pjsua.endpt, pjsua.sip_sock,
583 &addr_name, 1,
584 &udp_transport);
585 if (status != PJ_SUCCESS) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000586 pjsua_perror(THIS_FILE, "Unable to start UDP transport",
587 status);
Benny Prijonoccf95622006-02-07 18:48:01 +0000588 return status;
589 }
Benny Prijono268ca612006-02-07 12:34:11 +0000590 }
591
Benny Prijonoccf95622006-02-07 18:48:01 +0000592 /* Initialize Contact URI, if one is not specified: */
593
594 if (pjsua.contact_uri.slen == 0 && pjsua.local_uri.slen) {
595
596 pjsip_uri *uri;
597 pjsip_sip_uri *sip_uri;
598 char contact[128];
599 int len;
600
601 /* The local Contact is the username@ip-addr, where
602 * - username is taken from the local URI,
603 * - ip-addr in UDP transport's address name (which may have been
604 * resolved from STUN.
605 */
606
607 /* Need to parse local_uri to get the elements: */
608
609 uri = pjsip_parse_uri(pjsua.pool, pjsua.local_uri.ptr,
610 pjsua.local_uri.slen, 0);
611 if (uri == NULL) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000612 pjsua_perror(THIS_FILE, "Invalid local URI",
613 PJSIP_EINVALIDURI);
Benny Prijonoccf95622006-02-07 18:48:01 +0000614 return PJSIP_EINVALIDURI;
615 }
616
617
618 /* Local URI MUST be a SIP or SIPS: */
619
620 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri)) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000621 pjsua_perror(THIS_FILE, "Invalid local URI",
622 PJSIP_EINVALIDSCHEME);
Benny Prijonoccf95622006-02-07 18:48:01 +0000623 return PJSIP_EINVALIDSCHEME;
624 }
625
626
627 /* Get the SIP URI object: */
628
629 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
630
631
632 /* Build temporary contact string. */
633
634 if (sip_uri->user.slen) {
635
636 /* With the user part. */
637 len = pj_snprintf(contact, sizeof(contact),
638 "<sip:%.*s@%.*s:%d>",
Benny Prijono34a404e2006-02-09 14:38:30 +0000639 (int)sip_uri->user.slen,
Benny Prijonoccf95622006-02-07 18:48:01 +0000640 sip_uri->user.ptr,
Benny Prijono34a404e2006-02-09 14:38:30 +0000641 (int)udp_transport->local_name.host.slen,
Benny Prijonoccf95622006-02-07 18:48:01 +0000642 udp_transport->local_name.host.ptr,
643 udp_transport->local_name.port);
644 } else {
645
646 /* Without user part */
647
648 len = pj_snprintf(contact, sizeof(contact),
649 "<sip:%.*s:%d>",
Benny Prijono34a404e2006-02-09 14:38:30 +0000650 (int)udp_transport->local_name.host.slen,
Benny Prijonoccf95622006-02-07 18:48:01 +0000651 udp_transport->local_name.host.ptr,
652 udp_transport->local_name.port);
653 }
654
655 if (len < 1 || len >= sizeof(contact)) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000656 pjsua_perror(THIS_FILE, "Invalid Contact", PJSIP_EURITOOLONG);
Benny Prijonoccf95622006-02-07 18:48:01 +0000657 return PJSIP_EURITOOLONG;
658 }
659
660 /* Duplicate Contact uri. */
661
662 pj_strdup2(pjsua.pool, &pjsua.contact_uri, contact);
663
664 }
665
Benny Prijono95196582006-02-09 00:13:40 +0000666 /* If outbound_proxy is specified, put it in the route_set: */
Benny Prijonoccf95622006-02-07 18:48:01 +0000667
Benny Prijono95196582006-02-09 00:13:40 +0000668 if (pjsua.outbound_proxy.slen) {
669
670 pjsip_route_hdr *route;
671 const pj_str_t hname = { "Route", 5 };
672 int parsed_len;
673
674 route = pjsip_parse_hdr( pjsua.pool, &hname,
675 pjsua.outbound_proxy.ptr,
676 pjsua.outbound_proxy.slen,
677 &parsed_len);
678 if (route == NULL) {
Benny Prijonobcaed6c2006-02-19 15:37:19 +0000679 pjsua_perror(THIS_FILE, "Invalid outbound proxy URL",
680 PJSIP_EINVALIDURI);
Benny Prijono95196582006-02-09 00:13:40 +0000681 return PJSIP_EINVALIDURI;
682 }
683
684 pj_list_push_back(&pjsua.route_set, route);
685 }
Benny Prijonoccf95622006-02-07 18:48:01 +0000686
687
Benny Prijono268ca612006-02-07 12:34:11 +0000688 /* Create worker thread(s), if required: */
689
690 for (i=0; i<pjsua.thread_cnt; ++i) {
Benny Prijono834aee32006-02-19 01:38:06 +0000691 status = pj_thread_create( pjsua.pool, "pjsua", &pjsua_poll,
Benny Prijono268ca612006-02-07 12:34:11 +0000692 NULL, 0, 0, &pjsua.threads[i]);
693 if (status != PJ_SUCCESS) {
694 pjsua.quit_flag = 1;
695 for (--i; i>=0; --i) {
696 pj_thread_join(pjsua.threads[i]);
697 pj_thread_destroy(pjsua.threads[i]);
698 }
Benny Prijono268ca612006-02-07 12:34:11 +0000699 return status;
700 }
701 }
702
Benny Prijonoccf95622006-02-07 18:48:01 +0000703 /* Start registration: */
704
705 /* Create client registration session: */
706
707 status = pjsua_regc_init();
708 if (status != PJ_SUCCESS)
709 return status;
710
711 /* Perform registration, if required. */
712 if (pjsua.regc) {
713 pjsua_regc_update(1);
714 }
715
716
Benny Prijono834aee32006-02-19 01:38:06 +0000717 PJ_LOG(3,(THIS_FILE, "PJSUA version %s started", PJ_VERSION));
Benny Prijono268ca612006-02-07 12:34:11 +0000718 return PJ_SUCCESS;
719}
720
721
Benny Prijono834aee32006-02-19 01:38:06 +0000722/* Sleep with polling */
723static void busy_sleep(unsigned msec)
724{
725 pj_time_val timeout, now;
726
727 pj_gettimeofday(&timeout);
728 timeout.msec += msec;
729 pj_time_val_normalize(&timeout);
730
731 do {
732 pjsua_poll(NULL);
733 pj_gettimeofday(&now);
734 } while (PJ_TIME_VAL_LT(now, timeout));
735}
736
Benny Prijono268ca612006-02-07 12:34:11 +0000737/*
738 * Destroy pjsua.
739 */
740pj_status_t pjsua_destroy(void)
741{
742 int i;
743
744 /* Signal threads to quit: */
Benny Prijono268ca612006-02-07 12:34:11 +0000745 pjsua.quit_flag = 1;
746
747 /* Wait worker threads to quit: */
Benny Prijono268ca612006-02-07 12:34:11 +0000748 for (i=0; i<pjsua.thread_cnt; ++i) {
749
Benny Prijonoe4f2abb2006-02-10 14:04:05 +0000750 if (pjsua.threads[i]) {
751 pj_thread_join(pjsua.threads[i]);
752 pj_thread_destroy(pjsua.threads[i]);
753 pjsua.threads[i] = NULL;
754 }
Benny Prijono268ca612006-02-07 12:34:11 +0000755 }
756
Benny Prijono834aee32006-02-19 01:38:06 +0000757
758 /* Terminate all calls. */
759 pjsua_inv_shutdown();
760
761 /* Terminate all presence subscriptions. */
762 pjsua_pres_shutdown();
763
764 /* Unregister, if required: */
765 if (pjsua.regc) {
766 pjsua_regc_update(0);
767 }
768
769 /* Wait for some time to allow unregistration to complete: */
770 PJ_LOG(4,(THIS_FILE, "Shutting down..."));
771 busy_sleep(1000);
772
Benny Prijono26ff9062006-02-21 23:47:00 +0000773 /* Destroy conference bridge. */
774 if (pjsua.mconf)
775 pjmedia_conf_destroy(pjsua.mconf);
776
Benny Prijono834aee32006-02-19 01:38:06 +0000777 /* Shutdown pjmedia-codec: */
778 pjmedia_codec_deinit();
779
780 /* Destroy sound framework:
781 * (this should be done in pjmedia_shutdown())
782 */
783 pj_snd_deinit();
784
Benny Prijono268ca612006-02-07 12:34:11 +0000785 /* Destroy endpoint. */
Benny Prijono84126ab2006-02-09 09:30:09 +0000786
Benny Prijono268ca612006-02-07 12:34:11 +0000787 pjsip_endpt_destroy(pjsua.endpt);
788 pjsua.endpt = NULL;
789
790 /* Destroy caching pool. */
Benny Prijono84126ab2006-02-09 09:30:09 +0000791
Benny Prijono268ca612006-02-07 12:34:11 +0000792 pj_caching_pool_destroy(&pjsua.cp);
793
794
795 /* Done. */
796
797 return PJ_SUCCESS;
798}
799