Ignore:
Timestamp:
Oct 25, 2013 10:34:38 AM (10 years ago)
Author:
bennylp
Message:

Re #1519: Implementation of Account API, with inheritance approach:

  • With small demo app (samples/pjsua2_demo.cpp)
  • Endpoint changed to use inheritance approach too
  • Simple account registration demo and callback works
  • Further tests will be done in high level app (Python GUI?)
  • Temporary build setting fixes (Makefile) to allow linking with pjsua2 and libstdc++
  • Temporary hacks in Makefile to ignore other build targets to speed up build. This should be fixed during integration.

Issues:

  • incomplete Endpoint::on_incoming_subscribe() implementation. There is no Account::presNotify() yet.
  • incomplete Endpoint::on_pager2(), on_pager_status2(), to handle call's pager rather than account's pager
  • some SWIGTYPE (unknown type by Swig) still unresolved
File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/branches/projects/pjsua2/pjsip/src/pjsua2/endpoint.cpp

    r4631 r4638  
    1818 */ 
    1919#include <pjsua2/endpoint.hpp> 
     20#include <pjsua2/account.hpp> 
    2021#include "util.hpp" 
    2122 
     
    3435}; 
    3536 
     37Endpoint *Endpoint::instance_; 
    3638 
    3739/////////////////////////////////////////////////////////////////////////////// 
     
    210212 */ 
    211213Endpoint::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 
     223Endpoint& Endpoint::instance() throw(Error) 
     224{ 
     225    if (!instance_) { 
     226        PJSUA2_RAISE_ERROR(PJ_ENOTFOUND); 
     227    } 
     228    return *instance_; 
     229} 
     230 
     231Endpoint::~Endpoint() 
     232{ 
     233    try { 
     234        libDestroy(); 
     235    } catch (Error &err) { 
     236        // Ignore 
     237    } 
     238    delete writer; 
     239    instance_ = NULL; 
     240} 
    227241 
    228242/////////////////////////////////////////////////////////////////////////////// 
     
    250264    Endpoint &ep = Endpoint::instance(); 
    251265 
    252     if (!ep.epCallback || !res) 
     266    if (!res) 
    253267        return; 
    254268 
     
    265279    } 
    266280 
    267     ep.epCallback->onNatCheckStunServersComplete(prm); 
     281    ep.onNatCheckStunServersComplete(prm); 
    268282} 
    269283 
     
    274288    UserTimer *ut = (UserTimer*) entry->user_data; 
    275289 
    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); 
    280294} 
    281295 
     
    284298    Endpoint &ep = Endpoint::instance(); 
    285299 
    286     if (!ep.epCallback || !res) 
     300    if (!res) 
    287301        return; 
    288302 
     
    294308    prm.natTypeName = res->nat_type_name; 
    295309 
    296     ep.epCallback->onNatDetectionComplete(prm); 
     310    ep.onNatDetectionComplete(prm); 
    297311} 
    298312 
     
    303317    Endpoint &ep = Endpoint::instance(); 
    304318 
    305     if (!ep.epCallback) 
    306         return; 
    307  
    308319    OnTransportStateParam prm; 
    309320 
     
    312323    prm.lastError = info ? info->status : PJ_SUCCESS; 
    313324 
    314     ep.epCallback->onTransportState(prm); 
    315 } 
     325    ep.onTransportState(prm); 
     326} 
     327 
     328/////////////////////////////////////////////////////////////////////////////// 
     329/* 
     330 * Account static callbacks 
     331 */ 
     332 
     333Account *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 
     345void 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 
     372void 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 
     384void 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 
     402void 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 
     432void 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 
     464void 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 
     497void 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 
     526void 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 
    316542 
    317543/////////////////////////////////////////////////////////////////////////////// 
     
    321547void Endpoint::libCreate() throw(Error) 
    322548{ 
    323     pj_status_t status; 
    324  
    325     status = pjsua_create(); 
    326     PJSUA2_CHECK_RAISE_ERROR(status); 
     549    PJSUA2_CHECK_EXPR( pjsua_create() ); 
    327550} 
    328551 
     
    332555} 
    333556 
    334 void Endpoint::libInit( const EpConfig &prmEpConfig, 
    335                         EpCallback *prmCb) throw(Error) 
     557void Endpoint::libInit(const EpConfig &prmEpConfig) throw(Error) 
    336558{ 
    337559    pjsua_config ua_cfg; 
    338560    pjsua_logging_config log_cfg; 
    339561    pjsua_media_config med_cfg; 
    340     pj_status_t status; 
    341562 
    342563    ua_cfg = prmEpConfig.uaConfig.toPj(); 
     
    352573    /* Setup UA callbacks */ 
    353574    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; 
    355576    ua_cfg.cb.on_transport_state = &Endpoint::on_transport_state; 
    356577 
     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 
    357587    /* 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) ); 
    360589} 
    361590 
    362591void Endpoint::libStart() throw(Error) 
    363592{ 
     593    PJSUA2_CHECK_EXPR(pjsua_start()); 
     594} 
     595 
     596void Endpoint::libDestroy(unsigned flags) throw(Error) 
     597{ 
    364598    pj_status_t status; 
    365599 
    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  
    374600    status = pjsua_destroy2(flags); 
    375601 
     602    delete this->writer; 
    376603    this->writer = NULL; 
    377     this->epCallback = NULL; 
    378604 
    379605    if (pj_log_get_log_func() == &Endpoint::logFunc) { 
     
    469695    pj_ssl_cipher ciphers[64]; 
    470696    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) ); 
    475699 
    476700    return IntVector(ciphers, ciphers + count); 
     
    486710void Endpoint::natDetectType(void) throw(Error) 
    487711{ 
    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() ); 
    492713} 
    493714 
     
    495716{ 
    496717    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) ); 
    501720 
    502721    return type; 
     
    509728    pj_str_t srv[MAX_STUN_SERVERS]; 
    510729    unsigned i, count = 0; 
    511     pj_status_t status; 
    512730 
    513731    for (i=0; i<servers.size() && i<MAX_STUN_SERVERS; ++i) { 
     
    517735    } 
    518736 
    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) ); 
    522739} 
    523740 
     
    525742                                         bool notify_cb) throw(Error) 
    526743{ 
    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) ); 
    531745} 
    532746 
     
    540754    pjsua_transport_config tcfg; 
    541755    pjsua_transport_id tid; 
    542     pj_status_t status; 
    543756 
    544757    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) ); 
    547759 
    548760    return tid; 
     
    553765    pjsua_transport_id tids[32]; 
    554766    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) ); 
    559769 
    560770    return IntVector(tids, tids+count); 
     
    564774{ 
    565775    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) ); 
    570778 
    571779    return TransportInfo(tinfo); 
     
    574782void Endpoint::transportSetEnable(TransportId id, bool enabled) throw(Error) 
    575783{ 
    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) ); 
    580785} 
    581786 
    582787void Endpoint::transportClose(TransportId id) throw(Error) 
    583788{ 
    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.