From 420902fd4b501515600a6971c5b895d44121124c Mon Sep 17 00:00:00 2001 From: Nanang Izzuddin Date: Wed, 2 Oct 2013 03:19:54 +0000 Subject: Close #1701: added received message info into incoming call callback git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@4609 74dad513-b988-da41-8d7b-12977e46ad98 --- pjsip-apps/src/python/_pjsua.c | 41 +++++++----- pjsip-apps/src/python/_pjsua.h | 137 +++++++++++++++++++++++++++++++++++++++-- pjsip-apps/src/python/pjsua.py | 28 +++++++-- 3 files changed, 181 insertions(+), 25 deletions(-) (limited to 'pjsip-apps') diff --git a/pjsip-apps/src/python/_pjsua.c b/pjsip-apps/src/python/_pjsua.c index 91d86041..dffd3b86 100644 --- a/pjsip-apps/src/python/_pjsua.c +++ b/pjsip-apps/src/python/_pjsua.c @@ -131,25 +131,26 @@ static void cb_on_call_state(pjsua_call_id call_id, pjsip_event *e) * declares method on_incoming_call for callback struct */ static void cb_on_incoming_call(pjsua_acc_id acc_id, pjsua_call_id call_id, - pjsip_rx_data *rdata) + pjsip_rx_data *rdata) { - PJ_UNUSED_ARG(rdata); - if (PyCallable_Check(g_obj_callback->on_incoming_call)) { - PyObject *obj; - + PyObj_pjsip_rx_data *obj; + ENTER_PYTHON(); - - obj = Py_BuildValue(""); - - PyObject_CallFunction( - g_obj_callback->on_incoming_call, - "iiO", - acc_id, - call_id, - obj, - NULL - ); + + obj = (PyObj_pjsip_rx_data*) + PyObj_pjsip_rx_data_new(&PyTyp_pjsip_rx_data, + NULL, NULL); + PyObj_pjsip_rx_data_import(obj, rdata); + + PyObject_CallFunction( + g_obj_callback->on_incoming_call, + "iiO", + acc_id, + call_id, + obj, + NULL + ); Py_DECREF(obj); @@ -4453,6 +4454,9 @@ init_pjsua(void) PyTyp_pjsip_cred_info.tp_new = PyType_GenericNew; if (PyType_Ready(&PyTyp_pjsip_cred_info) < 0) return; + PyTyp_pjsip_rx_data.tp_new = PyType_GenericNew; + if (PyType_Ready(&PyTyp_pjsip_rx_data) < 0) + return; /* LIB TRANSPORT */ @@ -4537,6 +4541,11 @@ init_pjsua(void) (PyObject *)&PyTyp_pjsip_cred_info ); + Py_INCREF(&PyTyp_pjsip_rx_data); + PyModule_AddObject(m, "Pjsip_Rx_Data", + (PyObject *)&PyTyp_pjsip_rx_data + ); + /* LIB TRANSPORT */ Py_INCREF(&PyTyp_pjsua_transport_config); diff --git a/pjsip-apps/src/python/_pjsua.h b/pjsip-apps/src/python/_pjsua.h index 2ada85b5..4771148f 100644 --- a/pjsip-apps/src/python/_pjsua.h +++ b/pjsip-apps/src/python/_pjsua.h @@ -1769,6 +1769,7 @@ static void PyObj_pjsua_acc_config_delete(PyObj_pjsua_acc_config* self) static void PyObj_pjsua_acc_config_import(PyObj_pjsua_acc_config *obj, const pjsua_acc_config *cfg) { + PyObj_pjsua_transport_config *tconf; unsigned i; obj->priority = cfg->priority; @@ -1825,8 +1826,9 @@ static void PyObj_pjsua_acc_config_import(PyObj_pjsua_acc_config *obj, obj->srtp_secure_signaling = cfg->srtp_secure_signaling; Py_XDECREF(obj->rtp_transport_cfg); - PyObj_pjsua_transport_config *tconf; - tconf = (PyObj_pjsua_transport_config*) PyObj_pjsua_transport_config_new(&PyTyp_pjsua_transport_config,NULL, NULL); + tconf = (PyObj_pjsua_transport_config*) + PyObj_pjsua_transport_config_new(&PyTyp_pjsua_transport_config, + NULL, NULL); PyObj_pjsua_transport_config_import(tconf, &cfg->rtp_cfg); obj->rtp_transport_cfg = (PyObject *) tconf; } @@ -1834,6 +1836,7 @@ static void PyObj_pjsua_acc_config_import(PyObj_pjsua_acc_config *obj, static void PyObj_pjsua_acc_config_export(pjsua_acc_config *cfg, PyObj_pjsua_acc_config *obj) { + PyObj_pjsua_transport_config *tconf; unsigned i; cfg->priority = obj->priority; @@ -1880,9 +1883,8 @@ static void PyObj_pjsua_acc_config_export(pjsua_acc_config *cfg, cfg->use_srtp = obj->use_srtp; cfg->srtp_secure_signaling = obj->srtp_secure_signaling; - PyObj_pjsua_transport_config *tconf; - tconf = (PyObj_pjsua_transport_config*) obj->rtp_transport_cfg; - PyObj_pjsua_transport_config_export(&cfg->rtp_cfg, tconf); + tconf = (PyObj_pjsua_transport_config*)obj->rtp_transport_cfg; + PyObj_pjsua_transport_config_export(&cfg->rtp_cfg, tconf); } @@ -3584,6 +3586,131 @@ static PyTypeObject PyTyp_pjsua_call_info = }; +////////////////////////////////////////////////////////////////////////////// +/* + * PyObj_pjsip_rx_data + */ +typedef struct +{ + PyObject_HEAD + + /* Type-specific fields go here. */ + PyObject *msg_info_buffer; // string + PyObject *msg_info_info; // string + +} PyObj_pjsip_rx_data; + +/* + * PyObj_pjsip_rx_data_dealloc + * deletes rx_data from memory + */ +static void PyObj_pjsip_rx_data_delete(PyObj_pjsip_rx_data* self) +{ + Py_XDECREF(self->msg_info_buffer); + Py_XDECREF(self->msg_info_info); + + self->ob_type->tp_free((PyObject*)self); +} + + +static void PyObj_pjsip_rx_data_import(PyObj_pjsip_rx_data *obj, pjsip_rx_data *rx_data) +{ + Py_XDECREF(obj->msg_info_buffer); + obj->msg_info_buffer = PyString_FromString(rx_data->msg_info.msg_buf); + Py_XDECREF(obj->msg_info_info); + obj->msg_info_info = PyString_FromString(pjsip_rx_data_get_info(rx_data)); +} + + +/* + * PyObj_pjsip_rx_data_new + * constructor for PyObj_pjsip_rx_data object + */ +static PyObject * PyObj_pjsip_rx_data_new(PyTypeObject *type, PyObject *args, PyObject *kwds) +{ + PyObj_pjsip_rx_data *self; + + PJ_UNUSED_ARG(args); + PJ_UNUSED_ARG(kwds); + + self = (PyObj_pjsip_rx_data *)type->tp_alloc(type, 0); + if (self != NULL) { + self->msg_info_buffer = PyString_FromString(""); + self->msg_info_info = PyString_FromString(""); + } + + return (PyObject *)self; +} + + + +/* + * PyObj_pjsip_rx_data_members + */ +static PyMemberDef PyObj_pjsip_rx_data_members[] = +{ + { + "msg_info_buffer", T_OBJECT_EX, + offsetof(PyObj_pjsip_rx_data, msg_info_buffer), 0, + "Entire SIP-Message" + }, + { + "msg_info_info", T_OBJECT_EX, + offsetof(PyObj_pjsip_rx_data, msg_info_info), 0, + "Message Info" + }, + + {NULL} /* Sentinel */ +}; + +/* + * PyTyp_pjsip_rx_data + */ +static PyTypeObject PyTyp_pjsip_rx_data = +{ + PyObject_HEAD_INIT(NULL) + 0, /*ob_size*/ + "_pjsua.Pjsip_Rx_Data", /*tp_name*/ + sizeof(PyObj_pjsip_rx_data), /*tp_basicsize*/ + 0, /*tp_itemsize*/ + (destructor)PyObj_pjsip_rx_data_delete,/*tp_dealloc*/ + 0, /*tp_print*/ + 0, /*tp_getattr*/ + 0, /*tp_setattr*/ + 0, /*tp_compare*/ + 0, /*tp_repr*/ + 0, /*tp_as_number*/ + 0, /*tp_as_sequence*/ + 0, /*tp_as_mapping*/ + 0, /*tp_hash */ + 0, /*tp_call*/ + 0, /*tp_str*/ + 0, /*tp_getattro*/ + 0, /*tp_setattro*/ + 0, /*tp_as_buffer*/ + Py_TPFLAGS_DEFAULT, /*tp_flags*/ + "PJSIP request data information", /* tp_doc */ + 0, /* tp_traverse */ + 0, /* tp_clear */ + 0, /* tp_richcompare */ + 0, /* tp_weaklistoffset */ + 0, /* tp_iter */ + 0, /* tp_iternext */ + 0, /* tp_methods */ + PyObj_pjsip_rx_data_members, /* tp_members */ + 0, /* tp_getset */ + 0, /* tp_base */ + 0, /* tp_dict */ + 0, /* tp_descr_get */ + 0, /* tp_descr_set */ + 0, /* tp_dictoffset */ + 0, /* tp_init */ + 0, /* tp_alloc */ + PyObj_pjsip_rx_data_new, /* tp_new */ + +}; + + ////////////////////////////////////////////////////////////////////////////// diff --git a/pjsip-apps/src/python/pjsua.py b/pjsip-apps/src/python/pjsua.py index 3ed3bf84..73aeb48e 100644 --- a/pjsip-apps/src/python/pjsua.py +++ b/pjsip-apps/src/python/pjsua.py @@ -897,7 +897,8 @@ class AccountConfig: cfg.use_srtp = self.use_srtp cfg.srtp_secure_signaling = self.srtp_secure_signaling - cfg.rtp_transport_cfg = self.rtp_transport_cfg._cvt_to_pjsua() + if (self.rtp_transport_cfg is not None): + cfg.rtp_transport_cfg = self.rtp_transport_cfg._cvt_to_pjsua() return cfg @@ -978,14 +979,30 @@ class AccountCallback: def on_incoming_call(self, call): """Notification about incoming call. - Unless this callback is implemented, the default behavior is to - reject the call with default status code. + Application should implement one of on_incoming_call() or + on_incoming_call2(), otherwise, the default behavior is to + reject the call with default status code. Note that if both are + implemented, only on_incoming_call2() will be called. Keyword arguments: call -- the new incoming call """ call.hangup() + def on_incoming_call2(self, call, rdata): + """Notification about incoming call, with received SIP message info. + + Application should implement one of on_incoming_call() or + on_incoming_call2(), otherwise, the default behavior is to + reject the call with default status code. Note that if both are + implemented, only on_incoming_call2() will be called. + + Keyword arguments: + call -- the new incoming call + rdata -- the received message + """ + call.hangup() + def on_incoming_subscribe(self, buddy, from_uri, contact_uri, pres_obj): """Notification when incoming SUBSCRIBE request is received. @@ -2741,7 +2758,10 @@ class Lib: def _cb_on_incoming_call(self, acc_id, call_id, rdata): acc = self._lookup_account(acc_id) if acc: - acc._cb.on_incoming_call( Call(self, call_id) ) + if 'on_incoming_call2' in acc._cb.__class__.__dict__: + acc._cb.on_incoming_call2( Call(self, call_id), rdata ) + else: + acc._cb.on_incoming_call( Call(self, call_id) ) else: _pjsua.call_hangup(call_id, 603, None, None) -- cgit v1.2.3