- Timestamp:
- Oct 25, 2013 10:34:38 AM (10 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/branches/projects/pjsua2/pjsip/src/pjsua2/endpoint.cpp
r4631 r4638 18 18 */ 19 19 #include <pjsua2/endpoint.hpp> 20 #include <pjsua2/account.hpp> 20 21 #include "util.hpp" 21 22 … … 34 35 }; 35 36 37 Endpoint *Endpoint::instance_; 36 38 37 39 /////////////////////////////////////////////////////////////////////////////// … … 210 212 */ 211 213 Endpoint::Endpoint() 212 : writer(NULL), epCallback(NULL) 213 { 214 } 215 216 Endpoint& Endpoint::instance() 217 { 218 static Endpoint lib_; 219 return lib_; 220 } 221 222 void Endpoint::testException() throw(Error) 223 { 224 PJSUA2_CHECK_RAISE_ERROR(PJ_EINVALIDOP); 225 } 226 214 : writer(NULL) 215 { 216 if (instance_) { 217 PJSUA2_RAISE_ERROR(PJ_EEXISTS); 218 } 219 220 instance_ = this; 221 } 222 223 Endpoint& Endpoint::instance() throw(Error) 224 { 225 if (!instance_) { 226 PJSUA2_RAISE_ERROR(PJ_ENOTFOUND); 227 } 228 return *instance_; 229 } 230 231 Endpoint::~Endpoint() 232 { 233 try { 234 libDestroy(); 235 } catch (Error &err) { 236 // Ignore 237 } 238 delete writer; 239 instance_ = NULL; 240 } 227 241 228 242 /////////////////////////////////////////////////////////////////////////////// … … 250 264 Endpoint &ep = Endpoint::instance(); 251 265 252 if (! ep.epCallback || !res)266 if (!res) 253 267 return; 254 268 … … 265 279 } 266 280 267 ep. epCallback->onNatCheckStunServersComplete(prm);281 ep.onNatCheckStunServersComplete(prm); 268 282 } 269 283 … … 274 288 UserTimer *ut = (UserTimer*) entry->user_data; 275 289 276 if ( !ep.epCallback ||ut->signature != TIMER_SIGNATURE)277 return; 278 279 ep. epCallback->onTimer(ut->prm);290 if (ut->signature != TIMER_SIGNATURE) 291 return; 292 293 ep.onTimer(ut->prm); 280 294 } 281 295 … … 284 298 Endpoint &ep = Endpoint::instance(); 285 299 286 if (! ep.epCallback || !res)300 if (!res) 287 301 return; 288 302 … … 294 308 prm.natTypeName = res->nat_type_name; 295 309 296 ep. epCallback->onNatDetectionComplete(prm);310 ep.onNatDetectionComplete(prm); 297 311 } 298 312 … … 303 317 Endpoint &ep = Endpoint::instance(); 304 318 305 if (!ep.epCallback)306 return;307 308 319 OnTransportStateParam prm; 309 320 … … 312 323 prm.lastError = info ? info->status : PJ_SUCCESS; 313 324 314 ep.epCallback->onTransportState(prm); 315 } 325 ep.onTransportState(prm); 326 } 327 328 /////////////////////////////////////////////////////////////////////////////// 329 /* 330 * Account static callbacks 331 */ 332 333 Account *Endpoint::lookupAcc(int acc_id, const char *op) 334 { 335 Account *acc = Account::lookup(acc_id); 336 if (!acc) { 337 PJ_LOG(1,(THIS_FILE, 338 "Error: cannot find Account instance for account id %d in " 339 "%s", acc_id, op)); 340 } 341 342 return acc; 343 } 344 345 void Endpoint::on_incoming_call(pjsua_acc_id acc_id, pjsua_call_id call_id, 346 pjsip_rx_data *rdata) 347 { 348 Account *acc = lookupAcc(acc_id, "on_incoming_call()"); 349 if (!acc) { 350 pjsua_call_hangup(call_id, PJSIP_SC_INTERNAL_SERVER_ERROR, NULL, NULL); 351 return; 352 } 353 354 /* call callback */ 355 OnIncomingCallParam prm; 356 prm.callId = call_id; 357 prm.rdata.fromPj(*rdata); 358 359 acc->onIncomingCall(prm); 360 361 /* disconnect if callback doesn't handle the call */ 362 pjsua_call_info ci; 363 364 pjsua_call_get_info(call_id, &ci); 365 if (!pjsua_call_get_user_data(call_id) && 366 ci.state != PJSIP_INV_STATE_DISCONNECTED) 367 { 368 pjsua_call_hangup(call_id, PJSIP_SC_INTERNAL_SERVER_ERROR, NULL, NULL); 369 } 370 } 371 372 void Endpoint::on_reg_started(pjsua_acc_id acc_id, pj_bool_t renew) 373 { 374 Account *acc = lookupAcc(acc_id, "on_reg_started()"); 375 if (!acc) { 376 return; 377 } 378 379 OnRegStartedParam prm; 380 prm.renew = renew; 381 acc->onRegStarted(prm); 382 } 383 384 void Endpoint::on_reg_state2(pjsua_acc_id acc_id, pjsua_reg_info *info) 385 { 386 Account *acc = lookupAcc(acc_id, "on_reg_state2()"); 387 if (!acc) { 388 return; 389 } 390 391 OnRegStateParam prm; 392 prm.status = info->cbparam->status; 393 prm.code = (pjsip_status_code) info->cbparam->code; 394 prm.reason = pj2Str(info->cbparam->reason); 395 if (info->cbparam->rdata) 396 prm.rdata.fromPj(*info->cbparam->rdata); 397 prm.expiration = info->cbparam->expiration; 398 399 acc->onRegState(prm); 400 } 401 402 void Endpoint::on_incoming_subscribe(pjsua_acc_id acc_id, 403 pjsua_srv_pres *srv_pres, 404 pjsua_buddy_id buddy_id, 405 const pj_str_t *from, 406 pjsip_rx_data *rdata, 407 pjsip_status_code *code, 408 pj_str_t *reason, 409 pjsua_msg_data *msg_data) 410 { 411 Account *acc = lookupAcc(acc_id, "on_incoming_subscribe()"); 412 if (!acc) { 413 /* default behavior should apply */ 414 return; 415 } 416 417 OnIncomingSubscribeParam prm; 418 prm.fromUri = pj2Str(*from); 419 prm.rdata.fromPj(*rdata); 420 prm.code = *code; 421 prm.reason = pj2Str(*reason); 422 423 acc->onIncomingSubscribe(prm); 424 425 *code = prm.code; 426 acc->tmpReason = prm.reason; 427 *reason = str2Pj(acc->tmpReason); 428 // TODO: 429 // apply msg_data 430 } 431 432 void Endpoint::on_pager2(pjsua_call_id call_id, 433 const pj_str_t *from, 434 const pj_str_t *to, 435 const pj_str_t *contact, 436 const pj_str_t *mime_type, 437 const pj_str_t *body, 438 pjsip_rx_data *rdata, 439 pjsua_acc_id acc_id) 440 { 441 OnInstantMessageParam prm; 442 prm.fromUri = pj2Str(*from); 443 prm.toUri = pj2Str(*to); 444 prm.contactUri = pj2Str(*contact); 445 prm.contentType = pj2Str(*mime_type); 446 prm.msgBody = pj2Str(*body); 447 prm.rdata.fromPj(*rdata); 448 449 if (call_id != PJSUA_INVALID_ID) { 450 // TODO: 451 // handle call pager 452 return; 453 } else { 454 Account *acc = lookupAcc(acc_id, "on_pager2()"); 455 if (!acc) { 456 /* Ignored */ 457 return; 458 } 459 460 acc->onInstantMessage(prm); 461 } 462 } 463 464 void Endpoint::on_pager_status2( pjsua_call_id call_id, 465 const pj_str_t *to, 466 const pj_str_t *body, 467 void *user_data, 468 pjsip_status_code status, 469 const pj_str_t *reason, 470 pjsip_tx_data *tdata, 471 pjsip_rx_data *rdata, 472 pjsua_acc_id acc_id) 473 { 474 OnInstantMessageStatusParam prm; 475 prm.userData = user_data; 476 prm.toUri = pj2Str(*to); 477 prm.msgBody = pj2Str(*body); 478 prm.status = status; 479 prm.reason = pj2Str(*reason); 480 if (rdata) 481 prm.rdata.fromPj(*rdata); 482 483 if (call_id != PJSUA_INVALID_ID) { 484 // TODO: 485 // handle call pager 486 } else { 487 Account *acc = lookupAcc(acc_id, "on_pager_status2()"); 488 if (!acc) { 489 /* Ignored */ 490 return; 491 } 492 493 acc->onInstantMessageStatus(prm); 494 } 495 } 496 497 void Endpoint::on_typing2( pjsua_call_id call_id, 498 const pj_str_t *from, 499 const pj_str_t *to, 500 const pj_str_t *contact, 501 pj_bool_t is_typing, 502 pjsip_rx_data *rdata, 503 pjsua_acc_id acc_id) 504 { 505 OnTypingIndicationParam prm; 506 prm.fromUri = pj2Str(*from); 507 prm.toUri = pj2Str(*to); 508 prm.contactUri = pj2Str(*contact); 509 prm.isTyping = is_typing != 0; 510 prm.rdata.fromPj(*rdata); 511 512 if (call_id != PJSUA_INVALID_ID) { 513 // TODO: 514 // handle call indication 515 } else { 516 Account *acc = lookupAcc(acc_id, "on_typing2()"); 517 if (!acc) { 518 /* Ignored */ 519 return; 520 } 521 522 acc->onTypingIndication(prm); 523 } 524 } 525 526 void Endpoint::on_mwi_info(pjsua_acc_id acc_id, 527 pjsua_mwi_info *mwi_info) 528 { 529 OnMwiInfoParam prm; 530 prm.state = pjsip_evsub_get_state(mwi_info->evsub); 531 prm.rdata.fromPj(*mwi_info->rdata); 532 533 Account *acc = lookupAcc(acc_id, "on_mwi_info()"); 534 if (!acc) { 535 /* Ignored */ 536 return; 537 } 538 539 acc->onMwiInfo(prm); 540 } 541 316 542 317 543 /////////////////////////////////////////////////////////////////////////////// … … 321 547 void Endpoint::libCreate() throw(Error) 322 548 { 323 pj_status_t status; 324 325 status = pjsua_create(); 326 PJSUA2_CHECK_RAISE_ERROR(status); 549 PJSUA2_CHECK_EXPR( pjsua_create() ); 327 550 } 328 551 … … 332 555 } 333 556 334 void Endpoint::libInit( const EpConfig &prmEpConfig, 335 EpCallback *prmCb) throw(Error) 557 void Endpoint::libInit(const EpConfig &prmEpConfig) throw(Error) 336 558 { 337 559 pjsua_config ua_cfg; 338 560 pjsua_logging_config log_cfg; 339 561 pjsua_media_config med_cfg; 340 pj_status_t status;341 562 342 563 ua_cfg = prmEpConfig.uaConfig.toPj(); … … 352 573 /* Setup UA callbacks */ 353 574 pj_bzero(&ua_cfg.cb, sizeof(ua_cfg.cb)); 354 ua_cfg.cb.on_nat_detect = &Endpoint::on_nat_detect;575 ua_cfg.cb.on_nat_detect = &Endpoint::on_nat_detect; 355 576 ua_cfg.cb.on_transport_state = &Endpoint::on_transport_state; 356 577 578 ua_cfg.cb.on_incoming_call = &Endpoint::on_incoming_call; 579 ua_cfg.cb.on_reg_started = &Endpoint::on_reg_started; 580 ua_cfg.cb.on_reg_state2 = &Endpoint::on_reg_state2; 581 ua_cfg.cb.on_incoming_subscribe = &Endpoint::on_incoming_subscribe; 582 ua_cfg.cb.on_pager2 = &Endpoint::on_pager2; 583 ua_cfg.cb.on_pager_status2 = &Endpoint::on_pager_status2; 584 ua_cfg.cb.on_typing2 = &Endpoint::on_typing2; 585 ua_cfg.cb.on_mwi_info = &Endpoint::on_mwi_info; 586 357 587 /* Init! */ 358 status = pjsua_init(&ua_cfg, &log_cfg, &med_cfg); 359 PJSUA2_CHECK_RAISE_ERROR(status); 588 PJSUA2_CHECK_EXPR( pjsua_init(&ua_cfg, &log_cfg, &med_cfg) ); 360 589 } 361 590 362 591 void Endpoint::libStart() throw(Error) 363 592 { 593 PJSUA2_CHECK_EXPR(pjsua_start()); 594 } 595 596 void Endpoint::libDestroy(unsigned flags) throw(Error) 597 { 364 598 pj_status_t status; 365 599 366 status = pjsua_start();367 PJSUA2_CHECK_RAISE_ERROR(status);368 }369 370 void Endpoint::libDestroy(unsigned flags) throw(Error)371 {372 pj_status_t status;373 374 600 status = pjsua_destroy2(flags); 375 601 602 delete this->writer; 376 603 this->writer = NULL; 377 this->epCallback = NULL;378 604 379 605 if (pj_log_get_log_func() == &Endpoint::logFunc) { … … 469 695 pj_ssl_cipher ciphers[64]; 470 696 unsigned count = PJ_ARRAY_SIZE(ciphers); 471 pj_status_t status; 472 473 status = pj_ssl_cipher_get_availables(ciphers, &count); 474 PJSUA2_CHECK_RAISE_ERROR(status); 697 698 PJSUA2_CHECK_EXPR( pj_ssl_cipher_get_availables(ciphers, &count) ); 475 699 476 700 return IntVector(ciphers, ciphers + count); … … 486 710 void Endpoint::natDetectType(void) throw(Error) 487 711 { 488 pj_status_t status; 489 490 status = pjsua_detect_nat_type(); 491 PJSUA2_CHECK_RAISE_ERROR(status); 712 PJSUA2_CHECK_EXPR( pjsua_detect_nat_type() ); 492 713 } 493 714 … … 495 716 { 496 717 pj_stun_nat_type type; 497 pj_status_t status; 498 499 status = pjsua_get_nat_type(&type); 500 PJSUA2_CHECK_RAISE_ERROR(status); 718 719 PJSUA2_CHECK_EXPR( pjsua_get_nat_type(&type) ); 501 720 502 721 return type; … … 509 728 pj_str_t srv[MAX_STUN_SERVERS]; 510 729 unsigned i, count = 0; 511 pj_status_t status;512 730 513 731 for (i=0; i<servers.size() && i<MAX_STUN_SERVERS; ++i) { … … 517 735 } 518 736 519 status = pjsua_resolve_stun_servers(count, srv, wait, token, 520 &Endpoint::stun_resolve_cb); 521 PJSUA2_CHECK_RAISE_ERROR(status); 737 PJSUA2_CHECK_EXPR(pjsua_resolve_stun_servers(count, srv, wait, token, 738 &Endpoint::stun_resolve_cb) ); 522 739 } 523 740 … … 525 742 bool notify_cb) throw(Error) 526 743 { 527 pj_status_t status; 528 529 status = pjsua_cancel_stun_resolution(token, notify_cb); 530 PJSUA2_CHECK_RAISE_ERROR(status); 744 PJSUA2_CHECK_EXPR( pjsua_cancel_stun_resolution(token, notify_cb) ); 531 745 } 532 746 … … 540 754 pjsua_transport_config tcfg; 541 755 pjsua_transport_id tid; 542 pj_status_t status;543 756 544 757 tcfg = cfg.toPj(); 545 status = pjsua_transport_create(type, &tcfg, &tid); 546 PJSUA2_CHECK_RAISE_ERROR(status); 758 PJSUA2_CHECK_EXPR( pjsua_transport_create(type, &tcfg, &tid) ); 547 759 548 760 return tid; … … 553 765 pjsua_transport_id tids[32]; 554 766 unsigned count = PJ_ARRAY_SIZE(tids); 555 pj_status_t status; 556 557 status = pjsua_enum_transports(tids, &count); 558 PJSUA2_CHECK_RAISE_ERROR(status); 767 768 PJSUA2_CHECK_EXPR( pjsua_enum_transports(tids, &count) ); 559 769 560 770 return IntVector(tids, tids+count); … … 564 774 { 565 775 pjsua_transport_info tinfo; 566 pj_status_t status; 567 568 status = pjsua_transport_get_info(id, &tinfo); 569 PJSUA2_CHECK_RAISE_ERROR(status); 776 777 PJSUA2_CHECK_EXPR( pjsua_transport_get_info(id, &tinfo) ); 570 778 571 779 return TransportInfo(tinfo); … … 574 782 void Endpoint::transportSetEnable(TransportId id, bool enabled) throw(Error) 575 783 { 576 pj_status_t status; 577 578 status = pjsua_transport_set_enable(id, enabled); 579 PJSUA2_CHECK_RAISE_ERROR(status); 784 PJSUA2_CHECK_EXPR( pjsua_transport_set_enable(id, enabled) ); 580 785 } 581 786 582 787 void Endpoint::transportClose(TransportId id) throw(Error) 583 788 { 584 pj_status_t status; 585 586 status = pjsua_transport_close(id, PJ_FALSE); 587 PJSUA2_CHECK_RAISE_ERROR(status); 588 } 589 590 /////////////////////////////////////////////////////////////////////////////// 591 789 PJSUA2_CHECK_EXPR( pjsua_transport_close(id, PJ_FALSE) ); 790 } 791 792 /////////////////////////////////////////////////////////////////////////////// 793
Note: See TracChangeset
for help on using the changeset viewer.