blob: edf0b838769f3c5dbaef09e87ec178b239d7c1e0 [file] [log] [blame]
Benny Prijono53fde132006-03-17 19:41:19 +00001/* $Id$ */
2/*
Benny Prijonoa771a512007-02-19 01:13:53 +00003 * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org>
Benny Prijono53fde132006-03-17 19:41:19 +00004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
Benny Prijono6107a002006-03-17 18:01:27 +000019
Benny Prijono53fde132006-03-17 19:41:19 +000020
21/**
22 * simpleua.c
23 *
24 * This is a very simple SIP user agent complete with media. The user
25 * agent should do a proper SDP negotiation and start RTP media once
26 * SDP negotiation has completed.
27 *
28 * This program does not register to SIP server.
29 *
30 * Capabilities to be demonstrated here:
31 * - Basic call
32 * - UDP transport at port 5060 (hard coded)
33 * - RTP socket at port 4000 (hard coded)
34 * - proper SDP negotiation
35 * - PCMA/PCMU codec only.
36 * - Audio/media to sound device.
37 *
38 *
39 * Usage:
40 * - To make outgoing call, start simpleua with the URL of remote
41 * destination to contact.
42 * E.g.:
43 * simpleua sip:user@remote
44 *
45 * - Incoming calls will automatically be answered with 180, then 200.
46 *
47 * This program does not disconnect call.
48 *
49 * This program will quit once it has completed a single call.
50 */
51
52/* Include all headers. */
Benny Prijono6107a002006-03-17 18:01:27 +000053#include <pjsip.h>
Benny Prijono6107a002006-03-17 18:01:27 +000054#include <pjmedia.h>
Benny Prijono6107a002006-03-17 18:01:27 +000055#include <pjmedia-codec.h>
Benny Prijono6107a002006-03-17 18:01:27 +000056#include <pjsip_ua.h>
Benny Prijono6107a002006-03-17 18:01:27 +000057#include <pjsip_simple.h>
Benny Prijono6107a002006-03-17 18:01:27 +000058#include <pjlib-util.h>
Benny Prijono6107a002006-03-17 18:01:27 +000059#include <pjlib.h>
60
Benny Prijono53fde132006-03-17 19:41:19 +000061/* For logging purpose. */
Benny Prijono6107a002006-03-17 18:01:27 +000062#define THIS_FILE "simpleua.c"
63
Benny Prijonobc797312006-03-24 20:44:27 +000064#include "util.h"
Benny Prijono6107a002006-03-17 18:01:27 +000065
66/*
67 * Static variables.
68 */
Benny Prijono6107a002006-03-17 18:01:27 +000069
Benny Prijono53fde132006-03-17 19:41:19 +000070static pj_bool_t g_complete; /* Quit flag. */
71static pjsip_endpoint *g_endpt; /* SIP endpoint. */
72static pj_caching_pool cp; /* Global pool factory. */
Benny Prijono6107a002006-03-17 18:01:27 +000073
Benny Prijono53fde132006-03-17 19:41:19 +000074static pjmedia_endpt *g_med_endpt; /* Media endpoint. */
75static pjmedia_sock_info g_med_skinfo; /* Socket info for media */
Benny Prijonob04c9e02006-05-17 17:17:39 +000076static pjmedia_transport *g_med_transport;/* Media stream transport */
Benny Prijono6107a002006-03-17 18:01:27 +000077
Benny Prijono53fde132006-03-17 19:41:19 +000078/* Call variables: */
79static pjsip_inv_session *g_inv; /* Current invite session. */
80static pjmedia_session *g_med_session; /* Call's media session. */
81static pjmedia_snd_port *g_snd_player; /* Call's sound player */
82static pjmedia_snd_port *g_snd_rec; /* Call's sound recorder. */
Benny Prijono6107a002006-03-17 18:01:27 +000083
84
85/*
Benny Prijono53fde132006-03-17 19:41:19 +000086 * Prototypes:
Benny Prijono6107a002006-03-17 18:01:27 +000087 */
Benny Prijono53fde132006-03-17 19:41:19 +000088
89/* Callback to be called when SDP negotiation is done in the call: */
Benny Prijono6107a002006-03-17 18:01:27 +000090static void call_on_media_update( pjsip_inv_session *inv,
91 pj_status_t status);
Benny Prijono53fde132006-03-17 19:41:19 +000092
93/* Callback to be called when invite session's state has changed: */
Benny Prijono6107a002006-03-17 18:01:27 +000094static void call_on_state_changed( pjsip_inv_session *inv,
95 pjsip_event *e);
Benny Prijono53fde132006-03-17 19:41:19 +000096
97/* Callback to be called when dialog has forked: */
Benny Prijono6107a002006-03-17 18:01:27 +000098static void call_on_forked(pjsip_inv_session *inv, pjsip_event *e);
Benny Prijono53fde132006-03-17 19:41:19 +000099
100/* Callback to be called to handle incoming requests outside dialogs: */
Benny Prijono6107a002006-03-17 18:01:27 +0000101static pj_bool_t on_rx_request( pjsip_rx_data *rdata );
102
103
Benny Prijono53fde132006-03-17 19:41:19 +0000104
105
106/* This is a PJSIP module to be registered by application to handle
107 * incoming requests outside any dialogs/transactions. The main purpose
108 * here is to handle incoming INVITE request message, where we will
109 * create a dialog and INVITE session for it.
110 */
Benny Prijono6107a002006-03-17 18:01:27 +0000111static pjsip_module mod_simpleua =
112{
113 NULL, NULL, /* prev, next. */
114 { "mod-simpleua", 12 }, /* Name. */
115 -1, /* Id */
116 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
117 NULL, /* load() */
118 NULL, /* start() */
119 NULL, /* stop() */
120 NULL, /* unload() */
121 &on_rx_request, /* on_rx_request() */
122 NULL, /* on_rx_response() */
123 NULL, /* on_tx_request. */
124 NULL, /* on_tx_response() */
125 NULL, /* on_tsx_state() */
126};
127
128
Benny Prijono6107a002006-03-17 18:01:27 +0000129
130/*
131 * main()
Benny Prijono53fde132006-03-17 19:41:19 +0000132 *
133 * If called with argument, treat argument as SIP URL to be called.
134 * Otherwise wait for incoming calls.
Benny Prijono6107a002006-03-17 18:01:27 +0000135 */
136int main(int argc, char *argv[])
137{
138 pj_status_t status;
139
Benny Prijono53fde132006-03-17 19:41:19 +0000140 /* Must init PJLIB first: */
Benny Prijono6107a002006-03-17 18:01:27 +0000141 status = pj_init();
142 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
143
Benny Prijono53fde132006-03-17 19:41:19 +0000144
145 /* Then init PJLIB-UTIL: */
Benny Prijono6107a002006-03-17 18:01:27 +0000146 status = pjlib_util_init();
147 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
148
149
Benny Prijono53fde132006-03-17 19:41:19 +0000150 /* Must create a pool factory before we can allocate any memory. */
Benny Prijono6107a002006-03-17 18:01:27 +0000151 pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
152
Benny Prijono53fde132006-03-17 19:41:19 +0000153
Benny Prijono6107a002006-03-17 18:01:27 +0000154 /* Create global endpoint: */
155 {
156 const pj_str_t *hostname;
157 const char *endpt_name;
158
159 /* Endpoint MUST be assigned a globally unique name.
160 * The name will be used as the hostname in Warning header.
161 */
162
163 /* For this implementation, we'll use hostname for simplicity */
164 hostname = pj_gethostname();
165 endpt_name = hostname->ptr;
166
167 /* Create the endpoint: */
168
169 status = pjsip_endpt_create(&cp.factory, endpt_name,
170 &g_endpt);
171 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
172 }
173
Benny Prijono53fde132006-03-17 19:41:19 +0000174
Benny Prijono6107a002006-03-17 18:01:27 +0000175 /*
Benny Prijono53fde132006-03-17 19:41:19 +0000176 * Add UDP transport, with hard-coded port
177 * Alternatively, application can use pjsip_udp_transport_attach() to
178 * start UDP transport, if it already has an UDP socket (e.g. after it
179 * resolves the address with STUN).
Benny Prijono6107a002006-03-17 18:01:27 +0000180 */
181 {
182 pj_sockaddr_in addr;
183
Benny Prijono8ab968f2007-07-20 08:08:30 +0000184 addr.sin_family = pj_AF_INET();
Benny Prijono6107a002006-03-17 18:01:27 +0000185 addr.sin_addr.s_addr = 0;
186 addr.sin_port = pj_htons(5060);
187
188 status = pjsip_udp_transport_start( g_endpt, &addr, NULL, 1, NULL);
189 if (status != PJ_SUCCESS) {
190 app_perror(THIS_FILE, "Unable to start UDP transport", status);
191 return 1;
192 }
193 }
194
195
196 /*
Benny Prijono53fde132006-03-17 19:41:19 +0000197 * Init transaction layer.
198 * This will create/initialize transaction hash tables etc.
Benny Prijono6107a002006-03-17 18:01:27 +0000199 */
200 status = pjsip_tsx_layer_init_module(g_endpt);
201 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
202
Benny Prijono53fde132006-03-17 19:41:19 +0000203
Benny Prijono6107a002006-03-17 18:01:27 +0000204 /*
Benny Prijono53fde132006-03-17 19:41:19 +0000205 * Initialize UA layer module.
206 * This will create/initialize dialog hash tables etc.
Benny Prijono6107a002006-03-17 18:01:27 +0000207 */
208 status = pjsip_ua_init_module( g_endpt, NULL );
209 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
210
Benny Prijono53fde132006-03-17 19:41:19 +0000211
212 /*
Benny Prijono6107a002006-03-17 18:01:27 +0000213 * Init invite session module.
Benny Prijono53fde132006-03-17 19:41:19 +0000214 * The invite session module initialization takes additional argument,
215 * i.e. a structure containing callbacks to be called on specific
216 * occurence of events.
217 *
218 * The on_state_changed and on_new_session callbacks are mandatory.
219 * Application must supply the callback function.
220 *
221 * We use on_media_update() callback in this application to start
222 * media transmission.
Benny Prijono6107a002006-03-17 18:01:27 +0000223 */
224 {
225 pjsip_inv_callback inv_cb;
226
227 /* Init the callback for INVITE session: */
Benny Prijonoac623b32006-07-03 15:19:31 +0000228 pj_bzero(&inv_cb, sizeof(inv_cb));
Benny Prijono6107a002006-03-17 18:01:27 +0000229 inv_cb.on_state_changed = &call_on_state_changed;
230 inv_cb.on_new_session = &call_on_forked;
231 inv_cb.on_media_update = &call_on_media_update;
232
233 /* Initialize invite session module: */
234 status = pjsip_inv_usage_init(g_endpt, &inv_cb);
235 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
236 }
237
Benny Prijonodf912082007-10-30 16:41:45 +0000238 /* Initialize 100rel support */
239 status = pjsip_100rel_init_module(g_endpt);
240 PJ_ASSERT_RETURN(status == PJ_SUCCESS, status);
Benny Prijono6107a002006-03-17 18:01:27 +0000241
242 /*
Benny Prijono53fde132006-03-17 19:41:19 +0000243 * Register our module to receive incoming requests.
Benny Prijono6107a002006-03-17 18:01:27 +0000244 */
245 status = pjsip_endpt_register_module( g_endpt, &mod_simpleua);
246 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
247
248
249 /*
Benny Prijono53fde132006-03-17 19:41:19 +0000250 * Initialize media endpoint.
251 * This will implicitly initialize PJMEDIA too.
Benny Prijono6107a002006-03-17 18:01:27 +0000252 */
Benny Prijono54826a32007-03-30 18:57:01 +0000253#if PJ_HAS_THREADS
Benny Prijono275fd682006-03-22 11:59:11 +0000254 status = pjmedia_endpt_create(&cp.factory, NULL, 1, &g_med_endpt);
Benny Prijono54826a32007-03-30 18:57:01 +0000255#else
256 status = pjmedia_endpt_create(&cp.factory,
257 pjsip_endpt_get_ioqueue(g_endpt),
258 0, &g_med_endpt);
259#endif
Benny Prijono6107a002006-03-17 18:01:27 +0000260 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
261
262 /*
263 * Add PCMA/PCMU codec to the media endpoint.
264 */
Benny Prijonofc24e692007-01-27 18:31:51 +0000265#if defined(PJMEDIA_HAS_G711_CODEC) && PJMEDIA_HAS_G711_CODEC!=0
Benny Prijono6107a002006-03-17 18:01:27 +0000266 status = pjmedia_codec_g711_init(g_med_endpt);
267 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
Benny Prijonofc24e692007-01-27 18:31:51 +0000268#endif
Benny Prijono6107a002006-03-17 18:01:27 +0000269
Benny Prijonoc1e263f2006-08-09 13:53:59 +0000270
Benny Prijono6107a002006-03-17 18:01:27 +0000271 /*
Benny Prijonoc1e263f2006-08-09 13:53:59 +0000272 * Create media transport used to send/receive RTP/RTCP socket.
273 * One media transport is needed for each call. Application may
274 * opt to re-use the same media transport for subsequent calls.
Benny Prijono6107a002006-03-17 18:01:27 +0000275 */
Benny Prijonoc1e263f2006-08-09 13:53:59 +0000276 status = pjmedia_transport_udp_create(g_med_endpt, NULL, 4000, 0,
277 &g_med_transport);
Benny Prijonob04c9e02006-05-17 17:17:39 +0000278 if (status != PJ_SUCCESS) {
279 app_perror(THIS_FILE, "Unable to create media transport", status);
280 return 1;
281 }
282
Benny Prijonoc1e263f2006-08-09 13:53:59 +0000283 /*
284 * Get socket info (address, port) of the media transport. We will
285 * need this info to create SDP (i.e. the address and port info in
286 * the SDP).
287 */
288 {
289 pjmedia_transport_udp_info udp_info;
290
291 pjmedia_transport_udp_get_info(g_med_transport, &udp_info);
292 pj_memcpy(&g_med_skinfo, &udp_info.skinfo,
293 sizeof(pjmedia_sock_info));
294 }
295
296
Benny Prijono6107a002006-03-17 18:01:27 +0000297 /*
298 * If URL is specified, then make call immediately.
299 */
300 if (argc > 1) {
301 char temp[80];
302 pj_str_t dst_uri = pj_str(argv[1]);
303 pj_str_t local_uri;
304 pjsip_dialog *dlg;
305 pjmedia_sdp_session *local_sdp;
306 pjsip_tx_data *tdata;
307
308 pj_ansi_sprintf(temp, "sip:simpleuac@%s", pjsip_endpt_name(g_endpt)->ptr);
309 local_uri = pj_str(temp);
310
311 /* Create UAC dialog */
312 status = pjsip_dlg_create_uac( pjsip_ua_instance(),
313 &local_uri, /* local URI */
314 NULL, /* local Contact */
315 &dst_uri, /* remote URI */
316 &dst_uri, /* remote target */
317 &dlg); /* dialog */
318 if (status != PJ_SUCCESS) {
319 app_perror(THIS_FILE, "Unable to create UAC dialog", status);
320 return 1;
321 }
322
Benny Prijono0a968362006-03-18 12:29:01 +0000323 /* If we expect the outgoing INVITE to be challenged, then we should
324 * put the credentials in the dialog here, with something like this:
325 *
326 {
327 pjsip_cred_info cred[1];
328
329 cred[0].realm = pj_str("sip.server.realm");
330 cred[0].username = pj_str("theuser");
331 cred[0].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD;
332 cred[0].data = pj_str("thepassword");
333
334 pjsip_auth_clt_set_credentials( &dlg->auth_sess, 1, cred);
335 }
336 *
337 */
338
339
Benny Prijonodf912082007-10-30 16:41:45 +0000340 /* Get the SDP body to be put in the outgoing INVITE, by asking
341 * media endpoint to create one for us. The SDP will contain all
342 * codecs that have been registered to it (in this case, only
343 * PCMA and PCMU), plus telephony event.
344 */
345 status = pjmedia_endpt_create_sdp( g_med_endpt, /* the media endpt */
346 dlg->pool, /* pool. */
347 1, /* # of streams */
348 &g_med_skinfo, /* RTP sock info */
349 &local_sdp); /* the SDP result */
350 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
351
352
353
354 /* Create the INVITE session, and pass the SDP returned earlier
355 * as the session's initial capability.
356 */
357 status = pjsip_inv_create_uac( dlg, local_sdp, 0, &g_inv);
358 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
359
Benny Prijono0a968362006-03-18 12:29:01 +0000360 /* If we want the initial INVITE to travel to specific SIP proxies,
361 * then we should put the initial dialog's route set here. The final
362 * route set will be updated once a dialog has been established.
363 * To set the dialog's initial route set, we do it with something
364 * like this:
365 *
366 {
367 pjsip_route_hdr route_set;
368 pjsip_route_hdr *route;
369 const pj_str_t hname = { "Route", 5 };
370 char *uri = "sip:proxy.server;lr";
371
372 pj_list_init(&route_set);
373
374 route = pjsip_parse_hdr( dlg->pool, &hname,
375 uri, strlen(uri),
376 NULL);
377 PJ_ASSERT_RETURN(route != NULL, 1);
378 pj_list_push_back(&route_set, route);
379
380 pjsip_dlg_set_route_set(dlg, &route_set);
381 }
382 *
383 * Note that Route URI SHOULD have an ";lr" parameter!
384 */
385
Benny Prijono53fde132006-03-17 19:41:19 +0000386 /* Create initial INVITE request.
387 * This INVITE request will contain a perfectly good request and
388 * an SDP body as well.
389 */
Benny Prijono6107a002006-03-17 18:01:27 +0000390 status = pjsip_inv_invite(g_inv, &tdata);
391 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
392
Benny Prijono53fde132006-03-17 19:41:19 +0000393
394
395 /* Send initial INVITE request.
396 * From now on, the invite session's state will be reported to us
397 * via the invite session callbacks.
398 */
Benny Prijono6107a002006-03-17 18:01:27 +0000399 status = pjsip_inv_send_msg(g_inv, tdata);
400 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
401
Benny Prijono53fde132006-03-17 19:41:19 +0000402
Benny Prijono6107a002006-03-17 18:01:27 +0000403 } else {
Benny Prijono53fde132006-03-17 19:41:19 +0000404
405 /* No URL to make call to */
406
Benny Prijono6107a002006-03-17 18:01:27 +0000407 PJ_LOG(3,(THIS_FILE, "Ready to accept incoming calls..."));
408 }
409
410
411 /* Loop until one call is completed */
412 for (;!g_complete;) {
413 pj_time_val timeout = {0, 10};
414 pjsip_endpt_handle_events(g_endpt, &timeout);
415 }
416
Benny Prijonobc797312006-03-24 20:44:27 +0000417 /* On exit, dump current memory usage: */
418 dump_pool_usage(THIS_FILE, &cp);
Benny Prijono0a968362006-03-18 12:29:01 +0000419
Benny Prijono6107a002006-03-17 18:01:27 +0000420 return 0;
421}
422
423
424
425/*
426 * Callback when INVITE session state has changed.
Benny Prijono53fde132006-03-17 19:41:19 +0000427 * This callback is registered when the invite session module is initialized.
428 * We mostly want to know when the invite session has been disconnected,
429 * so that we can quit the application.
Benny Prijono6107a002006-03-17 18:01:27 +0000430 */
431static void call_on_state_changed( pjsip_inv_session *inv,
432 pjsip_event *e)
433{
Benny Prijono15953012006-04-27 22:37:08 +0000434 PJ_UNUSED_ARG(e);
435
Benny Prijono6107a002006-03-17 18:01:27 +0000436 if (inv->state == PJSIP_INV_STATE_DISCONNECTED) {
437
438 PJ_LOG(3,(THIS_FILE, "Call DISCONNECTED [reason=%d (%s)]",
439 inv->cause,
440 pjsip_get_status_text(inv->cause)->ptr));
441
442 PJ_LOG(3,(THIS_FILE, "One call completed, application quitting..."));
443 g_complete = 1;
444
445 } else {
446
447 PJ_LOG(3,(THIS_FILE, "Call state changed to %s",
448 pjsip_inv_state_name(inv->state)));
449
450 }
451}
452
Benny Prijono6107a002006-03-17 18:01:27 +0000453
Benny Prijono53fde132006-03-17 19:41:19 +0000454/* This callback is called when dialog has forked. */
Benny Prijono6107a002006-03-17 18:01:27 +0000455static void call_on_forked(pjsip_inv_session *inv, pjsip_event *e)
456{
Benny Prijono53fde132006-03-17 19:41:19 +0000457 /* To be done... */
Benny Prijono15953012006-04-27 22:37:08 +0000458 PJ_UNUSED_ARG(inv);
459 PJ_UNUSED_ARG(e);
Benny Prijono6107a002006-03-17 18:01:27 +0000460}
461
Benny Prijono53fde132006-03-17 19:41:19 +0000462
Benny Prijono6107a002006-03-17 18:01:27 +0000463/*
464 * Callback when incoming requests outside any transactions and any
Benny Prijono53fde132006-03-17 19:41:19 +0000465 * dialogs are received. We're only interested to hande incoming INVITE
466 * request, and we'll reject any other requests with 500 response.
Benny Prijono6107a002006-03-17 18:01:27 +0000467 */
468static pj_bool_t on_rx_request( pjsip_rx_data *rdata )
469{
470 pjsip_dialog *dlg;
471 pjmedia_sdp_session *local_sdp;
472 pjsip_tx_data *tdata;
473 unsigned options = 0;
474 pj_status_t status;
475
Benny Prijono53fde132006-03-17 19:41:19 +0000476
Benny Prijono6107a002006-03-17 18:01:27 +0000477 /*
478 * Respond (statelessly) any non-INVITE requests with 500
479 */
480 if (rdata->msg_info.msg->line.req.method.id != PJSIP_INVITE_METHOD) {
481
Benny Prijono53932c02007-02-13 18:51:44 +0000482 if (rdata->msg_info.msg->line.req.method.id != PJSIP_ACK_METHOD) {
483 pj_str_t reason = pj_str("Simple UA unable to handle "
484 "this request");
Benny Prijono6107a002006-03-17 18:01:27 +0000485
Benny Prijono53932c02007-02-13 18:51:44 +0000486 pjsip_endpt_respond_stateless( g_endpt, rdata,
487 500, &reason,
488 NULL, NULL);
489 }
Benny Prijono6107a002006-03-17 18:01:27 +0000490 return PJ_TRUE;
491 }
492
Benny Prijono53fde132006-03-17 19:41:19 +0000493
Benny Prijono6107a002006-03-17 18:01:27 +0000494 /*
495 * Reject INVITE if we already have an INVITE session in progress.
496 */
497 if (g_inv) {
498
499 pj_str_t reason = pj_str("Another call is in progress");
500
501 pjsip_endpt_respond_stateless( g_endpt, rdata,
502 500, &reason,
503 NULL, NULL);
504 return PJ_TRUE;
505
506 }
507
508 /* Verify that we can handle the request. */
509 status = pjsip_inv_verify_request(rdata, &options, NULL, NULL,
510 g_endpt, NULL);
511 if (status != PJ_SUCCESS) {
512
513 pj_str_t reason = pj_str("Sorry Simple UA can not handle this INVITE");
514
515 pjsip_endpt_respond_stateless( g_endpt, rdata,
516 500, &reason,
517 NULL, NULL);
518 return PJ_TRUE;
519 }
520
521 /*
522 * Create UAS dialog.
523 */
524 status = pjsip_dlg_create_uas( pjsip_ua_instance(),
525 rdata,
526 NULL, /* contact */
527 &dlg);
528 if (status != PJ_SUCCESS) {
529 pjsip_endpt_respond_stateless(g_endpt, rdata, 500, NULL,
530 NULL, NULL);
531 return PJ_TRUE;
532 }
533
534 /*
535 * Get media capability from media endpoint:
536 */
537
538 status = pjmedia_endpt_create_sdp( g_med_endpt, rdata->tp_info.pool, 1,
539 &g_med_skinfo,
540 &local_sdp);
541 PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE);
542
Benny Prijono53fde132006-03-17 19:41:19 +0000543
Benny Prijono6107a002006-03-17 18:01:27 +0000544 /*
Benny Prijono53fde132006-03-17 19:41:19 +0000545 * Create invite session, and pass both the UAS dialog and the SDP
546 * capability to the session.
Benny Prijono6107a002006-03-17 18:01:27 +0000547 */
548 status = pjsip_inv_create_uas( dlg, rdata, local_sdp, 0, &g_inv);
549 PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE);
550
Benny Prijono53fde132006-03-17 19:41:19 +0000551
Benny Prijono6107a002006-03-17 18:01:27 +0000552 /*
553 * Initially send 180 response.
Benny Prijono53fde132006-03-17 19:41:19 +0000554 *
555 * The very first response to an INVITE must be created with
556 * pjsip_inv_initial_answer(). Subsequent responses to the same
557 * transaction MUST use pjsip_inv_answer().
Benny Prijono6107a002006-03-17 18:01:27 +0000558 */
559 status = pjsip_inv_initial_answer(g_inv, rdata,
560 180,
561 NULL, NULL, &tdata);
562 PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE);
563
Benny Prijono53fde132006-03-17 19:41:19 +0000564
565 /* Send the 180 response. */
Benny Prijono6107a002006-03-17 18:01:27 +0000566 status = pjsip_inv_send_msg(g_inv, tdata);
567 PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE);
568
Benny Prijono53fde132006-03-17 19:41:19 +0000569
Benny Prijono6107a002006-03-17 18:01:27 +0000570 /*
Benny Prijono53fde132006-03-17 19:41:19 +0000571 * Now create 200 response.
Benny Prijono6107a002006-03-17 18:01:27 +0000572 */
573 status = pjsip_inv_answer( g_inv,
574 200, NULL, /* st_code and st_text */
575 NULL, /* SDP already specified */
576 &tdata);
577 PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE);
578
579 /*
580 * Send the 200 response.
581 */
582 status = pjsip_inv_send_msg(g_inv, tdata);
583 PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE);
584
Benny Prijono53fde132006-03-17 19:41:19 +0000585
586 /* Done.
587 * When the call is disconnected, it will be reported via the callback.
588 */
589
Benny Prijono6107a002006-03-17 18:01:27 +0000590 return PJ_TRUE;
591}
592
593
Benny Prijono53fde132006-03-17 19:41:19 +0000594
595/*
596 * Callback when SDP negotiation has completed.
597 * We are interested with this callback because we want to start media
598 * as soon as SDP negotiation is completed.
599 */
600static void call_on_media_update( pjsip_inv_session *inv,
601 pj_status_t status)
602{
Benny Prijono8befd9f2006-05-13 22:46:23 +0000603 pjmedia_session_info sess_info;
Benny Prijono53fde132006-03-17 19:41:19 +0000604 const pjmedia_sdp_session *local_sdp;
605 const pjmedia_sdp_session *remote_sdp;
606 pjmedia_port *media_port;
607
608 if (status != PJ_SUCCESS) {
609
610 app_perror(THIS_FILE, "SDP negotiation has failed", status);
611
612 /* Here we should disconnect call if we're not in the middle
613 * of initializing an UAS dialog and if this is not a re-INVITE.
614 */
615 return;
616 }
617
618 /* Get local and remote SDP.
619 * We need both SDPs to create a media session.
620 */
621 status = pjmedia_sdp_neg_get_active_local(inv->neg, &local_sdp);
622
623 status = pjmedia_sdp_neg_get_active_remote(inv->neg, &remote_sdp);
624
625
Benny Prijono8befd9f2006-05-13 22:46:23 +0000626 /* Create session info based on the two SDPs.
627 * We only support one stream per session for now.
628 */
Benny Prijonob04c9e02006-05-17 17:17:39 +0000629 status = pjmedia_session_info_from_sdp(inv->dlg->pool, g_med_endpt,
630 1, &sess_info,
Benny Prijono8befd9f2006-05-13 22:46:23 +0000631 local_sdp, remote_sdp);
632 if (status != PJ_SUCCESS) {
633 app_perror( THIS_FILE, "Unable to create media session", status);
634 return;
635 }
636
637 /* If required, we can also change some settings in the session info,
638 * (such as jitter buffer settings, codec settings, etc) before we
639 * create the session.
640 */
641
Benny Prijono53fde132006-03-17 19:41:19 +0000642 /* Create new media session, passing the two SDPs, and also the
643 * media socket that we created earlier.
644 * The media session is active immediately.
645 */
Benny Prijono8befd9f2006-05-13 22:46:23 +0000646 status = pjmedia_session_create( g_med_endpt, &sess_info,
Benny Prijonob04c9e02006-05-17 17:17:39 +0000647 &g_med_transport, NULL, &g_med_session );
Benny Prijono53fde132006-03-17 19:41:19 +0000648 if (status != PJ_SUCCESS) {
649 app_perror( THIS_FILE, "Unable to create media session", status);
650 return;
651 }
652
653
654 /* Get the media port interface of the first stream in the session.
655 * Media port interface is basicly a struct containing get_frame() and
656 * put_frame() function. With this media port interface, we can attach
657 * the port interface to conference bridge, or directly to a sound
658 * player/recorder device.
659 */
660 pjmedia_session_get_port(g_med_session, 0, &media_port);
661
662
663
664 /* Create a sound Player device and connect the media port to the
665 * sound device.
666 */
667 status = pjmedia_snd_port_create_player(
668 inv->pool, /* pool */
669 -1, /* sound dev id */
Benny Prijono15953012006-04-27 22:37:08 +0000670 media_port->info.clock_rate, /* clock rate */
Benny Prijono53fde132006-03-17 19:41:19 +0000671 media_port->info.channel_count, /* channel count */
672 media_port->info.samples_per_frame, /* samples per frame*/
673 media_port->info.bits_per_sample, /* bits per sample */
674 0, /* options */
675 &g_snd_player);
676 if (status != PJ_SUCCESS) {
677 app_perror( THIS_FILE, "Unable to create sound player", status);
678 PJ_LOG(3,(THIS_FILE, "%d %d %d %d",
Benny Prijono15953012006-04-27 22:37:08 +0000679 media_port->info.clock_rate, /* clock rate */
Benny Prijono53fde132006-03-17 19:41:19 +0000680 media_port->info.channel_count, /* channel count */
681 media_port->info.samples_per_frame, /* samples per frame*/
682 media_port->info.bits_per_sample /* bits per sample */
683 ));
684 return;
685 }
686
687 status = pjmedia_snd_port_connect(g_snd_player, media_port);
688
689
690 /* Create a sound recorder device and connect the media port to the
691 * sound device.
692 */
693 status = pjmedia_snd_port_create_rec(
694 inv->pool, /* pool */
695 -1, /* sound dev id */
Benny Prijono15953012006-04-27 22:37:08 +0000696 media_port->info.clock_rate, /* clock rate */
Benny Prijono53fde132006-03-17 19:41:19 +0000697 media_port->info.channel_count, /* channel count */
698 media_port->info.samples_per_frame, /* samples per frame*/
699 media_port->info.bits_per_sample, /* bits per sample */
700 0, /* options */
701 &g_snd_rec);
702 if (status != PJ_SUCCESS) {
703 app_perror( THIS_FILE, "Unable to create sound recorder", status);
704 return;
705 }
706
707 status = pjmedia_snd_port_connect(g_snd_rec, media_port);
708
709 /* Done with media. */
710}
711
712