From 6b83082d43aea3c1dcd562b95465e34f1b46c3af Mon Sep 17 00:00:00 2001 From: Benny Prijono Date: Sat, 3 Feb 2007 17:23:22 +0000 Subject: Added various PJSIP constants to Python module and bugfix in call_hangup() git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@928 74dad513-b988-da41-8d7b-12977e46ad98 --- pjsip-apps/src/py_pjsua/pjsua_app.py | 53 ++++++++--- pjsip-apps/src/py_pjsua/py_pjsua.c | 172 +++++++++++------------------------ 2 files changed, 94 insertions(+), 131 deletions(-) (limited to 'pjsip-apps/src/py_pjsua') diff --git a/pjsip-apps/src/py_pjsua/pjsua_app.py b/pjsip-apps/src/py_pjsua/pjsua_app.py index 7aa7d3e9..ce94d6a2 100644 --- a/pjsip-apps/src/py_pjsua/pjsua_app.py +++ b/pjsip-apps/src/py_pjsua/pjsua_app.py @@ -1,3 +1,10 @@ +# $Id$ +# +# Sample and simple Python script to make and receive calls, and do +# presence and instant messaging/IM using PJSUA-API binding for Python. +# +# Copyright (C) 2003-2007 Benny Prijono +# import py_pjsua import sys import thread @@ -29,33 +36,35 @@ def call_name(call_id): ci = py_pjsua.call_get_info(call_id) return "[Call " + `call_id` + " " + ci.remote_info + "]" -# Handler when invite state has changed. +# Callback when call state has changed. # def on_call_state(call_id, e): global g_current_call ci = py_pjsua.call_get_info(call_id) write_log(3, call_name(call_id) + " state = " + `ci.state_text`) - if ci.state == 6: + if ci.state == py_pjsua.PJSIP_INV_STATE_DISCONNECTED: g_current_call = py_pjsua.PJSUA_INVALID_ID -# Handler for incoming call +# Callback for incoming call # def on_incoming_call(acc_id, call_id, rdata): global g_current_call if g_current_call != py_pjsua.PJSUA_INVALID_ID: - py_pjsua.call_answer(call_id, 486, "", None) + # There's call in progress - answer Busy + py_pjsua.call_answer(call_id, 486, None, None) return + g_current_call = call_id ci = py_pjsua.call_get_info(call_id) - write_log(3, "Incoming call: " + call_name(call_id)) - py_pjsua.call_answer(call_id, 200, "", None) + write_log(3, "*** Incoming call: " + call_name(call_id) + "***") + write_log(3, "*** Press a to answer or h to hangup ***"); -# Handler when media state has changed (e.g. established or terminated) +# Callback when media state has changed (e.g. established or terminated) # def on_call_media_state(call_id): ci = py_pjsua.call_get_info(call_id) - if ci.media_status == 1: + if ci.media_status == py_pjsua.PJSUA_CALL_MEDIA_ACTIVE: py_pjsua.conf_connect(ci.conf_slot, 0) py_pjsua.conf_connect(0, ci.conf_slot) write_log(3, call_name(call_id) + ": media is active") @@ -63,7 +72,7 @@ def on_call_media_state(call_id): write_log(3, call_name(call_id) + ": media is inactive") -# Handler when account registration state has changed +# Callback when account registration state has changed # def on_reg_state(acc_id): acc_info = py_pjsua.acc_get_info(acc_id) @@ -73,6 +82,8 @@ def on_reg_state(acc_id): write_log(3, "Account successfully (un)registered") +# Callback when buddy's presence state has changed +# def on_buddy_state(buddy_id): write_log(3, "On Buddy state called") buddy_info = py_pjsua.buddy_get_info(buddy_id) @@ -80,13 +91,19 @@ def on_buddy_state(buddy_id): write_log(3, "Status of " + `buddy_info.uri` + " is " + `buddy_info.status_text`) else: write_log(3, "Status : " + `buddy_info.status`) - + +# Callback on incoming pager (MESSAGE) +# def on_pager(call_id, strfrom, strto, contact, mime_type, text): write_log(3, "MESSAGE from " + `strfrom` + " : " + `text`) - + + +# Callback on the delivery status of outgoing pager (MESSAGE) +# def on_pager_status(call_id, strto, body, user_data, status, reason): write_log(3, "MESSAGE to " + `strto` + " status " + `status` + " reason " + `reason`) + # Utility: display PJ error and exit # def err_exit(title, rc): @@ -162,7 +179,8 @@ def app_init(): transport_cfg.use_stun = 1 # Create UDP transport - status, transport_id = py_pjsua.transport_create(1, transport_cfg) + status, transport_id = \ + py_pjsua.transport_create(py_pjsua.PJSIP_TRANSPORT_UDP, transport_cfg) if status != 0: py_pjsua.destroy() err_exit("Error creating UDP transport", status) @@ -268,6 +286,7 @@ Menu: +a Add account +b Add buddy m Make call + a Answer current call (if any) h Hangup current call (if any) i Send instant message """ @@ -304,7 +323,7 @@ def app_menu(): continue # Send the IM! - py_pjsua.im_send(g_acc_id, url, "", message, None, 0) + py_pjsua.im_send(g_acc_id, url, None, message, None, 0) elif choice[0] == "m": # Make call @@ -342,7 +361,13 @@ def app_menu(): elif choice[0] == "h": if g_current_call != py_pjsua.PJSUA_INVALID_ID: - py_pjsua.call_hangup(g_current_call, 603, "", None) + py_pjsua.call_hangup(g_current_call, 603, None, None) + else: + print "No current call" + + elif choice[0] == "a": + if g_current_call != py_pjsua.PJSUA_INVALID_ID: + py_pjsua.call_answer(g_current_call, 200, None, None) else: print "No current call" diff --git a/pjsip-apps/src/py_pjsua/py_pjsua.c b/pjsip-apps/src/py_pjsua/py_pjsua.c index 77649f17..6ce18705 100644 --- a/pjsip-apps/src/py_pjsua/py_pjsua.c +++ b/pjsip-apps/src/py_pjsua/py_pjsua.c @@ -1,6 +1,6 @@ /* $Id$ */ /* - * Copyright (C) 2003-2006 Benny Prijono + * Copyright (C) 2003-2007 Benny Prijono * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -7510,7 +7510,7 @@ static PyObject *py_pjsua_call_hangup { return NULL; } - if (sr != Py_None) + if (sr == Py_None) { reason = NULL; } else { @@ -8359,6 +8359,7 @@ DL_EXPORT(void) initpy_pjsua(void) { PyObject* m = NULL; +#define ADD_CONSTANT(mod,name) PyModule_AddIntConstant(mod,#name,name) if (PyType_Ready(&callback_Type) < 0) @@ -8587,123 +8588,60 @@ initpy_pjsua(void) /* END OF LIB CALL */ -#ifdef PJSUA_INVALID_ID - /* - * Constant to identify invalid ID for all sorts of IDs. - */ - PyModule_AddIntConstant(m, "PJSUA_INVALID_ID", PJSUA_INVALID_ID); -#endif - -#ifdef PJSUA_ACC_MAX_PROXIES - /* - * Maximum proxies in account. - */ - PyModule_AddIntConstant( - m, "PJSUA_ACC_MAX_PROXIES ", PJSUA_ACC_MAX_PROXIES - ); -#endif - -#ifdef PJSUA_MAX_ACC - /* - * Maximum account. - */ - PyModule_AddIntConstant( - m, "PJSUA_MAX_ACC", PJSUA_MAX_ACC - ); -#endif - -#ifdef PJSUA_REG_INTERVAL - /* - * Default registration interval.. - */ - PyModule_AddIntConstant( - m, "PJSUA_REG_INTERVAL", PJSUA_REG_INTERVAL - ); -#endif - -#ifdef PJSUA_PUBLISH_EXPIRATION - /* - * Default PUBLISH expiration - */ - PyModule_AddIntConstant( - m, "PJSUA_PUBLISH_EXPIRATION", PJSUA_PUBLISH_EXPIRATION - ); -#endif - -#ifdef PJSUA_DEFAULT_ACC_PRIORITY - /* - * Default account priority. - */ - PyModule_AddIntConstant( - m, "PJSUA_DEFAULT_ACC_PRIORITY", PJSUA_DEFAULT_ACC_PRIORITY - ); -#endif - -#ifdef PJSUA_MAX_BUDDIES - /* - * Default account priority. - */ - PyModule_AddIntConstant( - m, "PJSUA_MAX_BUDDIES", PJSUA_MAX_BUDDIES - ); -#endif - -#ifdef PJSUA_MAX_CONF_PORTS /* - * Max ports in the conference bridge. + * Add various constants. */ - PyModule_AddIntConstant( - m, "PJSUA_MAX_CONF_PORTS", PJSUA_MAX_CONF_PORTS - ); - -#endif - -#ifdef PJSUA_DEFAULT_CLOCK_RATE - - PyModule_AddIntConstant( - m, "PJSUA_DEFAULT_CLOCK_RATE", PJSUA_DEFAULT_CLOCK_RATE - ); - -#endif - -#ifdef PJSUA_DEFAULT_CODEC_QUALITY - - PyModule_AddIntConstant( - m, "PJSUA_DEFAULT_CODEC_QUALITY", PJSUA_DEFAULT_CODEC_QUALITY - ); - -#endif - -#ifdef PJSUA_DEFAULT_ILBC_MODE - - PyModule_AddIntConstant( - m, "PJSUA_DEFAULT_ILBC_MODE", PJSUA_DEFAULT_ILBC_MODE - ); - -#endif - -#ifdef PJSUA_DEFAULT_EC_TAIL_LEN - - PyModule_AddIntConstant( - m, "PJSUA_DEFAULT_EC_TAIL_LEN", PJSUA_DEFAULT_EC_TAIL_LEN - ); - -#endif - -#ifdef PJSUA_MAX_CALLS - - PyModule_AddIntConstant( - m, "PJSUA_MAX_CALLS", PJSUA_MAX_CALLS - ); - -#endif - -#ifdef PJSUA_XFER_NO_REQUIRE_REPLACES - - PyModule_AddIntConstant( - m, "PJSUA_XFER_NO_REQUIRE_REPLACES", PJSUA_XFER_NO_REQUIRE_REPLACES - ); -#endif + /* Call states */ + ADD_CONSTANT(m, PJSIP_INV_STATE_NULL); + ADD_CONSTANT(m, PJSIP_INV_STATE_CALLING); + ADD_CONSTANT(m, PJSIP_INV_STATE_INCOMING); + ADD_CONSTANT(m, PJSIP_INV_STATE_EARLY); + ADD_CONSTANT(m, PJSIP_INV_STATE_CONNECTING); + ADD_CONSTANT(m, PJSIP_INV_STATE_CONFIRMED); + ADD_CONSTANT(m, PJSIP_INV_STATE_DISCONNECTED); + + /* Call media status (enum pjsua_call_media_status) */ + ADD_CONSTANT(m, PJSUA_CALL_MEDIA_NONE); + ADD_CONSTANT(m, PJSUA_CALL_MEDIA_ACTIVE); + ADD_CONSTANT(m, PJSUA_CALL_MEDIA_LOCAL_HOLD); + ADD_CONSTANT(m, PJSUA_CALL_MEDIA_REMOTE_HOLD); + + /* Buddy status */ + ADD_CONSTANT(m, PJSUA_BUDDY_STATUS_UNKNOWN); + ADD_CONSTANT(m, PJSUA_BUDDY_STATUS_ONLINE); + ADD_CONSTANT(m, PJSUA_BUDDY_STATUS_OFFLINE); + + /* PJSIP transport types (enum pjsip_transport_type_e) */ + ADD_CONSTANT(m, PJSIP_TRANSPORT_UNSPECIFIED); + ADD_CONSTANT(m, PJSIP_TRANSPORT_UDP); + ADD_CONSTANT(m, PJSIP_TRANSPORT_TCP); + ADD_CONSTANT(m, PJSIP_TRANSPORT_TLS); + ADD_CONSTANT(m, PJSIP_TRANSPORT_SCTP); + ADD_CONSTANT(m, PJSIP_TRANSPORT_LOOP); + ADD_CONSTANT(m, PJSIP_TRANSPORT_LOOP_DGRAM); + + + /* Invalid IDs */ + ADD_CONSTANT(m, PJSUA_INVALID_ID); + + + /* Various compile time constants */ + ADD_CONSTANT(m, PJSUA_ACC_MAX_PROXIES); + ADD_CONSTANT(m, PJSUA_MAX_ACC); + ADD_CONSTANT(m, PJSUA_REG_INTERVAL); + ADD_CONSTANT(m, PJSUA_PUBLISH_EXPIRATION); + ADD_CONSTANT(m, PJSUA_DEFAULT_ACC_PRIORITY); + ADD_CONSTANT(m, PJSUA_MAX_BUDDIES); + ADD_CONSTANT(m, PJSUA_MAX_CONF_PORTS); + ADD_CONSTANT(m, PJSUA_DEFAULT_CLOCK_RATE); + ADD_CONSTANT(m, PJSUA_DEFAULT_CODEC_QUALITY); + ADD_CONSTANT(m, PJSUA_DEFAULT_ILBC_MODE); + ADD_CONSTANT(m, PJSUA_DEFAULT_EC_TAIL_LEN); + ADD_CONSTANT(m, PJSUA_MAX_CALLS); + ADD_CONSTANT(m, PJSUA_XFER_NO_REQUIRE_REPLACES); + + +#undef ADD_CONSTANT } -- cgit v1.2.3