wiki:Python_SIP/Accounts

Version 2 (modified by bennylp, 11 years ago) (diff)

--

SIP Accounts

Accounts provide identity (or identities) of the user who is currently using the application. An account has one SIP Uniform Resource Identifier (URI) associated with it. In SIP terms, the identity is used as the From header in outgoing requests.

Account may or may not have client registration associated with it. An account is also associated with route set and some authentication credentials, which are used when sending SIP request messages using the account. An account also has presence online status, which will be reported to remote peer when they subscribe to the account's presence, or which is published to a presence server if presence publication is enabled for the account.

At least one account MUST be created in the application, since any outgoing requests require an account context. If no user association is required, application can create a user-less account by calling lib.create_account_for_transport(). A userless account identifies local endpoint instead of a particular user, and it corresponds to a particular transport instance.

Also one account must be set as the default account, which will be used as the account identity when pjsua fails to match the request with any accounts using the stricter matching rules.

Creating Userless Accounts

A user-less account is an account without user ID (duh!). It identifies a particular SIP endpoint rather than a particular user. Some other SIP softphones may call this peer-to-peer mode, which means that we are calling another computer via its address rather than calling a particular user ID.

So for example, we might identify ourself as "sip:192.168.0.15" (a user-less account) rather than "sip:user@pjsip.org" (an account).

In pjsua, a user-less account may be created for each particular transport instance. Creating user-less account is very simple, all we need is the transport instance which is returned by lib.create_transport() method as explained in previous chapter.

Here's a snippet:

acc_cb = MyAccountCallback()
acc = lib.create_account_for_transport(udp, cb=acc_cb)

The account callback will be explained later. Basically it is used to capture events emitted by the account, such as incoming call events.

Once the account is created, you can use the instance as a normal account. More will be explained later.

Accounts created this way will have its URI derived from the transport address. For example, if the transport address is "192.168.0.15:5080", then the account's URI for UDP transport will be "sip:192.168.0.15:5080", or "sip:192.168.0.15:5080;transport=tcp" for TCP transport.

Creating Account

For the "normal" account, we need to configure AccountConfig and call lib.create_account() to create the account.

At the very minimum, pjsua only requires the account's ID, which is an URI to identify the account (or in SIP terms, it's called Address of Record/AOR). Here's a snippet:

try:
    acc_cfg = pjsua.AccountConfig()
    acc_cfg.id = "sip:user@pjsip.org"

    acc_cb = MyAccountCallback()
    acc = lib.create_account(acc_cfg, cb=acc_cb)

except pjsua.Error, err:
    print 'Error creating account:', err

Again the account callback will be explained later.

The account created above doesn't do anything except to provide identity in the From: header for outgoing requests. The account will not register to SIP server or anything.

Typically you will want the account to authenticate and register to your SIP server so that you can receive incoming calls. To do that you will need to configure some more settings in your AccountConfig, something like this:

try:
    acc_cfg = pjsua.AccountConfig()
    acc_cfg.id = "sip:someuser@pjsip.org"
    acc_cfg.reg_uri = "sip:pjsip.org"
    acc_cfg.proxy = [ "sip:pjsip.org;lr" ]
    acc_cfg.auth_cred = [ AuthCred("*", "someuser", "secretpass") ]

    acc_cb = MyAccountCallback()
    acc = lib.create_account(acc_cfg, cb=acc_cb)

except pjsua.Error, err:
    print 'Error creating account:', err

Or alternatively, for typical account settings like above, you can build the account config like this:

    acc_cfg = pjsua.AccountConfig("pjsip.org", "someuser", "secretpass")

More on Account Settings

There are many more settings that can be specified in AccountConfig, like:

  • whether reliable provisional response (SIP 100rel) is required
  • whether presence publication (PUBLISH) is enabled
  • Secure RTP (SRTP) related settings
  • etc.

Please see AccountConfig reference documentation for more info.