Welcome to exosip2ctypes’s documentation!¶
Build Time: | Apr 19, 2018, 7:18:24 AM |
---|---|
Version: | 1.2 |
Release: | 1.2.4.post4+g911613c |
Summaries¶
exosip2ctypes.call |
eXosip call API |
exosip2ctypes.context |
eXosip2 context API |
exosip2ctypes.error |
Error definitions |
exosip2ctypes.event |
eXosip2 event API |
exosip2ctypes.message |
osip and eXosip messages |
exosip2ctypes.register |
eXosip2 REGISTER and Registration Management |
exosip2ctypes.sdp |
eXosip2 SDP helper API. |
exosip2ctypes.utils |
Some helper functions |
exosip2ctypes.version |
version infomation |
Contents¶
exosip2ctypes¶
exosip2ctypes package¶
Submodules¶
exosip2ctypes.call module¶
eXosip call API
This file provide the API needed to control calls. You can use it to:
- build initial invite.
- send initial invite.
- build request within the call.
- send request within the call.
- This API can be used to build the following messages:
- INVITE, INFO, OPTIONS, REFER, UPDATE, NOTIFY
-
class
exosip2ctypes.call.
InitInvite
(context, to_url, from_url, route=None, subject=None)¶ Bases:
exosip2ctypes.message.ExosipMessage
default INVITE message for a create call.
Build a default INVITE message for a create call.
Parameters:
-
class
exosip2ctypes.call.
Ack
(context, did)¶ Bases:
exosip2ctypes.message.ExosipMessage
default ACK for a 200ok received.
Build a default ACK for a 200ok received.
Parameters: -
send
()¶ Send the ACK for the 200ok received.
-
-
class
exosip2ctypes.call.
Answer
(context, tid, status)¶ Bases:
exosip2ctypes.message.ExosipMessage
default Answer for request.
Build default Answer for request.
Parameters: -
send
()¶ Send Answer for invite.
-
exosip2ctypes.context module¶
eXosip2 context API
-
class
exosip2ctypes.context.
Context
(event_callback=None)¶ Bases:
exosip2ctypes.context.BaseContext
,exosip2ctypes.utils.LoggerMixin
Allocate and Initiate an eXosip context.
Parameters: event_callback (callable) – Event callback.
It’s a callback function or any other callable object. You can avoid the parameter in constructor, and set
event_callback
later.The callback is like:
def event_callback(context, event): # do some thing... pass
- It has two parameters:
Context
: eXosip context on which the event happened.Event
: The event happened.
-
add_authentication_info
(user_name, user_id, password, realm)¶ Add authentication credentials.
Parameters: These are used when an outgoing request comes back with an authorization required response.
-
automatic_action
()¶ Initiate some automatic actions:
- Retry with credentials upon reception of 401/407.
- Retry with higher Sessions-Expires upon reception of 422.
- Refresh REGISTER and SUBSCRIBE before the expiration delay.
- Retry with Contact header upon reception of 3xx request.
- Send automatic UPDATE for session-timer feature.
-
build_message
(message_class, *args, **kwargs)¶ Build a default message for a eXosip transaction/dialog/call
Parameters: - message_class (type(ExosipMessage)) – Message Class
- args – Sequent arguments passed to the class constructor
- kwargs – Named arguments passed to the class constructor
Returns: New built message object
Return type:
-
call_send_ack
(did=None, ack=None)¶ Send the ACK for the 200ok received.
Parameters:
-
call_send_answer
(tid=None, status=None, answer=None)¶ Send Answer for invite.
Parameters: - tid (int) – id of transaction to answer.
- status (int) – response status if answer is NULL. (not allowed for 2XX)
- answer (call.Answer) – The sip answer to send. tid and status won’t be used if specified.
-
call_send_init_invite
(invite)¶ Initiate a call.
Parameters: invite (call.InitInvite) – SIP INVITE message to send. Returns: CID - unique id for SIP calls (but multiple dialogs!) Return type: int Attention
returned call id is an integer, which different from SIP message’s Call-Id header
-
call_terminate
(cid, did=0)¶ Terminate a call. send CANCEL, BYE or 603 Decline.
Parameters:
-
event_callback
¶ Set event callback
Return type: callable Attention
Event callback is invoked in a
concurrent.futures.Executor
-
event_wait
(s, ms)¶ Wait for an eXosip event.
Parameters: Returns: event or None if no event
Return type:
-
get_event_callback
()¶ Set event callback
Return type: callable Attention
Event callback is invoked in a
concurrent.futures.Executor
-
listen_on_address
(address=None, transport=17, port=5060, family=<AddressFamily.AF_INET: 2>, secure=False)¶ Listen on a specified socket.
Parameters: - address (str) – the address to bind (NULL for all interface)
- transport (int) –
socket.IPPROTO_UDP
for udp. (soon to come: TCP/TLS?) - port (int) – the listening port. (0 for random port)
- family (int) – the IP family (
socket.AF_INET
orsocket.AF_INET6
). - secure (bool) – False for UDP or TCP, True for TLS (with TCP).
-
lock
¶ eXosip Context lock.
Return type: ContextLock
-
lock_acquire
()¶ Lock the eXtented oSIP library.
-
lock_release
()¶ UnLock the eXtented oSIP library.
-
masquerade_contact
(public_address=None, port=0)¶ This method is used to replace contact address with the public address of your NAT. The ip address should be retrieved manually (fixed IP address) or with STUN. This address will only be used when the remote correspondent appears to be on an DIFFERENT LAN.
If set to None, then the local ip address will be guessed automatically (returns to default mode).
Parameters: Note
It’s advised to call the method after
listen_on_address()
-
ptr
¶ C Pointer to the context’s eXosip_t C structure
Return type: ctypes.c_void_p
-
quit
()¶ Release resource used by the eXtented oSIP library.
-
remove_authentication_info
(user_name, realm)¶ Remove authentication credentials.
Parameters: - user_name (str) – user name
- realm (str) – realm must be exact same arg as for
add_authentication_info()
-
run
(s=0, ms=50, event_executor=None, timeout=None)¶ Start the main loop for the context in a create thread, and then wait until the thread terminates.
Parameters: - s (int) – timeout value (seconds). Passed to
event_wait()
in the main loop. - ms (int) – timeout value (seconds). Passed to
event_wait()
in the main loop. - event_executor (concurrent.futures.Executor) – see the same parameter in
start()
. - timeout (float) – When the timeout argument is present and not None, it should be a floating point number specifying a timeout for the operation in seconds (or fractions thereof)
This method blocks, it equals:
trd = context.start(s, ms) trd.join(timeout)
- s (int) – timeout value (seconds). Passed to
-
set_event_callback
(val)¶ Set event callback
Parameters: val (callable) – callback function Attention
Event callback is invoked in a
concurrent.futures.Executor
-
start
(s=0, ms=50, event_executor=None)¶ Start the main loop for the context in a create thread, and then return.
Parameters: - s (int) – timeout value (seconds). Passed to
event_wait()
in the main loop. - ms (int) – timeout value (seconds). Passed to
event_wait()
in the main loop. - event_executor (concurrent.futures.Executor) – Event executor instance. Events will be fired in it.
Default is a
concurrent.futures.ThreadPoolExecutor
instance
Returns: New created event loop thread.
Return type: This method returns soon after the main loop thread started, so it does not block.
Equal to set
is_running
to True- s (int) – timeout value (seconds). Passed to
-
stop
()¶ Stop the context’s main loop thread and return after the thread stopped.
Equal to set
is_running
to False
-
class
exosip2ctypes.context.
ContextLock
(context)¶ Bases:
object
A helper class for eXosip Context lock
This class wraps eXosip’s native context lock function, which is invoked in
Context.lock_acquire()
andContext.lock_release()
. You can call theses methods directly, or useContext.lock
.with
statement is supported.eg:
context.lock.acquire() try: do_something() # ... finally: context.lock.release()
or:
with context.lock: do_something() # ...
Danger
Do NOT construct the class yourself, use
Context.lock
.Parameters: context (Context) – Context which the lock is for -
acquire
()¶ lock the context
-
locked
()¶ Return the status of the lock: True if it has been acquired by some thread, False if not.
Return type: bool
-
release
()¶ unlock the context
-
exosip2ctypes.error module¶
Error definitions
see osip/include/osipparser2/osip_port.h
-
exosip2ctypes.error.
raise_if_osip_error
(error_code, message=None)¶ raise an
OsipError
exception if error_code is notOSIP_SUCCESS
Parameters: Raises: OsipError – Throw exception if error_code is smaller than zero(
OSIP_SUCCESS
)Can be used to check osip2/eXosip2 API function integer return value
Attention
All error codes in osip are smaller than 0
-
exception
exosip2ctypes.error.
MallocError
¶ Bases:
RuntimeError
Failed to allocate an eXosip context.
-
exception
exosip2ctypes.error.
OsipError
¶ Bases:
RuntimeError
Base Osip error
Exception
Class
-
exception
exosip2ctypes.error.
OsipUnknownError
¶ Bases:
exosip2ctypes.error.OsipError
Osip error, but don’t known the error code
-
exception
exosip2ctypes.error.
OsipUndefinedError
¶
-
exception
exosip2ctypes.error.
OsipBadParameter
¶
-
exception
exosip2ctypes.error.
OsipWrongState
¶
-
exception
exosip2ctypes.error.
OsipNoMem
¶
-
exception
exosip2ctypes.error.
OsipSyntaxError
¶
-
exception
exosip2ctypes.error.
OsipNotFound
¶
-
exception
exosip2ctypes.error.
OsipApiNotInitialized
¶
-
exception
exosip2ctypes.error.
OsipNoNetwork
¶
-
exception
exosip2ctypes.error.
OsipPortBusy
¶
-
exception
exosip2ctypes.error.
OsipUnknownHost
¶
-
exception
exosip2ctypes.error.
OsipDiskFull
¶
-
exception
exosip2ctypes.error.
OsipNoRights
¶
-
exception
exosip2ctypes.error.
OsipFileNotExists
¶
-
exception
exosip2ctypes.error.
OsipTimeout
¶
-
exception
exosip2ctypes.error.
OsipTooMuchCall
¶
-
exception
exosip2ctypes.error.
OsipWrongFormat
¶
-
exception
exosip2ctypes.error.
OsipNoCommonCodec
¶
exosip2ctypes.event module¶
eXosip2 event API
see: http://www.antisip.com/doc/exosip2/group__eXosip2__event.html
-
class
exosip2ctypes.event.
Event
(ptr, context)¶ Bases:
object
Class for event description
Parameters: - ptr (ctypes.c_void_p) – struct eXosip_event_t *ptr
- context (Context) – eXosip context
-
ack
¶ Returns: ack within current transaction Return type: OsipMessage
-
dispose
()¶ Free resource in an eXosip event.
-
request
¶ Returns: request within current transaction Return type: ExosipMessage
-
response
¶ Returns: last response within current transaction Return type: ExosipMessage
-
class
exosip2ctypes.event.
EventType
¶ Bases:
enum.IntEnum
Enumeration of event types
-
call_ack
= 12¶
-
call_answered
= 7¶
-
call_cancelled
= 13¶
-
call_closed
= 21¶
-
call_globalfailure
= 11¶
-
call_invite
= 2¶
-
call_message_answered
= 16¶
-
call_message_globalfailure
= 20¶
-
call_message_new
= 14¶
-
call_message_proceeding
= 15¶
-
call_message_redirected
= 17¶
-
call_message_requestfailure
= 18¶
-
call_message_serverfailure
= 19¶
-
call_noanswer
= 4¶
-
call_proceeding
= 5¶
-
call_redirected
= 8¶
-
call_reinvite
= 3¶
-
call_released
= 22¶
-
call_requestfailure
= 9¶
-
call_ringing
= 6¶
-
call_serverfailure
= 10¶
-
in_subscription_new
= 38¶
-
message_answered
= 25¶
-
message_globalfailure
= 29¶
-
message_new
= 23¶
-
message_proceeding
= 24¶
-
message_redirected
= 26¶
-
message_requestfailure
= 27¶
-
message_serverfailure
= 28¶
-
notification_answered
= 41¶
-
notification_globalfailure
= 45¶
-
notification_noanswer
= 39¶
-
notification_proceeding
= 40¶
-
notification_redirected
= 42¶
-
notification_requestfailure
= 43¶
-
notification_serverfailure
= 44¶
-
registration_failure
= 1¶
-
registration_success
= 0¶
-
subscription_answered
= 32¶
-
subscription_globalfailure
= 36¶
-
subscription_noanswer
= 30¶
-
subscription_notify
= 37¶
-
subscription_proceeding
= 31¶
-
subscription_redirected
= 33¶
-
subscription_requestfailure
= 34¶
-
subscription_serverfailure
= 35¶
-
exosip2ctypes.message module¶
osip and eXosip messages
Message classes of osip and eXosip
-
class
exosip2ctypes.message.
OsipMessage
(ptr)¶ Bases:
object
class for osip2 message API
Parameters: ptr (ctypes.c_void_p) – Pointer to the osip_message_t structure in C library -
add_allow
(val)¶ Set the Allow header.
Parameters: val (str) – The string describing the element. Attention
This method will ADD a create ALLOW header
-
add_body
(val)¶ Fill the body of message.
Parameters: val (str) – Body string. Attention
This method will ADD a create body
-
add_contact
(val)¶ Set the Contact header.
Parameters: val (str) – The string describing the element. Attention
This method will ADD a create Contact header
-
add_header
(name, value)¶ Allocate and Add an “unknown” header (not defined in oSIP).
Parameters: Attention
This method will ADD a create header
-
get_headers
(name)¶ Find “unknown” header’s list. (not defined in oSIP)
Parameters: name (str) – The name of the header to find. Returns: Header’s value string list. Return type: list
-
ptr
¶ Pointer to the osip_message_t C Structure
Return type: ctypes.c_void_p
-
-
class
exosip2ctypes.message.
ExosipMessage
(ptr, context)¶ Bases:
exosip2ctypes.message.OsipMessage
class for eXosip2 message API
Parameters: - ptr (ctypes.c_void_p) – Pointer to the osip_message_t structure in C library
- context (Context) – eXosip context
Danger
Do NOT con/destruct the class yourself unless you known what you are doing.
Attention
In eXosip2, messages are managed inside the library, so we should NOT free
OsipMessage
object manually.-
send
()¶
exosip2ctypes.register module¶
eXosip2 REGISTER and Registration Management
-
class
exosip2ctypes.register.
InitialRegister
(context, from_, proxy, contact=None, expires=3600)¶ Bases:
exosip2ctypes.message.ExosipMessage
initial REGISTER request.
To start a registration, you need to build a default REGISTER request by providing several mandatory headers.
You can start as many registration you want even in one eXosip_t context.
The created instance has a registration identifier (
rid
) that you can use to update your registration. In future events about this registration, you’ll see that registration identifier when applicable.Note
You should let eXosip2 manage contact headers alone. The setup you have provided will generate various behavior, all being controlled by eXosip2. See the “NAT and Contact header” section in setup documentation.
Build initial REGISTER request.
Parameters: -
proxy
¶ :return:Proxy used for registration. :rtype: str
-
remove
()¶ Remove existing registration without sending REGISTER.
If you need to delete a context without sending a REGISTER with expires 0, you can use this method to release memory.
-
send
()¶ Send this REGISTER request
-
-
class
exosip2ctypes.register.
Register
(context, rid, expires=3600)¶ Bases:
exosip2ctypes.message.ExosipMessage
REGISTER request for an existing registration.
Use this class if you need to update a registration, You just need to reuse the registration identifier (
InitialRegister.rid
)Note
An UAC should delete its registration on the SIP server when terminating. To do so, you have to send a REGISTER request with the expires header set to value “0”.
Build a new REGISTER request for an existing registration.
Parameters: -
remove
()¶ Remove existing registration without sending REGISTER.
If you need to delete a context without sending a REGISTER with expires 0, you can use this method to release memory.
-
send
()¶ Send this REGISTER request
-
exosip2ctypes.sdp module¶
eXosip2 SDP helper API.
-
class
exosip2ctypes.sdp.
SdpMessage
(ptr)¶ Bases:
object
SDP body
Parameters: ptr (ctypes.c_void_p) – sdp_message_t* -
ptr
¶
-
exosip2ctypes.utils module¶
Some helper functions
-
exosip2ctypes.utils.
to_bytes
(s, encoding='utf-8')¶ Convert to bytes string.
Parameters: - s – String to convert.
- encoding (str) – Encoding codec.
Returns: bytes string, it’s bytes or str in Python 2.x, bytes in Python 3.x.
Return type: - In Python 2, convert s to bytes if it’s unicode.
- In Python 2, return original s if it’s not unicode.
- In Python 2, it equals to
to_str()
. - In Python 3, convert s to bytes if it’s unicode or str.
- In Python 3, return original s if it’s neither unicode nor str.
-
exosip2ctypes.utils.
to_str
(s, encoding='utf-8')¶ Convert to str string.
Parameters: - s – String to convert.
- encoding (str) – Decoding codec.
Returns: str string, it’s bytes in Python 2.x, unicode or str in Python 3.x.
Return type: - In Python 2, convert s to str if it’s unicode.
- In Python 2, return original s if it’s not unicode.
- In Python 2, it equals to
to_bytes()
. - In Python 3, convert s to str if it’s bytes.
- In Python 3, return original s if it’s not bytes.
- In Python 3, it equals to
to_unicode()
.
-
exosip2ctypes.utils.
to_unicode
(s, encoding='utf-8')¶ Convert to unicode string.
Parameters: - s – String to convert.
- encoding (str) – Encoding codec.
Returns: unicode string, it’s unicode in Python 2.x, str or unicode in Python 3.x.
Return type: unicode
- In Python 2, convert s to unicode if it’s str or bytes.
- In Python 2, return original s if it’s neither str or bytes.
- In Python 3, convert s to str or unicode if it’s bytes.
- In Python 3, return original s if it’s not bytes.
- In Python 3, it equals to
to_str()
.
-
class
exosip2ctypes.utils.
LoggerMixin
¶ Bases:
object
Mixin Class provide a
logger
property-
logger
¶ logger instance.
Return type: logging.Logger logger name format is ModuleName.ClassName
-
Module contents¶
eXosip API
author: | Liu Xue Yan |
---|---|
email: | realtanbro@gmail.com |
eXosip is a high layer library for rfc3261: the SIP protocol. It offers a simple API to make it easy to use. eXosip2 offers great flexibility for implementing SIP endpoint like:
- SIP User-Agents
- SIP Voicemail or IVR
- SIP B2BUA
- any SIP server acting as an endpoint (music server…)
If you need to implement proxy or complex SIP applications, you should consider using osip instead.
Here are the eXosip capabilities:
- REGISTER to rpc_handler registration.
- INVITE/BYE to start/stop VoIP sessions.
- INFO to send DTMF within a VoIP sessions.
- OPTIONS to simulate VoIP sessions.
- re-INVITE to modify VoIP sessions
- REFER/NOTIFY to transfer calls.
- MESSAGE to send Instant Message.
- SUBSCRIBE/NOTIFY to rpc_handler presence capabilities.
- any other request to rpc_handler what you want!
Functions¶
-
exosip2ctypes.
initialize
(path: str=None) → None:¶ Load libeXosip2 into this Python library
Parameters: path (str) – libeXosip2 SO/DLL path, default is None. When None or empty string, the function will try to find and load so/dll by DLL_NAME
Raises: RuntimeError – When failed loading so/dll Attention
You MUST call this function FIRST to initialize libeXosip2, before any other actions!