~ubuntu-branches/ubuntu/wily/sflphone/wily

« back to all changes in this revision

Viewing changes to daemon/libs/pjproject-2.1.0/pjsip-apps/src/python/samples/call.py

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2015-01-07 14:51:16 UTC
  • mfrom: (4.3.5 sid)
  • Revision ID: package-import@ubuntu.com-20150107145116-yxnafinf4lrdvrmx
Tags: 1.4.1-0.1ubuntu1
* Merge with Debian, remaining changes:
 - Drop soprano, nepomuk build-dep
* Drop ubuntu patches, now upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# $Id: call.py 2171 2008-07-24 09:01:33Z bennylp $
2
 
#
3
 
# SIP call sample.
4
 
#
5
 
# Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
6
 
#
7
 
# This program is free software; you can redistribute it and/or modify
8
 
# it under the terms of the GNU General Public License as published by
9
 
# the Free Software Foundation; either version 2 of the License, or
10
 
# (at your option) any later version.
11
 
#
12
 
# This program is distributed in the hope that it will be useful,
13
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
# GNU General Public License for more details.
16
 
#
17
 
# You should have received a copy of the GNU General Public License
18
 
# along with this program; if not, write to the Free Software
19
 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
20
 
#
21
 
import sys
22
 
import pjsua as pj
23
 
 
24
 
LOG_LEVEL=3
25
 
current_call = None
26
 
 
27
 
# Logging callback
28
 
def log_cb(level, str, len):
29
 
    print str,
30
 
 
31
 
 
32
 
# Callback to receive events from account
33
 
class MyAccountCallback(pj.AccountCallback):
34
 
 
35
 
    def __init__(self, account=None):
36
 
        pj.AccountCallback.__init__(self, account)
37
 
 
38
 
    # Notification on incoming call
39
 
    def on_incoming_call(self, call):
40
 
        global current_call 
41
 
        if current_call:
42
 
            call.answer(486, "Busy")
43
 
            return
44
 
            
45
 
        print "Incoming call from ", call.info().remote_uri
46
 
        print "Press 'a' to answer"
47
 
 
48
 
        current_call = call
49
 
 
50
 
        call_cb = MyCallCallback(current_call)
51
 
        current_call.set_callback(call_cb)
52
 
 
53
 
        current_call.answer(180)
54
 
 
55
 
        
56
 
# Callback to receive events from Call
57
 
class MyCallCallback(pj.CallCallback):
58
 
 
59
 
    def __init__(self, call=None):
60
 
        pj.CallCallback.__init__(self, call)
61
 
 
62
 
    # Notification when call state has changed
63
 
    def on_state(self):
64
 
        global current_call
65
 
        print "Call with", self.call.info().remote_uri,
66
 
        print "is", self.call.info().state_text,
67
 
        print "last code =", self.call.info().last_code, 
68
 
        print "(" + self.call.info().last_reason + ")"
69
 
        
70
 
        if self.call.info().state == pj.CallState.DISCONNECTED:
71
 
            current_call = None
72
 
            print 'Current call is', current_call
73
 
 
74
 
    # Notification when call's media state has changed.
75
 
    def on_media_state(self):
76
 
        if self.call.info().media_state == pj.MediaState.ACTIVE:
77
 
            # Connect the call to sound device
78
 
            call_slot = self.call.info().conf_slot
79
 
            pj.Lib.instance().conf_connect(call_slot, 0)
80
 
            pj.Lib.instance().conf_connect(0, call_slot)
81
 
            print "Media is now active"
82
 
        else:
83
 
            print "Media is inactive"
84
 
 
85
 
# Function to make call
86
 
def make_call(uri):
87
 
    try:
88
 
        print "Making call to", uri
89
 
        return acc.make_call(uri, cb=MyCallCallback())
90
 
    except pj.Error, e:
91
 
        print "Exception: " + str(e)
92
 
        return None
93
 
        
94
 
 
95
 
# Create library instance
96
 
lib = pj.Lib()
97
 
 
98
 
try:
99
 
    # Init library with default config and some customized
100
 
    # logging config.
101
 
    lib.init(log_cfg = pj.LogConfig(level=LOG_LEVEL, callback=log_cb))
102
 
 
103
 
    # Create UDP transport which listens to any available port
104
 
    transport = lib.create_transport(pj.TransportType.UDP, 
105
 
                                     pj.TransportConfig(0))
106
 
    print "\nListening on", transport.info().host, 
107
 
    print "port", transport.info().port, "\n"
108
 
    
109
 
    # Start the library
110
 
    lib.start()
111
 
 
112
 
    # Create local account
113
 
    acc = lib.create_account_for_transport(transport, cb=MyAccountCallback())
114
 
 
115
 
    # If argument is specified then make call to the URI
116
 
    if len(sys.argv) > 1:
117
 
        lck = lib.auto_lock()
118
 
        current_call = make_call(sys.argv[1])
119
 
        print 'Current call is', current_call
120
 
        del lck
121
 
 
122
 
    my_sip_uri = "sip:" + transport.info().host + \
123
 
                 ":" + str(transport.info().port)
124
 
 
125
 
    # Menu loop
126
 
    while True:
127
 
        print "My SIP URI is", my_sip_uri
128
 
        print "Menu:  m=make call, h=hangup call, a=answer call, q=quit"
129
 
 
130
 
        input = sys.stdin.readline().rstrip("\r\n")
131
 
        if input == "m":
132
 
            if current_call:
133
 
                print "Already have another call"
134
 
                continue
135
 
            print "Enter destination URI to call: ", 
136
 
            input = sys.stdin.readline().rstrip("\r\n")
137
 
            if input == "":
138
 
                continue
139
 
            lck = lib.auto_lock()
140
 
            current_call = make_call(input)
141
 
            del lck
142
 
 
143
 
        elif input == "h":
144
 
            if not current_call:
145
 
                print "There is no call"
146
 
                continue
147
 
            current_call.hangup()
148
 
 
149
 
        elif input == "a":
150
 
            if not current_call:
151
 
                print "There is no call"
152
 
                continue
153
 
            current_call.answer(200)
154
 
 
155
 
        elif input == "q":
156
 
            break
157
 
 
158
 
    # Shutdown the library
159
 
    transport = None
160
 
    acc.delete()
161
 
    acc = None
162
 
    lib.destroy()
163
 
    lib = None
164
 
 
165
 
except pj.Error, e:
166
 
    print "Exception: " + str(e)
167
 
    lib.destroy()
168
 
    lib = None
169