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
|
import os
import sys
import pjsua as pj
import threading
import signal
import datetime
from time import sleep
from PyQt5 import QtCore, QtGui, QtWidgets
LOG_LEVEL=3
current_call = None
lib = None
acc = None
# Logging callback
def log_cb(level, str, len):
print str,
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.setEnabled(True)
MainWindow.resize(640, 480)
....
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 640, 20))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
self.answBtn.setText(_translate("MainWindow", "Answ"))
self.hangUpBtn.setText(_translate("MainWindow", "Hang"))
self.dadBtn.setText(_translate("MainWindow", "Dad"))
self.mumBtn.setText(_translate("MainWindow", "Mum"))
self.answBtn.clicked.connect(self.Answer)
self.hangUpBtn.clicked.connect(self.Hang)
self.dadBtn.clicked.connect(self.DialDad)
self.mumBtn.clicked.connect(self.DialMum)
def Answer(self):
if not current_call:
print "There is no call"
else:
current_call.answer(200)
self.signLbl.setText(str('talking to ' + uri))
def Hang(self):
if not current_call:
print "There is no call"
else:
current_call.hangup()
self.signLbl.setText("")
def DialDad(self):
self.make_call('uri:dad number')
self.signLbl.setText("Dialing Dad ! ! !")
def DialMum(self):
self.make_call('uri:mum number')
self.signLbl.setText("Dialing Mum ! ! !")
def signal_handler(signal, frame):
if acc is not None:
acc.delete()
acc = None
if lib is not None:
lib.destroy()
lib = None
sys.exit(0)
# Callback to receive events from account
class MyAccountCallback(pj.AccountCallback):
sem = None
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
current_call = call
call_cb = MyCallCallback(current_call)
current_call.set_callback(call_cb)
current_call.answer(180)
def wait(self):
self.sem = threading.Semaphore(0)
self.sem.acquire()
def on_reg_state(self):
print "Registration complete, status=", self.account.info().reg_status, "(" + self.account.info().reg_reason + ")"
if self.sem:
if self.account.info().reg_status >= 200:
self.sem.release()
# 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):
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 "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, 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)
print "\nListening on", transport.info().host,
print "port", transport.info().port, "\n"
# Start the library
lib.start()
# Create account
acc_cfg = pj.AccountConfig()
print pj.AccountConfig()
acc_cfg.id = 'sip:ID'
acc_cfg.reg_uri = 'sip:Registrar'
print acc_cfg.reg_uri
acc_cfg.auth_cred = [ pj.AuthCred('*', 'ID', 'PW') ]
print acc_cfg.auth_cred
acc_cfg.reg_timeout = 1800
acc_cb = MyAccountCallback()
acc = lib.create_account(acc_cfg, cb=acc_cb)
acc_cb.wait()
# 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)
print my_sip_uri
signal.signal(signal.SIGINT, signal_handler)
signal.pause()
acc.delete()
acc = None
lib.destroy()
lib = None
except pj.Error, e:
print "Exception: " + str(e)
if acc is not None:
acc.delete()
acc = None
if lib is not None:
lib.destroy()
lib = None
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_()) |
Partager