blob: 46f7fde7815956a4827277e41d4c0f225b8ca8e6 [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 Prijono268ca612006-02-07 12:34:11 +000021
22#define THIS_FILE "pjsua.c"
23
Benny Prijono6782e092006-02-07 20:56:50 +000024struct pjsua_t pjsua;
Benny Prijono268ca612006-02-07 12:34:11 +000025
Benny Prijonoccf95622006-02-07 18:48:01 +000026#define PJSUA_LOCAL_URI "<sip:user@127.0.0.1>"
Benny Prijono268ca612006-02-07 12:34:11 +000027
28static char *PJSUA_DUMMY_SDP_OFFER =
29 "v=0\r\n"
30 "o=offer 2890844526 2890844526 IN IP4 127.0.0.1\r\n"
31 "s= \r\n"
32 "c=IN IP4 127.0.0.1\r\n"
33 "t=0 0\r\n"
34 "m=audio 49170 RTP/AVP 0\r\n"
35 "a=rtpmap:0 PCMU/8000\r\n";
36
37static char *PJSUA_DUMMY_SDP_ANSWER =
38 "v=0\r\n"
39 "o=answer 2890844730 2890844730 IN IP4 127.0.0.1\r\n"
40 "s= \r\n"
41 "c=IN IP4 127.0.0.1\r\n"
42 "t=0 0\r\n"
43 "m=audio 49920 RTP/AVP 0\r\n"
44 "a=rtpmap:0 PCMU/8000\r\n";
45
46/*
47 * Init default application parameters.
48 */
49void pjsua_default(void)
50{
51
52 /* Normally need another thread for console application, because main
53 * thread will be blocked in fgets().
54 */
55 pjsua.thread_cnt = 1;
56
57
58 /* Default transport settings: */
59
60 pjsua.sip_port = 5060;
61
62
63 /* Default logging settings: */
64
65 pjsua.log_level = 5;
66 pjsua.app_log_level = 4;
67 pjsua.log_decor = PJ_LOG_HAS_SENDER | PJ_LOG_HAS_TIME |
68 PJ_LOG_HAS_MICRO_SEC | PJ_LOG_HAS_NEWLINE;
69
70 /* Default: do not use STUN: */
71
72 pjsua.stun_port1 = pjsua.stun_port2 = 0;
Benny Prijonoccf95622006-02-07 18:48:01 +000073
74 /* Default URIs: */
75
76 pjsua.local_uri = pj_str(PJSUA_LOCAL_URI);
Benny Prijono268ca612006-02-07 12:34:11 +000077}
78
79
80/*
81 * Display error message for the specified error code.
82 */
83void pjsua_perror(const char *title, pj_status_t status)
84{
85 char errmsg[PJ_ERR_MSG_SIZE];
86
87 pj_strerror(status, errmsg, sizeof(errmsg));
88
89 PJ_LOG(1,(THIS_FILE, "%s: %s", title, errmsg));
90}
91
92
93/*
94 * Handler for receiving incoming requests.
95 *
96 * This handler serves multiple purposes:
97 * - it receives requests outside dialogs.
98 * - it receives requests inside dialogs, when the requests are
99 * unhandled by other dialog usages. Example of these
100 * requests are: MESSAGE.
101 */
102static pj_bool_t mod_pjsua_on_rx_request(pjsip_rx_data *rdata)
103{
Benny Prijono38998232006-02-08 22:44:25 +0000104 pjsip_dialog *dlg = pjsip_rdata_get_dlg(rdata);
105 pjsip_transaction *tsx = pjsip_rdata_get_tsx(rdata);
106 pjsip_msg *msg = rdata->msg_info.msg;
107
108 /*
109 * Handle incoming INVITE outside dialog.
110 */
111 if (dlg == NULL && tsx == NULL &&
112 msg->line.req.method.id == PJSIP_INVITE_METHOD)
113 {
114 pj_status_t status;
115 pjsip_tx_data *response = NULL;
116 unsigned options = 0;
117
118 /* Verify that we can handle the request. */
119 status = pjsip_inv_verify_request(rdata, &options, NULL, NULL,
120 pjsua.endpt, &response);
121 if (status != PJ_SUCCESS) {
122
123 /*
124 * No we can't handle the incoming INVITE request.
125 */
126
127 if (response) {
128 pjsip_response_addr res_addr;
129
130 pjsip_get_response_addr(response->pool, rdata, &res_addr);
131 pjsip_endpt_send_response(pjsua.endpt, &res_addr, response,
132 NULL, NULL);
133
134 } else {
135
136 /* Respond with 500 (Internal Server Error) */
137 pjsip_endpt_respond_stateless(pjsua.endpt, rdata, 500, NULL,
138 NULL, NULL);
139 }
140
141 } else {
142 /*
143 * Yes we can handle the incoming INVITE request.
144 */
145 pjsip_inv_session *inv;
146 pjmedia_sdp_session *answer;
147
148 /* Create dummy SDP answer: */
149
150
151 status = pjmedia_sdp_parse(pjsua.pool, PJSUA_DUMMY_SDP_ANSWER,
152 pj_native_strlen(PJSUA_DUMMY_SDP_ANSWER),
153 &answer);
154 if (status != PJ_SUCCESS) {
155
156 pjsip_endpt_respond_stateless(pjsua.endpt, rdata, 500, NULL,
157 NULL, NULL);
158 return PJ_TRUE;
159 }
160
161 /* Create dialog: */
162
163 status = pjsip_dlg_create_uas( pjsip_ua_instance(), rdata,
164 &pjsua.contact_uri, &dlg);
165 if (status != PJ_SUCCESS)
166 return PJ_TRUE;
167
168
169 /* Create invite session: */
170
171 status = pjsip_inv_create_uas( dlg, rdata, answer, 0, &inv);
172 if (status != PJ_SUCCESS) {
173
174 status = pjsip_dlg_create_response( dlg, rdata, 500, NULL,
175 &response);
176 if (status == PJ_SUCCESS)
177 status = pjsip_dlg_send_response(dlg,
178 pjsip_rdata_get_tsx(rdata),
179 response);
180 return PJ_TRUE;
181
182 }
183
184 /* Answer with 100 (using the dialog, not invite): */
185
186 status = pjsip_dlg_create_response(dlg, rdata, 100, NULL, &response);
187 if (status == PJ_SUCCESS)
188 status = pjsip_dlg_send_response(dlg, pjsip_rdata_get_tsx(rdata), response);
189 }
190
191 /* This INVITE request has been handled. */
192 return PJ_TRUE;
193 }
194
195
196
Benny Prijono268ca612006-02-07 12:34:11 +0000197 return PJ_FALSE;
198}
199
200
201/*
202 * Handler for receiving incoming responses.
203 *
204 * This handler serves multiple purposes:
205 * - it receives strayed responses (i.e. outside any dialog and
206 * outside any transactions).
207 * - it receives responses coming to a transaction, when pjsua
208 * module is set as transaction user for the transaction.
209 * - it receives responses inside a dialog, when these responses
210 * are unhandled by other dialog usages.
211 */
212static pj_bool_t mod_pjsua_on_rx_response(pjsip_rx_data *rdata)
213{
214 PJ_UNUSED_ARG(rdata);
Benny Prijono268ca612006-02-07 12:34:11 +0000215 return PJ_FALSE;
216}
217
218
219/*
220 * This callback receives notification from invite session when the
221 * session state has changed.
222 */
223static void pjsua_inv_on_state_changed(pjsip_inv_session *inv, pjsip_event *e)
224{
Benny Prijonoccf95622006-02-07 18:48:01 +0000225 pjsua_ui_inv_on_state_changed(inv, e);
Benny Prijono268ca612006-02-07 12:34:11 +0000226}
227
228
229/*
230 * This callback is called by invite session framework when UAC session
231 * has forked.
232 */
233static void pjsua_inv_on_new_session(pjsip_inv_session *inv, pjsip_event *e)
234{
235 PJ_UNUSED_ARG(inv);
236 PJ_UNUSED_ARG(e);
237
238 PJ_TODO(HANDLE_FORKED_DIALOG);
239}
240
241/*
242 * Initialize sockets and optionally get the public address via STUN.
243 */
244static pj_status_t init_sockets()
245{
246 enum {
247 RTP_START_PORT = 4000,
248 RTP_RANDOM_START = 2,
249 RTP_RETRY = 10
250 };
251 enum {
252 SIP_SOCK,
253 RTP_SOCK,
254 RTCP_SOCK,
255 };
256 int i;
257 pj_uint16_t rtp_port;
258 pj_sock_t sock[3];
259 pj_sockaddr_in mapped_addr[3];
260 pj_status_t status;
261
262 for (i=0; i<3; ++i)
263 sock[i] = PJ_INVALID_SOCKET;
264
265 /* Create and bind SIP UDP socket. */
266 status = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, &sock[SIP_SOCK]);
267 if (status != PJ_SUCCESS) {
268 pjsua_perror("socket() error", status);
269 goto on_error;
270 }
271
272 status = pj_sock_bind_in(sock[SIP_SOCK], 0, pjsua.sip_port);
273 if (status != PJ_SUCCESS) {
274 pjsua_perror("bind() error", status);
275 goto on_error;
276 }
277
278 /* Initialize start of RTP port to try. */
279 rtp_port = (pj_uint16_t)(RTP_START_PORT + (pj_rand() % RTP_RANDOM_START) / 2);
280
281 /* Loop retry to bind RTP and RTCP sockets. */
282 for (i=0; i<RTP_RETRY; ++i, rtp_port += 2) {
283
284 /* Create and bind RTP socket. */
285 status = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, &sock[RTP_SOCK]);
286 if (status != PJ_SUCCESS) {
287 pjsua_perror("socket() error", status);
288 goto on_error;
289 }
290
291 status = pj_sock_bind_in(sock[RTP_SOCK], 0, rtp_port);
292 if (status != PJ_SUCCESS) {
293 pj_sock_close(sock[RTP_SOCK]);
294 sock[RTP_SOCK] = PJ_INVALID_SOCKET;
295 continue;
296 }
297
298 /* Create and bind RTCP socket. */
299 status = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, &sock[RTCP_SOCK]);
300 if (status != PJ_SUCCESS) {
301 pjsua_perror("socket() error", status);
302 goto on_error;
303 }
304
305 status = pj_sock_bind_in(sock[RTCP_SOCK], 0, (pj_uint16_t)(rtp_port+1));
306 if (status != PJ_SUCCESS) {
307 pj_sock_close(sock[RTP_SOCK]);
308 sock[RTP_SOCK] = PJ_INVALID_SOCKET;
309
310 pj_sock_close(sock[RTCP_SOCK]);
311 sock[RTCP_SOCK] = PJ_INVALID_SOCKET;
312 continue;
313 }
314
315 /*
316 * If we're configured to use STUN, then find out the mapped address,
317 * and make sure that the mapped RTCP port is adjacent with the RTP.
318 */
319 if (pjsua.stun_port1 == 0) {
320 const pj_str_t *hostname;
321 pj_sockaddr_in addr;
322
323 /* Get local IP address. */
324 hostname = pj_gethostname();
325
326 pj_memset( &addr, 0, sizeof(addr));
327 addr.sin_family = PJ_AF_INET;
328 status = pj_sockaddr_in_set_str_addr( &addr, hostname);
329 if (status != PJ_SUCCESS) {
330 pjsua_perror("Unresolvable local hostname", status);
331 goto on_error;
332 }
333
334 for (i=0; i<3; ++i)
335 pj_memcpy(&mapped_addr[i], &addr, sizeof(addr));
336
337 mapped_addr[SIP_SOCK].sin_port = pj_htons((pj_uint16_t)pjsua.sip_port);
338 mapped_addr[RTP_SOCK].sin_port = pj_htons((pj_uint16_t)rtp_port);
339 mapped_addr[RTCP_SOCK].sin_port = pj_htons((pj_uint16_t)(rtp_port+1));
340 break;
341 } else {
342 status = pj_stun_get_mapped_addr( &pjsua.cp.factory, 3, sock,
343 &pjsua.stun_srv1, pjsua.stun_port1,
344 &pjsua.stun_srv2, pjsua.stun_port2,
345 mapped_addr);
346 if (status != PJ_SUCCESS) {
347 pjsua_perror("STUN error", status);
348 goto on_error;
349 }
350
351 if (pj_ntohs(mapped_addr[2].sin_port) == pj_ntohs(mapped_addr[1].sin_port)+1)
352 break;
353
354 pj_sock_close(sock[RTP_SOCK]); sock[RTP_SOCK] = PJ_INVALID_SOCKET;
355 pj_sock_close(sock[RTCP_SOCK]); sock[RTCP_SOCK] = PJ_INVALID_SOCKET;
356 }
357 }
358
359 if (sock[RTP_SOCK] == PJ_INVALID_SOCKET) {
360 PJ_LOG(1,(THIS_FILE, "Unable to find appropriate RTP/RTCP ports combination"));
361 goto on_error;
362 }
363
364 pjsua.sip_sock = sock[SIP_SOCK];
365 pj_memcpy(&pjsua.sip_sock_name, &mapped_addr[SIP_SOCK], sizeof(pj_sockaddr_in));
366 pjsua.rtp_sock = sock[RTP_SOCK];
367 pj_memcpy(&pjsua.rtp_sock_name, &mapped_addr[RTP_SOCK], sizeof(pj_sockaddr_in));
368 pjsua.rtcp_sock = sock[RTCP_SOCK];
369 pj_memcpy(&pjsua.rtcp_sock_name, &mapped_addr[RTCP_SOCK], sizeof(pj_sockaddr_in));
370
371 PJ_LOG(4,(THIS_FILE, "SIP UDP socket reachable at %s:%d",
372 pj_inet_ntoa(pjsua.sip_sock_name.sin_addr),
373 pj_ntohs(pjsua.sip_sock_name.sin_port)));
374 PJ_LOG(4,(THIS_FILE, "RTP socket reachable at %s:%d",
375 pj_inet_ntoa(pjsua.rtp_sock_name.sin_addr),
376 pj_ntohs(pjsua.rtp_sock_name.sin_port)));
377 PJ_LOG(4,(THIS_FILE, "RTCP UDP socket reachable at %s:%d",
378 pj_inet_ntoa(pjsua.rtcp_sock_name.sin_addr),
379 pj_ntohs(pjsua.rtcp_sock_name.sin_port)));
380
381 return PJ_SUCCESS;
382
383on_error:
384 for (i=0; i<3; ++i) {
385 if (sock[i] != PJ_INVALID_SOCKET)
386 pj_sock_close(sock[i]);
387 }
388 return status;
389}
390
391
392
393/*
394 * Initialize stack.
395 */
396static pj_status_t init_stack(void)
397{
398 pj_status_t status;
399
400 /* Create global endpoint: */
401
402 {
403 const pj_str_t *hostname;
404 const char *endpt_name;
405
406 /* Endpoint MUST be assigned a globally unique name.
407 * The name will be used as the hostname in Warning header.
408 */
409
410 /* For this implementation, we'll use hostname for simplicity */
411 hostname = pj_gethostname();
412 endpt_name = hostname->ptr;
413
414 /* Create the endpoint: */
415
416 status = pjsip_endpt_create(&pjsua.cp.factory, endpt_name,
417 &pjsua.endpt);
418 if (status != PJ_SUCCESS) {
419 pjsua_perror("Unable to create SIP endpoint", status);
420 return status;
421 }
422 }
423
424
425 /* Initialize transaction layer: */
426
427 status = pjsip_tsx_layer_init(pjsua.endpt);
428 if (status != PJ_SUCCESS) {
429 pjsua_perror("Transaction layer initialization error", status);
430 goto on_error;
431 }
432
433 /* Initialize UA layer module: */
434
435 status = pjsip_ua_init( pjsua.endpt, NULL );
436 if (status != PJ_SUCCESS) {
437 pjsua_perror("UA layer initialization error", status);
438 goto on_error;
439 }
440
441 /* Initialize and register pjsua's application module: */
442
443 {
444 pjsip_module my_mod =
445 {
446 NULL, NULL, /* prev, next. */
447 { "mod-pjsua", 9 }, /* Name. */
448 -1, /* Id */
449 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
450 NULL, /* User data. */
451 NULL, /* load() */
452 NULL, /* start() */
453 NULL, /* stop() */
454 NULL, /* unload() */
455 &mod_pjsua_on_rx_request, /* on_rx_request() */
456 &mod_pjsua_on_rx_response, /* on_rx_response() */
457 NULL, /* on_tx_request. */
458 NULL, /* on_tx_response() */
459 NULL, /* on_tsx_state() */
460 };
461
462 pjsua.mod = my_mod;
463
464 status = pjsip_endpt_register_module(pjsua.endpt, &pjsua.mod);
465 if (status != PJ_SUCCESS) {
466 pjsua_perror("Unable to register pjsua module", status);
467 goto on_error;
468 }
469 }
470
471 /* Initialize invite session module: */
472
473 {
474
475 /* Initialize invite session callback. */
476 pjsip_inv_callback inv_cb;
477
478 pj_memset(&inv_cb, 0, sizeof(inv_cb));
479 inv_cb.on_state_changed = &pjsua_inv_on_state_changed;
480 inv_cb.on_new_session = &pjsua_inv_on_new_session;
481
482 /* Initialize invite session module: */
483 status = pjsip_inv_usage_init(pjsua.endpt, &pjsua.mod, &inv_cb);
484 if (status != PJ_SUCCESS) {
485 pjsua_perror("Invite usage initialization error", status);
486 goto on_error;
487 }
488
489 }
490
491
Benny Prijonoccf95622006-02-07 18:48:01 +0000492 /* Done */
Benny Prijono268ca612006-02-07 12:34:11 +0000493
494 return PJ_SUCCESS;
495
496
497on_error:
498 pjsip_endpt_destroy(pjsua.endpt);
499 pjsua.endpt = NULL;
500 return status;
501}
502
503
504static int PJ_THREAD_FUNC pjsua_worker_thread(void *arg)
505{
506 PJ_UNUSED_ARG(arg);
507
508 while (!pjsua.quit_flag) {
509 pj_time_val timeout = { 0, 10 };
510 pjsip_endpt_handle_events (pjsua.endpt, &timeout);
511 }
512
513 return 0;
514}
515
516/*
517 * Initialize pjsua application.
Benny Prijonoccf95622006-02-07 18:48:01 +0000518 * This will initialize all libraries, create endpoint instance, and register
519 * pjsip modules.
Benny Prijono268ca612006-02-07 12:34:11 +0000520 */
521pj_status_t pjsua_init(void)
522{
Benny Prijono268ca612006-02-07 12:34:11 +0000523 pj_status_t status;
524
525 /* Init PJLIB logging: */
526
527 pj_log_set_level(pjsua.log_level);
528 pj_log_set_decor(pjsua.log_decor);
529
530
531 /* Init PJLIB: */
532
533 status = pj_init();
534 if (status != PJ_SUCCESS) {
535 pjsua_perror("pj_init() error", status);
536 return status;
537 }
538
539 /* Init memory pool: */
540
541 /* Init caching pool. */
542 pj_caching_pool_init(&pjsua.cp, &pj_pool_factory_default_policy, 0);
543
544 /* Create memory pool for application. */
545 pjsua.pool = pj_pool_create(&pjsua.cp.factory, "pjsua", 4000, 4000, NULL);
546
547
Benny Prijonoccf95622006-02-07 18:48:01 +0000548 /* Init PJSIP and all the modules: */
549
550 status = init_stack();
551 if (status != PJ_SUCCESS) {
552 pj_caching_pool_destroy(&pjsua.cp);
553 pjsua_perror("Stack initialization has returned error", status);
554 return status;
555 }
556
557 /* Done. */
558 return PJ_SUCCESS;
559}
560
561
562
563/*
564 * Start pjsua stack.
565 * This will start the registration process, if registration is configured.
566 */
567pj_status_t pjsua_start(void)
568{
569 int i; /* Must be signed */
570 pjsip_transport *udp_transport;
571 pj_status_t status;
572
Benny Prijono268ca612006-02-07 12:34:11 +0000573 /* Init sockets (STUN etc): */
574
575 status = init_sockets();
576 if (status != PJ_SUCCESS) {
577 pj_caching_pool_destroy(&pjsua.cp);
578 pjsua_perror("init_sockets() has returned error", status);
579 return status;
580 }
581
582
Benny Prijonoccf95622006-02-07 18:48:01 +0000583 /* Add UDP transport: */
Benny Prijono268ca612006-02-07 12:34:11 +0000584
Benny Prijonoccf95622006-02-07 18:48:01 +0000585 {
586 /* Init the published name for the transport.
587 * Depending whether STUN is used, this may be the STUN mapped
588 * address, or socket's bound address.
589 */
590 pjsip_host_port addr_name;
591
592 addr_name.host.ptr = pj_inet_ntoa(pjsua.sip_sock_name.sin_addr);
593 addr_name.host.slen = pj_native_strlen(addr_name.host.ptr);
594 addr_name.port = pj_ntohs(pjsua.sip_sock_name.sin_port);
595
596 /* Create UDP transport from previously created UDP socket: */
597
598 status = pjsip_udp_transport_attach( pjsua.endpt, pjsua.sip_sock,
599 &addr_name, 1,
600 &udp_transport);
601 if (status != PJ_SUCCESS) {
602 pjsua_perror("Unable to start UDP transport", status);
603 return status;
604 }
Benny Prijono268ca612006-02-07 12:34:11 +0000605 }
606
Benny Prijonoccf95622006-02-07 18:48:01 +0000607 /* Initialize Contact URI, if one is not specified: */
608
609 if (pjsua.contact_uri.slen == 0 && pjsua.local_uri.slen) {
610
611 pjsip_uri *uri;
612 pjsip_sip_uri *sip_uri;
613 char contact[128];
614 int len;
615
616 /* The local Contact is the username@ip-addr, where
617 * - username is taken from the local URI,
618 * - ip-addr in UDP transport's address name (which may have been
619 * resolved from STUN.
620 */
621
622 /* Need to parse local_uri to get the elements: */
623
624 uri = pjsip_parse_uri(pjsua.pool, pjsua.local_uri.ptr,
625 pjsua.local_uri.slen, 0);
626 if (uri == NULL) {
627 pjsua_perror("Invalid local URI", PJSIP_EINVALIDURI);
628 return PJSIP_EINVALIDURI;
629 }
630
631
632 /* Local URI MUST be a SIP or SIPS: */
633
634 if (!PJSIP_URI_SCHEME_IS_SIP(uri) && !PJSIP_URI_SCHEME_IS_SIPS(uri)) {
635 pjsua_perror("Invalid local URI", PJSIP_EINVALIDSCHEME);
636 return PJSIP_EINVALIDSCHEME;
637 }
638
639
640 /* Get the SIP URI object: */
641
642 sip_uri = (pjsip_sip_uri*) pjsip_uri_get_uri(uri);
643
644
645 /* Build temporary contact string. */
646
647 if (sip_uri->user.slen) {
648
649 /* With the user part. */
650 len = pj_snprintf(contact, sizeof(contact),
651 "<sip:%.*s@%.*s:%d>",
652 sip_uri->user.slen,
653 sip_uri->user.ptr,
654 udp_transport->local_name.host.slen,
655 udp_transport->local_name.host.ptr,
656 udp_transport->local_name.port);
657 } else {
658
659 /* Without user part */
660
661 len = pj_snprintf(contact, sizeof(contact),
662 "<sip:%.*s:%d>",
663 udp_transport->local_name.host.slen,
664 udp_transport->local_name.host.ptr,
665 udp_transport->local_name.port);
666 }
667
668 if (len < 1 || len >= sizeof(contact)) {
669 pjsua_perror("Invalid Contact", PJSIP_EURITOOLONG);
670 return PJSIP_EURITOOLONG;
671 }
672
673 /* Duplicate Contact uri. */
674
675 pj_strdup2(pjsua.pool, &pjsua.contact_uri, contact);
676
677 }
678
679 /* Initialize global route_set: */
680
681 PJ_TODO(INIT_GLOBAL_ROUTE_SET);
682
683
Benny Prijono268ca612006-02-07 12:34:11 +0000684 /* Create worker thread(s), if required: */
685
686 for (i=0; i<pjsua.thread_cnt; ++i) {
687 status = pj_thread_create( pjsua.pool, "pjsua", &pjsua_worker_thread,
688 NULL, 0, 0, &pjsua.threads[i]);
689 if (status != PJ_SUCCESS) {
690 pjsua.quit_flag = 1;
691 for (--i; i>=0; --i) {
692 pj_thread_join(pjsua.threads[i]);
693 pj_thread_destroy(pjsua.threads[i]);
694 }
695 pj_caching_pool_destroy(&pjsua.cp);
696 return status;
697 }
698 }
699
Benny Prijonoccf95622006-02-07 18:48:01 +0000700 /* Start registration: */
701
702 /* Create client registration session: */
703
704 status = pjsua_regc_init();
705 if (status != PJ_SUCCESS)
706 return status;
707
708 /* Perform registration, if required. */
709 if (pjsua.regc) {
710 pjsua_regc_update(1);
711 }
712
713
714
Benny Prijono268ca612006-02-07 12:34:11 +0000715 return PJ_SUCCESS;
716}
717
718
719/*
720 * Destroy pjsua.
721 */
722pj_status_t pjsua_destroy(void)
723{
724 int i;
725
Benny Prijonoccf95622006-02-07 18:48:01 +0000726 /* Unregister, if required: */
727 if (pjsua.regc) {
728
729 pjsua_regc_update(0);
730
731 /* Wait for some time to allow unregistration to complete: */
732
733 pj_thread_sleep(500);
734 }
735
Benny Prijono268ca612006-02-07 12:34:11 +0000736 /* Signal threads to quit: */
737
738 pjsua.quit_flag = 1;
739
740 /* Wait worker threads to quit: */
741
742 for (i=0; i<pjsua.thread_cnt; ++i) {
743
744 pj_thread_join(pjsua.threads[i]);
745 pj_thread_destroy(pjsua.threads[i]);
746 }
747
748 /* Destroy endpoint. */
749 pjsip_endpt_destroy(pjsua.endpt);
750 pjsua.endpt = NULL;
751
752 /* Destroy caching pool. */
753 pj_caching_pool_destroy(&pjsua.cp);
754
755
756 /* Done. */
757
758 return PJ_SUCCESS;
759}
760
761
762/**
763 * Make outgoing call.
764 */
765pj_status_t pjsua_invite(const char *cstr_dest_uri,
766 pjsip_inv_session **p_inv)
767{
768 pj_str_t dest_uri;
769 pjsip_dialog *dlg;
770 pjmedia_sdp_session *offer;
771 pjsip_inv_session *inv;
772 pjsip_tx_data *tdata;
773 pj_status_t status;
774
775 /* Convert cstr_dest_uri to dest_uri */
776
777 dest_uri = pj_str((char*)cstr_dest_uri);
778
779 /* Create outgoing dialog: */
780
781 status = pjsip_dlg_create_uac( pjsip_ua_instance(), &pjsua.local_uri,
782 &pjsua.contact_uri, &dest_uri, &dest_uri,
783 &dlg);
784 if (status != PJ_SUCCESS) {
785 pjsua_perror("Dialog creation failed", status);
786 return status;
787 }
788
789 /* Create dummy SDP for offer: */
790
791 status = pjmedia_sdp_parse(dlg->pool, PJSUA_DUMMY_SDP_OFFER,
792 pj_native_strlen(PJSUA_DUMMY_SDP_OFFER),
793 &offer);
794 if (status != PJ_SUCCESS) {
795 pjsua_perror("Dummy SDP offer parsing failed", status);
796 goto on_error;
797 }
798
799 /* Create the INVITE session: */
800
801 status = pjsip_inv_create_uac( dlg, offer, 0, &inv);
802 if (status != PJ_SUCCESS) {
803 pjsua_perror("Invite session creation failed", status);
804 goto on_error;
805 }
806
807
Benny Prijonoccf95622006-02-07 18:48:01 +0000808 /* Set dialog Route-Set: */
809
810 PJ_TODO(INIT_DIALOG_ROUTE_SET);
811
Benny Prijono268ca612006-02-07 12:34:11 +0000812 /* Set credentials: */
813
Benny Prijonoccf95622006-02-07 18:48:01 +0000814 pjsip_auth_clt_set_credentials( &dlg->auth_sess, pjsua.cred_count,
815 pjsua.cred_info);
Benny Prijono268ca612006-02-07 12:34:11 +0000816
817
818 /* Create initial INVITE: */
819
820 status = pjsip_inv_invite(inv, &tdata);
821 if (status != PJ_SUCCESS) {
822 pjsua_perror("Unable to create initial INVITE request", status);
823 goto on_error;
824 }
825
826
827 /* Send initial INVITE: */
828
829 status = pjsip_inv_send_msg(inv, tdata, NULL);
830 if (status != PJ_SUCCESS) {
831 pjsua_perror("Unable to send initial INVITE request", status);
832 goto on_error;
833 }
834
835
836 /* Done. */
837
838 *p_inv = inv;
839
840 return PJ_SUCCESS;
841
842
843on_error:
844
845 PJ_TODO(DESTROY_DIALOG_ON_FAIL);
846 return status;
847}
848