diff options
Diffstat (limited to 'pjsip-apps/src/python/samples')
-rw-r--r-- | pjsip-apps/src/python/samples/call.py | 169 | ||||
-rw-r--r-- | pjsip-apps/src/python/samples/presence.py | 175 | ||||
-rw-r--r-- | pjsip-apps/src/python/samples/registration.py | 70 | ||||
-rw-r--r-- | pjsip-apps/src/python/samples/simplecall.py | 88 |
4 files changed, 502 insertions, 0 deletions
diff --git a/pjsip-apps/src/python/samples/call.py b/pjsip-apps/src/python/samples/call.py new file mode 100644 index 0000000..60f9065 --- /dev/null +++ b/pjsip-apps/src/python/samples/call.py @@ -0,0 +1,169 @@ +# $Id: call.py 2171 2008-07-24 09:01:33Z bennylp $ +# +# SIP call sample. +# +# Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> +# +# 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 +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +import sys +import pjsua as pj + +LOG_LEVEL=3 +current_call = None + +# Logging callback +def log_cb(level, str, len): + print str, + + +# Callback to receive events from account +class MyAccountCallback(pj.AccountCallback): + + 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 + + print "Incoming call from ", call.info().remote_uri + print "Press 'a' to answer" + + current_call = call + + call_cb = MyCallCallback(current_call) + current_call.set_callback(call_cb) + + current_call.answer(180) + + +# Callback to receive events from Call +class MyCallCallback(pj.CallCallback): + + 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, + print "(" + self.call.info().last_reason + ")" + + 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): + if self.call.info().media_state == pj.MediaState.ACTIVE: + # Connect the call to sound device + call_slot = self.call.info().conf_slot + pj.Lib.instance().conf_connect(call_slot, 0) + pj.Lib.instance().conf_connect(0, call_slot) + print "Media is now active" + else: + print "Media is inactive" + +# Function to make call +def make_call(uri): + try: + print "Making call to", uri + return acc.make_call(uri, cb=MyCallCallback()) + except pj.Error, e: + print "Exception: " + str(e) + return None + + +# Create library instance +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, 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) + + # Menu loop + while True: + print "My SIP URI is", my_sip_uri + print "Menu: m=make call, h=hangup call, a=answer call, q=quit" + + input = sys.stdin.readline().rstrip("\r\n") + if input == "m": + if current_call: + print "Already have another call" + continue + print "Enter destination URI to call: ", + 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: + print "There is no call" + continue + current_call.hangup() + + elif input == "a": + if not current_call: + print "There is no call" + continue + current_call.answer(200) + + elif input == "q": + break + + # Shutdown the library + transport = None + acc.delete() + acc = None + lib.destroy() + lib = None + +except pj.Error, e: + print "Exception: " + str(e) + lib.destroy() + lib = None + diff --git a/pjsip-apps/src/python/samples/presence.py b/pjsip-apps/src/python/samples/presence.py new file mode 100644 index 0000000..afeaeab --- /dev/null +++ b/pjsip-apps/src/python/samples/presence.py @@ -0,0 +1,175 @@ +# $Id: presence.py 2171 2008-07-24 09:01:33Z bennylp $ +# +# Presence and instant messaging +# +# Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> +# +# 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 +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +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=None): + pj.BuddyCallback.__init__(self, buddy) + + def on_state(self): + print "Buddy", self.buddy.info().uri, "is", + print self.buddy.info().online_text + + def on_pager(self, mime_type, body): + print "Instant message from", self.buddy.info().uri, + print "(", mime_type, "):" + print body + + def on_pager_status(self, body, im_id, code, reason): + if code >= 300: + print "Message delivery failed for message", + print body, "to", self.buddy.info().uri, ":", reason + + def on_typing(self, is_typing): + if is_typing: + print self.buddy.info().uri, "is typing" + else: + print self.buddy.info().uri, "stops typing" + + +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, cb=MyAccountCallback()) + acc.set_basic_status(True) + + 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: 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": + # Add buddy + print "Enter buddy URI: ", + input = sys.stdin.readline().rstrip("\r\n") + if input == "": + continue + + buddy = acc.add_buddy(input, cb=MyBuddyCallback()) + buddy.subscribe() + + elif input == "t": + acc.set_basic_status(not acc.info().online_status) + + elif input == "i": + if not buddy: + print "Add buddy first" + continue + + buddy.send_typing_ind(True) + + print "Type the message: ", + input = sys.stdin.readline().rstrip("\r\n") + if input == "": + buddy.send_typing_ind(False) + 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 + +except pj.Error, e: + print "Exception: " + str(e) + lib.destroy() + lib = None + diff --git a/pjsip-apps/src/python/samples/registration.py b/pjsip-apps/src/python/samples/registration.py new file mode 100644 index 0000000..06670da --- /dev/null +++ b/pjsip-apps/src/python/samples/registration.py @@ -0,0 +1,70 @@ +# $Id: registration.py 2171 2008-07-24 09:01:33Z bennylp $ +# +# SIP account and registration sample. In this sample, the program +# will block to wait until registration is complete +# +# Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> +# +# 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 +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +import sys +import pjsua as pj +import threading + + +def log_cb(level, str, len): + print str, + +class MyAccountCallback(pj.AccountCallback): + sem = None + + def __init__(self, account): + pj.AccountCallback.__init__(self, account) + + def wait(self): + self.sem = threading.Semaphore(0) + self.sem.acquire() + + def on_reg_state(self): + if self.sem: + if self.account.info().reg_status >= 200: + self.sem.release() + +lib = pj.Lib() + +try: + lib.init(log_cfg = pj.LogConfig(level=4, callback=log_cb)) + lib.create_transport(pj.TransportType.UDP, pj.TransportConfig(5080)) + lib.start() + + acc = lib.create_account(pj.AccountConfig("pjsip.org", "bennylp", "***")) + + acc_cb = MyAccountCallback(acc) + acc.set_callback(acc_cb) + acc_cb.wait() + + print "\n" + print "Registration complete, status=", acc.info().reg_status, \ + "(" + acc.info().reg_reason + ")" + print "\nPress ENTER to quit" + sys.stdin.readline() + + lib.destroy() + lib = None + +except pj.Error, e: + print "Exception: " + str(e) + lib.destroy() + diff --git a/pjsip-apps/src/python/samples/simplecall.py b/pjsip-apps/src/python/samples/simplecall.py new file mode 100644 index 0000000..40ac28f --- /dev/null +++ b/pjsip-apps/src/python/samples/simplecall.py @@ -0,0 +1,88 @@ +# $Id: simplecall.py 2171 2008-07-24 09:01:33Z bennylp $ +# +# SIP account and registration sample. In this sample, the program +# will block to wait until registration is complete +# +# Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> +# +# 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 +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA +# +import sys +import pjsua as pj + +# Logging callback +def log_cb(level, str, len): + print str, + +# Callback to receive events from Call +class MyCallCallback(pj.CallCallback): + def __init__(self, call=None): + pj.CallCallback.__init__(self, call) + + # Notification when call state has changed + def on_state(self): + print "Call is ", self.call.info().state_text, + print "last code =", self.call.info().last_code, + print "(" + self.call.info().last_reason + ")" + + # Notification when call's media state has changed. + def on_media_state(self): + global lib + if self.call.info().media_state == pj.MediaState.ACTIVE: + # Connect the call to sound device + call_slot = self.call.info().conf_slot + lib.conf_connect(call_slot, 0) + lib.conf_connect(0, call_slot) + print "Hello world, I can talk!" + + +# Check command line argument +if len(sys.argv) != 2: + print "Usage: simplecall.py <dst-URI>" + sys.exit(1) + +try: + # Create library instance + lib = pj.Lib() + + # Init library with default config + lib.init(log_cfg = pj.LogConfig(level=3, callback=log_cb)) + + # Create UDP transport which listens to any available port + transport = lib.create_transport(pj.TransportType.UDP) + + # Start the library + lib.start() + + # Create local/user-less account + acc = lib.create_account_for_transport(transport) + + # Make call + call = acc.make_call(sys.argv[1], MyCallCallback()) + + # Wait for ENTER before quitting + print "Press <ENTER> to quit" + input = sys.stdin.readline().rstrip("\r\n") + + # We're done, shutdown the library + lib.destroy() + lib = None + +except pj.Error, e: + print "Exception: " + str(e) + lib.destroy() + lib = None + sys.exit(1) + |