Project

General

Profile

SipCoreApiDocumentation » History » Version 40

Ruud Klaver, 04/20/2009 06:09 PM

1 12 Adrian Georgescu
= SIP core API =
2 1 Adrian Georgescu
3 28 Adrian Georgescu
[[TOC(Sip*, depth=3)]]
4 5 Adrian Georgescu
5 1 Adrian Georgescu
== Introduction ==
6
7 13 Adrian Georgescu
This chapter describes the internal architecture and API of the SIP core of the {{{sipsimple}}} library.
8 1 Adrian Georgescu
{{{sipsimple}}} is a Python package, the core of which wrapps the PJSIP C library, which handles SIP signaling and audio media for the SIP SIMPLE client.
9
10
SIP stands for 'Sessions Initiation Protocol', an IETF standard described by [http://tools.ietf.org/html/rfc3261 RFC 3261]. SIP is an application-layer control protocol that can establish,
11
modify and terminate multimedia sessions such as Internet telephony calls
12
(VoIP). Media can be added to (and removed from) an existing session.
13
14
SIP transparently supports name mapping and redirection services, which
15
supports personal mobility, users can maintain a single externally visible
16
address identifier, which can be in the form of a standard email address or
17
E.164 telephone number regardless of their physical network location.
18
19
SIP allows the endpoints to negotiate and combine any type of session they
20
mutually understand like video, instant messaging (IM), file transfer,
21
desktop sharing and provides a generic event notification system with
22
real-time publications and subscriptions about state changes that can be
23
used for asynchronous services like presence, message waiting indicator and
24
busy line appearance.
25
26
For a comprehensive overview of SIP related protocols and use cases visit http://www.tech-invite.com
27
28 30 Adrian Georgescu
== PJSIP library ==
29 1 Adrian Georgescu
30
{{{sipsimple}}} builds on PJSIP [http://www.pjsip.org], a set of static libraries, written in C, which provide SIP signaling and media capabilities.
31
PJSIP is considered to be the most mature and advanced open source SIP stack available.
32
The following diagram, taken from the PJSIP documentation, illustrates the library stack of PJSIP:
33
34
[[Image(http://www.pjsip.org/images/diagram.jpg, nolink)]]
35
36
The diagram shows that there is a common base library, and two more or less independent stacks of libraries, one for SIP signaling and one for SIP media.
37
The latter also includes an abstraction layer for the soundcard.
38
Both of these stracks are integrated in the high level library, called PJSUA.
39
40
PJSIP itself provides a high-level [http://www.pjsip.org/python/pjsua.htm Python wrapper for PJSUA].
41
Despite this, the choice was made to bypass PJSUA and write the SIP core of the {{{sipsimple}}} package as a Python wrapper, which directly uses the PJSIP and PJMEDIA libraries.
42
The main reasons for this are the following:
43
 * PJSUA assumes a session with exactly one audio stream, whilst for the SIP SIMPLE client more advanced (i.e. low-level) manipulation of the SDP is needed.
44
 * What is advertised as SIMPLE functionality, it is minimal and incomplete subset of it. Only page mode messaging using SIP MESSAGE method and basic device status presence are possible, while session mode IM and rich presence are desired.
45
 * PJSUA integrates the decoding and encoding of payloads (e.g. presence related XML documents), while in the SIP SIMPLE client this should be done at a high level, not by the SIP stack.
46
47
PJSIP itself is by nature asynchronous.
48
In the case of PJSIP it means that in general there will be one thread which handles reception and transmission of SIP signaling messages by means of a polling function which is continually called by the application.
49
Whenever the application performs some action through a function, this function will return immediately.
50
If PJSIP has a result for this action, it will notify the application by means of a callback function in the context of the polling function thread.
51
52
> NOTE: Currently the core starts the media handling as a separate C thread to avoid lag caused by the GIL.
53
> The soundcard also has its own C thread.
54
55
== Architecture ==
56
57
The {{{sipsimple}}} core wrapper itself is mostly written using [http://cython.org/ Cython] (formerly [http://www.cosc.canterbury.ac.nz/greg.ewing/python/Pyrex/ Pyrex]).
58
It allows a Python-like file with some added C manipulation statements to be compiled to C.
59
This in turn compiles to a Python C extension module, which links with the PJSIP static libraries.
60
61
The SIP core part of the {{{sipsimple}}} Python package is subdivided into three modules:
62
 '''sipimple.!__init!__'''::
63
  The the top-level module for the package which just defines the module version and the objects that should be imported when the package user performs {{{import * from sipsimple}}}.
64
 '''sipsimple.engine'''::
65
  Python module that contains the {{{Engine}}} singleton class, which manages the thread that constantly polls the PJSIP library, i.e. the PJSIP worker thread.
66
  For the applications that use the core of {{{sipsimple}}}, the {{{Engine}}} object forms the main entry point.
67
 '''sipsimple.core'''::
68
  This is the Python C extension module ultimately compiled from the Cython file and PJSIP static libraries.
69
  It contains these types of classes:
70
   * The {{{PJSIPUA}}} class, which can only be instanced once, and is this case is only instanced once by the {{{Engine}}} object.
71
     In this way the {{{Engine}}} singleton class acts as a wrapper to the one {{{PJSIPUA}}} instance.
72
     The {{{PJSIPUA}}} class represents the SIP endpoint and manages the initialization and destruction of all the PJSIP libraries.
73
     It also provides a number of methods.
74
     The application however should never call these methods directly on the {{{PJSIPUA}}} object, rather it should call them on the {{{Engine}}} wrapper object.
75
     This object handles everything that for one reason or another cannot or should not be handled from Cython.
76
   * The classes that represent the main SIP primitives to be used by the application.
77
     The application can instantiate these classes once the {{{Engine}}} class has been instantiated and the PJSIP worker thread has been started.
78
     All of these classes represent a state machine.
79
     * {{{Registration}}}
80
     * {{{Publication}}}
81
     * {{{Subscription}}}
82
     * {{{Invitation}}}
83
   * Several helper classes, which represent some structured collection of data to be passed as parameter to methods of the SIP primitive classes and to parameters of notifications.
84
     * {{{SIPURI}}}
85
     * {{{Credentials}}}
86
     * {{{Route}}}
87
   * A number of SDP manipulation classes, which directly wrap the PJSIP structures representing either the parsed or to be generated SDP.
88
     {{{SDPSession}}} objects may contain references to the other classes and are passed as arguments to methods of a {{{Invitiation}}} object or notifications sent by it.
89
     * {{{SDPSession}}}
90
     * {{{SDPMedia}}}
91
     * {{{SDPConnection}}}
92
     * {{{SDPAttribute}}}
93
   * Two classes related to transport of media traffic and audio traffic specifically, built on PJMEDIA.
94
     These classes can be instantiated independently from the other classes in order to keep signaling and media separate.
95
     * {{{RTPTransport}}}
96
     * {{{AudioTransport}}}
97
   * Two classes related to {{{.wav}}} files, one for playback and one for recording.
98
     * {{{WaveFile}}}
99
     * {{{RecordingWaveFile}}}
100
   * Two exception classes, the second being a subclass of the first.
101
     * {{{SIPCoreError}}}
102
     * {{{PJSIPError}}}
103
   * Classes used internally within the {{{core}}} module, e.g. to wrap a particular PJSIP library.
104
     These classes are not exposed through the {{{__init__}}} module and should never be used by the application
105
106
These classes (except the ones internal to the {{{core}}} module) are illustrated in the following diagram:
107
108
[[Image(sipsimple-core-classes.png, nolink)]]
109
110 11 Adrian Georgescu
== Integration ==
111 1 Adrian Georgescu
112
The core itself has one Python dependency, the [http://pypi.python.org/pypi/python-application application] module, which in turn depends on the [http://pypi.python.org/pypi/zope.interface zope.interface] module.
113
These modules should be present on the system before the core can be used.
114
An application that uses the SIP core must use the notification system provided by the {{{application}}} module in order to receive notifications from it.
115
It does this by creating one or more classes that act as an observer for particular messages and registering it with the {{{NotificationCenter}}}, which is a singleton class.
116
This means that any call to instance an object from this class will result in the same object.
117
As an example, this bit of code will create an observer for logging messages only:
118
119
{{{
120
from zope.interface import implements
121
from application.notification import NotificationCenter, IObserver
122
123 29 Luci Stanescu
class SIPEngineLogObserver(object):
124 1 Adrian Georgescu
    implements(IObserver)
125
126
    def handle_notification(self, notification):
127
        print "%(timestamp)s (%(level)d) %(sender)14s: %(message)s" % notification.data.__dict__
128
129
notification_center = NotificationCenter()
130
log_observer = EngineLogObserver()
131 29 Luci Stanescu
notification_center.add_observer(self, name="SIPEngineLog")
132 1 Adrian Georgescu
}}}
133
134
Each notification object has three attributes:
135
 '''sender'''::
136
  The object that sent the notification.
137
  For generic notifications the sender will be the {{{Engine}}} instance, otherwise the relevant object.
138
 '''name'''::
139
  The name describing the notification.
140 29 Luci Stanescu
  All messages will be described in this document and start with the prefix "SIP".
141 1 Adrian Georgescu
 '''data'''::
142
  An instance of {{{application.notification.NotificationData}}} or a subclass of it.
143
  The attributes of this object provide additional data about the notification.
144
  Notifications described in this document will also have the data attributes described.
145
146
Besides setting up the notification observers, the application should import the relevant objects from the core by issuing the {{{from sipsimple import *}}} statement.
147
It can then instance the {{{Engine}}} class, which is also a singleton, and start the PJSIP worker thread by calling {{{Engine.start()}}}, optionally providing a number of initialization options.
148
Most of these options can later be changed at runtime, by setting attributes of the same name on the {{{Engine}}} object.
149
The application may then instance one of the SIP primitive classes and perform operations on it.
150
151
When starting the {{{Engine}}} class, the application can pass a number of keyword arguments that influence the behaviour of the SIP endpoint.
152
For example, the SIP network ports may be set through the {{{local_udp_port}}}, {{{local_tcp_port}}} and {{{local_tls_port}}} arguments.
153
The UDP/RTP ports are described by a range of ports through {{{rtp_port_range}}}, two of which will be randomly selected for each {{{RTPTransport}}} object and effectively each audio stream.
154
155
The methods called on the SIP primitive objects and the {{{Engine}}} object (proxied to the {{{PJSIPUA}}} instance) may be called from any thread.
156 31 Ruud Klaver
They will return immediately and any delayed result, such as results depending on network traffic, will be returned later using a notification.
157
In this manner the SIP core continues the asynchronous pattern of PJSIP.
158 1 Adrian Georgescu
If there is an error in processing the request, an instance of {{{SIPCoreError}}}, or its subclass {{{PJSIPError}}} will be raised.
159
The former will be raised whenever an error occurs inside the core, the latter whenever an underlying PJSIP function returns an error.
160
The {{{PJSIPError}}} object also contains a status attribute, which is the PJSIP errno as an integer.
161
162
As a very basic example, one can REGISTER for a sip account by typing the following lines on a Python console:
163
{{{
164
from sipsimple import *
165
e = Engine()
166
e.start()
167
cred = Credentials(SIPURI(user="alice", host="example.com"), "password")
168 22 Ruud Klaver
reg = Registration(cred, Route("1.2.3.4"))
169 1 Adrian Georgescu
reg.register()
170
}}}
171 26 Oliver Bril
Note that in this example no observer for notifications from this {{{Registration}}} object are registered, so the result of the operation cannot be seen.
172 1 Adrian Georgescu
173 32 Ruud Klaver
To see how this example can be converted to actually wait for the response without creating an observer, see the example for GreenRegistration at [wiki:SipSynchronousAPI#sipsimple.green.core.GreenRegistration SynchronousAPI].
174
175 1 Adrian Georgescu
== Components ==
176
177
=== Engine ===
178
179
As explained above, this singleton class needs to be instantiated by the application using the SIP core of {{{sipsimple}}} and represents the whole SIP core stack.
180
Once the {{{start()}}} method is called, it instantiates the {{{core.PJSIPUA}}} object and will proxy attribute and methods from it to the application.
181
182
==== attributes ====
183
184
 '''default_start_options''' (class attribute)::
185
  This dictionary is a class attribute that describes the default values for the initialization options passed as keyword arguments to the {{{start()}}} method.
186
  Consult this method for documentation of the contents.
187 32 Ruud Klaver
 '''is_running'''::
188
  A boolean property indicating if the {{{Engine}}} is running and if it is safe to try calling any proxied methods or attributes on it.
189 1 Adrian Georgescu
190
==== methods ====
191
192
 '''!__init!__'''(''self'')::
193
  This will either create the {{{Engine}}} if it is called for the first time or return the one {{{Engine}}} instance if it is called subsequently.
194
 '''start'''(''self'', '''**kwargs''')::
195
  Initialize all PJSIP libraries based on the keyword parameters provited and start the PJSIP worker thread.
196
  If this fails an appropriate exception is raised.
197
  After the {{{Engine}}} has been started successfully, it can never be started again after being stopped.
198
  The keyword arguments will be discussed here.
199
  Many of these values are also readable as (proxied) attributes on the Engine once the {{{start()}}} method has been called.
200
  Many of them can also be set at runtime, either by modifying the attribute or by calling a particular method.
201
  This will also be documented for each argument in the following list of options.
202
  [[BR]]''auto_sound'':[[BR]]
203
  A boolean indicating if PJSIP should automatically select and enable a soundcard to use for for recording and playing back sound.
204 39 Ruud Klaver
  If this is set to {{{False}}} the Dummy sound device is selected.
205
  To be able to hear and contribute audio, the application will have to select a sound device manually through the {{{set_sound_devices}}} method.
206 1 Adrian Georgescu
  This option is not accessible as an attribute on the object, as it is transitory.
207
  [[BR]]''local_ip'': (Default: {{{None}}})[[BR]]
208
  IP address of a local interface to bind to.
209
  If this is {{{None}}} on start, the {{{Engine}}} will try to determine the default outgoing interface and bind to it.
210
  Setting this to {{{0.0.0.0}}} will cause PJSIP to listen for traffic on any interface, but this is not recommended.
211
  As an attribute, this value is read-only.
212
  [[BR]]''local_udp_port'': (Default: {{{0}}})[[BR]]
213
  The local UDP port to listen on for UDP datagrams.
214
  If this is 0, a random port will be chosen.
215
  If it is {{{None}}}, the UDP transport is disabled, both for incoming and outgoing traffic.
216
  As an attribute, this value is read-only, but it can be changed at runtime using the {{{set_local_udp_port()}}} method.
217
  [[BR]]''local_tcp_port'': (Default: {{{0}}})[[BR]]
218
  The local TCP port to listen on for new TCP connections.
219
  If this is 0, a random port will be chosen.
220
  If it is {{{None}}}, the TCP transport is disabled, both for incoming and outgoing traffic.
221
  As an attribute, this value is read-only, but it can be changed at runtime using the {{{set_local_tcp_port()}}} method.
222
  [[BR]]''local_tls_port'': (Default: {{{0}}})[[BR]]
223
  The local TCP port to listen on for new TLS over TCP connections.
224 28 Adrian Georgescu
  If this is 0, a random port will be chosen.
225 1 Adrian Georgescu
  If it is {{{None}}}, the TLS transport is disabled, both for incoming and outgoing traffic.
226 35 Ruud Klaver
  As an attribute, this value is read-only, but it can be changed at runtime using the {{{set_tls_options()}}} method, as internally the TLS transport needs to be restarted for this operation. 
227 32 Ruud Klaver
  [[BR]]''tls_protocol'': (Default: "TLSv1")[[BR]] 
228
  This string describes the (minimum) TLS protocol that should be used. 
229
  Its values should be either {{{None}}}, "SSLv2", "SSLv23", "SSLv3" or "TLSv1". 
230
  If {{{None}}} is specified, the PJSIP default will be used, which is currently "TLSv1". 
231 1 Adrian Georgescu
  [[BR]]''tls_verify_server'': (Default: {{{False}}})[[BR]]
232 28 Adrian Georgescu
  This boolean indicates whether PJSIP should verify the certificate of the server against the local CA list when making an outgoing TLS connection.
233 32 Ruud Klaver
  As an attribute, this value is read-only, but it can be changed at runtime using the {{{set_tls_options()}}} method, as internally the TLS transport needs to be restarted for this operation.
234 1 Adrian Georgescu
  [[BR]]''tls_ca_file'': (Default: {{{None}}})[[BR]]
235
  This string indicates the location of the file containing the local list of CA certificates, to be used for TLS connections.
236 32 Ruud Klaver
  If this is set to {{{None}}}, no CA certificates will be read. 
237
  As an attribute, this value is read-only, but it can be changed at runtime using the {{{set_tls_options()}}} method, as internally the TLS transport needs to be restarted for this operation. 
238
  [[BR]]''tls_cert_file'': (Default: {{{None}}})[[BR]] 
239
  This string indicates the location of a file containing the TLS certificate to be used for TLS connections. 
240
  If this is set to {{{None}}}, no certificate file will be read. 
241
  As an attribute, this value is read-only, but it can be changed at runtime using the {{{set_tls_options()}}} method, as internally the TLS transport needs to be restarted for this operation. 
242
  [[BR]]''tls_privkey_file'': (Default: {{{None}}})[[BR]] 
243
  This string indicates the location of a file containing the TLS private key associated with the above mentioned certificated to be used for TLS connections. 
244
  If this is set to {{{None}}}, no private key file will be read. 
245
  As an attribute, this value is read-only, but it can be changed at runtime using the {{{set_tls_options()}}} method, as internally the TLS transport needs to be restarted for this operation. 
246
  [[BR]]''tls_timeout'': (Default: 1000)[[BR]] 
247
  The timeout value for a TLS negotiation in milliseconds. 
248
  Note that this value should be reasonably small, as a TLS negotiation blocks the whole PJSIP polling thread. 
249
  As an attribute, this value is read-only, but it can be changed at runtime using the {{{set_tls_options()}}} method, as internally the TLS transport needs to be restarted for this operation.
250 1 Adrian Georgescu
  [[BR]]''ec_tail_length'': (Default: {{{50}}})[[BR]]
251
  Echo cancellation tail length in milliseconds.
252
  A longer value should provide better echo cancellation but incurs more processing cost.
253
  Setting this to 0 will disable echo cancellation.
254 20 Ruud Klaver
  As an attribute, this value is read-only, but it can be set as an argument to the {{{set_sound_devices}}} method.
255 1 Adrian Georgescu
  [[BR]]''user_agent'': (Default: {{{"ag-projects/sipclient-%version-pjsip-%pjsip-version"}}})[[BR]]
256
  This value indicates what should be set in the {{{User-Agent}}} header, which is included in each request or response sent.
257
  It can be read and set directly as an attribute at runtime.
258
  [[BR]]''log_level'': (Default: 5)[[BR]]
259 29 Luci Stanescu
  This integer dictates the maximum log level that may be reported to the application by PJSIP through the {{{SIPEngineLog}}} notification.
260 1 Adrian Georgescu
  By default the maximum amount of logging information is reported.
261
  This value can be read and set directly as an attribute at runtime.
262
  [[BR]]''trace_sip'': (Default: {{{False}}})[[BR]]
263 29 Luci Stanescu
  This boolean indicates if the SIP core should send the application SIP messages as seen on the wire through the {{{SIPEngineSIPTrace}}} notification.
264 1 Adrian Georgescu
  It can be read and set directly as an attribute at runtime.
265
  [[BR]]''sample_rate'': (Default: {{{32}}})[[BR]]
266
  The sample rate in kHz at which the sound card should operate.
267
  Higher values allow some codecs (such as speex) to achieve better quality but will incur higher processing cost, particularly in combination with echo cancellation.
268
  This parameter should be either 8, 16 or 32.
269
  The corresponding attribute of this value is read-only.
270
  [[BR]]''playback_dtmf'': (Default: {{{True}}})[[BR]]
271
  If this boolean is set to {{{True}}}, both incoming and outgoing DTMF signals have their corresponding audio tones played back on the sound card.
272
  This value can be read and set directly as an attribute at runtime.
273
  [[BR]]''rtp_port_range'': (Default: (40000, 40100))[[BR]]
274
  This tuple of two ints indicates the range to select UDP ports from when creating a new {{{RTPTransport}}} object, which is used to transport media.
275
  It can be read and set directly as an attribute at runtime, but the ports of previously created {{{RTPTransport}}} objects remain unaffected.
276 40 Ruud Klaver
  [[BR]]''codecs'': (Default: {{{["speex", "G722", "PCMU", "PCMA", "iLBC", "GSM"]}}})[[BR]]
277 1 Adrian Georgescu
  This list specifies the codecs to use for audio sessions and their preferred order.
278
  It can be read and set directly as an attribute at runtime.
279 40 Ruud Klaver
  Note that this global option can be overridden by an argument passed to {{{AudioTransport.__init__()}}}.
280
  The strings in this list is case insensitive.
281 1 Adrian Georgescu
  [[BR]]''events'': (Default: <some sensible events>)[[BR]]
282
  PJSIP needs a mapping between SIP SIMPLE event packages and content types.
283
  This dictionary provides some default packages and their event types.
284
  As an attribute, this value is read-only, but it can be changed at runtime using the {{{set_local_tls_port()}}} method.
285 32 Ruud Klaver
 '''start_cfg'''(''self'', '''enable_sound'''={{{True}}}, '''**kwargs''')::
286 36 Adrian Georgescu
  This provides a [wiki:SipConfigurationAPI Configuration API] aware start method.
287 32 Ruud Klaver
  It will retrieve all the arguments that can be passed to {{{start()}}} from the configuration framework, unless they are overridden by the keyword arguments to this method.
288
  Any argument that can be passed to {{{start()}}} may be specified, except for {{{auto_sound}}}.
289 35 Ruud Klaver
  [[BR]]''enable_sound'':[[BR]]
290 1 Adrian Georgescu
  If this is set to {{{True}}}, the sound devices set up in the configuration framework will be started.
291 39 Ruud Klaver
  If it is {{{False}}}, the Dummy sound device will be used.
292 1 Adrian Georgescu
 '''stop'''(''self'')::
293
  Stop the PJSIP worker thread and unload all PJSIP libraries.
294
  Note that after this all references to SIP core objects can no longer be used, these should be properly removed by the application itself before stopping the {{{Engine}}}.
295
  Also note that, once stopped the {{{Engine}}} cannot be started again.
296
  This method is automatically called when the Python interpreter exits.
297
298
==== proxied attributes ====
299
300
Besides all the proxied attributes described for the {{{__init__}}} method above, two other attributes are provided once the {{{Engine}}} has been started.
301
302
 '''playback_devices'''::
303 20 Ruud Klaver
  This read-only attribute is a list of string, representing all audio playback devices on the system that can be used.
304
  These device names can be passed as the {{{playback_device}}} argument of the {{{set_sound_devices()}}} method.
305 1 Adrian Georgescu
 '''recording_devices'''::
306
  This read-only attribute is a list of string, representing all audio recording devices on the system that can be used.
307
  These device names can be passed as the {{{recording_device}}} argument of the {{{set_sound_devices()}}} method.
308 40 Ruud Klaver
 '''available_codecs'''::
309
  A read-only list of codecs available in the core, regardless of the codecs configured through the {{{codecs}}} attribute.
310 1 Adrian Georgescu
311
==== proxied methods ====
312
313
 '''add_event'''(''self'', '''event''', '''accept_types''')::
314
  Couple a certain event package to a list of content types.
315
  Once added it cannot be removed or modified.
316 20 Ruud Klaver
 '''set_sound_devices'''(''self'', '''playback_device'''={{{None}}}, '''recording_device'''={{{None}}}, '''tail_length'''={{{None}}})::
317 1 Adrian Georgescu
  Set and open the playback and recording device, using the specified echo cancellation tail length in milliseconds.
318
  If no echo cancellation value is specified, either the value set while calling the {{{start()}}} method will be used or the default value will be used (see the documentation of this method).
319
  A {{{tail_length}}} of 0 disables echo cancellation.
320
  The device attributes need to be strings and should be obtained from the {{{playback_devices}}} and {{{recording_devices}}} attributes respectively.
321
  Passing either of these as {{{None}}} means that PJSIP should select a sound device automatically.
322
  If sound devices were already opened these will be closed first.
323
 '''connect_audio_transport'''(''self'', '''transport''')::
324
  Connect a started audio transport, in the form of a {{{AudioTransport}}} object, to the recording and playback audio devices and other connected audio transports.
325
  This means that when more than one audio stream is connected they will form a conference.
326 29 Luci Stanescu
 '''disconnect_audio_transport'''(''self'', '''transport''')::
327 1 Adrian Georgescu
  Disconnect a previously connected audio transport, in the form of a {{{AudioTransport}}} object.
328
  Stopped audio streams are disconnected automatically.
329 38 Ruud Klaver
 '''detect_nat_type'''(''self'', '''stun_server_address''', '''stun_server_port'''=3478, '''user_data'''=None)::
330 1 Adrian Georgescu
  Will start a series of STUN requests which detect the type of NAT this host is behind.
331
  The {{{stun_server_address}}} parameter indicates the IP address or hostname of the STUN server to be used and {{{stun_server_port}}} specifies the remote UDP port to use.
332 38 Ruud Klaver
  When the type of NAT is detected, this will be reported back to the application by means of a {{{SIPEngineDetectedNATType}}} notification, including the user_data object passed with this method.
333 1 Adrian Georgescu
 '''set_local_udp_port'''(''self'', '''value''')::
334 28 Adrian Georgescu
  Update the {{{local_udp_port}}} attribute to the newly specified value.
335
 '''set_local_tcp_port'''(''self'', '''value''')::
336
  Update the {{{local_tcp_port}}} attribute to the newly specified value.
337 32 Ruud Klaver
 '''set_tls_options'''(''self'', '''local_port'''={{{None}}}, '''protocol'''="TLSv1", '''verify_server'''={{{False}}}, '''ca_file'''={{{None}}}, '''cert_file'''={{{None}}}, '''privkey_file'''={{{None}}}, '''timeout'''=1000):: 
338
  Calling this method will (re)start the TLS transport with the specified arguments, or stop it in the case that the '''local_port''' argument is set to {{{None}}}. 
339
  The semantics of the arguments are the same as on the {{{start()}}} method. 
340 1 Adrian Georgescu
 '''parse_sip_uri(''self'', '''uri_string''')::
341
  Will parse the provided SIP URI string using the PJSIP parsing capabilities and return a {{{SIPURI}}} object, or raise an exception if there was an error parsing the URI.
342
343
==== notifications ====
344
345 16 Ruud Klaver
Notifications sent by the {{{Engine}}} are notifications that are related to the {{{Engine}}} itself or unrelated to any SIP primitive object.
346 1 Adrian Georgescu
They are described here including the data attributes that is included with them.
347
348 29 Luci Stanescu
 '''SIPEngineWillStart'''::
349 16 Ruud Klaver
  This notification is sent when the {{{Engine}}} is about to start.
350
  [[BR]]''timestamp'':[[BR]]
351
  A {{{datetime.datetime}}} object indicating when the notification was sent.
352 29 Luci Stanescu
 '''SIPEngineDidStart'''::
353 16 Ruud Klaver
  This notification is sent when the {{{Engine}}} is has just been started.
354
  [[BR]]''timestamp'':[[BR]]
355
  A {{{datetime.datetime}}} object indicating when the notification was sent.
356 29 Luci Stanescu
 '''SIPEngineDidFail'''::
357 16 Ruud Klaver
  This notification is sent whenever the {{{Engine}}} has failed fatally and either cannot start or is about to stop.
358
  It is not recommended to call any methods on the {{{Engine}}} at this point.
359
  [[BR]]''timestamp'':[[BR]]
360
  A {{{datetime.datetime}}} object indicating when the notification was sent.
361 29 Luci Stanescu
 '''SIPEngineWillEnd'''::
362 16 Ruud Klaver
  This notification is sent when the {{{Engine}}} is about to stop because the application called the {{{stop()}}} method.
363
  Methods on the {{{Engine}}} can be called at this point, but anything that has a delayed result will probably not return any notification.
364
  [[BR]]''timestamp'':[[BR]]
365
  A {{{datetime.datetime}}} object indicating when the notification was sent.
366 29 Luci Stanescu
 '''SIPEngineDidEnd'''::
367 16 Ruud Klaver
  This notification is sent when the {{{Engine}}} was running and is now stopped, either because of failure or because the application requested it.
368
  [[BR]]''timestamp'':[[BR]]
369
  A {{{datetime.datetime}}} object indicating when the notification was sent.
370 29 Luci Stanescu
 '''SIPEngineLog'''::
371 1 Adrian Georgescu
  This notification is a wrapper for PJSIP logging messages.
372
  It can be used by the application to output PJSIP logging to somewhere meaningful, possibly doing filtering based on log level.
373
  [[BR]]''timestamp'':[[BR]]
374
  A {{{datetime.datetime}}} object representing the time when the log message was output by PJSIP.
375
  [[BR]]''sender'':[[BR]]
376
  The PJSIP module that originated this log message.
377
  [[BR]]''level'':[[BR]]
378
  The logging level of the message as an integer.
379
  Currently this is 1 through 5, 1 being the most critical.
380
  [[BR]]''message'':[[BR]]
381
  The actual log message.
382 29 Luci Stanescu
 '''SIPEngineSIPTrace'''::
383 1 Adrian Georgescu
  Will be sent only when the {{{do_siptrace}}} attribute of the {{{Engine}}} instance is set to {{{True}}}.
384
  The notification data attributes will contain the SIP messages as they are sent and received on the wire.
385
  [[BR]]''timestamp'':[[BR]]
386
  A {{{datetime.datetime}}} object indicating when the notification was sent.
387
  [[BR]]''received'':[[BR]]
388
  A boolean indicating if this message was sent from or received by PJSIP (i.e. the direction of the message).
389
  [[BR]]''source_ip'':[[BR]]
390
  The source IP address as a string.
391
  [[BR]]''source_port'':[[BR]]
392
  The source port of the message as an integer.
393
  [[BR]]''destination_ip'':[[BR]]
394
  The destination IP address as a string.
395
  [[BR]]''source_port'':[[BR]]
396
  The source port of the message as an integer.
397
  [[BR]]''data'':[[BR]]
398
  The contents of the message as a string.
399
400
> For received message the destination_ip and for sent messages the source_ip may not be reliable.
401
402 29 Luci Stanescu
 '''SIPEngineGotMessage'''::
403 1 Adrian Georgescu
  This notification is sent when there is an incoming {{{MESSAGE}}} request.
404
  Since this is a one-shot occurrence, it is not modeled as an object.
405
  [[BR]]''timestamp'':[[BR]]
406
  A {{{datetime.datetime}}} object indicating when the notification was sent.
407
  [[BR]]''to_uri'':[[BR]]
408
  The contents of the {{{To:}}} header of the received {{{MESSAGE}}} request represented as a {{{SIPURI}}} object.
409
  [[BR]]''from_uri'':[[BR]]
410
  The contents of the {{{From:}}} header of the received {{{MESSAGE}}} request represented as a {{{SIPURI}}} object.
411
  [[BR]]''content_type'':[[BR]]
412
  The first part of the {{{Content-Type:}}} header of the received {{{MESSAGE}}} request (before the {{{/}}}).
413
  [[BR]]''content_subtype'':[[BR]]
414
  The second part of the {{{Content-Type:}}} header of the received {{{MESSAGE}}} request (after the {{{/}}}).
415
  [[BR]]''body'':[[BR]]
416
  The body of the {{{MESSAGE}}} request.
417
418
> content_type and content_subtype should be combined in a single argument, also in other places where this occurs.
419
420 29 Luci Stanescu
 '''SIPEngineGotMessageResponse'''::
421 1 Adrian Georgescu
  When sending a {{{MESSAGE}}} through the {{{send_message}}} function, this notification will be sent whenever there is a final response to the sent {{{MESSAGE}}} request (which may be an internally generated timeout).
422
  [[BR]]''timestamp'':[[BR]]
423
  A {{{datetime.datetime}}} object indicating when the notification was sent.
424
  [[BR]]''to_uri'':[[BR]]
425
  The original {{{to_uri}}} parameter used when calling the {{{send_message}}} function.
426
  [[BR]]''code'':[[BR]]
427
  The status code of the response as integer.
428
  [[BR]]''reason'':[[BR]]
429
  The reason text of the response.
430 29 Luci Stanescu
 '''SIPEngineDetectedNATType'''::
431 1 Adrian Georgescu
  This notification is sent some time after the application request the NAT type this host behind to be detected using a STUN server.
432
  Note that there is no way to associate a request to do this with a notification, although every call to the {{{detect_nat_type()}}} method will generate exactly one notification.
433
  [[BR]]''timestamp'':[[BR]]
434
  A {{{datetime.datetime}}} object indicating when the notification was sent.
435
  [[BR]]''succeeded'':[[BR]]
436
  A boolean indicating if the NAT detection succeeded.
437 38 Ruud Klaver
  [[BR]]''user_data'':[[BR]]
438
  The {{{user_data}}} argument passed while calling the {{{detect_nat_type()}}} method.
439
  This can be any object and could be used for matching requests to responses.
440 1 Adrian Georgescu
  [[BR]]''nat_type'':[[BR]]
441
  A string describing the type of NAT found.
442
  This value is only present if NAT detection succeeded.
443
  [[BR]]''error'':[[BR]]
444
  A string indicating the error that occurred while attempting to detect the type of NAT.
445
  This value only present if NAT detection did not succeed.
446 29 Luci Stanescu
 '''SIPEngineGotException'''::
447 16 Ruud Klaver
  This notification is sent whenever there is an unexpected exception within the PJSIP working thread.
448
  The application should show the traceback to the user somehow.
449 29 Luci Stanescu
  An exception need not be fatal, but if it is it will be followed by a '''SIPEngineDidFail''' notification.
450 1 Adrian Georgescu
  [[BR]]''timestamp'':[[BR]]
451
  A {{{datetime.datetime}}} object indicating when the notification was sent.
452
  [[BR]]''traceback'':[[BR]]
453
  A string containing the traceback of the exception.
454
  In general this should be printed on the console.
455
456
=== send_message ===
457
458
> In the future, this function will probably be implemented as a class or as a method of PJSIPUA.
459
460
The only function of the API is {{{send_message}}}, which sends a {{{MESSAGE}}} request containing a body to the specified SIP URI.
461
As described above, a {{{message_response}}} is generated when the final response is received.
462
Until the final response is received it is not allowed to send a new {{{MESSAGE}}} request to the {{{to_uri}}} used, a {{{SIPCoreError}}} exception will be thrown if the application tries this.
463
It has the following format and arguments:
464
{{{
465 22 Ruud Klaver
send_message(credentials, to_uri, content_type, content_subtype, body, route)
466 1 Adrian Georgescu
}}}
467
 '''credentials'''::
468
  Credentials to be used if authentication is needed at the proxy in the form of a {{{Credentials}}} object.
469
  This object also contains the From URI.
470
 '''to_uri'''::
471
  The SIP URI to send the {{{MESSAGE}}} request to in the form of a {{{SIPURI}}} object.
472
 '''content_type'''::
473
  The first part of the {{{Content-Type:}}} header (before the {{{/}}}).
474
 '''content_subtype'''::
475
  The first part of the {{{Content-Type:}}} header (before the {{{/}}}).
476
 '''body'''::
477
  The body of the {{{MESSAGE}}} request that is to be sent.
478
 '''route'''::
479
  This represents the first host to send the request to in the form of a {{{Route}}} object.
480
481
> The exception thrown when the application tries to send a MESSAGE too fast should be customized.
482
> In this way the application may keep a queue of MESSAGE requests and send the next one when the last one was answered.
483
484
=== SIPURI ===
485
486
This is a helper object for representing a SIP URI.
487
This object needs to be used whenever a SIP URI should be specified to the SIP core.
488
It supports comparison to other {{{SIPURI}}} objects using the == and != expressions.
489
As all of its attributes are set by the {{{__init__}}} method, the individual attributes will not be documented here.
490
491
==== methods ====
492
493 23 Ruud Klaver
 '''!__init!__'''(''self'', '''host''', '''user'''={{{None}}}, '''port'''={{{None}}}, '''display'''={{{None}}}, '''secure'''={{{False}}}, '''parameters'''={{{None}}}, '''headers'''={{{None}}})::
494 1 Adrian Georgescu
  Creates the SIPURI object with the specified parameters as attributes.
495
  Each of these attributes can be accessed and changed on the object once instanced.
496
  {{{host}}} is the only mandatory attribute.
497
  [[BR]]''host'':[[BR]]
498
  The host part of the SIP URI as a string.
499
  [[BR]]''user'':[[BR]]
500
  The username part of the SIP URI as a string, or None if not set.
501
  [[BR]]''port'':[[BR]]
502
  The port part of the SIP URI as an int, or None or 0 if not set.
503
  [[BR]]''display'':[[BR]]
504
  The optional display name of the SIP URI as a string, or None if not set.
505
  [[BR]]''secure'':[[BR]]
506
  A boolean indicating whether this is a SIP or SIPS URI, the latter being indicated by a value of {{{True}}}.
507
  [[BR]]''parameters'':[[BR]]
508
  The URI parameters. represented by a dictionary.
509
  [[BR]]''headers'':[[BR]]
510
  The URI headers, represented by a dictionary.
511
 '''!__str!__'''(''self'')::
512
  The special Python method to represent this object as a string, the output is the properly formatted SIP URI.
513
 '''copy'''(''self'')::
514
  Returns a copy of the {{{SIPURI}}} object.
515
516
=== Credentials ===
517
518
This object represents authentication credentials for a particular SIP account.
519
These should be included whenever creating a SIP primitive object that originates SIP requests.
520
As with the {{{SIPURI}}} object, the attributes of this object are the same as the arguments to the {{{__init__}}} method.
521
522
==== methods ====
523
524 33 Ruud Klaver
 '''!__init!__'''(''self'', '''uri''', '''password'''={{{None}}})::
525 1 Adrian Georgescu
  Creates the Credentials object with the specified parameters as attributes.
526
  Each of these attributes can be accessed and changed on the object once instanced.
527
  [[BR]]''uri'':[[BR]]
528
  A {{{SIPURI}}} object representing the account for which these are the credentials.
529
  [[BR]]''password'':[[BR]]
530
  The password for this SIP account as a string.
531
  If a password is not needed, for example when sending SIP messages without a proxy, this can be {{{None}}}.
532
 '''copy'''(''self'')::
533
  Returns a copy of the {{{Credentials}}} object.
534
535
=== Route ===
536
537 22 Ruud Klaver
This class provides a means for the application using the SIP core to set the destination address, port and transport for a particular request, i.e. the outbound proxy.
538 33 Ruud Klaver
As it is the application's responsibility to look this up and pass it as an argument for every SIP primitive class it creates.
539 22 Ruud Klaver
The contents of the {{{Route}}} object will be placed in the {{{Route}}} header of the request.
540 1 Adrian Georgescu
As with the {{{SIPURI}}} object, the attributes of this object are the same as the arguments to the {{{__init__}}} method.
541
542
==== methods ====
543
544 22 Ruud Klaver
 '''!__init!__'''(''self'', '''address''', '''port'''=5060, '''transport'''={{{None}}})::
545 1 Adrian Georgescu
  Creates the Route object with the specified parameters as attributes.
546
  Each of these attributes can be accessed on the object once instanced.
547 22 Ruud Klaver
  [[BR]]''address'':[[BR]]
548
  The IPv4 or IPv6 address that the request in question should be sent to as a string.
549 1 Adrian Georgescu
  [[BR]]''port'':[[BR]]
550 22 Ruud Klaver
  The port to send the requests to, represented as an int.
551 1 Adrian Georgescu
  [[BR]]''transport'':[[BR]]
552 35 Ruud Klaver
  The transport to use, this can be a string of either "udp", "tcp" or "tls" (case insensitive), depending on what transports are enabled on the {{{PJSIPUA}}} object.
553 1 Adrian Georgescu
 '''copy'''(''self'')::
554
  Returns a copy of the {{{Route}}} object.
555
556 22 Ruud Klaver
> IPv6 transports are not supported yet.
557
558 1 Adrian Georgescu
=== Registration ===
559
560
A {{{Registration}}} object represents a SIP endpoint's registration for a particular SIP account using the {{{REGISTER}}} method at its SIP registrar.
561
In effect, the SIP endpoint can send a {{{REGISTER}}} to the registrar to indicate that it is a valid endpoint for the specified SIP account.
562
After the {{{REGISTER}}} request is successfully received, the SIP proxy will be able to contact the SIP endpoint whenever there is an {{{INVITE}}} or other relevant request sent to the SIP account.
563
In short, unless a SIP endpoint is registered, it cannot be contacted.
564
Internally it uses a state machine to represent the registration process.
565
The states of this state machine can be seen in the following diagram:
566
567 2 Adrian Georgescu
[[Image(sipsimple-registration-state-machine.png, nolink)]]
568 1 Adrian Georgescu
569
State changes are triggered by the following events:
570
 1. The initial state.
571
 2. User requests in the form of the {{{register()}}} and {{{unregister()}}} methods.
572
 3. A final response for a {{{REGISTER}}} is received from the network.
573
 4. The refresh timer expires.
574
The state machine of a {{{Registration}}} object has a queue, which means that for example when the object is in the {{{registering}}} state and the application calls the {{{unregister()}}} method, the object will unregister itself once a final response has been received for the registering {{{REGISTER}}}.
575
576
> The implementation of this object needs to be revised.
577
578
==== attributes ====
579
580
 '''state'''::
581
  Indicates which state the internal state machine is in.
582
  This is one of {{{unregistered}}}, {{{registering}}}, {{{registered}}}, {{{unregistering}}}.
583
 '''credentials'''::
584
  The SIP credentials needed to authenticate at the SIP proxy in the form of a {{{Credentials}}} object, including the SIP account for which we want to register.
585
  This attribute is set on object instantiation and is read-only.
586
 '''route'''::
587
  The outbound proxy to use in the form of a {{{Route}}} object.
588
  This attribute is set on object instantiation and is read-only.
589
 '''extra_headers'''::
590
  A dictionary of extra headers that should be added to any outgoing {{{REGISTER}}} request.
591
  This attribute is set on object instantiation and is read-only.
592
 '''expires'''::
593
  The amount of seconds to request the registration for, i.e. the value that should be put in the {{{Expires}}} header.
594
  This attribute is set on object instantiation and can be modified at runtime.
595
  A new value will be used during the next refreshing {{{REGISTER}}}.
596
 '''expires_received'''::
597
  The amount of seconds the last successful {{{REGISTER}}} is valid for.
598
  This value is read-only.
599
600
==== methods ====
601
602 33 Ruud Klaver
 '''!__init!__'''(''self'', '''credentials''', '''route''', '''expires'''=300, '''contact_uri'''={{{None}}}, '''extra_headers'''={{{None}}})::
603 1 Adrian Georgescu
  Creates a new {{{Registration}}} object.
604
  [[BR]]''credentials'':[[BR]]
605 23 Ruud Klaver
  The SIP credentials needed to authenticate at the SIP proxy in the form of a {{{Credentials}}} object, including the SIP account for which we want to register.
606 1 Adrian Georgescu
  [[BR]]''route'':[[BR]]
607
  The outbound proxy to use in the form of a {{{Route}}} object
608
  [[BR]]''expires'':[[BR]]
609
  The amount of seconds to request the registration for, i.e. the value that should be put in the {{{Expires}}} header.
610 33 Ruud Klaver
  [[BR]]''contact_uri'':[[BR]]
611
  The Contact URI that should be used to register as a {{{SIPURI}}} object.
612
  If this is {{{None}}}, a Contact URI will be generated.
613 1 Adrian Georgescu
  [[BR]]''extra_headers'':[[BR]]
614
  A dictionary of extra headers that should be added to any outgoing request.
615
 '''register'''(''self'')::
616
  Whenever the object is ready to send a {{{REGISTER}}} for the specified SIP account it will do so, moving the state machine into the {{{registering}}} state.
617
  If the {{{REGISTER}}} succeeds the state machines moves into the {{{registered}}} state and the object will automatically refresh the registration before it expires (again moving into the {{{registering}}} state).
618
  If it is unsuccessful the state machine reverts to the {{{unregistered}}} state.
619
 '''unregister'''(''self'')::
620
  If the object is registered it will send a {{{REGISTER}}} with an {{{Expires}}} header of 0, effectively unregistering the contact from the SIP account.
621
622
==== notifications ====
623
624 29 Luci Stanescu
 '''SIPRegistrationChangedState'''::
625 1 Adrian Georgescu
  This notification will be sent every time the internal state machine of a {{{Registeration}}} object changes state.
626
  [[BR]]''timestamp'':[[BR]]
627
  A {{{datetime.datetime}}} object indicating when the notification was sent.
628
  [[BR]]''state'':[[BR]]
629
  The new state the state machine moved into.
630
  [[BR]]''code'': (only on SIP response triggered state change)[[BR]]
631
  The status code of the response that caused the state change.
632
  This may be internally generated by PJSIP, e.g. on timeout.
633
  [[BR]]''reason'': (only on SIP response triggered state change)[[BR]]
634
  The status text of the response that caused the state change.
635
  This may be internally generated by PJSIP, e.g. on timeout.
636
  [[BR]]''contact_uri'': (only on successful registration)[[BR]]
637
  The {{{Contact}}} URI used to register as a string.
638
  [[BR]]''expires'': (only on successful registration)[[BR]]
639
  How many seconds until this registration expires.
640
  [[BR]]''contact_uri_list'': (only on successful registration)[[BR]]
641
  The full list of {{{Contact}}} URIs registered for this SIP account, including the one just performed by this object.
642
643
==== example code ====
644
645
This code shows how to make a {{{Registration}}} object for a particular SIP account and have it register.
646
647
{{{
648
accnt = SIPURI(user="username", host="domain.com")
649
creds = Credentials(accnt, "password")
650 22 Ruud Klaver
reg = Registration(creds, Route("1.2.3.4"))
651 1 Adrian Georgescu
reg.register()
652
}}}
653
654 29 Luci Stanescu
After executing this code, the application will have to wait until the {{{Registration}}} object sends the {{{SIPRegistrationChangedState}}} notification, which includes the result of the requested operation.
655 1 Adrian Georgescu
656
=== Publication ===
657
658
Publication of SIP events is an Internet standard published at [http://tools.ietf.org/html/rfc3903 RFC 3903]. 
659
PUBLISH is similar to REGISTER in that it allows a user to create, modify, and remove state in another entity which manages this state on behalf of the user.
660
661
A {{{Publication}}} object represents publishing some content for a particular SIP account and a particular event type at the SIP presence agent through a {{{PUBLISH}}} request.
662
The state machine of this object is very similar to that of the {{{Registration}}} object, as can be seen in the following diagram:
663
664 2 Adrian Georgescu
[[Image(sipsimple-publication-state-machine.png, nolink)]]
665 1 Adrian Georgescu
666
State changes are triggered by the following events:
667
 1. The initial state.
668
 2. User requests in the form of the {{{publish()}}} and {{{unpublish()}}} methods.
669
 3. A final response for a {{{PUBLISH}}} is received from the network.
670
 4. The refresh timer expires.
671
Like the {{{Registration}}} state machine, the {{{Publication}}} state machine is queued.
672
This means that the application may call either the {{{publish()}}} or {{{unpublish()}}} method at any point in the state machine.
673
The object will perform the requested action when ready.
674
When some content is published and the application wants to update the contents, it can directly call the {{{publish()}}} method with the new content.
675
676
> The implementation of this object needs to be revised.
677
678
> If this object is re-used after unpublication, the etag value is not reset by PJSIP.
679
> This needs to be fixed.
680
681
==== attributes ====
682
683
 '''state'''::
684
  Indicates which state the internal state machine is in.
685
  This is one of {{{unpublished}}}, {{{publishing}}}, {{{published}}}, {{{unpublishing}}}.
686
 '''credentials'''::
687
  The SIP credentials needed to authenticate at the SIP proxy in the form of a {{{Credentials}}} object, including the SIP account for which we want to register.
688
  This attribute is set on object instantiation and is read-only.
689
 '''route'''::
690
  The outbound proxy to use in the form of a {{{Route}}} object.
691
  This attribute is set on object instantiation and is read-only.
692
 '''extra_headers'''::
693
  A dictionary of extra headers that should be added to any outgoing {{{PUBLISH}}} request.
694
  This attribute is set on object instantiation and is read-only.
695
 '''expires'''::
696
  The amount of seconds the contents of the {{{PUBLISH}}} are valid, i.e. the value that should be put in the {{{Expires}}} header.
697
  This attribute is set on object instantiation and can be modified at runtime.
698
  A new value will be used during the next refreshing {{{PUBLISH}}}.
699
700
==== methods ====
701
702 23 Ruud Klaver
 '''!__init!__'''(''self'', '''credentials''', '''event''', '''route''', '''expires'''=300, '''extra_headers'''={{{None}}})::
703 1 Adrian Georgescu
  Creates a new {{{Publication}}} object.
704
  [[BR]]''credentials'':[[BR]]
705
  The SIP credentials needed to authenticate at the SIP proxy in the form of a {{{Credentials}}} object, including the SIP account that we want to publish the content for.
706
  [[BR]]''event'':[[BR]]
707
  The event package for which we want to publish content.
708
  [[BR]]''route'':[[BR]]
709
  The outbound proxy to use in the form of a {{{Route}}} object
710
  [[BR]]''expires'':[[BR]]
711
  The amount of seconds the contents of the {{{PUBLISH}}} are valid, i.e. the value that should be put in the {{{Expires}}} header.
712
  [[BR]]''extra_headers'':[[BR]]
713
  A dictionary of extra headers that should be added to any outgoing {{{PUBLISH}}} request.
714
 '''publish'''(''self'', '''content_type''', '''content_subtype''', '''body''')::
715
  Whenever the object is ready to send a {{{PUBLISH}}} for the specified SIP account it will do so, moving the state machine into the {{{publishing}}} state.
716
  If the {{{PUBLISH}}} succeeds the state machines moves into the {{{published}}} state and the object will automatically refresh the publication before it expires (again moving into the {{{publishing}}} state).
717
  If it is unsuccessful the state machine reverts to the {{{unregistered}}} state.
718
  [[BR]]''content_type'':[[BR]]
719
  The first part of the {{{Content-Type:}}} header of the {{{PUBLISH}}} request that is to be sent (before the {{{/}}}), indicating the type of content of the body.
720
  [[BR]]''content_subtype'':[[BR]]
721
  The second part of the {{{Content-Type:}}} header of the {{{PUBLISH}}} request that is to be sent (after the {{{/}}}), indicating the type of content of the body.
722
 '''unpublish'''(''self'')::
723
  If the object has some content published, it will send a {{{PUBLISH}}} with an {{{Expires}}} header of 0, effectively unpublishing the content for the specified SIP account.
724
725
==== notifications ====
726
727 29 Luci Stanescu
 '''SIPPublicationChangedState'''::
728 1 Adrian Georgescu
  This notification will be sent every time the internal state machine of a {{{Publication}}} object changes state.
729
  [[BR]]''timestamp'':[[BR]]
730
  A {{{datetime.datetime}}} object indicating when the notification was sent.
731
  [[BR]]''state'':[[BR]]
732
  The new state the state machine moved into.
733
  [[BR]]''code'': (only on SIP response triggered state change)[[BR]]
734
  The status code of the response that caused the state change.
735
  This may be internally generated by PJSIP, e.g. on timeout.
736
  [[BR]]''reason'': (only on SIP response triggered state change)[[BR]]
737
  The status text of the response that caused the state change.
738
  This may be internally generated by PJSIP, e.g. on timeout.
739
740
> On init the event package is not checked with known event packages, this is only used for {{{Subscription}}} objects.
741
> This could be done for the sake of consistency.
742
743
==== example code ====
744
745
This code shows how to make a {{{Publication}}} object for a particular SIP account and have it attempt to publish its presence.
746
747
{{{
748
accnt = SIPURI(user="username", host="domain.com")
749
creds = Credentials(accnt, "password")
750 22 Ruud Klaver
pub = Publication(creds, "presence", Route("1.2.3.4"))
751 1 Adrian Georgescu
pub.publish("text", "plain", "hi!")
752
}}}
753
754 29 Luci Stanescu
After executing this code, the application will have to wait until the {{{Publication}}} object sends the {{{SIPPublicationChangedState}}} notification, which includes the result of the requested operation.
755 1 Adrian Georgescu
In this case the presence agent will most likely reply with "Unsupported media type", as the code tries to submit Content-Type which is not valid for the presence event package.
756
757
=== Subscription ===
758
759
Subscription and notifications for SIP events is an Internet standard published at [http://tools.ietf.org/html/rfc3856 RFC 3856].
760
761
This SIP primitive class represents a subscription to a specific event type of a particular SIP URI.
762
This means that the application should instance this class for each combination of event and SIP URI that it wishes to subscribe to.
763
The event type and the content types that are acceptable for it need to be registered first, either through the {{{init_options}}} attribute of {{{Engine}}} (before starting it), or by calling the {{{add_event()}}} method of the {{{Engine}}} instance.
764
Whenever a {{{NOTIFY}}} is received, the application will receive the {{{Subcription_notify}}} event.
765
766
Internally a {{{Subscription}}} object has a state machine, which reflects the state of the subscription.
767
It is a direct mirror of the state machine of the underlying {{{pjsip_evsub}}} object, whose possible states are at least {{{NULL}}}, {{{SENT}}}, {{{ACCEPTED}}}, {{{PENDING}}}, {{{ACTIVE}}} or {{{TERMINATED}}}.
768
The last three states are directly copied from the contents of the {{{Subscription-State}}} header of the received {{{NOTIFY}}} request.
769
Also, the state can be an arbitrary string if the contents of the {{{Subscription-State}}} header are not one of the three above.
770
The state machine of the {{{Subscription}}} object is not queued, meaning that if an action is performed that is not allowed in the state the state machine is currently in, an exception will be raised.
771
772
> The implementation of this object needs to be revised.
773
774
==== attributes ====
775
776
 '''state'''::
777
  Indicates which state the internal state machine is in.
778
  See the previous section for a list of states the state machine can be in.
779
 '''credentials'''::
780
  The SIP credentials needed to authenticate at the SIP proxy in the form of a {{{Credentials}}} object.
781
  The {{{SIPURI}}} object included in the {{{Credentials}}} object is used for the {{{From}}} header of the {{{SUBSCRIBE}}} request.
782
  This attribute is set on object instantiation and is read-only.
783
 '''to_uri'''::
784
  The SIP URI we want to subscribe to a particular event of represented as a {{{SIPURI}}} object.
785
  This attribute is set on object instantiation and is read-only.
786
 '''event'''::
787
  The event package to which we want to subscribe at the given SIP URI.
788
  This attribute is set on object instantiation and is read-only.
789
 '''route'''::
790
  The outbound proxy to use in the form of a {{{Route}}} object.
791
  This attribute is set on object instantiation and is read-only.
792
 '''expires'''::
793
  The expires value that was requested on object instantiation.
794
  This attribute is read-only.
795
 '''extra_headers'''::
796
  A dictionary of extra headers that should be added to any outgoing {{{SUBSCRIBE}}} request.
797
  This attribute is set on object instantiation and is read-only.
798
799
==== methods ====
800
801 33 Ruud Klaver
 '''!__init!__'''(''self'', '''credentials''', '''to_uri''', '''event''', '''route''', '''expires'''=300, '''contact_uri'''={{{None}}}, '''extra_headers'''={{{None}}})::
802 1 Adrian Georgescu
  Creates a new {{{Subscription}}} object.
803
  [[BR]]''credentials'':[[BR]]
804
  The SIP credentials needed to authenticate at the SIP proxy in the form of a {{{Credentials}}} object.
805
  The {{{SIPURI}}} object included in the {{{Credentials}}} object is used for the {{{From}}} header of the {{{SUBSCRIBE}}} request.
806
  [[BR]]''to_uri'':[[BR]]
807
  The SIP URI we want to subscribe to a particular event of represented as a {{{SIPURI}}} object.
808
  [[BR]]''event'':[[BR]]
809
  The event package to which we want to subscribe at the given SIP URI.
810
  [[BR]]''route'':[[BR]]
811
  The outbound proxy to use in the form of a {{{Route}}} object
812
  [[BR]]''expires'':[[BR]]
813
  The amount of seconds  the {{{SUBSCRIBE}}} is valid, i.e. the value that should be put in the {{{Expires}}} header.
814 33 Ruud Klaver
  [[BR]]''contact_uri'':[[BR]]
815
  The Contact URI that should be used in the {{{SUBSCRIBE}}} requests.
816
  If this is {{{None}}}, a Contact URI will be generated internally.
817 1 Adrian Georgescu
  [[BR]]''extra_headers'':[[BR]]
818
  A dictionary of extra headers that should be added to any outgoing {{{SUBSCRIBE}}} request.
819
 '''subscribe'''(''self'')::
820
  This method activates the subscription and causes the object to send a {{{SUBSCRIBE}}} request to the relevant presence agent.
821
  It can only be used when the object is in the {{{TERMINATED}}} state.
822
 '''unsubscribe'''(''self'')::
823
  This method causes the object to send a {{{SUBSCRIBE}}} request with an {{{Expires}}} value of 0, effectively canceling the active subscription.
824
  It can be used when the object is not in the {{{TERMINATED}}} state.
825
826
==== notifications ====
827
828 29 Luci Stanescu
 '''SIPSubscriptionChangedState'''::
829 1 Adrian Georgescu
  This notification will be sent every time the internal state machine of a {{{Subscription}}} object changes state.
830
  [[BR]]''timestamp'':[[BR]]
831
  A {{{datetime.datetime}}} object indicating when the notification was sent.
832
  [[BR]]''state'':[[BR]]
833
  The new state the state machine moved into.
834
  [[BR]]''code'': (only on SIP response triggered state change)[[BR]]
835
  The status code of the response that caused the state change.
836
  This may be internally generated by PJSIP, e.g. on timeout.
837
  [[BR]]''reason'': (only on SIP response triggered state change)[[BR]]
838
  The status text of the response that caused the state change.
839
  This may be internally generated by PJSIP, e.g. on timeout.
840 29 Luci Stanescu
 '''SIPSubscriptionGotNotify'''::
841 1 Adrian Georgescu
  This notification will be sent when a {{{NOTIFY}}} is received that corresponds to a particular {{{Subscription}}} object.
842
  Note that this notification will not be sent when a {{{NOTIFY}}} with an empty body is received.
843
  [[BR]]''timestamp'':[[BR]]
844
  A {{{datetime.datetime}}} object indicating when the notification was sent.
845
  [[BR]]''content_type'':[[BR]]
846
  The first part of the {{{Content-Type:}}} header of the received {{{NOTIFY}}} request (before the {{{/}}}), indicating the type of the body.
847
  [[BR]]''content_subtype'':[[BR]]
848
  The second part of the {{{Content-Type:}}} header of the received {{{NOTIFY}}} request (after the {{{/}}}), indicating the type of the body.
849
  [[BR]]''body'':[[BR]]
850
  The body of the {{{NOTIFY}}} request.
851
852
==== example code ====
853
854
This code shows how to make a {{{Subscription}}} object that subscribes to the presence of another SIP account.
855
856
{{{
857
accnt = SIPURI(user="watcher", host="domain.com")
858
creds = Credentials(accnt, "password")
859
to_uri = SIPURI(user="watched", host="domain.com")
860 22 Ruud Klaver
sub = Subscription(creds, to_uri, "presence", Route("1.2.3.4"))
861 1 Adrian Georgescu
sub.subscribe()
862
}}}
863
864 29 Luci Stanescu
After executing this code, the application will have to wait until the {{{Subscription}}} object sends the {{{SIPSubscriptionChangedState}}} notification, which includes the result of the requested operation.
865
Independently of this, the object sends a {{{SIPSubscriptionGotNotify}}} notification anytime it receives a {{{NOTIFY}}} request for this subscription, as long as the subscription is active.
866 1 Adrian Georgescu
867
=== Invitation ===
868
869
The {{{Invitation}}} class represents an {{{INVITE}}} session, which governs a complete session of media exchange between two SIP endpoints from start to finish.
870
It is implemented to be agnostic to the media stream or streams negotiated, which is achieved by using the {{{SDPSession}}} class and its companion classes, which directly represents the parsed SDP.
871
The {{{Invitation}}} class represents both incoming and outgoing sessions.
872
873
The state machine contained in each {{{Invitation}}} object is based on the one used by the underlying PJSIP [http://www.pjsip.org/pjsip/docs/html/group__PJSIP__INV.htm pjsip_inv_session] object.
874
In order to represent re-{{{INVITE}}}s and user-requested disconnections, three more states have been added to this state machine.
875
The progression through this state machine is fairly linear and is dependent on whether this is an incoming or an outgoing session.
876
State changes are triggered either by incoming or by outgoing SIP requests and responses.
877
The states and the transitions between them are shown in the following diagram:
878
879
[[Image(sipsimple-core-invite-state-machine.png, nolink)]]
880
881
The state changes of this machine are triggered by the following:
882
 1. An {{{Invitation}}} object is newly created, either by the application for an outgoing session, or by the core for an incoming session.
883
 2. The application requested an outgoing session by calling the {{{send_invite()}}} method and and initial {{{INVITE}}} request is sent.
884
 3. A new incoming session is received by the core.
885
    The application should look out for state change to this state in order to be notified of new incoming sessions.
886
 4. A provisional response (1xx) is received from the remove party.
887
 5. A provisional response (1xx) is sent to the remote party, after the application called the {{{respond_to_invite_provisionally()}}} method.
888
 6. A positive final response (2xx) is received from the remote party.
889
 7. A positive final response (2xx) is sent to the remote party, after the application called the {{{accept_invite()}}} method.
890
 8. A positive final response (2xx) is sent or received, depending on the orientation of the session.
891
 9. An {{{ACK}}} is sent or received, depending on the orientation of the session.
892
    If the {{{ACK}}} is sent from the local to the remote party, it is initiated by PJSIP, not by a call from the application.
893
 10. The local party sent a re-{{{INVITE}}} to the remote party by calling the {{{send_reinvite()}}} method.
894
 11. The remote party has sent a final response to the re-{{{INVITE}}}.
895
 12. The remote party has sent a re-{{{INVITE}}}.
896
 13. The local party has responded to the re-{{{INVITE}}} by calling the {{{respond_to_reinvite()}}} method.
897
 14. The application requests that the session ends by calling the {{{disconnect()}}} method.
898
 15. A response is received from the remote party to whichever message was sent by the local party to end the session.
899
 16. A message is received from the remote party which ends the session.
900
901 29 Luci Stanescu
The application is notified of a state change in either state machine through the {{{SIPInvitationChangedState}}} notification, which has as data the current and previous states.
902 1 Adrian Georgescu
If the event is triggered by and incoming message, extensive data about that message, such as method/code, headers and body, is also included with the notification.
903
The application should compare the previous and current states and perform the appropriate action.
904
905 29 Luci Stanescu
An {{{Invitiation}}} object also emits the {{{SIPInvitationGotSDPUpdate}}} notification, which indicates that SDP negotiation between the two parties has been completed.
906 1 Adrian Georgescu
This will occur (at least) once during the initial session negotiation steps, during re-{{{INVITE}}}s in both directions and whenever an {{{UPDATE}}} request is received.
907
In the last case, the {{{Invitation}}} object will automatically include the current local SDP in the response.
908
909
==== attributes ====
910
911
 '''state'''::
912
  The state the {{{Invitation}}} state machine is currently in.
913
  See the diagram above for possible states.
914
  This attribute is read-only.
915
 '''is_outgoing'''::
916
  Boolean indicating if the original {{{INVITE}}} was outgoing, or incoming if set to {{{False}}}.
917
 '''credentials'''::
918
  The SIP credentials needed to authenticate at the SIP proxy in the form of a {{{Credentials}}} object.
919
  If this {{{Invitation}}} object represents an incoming {{{INVITE}}} session this attribute will be {{{None}}}.
920
  This attribute is set on object instantiation and is read-only.
921
 '''caller_uri'''::
922
  The SIP URI of the caller represented by a {{{SIPURI}}} object.
923
  If this is in an outgoing {{{INVITE}}} session, the caller_uri is taken from the supplied {{{Credentials}}} object.
924
  Otherwise the URI is taken from the {{{From:}}} header of the initial {{{INVITE}}}.
925
  This attribute is set on object instantiation and is read-only.
926
 '''callee_uri'''::
927
  The SIP URI of the callee represented by a {{{SIPURI}}} object.
928
  If this is an outgoing {{{INVITE}}} session, this is the callee_uri from the !__init!__ method.
929
  Otherwise the URI is taken from the {{{To:}}} header of the initial {{{INVITE}}}.
930
  This attribute is set on object instantiation and is read-only.
931
 '''local_uri'''::
932
  The local SIP URI used in this session.
933
  If the original {{{INVITE}}} was incoming, this is the same as {{{callee_uri}}}, otherwise it will be the same as {{{caller_uri}}}.
934
 '''remote_uri'''::
935
  The SIP URI of the remote party in this session.
936
  If the original {{{INVITE}}} was incoming, this is the same as {{{caller_uri}}}, otherwise it will be the same as {{{callee_uri}}}.
937
 '''route'''::
938
  The outbound proxy that was requested to be used in the form of a {{{Route}}} object, including the desired transport.
939
  If this {{{Invitation}}} object represents an incoming {{{INVITE}}} session this attribute will always be {{{None}}}.
940
  This attribute is set on object instantiation and is read-only.
941
 '''call_id'''::
942
  The call ID of the {{{INVITE}}} session as a read-only string.
943
  In the {{{NULL}}} and {{{DISCONNECTED}}} states, this attribute is {{{None}}}.
944 33 Ruud Klaver
 '''transport'''::
945
  A string indicating the transport used for the application.
946
  This can be "udp", "tcp" or "tls".
947
 '''local_contact_uri'''::
948
  The Contact URI that the local side provided to the remote side within this {{{INVITE}}} session as a {{{SIPURI}}} object.
949 1 Adrian Georgescu
950
==== methods ====
951
952 33 Ruud Klaver
 '''!__init!__'''(''self'', '''credentials''', '''callee_uri''', '''route''', '''contact_uri'''={{{None}}})::
953 1 Adrian Georgescu
  Creates a new {{{Invitation}}} object for an outgoing session.
954
  [[BR]]''credentials'':[[BR]]
955 22 Ruud Klaver
  The SIP credentials needed to authenticate at the SIP proxy in the form of a {{{Credentials}}} object.
956 1 Adrian Georgescu
  The {{{SIPURI}}} object included in the {{{Credentials}}} object is used for the {{{From}}} header of the {{{INVITE}}} request.
957
  [[BR]]''callee_uri'':[[BR]]
958
  The SIP URI we want to send the {{{INVITE}}} to, represented as a {{{SIPURI}}} object.
959
  [[BR]]''route'':[[BR]]
960
  The outbound proxy to use in the form of a {{{Route}}} object.
961
  This includes the desired transport to use.
962 33 Ruud Klaver
  [[BR]]''contact_uri'':[[BR]]
963
  The Contact URI to include in the {{{INVITE}}} request, a {{{SIPURI}}} object.
964
  If this is {{{None}}}, a Contact URI will be internally generated.
965 1 Adrian Georgescu
 '''get_active_local_sdp'''(''self'')::
966
  Returns a new {{{SDPSession}}} object representing the currently active local SDP.
967
  If no SDP was negotiated yet, this returns {{{None}}}.
968
 '''get_active_remote_sdp'''(''self'')::
969
  Returns a new {{{SDPSession}}} object representing the currently active local SDP.
970
  If no SDP was negotiated yet, this returns {{{None}}}.
971
 '''get_offered_local_sdp'''(''self'')::
972
  Returns a new {{{SDPSession}}} object representing the currently proposed local SDP.
973
  If no local offered SDP has been set, this returns {{{None}}}.
974
 '''set_offered_local_sdp'''(''self'', '''local_sdp''')::
975
  Sets the offered local SDP, either for an initial {{{INVITE}}} or re-{{{INVITE}}}, or as an SDP answer in response to an initial {{{INVITE}}} or re-{{{INVITE}}}.
976
  [[BR]]''local_sdp'':[[BR]]
977
  The SDP to send as offer or answer to the remote party.
978
 '''get_offered_remote_sdp'''(''self'')::
979
  Returns a new {{{SDPSession}}} object representing the currently proposed remote SDP.
980
  If no remote SDP has been offered in the current state, this returns {{{None}}}.
981
 '''send_invite'''(''self'', '''extra_headers'''={{{None}}})::
982
  This tries to move the state machine into the {{{CALLING}}} state by sending the initial {{{INVITE}}} request.
983
  It may only be called from the {{{NULL}}} state on an outgoing {{{Invitation}}} object.
984
  [[BR]]''extra_headers'':[[BR]]
985
  Any extra headers that should be included in the {{{INVITE}}} request in the form of a dict.
986
 '''respond_to_invite_provisionally'''(''self'', '''response_code'''=180, '''extra_headers'''={{{None}}})::
987
  This tries to move the state machine into the {{{EARLY}}} state by sending a provisional response to the initial {{{INVITE}}}.
988
  It may only be called from the {{{INCOMING}}} state on an incoming {{{Invitation}}} object.
989
  [[BR]]''response_code'':[[BR]]
990
  The code of the provisional response to use as an int.
991
  This should be in the 1xx range.
992
  [[BR]]''extra_headers'':[[BR]]
993
  Any extra headers that should be included in the response in the form of a dict.
994
 '''accept_invite'''(''self'', '''extra_headers'''={{{None}}})::
995
  This tries to move the state machine into the {{{CONNECTING}}} state by sending a 200 OK response to the initial {{{INVITE}}}.
996
  It may only be called from the {{{INCOMING}}} or {{{EARLY}}} states on an incoming {{{Invitation}}} object.
997
  [[BR]]''extra_headers'':[[BR]]
998
  Any extra headers that should be included in the response in the form of a dict.
999 37 Ruud Klaver
 '''disconnect'''(''self'', '''response_code'''=603, '''extra_headers'''={{{None}}})::
1000 1 Adrian Georgescu
  This moves the {{{INVITE}}} state machine into the {{{DISCONNECTING}}} state by sending the necessary SIP message.
1001
  When a response from the remote party is received, the state machine will go into the {{{DISCONNECTED}}} state.
1002
  Depending on the current state, this could be a CANCEL or BYE request or a negative response.
1003
  [[BR]]''response_code'':[[BR]]
1004
  The code of the response to use as an int, if transmission of a response is needed.
1005
  [[BR]]''extra_headers'':[[BR]]
1006
  Any extra headers that should be included in the request or response in the form of a dict.
1007 33 Ruud Klaver
 '''respond_to_reinvite'''(''self'', '''response_code'''=200, '''extra_headers'''={{{None}}})::
1008 1 Adrian Georgescu
  Respond to a received re-{{{INVITE}}} with a response that is either provisional (1xx), positive (2xx) or negative (3xx and upwards).
1009
  This method can be called by the application when the state machine is in the {{{REINVITED}}} state and will move the state machine back into the {{{CONFIRMED}}} state.
1010
  Before giving a positive final response, the SDP needs to be set using the {{{set_offered_local_sdp()}}} method.
1011
  [[BR]]''response_code'':[[BR]]
1012
  The code of the response to use as an int.
1013
  This should be a 3 digit number.
1014
  [[BR]]''extra_headers'':[[BR]]
1015
 '''send_reinvite'''(''self'', '''extra_headers'''={{{None}}})::
1016
  The application may only call this method when the state machine is in the {{{CONFIRMED}}} state to induce sending a re-{{{INVITE}}}.
1017
  Before doing this it needs to set the new local SDP offer by calling the {{{set_offered_local_sdp()}}} method.
1018
  After this method is called, the state machine will be in the {{{REINVITING}}} state, until a final response from the remote party is received.
1019
  [[BR]]''extra_headers'':[[BR]]
1020
  Any extra headers that should be included in the re-{{{INVITE}}} in the form of a dict.
1021
1022
==== notifications ====
1023
1024 29 Luci Stanescu
 '''SIPInvitationChangedState'''::
1025 1 Adrian Georgescu
  This notification is sent by an {{{Invitation}}} object whenever its state machine changes state.
1026
  [[BR]]''timestamp'':[[BR]]
1027
  A {{{datetime.datetime}}} object indicating when the notification was sent.
1028
  [[BR]]''prev_state'':[[BR]]
1029
  The previous state of the INVITE state machine.
1030
  [[BR]]''state'':[[BR]]
1031
  The new state of the INVITE state machine, which may be the same as the previous state.
1032
  [[BR]]''method'': (only if the state change got triggered by an incoming SIP request)[[BR]]
1033
  The method of the SIP request as a string.
1034
  [[BR]]''request_uri'': (only if the state change got triggered by an incoming SIP request)[[BR]]
1035
  The request URI of the SIP request as a {{{SIPURI}}} object.
1036
  [[BR]]''code'': (only if the state change got triggered by an incoming SIP response or internal timeout or error)[[BR]]
1037
  The code of the SIP response or error as an int.
1038
  [[BR]]''reason'': (only if the state change got triggered by an incoming SIP response or internal timeout or error)[[BR]]
1039
  The reason text of the SIP response or error as an int.
1040
  [[BR]]''headers'': (only if the state change got triggered by an incoming SIP request or response)[[BR]]
1041
  The headers of the SIP request or response as a dict.
1042
  Each SIP header is represented in its parsed for as long as PJSIP supports it.
1043
  The format of the parsed value depends on the header.
1044
  [[BR]]''body'': (only if the state change got triggered by an incoming SIP request or response)[[BR]]
1045
  The body of the SIP request or response as a string, or {{{None}}} if no body was included.
1046
  The content type of the body can be learned from the {{{Content-Type:}}} header in the headers argument.
1047 29 Luci Stanescu
 '''SIPInvitationGotSDPUpdate'''::
1048 1 Adrian Georgescu
  This notification is sent by an {{{Invitation}}} object whenever SDP negotation has been perfomed.
1049
  It should be used by the application as an indication to start, change or stop any associated media streams.
1050
  [[BR]]''timestamp'':[[BR]]
1051
  A {{{datetime.datetime}}} object indicating when the notification was sent.
1052
  [[BR]]''succeeded'':[[BR]]
1053
  A boolean indicating if the SDP negotation has succeeded.
1054
  [[BR]]''error'': (only if SDP negotation did not succeed)[[BR]]
1055
  A string indicating why SDP negotation failed.
1056
  [[BR]]''local_sdp'': (only if SDP negotation succeeded)[[BR]]
1057
  A SDPSession object indicating the local SDP that was negotiated.
1058
  [[BR]]''remote_sdp'': (only if SDP negotation succeeded)[[BR]]
1059
  A SDPSession object indicating the remote SDP that was negotiated.
1060
1061
=== SDPSession ===
1062
1063
SDP stands for Session Description Protocol. Session Description Protocol (SDP) is a format for describing streaming media initialization parameters in an ASCII string. SDP is intended for describing multimedia communication sessions for the purposes of session announcement, session invitation, and other forms of multimedia session initiation. It is an IETF standard described by [http://tools.ietf.org/html/rfc4566 RFC 4566]. [http://tools.ietf.org/html/rfc3264 RFC 3264] defines an Offer/Answer Model with the Session Description Protocol (SDP), a mechanism by which two entities can make use of the Session Description Protocol (SDP) to arrive at a common view of a multimedia session between them. 
1064
1065
SDPSession object directly represents the contents of a SDP body, as carried e.g. in a INVITE request, and is a simple wrapper for the PJSIP [http://www.pjsip.org/pjmedia/docs/html/structpjmedia__sdp__session.htm pjmedia_sdp_session] structure.
1066
It can be passed to those methods of an {{{Invitation}}} object that result in transmission of a message that includes SDP, or is passed to the application through a notification that is triggered by reception of a message that includes SDP.
1067
A {{{SDPSession}}} object may contain {{{SDPMedia}}}, {{{SDPConnection}}} and {{{SDPAttribute}}} objects.
1068
It supports comparison to other {{{SDPSession}}} objects using the == and != expressions.
1069
As all the attributes of the {{{SDPSession}}} class are set by attributes of the {{{__init__}}} method, they will be documented along with that method.
1070
1071
==== methods ====
1072
1073 23 Ruud Klaver
 '''!__init!__'''(''self'', '''address''', '''id'''={{{None}}}, '''version'''={{{None}}}, '''user='''"-", net_type="IN", '''address_type'''="IP4", '''name'''=" ", '''info'''={{{None}}}, '''connection'''={{{None}}}, '''start_time'''=0, '''stop_time'''=0, '''attributes'''={{{None}}}, '''media'''={{{None}}})::
1074 1 Adrian Georgescu
  Creates the SDPSession object with the specified parameters as attributes.
1075
  Each of these attributes can be accessed and changed on the object once instanced.
1076
  [[BR]]''address'':[[BR]]
1077
  The address that is contained in the "o" (origin) line of the SDP as a string.
1078
  [[BR]]''id'':[[BR]]
1079
  The session identifier contained in the "o" (origin) line of the SDP as an int.
1080
  If this is set to {{{None}}} on init, a session identifier will be generated.
1081
  [[BR]]''version'':[[BR]]
1082
  The version identifier contained in the "o" (origin) line of the SDP as an int.
1083
  If this is set to {{{None}}} on init, a version identifier will be generated.
1084
  [[BR]]''user'':[[BR]]
1085
  The user name contained in the "o" (origin) line of the SDP as a string.
1086
  [[BR]]''net_type'':[[BR]]
1087
  The network type contained in the "o" (origin) line of the SDP as a string.
1088
  [[BR]]''address_type'':[[BR]]
1089
  The address type contained in the "o" (origin) line of the SDP as a string.
1090
  [[BR]]''name'':[[BR]]
1091
  The contents of the "s" (session name) line of the SDP as a string.
1092
  [[BR]]''info'':[[BR]]
1093
  The contents of the session level "i" (information) line of the SDP as a string.
1094
  If this is {{{None}}} or an empty string, the SDP has no "i" line.
1095
  [[BR]]''connection'':[[BR]]
1096
  The contents of the "c" (connection) line of the SDP as a {{{SDPConnection}}} object.
1097
  If this is set to {{{None}}}, the SDP has no session level "c" line.
1098
  [[BR]]''start_time'':[[BR]]
1099
  The first value of the "t" (time) line of the SDP as an int.
1100
  [[BR]]''stop_time'':[[BR]]
1101
  The second value of the "t" (time) line of the SDP as an int.
1102
  [[BR]]''attributes'':[[BR]]
1103
  The session level "a" lines (attributes) in the SDP represented by a list of {{{SDPAttribute}}} objects.
1104
  [[BR]]''media'':[[BR]]
1105
  The media sections of the SDP represented by a list of {{{SDPMedia}}} objects.
1106
1107
=== SDPMedia ===
1108
1109
This object represents the contents of a media section of a SDP body, i.e. a "m" line and everything under it until the next "m" line.
1110
It is a simple wrapper for the PJSIP [http://www.pjsip.org/pjmedia/docs/html/structpjmedia__sdp__media.htm pjmedia_sdp_media] structure.
1111
One or more {{{SDPMedia}}} objects are usually contained in a {{{SDPSession}}} object.
1112
It supports comparison to other {{{SDPMedia}}} objects using the == and != expressions.
1113
As all the attributes of this class are set by attributes of the {{{__init__}}} method, they will be documented along with that method.
1114
1115
==== methods ====
1116
1117 24 Ruud Klaver
 '''!__init!__'''(''self'', '''media''', '''port''', '''transport''', '''port_count'''=1, '''formats'''={{{None}}}, '''info'''={{{None}}}, '''connection'''={{{None}}}, '''attributes'''={{{None}}})::
1118 1 Adrian Georgescu
  Creates the SDPMedia object with the specified parameters as attributes.
1119
  Each of these attributes can be accessed and changed on the object once instanced.
1120
  [[BR]]''media'':[[BR]]
1121
  The media type contained in the "m" (media) line as a string.
1122
  [[BR]]''port'':[[BR]]
1123
  The transport port contained in the "m" (media) line as an int.
1124
  [[BR]]''transport'':[[BR]]
1125
  The transport protocol in the "m" (media) line as a string.
1126
  [[BR]]''port_count'':[[BR]]
1127
  The port count in the "m" (media) line as an int.
1128
  If this is set to 1, it is not included in the SDP.
1129
  [[BR]]''formats'':[[BR]]
1130
  The media formats in the "m" (media) line represented by a list of strings.
1131
  [[BR]]''info'':[[BR]]
1132
  The contents of the "i" (information) line of this media section as a string.
1133
  If this is {{{None}}} or an empty string, the media section has no "i" line.
1134
  [[BR]]''connection'':[[BR]]
1135
  The contents of the "c" (connection) line that is somewhere below the "m" line of this section as a {{{SDPConnection}}} object.
1136
  If this is set to {{{None}}}, this media section has no "c" line.
1137
  [[BR]]''attributes'':[[BR]]
1138
  The "a" lines (attributes) that are somewhere below the "m" line of this section represented by a list of {{{SDPAttribute}}} objects.
1139
 '''get_direction'''(''self'')::
1140
  This is a convenience methods that goes through all the attributes of the media section and returns the direction, which is either "sendrecv", "sendonly", "recvonly" or "inactive".
1141
  If none of these attributes is present, the default direction is "sendrecv".
1142
1143
=== SDPConnection ===
1144
1145
This object represents the contents of a "c" (connection) line of a SDP body, either at the session level or for an individual media stream.
1146
It is a simple wrapper for the PJSIP [http://www.pjsip.org/pjmedia/docs/html/structpjmedia__sdp__conn.htm pjmedia_sdp_conn] structure.
1147
A {{{SDPConnection}}} object can be contained in a {{{SDPSession}}} object or {{{SDPMedia}}} object.
1148
It supports comparison to other {{{SDPConnection}}} objects using the == and != expressions.
1149
As all the attributes of this class are set by attributes of the {{{__init__}}} method, they will be documented along with that method.
1150
1151
==== methods ====
1152
1153
 '''!__init!__'''(''self'', '''address''', '''net_type'''="IN", '''address_type'''="IP4")::
1154
  Creates the SDPConnection object with the specified parameters as attributes.
1155
  Each of these attributes can be accessed and changed on the object once instanced.
1156
  [[BR]]''address'':[[BR]]
1157
  The address part of the connection line as a string.
1158
  [[BR]]''net_type'':[[BR]]
1159
  The network type part of the connection line as a string.
1160
  [[BR]]''address_type'':[[BR]]
1161
  The address type part of the connection line as a string.
1162
1163
=== SDPAttribute ===
1164
1165
This object represents the contents of a "a" (attribute) line of a SDP body, either at the session level or for an individual media stream.
1166
It is a simple wrapper for the PJSIP [http://www.pjsip.org/pjmedia/docs/html/structpjmedia__sdp__attr.htm pjmedia_sdp_attr] structure.
1167
One or more {{{SDPAttribute}}} objects can be contained in a {{{SDPSession}}} object or {{{SDPMedia}}} object.
1168
It supports comparison to other {{{SDPAttribute}}} objects using the == and != expressions.
1169
As all the attributes of this class are set by attributes of the {{{__init__}}} method, they will be documented along with that method.
1170
1171
==== methods ====
1172
1173
 '''!__init!__'''(''self'', '''name''', '''value''')::
1174
  Creates the SDPAttribute object with the specified parameters as attributes.
1175
  Each of these attributes can be accessed and changed on the object once instanced.
1176
  [[BR]]''name'':[[BR]]
1177
  The name part of the attribute line as a string.
1178
  [[BR]]''value'':[[BR]]
1179
  The value part of the attribute line as a string.
1180
1181
=== RTPTransport ===
1182
1183
This object represents a transport for RTP media, the basis of which is a pair of UDP sockets, one for RTP and one for RTCP.
1184
Internally it wraps a [http://www.pjsip.org/pjmedia/docs/html/group__PJMEDIA__TRANSPORT.htm pjmedia_transport] object.
1185
Initially this object will only be used by the {{{AudioTransport}}} object, but in the future it can also be used for video and [wiki:RTTProposal Real-Time Text].
1186
For this reason the {{{AudioTransport}}} and {{{RTPTransport}}} are two distinct objects.
1187
1188
The {{{RTPTransport}}} object also allows support for ICE and SRTP functionality from PJSIP.
1189
Because these features are related to both the UDP transport and the SDP formatting, the SDP carried in SIP signaling message will need to "pass through" this object during the SDP negotiation.
1190
The code using this object, which in most cases will be the {{{AudioTransport}}} object, will need to call certain methods on the object at appropriate times.
1191
This process of SDP negotiation is represented by the internal state machine of the object, as shown in the following diagram:
1192
1193
> The Real-time Transport Protocol (or RTP) defines a standardized packet format for delivering audio and video over the Internet.
1194
> It was developed by the Audio-Video Transport Working Group of the IETF and published in [http://tools.ietf.org/html/rfc3550 RFC 3550].
1195
> RTP is used in streaming media systems (together with the RTSP) as well as in videoconferencing and push to talk systems.
1196
> For these it carries media streams controlled by Session Initiation Protocol (SIP) signaling protocols, making it the technical foundation of the Voice over IP industry.
1197
1198 18 Adrian Georgescu
[[Image(sipsimple-rtp-transport-state-machine.png, nolink)]]
1199 1 Adrian Georgescu
1200
State changes are triggered by the following events:
1201 17 Ruud Klaver
 1. The application calls the {{{set_INIT()}}} method after object creation and ICE+STUN is not used.
1202
 2. The application calls the {{{set_INIT()}}} method after object creation and ICE+STUN is used.
1203
 3. A successful STUN response is received from the STUN server.
1204
 4. The {{{set_LOCAL()}}} method is called.
1205
 5. The {{{set_ESTABLISHED()}}} method is called.
1206
 6. The {{{set_INIT()}}} method is called while the object is in the {{{LOCAL}}} or {{{ESTABLISHED}}} state.
1207
 7. A method is called on the application, but in the meantime the {{{Engine}}} has stopped.
1208
    The object can no longer be used.
1209
 8. There was an error in getting the STUN candidates from the STUN server.
1210 1 Adrian Georgescu
1211
> It would make sense to be able to use the object even if the STUN request fails (and have ICE not include a STUN candidate), but for some reason the pjmedia_transport is unusable once STUN negotation has failed.
1212
> This means that the RTPTransport object is also unusable once it has reached the STUN_FAILED state.
1213
> A workaround would be destroy the RTPTransport object and create a new one that uses ICE without STUN.
1214
1215
These states allow for two SDP negotiation scenarios to occur, represented by two paths that can be followed through the state machine.
1216
In this example we will assume that ICE with STUN is not used, as it is independent of the SDP negotiation procedure.
1217
 * The first scenario is where the local party generates the SDP offer.
1218
   For a stream that it wishes to include in this SDP offer, it instantiates a {{{RTPTransport}}} object.
1219 17 Ruud Klaver
   After instantiation the object is initialized by calling the {{{set_INIT()}}} method and the local RTP address and port can be fetched from it using the {{{local_rtp_address}}} and {{{local_rtp_port}}} respectively, which can be used to generate the local SDP in the form of a {{{SDPSession}}} object (note that it needs the full object, not just the relevant {{{SDPMedia}}} object).
1220 1 Adrian Georgescu
   This local SDP then needs to be passed to the {{{set_LOCAL()}}} method, which moves the state machine into the {{{LOCAL}}} state.
1221
   Depending on the options used for the {{{RTPTransport}}} instantiation (such as ICE and SRTP), this may change the {{{SDPSession}}} object.
1222
   This (possibly changed) {{{SDPSession}}} object then needs to be passed to the {{{Invitation}}} object.
1223
   After SDP negotiation is completed, the application needs to pass both the local and remote SDP, in the form of {{{SDPSession}}} objects, to the {{{RTPTransport}}} object using the {{{set_ESTABLISHED()}}} method, moving the state machine into the {{{ESTABLISHED}}} state.
1224
   This will not change either of the {{{SDPSession}}} objects.
1225
 * The second scenario is where the local party is offered a media stream in SDP and wants to accept it.
1226 17 Ruud Klaver
   In this case a {{{RTPTransport}}} is also instantiated and initialized using the {{{set_INIT()}}} method, and the application can generate the local SDP in response to the remote SDP, using the {{{local_rtp_address}}} and {{{local_rtp_port}}} attributes.
1227 1 Adrian Georgescu
   Directly after this it should pass the generated local SDP and received remote SDP, in the form of {{{SDPSession}}} objects, to the {{{set_ESTABLISHED()}}} method.
1228
   In this case the local SDP object may be changed, after which it can be passed to the {{{Invitation}}} object.
1229
1230
Whenever the {{{RTPTransport}}} object is in the {{{LOCAL}}} or {{{ESTABLISHED}}} states, it may be reset to the {{{INIT}}} state to facilitate re-use of the existing transport and its features.
1231
Before doing this however, the internal transport object must no longer be in use.
1232
1233
==== attributes ====
1234
1235
 '''state'''::
1236
  Indicates which state the internal state machine is in.
1237
  See the previous section for a list of states the state machine can be in.
1238
  This attribute is read-only.
1239
 '''local_rtp_address'''::
1240
  The local IPv4 or IPv6 address of the interface the {{{RTPTransport}}} object is listening on and the address that should be included in the SDP.
1241
  If no address was specified during object instantiation, PJSIP will take guess out of the IP addresses of all interfaces.
1242
  This attribute is read-only and will be {{{None}}} if PJSIP is not listening on the transport.
1243
 '''local_rtp_port'''::
1244
  The UDP port PJSIP is listening on for RTP traffic.
1245
  RTCP traffic will always be this port plus one.
1246
  This attribute is read-only and will be {{{None}}} if PJSIP is not listening on the transport.
1247
 '''remote_rtp_address_sdp'''::
1248
  The remote IP address that was seen in the SDP.
1249
  This attribute is read-only and will be {{{None}}} unless the object is in the {{{ESTABLISHED}}} state.
1250
 '''remote_rtp_port_sdp'''::
1251
  The remote UDP port for RTP that was seen in the SDP.
1252
  This attribute is read-only and will be {{{None}}} unless the object is in the {{{ESTABLISHED}}} state.
1253
 '''remote_rtp_address_received'''::
1254
  The remote IP address from which RTP data was received.
1255
  This attribute is read-only and will be {{{None}}} unless RTP was actually received.
1256
 '''remote_rtp_port_received'''::
1257
  The remote UDP port from which RTP data was received.
1258
  This attribute is read-only and will be {{{None}}} unless RTP was actually received.
1259
 '''use_srtp'''::
1260
  A boolean indicating if the use of SRTP was requested when the object was instantiated.
1261
  This attribute is read-only.
1262
 '''force_srtp'''::
1263
  A boolean indicating if SRTP being mandatory for this transport if it is enabled was requested when the object was instantiated.
1264
  This attribute is read-only.
1265
 '''srtp_active'''::
1266
  A boolean indicating if SRTP encryption and decryption is running.
1267
  Querying this attribute only makes sense once the object is in the {{{ESTABLISHED}}} state and use of SRTP was requested.
1268
  This attribute is read-only.
1269
 '''use_ice'''::
1270
  A boolean indicating if the use of ICE was requested when the object was instantiated.
1271
  This attribute is read-only.
1272
 '''ice_stun_address'''::
1273 19 Ruud Klaver
  A string indicating the IP address of the STUN server that was requested to be used.
1274 1 Adrian Georgescu
  This attribute is read-only.
1275
 '''ice_stun_port'''::
1276
  A string indicating the UDP port of the STUN server that was requested to be used.
1277
  This attribute is read-only.
1278
1279
==== methods ====
1280
1281
 '''!__init!__'''(''self'', '''local_rtp_address'''={{{None}}}, '''use_srtp'''={{{False}}}, '''srtp_forced'''={{{False}}}, '''use_ice'''={{{False}}}, '''ice_stun_address'''={{{None}}}, '''ice_stun_port'''=3478)::
1282
  Creates a new {{{RTPTransport}}} object and opens the RTP and RTCP UDP sockets.
1283
  If aditional features are requested, they will be initialized.
1284
  After object instantiation, it is either in the {{{INIT}}} or the {{{WAIT_STUN}}} state, depending on the values of the {{{use_ice}}} and {{{ice_stun_address}}} arguments.
1285
  [[BR]]''local_rtp_address'':[[BR]]
1286
  Optionally contains the local IP address to listen on, either in IPv4 or IPv6 form.
1287
  If this is not specified, PJSIP will listen on all network interfaces.
1288
  [[BR]]''use_srtp'':[[BR]]
1289
  A boolean indicating if SRTP should be used.
1290
  If this is set to {{{True}}}, SRTP information will be added to the SDP when it passes this object.
1291
  [[BR]]''srtp_forced'':[[BR]]
1292
  A boolean indicating if use of SRTP is set to mandatory in the SDP.
1293
  If this is set to {{{True}}} and the remote party does not support SRTP, the SDP negotation for this stream will fail.
1294
  This argument is relevant only if {{{use_srtp}}} is set to {{{True}}}.
1295
  [[BR]]''use_ice'':[[BR]]
1296
  A boolean indicating if ICE should be used.
1297
  If this is set to {{{True}}}, ICE candidates will be added to the SDP when it passes this object.
1298
  [[BR]]''ice_stun_address'':[[BR]]
1299
  A string indicating the address (IP address or hostname) of the STUN server that should be used to add a STUN candidate to the ICE candidates.
1300
  If this is set to {{{None}}} no STUN candidate will be added, otherwise the object will be put into the {{{WAIT_STUN}}} state until a reply, either positive or negative, is received from the specified STUN server.
1301 29 Luci Stanescu
  When this happens a {{{RTPTransportGotSTUNResponse}}} notification is sent.
1302 1 Adrian Georgescu
  This argument is relevant only if {{{use_ice}}} is set to {{{True}}}.
1303
  [[BR]]''ice_stun_address'':[[BR]]
1304
  An int indicating the UDP port of the STUN server that should be used to add a STUN candidate to the ICE candidates.
1305
  This argument is relevant only if {{{use_ice}}} is set to {{{True}}} and {{{ice_stun_address}}} is not {{{None}}}.
1306 34 Ruud Klaver
 '''set_INIT'''(''self'')::
1307
  This moves the internal state machine into the {{{INIT}}} state.
1308
  If the state machine is in the {{{LOCAL}}} or {{{ESTABLISHED}}} states, this effectively resets the {{{RTPTransport}}} object for re-use.
1309
 '''set_LOCAL'''(''self'', '''local_sdp''', '''sdp_index''')::
1310 1 Adrian Georgescu
  This moves the the internal state machine into the {{{LOCAL}}} state.
1311
  [[BR]]''local_sdp'':[[BR]]
1312
  The local SDP to be proposed in the form of a {{{SDPSession}}} object.
1313
  Note that this object may be modified by this method.
1314
  [[BR]]''sdp_index'':[[BR]]
1315
  The index in the SDP for the media stream for which this object was created.
1316 34 Ruud Klaver
 '''set_ESTABLISHED'''(''self'', '''local_sdp''', '''remote_sdp''', '''sdp_index''')::
1317 1 Adrian Georgescu
  This moves the the internal state machine into the {{{ESTABLISHED}}} state.
1318
  [[BR]]''local_sdp'':[[BR]]
1319
  The local SDP to be proposed in the form of a {{{SDPSession}}} object.
1320
  Note that this object may be modified by this method, but only when moving from the {{{LOCAL}}} to the {{{ESTABLISHED}}} state.
1321
  [[BR]]''remote_sdp'':[[BR]]
1322
  The remote SDP that was received in in the form of a {{{SDPSession}}} object.
1323 17 Ruud Klaver
  [[BR]]''sdp_index'':[[BR]]
1324
  The index in the SDP for the media stream for which this object was created.
1325 1 Adrian Georgescu
1326
==== notifications ====
1327
1328 29 Luci Stanescu
 '''RTPTransportDidInitialize'''::
1329 17 Ruud Klaver
  This notification is sent when a {{{RTPTransport}}} object has successfully initialized.
1330
  If STUN+ICE is not requested, this is sent immediately on {{{set_INIT()}}}, otherwise it is sent after the STUN query has succeeded.
1331 1 Adrian Georgescu
  [[BR]]''timestamp'':[[BR]]
1332
  A {{{datetime.datetime}}} object indicating when the notification was sent.
1333 29 Luci Stanescu
 '''RTPTransportDidFail'''::
1334 17 Ruud Klaver
  This notification is sent by a {{{RTPTransport}}} object that fails to retrieve ICE candidates from the STUN server after {{{set_INIT()}}} is called.
1335
  [[BR]]''timestamp'':[[BR]]
1336
  A {{{datetime.datetime}}} object indicating when the notification was sent.
1337
  [[BR]]''reason'':[[BR]]
1338
  A string describing the failure reason.
1339 1 Adrian Georgescu
1340
=== AudioTransport ===
1341
1342
This object represent an audio stream as it is transported over the network.
1343
It contains an instance of {{{RTPTransport}}} and wraps a [http://www.pjsip.org/pjmedia/docs/html/group__PJMED__STRM.htm pjmedia_stream] object, which in turn manages the RTP encapsulation, RTCP session, audio codec and adaptive jitter buffer.
1344
It also generates a {{{SDPMedia}}} object to be included in the local SDP.
1345
1346
Like the {{{RTPTransport}}} object there are two usage scenarios.
1347
 * In the first scenario, only the {{{RTPTransport}}} instance to be used is passed to the AudioTransport object.
1348
   The application can then generate the {{{SDPMedia}}} object by calling the {{{get_local_media()}}} method and should include it in the SDP offer.
1349
   Once the remote SDP is received, it should be set along with the complete local SDP by calling the {{{start()}}} method, which will start the audio stream.
1350
   The stream can then be connected to the conference bridge.
1351
 * In the other scenario the remote SDP is already known because it was received in an SDP offer and can be passed directly on object instantiation.
1352
   The local {{{SDPMedia}}} object can again be generated by calling the {{{get_local_media()}}} method and is to be included in the SDP answer.
1353
   The audio stream is started directly when the object is created.
1354
1355
Unlike the {{{RTPTransport}}} object, this object cannot be reused.
1356
1357
==== attributes ====
1358
1359
 '''transport'''::
1360
  The {{{RTPTransport}}} object that was passed when the object got instatiated.
1361
  This attribute is read-only.
1362
 '''is_active'''::
1363
  A boolean indicating if the object is currently sending and receiving audio.
1364
  This attribute is read-only.
1365
 '''is_started'''::
1366
  A boolean indicating if the object has been started.
1367
  Both this attribute and the {{{is_active}}} attribute get set to {{{True}}} once the {{{start()}}} method is called, but unlike the {{{is_active}}} attribute this attribute does not get set to {{{False}}} once {{{stop()}}} is called.
1368
  This is to prevent the object from being re-used.
1369
  This attribute is read-only.
1370
 '''codec'''::
1371
  Once the SDP negotiation is complete, this attribute indicates the audio codec that was negotiated, otherwise it will be {{{None}}}.
1372
  This attribute is read-only.
1373
 '''sample_rate'''::
1374
  Once the SDP negotiation is complete, this attribute indicates the sample rate of the audio codec that was negotiated, otherwise it will be {{{None}}}.
1375
  This attribute is read-only.
1376
 '''direction'''::
1377
  The current direction of the audio transport, which is one of "sendrecv", "sendonly", "recvonly" or "inactive".
1378
  This attribute is read-only, although it can be set using the {{{update_direction()}}} method.
1379
1380
==== methods ====
1381
1382 40 Ruud Klaver
 '''!__init!__'''(''self'', '''transport''', '''remote_sdp'''={{{None}}}, '''sdp_index'''=0, '''enable_silence_detection'''=True, '''codecs'''={{{None}}})::
1383 1 Adrian Georgescu
  Creates a new {{{AudioTransport}}} object and start the underlying stream if the remote SDP is already known.
1384
  [[BR]]''transport'':[[BR]]
1385
  The transport to use in the form of a {{{RTPTransport}}} object.
1386
  [[BR]]''remote_sdp'':[[BR]]
1387
  The remote SDP that was received in the form of a {{{SDPSession}}} object.
1388
  [[BR]]''sdp_index'':[[BR]]
1389
  The index within the SDP of the audio stream that should be created.
1390
  [[BR]]''enable_silence_detection''[[BR]]
1391
  Boolean that indicates if silence detection should be used for this audio stream.
1392
  When enabled, this {{{AudioTransport}}} object will stop sending audio to the remote party if the input volume is below a certain threshold.
1393 40 Ruud Klaver
  [[BR]]''codecs''[[BR]]
1394
  A list of strings indicating the codecs that should be proposed in the SDP of this {{{AudioTransport}}}, in order of preference.
1395
  This overrides the global codecs list set on the {{{Engine}}}.
1396
  The values of this list are case insensitive.
1397 1 Adrian Georgescu
 '''get_local_media'''(''self'', '''is_offer''', '''direction'''="sendrecv")::
1398
  Generates a {{{SDPMedia}}} object which describes the audio stream.
1399
  This object should be included in a {{{SDPSession}}} object that gets passed to the {{{Invitation}}} object.
1400
  This method should also be used to obtain the SDP to include in re-INVITEs and replies to re-INVITEs.
1401
  [[BR]]''is_offer'':[[BR]]
1402
  A boolean indicating if the SDP requested is to be included in an offer.
1403
  If this is {{{False}}} it is to be included in an answer.
1404
  [[BR]]''direction'':[[BR]]
1405
  The direction attribute to put in the SDP.
1406
 '''start'''(''self'', '''local_sdp''', '''remote_sdp''', '''sdp_index''')::
1407
  This method should only be called once, when the application has previously sent an SDP offer and the answer has been received.
1408
  [[BR]]''local_sdp'':[[BR]]
1409
  The full local SDP that was included in the SDP negotiation in the form of a {{{SDPSession}}} object.
1410
  [[BR]]''remote_sdp'':[[BR]]
1411
  The remote SDP that was received in the form of a {{{SDPSession}}} object.
1412
  [[BR]]''sdp_index'':[[BR]]
1413
  The index within the SDP of the audio stream.
1414
 '''stop'''(''self'')::
1415
  This method stops and destroys the audio stream encapsulated by this object.
1416
  After this it can no longer be used and should be deleted, while the {{{RTPTransport}}} object used by it can be re-used for something else.
1417
  This method will be called automatically when the object is deleted after it was started, but this should not be relied on because of possible reference counting issues.
1418
 '''send_dtmf'''(''self'', '''digit''')::
1419
  For a negotiated audio transport this sends one DTMF digit to the other party
1420
  [[BR]]''digit'':[[BR]]
1421
  A string of length one indicating the DTMF digit to send.
1422
  This can be either a number or letters A through D.
1423
 '''update_direction'''(''self'', '''direction''')::
1424
  This method should be called after SDP negotiation has completed to update the direction of the media stream.
1425
  [[BR]]''direction'':[[BR]]
1426
  The direction that has been negotiated.
1427
1428
==== notifications ====
1429
1430 29 Luci Stanescu
 '''RTPAudioTransportGotDTMF'''::
1431 1 Adrian Georgescu
  This notification will be sent when an incoming DTMF digit is received from the remote party.
1432
  [[BR]]''timestamp'':[[BR]]
1433
  A {{{datetime.datetime}}} object indicating when the notification was sent.
1434
  [[BR]]''digit'':[[BR]]
1435
  The DTMF digit that was received, in the form of a string of length one.
1436
  This can be either a number or letters A through D.
1437
1438
=== WaveFile ===
1439
1440
This is a simple object that allows playing back of a {{{.wav}}} file over the PJSIP conference bridge, possibly looping a number of times.
1441
Only 16-bit PCM and A-law/U-law formats are supported.
1442
Its main purpose is the playback of ringtones.
1443
1444
This class can be instantiated by the application before the {{{Engine}}} is running, but in order to actually start playback, through the {{{start()}}} method, the {{{Engine}}} must have been started as well.
1445
Once the {{{start()}}} method is called, the {{{.wav}}} file will continue playing until the loop count specified is reached( or forever if the specified loop count is 0), or until the {{{stop()}}} method is called.
1446 29 Luci Stanescu
When playback stops for either of these reasons, a {{{WaveFileDidFinishPlaying}}} notification is sent by the object.
1447 1 Adrian Georgescu
After this the {{{start()}}} method may be called again in order to re-use the object.
1448
1449
It is the application's responsibility to keep a reference to the {{{WaveFile}}} object for the duration of playback.
1450
If the reference count of the object reaches 0, playback will be stopped.
1451
In this case no notification will be sent.
1452
1453
==== attributes ====
1454
1455
 '''file_name'''::
1456
  The name of the {{{.wav}}} file, as specified when the object was created.
1457
 '''is_active'''::
1458
  A boolean read-only property that indicates if the file is currently being played back.
1459
  Note that if the playback loop is currently in a pause between playbacks, this attribute will also be {{{True}}}.
1460
1461
==== methods ====
1462
1463
 '''!__init!__'''(''self'', '''file_name''')::
1464
  Creates a new {{{WaveFile}}} object.
1465
  [[BR]]''file_name'':[[BR]]
1466
  The name of the {{{.wav}}} file to be played back as a string.
1467
  This should include the full path to the file.
1468
 '''start'''(''self'', '''level'''=100, '''loop_count'''=1, '''pause_time'''=0)::
1469
  Start playback of the {{{.wav}}} file, optionally looping it.
1470
  [[BR]]''level'':[[BR]]
1471
  The level to play the file back at, in percentage.
1472
  A percentage lower than 100 means playback will be attenuated, a percentage higher than 100 means it will amplified.
1473
  [[BR]]''loop_count'':[[BR]]
1474
  The number of time to loop playing this file.
1475
  A value of 0 means looping infinitely.
1476
  [[BR]]''pause_time'':[[BR]]
1477
  The number of seconds to pause between consecutive loops.
1478
  This can be either an int or a float.
1479
 '''stop'''(''self'')::
1480
  Stop playback of the file.
1481
1482
==== notifications ====
1483
1484 29 Luci Stanescu
 '''WaveFileDidFinishPlaying'''::
1485 1 Adrian Georgescu
  This notification will be sent whenever the {{{.wav}}} file has been played back the specified number of times.
1486
  After sending this event, the playback may be re-started.
1487
  [[BR]]''timestamp'':[[BR]]
1488
  A {{{datetime.datetime}}} object indicating when the notification was sent.
1489
1490
=== RecordingWaveFile ===
1491
1492
This is a simple object that allows recording whatever is heard on the PJSIP conference bridge to a PCM {{{.wav}}} file.
1493
1494
This class can be instantiated by the application before the {{{Engine}}} is running, but in order to actually start playback, through the {{{start()}}} method, the {{{Engine}}} must have been started as well.
1495
Recording to the file can be stopped either by calling the {{{stop()}}} method or by removing all references to the object.
1496
Once the {{{stop()}}} method has been called, the {{{start()}}} method may not be called again.
1497
It is the application's responsibility to keep a reference to the {{{RecordingWaveFile}}} object for the duration of the recording.
1498
1499
==== attributes ====
1500
1501
 '''file_name'''::
1502 15 Ruud Klaver
  The name of the {{{.wav}}} file, as specified when the object was created.
1503
 '''is_active'''::
1504
  A boolean read-only attribute that indicates if the file is currently being written to.
1505 1 Adrian Georgescu
 '''is_paused'''::
1506
  A boolean read-only attribute that indicates if the active recording is currently paused.
1507
1508
==== methods ====
1509
1510
 '''!__init!__'''(''self'', '''file_name''')::
1511
  Creates a new {{{RecordingWaveFile}}} object.
1512
  [[BR]]''file_name'':[[BR]]
1513
  The name of the {{{.wav}}} file to record to as a string.
1514
  This should include the full path to the file.
1515 15 Ruud Klaver
 '''start'''(''self'')::
1516
  Start recording the sound on the conference bridge to the {{{.wav}}} file.
1517
 '''pause'''(''self'')::
1518
  Pause recording to the {{{.wav}}} file.
1519 1 Adrian Georgescu
 '''resume'''(''self'')::
1520
  Resume a previously paused recording.
1521
 '''stop'''(''self'')::
1522
  Stop recording to the file.