class Client: def __init__(self, name, server_addr): self.SERVER_ADDR = server_addr self.name = name self.voip_params = {u'username': self.name, u'sip_server_pwd': u'12345678abcd', u'sip_server_address': self.SERVER_ADDR, u'sip_server_user': self.name, u'sip_server_transport': u'udp', u'log_level': 1, u'debug': True} self.CALL_ACTIVE = 0 self.myVoip = VoipLib() print "Initializing the Voip Lib..." self.myVoip.init_lib(self.voip_params, self.call_events) print "Registering the account on the Sip Server..." self.myVoip.register_account() self.caller = 0 self.extension = None def set_caller(self, caller): self.caller = caller def set_exten(self, extension): self.extension = extension def call_events(self, voip_event_type, voip_event, params): print "Received event type:%s Event:%s -> Params: %s" % (voip_event_type, voip_event, params) # event triggered when the account registration has been confirmed by the remote Sip Server if voip_event == VoipEvent.ACCOUNT_REGISTERED: print "Account registered" # event triggered when a new call is incoming elif voip_event == VoipEvent.CALL_INCOMING: print "INCOMING CALL From %s" % params["from"] time.sleep(2) print "Answering..." self.myVoip.answer_call() # event triggered when a call has been established elif voip_event == VoipEvent.CALL_ACTIVE: print "The call with %s has been established" % self.myVoip.get_call().get_remote_uri() self.CALL_ACTIVE = 1 # events triggered when the call ends for some reasons elif (voip_event in [VoipEvent.CALL_REMOTE_DISCONNECTION_HANGUP, VoipEvent.CALL_REMOTE_HANGUP, VoipEvent.CALL_HANGUP]): print "End of call." self.CALL_ACTIVE = 0 # event triggered when the library was destroyed elif voip_event == VoipEvent.LIB_DEINITIALIZED: print "Lib Destroyed. Exiting from the app." return # just print informations about other events triggered by the library else: print "Received unhandled event type:%s --> %s" % (voip_event_type, voip_event) def start_call(self): if self.caller == 1: print "Making a call dialing the extension: %s" % self.extension self.myVoip.make_call(self.extension) def hangup(self): self.myVoip.hangup_call() def end(self): self.myVoip.destroy_lib()
class MostVoipGUI(QtGui.QMainWindow): # pyqt signal for most voip event notifications voip_signal = pyqtSignal(object, object, object) def __init__(self): QtGui.QMainWindow.__init__(self) self.setWindowTitle('Most Voip Demo Application') self._setup_logger() self.selectedAccount = self.selectedAccountDetails = self.selectedAccountBuddies = None self.voip_signal.connect(self.notify_events) self.myVoip = VoipLib() self._build_GUI() self._setupButtonsByVoipState() def _setup_logger(self): global logger if not logger: logger = logging.getLogger("VoipDemo") #('Voip') handler = logging.StreamHandler() # rootFormatter = logging.Formatter('%(name)s - %(levelname)s: %(msg)s') # handler.setFormatter(rootFormatter) logger.addHandler(handler) logger.setLevel(logging.DEBUG) #print "NUM LOGGER HANDLERS:%s" % len(logger.handlers) def voip_notify_events(self, voip_event_type, voip_event, params): self.voip_signal.emit(voip_event_type, voip_event, params) def notify_events(self, voip_event_type, voip_event, params): msg = "%s: %s" % (voip_event_type, voip_event) logger.debug("\n\nEVENT:%s\n\n" % msg) self.statusBar().showMessage(msg) self._update_status_labels() self._setupButtonsByVoipState() if voip_event == VoipEvent.LIB_INITIALIZED: self.myVoip.register_account() elif voip_event == VoipEvent.ACCOUNT_REGISTERED: self._add_buddies() self._update_buddy_list() elif voip_event_type == VoipEventType.BUDDY_EVENT: self._update_buddy_list() def _add_buddies(self): if not self.selectedAccountBuddies: return logger.debug("Adding buddies...") for b in self.selectedAccountBuddies: # the buddy related to the current registered account is not to be included if b["extension"] != self.selectedAccountDetails["extension"]: self.myVoip.get_account().add_buddy(b["extension"]) def _update_buddy_list(self): buddies = self.myVoip.get_account().get_buddies() logger.debug("Update Buddy Model...") self.buddiesModel.clear() for b in buddies: msg = "%s (%s)" % (b.get_extension(), b.get_status_text()) logger.debug("Appending buddy:%s" % msg) item = QtGui.QStandardItem(msg) self.buddiesModel.appendRow(item) def get_init_params(self): self.selectedAccount, self.selectedAccountDetails, self.selectedAccountBuddies, ok = AccountPickerDialog.getSelectedAccount( self.txtwebServerAddress.text(), self) print "SELECTED ACCOUNT:%s -> Data: %s Details:%s" % ( ok, self.selectedAccount, str(self.selectedAccountDetails)) if (not ok): raise Exception("No Valid Account Configuration Loaded") voip_params0 = { u'username': self.selectedAccount["name"], u'sip_server_pwd': self.selectedAccountDetails["sip_server"]["pwd"], u'sip_server_address': self.selectedAccountDetails["sip_server"] ["address"], #'u'192.168.1.79', u'sip_server_user': self.selectedAccountDetails["sip_server"]["user"], u'sip_server_transport': self.selectedAccountDetails["sip_server"]["transport"], #u'turn_server_address': self.selectedAccountDetails["turn_server"]["address"], #u'turn_server_user': self.selectedAccountDetails["turn_server"]["user"], #u'turn_server_pwd': self.selectedAccountDetails["turn_server"]["pwd"], u'log_level': 1, u'debug': True } print "VOIP CONF DICT:%s" % voip_params0 return voip_params0 def init_voip_lib(self): print "Called init_voip_lib" self.voip_params = self.get_init_params() self._update_account_details_info() self.myVoip.init_lib(self.voip_params, self.voip_notify_events) def _update_account_details_info(self): if self.selectedAccount: accountInfo = "%s [SIP USERMAME: %s - SIP SERVER:%s]" % ( self.selectedAccount["name"], self.selectedAccountDetails["sip_server"]["user"], self.selectedAccountDetails["sip_server"]["address"]) self.labAccountDetails.setText(accountInfo) else: self.labAccountDetails.setText("N.A") def _update_status_labels(self): self._update_server_state() self._update_call_state() self._update_account_state() def _update_server_state(self): server_state = self.myVoip.get_server().get_state() self.labServerStateInfo.setText(server_state) def _update_call_state(self): call_state = self.myVoip.get_call().get_state() self.labCallStateInfo.setText(call_state) def _update_account_state(self): account_state = self.myVoip.get_account().get_state() self.labAccountStateInfo.setText(account_state) def on_buddy_selected(self, item): buddies = self.myVoip.get_account().get_buddies() buddy_ext = buddies[item.row()].get_extension() self.txtExtension.setText(buddy_ext) def on_make_call_button_clicked(self): extension = self.txtExtension.text() logger.debug("Making call to %s" % extension) self.myVoip.make_call(extension) def _setupButtonsByVoipState(self): #myServerState = self.myVoip.get_server().get_state() myAccountState = self.myVoip.get_account().get_state() if myAccountState == AccountState.UNREGISTERED: self.butMakeCall.setEnabled(False) self.butAnswer.setEnabled(False) self.butHold.setEnabled(False) self.butHangup.setEnabled(False) self.butInit.setEnabled(True) else: myCallState = self.myVoip.get_call().get_state() if myCallState == CallState.IDLE: self.butMakeCall.setEnabled(True) self.butAnswer.setEnabled(False) self.butHangup.setEnabled(False) self.butHold.setEnabled(False) elif myCallState == CallState.INCOMING: self.butMakeCall.setEnabled(False) self.butAnswer.setEnabled(True) self.butHangup.setEnabled(True) self.butHold.setEnabled(False) elif myCallState == CallState.DIALING: self.butMakeCall.setEnabled(False) self.butAnswer.setEnabled(False) self.butHangup.setEnabled(True) self.butHold.setEnabled(False) elif myCallState == CallState.HOLDING: self.butMakeCall.setEnabled(False) self.butAnswer.setEnabled(False) self.butHangup.setEnabled(True) self.butHold.setEnabled(True) self.butHold.setText("Unhold") elif myCallState == CallState.ACTIVE: self.butMakeCall.setEnabled(False) self.butAnswer.setEnabled(False) self.butHangup.setEnabled(True) self.butHold.setEnabled(True) self.butHold.setText("Hold") def on_hold_toggle_button_clicked(self): if (self.myVoip.get_call().get_state() == CallState.ACTIVE): self.myVoip.hold_call() elif (self.myVoip.get_call().get_state() == CallState.HOLDING): self.myVoip.unhold_call() def on_answer_button_clicked(self): self.myVoip.answer_call() def on_hangup_button_clicked(self): self.myVoip.hangup_call() # ----------------------------------------------------------------------------- # GUI BUILDING SECTION # ----------------------------------------------------------------------------- def _build_GUI(self): cWidget = QtGui.QWidget(self) vBox = QtGui.QVBoxLayout() vBox.setSpacing(5) vBox.addLayout(self._buildAccountDetailsPanel(cWidget)) vBox.addLayout(self._buildStatesPanel(cWidget)) vBox.addLayout(self._buildBuddiesPanel(cWidget)) vBox.addLayout(self._buildMakeCallPanel(cWidget)) vBox.addLayout(self._buildLoadConfigPanel(cWidget)) vBox.addLayout(self._buildButtonsPanel(cWidget)) self.setMinimumWidth(600) cWidget.setLayout(vBox) self.setCentralWidget(cWidget) self.statusBar().showMessage( 'MostVoip Event Log') # crea una veloce barra di stato def _buildAccountDetailsPanel(self, cWidget): hBox1 = QtGui.QHBoxLayout() hBox1.setSpacing(5) labAccountDetailsTitle = QtGui.QLabel('Account', cWidget) self.labAccountDetails = QtGui.QLabel('N.A', cWidget) hBox1.addWidget(labAccountDetailsTitle) hBox1.addWidget(self.labAccountDetails) return hBox1 def _buildBuddiesPanel(self, cWidget): vBox = QtGui.QVBoxLayout() vBox.setSpacing(5) self.buddiesList = QtGui.QListView() self.buddiesList.setWindowTitle('Buddies') self.buddiesList.clicked.connect(self.on_buddy_selected) #self.buddiesList.setMinimumSize(600, 400) # Create an empty model for the list's data self.buddiesModel = QtGui.QStandardItemModel(self.buddiesList) self.buddiesList.setModel(self.buddiesModel) self.buddiesList.show() vBox.addWidget(QtGui.QLabel('Buddies', cWidget)) vBox.addWidget(self.buddiesList) return vBox def _buildStatesPanel(self, cWidget): hBox1 = QtGui.QHBoxLayout() hBox1.setSpacing(5) labServerState = QtGui.QLabel('Server State', cWidget) self.labServerStateInfo = QtGui.QLabel('N.A', cWidget) hBox1.addWidget(labServerState) hBox1.addWidget(self.labServerStateInfo) hBox2 = QtGui.QHBoxLayout() hBox2.setSpacing(5) labCallState = QtGui.QLabel('Call State', cWidget) self.labCallStateInfo = QtGui.QLabel('N.A', cWidget) hBox2.addWidget(labCallState) hBox2.addWidget(self.labCallStateInfo) hBox3 = QtGui.QHBoxLayout() hBox3.setSpacing(5) labAccountState = QtGui.QLabel('Account State', cWidget) self.labAccountStateInfo = QtGui.QLabel('N.A', cWidget) hBox3.addWidget(labAccountState) hBox3.addWidget(self.labAccountStateInfo) vBox = QtGui.QVBoxLayout() vBox.setSpacing(5) vBox.addLayout(hBox1) vBox.addLayout(hBox2) vBox.addLayout(hBox3) return vBox def _buildMakeCallPanel(self, cWidget): hBox = QtGui.QHBoxLayout() hBox.setSpacing(2) self.butMakeCall = QtGui.QPushButton('Make Call', cWidget) self.connect(self.butMakeCall, QtCore.SIGNAL('clicked()'), self.on_make_call_button_clicked) labExtension = QtGui.QLabel('Extension: ', cWidget) self.txtExtension = QtGui.QLineEdit(cWidget) hBox.addWidget(labExtension) hBox.addWidget(self.txtExtension) hBox.addWidget(self.butMakeCall) return hBox def _buildLoadConfigPanel(self, cWidget): hBox = QtGui.QHBoxLayout() hBox.setSpacing(2) labWebServerAddress = QtGui.QLabel('Web Server Address: ', cWidget) self.txtwebServerAddress = QtGui.QLineEdit(cWidget) self.txtwebServerAddress.setText("http://localhost:8000") hBox.addWidget(labWebServerAddress) hBox.addWidget(self.txtwebServerAddress) return hBox def _buildButtonsPanel(self, cWidget): hBox = QtGui.QHBoxLayout() hBox.setSpacing(2) self.butInit = QtGui.QPushButton('Init', cWidget) self.butAnswer = QtGui.QPushButton('Answer', cWidget) self.butHold = QtGui.QPushButton('Hold', cWidget) #self.butHold.setCheckable(True); self.butHangup = QtGui.QPushButton('Hangup', cWidget) hBox.addWidget(self.butInit) hBox.addWidget(self.butAnswer) hBox.addWidget(self.butHold) hBox.addWidget(self.butHangup) self.connect(self.butInit, QtCore.SIGNAL('clicked()'), self.init_voip_lib) self.connect(self.butHold, QtCore.SIGNAL('clicked()'), self.on_hold_toggle_button_clicked) self.connect(self.butAnswer, QtCore.SIGNAL('clicked()'), self.on_answer_button_clicked) self.connect(self.butHangup, QtCore.SIGNAL('clicked()'), self.on_hangup_button_clicked) return hBox
class MostVoipGUI(QtGui.QMainWindow): # pyqt signal for most voip event notifications voip_signal = pyqtSignal(object, object, object) def __init__(self): QtGui.QMainWindow.__init__(self) self.setWindowTitle('Most Voip Demo Application') self._setup_logger() self.voip_signal.connect(self.notify_events) self.myVoip = VoipLib() self._build_GUI() self._setupButtonsByVoipState() def _setup_logger(self): global logger if not logger: logger = logging.getLogger("VoipDemo") #('Voip') handler = logging.StreamHandler() # rootFormatter = logging.Formatter('%(name)s - %(levelname)s: %(msg)s') # handler.setFormatter(rootFormatter) logger.addHandler(handler) logger.setLevel(logging.DEBUG) #print "NUM LOGGER HANDLERS:%s" % len(logger.handlers) def voip_notify_events(self, voip_event_type, voip_event, params): self.voip_signal.emit(voip_event_type, voip_event, params) def notify_events(self, voip_event_type, voip_event, params): msg = "%s: %s" % (voip_event_type, voip_event) logger.debug("\n\nEVENT:%s\n\n" % msg) self.statusBar().showMessage(msg) self._update_status_labels() self._setupButtonsByVoipState() if voip_event == VoipEvent.LIB_INITIALIZED: self.myVoip.register_account() elif voip_event == VoipEvent.ACCOUNT_REGISTERED: self._add_buddies() self._update_buddy_list() elif voip_event_type == VoipEventType.BUDDY_EVENT: self._update_buddy_list() def _add_buddies(self): buddy_extensions = ["ste", "steand", "ste2"] logger.debug("Adding buddies...") for ext in buddy_extensions: if ext != self.voip_params.get("sip_server_user"): self.myVoip.get_account().add_buddy(ext) def _update_buddy_list(self): buddies = self.myVoip.get_account().get_buddies() logger.debug("Update Buddy Model...") self.buddiesModel.clear() for b in buddies: msg = "%s (%s)" % (b.get_extension(), b.get_status_text()) logger.debug("Appending buddy:%s" % msg) item = QtGui.QStandardItem(msg) self.buddiesModel.appendRow(item) def get_init_params(self): voip_params0 = { u'username': u'ste', u'sip_server_pwd': u'ste', u'sip_server_address': u'156.148.33.226:5062', # u'192.168.1.100' , 156.148.132.243 u'sip_server_user': u'ste', u'sip_server_transport': u'tcp', u'turn_server_address': u'156.148.33.226', u'turn_server_user': u'ste', u'turn_server_pwd': u'ste', u'log_level': 1, u'debug': True } return voip_params0 def init_voip_lib(self): print "Called init_voip_lib" self.voip_params = self.get_init_params() self.myVoip.init_lib(self.voip_params, self.voip_notify_events) def _update_status_labels(self): self._update_sip_username() self._update_server_state() self._update_call_state() self._update_account_state() def _update_sip_username(self): self.labSipUsernameInfo.setText( self.voip_params.get("sip_server_user", "N.A")) def _update_server_state(self): server_state = self.myVoip.get_server().get_state() self.labServerStateInfo.setText(server_state) def _update_call_state(self): call_state = self.myVoip.get_call().get_state() self.labCallStateInfo.setText(call_state) def _update_account_state(self): account_state = self.myVoip.get_account().get_state() self.labAccountStateInfo.setText(account_state) def on_buddy_selected(self, item): buddies = self.myVoip.get_account().get_buddies() buddy_ext = buddies[item.row()].get_extension() self.txtExtension.setText(buddy_ext) def on_make_call_button_clicked(self): extension = self.txtExtension.text() logger.debug("Making call to %s" % extension) self.myVoip.make_call(extension) def _setupButtonsByVoipState(self): #myServerState = self.myVoip.get_server().get_state() myAccountState = self.myVoip.get_account().get_state() if myAccountState == AccountState.UNREGISTERED: self.butMakeCall.setEnabled(False) self.butAnswer.setEnabled(False) self.butHold.setEnabled(False) self.butHangup.setEnabled(False) self.butInit.setEnabled(True) else: myCallState = self.myVoip.get_call().get_state() if myCallState == CallState.IDLE: self.butMakeCall.setEnabled(True) self.butAnswer.setEnabled(False) self.butHangup.setEnabled(False) self.butHold.setText("Hold") self.butHold.setEnabled(False) elif myCallState == CallState.INCOMING: self.butMakeCall.setEnabled(False) self.butAnswer.setEnabled(True) self.butHangup.setEnabled(True) self.butHold.setText("Hold") self.butHold.setEnabled(False) elif myCallState == CallState.DIALING: self.butMakeCall.setEnabled(False) self.butAnswer.setEnabled(False) self.butHangup.setEnabled(True) self.butHold.setText("Hold") self.butHold.setEnabled(False) elif myCallState == CallState.HOLDING: self.butMakeCall.setEnabled(False) self.butAnswer.setEnabled(False) self.butHangup.setEnabled(True) self.butHold.setEnabled(True) self.butHold.setText("Unhold") elif myCallState == CallState.ACTIVE: self.butMakeCall.setEnabled(False) self.butAnswer.setEnabled(False) self.butHangup.setEnabled(True) self.butHold.setEnabled(True) self.butHold.setText("Hold") def on_hold_toggle_button_clicked(self): if (self.myVoip.get_call().get_state() == CallState.ACTIVE): self.myVoip.hold_call() elif (self.myVoip.get_call().get_state() == CallState.HOLDING): self.myVoip.unhold_call() def on_answer_button_clicked(self): self.myVoip.answer_call() def on_hangup_button_clicked(self): self.myVoip.hangup_call() # ----------------------------------------------------------------------------- # GUI BUILDING SECTION # ----------------------------------------------------------------------------- def _build_GUI(self): cWidget = QtGui.QWidget(self) vBox = QtGui.QVBoxLayout() vBox.setSpacing(5) vBox.addLayout(self._buildStatesPanel(cWidget)) vBox.addLayout(self._buildBuddiesPanel(cWidget)) vBox.addLayout(self._buildMakeCallPanel(cWidget)) vBox.addLayout(self._buildButtonsPanel(cWidget)) self.setMinimumWidth(600) cWidget.setLayout(vBox) self.setCentralWidget(cWidget) self.statusBar().showMessage( 'MostVoip Event Log') # crea una veloce barra di stato def _buildBuddiesPanel(self, cWidget): vBox = QtGui.QVBoxLayout() vBox.setSpacing(5) self.buddiesList = QtGui.QListView() self.buddiesList.setWindowTitle('Buddies') self.buddiesList.clicked.connect(self.on_buddy_selected) #self.buddiesList.setMinimumSize(600, 400) # Create an empty model for the list's data self.buddiesModel = QtGui.QStandardItemModel(self.buddiesList) self.buddiesList.setModel(self.buddiesModel) self.buddiesList.show() vBox.addWidget(QtGui.QLabel('Buddies', cWidget)) vBox.addWidget(self.buddiesList) return vBox def _buildStatesPanel(self, cWidget): hBox0 = QtGui.QHBoxLayout() hBox0.setSpacing(5) labSipUsername = QtGui.QLabel('Sip Username', cWidget) self.labSipUsernameInfo = QtGui.QLabel('N.A', cWidget) hBox0.addWidget(labSipUsername) hBox0.addWidget(self.labSipUsernameInfo) hBox1 = QtGui.QHBoxLayout() hBox1.setSpacing(5) labServerState = QtGui.QLabel('Server State', cWidget) self.labServerStateInfo = QtGui.QLabel('N.A', cWidget) hBox1.addWidget(labServerState) hBox1.addWidget(self.labServerStateInfo) hBox2 = QtGui.QHBoxLayout() hBox2.setSpacing(5) labCallState = QtGui.QLabel('Call State', cWidget) self.labCallStateInfo = QtGui.QLabel('N.A', cWidget) hBox2.addWidget(labCallState) hBox2.addWidget(self.labCallStateInfo) hBox3 = QtGui.QHBoxLayout() hBox3.setSpacing(5) labAccountState = QtGui.QLabel('Account State', cWidget) self.labAccountStateInfo = QtGui.QLabel('N.A', cWidget) hBox3.addWidget(labAccountState) hBox3.addWidget(self.labAccountStateInfo) vBox = QtGui.QVBoxLayout() vBox.setSpacing(5) vBox.addLayout(hBox0) vBox.addLayout(hBox1) vBox.addLayout(hBox2) vBox.addLayout(hBox3) return vBox def _buildMakeCallPanel(self, cWidget): hBox = QtGui.QHBoxLayout() hBox.setSpacing(2) self.butMakeCall = QtGui.QPushButton('Make Call', cWidget) self.connect(self.butMakeCall, QtCore.SIGNAL('clicked()'), self.on_make_call_button_clicked) labExtension = QtGui.QLabel('Extension', cWidget) self.txtExtension = QtGui.QLineEdit(cWidget) hBox.addWidget(labExtension) hBox.addWidget(self.txtExtension) hBox.addWidget(self.butMakeCall) return hBox def _buildButtonsPanel(self, cWidget): hBox = QtGui.QHBoxLayout() hBox.setSpacing(2) self.butInit = QtGui.QPushButton('Init', cWidget) self.butAnswer = QtGui.QPushButton('Answer', cWidget) self.butHold = QtGui.QPushButton('Hold', cWidget) #self.butHold.setCheckable(True); self.butHangup = QtGui.QPushButton('Hangup', cWidget) hBox.addWidget(self.butInit) hBox.addWidget(self.butAnswer) hBox.addWidget(self.butHold) hBox.addWidget(self.butHangup) self.connect(self.butInit, QtCore.SIGNAL('clicked()'), self.init_voip_lib) self.connect(self.butHold, QtCore.SIGNAL('clicked()'), self.on_hold_toggle_button_clicked) self.connect(self.butAnswer, QtCore.SIGNAL('clicked()'), self.on_answer_button_clicked) self.connect(self.butHangup, QtCore.SIGNAL('clicked()'), self.on_hangup_button_clicked) return hBox
class MostVoipGUI(QtGui.QMainWindow): # pyqt signal for most voip event notifications voip_signal = pyqtSignal(object,object,object) def __init__(self): QtGui.QMainWindow.__init__(self) self.setWindowTitle('Most Voip Demo Application') self._setup_logger() self.selectedAccount = self.selectedAccountDetails = self.selectedAccountBuddies = None self.voip_signal.connect(self.notify_events) self.myVoip = VoipLib() self._build_GUI() self._setupButtonsByVoipState() def _setup_logger(self): global logger if not logger: logger = logging.getLogger("VoipDemo") #('Voip') handler = logging.StreamHandler() # rootFormatter = logging.Formatter('%(name)s - %(levelname)s: %(msg)s') # handler.setFormatter(rootFormatter) logger.addHandler(handler) logger.setLevel(logging.DEBUG) #print "NUM LOGGER HANDLERS:%s" % len(logger.handlers) def voip_notify_events(self, voip_event_type,voip_event, params): self.voip_signal.emit(voip_event_type,voip_event, params) def notify_events(self, voip_event_type,voip_event, params): msg = "%s: %s" % (voip_event_type,voip_event) logger.debug("\n\nEVENT:%s\n\n" % msg) self.statusBar().showMessage(msg) self._update_status_labels() self._setupButtonsByVoipState() if voip_event==VoipEvent.LIB_INITIALIZED: self.myVoip.register_account(); elif voip_event==VoipEvent.ACCOUNT_REGISTERED: self._add_buddies() self._update_buddy_list() elif voip_event_type==VoipEventType.BUDDY_EVENT: self._update_buddy_list() def _add_buddies(self): if not self.selectedAccountBuddies: return logger.debug("Adding buddies...") for b in self.selectedAccountBuddies: # the buddy related to the current registered account is not to be included if b["extension"]!=self.selectedAccountDetails["extension"]: self.myVoip.get_account().add_buddy(b["extension"]) def _update_buddy_list(self): buddies = self.myVoip.get_account().get_buddies() logger.debug("Update Buddy Model...") self.buddiesModel.clear(); for b in buddies: msg = "%s (%s)" % (b.get_extension(), b.get_status_text()) logger.debug("Appending buddy:%s" % msg) item = QtGui.QStandardItem(msg) self.buddiesModel.appendRow(item) def get_init_params(self): self.selectedAccount, self.selectedAccountDetails , self.selectedAccountBuddies, ok = AccountPickerDialog.getSelectedAccount(self.txtwebServerAddress.text(), self) print "SELECTED ACCOUNT:%s -> Data: %s Details:%s" % (ok, self.selectedAccount, str(self.selectedAccountDetails)) if (not ok): raise Exception("No Valid Account Configuration Loaded") voip_params0 = {u'username': self.selectedAccount["name"], u'sip_server_pwd': self.selectedAccountDetails["sip_server"]["pwd"], u'sip_server_address':self.selectedAccountDetails["sip_server"]["address"] , #'u'192.168.1.79', u'sip_server_user': self.selectedAccountDetails["sip_server"]["user"], u'sip_server_transport' :self.selectedAccountDetails["sip_server"]["transport"], #u'turn_server_address': self.selectedAccountDetails["turn_server"]["address"], #u'turn_server_user': self.selectedAccountDetails["turn_server"]["user"], #u'turn_server_pwd': self.selectedAccountDetails["turn_server"]["pwd"], u'log_level' : 1, u'debug' : True } print "VOIP CONF DICT:%s" % voip_params0 return voip_params0 def init_voip_lib(self): print "Called init_voip_lib" self.voip_params = self.get_init_params() self._update_account_details_info() self.myVoip.init_lib(self.voip_params, self.voip_notify_events) def _update_account_details_info(self): if self.selectedAccount: accountInfo = "%s [SIP USERMAME: %s - SIP SERVER:%s]" % (self.selectedAccount["name"],self.selectedAccountDetails["sip_server"]["user"],self.selectedAccountDetails["sip_server"]["address"] ) self.labAccountDetails.setText(accountInfo) else: self.labAccountDetails.setText("N.A") def _update_status_labels(self): self._update_server_state() self._update_call_state() self._update_account_state() def _update_server_state(self): server_state = self.myVoip.get_server().get_state() self.labServerStateInfo.setText(server_state) def _update_call_state(self): call_state = self.myVoip.get_call().get_state() self.labCallStateInfo.setText(call_state) def _update_account_state(self): account_state = self.myVoip.get_account().get_state() self.labAccountStateInfo.setText(account_state) def on_buddy_selected(self,item): buddies = self.myVoip.get_account().get_buddies() buddy_ext = buddies[item.row()].get_extension() self.txtExtension.setText(buddy_ext) def on_make_call_button_clicked(self): extension = self.txtExtension.text() logger.debug("Making call to %s" % extension) self.myVoip.make_call(extension) def _setupButtonsByVoipState(self): #myServerState = self.myVoip.get_server().get_state() myAccountState = self.myVoip.get_account().get_state() if myAccountState==AccountState.UNREGISTERED: self.butMakeCall.setEnabled(False) self.butAnswer.setEnabled(False) self.butHold.setEnabled(False) self.butHangup.setEnabled(False) self.butInit.setEnabled(True) else: myCallState = self.myVoip.get_call().get_state() if myCallState==CallState.IDLE: self.butMakeCall.setEnabled(True) self.butAnswer.setEnabled(False) self.butHangup.setEnabled(False) self.butHold.setEnabled(False) elif myCallState==CallState.INCOMING: self.butMakeCall.setEnabled(False) self.butAnswer.setEnabled(True) self.butHangup.setEnabled(True) self.butHold.setEnabled(False) elif myCallState==CallState.DIALING: self.butMakeCall.setEnabled(False) self.butAnswer.setEnabled(False) self.butHangup.setEnabled(True) self.butHold.setEnabled(False) elif myCallState==CallState.HOLDING: self.butMakeCall.setEnabled(False) self.butAnswer.setEnabled(False) self.butHangup.setEnabled(True) self.butHold.setEnabled(True) self.butHold.setText("Unhold") elif myCallState==CallState.ACTIVE: self.butMakeCall.setEnabled(False) self.butAnswer.setEnabled(False) self.butHangup.setEnabled(True) self.butHold.setEnabled(True) self.butHold.setText("Hold") def on_hold_toggle_button_clicked(self): if (self.myVoip.get_call().get_state()==CallState.ACTIVE): self.myVoip.hold_call() elif(self.myVoip.get_call().get_state()==CallState.HOLDING): self.myVoip.unhold_call() def on_answer_button_clicked(self): self.myVoip.answer_call() def on_hangup_button_clicked(self): self.myVoip.hangup_call() # ----------------------------------------------------------------------------- # GUI BUILDING SECTION # ----------------------------------------------------------------------------- def _build_GUI(self): cWidget = QtGui.QWidget(self) vBox = QtGui.QVBoxLayout() vBox.setSpacing(5) vBox.addLayout(self._buildAccountDetailsPanel(cWidget)) vBox.addLayout(self._buildStatesPanel(cWidget)) vBox.addLayout(self._buildBuddiesPanel(cWidget)) vBox.addLayout(self._buildMakeCallPanel(cWidget)) vBox.addLayout(self._buildLoadConfigPanel(cWidget)) vBox.addLayout(self._buildButtonsPanel(cWidget)) self.setMinimumWidth(600) cWidget.setLayout(vBox) self.setCentralWidget(cWidget) self.statusBar().showMessage('MostVoip Event Log') # crea una veloce barra di stato def _buildAccountDetailsPanel(self,cWidget): hBox1 = QtGui.QHBoxLayout() hBox1.setSpacing(5) labAccountDetailsTitle = QtGui.QLabel('Account', cWidget) self.labAccountDetails = QtGui.QLabel('N.A', cWidget) hBox1.addWidget(labAccountDetailsTitle) hBox1.addWidget(self.labAccountDetails) return hBox1 def _buildBuddiesPanel(self, cWidget): vBox = QtGui.QVBoxLayout() vBox.setSpacing(5) self.buddiesList = QtGui.QListView() self.buddiesList.setWindowTitle('Buddies') self.buddiesList.clicked.connect(self.on_buddy_selected) #self.buddiesList.setMinimumSize(600, 400) # Create an empty model for the list's data self.buddiesModel = QtGui.QStandardItemModel(self.buddiesList) self.buddiesList.setModel(self.buddiesModel) self.buddiesList.show() vBox.addWidget(QtGui.QLabel('Buddies', cWidget)) vBox.addWidget(self.buddiesList) return vBox def _buildStatesPanel(self,cWidget): hBox1 = QtGui.QHBoxLayout() hBox1.setSpacing(5) labServerState = QtGui.QLabel('Server State', cWidget) self.labServerStateInfo = QtGui.QLabel('N.A', cWidget) hBox1.addWidget(labServerState) hBox1.addWidget(self.labServerStateInfo) hBox2 = QtGui.QHBoxLayout() hBox2.setSpacing(5) labCallState = QtGui.QLabel('Call State', cWidget) self.labCallStateInfo = QtGui.QLabel('N.A', cWidget) hBox2.addWidget(labCallState) hBox2.addWidget(self.labCallStateInfo) hBox3 = QtGui.QHBoxLayout() hBox3.setSpacing(5) labAccountState = QtGui.QLabel('Account State', cWidget) self.labAccountStateInfo = QtGui.QLabel('N.A', cWidget) hBox3.addWidget(labAccountState) hBox3.addWidget(self.labAccountStateInfo) vBox = QtGui.QVBoxLayout() vBox.setSpacing(5) vBox.addLayout(hBox1) vBox.addLayout(hBox2) vBox.addLayout(hBox3) return vBox def _buildMakeCallPanel(self,cWidget): hBox = QtGui.QHBoxLayout() hBox.setSpacing(2) self.butMakeCall = QtGui.QPushButton('Make Call', cWidget) self.connect(self.butMakeCall, QtCore.SIGNAL('clicked()'), self.on_make_call_button_clicked); labExtension = QtGui.QLabel('Extension: ', cWidget) self.txtExtension = QtGui.QLineEdit(cWidget) hBox.addWidget(labExtension) hBox.addWidget(self.txtExtension) hBox.addWidget(self.butMakeCall) return hBox def _buildLoadConfigPanel(self,cWidget): hBox = QtGui.QHBoxLayout() hBox.setSpacing(2) labWebServerAddress = QtGui.QLabel('Web Server Address: ', cWidget) self.txtwebServerAddress = QtGui.QLineEdit(cWidget) self.txtwebServerAddress.setText("http://localhost:8000") hBox.addWidget(labWebServerAddress) hBox.addWidget(self.txtwebServerAddress) return hBox def _buildButtonsPanel(self,cWidget): hBox = QtGui.QHBoxLayout() hBox.setSpacing(2) self.butInit = QtGui.QPushButton('Init', cWidget) self.butAnswer = QtGui.QPushButton('Answer', cWidget) self.butHold = QtGui.QPushButton('Hold', cWidget) #self.butHold.setCheckable(True); self.butHangup = QtGui.QPushButton('Hangup', cWidget) hBox.addWidget(self.butInit) hBox.addWidget(self.butAnswer) hBox.addWidget(self.butHold) hBox.addWidget(self.butHangup) self.connect(self.butInit, QtCore.SIGNAL('clicked()'), self.init_voip_lib) self.connect(self.butHold, QtCore.SIGNAL('clicked()'), self.on_hold_toggle_button_clicked) self.connect(self.butAnswer, QtCore.SIGNAL('clicked()'), self.on_answer_button_clicked) self.connect(self.butHangup, QtCore.SIGNAL('clicked()'), self.on_hangup_button_clicked) return hBox
class Uzim(tk.Tk): def __init__(self, *args, **kwargs): tk.Tk.__init__(self, *args, **kwargs) self.title_font30 = tkfont.Font(family='Helvetica', size=30, weight="bold") self.title_font25 = tkfont.Font(family='Helvetica', size=25, weight="bold") self.title_font15 = tkfont.Font(family='Helvetica', size=15, weight="bold") self.title("Uzim") # instantiate the lib self.my_voip = VoipLib() # the container is where we'll stack a bunch of frames # on top of each other, then the one we want visible # will be raised above the others container = tk.Frame(self) container.pack(side="top", fill="both", expand=True) container.grid_rowconfigure(0, weight=1) container.grid_columnconfigure(0, weight=1) self.frames = { "LoginPage": LoginPage(parent=container, controller=self), "DialPage": DialPage(parent=container, controller=self), "OnGoingCallPage": OnGoingCallPage(parent=container, controller=self) } self.frames["LoginPage"].grid(row=0, column=0, sticky="nsew") self.frames["DialPage"].grid(row=0, column=0, sticky="nsew") self.frames["OnGoingCallPage"].grid(row=0, column=0, sticky="nsew") self.show_frame("LoginPage") def show_error(self, message): if isinstance(message, dict) and "error" in message: message = message["error"] tk.messagebox.showerror("Error", message) def show_frame(self, page_name): '''Show a frame for the given page name''' print(page_name) frame = self.frames[page_name] frame.tkraise() def login_on_server(self, Username, Password, ServerIP): self.voip_params = { u'username': Username, # a name describing the user u'sip_server_address': ServerIP, # the ip of the remote sip server (default port: 5060) u'sip_server_user': Username, # the username of the sip account u'sip_server_pwd': Password, # the password of the sip account u'sip_server_transport': u'udp', # the transport type (default: tcp) u'log_level': LogLevel, # the log level (greater values provide more informations) u'debug': False # enable/disable debugging messages } print(self.voip_params) # implement a method that will capture all the events triggered by the Voip Library def notify_events(voip_event_type, voip_event, params): print("Received Event Type:%s Event:%s -> Params: %s" % (voip_event_type, voip_event, params)) if "error" in params: self.show_error(params["error"]) return self.frames["OnGoingCallPage"].update_status() # event triggered when the account registration has been confirmed by the remote Sip Server if (voip_event == VoipEvent.ACCOUNT_REGISTERED): print("Account %s registered: ready to accept call!" % self.my_voip.get_account().get_uri()) # event triggered when a new call is incoming elif (voip_event == VoipEvent.CALL_INCOMING): print("INCOMING CALL From %s" % params["from"]) self.show_frame("OnGoingCallPage") # event triggered when the call has been established elif (voip_event == VoipEvent.CALL_ACTIVE): print("The call with %s has been established" % self.my_voip.get_call().get_remote_uri()) # events triggered when the call ends for some reasons elif (voip_event in [ VoipEvent.CALL_REMOTE_DISCONNECTION_HANGUP, VoipEvent.CALL_REMOTE_HANGUP, VoipEvent.CALL_HANGUP ]): print("End of call. Destroying lib...") # self.my_voip.destroy_lib() # event triggered when the library was destroyed elif (voip_event == VoipEvent.LIB_DEINITIALIZED): print("Call End. Exiting from the app.") end_of_call = True elif (voip_event == VoipEvent.CALL_DIALING): time.sleep(1) elif (voip_event == VoipEvent.LIB_INITIALIZATION_FAILED): print("Lib init failed. Destroying lib...") self.my_voip.destroy_lib() # just print informations about other events triggered by the library else: print("Received unhandled event type:%s --> %s" % (voip_event_type, voip_event)) # Unregister account # self.my_voip.unregister_account() # Destroy lib self.my_voip.destroy_lib() # Recreate lib self.my_voip = VoipLib() # initialize the lib passing the dictionary and the callback method defined above: self.my_voip.init_lib(self.voip_params, notify_events) # register the account self.my_voip.register_account()
#u'turn_server_address': u'192.168.1.79', #u'turn_server_user': u'', #u'turn_server_pwd': u'', u'log_level' : 1, u'debug' : True } myVoip = VoipLib() print "Initializing the Voip Lib..." myVoip.init_lib(voip_params, notify_events) print "Registering the account on the Sip Server..." myVoip.register_account() print "Server State:%s" % myVoip.get_server().get_state() print "Call State:%s" % myVoip.get_call().get_state() while True: if myVoip.get_call().get_state()==CallState.ACTIVE: # get info about the current state of the Sip Server print "Server State:%s" % myVoip.get_server().get_state() # get info about the current state of the active call print "Call State:%s" % myVoip.get_call().get_state() # get info about the current state of the called buddy print "Buddy State:%s" % myVoip.get_account().get_buddy(extension).get_state() cmd = raw_input("Enter 'h' to put on hold the call, 'e' to hangup:")
#u'turn_server_address': u'192.168.1.79', #u'turn_server_user': u'', #u'turn_server_pwd': u'', u'log_level': 1, u'debug': True } myVoip = VoipLib() print "Initializing the Voip Lib..." myVoip.init_lib(voip_params, notify_events) print "Registering the account on the Sip Server..." myVoip.register_account() print "Server State:%s" % myVoip.get_server().get_state() print "Call State:%s" % myVoip.get_call().get_state() while True: if myVoip.get_call().get_state() == CallState.ACTIVE: # get info about the current state of the Sip Server print "Server State:%s" % myVoip.get_server().get_state() # get info about the current state of the active call print "Call State:%s" % myVoip.get_call().get_state() # get info about the current state of the called buddy print "Buddy State:%s" % myVoip.get_account().get_buddy( extension).get_state() cmd = raw_input(