Exemplo n.º 1
0
    def onCallState(self, prm: pj.OnCallStateParam) -> None:
        ci = self.getInfo()
        sp = DoorPi().sipphone
        eh = DoorPi().event_handler
        conf = DoorPi().config

        if ci.state == pj.PJSIP_INV_STATE_CALLING:
            logger.debug("Call to %s is now calling", repr(ci.remoteUri))
        elif ci.state == pj.PJSIP_INV_STATE_INCOMING:
            logger.debug("Call from %s is incoming", repr(ci.remoteUri))
        elif ci.state == pj.PJSIP_INV_STATE_EARLY:
            logger.debug("Call to %s is in early state", repr(ci.remoteUri))
        elif ci.state == pj.PJSIP_INV_STATE_CONNECTING:
            logger.debug("Call to %s is now connecting", repr(ci.remoteUri))
        elif ci.state == pj.PJSIP_INV_STATE_CONFIRMED:
            logger.info("Call to %s was accepted", repr(ci.remoteUri))
            self.__fire_disconnect = True
            with sp._Pjsua2__call_lock:
                prm = pj.CallOpParam()
                if sp.current_call is not None:
                    # (note: this should not be possible)
                    sp.current_call.hangup(prm)
                    sp.current_call = None

                sp.current_call = self
                for ring in sp._Pjsua2__ringing_calls:
                    if self != ring: ring.hangup(prm)
                sp._Pjsua2__ringing_calls = []
                sp._Pjsua2__waiting_calls = []
                fire_event("OnCallConnect", remote_uri=ci.remoteUri)
        elif ci.state == pj.PJSIP_INV_STATE_DISCONNECTED:
            logger.info("Call to %s disconnected after %d seconds (%d total)",
                        repr(ci.remoteUri), ci.connectDuration.sec,
                        ci.totalDuration.sec)
            with sp._Pjsua2__call_lock:
                if sp.current_call == self:
                    sp.current_call = None
                elif self in sp._Pjsua2__ringing_calls:
                    sp._Pjsua2__ringing_calls.remove(self)

                if self.__fire_disconnect:
                    logger.trace("Firing disconnect event for call to %s",
                                 repr(ci.remoteUri))
                    fire_event("OnCallDisconnect", remote_uri=ci.remoteUri)
                elif len(sp._Pjsua2__ringing_calls) == 0:
                    logger.trace(
                        "Last ringing call disconnected, synthesizing disconnect"
                    )
                    fire_event("OnCallDisconnect", remote_uri="sip:null@null")
                else:
                    logger.trace("Skipping disconnect event for call to %s",
                                 repr(ci.remoteUri))
        else:
            logger.warning("Call to %s: unknown state %d", repr(ci.remoteUri),
                           ci.state)
Exemplo n.º 2
0
    def onIncomingCall(self, iprm: pj.OnIncomingCallParam) -> None:
        sp = DoorPi().sipphone
        call = CallCallback(self, iprm.callId)
        callInfo = call.getInfo()
        oprm = pj.CallOpParam(False)
        event = None

        fire_event("BeforeCallIncoming", remote_uri=callInfo.remoteUri)

        try:
            if not sp.is_admin(callInfo.remoteUri):
                logger.info("Rejecting call from unregistered number %s",
                            callInfo.remoteUri)
                oprm.statusCode = pj.PJSIP_SC_FORBIDDEN
                event = "OnCallReject"
                return

            with sp._Pjsua2__call_lock:
                if sp.current_call is not None and sp.current_call.isActive():
                    logger.info("Busy-rejecting call from %s",
                                callInfo.remoteUri)
                    oprm.statusCode = pj.PJSIP_SC_BUSY_HERE
                    event = "OnCallBusy"
                    return
                else:
                    logger.info("Accepting incoming call from %s",
                                callInfo.remoteUri)
                    oprm.statusCode = pj.PJSIP_SC_OK
                    event = "OnCallIncoming"
                    sp.current_call = call
                    return
        finally:
            call.answer(oprm)
            fire_event(event, remote_uri=callInfo.remoteUri)