Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
Benny Prijono | a771a51 | 2007-02-19 01:13:53 +0000 | [diff] [blame] | 3 | * Copyright (C) 2003-2007 Benny Prijono <benny@prijono.org> |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 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 Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 19 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 20 | |
| 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 Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 53 | #include <pjsip.h> |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 54 | #include <pjmedia.h> |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 55 | #include <pjmedia-codec.h> |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 56 | #include <pjsip_ua.h> |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 57 | #include <pjsip_simple.h> |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 58 | #include <pjlib-util.h> |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 59 | #include <pjlib.h> |
| 60 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 61 | /* For logging purpose. */ |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 62 | #define THIS_FILE "simpleua.c" |
| 63 | |
Benny Prijono | bc79731 | 2006-03-24 20:44:27 +0000 | [diff] [blame] | 64 | #include "util.h" |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 65 | |
| 66 | /* |
| 67 | * Static variables. |
| 68 | */ |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 69 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 70 | static pj_bool_t g_complete; /* Quit flag. */ |
| 71 | static pjsip_endpoint *g_endpt; /* SIP endpoint. */ |
| 72 | static pj_caching_pool cp; /* Global pool factory. */ |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 73 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 74 | static pjmedia_endpt *g_med_endpt; /* Media endpoint. */ |
| 75 | static pjmedia_sock_info g_med_skinfo; /* Socket info for media */ |
Benny Prijono | b04c9e0 | 2006-05-17 17:17:39 +0000 | [diff] [blame] | 76 | static pjmedia_transport *g_med_transport;/* Media stream transport */ |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 77 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 78 | /* Call variables: */ |
| 79 | static pjsip_inv_session *g_inv; /* Current invite session. */ |
| 80 | static pjmedia_session *g_med_session; /* Call's media session. */ |
| 81 | static pjmedia_snd_port *g_snd_player; /* Call's sound player */ |
| 82 | static pjmedia_snd_port *g_snd_rec; /* Call's sound recorder. */ |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 83 | |
| 84 | |
| 85 | /* |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 86 | * Prototypes: |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 87 | */ |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 88 | |
| 89 | /* Callback to be called when SDP negotiation is done in the call: */ |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 90 | static void call_on_media_update( pjsip_inv_session *inv, |
| 91 | pj_status_t status); |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 92 | |
| 93 | /* Callback to be called when invite session's state has changed: */ |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 94 | static void call_on_state_changed( pjsip_inv_session *inv, |
| 95 | pjsip_event *e); |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 96 | |
| 97 | /* Callback to be called when dialog has forked: */ |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 98 | static void call_on_forked(pjsip_inv_session *inv, pjsip_event *e); |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 99 | |
| 100 | /* Callback to be called to handle incoming requests outside dialogs: */ |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 101 | static pj_bool_t on_rx_request( pjsip_rx_data *rdata ); |
| 102 | |
| 103 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 104 | |
| 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 Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 111 | static 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 Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 129 | |
| 130 | /* |
| 131 | * main() |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 132 | * |
| 133 | * If called with argument, treat argument as SIP URL to be called. |
| 134 | * Otherwise wait for incoming calls. |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 135 | */ |
| 136 | int main(int argc, char *argv[]) |
| 137 | { |
| 138 | pj_status_t status; |
| 139 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 140 | /* Must init PJLIB first: */ |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 141 | status = pj_init(); |
| 142 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 143 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 144 | |
| 145 | /* Then init PJLIB-UTIL: */ |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 146 | status = pjlib_util_init(); |
| 147 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 148 | |
| 149 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 150 | /* Must create a pool factory before we can allocate any memory. */ |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 151 | pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0); |
| 152 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 153 | |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 154 | /* 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 Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 174 | |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 175 | /* |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 176 | * 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 Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 180 | */ |
| 181 | { |
| 182 | pj_sockaddr_in addr; |
| 183 | |
Benny Prijono | 8ab968f | 2007-07-20 08:08:30 +0000 | [diff] [blame] | 184 | addr.sin_family = pj_AF_INET(); |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 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 Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 197 | * Init transaction layer. |
| 198 | * This will create/initialize transaction hash tables etc. |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 199 | */ |
| 200 | status = pjsip_tsx_layer_init_module(g_endpt); |
| 201 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 202 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 203 | |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 204 | /* |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 205 | * Initialize UA layer module. |
| 206 | * This will create/initialize dialog hash tables etc. |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 207 | */ |
| 208 | status = pjsip_ua_init_module( g_endpt, NULL ); |
| 209 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 210 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 211 | |
| 212 | /* |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 213 | * Init invite session module. |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 214 | * 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 Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 223 | */ |
| 224 | { |
| 225 | pjsip_inv_callback inv_cb; |
| 226 | |
| 227 | /* Init the callback for INVITE session: */ |
Benny Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 228 | pj_bzero(&inv_cb, sizeof(inv_cb)); |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 229 | 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 Prijono | df91208 | 2007-10-30 16:41:45 +0000 | [diff] [blame] | 238 | /* Initialize 100rel support */ |
| 239 | status = pjsip_100rel_init_module(g_endpt); |
| 240 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 241 | |
| 242 | /* |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 243 | * Register our module to receive incoming requests. |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 244 | */ |
| 245 | status = pjsip_endpt_register_module( g_endpt, &mod_simpleua); |
| 246 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 247 | |
| 248 | |
| 249 | /* |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 250 | * Initialize media endpoint. |
| 251 | * This will implicitly initialize PJMEDIA too. |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 252 | */ |
Benny Prijono | 54826a3 | 2007-03-30 18:57:01 +0000 | [diff] [blame] | 253 | #if PJ_HAS_THREADS |
Benny Prijono | 275fd68 | 2006-03-22 11:59:11 +0000 | [diff] [blame] | 254 | status = pjmedia_endpt_create(&cp.factory, NULL, 1, &g_med_endpt); |
Benny Prijono | 54826a3 | 2007-03-30 18:57:01 +0000 | [diff] [blame] | 255 | #else |
| 256 | status = pjmedia_endpt_create(&cp.factory, |
| 257 | pjsip_endpt_get_ioqueue(g_endpt), |
| 258 | 0, &g_med_endpt); |
| 259 | #endif |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 260 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 261 | |
| 262 | /* |
| 263 | * Add PCMA/PCMU codec to the media endpoint. |
| 264 | */ |
Benny Prijono | fc24e69 | 2007-01-27 18:31:51 +0000 | [diff] [blame] | 265 | #if defined(PJMEDIA_HAS_G711_CODEC) && PJMEDIA_HAS_G711_CODEC!=0 |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 266 | status = pjmedia_codec_g711_init(g_med_endpt); |
| 267 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
Benny Prijono | fc24e69 | 2007-01-27 18:31:51 +0000 | [diff] [blame] | 268 | #endif |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 269 | |
Benny Prijono | c1e263f | 2006-08-09 13:53:59 +0000 | [diff] [blame] | 270 | |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 271 | /* |
Benny Prijono | c1e263f | 2006-08-09 13:53:59 +0000 | [diff] [blame] | 272 | * 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 Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 275 | */ |
Benny Prijono | c1e263f | 2006-08-09 13:53:59 +0000 | [diff] [blame] | 276 | status = pjmedia_transport_udp_create(g_med_endpt, NULL, 4000, 0, |
| 277 | &g_med_transport); |
Benny Prijono | b04c9e0 | 2006-05-17 17:17:39 +0000 | [diff] [blame] | 278 | if (status != PJ_SUCCESS) { |
| 279 | app_perror(THIS_FILE, "Unable to create media transport", status); |
| 280 | return 1; |
| 281 | } |
| 282 | |
Benny Prijono | c1e263f | 2006-08-09 13:53:59 +0000 | [diff] [blame] | 283 | /* |
| 284 | * Get socket info (address, port) of the media transport. We will |
| 285 | * need this info to create SDP (i.e. the address and port info in |
| 286 | * the SDP). |
| 287 | */ |
| 288 | { |
| 289 | pjmedia_transport_udp_info udp_info; |
| 290 | |
| 291 | pjmedia_transport_udp_get_info(g_med_transport, &udp_info); |
| 292 | pj_memcpy(&g_med_skinfo, &udp_info.skinfo, |
| 293 | sizeof(pjmedia_sock_info)); |
| 294 | } |
| 295 | |
| 296 | |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 297 | /* |
| 298 | * If URL is specified, then make call immediately. |
| 299 | */ |
| 300 | if (argc > 1) { |
| 301 | char temp[80]; |
| 302 | pj_str_t dst_uri = pj_str(argv[1]); |
| 303 | pj_str_t local_uri; |
| 304 | pjsip_dialog *dlg; |
| 305 | pjmedia_sdp_session *local_sdp; |
| 306 | pjsip_tx_data *tdata; |
| 307 | |
| 308 | pj_ansi_sprintf(temp, "sip:simpleuac@%s", pjsip_endpt_name(g_endpt)->ptr); |
| 309 | local_uri = pj_str(temp); |
| 310 | |
| 311 | /* Create UAC dialog */ |
| 312 | status = pjsip_dlg_create_uac( pjsip_ua_instance(), |
| 313 | &local_uri, /* local URI */ |
| 314 | NULL, /* local Contact */ |
| 315 | &dst_uri, /* remote URI */ |
| 316 | &dst_uri, /* remote target */ |
| 317 | &dlg); /* dialog */ |
| 318 | if (status != PJ_SUCCESS) { |
| 319 | app_perror(THIS_FILE, "Unable to create UAC dialog", status); |
| 320 | return 1; |
| 321 | } |
| 322 | |
Benny Prijono | 0a96836 | 2006-03-18 12:29:01 +0000 | [diff] [blame] | 323 | /* If we expect the outgoing INVITE to be challenged, then we should |
| 324 | * put the credentials in the dialog here, with something like this: |
| 325 | * |
| 326 | { |
| 327 | pjsip_cred_info cred[1]; |
| 328 | |
| 329 | cred[0].realm = pj_str("sip.server.realm"); |
| 330 | cred[0].username = pj_str("theuser"); |
| 331 | cred[0].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD; |
| 332 | cred[0].data = pj_str("thepassword"); |
| 333 | |
| 334 | pjsip_auth_clt_set_credentials( &dlg->auth_sess, 1, cred); |
| 335 | } |
| 336 | * |
| 337 | */ |
| 338 | |
| 339 | |
Benny Prijono | df91208 | 2007-10-30 16:41:45 +0000 | [diff] [blame] | 340 | /* Get the SDP body to be put in the outgoing INVITE, by asking |
| 341 | * media endpoint to create one for us. The SDP will contain all |
| 342 | * codecs that have been registered to it (in this case, only |
| 343 | * PCMA and PCMU), plus telephony event. |
| 344 | */ |
| 345 | status = pjmedia_endpt_create_sdp( g_med_endpt, /* the media endpt */ |
| 346 | dlg->pool, /* pool. */ |
| 347 | 1, /* # of streams */ |
| 348 | &g_med_skinfo, /* RTP sock info */ |
| 349 | &local_sdp); /* the SDP result */ |
| 350 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 351 | |
| 352 | |
| 353 | |
| 354 | /* Create the INVITE session, and pass the SDP returned earlier |
| 355 | * as the session's initial capability. |
| 356 | */ |
| 357 | status = pjsip_inv_create_uac( dlg, local_sdp, 0, &g_inv); |
| 358 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 359 | |
Benny Prijono | 0a96836 | 2006-03-18 12:29:01 +0000 | [diff] [blame] | 360 | /* If we want the initial INVITE to travel to specific SIP proxies, |
| 361 | * then we should put the initial dialog's route set here. The final |
| 362 | * route set will be updated once a dialog has been established. |
| 363 | * To set the dialog's initial route set, we do it with something |
| 364 | * like this: |
| 365 | * |
| 366 | { |
| 367 | pjsip_route_hdr route_set; |
| 368 | pjsip_route_hdr *route; |
| 369 | const pj_str_t hname = { "Route", 5 }; |
| 370 | char *uri = "sip:proxy.server;lr"; |
| 371 | |
| 372 | pj_list_init(&route_set); |
| 373 | |
| 374 | route = pjsip_parse_hdr( dlg->pool, &hname, |
| 375 | uri, strlen(uri), |
| 376 | NULL); |
| 377 | PJ_ASSERT_RETURN(route != NULL, 1); |
| 378 | pj_list_push_back(&route_set, route); |
| 379 | |
| 380 | pjsip_dlg_set_route_set(dlg, &route_set); |
| 381 | } |
| 382 | * |
| 383 | * Note that Route URI SHOULD have an ";lr" parameter! |
| 384 | */ |
| 385 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 386 | /* Create initial INVITE request. |
| 387 | * This INVITE request will contain a perfectly good request and |
| 388 | * an SDP body as well. |
| 389 | */ |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 390 | status = pjsip_inv_invite(g_inv, &tdata); |
| 391 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 392 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 393 | |
| 394 | |
| 395 | /* Send initial INVITE request. |
| 396 | * From now on, the invite session's state will be reported to us |
| 397 | * via the invite session callbacks. |
| 398 | */ |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 399 | status = pjsip_inv_send_msg(g_inv, tdata); |
| 400 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 401 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 402 | |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 403 | } else { |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 404 | |
| 405 | /* No URL to make call to */ |
| 406 | |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 407 | PJ_LOG(3,(THIS_FILE, "Ready to accept incoming calls...")); |
| 408 | } |
| 409 | |
| 410 | |
| 411 | /* Loop until one call is completed */ |
| 412 | for (;!g_complete;) { |
| 413 | pj_time_val timeout = {0, 10}; |
| 414 | pjsip_endpt_handle_events(g_endpt, &timeout); |
| 415 | } |
| 416 | |
Benny Prijono | bc79731 | 2006-03-24 20:44:27 +0000 | [diff] [blame] | 417 | /* On exit, dump current memory usage: */ |
| 418 | dump_pool_usage(THIS_FILE, &cp); |
Benny Prijono | 0a96836 | 2006-03-18 12:29:01 +0000 | [diff] [blame] | 419 | |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | |
| 424 | |
| 425 | /* |
| 426 | * Callback when INVITE session state has changed. |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 427 | * This callback is registered when the invite session module is initialized. |
| 428 | * We mostly want to know when the invite session has been disconnected, |
| 429 | * so that we can quit the application. |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 430 | */ |
| 431 | static void call_on_state_changed( pjsip_inv_session *inv, |
| 432 | pjsip_event *e) |
| 433 | { |
Benny Prijono | 1595301 | 2006-04-27 22:37:08 +0000 | [diff] [blame] | 434 | PJ_UNUSED_ARG(e); |
| 435 | |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 436 | if (inv->state == PJSIP_INV_STATE_DISCONNECTED) { |
| 437 | |
| 438 | PJ_LOG(3,(THIS_FILE, "Call DISCONNECTED [reason=%d (%s)]", |
| 439 | inv->cause, |
| 440 | pjsip_get_status_text(inv->cause)->ptr)); |
| 441 | |
| 442 | PJ_LOG(3,(THIS_FILE, "One call completed, application quitting...")); |
| 443 | g_complete = 1; |
| 444 | |
| 445 | } else { |
| 446 | |
| 447 | PJ_LOG(3,(THIS_FILE, "Call state changed to %s", |
| 448 | pjsip_inv_state_name(inv->state))); |
| 449 | |
| 450 | } |
| 451 | } |
| 452 | |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 453 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 454 | /* This callback is called when dialog has forked. */ |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 455 | static void call_on_forked(pjsip_inv_session *inv, pjsip_event *e) |
| 456 | { |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 457 | /* To be done... */ |
Benny Prijono | 1595301 | 2006-04-27 22:37:08 +0000 | [diff] [blame] | 458 | PJ_UNUSED_ARG(inv); |
| 459 | PJ_UNUSED_ARG(e); |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 460 | } |
| 461 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 462 | |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 463 | /* |
| 464 | * Callback when incoming requests outside any transactions and any |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 465 | * dialogs are received. We're only interested to hande incoming INVITE |
| 466 | * request, and we'll reject any other requests with 500 response. |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 467 | */ |
| 468 | static pj_bool_t on_rx_request( pjsip_rx_data *rdata ) |
| 469 | { |
| 470 | pjsip_dialog *dlg; |
| 471 | pjmedia_sdp_session *local_sdp; |
| 472 | pjsip_tx_data *tdata; |
| 473 | unsigned options = 0; |
| 474 | pj_status_t status; |
| 475 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 476 | |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 477 | /* |
| 478 | * Respond (statelessly) any non-INVITE requests with 500 |
| 479 | */ |
| 480 | if (rdata->msg_info.msg->line.req.method.id != PJSIP_INVITE_METHOD) { |
| 481 | |
Benny Prijono | 53932c0 | 2007-02-13 18:51:44 +0000 | [diff] [blame] | 482 | if (rdata->msg_info.msg->line.req.method.id != PJSIP_ACK_METHOD) { |
| 483 | pj_str_t reason = pj_str("Simple UA unable to handle " |
| 484 | "this request"); |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 485 | |
Benny Prijono | 53932c0 | 2007-02-13 18:51:44 +0000 | [diff] [blame] | 486 | pjsip_endpt_respond_stateless( g_endpt, rdata, |
| 487 | 500, &reason, |
| 488 | NULL, NULL); |
| 489 | } |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 490 | return PJ_TRUE; |
| 491 | } |
| 492 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 493 | |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 494 | /* |
| 495 | * Reject INVITE if we already have an INVITE session in progress. |
| 496 | */ |
| 497 | if (g_inv) { |
| 498 | |
| 499 | pj_str_t reason = pj_str("Another call is in progress"); |
| 500 | |
| 501 | pjsip_endpt_respond_stateless( g_endpt, rdata, |
| 502 | 500, &reason, |
| 503 | NULL, NULL); |
| 504 | return PJ_TRUE; |
| 505 | |
| 506 | } |
| 507 | |
| 508 | /* Verify that we can handle the request. */ |
| 509 | status = pjsip_inv_verify_request(rdata, &options, NULL, NULL, |
| 510 | g_endpt, NULL); |
| 511 | if (status != PJ_SUCCESS) { |
| 512 | |
| 513 | pj_str_t reason = pj_str("Sorry Simple UA can not handle this INVITE"); |
| 514 | |
| 515 | pjsip_endpt_respond_stateless( g_endpt, rdata, |
| 516 | 500, &reason, |
| 517 | NULL, NULL); |
| 518 | return PJ_TRUE; |
| 519 | } |
| 520 | |
| 521 | /* |
| 522 | * Create UAS dialog. |
| 523 | */ |
| 524 | status = pjsip_dlg_create_uas( pjsip_ua_instance(), |
| 525 | rdata, |
| 526 | NULL, /* contact */ |
| 527 | &dlg); |
| 528 | if (status != PJ_SUCCESS) { |
| 529 | pjsip_endpt_respond_stateless(g_endpt, rdata, 500, NULL, |
| 530 | NULL, NULL); |
| 531 | return PJ_TRUE; |
| 532 | } |
| 533 | |
| 534 | /* |
| 535 | * Get media capability from media endpoint: |
| 536 | */ |
| 537 | |
| 538 | status = pjmedia_endpt_create_sdp( g_med_endpt, rdata->tp_info.pool, 1, |
| 539 | &g_med_skinfo, |
| 540 | &local_sdp); |
| 541 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE); |
| 542 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 543 | |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 544 | /* |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 545 | * Create invite session, and pass both the UAS dialog and the SDP |
| 546 | * capability to the session. |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 547 | */ |
| 548 | status = pjsip_inv_create_uas( dlg, rdata, local_sdp, 0, &g_inv); |
| 549 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE); |
| 550 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 551 | |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 552 | /* |
| 553 | * Initially send 180 response. |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 554 | * |
| 555 | * The very first response to an INVITE must be created with |
| 556 | * pjsip_inv_initial_answer(). Subsequent responses to the same |
| 557 | * transaction MUST use pjsip_inv_answer(). |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 558 | */ |
| 559 | status = pjsip_inv_initial_answer(g_inv, rdata, |
| 560 | 180, |
| 561 | NULL, NULL, &tdata); |
| 562 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE); |
| 563 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 564 | |
| 565 | /* Send the 180 response. */ |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 566 | status = pjsip_inv_send_msg(g_inv, tdata); |
| 567 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE); |
| 568 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 569 | |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 570 | /* |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 571 | * Now create 200 response. |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 572 | */ |
| 573 | status = pjsip_inv_answer( g_inv, |
| 574 | 200, NULL, /* st_code and st_text */ |
| 575 | NULL, /* SDP already specified */ |
| 576 | &tdata); |
| 577 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE); |
| 578 | |
| 579 | /* |
| 580 | * Send the 200 response. |
| 581 | */ |
| 582 | status = pjsip_inv_send_msg(g_inv, tdata); |
| 583 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE); |
| 584 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 585 | |
| 586 | /* Done. |
| 587 | * When the call is disconnected, it will be reported via the callback. |
| 588 | */ |
| 589 | |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 590 | return PJ_TRUE; |
| 591 | } |
| 592 | |
| 593 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 594 | |
| 595 | /* |
| 596 | * Callback when SDP negotiation has completed. |
| 597 | * We are interested with this callback because we want to start media |
| 598 | * as soon as SDP negotiation is completed. |
| 599 | */ |
| 600 | static void call_on_media_update( pjsip_inv_session *inv, |
| 601 | pj_status_t status) |
| 602 | { |
Benny Prijono | 8befd9f | 2006-05-13 22:46:23 +0000 | [diff] [blame] | 603 | pjmedia_session_info sess_info; |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 604 | const pjmedia_sdp_session *local_sdp; |
| 605 | const pjmedia_sdp_session *remote_sdp; |
| 606 | pjmedia_port *media_port; |
| 607 | |
| 608 | if (status != PJ_SUCCESS) { |
| 609 | |
| 610 | app_perror(THIS_FILE, "SDP negotiation has failed", status); |
| 611 | |
| 612 | /* Here we should disconnect call if we're not in the middle |
| 613 | * of initializing an UAS dialog and if this is not a re-INVITE. |
| 614 | */ |
| 615 | return; |
| 616 | } |
| 617 | |
| 618 | /* Get local and remote SDP. |
| 619 | * We need both SDPs to create a media session. |
| 620 | */ |
| 621 | status = pjmedia_sdp_neg_get_active_local(inv->neg, &local_sdp); |
| 622 | |
| 623 | status = pjmedia_sdp_neg_get_active_remote(inv->neg, &remote_sdp); |
| 624 | |
| 625 | |
Benny Prijono | 8befd9f | 2006-05-13 22:46:23 +0000 | [diff] [blame] | 626 | /* Create session info based on the two SDPs. |
| 627 | * We only support one stream per session for now. |
| 628 | */ |
Benny Prijono | b04c9e0 | 2006-05-17 17:17:39 +0000 | [diff] [blame] | 629 | status = pjmedia_session_info_from_sdp(inv->dlg->pool, g_med_endpt, |
| 630 | 1, &sess_info, |
Benny Prijono | 8befd9f | 2006-05-13 22:46:23 +0000 | [diff] [blame] | 631 | local_sdp, remote_sdp); |
| 632 | if (status != PJ_SUCCESS) { |
| 633 | app_perror( THIS_FILE, "Unable to create media session", status); |
| 634 | return; |
| 635 | } |
| 636 | |
| 637 | /* If required, we can also change some settings in the session info, |
| 638 | * (such as jitter buffer settings, codec settings, etc) before we |
| 639 | * create the session. |
| 640 | */ |
| 641 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 642 | /* Create new media session, passing the two SDPs, and also the |
| 643 | * media socket that we created earlier. |
| 644 | * The media session is active immediately. |
| 645 | */ |
Benny Prijono | 8befd9f | 2006-05-13 22:46:23 +0000 | [diff] [blame] | 646 | status = pjmedia_session_create( g_med_endpt, &sess_info, |
Benny Prijono | b04c9e0 | 2006-05-17 17:17:39 +0000 | [diff] [blame] | 647 | &g_med_transport, NULL, &g_med_session ); |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 648 | if (status != PJ_SUCCESS) { |
| 649 | app_perror( THIS_FILE, "Unable to create media session", status); |
| 650 | return; |
| 651 | } |
| 652 | |
| 653 | |
| 654 | /* Get the media port interface of the first stream in the session. |
| 655 | * Media port interface is basicly a struct containing get_frame() and |
| 656 | * put_frame() function. With this media port interface, we can attach |
| 657 | * the port interface to conference bridge, or directly to a sound |
| 658 | * player/recorder device. |
| 659 | */ |
| 660 | pjmedia_session_get_port(g_med_session, 0, &media_port); |
| 661 | |
| 662 | |
| 663 | |
| 664 | /* Create a sound Player device and connect the media port to the |
| 665 | * sound device. |
| 666 | */ |
| 667 | status = pjmedia_snd_port_create_player( |
| 668 | inv->pool, /* pool */ |
| 669 | -1, /* sound dev id */ |
Benny Prijono | 1595301 | 2006-04-27 22:37:08 +0000 | [diff] [blame] | 670 | media_port->info.clock_rate, /* clock rate */ |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 671 | media_port->info.channel_count, /* channel count */ |
| 672 | media_port->info.samples_per_frame, /* samples per frame*/ |
| 673 | media_port->info.bits_per_sample, /* bits per sample */ |
| 674 | 0, /* options */ |
| 675 | &g_snd_player); |
| 676 | if (status != PJ_SUCCESS) { |
| 677 | app_perror( THIS_FILE, "Unable to create sound player", status); |
| 678 | PJ_LOG(3,(THIS_FILE, "%d %d %d %d", |
Benny Prijono | 1595301 | 2006-04-27 22:37:08 +0000 | [diff] [blame] | 679 | media_port->info.clock_rate, /* clock rate */ |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 680 | media_port->info.channel_count, /* channel count */ |
| 681 | media_port->info.samples_per_frame, /* samples per frame*/ |
| 682 | media_port->info.bits_per_sample /* bits per sample */ |
| 683 | )); |
| 684 | return; |
| 685 | } |
| 686 | |
| 687 | status = pjmedia_snd_port_connect(g_snd_player, media_port); |
| 688 | |
| 689 | |
| 690 | /* Create a sound recorder device and connect the media port to the |
| 691 | * sound device. |
| 692 | */ |
| 693 | status = pjmedia_snd_port_create_rec( |
| 694 | inv->pool, /* pool */ |
| 695 | -1, /* sound dev id */ |
Benny Prijono | 1595301 | 2006-04-27 22:37:08 +0000 | [diff] [blame] | 696 | media_port->info.clock_rate, /* clock rate */ |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 697 | media_port->info.channel_count, /* channel count */ |
| 698 | media_port->info.samples_per_frame, /* samples per frame*/ |
| 699 | media_port->info.bits_per_sample, /* bits per sample */ |
| 700 | 0, /* options */ |
| 701 | &g_snd_rec); |
| 702 | if (status != PJ_SUCCESS) { |
| 703 | app_perror( THIS_FILE, "Unable to create sound recorder", status); |
| 704 | return; |
| 705 | } |
| 706 | |
| 707 | status = pjmedia_snd_port_connect(g_snd_rec, media_port); |
| 708 | |
| 709 | /* Done with media. */ |
| 710 | } |
| 711 | |
| 712 | |