blob: 645e239d08c92d593f100f9af3599be90ccb3d92 [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");
Benny Prijonobecb31b2009-01-20 08:53:38 +0000351 cred[0].scheme = pj_str("digest");
Benny Prijono0a968362006-03-18 12:29:01 +0000352 cred[0].username = pj_str("theuser");
353 cred[0].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD;
354 cred[0].data = pj_str("thepassword");
355
356 pjsip_auth_clt_set_credentials( &dlg->auth_sess, 1, cred);
357 }
358 *
359 */
360
361
Benny Prijonodf912082007-10-30 16:41:45 +0000362 /* Get the SDP body to be put in the outgoing INVITE, by asking
363 * media endpoint to create one for us. The SDP will contain all
364 * codecs that have been registered to it (in this case, only
365 * PCMA and PCMU), plus telephony event.
366 */
367 status = pjmedia_endpt_create_sdp( g_med_endpt, /* the media endpt */
368 dlg->pool, /* pool. */
369 1, /* # of streams */
Benny Prijonoe1a5a852008-03-11 21:38:05 +0000370 &g_med_tpinfo.sock_info,
371 /* RTP sock info */
Benny Prijonodf912082007-10-30 16:41:45 +0000372 &local_sdp); /* the SDP result */
373 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
374
375
376
377 /* Create the INVITE session, and pass the SDP returned earlier
378 * as the session's initial capability.
379 */
380 status = pjsip_inv_create_uac( dlg, local_sdp, 0, &g_inv);
381 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
382
Benny Prijono0a968362006-03-18 12:29:01 +0000383 /* If we want the initial INVITE to travel to specific SIP proxies,
384 * then we should put the initial dialog's route set here. The final
385 * route set will be updated once a dialog has been established.
386 * To set the dialog's initial route set, we do it with something
387 * like this:
388 *
389 {
390 pjsip_route_hdr route_set;
391 pjsip_route_hdr *route;
392 const pj_str_t hname = { "Route", 5 };
393 char *uri = "sip:proxy.server;lr";
394
395 pj_list_init(&route_set);
396
397 route = pjsip_parse_hdr( dlg->pool, &hname,
398 uri, strlen(uri),
399 NULL);
400 PJ_ASSERT_RETURN(route != NULL, 1);
401 pj_list_push_back(&route_set, route);
402
403 pjsip_dlg_set_route_set(dlg, &route_set);
404 }
405 *
406 * Note that Route URI SHOULD have an ";lr" parameter!
407 */
408
Benny Prijono53fde132006-03-17 19:41:19 +0000409 /* Create initial INVITE request.
410 * This INVITE request will contain a perfectly good request and
411 * an SDP body as well.
412 */
Benny Prijono6107a002006-03-17 18:01:27 +0000413 status = pjsip_inv_invite(g_inv, &tdata);
414 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
415
Benny Prijono53fde132006-03-17 19:41:19 +0000416
417
418 /* Send initial INVITE request.
419 * From now on, the invite session's state will be reported to us
420 * via the invite session callbacks.
421 */
Benny Prijono6107a002006-03-17 18:01:27 +0000422 status = pjsip_inv_send_msg(g_inv, tdata);
423 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
424
Benny Prijono53fde132006-03-17 19:41:19 +0000425
Benny Prijono6107a002006-03-17 18:01:27 +0000426 } else {
Benny Prijono53fde132006-03-17 19:41:19 +0000427
428 /* No URL to make call to */
429
Benny Prijono6107a002006-03-17 18:01:27 +0000430 PJ_LOG(3,(THIS_FILE, "Ready to accept incoming calls..."));
431 }
432
433
434 /* Loop until one call is completed */
435 for (;!g_complete;) {
436 pj_time_val timeout = {0, 10};
437 pjsip_endpt_handle_events(g_endpt, &timeout);
438 }
439
Benny Prijonobc797312006-03-24 20:44:27 +0000440 /* On exit, dump current memory usage: */
441 dump_pool_usage(THIS_FILE, &cp);
Benny Prijono0a968362006-03-18 12:29:01 +0000442
Benny Prijono6107a002006-03-17 18:01:27 +0000443 return 0;
444}
445
446
447
448/*
449 * Callback when INVITE session state has changed.
Benny Prijono53fde132006-03-17 19:41:19 +0000450 * This callback is registered when the invite session module is initialized.
451 * We mostly want to know when the invite session has been disconnected,
452 * so that we can quit the application.
Benny Prijono6107a002006-03-17 18:01:27 +0000453 */
454static void call_on_state_changed( pjsip_inv_session *inv,
455 pjsip_event *e)
456{
Benny Prijono15953012006-04-27 22:37:08 +0000457 PJ_UNUSED_ARG(e);
458
Benny Prijono6107a002006-03-17 18:01:27 +0000459 if (inv->state == PJSIP_INV_STATE_DISCONNECTED) {
460
461 PJ_LOG(3,(THIS_FILE, "Call DISCONNECTED [reason=%d (%s)]",
462 inv->cause,
463 pjsip_get_status_text(inv->cause)->ptr));
464
465 PJ_LOG(3,(THIS_FILE, "One call completed, application quitting..."));
466 g_complete = 1;
467
468 } else {
469
470 PJ_LOG(3,(THIS_FILE, "Call state changed to %s",
471 pjsip_inv_state_name(inv->state)));
472
473 }
474}
475
Benny Prijono6107a002006-03-17 18:01:27 +0000476
Benny Prijono53fde132006-03-17 19:41:19 +0000477/* This callback is called when dialog has forked. */
Benny Prijono6107a002006-03-17 18:01:27 +0000478static void call_on_forked(pjsip_inv_session *inv, pjsip_event *e)
479{
Benny Prijono53fde132006-03-17 19:41:19 +0000480 /* To be done... */
Benny Prijono15953012006-04-27 22:37:08 +0000481 PJ_UNUSED_ARG(inv);
482 PJ_UNUSED_ARG(e);
Benny Prijono6107a002006-03-17 18:01:27 +0000483}
484
Benny Prijono53fde132006-03-17 19:41:19 +0000485
Benny Prijono6107a002006-03-17 18:01:27 +0000486/*
487 * Callback when incoming requests outside any transactions and any
Benny Prijono53fde132006-03-17 19:41:19 +0000488 * dialogs are received. We're only interested to hande incoming INVITE
489 * request, and we'll reject any other requests with 500 response.
Benny Prijono6107a002006-03-17 18:01:27 +0000490 */
491static pj_bool_t on_rx_request( pjsip_rx_data *rdata )
492{
Benny Prijonoe8df45f2008-03-08 09:26:22 +0000493 pj_sockaddr hostaddr;
494 char temp[80], hostip[PJ_INET6_ADDRSTRLEN];
495 pj_str_t local_uri;
Benny Prijono6107a002006-03-17 18:01:27 +0000496 pjsip_dialog *dlg;
497 pjmedia_sdp_session *local_sdp;
498 pjsip_tx_data *tdata;
499 unsigned options = 0;
500 pj_status_t status;
501
Benny Prijono53fde132006-03-17 19:41:19 +0000502
Benny Prijono6107a002006-03-17 18:01:27 +0000503 /*
504 * Respond (statelessly) any non-INVITE requests with 500
505 */
506 if (rdata->msg_info.msg->line.req.method.id != PJSIP_INVITE_METHOD) {
507
Benny Prijono53932c02007-02-13 18:51:44 +0000508 if (rdata->msg_info.msg->line.req.method.id != PJSIP_ACK_METHOD) {
509 pj_str_t reason = pj_str("Simple UA unable to handle "
510 "this request");
Benny Prijono6107a002006-03-17 18:01:27 +0000511
Benny Prijono53932c02007-02-13 18:51:44 +0000512 pjsip_endpt_respond_stateless( g_endpt, rdata,
513 500, &reason,
514 NULL, NULL);
515 }
Benny Prijono6107a002006-03-17 18:01:27 +0000516 return PJ_TRUE;
517 }
518
Benny Prijono53fde132006-03-17 19:41:19 +0000519
Benny Prijono6107a002006-03-17 18:01:27 +0000520 /*
521 * Reject INVITE if we already have an INVITE session in progress.
522 */
523 if (g_inv) {
524
525 pj_str_t reason = pj_str("Another call is in progress");
526
527 pjsip_endpt_respond_stateless( g_endpt, rdata,
528 500, &reason,
529 NULL, NULL);
530 return PJ_TRUE;
531
532 }
533
534 /* Verify that we can handle the request. */
535 status = pjsip_inv_verify_request(rdata, &options, NULL, NULL,
536 g_endpt, NULL);
537 if (status != PJ_SUCCESS) {
538
539 pj_str_t reason = pj_str("Sorry Simple UA can not handle this INVITE");
540
541 pjsip_endpt_respond_stateless( g_endpt, rdata,
542 500, &reason,
543 NULL, NULL);
544 return PJ_TRUE;
545 }
546
547 /*
Benny Prijonoe8df45f2008-03-08 09:26:22 +0000548 * Generate Contact URI
549 */
550 if (pj_gethostip(AF, &hostaddr) != PJ_SUCCESS) {
551 app_perror(THIS_FILE, "Unable to retrieve local host IP", status);
552 return PJ_TRUE;
553 }
554 pj_sockaddr_print(&hostaddr, hostip, sizeof(hostip), 2);
555
556 pj_ansi_sprintf(temp, "<sip:simpleuas@%s:%d>",
557 hostip, SIP_PORT);
558 local_uri = pj_str(temp);
559
560 /*
Benny Prijono6107a002006-03-17 18:01:27 +0000561 * Create UAS dialog.
562 */
563 status = pjsip_dlg_create_uas( pjsip_ua_instance(),
564 rdata,
Benny Prijonoe8df45f2008-03-08 09:26:22 +0000565 &local_uri, /* contact */
Benny Prijono6107a002006-03-17 18:01:27 +0000566 &dlg);
567 if (status != PJ_SUCCESS) {
568 pjsip_endpt_respond_stateless(g_endpt, rdata, 500, NULL,
569 NULL, NULL);
570 return PJ_TRUE;
571 }
572
573 /*
574 * Get media capability from media endpoint:
575 */
576
577 status = pjmedia_endpt_create_sdp( g_med_endpt, rdata->tp_info.pool, 1,
Benny Prijonoe1a5a852008-03-11 21:38:05 +0000578 &g_med_tpinfo.sock_info,
Benny Prijono6107a002006-03-17 18:01:27 +0000579 &local_sdp);
580 PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE);
581
Benny Prijono53fde132006-03-17 19:41:19 +0000582
Benny Prijono6107a002006-03-17 18:01:27 +0000583 /*
Benny Prijono53fde132006-03-17 19:41:19 +0000584 * Create invite session, and pass both the UAS dialog and the SDP
585 * capability to the session.
Benny Prijono6107a002006-03-17 18:01:27 +0000586 */
587 status = pjsip_inv_create_uas( dlg, rdata, local_sdp, 0, &g_inv);
588 PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE);
589
Benny Prijono53fde132006-03-17 19:41:19 +0000590
Benny Prijono6107a002006-03-17 18:01:27 +0000591 /*
592 * Initially send 180 response.
Benny Prijono53fde132006-03-17 19:41:19 +0000593 *
594 * The very first response to an INVITE must be created with
595 * pjsip_inv_initial_answer(). Subsequent responses to the same
596 * transaction MUST use pjsip_inv_answer().
Benny Prijono6107a002006-03-17 18:01:27 +0000597 */
598 status = pjsip_inv_initial_answer(g_inv, rdata,
599 180,
600 NULL, NULL, &tdata);
601 PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE);
602
Benny Prijono53fde132006-03-17 19:41:19 +0000603
604 /* Send the 180 response. */
Benny Prijono6107a002006-03-17 18:01:27 +0000605 status = pjsip_inv_send_msg(g_inv, tdata);
606 PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE);
607
Benny Prijono53fde132006-03-17 19:41:19 +0000608
Benny Prijono6107a002006-03-17 18:01:27 +0000609 /*
Benny Prijono53fde132006-03-17 19:41:19 +0000610 * Now create 200 response.
Benny Prijono6107a002006-03-17 18:01:27 +0000611 */
612 status = pjsip_inv_answer( g_inv,
613 200, NULL, /* st_code and st_text */
614 NULL, /* SDP already specified */
615 &tdata);
616 PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE);
617
618 /*
619 * Send the 200 response.
620 */
621 status = pjsip_inv_send_msg(g_inv, tdata);
622 PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE);
623
Benny Prijono53fde132006-03-17 19:41:19 +0000624
625 /* Done.
626 * When the call is disconnected, it will be reported via the callback.
627 */
628
Benny Prijono6107a002006-03-17 18:01:27 +0000629 return PJ_TRUE;
630}
631
632
Benny Prijono53fde132006-03-17 19:41:19 +0000633
634/*
635 * Callback when SDP negotiation has completed.
636 * We are interested with this callback because we want to start media
637 * as soon as SDP negotiation is completed.
638 */
639static void call_on_media_update( pjsip_inv_session *inv,
640 pj_status_t status)
641{
Benny Prijono8befd9f2006-05-13 22:46:23 +0000642 pjmedia_session_info sess_info;
Benny Prijono53fde132006-03-17 19:41:19 +0000643 const pjmedia_sdp_session *local_sdp;
644 const pjmedia_sdp_session *remote_sdp;
645 pjmedia_port *media_port;
646
647 if (status != PJ_SUCCESS) {
648
649 app_perror(THIS_FILE, "SDP negotiation has failed", status);
650
651 /* Here we should disconnect call if we're not in the middle
652 * of initializing an UAS dialog and if this is not a re-INVITE.
653 */
654 return;
655 }
656
657 /* Get local and remote SDP.
658 * We need both SDPs to create a media session.
659 */
660 status = pjmedia_sdp_neg_get_active_local(inv->neg, &local_sdp);
661
662 status = pjmedia_sdp_neg_get_active_remote(inv->neg, &remote_sdp);
663
664
Benny Prijono8befd9f2006-05-13 22:46:23 +0000665 /* Create session info based on the two SDPs.
666 * We only support one stream per session for now.
667 */
Benny Prijonob04c9e02006-05-17 17:17:39 +0000668 status = pjmedia_session_info_from_sdp(inv->dlg->pool, g_med_endpt,
669 1, &sess_info,
Benny Prijono8befd9f2006-05-13 22:46:23 +0000670 local_sdp, remote_sdp);
671 if (status != PJ_SUCCESS) {
672 app_perror( THIS_FILE, "Unable to create media session", status);
673 return;
674 }
675
676 /* If required, we can also change some settings in the session info,
677 * (such as jitter buffer settings, codec settings, etc) before we
678 * create the session.
679 */
680
Benny Prijono53fde132006-03-17 19:41:19 +0000681 /* Create new media session, passing the two SDPs, and also the
682 * media socket that we created earlier.
683 * The media session is active immediately.
684 */
Benny Prijono8befd9f2006-05-13 22:46:23 +0000685 status = pjmedia_session_create( g_med_endpt, &sess_info,
Benny Prijonob04c9e02006-05-17 17:17:39 +0000686 &g_med_transport, NULL, &g_med_session );
Benny Prijono53fde132006-03-17 19:41:19 +0000687 if (status != PJ_SUCCESS) {
688 app_perror( THIS_FILE, "Unable to create media session", status);
689 return;
690 }
691
692
693 /* Get the media port interface of the first stream in the session.
694 * Media port interface is basicly a struct containing get_frame() and
695 * put_frame() function. With this media port interface, we can attach
696 * the port interface to conference bridge, or directly to a sound
697 * player/recorder device.
698 */
699 pjmedia_session_get_port(g_med_session, 0, &media_port);
700
701
702
703 /* Create a sound Player device and connect the media port to the
704 * sound device.
705 */
706 status = pjmedia_snd_port_create_player(
707 inv->pool, /* pool */
708 -1, /* sound dev id */
Benny Prijono15953012006-04-27 22:37:08 +0000709 media_port->info.clock_rate, /* clock rate */
Benny Prijono53fde132006-03-17 19:41:19 +0000710 media_port->info.channel_count, /* channel count */
711 media_port->info.samples_per_frame, /* samples per frame*/
712 media_port->info.bits_per_sample, /* bits per sample */
713 0, /* options */
714 &g_snd_player);
715 if (status != PJ_SUCCESS) {
716 app_perror( THIS_FILE, "Unable to create sound player", status);
717 PJ_LOG(3,(THIS_FILE, "%d %d %d %d",
Benny Prijono15953012006-04-27 22:37:08 +0000718 media_port->info.clock_rate, /* clock rate */
Benny Prijono53fde132006-03-17 19:41:19 +0000719 media_port->info.channel_count, /* channel count */
720 media_port->info.samples_per_frame, /* samples per frame*/
721 media_port->info.bits_per_sample /* bits per sample */
722 ));
723 return;
724 }
725
726 status = pjmedia_snd_port_connect(g_snd_player, media_port);
727
728
729 /* Create a sound recorder device and connect the media port to the
730 * sound device.
731 */
732 status = pjmedia_snd_port_create_rec(
733 inv->pool, /* pool */
734 -1, /* sound dev id */
Benny Prijono15953012006-04-27 22:37:08 +0000735 media_port->info.clock_rate, /* clock rate */
Benny Prijono53fde132006-03-17 19:41:19 +0000736 media_port->info.channel_count, /* channel count */
737 media_port->info.samples_per_frame, /* samples per frame*/
738 media_port->info.bits_per_sample, /* bits per sample */
739 0, /* options */
740 &g_snd_rec);
741 if (status != PJ_SUCCESS) {
742 app_perror( THIS_FILE, "Unable to create sound recorder", status);
743 return;
744 }
745
746 status = pjmedia_snd_port_connect(g_snd_rec, media_port);
747
748 /* Done with media. */
749}
750
751