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

Fixed various bugs in Python module

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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.