| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
import sys |
| 21 |
import pjsua as pj |
| 22 |
|
| 23 |
LOG_LEVEL=3 |
| 24 |
current_call = None |
| 25 |
|
| 26 |
|
| 27 |
def log_cb(level, str, len): |
| 28 |
print str, |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
class MyAccountCallback(pj.AccountCallback): |
| 33 |
|
| 34 |
def __init__(self, account=None): |
| 35 |
pj.AccountCallback.__init__(self, account) |
| 36 |
|
| 37 |
|
| 38 |
def on_incoming_call(self, call): |
| 39 |
global current_call |
| 40 |
if current_call: |
| 41 |
call.answer(486, "Busy") |
| 42 |
return |
| 43 |
|
| 44 |
print "Incoming call from ", call.info().remote_uri |
| 45 |
print "Press 'a' to answer" |
| 46 |
|
| 47 |
current_call = call |
| 48 |
|
| 49 |
call_cb = MyCallCallback(current_call) |
| 50 |
current_call.set_callback(call_cb) |
| 51 |
|
| 52 |
current_call.answer(180) |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
class MyCallCallback(pj.CallCallback): |
| 57 |
|
| 58 |
def __init__(self, call=None): |
| 59 |
pj.CallCallback.__init__(self, call) |
| 60 |
|
| 61 |
|
| 62 |
def on_state(self): |
| 63 |
global current_call |
| 64 |
print "Call with", self.call.info().remote_uri, |
| 65 |
print "is", self.call.info().state_text, |
| 66 |
print "last code =", self.call.info().last_code, |
| 67 |
print "(" + self.call.info().last_reason + ")" |
| 68 |
|
| 69 |
if self.call.info().state == pj.CallState.DISCONNECTED: |
| 70 |
current_call = None |
| 71 |
print 'Current call is', current_call |
| 72 |
|
| 73 |
|
| 74 |
def on_media_state(self): |
| 75 |
if self.call.info().media_state == pj.MediaState.ACTIVE: |
| 76 |
|
| 77 |
call_slot = self.call.info().conf_slot |
| 78 |
pj.Lib.instance().conf_connect(call_slot, 0) |
| 79 |
pj.Lib.instance().conf_connect(0, call_slot) |
| 80 |
print "Media is now active" |
| 81 |
else: |
| 82 |
print "Media is inactive" |
| 83 |
|
| 84 |
|
| 85 |
def make_call(uri): |
| 86 |
try: |
| 87 |
print "Making call to", uri |
| 88 |
return acc.make_call(uri, cb=MyCallCallback()) |
| 89 |
except pj.Error, e: |
| 90 |
print "Exception: " + str(e) |
| 91 |
return None |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
lib = pj.Lib() |
| 96 |
|
| 97 |
try: |
| 98 |
|
| 99 |
|
| 100 |
lib.init(log_cfg = pj.LogConfig(level=LOG_LEVEL, callback=log_cb)) |
| 101 |
|
| 102 |
|
| 103 |
transport = lib.create_transport(pj.TransportType.UDP, |
| 104 |
pj.TransportConfig(0)) |
| 105 |
print "\nListening on", transport.info().host, |
| 106 |
print "port", transport.info().port, "\n" |
| 107 |
|
| 108 |
|
| 109 |
lib.start() |
| 110 |
|
| 111 |
|
| 112 |
acc = lib.create_account_for_transport(transport, cb=MyAccountCallback()) |
| 113 |
|
| 114 |
|
| 115 |
if len(sys.argv) > 1: |
| 116 |
lck = lib.auto_lock() |
| 117 |
current_call = make_call(sys.argv[1]) |
| 118 |
print 'Current call is', current_call |
| 119 |
del lck |
| 120 |
|
| 121 |
my_sip_uri = "sip:" + transport.info().host + \ |
| 122 |
":" + str(transport.info().port) |
| 123 |
|
| 124 |
|
| 125 |
while True: |
| 126 |
print "My SIP URI is", my_sip_uri |
| 127 |
print "Menu: m=make call, h=hangup call, a=answer call, q=quit" |
| 128 |
|
| 129 |
input = sys.stdin.readline().rstrip("\r\n") |
| 130 |
if input == "m": |
| 131 |
if current_call: |
| 132 |
print "Already have another call" |
| 133 |
continue |
| 134 |
print "Enter destination URI to call: ", |
| 135 |
input = sys.stdin.readline().rstrip("\r\n") |
| 136 |
if input == "": |
| 137 |
continue |
| 138 |
lck = lib.auto_lock() |
| 139 |
current_call = make_call(input) |
| 140 |
del lck |
| 141 |
|
| 142 |
elif input == "h": |
| 143 |
if not current_call: |
| 144 |
print "There is no call" |
| 145 |
continue |
| 146 |
current_call.hangup() |
| 147 |
|
| 148 |
elif input == "a": |
| 149 |
if not current_call: |
| 150 |
print "There is no call" |
| 151 |
continue |
| 152 |
current_call.answer(200) |
| 153 |
|
| 154 |
elif input == "q": |
| 155 |
break |
| 156 |
|
| 157 |
|
| 158 |
transport = None |
| 159 |
acc.delete() |
| 160 |
acc = None |
| 161 |
lib.destroy() |
| 162 |
lib = None |
| 163 |
|
| 164 |
except pj.Error, e: |
| 165 |
print "Exception: " + str(e) |
| 166 |
lib.destroy() |
| 167 |
lib = None |
| 168 |
|