Esempio n. 1
0
def account_config() -> pj.AccountConfig:
    """Creates the PJSUA2 AccountConfig object."""
    LOGGER.trace("Creating account config")
    acfg = pj.AccountConfig()
    identity = doorpi.INSTANCE.config["sipphone.server.identity"]
    sip_server = sipphone_server()
    sip_user = doorpi.INSTANCE.config["sipphone.server.username"]
    sip_pass = doorpi.INSTANCE.config["sipphone.server.password"]
    sip_realm = doorpi.INSTANCE.config["sipphone.server.realm"] or sip_server

    if identity:
        identity = identity.replace("\\", "\\\\").replace('"', '\\"')
        acfg.idUri = f'"{identity}" <sip:{sip_user}@{sip_server}>'
    else:
        acfg.idUri = f"sip:{sip_user}@{sip_server}"

    acfg.regConfig.registrarUri = f"sip:{sip_server}"
    acfg.regConfig.registerOnAdd = True

    authCred = pj.AuthCredInfo()
    authCred.scheme = "digest"
    authCred.realm = sip_realm
    authCred.username = sip_user
    authCred.dataType = 0  # plain text password
    authCred.data = sip_pass
    acfg.sipConfig.authCreds.append(authCred)

    acfg.presConfig.publishEnabled = True
    return acfg
Esempio n. 2
0
    def account_config() -> pj.AccountConfig:
        logger.trace("Creating account config")
        acfg = pj.AccountConfig()
        identity = conf.get_string(SIPPHONE_SECTION, "identity", "DoorPi")
        sip_server = Config.sipphone_server()
        sip_user = conf.get_string(SIPPHONE_SECTION, "sipserver_username")
        sip_pass = conf.get_string(SIPPHONE_SECTION,
                                   "sipserver_password",
                                   password=True)
        sip_realm = conf.get_string(SIPPHONE_SECTION, "sipserver_realm",
                                    sip_server)
        if not sip_user:
            raise ValueError(f"No username given in [{SIPPHONE_SECTION}]")
        if not sip_server:
            raise ValueError(f"No server given in [{SIPPHONE_SECTION}]")

        if identity:
            identity = identity.replace("\\", "\\\\").replace("\"", "\\\"")
            acfg.idUri = f"\"{identity}\" <sip:{sip_user}@{sip_server}>"
        else:
            acfg.idUri = f"sip:{sip_user}@{sip_server}"

        acfg.regConfig.registrarUri = f"sip:{sip_server}"
        acfg.regConfig.registerOnAdd = True

        authCred = pj.AuthCredInfo()
        authCred.scheme = "digest"
        authCred.realm = sip_realm
        authCred.username = sip_user
        authCred.dataType = 0  # plain text password
        authCred.data = sip_pass
        acfg.sipConfig.authCreds.append(authCred)

        acfg.presConfig.publishEnabled = True
        return acfg
Esempio n. 3
0
    def _login(self):
        errors = ''
        if not self.domain.get():
            errors += 'Domain is required\n'
        if not self.username.get():
            errors += 'Username is required\n'
        if not self.password.get():
            errors += 'Password is required\n'
        if not validateSipUri('sip:' + self.username.get() + '@' +
                              self.domain.get()):
            errors += 'Invalid SIP URI\n'

        if errors:
            msg.showerror('Error Detected', errors)
            return

        self.cfg.idUri = 'sip:' + self.username.get() + '@' + self.domain.get()
        self.cfg.regConfig.registrarUri = 'sip:' + self.domain.get()
        self.cfg.regConfig.registerOnAdd = True
        self.cfg.sipConfig.authCreds.append(
            pj.AuthCredInfo('digest', '*', self.username.get(),
                            pj.PJSIP_CRED_DATA_PLAIN_PASSWD,
                            self.password.get()))

        # Video support
        self.cfg.videoConfig.autoTransmitOutgoing = True
        self.cfg.videoConfig.defaultCaptureDevice = pj.PJMEDIA_VID_DEFAULT_CAPTURE_DEV
        self.cfg.videoConfig.defaultRenderDevice = pj.PJMEDIA_VID_DEFAULT_RENDER_DEV

        self.is_ok = True
        self.destroy()
