Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
Benny Prijono | 844653c | 2008-12-23 17:27:53 +0000 | [diff] [blame] | 3 | * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com) |
Benny Prijono | 32177c0 | 2008-06-20 22:44:47 +0000 | [diff] [blame] | 4 | * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 | */ |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 20 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 21 | |
| 22 | /** |
| 23 | * simpleua.c |
| 24 | * |
| 25 | * This is a very simple SIP user agent complete with media. The user |
| 26 | * agent should do a proper SDP negotiation and start RTP media once |
| 27 | * SDP negotiation has completed. |
| 28 | * |
| 29 | * This program does not register to SIP server. |
| 30 | * |
| 31 | * Capabilities to be demonstrated here: |
| 32 | * - Basic call |
Benny Prijono | e8df45f | 2008-03-08 09:26:22 +0000 | [diff] [blame] | 33 | * - Should support IPv6 (not tested) |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 34 | * - UDP transport at port 5060 (hard coded) |
| 35 | * - RTP socket at port 4000 (hard coded) |
| 36 | * - proper SDP negotiation |
| 37 | * - PCMA/PCMU codec only. |
| 38 | * - Audio/media to sound device. |
| 39 | * |
| 40 | * |
| 41 | * Usage: |
| 42 | * - To make outgoing call, start simpleua with the URL of remote |
| 43 | * destination to contact. |
| 44 | * E.g.: |
| 45 | * simpleua sip:user@remote |
| 46 | * |
| 47 | * - Incoming calls will automatically be answered with 180, then 200. |
| 48 | * |
| 49 | * This program does not disconnect call. |
| 50 | * |
| 51 | * This program will quit once it has completed a single call. |
| 52 | */ |
| 53 | |
| 54 | /* Include all headers. */ |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 55 | #include <pjsip.h> |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 56 | #include <pjmedia.h> |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 57 | #include <pjmedia-codec.h> |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 58 | #include <pjsip_ua.h> |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 59 | #include <pjsip_simple.h> |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 60 | #include <pjlib-util.h> |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 61 | #include <pjlib.h> |
| 62 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 63 | /* For logging purpose. */ |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 64 | #define THIS_FILE "simpleua.c" |
| 65 | |
Benny Prijono | bc79731 | 2006-03-24 20:44:27 +0000 | [diff] [blame] | 66 | #include "util.h" |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 67 | |
Benny Prijono | e8df45f | 2008-03-08 09:26:22 +0000 | [diff] [blame] | 68 | |
| 69 | /* Settings */ |
| 70 | #define AF pj_AF_INET() /* Change to pj_AF_INET6() for IPv6. |
| 71 | * PJ_HAS_IPV6 must be enabled and |
| 72 | * your system must support IPv6. */ |
| 73 | #define SIP_PORT 5060 /* Listening SIP port */ |
| 74 | #define RTP_PORT 4000 /* RTP port */ |
| 75 | |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 76 | /* |
| 77 | * Static variables. |
| 78 | */ |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 79 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 80 | static pj_bool_t g_complete; /* Quit flag. */ |
| 81 | static pjsip_endpoint *g_endpt; /* SIP endpoint. */ |
| 82 | static pj_caching_pool cp; /* Global pool factory. */ |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 83 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 84 | static pjmedia_endpt *g_med_endpt; /* Media endpoint. */ |
Benny Prijono | e1a5a85 | 2008-03-11 21:38:05 +0000 | [diff] [blame] | 85 | static pjmedia_transport_info g_med_tpinfo; /* Socket info for media */ |
Benny Prijono | b04c9e0 | 2006-05-17 17:17:39 +0000 | [diff] [blame] | 86 | static pjmedia_transport *g_med_transport;/* Media stream transport */ |
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 | /* Call variables: */ |
| 89 | static pjsip_inv_session *g_inv; /* Current invite session. */ |
| 90 | static pjmedia_session *g_med_session; /* Call's media session. */ |
| 91 | static pjmedia_snd_port *g_snd_player; /* Call's sound player */ |
| 92 | static pjmedia_snd_port *g_snd_rec; /* Call's sound recorder. */ |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 93 | |
| 94 | |
| 95 | /* |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 96 | * Prototypes: |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 97 | */ |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 98 | |
| 99 | /* Callback to be called when SDP negotiation is done in the call: */ |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 100 | static void call_on_media_update( pjsip_inv_session *inv, |
| 101 | pj_status_t status); |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 102 | |
| 103 | /* Callback to be called when invite session's state has changed: */ |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 104 | static void call_on_state_changed( pjsip_inv_session *inv, |
| 105 | pjsip_event *e); |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 106 | |
| 107 | /* Callback to be called when dialog has forked: */ |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 108 | static void call_on_forked(pjsip_inv_session *inv, pjsip_event *e); |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 109 | |
| 110 | /* Callback to be called to handle incoming requests outside dialogs: */ |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 111 | static pj_bool_t on_rx_request( pjsip_rx_data *rdata ); |
| 112 | |
| 113 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 114 | |
| 115 | |
| 116 | /* This is a PJSIP module to be registered by application to handle |
| 117 | * incoming requests outside any dialogs/transactions. The main purpose |
| 118 | * here is to handle incoming INVITE request message, where we will |
| 119 | * create a dialog and INVITE session for it. |
| 120 | */ |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 121 | static pjsip_module mod_simpleua = |
| 122 | { |
| 123 | NULL, NULL, /* prev, next. */ |
| 124 | { "mod-simpleua", 12 }, /* Name. */ |
| 125 | -1, /* Id */ |
| 126 | PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */ |
| 127 | NULL, /* load() */ |
| 128 | NULL, /* start() */ |
| 129 | NULL, /* stop() */ |
| 130 | NULL, /* unload() */ |
| 131 | &on_rx_request, /* on_rx_request() */ |
| 132 | NULL, /* on_rx_response() */ |
| 133 | NULL, /* on_tx_request. */ |
| 134 | NULL, /* on_tx_response() */ |
| 135 | NULL, /* on_tsx_state() */ |
| 136 | }; |
| 137 | |
| 138 | |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 139 | |
| 140 | /* |
| 141 | * main() |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 142 | * |
| 143 | * If called with argument, treat argument as SIP URL to be called. |
| 144 | * Otherwise wait for incoming calls. |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 145 | */ |
| 146 | int main(int argc, char *argv[]) |
| 147 | { |
| 148 | pj_status_t status; |
| 149 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 150 | /* Must init PJLIB first: */ |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 151 | status = pj_init(); |
| 152 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 153 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 154 | |
| 155 | /* Then init PJLIB-UTIL: */ |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 156 | status = pjlib_util_init(); |
| 157 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 158 | |
| 159 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 160 | /* Must create a pool factory before we can allocate any memory. */ |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 161 | pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0); |
| 162 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 163 | |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 164 | /* Create global endpoint: */ |
| 165 | { |
| 166 | const pj_str_t *hostname; |
| 167 | const char *endpt_name; |
| 168 | |
| 169 | /* Endpoint MUST be assigned a globally unique name. |
| 170 | * The name will be used as the hostname in Warning header. |
| 171 | */ |
| 172 | |
| 173 | /* For this implementation, we'll use hostname for simplicity */ |
| 174 | hostname = pj_gethostname(); |
| 175 | endpt_name = hostname->ptr; |
| 176 | |
| 177 | /* Create the endpoint: */ |
| 178 | |
| 179 | status = pjsip_endpt_create(&cp.factory, endpt_name, |
| 180 | &g_endpt); |
| 181 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 182 | } |
| 183 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 184 | |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 185 | /* |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 186 | * Add UDP transport, with hard-coded port |
| 187 | * Alternatively, application can use pjsip_udp_transport_attach() to |
| 188 | * start UDP transport, if it already has an UDP socket (e.g. after it |
| 189 | * resolves the address with STUN). |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 190 | */ |
| 191 | { |
Benny Prijono | e8df45f | 2008-03-08 09:26:22 +0000 | [diff] [blame] | 192 | pj_sockaddr addr; |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 193 | |
Benny Prijono | e8df45f | 2008-03-08 09:26:22 +0000 | [diff] [blame] | 194 | pj_sockaddr_init(AF, &addr, NULL, (pj_uint16_t)SIP_PORT); |
| 195 | |
| 196 | if (AF == pj_AF_INET()) { |
| 197 | status = pjsip_udp_transport_start( g_endpt, &addr.ipv4, NULL, |
| 198 | 1, NULL); |
| 199 | } else if (AF == pj_AF_INET6()) { |
| 200 | status = pjsip_udp_transport_start6(g_endpt, &addr.ipv6, NULL, |
| 201 | 1, NULL); |
| 202 | } else { |
| 203 | status = PJ_EAFNOTSUP; |
| 204 | } |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 205 | |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 206 | if (status != PJ_SUCCESS) { |
| 207 | app_perror(THIS_FILE, "Unable to start UDP transport", status); |
| 208 | return 1; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | |
| 213 | /* |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 214 | * Init transaction layer. |
| 215 | * This will create/initialize transaction hash tables etc. |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 216 | */ |
| 217 | status = pjsip_tsx_layer_init_module(g_endpt); |
| 218 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 219 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 220 | |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 221 | /* |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 222 | * Initialize UA layer module. |
| 223 | * This will create/initialize dialog hash tables etc. |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 224 | */ |
| 225 | status = pjsip_ua_init_module( g_endpt, NULL ); |
| 226 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 227 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 228 | |
| 229 | /* |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 230 | * Init invite session module. |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 231 | * The invite session module initialization takes additional argument, |
| 232 | * i.e. a structure containing callbacks to be called on specific |
| 233 | * occurence of events. |
| 234 | * |
| 235 | * The on_state_changed and on_new_session callbacks are mandatory. |
| 236 | * Application must supply the callback function. |
| 237 | * |
| 238 | * We use on_media_update() callback in this application to start |
| 239 | * media transmission. |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 240 | */ |
| 241 | { |
| 242 | pjsip_inv_callback inv_cb; |
| 243 | |
| 244 | /* Init the callback for INVITE session: */ |
Benny Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 245 | pj_bzero(&inv_cb, sizeof(inv_cb)); |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 246 | inv_cb.on_state_changed = &call_on_state_changed; |
| 247 | inv_cb.on_new_session = &call_on_forked; |
| 248 | inv_cb.on_media_update = &call_on_media_update; |
| 249 | |
| 250 | /* Initialize invite session module: */ |
| 251 | status = pjsip_inv_usage_init(g_endpt, &inv_cb); |
| 252 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 253 | } |
| 254 | |
Benny Prijono | df91208 | 2007-10-30 16:41:45 +0000 | [diff] [blame] | 255 | /* Initialize 100rel support */ |
| 256 | status = pjsip_100rel_init_module(g_endpt); |
| 257 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 258 | |
| 259 | /* |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 260 | * Register our module to receive incoming requests. |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 261 | */ |
| 262 | status = pjsip_endpt_register_module( g_endpt, &mod_simpleua); |
| 263 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 264 | |
| 265 | |
| 266 | /* |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 267 | * Initialize media endpoint. |
| 268 | * This will implicitly initialize PJMEDIA too. |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 269 | */ |
Benny Prijono | 54826a3 | 2007-03-30 18:57:01 +0000 | [diff] [blame] | 270 | #if PJ_HAS_THREADS |
Benny Prijono | 275fd68 | 2006-03-22 11:59:11 +0000 | [diff] [blame] | 271 | status = pjmedia_endpt_create(&cp.factory, NULL, 1, &g_med_endpt); |
Benny Prijono | 54826a3 | 2007-03-30 18:57:01 +0000 | [diff] [blame] | 272 | #else |
| 273 | status = pjmedia_endpt_create(&cp.factory, |
| 274 | pjsip_endpt_get_ioqueue(g_endpt), |
| 275 | 0, &g_med_endpt); |
| 276 | #endif |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 277 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 278 | |
| 279 | /* |
| 280 | * Add PCMA/PCMU codec to the media endpoint. |
| 281 | */ |
Benny Prijono | fc24e69 | 2007-01-27 18:31:51 +0000 | [diff] [blame] | 282 | #if defined(PJMEDIA_HAS_G711_CODEC) && PJMEDIA_HAS_G711_CODEC!=0 |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 283 | status = pjmedia_codec_g711_init(g_med_endpt); |
| 284 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
Benny Prijono | fc24e69 | 2007-01-27 18:31:51 +0000 | [diff] [blame] | 285 | #endif |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 286 | |
Benny Prijono | c1e263f | 2006-08-09 13:53:59 +0000 | [diff] [blame] | 287 | |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 288 | /* |
Benny Prijono | c1e263f | 2006-08-09 13:53:59 +0000 | [diff] [blame] | 289 | * Create media transport used to send/receive RTP/RTCP socket. |
| 290 | * One media transport is needed for each call. Application may |
| 291 | * opt to re-use the same media transport for subsequent calls. |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 292 | */ |
Benny Prijono | e8df45f | 2008-03-08 09:26:22 +0000 | [diff] [blame] | 293 | status = pjmedia_transport_udp_create3(g_med_endpt, AF, NULL, NULL, |
| 294 | RTP_PORT, 0, &g_med_transport); |
Benny Prijono | b04c9e0 | 2006-05-17 17:17:39 +0000 | [diff] [blame] | 295 | if (status != PJ_SUCCESS) { |
| 296 | app_perror(THIS_FILE, "Unable to create media transport", status); |
| 297 | return 1; |
| 298 | } |
| 299 | |
Benny Prijono | c1e263f | 2006-08-09 13:53:59 +0000 | [diff] [blame] | 300 | /* |
| 301 | * Get socket info (address, port) of the media transport. We will |
| 302 | * need this info to create SDP (i.e. the address and port info in |
| 303 | * the SDP). |
| 304 | */ |
Benny Prijono | 734fc2d | 2008-03-17 16:05:35 +0000 | [diff] [blame] | 305 | pjmedia_transport_info_init(&g_med_tpinfo); |
Benny Prijono | e1a5a85 | 2008-03-11 21:38:05 +0000 | [diff] [blame] | 306 | pjmedia_transport_get_info(g_med_transport, &g_med_tpinfo); |
Benny Prijono | c1e263f | 2006-08-09 13:53:59 +0000 | [diff] [blame] | 307 | |
| 308 | |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 309 | /* |
| 310 | * If URL is specified, then make call immediately. |
| 311 | */ |
| 312 | if (argc > 1) { |
Benny Prijono | e8df45f | 2008-03-08 09:26:22 +0000 | [diff] [blame] | 313 | pj_sockaddr hostaddr; |
| 314 | char hostip[PJ_INET6_ADDRSTRLEN+2]; |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 315 | char temp[80]; |
| 316 | pj_str_t dst_uri = pj_str(argv[1]); |
| 317 | pj_str_t local_uri; |
| 318 | pjsip_dialog *dlg; |
| 319 | pjmedia_sdp_session *local_sdp; |
| 320 | pjsip_tx_data *tdata; |
| 321 | |
Benny Prijono | e8df45f | 2008-03-08 09:26:22 +0000 | [diff] [blame] | 322 | if (pj_gethostip(AF, &hostaddr) != PJ_SUCCESS) { |
| 323 | app_perror(THIS_FILE, "Unable to retrieve local host IP", status); |
| 324 | return 1; |
| 325 | } |
| 326 | pj_sockaddr_print(&hostaddr, hostip, sizeof(hostip), 2); |
| 327 | |
| 328 | pj_ansi_sprintf(temp, "<sip:simpleuac@%s:%d>", |
| 329 | hostip, SIP_PORT); |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 330 | local_uri = pj_str(temp); |
| 331 | |
| 332 | /* Create UAC dialog */ |
| 333 | status = pjsip_dlg_create_uac( pjsip_ua_instance(), |
| 334 | &local_uri, /* local URI */ |
Benny Prijono | e8df45f | 2008-03-08 09:26:22 +0000 | [diff] [blame] | 335 | &local_uri, /* local Contact */ |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 336 | &dst_uri, /* remote URI */ |
| 337 | &dst_uri, /* remote target */ |
| 338 | &dlg); /* dialog */ |
| 339 | if (status != PJ_SUCCESS) { |
| 340 | app_perror(THIS_FILE, "Unable to create UAC dialog", status); |
| 341 | return 1; |
| 342 | } |
| 343 | |
Benny Prijono | 0a96836 | 2006-03-18 12:29:01 +0000 | [diff] [blame] | 344 | /* If we expect the outgoing INVITE to be challenged, then we should |
| 345 | * put the credentials in the dialog here, with something like this: |
| 346 | * |
| 347 | { |
| 348 | pjsip_cred_info cred[1]; |
| 349 | |
| 350 | cred[0].realm = pj_str("sip.server.realm"); |
Benny Prijono | becb31b | 2009-01-20 08:53:38 +0000 | [diff] [blame] | 351 | cred[0].scheme = pj_str("digest"); |
Benny Prijono | 0a96836 | 2006-03-18 12:29:01 +0000 | [diff] [blame] | 352 | cred[0].username = pj_str("theuser"); |
| 353 | cred[0].data_type = PJSIP_CRED_DATA_PLAIN_PASSWD; |
| 354 | cred[0].data = pj_str("thepassword"); |
| 355 | |
| 356 | pjsip_auth_clt_set_credentials( &dlg->auth_sess, 1, cred); |
| 357 | } |
| 358 | * |
| 359 | */ |
| 360 | |
| 361 | |
Benny Prijono | df91208 | 2007-10-30 16:41:45 +0000 | [diff] [blame] | 362 | /* Get the SDP body to be put in the outgoing INVITE, by asking |
| 363 | * media endpoint to create one for us. The SDP will contain all |
| 364 | * codecs that have been registered to it (in this case, only |
| 365 | * PCMA and PCMU), plus telephony event. |
| 366 | */ |
| 367 | status = pjmedia_endpt_create_sdp( g_med_endpt, /* the media endpt */ |
| 368 | dlg->pool, /* pool. */ |
| 369 | 1, /* # of streams */ |
Benny Prijono | e1a5a85 | 2008-03-11 21:38:05 +0000 | [diff] [blame] | 370 | &g_med_tpinfo.sock_info, |
| 371 | /* RTP sock info */ |
Benny Prijono | df91208 | 2007-10-30 16:41:45 +0000 | [diff] [blame] | 372 | &local_sdp); /* the SDP result */ |
| 373 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 374 | |
| 375 | |
| 376 | |
| 377 | /* Create the INVITE session, and pass the SDP returned earlier |
| 378 | * as the session's initial capability. |
| 379 | */ |
| 380 | status = pjsip_inv_create_uac( dlg, local_sdp, 0, &g_inv); |
| 381 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 382 | |
Benny Prijono | 0a96836 | 2006-03-18 12:29:01 +0000 | [diff] [blame] | 383 | /* If we want the initial INVITE to travel to specific SIP proxies, |
| 384 | * then we should put the initial dialog's route set here. The final |
| 385 | * route set will be updated once a dialog has been established. |
| 386 | * To set the dialog's initial route set, we do it with something |
| 387 | * like this: |
| 388 | * |
| 389 | { |
| 390 | pjsip_route_hdr route_set; |
| 391 | pjsip_route_hdr *route; |
| 392 | const pj_str_t hname = { "Route", 5 }; |
| 393 | char *uri = "sip:proxy.server;lr"; |
| 394 | |
| 395 | pj_list_init(&route_set); |
| 396 | |
| 397 | route = pjsip_parse_hdr( dlg->pool, &hname, |
| 398 | uri, strlen(uri), |
| 399 | NULL); |
| 400 | PJ_ASSERT_RETURN(route != NULL, 1); |
| 401 | pj_list_push_back(&route_set, route); |
| 402 | |
| 403 | pjsip_dlg_set_route_set(dlg, &route_set); |
| 404 | } |
| 405 | * |
| 406 | * Note that Route URI SHOULD have an ";lr" parameter! |
| 407 | */ |
| 408 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 409 | /* Create initial INVITE request. |
| 410 | * This INVITE request will contain a perfectly good request and |
| 411 | * an SDP body as well. |
| 412 | */ |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 413 | status = pjsip_inv_invite(g_inv, &tdata); |
| 414 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 415 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 416 | |
| 417 | |
| 418 | /* Send initial INVITE request. |
| 419 | * From now on, the invite session's state will be reported to us |
| 420 | * via the invite session callbacks. |
| 421 | */ |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 422 | status = pjsip_inv_send_msg(g_inv, tdata); |
| 423 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 424 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 425 | |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 426 | } else { |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 427 | |
| 428 | /* No URL to make call to */ |
| 429 | |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 430 | PJ_LOG(3,(THIS_FILE, "Ready to accept incoming calls...")); |
| 431 | } |
| 432 | |
| 433 | |
| 434 | /* Loop until one call is completed */ |
| 435 | for (;!g_complete;) { |
| 436 | pj_time_val timeout = {0, 10}; |
| 437 | pjsip_endpt_handle_events(g_endpt, &timeout); |
| 438 | } |
| 439 | |
Benny Prijono | bc79731 | 2006-03-24 20:44:27 +0000 | [diff] [blame] | 440 | /* On exit, dump current memory usage: */ |
| 441 | dump_pool_usage(THIS_FILE, &cp); |
Benny Prijono | 0a96836 | 2006-03-18 12:29:01 +0000 | [diff] [blame] | 442 | |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 443 | return 0; |
| 444 | } |
| 445 | |
| 446 | |
| 447 | |
| 448 | /* |
| 449 | * Callback when INVITE session state has changed. |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 450 | * This callback is registered when the invite session module is initialized. |
| 451 | * We mostly want to know when the invite session has been disconnected, |
| 452 | * so that we can quit the application. |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 453 | */ |
| 454 | static void call_on_state_changed( pjsip_inv_session *inv, |
| 455 | pjsip_event *e) |
| 456 | { |
Benny Prijono | 1595301 | 2006-04-27 22:37:08 +0000 | [diff] [blame] | 457 | PJ_UNUSED_ARG(e); |
| 458 | |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 459 | if (inv->state == PJSIP_INV_STATE_DISCONNECTED) { |
| 460 | |
| 461 | PJ_LOG(3,(THIS_FILE, "Call DISCONNECTED [reason=%d (%s)]", |
| 462 | inv->cause, |
| 463 | pjsip_get_status_text(inv->cause)->ptr)); |
| 464 | |
| 465 | PJ_LOG(3,(THIS_FILE, "One call completed, application quitting...")); |
| 466 | g_complete = 1; |
| 467 | |
| 468 | } else { |
| 469 | |
| 470 | PJ_LOG(3,(THIS_FILE, "Call state changed to %s", |
| 471 | pjsip_inv_state_name(inv->state))); |
| 472 | |
| 473 | } |
| 474 | } |
| 475 | |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 476 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 477 | /* This callback is called when dialog has forked. */ |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 478 | static void call_on_forked(pjsip_inv_session *inv, pjsip_event *e) |
| 479 | { |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 480 | /* To be done... */ |
Benny Prijono | 1595301 | 2006-04-27 22:37:08 +0000 | [diff] [blame] | 481 | PJ_UNUSED_ARG(inv); |
| 482 | PJ_UNUSED_ARG(e); |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 483 | } |
| 484 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 485 | |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 486 | /* |
| 487 | * Callback when incoming requests outside any transactions and any |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 488 | * dialogs are received. We're only interested to hande incoming INVITE |
| 489 | * request, and we'll reject any other requests with 500 response. |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 490 | */ |
| 491 | static pj_bool_t on_rx_request( pjsip_rx_data *rdata ) |
| 492 | { |
Benny Prijono | e8df45f | 2008-03-08 09:26:22 +0000 | [diff] [blame] | 493 | pj_sockaddr hostaddr; |
| 494 | char temp[80], hostip[PJ_INET6_ADDRSTRLEN]; |
| 495 | pj_str_t local_uri; |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 496 | pjsip_dialog *dlg; |
| 497 | pjmedia_sdp_session *local_sdp; |
| 498 | pjsip_tx_data *tdata; |
| 499 | unsigned options = 0; |
| 500 | pj_status_t status; |
| 501 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 502 | |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 503 | /* |
| 504 | * Respond (statelessly) any non-INVITE requests with 500 |
| 505 | */ |
| 506 | if (rdata->msg_info.msg->line.req.method.id != PJSIP_INVITE_METHOD) { |
| 507 | |
Benny Prijono | 53932c0 | 2007-02-13 18:51:44 +0000 | [diff] [blame] | 508 | if (rdata->msg_info.msg->line.req.method.id != PJSIP_ACK_METHOD) { |
| 509 | pj_str_t reason = pj_str("Simple UA unable to handle " |
| 510 | "this request"); |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 511 | |
Benny Prijono | 53932c0 | 2007-02-13 18:51:44 +0000 | [diff] [blame] | 512 | pjsip_endpt_respond_stateless( g_endpt, rdata, |
| 513 | 500, &reason, |
| 514 | NULL, NULL); |
| 515 | } |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 516 | return PJ_TRUE; |
| 517 | } |
| 518 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 519 | |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 520 | /* |
| 521 | * Reject INVITE if we already have an INVITE session in progress. |
| 522 | */ |
| 523 | if (g_inv) { |
| 524 | |
| 525 | pj_str_t reason = pj_str("Another call is in progress"); |
| 526 | |
| 527 | pjsip_endpt_respond_stateless( g_endpt, rdata, |
| 528 | 500, &reason, |
| 529 | NULL, NULL); |
| 530 | return PJ_TRUE; |
| 531 | |
| 532 | } |
| 533 | |
| 534 | /* Verify that we can handle the request. */ |
| 535 | status = pjsip_inv_verify_request(rdata, &options, NULL, NULL, |
| 536 | g_endpt, NULL); |
| 537 | if (status != PJ_SUCCESS) { |
| 538 | |
| 539 | pj_str_t reason = pj_str("Sorry Simple UA can not handle this INVITE"); |
| 540 | |
| 541 | pjsip_endpt_respond_stateless( g_endpt, rdata, |
| 542 | 500, &reason, |
| 543 | NULL, NULL); |
| 544 | return PJ_TRUE; |
| 545 | } |
| 546 | |
| 547 | /* |
Benny Prijono | e8df45f | 2008-03-08 09:26:22 +0000 | [diff] [blame] | 548 | * Generate Contact URI |
| 549 | */ |
| 550 | if (pj_gethostip(AF, &hostaddr) != PJ_SUCCESS) { |
| 551 | app_perror(THIS_FILE, "Unable to retrieve local host IP", status); |
| 552 | return PJ_TRUE; |
| 553 | } |
| 554 | pj_sockaddr_print(&hostaddr, hostip, sizeof(hostip), 2); |
| 555 | |
| 556 | pj_ansi_sprintf(temp, "<sip:simpleuas@%s:%d>", |
| 557 | hostip, SIP_PORT); |
| 558 | local_uri = pj_str(temp); |
| 559 | |
| 560 | /* |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 561 | * Create UAS dialog. |
| 562 | */ |
| 563 | status = pjsip_dlg_create_uas( pjsip_ua_instance(), |
| 564 | rdata, |
Benny Prijono | e8df45f | 2008-03-08 09:26:22 +0000 | [diff] [blame] | 565 | &local_uri, /* contact */ |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 566 | &dlg); |
| 567 | if (status != PJ_SUCCESS) { |
| 568 | pjsip_endpt_respond_stateless(g_endpt, rdata, 500, NULL, |
| 569 | NULL, NULL); |
| 570 | return PJ_TRUE; |
| 571 | } |
| 572 | |
| 573 | /* |
| 574 | * Get media capability from media endpoint: |
| 575 | */ |
| 576 | |
| 577 | status = pjmedia_endpt_create_sdp( g_med_endpt, rdata->tp_info.pool, 1, |
Benny Prijono | e1a5a85 | 2008-03-11 21:38:05 +0000 | [diff] [blame] | 578 | &g_med_tpinfo.sock_info, |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 579 | &local_sdp); |
| 580 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE); |
| 581 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 582 | |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 583 | /* |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 584 | * Create invite session, and pass both the UAS dialog and the SDP |
| 585 | * capability to the session. |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 586 | */ |
| 587 | status = pjsip_inv_create_uas( dlg, rdata, local_sdp, 0, &g_inv); |
| 588 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE); |
| 589 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 590 | |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 591 | /* |
| 592 | * Initially send 180 response. |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 593 | * |
| 594 | * The very first response to an INVITE must be created with |
| 595 | * pjsip_inv_initial_answer(). Subsequent responses to the same |
| 596 | * transaction MUST use pjsip_inv_answer(). |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 597 | */ |
| 598 | status = pjsip_inv_initial_answer(g_inv, rdata, |
| 599 | 180, |
| 600 | NULL, NULL, &tdata); |
| 601 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE); |
| 602 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 603 | |
| 604 | /* Send the 180 response. */ |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 605 | status = pjsip_inv_send_msg(g_inv, tdata); |
| 606 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE); |
| 607 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 608 | |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 609 | /* |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 610 | * Now create 200 response. |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 611 | */ |
| 612 | status = pjsip_inv_answer( g_inv, |
| 613 | 200, NULL, /* st_code and st_text */ |
| 614 | NULL, /* SDP already specified */ |
| 615 | &tdata); |
| 616 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE); |
| 617 | |
| 618 | /* |
| 619 | * Send the 200 response. |
| 620 | */ |
| 621 | status = pjsip_inv_send_msg(g_inv, tdata); |
| 622 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE); |
| 623 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 624 | |
| 625 | /* Done. |
| 626 | * When the call is disconnected, it will be reported via the callback. |
| 627 | */ |
| 628 | |
Benny Prijono | 6107a00 | 2006-03-17 18:01:27 +0000 | [diff] [blame] | 629 | return PJ_TRUE; |
| 630 | } |
| 631 | |
| 632 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 633 | |
| 634 | /* |
| 635 | * Callback when SDP negotiation has completed. |
| 636 | * We are interested with this callback because we want to start media |
| 637 | * as soon as SDP negotiation is completed. |
| 638 | */ |
| 639 | static void call_on_media_update( pjsip_inv_session *inv, |
| 640 | pj_status_t status) |
| 641 | { |
Benny Prijono | 8befd9f | 2006-05-13 22:46:23 +0000 | [diff] [blame] | 642 | pjmedia_session_info sess_info; |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 643 | const pjmedia_sdp_session *local_sdp; |
| 644 | const pjmedia_sdp_session *remote_sdp; |
| 645 | pjmedia_port *media_port; |
| 646 | |
| 647 | if (status != PJ_SUCCESS) { |
| 648 | |
| 649 | app_perror(THIS_FILE, "SDP negotiation has failed", status); |
| 650 | |
| 651 | /* Here we should disconnect call if we're not in the middle |
| 652 | * of initializing an UAS dialog and if this is not a re-INVITE. |
| 653 | */ |
| 654 | return; |
| 655 | } |
| 656 | |
| 657 | /* Get local and remote SDP. |
| 658 | * We need both SDPs to create a media session. |
| 659 | */ |
| 660 | status = pjmedia_sdp_neg_get_active_local(inv->neg, &local_sdp); |
| 661 | |
| 662 | status = pjmedia_sdp_neg_get_active_remote(inv->neg, &remote_sdp); |
| 663 | |
| 664 | |
Benny Prijono | 8befd9f | 2006-05-13 22:46:23 +0000 | [diff] [blame] | 665 | /* Create session info based on the two SDPs. |
| 666 | * We only support one stream per session for now. |
| 667 | */ |
Benny Prijono | b04c9e0 | 2006-05-17 17:17:39 +0000 | [diff] [blame] | 668 | status = pjmedia_session_info_from_sdp(inv->dlg->pool, g_med_endpt, |
| 669 | 1, &sess_info, |
Benny Prijono | 8befd9f | 2006-05-13 22:46:23 +0000 | [diff] [blame] | 670 | local_sdp, remote_sdp); |
| 671 | if (status != PJ_SUCCESS) { |
| 672 | app_perror( THIS_FILE, "Unable to create media session", status); |
| 673 | return; |
| 674 | } |
| 675 | |
| 676 | /* If required, we can also change some settings in the session info, |
| 677 | * (such as jitter buffer settings, codec settings, etc) before we |
| 678 | * create the session. |
| 679 | */ |
| 680 | |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 681 | /* Create new media session, passing the two SDPs, and also the |
| 682 | * media socket that we created earlier. |
| 683 | * The media session is active immediately. |
| 684 | */ |
Benny Prijono | 8befd9f | 2006-05-13 22:46:23 +0000 | [diff] [blame] | 685 | status = pjmedia_session_create( g_med_endpt, &sess_info, |
Benny Prijono | b04c9e0 | 2006-05-17 17:17:39 +0000 | [diff] [blame] | 686 | &g_med_transport, NULL, &g_med_session ); |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 687 | if (status != PJ_SUCCESS) { |
| 688 | app_perror( THIS_FILE, "Unable to create media session", status); |
| 689 | return; |
| 690 | } |
| 691 | |
| 692 | |
| 693 | /* Get the media port interface of the first stream in the session. |
| 694 | * Media port interface is basicly a struct containing get_frame() and |
| 695 | * put_frame() function. With this media port interface, we can attach |
| 696 | * the port interface to conference bridge, or directly to a sound |
| 697 | * player/recorder device. |
| 698 | */ |
| 699 | pjmedia_session_get_port(g_med_session, 0, &media_port); |
| 700 | |
| 701 | |
| 702 | |
| 703 | /* Create a sound Player device and connect the media port to the |
| 704 | * sound device. |
| 705 | */ |
| 706 | status = pjmedia_snd_port_create_player( |
| 707 | inv->pool, /* pool */ |
| 708 | -1, /* sound dev id */ |
Benny Prijono | 1595301 | 2006-04-27 22:37:08 +0000 | [diff] [blame] | 709 | media_port->info.clock_rate, /* clock rate */ |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 710 | media_port->info.channel_count, /* channel count */ |
| 711 | media_port->info.samples_per_frame, /* samples per frame*/ |
| 712 | media_port->info.bits_per_sample, /* bits per sample */ |
| 713 | 0, /* options */ |
| 714 | &g_snd_player); |
| 715 | if (status != PJ_SUCCESS) { |
| 716 | app_perror( THIS_FILE, "Unable to create sound player", status); |
| 717 | PJ_LOG(3,(THIS_FILE, "%d %d %d %d", |
Benny Prijono | 1595301 | 2006-04-27 22:37:08 +0000 | [diff] [blame] | 718 | media_port->info.clock_rate, /* clock rate */ |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 719 | media_port->info.channel_count, /* channel count */ |
| 720 | media_port->info.samples_per_frame, /* samples per frame*/ |
| 721 | media_port->info.bits_per_sample /* bits per sample */ |
| 722 | )); |
| 723 | return; |
| 724 | } |
| 725 | |
| 726 | status = pjmedia_snd_port_connect(g_snd_player, media_port); |
| 727 | |
| 728 | |
| 729 | /* Create a sound recorder device and connect the media port to the |
| 730 | * sound device. |
| 731 | */ |
| 732 | status = pjmedia_snd_port_create_rec( |
| 733 | inv->pool, /* pool */ |
| 734 | -1, /* sound dev id */ |
Benny Prijono | 1595301 | 2006-04-27 22:37:08 +0000 | [diff] [blame] | 735 | media_port->info.clock_rate, /* clock rate */ |
Benny Prijono | 53fde13 | 2006-03-17 19:41:19 +0000 | [diff] [blame] | 736 | media_port->info.channel_count, /* channel count */ |
| 737 | media_port->info.samples_per_frame, /* samples per frame*/ |
| 738 | media_port->info.bits_per_sample, /* bits per sample */ |
| 739 | 0, /* options */ |
| 740 | &g_snd_rec); |
| 741 | if (status != PJ_SUCCESS) { |
| 742 | app_perror( THIS_FILE, "Unable to create sound recorder", status); |
| 743 | return; |
| 744 | } |
| 745 | |
| 746 | status = pjmedia_snd_port_connect(g_snd_rec, media_port); |
| 747 | |
| 748 | /* Done with media. */ |
| 749 | } |
| 750 | |
| 751 | |