blob: ae90cd8dc76b36934365b8c46f85e33e9a278370 [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 */
262 status = pjmedia_endpt_create(&cp.factory, &g_med_endpt);
263 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 Prijono53fde132006-03-17 19:41:19 +0000322 /* Get the SDP body to be put in the outgoing INVITE, by asking
323 * media endpoint to create one for us. The SDP will contain all
324 * codecs that have been registered to it (in this case, only
325 * PCMA and PCMU), plus telephony event.
326 */
327 status = pjmedia_endpt_create_sdp( g_med_endpt, /* the media endpt */
328 dlg->pool, /* pool. */
329 1, /* # of streams */
330 &g_med_skinfo, /* RTP sock info */
331 &local_sdp); /* the SDP result */
Benny Prijono6107a002006-03-17 18:01:27 +0000332 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
333
334
Benny Prijono53fde132006-03-17 19:41:19 +0000335
336 /* Create the INVITE session, and pass the SDP returned earlier
337 * as the session's initial capability.
338 */
Benny Prijono6107a002006-03-17 18:01:27 +0000339 status = pjsip_inv_create_uac( dlg, local_sdp, 0, &g_inv);
340 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
341
342
Benny Prijono53fde132006-03-17 19:41:19 +0000343
344 /* Create initial INVITE request.
345 * This INVITE request will contain a perfectly good request and
346 * an SDP body as well.
347 */
Benny Prijono6107a002006-03-17 18:01:27 +0000348 status = pjsip_inv_invite(g_inv, &tdata);
349 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
350
Benny Prijono53fde132006-03-17 19:41:19 +0000351
352
353 /* Send initial INVITE request.
354 * From now on, the invite session's state will be reported to us
355 * via the invite session callbacks.
356 */
Benny Prijono6107a002006-03-17 18:01:27 +0000357 status = pjsip_inv_send_msg(g_inv, tdata);
358 PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1);
359
Benny Prijono53fde132006-03-17 19:41:19 +0000360
Benny Prijono6107a002006-03-17 18:01:27 +0000361 } else {
Benny Prijono53fde132006-03-17 19:41:19 +0000362
363 /* No URL to make call to */
364
Benny Prijono6107a002006-03-17 18:01:27 +0000365 PJ_LOG(3,(THIS_FILE, "Ready to accept incoming calls..."));
366 }
367
368
369 /* Loop until one call is completed */
370 for (;!g_complete;) {
371 pj_time_val timeout = {0, 10};
372 pjsip_endpt_handle_events(g_endpt, &timeout);
373 }
374
375 return 0;
376}
377
378
379
380/*
381 * Callback when INVITE session state has changed.
Benny Prijono53fde132006-03-17 19:41:19 +0000382 * This callback is registered when the invite session module is initialized.
383 * We mostly want to know when the invite session has been disconnected,
384 * so that we can quit the application.
Benny Prijono6107a002006-03-17 18:01:27 +0000385 */
386static void call_on_state_changed( pjsip_inv_session *inv,
387 pjsip_event *e)
388{
389 if (inv->state == PJSIP_INV_STATE_DISCONNECTED) {
390
391 PJ_LOG(3,(THIS_FILE, "Call DISCONNECTED [reason=%d (%s)]",
392 inv->cause,
393 pjsip_get_status_text(inv->cause)->ptr));
394
395 PJ_LOG(3,(THIS_FILE, "One call completed, application quitting..."));
396 g_complete = 1;
397
398 } else {
399
400 PJ_LOG(3,(THIS_FILE, "Call state changed to %s",
401 pjsip_inv_state_name(inv->state)));
402
403 }
404}
405
Benny Prijono6107a002006-03-17 18:01:27 +0000406
Benny Prijono53fde132006-03-17 19:41:19 +0000407/* This callback is called when dialog has forked. */
Benny Prijono6107a002006-03-17 18:01:27 +0000408static void call_on_forked(pjsip_inv_session *inv, pjsip_event *e)
409{
Benny Prijono53fde132006-03-17 19:41:19 +0000410 /* To be done... */
Benny Prijono6107a002006-03-17 18:01:27 +0000411}
412
Benny Prijono53fde132006-03-17 19:41:19 +0000413
Benny Prijono6107a002006-03-17 18:01:27 +0000414/*
415 * Callback when incoming requests outside any transactions and any
Benny Prijono53fde132006-03-17 19:41:19 +0000416 * dialogs are received. We're only interested to hande incoming INVITE
417 * request, and we'll reject any other requests with 500 response.
Benny Prijono6107a002006-03-17 18:01:27 +0000418 */
419static pj_bool_t on_rx_request( pjsip_rx_data *rdata )
420{
421 pjsip_dialog *dlg;
422 pjmedia_sdp_session *local_sdp;
423 pjsip_tx_data *tdata;
424 unsigned options = 0;
425 pj_status_t status;
426
Benny Prijono53fde132006-03-17 19:41:19 +0000427
Benny Prijono6107a002006-03-17 18:01:27 +0000428 /*
429 * Respond (statelessly) any non-INVITE requests with 500
430 */
431 if (rdata->msg_info.msg->line.req.method.id != PJSIP_INVITE_METHOD) {
432
433 pj_str_t reason = pj_str("Simple UA unable to handle this request");
434
435 pjsip_endpt_respond_stateless( g_endpt, rdata,
436 500, &reason,
437 NULL, NULL);
438 return PJ_TRUE;
439 }
440
Benny Prijono53fde132006-03-17 19:41:19 +0000441
Benny Prijono6107a002006-03-17 18:01:27 +0000442 /*
443 * Reject INVITE if we already have an INVITE session in progress.
444 */
445 if (g_inv) {
446
447 pj_str_t reason = pj_str("Another call is in progress");
448
449 pjsip_endpt_respond_stateless( g_endpt, rdata,
450 500, &reason,
451 NULL, NULL);
452 return PJ_TRUE;
453
454 }
455
456 /* Verify that we can handle the request. */
457 status = pjsip_inv_verify_request(rdata, &options, NULL, NULL,
458 g_endpt, NULL);
459 if (status != PJ_SUCCESS) {
460
461 pj_str_t reason = pj_str("Sorry Simple UA can not handle this INVITE");
462
463 pjsip_endpt_respond_stateless( g_endpt, rdata,
464 500, &reason,
465 NULL, NULL);
466 return PJ_TRUE;
467 }
468
469 /*
470 * Create UAS dialog.
471 */
472 status = pjsip_dlg_create_uas( pjsip_ua_instance(),
473 rdata,
474 NULL, /* contact */
475 &dlg);
476 if (status != PJ_SUCCESS) {
477 pjsip_endpt_respond_stateless(g_endpt, rdata, 500, NULL,
478 NULL, NULL);
479 return PJ_TRUE;
480 }
481
482 /*
483 * Get media capability from media endpoint:
484 */
485
486 status = pjmedia_endpt_create_sdp( g_med_endpt, rdata->tp_info.pool, 1,
487 &g_med_skinfo,
488 &local_sdp);
489 PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE);
490
Benny Prijono53fde132006-03-17 19:41:19 +0000491
Benny Prijono6107a002006-03-17 18:01:27 +0000492 /*
Benny Prijono53fde132006-03-17 19:41:19 +0000493 * Create invite session, and pass both the UAS dialog and the SDP
494 * capability to the session.
Benny Prijono6107a002006-03-17 18:01:27 +0000495 */
496 status = pjsip_inv_create_uas( dlg, rdata, local_sdp, 0, &g_inv);
497 PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE);
498
Benny Prijono53fde132006-03-17 19:41:19 +0000499
Benny Prijono6107a002006-03-17 18:01:27 +0000500 /*
501 * Initially send 180 response.
Benny Prijono53fde132006-03-17 19:41:19 +0000502 *
503 * The very first response to an INVITE must be created with
504 * pjsip_inv_initial_answer(). Subsequent responses to the same
505 * transaction MUST use pjsip_inv_answer().
Benny Prijono6107a002006-03-17 18:01:27 +0000506 */
507 status = pjsip_inv_initial_answer(g_inv, rdata,
508 180,
509 NULL, NULL, &tdata);
510 PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE);
511
Benny Prijono53fde132006-03-17 19:41:19 +0000512
513 /* Send the 180 response. */
Benny Prijono6107a002006-03-17 18:01:27 +0000514 status = pjsip_inv_send_msg(g_inv, tdata);
515 PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE);
516
Benny Prijono53fde132006-03-17 19:41:19 +0000517
Benny Prijono6107a002006-03-17 18:01:27 +0000518 /*
Benny Prijono53fde132006-03-17 19:41:19 +0000519 * Now create 200 response.
Benny Prijono6107a002006-03-17 18:01:27 +0000520 */
521 status = pjsip_inv_answer( g_inv,
522 200, NULL, /* st_code and st_text */
523 NULL, /* SDP already specified */
524 &tdata);
525 PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE);
526
527 /*
528 * Send the 200 response.
529 */
530 status = pjsip_inv_send_msg(g_inv, tdata);
531 PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE);
532
Benny Prijono53fde132006-03-17 19:41:19 +0000533
534 /* Done.
535 * When the call is disconnected, it will be reported via the callback.
536 */
537
Benny Prijono6107a002006-03-17 18:01:27 +0000538 return PJ_TRUE;
539}
540
541
Benny Prijono53fde132006-03-17 19:41:19 +0000542
543/*
544 * Callback when SDP negotiation has completed.
545 * We are interested with this callback because we want to start media
546 * as soon as SDP negotiation is completed.
547 */
548static void call_on_media_update( pjsip_inv_session *inv,
549 pj_status_t status)
550{
551 const pjmedia_sdp_session *local_sdp;
552 const pjmedia_sdp_session *remote_sdp;
553 pjmedia_port *media_port;
554
555 if (status != PJ_SUCCESS) {
556
557 app_perror(THIS_FILE, "SDP negotiation has failed", status);
558
559 /* Here we should disconnect call if we're not in the middle
560 * of initializing an UAS dialog and if this is not a re-INVITE.
561 */
562 return;
563 }
564
565 /* Get local and remote SDP.
566 * We need both SDPs to create a media session.
567 */
568 status = pjmedia_sdp_neg_get_active_local(inv->neg, &local_sdp);
569
570 status = pjmedia_sdp_neg_get_active_remote(inv->neg, &remote_sdp);
571
572
573 /* Create new media session, passing the two SDPs, and also the
574 * media socket that we created earlier.
575 * The media session is active immediately.
576 */
577 status = pjmedia_session_create( g_med_endpt, 1,
578 &g_med_skinfo,
579 local_sdp, remote_sdp,
580 NULL, &g_med_session );
581 if (status != PJ_SUCCESS) {
582 app_perror( THIS_FILE, "Unable to create media session", status);
583 return;
584 }
585
586
587 /* Get the media port interface of the first stream in the session.
588 * Media port interface is basicly a struct containing get_frame() and
589 * put_frame() function. With this media port interface, we can attach
590 * the port interface to conference bridge, or directly to a sound
591 * player/recorder device.
592 */
593 pjmedia_session_get_port(g_med_session, 0, &media_port);
594
595
596
597 /* Create a sound Player device and connect the media port to the
598 * sound device.
599 */
600 status = pjmedia_snd_port_create_player(
601 inv->pool, /* pool */
602 -1, /* sound dev id */
603 media_port->info.sample_rate, /* clock rate */
604 media_port->info.channel_count, /* channel count */
605 media_port->info.samples_per_frame, /* samples per frame*/
606 media_port->info.bits_per_sample, /* bits per sample */
607 0, /* options */
608 &g_snd_player);
609 if (status != PJ_SUCCESS) {
610 app_perror( THIS_FILE, "Unable to create sound player", status);
611 PJ_LOG(3,(THIS_FILE, "%d %d %d %d",
612 media_port->info.sample_rate, /* clock rate */
613 media_port->info.channel_count, /* channel count */
614 media_port->info.samples_per_frame, /* samples per frame*/
615 media_port->info.bits_per_sample /* bits per sample */
616 ));
617 return;
618 }
619
620 status = pjmedia_snd_port_connect(g_snd_player, media_port);
621
622
623 /* Create a sound recorder device and connect the media port to the
624 * sound device.
625 */
626 status = pjmedia_snd_port_create_rec(
627 inv->pool, /* pool */
628 -1, /* sound dev id */
629 media_port->info.sample_rate, /* clock rate */
630 media_port->info.channel_count, /* channel count */
631 media_port->info.samples_per_frame, /* samples per frame*/
632 media_port->info.bits_per_sample, /* bits per sample */
633 0, /* options */
634 &g_snd_rec);
635 if (status != PJ_SUCCESS) {
636 app_perror( THIS_FILE, "Unable to create sound recorder", status);
637 return;
638 }
639
640 status = pjmedia_snd_port_connect(g_snd_rec, media_port);
641
642 /* Done with media. */
643}
644
645