Ignore:
Timestamp:
Aug 2, 2017 9:45:09 AM (6 years ago)
Author:
riza
Message:

Close #2034: Add support to Python3 using PJSUA2 API.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjsip-apps/src/swig/python/test.py

    r4845 r5638  
    22import sys 
    33import time 
     4 
     5write=sys.stdout.write 
    46 
    57# 
     
    810# 
    911def ua_data_test(): 
    10         # 
    11         # AuthCredInfo 
    12         # 
    13         print "UA data types test.." 
    14         the_realm = "pjsip.org" 
    15         ci = pj.AuthCredInfo() 
    16         ci.realm = the_realm 
    17         ci.dataType = 20 
    18          
    19         ci2 = ci 
    20         assert ci.dataType == 20 
    21         assert ci2.realm == the_realm 
    22          
    23         # 
    24         # UaConfig 
    25         # See here how we manipulate std::vector 
    26         # 
    27         uc = pj.UaConfig() 
    28         uc.maxCalls = 10 
    29         uc.userAgent = "Python" 
    30         uc.nameserver = pj.StringVector(["10.0.0.1", "10.0.0.2"]) 
    31         uc.nameserver.append("NS1") 
    32          
    33         uc2 = uc 
    34         assert uc2.maxCalls == 10 
    35         assert uc2.userAgent == "Python" 
    36         assert len(uc2.nameserver) == 3 
    37         assert uc2.nameserver[0] == "10.0.0.1" 
    38         assert uc2.nameserver[1] == "10.0.0.2" 
    39         assert uc2.nameserver[2] == "NS1" 
     12    # 
     13    # AuthCredInfo 
     14    # 
     15    write("UA data types test..") 
     16    the_realm = "pjsip.org" 
     17    ci = pj.AuthCredInfo() 
     18    ci.realm = the_realm 
     19    ci.dataType = 20 
    4020 
    41         print "  Dumping nameservers: ", 
    42         for s in uc2.nameserver: 
    43                 print s, 
    44         print "" 
     21    ci2 = ci 
     22    assert ci.dataType == 20 
     23    assert ci2.realm == the_realm 
     24 
     25    # 
     26    # UaConfig 
     27    # See here how we manipulate std::vector 
     28    # 
     29    uc = pj.UaConfig() 
     30    uc.maxCalls = 10 
     31    uc.userAgent = "Python" 
     32    uc.nameserver = pj.StringVector(["10.0.0.1", "10.0.0.2"]) 
     33    uc.nameserver.append("NS1") 
     34 
     35    uc2 = uc 
     36    assert uc2.maxCalls == 10 
     37    assert uc2.userAgent == "Python" 
     38    assert len(uc2.nameserver) == 3 
     39    assert uc2.nameserver[0] == "10.0.0.1" 
     40    assert uc2.nameserver[1] == "10.0.0.2" 
     41    assert uc2.nameserver[2] == "NS1" 
     42 
     43    write("  Dumping nameservers: " + "\r\n") 
     44    for s in uc2.nameserver: 
     45        write(s  + "\r\n") 
     46    write("\r\n") 
    4547 
    4648# 
     
    4850# 
    4951def ua_run_test_exception(): 
    50         print "Exception test.." 
    51         ep = pj.Endpoint() 
    52         ep.libCreate() 
    53         got_exception = False 
    54         try: 
    55                 ep.natDetectType() 
    56         except pj.Error, e: 
    57                 got_exception = True 
    58                 print "  Got exception: status=%u, reason=%s,\n  title=%s,\n  srcFile=%s, srcLine=%d" % \ 
    59                         (e.status, e.reason, e.title, e.srcFile, e.srcLine) 
    60                 assert e.status == 370050 
    61                 assert e.reason.find("PJNATH_ESTUNINSERVER") >= 0 
    62                 assert e.title == "pjsua_detect_nat_type()" 
    63         assert got_exception 
     52    write("Exception test.." + "\r\n") 
     53    ep = pj.Endpoint() 
     54    ep.libCreate() 
     55    got_exception = False 
     56    try: 
     57        ep.natDetectType() 
     58    except pj.Error as e: 
     59        #t, e = sys.exc_info()[:2] 
     60        got_exception = True 
     61        write("  Got exception: status=%u, reason=%s,\n  title=%s,\n  srcFile=%s, srcLine=%d" % \ 
     62            (e.status, e.reason, e.title, e.srcFile, e.srcLine) + "\r\n") 
     63        assert e.status == 370050 
     64        assert e.reason.find("PJNATH_ESTUNINSERVER") >= 0 
     65        assert e.title == "pjsua_detect_nat_type()" 
     66    assert got_exception 
    6467 
    6568# 
     
    6770# 
    6871class MyLogWriter(pj.LogWriter): 
    69         def write(self, entry): 
    70                 print "This is Python:", entry.msg 
    71                  
     72    def write(self, entry): 
     73        write("This is Python:" + entry.msg + "\r\n") 
     74 
    7275# 
    7376# Testing log writer callback 
    7477# 
    7578def ua_run_log_test(): 
    76         print "Logging test.." 
    77         ep_cfg = pj.EpConfig() 
    78          
    79         lw = MyLogWriter() 
    80         ep_cfg.logConfig.writer = lw 
    81         ep_cfg.logConfig.decor = ep_cfg.logConfig.decor & ~(pj.PJ_LOG_HAS_CR | pj.PJ_LOG_HAS_NEWLINE)  
    82          
    83         ep = pj.Endpoint() 
    84         ep.libCreate() 
    85         ep.libInit(ep_cfg) 
    86         ep.libDestroy() 
    87          
     79    write("Logging test.." + "\r\n") 
     80    ep_cfg = pj.EpConfig() 
     81 
     82    lw = MyLogWriter() 
     83    ep_cfg.logConfig.writer = lw 
     84    ep_cfg.logConfig.decor = ep_cfg.logConfig.decor & ~(pj.PJ_LOG_HAS_CR | pj.PJ_LOG_HAS_NEWLINE) 
     85 
     86    ep = pj.Endpoint() 
     87    ep.libCreate() 
     88    ep.libInit(ep_cfg) 
     89    ep.libDestroy() 
     90 
    8891# 
    8992# Simple create, init, start, and destroy sequence 
    9093# 
    9194def ua_run_ua_test(): 
    92         print "UA test run.." 
    93         ep_cfg = pj.EpConfig() 
    94          
    95         ep = pj.Endpoint() 
    96         ep.libCreate() 
    97         ep.libInit(ep_cfg) 
    98         ep.libStart() 
    99          
    100         print "************* Endpoint started ok, now shutting down... *************" 
    101         ep.libDestroy() 
     95    write("UA test run.." + "\r\n") 
     96    ep_cfg = pj.EpConfig() 
     97 
     98    ep = pj.Endpoint() 
     99    ep.libCreate() 
     100    ep.libInit(ep_cfg) 
     101    ep.libStart() 
     102 
     103    write("************* Endpoint started ok, now shutting down... *************" + "\r\n") 
     104    ep.libDestroy() 
    102105 
    103106# 
     
    105108# 
    106109def ua_tonegen_test(): 
    107         print "UA tonegen test.." 
    108         ep_cfg = pj.EpConfig() 
     110    write("UA tonegen test.." + "\r\n") 
     111    ep_cfg = pj.EpConfig() 
    109112 
    110         ep = pj.Endpoint() 
    111         ep.libCreate() 
    112         ep.libInit(ep_cfg) 
    113         ep.libStart() 
    114          
    115         tonegen = pj.ToneGenerator() 
    116         tonegen.createToneGenerator() 
     113    ep = pj.Endpoint() 
     114    ep.libCreate() 
     115    ep.libInit(ep_cfg) 
     116    ep.libStart() 
    117117 
    118         tone = pj.ToneDesc() 
    119         tone.freq1 = 400 
    120         tone.freq2 = 600 
    121         tone.on_msec = 1000 
    122         tone.off_msec = 1000 
    123         tones = pj.ToneDescVector() 
    124         tones.append(tone) 
     118    tonegen = pj.ToneGenerator() 
     119    tonegen.createToneGenerator() 
    125120 
    126         digit = pj.ToneDigit() 
    127         digit.digit = '0' 
    128         digit.on_msec = 1000 
    129         digit.off_msec = 1000 
    130         digits = pj.ToneDigitVector() 
    131         digits.append(digit) 
     121    tone = pj.ToneDesc() 
     122    tone.freq1 = 400 
     123    tone.freq2 = 600 
     124    tone.on_msec = 1000 
     125    tone.off_msec = 1000 
     126    tones = pj.ToneDescVector() 
     127    tones.append(tone) 
    132128 
    133         adm = ep.audDevManager() 
    134         spk = adm.getPlaybackDevMedia() 
     129    digit = pj.ToneDigit() 
     130    digit.digit = '0' 
     131    digit.on_msec = 1000 
     132    digit.off_msec = 1000 
     133    digits = pj.ToneDigitVector() 
     134    digits.append(digit) 
    135135 
    136         tonegen.play(tones, True) 
    137         tonegen.startTransmit(spk) 
    138         time.sleep(5) 
     136    adm = ep.audDevManager() 
     137    spk = adm.getPlaybackDevMedia() 
    139138 
    140         tonegen.stop() 
    141         tonegen.playDigits(digits, True) 
    142         time.sleep(5) 
     139    tonegen.play(tones, True) 
     140    tonegen.startTransmit(spk) 
     141    time.sleep(5) 
    143142 
    144         dm = tonegen.getDigitMap() 
    145         print dm[0].digit 
    146         dm[0].freq1 = 400 
    147         dm[0].freq2 = 600 
    148         tonegen.setDigitMap(dm) 
    149          
    150         tonegen.stop() 
    151         tonegen.playDigits(digits, True) 
    152         time.sleep(5) 
    153          
    154         tonegen = None 
     143    tonegen.stop() 
     144    tonegen.playDigits(digits, True) 
     145    time.sleep(5) 
    155146 
    156         ep.libDestroy() 
     147    dm = tonegen.getDigitMap() 
     148    write(dm[0].digit + "\r\n") 
     149    dm[0].freq1 = 400 
     150    dm[0].freq2 = 600 
     151    tonegen.setDigitMap(dm) 
     152 
     153    tonegen.stop() 
     154    tonegen.playDigits(digits, True) 
     155    time.sleep(5) 
     156 
     157    tonegen = None 
     158 
     159    ep.libDestroy() 
    157160 
    158161# 
     
    160163# 
    161164if __name__ == "__main__": 
    162         ua_data_test() 
    163         ua_run_test_exception() 
    164         ua_run_log_test() 
    165         ua_run_ua_test() 
    166         ua_tonegen_test() 
    167         sys.exit(0) 
     165    ua_data_test() 
     166    ua_run_test_exception() 
     167    ua_run_log_test() 
     168    ua_run_ua_test() 
     169    ua_tonegen_test() 
     170    sys.exit(0) 
    168171 
    169          
     172 
Note: See TracChangeset for help on using the changeset viewer.