Ignore:
Timestamp:
Feb 27, 2014 2:03:59 AM (10 years ago)
Author:
bennylp
Message:

Re #1715: updated book with latest Rst

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/doc/pjsip-book/intro_pjsua2.rst

    r4762 r4763  
    55PJSUA2 is implemented on top of PJSUA-LIB API. The SIP and media features and object modelling follows what PJSUA-LIB provides (for example, we still have accounts, call, buddy, and so on), but the API to access them is different. These features will be described later in this chapter. PJSUA2 is a C++ library, which you can find under ``pjsip`` directory in the PJSIP distribution. The C++ library can be used by native C++ applications directly. But PJSUA2 is not just a C++ library. From the beginning, it has been designed to be accessible from high level non-native languages such as Java and Python. This is achieved by SWIG binding. And thanks to SWIG, binding to other languages can be added relatively easily in the future. 
    66 
    7  
    8 Building PJSUA2 
    9 ====================== 
    10 The PJSUA2 C++ library will be built by default by PJSIP build system. 
    11  
    12 The SWIG modules for Python and Java are built by invoking ``make`` manually from ``pjsip-apps/src/swig`` directory. 
     7PJSUA2 API declaration can be found in ``pjsip/include/pjsua2`` while the source codes are located in ``pjsip/src/pjsua2``. It will be automatically built when you compile PJSIP. 
    138 
    149 
     
    10095 
    10196 
     97Building PJSUA2 
     98====================== 
     99The PJSUA2 C++ library will be built by default by PJSIP build system. 
     100 
     101Building Python and Java SWIG Modules 
     102====================================== 
     103The SWIG modules for Python and Java are built by invoking ``make`` and ``make install`` manually from ``pjsip-apps/src/swig`` directory. The ``make install`` will install the Python SWIG module to user's ``site-packages`` directory. 
     104 
     105Requirements 
     106------------ 
     107 
     108#. ``JDK``. 
     109#. ``Python``, version 2.7 or above is required. 
     110   For **Linux/UNIX**, you will also need ``Python developent package`` (called ``python-devel`` (e.g. on Fedora) or ``python2.7-dev`` (e.g. on Ubuntu)). For **Windows**, you will need MinGW and ``Python SDK`` such as `ActivePython-2.7.5`_ from `ActiveState`_. 
     111 
     112.. _`ActivePython-2.7.5`: http://www.activestate.com/activepython/downloads 
     113.. _`ActiveState`: http://www.activestate.com 
     114 
     115Testing The Installation 
     116------------------------ 
     117To test the installation, simply run python and import ``pjsua2`` module:: 
     118 
     119  $ python 
     120  > import pjsua2 
     121  > ^Z 
     122 
     123 
     124Using in C++ Application 
     125======================== 
     126As mentioned in previous chapter, a C++ application can use *pjsua2* natively, while at the same time still has access to the lower level objects and the ability to extend the libraries if it needs to. Using the API will be exactly the same as the API reference that is written in this book. 
     127 
     128Here is a sample complete C++ application to give you some idea about the API. The snippet below initializes the library and creates an account that registers to our pjsip.org SIP server. 
     129 
     130.. code-block:: c++ 
     131     
     132  #include <pjsua2.hpp> 
     133  #include <iostream> 
     134   
     135  using namespace pj; 
     136   
     137  // Subclass to extend the Account and get notifications etc. 
     138  class MyAccount : public Account { 
     139  public: 
     140      virtual void onRegState(OnRegStateParam &prm) { 
     141          AccountInfo ai = getInfo(); 
     142          std::cout << (ai.regIsActive? "*** Register:" : "*** Unregister:") 
     143                    << " code=" << prm.code << std::endl; 
     144      } 
     145  }; 
     146 
     147  int main() 
     148  { 
     149      Endpoint ep; 
     150       
     151      ep.libCreate(); 
     152       
     153      // Initialize endpoint 
     154      EpConfig ep_cfg; 
     155      ep.libInit( ep_cfg ); 
     156       
     157      // Create SIP transport. Error handling sample is shown 
     158      TransportConfig tcfg; 
     159      tcfg.port = 5060; 
     160      try { 
     161          ep.transportCreate(PJSIP_TRANSPORT_UDP, tcfg); 
     162      } catch (Error &err) { 
     163          std::cout << err.info() << std::endl; 
     164          return 1; 
     165      } 
     166       
     167      // Start the library (worker threads etc) 
     168      ep.libStart(); 
     169      std::cout << "*** PJSUA2 STARTED ***" << std::endl; 
     170       
     171      // Configure an AccountConfig 
     172      AccountConfig acfg; 
     173      acfg.idUri = "sip:test@pjsip.org"; 
     174      acfg.regConfig.registrarUri = "sip:pjsip.org"; 
     175      AuthCredInfo cred("digest", "*", "test", 0, "secret"); 
     176      acfg.sipConfig.authCreds.push_back( cred ); 
     177       
     178      // Create the account 
     179      MyAccount *acc = new MyAccount; 
     180      acc->create(acfg); 
     181       
     182      // Here we don't have anything else to do.. 
     183      pj_thread_sleep(10000); 
     184       
     185      // Delete the account. This will unregister from server 
     186      delete acc; 
     187       
     188      // This will implicitly shutdown the library 
     189      return 0; 
     190  } 
     191 
     192 
     193Using in Python Application 
     194=========================== 
     195The equivalence of the C++ sample code above in Python is as follows: 
     196 
     197.. code-block:: python 
     198 
     199  # Subclass to extend the Account and get notifications etc. 
     200  class Account(pj.Account): 
     201    def onRegState(self, prm): 
     202        print "***OnRegState: " + prm.reason 
     203 
     204  # pjsua2 test function 
     205  def pjsua2_test(): 
     206    # Create and initialize the library 
     207    ep_cfg = pj.EpConfig() 
     208    ep = pj.Endpoint() 
     209    ep.libCreate() 
     210    ep.libInit(ep_cfg) 
     211     
     212    # Create SIP transport. Error handling sample is shown 
     213    sipTpConfig = pj.TransportConfig(); 
     214    sipTpConfig.port = 5060; 
     215    ep.transportCreate(pj.PJSIP_TRANSPORT_UDP, sipTpConfig); 
     216    # Start the library 
     217    ep.libStart(); 
     218     
     219    acfg = pj.AccountConfig(); 
     220    acfg.idUri = "sip:test@pjsip.org"; 
     221    acfg.regConfig.registrarUri = "sip:pjsip.org"; 
     222    cred = pj.AuthCredInfo("digest", "*", "test", 0, "pwtest"); 
     223    acfg.sipConfig.authCreds.append( cred ); 
     224    # Create the account 
     225    acc = Account(); 
     226    acc.create(acfg); 
     227    # Here we don't have anything else to do.. 
     228    time.sleep(10); 
     229 
     230    # Destroy the library 
     231    ep.libDestroy() 
     232 
     233  # 
     234  # main() 
     235  # 
     236  if __name__ == "__main__": 
     237    pjsua2_test() 
     238 
     239 
     240Using in Java Application 
     241========================= 
     242The equivalence of the C++ sample code above in Java is as follows: 
     243 
     244.. code-block:: java 
     245 
     246  import org.pjsip.pjsua2.*; 
     247 
     248  // Subclass to extend the Account and get notifications etc. 
     249  class MyAccount extends Account { 
     250    @Override 
     251    public void onRegState(OnRegStateParam prm) { 
     252        System.out.println("*** On registration state: " + prm.getCode() + prm.getReason()); 
     253    } 
     254  } 
     255 
     256  public class test { 
     257    static { 
     258        System.loadLibrary("pjsua2"); 
     259        System.out.println("Library loaded"); 
     260    } 
     261     
     262    public static void main(String argv[]) { 
     263        try { 
     264            // Create endpoint 
     265            Endpoint ep = new Endpoint(); 
     266            ep.libCreate(); 
     267            // Initialize endpoint 
     268            EpConfig epConfig = new EpConfig(); 
     269            ep.libInit( epConfig ); 
     270            // Create SIP transport. Error handling sample is shown 
     271            TransportConfig sipTpConfig = new TransportConfig(); 
     272            sipTpConfig.setPort(5060); 
     273            ep.transportCreate(pjsip_transport_type_e.PJSIP_TRANSPORT_UDP, sipTpConfig); 
     274            // Start the library 
     275            ep.libStart(); 
     276 
     277            AccountConfig acfg = new AccountConfig(); 
     278            acfg.setIdUri("sip:test@pjsip.org"); 
     279            acfg.getRegConfig().setRegistrarUri("sip:pjsip.org"); 
     280            AuthCredInfo cred = new AuthCredInfo("digest", "*", "test", 0, "secret"); 
     281            acfg.getSipConfig().getAuthCreds().add( cred ); 
     282            // Create the account 
     283            MyAccount acc = new MyAccount(); 
     284            acc.create(acfg); 
     285            // Here we don't have anything else to do.. 
     286            Thread.sleep(10000); 
     287            /* Explicitly delete the account. 
     288             * This is to avoid GC to delete the endpoint first before deleting 
     289             * the account. 
     290             */ 
     291            acc.delete(); 
     292             
     293            // Explicitly destroy and delete endpoint 
     294            ep.libDestroy(); 
     295            ep.delete(); 
     296             
     297        } catch (Exception e) { 
     298            System.out.println(e); 
     299            return; 
     300        } 
     301    } 
     302  } 
Note: See TracChangeset for help on using the changeset viewer.