Esempio n. 4
0
    def _test_account(self):
        acfg = AccountConfig(enabled=True)
        acfg.config.idUri = "sip:[email protected]"

        # regConfig
        acfg.config.regConfig.registrarUri = "sip:us-west.sb.2600hz.com"
        acfg.config.regConfig.registerOnAdd = True
        # acfg.config.regConfig.headers = # [{K, V}] The optional custom SIP headers to be put in the registration request.
        # acfg.config.regConfig.contactParams = # Additional parameters that will be appended in the Contact header of the registration requests.
        acfg.config.regConfig.timeoutSec = 300
        acfg.config.regConfig.dropCallsOnFail = False
        acfg.config.regConfig.proxyUse = 3

        # sipConfig
        cred = pj.AuthCredInfo("digest", "4a6863.sip.sandbox.2600hz.com",
                               "user_6kk5hzm2tf", 0, "wxhjrvuwf8ph")
        acfg.config.sipConfig.authCreds.append(cred)
        acfg.config.sipConfig.proxies.append("sip:us-west.sb.2600hz.com;lr")
        # acfg.config.sipConfig.contactParams = ";my-param=X;another-param=Hi%20there"

        # callConfig
        # ....

        # presConfig
        # .....

        # mwiConfig
        # acfg.config.mwiConfig.enabled = True # subscribe

        # natConfig
        # ....

        # mediaConfig
        # ....

        # videConfig
        # ....

        # ipChangeConfig
        # ....

        return acfg
Esempio n. 5
0
def ua_data_test():
    #
    # AuthCredInfo
    #
    write("UA data types test..")
    the_realm = "pjsip.org"
    ci = pj.AuthCredInfo()
    ci.realm = the_realm
    ci.dataType = 20

    ci2 = ci
    assert ci.dataType == 20
    assert ci2.realm == the_realm

    #
    # UaConfig
    # See here how we manipulate std::vector
    #
    uc = pj.UaConfig()
    uc.maxCalls = 10
    uc.userAgent = "Python"
    uc.nameserver = pj.StringVector(["10.0.0.1", "10.0.0.2"])
    uc.nameserver.append("NS1")

    uc2 = uc
    assert uc2.maxCalls == 10
    assert uc2.userAgent == "Python"
    assert len(uc2.nameserver) == 3
    assert uc2.nameserver[0] == "10.0.0.1"
    assert uc2.nameserver[1] == "10.0.0.2"
    assert uc2.nameserver[2] == "NS1"

    write("  Dumping nameservers: " + "\r\n")
    for s in uc2.nameserver:
        write(s + "\r\n")
    write("\r\n")
