Changeset 972 for pjproject


Ignore:
Timestamp:
Feb 18, 2007 11:49:14 PM (18 years ago)
Author:
bennylp
Message:

Fixed various bugs in Python module

Location:
pjproject/trunk/pjsip-apps/src/py_pjsua
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjsip-apps/src/py_pjsua/pjsua_app.py

    r934 r972  
    3737g_rec_port = 0 
    3838 
     39# Utility: display PJ error and exit 
     40# 
     41def err_exit(title, rc): 
     42    py_pjsua.perror(THIS_FILE, title, rc) 
     43    exit(1) 
     44 
     45 
     46# Logging function (also callback, called by pjsua-lib) 
     47# 
     48def log_cb(level, str, len): 
     49    if level <= C_LOG_LEVEL: 
     50        print str, 
     51 
     52def write_log(level, str): 
     53    log_cb(level, str + "\n", 0) 
     54 
     55 
    3956# Utility to get call info 
    4057# 
     
    85102def on_reg_state(acc_id): 
    86103        acc_info = py_pjsua.acc_get_info(acc_id) 
     104        if acc_info.has_registration != 0: 
     105                cmd = "registration" 
     106        else: 
     107                cmd = "unregistration" 
    87108        if acc_info.status != 0 and acc_info.status != 200: 
    88                 write_log(3, "Account (un)registration failed: rc=" + `acc_info.status` + " " + acc_info.status_text) 
    89         else: 
    90                 write_log(3, "Account successfully (un)registered") 
     109                write_log(3, "Account " + cmd + " failed: rc=" + `acc_info.status` + " " + acc_info.status_text) 
     110        else: 
     111                write_log(3, "Account " + cmd + " success") 
    91112 
    92113 
     
    123144        write_log(3, "IM indication: " + strfrom + " " + str_t) 
    124145 
    125 # on call transfer status 
     146# Received the status of previous call transfer request 
    126147# 
    127148def on_call_transfer_status(call_id,status_code,status_text,final,p_cont): 
     
    137158                p_cont = 0 
    138159 
    139 # on call transfer request 
     160# Callback on incoming call transfer request 
    140161#                
    141162def on_call_transfer_request(call_id, dst, code): 
    142163        write_log(3, "Call transfer request from " + `call_id` + " to " + dst + " with code " + `code`) 
    143  
    144 # Utility: display PJ error and exit 
    145 # 
    146 def err_exit(title, rc): 
    147     py_pjsua.perror(THIS_FILE, title, rc) 
    148     exit(1) 
    149  
    150  
    151 # Logging function (also callback, called by pjsua-lib) 
    152 # 
    153 def log_cb(level, str, len): 
    154     if level <= C_LOG_LEVEL: 
    155         print str, 
    156  
    157 def write_log(level, str): 
    158     log_cb(level, str + "\n", 0) 
    159  
    160164 
    161165# 
     
    330334 
    331335def conf_list(): 
    332          
    333          
    334336        ports = None 
    335  
    336337        print "Conference ports : " 
    337  
    338          
    339338        ports = py_pjsua.enum_conf_ports() 
    340339 
     
    401400        else: 
    402401                write_log(3, "No current call") 
    403                  
     402 
    404403def xfer_call(): 
    405404        global g_current_call 
     
    507506 
    508507 
     508# Print account and buddy list 
     509def print_acc_buddy_list(): 
     510        global g_acc_id 
     511         
     512        acc_ids = py_pjsua.enum_accs() 
     513        print "Account list:" 
     514        for acc_id in acc_ids: 
     515                acc_info = py_pjsua.acc_get_info(acc_id) 
     516                if acc_info.has_registration == 0: 
     517                        acc_status = acc_info.status_text 
     518                else: 
     519                        acc_status = `acc_info.status` + "/" + acc_info.status_text + " (expires=" + `acc_info.expires` + ")" 
     520 
     521                if acc_id == g_acc_id: 
     522                        print " *", 
     523                else: 
     524                        print "  ", 
     525 
     526                print "[" + `acc_id` + "] " + acc_info.acc_uri + ": " + acc_status 
     527                print "       Presence status: ", 
     528                if acc_info.online_status != 0: 
     529                        print "Online" 
     530                else: 
     531                        print "Invisible" 
     532 
     533        if py_pjsua.get_buddy_count() > 0: 
     534                print "" 
     535                print "Buddy list:" 
     536                buddy_ids = py_pjsua.enum_buddies() 
     537                for buddy_id in buddy_ids: 
     538                        bi = py_pjsua.buddy_get_info(buddy_id) 
     539                        print "   [" + `buddy_id` + "] " + bi.status_text + " " + bi.uri 
     540         
     541                 
    509542# Print application menu 
    510543# 
    511544def print_menu(): 
     545        print "" 
     546        print ">>>" 
     547        print_acc_buddy_list() 
    512548        print """ 
    513549+============================================================================+ 
     
    525561|  X  Xfer with Replaces       |                          |                  | 
    526562|                              | cl  List ports           |  d  Dump status  | 
    527 |                              | cc  Connect port         |                  | 
     563|                              | cc  Connect port         | dd  Dump detail  | 
    528564|                              | cd  Disconnect port      |                  | 
    529565|                              | +p  Add file player      |                  | 
    530566|------------------------------+ +r  Add file recorder    |                  | 
    531567|  q  Quit application         |                          |                  | 
    532 +============================================================================+ 
    533         """ 
    534         print "Choice: ",  
     568+============================================================================+""" 
     569        print "You have " + `py_pjsua.call_get_count()` + " active call(s)" 
     570        print ">>>",  
    535571 
    536572# Menu 
     
    592628                                continue 
    593629             
     630                        bc.uri = bc.uri.replace("\n", "") 
    594631                        bc.subscribe = 1 
    595632                        status, buddy_id = py_pjsua.buddy_add(bc) 
     
    714751                                py_pjsua.acc_set_registration(g_acc_id, 0) 
    715752                elif choice[0] == "d": 
    716                          
    717                         write_log(3, "Start dumping application states : ") 
    718                         write_log(3, "Dumping invite sessions : ") 
    719  
    720                         if py_pjsua.call_get_count() == 0: 
    721                                 write_log(3, "  - no sessions -") 
    722                         else: 
    723                                 for i in range(0, g_ua_cfg.max_calls): 
    724                                         if py_pjsua.call_is_active(i): 
    725                                                 buf = py_pjsua.call_dump(i, 1, 1024, "  ") 
    726                                                 write_log(3, buf)        
    727                         py_pjsua.pres_dump(1) 
    728                         write_log(3, "Dump complete") 
     753                        py_pjsua.dump(choice[1] == "d") 
    729754                elif choice[0] == "a": 
    730755                        if g_current_call != py_pjsua.PJSUA_INVALID_ID:                          
  • pjproject/trunk/pjsip-apps/src/py_pjsua/py_pjsua.c

    r945 r972  
    2828/* LIB BASE */ 
    2929 
    30 static PyObject* obj_reconfigure_logging; 
    31 static PyObject* obj_logging_init; 
     30static PyObject* obj_log_cb; 
    3231static long thread_id; 
    3332 
    34 /* 
    35  * cb_reconfigure_logging 
     33#define ENTER_PYTHON()      PyGILState_STATE state = PyGILState_Ensure() 
     34#define LEAVE_PYTHON()      PyGILState_Release(state) 
     35 
     36/* 
     37 * cb_log_cb 
    3638 * declares method for reconfiguring logging process for callback struct 
    3739 */ 
    38 static void cb_reconfigure_logging(int level, const char *data, pj_size_t len) 
     40static void cb_log_cb(int level, const char *data, pj_size_t len) 
    3941{ 
    4042         
    41     if (PyCallable_Check(obj_reconfigure_logging)) 
    42     { 
    43         PyObject_CallFunctionObjArgs( 
    44             obj_reconfigure_logging, Py_BuildValue("i",level), 
    45             PyString_FromString(data), Py_BuildValue("i",len), NULL 
    46         ); 
    47     } 
    48 } 
    49  
    50  
    51 /* 
    52  * cb_logging_init 
    53  * declares method logging_init for callback struct 
    54  */ 
    55 static void cb_logging_init(int level, const char *data, pj_size_t len) 
    56 { 
    5743    /* Ignore if this callback is called from alien thread context, 
    5844     * or otherwise it will crash Python. 
     
    6147        return; 
    6248 
    63     if (PyCallable_Check(obj_logging_init)) 
    64     { 
    65          
     49    if (PyCallable_Check(obj_log_cb)) 
     50    { 
     51        ENTER_PYTHON(); 
     52 
    6653        PyObject_CallFunctionObjArgs( 
    67             obj_logging_init, Py_BuildValue("i",level), 
     54            obj_log_cb, Py_BuildValue("i",level), 
    6855            PyString_FromString(data), Py_BuildValue("i",len), NULL 
    6956        ); 
     57 
     58        LEAVE_PYTHON(); 
    7059    } 
    7160} 
     
    196185    {    
    197186        pjsip_event_Object * obj; 
    198                  
    199         obj = 
    200                 (pjsip_event_Object *)PyType_GenericNew(&pjsip_event_Type,  
    201                                                     NULL, NULL); 
     187 
     188        ENTER_PYTHON(); 
     189 
     190        obj = (pjsip_event_Object *)PyType_GenericNew(&pjsip_event_Type, 
     191                                                      NULL, NULL); 
    202192                 
    203193        obj->event = e; 
     
    206196            g_obj_callback->on_call_state,Py_BuildValue("i",call_id),obj,NULL 
    207197        ); 
    208                  
     198 
     199        LEAVE_PYTHON(); 
    209200    } 
    210201} 
     
    220211    if (PyCallable_Check(g_obj_callback->on_incoming_call)) 
    221212    { 
    222         pjsip_rx_data_Object * obj = (pjsip_rx_data_Object *) 
    223                                       PyType_GenericNew(&pjsip_rx_data_Type,  
     213        pjsip_rx_data_Object * obj; 
     214 
     215        ENTER_PYTHON(); 
     216 
     217        obj = (pjsip_rx_data_Object *)PyType_GenericNew(&pjsip_rx_data_Type,  
    224218                                                        NULL, NULL); 
    225219        obj->rdata = rdata; 
     
    232226                NULL 
    233227        ); 
     228 
     229        LEAVE_PYTHON(); 
    234230    } 
    235231} 
     
    244240    if (PyCallable_Check(g_obj_callback->on_call_media_state)) 
    245241    { 
     242        ENTER_PYTHON(); 
     243 
    246244        PyObject_CallFunction(g_obj_callback->on_call_media_state,"i",call_id); 
     245 
     246        LEAVE_PYTHON(); 
    247247    } 
    248248} 
     
    257257                                        pjsip_status_code *code) 
    258258{ 
    259     PyObject * ret; 
    260     int cd; 
    261259    if (PyCallable_Check(g_obj_callback->on_call_transfer_request)) 
    262260    { 
     261        PyObject * ret; 
     262        int cd; 
     263 
     264        ENTER_PYTHON(); 
     265 
    263266        ret = PyObject_CallFunctionObjArgs( 
    264267            g_obj_callback->on_call_transfer_request, 
     
    275278            } 
    276279        } 
     280 
     281        LEAVE_PYTHON(); 
    277282    } 
    278283} 
     
    292297                                        pj_bool_t *p_cont) 
    293298{ 
    294     PyObject * ret; 
    295     int cnt; 
    296299    if (PyCallable_Check(g_obj_callback->on_call_transfer_status)) 
    297300    { 
     301        PyObject * ret; 
     302        int cnt; 
     303 
     304        ENTER_PYTHON(); 
     305 
    298306        ret = PyObject_CallFunctionObjArgs( 
    299307            g_obj_callback->on_call_transfer_status, 
     
    312320            } 
    313321        } 
     322 
     323        LEAVE_PYTHON(); 
    314324    } 
    315325} 
     
    326336                                        pj_str_t *st_text) 
    327337{ 
    328     PyObject * ret; 
    329     PyObject * txt; 
    330     int cd; 
    331338    if (PyCallable_Check(g_obj_callback->on_call_replace_request)) 
    332339    { 
    333         pjsip_rx_data_Object * obj = (pjsip_rx_data_Object *) 
    334                                       PyType_GenericNew(&pjsip_rx_data_Type, 
     340        PyObject * ret; 
     341        PyObject * txt; 
     342        int cd; 
     343        pjsip_rx_data_Object * obj; 
     344 
     345        ENTER_PYTHON(); 
     346 
     347        obj = (pjsip_rx_data_Object *)PyType_GenericNew(&pjsip_rx_data_Type, 
    335348                                                        NULL, NULL); 
    336349        obj->rdata = rdata; 
     
    353366            } 
    354367        } 
     368 
     369        LEAVE_PYTHON(); 
    355370    } 
    356371} 
     
    367382    if (PyCallable_Check(g_obj_callback->on_call_replaced)) 
    368383    { 
     384        ENTER_PYTHON(); 
     385 
    369386        PyObject_CallFunctionObjArgs( 
    370387            g_obj_callback->on_call_replaced, 
     
    373390            NULL 
    374391        ); 
     392 
     393        LEAVE_PYTHON(); 
    375394    } 
    376395} 
     
    385404    if (PyCallable_Check(g_obj_callback->on_reg_state)) 
    386405    { 
     406        ENTER_PYTHON(); 
     407 
    387408        PyObject_CallFunction(g_obj_callback->on_reg_state,"i",acc_id); 
     409 
     410        LEAVE_PYTHON(); 
    388411    } 
    389412} 
     
    398421    if (PyCallable_Check(g_obj_callback->on_buddy_state)) 
    399422    { 
     423        ENTER_PYTHON(); 
     424 
    400425        PyObject_CallFunction(g_obj_callback->on_buddy_state,"i",buddy_id); 
     426 
     427        LEAVE_PYTHON(); 
    401428    } 
    402429} 
     
    412439    if (PyCallable_Check(g_obj_callback->on_pager)) 
    413440    { 
     441        ENTER_PYTHON(); 
     442 
    414443        PyObject_CallFunctionObjArgs( 
    415444            g_obj_callback->on_pager,Py_BuildValue("i",call_id), 
     
    420449            PyString_FromStringAndSize(body->ptr, body->slen), NULL 
    421450        ); 
     451 
     452        LEAVE_PYTHON(); 
    422453    } 
    423454} 
     
    433464                                const pj_str_t *reason) 
    434465{ 
    435          
    436     PyObject * obj = PyType_GenericNew(user_data, NULL, NULL); 
    437466    if (PyCallable_Check(g_obj_callback->on_pager)) 
    438467    { 
     468        PyObject * obj_user_data; 
     469 
     470        ENTER_PYTHON(); 
     471 
     472        obj_user_data = Py_BuildValue("i", user_data); 
     473 
    439474        PyObject_CallFunctionObjArgs( 
    440             g_obj_callback->on_pager,Py_BuildValue("i",call_id), 
     475            g_obj_callback->on_pager_status, 
     476            Py_BuildValue("i",call_id), 
    441477            PyString_FromStringAndSize(to->ptr, to->slen), 
    442             PyString_FromStringAndSize(body->ptr, body->slen),obj, 
    443             Py_BuildValue("i",status),PyString_FromStringAndSize(reason->ptr, 
    444             reason->slen),NULL 
     478            PyString_FromStringAndSize(body->ptr, body->slen),  
     479            obj_user_data, 
     480            Py_BuildValue("i",status), 
     481            PyString_FromStringAndSize(reason->ptr,reason->slen), 
     482            NULL 
    445483        ); 
     484 
     485        LEAVE_PYTHON(); 
    446486    } 
    447487} 
     
    458498    if (PyCallable_Check(g_obj_callback->on_typing)) 
    459499    { 
     500        ENTER_PYTHON(); 
     501 
    460502        PyObject_CallFunctionObjArgs( 
    461503            g_obj_callback->on_typing,Py_BuildValue("i",call_id), 
     
    465507            Py_BuildValue("i",is_typing),NULL 
    466508        ); 
     509 
     510        LEAVE_PYTHON(); 
    467511    } 
    468512} 
     
    13001344void translate_hdr(pj_pool_t *pool, pjsip_hdr *hdr, PyObject *py_hdr_list) 
    13011345{ 
    1302     int i; 
     1346    pj_list_init(hdr); 
    13031347 
    13041348    if (PyList_Check(py_hdr_list)) { 
    1305         pj_list_init(hdr); 
     1349        int i; 
    13061350 
    13071351        for (i = 0; i < PyList_Size(py_hdr_list); i++)  
     
    18671911        cfg.log_filename.ptr = PyString_AsString(log->log_filename); 
    18681912        cfg.log_filename.slen = strlen(cfg.log_filename.ptr); 
    1869         Py_XDECREF(obj_reconfigure_logging); 
    1870         obj_reconfigure_logging = log->cb; 
    1871         Py_INCREF(obj_reconfigure_logging); 
    1872         cfg.cb = &cb_reconfigure_logging; 
     1913        Py_XDECREF(obj_log_cb); 
     1914        obj_log_cb = log->cb; 
     1915        Py_INCREF(obj_log_cb); 
     1916        cfg.cb = &cb_log_cb; 
    18731917        status = pjsua_reconfigure_logging(&cfg); 
    18741918    } else { 
     
    20872131        cfg_log.log_filename.ptr = PyString_AsString(log_cfg->log_filename); 
    20882132        cfg_log.log_filename.slen = strlen(cfg_log.log_filename.ptr); 
    2089         Py_XDECREF(obj_logging_init); 
    2090         obj_logging_init = log_cfg->cb; 
    2091         Py_INCREF(obj_logging_init); 
    2092         cfg_log.cb = &cb_logging_init; 
     2133        Py_XDECREF(obj_log_cb); 
     2134        obj_log_cb = log_cfg->cb; 
     2135        Py_INCREF(obj_log_cb); 
     2136        cfg_log.cb = &cb_log_cb; 
    20932137        p_cfg_log = &cfg_log; 
    20942138    } else { 
     
    78637907} 
    78647908 
     7909 
     7910/* 
     7911 * py_pjsua_dump 
     7912 * Dump application states. 
     7913 */ 
     7914static PyObject *py_pjsua_dump(PyObject *pSelf, PyObject *pArgs) 
     7915{ 
     7916    unsigned old_decor; 
     7917    char buf[1024]; 
     7918    int detail; 
     7919 
     7920    if (!PyArg_ParseTuple(pArgs, "i", &detail)) 
     7921    { 
     7922        return NULL; 
     7923    }    
     7924 
     7925    PJ_LOG(3,(THIS_FILE, "Start dumping application states:")); 
     7926 
     7927    old_decor = pj_log_get_decor(); 
     7928    pj_log_set_decor(old_decor & (PJ_LOG_HAS_NEWLINE | PJ_LOG_HAS_CR)); 
     7929 
     7930    if (detail) 
     7931        pj_dump_config(); 
     7932 
     7933    pjsip_endpt_dump(pjsua_get_pjsip_endpt(), detail); 
     7934    pjmedia_endpt_dump(pjsua_get_pjmedia_endpt()); 
     7935    pjsip_tsx_layer_dump(detail); 
     7936    pjsip_ua_dump(detail); 
     7937 
     7938 
     7939    /* Dump all invite sessions: */ 
     7940    PJ_LOG(3,(THIS_FILE, "Dumping invite sessions:")); 
     7941 
     7942    if (pjsua_call_get_count() == 0) { 
     7943 
     7944        PJ_LOG(3,(THIS_FILE, "  - no sessions -")); 
     7945 
     7946    } else { 
     7947        unsigned i, max; 
     7948 
     7949        max = pjsua_call_get_max_count(); 
     7950        for (i=0; i<max; ++i) { 
     7951            if (pjsua_call_is_active(i)) { 
     7952                pjsua_call_dump(i, detail, buf, sizeof(buf), "  "); 
     7953                PJ_LOG(3,(THIS_FILE, "%s", buf)); 
     7954            } 
     7955        } 
     7956    } 
     7957 
     7958    /* Dump presence status */ 
     7959    pjsua_pres_dump(detail); 
     7960 
     7961    pj_log_set_decor(old_decor); 
     7962    PJ_LOG(3,(THIS_FILE, "Dump complete")); 
     7963 
     7964    Py_INCREF(Py_None); 
     7965    return Py_None; 
     7966} 
     7967 
     7968 
    78657969static char pjsua_call_get_max_count_doc[] = 
    78667970    "int py_pjsua.call_get_max_count () " 
     
    83568460        pjsua_call_dump_doc 
    83578461    }, 
    8358  
     8462    { 
     8463        "dump", py_pjsua_dump, METH_VARARGS, "Dump application state" 
     8464    }, 
    83598465 
    83608466     
     
    83748480 
    83758481     
     8482    PyEval_InitThreads(); 
     8483 
    83768484    if (PyType_Ready(&callback_Type) < 0) 
    83778485        return; 
Note: See TracChangeset for help on using the changeset viewer.