Add support for ICE/STUN/TURN in res_rtp_asterisk and chan_sip.
[asterisk/asterisk.git] / res / pjproject / pjsip-apps / src / python / _pjsua.c
1 /* $Id$ */
2 /* 
3  * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4  * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
19  */
20 #include "_pjsua.h"
21
22 #define THIS_FILE    "main.c"
23 #define POOL_SIZE    512
24 #define SND_DEV_NUM  64
25 #define SND_NAME_LEN  64
26
27 /* LIB BASE */
28
29 static PyObject* g_obj_log_cb;
30 static long g_thread_id;
31 static struct py_thread_desc
32 {
33     struct py_thread_desc *next;
34     pj_thread_desc         desc;
35 } *py_thread_desc;
36
37 /*
38  * The global callback object.
39  */
40 static PyObj_pjsua_callback * g_obj_callback;
41
42 /* Set this to 1 if all threads are created by Python */
43 #define NO_PJSIP_THREAD 1
44
45 #if NO_PJSIP_THREAD
46 #   define ENTER_PYTHON()
47 #   define LEAVE_PYTHON()
48 #else
49 #   define ENTER_PYTHON()   PyGILState_STATE state = PyGILState_Ensure()
50 #   define LEAVE_PYTHON()   PyGILState_Release(state)
51 #endif
52
53
54 static void clear_py_thread_desc(void)
55 {
56     while (py_thread_desc) {
57         struct py_thread_desc *next = py_thread_desc->next;
58         free(py_thread_desc);
59         py_thread_desc = next;
60     }
61 }
62
63
64 /*
65  * cb_log_cb
66  * declares method for reconfiguring logging process for callback struct
67  */
68 static void cb_log_cb(int level, const char *data, int len)
69 {
70         
71     /* Ignore if this callback is called from alien thread context,
72      * or otherwise it will crash Python.
73      */
74     if (pj_thread_local_get(g_thread_id) == 0)
75         return;
76
77     if (PyCallable_Check(g_obj_log_cb)) {
78         PyObject *param_data;
79
80         ENTER_PYTHON();
81
82         param_data = PyString_FromStringAndSize(data, len);
83
84         PyObject_CallFunction(
85             g_obj_log_cb, 
86             "iOi",
87             level,
88             param_data, 
89             len, 
90             NULL
91         );
92
93         Py_DECREF(param_data);
94
95         LEAVE_PYTHON();
96     }
97 }
98
99 /*
100  * cb_on_call_state
101  * declares method on_call_state for callback struct
102  */
103 static void cb_on_call_state(pjsua_call_id call_id, pjsip_event *e)
104 {
105     PJ_UNUSED_ARG(e);
106
107     if (PyCallable_Check(g_obj_callback->on_call_state)) {      
108         PyObject * obj;
109
110         ENTER_PYTHON();
111
112         obj = Py_BuildValue("");
113                 
114         PyObject_CallFunction(
115             g_obj_callback->on_call_state,
116             "iO",
117             call_id,
118             obj,
119             NULL
120         );
121
122         Py_DECREF(obj);
123
124         LEAVE_PYTHON();
125     }
126 }
127
128
129 /*
130  * cb_on_incoming_call
131  * declares method on_incoming_call for callback struct
132  */
133 static void cb_on_incoming_call(pjsua_acc_id acc_id, pjsua_call_id call_id,
134                                 pjsip_rx_data *rdata)
135 {
136     PJ_UNUSED_ARG(rdata);
137
138     if (PyCallable_Check(g_obj_callback->on_incoming_call)) {
139         PyObject *obj;
140
141         ENTER_PYTHON();
142
143         obj = Py_BuildValue("");
144
145         PyObject_CallFunction(
146                 g_obj_callback->on_incoming_call,
147                 "iiO",
148                 acc_id,
149                 call_id,
150                 obj,
151                 NULL
152         );
153
154         Py_DECREF(obj);
155
156         LEAVE_PYTHON();
157     }
158 }
159
160
161 /*
162  * cb_on_call_media_state
163  * declares method on_call_media_state for callback struct
164  */
165 static void cb_on_call_media_state(pjsua_call_id call_id)
166 {
167     if (PyCallable_Check(g_obj_callback->on_call_media_state)) {
168
169         ENTER_PYTHON();
170
171         PyObject_CallFunction(
172             g_obj_callback->on_call_media_state,
173             "i",
174             call_id,
175             NULL
176         );
177
178         LEAVE_PYTHON();
179     }
180 }
181
182
183 /*
184  * cb_on_dtmf_digit()
185  * Callback from PJSUA-LIB on receiving DTMF digit
186  */
187 static void cb_on_dtmf_digit(pjsua_call_id call_id, int digit)
188 {
189     if (PyCallable_Check(g_obj_callback->on_dtmf_digit)) {
190         char digit_str[10];
191
192         PyGILState_STATE state = PyGILState_Ensure();
193
194         pj_ansi_snprintf(digit_str, sizeof(digit_str), "%c", digit);
195
196         PyObject_CallFunction(
197             g_obj_callback->on_dtmf_digit,
198             "is",
199             call_id,
200             digit_str,
201             NULL
202         );
203
204         PyGILState_Release(state);
205     }
206 }
207
208
209 /*
210  * Notify application on call being transfered.
211  * !modified @061206
212  */
213 static void cb_on_call_transfer_request(pjsua_call_id call_id,
214                                         const pj_str_t *dst,
215                                         pjsip_status_code *code)
216 {
217     if (PyCallable_Check(g_obj_callback->on_call_transfer_request)) {
218         PyObject *ret, *param_dst;
219         int cd;
220
221         ENTER_PYTHON();
222
223         param_dst = PyString_FromPJ(dst);
224
225         ret = PyObject_CallFunction(
226                     g_obj_callback->on_call_transfer_request,
227                     "iOi",
228                     call_id,
229                     param_dst,
230                     *code,
231                     NULL
232                 );
233
234         Py_DECREF(param_dst);
235
236         if (ret != NULL) {
237             if (ret != Py_None) {
238                 if (PyArg_Parse(ret,"i",&cd)) {
239                     *code = cd;
240                 }
241             }
242             Py_DECREF(ret);
243         }
244
245         LEAVE_PYTHON();
246     }
247 }
248
249
250 /*
251  * Notify application of the status of previously sent call
252  * transfer request. Application can monitor the status of the
253  * call transfer request, for example to decide whether to 
254  * terminate existing call.
255  * !modified @061206
256  */
257 static void cb_on_call_transfer_status( pjsua_call_id call_id,
258                                         int status_code,
259                                         const pj_str_t *status_text,
260                                         pj_bool_t final,
261                                         pj_bool_t *p_cont)
262 {
263     if (PyCallable_Check(g_obj_callback->on_call_transfer_status)) {
264         PyObject *ret, *param_reason;
265
266         ENTER_PYTHON();
267
268         param_reason = PyString_FromPJ(status_text);
269
270         ret = PyObject_CallFunction(
271                     g_obj_callback->on_call_transfer_status,
272                     "iiOii",
273                     call_id,
274                     status_code,
275                     param_reason,
276                     final,
277                     *p_cont,
278                     NULL
279                 );
280
281         Py_DECREF(param_reason);
282
283         if (ret != NULL) {
284             if (ret != Py_None) {
285                 int cnt;
286                 if (PyArg_Parse(ret,"i",&cnt)) {
287                     *p_cont = cnt;
288                 }
289             }
290             Py_DECREF(ret);
291         }
292
293         LEAVE_PYTHON();
294     }
295 }
296
297
298 /*
299  * Notify application about incoming INVITE with Replaces header.
300  * Application may reject the request by setting non-2xx code.
301  * !modified @061206
302  */
303 static void cb_on_call_replace_request( pjsua_call_id call_id,
304                                         pjsip_rx_data *rdata,
305                                         int *st_code,
306                                         pj_str_t *st_text)
307 {
308     PJ_UNUSED_ARG(rdata);
309
310     if (PyCallable_Check(g_obj_callback->on_call_replace_request)) {
311         PyObject *ret, *param_reason, *param_rdata;
312         int cd;
313
314         ENTER_PYTHON();
315
316         param_reason = PyString_FromPJ(st_text);
317         param_rdata = Py_BuildValue("");
318
319         ret = PyObject_CallFunction(
320                     g_obj_callback->on_call_replace_request,
321                     "iOiO",
322                     call_id,
323                     param_rdata,
324                     *st_code,
325                     param_reason,
326                     NULL
327                 );
328
329         Py_DECREF(param_rdata);
330         Py_DECREF(param_reason);
331
332         if (ret != NULL) {
333             if (ret != Py_None) {
334                 PyObject * txt;
335                 if (PyArg_ParseTuple(ret,"iO",&cd, &txt)) {
336                     *st_code = cd;
337                     *st_text = PyString_ToPJ(txt);
338                 }
339             }
340             Py_DECREF(ret);
341         }
342
343         LEAVE_PYTHON();
344     }
345 }
346
347
348 /*
349  * Notify application that an existing call has been replaced with
350  * a new call. This happens when PJSUA-API receives incoming INVITE
351  * request with Replaces header.
352  */
353 static void cb_on_call_replaced(pjsua_call_id old_call_id,
354                                 pjsua_call_id new_call_id)
355 {
356     if (PyCallable_Check(g_obj_callback->on_call_replaced)) {
357         ENTER_PYTHON();
358
359         PyObject_CallFunction(
360             g_obj_callback->on_call_replaced,
361             "ii",
362             old_call_id,
363             new_call_id,
364             NULL
365         );
366
367         LEAVE_PYTHON();
368     }
369 }
370
371
372 /*
373  * cb_on_reg_state
374  * declares method on_reg_state for callback struct
375  */
376 static void cb_on_reg_state(pjsua_acc_id acc_id)
377 {
378     if (PyCallable_Check(g_obj_callback->on_reg_state)) {
379         ENTER_PYTHON();
380
381         PyObject_CallFunction(
382             g_obj_callback->on_reg_state,
383             "i",
384             acc_id,
385             NULL
386         );
387
388         LEAVE_PYTHON();
389     }
390 }
391
392 /* 
393  * cb_on_incoming_subscribe
394  */
395 static void cb_on_incoming_subscribe( pjsua_acc_id acc_id,
396                                       pjsua_srv_pres *srv_pres,
397                                       pjsua_buddy_id buddy_id,
398                                       const pj_str_t *from,
399                                       pjsip_rx_data *rdata,
400                                       pjsip_status_code *code,
401                                       pj_str_t *reason,
402                                       pjsua_msg_data *msg_data)
403 {
404     static char reason_buf[64];
405
406     PJ_UNUSED_ARG(rdata);
407     PJ_UNUSED_ARG(msg_data);
408
409     if (PyCallable_Check(g_obj_callback->on_incoming_subscribe)) {
410         PyObject *ret, *param_from, *param_contact, *param_srv_pres;
411         pjsip_contact_hdr *contact_hdr;
412         pj_pool_t *pool = NULL;
413
414         ENTER_PYTHON();
415
416         param_from = PyString_FromPJ(from);
417         param_srv_pres = PyLong_FromLong((long)srv_pres);
418
419         contact_hdr = (pjsip_contact_hdr*)
420                       pjsip_msg_find_hdr(rdata->msg_info.msg, PJSIP_H_CONTACT,
421                                          NULL);
422         if (contact_hdr) {
423             char *contact;
424             int len;
425
426             pool = pjsua_pool_create("pytmp", 512, 512);
427             contact = (char*) pj_pool_alloc(pool, PJSIP_MAX_URL_SIZE+1);
428             len = pjsip_uri_print(PJSIP_URI_IN_CONTACT_HDR, contact_hdr->uri, 
429                                   contact, PJSIP_MAX_URL_SIZE);
430             if (len < 1)
431                 len = 0;
432             contact[len] = '\0';
433
434             param_contact = PyString_FromStringAndSize(contact, len);
435         } else {
436             param_contact = Py_BuildValue("");
437         }
438
439         ret = PyObject_CallFunction(
440                     g_obj_callback->on_incoming_subscribe,
441                     "iiOOO",
442                     acc_id,
443                     buddy_id,
444                     param_from,
445                     param_contact,
446                     param_srv_pres,
447                     NULL
448                 );
449
450         if (pool)
451             pj_pool_release(pool);
452
453         Py_DECREF(param_from);
454         Py_DECREF(param_contact);
455         Py_DECREF(param_srv_pres);
456
457         if (ret && PyTuple_Check(ret)) {
458             if (PyTuple_Size(ret) >= 1)
459                 *code = (int)PyInt_AsLong(PyTuple_GetItem(ret, 0));
460             if (PyTuple_Size(ret) >= 2) {
461                 if (PyTuple_GetItem(ret, 1) != Py_None) {
462                     pj_str_t tmp;
463                     tmp = PyString_ToPJ(PyTuple_GetItem(ret, 1));
464                     reason->ptr = reason_buf;
465                     pj_strncpy(reason, &tmp, sizeof(reason_buf));
466                 } else {
467                     reason->slen = 0;
468                 }
469             }
470             Py_XDECREF(ret);
471         } else if (ret) {
472             Py_XDECREF(ret);
473         }
474
475         LEAVE_PYTHON();
476     }
477 }
478
479 /*
480  * cb_on_buddy_state
481  * declares method on_buddy state for callback struct
482  */
483 static void cb_on_buddy_state(pjsua_buddy_id buddy_id)
484 {
485     if (PyCallable_Check(g_obj_callback->on_buddy_state)) {
486         ENTER_PYTHON();
487
488         PyObject_CallFunction(
489             g_obj_callback->on_buddy_state,
490             "i",
491             buddy_id,
492             NULL
493         );
494
495         LEAVE_PYTHON();
496     }
497 }
498
499 /*
500  * cb_on_pager
501  * declares method on_pager for callback struct
502  */
503 static void cb_on_pager(pjsua_call_id call_id, const pj_str_t *from,
504                         const pj_str_t *to, const pj_str_t *contact,
505                         const pj_str_t *mime_type, const pj_str_t *body,
506                         pjsip_rx_data *rdata, pjsua_acc_id acc_id)
507 {
508     PJ_UNUSED_ARG(rdata);
509
510     if (PyCallable_Check(g_obj_callback->on_pager)) {
511         PyObject *param_from, *param_to, *param_contact, *param_mime_type,
512                  *param_body;
513
514         ENTER_PYTHON();
515
516         param_from = PyString_FromPJ(from);
517         param_to = PyString_FromPJ(to);
518         param_contact = PyString_FromPJ(contact);
519         param_mime_type = PyString_FromPJ(mime_type);
520         param_body = PyString_FromPJ(body);
521
522         PyObject_CallFunction(
523                 g_obj_callback->on_pager,
524                 "iOOOOOi",
525                 call_id,
526                 param_from,
527                 param_to,
528                 param_contact,
529                 param_mime_type,
530                 param_body, 
531                 acc_id,
532                 NULL
533             );
534
535         Py_DECREF(param_body);
536         Py_DECREF(param_mime_type);
537         Py_DECREF(param_contact);
538         Py_DECREF(param_to);
539         Py_DECREF(param_from);
540
541         LEAVE_PYTHON();
542     }
543 }
544
545
546 /*
547  * cb_on_pager_status
548  * declares method on_pager_status for callback struct
549  */
550 static void cb_on_pager_status(pjsua_call_id call_id, const pj_str_t *to,
551                                 const pj_str_t *body, void *user_data,
552                                 pjsip_status_code status,
553                                 const pj_str_t *reason,
554                                 pjsip_tx_data *tdata,
555                                 pjsip_rx_data *rdata,
556                                 pjsua_acc_id acc_id)
557 {
558     if (PyCallable_Check(g_obj_callback->on_pager)) {
559         PyObject *param_call_id, *param_to, *param_body,
560                  *param_user_data, *param_status, *param_reason,
561                  *param_acc_id;
562
563         ENTER_PYTHON();
564
565         PJ_UNUSED_ARG(tdata);
566         PJ_UNUSED_ARG(rdata);
567
568         PyObject_CallFunctionObjArgs(
569                 g_obj_callback->on_pager_status,
570                 param_call_id   = Py_BuildValue("i",call_id),
571                 param_to        = PyString_FromPJ(to),
572                 param_body      = PyString_FromPJ(body), 
573                 param_user_data = Py_BuildValue("i", user_data),
574                 param_status    = Py_BuildValue("i",status),
575                 param_reason    = PyString_FromPJ(reason),
576                 param_acc_id    = Py_BuildValue("i",acc_id),
577                 NULL
578             );
579
580         Py_DECREF(param_call_id);
581         Py_DECREF(param_to);
582         Py_DECREF(param_body);
583         Py_DECREF(param_user_data);
584         Py_DECREF(param_status);
585         Py_DECREF(param_reason);
586         Py_DECREF(param_acc_id);
587
588         LEAVE_PYTHON();
589     }
590 }
591
592
593 /*
594  * cb_on_typing
595  * declares method on_typing for callback struct
596  */
597 static void cb_on_typing(pjsua_call_id call_id, const pj_str_t *from,
598                             const pj_str_t *to, const pj_str_t *contact,
599                             pj_bool_t is_typing, pjsip_rx_data *rdata,
600                             pjsua_acc_id acc_id)
601 {
602     if (PyCallable_Check(g_obj_callback->on_typing)) {
603         PyObject *param_call_id, *param_from, *param_to, *param_contact,
604                  *param_is_typing, *param_acc_id;
605
606         ENTER_PYTHON();
607
608         PJ_UNUSED_ARG(rdata);
609
610         PyObject_CallFunctionObjArgs(
611                 g_obj_callback->on_typing,
612                 param_call_id   = Py_BuildValue("i",call_id),
613                 param_from      = PyString_FromPJ(from),
614                 param_to        = PyString_FromPJ(to),
615                 param_contact   = PyString_FromPJ(contact),
616                 param_is_typing = Py_BuildValue("i",is_typing),
617                 param_acc_id    = Py_BuildValue("i",acc_id),
618                 NULL
619             );
620
621         Py_DECREF(param_call_id);
622         Py_DECREF(param_from);
623         Py_DECREF(param_to);
624         Py_DECREF(param_contact);
625         Py_DECREF(param_is_typing); 
626         Py_DECREF(param_acc_id);
627
628         LEAVE_PYTHON();
629     }
630 }
631
632
633 /*
634  * on_mwi_info
635  */
636 static void cb_on_mwi_info(pjsua_acc_id acc_id, pjsua_mwi_info *mwi_info)
637 {
638     if (PyCallable_Check(g_obj_callback->on_mwi_info)) {
639         PyObject *param_acc_id, *param_body;
640         pj_str_t body;
641
642         ENTER_PYTHON();
643
644         body.ptr = mwi_info->rdata->msg_info.msg->body->data;
645         body.slen = mwi_info->rdata->msg_info.msg->body->len;
646
647         PyObject_CallFunctionObjArgs(
648                 g_obj_callback->on_mwi_info,
649                 param_acc_id    = Py_BuildValue("i",acc_id),
650                 param_body      = PyString_FromPJ(&body),
651                 NULL
652             );
653
654         Py_DECREF(param_acc_id);
655         Py_DECREF(param_body);
656
657         LEAVE_PYTHON();
658     }
659 }
660
661 /* 
662  * translate_hdr
663  * internal function 
664  * translate from hdr_list to pjsip_generic_string_hdr
665  */
666 void translate_hdr(pj_pool_t *pool, pjsip_hdr *hdr, PyObject *py_hdr_list)
667 {
668     pj_list_init(hdr);
669
670     if (PyList_Check(py_hdr_list)) {
671         int i;
672
673         for (i=0; i<PyList_Size(py_hdr_list); ++i)  { 
674             pj_str_t hname, hvalue;
675             pjsip_generic_string_hdr * new_hdr;
676             PyObject * tuple = PyList_GetItem(py_hdr_list, i);
677
678             if (PyTuple_Check(tuple)) {
679                 if (PyTuple_Size(tuple) >= 1)
680                     hname = PyString_ToPJ(PyTuple_GetItem(tuple,0));
681                 else
682                     hname.slen = 0;
683                 if (PyTuple_Size(tuple) >= 2)
684                     hvalue = PyString_ToPJ(PyTuple_GetItem(tuple,1));
685                 else
686                     hvalue.slen = 0;
687             } else {
688                 hname.ptr = "";
689                 hname.slen = 0;
690                 hvalue.ptr = "";
691                 hvalue.slen = 0;
692             }  
693             new_hdr = pjsip_generic_string_hdr_create(pool, &hname, &hvalue);
694             pj_list_push_back((pj_list_type *)hdr, (pj_list_type *)new_hdr);
695         }     
696     }
697 }
698
699 /*
700  * py_pjsua_thread_register
701  */
702 static PyObject *py_pjsua_thread_register(PyObject *pSelf, PyObject *pArgs)
703 {
704     pj_status_t status; 
705     const char *name;
706     PyObject *py_desc;
707     pj_thread_t *thread;
708     struct py_thread_desc *thread_desc;
709
710     PJ_UNUSED_ARG(pSelf);
711
712     if (!PyArg_ParseTuple(pArgs, "sO", &name, &py_desc)) {
713          return NULL;
714     }
715     thread_desc = (struct py_thread_desc*)
716                   malloc(sizeof(struct py_thread_desc));
717     thread_desc->next = py_thread_desc;
718     py_thread_desc = thread_desc;
719
720     status = pj_thread_register(name, thread_desc->desc, &thread);
721
722     if (status == PJ_SUCCESS)
723         status = pj_thread_local_set(g_thread_id, (void*)1);
724
725     return Py_BuildValue("i",status);
726 }
727
728 /*
729  * py_pjsua_logging_config_default
730  */
731 static PyObject *py_pjsua_logging_config_default(PyObject *pSelf,
732                                                  PyObject *pArgs)
733 {
734     PyObj_pjsua_logging_config *obj;    
735     pjsua_logging_config cfg;
736
737     PJ_UNUSED_ARG(pSelf);
738     PJ_UNUSED_ARG(pArgs);
739
740     pjsua_logging_config_default(&cfg);
741     obj = (PyObj_pjsua_logging_config*) 
742           PyObj_pjsua_logging_config_new(&PyTyp_pjsua_logging_config, 
743                                          NULL, NULL);
744     PyObj_pjsua_logging_config_import(obj, &cfg);
745     
746     return (PyObject*)obj;
747 }
748
749
750 /*
751  * py_pjsua_config_default
752  */
753 static PyObject *py_pjsua_config_default(PyObject *pSelf, PyObject *pArgs)
754 {
755     PyObj_pjsua_config *obj;
756     pjsua_config cfg;
757
758     PJ_UNUSED_ARG(pSelf);
759     PJ_UNUSED_ARG(pArgs);
760
761     pjsua_config_default(&cfg);
762     obj = (PyObj_pjsua_config *) PyObj_pjsua_config_new(&PyTyp_pjsua_config, 
763                                                         NULL, NULL);
764     PyObj_pjsua_config_import(obj, &cfg);
765
766     return (PyObject*)obj;
767 }
768
769
770 /*
771  * py_pjsua_media_config_default
772  */
773 static PyObject * py_pjsua_media_config_default(PyObject *pSelf,
774                                                 PyObject *pArgs)
775 {
776     PyObj_pjsua_media_config *obj;
777     pjsua_media_config cfg;
778
779     PJ_UNUSED_ARG(pSelf);
780     PJ_UNUSED_ARG(pArgs);
781
782     pjsua_media_config_default(&cfg);
783     obj = (PyObj_pjsua_media_config *)
784           PyType_GenericNew(&PyTyp_pjsua_media_config, NULL, NULL);
785     PyObj_pjsua_media_config_import(obj, &cfg);
786
787     return (PyObject *)obj;
788 }
789
790
791 /*
792  * py_pjsua_msg_data_init
793  */
794 static PyObject *py_pjsua_msg_data_init(PyObject *pSelf, PyObject *pArgs)
795 {
796     PJ_UNUSED_ARG(pSelf);
797     PJ_UNUSED_ARG(pArgs);
798
799     return (PyObject *)PyObj_pjsua_msg_data_new(&PyTyp_pjsua_msg_data, 
800                                                 NULL, NULL);
801 }
802
803
804 /*
805  * py_pjsua_reconfigure_logging
806  */
807 static PyObject *py_pjsua_reconfigure_logging(PyObject *pSelf, 
808                                               PyObject *pArgs)
809 {
810     PyObject *logObj;
811     pj_status_t status;
812
813     PJ_UNUSED_ARG(pSelf);
814
815     if (!PyArg_ParseTuple(pArgs, "O", &logObj)) {
816         return NULL;
817     }
818
819     if (logObj != Py_None) {
820         PyObj_pjsua_logging_config *log;
821         pjsua_logging_config cfg;
822
823         log = (PyObj_pjsua_logging_config*)logObj;
824         cfg.msg_logging = log->msg_logging;
825         cfg.level = log->level;
826         cfg.console_level = log->console_level;
827         cfg.decor = log->decor;
828         cfg.log_filename = PyString_ToPJ(log->log_filename);
829         Py_XDECREF(g_obj_log_cb);
830         g_obj_log_cb = log->cb;
831         Py_INCREF(g_obj_log_cb);
832         cfg.cb = &cb_log_cb;
833         status = pjsua_reconfigure_logging(&cfg);
834     } else {
835         status = pjsua_reconfigure_logging(NULL);
836     }
837
838     return Py_BuildValue("i",status);
839 }
840
841
842 /*
843  * py_pjsua_perror
844  */
845 static PyObject *py_pjsua_perror(PyObject *pSelf, PyObject *pArgs)
846 {
847     const char *sender;
848     const char *title;
849     pj_status_t status;
850
851     PJ_UNUSED_ARG(pSelf);
852
853     if (!PyArg_ParseTuple(pArgs, "ssi", &sender, &title, &status)) {
854         return NULL;
855     }
856         
857     pjsua_perror(sender, title, status);
858
859     return Py_BuildValue("");
860 }
861
862
863 /*
864  * py_pjsua_create
865  */
866 static PyObject *py_pjsua_create(PyObject *pSelf, PyObject *pArgs)
867 {
868     pj_status_t status;
869
870     PJ_UNUSED_ARG(pSelf);
871     PJ_UNUSED_ARG(pArgs);
872
873     status = pjsua_create();
874     
875     if (status == PJ_SUCCESS)  {
876         status = pj_thread_local_alloc(&g_thread_id);
877         if (status == PJ_SUCCESS)
878             status = pj_thread_local_set(g_thread_id, (void*)1);
879
880         pj_atexit(&clear_py_thread_desc);
881     }
882
883     return Py_BuildValue("i",status);
884 }
885
886
887 /*
888  * py_pjsua_init
889  */
890 static PyObject *py_pjsua_init(PyObject *pSelf, PyObject *pArgs)
891 {
892     pj_status_t status;
893     PyObject *o_ua_cfg, *o_log_cfg, *o_media_cfg;
894     pjsua_config cfg_ua, *p_cfg_ua;
895     pjsua_logging_config cfg_log, *p_cfg_log;
896     pjsua_media_config cfg_media, *p_cfg_media;
897
898     PJ_UNUSED_ARG(pSelf);
899
900     if (!PyArg_ParseTuple(pArgs, "OOO", &o_ua_cfg, &o_log_cfg, &o_media_cfg)) {
901         return NULL;
902     }
903     
904     pjsua_config_default(&cfg_ua);
905     pjsua_logging_config_default(&cfg_log);
906     pjsua_media_config_default(&cfg_media);
907
908     if (o_ua_cfg != Py_None) {
909         PyObj_pjsua_config *obj_ua_cfg = (PyObj_pjsua_config*)o_ua_cfg;
910
911         PyObj_pjsua_config_export(&cfg_ua, obj_ua_cfg);
912
913         Py_XDECREF(g_obj_callback);
914         g_obj_callback = obj_ua_cfg->cb;
915         Py_INCREF(g_obj_callback);
916
917         cfg_ua.cb.on_call_state = &cb_on_call_state;
918         cfg_ua.cb.on_incoming_call = &cb_on_incoming_call;
919         cfg_ua.cb.on_call_media_state = &cb_on_call_media_state;
920         cfg_ua.cb.on_dtmf_digit = &cb_on_dtmf_digit;
921         cfg_ua.cb.on_call_transfer_request = &cb_on_call_transfer_request;
922         cfg_ua.cb.on_call_transfer_status = &cb_on_call_transfer_status;
923         cfg_ua.cb.on_call_replace_request = &cb_on_call_replace_request;
924         cfg_ua.cb.on_call_replaced = &cb_on_call_replaced;
925         cfg_ua.cb.on_reg_state = &cb_on_reg_state;
926         cfg_ua.cb.on_incoming_subscribe = &cb_on_incoming_subscribe;
927         cfg_ua.cb.on_buddy_state = &cb_on_buddy_state;
928         cfg_ua.cb.on_pager2 = &cb_on_pager;
929         cfg_ua.cb.on_pager_status2 = &cb_on_pager_status;
930         cfg_ua.cb.on_typing2 = &cb_on_typing;
931         cfg_ua.cb.on_mwi_info = &cb_on_mwi_info;
932
933         p_cfg_ua = &cfg_ua;
934
935     } else {
936         p_cfg_ua = NULL;
937     }
938
939     if (o_log_cfg != Py_None)  {
940         PyObj_pjsua_logging_config * obj_log;
941
942         obj_log = (PyObj_pjsua_logging_config *)o_log_cfg;
943         
944         PyObj_pjsua_logging_config_export(&cfg_log, obj_log);
945
946         Py_XDECREF(g_obj_log_cb);
947         g_obj_log_cb = obj_log->cb;
948         Py_INCREF(g_obj_log_cb);
949
950         cfg_log.cb = &cb_log_cb;
951         p_cfg_log = &cfg_log;
952
953     } else {
954         p_cfg_log = NULL;
955     }
956
957     if (o_media_cfg != Py_None) {
958         PyObj_pjsua_media_config_export(&cfg_media, 
959                                         (PyObj_pjsua_media_config*)o_media_cfg);
960         p_cfg_media = &cfg_media;
961
962     } else {
963         p_cfg_media = NULL;
964     }
965
966     status = pjsua_init(p_cfg_ua, p_cfg_log, p_cfg_media);
967
968     return Py_BuildValue("i", status);
969 }
970
971
972 /*
973  * py_pjsua_start
974  */
975 static PyObject *py_pjsua_start(PyObject *pSelf, PyObject *pArgs)
976 {
977     pj_status_t status;
978
979     PJ_UNUSED_ARG(pSelf);
980     PJ_UNUSED_ARG(pArgs);
981
982     status = pjsua_start();
983     
984     return Py_BuildValue("i", status);
985 }
986
987
988 /*
989  * py_pjsua_destroy
990  */
991 static PyObject *py_pjsua_destroy(PyObject *pSelf, PyObject *pArgs)
992 {
993     pj_status_t status;
994
995     PJ_UNUSED_ARG(pSelf);
996     PJ_UNUSED_ARG(pArgs);
997
998     status = pjsua_destroy();
999     
1000     return Py_BuildValue("i", status);
1001 }
1002
1003
1004 /*
1005  * py_pjsua_handle_events
1006  */
1007 static PyObject *py_pjsua_handle_events(PyObject *pSelf, PyObject *pArgs)
1008 {
1009     int ret;
1010     int msec;
1011
1012     PJ_UNUSED_ARG(pSelf);
1013
1014     if (!PyArg_ParseTuple(pArgs, "i", &msec)) {
1015         return NULL;
1016     }
1017
1018     if (msec < 0)
1019         msec = 0;
1020
1021 #if !NO_PJSIP_THREAD
1022     /* Since handle_events() will block, we must wrap it with ALLOW_THREADS
1023      * construct, or otherwise many Python blocking functions (such as
1024      * time.sleep(), readline(), etc.) may hang/block indefinitely.
1025      * See http://www.python.org/doc/current/api/threads.html for more info.
1026      */
1027     Py_BEGIN_ALLOW_THREADS
1028 #endif
1029
1030     ret = pjsua_handle_events(msec);
1031
1032 #if !NO_PJSIP_THREAD
1033     Py_END_ALLOW_THREADS
1034 #endif
1035     
1036     return Py_BuildValue("i", ret);
1037 }
1038
1039
1040 /*
1041  * py_pjsua_verify_sip_url
1042  */
1043 static PyObject *py_pjsua_verify_sip_url(PyObject *pSelf, PyObject *pArgs)
1044 {
1045     pj_status_t status;
1046     const char *url;
1047
1048     PJ_UNUSED_ARG(pSelf);
1049
1050     if (!PyArg_ParseTuple(pArgs, "s", &url)) {
1051         return NULL;
1052     }
1053
1054     status = pjsua_verify_sip_url(url);
1055     
1056     return Py_BuildValue("i", status);
1057 }
1058
1059
1060 /*
1061  * function doc
1062  */
1063
1064 static char pjsua_thread_register_doc[] =
1065     "int _pjsua.thread_register(string name, int[] desc)";
1066 static char pjsua_perror_doc[] =
1067     "void _pjsua.perror (string sender, string title, int status) "
1068     "Display error message for the specified error code. Parameters: "
1069     "sender: The log sender field;  "
1070     "title: Message title for the error; "
1071     "status: Status code.";
1072
1073 static char pjsua_create_doc[] =
1074     "int _pjsua.create (void) "
1075     "Instantiate pjsua application. Application "
1076     "must call this function before calling any other functions, to make sure "
1077     "that the underlying libraries are properly initialized. Once this "
1078     "function has returned success, application must call pjsua_destroy() "
1079     "before quitting.";
1080
1081 static char pjsua_init_doc[] =
1082     "int _pjsua.init (_pjsua.Config obj_ua_cfg, "
1083         "_pjsua.Logging_Config log_cfg, _pjsua.Media_Config media_cfg) "
1084     "Initialize pjsua with the specified settings. All the settings are "
1085     "optional, and the default values will be used when the config is not "
1086     "specified. Parameters: "
1087     "obj_ua_cfg : User agent configuration;  "
1088     "log_cfg : Optional logging configuration; "
1089     "media_cfg : Optional media configuration.";
1090
1091 static char pjsua_start_doc[] =
1092     "int _pjsua.start (void) "
1093     "Application is recommended to call this function after all "
1094     "initialization is done, so that the library can do additional checking "
1095     "set up additional";
1096
1097 static char pjsua_destroy_doc[] =
1098     "int _pjsua.destroy (void) "
1099     "Destroy pjsua This function must be called once PJSUA is created. To "
1100     "make it easier for application, application may call this function "
1101     "several times with no danger.";
1102
1103 static char pjsua_handle_events_doc[] =
1104     "int _pjsua.handle_events (int msec_timeout) "
1105     "Poll pjsua for events, and if necessary block the caller thread for the "
1106     "specified maximum interval (in miliseconds) Parameters: "
1107     "msec_timeout: Maximum time to wait, in miliseconds. "
1108     "Returns: The number of events that have been handled during the poll. "
1109     "Negative value indicates error, and application can retrieve the error "
1110     "as (err = -return_value).";
1111
1112 static char pjsua_verify_sip_url_doc[] =
1113     "int _pjsua.verify_sip_url (string c_url) "
1114     "Verify that valid SIP url is given Parameters: "
1115     "c_url: The URL, as NULL terminated string.";
1116
1117 static char pjsua_reconfigure_logging_doc[] =
1118     "int _pjsua.reconfigure_logging (_pjsua.Logging_Config c) "
1119     "Application can call this function at any time (after pjsua_create(), of "
1120     "course) to change logging settings. Parameters: "
1121     "c: Logging configuration.";
1122
1123 static char pjsua_logging_config_default_doc[] =
1124     "_pjsua.Logging_Config _pjsua.logging_config_default  ()  "
1125     "Use this function to initialize logging config.";
1126
1127 static char pjsua_config_default_doc[] =
1128     "_pjsua.Config _pjsua.config_default (). Use this function to "
1129     "initialize pjsua config. ";
1130
1131 static char pjsua_media_config_default_doc[] =
1132     "_pjsua.Media_Config _pjsua.media_config_default (). "
1133     "Use this function to initialize media config.";
1134
1135 static char pjsua_msg_data_init_doc[] =
1136     "_pjsua.Msg_Data void _pjsua.msg_data_init () "
1137     "Initialize message data ";
1138         
1139
1140 /* END OF LIB BASE */
1141
1142 /* LIB TRANSPORT */
1143
1144 /*
1145  * py_pjsua_transport_config_default
1146  */
1147 static PyObject *py_pjsua_transport_config_default(PyObject *pSelf, 
1148                                                    PyObject *pArgs)
1149 {
1150     PyObj_pjsua_transport_config *obj;
1151     pjsua_transport_config cfg;
1152
1153     PJ_UNUSED_ARG(pSelf);
1154     PJ_UNUSED_ARG(pArgs);
1155
1156     pjsua_transport_config_default(&cfg);
1157     obj = (PyObj_pjsua_transport_config*)
1158           PyObj_pjsua_transport_config_new(&PyTyp_pjsua_transport_config,
1159                                            NULL, NULL);
1160     PyObj_pjsua_transport_config_import(obj, &cfg);
1161
1162     return (PyObject *)obj;
1163 }
1164
1165 /*
1166  * py_pjsua_transport_create
1167  */
1168 static PyObject *py_pjsua_transport_create(PyObject *pSelf, PyObject *pArgs)
1169 {
1170     pj_status_t status;
1171     int type;
1172     PyObject *pCfg;
1173     pjsua_transport_config cfg;
1174     pjsua_transport_id id;
1175
1176     PJ_UNUSED_ARG(pSelf);
1177
1178     if (!PyArg_ParseTuple(pArgs, "iO", &type, &pCfg)) {
1179         return NULL;
1180     }
1181
1182     if (pCfg != Py_None) {
1183         PyObj_pjsua_transport_config *obj;
1184
1185         obj = (PyObj_pjsua_transport_config*)pCfg;
1186         PyObj_pjsua_transport_config_export(&cfg, obj);
1187         status = pjsua_transport_create(type, &cfg, &id);
1188     } else {
1189         status = pjsua_transport_create(type, NULL, &id);
1190     }
1191     
1192     
1193     return Py_BuildValue("ii", status, id);
1194 }
1195
1196 /*
1197  * py_pjsua_enum_transports
1198  */
1199 static PyObject *py_pjsua_enum_transports(PyObject *pSelf, PyObject *pArgs)
1200 {
1201     pj_status_t status;
1202     PyObject *list;
1203     pjsua_transport_id id[PJSIP_MAX_TRANSPORTS];
1204     unsigned c, i;
1205
1206     PJ_UNUSED_ARG(pSelf);
1207     PJ_UNUSED_ARG(pArgs);
1208
1209     c = PJ_ARRAY_SIZE(id);
1210     status = pjsua_enum_transports(id, &c);
1211     
1212     list = PyList_New(c);
1213     for (i = 0; i < c; i++) {     
1214         PyList_SetItem(list, i, Py_BuildValue("i", id[i]));
1215     }
1216     
1217     return (PyObject*)list;
1218 }
1219
1220 /*
1221  * py_pjsua_transport_get_info
1222  * !modified @ 051206
1223  */
1224 static PyObject *py_pjsua_transport_get_info(PyObject *pSelf, PyObject *pArgs)
1225 {
1226     pj_status_t status;
1227     int id;
1228     pjsua_transport_info info;
1229     
1230     PJ_UNUSED_ARG(pSelf);
1231
1232     if (!PyArg_ParseTuple(pArgs, "i", &id)) {
1233         return NULL;
1234     }   
1235     
1236     status = pjsua_transport_get_info(id, &info);       
1237     if (status == PJ_SUCCESS) {
1238         PyObj_pjsua_transport_info *obj;
1239         obj = (PyObj_pjsua_transport_info *) 
1240               PyObj_pjsua_transport_info_new(&PyTyp_pjsua_transport_info, 
1241                                              NULL, NULL);
1242         PyObj_pjsua_transport_info_import(obj, &info);
1243         return (PyObject*)obj;
1244     } else {
1245         return Py_BuildValue("");
1246     }
1247 }
1248
1249 /*
1250  * py_pjsua_transport_set_enable
1251  */
1252 static PyObject *py_pjsua_transport_set_enable(PyObject *pSelf, 
1253                                                PyObject *pArgs)
1254 {
1255     pj_status_t status;
1256     int id;
1257     int enabled;
1258
1259     PJ_UNUSED_ARG(pSelf);
1260
1261     if (!PyArg_ParseTuple(pArgs, "ii", &id, &enabled)) {
1262         return NULL;
1263     }
1264     status = pjsua_transport_set_enable(id, enabled);
1265
1266     return Py_BuildValue("i", status);
1267 }
1268
1269 /*
1270  * py_pjsua_transport_close
1271  */
1272 static PyObject *py_pjsua_transport_close(PyObject *pSelf, PyObject *pArgs)
1273 {
1274     pj_status_t status;
1275     int id;
1276     int force;
1277
1278     PJ_UNUSED_ARG(pSelf);
1279
1280     if (!PyArg_ParseTuple(pArgs, "ii", &id, &force)) {
1281         return NULL;
1282     }   
1283     status = pjsua_transport_close(id, force);  
1284     
1285     return Py_BuildValue("i", status);
1286 }
1287
1288 static char pjsua_transport_config_default_doc[] =
1289     "_pjsua.Transport_Config _pjsua.transport_config_default () "
1290     "Call this function to initialize UDP config with default values.";
1291 static char pjsua_transport_create_doc[] =
1292     "int, int _pjsua.transport_create (int type, "
1293     "_pjsua.Transport_Config cfg) "
1294     "Create SIP transport.";
1295 static char pjsua_enum_transports_doc[] =
1296     "int[] _pjsua.enum_transports () "
1297     "Enumerate all transports currently created in the system.";
1298 static char pjsua_transport_get_info_doc[] =
1299     "void _pjsua.transport_get_info "
1300     "(_pjsua.Transport_ID id, _pjsua.Transport_Info info) "
1301     "Get information about transports.";
1302 static char pjsua_transport_set_enable_doc[] =
1303     "void _pjsua.transport_set_enable "
1304     "(_pjsua.Transport_ID id, int enabled) "
1305     "Disable a transport or re-enable it. "
1306     "By default transport is always enabled after it is created. "
1307     "Disabling a transport does not necessarily close the socket, "
1308     "it will only discard incoming messages and prevent the transport "
1309     "from being used to send outgoing messages.";
1310 static char pjsua_transport_close_doc[] =
1311     "void _pjsua.transport_close (_pjsua.Transport_ID id, int force) "
1312     "Close the transport. If transport is forcefully closed, "
1313     "it will be immediately closed, and any pending transactions "
1314     "that are using the transport may not terminate properly. "
1315     "Otherwise, the system will wait until all transactions are closed "
1316     "while preventing new users from using the transport, and will close "
1317     "the transport when it is safe to do so.";
1318
1319 /* END OF LIB TRANSPORT */
1320
1321 /* LIB ACCOUNT */
1322
1323
1324 /*
1325  * py_pjsua_acc_config_default
1326  */
1327 static PyObject *py_pjsua_acc_config_default(PyObject *pSelf, PyObject *pArgs)
1328 {
1329     PyObj_pjsua_acc_config *obj;
1330     pjsua_acc_config cfg;
1331
1332     PJ_UNUSED_ARG(pSelf);
1333     PJ_UNUSED_ARG(pArgs);
1334
1335     if (!PyArg_ParseTuple(pArgs, "")) {
1336         return NULL;
1337     }
1338
1339     pjsua_acc_config_default(&cfg);
1340     obj = (PyObj_pjsua_acc_config *)
1341           PyObj_pjsua_acc_config_new(&PyTyp_pjsua_acc_config, 
1342                                      NULL, NULL);
1343     PyObj_pjsua_acc_config_import(obj, &cfg);
1344     return (PyObject *)obj;
1345 }
1346
1347 /*
1348  * py_pjsua_acc_get_count
1349  */
1350 static PyObject *py_pjsua_acc_get_count(PyObject *pSelf, PyObject *pArgs)
1351 {
1352     int count;
1353
1354     PJ_UNUSED_ARG(pSelf);
1355     PJ_UNUSED_ARG(pArgs);
1356
1357     count = pjsua_acc_get_count();
1358     return Py_BuildValue("i", count);
1359 }
1360
1361 /*
1362  * py_pjsua_acc_is_valid
1363  */
1364 static PyObject *py_pjsua_acc_is_valid(PyObject *pSelf, PyObject *pArgs)
1365 {    
1366     int id;
1367     int is_valid;
1368
1369     PJ_UNUSED_ARG(pSelf);
1370
1371     if (!PyArg_ParseTuple(pArgs, "i", &id)) {
1372         return NULL;
1373     }
1374
1375     is_valid = pjsua_acc_is_valid(id);  
1376     return Py_BuildValue("i", is_valid);
1377 }
1378
1379 /*
1380  * py_pjsua_acc_set_default
1381  */
1382 static PyObject *py_pjsua_acc_set_default(PyObject *pSelf, PyObject *pArgs)
1383 {    
1384     int id;
1385     int status;
1386
1387     PJ_UNUSED_ARG(pSelf);
1388
1389     if (!PyArg_ParseTuple(pArgs, "i", &id)) {
1390         return NULL;
1391     }
1392     status = pjsua_acc_set_default(id);
1393         
1394     return Py_BuildValue("i", status);
1395 }
1396
1397 /*
1398  * py_pjsua_acc_get_default
1399  */
1400 static PyObject *py_pjsua_acc_get_default(PyObject *pSelf, PyObject *pArgs)
1401 {    
1402     int id;
1403         
1404     PJ_UNUSED_ARG(pSelf);
1405     PJ_UNUSED_ARG(pArgs);
1406
1407     id = pjsua_acc_get_default();
1408         
1409     return Py_BuildValue("i", id);
1410 }
1411
1412 /*
1413  * py_pjsua_acc_add
1414  */
1415 static PyObject *py_pjsua_acc_add(PyObject *pSelf, PyObject *pArgs)
1416 {    
1417     int is_default;
1418     PyObject *pCfg;
1419     int acc_id;
1420     int status;
1421
1422     PJ_UNUSED_ARG(pSelf);
1423
1424     if (!PyArg_ParseTuple(pArgs, "Oi", &pCfg, &is_default)) {
1425         return NULL;
1426     }
1427     
1428     if (pCfg != Py_None) {
1429         pjsua_acc_config cfg;
1430         PyObj_pjsua_acc_config *ac;
1431
1432         pjsua_acc_config_default(&cfg);
1433         ac = (PyObj_pjsua_acc_config *)pCfg;
1434         PyObj_pjsua_acc_config_export(&cfg, ac);
1435         status = pjsua_acc_add(&cfg, is_default, &acc_id);
1436     } else {
1437         status = PJ_EINVAL;
1438         acc_id = PJSUA_INVALID_ID;
1439     }
1440     
1441     return Py_BuildValue("ii", status, acc_id);
1442 }
1443
1444 /*
1445  * py_pjsua_acc_add_local
1446  */
1447 static PyObject *py_pjsua_acc_add_local(PyObject *pSelf, PyObject *pArgs)
1448 {    
1449     int is_default;
1450     int tid;
1451     int acc_id;
1452     int status;
1453         
1454     PJ_UNUSED_ARG(pSelf);
1455
1456     if (!PyArg_ParseTuple(pArgs, "ii", &tid, &is_default)) {
1457         return NULL;
1458     }
1459         
1460     status = pjsua_acc_add_local(tid, is_default, &acc_id);
1461     
1462     return Py_BuildValue("ii", status, acc_id);
1463 }
1464
1465 /*
1466  * py_pjsua_acc_set_user_data
1467  */
1468 static PyObject *py_pjsua_acc_set_user_data(PyObject *pSelf, PyObject *pArgs)
1469 {    
1470     int acc_id;
1471     PyObject *pUserData, *old_user_data;
1472     int status;
1473
1474     PJ_UNUSED_ARG(pSelf);
1475
1476     if (!PyArg_ParseTuple(pArgs, "iO", &acc_id, &pUserData)) {
1477         return NULL;
1478     }
1479
1480     old_user_data = (PyObject*) pjsua_acc_get_user_data(acc_id);
1481
1482     status = pjsua_acc_set_user_data(acc_id, (void*)pUserData);
1483
1484     if (status == PJ_SUCCESS) {
1485         Py_XINCREF(pUserData);
1486         Py_XDECREF(old_user_data);
1487     }
1488
1489     return Py_BuildValue("i", status);
1490 }
1491
1492 /*
1493  * py_pjsua_acc_get_user_data
1494  */
1495 static PyObject *py_pjsua_acc_get_user_data(PyObject *pSelf, PyObject *pArgs)
1496 {    
1497     int acc_id;
1498     PyObject *user_data;
1499
1500     PJ_UNUSED_ARG(pSelf);
1501
1502     if (!PyArg_ParseTuple(pArgs, "i", &acc_id)) {
1503         return NULL;
1504     }
1505
1506     user_data = (PyObject*) pjsua_acc_get_user_data(acc_id);
1507
1508     return user_data ? Py_BuildValue("O", user_data) : Py_BuildValue("");
1509 }
1510
1511 /*
1512  * py_pjsua_acc_del
1513  */
1514 static PyObject *py_pjsua_acc_del(PyObject *pSelf, PyObject *pArgs)
1515 {    
1516     int acc_id;
1517     PyObject *user_data;
1518     int status;
1519
1520     PJ_UNUSED_ARG(pSelf);
1521
1522     if (!PyArg_ParseTuple(pArgs, "i", &acc_id)) {
1523         return NULL;
1524     }
1525
1526     user_data = (PyObject*) pjsua_acc_get_user_data(acc_id);
1527     Py_XDECREF(user_data);
1528
1529     status = pjsua_acc_del(acc_id);
1530
1531     return Py_BuildValue("i", status);
1532 }
1533
1534 /*
1535  * py_pjsua_acc_modify
1536  */
1537 static PyObject *py_pjsua_acc_modify(PyObject *pSelf, PyObject *pArgs)
1538 {       
1539     PyObject *pCfg;
1540     PyObj_pjsua_acc_config * ac;
1541     int acc_id;
1542     int status;
1543
1544     PJ_UNUSED_ARG(pSelf);
1545
1546     if (!PyArg_ParseTuple(pArgs, "iO", &acc_id, &pCfg)) {
1547         return NULL;
1548     }
1549
1550     if (pCfg != Py_None) {
1551         pjsua_acc_config cfg;   
1552
1553         pjsua_acc_config_default(&cfg);
1554         ac = (PyObj_pjsua_acc_config*)pCfg;
1555         PyObj_pjsua_acc_config_export(&cfg, ac);
1556
1557         status = pjsua_acc_modify(acc_id, &cfg);
1558     } else {
1559         status = PJ_EINVAL;
1560     }
1561     return Py_BuildValue("i", status);
1562 }
1563
1564 /*
1565  * py_pjsua_acc_set_online_status
1566  */
1567 static PyObject *py_pjsua_acc_set_online_status(PyObject *pSelf, 
1568                                                 PyObject *pArgs)
1569 {    
1570     int is_online;      
1571     int acc_id;
1572     int status; 
1573
1574     PJ_UNUSED_ARG(pSelf);
1575
1576     if (!PyArg_ParseTuple(pArgs, "ii", &acc_id, &is_online)) {
1577         return NULL;
1578     }
1579         
1580     status = pjsua_acc_set_online_status(acc_id, is_online);
1581         
1582     return Py_BuildValue("i", status);
1583 }
1584
1585 /*
1586  * py_pjsua_acc_set_online_status2
1587  */
1588 static PyObject *py_pjsua_acc_set_online_status2(PyObject *pSelf, 
1589                                                  PyObject *pArgs)
1590 {    
1591     int is_online;      
1592     int acc_id;
1593     int activity_id;
1594     const char *activity_text = NULL;
1595     const char *rpid_id = NULL;
1596     pjrpid_element rpid;
1597     pj_status_t status; 
1598
1599     PJ_UNUSED_ARG(pSelf);
1600
1601     if (!PyArg_ParseTuple(pArgs, "iiiss", &acc_id, &is_online,
1602                           &activity_id, &activity_text, &rpid_id)) 
1603     {
1604         return NULL;
1605     }
1606
1607     pj_bzero(&rpid, sizeof(rpid));
1608     rpid.type = PJRPID_ELEMENT_TYPE_PERSON;
1609     rpid.activity = activity_id;
1610     if (activity_text)
1611         rpid.note = pj_str((char*)activity_text);
1612
1613     if (rpid_id)
1614         rpid.id = pj_str((char*)rpid_id);
1615
1616     status = pjsua_acc_set_online_status2(acc_id, is_online, &rpid);
1617
1618     return Py_BuildValue("i", status);
1619 }
1620
1621 /*
1622  * py_pjsua_acc_set_registration
1623  */
1624 static PyObject *py_pjsua_acc_set_registration(PyObject *pSelf, 
1625                                                PyObject *pArgs)
1626 {    
1627     int renew;  
1628     int acc_id;
1629     int status; 
1630
1631     PJ_UNUSED_ARG(pSelf);
1632
1633     if (!PyArg_ParseTuple(pArgs, "ii", &acc_id, &renew)) {
1634         return NULL;
1635     }
1636         
1637     status = pjsua_acc_set_registration(acc_id, renew);
1638         
1639     return Py_BuildValue("i", status);
1640 }
1641
1642 /*
1643  * py_pjsua_acc_get_info
1644  */
1645 static PyObject *py_pjsua_acc_get_info(PyObject *pSelf, PyObject *pArgs)
1646 {       
1647     int acc_id;
1648     PyObj_pjsua_acc_info * obj;
1649     pjsua_acc_info info;
1650     int status; 
1651
1652     PJ_UNUSED_ARG(pSelf);
1653
1654     if (!PyArg_ParseTuple(pArgs, "i", &acc_id)) {
1655         return NULL;
1656     }
1657     
1658     status = pjsua_acc_get_info(acc_id, &info);
1659     if (status == PJ_SUCCESS) {
1660         obj = (PyObj_pjsua_acc_info*)
1661               PyObj_pjsua_acc_info_new(&PyTyp_pjsua_acc_info, NULL, NULL);
1662         PyObj_pjsua_acc_info_import(obj, &info);
1663         return (PyObject*)obj;
1664     } else {
1665         return Py_BuildValue("");
1666     }
1667 }
1668
1669 /*
1670  * py_pjsua_enum_accs
1671  */
1672 static PyObject *py_pjsua_enum_accs(PyObject *pSelf, PyObject *pArgs)
1673 {
1674     pj_status_t status;
1675     PyObject *list;
1676     pjsua_acc_id id[PJSUA_MAX_ACC];
1677     unsigned c, i;
1678
1679     PJ_UNUSED_ARG(pSelf);
1680     PJ_UNUSED_ARG(pArgs);
1681
1682     c = PJ_ARRAY_SIZE(id);
1683     status = pjsua_enum_accs(id, &c);
1684     if (status != PJ_SUCCESS)
1685         c = 0;
1686     
1687     list = PyList_New(c);
1688     for (i = 0; i < c; i++) {
1689         PyList_SetItem(list, i, Py_BuildValue("i", id[i]));
1690     }
1691     
1692     return (PyObject*)list;
1693 }
1694
1695 /*
1696  * py_pjsua_acc_enum_info
1697  */
1698 static PyObject *py_pjsua_acc_enum_info(PyObject *pSelf, PyObject *pArgs)
1699 {
1700     pj_status_t status;
1701     PyObject *list;
1702     pjsua_acc_info info[PJSUA_MAX_ACC];
1703     unsigned c, i;
1704
1705     PJ_UNUSED_ARG(pSelf);
1706     PJ_UNUSED_ARG(pArgs);
1707
1708     if (!PyArg_ParseTuple(pArgs, "")) {
1709         return NULL;
1710     }   
1711     
1712     c = PJ_ARRAY_SIZE(info);
1713     status = pjsua_acc_enum_info(info, &c);
1714     if (status != PJ_SUCCESS)
1715         c = 0;
1716
1717     list = PyList_New(c);
1718     for (i = 0; i < c; i++) {
1719         PyObj_pjsua_acc_info *obj;
1720         obj = (PyObj_pjsua_acc_info *)
1721               PyObj_pjsua_acc_info_new(&PyTyp_pjsua_acc_info, NULL, NULL);
1722
1723         PyObj_pjsua_acc_info_import(obj, &info[i]);
1724
1725         PyList_SetItem(list, i, (PyObject*)obj);
1726     }
1727     
1728     return (PyObject*)list;
1729 }
1730
1731 /*
1732  * py_pjsua_acc_set_transport
1733  */
1734 static PyObject *py_pjsua_acc_set_transport(PyObject *pSelf, PyObject *pArgs)
1735 {       
1736     int acc_id, transport_id;
1737     int status; 
1738     
1739     PJ_UNUSED_ARG(pSelf);
1740
1741     if (!PyArg_ParseTuple(pArgs, "ii", &acc_id, &transport_id)) {
1742         return NULL;
1743     }   
1744     
1745     status = pjsua_acc_set_transport(acc_id, transport_id);
1746     
1747     
1748     return Py_BuildValue("i", status);
1749 }
1750
1751
1752 /*
1753  * py_pjsua_acc_pres_notify
1754  */
1755 static PyObject *py_pjsua_acc_pres_notify(PyObject *pSelf, 
1756                                           PyObject *pArgs)
1757 {
1758     int acc_id, state;
1759     PyObject *arg_pres, *arg_msg_data, *arg_reason;
1760     void *srv_pres;
1761     pjsua_msg_data msg_data;
1762     pj_str_t reason;
1763     pj_bool_t with_body;
1764     pj_pool_t *pool = NULL;
1765     int status; 
1766     
1767     PJ_UNUSED_ARG(pSelf);
1768
1769     if (!PyArg_ParseTuple(pArgs, "iOiOO", &acc_id, &arg_pres, 
1770                           &state, &arg_reason, &arg_msg_data))
1771     {
1772         return NULL;
1773     }   
1774     
1775     srv_pres = (void*) PyLong_AsLong(arg_pres);
1776     with_body = (state != PJSIP_EVSUB_STATE_TERMINATED);
1777
1778     if (arg_reason && PyString_Check(arg_reason)) {
1779         reason = PyString_ToPJ(arg_reason);
1780     } else {
1781         reason = pj_str("");
1782     }
1783
1784     pjsua_msg_data_init(&msg_data);
1785     if (arg_msg_data && arg_msg_data != Py_None) {
1786         PyObj_pjsua_msg_data *omd = (PyObj_pjsua_msg_data *)arg_msg_data;
1787         msg_data.content_type = PyString_ToPJ(omd->content_type);
1788         msg_data.msg_body = PyString_ToPJ(omd->msg_body);
1789         pool = pjsua_pool_create("pytmp", POOL_SIZE, POOL_SIZE);
1790         translate_hdr(pool, &msg_data.hdr_list, omd->hdr_list);
1791     }
1792
1793     status = pjsua_pres_notify(acc_id, (pjsua_srv_pres*)srv_pres,
1794                                (pjsip_evsub_state)state, NULL,
1795                                &reason, with_body, &msg_data);
1796     
1797     if (pool) {
1798         pj_pool_release(pool);
1799     }
1800
1801     return Py_BuildValue("i", status);
1802 }
1803
1804 static char pjsua_acc_config_default_doc[] =
1805     "_pjsua.Acc_Config _pjsua.acc_config_default () "
1806     "Call this function to initialize account config with default values.";
1807 static char pjsua_acc_get_count_doc[] =
1808     "int _pjsua.acc_get_count () "
1809     "Get number of current accounts.";
1810 static char pjsua_acc_is_valid_doc[] =
1811     "int _pjsua.acc_is_valid (int acc_id)  "
1812     "Check if the specified account ID is valid.";
1813 static char pjsua_acc_set_default_doc[] =
1814     "int _pjsua.acc_set_default (int acc_id) "
1815     "Set default account to be used when incoming "
1816     "and outgoing requests doesn't match any accounts.";
1817 static char pjsua_acc_get_default_doc[] =
1818     "int _pjsua.acc_get_default () "
1819     "Get default account.";
1820 static char pjsua_acc_add_doc[] =
1821     "int, int _pjsua.acc_add (_pjsua.Acc_Config cfg, "
1822     "int is_default) "
1823     "Add a new account to pjsua. PJSUA must have been initialized "
1824     "(with pjsua_init()) before calling this function.";
1825 static char pjsua_acc_add_local_doc[] =
1826     "int,int _pjsua.acc_add_local (int tid, "
1827     "int is_default) "
1828     "Add a local account. A local account is used to identify "
1829     "local endpoint instead of a specific user, and for this reason, "
1830     "a transport ID is needed to obtain the local address information.";
1831 static char pjsua_acc_del_doc[] =
1832     "int _pjsua.acc_del (int acc_id) "
1833     "Delete account.";
1834 static char pjsua_acc_modify_doc[] =
1835     "int _pjsua.acc_modify (int acc_id, _pjsua.Acc_Config cfg) "
1836     "Modify account information.";
1837 static char pjsua_acc_set_online_status_doc[] =
1838     "int _pjsua.acc_set_online_status2(int acc_id, int is_online) "
1839     "Modify account's presence status to be advertised "
1840     "to remote/presence subscribers.";
1841 static char pjsua_acc_set_online_status2_doc[] =
1842     "int _pjsua.acc_set_online_status (int acc_id, int is_online, "
1843                                          "int activity_id, string activity_text) "
1844     "Modify account's presence status to be advertised "
1845     "to remote/presence subscribers.";
1846 static char pjsua_acc_set_registration_doc[] =
1847     "int _pjsua.acc_set_registration (int acc_id, int renew) "
1848     "Update registration or perform unregistration.";
1849 static char pjsua_acc_get_info_doc[] =
1850     "_pjsua.Acc_Info _pjsua.acc_get_info (int acc_id) "
1851     "Get account information.";
1852 static char pjsua_enum_accs_doc[] =
1853     "int[] _pjsua.enum_accs () "
1854     "Enum accounts all account ids.";
1855 static char pjsua_acc_enum_info_doc[] =
1856     "_pjsua.Acc_Info[] _pjsua.acc_enum_info () "
1857     "Enum accounts info.";
1858
1859 /* END OF LIB ACCOUNT */
1860
1861 /* LIB BUDDY */
1862
1863
1864
1865 /*
1866  * py_pjsua_buddy_config_default
1867  */
1868 static PyObject *py_pjsua_buddy_config_default(PyObject *pSelf, 
1869                                                PyObject *pArgs)
1870 {    
1871     PyObj_pjsua_buddy_config *obj;      
1872     pjsua_buddy_config cfg;
1873
1874     PJ_UNUSED_ARG(pSelf);
1875     PJ_UNUSED_ARG(pArgs);
1876
1877     pjsua_buddy_config_default(&cfg);
1878     obj = (PyObj_pjsua_buddy_config *) 
1879           PyObj_pjsua_buddy_config_new(&PyTyp_pjsua_buddy_config, NULL, NULL);
1880     PyObj_pjsua_buddy_config_import(obj, &cfg);
1881     
1882     return (PyObject *)obj;
1883 }
1884
1885 /*
1886  * py_pjsua_get_buddy_count
1887  */
1888 static PyObject *py_pjsua_get_buddy_count(PyObject *pSelf, PyObject *pArgs)
1889 {    
1890     PJ_UNUSED_ARG(pSelf);
1891     PJ_UNUSED_ARG(pArgs);
1892
1893     return Py_BuildValue("i", pjsua_get_buddy_count());
1894 }
1895
1896 /*
1897  * py_pjsua_buddy_is_valid
1898  */
1899 static PyObject *py_pjsua_buddy_is_valid(PyObject *pSelf, PyObject *pArgs)
1900 {    
1901     int id;
1902     int is_valid;
1903
1904     PJ_UNUSED_ARG(pSelf);
1905
1906     if (!PyArg_ParseTuple(pArgs, "i", &id)) {
1907         return NULL;
1908     }
1909     is_valid = pjsua_buddy_is_valid(id);
1910         
1911     return Py_BuildValue("i", is_valid);
1912 }
1913
1914 /*
1915  * py_pjsua_enum_buddies
1916  */
1917 static PyObject *py_pjsua_enum_buddies(PyObject *pSelf, PyObject *pArgs)
1918 {
1919     pj_status_t status;
1920     PyObject *list;
1921     pjsua_buddy_id id[PJSUA_MAX_BUDDIES];
1922     unsigned c, i;
1923
1924     PJ_UNUSED_ARG(pSelf);
1925     PJ_UNUSED_ARG(pArgs);
1926
1927     c = PJ_ARRAY_SIZE(id);
1928     status = pjsua_enum_buddies(id, &c);
1929     if (status != PJ_SUCCESS)
1930         c = 0;
1931
1932     list = PyList_New(c);
1933     for (i = 0; i < c; i++) {
1934         PyList_SetItem(list, i, Py_BuildValue("i", id[i]));
1935     }
1936     
1937     return (PyObject*)list;
1938 }
1939
1940 /*
1941  * py_pjsua_buddy_find
1942  */
1943 static PyObject *py_pjsua_buddy_find(PyObject *pSelf, PyObject *pArgs)
1944 {    
1945     PyObject *pURI;
1946     pj_str_t uri;
1947     pjsua_buddy_id buddy_id;
1948
1949     PJ_UNUSED_ARG(pSelf);
1950
1951     if (!PyArg_ParseTuple(pArgs, "O", &pURI)) {
1952         return NULL;
1953     }
1954
1955     if (!PyString_Check(pURI))
1956         return Py_BuildValue("i", PJSUA_INVALID_ID);
1957
1958     uri = PyString_ToPJ(pURI);
1959     buddy_id = pjsua_buddy_find(&uri);
1960
1961     return Py_BuildValue("i", buddy_id);
1962 }
1963
1964 /*
1965  * py_pjsua_buddy_get_info
1966  */
1967 static PyObject *py_pjsua_buddy_get_info(PyObject *pSelf, PyObject *pArgs)
1968 {       
1969     int buddy_id;
1970     pjsua_buddy_info info;
1971     int status; 
1972
1973     PJ_UNUSED_ARG(pSelf);
1974
1975     if (!PyArg_ParseTuple(pArgs, "i", &buddy_id)) {
1976         return NULL;
1977     }
1978
1979     status = pjsua_buddy_get_info(buddy_id, &info);
1980     if (status == PJ_SUCCESS) {
1981         PyObj_pjsua_buddy_info *obj;
1982
1983         obj = (PyObj_pjsua_buddy_info *)
1984               PyObj_pjsua_buddy_config_new(&PyTyp_pjsua_buddy_info, 
1985                                            NULL, NULL);
1986         PyObj_pjsua_buddy_info_import(obj, &info);      
1987         return (PyObject*)obj;
1988     } else {
1989         return Py_BuildValue("");
1990     }
1991 }
1992
1993 /*
1994  * py_pjsua_buddy_add
1995  */
1996 static PyObject *py_pjsua_buddy_add(PyObject *pSelf, PyObject *pArgs)
1997 {   
1998     PyObject *pCfg;
1999     int buddy_id;
2000     int status;
2001
2002     PJ_UNUSED_ARG(pSelf);
2003
2004     if (!PyArg_ParseTuple(pArgs, "O", &pCfg)) {
2005         return NULL;
2006     }
2007
2008     if (pCfg != Py_None) {
2009         pjsua_buddy_config cfg;
2010         PyObj_pjsua_buddy_config *bc;
2011
2012         bc = (PyObj_pjsua_buddy_config *)pCfg;
2013
2014         pjsua_buddy_config_default(&cfg);
2015         PyObj_pjsua_buddy_config_export(&cfg, bc);  
2016     
2017         status = pjsua_buddy_add(&cfg, &buddy_id);
2018
2019     } else {
2020         status = PJ_EINVAL;
2021         buddy_id = PJSUA_INVALID_ID;
2022     }
2023     return Py_BuildValue("ii", status, buddy_id);
2024 }
2025
2026 /*
2027  * py_pjsua_buddy_del
2028  */
2029 static PyObject *py_pjsua_buddy_del(PyObject *pSelf, PyObject *pArgs)
2030 {    
2031     int buddy_id;
2032     int status;
2033     PyObject *user_data;
2034
2035     PJ_UNUSED_ARG(pSelf);
2036
2037     if (!PyArg_ParseTuple(pArgs, "i", &buddy_id)) {
2038         return NULL;
2039     }
2040
2041     user_data = (PyObject*) pjsua_buddy_get_user_data(buddy_id);
2042     Py_XDECREF(user_data);
2043
2044     status = pjsua_buddy_del(buddy_id);
2045
2046     return Py_BuildValue("i", status);
2047 }
2048
2049 /*
2050  * py_pjsua_buddy_set_user_data
2051  */
2052 static PyObject *py_pjsua_buddy_set_user_data(PyObject *pSelf, PyObject *pArgs)
2053 {    
2054     int buddy_id;
2055     int status;
2056     PyObject *user_data, *old_user_data;
2057
2058     PJ_UNUSED_ARG(pSelf);
2059
2060     if (!PyArg_ParseTuple(pArgs, "iO", &buddy_id, &user_data)) {
2061         return NULL;
2062     }
2063
2064     if (!pjsua_buddy_is_valid(buddy_id)) {
2065         return Py_BuildValue("i", 0);
2066     }
2067
2068     old_user_data = (PyObject*) pjsua_buddy_get_user_data(buddy_id);
2069
2070     status = pjsua_buddy_set_user_data(buddy_id, (void*)user_data);
2071
2072     if (status == PJ_SUCCESS) {
2073         Py_XINCREF(user_data);
2074         Py_XDECREF(old_user_data);
2075     }
2076
2077     return Py_BuildValue("i", status);
2078 }
2079
2080 /*
2081  * py_pjsua_buddy_get_user_data
2082  */
2083 static PyObject *py_pjsua_buddy_get_user_data(PyObject *pSelf, PyObject *pArgs)
2084 {    
2085     int buddy_id;
2086     PyObject *user_data;
2087
2088     PJ_UNUSED_ARG(pSelf);
2089
2090     if (!PyArg_ParseTuple(pArgs, "i", &buddy_id)) {
2091         return NULL;
2092     }
2093
2094     user_data = (PyObject*) pjsua_buddy_get_user_data(buddy_id);
2095
2096     return user_data? Py_BuildValue("O", user_data) : Py_BuildValue("");
2097 }
2098
2099 /*
2100  * py_pjsua_buddy_subscribe_pres
2101  */
2102 static PyObject *py_pjsua_buddy_subscribe_pres(PyObject *pSelf, 
2103                                                PyObject *pArgs)
2104 {
2105     int buddy_id;
2106     int status;
2107     int subscribe;
2108
2109     PJ_UNUSED_ARG(pSelf);
2110
2111     if (!PyArg_ParseTuple(pArgs, "ii", &buddy_id, &subscribe)) {
2112         return NULL;
2113     }
2114
2115     status = pjsua_buddy_subscribe_pres(buddy_id, subscribe);
2116
2117     return Py_BuildValue("i", status);
2118 }
2119
2120 /*
2121  * py_pjsua_pres_dump
2122  */
2123 static PyObject *py_pjsua_pres_dump(PyObject *pSelf, PyObject *pArgs)
2124 {    
2125     int verbose;
2126
2127     PJ_UNUSED_ARG(pSelf);
2128
2129     if (!PyArg_ParseTuple(pArgs, "i", &verbose)) {
2130         return NULL;
2131     }
2132
2133     pjsua_pres_dump(verbose);   
2134
2135     return Py_BuildValue("");
2136 }
2137
2138 /*
2139  * py_pjsua_im_send
2140  */
2141 static PyObject *py_pjsua_im_send(PyObject *pSelf, PyObject *pArgs)
2142 {    
2143     int status;
2144     int acc_id;
2145     pj_str_t *mime_type, tmp_mime_type;
2146     pj_str_t to, content;
2147     PyObject *pTo;
2148     PyObject *pMimeType;
2149     PyObject *pContent;
2150     pjsua_msg_data msg_data;
2151     PyObject *pMsgData;
2152     int user_data;
2153     pj_pool_t *pool = NULL;
2154
2155     PJ_UNUSED_ARG(pSelf);
2156
2157     if (!PyArg_ParseTuple(pArgs, "iOOOOi", &acc_id, 
2158                 &pTo, &pMimeType, &pContent, &pMsgData, &user_data))
2159     {
2160         return NULL;
2161     }
2162
2163     if (pMimeType != Py_None) {
2164         mime_type = &tmp_mime_type;
2165         tmp_mime_type = PyString_ToPJ(pMimeType);
2166     } else {
2167         mime_type = NULL;
2168     }
2169
2170     to = PyString_ToPJ(pTo);
2171     content = PyString_ToPJ(pContent);
2172
2173     pjsua_msg_data_init(&msg_data);
2174
2175     if (pMsgData != Py_None) {
2176         PyObj_pjsua_msg_data *omd;
2177
2178         omd = (PyObj_pjsua_msg_data *)pMsgData;
2179         msg_data.content_type = PyString_ToPJ(omd->content_type);
2180         msg_data.msg_body = PyString_ToPJ(omd->msg_body);
2181         pool = pjsua_pool_create("pytmp", POOL_SIZE, POOL_SIZE);
2182         translate_hdr(pool, &msg_data.hdr_list, omd->hdr_list);
2183     }
2184
2185     status = pjsua_im_send(acc_id, &to, mime_type, &content, 
2186                            &msg_data, (void*)(long)user_data);
2187     if (pool)
2188         pj_pool_release(pool);
2189     
2190     return Py_BuildValue("i",status);
2191 }
2192
2193 /*
2194  * py_pjsua_im_typing
2195  */
2196 static PyObject *py_pjsua_im_typing(PyObject *pSelf, PyObject *pArgs)
2197 {    
2198     int status;
2199     int acc_id;
2200     pj_str_t to;
2201     PyObject *pTo;
2202     int is_typing;
2203     pjsua_msg_data msg_data;
2204     PyObject *pMsgData;
2205     pj_pool_t *pool = NULL;
2206
2207     PJ_UNUSED_ARG(pSelf);
2208
2209     if (!PyArg_ParseTuple(pArgs, "iOiO", &acc_id, &pTo, &is_typing, 
2210                           &pMsgData)) 
2211     {
2212         return NULL;
2213     }
2214         
2215     to = PyString_ToPJ(pTo);
2216
2217     pjsua_msg_data_init(&msg_data);
2218
2219     if (pMsgData != Py_None) {
2220         PyObj_pjsua_msg_data *omd;
2221
2222         omd = (PyObj_pjsua_msg_data *)pMsgData;
2223         msg_data.content_type = PyString_ToPJ(omd->content_type);
2224         msg_data.msg_body = PyString_ToPJ(omd->msg_body);
2225         pool = pjsua_pool_create("pytmp", POOL_SIZE, POOL_SIZE);
2226
2227         translate_hdr(pool, &msg_data.hdr_list, omd->hdr_list);
2228     }
2229
2230     status = pjsua_im_typing(acc_id, &to, is_typing, &msg_data);
2231
2232     if (pool)
2233         pj_pool_release(pool);
2234
2235     return Py_BuildValue("i", status);
2236 }
2237
2238 static char pjsua_buddy_config_default_doc[] =
2239     "_pjsua.Buddy_Config _pjsua.buddy_config_default () "
2240     "Set default values to the buddy config.";
2241 static char pjsua_get_buddy_count_doc[] =
2242     "int _pjsua.get_buddy_count () "
2243     "Get total number of buddies.";
2244 static char pjsua_buddy_is_valid_doc[] =
2245     "int _pjsua.buddy_is_valid (int buddy_id) "
2246     "Check if buddy ID is valid.";
2247 static char pjsua_enum_buddies_doc[] =
2248     "int[] _pjsua.enum_buddies () "
2249     "Enum buddy IDs.";
2250 static char pjsua_buddy_get_info_doc[] =
2251     "_pjsua.Buddy_Info _pjsua.buddy_get_info (int buddy_id) "
2252     "Get detailed buddy info.";
2253 static char pjsua_buddy_add_doc[] =
2254     "int,int _pjsua.buddy_add (_pjsua.Buddy_Config cfg) "
2255     "Add new buddy.";
2256 static char pjsua_buddy_del_doc[] =
2257     "int _pjsua.buddy_del (int buddy_id) "
2258     "Delete buddy.";
2259 static char pjsua_buddy_subscribe_pres_doc[] =
2260     "int _pjsua.buddy_subscribe_pres (int buddy_id, int subscribe) "
2261     "Enable/disable buddy's presence monitoring.";
2262 static char pjsua_pres_dump_doc[] =
2263     "void _pjsua.pres_dump (int verbose) "
2264     "Dump presence subscriptions to log file.";
2265 static char pjsua_im_send_doc[] =
2266     "int _pjsua.im_send (int acc_id, string to, string mime_type, "
2267     "string content, _pjsua.Msg_Data msg_data, int user_data) "
2268     "Send instant messaging outside dialog, using the specified account "
2269     "for route set and authentication.";
2270 static char pjsua_im_typing_doc[] =
2271     "int _pjsua.im_typing (int acc_id, string to, int is_typing, "
2272     "_pjsua.Msg_Data msg_data) "
2273     "Send typing indication outside dialog.";
2274
2275 /* END OF LIB BUDDY */
2276
2277 /* LIB MEDIA */
2278
2279
2280 /*
2281  * py_pjsua_conf_get_max_ports
2282  */
2283 static PyObject *py_pjsua_conf_get_max_ports(PyObject *pSelf, PyObject *pArgs)
2284 {    
2285     PJ_UNUSED_ARG(pSelf);
2286     PJ_UNUSED_ARG(pArgs);
2287
2288     return Py_BuildValue("i", pjsua_conf_get_max_ports());
2289 }
2290
2291 /*
2292  * py_pjsua_conf_get_active_ports
2293  */
2294 static PyObject *py_pjsua_conf_get_active_ports(PyObject *pSelf, 
2295                                                 PyObject *pArgs)
2296 {    
2297     PJ_UNUSED_ARG(pSelf);
2298     PJ_UNUSED_ARG(pArgs);
2299
2300     return Py_BuildValue("i", pjsua_conf_get_active_ports());
2301 }
2302
2303 /*
2304  * py_pjsua_enum_conf_ports
2305  */
2306 static PyObject *py_pjsua_enum_conf_ports(PyObject *pSelf, PyObject *pArgs)
2307 {
2308     pj_status_t status;
2309     PyObject *list;
2310     pjsua_conf_port_id id[PJSUA_MAX_CONF_PORTS];
2311     unsigned c, i;
2312
2313     PJ_UNUSED_ARG(pSelf);
2314     PJ_UNUSED_ARG(pArgs);
2315
2316     c = PJ_ARRAY_SIZE(id);
2317     status = pjsua_enum_conf_ports(id, &c);
2318     if (status != PJ_SUCCESS)
2319         c = 0;
2320     
2321     list = PyList_New(c);
2322     for (i = 0; i < c; i++) {
2323         PyList_SetItem(list, i, Py_BuildValue("i", id[i]));
2324     }
2325     
2326     return (PyObject*)list;
2327 }
2328
2329 /*
2330  * py_pjsua_conf_get_port_info
2331  */
2332 static PyObject *py_pjsua_conf_get_port_info(PyObject *pSelf, PyObject *pArgs)
2333 {       
2334     int id;
2335     PyObj_pjsua_conf_port_info *ret;
2336     pjsua_conf_port_info info;
2337     int status; 
2338     unsigned i;
2339
2340     PJ_UNUSED_ARG(pSelf);
2341
2342     if (!PyArg_ParseTuple(pArgs, "i", &id)) {
2343         return NULL;
2344     }
2345     
2346     status = pjsua_conf_get_port_info(id, &info);
2347     ret = (PyObj_pjsua_conf_port_info *)
2348           conf_port_info_new(&PyTyp_pjsua_conf_port_info, NULL, NULL);
2349     ret->bits_per_sample = info.bits_per_sample;
2350     ret->channel_count = info.channel_count;
2351     ret->clock_rate = info.clock_rate;
2352     ret->name = PyString_FromPJ(&info.name);
2353     ret->samples_per_frame = info.samples_per_frame;
2354     ret->slot_id = info.slot_id;
2355     Py_XDECREF(ret->listeners);
2356     ret->listeners = PyList_New(info.listener_cnt);
2357     for (i = 0; i < info.listener_cnt; i++) {
2358         PyObject *item = Py_BuildValue("i",info.listeners[i]);
2359         PyList_SetItem(ret->listeners, i, item);
2360     }
2361     return (PyObject*)ret;
2362 }
2363
2364 /*
2365  * py_pjsua_conf_remove_port
2366  */
2367 static PyObject *py_pjsua_conf_remove_port(PyObject *pSelf, PyObject *pArgs)
2368 {       
2369     int id;
2370     int status; 
2371     
2372     PJ_UNUSED_ARG(pSelf);
2373
2374     if (!PyArg_ParseTuple(pArgs, "i", &id)) {
2375         return NULL;
2376     }   
2377     
2378     status = pjsua_conf_remove_port(id);
2379     
2380     return Py_BuildValue("i", status);
2381 }
2382
2383 /*
2384  * py_pjsua_conf_connect
2385  */
2386 static PyObject *py_pjsua_conf_connect(PyObject *pSelf, PyObject *pArgs)
2387 {       
2388     int source, sink;
2389     int status; 
2390     
2391     PJ_UNUSED_ARG(pSelf);
2392
2393     if (!PyArg_ParseTuple(pArgs, "ii", &source, &sink)) {
2394         return NULL;
2395     }   
2396     
2397     status = pjsua_conf_connect(source, sink);
2398     
2399     return Py_BuildValue("i", status);
2400 }
2401
2402 /*
2403  * py_pjsua_conf_disconnect
2404  */
2405 static PyObject *py_pjsua_conf_disconnect(PyObject *pSelf, PyObject *pArgs)
2406 {       
2407     int source, sink;
2408     int status; 
2409     
2410     PJ_UNUSED_ARG(pSelf);
2411
2412     if (!PyArg_ParseTuple(pArgs, "ii", &source, &sink)) {
2413         return NULL;
2414     }   
2415     
2416     status = pjsua_conf_disconnect(source, sink);
2417     
2418     return Py_BuildValue("i", status);
2419 }
2420
2421 /*
2422  * py_pjsua_conf_set_tx_level
2423  */
2424 static PyObject *py_pjsua_conf_set_tx_level(PyObject *pSelf, PyObject *pArgs)
2425 {       
2426     int slot;
2427     float level;
2428     int status; 
2429     
2430     PJ_UNUSED_ARG(pSelf);
2431
2432     if (!PyArg_ParseTuple(pArgs, "if", &slot, &level)) {
2433         return NULL;
2434     }   
2435     
2436     status = pjsua_conf_adjust_tx_level(slot, level);
2437     
2438     return Py_BuildValue("i", status);
2439 }
2440
2441 /*
2442  * py_pjsua_conf_set_rx_level
2443  */
2444 static PyObject *py_pjsua_conf_set_rx_level(PyObject *pSelf, PyObject *pArgs)
2445 {       
2446     int slot;
2447     float level;
2448     int status; 
2449     
2450     PJ_UNUSED_ARG(pSelf);
2451
2452     if (!PyArg_ParseTuple(pArgs, "if", &slot, &level)) {
2453         return NULL;
2454     }   
2455     
2456     status = pjsua_conf_adjust_rx_level(slot, level);
2457     
2458     return Py_BuildValue("i", status);
2459 }
2460
2461 /*
2462  * py_pjsua_conf_get_signal_level
2463  */
2464 static PyObject *py_pjsua_conf_get_signal_level(PyObject *pSelf, 
2465                                                 PyObject *pArgs)
2466 {       
2467     int slot;
2468     unsigned tx_level, rx_level;
2469     int status; 
2470     
2471     PJ_UNUSED_ARG(pSelf);
2472
2473     if (!PyArg_ParseTuple(pArgs, "i", &slot)) {
2474         return NULL;
2475     }   
2476     
2477     status = pjsua_conf_get_signal_level(slot, &tx_level, &rx_level);
2478     
2479     return Py_BuildValue("iff", status, (float)(tx_level/255.0), 
2480                          (float)(rx_level/255.0));
2481 }
2482
2483 /*
2484  * py_pjsua_player_create
2485  */
2486 static PyObject *py_pjsua_player_create(PyObject *pSelf, PyObject *pArgs)
2487 {       
2488     int id;
2489     int options;
2490     PyObject *pFilename;
2491     pj_str_t filename;
2492     int status; 
2493     
2494     PJ_UNUSED_ARG(pSelf);
2495
2496     if (!PyArg_ParseTuple(pArgs, "Oi", &pFilename, &options)) {
2497         return NULL;
2498     }
2499
2500     filename = PyString_ToPJ(pFilename);
2501     status = pjsua_player_create(&filename, options, &id);
2502     
2503     return Py_BuildValue("ii", status, id);
2504 }
2505
2506 /*
2507  * py_pjsua_playlist_create
2508  */
2509 static PyObject *py_pjsua_playlist_create(PyObject *pSelf, PyObject *pArgs)
2510 {       
2511     int id;
2512     int options;
2513     PyObject *pLabel, *pFileList;
2514     pj_str_t label;
2515     int count;
2516     pj_str_t files[64];
2517     int status; 
2518     
2519     PJ_UNUSED_ARG(pSelf);
2520
2521     if (!PyArg_ParseTuple(pArgs, "OOi", &pLabel, &pFileList, &options)) {
2522         return NULL;
2523     }
2524
2525     label = PyString_ToPJ(pLabel);
2526     if (!PyList_Check(pFileList))
2527         return Py_BuildValue("ii", PJ_EINVAL, PJSUA_INVALID_ID);
2528
2529     count = 0;
2530     for (count=0; count<PyList_Size(pFileList) && 
2531                   count<PJ_ARRAY_SIZE(files); ++count) 
2532     {
2533         files[count] = PyString_ToPJ(PyList_GetItem(pFileList, count));
2534     }
2535
2536     status = pjsua_playlist_create(files, count, &label, options, &id);
2537     
2538     return Py_BuildValue("ii", status, id);
2539 }
2540
2541 /*
2542  * py_pjsua_player_get_conf_port
2543  */
2544 static PyObject *py_pjsua_player_get_conf_port(PyObject *pSelf, 
2545                                                PyObject *pArgs)
2546 {       
2547     
2548     int id, port_id;    
2549     
2550     PJ_UNUSED_ARG(pSelf);
2551
2552     if (!PyArg_ParseTuple(pArgs, "i", &id)) {
2553         return NULL;
2554     }   
2555     
2556     port_id = pjsua_player_get_conf_port(id);
2557     
2558     return Py_BuildValue("i", port_id);
2559 }
2560
2561 /*
2562  * py_pjsua_player_set_pos
2563  */
2564 static PyObject *py_pjsua_player_set_pos(PyObject *pSelf, PyObject *pArgs)
2565 {       
2566     int id;
2567     int samples;
2568     int status; 
2569     
2570     PJ_UNUSED_ARG(pSelf);
2571
2572     if (!PyArg_ParseTuple(pArgs, "ii", &id, &samples)) {
2573         return NULL;
2574     }   
2575     
2576     if (samples < 0)
2577         samples = 0;
2578
2579     status = pjsua_player_set_pos(id, samples);
2580     
2581     return Py_BuildValue("i", status);
2582 }
2583
2584 /*
2585  * py_pjsua_player_destroy
2586  */
2587 static PyObject *py_pjsua_player_destroy(PyObject *pSelf, PyObject *pArgs)
2588 {       
2589     int id;
2590     int status; 
2591     
2592     PJ_UNUSED_ARG(pSelf);
2593
2594     if (!PyArg_ParseTuple(pArgs, "i", &id)) {
2595         return NULL;
2596     }   
2597     
2598     status = pjsua_player_destroy(id);
2599     
2600     return Py_BuildValue("i", status);
2601 }
2602
2603 /*
2604  * py_pjsua_recorder_create
2605  */
2606 static PyObject *py_pjsua_recorder_create(PyObject *pSelf, PyObject *pArgs)
2607 {       
2608     int id, options;
2609     int max_size;
2610     PyObject *pFilename, *pEncParam;
2611     pj_str_t filename;
2612     int enc_type;
2613     
2614     int status; 
2615     
2616     PJ_UNUSED_ARG(pSelf);
2617
2618     if (!PyArg_ParseTuple(pArgs, "OiOii", &pFilename, &enc_type, &pEncParam,
2619                           &max_size, &options))
2620     {
2621         return NULL;
2622     }
2623
2624     filename = PyString_ToPJ(pFilename);
2625
2626     status = pjsua_recorder_create(&filename, enc_type, NULL, max_size,
2627                                    options, &id);
2628
2629     return Py_BuildValue("ii", status, id);
2630 }
2631
2632 /*
2633  * py_pjsua_recorder_get_conf_port
2634  */
2635 static PyObject *py_pjsua_recorder_get_conf_port(PyObject *pSelf, 
2636                                                  PyObject *pArgs)
2637 {       
2638     
2639     int id, port_id;    
2640     
2641     PJ_UNUSED_ARG(pSelf);
2642
2643     if (!PyArg_ParseTuple(pArgs, "i", &id)) {
2644         return NULL;
2645     }   
2646     
2647     port_id = pjsua_recorder_get_conf_port(id);
2648     
2649     return Py_BuildValue("i", port_id);
2650 }
2651
2652 /*
2653  * py_pjsua_recorder_destroy
2654  */
2655 static PyObject *py_pjsua_recorder_destroy(PyObject *pSelf, PyObject *pArgs)
2656 {       
2657     int id;
2658     int status; 
2659     
2660     PJ_UNUSED_ARG(pSelf);
2661
2662     if (!PyArg_ParseTuple(pArgs, "i", &id)) {
2663         return NULL;
2664     }   
2665     
2666     status = pjsua_recorder_destroy(id);
2667     
2668     return Py_BuildValue("i", status);
2669 }
2670
2671 /*
2672  * py_pjsua_enum_snd_devs
2673  */
2674 static PyObject *py_pjsua_enum_snd_devs(PyObject *pSelf, PyObject *pArgs)
2675 {
2676     pj_status_t status;
2677     PyObject *ret;
2678     pjmedia_snd_dev_info info[SND_DEV_NUM];
2679     unsigned c, i;
2680
2681     PJ_UNUSED_ARG(pSelf);
2682     PJ_UNUSED_ARG(pArgs);
2683
2684     c = PJ_ARRAY_SIZE(info);
2685     status = pjsua_enum_snd_devs(info, &c);
2686     if (status != PJ_SUCCESS)
2687         c = 0;
2688     
2689     ret = PyList_New(c);
2690     for (i = 0; i < c; i++)  {
2691         PyObj_pjmedia_snd_dev_info * obj;
2692
2693         obj = (PyObj_pjmedia_snd_dev_info *)
2694               pjmedia_snd_dev_info_new(&PyTyp_pjmedia_snd_dev_info, 
2695                                        NULL, NULL);
2696         obj->default_samples_per_sec = info[i].default_samples_per_sec;
2697         obj->input_count = info[i].input_count;
2698         obj->output_count = info[i].output_count;
2699         obj->name = PyString_FromString(info[i].name);
2700
2701         PyList_SetItem(ret, i, (PyObject *)obj);
2702     }
2703     
2704     return (PyObject*)ret;
2705 }
2706
2707 /*
2708  * py_pjsua_get_snd_dev
2709  */
2710 static PyObject *py_pjsua_get_snd_dev(PyObject *pSelf, PyObject *pArgs)
2711 {       
2712     int capture_dev, playback_dev;
2713     int status; 
2714     
2715     PJ_UNUSED_ARG(pSelf);
2716     PJ_UNUSED_ARG(pArgs);
2717
2718     status = pjsua_get_snd_dev(&capture_dev, &playback_dev);
2719     
2720     return Py_BuildValue("ii", capture_dev, playback_dev);
2721 }
2722
2723 /*
2724  * py_pjsua_set_snd_dev
2725  */
2726 static PyObject *py_pjsua_set_snd_dev(PyObject *pSelf, PyObject *pArgs)
2727 {       
2728     int capture_dev, playback_dev;
2729     int status; 
2730     
2731     PJ_UNUSED_ARG(pSelf);
2732
2733     if (!PyArg_ParseTuple(pArgs, "ii", &capture_dev, &playback_dev)) {
2734         return NULL;
2735     }   
2736     
2737     status = pjsua_set_snd_dev(capture_dev, playback_dev);
2738     
2739     return Py_BuildValue("i", status);
2740 }
2741
2742 /*
2743  * py_pjsua_set_null_snd_dev
2744  */
2745 static PyObject *py_pjsua_set_null_snd_dev(PyObject *pSelf, PyObject *pArgs)
2746 {       
2747     int status; 
2748
2749     PJ_UNUSED_ARG(pSelf);
2750     PJ_UNUSED_ARG(pArgs);
2751
2752     status = pjsua_set_null_snd_dev();
2753
2754     return Py_BuildValue("i", status);
2755 }
2756
2757 /*
2758  * py_pjsua_set_ec
2759  */
2760 static PyObject *py_pjsua_set_ec(PyObject *pSelf, PyObject *pArgs)
2761 {       
2762     int options;
2763     int tail_ms;
2764     int status; 
2765     
2766     PJ_UNUSED_ARG(pSelf);
2767
2768     if (!PyArg_ParseTuple(pArgs, "ii", &tail_ms, &options)) {
2769         return NULL;
2770     }   
2771
2772     status = pjsua_set_ec(tail_ms, options);
2773     
2774     return Py_BuildValue("i", status);
2775 }
2776
2777 /*
2778  * py_pjsua_get_ec_tail
2779  */
2780 static PyObject *py_pjsua_get_ec_tail(PyObject *pSelf, PyObject *pArgs)
2781 {       
2782     int status; 
2783     unsigned tail_ms;
2784
2785     PJ_UNUSED_ARG(pSelf);
2786     PJ_UNUSED_ARG(pArgs);
2787
2788     status = pjsua_get_ec_tail(&tail_ms);
2789     if (status != PJ_SUCCESS)
2790         tail_ms = 0;
2791     
2792     return Py_BuildValue("i", tail_ms);
2793 }
2794
2795 /*
2796  * py_pjsua_enum_codecs
2797  */
2798 static PyObject *py_pjsua_enum_codecs(PyObject *pSelf, PyObject *pArgs)
2799 {
2800     pj_status_t status;
2801     PyObject *ret;
2802     pjsua_codec_info info[PJMEDIA_CODEC_MGR_MAX_CODECS];
2803     unsigned c, i;
2804
2805     PJ_UNUSED_ARG(pSelf);
2806     PJ_UNUSED_ARG(pArgs);
2807
2808     c = PJ_ARRAY_SIZE(info);
2809     status = pjsua_enum_codecs(info, &c);
2810     if (status != PJ_SUCCESS)
2811         c = 0;
2812     
2813     ret = PyList_New(c);
2814     for (i = 0; i < c; i++)  {
2815         PyObj_pjsua_codec_info * obj;
2816         obj = (PyObj_pjsua_codec_info *)
2817               codec_info_new(&PyTyp_pjsua_codec_info, NULL, NULL);
2818         obj->codec_id = PyString_FromPJ(&info[i].codec_id);
2819         obj->priority = info[i].priority;
2820
2821         PyList_SetItem(ret, i, (PyObject *)obj);
2822     }
2823
2824     return (PyObject*)ret;
2825 }
2826
2827 /*
2828  * py_pjsua_codec_set_priority
2829  */
2830 static PyObject *py_pjsua_codec_set_priority(PyObject *pSelf, PyObject *pArgs)
2831 {       
2832     int status; 
2833     PyObject *pCodecId;
2834     pj_str_t codec_id;
2835     int priority;
2836     
2837     PJ_UNUSED_ARG(pSelf);
2838
2839     if (!PyArg_ParseTuple(pArgs, "Oi", &pCodecId, &priority)) {
2840         return NULL;
2841     }
2842
2843     codec_id = PyString_ToPJ(pCodecId);
2844     if (priority < 0)
2845         priority = 0;
2846     if (priority > 255)
2847         priority = 255;
2848
2849     status = pjsua_codec_set_priority(&codec_id, (pj_uint8_t)priority);
2850     
2851     return Py_BuildValue("i", status);
2852 }
2853
2854 /*
2855  * py_pjsua_codec_get_param
2856  */
2857 static PyObject *py_pjsua_codec_get_param(PyObject *pSelf, PyObject *pArgs)
2858 {       
2859     int status; 
2860     PyObject *pCodecId;
2861     pj_str_t codec_id;
2862     pjmedia_codec_param param;
2863     PyObj_pjmedia_codec_param *ret;
2864     
2865     PJ_UNUSED_ARG(pSelf);
2866
2867     if (!PyArg_ParseTuple(pArgs, "O", &pCodecId)) {
2868         return NULL;
2869     }   
2870
2871     codec_id = PyString_ToPJ(pCodecId);
2872
2873     status = pjsua_codec_get_param(&codec_id, &param);
2874     if (status != PJ_SUCCESS)
2875         return Py_BuildValue("");
2876
2877     ret = (PyObj_pjmedia_codec_param *)
2878           pjmedia_codec_param_new(&PyTyp_pjmedia_codec_param, NULL, NULL);
2879
2880     ret->info->avg_bps = param.info.avg_bps;
2881     ret->info->channel_cnt = param.info.channel_cnt;
2882     ret->info->clock_rate = param.info.clock_rate;
2883     ret->info->frm_ptime = param.info.frm_ptime;
2884     ret->info->pcm_bits_per_sample = param.info.pcm_bits_per_sample;
2885     ret->info->pt = param.info.pt;
2886     ret->setting->cng = param.setting.cng;
2887     //ret->setting->dec_fmtp_mode = param.setting.dec_fmtp_mode;
2888     //ret->setting->enc_fmtp_mode = param.setting.enc_fmtp_mode;
2889     ret->setting->frm_per_pkt = param.setting.frm_per_pkt;
2890     ret->setting->penh = param.setting.penh;
2891     ret->setting->plc = param.setting.plc;
2892     ret->setting->vad = param.setting.vad;
2893
2894     return (PyObject*)ret;
2895 }
2896
2897
2898 /*
2899  * py_pjsua_codec_set_param
2900  */
2901 static PyObject *py_pjsua_codec_set_param(PyObject *pSelf, PyObject *pArgs)
2902 {       
2903     int status; 
2904     PyObject *pCodecId, *pCodecParam;
2905     pj_str_t codec_id;
2906     pjmedia_codec_param param;
2907     
2908     PJ_UNUSED_ARG(pSelf);
2909
2910     if (!PyArg_ParseTuple(pArgs, "OO", &pCodecId, &pCodecParam)) {
2911         return NULL;
2912     }   
2913
2914     codec_id = PyString_ToPJ(pCodecId);
2915
2916     if (pCodecParam != Py_None) {
2917         PyObj_pjmedia_codec_param *obj;
2918
2919         obj = (PyObj_pjmedia_codec_param *)pCodecParam;
2920
2921         param.info.avg_bps = obj->info->avg_bps;
2922         param.info.channel_cnt = obj->info->channel_cnt;
2923         param.info.clock_rate = obj->info->clock_rate;
2924         param.info.frm_ptime = obj->info->frm_ptime;
2925         param.info.pcm_bits_per_sample = obj->info->pcm_bits_per_sample;
2926         param.info.pt = obj->info->pt;
2927         param.setting.cng = obj->setting->cng;
2928         //param.setting.dec_fmtp_mode = obj->setting->dec_fmtp_mode;
2929         //param.setting.enc_fmtp_mode = obj->setting->enc_fmtp_mode;
2930         param.setting.frm_per_pkt = obj->setting->frm_per_pkt;
2931         param.setting.penh = obj->setting->penh;
2932         param.setting.plc = obj->setting->plc;
2933         param.setting.vad = obj->setting->vad;
2934         status = pjsua_codec_set_param(&codec_id, &param);
2935
2936     } else {
2937         status = pjsua_codec_set_param(&codec_id, NULL);
2938     }
2939
2940     return Py_BuildValue("i", status);
2941 }
2942
2943 static char pjsua_conf_get_max_ports_doc[] =
2944     "int _pjsua.conf_get_max_ports () "
2945     "Get maxinum number of conference ports.";
2946 static char pjsua_conf_get_active_ports_doc[] =
2947     "int _pjsua.conf_get_active_ports () "
2948     "Get current number of active ports in the bridge.";
2949 static char pjsua_enum_conf_ports_doc[] =
2950     "int[] _pjsua.enum_conf_ports () "
2951     "Enumerate all conference ports.";
2952 static char pjsua_conf_get_port_info_doc[] =
2953     "_pjsua.Conf_Port_Info _pjsua.conf_get_port_info (int id) "
2954     "Get information about the specified conference port";
2955 static char pjsua_conf_remove_port_doc[] =
2956     "int _pjsua.conf_remove_port (int id) "
2957     "Remove arbitrary slot from the conference bridge. "
2958     "Application should only call this function "
2959     "if it registered the port manually.";
2960 static char pjsua_conf_connect_doc[] =
2961     "int _pjsua.conf_connect (int source, int sink) "
2962     "Establish unidirectional media flow from souce to sink. "
2963     "One source may transmit to multiple destinations/sink. "
2964     "And if multiple sources are transmitting to the same sink, "
2965     "the media will be mixed together. Source and sink may refer "
2966     "to the same ID, effectively looping the media. "
2967     "If bidirectional media flow is desired, application "
2968     "needs to call this function twice, with the second "
2969     "one having the arguments reversed.";
2970 static char pjsua_conf_disconnect_doc[] =
2971     "int _pjsua.conf_disconnect (int source, int sink) "
2972     "Disconnect media flow from the source to destination port.";
2973 static char pjsua_player_create_doc[] =
2974     "int, int _pjsua.player_create (string filename, int options) "
2975     "Create a file player, and automatically connect "
2976     "this player to the conference bridge.";
2977 static char pjsua_player_get_conf_port_doc[] =
2978     "int _pjsua.player_get_conf_port (int) "
2979     "Get conference port ID associated with player.";
2980 static char pjsua_player_set_pos_doc[] =
2981     "int _pjsua.player_set_pos (int id, int samples) "
2982     "Set playback position.";
2983 static char pjsua_player_destroy_doc[] =
2984     "int _pjsua.player_destroy (int id) "
2985     "Close the file, remove the player from the bridge, "
2986     "and free resources associated with the file player.";
2987 static char pjsua_recorder_create_doc[] =
2988     "int, int _pjsua.recorder_create (string filename, "
2989     "int enc_type, int enc_param, int max_size, int options) "
2990     "Create a file recorder, and automatically connect this recorder "
2991     "to the conference bridge. The recorder currently supports recording "
2992     "WAV file, and on Windows, MP3 file. The type of the recorder to use "
2993     "is determined by the extension of the file (e.g. '.wav' or '.mp3').";
2994 static char pjsua_recorder_get_conf_port_doc[] =
2995     "int _pjsua.recorder_get_conf_port (int id) "
2996     "Get conference port associated with recorder.";
2997 static char pjsua_recorder_destroy_doc[] =
2998     "int _pjsua.recorder_destroy (int id) "
2999     "Destroy recorder (this will complete recording).";
3000 static char pjsua_enum_snd_devs_doc[] =
3001     "_pjsua.PJMedia_Snd_Dev_Info[] _pjsua.enum_snd_devs (int count) "
3002     "Enum sound devices.";
3003 static char pjsua_get_snd_dev_doc[] =
3004     "int, int _pjsua.get_snd_dev () "
3005     "Get currently active sound devices. "
3006     "If sound devices has not been created "
3007     "(for example when pjsua_start() is not called), "
3008     "it is possible that the function returns "
3009     "PJ_SUCCESS with -1 as device IDs.";
3010 static char pjsua_set_snd_dev_doc[] =
3011     "int _pjsua.set_snd_dev (int capture_dev, int playback_dev) "
3012     "Select or change sound device. Application may call this function "
3013     "at any time to replace current sound device.";
3014 static char pjsua_set_null_snd_dev_doc[] =
3015     "int _pjsua.set_null_snd_dev () "
3016     "Set pjsua to use null sound device. The null sound device only "
3017     "provides the timing needed by the conference bridge, and will not "
3018     "interract with any hardware.";
3019 static char pjsua_set_ec_doc[] =
3020     "int _pjsua.set_ec (int tail_ms, int options) "
3021     "Configure the echo canceller tail length of the sound port.";
3022 static char pjsua_get_ec_tail_doc[] =
3023     "int _pjsua.get_ec_tail () "
3024     "Get current echo canceller tail length.";
3025 static char pjsua_enum_codecs_doc[] =
3026     "_pjsua.Codec_Info[] _pjsua.enum_codecs () "
3027     "Enum all supported codecs in the system.";
3028 static char pjsua_codec_set_priority_doc[] =
3029     "int _pjsua.codec_set_priority (string id, int priority) "
3030     "Change codec priority.";
3031 static char pjsua_codec_get_param_doc[] =
3032     "_pjsua.PJMedia_Codec_Param _pjsua.codec_get_param (string id) "
3033     "Get codec parameters";
3034 static char pjsua_codec_set_param_doc[] =
3035     "int _pjsua.codec_set_param (string id, "
3036     "_pjsua.PJMedia_Codec_Param param) "
3037     "Set codec parameters.";
3038
3039 /* END OF LIB MEDIA */
3040
3041 /* LIB CALL */
3042
3043 /*
3044  * py_pjsua_call_get_max_count
3045  */
3046 static PyObject *py_pjsua_call_get_max_count(PyObject *pSelf, PyObject *pArgs)
3047 {       
3048     int count;
3049
3050     PJ_UNUSED_ARG(pSelf);
3051     PJ_UNUSED_ARG(pArgs);
3052
3053     count = pjsua_call_get_max_count();
3054     
3055     return Py_BuildValue("i", count);
3056 }
3057
3058 /*
3059  * py_pjsua_call_get_count
3060  */
3061 static PyObject *py_pjsua_call_get_count(PyObject *pSelf, PyObject *pArgs)
3062 {       
3063     int count;  
3064     
3065     PJ_UNUSED_ARG(pSelf);
3066     PJ_UNUSED_ARG(pArgs);
3067
3068     count = pjsua_call_get_count();
3069     
3070     return Py_BuildValue("i", count);
3071 }
3072
3073 /*
3074  * py_pjsua_enum_calls
3075  */
3076 static PyObject *py_pjsua_enum_calls(PyObject *pSelf, PyObject *pArgs)
3077 {
3078     pj_status_t status;
3079     PyObject *ret;
3080     pjsua_transport_id id[PJSUA_MAX_CALLS];
3081     unsigned c, i;
3082
3083     PJ_UNUSED_ARG(pSelf);
3084     PJ_UNUSED_ARG(pArgs);
3085
3086     c = PJ_ARRAY_SIZE(id);
3087     status = pjsua_enum_calls(id, &c);
3088     if (status != PJ_SUCCESS)
3089         c = 0;
3090     
3091     ret = PyList_New(c);
3092     for (i = 0; i < c; i++)  {     
3093         PyList_SetItem(ret, i, Py_BuildValue("i", id[i]));
3094     }
3095     
3096     return (PyObject*)ret;
3097 }
3098
3099 /*
3100  * py_pjsua_call_make_call
3101  */
3102 static PyObject *py_pjsua_call_make_call(PyObject *pSelf, PyObject *pArgs)
3103 {    
3104     int status;
3105     int acc_id;
3106     pj_str_t dst_uri;
3107     PyObject *pDstUri, *pMsgData, *pUserData;
3108     unsigned options;
3109     pjsua_msg_data msg_data;
3110     int call_id;
3111     pj_pool_t *pool = NULL;
3112
3113     PJ_UNUSED_ARG(pSelf);
3114
3115     if (!PyArg_ParseTuple(pArgs, "iOIOO", &acc_id, &pDstUri, &options, 
3116                           &pUserData, &pMsgData))
3117     {
3118         return NULL;
3119     }
3120         
3121     dst_uri = PyString_ToPJ(pDstUri);
3122     pjsua_msg_data_init(&msg_data);
3123
3124     if (pMsgData != Py_None) {
3125         PyObj_pjsua_msg_data * omd;
3126
3127         omd = (PyObj_pjsua_msg_data *)pMsgData;
3128
3129         msg_data.content_type = PyString_ToPJ(omd->content_type);
3130         msg_data.msg_body = PyString_ToPJ(omd->msg_body);
3131         pool = pjsua_pool_create("pytmp", POOL_SIZE, POOL_SIZE);
3132         translate_hdr(pool, &msg_data.hdr_list, omd->hdr_list);
3133     }
3134
3135     Py_XINCREF(pUserData);
3136
3137     status = pjsua_call_make_call(acc_id, &dst_uri, 
3138                                   options, (void*)pUserData, 
3139                                   &msg_data, &call_id); 
3140     if (pool != NULL)
3141         pj_pool_release(pool);
3142     
3143     if (status != PJ_SUCCESS) {
3144         Py_XDECREF(pUserData);
3145     }
3146
3147     return Py_BuildValue("ii", status, call_id);        
3148 }
3149
3150 /*
3151  * py_pjsua_call_is_active
3152  */
3153 static PyObject *py_pjsua_call_is_active(PyObject *pSelf, PyObject *pArgs)
3154 {       
3155     int call_id;
3156     int is_active;
3157     
3158     PJ_UNUSED_ARG(pSelf);
3159
3160     if (!PyArg_ParseTuple(pArgs, "i", &call_id)) {
3161         return NULL;
3162     }   
3163     
3164     is_active = pjsua_call_is_active(call_id);
3165     
3166     return Py_BuildValue("i", is_active);
3167 }
3168
3169 /*
3170  * py_pjsua_call_has_media
3171  */
3172 static PyObject *py_pjsua_call_has_media(PyObject *pSelf, PyObject *pArgs)
3173 {       
3174     int call_id;
3175     int has_media;
3176     
3177     PJ_UNUSED_ARG(pSelf);
3178
3179     if (!PyArg_ParseTuple(pArgs, "i", &call_id)) {
3180         return NULL;
3181     }   
3182     
3183     has_media = pjsua_call_has_media(call_id);
3184
3185     return Py_BuildValue("i", has_media);
3186 }
3187
3188 /*
3189  * py_pjsua_call_get_conf_port
3190  */
3191 static PyObject* py_pjsua_call_get_conf_port(PyObject *pSelf, PyObject *pArgs)
3192 {       
3193     int call_id;
3194     int port_id;
3195
3196     PJ_UNUSED_ARG(pSelf);
3197
3198     if (!PyArg_ParseTuple(pArgs, "i", &call_id)) {
3199         return NULL;
3200     }
3201
3202     port_id = pjsua_call_get_conf_port(call_id);
3203
3204     return Py_BuildValue("i", port_id);
3205 }
3206
3207 /*
3208  * py_pjsua_call_get_info
3209  */
3210 static PyObject* py_pjsua_call_get_info(PyObject *pSelf, PyObject *pArgs)
3211 {       
3212     int call_id;
3213     int status;
3214     PyObj_pjsua_call_info *ret;
3215     pjsua_call_info info;
3216     
3217     PJ_UNUSED_ARG(pSelf);
3218
3219     if (!PyArg_ParseTuple(pArgs, "i", &call_id)) {
3220         return NULL;
3221     }   
3222     
3223     status = pjsua_call_get_info(call_id, &info);
3224     if (status != PJ_SUCCESS)
3225         return Py_BuildValue("");
3226
3227     ret = (PyObj_pjsua_call_info *)call_info_new(&PyTyp_pjsua_call_info,
3228                                                  NULL, NULL);
3229     ret->acc_id = info.acc_id;
3230     Py_XDECREF(ret->call_id);
3231     ret->call_id = PyString_FromPJ(&info.call_id);
3232     ret->conf_slot = info.conf_slot;
3233     ret->connect_duration = info.connect_duration.sec * 1000 +
3234                             info.connect_duration.msec;
3235     ret->total_duration = info.total_duration.sec * 1000 +
3236                           info.total_duration.msec;
3237     ret->id = info.id;
3238     ret->last_status = info.last_status;
3239     Py_XDECREF(ret->last_status_text);
3240     ret->last_status_text = PyString_FromPJ(&info.last_status_text);
3241     Py_XDECREF(ret->local_contact);
3242     ret->local_contact = PyString_FromPJ(&info.local_contact);
3243     Py_XDECREF(ret->local_info);
3244     ret->local_info = PyString_FromPJ(&info.local_info);
3245     Py_XDECREF(ret->remote_contact);
3246     ret->remote_contact = PyString_FromPJ(&info.remote_contact);
3247     Py_XDECREF(ret->remote_info);
3248     ret->remote_info = PyString_FromPJ(&info.remote_info);
3249     ret->media_dir = info.media_dir;
3250     ret->media_status = info.media_status;
3251     ret->role = info.role;
3252     ret->state = info.state;
3253     Py_XDECREF(ret->state_text);
3254     ret->state_text = PyString_FromPJ(&info.state_text);
3255
3256     return (PyObject*)ret;
3257 }
3258
3259 /*
3260  * py_pjsua_call_set_user_data
3261  */
3262 static PyObject *py_pjsua_call_set_user_data(PyObject *pSelf, PyObject *pArgs)
3263 {       
3264     int call_id;
3265     PyObject *pUserData, *old_user_data;
3266     int status;
3267
3268     PJ_UNUSED_ARG(pSelf);
3269
3270     if (!PyArg_ParseTuple(pArgs, "iO", &call_id, &pUserData)) {
3271         return NULL;
3272     }
3273
3274     old_user_data = (PyObject*) pjsua_call_get_user_data(call_id);
3275
3276     if (old_user_data == pUserData) {
3277         return Py_BuildValue("i", PJ_SUCCESS);
3278     }
3279
3280     Py_XINCREF(pUserData);
3281     Py_XDECREF(old_user_data);
3282
3283     status = pjsua_call_set_user_data(call_id, (void*)pUserData);
3284     
3285     if (status != PJ_SUCCESS) {
3286         Py_XDECREF(pUserData);
3287     }
3288
3289     return Py_BuildValue("i", status);
3290 }
3291
3292 /*
3293  * py_pjsua_call_get_user_data
3294  */
3295 static PyObject *py_pjsua_call_get_user_data(PyObject *pSelf, PyObject *pArgs)
3296 {       
3297     int call_id;
3298     PyObject *user_data;        
3299     
3300     PJ_UNUSED_ARG(pSelf);
3301
3302     if (!PyArg_ParseTuple(pArgs, "i", &call_id)) {
3303         return NULL;
3304     }   
3305     
3306     user_data = (PyObject*)pjsua_call_get_user_data(call_id);
3307     return user_data ? Py_BuildValue("O", user_data) : Py_BuildValue("");
3308 }
3309
3310 /*
3311  * py_pjsua_call_answer
3312  */
3313 static PyObject *py_pjsua_call_answer(PyObject *pSelf, PyObject *pArgs)
3314 {    
3315     int status;
3316     int call_id;
3317     pj_str_t * reason, tmp_reason;
3318     PyObject *pReason;
3319     unsigned code;
3320     pjsua_msg_data msg_data;
3321     PyObject * omdObj;
3322     pj_pool_t * pool = NULL;
3323
3324     PJ_UNUSED_ARG(pSelf);
3325
3326     if (!PyArg_ParseTuple(pArgs, "iIOO", &call_id, &code, &pReason, &omdObj)) {
3327         return NULL;
3328     }
3329
3330     if (pReason == Py_None) {
3331         reason = NULL;
3332     } else {
3333         reason = &tmp_reason;
3334         tmp_reason = PyString_ToPJ(pReason);
3335     }
3336
3337     pjsua_msg_data_init(&msg_data);
3338     if (omdObj != Py_None) {
3339         PyObj_pjsua_msg_data *omd;
3340
3341         omd = (PyObj_pjsua_msg_data *)omdObj;
3342         msg_data.content_type = PyString_ToPJ(omd->content_type);
3343         msg_data.msg_body = PyString_ToPJ(omd->msg_body);
3344         pool = pjsua_pool_create("pytmp", POOL_SIZE, POOL_SIZE);
3345         translate_hdr(pool, &msg_data.hdr_list, omd->hdr_list);
3346     }
3347     
3348     status = pjsua_call_answer(call_id, code, reason, &msg_data);       
3349
3350     if (pool)
3351         pj_pool_release(pool);
3352
3353     return Py_BuildValue("i", status);
3354 }
3355
3356 /*
3357  * py_pjsua_call_hangup
3358  */
3359 static PyObject *py_pjsua_call_hangup(PyObject *pSelf, PyObject *pArgs)
3360 {    
3361     int status;
3362     int call_id;
3363     pj_str_t *reason, tmp_reason;
3364     PyObject *pReason;
3365     unsigned code;
3366     pjsua_msg_data msg_data;
3367     PyObject *omdObj;
3368     pj_pool_t *pool = NULL;
3369
3370     PJ_UNUSED_ARG(pSelf);
3371
3372     if (!PyArg_ParseTuple(pArgs, "iIOO", &call_id, &code, &pReason, 
3373                           &omdObj))
3374     {
3375         return NULL;
3376     }
3377
3378     if (pReason == Py_None) {
3379         reason = NULL;
3380     } else {
3381         reason = &tmp_reason;
3382         tmp_reason = PyString_ToPJ(pReason);
3383     }
3384
3385     pjsua_msg_data_init(&msg_data);
3386     if (omdObj != Py_None)  {
3387         PyObj_pjsua_msg_data *omd;
3388
3389         omd = (PyObj_pjsua_msg_data *)omdObj;
3390         msg_data.content_type = PyString_ToPJ(omd->content_type);
3391         msg_data.msg_body = PyString_ToPJ(omd->msg_body);
3392         pool = pjsua_pool_create("pytmp", POOL_SIZE, POOL_SIZE);
3393         translate_hdr(pool, &msg_data.hdr_list, omd->hdr_list);
3394     }
3395     
3396     status = pjsua_call_hangup(call_id, code, reason, &msg_data);
3397     if (pool)
3398         pj_pool_release(pool);
3399
3400     return Py_BuildValue("i", status);
3401 }
3402
3403 /*
3404  * py_pjsua_call_set_hold
3405  */
3406 static PyObject *py_pjsua_call_set_hold(PyObject *pSelf, PyObject *pArgs)
3407 {    
3408     int status;
3409     int call_id;    
3410     pjsua_msg_data msg_data;
3411     PyObject *omdObj;
3412     pj_pool_t *pool = NULL;
3413
3414     PJ_UNUSED_ARG(pSelf);
3415
3416     if (!PyArg_ParseTuple(pArgs, "iO", &call_id, &omdObj)) {
3417         return NULL;
3418     }
3419
3420     pjsua_msg_data_init(&msg_data);
3421     if (omdObj != Py_None) {
3422         PyObj_pjsua_msg_data *omd;    </