Esempio n. 6
0
    def onOk(self):
        # Check basic settings
        errors = ""
        if not self.cfgAccId.get():
            errors += "Account ID is required\n"
        if self.cfgAccId.get():
            if not endpoint.validateSipUri(self.cfgAccId.get()):
                errors += "Invalid SIP ID URI: '%s'\n" % (self.cfgAccId.get())
        if self.cfgRegistrar.get():
            if not endpoint.validateSipUri(self.cfgRegistrar.get()):
                errors += "Invalid SIP registrar URI: '%s'\n" % (
                    self.cfgRegistrar.get())
        if self.cfgProxy.get():
            if not endpoint.validateSipUri(self.cfgProxy.get()):
                errors += "Invalid SIP proxy URI: '%s'\n" % (
                    self.cfgProxy.get())
        if self.cfgTurnEnabled.get():
            if not self.cfgTurnServer.get():
                errors += "TURN server is required\n"
        if errors:
            msgbox.showerror("Error detected:", errors)
            return

        # Basic settings
        self.cfg.priority = self.cfgPriority.get()
        self.cfg.idUri = self.cfgAccId.get()
        self.cfg.regConfig.registrarUri = self.cfgRegistrar.get()
        self.cfg.regConfig.registerOnAdd = self.cfgRegisterOnAdd.get()
        while len(self.cfg.sipConfig.authCreds):
            self.cfg.sipConfig.authCreds.pop()
        if self.cfgUsername.get():
            cred = pj.AuthCredInfo()
            cred.scheme = "digest"
            cred.realm = "*"
            cred.username = self.cfgUsername.get()
            cred.data = self.cfgPassword.get()
            self.cfg.sipConfig.authCreds.append(cred)
        while len(self.cfg.sipConfig.proxies):
            self.cfg.sipConfig.proxies.pop()
        if self.cfgProxy.get():
            self.cfg.sipConfig.proxies.append(self.cfgProxy.get())

        # SIP features
        self.cfg.callConfig.prackUse = self.cfgPrackUse.get()
        self.cfg.callConfig.timerUse = self.cfgTimerUse.get()
        self.cfg.callConfig.timerSessExpiresSec = self.cfgTimerExpires.get()
        self.cfg.presConfig.publishEnabled = self.cfgPublish.get()
        self.cfg.mwiConfig.enabled = self.cfgMwiEnabled.get()
        self.cfg.natConfig.contactRewriteUse = 1 if self.cfgEnableContactRewrite.get(
        ) else 0
        self.cfg.natConfig.viaRewriteUse = 1 if self.cfgEnableViaRewrite.get(
        ) else 0
        self.cfg.natConfig.sdpNatRewriteUse = 1 if self.cfgEnableSdpRewrite.get(
        ) else 0
        self.cfg.natConfig.sipOutboundUse = 1 if self.cfgEnableSipOutbound.get(
        ) else 0
        self.cfg.natConfig.udpKaIntervalSec = self.cfgKaInterval.get()

        # Media
        self.cfg.mediaConfig.transportConfig.port = self.cfgMedPort.get()
        self.cfg.mediaConfig.transportConfig.portRange = self.cfgMedPortRange.get(
        )
        self.cfg.mediaConfig.lockCodecEnabled = self.cfgMedLockCodec.get()
        self.cfg.mediaConfig.srtpUse = self.cfgMedSrtp.get()
        self.cfg.mediaConfig.srtpSecureSignaling = self.cfgMedSrtpSecure.get()
        self.cfg.mediaConfig.ipv6Use = pj.PJSUA_IPV6_ENABLED if self.cfgMedIpv6.get(
        ) else pj.PJSUA_IPV6_DISABLED

        # NAT
        self.cfg.natConfig.sipStunUse = self.cfgSipUseStun.get()
        self.cfg.natConfig.mediaStunUse = self.cfgMediaUseStun.get()
        self.cfg.natConfig.iceEnabled = self.cfgIceEnabled.get()
        self.cfg.natConfig.iceAggressiveNomination = self.cfgIceAggressive.get(
        )
        self.cfg.natConfig.iceAlwaysUpdate = self.cfgAlwaysUpdate.get()
        self.cfg.natConfig.iceMaxHostCands = 0 if self.cfgIceNoHostCands.get(
        ) else -1
        self.cfg.natConfig.turnEnabled = self.cfgTurnEnabled.get()
        self.cfg.natConfig.turnServer = self.cfgTurnServer.get()
        self.cfg.natConfig.turnConnType = self.cfgTurnConnType.get()
        self.cfg.natConfig.turnUserName = self.cfgTurnUser.get()
        self.cfg.natConfig.turnPasswordType = 0
        self.cfg.natConfig.turnPassword = self.cfgTurnPasswd.get()

        self.isOk = True
        self.destroy()
