blob: b2881906599d0ecef4957106ca815bcb09a0b406 [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 */
Benny Prijonod8179652008-01-23 20:39:07 +0000288 pjmedia_transport_get_info(g_med_transport, &g_med_skinfo);
Benny Prijonoc1e263f2006-08-09 13:53:59 +0000289
290
Benny Prijono6107a002006-03-17 18:01:27 +0000291 /*
292 * If URL is specified, then make call immediately.
293 */
294 if (argc > 1) {
295 char temp[80];
296 pj_str_t dst_uri = pj_str(argv[1]);
297 pj_str_t local_uri;
298 pjsip_dialog *dlg;
299 pjmedia_sdp_session *local_sdp;
300 pjsip_tx_data *tdata;
301
302 pj_ansi_sprintf(temp, "sip:simpleuac@%s", pjsip_endpt_name(g_endpt)->ptr);
303 local_uri = pj_str(temp);
304
305 /* Create UAC dialog */
306 status = pjsip_dlg_create_uac( pjsip_ua_instance(),
307 &local_uri, /* local URI */
308 NULL, /* local Contact */
309 &dst_uri, /* remote URI */
310 &dst_uri, /* remote target */
311 &dlg); /* dialog */
312 if (status != PJ_SUCCESS) {
313 app_perror(THIS_FILE, "Unable to create UAC dialog", status);
314 return 1;
315 }
316
Benny Prijono0a968362006-03-18 12:29:01 +0000317 /* If we expect the outgoing INVITE to be challenged, then we should
318 * put the credentials in the dialog here, with something like this:
319 *
320 {
321 pjsip_cred_info cred[1];
322
323 cred[0].realm = pj_str("sip.server.realm");
324 cred[0].username = pj_str("theuser");
325 cred[0].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD;
326 cred[0].data = pj_str("thepassword");
327
328 pjsip_auth_clt_set_credentials( &dlg->auth_sess, 1, cred);
329 }
330 *
331 */
332
333
Benny Prijonodf912082007-10-30 16:41:45 +0000334 /* Get the SDP body to be put in the outgoing INVITE, by asking
335 * media endpoint to create one for us. The SDP will contain all
336 * codecs that have been registered to it (in this case, only
337 * PCMA and PCMU), plus telephony event.
338 */
339 status = pjmedia_endpt_create_sdp( g_med_endpt, /* the media endpt */
340 dlg->pool, /* pool. */
341 1, /* # of streams */
342 &g_med_skinfo, /* RTP sock info */
343 &local_sdp); /* the SDP result */
344 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
345
346
347
348 /* Create the INVITE session, and pass the SDP returned earlier
349 * as the session's initial capability.
350 */
351 status = pjsip_inv_create_uac( dlg, local_sdp, 0, &g_inv);
352 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
353
Benny Prijono0a968362006-03-18 12:29:01 +0000354 /* If we want the initial INVITE to travel to specific SIP proxies,
355 * then we should put the initial dialog's route set here. The final
356 * route set will be updated once a dialog has been established.
357 * To set the dialog's initial route set, we do it with something
358 * like this:
359 *
360 {
361 pjsip_route_hdr route_set;
362 pjsip_route_hdr *route;
363 const pj_str_t hname = { "Route", 5 };
364 char *uri = "sip:proxy.server;lr";
365
366 pj_list_init(&route_set);
367
368 route = pjsip_parse_hdr( dlg->pool, &hname,
369 uri, strlen(uri),
370 NULL);
371 PJ_ASSERT_RETURN(route != NULL, 1);
372 pj_list_push_back(&route_set, route);
373
374 pjsip_dlg_set_route_set(dlg, &route_set);
375 }
376 *
377 * Note that Route URI SHOULD have an ";lr" parameter!
378 */
379
Benny Prijono53fde132006-03-17 19:41:19 +0000380 /* Create initial INVITE request.
381 * This INVITE request will contain a perfectly good request and
382 * an SDP body as well.
383 */
Benny Prijono6107a002006-03-17 18:01:27 +0000384 status = pjsip_inv_invite(g_inv, &tdata);
385 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
386
Benny Prijono53fde132006-03-17 19:41:19 +0000387
388
389 /* Send initial INVITE request.
390 * From now on, the invite session's state will be reported to us
391 * via the invite session callbacks.
392 */
Benny Prijono6107a002006-03-17 18:01:27 +0000393 status = pjsip_inv_send_msg(g_inv, tdata);
394 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
395
Benny Prijono53fde132006-03-17 19:41:19 +0000396
Benny Prijono6107a002006-03-17 18:01:27 +0000397 } else {
Benny Prijono53fde132006-03-17 19:41:19 +0000398
399 /* No URL to make call to */
400
Benny Prijono6107a002006-03-17 18:01:27 +0000401 PJ_LOG(3,(THIS_FILE, "Ready to accept incoming calls..."));
402 }
403
404
405 /* Loop until one call is completed */
406 for (;!g_complete;) {
407 pj_time_val timeout = {0, 10};
408 pjsip_endpt_handle_events(g_endpt, &timeout);
409 }
410
Benny Prijonobc797312006-03-24 20:44:27 +0000411 /* On exit, dump current memory usage: */
412 dump_pool_usage(THIS_FILE, &cp);
Benny Prijono0a968362006-03-18 12:29:01 +0000413
Benny Prijono6107a002006-03-17 18:01:27 +0000414 return 0;
415}
416
417
418
419/*
420 * Callback when INVITE session state has changed.
Benny Prijono53fde132006-03-17 19:41:19 +0000421 * This callback is registered when the invite session module is initialized.
422 * We mostly want to know when the invite session has been disconnected,
423 * so that we can quit the application.
Benny Prijono6107a002006-03-17 18:01:27 +0000424 */
425static void call_on_state_changed( pjsip_inv_session *inv,
426 pjsip_event *e)
427{
Benny Prijono15953012006-04-27 22:37:08 +0000428 PJ_UNUSED_ARG(e);
429
Benny Prijono6107a002006-03-17 18:01:27 +0000430 if (inv->state == PJSIP_INV_STATE_DISCONNECTED) {
431
432 PJ_LOG(3,(THIS_FILE, "Call DISCONNECTED [reason=%d (%s)]",
433 inv->cause,
434 pjsip_get_status_text(inv->cause)->ptr));
435
436 PJ_LOG(3,(THIS_FILE, "One call completed, application quitting..."));
437 g_complete = 1;
438
439 } else {
440
441 PJ_LOG(3,(THIS_FILE, "Call state changed to %s",
442 pjsip_inv_state_name(inv->state)));
443
444 }
445}
446
Benny Prijono6107a002006-03-17 18:01:27 +0000447
Benny Prijono53fde132006-03-17 19:41:19 +0000448/* This callback is called when dialog has forked. */
Benny Prijono6107a002006-03-17 18:01:27 +0000449static void call_on_forked(pjsip_inv_session *inv, pjsip_event *e)
450{
Benny Prijono53fde132006-03-17 19:41:19 +0000451 /* To be done... */
Benny Prijono15953012006-04-27 22:37:08 +0000452 PJ_UNUSED_ARG(inv);
453 PJ_UNUSED_ARG(e);
Benny Prijono6107a002006-03-17 18:01:27 +0000454}
455
Benny Prijono53fde132006-03-17 19:41:19 +0000456
Benny Prijono6107a002006-03-17 18:01:27 +0000457/*
458 * Callback when incoming requests outside any transactions and any
Benny Prijono53fde132006-03-17 19:41:19 +0000459 * dialogs are received. We're only interested to hande incoming INVITE
460 * request, and we'll reject any other requests with 500 response.
Benny Prijono6107a002006-03-17 18:01:27 +0000461 */
462static pj_bool_t on_rx_request( pjsip_rx_data *rdata )
463{
464 pjsip_dialog *dlg;
465 pjmedia_sdp_session *local_sdp;
466 pjsip_tx_data *tdata;
467 unsigned options = 0;
468 pj_status_t status;
469
Benny Prijono53fde132006-03-17 19:41:19 +0000470
Benny Prijono6107a002006-03-17 18:01:27 +0000471 /*
472 * Respond (statelessly) any non-INVITE requests with 500
473 */
474 if (rdata->msg_info.msg->line.req.method.id != PJSIP_INVITE_METHOD) {
475
Benny Prijono53932c02007-02-13 18:51:44 +0000476 if (rdata->msg_info.msg->line.req.method.id != PJSIP_ACK_METHOD) {
477 pj_str_t reason = pj_str("Simple UA unable to handle "
478 "this request");
Benny Prijono6107a002006-03-17 18:01:27 +0000479
Benny Prijono53932c02007-02-13 18:51:44 +0000480 pjsip_endpt_respond_stateless( g_endpt, rdata,
481 500, &reason,
482 NULL, NULL);
483 }
Benny Prijono6107a002006-03-17 18:01:27 +0000484 return PJ_TRUE;
485 }
486
Benny Prijono53fde132006-03-17 19:41:19 +0000487
Benny Prijono6107a002006-03-17 18:01:27 +0000488 /*
489 * Reject INVITE if we already have an INVITE session in progress.
490 */
491 if (g_inv) {
492
493 pj_str_t reason = pj_str("Another call is in progress");
494
495 pjsip_endpt_respond_stateless( g_endpt, rdata,
496 500, &reason,
497 NULL, NULL);
498 return PJ_TRUE;
499
500 }
501
502 /* Verify that we can handle the request. */
503 status = pjsip_inv_verify_request(rdata, &options, NULL, NULL,
504 g_endpt, NULL);
505 if (status != PJ_SUCCESS) {
506
507 pj_str_t reason = pj_str("Sorry Simple UA can not handle this INVITE");
508
509 pjsip_endpt_respond_stateless( g_endpt, rdata,
510 500, &reason,
511 NULL, NULL);
512 return PJ_TRUE;
513 }
514
515 /*
516 * Create UAS dialog.
517 */
518 status = pjsip_dlg_create_uas( pjsip_ua_instance(),
519 rdata,
520 NULL, /* contact */
521 &dlg);
522 if (status != PJ_SUCCESS) {
523 pjsip_endpt_respond_stateless(g_endpt, rdata, 500, NULL,
524 NULL, NULL);
525 return PJ_TRUE;
526 }
527
528 /*
529 * Get media capability from media endpoint:
530 */
531
532 status = pjmedia_endpt_create_sdp( g_med_endpt, rdata->tp_info.pool, 1,
533 &g_med_skinfo,
534 &local_sdp);
535 PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE);
536
Benny Prijono53fde132006-03-17 19:41:19 +0000537
Benny Prijono6107a002006-03-17 18:01:27 +0000538 /*
Benny Prijono53fde132006-03-17 19:41:19 +0000539 * Create invite session, and pass both the UAS dialog and the SDP
540 * capability to the session.
Benny Prijono6107a002006-03-17 18:01:27 +0000541 */
542 status = pjsip_inv_create_uas( dlg, rdata, local_sdp, 0, &g_inv);
543 PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE);
544
Benny Prijono53fde132006-03-17 19:41:19 +0000545
Benny Prijono6107a002006-03-17 18:01:27 +0000546 /*
547 * Initially send 180 response.
Benny Prijono53fde132006-03-17 19:41:19 +0000548 *
549 * The very first response to an INVITE must be created with
550 * pjsip_inv_initial_answer(). Subsequent responses to the same
551 * transaction MUST use pjsip_inv_answer().
Benny Prijono6107a002006-03-17 18:01:27 +0000552 */
553 status = pjsip_inv_initial_answer(g_inv, rdata,
554 180,
555 NULL, NULL, &tdata);
556 PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE);
557
Benny Prijono53fde132006-03-17 19:41:19 +0000558
559 /* Send the 180 response. */
Benny Prijono6107a002006-03-17 18:01:27 +0000560 status = pjsip_inv_send_msg(g_inv, tdata);
561 PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE);
562
Benny Prijono53fde132006-03-17 19:41:19 +0000563
Benny Prijono6107a002006-03-17 18:01:27 +0000564 /*
Benny Prijono53fde132006-03-17 19:41:19 +0000565 * Now create 200 response.
Benny Prijono6107a002006-03-17 18:01:27 +0000566 */
567 status = pjsip_inv_answer( g_inv,
568 200, NULL, /* st_code and st_text */
569 NULL, /* SDP already specified */
570 &tdata);
571 PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE);
572
573 /*
574 * Send the 200 response.
575 */
576 status = pjsip_inv_send_msg(g_inv, tdata);
577 PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE);
578
Benny Prijono53fde132006-03-17 19:41:19 +0000579
580 /* Done.
581 * When the call is disconnected, it will be reported via the callback.
582 */
583
Benny Prijono6107a002006-03-17 18:01:27 +0000584 return PJ_TRUE;
585}
586
587
Benny Prijono53fde132006-03-17 19:41:19 +0000588
589/*
590 * Callback when SDP negotiation has completed.
591 * We are interested with this callback because we want to start media
592 * as soon as SDP negotiation is completed.
593 */
594static void call_on_media_update( pjsip_inv_session *inv,
595 pj_status_t status)
596{
Benny Prijono8befd9f2006-05-13 22:46:23 +0000597 pjmedia_session_info sess_info;
Benny Prijono53fde132006-03-17 19:41:19 +0000598 const pjmedia_sdp_session *local_sdp;
599 const pjmedia_sdp_session *remote_sdp;
600 pjmedia_port *media_port;
601
602 if (status != PJ_SUCCESS) {
603
604 app_perror(THIS_FILE, "SDP negotiation has failed", status);
605
606 /* Here we should disconnect call if we're not in the middle
607 * of initializing an UAS dialog and if this is not a re-INVITE.
608 */
609 return;
610 }
611
612 /* Get local and remote SDP.
613 * We need both SDPs to create a media session.
614 */
615 status = pjmedia_sdp_neg_get_active_local(inv->neg, &local_sdp);
616
617 status = pjmedia_sdp_neg_get_active_remote(inv->neg, &remote_sdp);
618
619
Benny Prijono8befd9f2006-05-13 22:46:23 +0000620 /* Create session info based on the two SDPs.
621 * We only support one stream per session for now.
622 */
Benny Prijonob04c9e02006-05-17 17:17:39 +0000623 status = pjmedia_session_info_from_sdp(inv->dlg->pool, g_med_endpt,
624 1, &sess_info,
Benny Prijono8befd9f2006-05-13 22:46:23 +0000625 local_sdp, remote_sdp);
626 if (status != PJ_SUCCESS) {
627 app_perror( THIS_FILE, "Unable to create media session", status);
628 return;
629 }
630
631 /* If required, we can also change some settings in the session info,
632 * (such as jitter buffer settings, codec settings, etc) before we
633 * create the session.
634 */
635
Benny Prijono53fde132006-03-17 19:41:19 +0000636 /* Create new media session, passing the two SDPs, and also the
637 * media socket that we created earlier.
638 * The media session is active immediately.
639 */
Benny Prijono8befd9f2006-05-13 22:46:23 +0000640 status = pjmedia_session_create( g_med_endpt, &sess_info,
Benny Prijonob04c9e02006-05-17 17:17:39 +0000641 &g_med_transport, NULL, &g_med_session );
Benny Prijono53fde132006-03-17 19:41:19 +0000642 if (status != PJ_SUCCESS) {
643 app_perror( THIS_FILE, "Unable to create media session", status);
644 return;
645 }
646
647
648 /* Get the media port interface of the first stream in the session.
649 * Media port interface is basicly a struct containing get_frame() and
650 * put_frame() function. With this media port interface, we can attach
651 * the port interface to conference bridge, or directly to a sound
652 * player/recorder device.
653 */
654 pjmedia_session_get_port(g_med_session, 0, &media_port);
655
656
657
658 /* Create a sound Player device and connect the media port to the
659 * sound device.
660 */
661 status = pjmedia_snd_port_create_player(
662 inv->pool, /* pool */
663 -1, /* sound dev id */
Benny Prijono15953012006-04-27 22:37:08 +0000664 media_port->info.clock_rate, /* clock rate */
Benny Prijono53fde132006-03-17 19:41:19 +0000665 media_port->info.channel_count, /* channel count */
666 media_port->info.samples_per_frame, /* samples per frame*/
667 media_port->info.bits_per_sample, /* bits per sample */
668 0, /* options */
669 &g_snd_player);
670 if (status != PJ_SUCCESS) {
671 app_perror( THIS_FILE, "Unable to create sound player", status);
672 PJ_LOG(3,(THIS_FILE, "%d %d %d %d",
Benny Prijono15953012006-04-27 22:37:08 +0000673 media_port->info.clock_rate, /* clock rate */
Benny Prijono53fde132006-03-17 19:41:19 +0000674 media_port->info.channel_count, /* channel count */
675 media_port->info.samples_per_frame, /* samples per frame*/
676 media_port->info.bits_per_sample /* bits per sample */
677 ));
678 return;
679 }
680
681 status = pjmedia_snd_port_connect(g_snd_player, media_port);
682
683
684 /* Create a sound recorder device and connect the media port to the
685 * sound device.
686 */
687 status = pjmedia_snd_port_create_rec(
688 inv->pool, /* pool */
689 -1, /* sound dev id */
Benny Prijono15953012006-04-27 22:37:08 +0000690 media_port->info.clock_rate, /* clock rate */
Benny Prijono53fde132006-03-17 19:41:19 +0000691 media_port->info.channel_count, /* channel count */
692 media_port->info.samples_per_frame, /* samples per frame*/
693 media_port->info.bits_per_sample, /* bits per sample */
694 0, /* options */
695 &g_snd_rec);
696 if (status != PJ_SUCCESS) {
697 app_perror( THIS_FILE, "Unable to create sound recorder", status);
698 return;
699 }
700
701 status = pjmedia_snd_port_connect(g_snd_rec, media_port);
702
703 /* Done with media. */
704}
705
706