Search code examples
pythonsippjsip

pjsua and sip registration: error=Invalid URI (PJSIP_EINVALIDURI)


I'm trying to use PJSUA for SIP registration:

import pjsua as pj
import threading


def log_cb(level, str, len):
    print str

class MyAccountCallback(pj.AccountCallback):
    sem = None

    def __init__(self, account):
        pj.AccountCallback.__init__(self, account)

    def wait(self):
        self.sem = threading.Semaphore(0)
        self.sem.acquire()


    def on_reg_state(self):
        if self.sem:
            self.sem.release()

lib = pj.Lib()

try:
    lib.init(log_cfg = pj.LogConfig(level=0, callback=log_cb))
    lib.create_transport(pj.TransportType.UDP, pj.TransportConfig(5080))
    lib.start()

    acc = lib.create_account(pj.AccountConfig(username='79xxxxxxxxx', password='my_pass', domain='multifon.ru', proxy='sbc.multifon.ru'))

    acc_cb = MyAccountCallback(acc)
    acc.set_callback(acc_cb)
    acc_cb.wait()

    print "\n"
    print "Registration complete, status=", acc.info().reg_status, \
          "(" + acc.info().reg_reason + ")"

    lib.destroy()
    lib = None

except pj.Error, e:
    print "Exception: " + str(e)
    lib.destroy()

If I am using proxy parameter, I get follow error:

Exception: Object: Lib, operation=create_account(), error=Invalid URI (PJSIP_EINVALIDURI)

What am I doing wrong?


Solution

  • Ok, I found solution. Address of the proxy server should be presented in the form of IP address and port number with sip: prefix:

    acc = lib.create_account(pj.AccountConfig(username='79xxxxxxxxx', password='my_pass', domain='multifon.ru', proxy='sip:193.201.229.35:5060')
    

    Here 193.201.229.35 - IP address corresponding to the domain address sbc.megafon.ru

    And a couple of comments on the code:

    1) Instead of explicitly specifying IP-address in the proxy field it is better to use the socket.gethostbyname:

    proxy_ip = socket.gethostbyname('sbc.megafon.ru')
    proxy_str = 'sip:%s:%s' % (proxy_ip, port)
    
    acc = lib.create_account(pj.AccountConfig(username=sip_login, password=sip_password,
                                                       domain=sip_domain, proxy=proxy_str))
    

    2) In line lib.create_transport (pj.TransportType.UDP, pj.TransportConfig (5080)) port can be set to 0 - the operating system will automatically choose a free port.