blob: 9a48a13dd3a14fc26d31f4c51ebb56b863de9be5 [file] [log] [blame]
Benny Prijono53fde132006-03-17 19:41:19 +00001/* $Id$ */
2/*
Benny Prijono844653c2008-12-23 17:27:53 +00003 * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
Benny Prijono32177c02008-06-20 22:44:47 +00004 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijono53fde132006-03-17 19:41:19 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
Benny Prijono6107a002006-03-17 18:01:27 +000020
Benny Prijono53fde132006-03-17 19:41:19 +000021
22/**
23 * simpleua.c
24 *
25 * This is a very simple SIP user agent complete with media. The user
26 * agent should do a proper SDP negotiation and start RTP media once
27 * SDP negotiation has completed.
28 *
29 * This program does not register to SIP server.
30 *
31 * Capabilities to be demonstrated here:
32 * - Basic call
Benny Prijonoe8df45f2008-03-08 09:26:22 +000033 * - Should support IPv6 (not tested)
Benny Prijono53fde132006-03-17 19:41:19 +000034 * - UDP transport at port 5060 (hard coded)
35 * - RTP socket at port 4000 (hard coded)
36 * - proper SDP negotiation
37 * - PCMA/PCMU codec only.
38 * - Audio/media to sound device.
39 *
40 *
41 * Usage:
42 * - To make outgoing call, start simpleua with the URL of remote
43 * destination to contact.
44 * E.g.:
45 * simpleua sip:user@remote
46 *
47 * - Incoming calls will automatically be answered with 180, then 200.
48 *
49 * This program does not disconnect call.
50 *
51 * This program will quit once it has completed a single call.
52 */
53
54/* Include all headers. */
Benny Prijono6107a002006-03-17 18:01:27 +000055#include <pjsip.h>
Benny Prijono6107a002006-03-17 18:01:27 +000056#include <pjmedia.h>
Benny Prijono6107a002006-03-17 18:01:27 +000057#include <pjmedia-codec.h>
Benny Prijono6107a002006-03-17 18:01:27 +000058#include <pjsip_ua.h>
Benny Prijono6107a002006-03-17 18:01:27 +000059#include <pjsip_simple.h>
Benny Prijono6107a002006-03-17 18:01:27 +000060#include <pjlib-util.h>
Benny Prijono6107a002006-03-17 18:01:27 +000061#include <pjlib.h>
62
Benny Prijono53fde132006-03-17 19:41:19 +000063/* For logging purpose. */
Benny Prijono6107a002006-03-17 18:01:27 +000064#define THIS_FILE "simpleua.c"
65
Benny Prijonobc797312006-03-24 20:44:27 +000066#include "util.h"
Benny Prijono6107a002006-03-17 18:01:27 +000067
Benny Prijonoe8df45f2008-03-08 09:26:22 +000068
69/* Settings */
70#define AF pj_AF_INET() /* Change to pj_AF_INET6() for IPv6.
71 * PJ_HAS_IPV6 must be enabled and
72 * your system must support IPv6. */
73#define SIP_PORT 5060 /* Listening SIP port */
74#define RTP_PORT 4000 /* RTP port */
75
Benny Prijono6107a002006-03-17 18:01:27 +000076/*
77 * Static variables.
78 */
Benny Prijono6107a002006-03-17 18:01:27 +000079
Benny Prijono53fde132006-03-17 19:41:19 +000080static pj_bool_t g_complete; /* Quit flag. */
81static pjsip_endpoint *g_endpt; /* SIP endpoint. */
82static pj_caching_pool cp; /* Global pool factory. */
Benny Prijono6107a002006-03-17 18:01:27 +000083
Benny Prijono53fde132006-03-17 19:41:19 +000084static pjmedia_endpt *g_med_endpt; /* Media endpoint. */
Benny Prijonoe1a5a852008-03-11 21:38:05 +000085static pjmedia_transport_info g_med_tpinfo; /* Socket info for media */
Benny Prijonob04c9e02006-05-17 17:17:39 +000086static pjmedia_transport *g_med_transport;/* Media stream transport */
Benny Prijono6107a002006-03-17 18:01:27 +000087
Benny Prijono53fde132006-03-17 19:41:19 +000088/* Call variables: */
89static pjsip_inv_session *g_inv; /* Current invite session. */
90static pjmedia_session *g_med_session; /* Call's media session. */
91static pjmedia_snd_port *g_snd_player; /* Call's sound player */
92static pjmedia_snd_port *g_snd_rec; /* Call's sound recorder. */
Benny Prijono6107a002006-03-17 18:01:27 +000093
94
95/*
Benny Prijono53fde132006-03-17 19:41:19 +000096 * Prototypes:
Benny Prijono6107a002006-03-17 18:01:27 +000097 */
Benny Prijono53fde132006-03-17 19:41:19 +000098
99/* Callback to be called when SDP negotiation is done in the call: */
Benny Prijono6107a002006-03-17 18:01:27 +0000100static void call_on_media_update( pjsip_inv_session *inv,
101 pj_status_t status);
Benny Prijono53fde132006-03-17 19:41:19 +0000102
103/* Callback to be called when invite session's state has changed: */
Benny Prijono6107a002006-03-17 18:01:27 +0000104static void call_on_state_changed( pjsip_inv_session *inv,
105 pjsip_event *e);
Benny Prijono53fde132006-03-17 19:41:19 +0000106
107/* Callback to be called when dialog has forked: */
Benny Prijono6107a002006-03-17 18:01:27 +0000108static void call_on_forked(pjsip_inv_session *inv, pjsip_event *e);
Benny Prijono53fde132006-03-17 19:41:19 +0000109
110/* Callback to be called to handle incoming requests outside dialogs: */
Benny Prijono6107a002006-03-17 18:01:27 +0000111static pj_bool_t on_rx_request( pjsip_rx_data *rdata );
112
113
Benny Prijono53fde132006-03-17 19:41:19 +0000114
115
116/* This is a PJSIP module to be registered by application to handle
117 * incoming requests outside any dialogs/transactions. The main purpose
118 * here is to handle incoming INVITE request message, where we will
119 * create a dialog and INVITE session for it.
120 */
Benny Prijono6107a002006-03-17 18:01:27 +0000121static pjsip_module mod_simpleua =
122{
123 NULL, NULL, /* prev, next. */
124 { "mod-simpleua", 12 }, /* Name. */
125 -1, /* Id */
126 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
127 NULL, /* load() */
128 NULL, /* start() */
129 NULL, /* stop() */
130 NULL, /* unload() */
131 &on_rx_request, /* on_rx_request() */
132 NULL, /* on_rx_response() */
133 NULL, /* on_tx_request. */
134 NULL, /* on_tx_response() */
135 NULL, /* on_tsx_state() */
136};
137
138
Benny Prijono6107a002006-03-17 18:01:27 +0000139
140/*
141 * main()
Benny Prijono53fde132006-03-17 19:41:19 +0000142 *
143 * If called with argument, treat argument as SIP URL to be called.
144 * Otherwise wait for incoming calls.
Benny Prijono6107a002006-03-17 18:01:27 +0000145 */
146int main(int argc, char *argv[])
147{
148 pj_status_t status;
149
Benny Prijono53fde132006-03-17 19:41:19 +0000150 /* Must init PJLIB first: */
Benny Prijono6107a002006-03-17 18:01:27 +0000151 status = pj_init();
152 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
153
Benny Prijono53fde132006-03-17 19:41:19 +0000154
155 /* Then init PJLIB-UTIL: */
Benny Prijono6107a002006-03-17 18:01:27 +0000156 status = pjlib_util_init();
157 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
158
159
Benny Prijono53fde132006-03-17 19:41:19 +0000160 /* Must create a pool factory before we can allocate any memory. */
Benny Prijono6107a002006-03-17 18:01:27 +0000161 pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
162
Benny Prijono53fde132006-03-17 19:41:19 +0000163
Benny Prijono6107a002006-03-17 18:01:27 +0000164 /* Create global endpoint: */
165 {
166 const pj_str_t *hostname;
167 const char *endpt_name;
168
169 /* Endpoint MUST be assigned a globally unique name.
170 * The name will be used as the hostname in Warning header.
171 */
172
173 /* For this implementation, we'll use hostname for simplicity */
174 hostname = pj_gethostname();
175 endpt_name = hostname->ptr;
176
177 /* Create the endpoint: */
178
179 status = pjsip_endpt_create(&cp.factory, endpt_name,
180 &g_endpt);
181 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
182 }
183
Benny Prijono53fde132006-03-17 19:41:19 +0000184
Benny Prijono6107a002006-03-17 18:01:27 +0000185 /*
Benny Prijono53fde132006-03-17 19:41:19 +0000186 * Add UDP transport, with hard-coded port
187 * Alternatively, application can use pjsip_udp_transport_attach() to
188 * start UDP transport, if it already has an UDP socket (e.g. after it
189 * resolves the address with STUN).
Benny Prijono6107a002006-03-17 18:01:27 +0000190 */
191 {
Benny Prijonoe8df45f2008-03-08 09:26:22 +0000192 pj_sockaddr addr;
Benny Prijono6107a002006-03-17 18:01:27 +0000193
Benny Prijonoe8df45f2008-03-08 09:26:22 +0000194 pj_sockaddr_init(AF, &addr, NULL, (pj_uint16_t)SIP_PORT);
195
196 if (AF == pj_AF_INET()) {
197 status = pjsip_udp_transport_start( g_endpt, &addr.ipv4, NULL,
198 1, NULL);
199 } else if (AF == pj_AF_INET6()) {
200 status = pjsip_udp_transport_start6(g_endpt, &addr.ipv6, NULL,
201 1, NULL);
202 } else {
203 status = PJ_EAFNOTSUP;
204 }
Benny Prijono6107a002006-03-17 18:01:27 +0000205
Benny Prijono6107a002006-03-17 18:01:27 +0000206 if (status != PJ_SUCCESS) {
207 app_perror(THIS_FILE, "Unable to start UDP transport", status);
208 return 1;
209 }
210 }
211
212
213 /*
Benny Prijono53fde132006-03-17 19:41:19 +0000214 * Init transaction layer.
215 * This will create/initialize transaction hash tables etc.
Benny Prijono6107a002006-03-17 18:01:27 +0000216 */
217 status = pjsip_tsx_layer_init_module(g_endpt);
218 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
219
Benny Prijono53fde132006-03-17 19:41:19 +0000220
Benny Prijono6107a002006-03-17 18:01:27 +0000221 /*
Benny Prijono53fde132006-03-17 19:41:19 +0000222 * Initialize UA layer module.
223 * This will create/initialize dialog hash tables etc.
Benny Prijono6107a002006-03-17 18:01:27 +0000224 */
225 status = pjsip_ua_init_module( g_endpt, NULL );
226 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
227
Benny Prijono53fde132006-03-17 19:41:19 +0000228
229 /*
Benny Prijono6107a002006-03-17 18:01:27 +0000230 * Init invite session module.
Benny Prijono53fde132006-03-17 19:41:19 +0000231 * The invite session module initialization takes additional argument,
232 * i.e. a structure containing callbacks to be called on specific
233 * occurence of events.
234 *
235 * The on_state_changed and on_new_session callbacks are mandatory.
236 * Application must supply the callback function.
237 *
238 * We use on_media_update() callback in this application to start
239 * media transmission.
Benny Prijono6107a002006-03-17 18:01:27 +0000240 */
241 {
242 pjsip_inv_callback inv_cb;
243
244 /* Init the callback for INVITE session: */
Benny Prijonoac623b32006-07-03 15:19:31 +0000245 pj_bzero(&inv_cb, sizeof(inv_cb));
Benny Prijono6107a002006-03-17 18:01:27 +0000246 inv_cb.on_state_changed = &call_on_state_changed;
247 inv_cb.on_new_session = &call_on_forked;
248 inv_cb.on_media_update = &call_on_media_update;
249
250 /* Initialize invite session module: */
251 status = pjsip_inv_usage_init(g_endpt, &inv_cb);
252 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
253 }
254
Benny Prijonodf912082007-10-30 16:41:45 +0000255 /* Initialize 100rel support */
256 status = pjsip_100rel_init_module(g_endpt);
257 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono6107a002006-03-17 18:01:27 +0000258
259 /*
Benny Prijono53fde132006-03-17 19:41:19 +0000260 * Register our module to receive incoming requests.
Benny Prijono6107a002006-03-17 18:01:27 +0000261 */
262 status = pjsip_endpt_register_module( g_endpt, &mod_simpleua);
263 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
264
265
266 /*
Benny Prijono53fde132006-03-17 19:41:19 +0000267 * Initialize media endpoint.
268 * This will implicitly initialize PJMEDIA too.
Benny Prijono6107a002006-03-17 18:01:27 +0000269 */
Benny Prijono54826a32007-03-30 18:57:01 +0000270#if PJ_HAS_THREADS
Benny Prijono275fd682006-03-22 11:59:11 +0000271 status = pjmedia_endpt_create(&cp.factory, NULL, 1, &g_med_endpt);
Benny Prijono54826a32007-03-30 18:57:01 +0000272#else
273 status = pjmedia_endpt_create(&cp.factory,
274 pjsip_endpt_get_ioqueue(g_endpt),
275 0, &g_med_endpt);
276#endif
Benny Prijono6107a002006-03-17 18:01:27 +0000277 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
278
279 /*
280 * Add PCMA/PCMU codec to the media endpoint.
281 */
Benny Prijonofc24e692007-01-27 18:31:51 +0000282#if defined(PJMEDIA_HAS_G711_CODEC) && PJMEDIA_HAS_G711_CODEC!=0
Benny Prijono6107a002006-03-17 18:01:27 +0000283 status = pjmedia_codec_g711_init(g_med_endpt);
284 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
Benny Prijonofc24e692007-01-27 18:31:51 +0000285#endif
Benny Prijono6107a002006-03-17 18:01:27 +0000286
Benny Prijonoc1e263f2006-08-09 13:53:59 +0000287
Benny Prijono6107a002006-03-17 18:01:27 +0000288 /*
Benny Prijonoc1e263f2006-08-09 13:53:59 +0000289 * Create media transport used to send/receive RTP/RTCP socket.
290 * One media transport is needed for each call. Application may
291 * opt to re-use the same media transport for subsequent calls.
Benny Prijono6107a002006-03-17 18:01:27 +0000292 */
Benny Prijonoe8df45f2008-03-08 09:26:22 +0000293 status = pjmedia_transport_udp_create3(g_med_endpt, AF, NULL, NULL,
294 RTP_PORT, 0, &g_med_transport);
Benny Prijonob04c9e02006-05-17 17:17:39 +0000295 if (status != PJ_SUCCESS) {
296 app_perror(THIS_FILE, "Unable to create media transport", status);
297 return 1;
298 }
299
Benny Prijonoc1e263f2006-08-09 13:53:59 +0000300 /*
301 * Get socket info (address, port) of the media transport. We will
302 * need this info to create SDP (i.e. the address and port info in
303 * the SDP).
304 */
Benny Prijono734fc2d2008-03-17 16:05:35 +0000305 pjmedia_transport_info_init(&g_med_tpinfo);
Benny Prijonoe1a5a852008-03-11 21:38:05 +0000306 pjmedia_transport_get_info(g_med_transport, &g_med_tpinfo);
Benny Prijonoc1e263f2006-08-09 13:53:59 +0000307
308
Benny Prijono6107a002006-03-17 18:01:27 +0000309 /*
310 * If URL is specified, then make call immediately.
311 */
312 if (argc > 1) {
Benny Prijonoe8df45f2008-03-08 09:26:22 +0000313 pj_sockaddr hostaddr;
314 char hostip[PJ_INET6_ADDRSTRLEN+2];
Benny Prijono6107a002006-03-17 18:01:27 +0000315 char temp[80];
316 pj_str_t dst_uri = pj_str(argv[1]);
317 pj_str_t local_uri;
318 pjsip_dialog *dlg;
319 pjmedia_sdp_session *local_sdp;
320 pjsip_tx_data *tdata;
321
Benny Prijonoe8df45f2008-03-08 09:26:22 +0000322 if (pj_gethostip(AF, &hostaddr) != PJ_SUCCESS) {
323 app_perror(THIS_FILE, "Unable to retrieve local host IP", status);
324 return 1;
325 }
326 pj_sockaddr_print(&hostaddr, hostip, sizeof(hostip), 2);
327
328 pj_ansi_sprintf(temp, "<sip:simpleuac@%s:%d>",
329 hostip, SIP_PORT);
Benny Prijono6107a002006-03-17 18:01:27 +0000330 local_uri = pj_str(temp);
331
332 /* Create UAC dialog */
333 status = pjsip_dlg_create_uac( pjsip_ua_instance(),
334 &local_uri, /* local URI */
Benny Prijonoe8df45f2008-03-08 09:26:22 +0000335 &local_uri, /* local Contact */
Benny Prijono6107a002006-03-17 18:01:27 +0000336 &dst_uri, /* remote URI */
337 &dst_uri, /* remote target */
338 &dlg); /* dialog */
339 if (status != PJ_SUCCESS) {
340 app_perror(THIS_FILE, "Unable to create UAC dialog", status);
341 return 1;
342 }
343
Benny Prijono0a968362006-03-18 12:29:01 +0000344 /* If we expect the outgoing INVITE to be challenged, then we should
345 * put the credentials in the dialog here, with something like this:
346 *
347 {
348 pjsip_cred_info cred[1];
349
350 cred[0].realm = pj_str("sip.server.realm");
351 cred[0].username = pj_str("theuser");
352 cred[0].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD;
353 cred[0].data = pj_str("thepassword");
354
355 pjsip_auth_clt_set_credentials( &dlg->auth_sess, 1, cred);
356 }
357 *
358 */
359
360
Benny Prijonodf912082007-10-30 16:41:45 +0000361 /* Get the SDP body to be put in the outgoing INVITE, by asking
362 * media endpoint to create one for us. The SDP will contain all
363 * codecs that have been registered to it (in this case, only
364 * PCMA and PCMU), plus telephony event.
365 */
366 status = pjmedia_endpt_create_sdp( g_med_endpt, /* the media endpt */
367 dlg->pool, /* pool. */
368 1, /* # of streams */
Benny Prijonoe1a5a852008-03-11 21:38:05 +0000369 &g_med_tpinfo.sock_info,
370 /* RTP sock info */
Benny Prijonodf912082007-10-30 16:41:45 +0000371 &local_sdp); /* the SDP result */
372 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
373
374
375
376 /* Create the INVITE session, and pass the SDP returned earlier
377 * as the session's initial capability.
378 */
379 status = pjsip_inv_create_uac( dlg, local_sdp, 0, &g_inv);
380 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
381
Benny Prijono0a968362006-03-18 12:29:01 +0000382 /* If we want the initial INVITE to travel to specific SIP proxies,
383 * then we should put the initial dialog's route set here. The final
384 * route set will be updated once a dialog has been established.
385 * To set the dialog's initial route set, we do it with something
386 * like this:
387 *
388 {
389 pjsip_route_hdr route_set;
390 pjsip_route_hdr *route;
391 const pj_str_t hname = { "Route", 5 };
392 char *uri = "sip:proxy.server;lr";
393
394 pj_list_init(&route_set);
395
396 route = pjsip_parse_hdr( dlg->pool, &hname,
397 uri, strlen(uri),
398 NULL);
399 PJ_ASSERT_RETURN(route != NULL, 1);
400 pj_list_push_back(&route_set, route);
401
402 pjsip_dlg_set_route_set(dlg, &route_set);
403 }
404 *
405 * Note that Route URI SHOULD have an ";lr" parameter!
406 */
407
Benny Prijono53fde132006-03-17 19:41:19 +0000408 /* Create initial INVITE request.
409 * This INVITE request will contain a perfectly good request and
410 * an SDP body as well.
411 */
Benny Prijono6107a002006-03-17 18:01:27 +0000412 status = pjsip_inv_invite(g_inv, &tdata);
413 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
414
Benny Prijono53fde132006-03-17 19:41:19 +0000415
416
417 /* Send initial INVITE request.
418 * From now on, the invite session's state will be reported to us
419 * via the invite session callbacks.
420 */
Benny Prijono6107a002006-03-17 18:01:27 +0000421 status = pjsip_inv_send_msg(g_inv, tdata);
422 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
423
Benny Prijono53fde132006-03-17 19:41:19 +0000424
Benny Prijono6107a002006-03-17 18:01:27 +0000425 } else {
Benny Prijono53fde132006-03-17 19:41:19 +0000426
427 /* No URL to make call to */
428
Benny Prijono6107a002006-03-17 18:01:27 +0000429 PJ_LOG(3,(THIS_FILE, "Ready to accept incoming calls..."));
430 }
431
432
433 /* Loop until one call is completed */
434 for (;!g_complete;) {
435 pj_time_val timeout = {0, 10};
436 pjsip_endpt_handle_events(g_endpt, &timeout);
437 }
438
Benny Prijonobc797312006-03-24 20:44:27 +0000439 /* On exit, dump current memory usage: */
440 dump_pool_usage(THIS_FILE, &cp);
Benny Prijono0a968362006-03-18 12:29:01 +0000441
Benny Prijono6107a002006-03-17 18:01:27 +0000442 return 0;
443}
444
445
446
447/*
448 * Callback when INVITE session state has changed.
Benny Prijono53fde132006-03-17 19:41:19 +0000449 * This callback is registered when the invite session module is initialized.
450 * We mostly want to know when the invite session has been disconnected,
451 * so that we can quit the application.
Benny Prijono6107a002006-03-17 18:01:27 +0000452 */
453static void call_on_state_changed( pjsip_inv_session *inv,
454 pjsip_event *e)
455{
Benny Prijono15953012006-04-27 22:37:08 +0000456 PJ_UNUSED_ARG(e);
457
Benny Prijono6107a002006-03-17 18:01:27 +0000458 if (inv->state == PJSIP_INV_STATE_DISCONNECTED) {
459
460 PJ_LOG(3,(THIS_FILE, "Call DISCONNECTED [reason=%d (%s)]",
461 inv->cause,
462 pjsip_get_status_text(inv->cause)->ptr));
463
464 PJ_LOG(3,(THIS_FILE, "One call completed, application quitting..."));
465 g_complete = 1;
466
467 } else {
468
469 PJ_LOG(3,(THIS_FILE, "Call state changed to %s",
470 pjsip_inv_state_name(inv->state)));
471
472 }
473}
474
Benny Prijono6107a002006-03-17 18:01:27 +0000475
Benny Prijono53fde132006-03-17 19:41:19 +0000476/* This callback is called when dialog has forked. */
Benny Prijono6107a002006-03-17 18:01:27 +0000477static void call_on_forked(pjsip_inv_session *inv, pjsip_event *e)
478{
Benny Prijono53fde132006-03-17 19:41:19 +0000479 /* To be done... */
Benny Prijono15953012006-04-27 22:37:08 +0000480 PJ_UNUSED_ARG(inv);
481 PJ_UNUSED_ARG(e);
Benny Prijono6107a002006-03-17 18:01:27 +0000482}
483
Benny Prijono53fde132006-03-17 19:41:19 +0000484
Benny Prijono6107a002006-03-17 18:01:27 +0000485/*
486 * Callback when incoming requests outside any transactions and any
Benny Prijono53fde132006-03-17 19:41:19 +0000487 * dialogs are received. We're only interested to hande incoming INVITE
488 * request, and we'll reject any other requests with 500 response.
Benny Prijono6107a002006-03-17 18:01:27 +0000489 */
490static pj_bool_t on_rx_request( pjsip_rx_data *rdata )
491{
Benny Prijonoe8df45f2008-03-08 09:26:22 +0000492 pj_sockaddr hostaddr;
493 char temp[80], hostip[PJ_INET6_ADDRSTRLEN];
494 pj_str_t local_uri;
Benny Prijono6107a002006-03-17 18:01:27 +0000495 pjsip_dialog *dlg;
496 pjmedia_sdp_session *local_sdp;
497 pjsip_tx_data *tdata;
498 unsigned options = 0;
499 pj_status_t status;
500
Benny Prijono53fde132006-03-17 19:41:19 +0000501
Benny Prijono6107a002006-03-17 18:01:27 +0000502 /*
503 * Respond (statelessly) any non-INVITE requests with 500
504 */
505 if (rdata->msg_info.msg->line.req.method.id != PJSIP_INVITE_METHOD) {
506
Benny Prijono53932c02007-02-13 18:51:44 +0000507 if (rdata->msg_info.msg->line.req.method.id != PJSIP_ACK_METHOD) {
508 pj_str_t reason = pj_str("Simple UA unable to handle "
509 "this request");
Benny Prijono6107a002006-03-17 18:01:27 +0000510
Benny Prijono53932c02007-02-13 18:51:44 +0000511 pjsip_endpt_respond_stateless( g_endpt, rdata,
512 500, &reason,
513 NULL, NULL);
514 }
Benny Prijono6107a002006-03-17 18:01:27 +0000515 return PJ_TRUE;
516 }
517
Benny Prijono53fde132006-03-17 19:41:19 +0000518
Benny Prijono6107a002006-03-17 18:01:27 +0000519 /*
520 * Reject INVITE if we already have an INVITE session in progress.
521 */
522 if (g_inv) {
523
524 pj_str_t reason = pj_str("Another call is in progress");
525
526 pjsip_endpt_respond_stateless( g_endpt, rdata,
527 500, &reason,
528 NULL, NULL);
529 return PJ_TRUE;
530
531 }
532
533 /* Verify that we can handle the request. */
534 status = pjsip_inv_verify_request(rdata, &options, NULL, NULL,
535 g_endpt, NULL);
536 if (status != PJ_SUCCESS) {
537
538 pj_str_t reason = pj_str("Sorry Simple UA can not handle this INVITE");
539
540 pjsip_endpt_respond_stateless( g_endpt, rdata,
541 500, &reason,
542 NULL, NULL);
543 return PJ_TRUE;
544 }
545
546 /*
Benny Prijonoe8df45f2008-03-08 09:26:22 +0000547 * Generate Contact URI
548 */
549 if (pj_gethostip(AF, &hostaddr) != PJ_SUCCESS) {
550 app_perror(THIS_FILE, "Unable to retrieve local host IP", status);
551 return PJ_TRUE;
552 }
553 pj_sockaddr_print(&hostaddr, hostip, sizeof(hostip), 2);
554
555 pj_ansi_sprintf(temp, "<sip:simpleuas@%s:%d>",
556 hostip, SIP_PORT);
557 local_uri = pj_str(temp);
558
559 /*
Benny Prijono6107a002006-03-17 18:01:27 +0000560 * Create UAS dialog.
561 */
562 status = pjsip_dlg_create_uas( pjsip_ua_instance(),
563 rdata,
Benny Prijonoe8df45f2008-03-08 09:26:22 +0000564 &local_uri, /* contact */
Benny Prijono6107a002006-03-17 18:01:27 +0000565 &dlg);
566 if (status != PJ_SUCCESS) {
567 pjsip_endpt_respond_stateless(g_endpt, rdata, 500, NULL,
568 NULL, NULL);
569 return PJ_TRUE;
570 }
571
572 /*
573 * Get media capability from media endpoint:
574 */
575
576 status = pjmedia_endpt_create_sdp( g_med_endpt, rdata->tp_info.pool, 1,
Benny Prijonoe1a5a852008-03-11 21:38:05 +0000577 &g_med_tpinfo.sock_info,
Benny Prijono6107a002006-03-17 18:01:27 +0000578 &local_sdp);
579 PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE);
580
Benny Prijono53fde132006-03-17 19:41:19 +0000581
Benny Prijono6107a002006-03-17 18:01:27 +0000582 /*
Benny Prijono53fde132006-03-17 19:41:19 +0000583 * Create invite session, and pass both the UAS dialog and the SDP
584 * capability to the session.
Benny Prijono6107a002006-03-17 18:01:27 +0000585 */
586 status = pjsip_inv_create_uas( dlg, rdata, local_sdp, 0, &g_inv);
587 PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE);
588
Benny Prijono53fde132006-03-17 19:41:19 +0000589
Benny Prijono6107a002006-03-17 18:01:27 +0000590 /*
591 * Initially send 180 response.
Benny Prijono53fde132006-03-17 19:41:19 +0000592 *
593 * The very first response to an INVITE must be created with
594 * pjsip_inv_initial_answer(). Subsequent responses to the same
595 * transaction MUST use pjsip_inv_answer().
Benny Prijono6107a002006-03-17 18:01:27 +0000596 */
597 status = pjsip_inv_initial_answer(g_inv, rdata,
598 180,
599 NULL, NULL, &tdata);
600 PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE);
601
Benny Prijono53fde132006-03-17 19:41:19 +0000602
603 /* Send the 180 response. */
Benny Prijono6107a002006-03-17 18:01:27 +0000604 status = pjsip_inv_send_msg(g_inv, tdata);
605 PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE);
606
Benny Prijono53fde132006-03-17 19:41:19 +0000607
Benny Prijono6107a002006-03-17 18:01:27 +0000608 /*
Benny Prijono53fde132006-03-17 19:41:19 +0000609 * Now create 200 response.
Benny Prijono6107a002006-03-17 18:01:27 +0000610 */
611 status = pjsip_inv_answer( g_inv,
612 200, NULL, /* st_code and st_text */
613 NULL, /* SDP already specified */
614 &tdata);
615 PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE);
616
617 /*
618 * Send the 200 response.
619 */
620 status = pjsip_inv_send_msg(g_inv, tdata);
621 PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE);
622
Benny Prijono53fde132006-03-17 19:41:19 +0000623
624 /* Done.
625 * When the call is disconnected, it will be reported via the callback.
626 */
627
Benny Prijono6107a002006-03-17 18:01:27 +0000628 return PJ_TRUE;
629}
630
631
Benny Prijono53fde132006-03-17 19:41:19 +0000632
633/*
634 * Callback when SDP negotiation has completed.
635 * We are interested with this callback because we want to start media
636 * as soon as SDP negotiation is completed.
637 */
638static void call_on_media_update( pjsip_inv_session *inv,
639 pj_status_t status)
640{
Benny Prijono8befd9f2006-05-13 22:46:23 +0000641 pjmedia_session_info sess_info;
Benny Prijono53fde132006-03-17 19:41:19 +0000642 const pjmedia_sdp_session *local_sdp;
643 const pjmedia_sdp_session *remote_sdp;
644 pjmedia_port *media_port;
645
646 if (status != PJ_SUCCESS) {
647
648 app_perror(THIS_FILE, "SDP negotiation has failed", status);
649
650 /* Here we should disconnect call if we're not in the middle
651 * of initializing an UAS dialog and if this is not a re-INVITE.
652 */
653 return;
654 }
655
656 /* Get local and remote SDP.
657 * We need both SDPs to create a media session.
658 */
659 status = pjmedia_sdp_neg_get_active_local(inv->neg, &local_sdp);
660
661 status = pjmedia_sdp_neg_get_active_remote(inv->neg, &remote_sdp);
662
663
Benny Prijono8befd9f2006-05-13 22:46:23 +0000664 /* Create session info based on the two SDPs.
665 * We only support one stream per session for now.
666 */
Benny Prijonob04c9e02006-05-17 17:17:39 +0000667 status = pjmedia_session_info_from_sdp(inv->dlg->pool, g_med_endpt,
668 1, &sess_info,
Benny Prijono8befd9f2006-05-13 22:46:23 +0000669 local_sdp, remote_sdp);
670 if (status != PJ_SUCCESS) {
671 app_perror( THIS_FILE, "Unable to create media session", status);
672 return;
673 }
674
675 /* If required, we can also change some settings in the session info,
676 * (such as jitter buffer settings, codec settings, etc) before we
677 * create the session.
678 */
679
Benny Prijono53fde132006-03-17 19:41:19 +0000680 /* Create new media session, passing the two SDPs, and also the
681 * media socket that we created earlier.
682 * The media session is active immediately.
683 */
Benny Prijono8befd9f2006-05-13 22:46:23 +0000684 status = pjmedia_session_create( g_med_endpt, &sess_info,
Benny Prijonob04c9e02006-05-17 17:17:39 +0000685 &g_med_transport, NULL, &g_med_session );
Benny Prijono53fde132006-03-17 19:41:19 +0000686 if (status != PJ_SUCCESS) {
687 app_perror( THIS_FILE, "Unable to create media session", status);
688 return;
689 }
690
691
692 /* Get the media port interface of the first stream in the session.
693 * Media port interface is basicly a struct containing get_frame() and
694 * put_frame() function. With this media port interface, we can attach
695 * the port interface to conference bridge, or directly to a sound
696 * player/recorder device.
697 */
698 pjmedia_session_get_port(g_med_session, 0, &media_port);
699
700
701
702 /* Create a sound Player device and connect the media port to the
703 * sound device.
704 */
705 status = pjmedia_snd_port_create_player(
706 inv->pool, /* pool */
707 -1, /* sound dev id */
Benny Prijono15953012006-04-27 22:37:08 +0000708 media_port->info.clock_rate, /* clock rate */
Benny Prijono53fde132006-03-17 19:41:19 +0000709 media_port->info.channel_count, /* channel count */
710 media_port->info.samples_per_frame, /* samples per frame*/
711 media_port->info.bits_per_sample, /* bits per sample */
712 0, /* options */
713 &g_snd_player);
714 if (status != PJ_SUCCESS) {
715 app_perror( THIS_FILE, "Unable to create sound player", status);
716 PJ_LOG(3,(THIS_FILE, "%d %d %d %d",
Benny Prijono15953012006-04-27 22:37:08 +0000717 media_port->info.clock_rate, /* clock rate */
Benny Prijono53fde132006-03-17 19:41:19 +0000718 media_port->info.channel_count, /* channel count */
719 media_port->info.samples_per_frame, /* samples per frame*/
720 media_port->info.bits_per_sample /* bits per sample */
721 ));
722 return;
723 }
724
725 status = pjmedia_snd_port_connect(g_snd_player, media_port);
726
727
728 /* Create a sound recorder device and connect the media port to the
729 * sound device.
730 */
731 status = pjmedia_snd_port_create_rec(
732 inv->pool, /* pool */
733 -1, /* sound dev id */
Benny Prijono15953012006-04-27 22:37:08 +0000734 media_port->info.clock_rate, /* clock rate */
Benny Prijono53fde132006-03-17 19:41:19 +0000735 media_port->info.channel_count, /* channel count */
736 media_port->info.samples_per_frame, /* samples per frame*/
737 media_port->info.bits_per_sample, /* bits per sample */
738 0, /* options */
739 &g_snd_rec);
740 if (status != PJ_SUCCESS) {
741 app_perror( THIS_FILE, "Unable to create sound recorder", status);
742 return;
743 }
744
745 status = pjmedia_snd_port_connect(g_snd_rec, media_port);
746
747 /* Done with media. */
748}
749
750