Esempio n. 7
0
def pjsua2_test():
    global ep
    # Create and initialize the library
    ep_cfg = pj.EpConfig()
    ep_cfg.logConfig.level = 5;
    ep = pj.Endpoint()
    ep.libCreate()
    ep.libInit(ep_cfg)
    ep.audDevManager().setNullDev()

    for codec in ep.codecEnum2():
        priority = 0
        if 'PCMA/8000' in codec.codecId:
            priority = 255
        ep.codecSetPriority(codec.codecId, priority)

    # Create SIP transport. Error handling sample is shown
    sipTpConfig = pj.TransportConfig();
    sipTpConfig.port = 15060;
    ep.transportCreate(pj.PJSIP_TRANSPORT_UDP, sipTpConfig);
    # Start the library
    ep.libStart();

    acfg = pj.AccountConfig();
    acfg.idUri = "sip:1002@asterisk";
    acfg.regConfig.registrarUri = "sip:asterisk";
    cred = pj.AuthCredInfo("digest", "*", "1002", 0, "12345");
    acfg.sipConfig.authCreds.append( cred )
    # Create the account
    acc = pj.Account()
    acc.create(acfg)
    # Here we don't have anything else to do..
    while not acc.getInfo().regIsActive:
        print('========== registering')
        time.sleep(0.1)

    if acc.getInfo().regStatus != 200:
        print('++++++++++++++++++')
        print('no registration')
        return

    call = Call(acc)
    prm = pj.CallOpParam(True)
    prm.opt.audioCount = 1
    prm.opt.videoCount = 0

    call.makeCall("sip:8080@asterisk", prm)

    while call.getInfo().state != pj.PJSIP_INV_STATE_CONFIRMED:
        print(call.getInfo().stateText)
        time.sleep(0.1)

    f = open('output.lpcm', 'wb')
    f2 = open('hw.raw', 'rb')
    hwraw = f2.read()
    n = 320
    [call.putFrame(hwraw[i:i+320]) for i in range(0, len(hwraw), n)]

    for i in range(10):
        time.sleep(1)
        data = call.getFrames()
        if data:
            f.write(data)
    f.close()

    prm = pj.CallOpParam()
    call.hangup(prm)

    # Destroy the library
    ep.hangupAllCalls()
    print('unregistering')
    acc.delete()
