diff options
author | Benny Prijono <bennylp@teluu.com> | 2008-07-21 18:20:57 +0000 |
---|---|---|
committer | Benny Prijono <bennylp@teluu.com> | 2008-07-21 18:20:57 +0000 |
commit | f50466cf0839b9abd6beb12576e83b3a78b9013c (patch) | |
tree | 3da37da741d3cba8b6faeb0d0b3cb379358b249c /pjsip-apps/src/python/samples | |
parent | d96688e27a0e7d8c06ac3e399718a217daf6994e (diff) |
Major modifications in Python module and pjsua.py wrapper:
- replaced call/acc/buddy dictionaries with user data attachment
- recommended to install callback when creating the object, to
prevent missing some events
- fixed circular references by using weakref
- protect access to pjsua with mutex; found out that without this
there will be deadlock in Python
- fixed memory leaks in the _pjsua.c module (objects reference
counter not properly maintained)
git-svn-id: http://svn.pjsip.org/repos/pjproject/trunk@2163 74dad513-b988-da41-8d7b-12977e46ad98
Diffstat (limited to 'pjsip-apps/src/python/samples')
-rw-r--r-- | pjsip-apps/src/python/samples/call.py | 28 | ||||
-rw-r--r-- | pjsip-apps/src/python/samples/presence.py | 67 | ||||
-rw-r--r-- | pjsip-apps/src/python/samples/registration.py | 2 | ||||
-rw-r--r-- | pjsip-apps/src/python/samples/subscribe.py | 94 |
4 files changed, 74 insertions, 117 deletions
diff --git a/pjsip-apps/src/python/samples/call.py b/pjsip-apps/src/python/samples/call.py index 2f44f62b..ac053ad5 100644 --- a/pjsip-apps/src/python/samples/call.py +++ b/pjsip-apps/src/python/samples/call.py @@ -1,4 +1,4 @@ -# $Id:$ +# $Id$ # # SIP call sample. # @@ -18,13 +18,12 @@ def log_cb(level, str, len): # Callback to receive events from account class MyAccountCallback(pj.AccountCallback): - def __init__(self, account): + def __init__(self, account=None): pj.AccountCallback.__init__(self, account) # Notification on incoming call def on_incoming_call(self, call): global current_call - if current_call: call.answer(486, "Busy") return @@ -43,13 +42,12 @@ class MyAccountCallback(pj.AccountCallback): # Callback to receive events from Call class MyCallCallback(pj.CallCallback): - def __init__(self, call): + def __init__(self, call=None): pj.CallCallback.__init__(self, call) # Notification when call state has changed def on_state(self): global current_call - print "Call with", self.call.info().remote_uri, print "is", self.call.info().state_text, print "last code =", self.call.info().last_code, @@ -57,6 +55,7 @@ class MyCallCallback(pj.CallCallback): if self.call.info().state == pj.CallState.DISCONNECTED: current_call = None + print 'Current call is', current_call # Notification when call's media state has changed. def on_media_state(self): @@ -73,12 +72,9 @@ class MyCallCallback(pj.CallCallback): def make_call(uri): try: print "Making call to", uri - call = acc.make_call(uri) - call_cb = MyCallCallback(call) - call.set_callback(call_cb) - return call + return acc.make_call(uri, cb=MyCallCallback()) except pj.Error, e: - print "Error: " + str(e) + print "Exception: " + str(e) return None @@ -100,13 +96,14 @@ try: lib.start() # Create local account - acc = lib.create_account_for_transport(transport) - acc_cb = MyAccountCallback(acc) - acc.set_callback(acc_cb) + acc = lib.create_account_for_transport(transport, cb=MyAccountCallback()) # If argument is specified then make call to the URI if len(sys.argv) > 1: + lck = lib.auto_lock() current_call = make_call(sys.argv[1]) + print 'Current call is', current_call + del lck my_sip_uri = "sip:" + transport.info().host + \ ":" + str(transport.info().port) @@ -125,7 +122,9 @@ try: input = sys.stdin.readline().rstrip("\r\n") if input == "": continue + lck = lib.auto_lock() current_call = make_call(input) + del lck elif input == "h": if not current_call: @@ -143,6 +142,9 @@ try: break # Shutdown the library + transport = None + acc.delete() + acc = None lib.destroy() lib = None diff --git a/pjsip-apps/src/python/samples/presence.py b/pjsip-apps/src/python/samples/presence.py index 5ae48645..20c2e518 100644 --- a/pjsip-apps/src/python/samples/presence.py +++ b/pjsip-apps/src/python/samples/presence.py @@ -8,12 +8,30 @@ import sys import pjsua as pj LOG_LEVEL = 3 +pending_pres = None +pending_uri = None def log_cb(level, str, len): print str, +class MyAccountCallback(pj.AccountCallback): + def __init__(self, account=None): + pj.AccountCallback.__init__(self, account) + + def on_incoming_subscribe(self, buddy, from_uri, contact_uri, pres): + global pending_pres, pending_uri + # Allow buddy to subscribe to our presence + if buddy: + return (200, None) + print 'Incoming SUBSCRIBE request from', from_uri + print 'Press "A" to accept and add, "R" to reject the request' + pending_pres = pres + pending_uri = from_uri + return (202, None) + + class MyBuddyCallback(pj.BuddyCallback): - def __init__(self, buddy): + def __init__(self, buddy=None): pj.BuddyCallback.__init__(self, buddy) def on_state(self): @@ -54,8 +72,9 @@ try: lib.start() # Create local account - acc = lib.create_account_for_transport(transport) - + acc = lib.create_account_for_transport(transport, cb=MyAccountCallback()) + acc.set_basic_status(True) + my_sip_uri = "sip:" + transport.info().host + \ ":" + str(transport.info().port) @@ -64,7 +83,8 @@ try: # Menu loop while True: print "My SIP URI is", my_sip_uri - print "Menu: a=add buddy, t=toggle online status, i=send IM, q=quit" + print "Menu: a=add buddy, d=delete buddy, t=toggle", \ + " online status, i=send IM, q=quit" input = sys.stdin.readline().rstrip("\r\n") if input == "a": @@ -74,10 +94,7 @@ try: if input == "": continue - buddy = acc.add_buddy(input) - cb = MyBuddyCallback(buddy) - buddy.set_callback(cb) - + buddy = acc.add_buddy(input, cb=MyBuddyCallback()) buddy.subscribe() elif input == "t": @@ -97,11 +114,43 @@ try: continue buddy.send_pager(input) - + + elif input == "d": + if buddy: + buddy.delete() + buddy = None + else: + print 'No buddy was added' + + elif input == "A": + if pending_pres: + acc.pres_notify(pending_pres, pj.SubscriptionState.ACTIVE) + buddy = acc.add_buddy(pending_uri, cb=MyBuddyCallback()) + buddy.subscribe() + pending_pres = None + pending_uri = None + else: + print "No pending request" + + elif input == "R": + if pending_pres: + acc.pres_notify(pending_pres, pj.SubscriptionState.TERMINATED, + "rejected") + pending_pres = None + pending_uri = None + else: + print "No pending request" + elif input == "q": break # Shutdown the library + acc.delete() + acc = None + if pending_pres: + acc.pres_notify(pending_pres, pj.SubscriptionState.TERMINATED, + "rejected") + transport = None lib.destroy() lib = None diff --git a/pjsip-apps/src/python/samples/registration.py b/pjsip-apps/src/python/samples/registration.py index 16cae8b2..973ea2f4 100644 --- a/pjsip-apps/src/python/samples/registration.py +++ b/pjsip-apps/src/python/samples/registration.py @@ -1,4 +1,4 @@ -# $Id:$ +# $Id$ # # SIP account and registration sample. In this sample, the program # will block to wait until registration is complete diff --git a/pjsip-apps/src/python/samples/subscribe.py b/pjsip-apps/src/python/samples/subscribe.py deleted file mode 100644 index e48aeee8..00000000 --- a/pjsip-apps/src/python/samples/subscribe.py +++ /dev/null @@ -1,94 +0,0 @@ -# $Id$ -# -# Authorization of incoming subscribe request -# -# Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> -# -import sys -import pjsua as pj - -LOG_LEVEL = 3 - -pending_pres = None - -def log_cb(level, str, len): - print str, - -class MyAccountCallback(pj.AccountCallback): - def __init__(self, account): - pj.AccountCallback.__init__(self, account) - - def on_incoming_subscribe(self, buddy, from_uri, pres): - # Allow buddy to subscribe to our presence - global pending_pres - - if buddy: - return (200, None) - print 'Incoming SUBSCRIBE request from', from_uri - print 'Press "A" to accept and add, "R" to reject the request' - pending_pres = pres - return (202, None) - - -lib = pj.Lib() - -try: - # Init library with default config and some customized - # logging config. - lib.init(log_cfg = pj.LogConfig(level=LOG_LEVEL, callback=log_cb)) - - # Create UDP transport which listens to any available port - transport = lib.create_transport(pj.TransportType.UDP, - pj.TransportConfig(0)) - print "\nListening on", transport.info().host, - print "port", transport.info().port, "\n" - - # Start the library - lib.start() - - # Create local account - acc = lib.create_account_for_transport(transport) - acc.set_callback(MyAccountCallback(acc)) - - my_sip_uri = "sip:" + transport.info().host + \ - ":" + str(transport.info().port) - - buddy = None - - # Menu loop - while True: - print "My SIP URI is", my_sip_uri - print "Menu: t=toggle online status, q=quit" - - input = sys.stdin.readline().rstrip("\r\n") - - if input == "t": - acc.set_basic_status(not acc.info().online_status) - - elif input == "A": - if pending_pres: - acc.pres_notify(pending_pres, pj.SubscriptionState.ACTIVE) - pending_pres = None - else: - print "No pending request" - - elif input == "R": - if pending_pres: - acc.pres_notify(pending_pres, pj.SubscriptionState.TERMINATED, - "rejected") - pending_pres = None - else: - print "No pending request" - - elif input == "q": - break - - # Shutdown the library - lib.destroy() - lib = None - -except pj.Error, e: - print "Exception: " + str(e) - lib.destroy() - lib = None - |