| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
static const char *USAGE = |
| 26 |
" PURPOSE: \n" |
| 27 |
" This program establishes SIP INVITE session and media, and calculate \n" |
| 28 |
" the media quality (packet lost, jitter, rtt, etc.). Unlike normal \n" |
| 29 |
" pjmedia applications, this program bypasses all pjmedia stream \n" |
| 30 |
" framework and transmit encoded RTP packets manually using own thread. \n" |
| 31 |
"\n" |
| 32 |
" USAGE:\n" |
| 33 |
" siprtp [options] => to start in server mode\n" |
| 34 |
" siprtp [options] URL => to start in client mode\n" |
| 35 |
"\n" |
| 36 |
" Program options:\n" |
| 37 |
" --count=N, -c Set number of calls to create (default:1) \n" |
| 38 |
" --gap=N -g Set call gapping to N msec (default:0)\n" |
| 39 |
" --duration=SEC, -d Set maximum call duration (default:unlimited) \n" |
| 40 |
" --auto-quit, -q Quit when calls have been completed (default:no)\n" |
| 41 |
" --call-report -R Display report on call termination (default:yes)\n" |
| 42 |
"\n" |
| 43 |
" Address and ports options:\n" |
| 44 |
" --local-port=PORT,-p Set local SIP port (default: 5060)\n" |
| 45 |
" --rtp-port=PORT, -r Set start of RTP port (default: 4000)\n" |
| 46 |
" --ip-addr=IP, -i Set local IP address to use (otherwise it will\n" |
| 47 |
" try to determine local IP address from hostname)\n" |
| 48 |
"\n" |
| 49 |
" Logging Options:\n" |
| 50 |
" --log-level=N, -l Set log verbosity level (default=5)\n" |
| 51 |
" --app-log-level=N Set app screen log verbosity (default=3)\n" |
| 52 |
" --log-file=FILE Write log to file FILE\n" |
| 53 |
" --report-file=FILE Write report to file FILE\n" |
| 54 |
"\n" |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
#include <pjsip.h> |
| 70 |
#include <pjmedia.h> |
| 71 |
#include <pjmedia-codec.h> |
| 72 |
#include <pjsip_ua.h> |
| 73 |
#include <pjsip_simple.h> |
| 74 |
#include <pjlib-util.h> |
| 75 |
#include <pjlib.h> |
| 76 |
|
| 77 |
#include <stdlib.h> |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
#if PJ_HAS_HIGH_RES_TIMER==0 |
| 91 |
# error "High resolution timer is needed for this sample" |
| 92 |
#endif |
| 93 |
|
| 94 |
#define THIS_FILE "siprtp.c" |
| 95 |
#define MAX_CALLS 1024 |
| 96 |
#define RTP_START_PORT 4000 |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
struct codec |
| 101 |
{ |
| 102 |
unsigned pt; |
| 103 |
char* name; |
| 104 |
unsigned clock_rate; |
| 105 |
unsigned bit_rate; |
| 106 |
unsigned ptime; |
| 107 |
char* description; |
| 108 |
}; |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
struct media_stream |
| 113 |
{ |
| 114 |
|
| 115 |
unsigned call_index; |
| 116 |
unsigned media_index; |
| 117 |
pjmedia_transport *transport; |
| 118 |
|
| 119 |
|
| 120 |
pj_bool_t active; |
| 121 |
|
| 122 |
|
| 123 |
pjmedia_stream_info si; |
| 124 |
|
| 125 |
|
| 126 |
unsigned clock_rate; |
| 127 |
unsigned samples_per_frame; |
| 128 |
unsigned bytes_per_frame; |
| 129 |
|
| 130 |
|
| 131 |
pjmedia_rtp_session out_sess; |
| 132 |
pjmedia_rtp_session in_sess; |
| 133 |
|
| 134 |
|
| 135 |
pjmedia_rtcp_session rtcp; |
| 136 |
|
| 137 |
|
| 138 |
pj_bool_t thread_quit_flag; |
| 139 |
pj_thread_t *thread; |
| 140 |
}; |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
struct call |
| 147 |
{ |
| 148 |
unsigned index; |
| 149 |
pjsip_inv_session *inv; |
| 150 |
unsigned media_count; |
| 151 |
struct media_stream media[1]; |
| 152 |
pj_time_val start_time; |
| 153 |
pj_time_val response_time; |
| 154 |
pj_time_val connect_time; |
| 155 |
|
| 156 |
pj_timer_entry d_timer; |
| 157 |
}; |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
static struct app |
| 162 |
{ |
| 163 |
unsigned max_calls; |
| 164 |
unsigned call_gap; |
| 165 |
pj_bool_t call_report; |
| 166 |
unsigned uac_calls; |
| 167 |
unsigned duration; |
| 168 |
pj_bool_t auto_quit; |
| 169 |
unsigned thread_count; |
| 170 |
int sip_port; |
| 171 |
int rtp_start_port; |
| 172 |
pj_str_t local_addr; |
| 173 |
pj_str_t local_uri; |
| 174 |
pj_str_t local_contact; |
| 175 |
|
| 176 |
int app_log_level; |
| 177 |
int log_level; |
| 178 |
char *log_filename; |
| 179 |
char *report_filename; |
| 180 |
|
| 181 |
struct codec audio_codec; |
| 182 |
|
| 183 |
pj_str_t uri_to_call; |
| 184 |
|
| 185 |
pj_caching_pool cp; |
| 186 |
pj_pool_t *pool; |
| 187 |
|
| 188 |
pjsip_endpoint *sip_endpt; |
| 189 |
pj_bool_t thread_quit; |
| 190 |
pj_thread_t *sip_thread[1]; |
| 191 |
|
| 192 |
pjmedia_endpt *med_endpt; |
| 193 |
struct call call[MAX_CALLS]; |
| 194 |
} app; |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
static void call_on_media_update( pjsip_inv_session *inv, |
| 204 |
pj_status_t status); |
| 205 |
|
| 206 |
|
| 207 |
static void call_on_state_changed( pjsip_inv_session *inv, |
| 208 |
pjsip_event *e); |
| 209 |
|
| 210 |
|
| 211 |
static void call_on_forked(pjsip_inv_session *inv, pjsip_event *e); |
| 212 |
|
| 213 |
|
| 214 |
static pj_bool_t on_rx_request( pjsip_rx_data *rdata ); |
| 215 |
|
| 216 |
|
| 217 |
static int sip_worker_thread(void *arg); |
| 218 |
|
| 219 |
|
| 220 |
static pj_status_t create_sdp( pj_pool_t *pool, |
| 221 |
struct call *call, |
| 222 |
pjmedia_sdp_session **p_sdp); |
| 223 |
|
| 224 |
|
| 225 |
static void hangup_call(unsigned index); |
| 226 |
|
| 227 |
|
| 228 |
static void destroy_call_media(unsigned call_index); |
| 229 |
|
| 230 |
|
| 231 |
static void destroy_media(); |
| 232 |
|
| 233 |
|
| 234 |
static void on_rx_rtp(void *user_data, void *pkt, pj_ssize_t size); |
| 235 |
|
| 236 |
|
| 237 |
static void on_rx_rtcp(void *user_data, void *pkt, pj_ssize_t size); |
| 238 |
|
| 239 |
|
| 240 |
static void app_perror(const char *sender, const char *title, |
| 241 |
pj_status_t status); |
| 242 |
|
| 243 |
|
| 244 |
static void print_call(int call_index); |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
static pjsip_module mod_siprtp = |
| 253 |
{ |
| 254 |
NULL, NULL, |
| 255 |
{ "mod-siprtpapp", 13 }, |
| 256 |
-1, |
| 257 |
PJSIP_MOD_PRIORITY_APPLICATION, |
| 258 |
NULL, |
| 259 |
NULL, |
| 260 |
NULL, |
| 261 |
NULL, |
| 262 |
&on_rx_request, |
| 263 |
NULL, |
| 264 |
NULL, |
| 265 |
NULL, |
| 266 |
NULL, |
| 267 |
}; |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
struct codec audio_codecs[] = |
| 272 |
{ |
| 273 |
{ 0, "PCMU", 8000, 64000, 20, "G.711 ULaw" }, |
| 274 |
{ 3, "GSM", 8000, 13200, 20, "GSM" }, |
| 275 |
{ 4, "G723", 8000, 6400, 30, "G.723.1" }, |
| 276 |
{ 8, "PCMA", 8000, 64000, 20, "G.711 ALaw" }, |
| 277 |
{ 18, "G729", 8000, 8000, 20, "G.729" }, |
| 278 |
}; |
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
static pj_status_t init_sip() |
| 285 |
{ |
| 286 |
unsigned i; |
| 287 |
pj_status_t status; |
| 288 |
|
| 289 |
|
| 290 |
status = pjlib_util_init(); |
| 291 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
| 292 |
|
| 293 |
|
| 294 |
pj_caching_pool_init(&app.cp, &pj_pool_factory_default_policy, 0); |
| 295 |
|
| 296 |
|
| 297 |
app.pool = pj_pool_create(&app.cp.factory, "app", 1000, 1000, NULL); |
| 298 |
|
| 299 |
|
| 300 |
status = pjsip_endpt_create(&app.cp.factory, pj_gethostname()->ptr, |
| 301 |
&app.sip_endpt); |
| 302 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
{ |
| 307 |
pj_sockaddr_in addr; |
| 308 |
pjsip_host_port addrname; |
| 309 |
pjsip_transport *tp; |
| 310 |
|
| 311 |
pj_bzero(&addr, sizeof(addr)); |
| 312 |
addr.sin_family = pj_AF_INET(); |
| 313 |
addr.sin_addr.s_addr = 0; |
| 314 |
addr.sin_port = pj_htons((pj_uint16_t)app.sip_port); |
| 315 |
|
| 316 |
if (app.local_addr.slen) { |
| 317 |
|
| 318 |
addrname.host = app.local_addr; |
| 319 |
addrname.port = app.sip_port; |
| 320 |
|
| 321 |
status = pj_sockaddr_in_init(&addr, &app.local_addr, |
| 322 |
(pj_uint16_t)app.sip_port); |
| 323 |
if (status != PJ_SUCCESS) { |
| 324 |
app_perror(THIS_FILE, "Unable to resolve IP interface", status); |
| 325 |
return status; |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
status = pjsip_udp_transport_start( app.sip_endpt, &addr, |
| 330 |
(app.local_addr.slen ? &addrname:NULL), |
| 331 |
1, &tp); |
| 332 |
if (status != PJ_SUCCESS) { |
| 333 |
app_perror(THIS_FILE, "Unable to start UDP transport", status); |
| 334 |
return status; |
| 335 |
} |
| 336 |
|
| 337 |
PJ_LOG(3,(THIS_FILE, "SIP UDP listening on %.*s:%d", |
| 338 |
(int)tp->local_name.host.slen, tp->local_name.host.ptr, |
| 339 |
tp->local_name.port)); |
| 340 |
} |
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
status = pjsip_tsx_layer_init_module(app.sip_endpt); |
| 347 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
| 348 |
|
| 349 |
|
| 350 |
status = pjsip_ua_init_module( app.sip_endpt, NULL ); |
| 351 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
| 352 |
|
| 353 |
|
| 354 |
status = pjsip_100rel_init_module(app.sip_endpt); |
| 355 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
| 356 |
|
| 357 |
|
| 358 |
{ |
| 359 |
pjsip_inv_callback inv_cb; |
| 360 |
|
| 361 |
|
| 362 |
pj_bzero(&inv_cb, sizeof(inv_cb)); |
| 363 |
inv_cb.on_state_changed = &call_on_state_changed; |
| 364 |
inv_cb.on_new_session = &call_on_forked; |
| 365 |
inv_cb.on_media_update = &call_on_media_update; |
| 366 |
|
| 367 |
|
| 368 |
status = pjsip_inv_usage_init(app.sip_endpt, &inv_cb); |
| 369 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
status = pjsip_endpt_register_module( app.sip_endpt, &mod_siprtp); |
| 374 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
| 375 |
|
| 376 |
|
| 377 |
for (i=0; i<app.max_calls; ++i) |
| 378 |
app.call[i].index = i; |
| 379 |
|
| 380 |
|
| 381 |
return PJ_SUCCESS; |
| 382 |
} |
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
static void destroy_sip() |
| 389 |
{ |
| 390 |
unsigned i; |
| 391 |
|
| 392 |
app.thread_quit = 1; |
| 393 |
for (i=0; i<app.thread_count; ++i) { |
| 394 |
if (app.sip_thread[i]) { |
| 395 |
pj_thread_join(app.sip_thread[i]); |
| 396 |
pj_thread_destroy(app.sip_thread[i]); |
| 397 |
app.sip_thread[i] = NULL; |
| 398 |
} |
| 399 |
} |
| 400 |
|
| 401 |
if (app.sip_endpt) { |
| 402 |
pjsip_endpt_destroy(app.sip_endpt); |
| 403 |
app.sip_endpt = NULL; |
| 404 |
} |
| 405 |
|
| 406 |
} |
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
static pj_status_t init_media() |
| 413 |
{ |
| 414 |
unsigned i, count; |
| 415 |
pj_uint16_t rtp_port; |
| 416 |
pj_status_t status; |
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
#if PJ_HAS_THREADS |
| 423 |
status = pjmedia_endpt_create(&app.cp.factory, NULL, 1, &app.med_endpt); |
| 424 |
#else |
| 425 |
status = pjmedia_endpt_create(&app.cp.factory, |
| 426 |
pjsip_endpt_get_ioqueue(app.sip_endpt), |
| 427 |
0, &app.med_endpt); |
| 428 |
#endif |
| 429 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
#if defined(PJMEDIA_HAS_G711_CODEC) && PJMEDIA_HAS_G711_CODEC!=0 |
| 434 |
pjmedia_codec_g711_init(app.med_endpt); |
| 435 |
#endif |
| 436 |
|
| 437 |
|
| 438 |
rtp_port = (pj_uint16_t)(app.rtp_start_port & 0xFFFE); |
| 439 |
|
| 440 |
|
| 441 |
for (i=0, count=0; i<app.max_calls; ++i, ++count) { |
| 442 |
|
| 443 |
unsigned j; |
| 444 |
|
| 445 |
|
| 446 |
for (j=0; j<PJ_ARRAY_SIZE(app.call[0].media); ++j) { |
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
int retry; |
| 451 |
|
| 452 |
app.call[i].media[j].call_index = i; |
| 453 |
app.call[i].media[j].media_index = j; |
| 454 |
|
| 455 |
status = -1; |
| 456 |
for (retry=0; retry<100; ++retry,rtp_port+=2) { |
| 457 |
struct media_stream *m = &app.call[i].media[j]; |
| 458 |
|
| 459 |
status = pjmedia_transport_udp_create2(app.med_endpt, |
| 460 |
"siprtp", |
| 461 |
&app.local_addr, |
| 462 |
rtp_port, 0, |
| 463 |
&m->transport); |
| 464 |
if (status == PJ_SUCCESS) { |
| 465 |
rtp_port += 2; |
| 466 |
break; |
| 467 |
} |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
if (status != PJ_SUCCESS) |
| 472 |
goto on_error; |
| 473 |
} |
| 474 |
|
| 475 |
|
| 476 |
return PJ_SUCCESS; |
| 477 |
|
| 478 |
on_error: |
| 479 |
destroy_media(); |
| 480 |
return status; |
| 481 |
} |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
static void destroy_media() |
| 488 |
{ |
| 489 |
unsigned i; |
| 490 |
|
| 491 |
for (i=0; i<app.max_calls; ++i) { |
| 492 |
unsigned j; |
| 493 |
for (j=0; j<PJ_ARRAY_SIZE(app.call[0].media); ++j) { |
| 494 |
struct media_stream *m = &app.call[i].media[j]; |
| 495 |
|
| 496 |
if (m->transport) { |
| 497 |
pjmedia_transport_close(m->transport); |
| 498 |
m->transport = NULL; |
| 499 |
} |
| 500 |
} |
| 501 |
} |
| 502 |
|
| 503 |
if (app.med_endpt) { |
| 504 |
pjmedia_endpt_destroy(app.med_endpt); |
| 505 |
app.med_endpt = NULL; |
| 506 |
} |
| 507 |
} |
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
static pj_status_t make_call(const pj_str_t *dst_uri) |
| 514 |
{ |
| 515 |
unsigned i; |
| 516 |
struct call *call; |
| 517 |
pjsip_dialog *dlg; |
| 518 |
pjmedia_sdp_session *sdp; |
| 519 |
pjsip_tx_data *tdata; |
| 520 |
pj_status_t status; |
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
for (i=0; i<app.max_calls; ++i) { |
| 525 |
if (app.call[i].inv == NULL) |
| 526 |
break; |
| 527 |
} |
| 528 |
|
| 529 |
if (i == app.max_calls) |
| 530 |
return PJ_ETOOMANY; |
| 531 |
|
| 532 |
call = &app.call[i]; |
| 533 |
|
| 534 |
|
| 535 |
status = pjsip_dlg_create_uac( pjsip_ua_instance(), |
| 536 |
&app.local_uri, |
| 537 |
&app.local_contact, |
| 538 |
dst_uri, |
| 539 |
dst_uri, |
| 540 |
&dlg); |
| 541 |
if (status != PJ_SUCCESS) { |
| 542 |
++app.uac_calls; |
| 543 |
return status; |
| 544 |
} |
| 545 |
|
| 546 |
|
| 547 |
create_sdp( dlg->pool, call, &sdp); |
| 548 |
|
| 549 |
|
| 550 |
status = pjsip_inv_create_uac( dlg, sdp, 0, &call->inv); |
| 551 |
if (status != PJ_SUCCESS) { |
| 552 |
pjsip_dlg_terminate(dlg); |
| 553 |
++app.uac_calls; |
| 554 |
return status; |
| 555 |
} |
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
call->inv->mod_data[mod_siprtp.id] = call; |
| 560 |
|
| 561 |
|
| 562 |
pj_gettimeofday(&call->start_time); |
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
status = pjsip_inv_invite(call->inv, &tdata); |
| 570 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
status = pjsip_inv_send_msg(call->inv, tdata); |
| 578 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
| 579 |
|
| 580 |
|
| 581 |
return PJ_SUCCESS; |
| 582 |
} |
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
static void process_incoming_call(pjsip_rx_data *rdata) |
| 589 |
{ |
| 590 |
unsigned i, options; |
| 591 |
struct call *call; |
| 592 |
pjsip_dialog *dlg; |
| 593 |
pjmedia_sdp_session *sdp; |
| 594 |
pjsip_tx_data *tdata; |
| 595 |
pj_status_t status; |
| 596 |
|
| 597 |
|
| 598 |
for (i=0; i<app.max_calls; ++i) { |
| 599 |
if (app.call[i].inv == NULL) |
| 600 |
break; |
| 601 |
} |
| 602 |
|
| 603 |
if (i == app.max_calls) { |
| 604 |
const pj_str_t reason = pj_str("Too many calls"); |
| 605 |
pjsip_endpt_respond_stateless( app.sip_endpt, rdata, |
| 606 |
500, &reason, |
| 607 |
NULL, NULL); |
| 608 |
return; |
| 609 |
} |
| 610 |
|
| 611 |
call = &app.call[i]; |
| 612 |
|
| 613 |
|
| 614 |
options = 0; |
| 615 |
status = pjsip_inv_verify_request(rdata, &options, NULL, NULL, |
| 616 |
app.sip_endpt, &tdata); |
| 617 |
if (status != PJ_SUCCESS) { |
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
if (tdata) { |
| 622 |
pjsip_response_addr res_addr; |
| 623 |
|
| 624 |
pjsip_get_response_addr(tdata->pool, rdata, &res_addr); |
| 625 |
status = pjsip_endpt_send_response(app.sip_endpt, &res_addr, tdata, |
| 626 |
NULL, NULL); |
| 627 |
if (status != PJ_SUCCESS) pjsip_tx_data_dec_ref(tdata); |
| 628 |
|
| 629 |
} else { |
| 630 |
|
| 631 |
|
| 632 |
pjsip_endpt_respond_stateless(app.sip_endpt, rdata, 500, NULL, |
| 633 |
NULL, NULL); |
| 634 |
} |
| 635 |
|
| 636 |
return; |
| 637 |
} |
| 638 |
|
| 639 |
|
| 640 |
status = pjsip_dlg_create_uas_and_inc_lock( pjsip_ua_instance(), rdata, |
| 641 |
&app.local_contact, &dlg); |
| 642 |
if (status != PJ_SUCCESS) { |
| 643 |
const pj_str_t reason = pj_str("Unable to create dialog"); |
| 644 |
pjsip_endpt_respond_stateless( app.sip_endpt, rdata, |
| 645 |
500, &reason, |
| 646 |
NULL, NULL); |
| 647 |
return; |
| 648 |
} |
| 649 |
|
| 650 |
|
| 651 |
create_sdp( dlg->pool, call, &sdp); |
| 652 |
|
| 653 |
|
| 654 |
status = pjsip_inv_create_uas( dlg, rdata, sdp, 0, &call->inv); |
| 655 |
if (status != PJ_SUCCESS) { |
| 656 |
pjsip_dlg_create_response(dlg, rdata, 500, NULL, &tdata); |
| 657 |
pjsip_dlg_send_response(dlg, pjsip_rdata_get_tsx(rdata), tdata); |
| 658 |
pjsip_dlg_dec_lock(dlg); |
| 659 |
return; |
| 660 |
} |
| 661 |
|
| 662 |
|
| 663 |
pjsip_dlg_dec_lock(dlg); |
| 664 |
|
| 665 |
|
| 666 |
call->inv->mod_data[mod_siprtp.id] = call; |
| 667 |
|
| 668 |
|
| 669 |
pj_gettimeofday(&call->start_time); |
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
status = pjsip_inv_initial_answer(call->inv, rdata, 200, |
| 675 |
NULL, NULL, &tdata); |
| 676 |
if (status != PJ_SUCCESS) { |
| 677 |
status = pjsip_inv_initial_answer(call->inv, rdata, |
| 678 |
PJSIP_SC_NOT_ACCEPTABLE, |
| 679 |
NULL, NULL, &tdata); |
| 680 |
if (status == PJ_SUCCESS) |
| 681 |
pjsip_inv_send_msg(call->inv, tdata); |
| 682 |
else |
| 683 |
pjsip_inv_terminate(call->inv, 500, PJ_FALSE); |
| 684 |
return; |
| 685 |
} |
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
status = pjsip_inv_send_msg(call->inv, tdata); |
| 690 |
PJ_ASSERT_ON_FAIL(status == PJ_SUCCESS, return); |
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
} |
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
static void call_on_forked(pjsip_inv_session *inv, pjsip_event *e) |
| 699 |
{ |
| 700 |
PJ_UNUSED_ARG(inv); |
| 701 |
PJ_UNUSED_ARG(e); |
| 702 |
|
| 703 |
PJ_TODO( HANDLE_FORKING ); |
| 704 |
} |
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
static pj_bool_t on_rx_request( pjsip_rx_data *rdata ) |
| 709 |
{ |
| 710 |
|
| 711 |
if (rdata->msg_info.msg->line.req.method.id == PJSIP_ACK_METHOD) |
| 712 |
return PJ_FALSE; |
| 713 |
|
| 714 |
|
| 715 |
if (rdata->msg_info.msg->line.req.method.id != PJSIP_INVITE_METHOD) { |
| 716 |
pj_str_t reason = pj_str("Unsupported Operation"); |
| 717 |
pjsip_endpt_respond_stateless( app.sip_endpt, rdata, |
| 718 |
500, &reason, |
| 719 |
NULL, NULL); |
| 720 |
return PJ_TRUE; |
| 721 |
} |
| 722 |
|
| 723 |
|
| 724 |
process_incoming_call(rdata); |
| 725 |
|
| 726 |
|
| 727 |
return PJ_TRUE; |
| 728 |
} |
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
static void timer_disconnect_call( pj_timer_heap_t *timer_heap, |
| 733 |
struct pj_timer_entry *entry) |
| 734 |
{ |
| 735 |
struct call *call = (struct call *)(entry->user_data); |
| 736 |
|
| 737 |
PJ_UNUSED_ARG(timer_heap); |
| 738 |
|
| 739 |
entry->id = 0; |
| 740 |
hangup_call(call->index); |
| 741 |
} |
| 742 |
|
| 743 |
|
| 744 |
|
| 745 |
static void call_on_state_changed( pjsip_inv_session *inv, |
| 746 |
pjsip_event *e) |
| 747 |
{ |
| 748 |
struct call *call = (struct call *)inv->mod_data[mod_siprtp.id]; |
| 749 |
|
| 750 |
PJ_UNUSED_ARG(e); |
| 751 |
|
| 752 |
if (!call) |
| 753 |
return; |
| 754 |
|
| 755 |
if (inv->state == PJSIP_INV_STATE_DISCONNECTED) { |
| 756 |
|
| 757 |
pj_time_val null_time = {0, 0}; |
| 758 |
|
| 759 |
if (call->d_timer.id != 0) { |
| 760 |
pjsip_endpt_cancel_timer(app.sip_endpt, &call->d_timer); |
| 761 |
call->d_timer.id = 0; |
| 762 |
} |
| 763 |
|
| 764 |
PJ_LOG(3,(THIS_FILE, "Call #%d disconnected. Reason=%d (%.*s)", |
| 765 |
call->index, |
| 766 |
inv->cause, |
| 767 |
(int)inv->cause_text.slen, |
| 768 |
inv->cause_text.ptr)); |
| 769 |
|
| 770 |
if (app.call_report) { |
| 771 |
PJ_LOG(3,(THIS_FILE, "Call #%d statistics:", call->index)); |
| 772 |
print_call(call->index); |
| 773 |
} |
| 774 |
|
| 775 |
|
| 776 |
call->inv = NULL; |
| 777 |
inv->mod_data[mod_siprtp.id] = NULL; |
| 778 |
|
| 779 |
destroy_call_media(call->index); |
| 780 |
|
| 781 |
call->start_time = null_time; |
| 782 |
call->response_time = null_time; |
| 783 |
call->connect_time = null_time; |
| 784 |
|
| 785 |
++app.uac_calls; |
| 786 |
|
| 787 |
} else if (inv->state == PJSIP_INV_STATE_CONFIRMED) { |
| 788 |
|
| 789 |
pj_time_val t; |
| 790 |
|
| 791 |
pj_gettimeofday(&call->connect_time); |
| 792 |
if (call->response_time.sec == 0) |
| 793 |
call->response_time = call->connect_time; |
| 794 |
|
| 795 |
t = call->connect_time; |
| 796 |
PJ_TIME_VAL_SUB(t, call->start_time); |
| 797 |
|
| 798 |
PJ_LOG(3,(THIS_FILE, "Call #%d connected in %ld ms", call->index, |
| 799 |
PJ_TIME_VAL_MSEC(t))); |
| 800 |
|
| 801 |
if (app.duration != 0) { |
| 802 |
call->d_timer.id = 1; |
| 803 |
call->d_timer.user_data = call; |
| 804 |
call->d_timer.cb = &timer_disconnect_call; |
| 805 |
|
| 806 |
t.sec = app.duration; |
| 807 |
t.msec = 0; |
| 808 |
|
| 809 |
pjsip_endpt_schedule_timer(app.sip_endpt, &call->d_timer, &t); |
| 810 |
} |
| 811 |
|
| 812 |
} else if ( inv->state == PJSIP_INV_STATE_EARLY || |
| 813 |
inv->state == PJSIP_INV_STATE_CONNECTING) { |
| 814 |
|
| 815 |
if (call->response_time.sec == 0) |
| 816 |
pj_gettimeofday(&call->response_time); |
| 817 |
|
| 818 |
} |
| 819 |
} |
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
static void app_perror(const char *sender, const char *title, |
| 824 |
pj_status_t status) |
| 825 |
{ |
| 826 |
char errmsg[PJ_ERR_MSG_SIZE]; |
| 827 |
|
| 828 |
pj_strerror(status, errmsg, sizeof(errmsg)); |
| 829 |
PJ_LOG(3,(sender, "%s: %s [status=%d]", title, errmsg, status)); |
| 830 |
} |
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
static int sip_worker_thread(void *arg) |
| 835 |
{ |
| 836 |
PJ_UNUSED_ARG(arg); |
| 837 |
|
| 838 |
while (!app.thread_quit) { |
| 839 |
pj_time_val timeout = {0, 10}; |
| 840 |
pjsip_endpt_handle_events(app.sip_endpt, &timeout); |
| 841 |
} |
| 842 |
|
| 843 |
return 0; |
| 844 |
} |
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
static pj_status_t init_options(int argc, char *argv[]) |
| 849 |
{ |
| 850 |
static char ip_addr[PJ_INET_ADDRSTRLEN]; |
| 851 |
static char local_uri[64]; |
| 852 |
|
| 853 |
enum { OPT_START, |
| 854 |
OPT_APP_LOG_LEVEL, OPT_LOG_FILE, |
| 855 |
OPT_A_PT, OPT_A_NAME, OPT_A_CLOCK, OPT_A_BITRATE, OPT_A_PTIME, |
| 856 |
OPT_REPORT_FILE }; |
| 857 |
|
| 858 |
struct pj_getopt_option long_options[] = { |
| 859 |
{ "count", 1, 0, 'c' }, |
| 860 |
{ "gap", 1, 0, 'g' }, |
| 861 |
{ "call-report", 0, 0, 'R' }, |
| 862 |
{ "duration", 1, 0, 'd' }, |
| 863 |
{ "auto-quit", 0, 0, 'q' }, |
| 864 |
{ "local-port", 1, 0, 'p' }, |
| 865 |
{ "rtp-port", 1, 0, 'r' }, |
| 866 |
{ "ip-addr", 1, 0, 'i' }, |
| 867 |
|
| 868 |
{ "log-level", 1, 0, 'l' }, |
| 869 |
{ "app-log-level", 1, 0, OPT_APP_LOG_LEVEL }, |
| 870 |
{ "log-file", 1, 0, OPT_LOG_FILE }, |
| 871 |
|
| 872 |
{ "report-file", 1, 0, OPT_REPORT_FILE }, |
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
{ NULL, 0, 0, 0 }, |
| 883 |
}; |
| 884 |
int c; |
| 885 |
int option_index; |
| 886 |
|
| 887 |
|
| 888 |
{ |
| 889 |
const pj_str_t *hostname; |
| 890 |
pj_sockaddr_in tmp_addr; |
| 891 |
|
| 892 |
hostname = pj_gethostname(); |
| 893 |
pj_sockaddr_in_init(&tmp_addr, hostname, 0); |
| 894 |
pj_inet_ntop(pj_AF_INET(), &tmp_addr.sin_addr, ip_addr, |
| 895 |
sizeof(ip_addr)); |
| 896 |
} |
| 897 |
|
| 898 |
|
| 899 |
app.max_calls = 1; |
| 900 |
app.thread_count = 1; |
| 901 |
app.sip_port = 5060; |
| 902 |
app.rtp_start_port = RTP_START_PORT; |
| 903 |
app.local_addr = pj_str(ip_addr); |
| 904 |
app.log_level = 5; |
| 905 |
app.app_log_level = 3; |
| 906 |
app.log_filename = NULL; |
| 907 |
|
| 908 |
|
| 909 |
app.audio_codec = audio_codecs[0]; |
| 910 |
|
| 911 |
|
| 912 |
pj_optind = 0; |
| 913 |
while((c=pj_getopt_long(argc,argv, "c:d:p:r:i:l:g:qR", |
| 914 |
long_options, &option_index))!=-1) |
| 915 |
{ |
| 916 |
switch (c) { |
| 917 |
case 'c': |
| 918 |
app.max_calls = atoi(pj_optarg); |
| 919 |
if (app.max_calls > MAX_CALLS) { |
| 920 |
PJ_LOG(3,(THIS_FILE,"Invalid max calls value %s " |
| 921 |
"(must be <= %d)", pj_optarg, MAX_CALLS)); |
| 922 |
return 1; |
| 923 |
} |
| 924 |
break; |
| 925 |
case 'g': |
| 926 |
app.call_gap = atoi(pj_optarg); |
| 927 |
break; |
| 928 |
case 'R': |
| 929 |
app.call_report = PJ_TRUE; |
| 930 |
break; |
| 931 |
case 'd': |
| 932 |
app.duration = atoi(pj_optarg); |
| 933 |
break; |
| 934 |
case 'q': |
| 935 |
app.auto_quit = 1; |
| 936 |
break; |
| 937 |
|
| 938 |
case 'p': |
| 939 |
app.sip_port = atoi(pj_optarg); |
| 940 |
break; |
| 941 |
case 'r': |
| 942 |
app.rtp_start_port = atoi(pj_optarg); |
| 943 |
break; |
| 944 |
case 'i': |
| 945 |
app.local_addr = pj_str(pj_optarg); |
| 946 |
break; |
| 947 |
|
| 948 |
case 'l': |
| 949 |
app.log_level = atoi(pj_optarg); |
| 950 |
break; |
| 951 |
case OPT_APP_LOG_LEVEL: |
| 952 |
app.app_log_level = atoi(pj_optarg); |
| 953 |
break; |
| 954 |
case OPT_LOG_FILE: |
| 955 |
app.log_filename = pj_optarg; |
| 956 |
break; |
| 957 |
|
| 958 |
case OPT_A_PT: |
| 959 |
app.audio_codec.pt = atoi(pj_optarg); |
| 960 |
break; |
| 961 |
case OPT_A_NAME: |
| 962 |
app.audio_codec.name = pj_optarg; |
| 963 |
break; |
| 964 |
case OPT_A_CLOCK: |
| 965 |
app.audio_codec.clock_rate = atoi(pj_optarg); |
| 966 |
break; |
| 967 |
case OPT_A_BITRATE: |
| 968 |
app.audio_codec.bit_rate = atoi(pj_optarg); |
| 969 |
break; |
| 970 |
case OPT_A_PTIME: |
| 971 |
app.audio_codec.ptime = atoi(pj_optarg); |
| 972 |
break; |
| 973 |
case OPT_REPORT_FILE: |
| 974 |
app.report_filename = pj_optarg; |
| 975 |
break; |
| 976 |
|
| 977 |
default: |
| 978 |
puts(USAGE); |
| 979 |
return 1; |
| 980 |
} |
| 981 |
} |
| 982 |
|
| 983 |
|
| 984 |
if (pj_optind < argc) |
| 985 |
app.uri_to_call = pj_str(argv[pj_optind]); |
| 986 |
|
| 987 |
|
| 988 |
pj_ansi_snprintf(local_uri, sizeof(local_uri), |
| 989 |
"sip:%s:%d", app.local_addr.ptr, app.sip_port); |
| 990 |
app.local_uri = pj_str(local_uri); |
| 991 |
app.local_contact = app.local_uri; |
| 992 |
|
| 993 |
|
| 994 |
return PJ_SUCCESS; |
| 995 |
} |
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
static pj_status_t create_sdp( pj_pool_t *pool, |
| 1006 |
struct call *call, |
| 1007 |
pjmedia_sdp_session **p_sdp) |
| 1008 |
{ |
| 1009 |
pj_time_val tv; |
| 1010 |
pjmedia_sdp_session *sdp; |
| 1011 |
pjmedia_sdp_media *m; |
| 1012 |
pjmedia_sdp_attr *attr; |
| 1013 |
pjmedia_transport_info tpinfo; |
| 1014 |
struct media_stream *audio = &call->media[0]; |
| 1015 |
|
| 1016 |
PJ_ASSERT_RETURN(pool && p_sdp, PJ_EINVAL); |
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
pjmedia_transport_info_init(&tpinfo); |
| 1021 |
pjmedia_transport_get_info(audio->transport, &tpinfo); |
| 1022 |
|
| 1023 |
|
| 1024 |
sdp = PJ_POOL_ZALLOC_T(pool, pjmedia_sdp_session); |
| 1025 |
|
| 1026 |
pj_gettimeofday(&tv); |
| 1027 |
sdp->origin.user = pj_str("pjsip-siprtp"); |
| 1028 |
sdp->origin.version = sdp->origin.id = tv.sec + 2208988800UL; |
| 1029 |
sdp->origin.net_type = pj_str("IN"); |
| 1030 |
sdp->origin.addr_type = pj_str("IP4"); |
| 1031 |
sdp->origin.addr = *pj_gethostname(); |
| 1032 |
sdp->name = pj_str("pjsip"); |
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
sdp->conn = PJ_POOL_ZALLOC_T(pool, pjmedia_sdp_conn); |
| 1038 |
sdp->conn->net_type = pj_str("IN"); |
| 1039 |
sdp->conn->addr_type = pj_str("IP4"); |
| 1040 |
sdp->conn->addr = app.local_addr; |
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
sdp->time.start = sdp->time.stop = 0; |
| 1045 |
sdp->attr_count = 0; |
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
sdp->media_count = 1; |
| 1050 |
m = PJ_POOL_ZALLOC_T(pool, pjmedia_sdp_media); |
| 1051 |
sdp->media[0] = m; |
| 1052 |
|
| 1053 |
|
| 1054 |
m->desc.media = pj_str("audio"); |
| 1055 |
m->desc.port = pj_ntohs(tpinfo.sock_info.rtp_addr_name.ipv4.sin_port); |
| 1056 |
m->desc.port_count = 1; |
| 1057 |
m->desc.transport = pj_str("RTP/AVP"); |
| 1058 |
|
| 1059 |
|
| 1060 |
m->desc.fmt_count = 1; |
| 1061 |
m->attr_count = 0; |
| 1062 |
|
| 1063 |
{ |
| 1064 |
pjmedia_sdp_rtpmap rtpmap; |
| 1065 |
char ptstr[10]; |
| 1066 |
|
| 1067 |
snprintf(ptstr, sizeof(ptstr), "%d", app.audio_codec.pt); |
| 1068 |
pj_strdup2(pool, &m->desc.fmt[0], ptstr); |
| 1069 |
rtpmap.pt = m->desc.fmt[0]; |
| 1070 |
rtpmap.clock_rate = app.audio_codec.clock_rate; |
| 1071 |
rtpmap.enc_name = pj_str(app.audio_codec.name); |
| 1072 |
pj_bzero(&rtpmap.param, sizeof(pj_str_t)); |
| 1073 |
|
| 1074 |
pjmedia_sdp_rtpmap_to_attr(pool, &rtpmap, &attr); |
| 1075 |
m->attr[m->attr_count++] = attr; |
| 1076 |
} |
| 1077 |
|
| 1078 |
|
| 1079 |
attr = PJ_POOL_ZALLOC_T(pool, pjmedia_sdp_attr); |
| 1080 |
attr->name = pj_str("sendrecv"); |
| 1081 |
m->attr[m->attr_count++] = attr; |
| 1082 |
|
| 1083 |
#if 1 |
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
m->desc.fmt[m->desc.fmt_count++] = pj_str("121"); |
| 1088 |
|
| 1089 |
attr = PJ_POOL_ZALLOC_T(pool, pjmedia_sdp_attr); |
| 1090 |
attr->name = pj_str("rtpmap"); |
| 1091 |
attr->value = pj_str("121 telephone-event/8000"); |
| 1092 |
m->attr[m->attr_count++] = attr; |
| 1093 |
|
| 1094 |
attr = PJ_POOL_ZALLOC_T(pool, pjmedia_sdp_attr); |
| 1095 |
attr->name = pj_str("fmtp"); |
| 1096 |
attr->value = pj_str("121 0-15"); |
| 1097 |
m->attr[m->attr_count++] = attr; |
| 1098 |
#endif |
| 1099 |
|
| 1100 |
|
| 1101 |
*p_sdp = sdp; |
| 1102 |
|
| 1103 |
return PJ_SUCCESS; |
| 1104 |
} |
| 1105 |
|
| 1106 |
|
| 1107 |
#if (defined(PJ_WIN32) && PJ_WIN32 != 0) || (defined(PJ_WIN64) && PJ_WIN64 != 0) |
| 1108 |
#include <windows.h> |
| 1109 |
static void boost_priority(void) |
| 1110 |
{ |
| 1111 |
SetPriorityClass( GetCurrentProcess(), REALTIME_PRIORITY_CLASS); |
| 1112 |
SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_HIGHEST); |
| 1113 |
} |
| 1114 |
|
| 1115 |
#elif defined(PJ_LINUX) && PJ_LINUX != 0 |
| 1116 |
#include <pthread.h> |
| 1117 |
static void boost_priority(void) |
| 1118 |
{ |
| 1119 |
#define POLICY SCHED_FIFO |
| 1120 |
struct sched_param tp; |
| 1121 |
int max_prio; |
| 1122 |
int policy; |
| 1123 |
int rc; |
| 1124 |
|
| 1125 |
if (sched_get_priority_min(POLICY) < sched_get_priority_max(POLICY)) |
| 1126 |
max_prio = sched_get_priority_max(POLICY)-1; |
| 1127 |
else |
| 1128 |
max_prio = sched_get_priority_max(POLICY)+1; |
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
rc = sched_getparam(0, &tp); |
| 1134 |
if (rc != 0) { |
| 1135 |
app_perror( THIS_FILE, "sched_getparam error", |
| 1136 |
PJ_RETURN_OS_ERROR(rc)); |
| 1137 |
return; |
| 1138 |
} |
| 1139 |
tp.sched_priority = max_prio; |
| 1140 |
|
| 1141 |
rc = sched_setscheduler(0, POLICY, &tp); |
| 1142 |
if (rc != 0) { |
| 1143 |
app_perror( THIS_FILE, "sched_setscheduler error", |
| 1144 |
PJ_RETURN_OS_ERROR(rc)); |
| 1145 |
return; |
| 1146 |
} |
| 1147 |
|
| 1148 |
|
| 1149 |
|
| 1150 |
|
| 1151 |
rc = pthread_getschedparam(pthread_self(), &policy, &tp); |
| 1152 |
if (rc != 0) { |
| 1153 |
app_perror( THIS_FILE, "pthread_getschedparam error", |
| 1154 |
PJ_RETURN_OS_ERROR(rc)); |
| 1155 |
return; |
| 1156 |
} |
| 1157 |
|
| 1158 |
PJ_LOG(4, (THIS_FILE, "Old thread policy=%d, priority=%d", |
| 1159 |
policy, tp.sched_priority)); |
| 1160 |
|
| 1161 |
policy = POLICY; |
| 1162 |
tp.sched_priority = max_prio; |
| 1163 |
|
| 1164 |
rc = pthread_setschedparam(pthread_self(), policy, &tp); |
| 1165 |
if (rc != 0) { |
| 1166 |
app_perror( THIS_FILE, "pthread_setschedparam error", |
| 1167 |
PJ_RETURN_OS_ERROR(rc)); |
| 1168 |
return; |
| 1169 |
} |
| 1170 |
|
| 1171 |
PJ_LOG(4, (THIS_FILE, "New thread policy=%d, priority=%d", |
| 1172 |
policy, tp.sched_priority)); |
| 1173 |
} |
| 1174 |
|
| 1175 |
#else |
| 1176 |
# define boost_priority() |
| 1177 |
#endif |
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
static void on_rx_rtp(void *user_data, void *pkt, pj_ssize_t size) |
| 1184 |
{ |
| 1185 |
struct media_stream *strm; |
| 1186 |
pj_status_t status; |
| 1187 |
const pjmedia_rtp_hdr *hdr; |
| 1188 |
const void *payload; |
| 1189 |
unsigned payload_len; |
| 1190 |
|
| 1191 |
strm = (struct media_stream *)user_data; |
| 1192 |
|
| 1193 |
|
| 1194 |
if (!strm->active) |
| 1195 |
return; |
| 1196 |
|
| 1197 |
|
| 1198 |
if (size < 0) { |
| 1199 |
app_perror(THIS_FILE, "RTP recv() error", (pj_status_t)-size); |
| 1200 |
return; |
| 1201 |
} |
| 1202 |
|
| 1203 |
|
| 1204 |
status = pjmedia_rtp_decode_rtp(&strm->in_sess, |
| 1205 |
pkt, (int)size, |
| 1206 |
&hdr, &payload, &payload_len); |
| 1207 |
if (status != PJ_SUCCESS) { |
| 1208 |
app_perror(THIS_FILE, "RTP decode error", status); |
| 1209 |
return; |
| 1210 |
} |
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
pjmedia_rtcp_rx_rtp(&strm->rtcp, pj_ntohs(hdr->seq), |
| 1216 |
pj_ntohl(hdr->ts), payload_len); |
| 1217 |
|
| 1218 |
|
| 1219 |
pjmedia_rtp_session_update(&strm->in_sess, hdr, NULL); |
| 1220 |
|
| 1221 |
} |
| 1222 |
|
| 1223 |
|
| 1224 |
|
| 1225 |
|
| 1226 |
static void on_rx_rtcp(void *user_data, void *pkt, pj_ssize_t size) |
| 1227 |
{ |
| 1228 |
struct media_stream *strm; |
| 1229 |
|
| 1230 |
strm = (struct media_stream *)user_data; |
| 1231 |
|
| 1232 |
|
| 1233 |
if (!strm->active) |
| 1234 |
return; |
| 1235 |
|
| 1236 |
|
| 1237 |
if (size < 0) { |
| 1238 |
app_perror(THIS_FILE, "Error receiving RTCP packet",(pj_status_t)-size); |
| 1239 |
return; |
| 1240 |
} |
| 1241 |
|
| 1242 |
|
| 1243 |
pjmedia_rtcp_rx_rtcp(&strm->rtcp, pkt, size); |
| 1244 |
} |
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
static int media_thread(void *arg) |
| 1253 |
{ |
| 1254 |
enum { RTCP_INTERVAL = 5000, RTCP_RAND = 2000 }; |
| 1255 |
struct media_stream *strm = (struct media_stream *)arg; |
| 1256 |
char packet[1500]; |
| 1257 |
unsigned msec_interval; |
| 1258 |
pj_timestamp freq, next_rtp, next_rtcp; |
| 1259 |
|
| 1260 |
|
| 1261 |
|
| 1262 |
boost_priority(); |
| 1263 |
|
| 1264 |
|
| 1265 |
pj_thread_sleep(100); |
| 1266 |
|
| 1267 |
msec_interval = strm->samples_per_frame * 1000 / strm->clock_rate; |
| 1268 |
pj_get_timestamp_freq(&freq); |
| 1269 |
|
| 1270 |
pj_get_timestamp(&next_rtp); |
| 1271 |
next_rtp.u64 += (freq.u64 * msec_interval / 1000); |
| 1272 |
|
| 1273 |
next_rtcp = next_rtp; |
| 1274 |
next_rtcp.u64 += (freq.u64 * (RTCP_INTERVAL+(pj_rand()%RTCP_RAND)) / 1000); |
| 1275 |
|
| 1276 |
|
| 1277 |
while (!strm->thread_quit_flag) { |
| 1278 |
pj_timestamp now, lesser; |
| 1279 |
pj_time_val timeout; |
| 1280 |
pj_bool_t send_rtp, send_rtcp; |
| 1281 |
|
| 1282 |
send_rtp = send_rtcp = PJ_FALSE; |
| 1283 |
|
| 1284 |
|
| 1285 |
if (next_rtp.u64 < next_rtcp.u64) { |
| 1286 |
lesser = next_rtp; |
| 1287 |
send_rtp = PJ_TRUE; |
| 1288 |
} else { |
| 1289 |
lesser = next_rtcp; |
| 1290 |
send_rtcp = PJ_TRUE; |
| 1291 |
} |
| 1292 |
|
| 1293 |
pj_get_timestamp(&now); |
| 1294 |
if (lesser.u64 <= now.u64) { |
| 1295 |
timeout.sec = timeout.msec = 0; |
| 1296 |
|
| 1297 |
} else { |
| 1298 |
pj_uint64_t tick_delay; |
| 1299 |
tick_delay = lesser.u64 - now.u64; |
| 1300 |
timeout.sec = 0; |
| 1301 |
timeout.msec = (pj_uint32_t)(tick_delay * 1000 / freq.u64); |
| 1302 |
pj_time_val_normalize(&timeout); |
| 1303 |
|
| 1304 |
|
| 1305 |
} |
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
pj_thread_sleep(PJ_TIME_VAL_MSEC(timeout)); |
| 1310 |
if (strm->thread_quit_flag) |
| 1311 |
break; |
| 1312 |
|
| 1313 |
|
| 1314 |
pj_get_timestamp(&now); |
| 1315 |
|
| 1316 |
if (send_rtp || next_rtp.u64 <= now.u64) { |
| 1317 |
|
| 1318 |
|
| 1319 |
|
| 1320 |
pj_status_t status; |
| 1321 |
const void *p_hdr; |
| 1322 |
const pjmedia_rtp_hdr *hdr; |
| 1323 |
pj_ssize_t size; |
| 1324 |
int hdrlen; |
| 1325 |
|
| 1326 |
|
| 1327 |
status = pjmedia_rtp_encode_rtp( &strm->out_sess, strm->si.tx_pt, |
| 1328 |
0, |
| 1329 |
strm->bytes_per_frame, |
| 1330 |
strm->samples_per_frame, |
| 1331 |
&p_hdr, &hdrlen); |
| 1332 |
if (status == PJ_SUCCESS) { |
| 1333 |
|
| 1334 |
|
| 1335 |
|
| 1336 |
hdr = (const pjmedia_rtp_hdr*) p_hdr; |
| 1337 |
|
| 1338 |
|
| 1339 |
pj_memcpy(packet, hdr, hdrlen); |
| 1340 |
|
| 1341 |
|
| 1342 |
pj_bzero(packet+hdrlen, strm->bytes_per_frame); |
| 1343 |
|
| 1344 |
|
| 1345 |
size = hdrlen + strm->bytes_per_frame; |
| 1346 |
status = pjmedia_transport_send_rtp(strm->transport, |
| 1347 |
packet, size); |
| 1348 |
if (status != PJ_SUCCESS) |
| 1349 |
app_perror(THIS_FILE, "Error sending RTP packet", status); |
| 1350 |
|
| 1351 |
} else { |
| 1352 |
pj_assert(!"RTP encode() error"); |
| 1353 |
} |
| 1354 |
|
| 1355 |
|
| 1356 |
pjmedia_rtcp_tx_rtp( &strm->rtcp, (pj_uint16_t)strm->bytes_per_frame); |
| 1357 |
|
| 1358 |
|
| 1359 |
next_rtp.u64 += (msec_interval * freq.u64 / 1000); |
| 1360 |
} |
| 1361 |
|
| 1362 |
|
| 1363 |
if (send_rtcp || next_rtcp.u64 <= now.u64) { |
| 1364 |
|
| 1365 |
|
| 1366 |
|
| 1367 |
void *rtcp_pkt; |
| 1368 |
int rtcp_len; |
| 1369 |
pj_ssize_t size; |
| 1370 |
pj_status_t status; |
| 1371 |
|
| 1372 |
|
| 1373 |
pjmedia_rtcp_build_rtcp(&strm->rtcp, &rtcp_pkt, &rtcp_len); |
| 1374 |
|
| 1375 |
|
| 1376 |
|
| 1377 |
size = rtcp_len; |
| 1378 |
status = pjmedia_transport_send_rtcp(strm->transport, |
| 1379 |
rtcp_pkt, size); |
| 1380 |
if (status != PJ_SUCCESS) { |
| 1381 |
app_perror(THIS_FILE, "Error sending RTCP packet", status); |
| 1382 |
} |
| 1383 |
|
| 1384 |
|
| 1385 |
next_rtcp.u64 += (freq.u64 * (RTCP_INTERVAL+(pj_rand()%RTCP_RAND)) / |
| 1386 |
1000); |
| 1387 |
} |
| 1388 |
} |
| 1389 |
|
| 1390 |
return 0; |
| 1391 |
} |
| 1392 |
|
| 1393 |
|
| 1394 |
|
| 1395 |
static void call_on_media_update( pjsip_inv_session *inv, |
| 1396 |
pj_status_t status) |
| 1397 |
{ |
| 1398 |
struct call *call; |
| 1399 |
struct media_stream *audio; |
| 1400 |
const pjmedia_sdp_session *local_sdp, *remote_sdp; |
| 1401 |
struct codec *codec_desc = NULL; |
| 1402 |
unsigned i; |
| 1403 |
|
| 1404 |
call = (struct call *)inv->mod_data[mod_siprtp.id]; |
| 1405 |
audio = &call->media[0]; |
| 1406 |
|
| 1407 |
|
| 1408 |
if (audio->thread != NULL) |
| 1409 |
destroy_call_media(call->index); |
| 1410 |
|
| 1411 |
|
| 1412 |
|
| 1413 |
if (status != PJ_SUCCESS) { |
| 1414 |
app_perror(THIS_FILE, "SDP negotiation failed", status); |
| 1415 |
return; |
| 1416 |
} |
| 1417 |
|
| 1418 |
|
| 1419 |
|
| 1420 |
pjmedia_sdp_neg_get_active_local(inv->neg, &local_sdp); |
| 1421 |
pjmedia_sdp_neg_get_active_remote(inv->neg, &remote_sdp); |
| 1422 |
|
| 1423 |
if (local_sdp->media[0]->desc.port == 0) { |
| 1424 |
PJ_LOG(3, (THIS_FILE, "Audio media inactive")); |
| 1425 |
return; |
| 1426 |
} |
| 1427 |
|
| 1428 |
status = pjmedia_stream_info_from_sdp(&audio->si, inv->pool, app.med_endpt, |
| 1429 |
local_sdp, remote_sdp, 0); |
| 1430 |
if (status != PJ_SUCCESS) { |
| 1431 |
app_perror(THIS_FILE, "Error creating stream info from SDP", status); |
| 1432 |
return; |
| 1433 |
} |
| 1434 |
|
| 1435 |
|
| 1436 |
if (audio->si.fmt.pt == app.audio_codec.pt) |
| 1437 |
codec_desc = &app.audio_codec; |
| 1438 |
else { |
| 1439 |
|
| 1440 |
for (i=0; i<PJ_ARRAY_SIZE(audio_codecs); ++i) { |
| 1441 |
if (audio_codecs[i].pt == audio->si.fmt.pt) { |
| 1442 |
codec_desc = &audio_codecs[i]; |
| 1443 |
break; |
| 1444 |
} |
| 1445 |
} |
| 1446 |
|
| 1447 |
if (codec_desc == NULL) { |
| 1448 |
PJ_LOG(3, (THIS_FILE, "Error: Invalid codec payload type")); |
| 1449 |
return; |
| 1450 |
} |
| 1451 |
} |
| 1452 |
|
| 1453 |
audio->clock_rate = audio->si.fmt.clock_rate; |
| 1454 |
audio->samples_per_frame = audio->clock_rate * codec_desc->ptime / 1000; |
| 1455 |
audio->bytes_per_frame = codec_desc->bit_rate * codec_desc->ptime / 1000 / 8; |
| 1456 |
|
| 1457 |
|
| 1458 |
pjmedia_rtp_session_init(&audio->out_sess, audio->si.tx_pt, |
| 1459 |
pj_rand()); |
| 1460 |
pjmedia_rtp_session_init(&audio->in_sess, audio->si.fmt.pt, 0); |
| 1461 |
pjmedia_rtcp_init(&audio->rtcp, "rtcp", audio->clock_rate, |
| 1462 |
audio->samples_per_frame, 0); |
| 1463 |
|
| 1464 |
|
| 1465 |
|
| 1466 |
status = pjmedia_transport_attach(audio->transport, audio, |
| 1467 |
&audio->si.rem_addr, |
| 1468 |
&audio->si.rem_rtcp, |
| 1469 |
sizeof(pj_sockaddr_in), |
| 1470 |
&on_rx_rtp, |
| 1471 |
&on_rx_rtcp); |
| 1472 |
if (status != PJ_SUCCESS) { |
| 1473 |
app_perror(THIS_FILE, "Error on pjmedia_transport_attach()", status); |
| 1474 |
return; |
| 1475 |
} |
| 1476 |
|
| 1477 |
|
| 1478 |
pjmedia_transport_media_start(audio->transport, 0, 0, 0, 0); |
| 1479 |
|
| 1480 |
|
| 1481 |
audio->thread_quit_flag = 0; |
| 1482 |
#if PJ_HAS_THREADS |
| 1483 |
status = pj_thread_create( inv->pool, "media", &media_thread, audio, |
| 1484 |
0, 0, &audio->thread); |
| 1485 |
if (status != PJ_SUCCESS) { |
| 1486 |
app_perror(THIS_FILE, "Error creating media thread", status); |
| 1487 |
return; |
| 1488 |
} |
| 1489 |
#endif |
| 1490 |
|
| 1491 |
|
| 1492 |
audio->active = PJ_TRUE; |
| 1493 |
} |
| 1494 |
|
| 1495 |
|
| 1496 |
|
| 1497 |
|
| 1498 |
static void destroy_call_media(unsigned call_index) |
| 1499 |
{ |
| 1500 |
struct media_stream *audio = &app.call[call_index].media[0]; |
| 1501 |
|
| 1502 |
if (audio) { |
| 1503 |
audio->active = PJ_FALSE; |
| 1504 |
|
| 1505 |
if (audio->thread) { |
| 1506 |
audio->thread_quit_flag = 1; |
| 1507 |
pj_thread_join(audio->thread); |
| 1508 |
pj_thread_destroy(audio->thread); |
| 1509 |
audio->thread = NULL; |
| 1510 |
audio->thread_quit_flag = 0; |
| 1511 |
} |
| 1512 |
|
| 1513 |
pjmedia_transport_detach(audio->transport, audio); |
| 1514 |
} |
| 1515 |
} |
| 1516 |
|
| 1517 |
|
| 1518 |
|
| 1519 |
|
| 1520 |
|
| 1521 |
|
| 1522 |
static void call_get_duration(int call_index, pj_time_val *dur) |
| 1523 |
{ |
| 1524 |
struct call *call = &app.call[call_index]; |
| 1525 |
pjsip_inv_session *inv; |
| 1526 |
|
| 1527 |
dur->sec = dur->msec = 0; |
| 1528 |
|
| 1529 |
if (!call) |
| 1530 |
return; |
| 1531 |
|
| 1532 |
inv = call->inv; |
| 1533 |
if (!inv) |
| 1534 |
return; |
| 1535 |
|
| 1536 |
if (inv->state >= PJSIP_INV_STATE_CONFIRMED && call->connect_time.sec) { |
| 1537 |
|
| 1538 |
pj_gettimeofday(dur); |
| 1539 |
PJ_TIME_VAL_SUB((*dur), call->connect_time); |
| 1540 |
} |
| 1541 |
} |
| 1542 |
|
| 1543 |
|
| 1544 |
static const char *good_number(char *buf, unsigned buf_size, pj_int32_t val) |
| 1545 |
{ |
| 1546 |
if (val < 1000) { |
| 1547 |
pj_ansi_snprintf(buf, buf_size, "%d", val); |
| 1548 |
} else if (val < 1000000) { |
| 1549 |
pj_ansi_snprintf(buf, buf_size, "%d.%02dK", |
| 1550 |
val / 1000, |
| 1551 |
(val % 1000) / 100); |
| 1552 |
} else { |
| 1553 |
pj_ansi_snprintf(buf, buf_size, "%d.%02dM", |
| 1554 |
val / 1000000, |
| 1555 |
(val % 1000000) / 10000); |
| 1556 |
} |
| 1557 |
|
| 1558 |
return buf; |
| 1559 |
} |
| 1560 |
|
| 1561 |
|
| 1562 |
|
| 1563 |
static void print_avg_stat(void) |
| 1564 |
{ |
| 1565 |
#define MIN_(var,val) if ((int)val < (int)var) var = val |
| 1566 |
#define MAX_(var,val) if ((int)val > (int)var) var = val |
| 1567 |
#define AVG_(var,val) var = ( ((var * count) + val) / (count+1) ) |
| 1568 |
#define BIGVAL 0x7FFFFFFFL |
| 1569 |
struct stat_entry |
| 1570 |
{ |
| 1571 |
int min, avg, max; |
| 1572 |
}; |
| 1573 |
|
| 1574 |
struct stat_entry call_dur, call_pdd; |
| 1575 |
pjmedia_rtcp_stat min_stat, avg_stat, max_stat; |
| 1576 |
|
| 1577 |
char srx_min[16], srx_avg[16], srx_max[16]; |
| 1578 |
char brx_min[16], brx_avg[16], brx_max[16]; |
| 1579 |
char stx_min[16], stx_avg[16], stx_max[16]; |
| 1580 |
char btx_min[16], btx_avg[16], btx_max[16]; |
| 1581 |
|
| 1582 |
|
| 1583 |
unsigned i, count; |
| 1584 |
|
| 1585 |
pj_bzero(&call_dur, sizeof(call_dur)); |
| 1586 |
call_dur.min = BIGVAL; |
| 1587 |
|
| 1588 |
pj_bzero(&call_pdd, sizeof(call_pdd)); |
| 1589 |
call_pdd.min = BIGVAL; |
| 1590 |
|
| 1591 |
pj_bzero(&min_stat, sizeof(min_stat)); |
| 1592 |
min_stat.rx.pkt = min_stat.tx.pkt = BIGVAL; |
| 1593 |
min_stat.rx.bytes = min_stat.tx.bytes = BIGVAL; |
| 1594 |
min_stat.rx.loss = min_stat.tx.loss = BIGVAL; |
| 1595 |
min_stat.rx.dup = min_stat.tx.dup = BIGVAL; |
| 1596 |
min_stat.rx.reorder = min_stat.tx.reorder = BIGVAL; |
| 1597 |
min_stat.rx.jitter.min = min_stat.tx.jitter.min = BIGVAL; |
| 1598 |
min_stat.rtt.min = BIGVAL; |
| 1599 |
|
| 1600 |
pj_bzero(&avg_stat, sizeof(avg_stat)); |
| 1601 |
pj_bzero(&max_stat, sizeof(max_stat)); |
| 1602 |
|
| 1603 |
|
| 1604 |
for (i=0, count=0; i<app.max_calls; ++i) { |
| 1605 |
|
| 1606 |
struct call *call = &app.call[i]; |
| 1607 |
struct media_stream *audio = &call->media[0]; |
| 1608 |
pj_time_val dur; |
| 1609 |
unsigned msec_dur; |
| 1610 |
|
| 1611 |
if (call->inv == NULL || |
| 1612 |
call->inv->state < PJSIP_INV_STATE_CONFIRMED || |
| 1613 |
call->connect_time.sec == 0) |
| 1614 |
{ |
| 1615 |
continue; |
| 1616 |
} |
| 1617 |
|
| 1618 |
|
| 1619 |
call_get_duration(i, &dur); |
| 1620 |
msec_dur = PJ_TIME_VAL_MSEC(dur); |
| 1621 |
|
| 1622 |
MIN_(call_dur.min, msec_dur); |
| 1623 |
MAX_(call_dur.max, msec_dur); |
| 1624 |
AVG_(call_dur.avg, msec_dur); |
| 1625 |
|
| 1626 |
|
| 1627 |
if (call->connect_time.sec) { |
| 1628 |
pj_time_val t = call->connect_time; |
| 1629 |
PJ_TIME_VAL_SUB(t, call->start_time); |
| 1630 |
msec_dur = PJ_TIME_VAL_MSEC(t); |
| 1631 |
} else { |
| 1632 |
msec_dur = 10; |
| 1633 |
} |
| 1634 |
|
| 1635 |
MIN_(call_pdd.min, msec_dur); |
| 1636 |
MAX_(call_pdd.max, msec_dur); |
| 1637 |
AVG_(call_pdd.avg, msec_dur); |
| 1638 |
|
| 1639 |
|
| 1640 |
|
| 1641 |
|
| 1642 |
MIN_(min_stat.rx.pkt, audio->rtcp.stat.rx.pkt); |
| 1643 |
MAX_(max_stat.rx.pkt, audio->rtcp.stat.rx.pkt); |
| 1644 |
AVG_(avg_stat.rx.pkt, audio->rtcp.stat.rx.pkt); |
| 1645 |
|
| 1646 |
|
| 1647 |
MIN_(min_stat.rx.bytes, audio->rtcp.stat.rx.bytes); |
| 1648 |
MAX_(max_stat.rx.bytes, audio->rtcp.stat.rx.bytes); |
| 1649 |
AVG_(avg_stat.rx.bytes, audio->rtcp.stat.rx.bytes); |
| 1650 |
|
| 1651 |
|
| 1652 |
|
| 1653 |
MIN_(min_stat.rx.loss, audio->rtcp.stat.rx.loss); |
| 1654 |
MAX_(max_stat.rx.loss, audio->rtcp.stat.rx.loss); |
| 1655 |
AVG_(avg_stat.rx.loss, audio->rtcp.stat.rx.loss); |
| 1656 |
|
| 1657 |
|
| 1658 |
MIN_(min_stat.rx.dup, audio->rtcp.stat.rx.dup); |
| 1659 |
MAX_(max_stat.rx.dup, audio->rtcp.stat.rx.dup); |
| 1660 |
AVG_(avg_stat.rx.dup, audio->rtcp.stat.rx.dup); |
| 1661 |
|
| 1662 |
|
| 1663 |
MIN_(min_stat.rx.reorder, audio->rtcp.stat.rx.reorder); |
| 1664 |
MAX_(max_stat.rx.reorder, audio->rtcp.stat.rx.reorder); |
| 1665 |
AVG_(avg_stat.rx.reorder, audio->rtcp.stat.rx.reorder); |
| 1666 |
|
| 1667 |
|
| 1668 |
MIN_(min_stat.rx.jitter.min, audio->rtcp.stat.rx.jitter.min); |
| 1669 |
MAX_(max_stat.rx.jitter.max, audio->rtcp.stat.rx.jitter.max); |
| 1670 |
AVG_(avg_stat.rx.jitter.mean, audio->rtcp.stat.rx.jitter.mean); |
| 1671 |
|
| 1672 |
|
| 1673 |
|
| 1674 |
|
| 1675 |
|
| 1676 |
MIN_(min_stat.tx.pkt, audio->rtcp.stat.tx.pkt); |
| 1677 |
MAX_(max_stat.tx.pkt, audio->rtcp.stat.tx.pkt); |
| 1678 |
AVG_(avg_stat.tx.pkt, audio->rtcp.stat.tx.pkt); |
| 1679 |
|
| 1680 |
|
| 1681 |
MIN_(min_stat.tx.bytes, audio->rtcp.stat.tx.bytes); |
| 1682 |
MAX_(max_stat.tx.bytes, audio->rtcp.stat.tx.bytes); |
| 1683 |
AVG_(avg_stat.tx.bytes, audio->rtcp.stat.tx.bytes); |
| 1684 |
|
| 1685 |
|
| 1686 |
MIN_(min_stat.tx.loss, audio->rtcp.stat.tx.loss); |
| 1687 |
MAX_(max_stat.tx.loss, audio->rtcp.stat.tx.loss); |
| 1688 |
AVG_(avg_stat.tx.loss, audio->rtcp.stat.tx.loss); |
| 1689 |
|
| 1690 |
|
| 1691 |
MIN_(min_stat.tx.dup, audio->rtcp.stat.tx.dup); |
| 1692 |
MAX_(max_stat.tx.dup, audio->rtcp.stat.tx.dup); |
| 1693 |
AVG_(avg_stat.tx.dup, audio->rtcp.stat.tx.dup); |
| 1694 |
|
| 1695 |
|
| 1696 |
MIN_(min_stat.tx.reorder, audio->rtcp.stat.tx.reorder); |
| 1697 |
MAX_(max_stat.tx.reorder, audio->rtcp.stat.tx.reorder); |
| 1698 |
AVG_(avg_stat.tx.reorder, audio->rtcp.stat.tx.reorder); |
| 1699 |
|
| 1700 |
|
| 1701 |
MIN_(min_stat.tx.jitter.min, audio->rtcp.stat.tx.jitter.min); |
| 1702 |
MAX_(max_stat.tx.jitter.max, audio->rtcp.stat.tx.jitter.max); |
| 1703 |
AVG_(avg_stat.tx.jitter.mean, audio->rtcp.stat.tx.jitter.mean); |
| 1704 |
|
| 1705 |
|
| 1706 |
|
| 1707 |
MIN_(min_stat.rtt.min, audio->rtcp.stat.rtt.min); |
| 1708 |
MAX_(max_stat.rtt.max, audio->rtcp.stat.rtt.max); |
| 1709 |
AVG_(avg_stat.rtt.mean, audio->rtcp.stat.rtt.mean); |
| 1710 |
|
| 1711 |
++count; |
| 1712 |
} |
| 1713 |
|
| 1714 |
if (count == 0) { |
| 1715 |
puts("No active calls"); |
| 1716 |
return; |
| 1717 |
} |
| 1718 |
|
| 1719 |
printf("Total %d call(s) active.\n" |
| 1720 |
" Average Statistics\n" |
| 1721 |
" min avg max \n" |
| 1722 |
" -----------------------\n" |
| 1723 |
" call duration: %7d %7d %7d %s\n" |
| 1724 |
" connect delay: %7d %7d %7d %s\n" |
| 1725 |
" RX stat:\n" |
| 1726 |
" packets: %7s %7s %7s %s\n" |
| 1727 |
" payload: %7s %7s %7s %s\n" |
| 1728 |
" loss: %7d %7d %7d %s\n" |
| 1729 |
" percent loss: %7.3f %7.3f %7.3f %s\n" |
| 1730 |
" dup: %7d %7d %7d %s\n" |
| 1731 |
" reorder: %7d %7d %7d %s\n" |
| 1732 |
" jitter: %7.3f %7.3f %7.3f %s\n" |
| 1733 |
" TX stat:\n" |
| 1734 |
" packets: %7s %7s %7s %s\n" |
| 1735 |
" payload: %7s %7s %7s %s\n" |
| 1736 |
" loss: %7d %7d %7d %s\n" |
| 1737 |
" percent loss: %7.3f %7.3f %7.3f %s\n" |
| 1738 |
" dup: %7d %7d %7d %s\n" |
| 1739 |
" reorder: %7d %7d %7d %s\n" |
| 1740 |
" jitter: %7.3f %7.3f %7.3f %s\n" |
| 1741 |
" RTT : %7.3f %7.3f %7.3f %s\n" |
| 1742 |
, |
| 1743 |
count, |
| 1744 |
call_dur.min/1000, call_dur.avg/1000, call_dur.max/1000, |
| 1745 |
"seconds", |
| 1746 |
|
| 1747 |
call_pdd.min, call_pdd.avg, call_pdd.max, |
| 1748 |
"ms", |
| 1749 |
|
| 1750 |
|
| 1751 |
|
| 1752 |
good_number(srx_min, sizeof(srx_min), min_stat.rx.pkt), |
| 1753 |
good_number(srx_avg, sizeof(srx_avg), avg_stat.rx.pkt), |
| 1754 |
good_number(srx_max, sizeof(srx_max), max_stat.rx.pkt), |
| 1755 |
"packets", |
| 1756 |
|
| 1757 |
good_number(brx_min, sizeof(brx_min), min_stat.rx.bytes), |
| 1758 |
good_number(brx_avg, sizeof(brx_avg), avg_stat.rx.bytes), |
| 1759 |
good_number(brx_max, sizeof(brx_max), max_stat.rx.bytes), |
| 1760 |
"bytes", |
| 1761 |
|
| 1762 |
min_stat.rx.loss, avg_stat.rx.loss, max_stat.rx.loss, |
| 1763 |
"packets", |
| 1764 |
|
| 1765 |
min_stat.rx.loss*100.0/(min_stat.rx.pkt+min_stat.rx.loss), |
| 1766 |
avg_stat.rx.loss*100.0/(avg_stat.rx.pkt+avg_stat.rx.loss), |
| 1767 |
max_stat.rx.loss*100.0/(max_stat.rx.pkt+max_stat.rx.loss), |
| 1768 |
"%", |
| 1769 |
|
| 1770 |
|
| 1771 |
min_stat.rx.dup, avg_stat.rx.dup, max_stat.rx.dup, |
| 1772 |
"packets", |
| 1773 |
|
| 1774 |
min_stat.rx.reorder, avg_stat.rx.reorder, max_stat.rx.reorder, |
| 1775 |
"packets", |
| 1776 |
|
| 1777 |
min_stat.rx.jitter.min/1000.0, |
| 1778 |
avg_stat.rx.jitter.mean/1000.0, |
| 1779 |
max_stat.rx.jitter.max/1000.0, |
| 1780 |
"ms", |
| 1781 |
|
| 1782 |
|
| 1783 |
|
| 1784 |
good_number(stx_min, sizeof(stx_min), min_stat.tx.pkt), |
| 1785 |
good_number(stx_avg, sizeof(stx_avg), avg_stat.tx.pkt), |
| 1786 |
good_number(stx_max, sizeof(stx_max), max_stat.tx.pkt), |
| 1787 |
"packets", |
| 1788 |
|
| 1789 |
good_number(btx_min, sizeof(btx_min), min_stat.tx.bytes), |
| 1790 |
good_number(btx_avg, sizeof(btx_avg), avg_stat.tx.bytes), |
| 1791 |
good_number(btx_max, sizeof(btx_max), max_stat.tx.bytes), |
| 1792 |
"bytes", |
| 1793 |
|
| 1794 |
min_stat.tx.loss, avg_stat.tx.loss, max_stat.tx.loss, |
| 1795 |
"packets", |
| 1796 |
|
| 1797 |
min_stat.tx.loss*100.0/(min_stat.tx.pkt), |
| 1798 |
avg_stat.tx.loss*100.0/(avg_stat.tx.pkt), |
| 1799 |
max_stat.tx.loss*100.0/(max_stat.tx.pkt), |
| 1800 |
"%", |
| 1801 |
|
| 1802 |
min_stat.tx.dup, avg_stat.tx.dup, max_stat.tx.dup, |
| 1803 |
"packets", |
| 1804 |
|
| 1805 |
min_stat.tx.reorder, avg_stat.tx.reorder, max_stat.tx.reorder, |
| 1806 |
"packets", |
| 1807 |
|
| 1808 |
min_stat.tx.jitter.min/1000.0, |
| 1809 |
avg_stat.tx.jitter.mean/1000.0, |
| 1810 |
max_stat.tx.jitter.max/1000.0, |
| 1811 |
"ms", |
| 1812 |
|
| 1813 |
|
| 1814 |
min_stat.rtt.min/1000.0, |
| 1815 |
avg_stat.rtt.mean/1000.0, |
| 1816 |
max_stat.rtt.max/1000.0, |
| 1817 |
"ms" |
| 1818 |
); |
| 1819 |
|
| 1820 |
} |
| 1821 |
|
| 1822 |
|
| 1823 |
#include "siprtp_report.c" |
| 1824 |
|
| 1825 |
|
| 1826 |
static void list_calls() |
| 1827 |
{ |
| 1828 |
unsigned i; |
| 1829 |
puts("List all calls:"); |
| 1830 |
for (i=0; i<app.max_calls; ++i) { |
| 1831 |
if (!app.call[i].inv) |
| 1832 |
continue; |
| 1833 |
print_call(i); |
| 1834 |
} |
| 1835 |
} |
| 1836 |
|
| 1837 |
static void hangup_call(unsigned index) |
| 1838 |
{ |
| 1839 |
pjsip_tx_data *tdata; |
| 1840 |
pj_status_t status; |
| 1841 |
|
| 1842 |
if (app.call[index].inv == NULL) |
| 1843 |
return; |
| 1844 |
|
| 1845 |
status = pjsip_inv_end_session(app.call[index].inv, 603, NULL, &tdata); |
| 1846 |
if (status==PJ_SUCCESS && tdata!=NULL) |
| 1847 |
pjsip_inv_send_msg(app.call[index].inv, tdata); |
| 1848 |
} |
| 1849 |
|
| 1850 |
static void hangup_all_calls() |
| 1851 |
{ |
| 1852 |
unsigned i; |
| 1853 |
for (i=0; i<app.max_calls; ++i) { |
| 1854 |
if (!app.call[i].inv) |
| 1855 |
continue; |
| 1856 |
hangup_call(i); |
| 1857 |
pj_thread_sleep(app.call_gap); |
| 1858 |
} |
| 1859 |
|
| 1860 |
|
| 1861 |
for (i=0; i<app.max_calls; ++i) { |
| 1862 |
while (app.call[i].inv) |
| 1863 |
pj_thread_sleep(10); |
| 1864 |
} |
| 1865 |
} |
| 1866 |
|
| 1867 |
static pj_bool_t simple_input(const char *title, char *buf, pj_size_t len) |
| 1868 |
{ |
| 1869 |
char *p; |
| 1870 |
|
| 1871 |
printf("%s (empty to cancel): ", title); fflush(stdout); |
| 1872 |
if (fgets(buf, (int)len, stdin) == NULL) |
| 1873 |
return PJ_FALSE; |
| 1874 |
|
| 1875 |
|
| 1876 |
for (p=buf; ; ++p) { |
| 1877 |
if (*p=='\r' || *p=='\n') *p='\0'; |
| 1878 |
else if (!*p) break; |
| 1879 |
} |
| 1880 |
|
| 1881 |
if (!*buf) |
| 1882 |
return PJ_FALSE; |
| 1883 |
|
| 1884 |
return PJ_TRUE; |
| 1885 |
} |
| 1886 |
|
| 1887 |
|
| 1888 |
static const char *MENU = |
| 1889 |
"\n" |
| 1890 |
"Enter menu character:\n" |
| 1891 |
" s Summary\n" |
| 1892 |
" l List all calls\n" |
| 1893 |
" h Hangup a call\n" |
| 1894 |
" H Hangup all calls\n" |
| 1895 |
" q Quit\n" |
| 1896 |
"\n"; |
| 1897 |
|
| 1898 |
|
| 1899 |
|
| 1900 |
static void console_main() |
| 1901 |
{ |
| 1902 |
char input1[10]; |
| 1903 |
unsigned i; |
| 1904 |
|
| 1905 |
printf("%s", MENU); |
| 1906 |
|
| 1907 |
for (;;) { |
| 1908 |
printf(">>> "); fflush(stdout); |
| 1909 |
if (fgets(input1, sizeof(input1), stdin) == NULL) { |
| 1910 |
puts("EOF while reading stdin, will quit now.."); |
| 1911 |
break; |
| 1912 |
} |
| 1913 |
|
| 1914 |
switch (input1[0]) { |
| 1915 |
|
| 1916 |
case 's': |
| 1917 |
print_avg_stat(); |
| 1918 |
break; |
| 1919 |
|
| 1920 |
case 'l': |
| 1921 |
list_calls(); |
| 1922 |
break; |
| 1923 |
|
| 1924 |
case 'h': |
| 1925 |
if (!simple_input("Call number to hangup", input1, sizeof(input1))) |
| 1926 |
break; |
| 1927 |
|
| 1928 |
i = atoi(input1); |
| 1929 |
hangup_call(i); |
| 1930 |
break; |
| 1931 |
|
| 1932 |
case 'H': |
| 1933 |
hangup_all_calls(); |
| 1934 |
break; |
| 1935 |
|
| 1936 |
case 'q': |
| 1937 |
goto on_exit; |
| 1938 |
|
| 1939 |
default: |
| 1940 |
puts("Invalid command"); |
| 1941 |
printf("%s", MENU); |
| 1942 |
break; |
| 1943 |
} |
| 1944 |
|
| 1945 |
fflush(stdout); |
| 1946 |
} |
| 1947 |
|
| 1948 |
on_exit: |
| 1949 |
hangup_all_calls(); |
| 1950 |
} |
| 1951 |
|
| 1952 |
|
| 1953 |
|
| 1954 |
|
| 1955 |
|
| 1956 |
|
| 1957 |
|
| 1958 |
|
| 1959 |
static pj_bool_t logger_on_rx_msg(pjsip_rx_data *rdata) |
| 1960 |
{ |
| 1961 |
PJ_LOG(4,(THIS_FILE, "RX %d bytes %s from %s:%d:\n" |
| 1962 |
"%s\n" |
| 1963 |
"--end msg--", |
| 1964 |
rdata->msg_info.len, |
| 1965 |
pjsip_rx_data_get_info(rdata), |
| 1966 |
rdata->pkt_info.src_name, |
| 1967 |
rdata->pkt_info.src_port, |
| 1968 |
rdata->msg_info.msg_buf)); |
| 1969 |
|
| 1970 |
|
| 1971 |
return PJ_FALSE; |
| 1972 |
} |
| 1973 |
|
| 1974 |
|
| 1975 |
static pj_status_t logger_on_tx_msg(pjsip_tx_data *tdata) |
| 1976 |
{ |
| 1977 |
|
| 1978 |
|
| 1979 |
|
| 1980 |
|
| 1981 |
|
| 1982 |
|
| 1983 |
|
| 1984 |
PJ_LOG(4,(THIS_FILE, "TX %ld bytes %s to %s:%d:\n" |
| 1985 |
"%s\n" |
| 1986 |
"--end msg--", |
| 1987 |
(tdata->buf.cur - tdata->buf.start), |
| 1988 |
pjsip_tx_data_get_info(tdata), |
| 1989 |
tdata->tp_info.dst_name, |
| 1990 |
tdata->tp_info.dst_port, |
| 1991 |
tdata->buf.start)); |
| 1992 |
|
| 1993 |
|
| 1994 |
return PJ_SUCCESS; |
| 1995 |
} |
| 1996 |
|
| 1997 |
|
| 1998 |
static pjsip_module msg_logger = |
| 1999 |
{ |
| 2000 |
NULL, NULL, |
| 2001 |
{ "mod-siprtp-log", 14 }, |
| 2002 |
-1, |
| 2003 |
PJSIP_MOD_PRIORITY_TRANSPORT_LAYER-1, |
| 2004 |
NULL, |
| 2005 |
NULL, |
| 2006 |
NULL, |
| 2007 |
NULL, |
| 2008 |
&logger_on_rx_msg, |
| 2009 |
&logger_on_rx_msg, |
| 2010 |
&logger_on_tx_msg, |
| 2011 |
&logger_on_tx_msg, |
| 2012 |
NULL, |
| 2013 |
|
| 2014 |
}; |
| 2015 |
|
| 2016 |
|
| 2017 |
|
| 2018 |
|
| 2019 |
|
| 2020 |
|
| 2021 |
|
| 2022 |
|
| 2023 |
static FILE *log_file; |
| 2024 |
|
| 2025 |
|
| 2026 |
static void app_log_writer(int level, const char *buffer, int len) |
| 2027 |
{ |
| 2028 |
|
| 2029 |
|
| 2030 |
if (level <= app.app_log_level) |
| 2031 |
pj_log_write(level, buffer, len); |
| 2032 |
|
| 2033 |
if (log_file) { |
| 2034 |
pj_size_t count = fwrite(buffer, len, 1, log_file); |
| 2035 |
PJ_UNUSED_ARG(count); |
| 2036 |
fflush(log_file); |
| 2037 |
} |
| 2038 |
} |
| 2039 |
|
| 2040 |
|
| 2041 |
pj_status_t app_logging_init(void) |
| 2042 |
{ |
| 2043 |
|
| 2044 |
|
| 2045 |
pj_log_set_log_func( &app_log_writer ); |
| 2046 |
|
| 2047 |
|
| 2048 |
|
| 2049 |
if (app.log_filename) { |
| 2050 |
log_file = fopen(app.log_filename, "wt"); |
| 2051 |
if (log_file == NULL) { |
| 2052 |
PJ_LOG(1,(THIS_FILE, "Unable to open log file %s", |
| 2053 |
app.log_filename)); |
| 2054 |
return -1; |
| 2055 |
} |
| 2056 |
} |
| 2057 |
|
| 2058 |
return PJ_SUCCESS; |
| 2059 |
} |
| 2060 |
|
| 2061 |
|
| 2062 |
void app_logging_shutdown(void) |
| 2063 |
{ |
| 2064 |
|
| 2065 |
|
| 2066 |
if (log_file) { |
| 2067 |
fclose(log_file); |
| 2068 |
log_file = NULL; |
| 2069 |
} |
| 2070 |
} |
| 2071 |
|
| 2072 |
|
| 2073 |
|
| 2074 |
|
| 2075 |
|
| 2076 |
int main(int argc, char *argv[]) |
| 2077 |
{ |
| 2078 |
unsigned i; |
| 2079 |
pj_status_t status; |
| 2080 |
|
| 2081 |
|
| 2082 |
status = pj_init(); |
| 2083 |
if (status != PJ_SUCCESS) |
| 2084 |
return 1; |
| 2085 |
|
| 2086 |
|
| 2087 |
status = init_options(argc, argv); |
| 2088 |
if (status != PJ_SUCCESS) |
| 2089 |
return 1; |
| 2090 |
|
| 2091 |
|
| 2092 |
|
| 2093 |
|
| 2094 |
if (app.auto_quit && app.uri_to_call.slen == 0) { |
| 2095 |
printf("Error: --auto-quit option only valid for outgoing " |
| 2096 |
"mode (UAC) only\n"); |
| 2097 |
return 1; |
| 2098 |
} |
| 2099 |
|
| 2100 |
|
| 2101 |
status = app_logging_init(); |
| 2102 |
if (status != PJ_SUCCESS) |
| 2103 |
return 1; |
| 2104 |
|
| 2105 |
|
| 2106 |
status = init_sip(); |
| 2107 |
if (status != PJ_SUCCESS) { |
| 2108 |
app_perror(THIS_FILE, "Initialization has failed", status); |
| 2109 |
destroy_sip(); |
| 2110 |
return 1; |
| 2111 |
} |
| 2112 |
|
| 2113 |
|
| 2114 |
pjsip_endpt_register_module(app.sip_endpt, &msg_logger); |
| 2115 |
|
| 2116 |
|
| 2117 |
status = init_media(); |
| 2118 |
if (status != PJ_SUCCESS) { |
| 2119 |
app_perror(THIS_FILE, "Media initialization failed", status); |
| 2120 |
destroy_sip(); |
| 2121 |
return 1; |
| 2122 |
} |
| 2123 |
|
| 2124 |
|
| 2125 |
#if PJ_HAS_THREADS |
| 2126 |
for (i=0; i<app.thread_count; ++i) { |
| 2127 |
pj_thread_create( app.pool, "app", &sip_worker_thread, NULL, |
| 2128 |
0, 0, &app.sip_thread[i]); |
| 2129 |
} |
| 2130 |
#endif |
| 2131 |
|
| 2132 |
|
| 2133 |
if (app.uri_to_call.slen) { |
| 2134 |
PJ_LOG(3,(THIS_FILE, "Making %d calls to %s..", app.max_calls, |
| 2135 |
app.uri_to_call.ptr)); |
| 2136 |
|
| 2137 |
for (i=0; i<app.max_calls; ++i) { |
| 2138 |
status = make_call(&app.uri_to_call); |
| 2139 |
if (status != PJ_SUCCESS) { |
| 2140 |
app_perror(THIS_FILE, "Error making call", status); |
| 2141 |
break; |
| 2142 |
} |
| 2143 |
pj_thread_sleep(app.call_gap); |
| 2144 |
} |
| 2145 |
|
| 2146 |
if (app.auto_quit) { |
| 2147 |
|
| 2148 |
while (app.uac_calls < app.max_calls) |
| 2149 |
pj_thread_sleep(100); |
| 2150 |
pj_thread_sleep(200); |
| 2151 |
} else { |
| 2152 |
#if PJ_HAS_THREADS |
| 2153 |
|
| 2154 |
console_main(); |
| 2155 |
#endif |
| 2156 |
} |
| 2157 |
|
| 2158 |
} else { |
| 2159 |
|
| 2160 |
PJ_LOG(3,(THIS_FILE, "Ready for incoming calls (max=%d)", |
| 2161 |
app.max_calls)); |
| 2162 |
|
| 2163 |
#if PJ_HAS_THREADS |
| 2164 |
|
| 2165 |
console_main(); |
| 2166 |
#endif |
| 2167 |
} |
| 2168 |
|
| 2169 |
#if !PJ_HAS_THREADS |
| 2170 |
PJ_LOG(3,(THIS_FILE, "Press Ctrl-C to quit")); |
| 2171 |
for (;;) { |
| 2172 |
pj_time_val t = {0, 10}; |
| 2173 |
pjsip_endpt_handle_events(app.sip_endpt, &t); |
| 2174 |
} |
| 2175 |
#endif |
| 2176 |
|
| 2177 |
|
| 2178 |
destroy_sip(); |
| 2179 |
destroy_media(); |
| 2180 |
|
| 2181 |
if (app.pool) { |
| 2182 |
pj_pool_release(app.pool); |
| 2183 |
app.pool = NULL; |
| 2184 |
pj_caching_pool_destroy(&app.cp); |
| 2185 |
} |
| 2186 |
|
| 2187 |
app_logging_shutdown(); |
| 2188 |
|
| 2189 |
|
| 2190 |
pj_shutdown(); |
| 2191 |
|
| 2192 |
return 0; |
| 2193 |
} |
| 2194 |
|