-
Notifications
You must be signed in to change notification settings - Fork 13
/
sipamos.py
130 lines (100 loc) · 3.67 KB
/
sipamos.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import sys
import pjsua
import threading
import wave
from time import sleep
def log_cb(level, str, len):
print str,
class MyAccountCallback(pjsua.AccountCallback):
sem = None
def __init__(self, account=None):
pjsua.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()
def cb_func(pid) :
print '%s playback is done' % pid
current_call.hangup()
# Callback to receive events from Call
class MyCallCallback(pjsua.CallCallback):
def __init__(self, call=None):
pjsua.CallCallback.__init__(self, call)
# Notification when call state has changed
def on_state(self):
global current_call
global in_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 == pjsua.CallState.DISCONNECTED:
current_call = None
print 'Current call is', current_call
in_call = False
elif self.call.info().state == pjsua.CallState.CONFIRMED:
#Call is Answred
print "Call Answred"
wfile = wave.open("message.wav")
time = (1.0 * wfile.getnframes ()) / wfile.getframerate ()
print str(time) + "ms"
wfile.close()
call_slot = self.call.info().conf_slot
self.wav_player_id=pjsua.Lib.instance().create_player('message.wav',loop=False)
self.wav_slot=pjsua.Lib.instance().player_get_slot(self.wav_player_id)
pjsua.Lib.instance().conf_connect(self.wav_slot, call_slot)
sleep(time)
pjsua.Lib.instance().player_destroy(self.wav_player_id)
self.call.hangup()
in_call = False
# Notification when call's media state has changed.
def on_media_state(self):
if self.call.info().media_state == pjsua.MediaState.ACTIVE:
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 pjsua.Error, e:
print "Exception: " + str(e)
return None
lib = pjsua.Lib()
try:
lib.init(log_cfg = pjsua.LogConfig(level=4, callback=log_cb))
lib.create_transport(pjsua.TransportType.UDP, pjsua.TransportConfig(5080))
lib.set_null_snd_dev()
lib.start()
lib.handle_events()
acc_cfg = pjsua.AccountConfig()
acc_cfg.id = "sip:[email protected]"
acc_cfg.reg_uri = "sip:YOURSIPSERVER.COM"
acc_cfg.proxy = [ "sip:PROXY.YOURSIPSERVER.COM;lr" ]
acc_cfg.auth_cred = [ pjsua.AuthCred("*", "[email protected]", "YOURPASSWORD") ]
acc_cb = MyAccountCallback()
acc = lib.create_account(acc_cfg, cb=acc_cb)
acc_cb.wait()
print "\n"
print "Registration complete, status=", acc.info().reg_status, \
"(" + acc.info().reg_reason + ")"
#YOURDESTINATION is landline or mobile number you want to call
dst_uri="sip:[email protected]"
in_call = True
lck = lib.auto_lock()
current_call = make_call(dst_uri)
print 'Current call is', current_call
del lck
#wait for the call to end before shuting down
while in_call:
pass
#sys.stdin.readline()
lib.destroy()
lib = None
except pjsua.Error, e:
print "Exception: " + str(e)
lib.destroy()