-
Notifications
You must be signed in to change notification settings - Fork 0
/
iaxclient.py
550 lines (453 loc) · 15.6 KB
/
iaxclient.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
'''Iax Client library wrapper'''
from ctypes import *
import time
import sys
import math
import os
IAXC_FORMAT_G723_1 = (1 << 0) # G.723.1 compression
IAXC_FORMAT_GSM = (1 << 1) # GSM compression
IAXC_FORMAT_ULAW = (1 << 2) # Raw mu-law data (G.711)
IAXC_FORMAT_ALAW = (1 << 3) # Raw A-law data (G.711)
IAXC_FORMAT_G726 = (1 << 4) # ADPCM, 32kbps
IAXC_FORMAT_ADPCM = (1 << 5) # ADPCM IMA
IAXC_FORMAT_SLINEAR = (1 << 6) # Raw 16-bit Signed Linear (8000 Hz) PCM
IAXC_FORMAT_LPC10 = (1 << 7) # LPC10, 180 samples/frame
IAXC_FORMAT_G729A = (1 << 8) # G.729a Audio
IAXC_FORMAT_SPEEX = (1 << 9) # Speex Audio
IAXC_FORMAT_ILBC = (1 << 10) # iLBC Audio
IAXC_CALL_STATE_FREE = 0
IAXC_CALL_STATE_ACTIVE = (1<<1)
IAXC_CALL_STATE_OUTGOING = (1<<2)
IAXC_CALL_STATE_RINGING = (1<<3)
IAXC_CALL_STATE_COMPLETE = (1<<4)
IAXC_CALL_STATE_SELECTED = (1<<5)
IAXC_CALL_STATE_BUSY = (1<<6)
IAXC_CALL_STATE_TRANSFER = (1<<7)
IAXC_EVENT_TEXT = 1
IAXC_EVENT_LEVELS = 2
IAXC_EVENT_STATE = 3
IAXC_EVENT_NETSTAT = 4
IAXC_EVENT_URL = 5 # URL push via IAX(2)
IAXC_EVENT_VIDEO = 6
IAXC_EVENT_REGISTRATION = 8
IAXC_EVENT_DTMF = 9
IAXC_EVENT_AUDIO = 10
IAXC_EVENT_VIDEOSTATS = 11
IAXC_EVENT_BUFSIZ = 256
# registration accepted
IAXC_REGISTRATION_REPLY_ACK = 18
# registration rejected
IAXC_REGISTRATION_REPLY_REJ = 30
# registraton timeout
IAXC_REGISTRATION_REPLY_TIMEOUT = 6
class Timeval(Structure):
pass
Timeval._fields_ = [("tv_sec", c_int),
("tv_usec", c_int)]
class EventLevels(Structure):
pass
EventLevels._fields_ = [("input", c_float),
("output", c_float)]
class EventText(Structure):
pass
EventText._fields_ = [("type", c_int),
("callNo", c_int),
("message", c_char * IAXC_EVENT_BUFSIZ)]
class EventCallState(Structure):
pass
EventCallState._fields_ = [("callNo", c_int),
("state", c_int),
("format", c_int),
("vformat", c_int),
("remote", c_char * IAXC_EVENT_BUFSIZ),
("remote_name", c_char * IAXC_EVENT_BUFSIZ),
("local", c_char * IAXC_EVENT_BUFSIZ),
("local_context", c_char * IAXC_EVENT_BUFSIZ)]
class Netstat(Structure):
pass
Netstat._fields_ = [("jitter", c_int),
("losspct", c_int),
("losscnt", c_int),
("packets", c_int),
("delay", c_int),
("dropped", c_int),
("ooo", c_int)]
class EventNetStats(Structure):
pass
EventNetStats._fields_ = [("callNo", c_int),
("rtt", c_int),
("local", Netstat),
("remote", Netstat)]
class VideoStat(Structure):
pass
VideoStat._fields_ = [("received_slices", c_ulong),
("acc_recv_size", c_ulong),
("sent_slices", c_ulong),
("acc_sent_size", c_ulong),
("dropped_frames", c_ulong),
("inbound_frames", c_ulong),
("outbound_frames", c_ulong),
("avg_inbound_fps", c_float),
("avg_inbound_bps", c_ulong),
("avg_outbound_fps", c_float),
("avg_outbound_bps", c_ulong),
("start_time", Timeval)]
class EventVideoStats(Structure):
pass
EventVideoStats._fields_ = [("callNo", c_int),
("stats", VideoStat)]
class EventUrl(Structure):
pass
EventUrl._fields_ = [("callNo", c_int),
("type", c_int),
("url", c_char * IAXC_EVENT_BUFSIZ)]
class EventVideo(Structure):
pass
EventVideo._fields_ = [("callNo", c_int),
("ts", c_uint),
("format", c_int),
("width", c_int),
("height", c_int),
("encoded", c_int),
("source", c_int),
("size", c_int),
("data", c_char_p)]
class EventAudio(Structure):
pass
EventAudio._fields_ = [("callNo", c_int),
("ts", c_uint),
("format", c_int),
("encoded", c_int),
("source", c_int),
("size", c_int),
("data", c_char_p)]
class EventRegistration(Structure):
pass
EventRegistration._fields_ = [("id", c_int),
("reply", c_int),
("msgcount", c_int)]
class Ev(Union):
pass
Ev._fields_ = [("levels", EventLevels),
("text", EventText),
("call", EventCallState),
("netstats", EventNetStats),
("videostats", EventVideoStats),
("url", EventUrl),
("video", EventVideo),
("audio", EventAudio),
("reg", EventRegistration)]
class Event(Structure):
pass
Event._fields_ = [("next", POINTER(Event)),
("type", c_int),
("ev", Ev)]
class Sound(Structure):
pass
Sound._fields_ = [("data", POINTER(c_short)),
("len", c_long),
("malloced", c_int),
("channel", c_int),
("repeat", c_int),
("pos", c_long),
("id", c_int),
("next", POINTER(Sound))]
DTMF_HZ = {'0':(1336, 941),
'1':(1209, 697),
'2':(1336, 697),
'3':(1477, 697),
'4':(1209, 770),
'5':(1336, 770),
'6':(1477, 770),
'7':(1209, 852),
'8':(1336, 852),
'9':(1477, 852),
'*':(1209, 941),
'#':(1477, 941)}
class IAXWrapper:
'''Wrapper class for iax client C library'''
def __init__(self):
if os.name == 'nt':
self.iax = cdll.libiaxclient
else:
try:
self.iax = cdll.LoadLibrary("libiaxclient.so")
except OSError, err:
self.iax = cdll.LoadLibrary("libiaxclient.so.1")
self._get_event = self.iax.iaxc_get_event_state
self._get_event.restype = POINTER(EventCallState)
self._get_event.argtypes = [POINTER(Event)]
self._play_sound = self.iax.iaxc_play_sound
self._play_sound.restype = c_int
self._play_sound.argtypes = [POINTER(Sound), c_int]
self._stop_sound = self.iax.iaxc_stop_sound
self._stop_sound.restype = c_int
self._stop_sound.argtypes = [c_int]
self._send_dtmf = self.iax.iaxc_send_dtmf
self._send_dtmf.argtypes = [c_char]
def initialize(self, maxcalls=1):
self.iax.iaxc_initialize(maxcalls)
def set_preferred_source_udp_port(self, sourceport=-1):
srcport = self.iax.iaxc_set_preferred_source_udp_port(sourceport)
return srcport
def set_audio_output(self, audiomode=0):
self.iax.iaxc_set_audio_output(audiomode)
def set_formats(self, preferred, allowed):
self.iax.iaxc_set_formats(preferred, allowed)
def set_callerid(self, name, number):
self.iax.iaxc_set_callerid(name, number)
def send_dtmf(self, digit):
self._send_dtmf(c_char(digit))
def play_sound(self, sound, ring):
soundid = self._play_sound(byref(sound), c_int(ring))
return soundid
def stop_sound(self, soundid):
self._stop_sound(c_int(soundid))
def register(self, user, password, host):
registerid = self.iax.iaxc_register(user, password, host)
return registerid
def register_ex(self, user, host, refresh):
registerid = self.iax.iaxc_register_ex(user, password, host, refresh)
return registerid
def unregister(self, registerid):
self.iax.iaxc_unregister(registerid)
def call(self, num):
return self.iax.iaxc_call(num)
def start_processing_thread(self):
self.iax.iaxc_start_processing_thread()
def stop_processing_thread(self):
self.iax.iaxc_stop_processing_thread()
def set_silence_threshold(self, thr):
self.iax.iaxc_set_silence_threshold(c_float(thr))
def dump_call(self):
self.iax.iaxc_dump_call()
def dump_all_calls(self):
self.iax.iaxc_dump_all_calls()
def dump_call_number(callid):
self.iax.iaxc_dump_call_number(callid)
def millisleep(self, ms):
self.iax.iaxc_millisleep(c_long(ms))
def shutdown(self):
self.iax.iaxc_shutdown()
def get_event(self, p_event):
return self._get_event(byref(p_event))
def set_event_callback(self, cb):
cb_func = CFUNCTYPE(c_int, Event)
self.pcb = cb_func(cb)
self.iax.iaxc_set_event_callback(self.pcb)
class IAXClient(IAXWrapper):
valid_dtmfs = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '*', '#']
def __init__(self,
prefered_codec=IAXC_FORMAT_ALAW,
other_codecs=[IAXC_FORMAT_ULAW, IAXC_FORMAT_GSM, IAXC_FORMAT_SPEEX, IAXC_FORMAT_ILBC],
ringin=False,
ringout=False,
debug=False):
IAXWrapper.__init__(self)
self.debug = debug
self.disconnected = False
self.ringin = ringin
self.ringout = ringout
# init iaxclient
self.set_preferred_source_udp_port(-1)
self.initialize()
# codec settings
self.pref_codec = prefered_codec
self.others_codecs = 0
self.others_codecs |= self.pref_codec
for codec in other_codecs:
if codec != self.pref_codec:
self.others_codecs |= codec
self.set_formats(self.pref_codec, self.others_codecs)
# set ring tones
if self.ringout:
self.low_ring = self._build_tone(1500, 500, 5500)
if self.ringin:
self.high_ring = self._build_tone(2500, 1200, 3000)
self.ringid = 0
# load tones for dtmf
self.tones = {}
for x in self.valid_dtmfs:
self.tones[x] = self._build_dtmf_tone(x)
# set event callback
self.set_event_callback(self.event_cb)
def _build_dtmf_tone(self, dtmf):
fq1 = DTMF_HZ[dtmf][0]
fq2 = DTMF_HZ[dtmf][1]
return self._build_tone(fq1, fq2, 500)
def _build_tone(self, fq1, fq2, length):
tone = Sound()
memset(byref(tone), c_int(0), sizeof(tone))
tone.len = length
ArrayData = c_short * tone.len
data = ArrayData()
data_l = []
for x in xrange(0, tone.len):
u1 = (0x7fff*0.4*math.sin(float(x)*fq1*math.pi/8000))
u2 = (0x7fff*0.4*math.sin(float(x)*fq2*math.pi/8000))
d = c_short(int(u1 + u2))
data[x] = d
tone.repeat = 0
tone.data = data
return tone
def play_low_ring(self):
if self.ringout:
self.ringid = self.play_sound(self.low_ring, 1)
self.millisleep(2500)
def play_high_ring(self):
if self.ringin:
for x in xrange(3):
self.ringid = self.play_sound(self.high_ring, 1)
self.millisleep(500)
self.millisleep(2500)
def _log(self, msg):
sys.stdout.write(msg+'\n')
sys.stdout.flush()
def log_debug(self, msg):
if self.debug:
msg = "iaxclient.py - DEBUG: "+str(msg)
self._log(msg)
def get_tone(self, dtmf):
return self.tones[dtmf]
def is_valid_dtmf(self, digit):
return digit in self.valid_dtmfs
def is_valid_dtmfs(self, dtmfs):
for c in dtmfs:
if c not in self.valid_dtmfs:
return False
return True
def is_call_disconnected(self):
return self.disconnected
def play_dtmf(self, dtmf):
tone = self.get_tone(dtmf)
self.play_sound(tone, 1)
def play_dtmfs(self, dtmfs, timerms=200):
'''play_dtmfs(str, int)'''
for x in dtmfs:
if not x in self.valid_dtmfs:
self.log_debug("Invalid dtmf : %s in '%s'" % (x, dtmfs))
return False
for x in dtmfs:
self.play_dtmf(x)
self.millisleep(timerms)
return True
def send_dtmfs(self, dtmfs, timerms=200):
'''send_dtmfs(str, int)'''
for x in dtmfs:
if not x in self.valid_dtmfs:
self.log_debug("Invalid dtmf : %s in '%s'" % (x, dtmfs))
return False
for x in dtmfs:
self.send_dtmf(x)
self.millisleep(timerms)
return True
def send_and_play_dtmfs(self, dtmfs, timerms=200):
'''send_and_play_dtmfs(str, int)'''
for x in dtmfs:
if not x in self.valid_dtmfs:
self.log_debug("Invalid dtmf : %s in '%s'" % (x, dtmfs))
return False
for x in dtmfs:
self.send_dtmf(x)
self.play_dtmf(x)
self.millisleep(timerms)
return True
def start(self, iaxuser, host, exten, iaxpw=None, context=None, port=4569, callername="Wphoniax", callernum="0000"):
self.set_callerid(callername, callernum)
self.set_silence_threshold(-99.0)
peer = iaxuser
if iaxpw:
peer += ":%s" % iaxpw
peer += "@%s" % host
peer += ":%d" % port
peer += "/%s" % exten
if context:
peer += "@%s" % context
self.log_debug("Calling %s at %s:%d" % (exten, host, port))
self.log_debug("Caller is %s <%s>" % (callernum, callername))
self.call(peer)
self.start_processing_thread()
self.log_debug("Started")
time.sleep(0.1)
def setup(self, iaxuser, host, exten, iaxpw=None, context=None, port=4569, callername="Wphoniax", callernum="0000"):
self.set_callerid(callername, callernum)
self.set_silence_threshold(-99.0)
peer = iaxuser
if iaxpw:
peer += ":%s" % iaxpw
peer += "@%s" % host
peer += ":%d" % port
peer += "/%s" % exten
if context:
peer += "@%s" % context
self.log_debug("Calling %s at %s:%d" % (exten, host, port))
self.log_debug("Caller is %s <%s>" % (callernum, callername))
self.call(peer)
self.log_debug("Setup")
def run(self):
self.log_debug("Run")
self.start_processing_thread()
while not self.is_call_disconnected():
self.millisleep(100)
def event_cb(self, ev):
if not ev:
return 1
if ev.type == IAXC_EVENT_DTMF:
self.log_debug("Dtmf received")
return 1
elif ev.type == IAXC_EVENT_STATE:
return self.handle_event_state(ev)
elif ev.type == IAXC_EVENT_REGISTRATION:
self.log_debug("Register event received")
return 1
return 1
def handle_event_state(self, ev_state):
self.log_debug("Disconnected=%s" % str(self.disconnected))
st = self.get_event(ev_state)
callno = st.contents.callNo
if st:
callstate = st.contents.state
self.log_debug("Callstate : %s" % str(callstate))
if callstate == 0:
if self.ringid:
self.stop_sound(self.ringid)
self.disconnected = True
self.log_debug("Call %d Hangup" % callno)
return 1
ringing = False
complete = False
outgoing = False
self.disconnected = False
if callstate & IAXC_CALL_STATE_RINGING:
ringing = True
if callstate & IAXC_CALL_STATE_ACTIVE:
if callstate & IAXC_CALL_STATE_COMPLETE:
complete = True
if callstate & IAXC_CALL_STATE_OUTGOING:
outgoing = True
if outgoing:
if ringing:
self.log_debug("Call %d OUT Ringing" % callno)
self.play_low_ring()
elif complete:
self.stop_sound(self.ringid)
self.log_debug("Call %d OUT Complete" % callno)
else:
self.log_debug("Call %d OUT Not Yet Accepted" % callno)
else:
if ringing:
self.log_debug("Call %d IN Ringing" % callno)
self.play_high_ring()
elif complete:
self.stop_sound(self.ringid)
self.log_debug("Call %d IN Complete" % callno)
else:
self.log_debug("Call %d IN Not Yet Accepted" % callno)
return 1
def hangup(self):
self.dump_call()
self.millisleep(600)
self.stop_processing_thread()
self.disconnected = True
def stop(self):
self.hangup()