blob: bed26711ea1010791314797a2d463d1f1dd79e99 [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
64
65/*
66 * Static variables.
67 */
Benny Prijono6107a002006-03-17 18:01:27 +000068
Benny Prijono53fde132006-03-17 19:41:19 +000069static pj_bool_t g_complete; /* Quit flag. */
70static pjsip_endpoint *g_endpt; /* SIP endpoint. */
71static pj_caching_pool cp; /* Global pool factory. */
Benny Prijono6107a002006-03-17 18:01:27 +000072
Benny Prijono53fde132006-03-17 19:41:19 +000073static pjmedia_endpt *g_med_endpt; /* Media endpoint. */
74static pjmedia_sock_info g_med_skinfo; /* Socket info for media */
Benny Prijono6107a002006-03-17 18:01:27 +000075
Benny Prijono53fde132006-03-17 19:41:19 +000076/* Call variables: */
77static pjsip_inv_session *g_inv; /* Current invite session. */
78static pjmedia_session *g_med_session; /* Call's media session. */
79static pjmedia_snd_port *g_snd_player; /* Call's sound player */
80static pjmedia_snd_port *g_snd_rec; /* Call's sound recorder. */
Benny Prijono6107a002006-03-17 18:01:27 +000081
82
83/*
Benny Prijono53fde132006-03-17 19:41:19 +000084 * Prototypes:
Benny Prijono6107a002006-03-17 18:01:27 +000085 */
Benny Prijono53fde132006-03-17 19:41:19 +000086
87/* Callback to be called when SDP negotiation is done in the call: */
Benny Prijono6107a002006-03-17 18:01:27 +000088static void call_on_media_update( pjsip_inv_session *inv,
89 pj_status_t status);
Benny Prijono53fde132006-03-17 19:41:19 +000090
91/* Callback to be called when invite session's state has changed: */
Benny Prijono6107a002006-03-17 18:01:27 +000092static void call_on_state_changed( pjsip_inv_session *inv,
93 pjsip_event *e);
Benny Prijono53fde132006-03-17 19:41:19 +000094
95/* Callback to be called when dialog has forked: */
Benny Prijono6107a002006-03-17 18:01:27 +000096static void call_on_forked(pjsip_inv_session *inv, pjsip_event *e);
Benny Prijono53fde132006-03-17 19:41:19 +000097
98/* Callback to be called to handle incoming requests outside dialogs: */
Benny Prijono6107a002006-03-17 18:01:27 +000099static pj_bool_t on_rx_request( pjsip_rx_data *rdata );
100
101
Benny Prijono53fde132006-03-17 19:41:19 +0000102
103
104/* This is a PJSIP module to be registered by application to handle
105 * incoming requests outside any dialogs/transactions. The main purpose
106 * here is to handle incoming INVITE request message, where we will
107 * create a dialog and INVITE session for it.
108 */
Benny Prijono6107a002006-03-17 18:01:27 +0000109static pjsip_module mod_simpleua =
110{
111 NULL, NULL, /* prev, next. */
112 { "mod-simpleua", 12 }, /* Name. */
113 -1, /* Id */
114 PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */
115 NULL, /* load() */
116 NULL, /* start() */
117 NULL, /* stop() */
118 NULL, /* unload() */
119 &on_rx_request, /* on_rx_request() */
120 NULL, /* on_rx_response() */
121 NULL, /* on_tx_request. */
122 NULL, /* on_tx_response() */
123 NULL, /* on_tsx_state() */
124};
125
126
127/*
Benny Prijono53fde132006-03-17 19:41:19 +0000128 * Util to display the error message for the specified error code
Benny Prijono6107a002006-03-17 18:01:27 +0000129 */
130static int app_perror( const char *sender, const char *title,
131 pj_status_t status)
132{
133 char errmsg[PJ_ERR_MSG_SIZE];
134
135 pj_strerror(status, errmsg, sizeof(errmsg));
136
137 PJ_LOG(1,(sender, "%s: %s [code=%d]", title, errmsg, status));
138 return 1;
139}
140
141
142/*
143 * main()
Benny Prijono53fde132006-03-17 19:41:19 +0000144 *
145 * If called with argument, treat argument as SIP URL to be called.
146 * Otherwise wait for incoming calls.
Benny Prijono6107a002006-03-17 18:01:27 +0000147 */
148int main(int argc, char *argv[])
149{
150 pj_status_t status;
151
Benny Prijono53fde132006-03-17 19:41:19 +0000152 /* Must init PJLIB first: */
Benny Prijono6107a002006-03-17 18:01:27 +0000153 status = pj_init();
154 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
155
Benny Prijono53fde132006-03-17 19:41:19 +0000156
157 /* Then init PJLIB-UTIL: */
Benny Prijono6107a002006-03-17 18:01:27 +0000158 status = pjlib_util_init();
159 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
160
161
Benny Prijono53fde132006-03-17 19:41:19 +0000162 /* Must create a pool factory before we can allocate any memory. */
Benny Prijono6107a002006-03-17 18:01:27 +0000163 pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0);
164
Benny Prijono53fde132006-03-17 19:41:19 +0000165
Benny Prijono6107a002006-03-17 18:01:27 +0000166 /* Create global endpoint: */
167 {
168 const pj_str_t *hostname;
169 const char *endpt_name;
170
171 /* Endpoint MUST be assigned a globally unique name.
172 * The name will be used as the hostname in Warning header.
173 */
174
175 /* For this implementation, we'll use hostname for simplicity */
176 hostname = pj_gethostname();
177 endpt_name = hostname->ptr;
178
179 /* Create the endpoint: */
180
181 status = pjsip_endpt_create(&cp.factory, endpt_name,
182 &g_endpt);
183 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
184 }
185
Benny Prijono53fde132006-03-17 19:41:19 +0000186
Benny Prijono6107a002006-03-17 18:01:27 +0000187 /*
Benny Prijono53fde132006-03-17 19:41:19 +0000188 * Add UDP transport, with hard-coded port
189 * Alternatively, application can use pjsip_udp_transport_attach() to
190 * start UDP transport, if it already has an UDP socket (e.g. after it
191 * resolves the address with STUN).
Benny Prijono6107a002006-03-17 18:01:27 +0000192 */
193 {
194 pj_sockaddr_in addr;
195
196 addr.sin_family = PJ_AF_INET;
197 addr.sin_addr.s_addr = 0;
198 addr.sin_port = pj_htons(5060);
199
200 status = pjsip_udp_transport_start( g_endpt, &addr, NULL, 1, NULL);
201 if (status != PJ_SUCCESS) {
202 app_perror(THIS_FILE, "Unable to start UDP transport", status);
203 return 1;
204 }
205 }
206
207
208 /*
Benny Prijono53fde132006-03-17 19:41:19 +0000209 * Init transaction layer.
210 * This will create/initialize transaction hash tables etc.
Benny Prijono6107a002006-03-17 18:01:27 +0000211 */
212 status = pjsip_tsx_layer_init_module(g_endpt);
213 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
214
Benny Prijono53fde132006-03-17 19:41:19 +0000215
Benny Prijono6107a002006-03-17 18:01:27 +0000216 /*
Benny Prijono53fde132006-03-17 19:41:19 +0000217 * Initialize UA layer module.
218 * This will create/initialize dialog hash tables etc.
Benny Prijono6107a002006-03-17 18:01:27 +0000219 */
220 status = pjsip_ua_init_module( g_endpt, NULL );
221 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
222
Benny Prijono53fde132006-03-17 19:41:19 +0000223
224 /*
Benny Prijono6107a002006-03-17 18:01:27 +0000225 * Init invite session module.
Benny Prijono53fde132006-03-17 19:41:19 +0000226 * The invite session module initialization takes additional argument,
227 * i.e. a structure containing callbacks to be called on specific
228 * occurence of events.
229 *
230 * The on_state_changed and on_new_session callbacks are mandatory.
231 * Application must supply the callback function.
232 *
233 * We use on_media_update() callback in this application to start
234 * media transmission.
Benny Prijono6107a002006-03-17 18:01:27 +0000235 */
236 {
237 pjsip_inv_callback inv_cb;
238
239 /* Init the callback for INVITE session: */
240 pj_memset(&inv_cb, 0, sizeof(inv_cb));
241 inv_cb.on_state_changed = &call_on_state_changed;
242 inv_cb.on_new_session = &call_on_forked;
243 inv_cb.on_media_update = &call_on_media_update;
244
245 /* Initialize invite session module: */
246 status = pjsip_inv_usage_init(g_endpt, &inv_cb);
247 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
248 }
249
250
251 /*
Benny Prijono53fde132006-03-17 19:41:19 +0000252 * Register our module to receive incoming requests.
Benny Prijono6107a002006-03-17 18:01:27 +0000253 */
254 status = pjsip_endpt_register_module( g_endpt, &mod_simpleua);
255 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
256
257
258 /*
Benny Prijono53fde132006-03-17 19:41:19 +0000259 * Initialize media endpoint.
260 * This will implicitly initialize PJMEDIA too.
Benny Prijono6107a002006-03-17 18:01:27 +0000261 */
Benny Prijono275fd682006-03-22 11:59:11 +0000262 status = pjmedia_endpt_create(&cp.factory, NULL, 1, &g_med_endpt);
Benny Prijono6107a002006-03-17 18:01:27 +0000263 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
264
265 /*
266 * Add PCMA/PCMU codec to the media endpoint.
267 */
268 status = pjmedia_codec_g711_init(g_med_endpt);
269 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
270
271 /*
272 * Initialize RTP socket info for the media.
Benny Prijono53fde132006-03-17 19:41:19 +0000273 * The RTP socket is hard-codec to port 4000.
Benny Prijono6107a002006-03-17 18:01:27 +0000274 */
275 status = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, &g_med_skinfo.rtp_sock);
276 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
277
278 pj_sockaddr_in_init( &g_med_skinfo.rtp_addr_name,
279 pjsip_endpt_name(g_endpt), 4000);
280
281 status = pj_sock_bind(g_med_skinfo.rtp_sock, &g_med_skinfo.rtp_addr_name,
282 sizeof(pj_sockaddr_in));
283 if (status != PJ_SUCCESS) {
284 app_perror( THIS_FILE,
285 "Unable to bind RTP socket",
286 status);
287 return 1;
288 }
289
290
291 /* For simplicity, ignore RTCP socket. */
292 g_med_skinfo.rtcp_sock = PJ_INVALID_SOCKET;
293 g_med_skinfo.rtcp_addr_name = g_med_skinfo.rtp_addr_name;
294
295
296 /*
297 * If URL is specified, then make call immediately.
298 */
299 if (argc > 1) {
300 char temp[80];
301 pj_str_t dst_uri = pj_str(argv[1]);
302 pj_str_t local_uri;
303 pjsip_dialog *dlg;
304 pjmedia_sdp_session *local_sdp;
305 pjsip_tx_data *tdata;
306
307 pj_ansi_sprintf(temp, "sip:simpleuac@%s", pjsip_endpt_name(g_endpt)->ptr);
308 local_uri = pj_str(temp);
309
310 /* Create UAC dialog */
311 status = pjsip_dlg_create_uac( pjsip_ua_instance(),
312 &local_uri, /* local URI */
313 NULL, /* local Contact */
314 &dst_uri, /* remote URI */
315 &dst_uri, /* remote target */
316 &dlg); /* dialog */
317 if (status != PJ_SUCCESS) {
318 app_perror(THIS_FILE, "Unable to create UAC dialog", status);
319 return 1;
320 }
321
Benny Prijono0a968362006-03-18 12:29:01 +0000322 /* If we expect the outgoing INVITE to be challenged, then we should
323 * put the credentials in the dialog here, with something like this:
324 *
325 {
326 pjsip_cred_info cred[1];
327
328 cred[0].realm = pj_str("sip.server.realm");
329 cred[0].username = pj_str("theuser");
330 cred[0].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD;
331 cred[0].data = pj_str("thepassword");
332
333 pjsip_auth_clt_set_credentials( &dlg->auth_sess, 1, cred);
334 }
335 *
336 */
337
338
339 /* If we want the initial INVITE to travel to specific SIP proxies,
340 * then we should put the initial dialog's route set here. The final
341 * route set will be updated once a dialog has been established.
342 * To set the dialog's initial route set, we do it with something
343 * like this:
344 *
345 {
346 pjsip_route_hdr route_set;
347 pjsip_route_hdr *route;
348 const pj_str_t hname = { "Route", 5 };
349 char *uri = "sip:proxy.server;lr";
350
351 pj_list_init(&route_set);
352
353 route = pjsip_parse_hdr( dlg->pool, &hname,
354 uri, strlen(uri),
355 NULL);
356 PJ_ASSERT_RETURN(route != NULL, 1);
357 pj_list_push_back(&route_set, route);
358
359 pjsip_dlg_set_route_set(dlg, &route_set);
360 }
361 *
362 * Note that Route URI SHOULD have an ";lr" parameter!
363 */
364
365
Benny Prijono53fde132006-03-17 19:41:19 +0000366 /* Get the SDP body to be put in the outgoing INVITE, by asking
367 * media endpoint to create one for us. The SDP will contain all
368 * codecs that have been registered to it (in this case, only
369 * PCMA and PCMU), plus telephony event.
370 */
371 status = pjmedia_endpt_create_sdp( g_med_endpt, /* the media endpt */
372 dlg->pool, /* pool. */
373 1, /* # of streams */
374 &g_med_skinfo, /* RTP sock info */
375 &local_sdp); /* the SDP result */
Benny Prijono6107a002006-03-17 18:01:27 +0000376 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
377
378
Benny Prijono53fde132006-03-17 19:41:19 +0000379
380 /* Create the INVITE session, and pass the SDP returned earlier
381 * as the session's initial capability.
382 */
Benny Prijono6107a002006-03-17 18:01:27 +0000383 status = pjsip_inv_create_uac( dlg, local_sdp, 0, &g_inv);
384 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
385
386
Benny Prijono53fde132006-03-17 19:41:19 +0000387 /* Create initial INVITE request.
388 * This INVITE request will contain a perfectly good request and
389 * an SDP body as well.
390 */
Benny Prijono6107a002006-03-17 18:01:27 +0000391 status = pjsip_inv_invite(g_inv, &tdata);
392 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
393
Benny Prijono53fde132006-03-17 19:41:19 +0000394
395
396 /* Send initial INVITE request.
397 * From now on, the invite session's state will be reported to us
398 * via the invite session callbacks.
399 */
Benny Prijono6107a002006-03-17 18:01:27 +0000400 status = pjsip_inv_send_msg(g_inv, tdata);
401 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
402
Benny Prijono53fde132006-03-17 19:41:19 +0000403
Benny Prijono6107a002006-03-17 18:01:27 +0000404 } else {
Benny Prijono53fde132006-03-17 19:41:19 +0000405
406 /* No URL to make call to */
407
Benny Prijono6107a002006-03-17 18:01:27 +0000408 PJ_LOG(3,(THIS_FILE, "Ready to accept incoming calls..."));
409 }
410
411
412 /* Loop until one call is completed */
413 for (;!g_complete;) {
414 pj_time_val timeout = {0, 10};
415 pjsip_endpt_handle_events(g_endpt, &timeout);
416 }
417
Benny Prijono0a968362006-03-18 12:29:01 +0000418 /* On exit, dump memory usage: */
419 {
420 pj_pool_t *p;
421 unsigned total_alloc = 0;
422 unsigned total_used = 0;
423
424 /* Accumulate memory usage in active list. */
425 p = cp.used_list.next;
426 while (p != (pj_pool_t*) &cp.used_list) {
427 total_alloc += pj_pool_get_capacity(p);
428 total_used += pj_pool_get_used_size(p);
429 p = p->next;
430 }
431
432 printf("Total pool memory allocated=%d KB, used=%d KB\n",
433 total_alloc / 1000,
434 total_used / 1000);
435 }
436
Benny Prijono6107a002006-03-17 18:01:27 +0000437 return 0;
438}
439
440
441
442/*
443 * Callback when INVITE session state has changed.
Benny Prijono53fde132006-03-17 19:41:19 +0000444 * This callback is registered when the invite session module is initialized.
445 * We mostly want to know when the invite session has been disconnected,
446 * so that we can quit the application.
Benny Prijono6107a002006-03-17 18:01:27 +0000447 */
448static void call_on_state_changed( pjsip_inv_session *inv,
449 pjsip_event *e)
450{
451 if (inv->state == PJSIP_INV_STATE_DISCONNECTED) {
452
453 PJ_LOG(3,(THIS_FILE, "Call DISCONNECTED [reason=%d (%s)]",
454 inv->cause,
455 pjsip_get_status_text(inv->cause)->ptr));
456
457 PJ_LOG(3,(THIS_FILE, "One call completed, application quitting..."));
458 g_complete = 1;
459
460 } else {
461
462 PJ_LOG(3,(THIS_FILE, "Call state changed to %s",
463 pjsip_inv_state_name(inv->state)));
464
465 }
466}
467
Benny Prijono6107a002006-03-17 18:01:27 +0000468
Benny Prijono53fde132006-03-17 19:41:19 +0000469/* This callback is called when dialog has forked. */
Benny Prijono6107a002006-03-17 18:01:27 +0000470static void call_on_forked(pjsip_inv_session *inv, pjsip_event *e)
471{
Benny Prijono53fde132006-03-17 19:41:19 +0000472 /* To be done... */
Benny Prijono6107a002006-03-17 18:01:27 +0000473}
474
Benny Prijono53fde132006-03-17 19:41:19 +0000475
Benny Prijono6107a002006-03-17 18:01:27 +0000476/*
477 * Callback when incoming requests outside any transactions and any
Benny Prijono53fde132006-03-17 19:41:19 +0000478 * dialogs are received. We're only interested to hande incoming INVITE
479 * request, and we'll reject any other requests with 500 response.
Benny Prijono6107a002006-03-17 18:01:27 +0000480 */
481static pj_bool_t on_rx_request( pjsip_rx_data *rdata )
482{
483 pjsip_dialog *dlg;
484 pjmedia_sdp_session *local_sdp;
485 pjsip_tx_data *tdata;
486 unsigned options = 0;
487 pj_status_t status;
488
Benny Prijono53fde132006-03-17 19:41:19 +0000489
Benny Prijono6107a002006-03-17 18:01:27 +0000490 /*
491 * Respond (statelessly) any non-INVITE requests with 500
492 */
493 if (rdata->msg_info.msg->line.req.method.id != PJSIP_INVITE_METHOD) {
494
495 pj_str_t reason = pj_str("Simple UA unable to handle this request");
496
497 pjsip_endpt_respond_stateless( g_endpt, rdata,
498 500, &reason,
499 NULL, NULL);
500 return PJ_TRUE;
501 }
502
Benny Prijono53fde132006-03-17 19:41:19 +0000503
Benny Prijono6107a002006-03-17 18:01:27 +0000504 /*
505 * Reject INVITE if we already have an INVITE session in progress.
506 */
507 if (g_inv) {
508
509 pj_str_t reason = pj_str("Another call is in progress");
510
511 pjsip_endpt_respond_stateless( g_endpt, rdata,
512 500, &reason,
513 NULL, NULL);
514 return PJ_TRUE;
515
516 }
517
518 /* Verify that we can handle the request. */
519 status = pjsip_inv_verify_request(rdata, &options, NULL, NULL,
520 g_endpt, NULL);
521 if (status != PJ_SUCCESS) {
522
523 pj_str_t reason = pj_str("Sorry Simple UA can not handle this INVITE");
524
525 pjsip_endpt_respond_stateless( g_endpt, rdata,
526 500, &reason,
527 NULL, NULL);
528 return PJ_TRUE;
529 }
530
531 /*
532 * Create UAS dialog.
533 */
534 status = pjsip_dlg_create_uas( pjsip_ua_instance(),
535 rdata,
536 NULL, /* contact */
537 &dlg);
538 if (status != PJ_SUCCESS) {
539 pjsip_endpt_respond_stateless(g_endpt, rdata, 500, NULL,
540 NULL, NULL);
541 return PJ_TRUE;
542 }
543
544 /*
545 * Get media capability from media endpoint:
546 */
547
548 status = pjmedia_endpt_create_sdp( g_med_endpt, rdata->tp_info.pool, 1,
549 &g_med_skinfo,
550 &local_sdp);
551 PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE);
552
Benny Prijono53fde132006-03-17 19:41:19 +0000553
Benny Prijono6107a002006-03-17 18:01:27 +0000554 /*
Benny Prijono53fde132006-03-17 19:41:19 +0000555 * Create invite session, and pass both the UAS dialog and the SDP
556 * capability to the session.
Benny Prijono6107a002006-03-17 18:01:27 +0000557 */
558 status = pjsip_inv_create_uas( dlg, rdata, local_sdp, 0, &g_inv);
559 PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE);
560
Benny Prijono53fde132006-03-17 19:41:19 +0000561
Benny Prijono6107a002006-03-17 18:01:27 +0000562 /*
563 * Initially send 180 response.
Benny Prijono53fde132006-03-17 19:41:19 +0000564 *
565 * The very first response to an INVITE must be created with
566 * pjsip_inv_initial_answer(). Subsequent responses to the same
567 * transaction MUST use pjsip_inv_answer().
Benny Prijono6107a002006-03-17 18:01:27 +0000568 */
569 status = pjsip_inv_initial_answer(g_inv, rdata,
570 180,
571 NULL, NULL, &tdata);
572 PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE);
573
Benny Prijono53fde132006-03-17 19:41:19 +0000574
575 /* Send the 180 response. */
Benny Prijono6107a002006-03-17 18:01:27 +0000576 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
Benny Prijono6107a002006-03-17 18:01:27 +0000580 /*
Benny Prijono53fde132006-03-17 19:41:19 +0000581 * Now create 200 response.
Benny Prijono6107a002006-03-17 18:01:27 +0000582 */
583 status = pjsip_inv_answer( g_inv,
584 200, NULL, /* st_code and st_text */
585 NULL, /* SDP already specified */
586 &tdata);
587 PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE);
588
589 /*
590 * Send the 200 response.
591 */
592 status = pjsip_inv_send_msg(g_inv, tdata);
593 PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE);
594
Benny Prijono53fde132006-03-17 19:41:19 +0000595
596 /* Done.
597 * When the call is disconnected, it will be reported via the callback.
598 */
599
Benny Prijono6107a002006-03-17 18:01:27 +0000600 return PJ_TRUE;
601}
602
603
Benny Prijono53fde132006-03-17 19:41:19 +0000604
605/*
606 * Callback when SDP negotiation has completed.
607 * We are interested with this callback because we want to start media
608 * as soon as SDP negotiation is completed.
609 */
610static void call_on_media_update( pjsip_inv_session *inv,
611 pj_status_t status)
612{
613 const pjmedia_sdp_session *local_sdp;
614 const pjmedia_sdp_session *remote_sdp;
615 pjmedia_port *media_port;
616
617 if (status != PJ_SUCCESS) {
618
619 app_perror(THIS_FILE, "SDP negotiation has failed", status);
620
621 /* Here we should disconnect call if we're not in the middle
622 * of initializing an UAS dialog and if this is not a re-INVITE.
623 */
624 return;
625 }
626
627 /* Get local and remote SDP.
628 * We need both SDPs to create a media session.
629 */
630 status = pjmedia_sdp_neg_get_active_local(inv->neg, &local_sdp);
631
632 status = pjmedia_sdp_neg_get_active_remote(inv->neg, &remote_sdp);
633
634
635 /* Create new media session, passing the two SDPs, and also the
636 * media socket that we created earlier.
637 * The media session is active immediately.
638 */
639 status = pjmedia_session_create( g_med_endpt, 1,
640 &g_med_skinfo,
641 local_sdp, remote_sdp,
642 NULL, &g_med_session );
643 if (status != PJ_SUCCESS) {
644 app_perror( THIS_FILE, "Unable to create media session", status);
645 return;
646 }
647
648
649 /* Get the media port interface of the first stream in the session.
650 * Media port interface is basicly a struct containing get_frame() and
651 * put_frame() function. With this media port interface, we can attach
652 * the port interface to conference bridge, or directly to a sound
653 * player/recorder device.
654 */
655 pjmedia_session_get_port(g_med_session, 0, &media_port);
656
657
658
659 /* Create a sound Player device and connect the media port to the
660 * sound device.
661 */
662 status = pjmedia_snd_port_create_player(
663 inv->pool, /* pool */
664 -1, /* sound dev id */
665 media_port->info.sample_rate, /* clock rate */
666 media_port->info.channel_count, /* channel count */
667 media_port->info.samples_per_frame, /* samples per frame*/
668 media_port->info.bits_per_sample, /* bits per sample */
669 0, /* options */
670 &g_snd_player);
671 if (status != PJ_SUCCESS) {
672 app_perror( THIS_FILE, "Unable to create sound player", status);
673 PJ_LOG(3,(THIS_FILE, "%d %d %d %d",
674 media_port->info.sample_rate, /* clock rate */
675 media_port->info.channel_count, /* channel count */
676 media_port->info.samples_per_frame, /* samples per frame*/
677 media_port->info.bits_per_sample /* bits per sample */
678 ));
679 return;
680 }
681
682 status = pjmedia_snd_port_connect(g_snd_player, media_port);
683
684
685 /* Create a sound recorder device and connect the media port to the
686 * sound device.
687 */
688 status = pjmedia_snd_port_create_rec(
689 inv->pool, /* pool */
690 -1, /* sound dev id */
691 media_port->info.sample_rate, /* clock rate */
692 media_port->info.channel_count, /* channel count */
693 media_port->info.samples_per_frame, /* samples per frame*/
694 media_port->info.bits_per_sample, /* bits per sample */
695 0, /* options */
696 &g_snd_rec);
697 if (status != PJ_SUCCESS) {
698 app_perror( THIS_FILE, "Unable to create sound recorder", status);
699 return;
700 }
701
702 status = pjmedia_snd_port_connect(g_snd_rec, media_port);
703
704 /* Done with media. */
705}
706
707