blob: d7b6508670be8bb02f142058cf7d587e5baef661 [file] [log] [blame]
Benny Prijono53fde132006-03-17 19:41:19 +00001/* $Id$ */
2/*
3 * Copyright (C) 2003-2006 Benny Prijono <benny@prijono.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
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
184 addr.sin_family = PJ_AF_INET;
185 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
238
239 /*
Benny Prijono53fde132006-03-17 19:41:19 +0000240 * Register our module to receive incoming requests.
Benny Prijono6107a002006-03-17 18:01:27 +0000241 */
242 status = pjsip_endpt_register_module( g_endpt, &mod_simpleua);
243 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
244
245
246 /*
Benny Prijono53fde132006-03-17 19:41:19 +0000247 * Initialize media endpoint.
248 * This will implicitly initialize PJMEDIA too.
Benny Prijono6107a002006-03-17 18:01:27 +0000249 */
Benny Prijono275fd682006-03-22 11:59:11 +0000250 status = pjmedia_endpt_create(&cp.factory, NULL, 1, &g_med_endpt);
Benny Prijono6107a002006-03-17 18:01:27 +0000251 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
252
253 /*
254 * Add PCMA/PCMU codec to the media endpoint.
255 */
256 status = pjmedia_codec_g711_init(g_med_endpt);
257 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
258
Benny Prijonoc1e263f2006-08-09 13:53:59 +0000259
Benny Prijono6107a002006-03-17 18:01:27 +0000260 /*
Benny Prijonoc1e263f2006-08-09 13:53:59 +0000261 * Create media transport used to send/receive RTP/RTCP socket.
262 * One media transport is needed for each call. Application may
263 * opt to re-use the same media transport for subsequent calls.
Benny Prijono6107a002006-03-17 18:01:27 +0000264 */
Benny Prijonoc1e263f2006-08-09 13:53:59 +0000265 status = pjmedia_transport_udp_create(g_med_endpt, NULL, 4000, 0,
266 &g_med_transport);
Benny Prijonob04c9e02006-05-17 17:17:39 +0000267 if (status != PJ_SUCCESS) {
268 app_perror(THIS_FILE, "Unable to create media transport", status);
269 return 1;
270 }
271
Benny Prijonoc1e263f2006-08-09 13:53:59 +0000272 /*
273 * Get socket info (address, port) of the media transport. We will
274 * need this info to create SDP (i.e. the address and port info in
275 * the SDP).
276 */
277 {
278 pjmedia_transport_udp_info udp_info;
279
280 pjmedia_transport_udp_get_info(g_med_transport, &udp_info);
281 pj_memcpy(&g_med_skinfo, &udp_info.skinfo,
282 sizeof(pjmedia_sock_info));
283 }
284
285
Benny Prijono6107a002006-03-17 18:01:27 +0000286 /*
287 * If URL is specified, then make call immediately.
288 */
289 if (argc > 1) {
290 char temp[80];
291 pj_str_t dst_uri = pj_str(argv[1]);
292 pj_str_t local_uri;
293 pjsip_dialog *dlg;
294 pjmedia_sdp_session *local_sdp;
295 pjsip_tx_data *tdata;
296
297 pj_ansi_sprintf(temp, "sip:simpleuac@%s", pjsip_endpt_name(g_endpt)->ptr);
298 local_uri = pj_str(temp);
299
300 /* Create UAC dialog */
301 status = pjsip_dlg_create_uac( pjsip_ua_instance(),
302 &local_uri, /* local URI */
303 NULL, /* local Contact */
304 &dst_uri, /* remote URI */
305 &dst_uri, /* remote target */
306 &dlg); /* dialog */
307 if (status != PJ_SUCCESS) {
308 app_perror(THIS_FILE, "Unable to create UAC dialog", status);
309 return 1;
310 }
311
Benny Prijono0a968362006-03-18 12:29:01 +0000312 /* If we expect the outgoing INVITE to be challenged, then we should
313 * put the credentials in the dialog here, with something like this:
314 *
315 {
316 pjsip_cred_info cred[1];
317
318 cred[0].realm = pj_str("sip.server.realm");
319 cred[0].username = pj_str("theuser");
320 cred[0].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD;
321 cred[0].data = pj_str("thepassword");
322
323 pjsip_auth_clt_set_credentials( &dlg->auth_sess, 1, cred);
324 }
325 *
326 */
327
328
329 /* If we want the initial INVITE to travel to specific SIP proxies,
330 * then we should put the initial dialog's route set here. The final
331 * route set will be updated once a dialog has been established.
332 * To set the dialog's initial route set, we do it with something
333 * like this:
334 *
335 {
336 pjsip_route_hdr route_set;
337 pjsip_route_hdr *route;
338 const pj_str_t hname = { "Route", 5 };
339 char *uri = "sip:proxy.server;lr";
340
341 pj_list_init(&route_set);
342
343 route = pjsip_parse_hdr( dlg->pool, &hname,
344 uri, strlen(uri),
345 NULL);
346 PJ_ASSERT_RETURN(route != NULL, 1);
347 pj_list_push_back(&route_set, route);
348
349 pjsip_dlg_set_route_set(dlg, &route_set);
350 }
351 *
352 * Note that Route URI SHOULD have an ";lr" parameter!
353 */
354
355
Benny Prijono53fde132006-03-17 19:41:19 +0000356 /* Get the SDP body to be put in the outgoing INVITE, by asking
357 * media endpoint to create one for us. The SDP will contain all
358 * codecs that have been registered to it (in this case, only
359 * PCMA and PCMU), plus telephony event.
360 */
361 status = pjmedia_endpt_create_sdp( g_med_endpt, /* the media endpt */
362 dlg->pool, /* pool. */
363 1, /* # of streams */
364 &g_med_skinfo, /* RTP sock info */
365 &local_sdp); /* the SDP result */
Benny Prijono6107a002006-03-17 18:01:27 +0000366 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
367
368
Benny Prijono53fde132006-03-17 19:41:19 +0000369
370 /* Create the INVITE session, and pass the SDP returned earlier
371 * as the session's initial capability.
372 */
Benny Prijono6107a002006-03-17 18:01:27 +0000373 status = pjsip_inv_create_uac( dlg, local_sdp, 0, &g_inv);
374 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
375
376
Benny Prijono53fde132006-03-17 19:41:19 +0000377 /* Create initial INVITE request.
378 * This INVITE request will contain a perfectly good request and
379 * an SDP body as well.
380 */
Benny Prijono6107a002006-03-17 18:01:27 +0000381 status = pjsip_inv_invite(g_inv, &tdata);
382 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
383
Benny Prijono53fde132006-03-17 19:41:19 +0000384
385
386 /* Send initial INVITE request.
387 * From now on, the invite session's state will be reported to us
388 * via the invite session callbacks.
389 */
Benny Prijono6107a002006-03-17 18:01:27 +0000390 status = pjsip_inv_send_msg(g_inv, tdata);
391 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
392
Benny Prijono53fde132006-03-17 19:41:19 +0000393
Benny Prijono6107a002006-03-17 18:01:27 +0000394 } else {
Benny Prijono53fde132006-03-17 19:41:19 +0000395
396 /* No URL to make call to */
397
Benny Prijono6107a002006-03-17 18:01:27 +0000398 PJ_LOG(3,(THIS_FILE, "Ready to accept incoming calls..."));
399 }
400
401
402 /* Loop until one call is completed */
403 for (;!g_complete;) {
404 pj_time_val timeout = {0, 10};
405 pjsip_endpt_handle_events(g_endpt, &timeout);
406 }
407
Benny Prijonobc797312006-03-24 20:44:27 +0000408 /* On exit, dump current memory usage: */
409 dump_pool_usage(THIS_FILE, &cp);
Benny Prijono0a968362006-03-18 12:29:01 +0000410
Benny Prijono6107a002006-03-17 18:01:27 +0000411 return 0;
412}
413
414
415
416/*
417 * Callback when INVITE session state has changed.
Benny Prijono53fde132006-03-17 19:41:19 +0000418 * This callback is registered when the invite session module is initialized.
419 * We mostly want to know when the invite session has been disconnected,
420 * so that we can quit the application.
Benny Prijono6107a002006-03-17 18:01:27 +0000421 */
422static void call_on_state_changed( pjsip_inv_session *inv,
423 pjsip_event *e)
424{
Benny Prijono15953012006-04-27 22:37:08 +0000425 PJ_UNUSED_ARG(e);
426
Benny Prijono6107a002006-03-17 18:01:27 +0000427 if (inv->state == PJSIP_INV_STATE_DISCONNECTED) {
428
429 PJ_LOG(3,(THIS_FILE, "Call DISCONNECTED [reason=%d (%s)]",
430 inv->cause,
431 pjsip_get_status_text(inv->cause)->ptr));
432
433 PJ_LOG(3,(THIS_FILE, "One call completed, application quitting..."));
434 g_complete = 1;
435
436 } else {
437
438 PJ_LOG(3,(THIS_FILE, "Call state changed to %s",
439 pjsip_inv_state_name(inv->state)));
440
441 }
442}
443
Benny Prijono6107a002006-03-17 18:01:27 +0000444
Benny Prijono53fde132006-03-17 19:41:19 +0000445/* This callback is called when dialog has forked. */
Benny Prijono6107a002006-03-17 18:01:27 +0000446static void call_on_forked(pjsip_inv_session *inv, pjsip_event *e)
447{
Benny Prijono53fde132006-03-17 19:41:19 +0000448 /* To be done... */
Benny Prijono15953012006-04-27 22:37:08 +0000449 PJ_UNUSED_ARG(inv);
450 PJ_UNUSED_ARG(e);
Benny Prijono6107a002006-03-17 18:01:27 +0000451}
452
Benny Prijono53fde132006-03-17 19:41:19 +0000453
Benny Prijono6107a002006-03-17 18:01:27 +0000454/*
455 * Callback when incoming requests outside any transactions and any
Benny Prijono53fde132006-03-17 19:41:19 +0000456 * dialogs are received. We're only interested to hande incoming INVITE
457 * request, and we'll reject any other requests with 500 response.
Benny Prijono6107a002006-03-17 18:01:27 +0000458 */
459static pj_bool_t on_rx_request( pjsip_rx_data *rdata )
460{
461 pjsip_dialog *dlg;
462 pjmedia_sdp_session *local_sdp;
463 pjsip_tx_data *tdata;
464 unsigned options = 0;
465 pj_status_t status;
466
Benny Prijono53fde132006-03-17 19:41:19 +0000467
Benny Prijono6107a002006-03-17 18:01:27 +0000468 /*
469 * Respond (statelessly) any non-INVITE requests with 500
470 */
471 if (rdata->msg_info.msg->line.req.method.id != PJSIP_INVITE_METHOD) {
472
473 pj_str_t reason = pj_str("Simple UA unable to handle this request");
474
475 pjsip_endpt_respond_stateless( g_endpt, rdata,
476 500, &reason,
477 NULL, NULL);
478 return PJ_TRUE;
479 }
480
Benny Prijono53fde132006-03-17 19:41:19 +0000481
Benny Prijono6107a002006-03-17 18:01:27 +0000482 /*
483 * Reject INVITE if we already have an INVITE session in progress.
484 */
485 if (g_inv) {
486
487 pj_str_t reason = pj_str("Another call is in progress");
488
489 pjsip_endpt_respond_stateless( g_endpt, rdata,
490 500, &reason,
491 NULL, NULL);
492 return PJ_TRUE;
493
494 }
495
496 /* Verify that we can handle the request. */
497 status = pjsip_inv_verify_request(rdata, &options, NULL, NULL,
498 g_endpt, NULL);
499 if (status != PJ_SUCCESS) {
500
501 pj_str_t reason = pj_str("Sorry Simple UA can not handle this INVITE");
502
503 pjsip_endpt_respond_stateless( g_endpt, rdata,
504 500, &reason,
505 NULL, NULL);
506 return PJ_TRUE;
507 }
508
509 /*
510 * Create UAS dialog.
511 */
512 status = pjsip_dlg_create_uas( pjsip_ua_instance(),
513 rdata,
514 NULL, /* contact */
515 &dlg);
516 if (status != PJ_SUCCESS) {
517 pjsip_endpt_respond_stateless(g_endpt, rdata, 500, NULL,
518 NULL, NULL);
519 return PJ_TRUE;
520 }
521
522 /*
523 * Get media capability from media endpoint:
524 */
525
526 status = pjmedia_endpt_create_sdp( g_med_endpt, rdata->tp_info.pool, 1,
527 &g_med_skinfo,
528 &local_sdp);
529 PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE);
530
Benny Prijono53fde132006-03-17 19:41:19 +0000531
Benny Prijono6107a002006-03-17 18:01:27 +0000532 /*
Benny Prijono53fde132006-03-17 19:41:19 +0000533 * Create invite session, and pass both the UAS dialog and the SDP
534 * capability to the session.
Benny Prijono6107a002006-03-17 18:01:27 +0000535 */
536 status = pjsip_inv_create_uas( dlg, rdata, local_sdp, 0, &g_inv);
537 PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE);
538
Benny Prijono53fde132006-03-17 19:41:19 +0000539
Benny Prijono6107a002006-03-17 18:01:27 +0000540 /*
541 * Initially send 180 response.
Benny Prijono53fde132006-03-17 19:41:19 +0000542 *
543 * The very first response to an INVITE must be created with
544 * pjsip_inv_initial_answer(). Subsequent responses to the same
545 * transaction MUST use pjsip_inv_answer().
Benny Prijono6107a002006-03-17 18:01:27 +0000546 */
547 status = pjsip_inv_initial_answer(g_inv, rdata,
548 180,
549 NULL, NULL, &tdata);
550 PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE);
551
Benny Prijono53fde132006-03-17 19:41:19 +0000552
553 /* Send the 180 response. */
Benny Prijono6107a002006-03-17 18:01:27 +0000554 status = pjsip_inv_send_msg(g_inv, tdata);
555 PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE);
556
Benny Prijono53fde132006-03-17 19:41:19 +0000557
Benny Prijono6107a002006-03-17 18:01:27 +0000558 /*
Benny Prijono53fde132006-03-17 19:41:19 +0000559 * Now create 200 response.
Benny Prijono6107a002006-03-17 18:01:27 +0000560 */
561 status = pjsip_inv_answer( g_inv,
562 200, NULL, /* st_code and st_text */
563 NULL, /* SDP already specified */
564 &tdata);
565 PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE);
566
567 /*
568 * Send the 200 response.
569 */
570 status = pjsip_inv_send_msg(g_inv, tdata);
571 PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE);
572
Benny Prijono53fde132006-03-17 19:41:19 +0000573
574 /* Done.
575 * When the call is disconnected, it will be reported via the callback.
576 */
577
Benny Prijono6107a002006-03-17 18:01:27 +0000578 return PJ_TRUE;
579}
580
581
Benny Prijono53fde132006-03-17 19:41:19 +0000582
583/*
584 * Callback when SDP negotiation has completed.
585 * We are interested with this callback because we want to start media
586 * as soon as SDP negotiation is completed.
587 */
588static void call_on_media_update( pjsip_inv_session *inv,
589 pj_status_t status)
590{
Benny Prijono8befd9f2006-05-13 22:46:23 +0000591 pjmedia_session_info sess_info;
Benny Prijono53fde132006-03-17 19:41:19 +0000592 const pjmedia_sdp_session *local_sdp;
593 const pjmedia_sdp_session *remote_sdp;
594 pjmedia_port *media_port;
595
596 if (status != PJ_SUCCESS) {
597
598 app_perror(THIS_FILE, "SDP negotiation has failed", status);
599
600 /* Here we should disconnect call if we're not in the middle
601 * of initializing an UAS dialog and if this is not a re-INVITE.
602 */
603 return;
604 }
605
606 /* Get local and remote SDP.
607 * We need both SDPs to create a media session.
608 */
609 status = pjmedia_sdp_neg_get_active_local(inv->neg, &local_sdp);
610
611 status = pjmedia_sdp_neg_get_active_remote(inv->neg, &remote_sdp);
612
613
Benny Prijono8befd9f2006-05-13 22:46:23 +0000614 /* Create session info based on the two SDPs.
615 * We only support one stream per session for now.
616 */
Benny Prijonob04c9e02006-05-17 17:17:39 +0000617 status = pjmedia_session_info_from_sdp(inv->dlg->pool, g_med_endpt,
618 1, &sess_info,
Benny Prijono8befd9f2006-05-13 22:46:23 +0000619 local_sdp, remote_sdp);
620 if (status != PJ_SUCCESS) {
621 app_perror( THIS_FILE, "Unable to create media session", status);
622 return;
623 }
624
625 /* If required, we can also change some settings in the session info,
626 * (such as jitter buffer settings, codec settings, etc) before we
627 * create the session.
628 */
629
Benny Prijono53fde132006-03-17 19:41:19 +0000630 /* Create new media session, passing the two SDPs, and also the
631 * media socket that we created earlier.
632 * The media session is active immediately.
633 */
Benny Prijono8befd9f2006-05-13 22:46:23 +0000634 status = pjmedia_session_create( g_med_endpt, &sess_info,
Benny Prijonob04c9e02006-05-17 17:17:39 +0000635 &g_med_transport, NULL, &g_med_session );
Benny Prijono53fde132006-03-17 19:41:19 +0000636 if (status != PJ_SUCCESS) {
637 app_perror( THIS_FILE, "Unable to create media session", status);
638 return;
639 }
640
641
642 /* Get the media port interface of the first stream in the session.
643 * Media port interface is basicly a struct containing get_frame() and
644 * put_frame() function. With this media port interface, we can attach
645 * the port interface to conference bridge, or directly to a sound
646 * player/recorder device.
647 */
648 pjmedia_session_get_port(g_med_session, 0, &media_port);
649
650
651
652 /* Create a sound Player device and connect the media port to the
653 * sound device.
654 */
655 status = pjmedia_snd_port_create_player(
656 inv->pool, /* pool */
657 -1, /* sound dev id */
Benny Prijono15953012006-04-27 22:37:08 +0000658 media_port->info.clock_rate, /* clock rate */
Benny Prijono53fde132006-03-17 19:41:19 +0000659 media_port->info.channel_count, /* channel count */
660 media_port->info.samples_per_frame, /* samples per frame*/
661 media_port->info.bits_per_sample, /* bits per sample */
662 0, /* options */
663 &g_snd_player);
664 if (status != PJ_SUCCESS) {
665 app_perror( THIS_FILE, "Unable to create sound player", status);
666 PJ_LOG(3,(THIS_FILE, "%d %d %d %d",
Benny Prijono15953012006-04-27 22:37:08 +0000667 media_port->info.clock_rate, /* clock rate */
Benny Prijono53fde132006-03-17 19:41:19 +0000668 media_port->info.channel_count, /* channel count */
669 media_port->info.samples_per_frame, /* samples per frame*/
670 media_port->info.bits_per_sample /* bits per sample */
671 ));
672 return;
673 }
674
675 status = pjmedia_snd_port_connect(g_snd_player, media_port);
676
677
678 /* Create a sound recorder device and connect the media port to the
679 * sound device.
680 */
681 status = pjmedia_snd_port_create_rec(
682 inv->pool, /* pool */
683 -1, /* sound dev id */
Benny Prijono15953012006-04-27 22:37:08 +0000684 media_port->info.clock_rate, /* clock rate */
Benny Prijono53fde132006-03-17 19:41:19 +0000685 media_port->info.channel_count, /* channel count */
686 media_port->info.samples_per_frame, /* samples per frame*/
687 media_port->info.bits_per_sample, /* bits per sample */
688 0, /* options */
689 &g_snd_rec);
690 if (status != PJ_SUCCESS) {
691 app_perror( THIS_FILE, "Unable to create sound recorder", status);
692 return;
693 }
694
695 status = pjmedia_snd_port_connect(g_snd_rec, media_port);
696
697 /* Done with media. */
698}
699
700