Ignore:
Timestamp:
Nov 29, 2013 5:56:02 AM (10 years ago)
Author:
ming
Message:

Re #1519: Add Call API in pjsua2.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/branches/projects/pjsua2/pjsip-apps/src/samples/pjsua2_demo.cpp

    r4647 r4663  
    2424using namespace pj; 
    2525 
     26class MyAccount; 
     27 
     28class MyCall : public Call 
     29{ 
     30private: 
     31    MyAccount *myAcc; 
     32 
     33public: 
     34    MyCall(Account &acc, int call_id = PJSUA_INVALID_ID) 
     35    : Call(acc, call_id) 
     36    { 
     37        myAcc = (MyAccount *)&acc; 
     38    } 
     39     
     40    virtual void onCallState(OnCallStateParam &prm); 
     41}; 
     42 
    2643class MyAccount : public Account 
    2744{ 
     45public: 
     46    std::vector<Call *> calls; 
     47     
    2848public: 
    2949    MyAccount() 
    3050    {} 
    3151 
     52    ~MyAccount() 
     53    { 
     54        std::cout << "*** Account is being deleted: No of calls=" 
     55                  << calls.size() << std::endl; 
     56    } 
     57     
     58    void removeCall(Call *call) 
     59    { 
     60        for (std::vector<Call *>::iterator it = calls.begin(); 
     61             it != calls.end(); ++it) 
     62        { 
     63            if (*it == call) { 
     64                calls.erase(it); 
     65                break; 
     66            } 
     67        } 
     68    } 
     69 
    3270    virtual void onRegState(OnRegStateParam &prm) 
    3371    { 
     
    3674                  << prm.code << std::endl; 
    3775    } 
     76     
     77    virtual void onIncomingCall(OnIncomingCallParam &iprm) 
     78    { 
     79        Call *call = new MyCall(*this, iprm.callId); 
     80        CallInfo ci = call->getInfo(); 
     81        CallOpParam prm; 
     82         
     83        std::cout << "*** Incoming Call: " <<  ci.remoteURI << " [" 
     84                  << ci.stateText << "]" << std::endl; 
     85         
     86        calls.push_back(call); 
     87        prm.statusCode = (pjsip_status_code)200; 
     88        call->answer(prm); 
     89    } 
    3890}; 
     91 
     92void MyCall::onCallState(OnCallStateParam &prm) 
     93{ 
     94    CallInfo ci = getInfo(); 
     95    std::cout << "*** Call: " <<  ci.remoteURI << " [" << ci.stateText 
     96              << "]" << std::endl; 
     97     
     98    if (ci.state == PJSIP_INV_STATE_DISCONNECTED) { 
     99        myAcc->removeCall(this); 
     100        /* Delete the call */ 
     101        delete this; 
     102    } 
     103} 
    39104 
    40105static void mainProg1() throw(Error) 
     
    65130    acc_cfg.sipConfig.authCreds.push_back( AuthCredInfo("digest", "*", 
    66131                                                        "test1", 0, "test1") ); 
    67     std::auto_ptr<Account> acc(new MyAccount); 
     132    std::auto_ptr<MyAccount> acc(new MyAccount); 
    68133    acc->create(acc_cfg); 
    69  
     134     
    70135    pj_thread_sleep(2000); 
    71  
     136     
     137    // Make outgoing call 
     138    Call *call = new MyCall(*acc); 
     139    acc->calls.push_back(call); 
     140    CallOpParam prm(true); 
     141    prm.opt.audioCount = 1; 
     142    prm.opt.videoCount = 0; 
     143    call->makeCall("sip:test1@pjsip.org", prm); 
     144     
     145    // Hangup all calls 
     146    pj_thread_sleep(8000); 
     147    ep.hangupAllCalls(); 
     148    pj_thread_sleep(4000); 
     149     
    72150    // Destroy library 
    73151    std::cout << "*** PJSUA2 SHUTTING DOWN ***" << std::endl; 
     
    190268 
    191269    try { 
    192         mainProg(); 
     270        mainProg1(); 
    193271        std::cout << "Success" << std::endl; 
    194272    } catch (Error & err) { 
Note: See TracChangeset for help on using the changeset viewer.