Esempio n. 8
0
 def startup(self):
     self._logger.debug("entered startup()")
     ep_cfg = pjsua2.EpConfig()
     ep_cfg.uaConfig.mainThreadOnly = True
     ep_cfg.uaConfig.threadCnt = 0
     version = pkg_resources.require("gtkapplication")[0].version
     ep_cfg.uaConfig.userAgent = "pyphone-{0}".format(version)
     self._logger.debug('ep_cfg.uaConfig.userAgent = %s',
                        ep_cfg.uaConfig.userAgent)
     ep_cfg.logConfig.msgLogging = 1
     ep_cfg.logConfig.level = 5
     ep_cfg.logConfig.consoleLevel = 5
     decor = pjsua2.PJ_LOG_HAS_YEAR | pjsua2.PJ_LOG_HAS_MONTH | pjsua2.PJ_LOG_HAS_DAY_OF_MON | pjsua2.PJ_LOG_HAS_TIME | pjsua2.PJ_LOG_HAS_MICRO_SEC | pjsua2.PJ_LOG_HAS_SENDER | pjsua2.PJ_LOG_HAS_NEWLINE | pjsua2.PJ_LOG_HAS_SPACE | pjsua2.PJ_LOG_HAS_THREAD_SWC | pjsua2.PJ_LOG_HAS_INDENT
     self._logger.debug('decor = %s', decor)
     ep_cfg.logConfig.decor = decor
     ep_cfg.logConfig.filename = "logs/pjsip.log"
     ep_cfg.logConfig.fileFlags = pjsua2.PJ_O_APPEND
     pjsip_log_writer = gtkapplication.api.sip.logger.PjLogger()
     ep_cfg.logConfig.writer = pjsip_log_writer
     sip_container = gtkapplication.api.sip.pjsip_container.sip_container
     sip_container.pjsip_log_writer = pjsip_log_writer
     sip_container.ep = pjsua2.Endpoint()
     ep = sip_container.ep
     ep.libCreate()
     ep.libInit(ep_cfg)
     pjsip_version = ep.libVersion().full
     self._logger.debug('pjsip_version = %s', pjsip_version)
     sip_udp_tranport_config = pjsua2.TransportConfig()
     sip_udp_tranport_config.port = 5060
     sip_udp_tranport_config.enabled = 1
     ep.transportCreate(pjsua2.PJSIP_TRANSPORT_UDP, sip_udp_tranport_config)
     self._logger.debug("transport created")
     acfg = pjsua2.AccountConfig()
     acfg.idUri = gtkapplication.data.config_data.PROFILE_DATA['sip_uri']
     acfg.regConfig.registrarUri = gtkapplication.data.config_data.PROFILE_DATA[
         'sip_registrar_uri']
     acfg.regConfig.registerOnAdd = True
     acfg.regConfig.timeoutSec = 60
     acfg.regConfig.retryIntervalSec = 60
     aci = pjsua2.AuthCredInfo()
     aci.scheme = "digest"
     aci.realm = "*"
     aci.username = gtkapplication.data.config_data.PROFILE_DATA[
         'sip_username']
     aci.dataType = 0
     aci.data = gtkapplication.data.config_data.PROFILE_DATA['sip_password']
     aciv = pjsua2.AuthCredInfoVector()
     aciv.append(aci)
     acfg.sipConfig.authCreds = aciv
     sip_container.sip_account_list.append(
         gtkapplication.api.sip.account.Account(acfg))
     sip_account = sip_container.sip_account_list[0]
     sip_account.cfg = acfg
     sip_account.create(acfg, True)
     presence_status = pjsua2.PresenceStatus()
     presence_status.status = pjsua2.PJSUA_BUDDY_STATUS_ONLINE
     sip_account.setOnlineStatus(presence_status)
     self._logger.debug("sip account created: %r", sip_account)
     bcfg = pjsua2.BuddyConfig()
     bcfg.uri = gtkapplication.data.config_data.PROFILE_DATA[
         'sip_buddy_uri']
     bcfg.subscribe = True
     buddy = gtkapplication.api.sip.buddy.Buddy(sip_account)
     buddy.create(sip_account, bcfg)
     sip_account.server_buddy = buddy
     contact_manager = gtkapplication.api.contacts.contact_manager.ContactManager(
     )
     contact_manager.create_free_buddy_contacts(sip_account)
     acfg = pjsua2.AccountConfig()
     acfg.idUri = gtkapplication.data.config_data.PROFILE_DATA[
         'twilio_sip_uri']
     acfg.regConfig.registrarUri = gtkapplication.data.config_data.PROFILE_DATA[
         'twilio_sip_registrar_uri']
     self._logger.debug("twilio sip_uri = '%s', registrar = %s", acfg.idUri,
                        acfg.regConfig.registrarUri)
     acfg.regConfig.registerOnAdd = True
     acfg.regConfig.timeoutSec = 60
     acfg.regConfig.retryIntervalSec = 60
     aci = pjsua2.AuthCredInfo()
     aci.scheme = "digest"
     aci.realm = "*"
     aci.username = gtkapplication.data.config_data.PROFILE_DATA[
         'twilio_sip_username']
     aci.dataType = 0
     aci.data = gtkapplication.data.config_data.PROFILE_DATA[
         'twilio_sip_password']
     aciv = pjsua2.AuthCredInfoVector()
     aciv.append(aci)
     acfg.sipConfig.authCreds = aciv
     sip_container.sip_account_list.append(
         gtkapplication.api.sip.account.Account(acfg))
     twilio_sip_account = sip_container.sip_account_list[1]
     twilio_sip_account.cfg = acfg
     twilio_sip_account.create(acfg, True)
     presence_status = pjsua2.PresenceStatus()
     presence_status.status = pjsua2.PJSUA_BUDDY_STATUS_ONLINE
     twilio_sip_account.setOnlineStatus(presence_status)
     ep.libStart()
     self._logger.debug("after libStart()")
     tone_generator = pjsua2.ToneGenerator()
     tone_generator.createToneGenerator(16000, 1)
     sip_container.tone_generator = tone_generator
     self._logger.debug("tone generator created")
     self._source_tag = GObject.timeout_add(50,
                                            self.poll_pjsip_events_timer,
                                            False)
     self._logger.debug("timer started source_tag is %s", self._source_tag)
     self._logger.info("timer started")