Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1 | /* $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 | */ |
| 19 | |
| 20 | |
| 21 | /* Include all headers. */ |
| 22 | #include <pjsip.h> |
| 23 | #include <pjmedia.h> |
| 24 | #include <pjmedia-codec.h> |
| 25 | #include <pjsip_ua.h> |
| 26 | #include <pjsip_simple.h> |
| 27 | #include <pjlib-util.h> |
| 28 | #include <pjlib.h> |
| 29 | |
| 30 | #include <stdlib.h> |
| 31 | |
Benny Prijono | 9a0eab5 | 2006-04-04 19:43:24 +0000 | [diff] [blame] | 32 | |
| 33 | #if PJ_HAS_HIGH_RES_TIMER==0 |
| 34 | # error "High resolution timer is needed for this sample" |
| 35 | #endif |
| 36 | |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 37 | #define THIS_FILE "siprtp.c" |
| 38 | #define MAX_CALLS 1024 |
| 39 | #define RTP_START_PORT 44100 |
| 40 | |
| 41 | |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 42 | /* Codec descriptor: */ |
| 43 | struct codec |
| 44 | { |
| 45 | unsigned pt; |
| 46 | char* name; |
| 47 | unsigned clock_rate; |
| 48 | unsigned bit_rate; |
| 49 | unsigned ptime; |
| 50 | char* description; |
| 51 | }; |
| 52 | |
| 53 | |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 54 | /* A bidirectional media stream */ |
| 55 | struct media_stream |
| 56 | { |
| 57 | /* Static: */ |
| 58 | pj_uint16_t port; /* RTP port (RTCP is +1) */ |
| 59 | |
| 60 | /* Current stream info: */ |
| 61 | pjmedia_stream_info si; /* Current stream info. */ |
| 62 | |
| 63 | /* More info: */ |
| 64 | unsigned clock_rate; /* clock rate */ |
| 65 | unsigned samples_per_frame; /* samples per frame */ |
| 66 | unsigned bytes_per_frame; /* frame size. */ |
| 67 | |
| 68 | /* Sockets: */ |
| 69 | pj_sock_t rtp_sock; /* RTP socket. */ |
| 70 | pj_sock_t rtcp_sock; /* RTCP socket. */ |
| 71 | |
| 72 | /* RTP session: */ |
| 73 | pjmedia_rtp_session out_sess; /* outgoing RTP session */ |
| 74 | pjmedia_rtp_session in_sess; /* incoming RTP session */ |
| 75 | |
| 76 | /* RTCP stats: */ |
| 77 | pjmedia_rtcp_session rtcp; /* incoming RTCP session. */ |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 78 | |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 79 | /* Thread: */ |
| 80 | pj_bool_t thread_quit_flag; /* worker thread quit flag */ |
| 81 | pj_thread_t *thread; /* RTP/RTCP worker thread */ |
| 82 | }; |
| 83 | |
| 84 | |
| 85 | struct call |
| 86 | { |
| 87 | unsigned index; |
| 88 | pjsip_inv_session *inv; |
| 89 | unsigned media_count; |
| 90 | struct media_stream media[2]; |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 91 | pj_time_val start_time; |
| 92 | pj_time_val response_time; |
| 93 | pj_time_val connect_time; |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | |
| 97 | static struct app |
| 98 | { |
| 99 | unsigned max_calls; |
| 100 | unsigned thread_count; |
| 101 | int sip_port; |
| 102 | int rtp_start_port; |
| 103 | char *local_addr; |
| 104 | pj_str_t local_uri; |
| 105 | pj_str_t local_contact; |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 106 | |
| 107 | int app_log_level; |
| 108 | int log_level; |
| 109 | char *log_filename; |
| 110 | |
| 111 | struct codec audio_codec; |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 112 | |
| 113 | pj_str_t uri_to_call; |
| 114 | |
| 115 | pj_caching_pool cp; |
| 116 | pj_pool_t *pool; |
| 117 | |
| 118 | pjsip_endpoint *sip_endpt; |
| 119 | pj_bool_t thread_quit; |
| 120 | pj_thread_t *thread[1]; |
| 121 | |
| 122 | pjmedia_endpt *med_endpt; |
| 123 | struct call call[MAX_CALLS]; |
| 124 | } app; |
| 125 | |
| 126 | |
| 127 | |
| 128 | /* |
| 129 | * Prototypes: |
| 130 | */ |
| 131 | |
| 132 | /* Callback to be called when SDP negotiation is done in the call: */ |
| 133 | static void call_on_media_update( pjsip_inv_session *inv, |
| 134 | pj_status_t status); |
| 135 | |
| 136 | /* Callback to be called when invite session's state has changed: */ |
| 137 | static void call_on_state_changed( pjsip_inv_session *inv, |
| 138 | pjsip_event *e); |
| 139 | |
| 140 | /* Callback to be called when dialog has forked: */ |
| 141 | static void call_on_forked(pjsip_inv_session *inv, pjsip_event *e); |
| 142 | |
| 143 | /* Callback to be called to handle incoming requests outside dialogs: */ |
| 144 | static pj_bool_t on_rx_request( pjsip_rx_data *rdata ); |
| 145 | |
| 146 | /* Worker thread prototype */ |
| 147 | static int worker_thread(void *arg); |
| 148 | |
| 149 | /* Create SDP for call */ |
| 150 | static pj_status_t create_sdp( pj_pool_t *pool, |
| 151 | struct call *call, |
| 152 | pjmedia_sdp_session **p_sdp); |
| 153 | |
| 154 | /* Destroy the call's media */ |
| 155 | static void destroy_call_media(unsigned call_index); |
| 156 | |
| 157 | /* Display error */ |
| 158 | static void app_perror(const char *sender, const char *title, |
| 159 | pj_status_t status); |
| 160 | |
Benny Prijono | d7a13f1 | 2006-04-05 19:08:16 +0000 | [diff] [blame] | 161 | /* Print call */ |
| 162 | static void print_call(int call_index); |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 163 | |
| 164 | |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 165 | /* This is a PJSIP module to be registered by application to handle |
| 166 | * incoming requests outside any dialogs/transactions. The main purpose |
| 167 | * here is to handle incoming INVITE request message, where we will |
| 168 | * create a dialog and INVITE session for it. |
| 169 | */ |
| 170 | static pjsip_module mod_siprtp = |
| 171 | { |
| 172 | NULL, NULL, /* prev, next. */ |
| 173 | { "mod-siprtpapp", 13 }, /* Name. */ |
| 174 | -1, /* Id */ |
| 175 | PJSIP_MOD_PRIORITY_APPLICATION, /* Priority */ |
| 176 | NULL, /* load() */ |
| 177 | NULL, /* start() */ |
| 178 | NULL, /* stop() */ |
| 179 | NULL, /* unload() */ |
| 180 | &on_rx_request, /* on_rx_request() */ |
| 181 | NULL, /* on_rx_response() */ |
| 182 | NULL, /* on_tx_request. */ |
| 183 | NULL, /* on_tx_response() */ |
| 184 | NULL, /* on_tsx_state() */ |
| 185 | }; |
| 186 | |
| 187 | |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 188 | /* Codec constants */ |
| 189 | struct codec audio_codecs[] = |
| 190 | { |
| 191 | { 0, "pcmu", 8000, 64000, 20, "G.711 ULaw" }, |
| 192 | { 3, "gsm", 8000, 13200, 20, "GSM" }, |
| 193 | { 4, "g723", 8000, 6400, 30, "G.723.1" }, |
| 194 | { 8, "pcma", 8000, 64000, 20, "G.711 ALaw" }, |
| 195 | { 18, "g729", 8000, 8000, 20, "G.729" }, |
| 196 | }; |
| 197 | |
| 198 | |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 199 | /* |
| 200 | * Init SIP stack |
| 201 | */ |
| 202 | static pj_status_t init_sip() |
| 203 | { |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 204 | pj_status_t status; |
| 205 | |
| 206 | /* init PJLIB-UTIL: */ |
| 207 | status = pjlib_util_init(); |
| 208 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
| 209 | |
| 210 | /* Must create a pool factory before we can allocate any memory. */ |
| 211 | pj_caching_pool_init(&app.cp, &pj_pool_factory_default_policy, 0); |
| 212 | |
| 213 | /* Create application pool for misc. */ |
| 214 | app.pool = pj_pool_create(&app.cp.factory, "app", 1000, 1000, NULL); |
| 215 | |
| 216 | /* Create global endpoint: */ |
| 217 | { |
| 218 | const pj_str_t *hostname; |
| 219 | const char *endpt_name; |
| 220 | |
| 221 | /* Endpoint MUST be assigned a globally unique name. |
| 222 | * The name will be used as the hostname in Warning header. |
| 223 | */ |
| 224 | |
| 225 | /* For this implementation, we'll use hostname for simplicity */ |
| 226 | hostname = pj_gethostname(); |
| 227 | endpt_name = hostname->ptr; |
| 228 | |
| 229 | /* Create the endpoint: */ |
| 230 | |
| 231 | status = pjsip_endpt_create(&app.cp.factory, endpt_name, |
| 232 | &app.sip_endpt); |
| 233 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
| 234 | } |
| 235 | |
| 236 | |
| 237 | /* Add UDP transport. */ |
| 238 | { |
| 239 | pj_sockaddr_in addr; |
Benny Prijono | 49ce9a7 | 2006-04-05 16:56:19 +0000 | [diff] [blame] | 240 | pjsip_host_port addrname; |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 241 | |
Benny Prijono | 49ce9a7 | 2006-04-05 16:56:19 +0000 | [diff] [blame] | 242 | pj_memset(&addr, 0, sizeof(addr)); |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 243 | addr.sin_family = PJ_AF_INET; |
| 244 | addr.sin_addr.s_addr = 0; |
| 245 | addr.sin_port = pj_htons((pj_uint16_t)app.sip_port); |
| 246 | |
Benny Prijono | 49ce9a7 | 2006-04-05 16:56:19 +0000 | [diff] [blame] | 247 | if (app.local_addr) { |
| 248 | addrname.host = pj_str(app.local_addr); |
| 249 | addrname.port = app.sip_port; |
| 250 | } |
| 251 | |
| 252 | status = pjsip_udp_transport_start( app.sip_endpt, &addr, |
| 253 | (app.local_addr ? &addrname:NULL), |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 254 | 1, NULL); |
Benny Prijono | 49ce9a7 | 2006-04-05 16:56:19 +0000 | [diff] [blame] | 255 | if (status != PJ_SUCCESS) { |
| 256 | app_perror(THIS_FILE, "Unable to start UDP transport", status); |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 257 | return status; |
Benny Prijono | 49ce9a7 | 2006-04-05 16:56:19 +0000 | [diff] [blame] | 258 | } |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | /* |
| 262 | * Init transaction layer. |
| 263 | * This will create/initialize transaction hash tables etc. |
| 264 | */ |
| 265 | status = pjsip_tsx_layer_init_module(app.sip_endpt); |
| 266 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
| 267 | |
| 268 | /* Initialize UA layer. */ |
| 269 | status = pjsip_ua_init_module( app.sip_endpt, NULL ); |
| 270 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
| 271 | |
| 272 | /* Init invite session module. */ |
| 273 | { |
| 274 | pjsip_inv_callback inv_cb; |
| 275 | |
| 276 | /* Init the callback for INVITE session: */ |
| 277 | pj_memset(&inv_cb, 0, sizeof(inv_cb)); |
| 278 | inv_cb.on_state_changed = &call_on_state_changed; |
| 279 | inv_cb.on_new_session = &call_on_forked; |
| 280 | inv_cb.on_media_update = &call_on_media_update; |
| 281 | |
| 282 | /* Initialize invite session module: */ |
| 283 | status = pjsip_inv_usage_init(app.sip_endpt, &inv_cb); |
| 284 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 285 | } |
| 286 | |
| 287 | /* Register our module to receive incoming requests. */ |
| 288 | status = pjsip_endpt_register_module( app.sip_endpt, &mod_siprtp); |
| 289 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
| 290 | |
| 291 | |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 292 | /* Done */ |
| 293 | return PJ_SUCCESS; |
| 294 | } |
| 295 | |
| 296 | |
| 297 | /* |
| 298 | * Destroy SIP |
| 299 | */ |
| 300 | static void destroy_sip() |
| 301 | { |
| 302 | unsigned i; |
| 303 | |
| 304 | app.thread_quit = 1; |
| 305 | for (i=0; i<app.thread_count; ++i) { |
| 306 | if (app.thread[i]) { |
| 307 | pj_thread_join(app.thread[i]); |
| 308 | pj_thread_destroy(app.thread[i]); |
| 309 | app.thread[i] = NULL; |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | if (app.sip_endpt) { |
| 314 | pjsip_endpt_destroy(app.sip_endpt); |
| 315 | app.sip_endpt = NULL; |
| 316 | } |
| 317 | |
| 318 | if (app.pool) { |
| 319 | pj_pool_release(app.pool); |
| 320 | app.pool = NULL; |
| 321 | pj_caching_pool_destroy(&app.cp); |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | |
| 326 | /* |
| 327 | * Init media stack. |
| 328 | */ |
| 329 | static pj_status_t init_media() |
| 330 | { |
| 331 | pj_ioqueue_t *ioqueue; |
| 332 | unsigned i, count; |
| 333 | pj_uint16_t rtp_port; |
| 334 | pj_str_t temp; |
| 335 | pj_sockaddr_in addr; |
| 336 | pj_status_t status; |
| 337 | |
| 338 | |
| 339 | /* Get the ioqueue from the SIP endpoint */ |
| 340 | ioqueue = pjsip_endpt_get_ioqueue(app.sip_endpt); |
| 341 | |
| 342 | |
| 343 | /* Initialize media endpoint so that at least error subsystem is properly |
| 344 | * initialized. |
| 345 | */ |
| 346 | status = pjmedia_endpt_create(&app.cp.factory, ioqueue, 1, |
| 347 | &app.med_endpt); |
| 348 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
| 349 | |
| 350 | |
| 351 | /* Determine address to bind socket */ |
| 352 | pj_memset(&addr, 0, sizeof(addr)); |
| 353 | addr.sin_family = PJ_AF_INET; |
| 354 | i = pj_inet_aton(pj_cstr(&temp, app.local_addr), &addr.sin_addr); |
| 355 | if (i == 0) { |
| 356 | PJ_LOG(3,(THIS_FILE, |
| 357 | "Error: invalid local address %s (expecting IP)", |
| 358 | app.local_addr)); |
| 359 | return -1; |
| 360 | } |
| 361 | |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 362 | /* RTP port counter */ |
| 363 | rtp_port = (pj_uint16_t)(app.rtp_start_port & 0xFFFE); |
| 364 | |
| 365 | |
| 366 | /* Init media sockets. */ |
| 367 | for (i=0, count=0; i<app.max_calls; ++i, ++count) { |
| 368 | |
| 369 | int retry; |
| 370 | |
| 371 | app.call[i].index = i; |
| 372 | |
| 373 | /* Repeat binding media socket to next port when fails to bind |
| 374 | * to current port number. |
| 375 | */ |
| 376 | retry = 0; |
| 377 | do { |
| 378 | struct media_stream *m = &app.call[i].media[0]; |
| 379 | |
| 380 | ++retry; |
| 381 | rtp_port += 2; |
| 382 | m->port = rtp_port; |
| 383 | |
| 384 | /* Create and bind RTP socket */ |
| 385 | status = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, |
| 386 | &m->rtp_sock); |
| 387 | if (status != PJ_SUCCESS) |
| 388 | goto on_error; |
| 389 | |
| 390 | addr.sin_port = pj_htons(rtp_port); |
| 391 | status = pj_sock_bind(m->rtp_sock, &addr, sizeof(addr)); |
| 392 | if (status != PJ_SUCCESS) { |
| 393 | pj_sock_close(m->rtp_sock), m->rtp_sock=0; |
| 394 | continue; |
| 395 | } |
| 396 | |
| 397 | |
| 398 | /* Create and bind RTCP socket */ |
| 399 | status = pj_sock_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, |
| 400 | &m->rtcp_sock); |
| 401 | if (status != PJ_SUCCESS) |
| 402 | goto on_error; |
| 403 | |
| 404 | addr.sin_port = pj_htons((pj_uint16_t)(rtp_port+1)); |
| 405 | status = pj_sock_bind(m->rtcp_sock, &addr, sizeof(addr)); |
| 406 | if (status != PJ_SUCCESS) { |
| 407 | pj_sock_close(m->rtp_sock), m->rtp_sock=0; |
| 408 | pj_sock_close(m->rtcp_sock), m->rtcp_sock=0; |
| 409 | continue; |
| 410 | } |
| 411 | |
| 412 | } while (status != PJ_SUCCESS && retry < 100); |
| 413 | |
| 414 | if (status != PJ_SUCCESS) |
| 415 | goto on_error; |
| 416 | |
| 417 | } |
| 418 | |
| 419 | /* Done */ |
| 420 | return PJ_SUCCESS; |
| 421 | |
| 422 | on_error: |
| 423 | for (i=0; i<count; ++i) { |
| 424 | struct media_stream *m = &app.call[i].media[0]; |
| 425 | |
| 426 | pj_sock_close(m->rtp_sock), m->rtp_sock=0; |
| 427 | pj_sock_close(m->rtcp_sock), m->rtcp_sock=0; |
| 428 | } |
| 429 | |
| 430 | return status; |
| 431 | } |
| 432 | |
| 433 | |
| 434 | /* |
| 435 | * Destroy media. |
| 436 | */ |
| 437 | static void destroy_media() |
| 438 | { |
| 439 | unsigned i; |
| 440 | |
| 441 | for (i=0; i<app.max_calls; ++i) { |
| 442 | struct media_stream *m = &app.call[i].media[0]; |
| 443 | |
| 444 | if (m->rtp_sock) |
| 445 | pj_sock_close(m->rtp_sock), m->rtp_sock = 0; |
| 446 | |
| 447 | if (m->rtcp_sock) |
| 448 | pj_sock_close(m->rtcp_sock), m->rtcp_sock = 0; |
| 449 | } |
| 450 | |
| 451 | if (app.med_endpt) { |
| 452 | pjmedia_endpt_destroy(app.med_endpt); |
| 453 | app.med_endpt = NULL; |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | |
| 458 | /* |
| 459 | * Make outgoing call. |
| 460 | */ |
| 461 | static pj_status_t make_call(const pj_str_t *dst_uri) |
| 462 | { |
| 463 | unsigned i; |
| 464 | struct call *call; |
| 465 | pjsip_dialog *dlg; |
| 466 | pjmedia_sdp_session *sdp; |
| 467 | pjsip_tx_data *tdata; |
| 468 | pj_status_t status; |
| 469 | |
| 470 | |
| 471 | /* Find unused call slot */ |
| 472 | for (i=0; i<app.max_calls; ++i) { |
| 473 | if (app.call[i].inv == NULL) |
| 474 | break; |
| 475 | } |
| 476 | |
| 477 | if (i == app.max_calls) |
| 478 | return PJ_ETOOMANY; |
| 479 | |
| 480 | call = &app.call[i]; |
| 481 | |
| 482 | /* Create UAC dialog */ |
| 483 | status = pjsip_dlg_create_uac( pjsip_ua_instance(), |
| 484 | &app.local_uri, /* local URI */ |
| 485 | &app.local_contact, /* local Contact */ |
| 486 | dst_uri, /* remote URI */ |
| 487 | dst_uri, /* remote target */ |
| 488 | &dlg); /* dialog */ |
| 489 | if (status != PJ_SUCCESS) |
| 490 | return status; |
| 491 | |
| 492 | /* Create SDP */ |
| 493 | create_sdp( dlg->pool, call, &sdp); |
| 494 | |
| 495 | /* Create the INVITE session. */ |
| 496 | status = pjsip_inv_create_uac( dlg, sdp, 0, &call->inv); |
| 497 | if (status != PJ_SUCCESS) { |
| 498 | pjsip_dlg_terminate(dlg); |
| 499 | return status; |
| 500 | } |
| 501 | |
| 502 | |
| 503 | /* Attach call data to invite session */ |
| 504 | call->inv->mod_data[mod_siprtp.id] = call; |
| 505 | |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 506 | /* Mark start of call */ |
| 507 | pj_gettimeofday(&call->start_time); |
| 508 | |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 509 | |
| 510 | /* Create initial INVITE request. |
| 511 | * This INVITE request will contain a perfectly good request and |
| 512 | * an SDP body as well. |
| 513 | */ |
| 514 | status = pjsip_inv_invite(call->inv, &tdata); |
| 515 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
| 516 | |
| 517 | |
| 518 | /* Send initial INVITE request. |
| 519 | * From now on, the invite session's state will be reported to us |
| 520 | * via the invite session callbacks. |
| 521 | */ |
| 522 | status = pjsip_inv_send_msg(call->inv, tdata); |
| 523 | PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
| 524 | |
| 525 | |
| 526 | return PJ_SUCCESS; |
| 527 | } |
| 528 | |
| 529 | |
| 530 | /* |
| 531 | * Receive incoming call |
| 532 | */ |
| 533 | static void process_incoming_call(pjsip_rx_data *rdata) |
| 534 | { |
| 535 | unsigned i; |
| 536 | struct call *call; |
| 537 | pjsip_dialog *dlg; |
| 538 | pjmedia_sdp_session *sdp; |
| 539 | pjsip_tx_data *tdata; |
| 540 | pj_status_t status; |
| 541 | |
| 542 | /* Find free call slot */ |
| 543 | for (i=0; i<app.max_calls; ++i) { |
| 544 | if (app.call[i].inv == NULL) |
| 545 | break; |
| 546 | } |
| 547 | |
| 548 | if (i == app.max_calls) { |
| 549 | const pj_str_t reason = pj_str("Too many calls"); |
| 550 | pjsip_endpt_respond_stateless( app.sip_endpt, rdata, |
| 551 | 500, &reason, |
| 552 | NULL, NULL); |
| 553 | return; |
| 554 | } |
| 555 | |
| 556 | call = &app.call[i]; |
| 557 | |
| 558 | /* Create UAS dialog */ |
| 559 | status = pjsip_dlg_create_uas( pjsip_ua_instance(), rdata, |
| 560 | &app.local_contact, &dlg); |
| 561 | if (status != PJ_SUCCESS) { |
| 562 | const pj_str_t reason = pj_str("Unable to create dialog"); |
| 563 | pjsip_endpt_respond_stateless( app.sip_endpt, rdata, |
| 564 | 500, &reason, |
| 565 | NULL, NULL); |
| 566 | return; |
| 567 | } |
| 568 | |
| 569 | /* Create SDP */ |
| 570 | create_sdp( dlg->pool, call, &sdp); |
| 571 | |
| 572 | /* Create UAS invite session */ |
| 573 | status = pjsip_inv_create_uas( dlg, rdata, sdp, 0, &call->inv); |
| 574 | if (status != PJ_SUCCESS) { |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 575 | pjsip_dlg_create_response(dlg, rdata, 500, NULL, &tdata); |
| 576 | pjsip_dlg_send_response(dlg, pjsip_rdata_get_tsx(rdata), tdata); |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 577 | return; |
| 578 | } |
| 579 | |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 580 | |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 581 | /* Attach call data to invite session */ |
| 582 | call->inv->mod_data[mod_siprtp.id] = call; |
| 583 | |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 584 | /* Mark start of call */ |
| 585 | pj_gettimeofday(&call->start_time); |
| 586 | |
| 587 | |
| 588 | |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 589 | /* Create 200 response .*/ |
| 590 | status = pjsip_inv_initial_answer(call->inv, rdata, 200, |
| 591 | NULL, NULL, &tdata); |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 592 | if (status != PJ_SUCCESS) { |
| 593 | status = pjsip_inv_initial_answer(call->inv, rdata, |
| 594 | PJSIP_SC_NOT_ACCEPTABLE, |
| 595 | NULL, NULL, &tdata); |
| 596 | if (status == PJ_SUCCESS) |
| 597 | pjsip_inv_send_msg(call->inv, tdata); |
| 598 | else |
| 599 | pjsip_inv_terminate(call->inv, 500, PJ_FALSE); |
| 600 | return; |
| 601 | } |
| 602 | |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 603 | |
| 604 | /* Send the 200 response. */ |
| 605 | status = pjsip_inv_send_msg(call->inv, tdata); |
| 606 | PJ_ASSERT_ON_FAIL(status == PJ_SUCCESS, return); |
| 607 | |
| 608 | |
| 609 | /* Done */ |
| 610 | } |
| 611 | |
| 612 | |
| 613 | /* Callback to be called when dialog has forked: */ |
| 614 | static void call_on_forked(pjsip_inv_session *inv, pjsip_event *e) |
| 615 | { |
| 616 | PJ_UNUSED_ARG(inv); |
| 617 | PJ_UNUSED_ARG(e); |
| 618 | |
| 619 | PJ_TODO( HANDLE_FORKING ); |
| 620 | } |
| 621 | |
| 622 | |
| 623 | /* Callback to be called to handle incoming requests outside dialogs: */ |
| 624 | static pj_bool_t on_rx_request( pjsip_rx_data *rdata ) |
| 625 | { |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 626 | /* Ignore strandled ACKs (must not send respone */ |
| 627 | if (rdata->msg_info.msg->line.req.method.id == PJSIP_ACK_METHOD) |
| 628 | return PJ_FALSE; |
| 629 | |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 630 | /* Respond (statelessly) any non-INVITE requests with 500 */ |
| 631 | if (rdata->msg_info.msg->line.req.method.id != PJSIP_INVITE_METHOD) { |
| 632 | pj_str_t reason = pj_str("Unsupported Operation"); |
| 633 | pjsip_endpt_respond_stateless( app.sip_endpt, rdata, |
| 634 | 500, &reason, |
| 635 | NULL, NULL); |
| 636 | return PJ_TRUE; |
| 637 | } |
| 638 | |
| 639 | /* Handle incoming INVITE */ |
| 640 | process_incoming_call(rdata); |
| 641 | |
| 642 | /* Done */ |
| 643 | return PJ_TRUE; |
| 644 | } |
| 645 | |
| 646 | |
| 647 | /* Callback to be called when invite session's state has changed: */ |
| 648 | static void call_on_state_changed( pjsip_inv_session *inv, |
| 649 | pjsip_event *e) |
| 650 | { |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 651 | struct call *call = inv->mod_data[mod_siprtp.id]; |
| 652 | |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 653 | PJ_UNUSED_ARG(e); |
| 654 | |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 655 | if (!call) |
| 656 | return; |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 657 | |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 658 | if (inv->state == PJSIP_INV_STATE_DISCONNECTED) { |
| 659 | |
| 660 | pj_time_val null_time = {0, 0}; |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 661 | |
Benny Prijono | d7a13f1 | 2006-04-05 19:08:16 +0000 | [diff] [blame] | 662 | PJ_LOG(3,(THIS_FILE, "Call #%d disconnected. Reason=%s", |
| 663 | call->index, |
| 664 | pjsip_get_status_text(inv->cause)->ptr)); |
| 665 | PJ_LOG(3,(THIS_FILE, "Call #%d statistics:", call->index)); |
| 666 | print_call(call->index); |
| 667 | |
| 668 | |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 669 | call->inv = NULL; |
| 670 | inv->mod_data[mod_siprtp.id] = NULL; |
| 671 | |
| 672 | destroy_call_media(call->index); |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 673 | |
| 674 | call->start_time = null_time; |
| 675 | call->response_time = null_time; |
| 676 | call->connect_time = null_time; |
| 677 | |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 678 | |
| 679 | } else if (inv->state == PJSIP_INV_STATE_CONFIRMED) { |
| 680 | |
| 681 | pj_time_val t; |
| 682 | |
| 683 | pj_gettimeofday(&call->connect_time); |
| 684 | if (call->response_time.sec == 0) |
| 685 | call->response_time = call->connect_time; |
| 686 | |
| 687 | t = call->connect_time; |
| 688 | PJ_TIME_VAL_SUB(t, call->start_time); |
| 689 | |
| 690 | PJ_LOG(3,(THIS_FILE, "Call #%d connected in %d ms", call->index, |
| 691 | PJ_TIME_VAL_MSEC(t))); |
| 692 | |
| 693 | } else if ( inv->state == PJSIP_INV_STATE_EARLY || |
| 694 | inv->state == PJSIP_INV_STATE_CONNECTING) { |
| 695 | |
| 696 | if (call->response_time.sec == 0) |
| 697 | pj_gettimeofday(&call->response_time); |
| 698 | |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 699 | } |
| 700 | } |
| 701 | |
| 702 | |
| 703 | /* Utility */ |
| 704 | static void app_perror(const char *sender, const char *title, |
| 705 | pj_status_t status) |
| 706 | { |
| 707 | char errmsg[PJ_ERR_MSG_SIZE]; |
| 708 | |
| 709 | pj_strerror(status, errmsg, sizeof(errmsg)); |
| 710 | PJ_LOG(3,(sender, "%s: %s [status=%d]", title, errmsg, status)); |
| 711 | } |
| 712 | |
| 713 | |
| 714 | /* Worker thread */ |
| 715 | static int worker_thread(void *arg) |
| 716 | { |
| 717 | PJ_UNUSED_ARG(arg); |
| 718 | |
| 719 | while (!app.thread_quit) { |
| 720 | pj_time_val timeout = {0, 10}; |
| 721 | pjsip_endpt_handle_events(app.sip_endpt, &timeout); |
| 722 | } |
| 723 | |
| 724 | return 0; |
| 725 | } |
| 726 | |
| 727 | |
| 728 | /* Usage */ |
| 729 | static const char *USAGE = |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 730 | "Usage:\n" |
| 731 | " siprtp [options] => to start in server mode\n" |
| 732 | " siprtp [options] URL => to start in client mode\n" |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 733 | "\n" |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 734 | "Program options:\n" |
| 735 | " --count=N, -c Set number of calls to create (default:1) \n" |
| 736 | "\n" |
| 737 | "Address and ports options:\n" |
| 738 | " --local-port=PORT,-p Set local SIP port (default: 5060)\n" |
| 739 | " --rtp-port=PORT, -r Set start of RTP port (default: 4000)\n" |
| 740 | " --ip-addr=IP, -i Set local IP address to use (otherwise it will\n" |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 741 | " try to determine local IP address from hostname)\n" |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 742 | "\n" |
| 743 | "Logging Options:\n" |
| 744 | " --log-level=N, -l Set log verbosity level (default=5)\n" |
| 745 | " --app-log-level=N Set app screen log verbosity (default=3)\n" |
| 746 | " --log-file=FILE Write log to file FILE\n" |
| 747 | "\n" |
| 748 | "Codec Options:\n" |
| 749 | " --a-pt=PT Set audio payload type to PT (default=0)\n" |
| 750 | " --a-name=NAME Set audio codec name to NAME (default=pcmu)\n" |
| 751 | " --a-clock=RATE Set audio codec rate to RATE Hz (default=8000 Hz)\n" |
| 752 | " --a-bitrate=BPS Set audio codec bitrate to BPS (default=64000 bps)\n" |
| 753 | " --a-ptime=MS Set audio frame time to MS msec (default=20 msec)\n" |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 754 | ; |
| 755 | |
| 756 | |
| 757 | /* Init application options */ |
| 758 | static pj_status_t init_options(int argc, char *argv[]) |
| 759 | { |
| 760 | static char ip_addr[32]; |
| 761 | static char local_uri[64]; |
| 762 | |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 763 | enum { OPT_START, |
| 764 | OPT_APP_LOG_LEVEL, OPT_LOG_FILE, |
| 765 | OPT_A_PT, OPT_A_NAME, OPT_A_CLOCK, OPT_A_BITRATE, OPT_A_PTIME }; |
| 766 | |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 767 | struct pj_getopt_option long_options[] = { |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 768 | { "count", 1, 0, 'c' }, |
| 769 | { "local-port", 1, 0, 'p' }, |
| 770 | { "rtp-port", 1, 0, 'r' }, |
| 771 | { "ip-addr", 1, 0, 'i' }, |
| 772 | |
| 773 | { "log-level", 1, 0, 'l' }, |
| 774 | { "app-log-level", 1, 0, OPT_APP_LOG_LEVEL }, |
| 775 | { "log-file", 1, 0, OPT_LOG_FILE }, |
| 776 | { "a-pt", 1, 0, OPT_A_PT }, |
| 777 | { "a-name", 1, 0, OPT_A_NAME }, |
| 778 | { "a-clock", 1, 0, OPT_A_CLOCK }, |
| 779 | { "a-bitrate", 1, 0, OPT_A_BITRATE }, |
| 780 | { "a-ptime", 1, 0, OPT_A_PTIME }, |
| 781 | |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 782 | { NULL, 0, 0, 0 }, |
| 783 | }; |
| 784 | int c; |
| 785 | int option_index; |
| 786 | |
| 787 | /* Get local IP address for the default IP address */ |
| 788 | { |
| 789 | const pj_str_t *hostname; |
| 790 | pj_sockaddr_in tmp_addr; |
| 791 | char *addr; |
| 792 | |
| 793 | hostname = pj_gethostname(); |
| 794 | pj_sockaddr_in_init(&tmp_addr, hostname, 0); |
| 795 | addr = pj_inet_ntoa(tmp_addr.sin_addr); |
| 796 | pj_ansi_strcpy(ip_addr, addr); |
| 797 | } |
| 798 | |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 799 | /* Init defaults */ |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 800 | app.max_calls = 1; |
| 801 | app.thread_count = 1; |
| 802 | app.sip_port = 5060; |
| 803 | app.rtp_start_port = 4000; |
| 804 | app.local_addr = ip_addr; |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 805 | app.log_level = 5; |
| 806 | app.app_log_level = 3; |
| 807 | app.log_filename = NULL; |
| 808 | |
| 809 | /* Default codecs: */ |
| 810 | app.audio_codec = audio_codecs[0]; |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 811 | |
| 812 | /* Parse options */ |
| 813 | pj_optind = 0; |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 814 | while((c=pj_getopt_long(argc,argv, "c:p:r:i:l:", |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 815 | long_options, &option_index))!=-1) |
| 816 | { |
| 817 | switch (c) { |
| 818 | case 'c': |
| 819 | app.max_calls = atoi(pj_optarg); |
| 820 | if (app.max_calls < 0 || app.max_calls > MAX_CALLS) { |
| 821 | PJ_LOG(3,(THIS_FILE, "Invalid max calls value %s", pj_optarg)); |
| 822 | return 1; |
| 823 | } |
| 824 | break; |
| 825 | case 'p': |
| 826 | app.sip_port = atoi(pj_optarg); |
| 827 | break; |
| 828 | case 'r': |
| 829 | app.rtp_start_port = atoi(pj_optarg); |
| 830 | break; |
| 831 | case 'i': |
| 832 | app.local_addr = pj_optarg; |
| 833 | break; |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 834 | |
| 835 | case 'l': |
| 836 | app.log_level = atoi(pj_optarg); |
| 837 | break; |
| 838 | case OPT_APP_LOG_LEVEL: |
| 839 | app.app_log_level = atoi(pj_optarg); |
| 840 | break; |
| 841 | case OPT_LOG_FILE: |
| 842 | app.log_filename = pj_optarg; |
| 843 | break; |
| 844 | |
| 845 | case OPT_A_PT: |
| 846 | app.audio_codec.pt = atoi(pj_optarg); |
| 847 | break; |
| 848 | case OPT_A_NAME: |
| 849 | app.audio_codec.name = pj_optarg; |
| 850 | break; |
| 851 | case OPT_A_CLOCK: |
| 852 | app.audio_codec.clock_rate = atoi(pj_optarg); |
| 853 | break; |
| 854 | case OPT_A_BITRATE: |
| 855 | app.audio_codec.bit_rate = atoi(pj_optarg); |
| 856 | break; |
| 857 | case OPT_A_PTIME: |
| 858 | app.audio_codec.ptime = atoi(pj_optarg); |
| 859 | break; |
| 860 | |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 861 | default: |
| 862 | puts(USAGE); |
| 863 | return 1; |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | /* Check if URL is specified */ |
| 868 | if (pj_optind < argc) |
| 869 | app.uri_to_call = pj_str(argv[pj_optind]); |
| 870 | |
| 871 | /* Build local URI and contact */ |
| 872 | pj_ansi_sprintf( local_uri, "sip:%s:%d", app.local_addr, app.sip_port); |
| 873 | app.local_uri = pj_str(local_uri); |
| 874 | app.local_contact = app.local_uri; |
| 875 | |
| 876 | |
| 877 | return PJ_SUCCESS; |
| 878 | } |
| 879 | |
| 880 | |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 881 | /***************************************************************************** |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 882 | * MEDIA STUFFS |
| 883 | */ |
| 884 | |
| 885 | /* |
| 886 | * Create SDP session for a call. |
| 887 | */ |
| 888 | static pj_status_t create_sdp( pj_pool_t *pool, |
| 889 | struct call *call, |
| 890 | pjmedia_sdp_session **p_sdp) |
| 891 | { |
| 892 | pj_time_val tv; |
| 893 | pjmedia_sdp_session *sdp; |
| 894 | pjmedia_sdp_media *m; |
| 895 | pjmedia_sdp_attr *attr; |
| 896 | struct media_stream *audio = &call->media[0]; |
| 897 | |
| 898 | PJ_ASSERT_RETURN(pool && p_sdp, PJ_EINVAL); |
| 899 | |
| 900 | |
| 901 | /* Create and initialize basic SDP session */ |
| 902 | sdp = pj_pool_zalloc (pool, sizeof(pjmedia_sdp_session)); |
| 903 | |
| 904 | pj_gettimeofday(&tv); |
| 905 | sdp->origin.user = pj_str("pjsip-siprtp"); |
| 906 | sdp->origin.version = sdp->origin.id = tv.sec + 2208988800UL; |
| 907 | sdp->origin.net_type = pj_str("IN"); |
| 908 | sdp->origin.addr_type = pj_str("IP4"); |
| 909 | sdp->origin.addr = *pj_gethostname(); |
| 910 | sdp->name = pj_str("pjsip"); |
| 911 | |
| 912 | /* Since we only support one media stream at present, put the |
| 913 | * SDP connection line in the session level. |
| 914 | */ |
| 915 | sdp->conn = pj_pool_zalloc (pool, sizeof(pjmedia_sdp_conn)); |
| 916 | sdp->conn->net_type = pj_str("IN"); |
| 917 | sdp->conn->addr_type = pj_str("IP4"); |
| 918 | sdp->conn->addr = pj_str(app.local_addr); |
| 919 | |
| 920 | |
| 921 | /* SDP time and attributes. */ |
| 922 | sdp->time.start = sdp->time.stop = 0; |
| 923 | sdp->attr_count = 0; |
| 924 | |
| 925 | /* Create media stream 0: */ |
| 926 | |
| 927 | sdp->media_count = 1; |
| 928 | m = pj_pool_zalloc (pool, sizeof(pjmedia_sdp_media)); |
| 929 | sdp->media[0] = m; |
| 930 | |
| 931 | /* Standard media info: */ |
| 932 | m->desc.media = pj_str("audio"); |
| 933 | m->desc.port = audio->port; |
| 934 | m->desc.port_count = 1; |
| 935 | m->desc.transport = pj_str("RTP/AVP"); |
| 936 | |
| 937 | /* Add format and rtpmap for each codec. */ |
| 938 | m->desc.fmt_count = 1; |
| 939 | m->attr_count = 0; |
| 940 | |
| 941 | { |
| 942 | pjmedia_sdp_rtpmap rtpmap; |
| 943 | pjmedia_sdp_attr *attr; |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 944 | char ptstr[10]; |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 945 | |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 946 | sprintf(ptstr, "%d", app.audio_codec.pt); |
| 947 | pj_strdup2(pool, &m->desc.fmt[0], ptstr); |
| 948 | rtpmap.pt = m->desc.fmt[0]; |
| 949 | rtpmap.clock_rate = app.audio_codec.clock_rate; |
| 950 | rtpmap.enc_name = pj_str(app.audio_codec.name); |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 951 | rtpmap.param.slen = 0; |
| 952 | |
| 953 | pjmedia_sdp_rtpmap_to_attr(pool, &rtpmap, &attr); |
| 954 | m->attr[m->attr_count++] = attr; |
| 955 | } |
| 956 | |
| 957 | /* Add sendrecv attribute. */ |
| 958 | attr = pj_pool_zalloc(pool, sizeof(pjmedia_sdp_attr)); |
| 959 | attr->name = pj_str("sendrecv"); |
| 960 | m->attr[m->attr_count++] = attr; |
| 961 | |
| 962 | #if 1 |
| 963 | /* |
| 964 | * Add support telephony event |
| 965 | */ |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 966 | m->desc.fmt[m->desc.fmt_count++] = pj_str("121"); |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 967 | /* Add rtpmap. */ |
| 968 | attr = pj_pool_zalloc(pool, sizeof(pjmedia_sdp_attr)); |
| 969 | attr->name = pj_str("rtpmap"); |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 970 | attr->value = pj_str(":121 telephone-event/8000"); |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 971 | m->attr[m->attr_count++] = attr; |
| 972 | /* Add fmtp */ |
| 973 | attr = pj_pool_zalloc(pool, sizeof(pjmedia_sdp_attr)); |
| 974 | attr->name = pj_str("fmtp"); |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 975 | attr->value = pj_str(":121 0-15"); |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 976 | m->attr[m->attr_count++] = attr; |
| 977 | #endif |
| 978 | |
| 979 | /* Done */ |
| 980 | *p_sdp = sdp; |
| 981 | |
| 982 | return PJ_SUCCESS; |
| 983 | } |
| 984 | |
| 985 | |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 986 | /* |
| 987 | * Media thread |
| 988 | * |
| 989 | * This is the thread to send and receive both RTP and RTCP packets. |
| 990 | */ |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 991 | static int media_thread(void *arg) |
| 992 | { |
Benny Prijono | 9a0eab5 | 2006-04-04 19:43:24 +0000 | [diff] [blame] | 993 | enum { RTCP_INTERVAL = 5 }; |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 994 | struct media_stream *strm = arg; |
| 995 | char packet[1500]; |
Benny Prijono | 9a0eab5 | 2006-04-04 19:43:24 +0000 | [diff] [blame] | 996 | unsigned msec_interval; |
| 997 | pj_timestamp freq, next_rtp, next_rtcp; |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 998 | |
Benny Prijono | 9a0eab5 | 2006-04-04 19:43:24 +0000 | [diff] [blame] | 999 | msec_interval = strm->samples_per_frame * 1000 / strm->clock_rate; |
| 1000 | pj_get_timestamp_freq(&freq); |
| 1001 | |
| 1002 | pj_get_timestamp(&next_rtp); |
| 1003 | next_rtp.u64 += (freq.u64 * msec_interval / 1000); |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1004 | |
| 1005 | next_rtcp = next_rtp; |
Benny Prijono | 9a0eab5 | 2006-04-04 19:43:24 +0000 | [diff] [blame] | 1006 | next_rtcp.u64 += (freq.u64 * RTCP_INTERVAL); |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1007 | |
| 1008 | |
| 1009 | while (!strm->thread_quit_flag) { |
| 1010 | pj_fd_set_t set; |
Benny Prijono | 9a0eab5 | 2006-04-04 19:43:24 +0000 | [diff] [blame] | 1011 | pj_timestamp now, lesser; |
| 1012 | pj_time_val timeout; |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1013 | int rc; |
| 1014 | |
| 1015 | /* Determine how long to sleep */ |
Benny Prijono | 9a0eab5 | 2006-04-04 19:43:24 +0000 | [diff] [blame] | 1016 | if (next_rtp.u64 < next_rtcp.u64) |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1017 | lesser = next_rtp; |
| 1018 | else |
| 1019 | lesser = next_rtcp; |
| 1020 | |
Benny Prijono | 9a0eab5 | 2006-04-04 19:43:24 +0000 | [diff] [blame] | 1021 | pj_get_timestamp(&now); |
| 1022 | if (lesser.u64 <= now.u64) { |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1023 | timeout.sec = timeout.msec = 0; |
Benny Prijono | 9a0eab5 | 2006-04-04 19:43:24 +0000 | [diff] [blame] | 1024 | //printf("immediate "); fflush(stdout); |
| 1025 | } else { |
| 1026 | pj_uint64_t tick_delay; |
| 1027 | tick_delay = lesser.u64 - now.u64; |
| 1028 | timeout.sec = 0; |
| 1029 | timeout.msec = (pj_uint32_t)(tick_delay * 1000 / freq.u64); |
| 1030 | pj_time_val_normalize(&timeout); |
| 1031 | |
| 1032 | //printf("%d:%03d ", timeout.sec, timeout.msec); fflush(stdout); |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1033 | } |
| 1034 | |
| 1035 | PJ_FD_ZERO(&set); |
| 1036 | PJ_FD_SET(strm->rtp_sock, &set); |
| 1037 | PJ_FD_SET(strm->rtcp_sock, &set); |
| 1038 | |
| 1039 | rc = pj_sock_select(FD_SETSIZE, &set, NULL, NULL, &timeout); |
| 1040 | |
Benny Prijono | 9a0eab5 | 2006-04-04 19:43:24 +0000 | [diff] [blame] | 1041 | if (rc > 0 && PJ_FD_ISSET(strm->rtp_sock, &set)) { |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1042 | |
| 1043 | /* |
| 1044 | * Process incoming RTP packet. |
| 1045 | */ |
| 1046 | pj_status_t status; |
| 1047 | pj_ssize_t size; |
| 1048 | const pjmedia_rtp_hdr *hdr; |
| 1049 | const void *payload; |
| 1050 | unsigned payload_len; |
| 1051 | |
| 1052 | size = sizeof(packet); |
| 1053 | status = pj_sock_recv(strm->rtp_sock, packet, &size, 0); |
| 1054 | if (status != PJ_SUCCESS) { |
| 1055 | app_perror(THIS_FILE, "RTP recv() error", status); |
| 1056 | continue; |
| 1057 | } |
| 1058 | |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1059 | |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1060 | /* Decode RTP packet. */ |
| 1061 | status = pjmedia_rtp_decode_rtp(&strm->in_sess, |
| 1062 | packet, size, |
| 1063 | &hdr, |
| 1064 | &payload, &payload_len); |
| 1065 | if (status != PJ_SUCCESS) { |
| 1066 | app_perror(THIS_FILE, "RTP decode error", status); |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1067 | continue; |
| 1068 | } |
| 1069 | |
| 1070 | /* Update the RTCP session. */ |
| 1071 | pjmedia_rtcp_rx_rtp(&strm->rtcp, pj_ntohs(hdr->seq), |
Benny Prijono | 6996823 | 2006-04-06 19:29:03 +0000 | [diff] [blame] | 1072 | pj_ntohl(hdr->ts), payload_len); |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1073 | |
Benny Prijono | 6996823 | 2006-04-06 19:29:03 +0000 | [diff] [blame] | 1074 | /* Update RTP session */ |
| 1075 | pjmedia_rtp_session_update(&strm->in_sess, hdr, NULL); |
Benny Prijono | 9a0eab5 | 2006-04-04 19:43:24 +0000 | [diff] [blame] | 1076 | } |
| 1077 | |
| 1078 | if (rc > 0 && PJ_FD_ISSET(strm->rtcp_sock, &set)) { |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1079 | |
| 1080 | /* |
| 1081 | * Process incoming RTCP |
| 1082 | */ |
| 1083 | pj_status_t status; |
| 1084 | pj_ssize_t size; |
| 1085 | |
| 1086 | size = sizeof(packet); |
| 1087 | status = pj_sock_recv( strm->rtcp_sock, packet, &size, 0); |
| 1088 | if (status != PJ_SUCCESS) |
| 1089 | app_perror(THIS_FILE, "Error receiving RTCP packet", status); |
Benny Prijono | 6996823 | 2006-04-06 19:29:03 +0000 | [diff] [blame] | 1090 | else |
| 1091 | pjmedia_rtcp_rx_rtcp(&strm->rtcp, packet, size); |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | |
Benny Prijono | 9a0eab5 | 2006-04-04 19:43:24 +0000 | [diff] [blame] | 1095 | pj_get_timestamp(&now); |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1096 | |
Benny Prijono | 9a0eab5 | 2006-04-04 19:43:24 +0000 | [diff] [blame] | 1097 | if (next_rtp.u64 <= now.u64) { |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1098 | /* |
| 1099 | * Time to send RTP packet. |
| 1100 | */ |
| 1101 | pj_status_t status; |
| 1102 | const pjmedia_rtp_hdr *hdr; |
| 1103 | pj_ssize_t size; |
| 1104 | int hdrlen; |
| 1105 | |
| 1106 | /* Format RTP header */ |
| 1107 | status = pjmedia_rtp_encode_rtp( &strm->out_sess, strm->si.tx_pt, |
| 1108 | 0, /* marker bit */ |
| 1109 | strm->bytes_per_frame, |
| 1110 | strm->samples_per_frame, |
| 1111 | &hdr, &hdrlen); |
| 1112 | if (status == PJ_SUCCESS) { |
| 1113 | |
| 1114 | /* Copy RTP header to packet */ |
| 1115 | pj_memcpy(packet, hdr, hdrlen); |
| 1116 | |
| 1117 | /* Zero the payload */ |
| 1118 | pj_memset(packet+hdrlen, 0, strm->bytes_per_frame); |
| 1119 | |
| 1120 | /* Send RTP packet */ |
| 1121 | size = hdrlen + strm->bytes_per_frame; |
| 1122 | status = pj_sock_sendto( strm->rtp_sock, packet, &size, 0, |
| 1123 | &strm->si.rem_addr, |
| 1124 | sizeof(strm->si.rem_addr)); |
| 1125 | |
| 1126 | if (status != PJ_SUCCESS) |
| 1127 | app_perror(THIS_FILE, "Error sending RTP packet", status); |
| 1128 | |
| 1129 | } |
| 1130 | |
| 1131 | /* Update RTCP SR */ |
| 1132 | pjmedia_rtcp_tx_rtp( &strm->rtcp, (pj_uint16_t)strm->bytes_per_frame); |
| 1133 | |
| 1134 | /* Schedule next send */ |
Benny Prijono | 9a0eab5 | 2006-04-04 19:43:24 +0000 | [diff] [blame] | 1135 | next_rtp.u64 += (msec_interval * freq.u64 / 1000); |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
| 1138 | |
Benny Prijono | 9a0eab5 | 2006-04-04 19:43:24 +0000 | [diff] [blame] | 1139 | if (next_rtcp.u64 <= now.u64) { |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1140 | /* |
| 1141 | * Time to send RTCP packet. |
| 1142 | */ |
| 1143 | pjmedia_rtcp_pkt *rtcp_pkt; |
| 1144 | int rtcp_len; |
| 1145 | pj_sockaddr_in rem_addr; |
| 1146 | pj_ssize_t size; |
| 1147 | int port; |
| 1148 | pj_status_t status; |
| 1149 | |
| 1150 | /* Build RTCP packet */ |
| 1151 | pjmedia_rtcp_build_rtcp(&strm->rtcp, &rtcp_pkt, &rtcp_len); |
| 1152 | |
| 1153 | |
| 1154 | /* Calculate address based on RTP address */ |
| 1155 | rem_addr = strm->si.rem_addr; |
| 1156 | port = pj_ntohs(strm->si.rem_addr.sin_port) + 1; |
| 1157 | rem_addr.sin_port = pj_htons((pj_uint16_t)port); |
| 1158 | |
| 1159 | /* Send packet */ |
| 1160 | size = rtcp_len; |
| 1161 | status = pj_sock_sendto(strm->rtcp_sock, rtcp_pkt, &size, 0, |
| 1162 | &rem_addr, sizeof(rem_addr)); |
| 1163 | if (status != PJ_SUCCESS) { |
| 1164 | app_perror(THIS_FILE, "Error sending RTCP packet", status); |
| 1165 | } |
| 1166 | |
Benny Prijono | 6996823 | 2006-04-06 19:29:03 +0000 | [diff] [blame] | 1167 | /* Schedule next send */ |
Benny Prijono | 9a0eab5 | 2006-04-04 19:43:24 +0000 | [diff] [blame] | 1168 | next_rtcp.u64 += (freq.u64 * RTCP_INTERVAL); |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1169 | } |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1170 | } |
| 1171 | |
| 1172 | return 0; |
| 1173 | } |
| 1174 | |
| 1175 | |
| 1176 | /* Callback to be called when SDP negotiation is done in the call: */ |
| 1177 | static void call_on_media_update( pjsip_inv_session *inv, |
| 1178 | pj_status_t status) |
| 1179 | { |
| 1180 | struct call *call; |
| 1181 | pj_pool_t *pool; |
| 1182 | struct media_stream *audio; |
Benny Prijono | 49ce9a7 | 2006-04-05 16:56:19 +0000 | [diff] [blame] | 1183 | const pjmedia_sdp_session *local_sdp, *remote_sdp; |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1184 | struct codec *codec_desc = NULL; |
| 1185 | unsigned i; |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1186 | |
| 1187 | call = inv->mod_data[mod_siprtp.id]; |
| 1188 | pool = inv->dlg->pool; |
| 1189 | audio = &call->media[0]; |
| 1190 | |
| 1191 | /* If this is a mid-call media update, then destroy existing media */ |
| 1192 | if (audio->thread != NULL) |
| 1193 | destroy_call_media(call->index); |
| 1194 | |
| 1195 | |
| 1196 | /* Do nothing if media negotiation has failed */ |
| 1197 | if (status != PJ_SUCCESS) { |
| 1198 | app_perror(THIS_FILE, "SDP negotiation failed", status); |
| 1199 | return; |
| 1200 | } |
| 1201 | |
| 1202 | |
| 1203 | /* Capture stream definition from the SDP */ |
| 1204 | pjmedia_sdp_neg_get_active_local(inv->neg, &local_sdp); |
| 1205 | pjmedia_sdp_neg_get_active_remote(inv->neg, &remote_sdp); |
| 1206 | |
| 1207 | status = pjmedia_stream_info_from_sdp(&audio->si, inv->pool, app.med_endpt, |
| 1208 | local_sdp, remote_sdp, 0); |
| 1209 | if (status != PJ_SUCCESS) { |
| 1210 | app_perror(THIS_FILE, "Error creating stream info from SDP", status); |
| 1211 | return; |
| 1212 | } |
| 1213 | |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1214 | /* Get the remainder of codec information from codec descriptor */ |
| 1215 | if (audio->si.fmt.pt == app.audio_codec.pt) |
| 1216 | codec_desc = &app.audio_codec; |
| 1217 | else { |
| 1218 | /* Find the codec description in codec array */ |
| 1219 | for (i=0; i<PJ_ARRAY_SIZE(audio_codecs); ++i) { |
| 1220 | if (audio_codecs[i].pt == audio->si.fmt.pt) { |
| 1221 | codec_desc = &audio_codecs[i]; |
| 1222 | break; |
| 1223 | } |
| 1224 | } |
| 1225 | |
| 1226 | if (codec_desc == NULL) { |
| 1227 | PJ_LOG(3, (THIS_FILE, "Error: Invalid codec payload type")); |
| 1228 | return; |
| 1229 | } |
| 1230 | } |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1231 | |
| 1232 | audio->clock_rate = audio->si.fmt.sample_rate; |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1233 | audio->samples_per_frame = audio->clock_rate * codec_desc->ptime / 1000; |
| 1234 | audio->bytes_per_frame = codec_desc->bit_rate * codec_desc->ptime / 1000 / 8; |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1235 | |
| 1236 | |
| 1237 | pjmedia_rtp_session_init(&audio->out_sess, audio->si.tx_pt, |
Benny Prijono | 9d8a873 | 2006-04-04 13:39:58 +0000 | [diff] [blame] | 1238 | pj_rand()); |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1239 | pjmedia_rtp_session_init(&audio->in_sess, audio->si.fmt.pt, 0); |
Benny Prijono | 6996823 | 2006-04-06 19:29:03 +0000 | [diff] [blame] | 1240 | pjmedia_rtcp_init(&audio->rtcp, audio->clock_rate, |
| 1241 | audio->samples_per_frame, 0); |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1242 | |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1243 | |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1244 | |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1245 | /* Start media thread. */ |
| 1246 | audio->thread_quit_flag = 0; |
| 1247 | status = pj_thread_create( inv->pool, "media", &media_thread, audio, |
| 1248 | 0, 0, &audio->thread); |
| 1249 | if (status != PJ_SUCCESS) { |
| 1250 | app_perror(THIS_FILE, "Error creating media thread", status); |
| 1251 | } |
| 1252 | } |
| 1253 | |
| 1254 | |
| 1255 | |
| 1256 | /* Destroy call's media */ |
| 1257 | static void destroy_call_media(unsigned call_index) |
| 1258 | { |
| 1259 | struct media_stream *audio = &app.call[call_index].media[0]; |
| 1260 | |
| 1261 | if (audio->thread) { |
| 1262 | audio->thread_quit_flag = 1; |
| 1263 | pj_thread_join(audio->thread); |
| 1264 | pj_thread_destroy(audio->thread); |
| 1265 | audio->thread = NULL; |
| 1266 | audio->thread_quit_flag = 0; |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1267 | |
| 1268 | /* Flush RTP/RTCP packets */ |
| 1269 | { |
| 1270 | pj_fd_set_t set; |
| 1271 | pj_time_val timeout = {0, 0}; |
| 1272 | char packet[1500]; |
| 1273 | pj_ssize_t size; |
| 1274 | pj_status_t status; |
| 1275 | int rc; |
| 1276 | |
| 1277 | do { |
| 1278 | PJ_FD_ZERO(&set); |
| 1279 | PJ_FD_SET(audio->rtp_sock, &set); |
| 1280 | PJ_FD_SET(audio->rtcp_sock, &set); |
| 1281 | |
| 1282 | rc = pj_sock_select(FD_SETSIZE, &set, NULL, NULL, &timeout); |
| 1283 | if (rc > 0 && PJ_FD_ISSET(audio->rtp_sock, &set)) { |
| 1284 | size = sizeof(packet); |
| 1285 | status = pj_sock_recv(audio->rtp_sock, packet, &size, 0); |
| 1286 | |
| 1287 | } |
| 1288 | if (rc > 0 && PJ_FD_ISSET(audio->rtcp_sock, &set)) { |
| 1289 | size = sizeof(packet); |
| 1290 | status = pj_sock_recv(audio->rtcp_sock, packet, &size, 0); |
| 1291 | } |
| 1292 | |
| 1293 | } while (rc > 0); |
| 1294 | } |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1295 | } |
| 1296 | } |
| 1297 | |
| 1298 | |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1299 | /***************************************************************************** |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1300 | * USER INTERFACE STUFFS |
| 1301 | */ |
| 1302 | |
| 1303 | static const char *good_number(char *buf, pj_int32_t val) |
| 1304 | { |
| 1305 | if (val < 1000) { |
| 1306 | pj_ansi_sprintf(buf, "%d", val); |
| 1307 | } else if (val < 1000000) { |
| 1308 | pj_ansi_sprintf(buf, "%d.%dK", |
| 1309 | val / 1000, |
| 1310 | (val % 1000) / 100); |
| 1311 | } else { |
| 1312 | pj_ansi_sprintf(buf, "%d.%02dM", |
| 1313 | val / 1000000, |
| 1314 | (val % 1000000) / 10000); |
| 1315 | } |
| 1316 | |
| 1317 | return buf; |
| 1318 | } |
| 1319 | |
| 1320 | |
| 1321 | static void print_call(int call_index) |
| 1322 | { |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1323 | struct call *call = &app.call[call_index]; |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1324 | int len; |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1325 | pjsip_inv_session *inv = call->inv; |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1326 | pjsip_dialog *dlg = inv->dlg; |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1327 | struct media_stream *audio = &call->media[0]; |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1328 | char userinfo[128]; |
Benny Prijono | 6996823 | 2006-04-06 19:29:03 +0000 | [diff] [blame] | 1329 | char duration[80], last_update[80]; |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1330 | char bps[16], ipbps[16], packets[16], bytes[16], ipbytes[16]; |
Benny Prijono | 6996823 | 2006-04-06 19:29:03 +0000 | [diff] [blame] | 1331 | pj_time_val now; |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1332 | |
Benny Prijono | 6996823 | 2006-04-06 19:29:03 +0000 | [diff] [blame] | 1333 | pj_gettimeofday(&now); |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1334 | |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1335 | /* Print duration */ |
Benny Prijono | c323807 | 2006-04-05 22:05:04 +0000 | [diff] [blame] | 1336 | if (inv->state >= PJSIP_INV_STATE_CONFIRMED) { |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1337 | |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1338 | PJ_TIME_VAL_SUB(now, call->connect_time); |
| 1339 | |
Benny Prijono | 49ce9a7 | 2006-04-05 16:56:19 +0000 | [diff] [blame] | 1340 | sprintf(duration, " [duration: %02ld:%02ld:%02ld.%03ld]", |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1341 | now.sec / 3600, |
| 1342 | (now.sec % 3600) / 60, |
| 1343 | (now.sec % 60), |
| 1344 | now.msec); |
| 1345 | |
| 1346 | } else { |
| 1347 | duration[0] = '\0'; |
| 1348 | } |
| 1349 | |
| 1350 | |
| 1351 | |
| 1352 | /* Call number and state */ |
| 1353 | printf("Call #%d: %s%s\n", call_index, pjsip_inv_state_name(inv->state), |
| 1354 | duration); |
| 1355 | |
| 1356 | |
| 1357 | |
| 1358 | /* Call identification */ |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1359 | len = pjsip_hdr_print_on(dlg->remote.info, userinfo, sizeof(userinfo)); |
| 1360 | if (len < 1) |
| 1361 | pj_ansi_strcpy(userinfo, "<--uri too long-->"); |
| 1362 | else |
| 1363 | userinfo[len] = '\0'; |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1364 | |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1365 | printf(" %s\n", userinfo); |
| 1366 | |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1367 | |
Benny Prijono | 5b3b460 | 2006-04-06 20:36:27 +0000 | [diff] [blame] | 1368 | if (call->inv == NULL || call->inv->state < PJSIP_INV_STATE_CONFIRMED || |
| 1369 | call->connect_time.sec == 0) |
| 1370 | { |
| 1371 | return; |
| 1372 | } |
| 1373 | |
| 1374 | |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1375 | /* Signaling quality */ |
| 1376 | { |
| 1377 | char pdd[64], connectdelay[64]; |
| 1378 | pj_time_val t; |
| 1379 | |
| 1380 | if (call->response_time.sec) { |
| 1381 | t = call->response_time; |
| 1382 | PJ_TIME_VAL_SUB(t, call->start_time); |
Benny Prijono | 49ce9a7 | 2006-04-05 16:56:19 +0000 | [diff] [blame] | 1383 | sprintf(pdd, "got 1st response in %ld ms", PJ_TIME_VAL_MSEC(t)); |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1384 | } else { |
| 1385 | pdd[0] = '\0'; |
| 1386 | } |
| 1387 | |
| 1388 | if (call->connect_time.sec) { |
| 1389 | t = call->connect_time; |
| 1390 | PJ_TIME_VAL_SUB(t, call->start_time); |
Benny Prijono | 49ce9a7 | 2006-04-05 16:56:19 +0000 | [diff] [blame] | 1391 | sprintf(connectdelay, ", connected after: %ld ms", |
| 1392 | PJ_TIME_VAL_MSEC(t)); |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1393 | } else { |
| 1394 | connectdelay[0] = '\0'; |
| 1395 | } |
| 1396 | |
| 1397 | printf(" Signaling quality: %s%s\n", pdd, connectdelay); |
| 1398 | } |
| 1399 | |
| 1400 | |
Benny Prijono | 49ce9a7 | 2006-04-05 16:56:19 +0000 | [diff] [blame] | 1401 | printf(" Stream #0: audio %.*s@%dHz, %dms/frame, %sB/s (%sB/s +IP hdr)\n", |
| 1402 | (int)audio->si.fmt.encoding_name.slen, |
| 1403 | audio->si.fmt.encoding_name.ptr, |
| 1404 | audio->clock_rate, |
| 1405 | audio->samples_per_frame * 1000 / audio->clock_rate, |
| 1406 | good_number(bps, audio->bytes_per_frame * audio->clock_rate / audio->samples_per_frame), |
| 1407 | good_number(ipbps, (audio->bytes_per_frame+32) * audio->clock_rate / audio->samples_per_frame)); |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1408 | |
Benny Prijono | 6996823 | 2006-04-06 19:29:03 +0000 | [diff] [blame] | 1409 | if (audio->rtcp.stat.rx.update_cnt == 0) |
| 1410 | strcpy(last_update, "never"); |
| 1411 | else { |
| 1412 | pj_gettimeofday(&now); |
| 1413 | PJ_TIME_VAL_SUB(now, audio->rtcp.stat.rx.update); |
| 1414 | sprintf(last_update, "%02dh:%02dm:%02d.%03ds ago", |
| 1415 | now.sec / 3600, |
| 1416 | (now.sec % 3600) / 60, |
| 1417 | now.sec % 60, |
| 1418 | now.msec); |
| 1419 | } |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1420 | |
Benny Prijono | 6996823 | 2006-04-06 19:29:03 +0000 | [diff] [blame] | 1421 | printf(" RX stat last update: %s\n" |
| 1422 | " total %s packets %sB received (%sB +IP hdr)%s\n" |
| 1423 | " pkt loss=%d (%3.1f%%), dup=%d (%3.1f%%), reorder=%d (%3.1f%%)%s\n" |
Benny Prijono | 28903eb | 2006-04-06 21:01:25 +0000 | [diff] [blame^] | 1424 | " (msec) min avg max last\n" |
| 1425 | " loss period: %7.3f %7.3f %7.3f %7.3f%s\n" |
| 1426 | " jitter : %7.3f %7.3f %7.3f %7.3f%s\n", |
Benny Prijono | 6996823 | 2006-04-06 19:29:03 +0000 | [diff] [blame] | 1427 | last_update, |
| 1428 | good_number(packets, audio->rtcp.stat.rx.pkt), |
| 1429 | good_number(bytes, audio->rtcp.stat.rx.bytes), |
| 1430 | good_number(ipbytes, audio->rtcp.stat.rx.bytes + audio->rtcp.stat.rx.pkt * 32), |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1431 | "", |
Benny Prijono | 6996823 | 2006-04-06 19:29:03 +0000 | [diff] [blame] | 1432 | audio->rtcp.stat.rx.loss, |
| 1433 | audio->rtcp.stat.rx.loss * 100.0 / audio->rtcp.stat.rx.pkt, |
| 1434 | audio->rtcp.stat.rx.dup, |
| 1435 | audio->rtcp.stat.rx.dup * 100.0 / audio->rtcp.stat.rx.pkt, |
| 1436 | audio->rtcp.stat.rx.reorder, |
| 1437 | audio->rtcp.stat.rx.reorder * 100.0 / audio->rtcp.stat.rx.pkt, |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1438 | "", |
Benny Prijono | 6996823 | 2006-04-06 19:29:03 +0000 | [diff] [blame] | 1439 | audio->rtcp.stat.rx.loss_period.min / 1000.0, |
| 1440 | audio->rtcp.stat.rx.loss_period.avg / 1000.0, |
| 1441 | audio->rtcp.stat.rx.loss_period.max / 1000.0, |
| 1442 | audio->rtcp.stat.rx.loss_period.last / 1000.0, |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1443 | "", |
Benny Prijono | 6996823 | 2006-04-06 19:29:03 +0000 | [diff] [blame] | 1444 | audio->rtcp.stat.rx.jitter.min / 1000.0, |
| 1445 | audio->rtcp.stat.rx.jitter.avg / 1000.0, |
| 1446 | audio->rtcp.stat.rx.jitter.max / 1000.0, |
| 1447 | audio->rtcp.stat.rx.jitter.last / 1000.0, |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1448 | "" |
| 1449 | ); |
| 1450 | |
| 1451 | |
Benny Prijono | 6996823 | 2006-04-06 19:29:03 +0000 | [diff] [blame] | 1452 | if (audio->rtcp.stat.tx.update_cnt == 0) |
| 1453 | strcpy(last_update, "never"); |
| 1454 | else { |
| 1455 | pj_gettimeofday(&now); |
| 1456 | PJ_TIME_VAL_SUB(now, audio->rtcp.stat.tx.update); |
| 1457 | sprintf(last_update, "%02dh:%02dm:%02d.%03ds ago", |
| 1458 | now.sec / 3600, |
| 1459 | (now.sec % 3600) / 60, |
| 1460 | now.sec % 60, |
| 1461 | now.msec); |
| 1462 | } |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1463 | |
Benny Prijono | 6996823 | 2006-04-06 19:29:03 +0000 | [diff] [blame] | 1464 | printf(" TX stat last update: %s\n" |
| 1465 | " total %s packets %sB received (%sB +IP hdr)%s\n" |
| 1466 | " pkt loss=%d (%3.1f%%), dup=%d (%3.1f%%), reorder=%d (%3.1f%%)%s\n" |
Benny Prijono | 28903eb | 2006-04-06 21:01:25 +0000 | [diff] [blame^] | 1467 | " (msec) min avg max last\n" |
| 1468 | " loss period: %7.3f %7.3f %7.3f %7.3f%s\n" |
| 1469 | " jitter : %7.3f %7.3f %7.3f %7.3f%s\n", |
Benny Prijono | 6996823 | 2006-04-06 19:29:03 +0000 | [diff] [blame] | 1470 | last_update, |
| 1471 | good_number(packets, audio->rtcp.stat.tx.pkt), |
| 1472 | good_number(bytes, audio->rtcp.stat.tx.bytes), |
| 1473 | good_number(ipbytes, audio->rtcp.stat.tx.bytes + audio->rtcp.stat.tx.pkt * 32), |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1474 | "", |
Benny Prijono | 6996823 | 2006-04-06 19:29:03 +0000 | [diff] [blame] | 1475 | audio->rtcp.stat.tx.loss, |
| 1476 | audio->rtcp.stat.tx.loss * 100.0 / audio->rtcp.stat.tx.pkt, |
| 1477 | audio->rtcp.stat.tx.dup, |
| 1478 | audio->rtcp.stat.tx.dup * 100.0 / audio->rtcp.stat.tx.pkt, |
| 1479 | audio->rtcp.stat.tx.reorder, |
| 1480 | audio->rtcp.stat.tx.reorder * 100.0 / audio->rtcp.stat.tx.pkt, |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1481 | "", |
Benny Prijono | 6996823 | 2006-04-06 19:29:03 +0000 | [diff] [blame] | 1482 | audio->rtcp.stat.tx.loss_period.min / 1000.0, |
| 1483 | audio->rtcp.stat.tx.loss_period.avg / 1000.0, |
| 1484 | audio->rtcp.stat.tx.loss_period.max / 1000.0, |
| 1485 | audio->rtcp.stat.tx.loss_period.last / 1000.0, |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1486 | "", |
Benny Prijono | 6996823 | 2006-04-06 19:29:03 +0000 | [diff] [blame] | 1487 | audio->rtcp.stat.tx.jitter.min / 1000.0, |
| 1488 | audio->rtcp.stat.tx.jitter.avg / 1000.0, |
| 1489 | audio->rtcp.stat.tx.jitter.max / 1000.0, |
| 1490 | audio->rtcp.stat.tx.jitter.last / 1000.0, |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1491 | "" |
| 1492 | ); |
| 1493 | |
Benny Prijono | 6996823 | 2006-04-06 19:29:03 +0000 | [diff] [blame] | 1494 | |
Benny Prijono | 28903eb | 2006-04-06 21:01:25 +0000 | [diff] [blame^] | 1495 | printf(" RTT msec : %7.3f %7.3f %7.3f %7.3f%s\n", |
Benny Prijono | 6996823 | 2006-04-06 19:29:03 +0000 | [diff] [blame] | 1496 | audio->rtcp.stat.rtt.min / 1000.0, |
| 1497 | audio->rtcp.stat.rtt.avg / 1000.0, |
| 1498 | audio->rtcp.stat.rtt.max / 1000.0, |
| 1499 | audio->rtcp.stat.rtt.last / 1000.0, |
| 1500 | "" |
| 1501 | ); |
Benny Prijono | a1d03b4 | 2006-04-05 12:53:42 +0000 | [diff] [blame] | 1502 | |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1503 | } |
| 1504 | |
| 1505 | |
| 1506 | static void list_calls() |
| 1507 | { |
| 1508 | unsigned i; |
| 1509 | puts("List all calls:"); |
| 1510 | for (i=0; i<app.max_calls; ++i) { |
| 1511 | if (!app.call[i].inv) |
| 1512 | continue; |
| 1513 | print_call(i); |
| 1514 | } |
| 1515 | } |
| 1516 | |
| 1517 | static void hangup_call(unsigned index) |
| 1518 | { |
| 1519 | pjsip_tx_data *tdata; |
| 1520 | pj_status_t status; |
| 1521 | |
| 1522 | if (app.call[index].inv == NULL) |
| 1523 | return; |
| 1524 | |
| 1525 | status = pjsip_inv_end_session(app.call[index].inv, 603, NULL, &tdata); |
| 1526 | if (status==PJ_SUCCESS && tdata!=NULL) |
| 1527 | pjsip_inv_send_msg(app.call[index].inv, tdata); |
| 1528 | } |
| 1529 | |
| 1530 | static void hangup_all_calls() |
| 1531 | { |
| 1532 | unsigned i; |
| 1533 | for (i=0; i<app.max_calls; ++i) { |
| 1534 | if (!app.call[i].inv) |
| 1535 | continue; |
| 1536 | hangup_call(i); |
| 1537 | } |
| 1538 | } |
| 1539 | |
| 1540 | static pj_bool_t simple_input(const char *title, char *buf, pj_size_t len) |
| 1541 | { |
| 1542 | char *p; |
| 1543 | |
| 1544 | printf("%s (empty to cancel): ", title); fflush(stdout); |
| 1545 | fgets(buf, len, stdin); |
| 1546 | |
| 1547 | /* Remove trailing newlines. */ |
| 1548 | for (p=buf; ; ++p) { |
| 1549 | if (*p=='\r' || *p=='\n') *p='\0'; |
| 1550 | else if (!*p) break; |
| 1551 | } |
| 1552 | |
| 1553 | if (!*buf) |
| 1554 | return PJ_FALSE; |
| 1555 | |
| 1556 | return PJ_TRUE; |
| 1557 | } |
| 1558 | |
| 1559 | |
| 1560 | static const char *MENU = |
| 1561 | "\n" |
| 1562 | "Enter menu character:\n" |
| 1563 | " l List all calls\n" |
| 1564 | " h Hangup a call\n" |
| 1565 | " H Hangup all calls\n" |
| 1566 | " q Quit\n" |
| 1567 | "\n"; |
| 1568 | |
| 1569 | |
| 1570 | /* Main screen menu */ |
| 1571 | static void console_main() |
| 1572 | { |
| 1573 | char input1[10]; |
| 1574 | unsigned i; |
| 1575 | |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1576 | printf("%s", MENU); |
| 1577 | |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1578 | for (;;) { |
| 1579 | printf(">>> "); fflush(stdout); |
| 1580 | fgets(input1, sizeof(input1), stdin); |
| 1581 | |
| 1582 | switch (input1[0]) { |
| 1583 | case 'l': |
| 1584 | list_calls(); |
| 1585 | break; |
| 1586 | |
| 1587 | case 'h': |
| 1588 | if (!simple_input("Call number to hangup", input1, sizeof(input1))) |
| 1589 | break; |
| 1590 | |
| 1591 | i = atoi(input1); |
| 1592 | hangup_call(i); |
| 1593 | break; |
| 1594 | |
| 1595 | case 'H': |
| 1596 | hangup_all_calls(); |
| 1597 | break; |
| 1598 | |
| 1599 | case 'q': |
| 1600 | goto on_exit; |
| 1601 | |
| 1602 | default: |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1603 | puts("Invalid command"); |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1604 | printf("%s", MENU); |
| 1605 | break; |
| 1606 | } |
| 1607 | |
| 1608 | fflush(stdout); |
| 1609 | } |
| 1610 | |
| 1611 | on_exit: |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1612 | hangup_all_calls(); |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1613 | } |
| 1614 | |
| 1615 | |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1616 | /***************************************************************************** |
| 1617 | * Below is a simple module to log all incoming and outgoing SIP messages |
| 1618 | */ |
| 1619 | |
| 1620 | |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1621 | /* Notification on incoming messages */ |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1622 | static pj_bool_t logger_on_rx_msg(pjsip_rx_data *rdata) |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1623 | { |
| 1624 | PJ_LOG(4,(THIS_FILE, "RX %d bytes %s from %s:%d:\n" |
| 1625 | "%s\n" |
| 1626 | "--end msg--", |
| 1627 | rdata->msg_info.len, |
| 1628 | pjsip_rx_data_get_info(rdata), |
| 1629 | rdata->pkt_info.src_name, |
| 1630 | rdata->pkt_info.src_port, |
| 1631 | rdata->msg_info.msg_buf)); |
| 1632 | |
| 1633 | /* Always return false, otherwise messages will not get processed! */ |
| 1634 | return PJ_FALSE; |
| 1635 | } |
| 1636 | |
| 1637 | /* Notification on outgoing messages */ |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1638 | static pj_status_t logger_on_tx_msg(pjsip_tx_data *tdata) |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1639 | { |
| 1640 | |
| 1641 | /* Important note: |
| 1642 | * tp_info field is only valid after outgoing messages has passed |
| 1643 | * transport layer. So don't try to access tp_info when the module |
| 1644 | * has lower priority than transport layer. |
| 1645 | */ |
| 1646 | |
| 1647 | PJ_LOG(4,(THIS_FILE, "TX %d bytes %s to %s:%d:\n" |
| 1648 | "%s\n" |
| 1649 | "--end msg--", |
| 1650 | (tdata->buf.cur - tdata->buf.start), |
| 1651 | pjsip_tx_data_get_info(tdata), |
| 1652 | tdata->tp_info.dst_name, |
| 1653 | tdata->tp_info.dst_port, |
| 1654 | tdata->buf.start)); |
| 1655 | |
| 1656 | /* Always return success, otherwise message will not get sent! */ |
| 1657 | return PJ_SUCCESS; |
| 1658 | } |
| 1659 | |
| 1660 | /* The module instance. */ |
| 1661 | static pjsip_module msg_logger = |
| 1662 | { |
| 1663 | NULL, NULL, /* prev, next. */ |
| 1664 | { "mod-siprtp-log", 14 }, /* Name. */ |
| 1665 | -1, /* Id */ |
| 1666 | PJSIP_MOD_PRIORITY_TRANSPORT_LAYER-1,/* Priority */ |
| 1667 | NULL, /* load() */ |
| 1668 | NULL, /* start() */ |
| 1669 | NULL, /* stop() */ |
| 1670 | NULL, /* unload() */ |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1671 | &logger_on_rx_msg, /* on_rx_request() */ |
| 1672 | &logger_on_rx_msg, /* on_rx_response() */ |
| 1673 | &logger_on_tx_msg, /* on_tx_request. */ |
| 1674 | &logger_on_tx_msg, /* on_tx_response() */ |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1675 | NULL, /* on_tsx_state() */ |
| 1676 | |
| 1677 | }; |
| 1678 | |
| 1679 | |
| 1680 | |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1681 | /***************************************************************************** |
| 1682 | * Console application custom logging: |
| 1683 | */ |
| 1684 | |
| 1685 | |
| 1686 | static FILE *log_file; |
| 1687 | |
| 1688 | |
| 1689 | static void app_log_writer(int level, const char *buffer, int len) |
| 1690 | { |
| 1691 | /* Write to both stdout and file. */ |
| 1692 | |
| 1693 | if (level <= app.app_log_level) |
| 1694 | pj_log_write(level, buffer, len); |
| 1695 | |
| 1696 | if (log_file) { |
| 1697 | fwrite(buffer, len, 1, log_file); |
| 1698 | fflush(log_file); |
| 1699 | } |
| 1700 | } |
| 1701 | |
| 1702 | |
| 1703 | pj_status_t app_logging_init(void) |
| 1704 | { |
| 1705 | /* Redirect log function to ours */ |
| 1706 | |
| 1707 | pj_log_set_log_func( &app_log_writer ); |
| 1708 | |
| 1709 | /* If output log file is desired, create the file: */ |
| 1710 | |
| 1711 | if (app.log_filename) { |
| 1712 | log_file = fopen(app.log_filename, "wt"); |
| 1713 | if (log_file == NULL) { |
| 1714 | PJ_LOG(1,(THIS_FILE, "Unable to open log file %s", |
| 1715 | app.log_filename)); |
| 1716 | return -1; |
| 1717 | } |
| 1718 | } |
| 1719 | |
| 1720 | return PJ_SUCCESS; |
| 1721 | } |
| 1722 | |
| 1723 | |
| 1724 | void app_logging_shutdown(void) |
| 1725 | { |
| 1726 | /* Close logging file, if any: */ |
| 1727 | |
| 1728 | if (log_file) { |
| 1729 | fclose(log_file); |
| 1730 | log_file = NULL; |
| 1731 | } |
| 1732 | } |
| 1733 | |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1734 | |
| 1735 | /* |
| 1736 | * main() |
| 1737 | */ |
| 1738 | int main(int argc, char *argv[]) |
| 1739 | { |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1740 | unsigned i; |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1741 | pj_status_t status; |
| 1742 | |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1743 | /* Must init PJLIB first */ |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1744 | status = pj_init(); |
| 1745 | if (status != PJ_SUCCESS) |
| 1746 | return 1; |
| 1747 | |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1748 | /* Get command line options */ |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1749 | status = init_options(argc, argv); |
| 1750 | if (status != PJ_SUCCESS) |
| 1751 | return 1; |
| 1752 | |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1753 | /* Init logging */ |
| 1754 | status = app_logging_init(); |
| 1755 | if (status != PJ_SUCCESS) |
| 1756 | return 1; |
| 1757 | |
| 1758 | /* Init SIP etc */ |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1759 | status = init_sip(); |
| 1760 | if (status != PJ_SUCCESS) { |
| 1761 | app_perror(THIS_FILE, "Initialization has failed", status); |
| 1762 | destroy_sip(); |
| 1763 | return 1; |
| 1764 | } |
| 1765 | |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1766 | /* Register module to log incoming/outgoing messages */ |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1767 | pjsip_endpt_register_module(app.sip_endpt, &msg_logger); |
| 1768 | |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1769 | /* Init media */ |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1770 | status = init_media(); |
| 1771 | if (status != PJ_SUCCESS) { |
| 1772 | app_perror(THIS_FILE, "Media initialization failed", status); |
| 1773 | destroy_sip(); |
| 1774 | return 1; |
| 1775 | } |
| 1776 | |
Benny Prijono | 9a0eab5 | 2006-04-04 19:43:24 +0000 | [diff] [blame] | 1777 | /* Start worker threads */ |
| 1778 | for (i=0; i<app.thread_count; ++i) { |
| 1779 | pj_thread_create( app.pool, "app", &worker_thread, NULL, |
| 1780 | 0, 0, &app.thread[i]); |
| 1781 | } |
| 1782 | |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1783 | /* If URL is specified, then make call immediately */ |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1784 | if (app.uri_to_call.slen) { |
| 1785 | unsigned i; |
| 1786 | |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1787 | PJ_LOG(3,(THIS_FILE, "Making %d calls to %s..", app.max_calls, |
| 1788 | app.uri_to_call.ptr)); |
| 1789 | |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1790 | for (i=0; i<app.max_calls; ++i) { |
| 1791 | status = make_call(&app.uri_to_call); |
| 1792 | if (status != PJ_SUCCESS) { |
| 1793 | app_perror(THIS_FILE, "Error making call", status); |
| 1794 | break; |
| 1795 | } |
| 1796 | } |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1797 | |
| 1798 | } else { |
| 1799 | |
| 1800 | PJ_LOG(3,(THIS_FILE, "Ready for incoming calls (max=%d)", |
| 1801 | app.max_calls)); |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1802 | } |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1803 | |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1804 | /* Start user interface loop */ |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1805 | console_main(); |
| 1806 | |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1807 | |
| 1808 | /* Shutting down... */ |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1809 | destroy_media(); |
| 1810 | destroy_sip(); |
Benny Prijono | 4adcb91 | 2006-04-04 13:12:38 +0000 | [diff] [blame] | 1811 | app_logging_shutdown(); |
| 1812 | |
Benny Prijono | 60b980e | 2006-04-03 22:41:26 +0000 | [diff] [blame] | 1813 | |
| 1814 | return 0; |
| 1815 | } |
| 1816 | |