| 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 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
#include <pjsip.h> |
| 55 |
#include <pjmedia.h> |
| 56 |
#include <pjmedia-codec.h> |
| 57 |
#include <pjsip_ua.h> |
| 58 |
#include <pjsip_simple.h> |
| 59 |
#include <pjlib-util.h> |
| 60 |
#include <pjlib.h> |
| 61 |
|
| 62 |
|
| 63 |
#define THIS_FILE "simpleua.c" |
| 64 |
|
| 65 |
#include "util.h" |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
#define AF pj_AF_INET() |
| 70 |
|
| 71 |
|
| 72 |
#if 0 |
| 73 |
#define SIP_PORT 5080 |
| 74 |
#define RTP_PORT 5000 |
| 75 |
#else |
| 76 |
#define SIP_PORT 5060 |
| 77 |
#define RTP_PORT 4000 |
| 78 |
#endif |
| 79 |
|
| 80 |
#define MAX_MEDIA_CNT 2 |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
static pj_bool_t g_complete; |
| 88 |
static pjsip_endpoint *g_endpt; |
| 89 |
static pj_caching_pool cp; |
| 90 |
|
| 91 |
static pjmedia_endpt *g_med_endpt; |
| 92 |
|
| 93 |
static pjmedia_transport_info g_med_tpinfo[MAX_MEDIA_CNT]; |
| 94 |
|
| 95 |
static pjmedia_transport *g_med_transport[MAX_MEDIA_CNT]; |
| 96 |
|
| 97 |
static pjmedia_sock_info g_sock_info[MAX_MEDIA_CNT]; |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
static pjsip_inv_session *g_inv; |
| 102 |
static pjmedia_stream *g_med_stream; |
| 103 |
static pjmedia_snd_port *g_snd_port; |
| 104 |
|
| 105 |
#if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0) |
| 106 |
static pjmedia_vid_stream *g_med_vstream; |
| 107 |
static pjmedia_vid_port *g_vid_capturer; |
| 108 |
static pjmedia_vid_port *g_vid_renderer; |
| 109 |
#endif |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
static void call_on_media_update( pjsip_inv_session *inv, |
| 117 |
pj_status_t status); |
| 118 |
|
| 119 |
|
| 120 |
static void call_on_state_changed( pjsip_inv_session *inv, |
| 121 |
pjsip_event *e); |
| 122 |
|
| 123 |
|
| 124 |
static void call_on_forked(pjsip_inv_session *inv, pjsip_event *e); |
| 125 |
|
| 126 |
|
| 127 |
static pj_bool_t on_rx_request( pjsip_rx_data *rdata ); |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
static pjsip_module mod_simpleua = |
| 138 |
{ |
| 139 |
NULL, NULL, |
| 140 |
{ "mod-simpleua", 12 }, |
| 141 |
-1, |
| 142 |
PJSIP_MOD_PRIORITY_APPLICATION, |
| 143 |
NULL, |
| 144 |
NULL, |
| 145 |
NULL, |
| 146 |
NULL, |
| 147 |
&on_rx_request, |
| 148 |
NULL, |
| 149 |
NULL, |
| 150 |
NULL, |
| 151 |
NULL, |
| 152 |
}; |
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
static pj_bool_t logging_on_rx_msg(pjsip_rx_data *rdata) |
| 157 |
{ |
| 158 |
PJ_LOG(4,(THIS_FILE, "RX %d bytes %s from %s %s:%d:\n" |
| 159 |
"%.*s\n" |
| 160 |
"--end msg--", |
| 161 |
rdata->msg_info.len, |
| 162 |
pjsip_rx_data_get_info(rdata), |
| 163 |
rdata->tp_info.transport->type_name, |
| 164 |
rdata->pkt_info.src_name, |
| 165 |
rdata->pkt_info.src_port, |
| 166 |
(int)rdata->msg_info.len, |
| 167 |
rdata->msg_info.msg_buf)); |
| 168 |
|
| 169 |
|
| 170 |
return PJ_FALSE; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
static pj_status_t logging_on_tx_msg(pjsip_tx_data *tdata) |
| 175 |
{ |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
PJ_LOG(4,(THIS_FILE, "TX %ld bytes %s to %s %s:%d:\n" |
| 184 |
"%.*s\n" |
| 185 |
"--end msg--", |
| 186 |
(tdata->buf.cur - tdata->buf.start), |
| 187 |
pjsip_tx_data_get_info(tdata), |
| 188 |
tdata->tp_info.transport->type_name, |
| 189 |
tdata->tp_info.dst_name, |
| 190 |
tdata->tp_info.dst_port, |
| 191 |
(int)(tdata->buf.cur - tdata->buf.start), |
| 192 |
tdata->buf.start)); |
| 193 |
|
| 194 |
|
| 195 |
return PJ_SUCCESS; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
static pjsip_module msg_logger = |
| 200 |
{ |
| 201 |
NULL, NULL, |
| 202 |
{ "mod-msg-log", 13 }, |
| 203 |
-1, |
| 204 |
PJSIP_MOD_PRIORITY_TRANSPORT_LAYER-1, |
| 205 |
NULL, |
| 206 |
NULL, |
| 207 |
NULL, |
| 208 |
NULL, |
| 209 |
&logging_on_rx_msg, |
| 210 |
&logging_on_rx_msg, |
| 211 |
&logging_on_tx_msg, |
| 212 |
&logging_on_tx_msg, |
| 213 |
NULL, |
| 214 |
|
| 215 |
}; |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
int main(int argc, char *argv[]) |
| 225 |
{ |
| 226 |
pj_pool_t *pool = NULL; |
| 227 |
pj_status_t status; |
| 228 |
unsigned i; |
| 229 |
|
| 230 |
|
| 231 |
status = pj_init(); |
| 232 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 233 |
|
| 234 |
pj_log_set_level(5); |
| 235 |
|
| 236 |
|
| 237 |
status = pjlib_util_init(); |
| 238 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
pj_caching_pool_init(&cp, &pj_pool_factory_default_policy, 0); |
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
{ |
| 247 |
const pj_str_t *hostname; |
| 248 |
const char *endpt_name; |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
hostname = pj_gethostname(); |
| 256 |
endpt_name = hostname->ptr; |
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
status = pjsip_endpt_create(&cp.factory, endpt_name, |
| 261 |
&g_endpt); |
| 262 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
{ |
| 273 |
pj_sockaddr addr; |
| 274 |
int af = AF; |
| 275 |
|
| 276 |
pj_sockaddr_init(af, &addr, NULL, (pj_uint16_t)SIP_PORT); |
| 277 |
|
| 278 |
if (af == pj_AF_INET()) { |
| 279 |
status = pjsip_udp_transport_start( g_endpt, &addr.ipv4, NULL, |
| 280 |
1, NULL); |
| 281 |
} else if (af == pj_AF_INET6()) { |
| 282 |
status = pjsip_udp_transport_start6(g_endpt, &addr.ipv6, NULL, |
| 283 |
1, NULL); |
| 284 |
} else { |
| 285 |
status = PJ_EAFNOTSUP; |
| 286 |
} |
| 287 |
|
| 288 |
if (status != PJ_SUCCESS) { |
| 289 |
app_perror(THIS_FILE, "Unable to start UDP transport", status); |
| 290 |
return 1; |
| 291 |
} |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
status = pjsip_tsx_layer_init_module(g_endpt); |
| 300 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
status = pjsip_ua_init_module( g_endpt, NULL ); |
| 308 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
{ |
| 324 |
pjsip_inv_callback inv_cb; |
| 325 |
|
| 326 |
|
| 327 |
pj_bzero(&inv_cb, sizeof(inv_cb)); |
| 328 |
inv_cb.on_state_changed = &call_on_state_changed; |
| 329 |
inv_cb.on_new_session = &call_on_forked; |
| 330 |
inv_cb.on_media_update = &call_on_media_update; |
| 331 |
|
| 332 |
|
| 333 |
status = pjsip_inv_usage_init(g_endpt, &inv_cb); |
| 334 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
status = pjsip_100rel_init_module(g_endpt); |
| 339 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, status); |
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
status = pjsip_endpt_register_module( g_endpt, &mod_simpleua); |
| 345 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
status = pjsip_endpt_register_module( g_endpt, &msg_logger); |
| 351 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
#if PJ_HAS_THREADS |
| 359 |
status = pjmedia_endpt_create(&cp.factory, NULL, 1, &g_med_endpt); |
| 360 |
#else |
| 361 |
status = pjmedia_endpt_create(&cp.factory, |
| 362 |
pjsip_endpt_get_ioqueue(g_endpt), |
| 363 |
0, &g_med_endpt); |
| 364 |
#endif |
| 365 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 366 |
|
| 367 |
|
| 368 |
pool = pjmedia_endpt_create_pool(g_med_endpt, "Media pool", 512, 512); |
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
#if defined(PJMEDIA_HAS_G711_CODEC) && PJMEDIA_HAS_G711_CODEC!=0 |
| 374 |
status = pjmedia_codec_g711_init(g_med_endpt); |
| 375 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 376 |
#endif |
| 377 |
|
| 378 |
|
| 379 |
#if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0) |
| 380 |
|
| 381 |
status = pjmedia_video_format_mgr_create(pool, 64, 0, NULL); |
| 382 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 383 |
status = pjmedia_converter_mgr_create(pool, NULL); |
| 384 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 385 |
status = pjmedia_vid_codec_mgr_create(pool, NULL); |
| 386 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 387 |
status = pjmedia_vid_dev_subsys_init(&cp.factory); |
| 388 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 389 |
|
| 390 |
#if PJMEDIA_HAS_VIDEO && PJMEDIA_HAS_VID_TOOLBOX_CODEC |
| 391 |
status = pjmedia_codec_vid_toolbox_init(NULL, &cp.factory); |
| 392 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 393 |
#endif |
| 394 |
|
| 395 |
# if defined(PJMEDIA_HAS_OPENH264_CODEC) && PJMEDIA_HAS_OPENH264_CODEC != 0 |
| 396 |
status = pjmedia_codec_openh264_vid_init(NULL, &cp.factory); |
| 397 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 398 |
# endif |
| 399 |
|
| 400 |
# if defined(PJMEDIA_HAS_VPX_CODEC) && PJMEDIA_HAS_VPX_CODEC != 0 |
| 401 |
status = pjmedia_codec_vpx_vid_init(NULL, &cp.factory); |
| 402 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 403 |
# endif |
| 404 |
|
| 405 |
# if defined(PJMEDIA_HAS_FFMPEG_VID_CODEC) && PJMEDIA_HAS_FFMPEG_VID_CODEC!=0 |
| 406 |
|
| 407 |
status = pjmedia_codec_ffmpeg_vid_init(NULL, &cp.factory); |
| 408 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 409 |
# endif |
| 410 |
|
| 411 |
#endif |
| 412 |
|
| 413 |
|
| 414 |
status = pjmedia_event_mgr_create(pool, 0, NULL); |
| 415 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
for (i = 0; i < PJ_ARRAY_SIZE(g_med_transport); ++i) { |
| 423 |
status = pjmedia_transport_udp_create3(g_med_endpt, AF, NULL, NULL, |
| 424 |
RTP_PORT + i*2, 0, |
| 425 |
&g_med_transport[i]); |
| 426 |
if (status != PJ_SUCCESS) { |
| 427 |
app_perror(THIS_FILE, "Unable to create media transport", status); |
| 428 |
return 1; |
| 429 |
} |
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
pjmedia_transport_info_init(&g_med_tpinfo[i]); |
| 437 |
pjmedia_transport_get_info(g_med_transport[i], &g_med_tpinfo[i]); |
| 438 |
|
| 439 |
pj_memcpy(&g_sock_info[i], &g_med_tpinfo[i].sock_info, |
| 440 |
sizeof(pjmedia_sock_info)); |
| 441 |
} |
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
if (argc > 1) { |
| 447 |
pj_sockaddr hostaddr; |
| 448 |
char hostip[PJ_INET6_ADDRSTRLEN+2]; |
| 449 |
char temp[80]; |
| 450 |
pj_str_t dst_uri = pj_str(argv[1]); |
| 451 |
pj_str_t local_uri; |
| 452 |
pjsip_dialog *dlg; |
| 453 |
pjmedia_sdp_session *local_sdp; |
| 454 |
pjsip_tx_data *tdata; |
| 455 |
|
| 456 |
if (pj_gethostip(AF, &hostaddr) != PJ_SUCCESS) { |
| 457 |
app_perror(THIS_FILE, "Unable to retrieve local host IP", status); |
| 458 |
return 1; |
| 459 |
} |
| 460 |
pj_sockaddr_print(&hostaddr, hostip, sizeof(hostip), 2); |
| 461 |
|
| 462 |
pj_ansi_snprintf(temp, sizeof(temp), "<sip:simpleuac@%s:%d>", |
| 463 |
hostip, SIP_PORT); |
| 464 |
local_uri = pj_str(temp); |
| 465 |
|
| 466 |
|
| 467 |
status = pjsip_dlg_create_uac( pjsip_ua_instance(), |
| 468 |
&local_uri, |
| 469 |
&local_uri, |
| 470 |
&dst_uri, |
| 471 |
&dst_uri, |
| 472 |
&dlg); |
| 473 |
if (status != PJ_SUCCESS) { |
| 474 |
app_perror(THIS_FILE, "Unable to create UAC dialog", status); |
| 475 |
return 1; |
| 476 |
} |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
status = pjmedia_endpt_create_sdp( g_med_endpt, |
| 500 |
dlg->pool, |
| 501 |
MAX_MEDIA_CNT, |
| 502 |
g_sock_info, |
| 503 |
&local_sdp); |
| 504 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
status = pjsip_inv_create_uac( dlg, local_sdp, 0, &g_inv); |
| 512 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
status = pjsip_inv_invite(g_inv, &tdata); |
| 545 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
status = pjsip_inv_send_msg(g_inv, tdata); |
| 554 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, 1); |
| 555 |
|
| 556 |
|
| 557 |
} else { |
| 558 |
|
| 559 |
|
| 560 |
|
| 561 |
PJ_LOG(3,(THIS_FILE, "Ready to accept incoming calls...")); |
| 562 |
} |
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
for (;!g_complete;) { |
| 567 |
pj_time_val timeout = {0, 10}; |
| 568 |
pjsip_endpt_handle_events(g_endpt, &timeout); |
| 569 |
} |
| 570 |
|
| 571 |
|
| 572 |
dump_pool_usage(THIS_FILE, &cp); |
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
if (g_snd_port) |
| 579 |
pjmedia_snd_port_destroy(g_snd_port); |
| 580 |
|
| 581 |
#if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0) |
| 582 |
|
| 583 |
if (g_vid_capturer) |
| 584 |
pjmedia_vid_port_destroy(g_vid_capturer); |
| 585 |
if (g_vid_renderer) |
| 586 |
pjmedia_vid_port_destroy(g_vid_renderer); |
| 587 |
#endif |
| 588 |
|
| 589 |
|
| 590 |
if (g_med_stream) |
| 591 |
pjmedia_stream_destroy(g_med_stream); |
| 592 |
#if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0) |
| 593 |
if (g_med_vstream) |
| 594 |
pjmedia_vid_stream_destroy(g_med_vstream); |
| 595 |
|
| 596 |
|
| 597 |
# if defined(PJMEDIA_HAS_FFMPEG_VID_CODEC) && PJMEDIA_HAS_FFMPEG_VID_CODEC!=0 |
| 598 |
pjmedia_codec_ffmpeg_vid_deinit(); |
| 599 |
# endif |
| 600 |
# if defined(PJMEDIA_HAS_OPENH264_CODEC) && PJMEDIA_HAS_OPENH264_CODEC != 0 |
| 601 |
pjmedia_codec_openh264_vid_deinit(); |
| 602 |
# endif |
| 603 |
# if PJMEDIA_HAS_VIDEO && PJMEDIA_HAS_VID_TOOLBOX_CODEC |
| 604 |
pjmedia_codec_vid_toolbox_deinit(); |
| 605 |
# endif |
| 606 |
# if defined(PJMEDIA_HAS_VPX_CODEC) && PJMEDIA_HAS_VPX_CODEC != 0 |
| 607 |
pjmedia_codec_vpx_vid_deinit(); |
| 608 |
# endif |
| 609 |
|
| 610 |
#endif |
| 611 |
|
| 612 |
|
| 613 |
for (i = 0; i < MAX_MEDIA_CNT; ++i) { |
| 614 |
if (g_med_transport[i]) |
| 615 |
pjmedia_transport_close(g_med_transport[i]); |
| 616 |
} |
| 617 |
|
| 618 |
|
| 619 |
pjmedia_event_mgr_destroy(NULL); |
| 620 |
|
| 621 |
|
| 622 |
if (g_med_endpt) |
| 623 |
pjmedia_endpt_destroy(g_med_endpt); |
| 624 |
|
| 625 |
|
| 626 |
if (g_endpt) |
| 627 |
pjsip_endpt_destroy(g_endpt); |
| 628 |
|
| 629 |
|
| 630 |
if (pool) |
| 631 |
pj_pool_release(pool); |
| 632 |
|
| 633 |
return 0; |
| 634 |
} |
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
static void call_on_state_changed( pjsip_inv_session *inv, |
| 645 |
pjsip_event *e) |
| 646 |
{ |
| 647 |
PJ_UNUSED_ARG(e); |
| 648 |
|
| 649 |
if (inv->state == PJSIP_INV_STATE_DISCONNECTED) { |
| 650 |
|
| 651 |
PJ_LOG(3,(THIS_FILE, "Call DISCONNECTED [reason=%d (%s)]", |
| 652 |
inv->cause, |
| 653 |
pjsip_get_status_text(inv->cause)->ptr)); |
| 654 |
|
| 655 |
PJ_LOG(3,(THIS_FILE, "One call completed, application quitting...")); |
| 656 |
g_complete = 1; |
| 657 |
|
| 658 |
} else { |
| 659 |
|
| 660 |
PJ_LOG(3,(THIS_FILE, "Call state changed to %s", |
| 661 |
pjsip_inv_state_name(inv->state))); |
| 662 |
|
| 663 |
} |
| 664 |
} |
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
static void call_on_forked(pjsip_inv_session *inv, pjsip_event *e) |
| 669 |
{ |
| 670 |
|
| 671 |
PJ_UNUSED_ARG(inv); |
| 672 |
PJ_UNUSED_ARG(e); |
| 673 |
} |
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
static pj_bool_t on_rx_request( pjsip_rx_data *rdata ) |
| 682 |
{ |
| 683 |
pj_sockaddr hostaddr; |
| 684 |
char temp[80], hostip[PJ_INET6_ADDRSTRLEN]; |
| 685 |
pj_str_t local_uri; |
| 686 |
pjsip_dialog *dlg; |
| 687 |
pjmedia_sdp_session *local_sdp; |
| 688 |
pjsip_tx_data *tdata; |
| 689 |
unsigned options = 0; |
| 690 |
pj_status_t status; |
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
if (rdata->msg_info.msg->line.req.method.id != PJSIP_INVITE_METHOD) { |
| 697 |
|
| 698 |
if (rdata->msg_info.msg->line.req.method.id != PJSIP_ACK_METHOD) { |
| 699 |
pj_str_t reason = pj_str("Simple UA unable to handle " |
| 700 |
"this request"); |
| 701 |
|
| 702 |
pjsip_endpt_respond_stateless( g_endpt, rdata, |
| 703 |
500, &reason, |
| 704 |
NULL, NULL); |
| 705 |
} |
| 706 |
return PJ_TRUE; |
| 707 |
} |
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
if (g_inv) { |
| 714 |
|
| 715 |
pj_str_t reason = pj_str("Another call is in progress"); |
| 716 |
|
| 717 |
pjsip_endpt_respond_stateless( g_endpt, rdata, |
| 718 |
500, &reason, |
| 719 |
NULL, NULL); |
| 720 |
return PJ_TRUE; |
| 721 |
|
| 722 |
} |
| 723 |
|
| 724 |
|
| 725 |
status = pjsip_inv_verify_request(rdata, &options, NULL, NULL, |
| 726 |
g_endpt, NULL); |
| 727 |
if (status != PJ_SUCCESS) { |
| 728 |
|
| 729 |
pj_str_t reason = pj_str("Sorry Simple UA can not handle this INVITE"); |
| 730 |
|
| 731 |
pjsip_endpt_respond_stateless( g_endpt, rdata, |
| 732 |
500, &reason, |
| 733 |
NULL, NULL); |
| 734 |
return PJ_TRUE; |
| 735 |
} |
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
if (pj_gethostip(AF, &hostaddr) != PJ_SUCCESS) { |
| 741 |
app_perror(THIS_FILE, "Unable to retrieve local host IP", status); |
| 742 |
return PJ_TRUE; |
| 743 |
} |
| 744 |
pj_sockaddr_print(&hostaddr, hostip, sizeof(hostip), 2); |
| 745 |
|
| 746 |
pj_ansi_snprintf(temp, sizeof(temp), "<sip:simpleuas@%s:%d>", |
| 747 |
hostip, SIP_PORT); |
| 748 |
local_uri = pj_str(temp); |
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
status = pjsip_dlg_create_uas_and_inc_lock( pjsip_ua_instance(), |
| 754 |
rdata, |
| 755 |
&local_uri, |
| 756 |
&dlg); |
| 757 |
if (status != PJ_SUCCESS) { |
| 758 |
pjsip_endpt_respond_stateless(g_endpt, rdata, 500, NULL, |
| 759 |
NULL, NULL); |
| 760 |
return PJ_TRUE; |
| 761 |
} |
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
status = pjmedia_endpt_create_sdp( g_med_endpt, rdata->tp_info.pool, |
| 768 |
MAX_MEDIA_CNT, g_sock_info, &local_sdp); |
| 769 |
pj_assert(status == PJ_SUCCESS); |
| 770 |
if (status != PJ_SUCCESS) { |
| 771 |
pjsip_dlg_dec_lock(dlg); |
| 772 |
return PJ_TRUE; |
| 773 |
} |
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
status = pjsip_inv_create_uas( dlg, rdata, local_sdp, 0, &g_inv); |
| 781 |
pj_assert(status == PJ_SUCCESS); |
| 782 |
if (status != PJ_SUCCESS) { |
| 783 |
pjsip_dlg_dec_lock(dlg); |
| 784 |
return PJ_TRUE; |
| 785 |
} |
| 786 |
|
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
pjsip_dlg_dec_lock(dlg); |
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
status = pjsip_inv_initial_answer(g_inv, rdata, |
| 801 |
180, |
| 802 |
NULL, NULL, &tdata); |
| 803 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE); |
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
status = pjsip_inv_send_msg(g_inv, tdata); |
| 808 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE); |
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
status = pjsip_inv_answer( g_inv, |
| 815 |
200, NULL, |
| 816 |
NULL, |
| 817 |
&tdata); |
| 818 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE); |
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
status = pjsip_inv_send_msg(g_inv, tdata); |
| 824 |
PJ_ASSERT_RETURN(status == PJ_SUCCESS, PJ_TRUE); |
| 825 |
|
| 826 |
|
| 827 |
|
| 828 |
|
| 829 |
|
| 830 |
|
| 831 |
return PJ_TRUE; |
| 832 |
} |
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
static void call_on_media_update( pjsip_inv_session *inv, |
| 842 |
pj_status_t status) |
| 843 |
{ |
| 844 |
pjmedia_stream_info stream_info; |
| 845 |
const pjmedia_sdp_session *local_sdp; |
| 846 |
const pjmedia_sdp_session *remote_sdp; |
| 847 |
pjmedia_port *media_port; |
| 848 |
|
| 849 |
if (status != PJ_SUCCESS) { |
| 850 |
|
| 851 |
app_perror(THIS_FILE, "SDP negotiation has failed", status); |
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
return; |
| 857 |
} |
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
|
| 862 |
status = pjmedia_sdp_neg_get_active_local(inv->neg, &local_sdp); |
| 863 |
|
| 864 |
status = pjmedia_sdp_neg_get_active_remote(inv->neg, &remote_sdp); |
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
status = pjmedia_stream_info_from_sdp(&stream_info, inv->dlg->pool, |
| 869 |
g_med_endpt, |
| 870 |
local_sdp, remote_sdp, 0); |
| 871 |
if (status != PJ_SUCCESS) { |
| 872 |
app_perror(THIS_FILE,"Unable to create audio stream info",status); |
| 873 |
return; |
| 874 |
} |
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
status = pjmedia_stream_create(g_med_endpt, inv->dlg->pool, &stream_info, |
| 885 |
g_med_transport[0], NULL, &g_med_stream); |
| 886 |
if (status != PJ_SUCCESS) { |
| 887 |
app_perror( THIS_FILE, "Unable to create audio stream", status); |
| 888 |
return; |
| 889 |
} |
| 890 |
|
| 891 |
|
| 892 |
status = pjmedia_stream_start(g_med_stream); |
| 893 |
if (status != PJ_SUCCESS) { |
| 894 |
app_perror( THIS_FILE, "Unable to start audio stream", status); |
| 895 |
return; |
| 896 |
} |
| 897 |
|
| 898 |
|
| 899 |
status = pjmedia_transport_media_start(g_med_transport[0], 0, 0, 0, 0); |
| 900 |
if (status != PJ_SUCCESS) { |
| 901 |
app_perror( THIS_FILE, "Unable to start UDP media transport", status); |
| 902 |
return; |
| 903 |
} |
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
status = pjmedia_stream_get_port(g_med_stream, &media_port); |
| 912 |
if (status != PJ_SUCCESS) { |
| 913 |
app_perror( THIS_FILE, "Unable to create media port interface of the audio stream", status); |
| 914 |
return; |
| 915 |
} |
| 916 |
|
| 917 |
|
| 918 |
status = pjmedia_snd_port_create(inv->pool, |
| 919 |
PJMEDIA_AUD_DEFAULT_CAPTURE_DEV, |
| 920 |
PJMEDIA_AUD_DEFAULT_PLAYBACK_DEV, |
| 921 |
PJMEDIA_PIA_SRATE(&media_port->info), |
| 922 |
PJMEDIA_PIA_CCNT(&media_port->info), |
| 923 |
PJMEDIA_PIA_SPF(&media_port->info), |
| 924 |
PJMEDIA_PIA_BITS(&media_port->info), |
| 925 |
0, |
| 926 |
&g_snd_port); |
| 927 |
|
| 928 |
if (status != PJ_SUCCESS) { |
| 929 |
app_perror( THIS_FILE, "Unable to create sound port", status); |
| 930 |
PJ_LOG(3,(THIS_FILE, "%d %d %d %d", |
| 931 |
PJMEDIA_PIA_SRATE(&media_port->info), |
| 932 |
PJMEDIA_PIA_CCNT(&media_port->info), |
| 933 |
PJMEDIA_PIA_SPF(&media_port->info), |
| 934 |
PJMEDIA_PIA_BITS(&media_port->info) |
| 935 |
)); |
| 936 |
return; |
| 937 |
} |
| 938 |
|
| 939 |
status = pjmedia_snd_port_connect(g_snd_port, media_port); |
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
#if defined(PJMEDIA_HAS_VIDEO) && (PJMEDIA_HAS_VIDEO != 0) |
| 947 |
if (local_sdp->media_count > 1) { |
| 948 |
pjmedia_vid_stream_info vstream_info; |
| 949 |
pjmedia_vid_port_param vport_param; |
| 950 |
|
| 951 |
pjmedia_vid_port_param_default(&vport_param); |
| 952 |
|
| 953 |
|
| 954 |
status = pjmedia_vid_stream_info_from_sdp(&vstream_info, |
| 955 |
inv->dlg->pool, g_med_endpt, |
| 956 |
local_sdp, remote_sdp, 1); |
| 957 |
if (status != PJ_SUCCESS) { |
| 958 |
app_perror(THIS_FILE,"Unable to create video stream info",status); |
| 959 |
return; |
| 960 |
} |
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
status = pjmedia_vid_stream_create(g_med_endpt, NULL, &vstream_info, |
| 971 |
g_med_transport[1], NULL, |
| 972 |
&g_med_vstream); |
| 973 |
if (status != PJ_SUCCESS) { |
| 974 |
app_perror( THIS_FILE, "Unable to create video stream", status); |
| 975 |
return; |
| 976 |
} |
| 977 |
|
| 978 |
|
| 979 |
status = pjmedia_vid_stream_start(g_med_vstream); |
| 980 |
if (status != PJ_SUCCESS) { |
| 981 |
app_perror( THIS_FILE, "Unable to start video stream", status); |
| 982 |
return; |
| 983 |
} |
| 984 |
|
| 985 |
|
| 986 |
pjmedia_transport_media_start(g_med_transport[1], 0, 0, 0, 0); |
| 987 |
|
| 988 |
if (vstream_info.dir & PJMEDIA_DIR_DECODING) { |
| 989 |
status = pjmedia_vid_dev_default_param( |
| 990 |
inv->pool, PJMEDIA_VID_DEFAULT_RENDER_DEV, |
| 991 |
&vport_param.vidparam); |
| 992 |
if (status != PJ_SUCCESS) { |
| 993 |
app_perror(THIS_FILE, "Unable to get default param of video " |
| 994 |
"renderer device", status); |
| 995 |
return; |
| 996 |
} |
| 997 |
|
| 998 |
|
| 999 |
pjmedia_vid_stream_get_port(g_med_vstream, PJMEDIA_DIR_DECODING, |
| 1000 |
&media_port); |
| 1001 |
|
| 1002 |
|
| 1003 |
pjmedia_format_copy(&vport_param.vidparam.fmt, |
| 1004 |
&media_port->info.fmt); |
| 1005 |
vport_param.vidparam.dir = PJMEDIA_DIR_RENDER; |
| 1006 |
vport_param.active = PJ_TRUE; |
| 1007 |
|
| 1008 |
|
| 1009 |
status = pjmedia_vid_port_create(inv->pool, &vport_param, |
| 1010 |
&g_vid_renderer); |
| 1011 |
if (status != PJ_SUCCESS) { |
| 1012 |
app_perror(THIS_FILE, "Unable to create video renderer device", |
| 1013 |
status); |
| 1014 |
return; |
| 1015 |
} |
| 1016 |
|
| 1017 |
|
| 1018 |
status = pjmedia_vid_port_connect(g_vid_renderer, media_port, |
| 1019 |
PJ_FALSE); |
| 1020 |
if (status != PJ_SUCCESS) { |
| 1021 |
app_perror(THIS_FILE, "Unable to connect renderer to stream", |
| 1022 |
status); |
| 1023 |
return; |
| 1024 |
} |
| 1025 |
} |
| 1026 |
|
| 1027 |
|
| 1028 |
if (vstream_info.dir & PJMEDIA_DIR_ENCODING) { |
| 1029 |
status = pjmedia_vid_dev_default_param( |
| 1030 |
inv->pool, PJMEDIA_VID_DEFAULT_CAPTURE_DEV, |
| 1031 |
&vport_param.vidparam); |
| 1032 |
if (status != PJ_SUCCESS) { |
| 1033 |
app_perror(THIS_FILE, "Unable to get default param of video " |
| 1034 |
"capture device", status); |
| 1035 |
return; |
| 1036 |
} |
| 1037 |
|
| 1038 |
|
| 1039 |
pjmedia_vid_stream_get_port(g_med_vstream, PJMEDIA_DIR_ENCODING, |
| 1040 |
&media_port); |
| 1041 |
|
| 1042 |
|
| 1043 |
pjmedia_format_copy(&vport_param.vidparam.fmt, |
| 1044 |
&media_port->info.fmt); |
| 1045 |
vport_param.vidparam.dir = PJMEDIA_DIR_CAPTURE; |
| 1046 |
vport_param.active = PJ_TRUE; |
| 1047 |
|
| 1048 |
|
| 1049 |
status = pjmedia_vid_port_create(inv->pool, &vport_param, |
| 1050 |
&g_vid_capturer); |
| 1051 |
if (status != PJ_SUCCESS) { |
| 1052 |
app_perror(THIS_FILE, "Unable to create video capture device", |
| 1053 |
status); |
| 1054 |
return; |
| 1055 |
} |
| 1056 |
|
| 1057 |
|
| 1058 |
status = pjmedia_vid_port_connect(g_vid_capturer, media_port, |
| 1059 |
PJ_FALSE); |
| 1060 |
if (status != PJ_SUCCESS) { |
| 1061 |
app_perror(THIS_FILE, "Unable to connect capturer to stream", |
| 1062 |
status); |
| 1063 |
return; |
| 1064 |
} |
| 1065 |
} |
| 1066 |
|
| 1067 |
|
| 1068 |
if (g_vid_renderer) { |
| 1069 |
status = pjmedia_vid_port_start(g_vid_renderer); |
| 1070 |
if (status != PJ_SUCCESS) { |
| 1071 |
app_perror(THIS_FILE, "Unable to start video renderer", |
| 1072 |
status); |
| 1073 |
return; |
| 1074 |
} |
| 1075 |
} |
| 1076 |
if (g_vid_capturer) { |
| 1077 |
status = pjmedia_vid_port_start(g_vid_capturer); |
| 1078 |
if (status != PJ_SUCCESS) { |
| 1079 |
app_perror(THIS_FILE, "Unable to start video capturer", |
| 1080 |
status); |
| 1081 |
return; |
| 1082 |
} |
| 1083 |
} |
| 1084 |
} |
| 1085 |
#endif |
| 1086 |
|
| 1087 |
|
| 1088 |
} |
| 1089 |
|