Project

General

Profile

SipCoreApiDocumentation » History » Version 66

Ruud Klaver, 05/26/2009 07:22 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
 '''sipsimple.engine'''::
63
  Python module that contains the {{{Engine}}} singleton class, which manages the thread that constantly polls the PJSIP library, i.e. the PJSIP worker thread.
64
  For the applications that use the core of {{{sipsimple}}}, the {{{Engine}}} object forms the main entry point.
65
 '''sipsimple.core'''::
66
  This is the Python C extension module ultimately compiled from the Cython file and PJSIP static libraries.
67
  It contains these types of classes:
68
   * The {{{PJSIPUA}}} class, which can only be instanced once, and is this case is only instanced once by the {{{Engine}}} object.
69
     In this way the {{{Engine}}} singleton class acts as a wrapper to the one {{{PJSIPUA}}} instance.
70
     The {{{PJSIPUA}}} class represents the SIP endpoint and manages the initialization and destruction of all the PJSIP libraries.
71
     It also provides a number of methods.
72
     The application however should never call these methods directly on the {{{PJSIPUA}}} object, rather it should call them on the {{{Engine}}} wrapper object.
73
     This object handles everything that for one reason or another cannot or should not be handled from Cython.
74 42 Ruud Klaver
   * The classes that represent the main SIP primitives to be used by the application, or in the case of {{{Request}}} a generic SIP transction.
75 1 Adrian Georgescu
     The application can instantiate these classes once the {{{Engine}}} class has been instantiated and the PJSIP worker thread has been started.
76
     All of these classes represent a state machine.
77 42 Ruud Klaver
     * {{{Request}}}
78 1 Adrian Georgescu
     * {{{Publication}}}
79
     * {{{Subscription}}}
80
     * {{{Invitation}}}
81
   * 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.
82
     * {{{SIPURI}}}
83
     * {{{Credentials}}}
84
     * {{{Route}}}
85
   * A number of SDP manipulation classes, which directly wrap the PJSIP structures representing either the parsed or to be generated SDP.
86
     {{{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.
87
     * {{{SDPSession}}}
88
     * {{{SDPMedia}}}
89
     * {{{SDPConnection}}}
90
     * {{{SDPAttribute}}}
91
   * Two classes related to transport of media traffic and audio traffic specifically, built on PJMEDIA.
92
     These classes can be instantiated independently from the other classes in order to keep signaling and media separate.
93
     * {{{RTPTransport}}}
94
     * {{{AudioTransport}}}
95
   * Two classes related to {{{.wav}}} files, one for playback and one for recording.
96
     * {{{WaveFile}}}
97
     * {{{RecordingWaveFile}}}
98
   * Two exception classes, the second being a subclass of the first.
99
     * {{{SIPCoreError}}}
100
     * {{{PJSIPError}}}
101
   * Classes used internally within the {{{core}}} module, e.g. to wrap a particular PJSIP library.
102 42 Ruud Klaver
     These classes should not be imported directly and are not included in the {{{__all__}}} variable of this module.
103
 '''sipsimple.primitives'''::
104
  This module contains several SIP primitive classes that are based on the {{{Request}}} object from the core:
105
   * {{{Registration}}}
106
   * {{{Message}}}
107 1 Adrian Georgescu
108
These classes (except the ones internal to the {{{core}}} module) are illustrated in the following diagram:
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 43 Ruud Klaver
Besides setting up the notification observers, the application should import the relevant objects from the {{{sipsimple.core}}}, {{{sipsimple.engine}}} and {{{sipsimple.primitives}}} modules.
147 1 Adrian Georgescu
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 43 Ruud Klaver
As a very basic example, one can {{{REGISTER}}} for a sip account by typing the following lines on a Python console:
163 1 Adrian Georgescu
{{{
164 43 Ruud Klaver
from sipsimple.engine import Engine
165
from sipsimple.primitives import Registration
166
from sipsimple.core import Credentials, SIPURI, Route
167 1 Adrian Georgescu
e = Engine()
168
e.start()
169 65 Ruud Klaver
uri = SIPURI(user="alice", host="example.com")
170
cred = Credentials("alice", "mypassword")
171
reg = Registration(uri, credentials=cred)
172 43 Ruud Klaver
reg.register(SIPURI("127.0.0.1",port=12345), Route("1.2.3.4"))
173 1 Adrian Georgescu
}}}
174
Note that in this example no observer for notifications from this {{{Registration}}} object are registered, so the result of the operation cannot be seen.
175 43 Ruud Klaver
Also note that this will not keep the registration registered when it is about to expire, as it is the application's responsibility.
176
See the {{{Registration}}} documentation for more details.
177
When performing operations with a running {{{Engine}}} on a Python console, be sure to call the {{{stop()}}} method of the {{{Engine}}} before exiting the console.
178
Failure to do so will keep the {{{Engine}}} running and will cause the program to hang.
179 1 Adrian Georgescu
180
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].
181 43 Ruud Klaver
182
> NOTE: GreenRegistration is outdated and should probably not be used anymore.
183
184
Another convention that is worth mentioning at this point is that the SIP core will never perform DNS lookups.
185
For the sake of flexibility, it is the responsibility of the application to do this and pass the result to SIP core objects using the {{{Route}}} helper object, indicating the destination IP address the resulting SIP request should be sent to.
186 32 Ruud Klaver
187 1 Adrian Georgescu
== Components ==
188
189
=== Engine ===
190
191
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.
192
Once the {{{start()}}} method is called, it instantiates the {{{core.PJSIPUA}}} object and will proxy attribute and methods from it to the application.
193
194
==== attributes ====
195
196
 '''default_start_options''' (class attribute)::
197
  This dictionary is a class attribute that describes the default values for the initialization options passed as keyword arguments to the {{{start()}}} method.
198
  Consult this method for documentation of the contents.
199 32 Ruud Klaver
 '''is_running'''::
200
  A boolean property indicating if the {{{Engine}}} is running and if it is safe to try calling any proxied methods or attributes on it.
201 1 Adrian Georgescu
202
==== methods ====
203
204
 '''!__init!__'''(''self'')::
205
  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.
206
 '''start'''(''self'', '''**kwargs''')::
207
  Initialize all PJSIP libraries based on the keyword parameters provited and start the PJSIP worker thread.
208
  If this fails an appropriate exception is raised.
209
  After the {{{Engine}}} has been started successfully, it can never be started again after being stopped.
210
  The keyword arguments will be discussed here.
211
  Many of these values are also readable as (proxied) attributes on the Engine once the {{{start()}}} method has been called.
212
  Many of them can also be set at runtime, either by modifying the attribute or by calling a particular method.
213
  This will also be documented for each argument in the following list of options.
214
  [[BR]]''auto_sound'':[[BR]]
215
  A boolean indicating if PJSIP should automatically select and enable a soundcard to use for for recording and playing back sound.
216 39 Ruud Klaver
  If this is set to {{{False}}} the Dummy sound device is selected.
217
  To be able to hear and contribute audio, the application will have to select a sound device manually through the {{{set_sound_devices}}} method.
218 1 Adrian Georgescu
  This option is not accessible as an attribute on the object, as it is transitory.
219
  [[BR]]''local_ip'': (Default: {{{None}}})[[BR]]
220
  IP address of a local interface to bind to.
221 44 Ruud Klaver
  If this is {{{None}}} or {{{0.0.0.0}}} on start, the {{{Engine}}} will listen on all network interfaces.
222
  As an attribute, this value is read-only and cannot be changed at runtime.
223 1 Adrian Georgescu
  [[BR]]''local_udp_port'': (Default: {{{0}}})[[BR]]
224
  The local UDP port to listen on for UDP datagrams.
225
  If this is 0, a random port will be chosen.
226
  If it is {{{None}}}, the UDP transport is disabled, both for incoming and outgoing traffic.
227
  As an attribute, this value is read-only, but it can be changed at runtime using the {{{set_local_udp_port()}}} method.
228
  [[BR]]''local_tcp_port'': (Default: {{{0}}})[[BR]]
229
  The local TCP port to listen on for new TCP connections.
230
  If this is 0, a random port will be chosen.
231
  If it is {{{None}}}, the TCP transport is disabled, both for incoming and outgoing traffic.
232
  As an attribute, this value is read-only, but it can be changed at runtime using the {{{set_local_tcp_port()}}} method.
233
  [[BR]]''local_tls_port'': (Default: {{{0}}})[[BR]]
234
  The local TCP port to listen on for new TLS over TCP connections.
235 28 Adrian Georgescu
  If this is 0, a random port will be chosen.
236 1 Adrian Georgescu
  If it is {{{None}}}, the TLS transport is disabled, both for incoming and outgoing traffic.
237 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. 
238 32 Ruud Klaver
  [[BR]]''tls_protocol'': (Default: "TLSv1")[[BR]] 
239
  This string describes the (minimum) TLS protocol that should be used. 
240
  Its values should be either {{{None}}}, "SSLv2", "SSLv23", "SSLv3" or "TLSv1". 
241
  If {{{None}}} is specified, the PJSIP default will be used, which is currently "TLSv1". 
242 1 Adrian Georgescu
  [[BR]]''tls_verify_server'': (Default: {{{False}}})[[BR]]
243 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.
244 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.
245 1 Adrian Georgescu
  [[BR]]''tls_ca_file'': (Default: {{{None}}})[[BR]]
246
  This string indicates the location of the file containing the local list of CA certificates, to be used for TLS connections.
247 32 Ruud Klaver
  If this is set to {{{None}}}, no CA certificates will be read. 
248
  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. 
249
  [[BR]]''tls_cert_file'': (Default: {{{None}}})[[BR]] 
250
  This string indicates the location of a file containing the TLS certificate to be used for TLS connections. 
251
  If this is set to {{{None}}}, no certificate file will be read. 
252
  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. 
253
  [[BR]]''tls_privkey_file'': (Default: {{{None}}})[[BR]] 
254
  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. 
255
  If this is set to {{{None}}}, no private key file will be read. 
256
  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. 
257
  [[BR]]''tls_timeout'': (Default: 1000)[[BR]] 
258
  The timeout value for a TLS negotiation in milliseconds. 
259
  Note that this value should be reasonably small, as a TLS negotiation blocks the whole PJSIP polling thread. 
260
  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.
261 1 Adrian Georgescu
  [[BR]]''ec_tail_length'': (Default: {{{50}}})[[BR]]
262
  Echo cancellation tail length in milliseconds.
263
  A longer value should provide better echo cancellation but incurs more processing cost.
264
  Setting this to 0 will disable echo cancellation.
265 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.
266 1 Adrian Georgescu
  [[BR]]''user_agent'': (Default: {{{"ag-projects/sipclient-%version-pjsip-%pjsip-version"}}})[[BR]]
267
  This value indicates what should be set in the {{{User-Agent}}} header, which is included in each request or response sent.
268
  It can be read and set directly as an attribute at runtime.
269
  [[BR]]''log_level'': (Default: 5)[[BR]]
270 29 Luci Stanescu
  This integer dictates the maximum log level that may be reported to the application by PJSIP through the {{{SIPEngineLog}}} notification.
271 1 Adrian Georgescu
  By default the maximum amount of logging information is reported.
272
  This value can be read and set directly as an attribute at runtime.
273
  [[BR]]''trace_sip'': (Default: {{{False}}})[[BR]]
274 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.
275 1 Adrian Georgescu
  It can be read and set directly as an attribute at runtime.
276
  [[BR]]''sample_rate'': (Default: {{{32}}})[[BR]]
277
  The sample rate in kHz at which the sound card should operate.
278
  Higher values allow some codecs (such as speex) to achieve better quality but will incur higher processing cost, particularly in combination with echo cancellation.
279
  This parameter should be either 8, 16 or 32.
280
  The corresponding attribute of this value is read-only.
281
  [[BR]]''playback_dtmf'': (Default: {{{True}}})[[BR]]
282
  If this boolean is set to {{{True}}}, both incoming and outgoing DTMF signals have their corresponding audio tones played back on the sound card.
283
  This value can be read and set directly as an attribute at runtime.
284
  [[BR]]''rtp_port_range'': (Default: (40000, 40100))[[BR]]
285
  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.
286
  It can be read and set directly as an attribute at runtime, but the ports of previously created {{{RTPTransport}}} objects remain unaffected.
287 40 Ruud Klaver
  [[BR]]''codecs'': (Default: {{{["speex", "G722", "PCMU", "PCMA", "iLBC", "GSM"]}}})[[BR]]
288 1 Adrian Georgescu
  This list specifies the codecs to use for audio sessions and their preferred order.
289
  It can be read and set directly as an attribute at runtime.
290 40 Ruud Klaver
  Note that this global option can be overridden by an argument passed to {{{AudioTransport.__init__()}}}.
291
  The strings in this list is case insensitive.
292 1 Adrian Georgescu
  [[BR]]''events'': (Default: <some sensible events>)[[BR]]
293
  PJSIP needs a mapping between SIP SIMPLE event packages and content types.
294
  This dictionary provides some default packages and their event types.
295
  As an attribute, this value is read-only, but it can be changed at runtime using the {{{set_local_tls_port()}}} method.
296 32 Ruud Klaver
 '''start_cfg'''(''self'', '''enable_sound'''={{{True}}}, '''**kwargs''')::
297 36 Adrian Georgescu
  This provides a [wiki:SipConfigurationAPI Configuration API] aware start method.
298 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.
299
  Any argument that can be passed to {{{start()}}} may be specified, except for {{{auto_sound}}}.
300 35 Ruud Klaver
  [[BR]]''enable_sound'':[[BR]]
301 1 Adrian Georgescu
  If this is set to {{{True}}}, the sound devices set up in the configuration framework will be started.
302 39 Ruud Klaver
  If it is {{{False}}}, the Dummy sound device will be used.
303 1 Adrian Georgescu
 '''stop'''(''self'')::
304
  Stop the PJSIP worker thread and unload all PJSIP libraries.
305
  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}}}.
306
  Also note that, once stopped the {{{Engine}}} cannot be started again.
307
  This method is automatically called when the Python interpreter exits.
308
309
==== proxied attributes ====
310
311
Besides all the proxied attributes described for the {{{__init__}}} method above, two other attributes are provided once the {{{Engine}}} has been started.
312
313
 '''playback_devices'''::
314 20 Ruud Klaver
  This read-only attribute is a list of string, representing all audio playback devices on the system that can be used.
315
  These device names can be passed as the {{{playback_device}}} argument of the {{{set_sound_devices()}}} method.
316 1 Adrian Georgescu
 '''recording_devices'''::
317
  This read-only attribute is a list of string, representing all audio recording devices on the system that can be used.
318
  These device names can be passed as the {{{recording_device}}} argument of the {{{set_sound_devices()}}} method.
319 44 Ruud Klaver
 '''current_playback_device'''::
320
  The name of the audio playback device currently in use.
321
 '''current_recording_device'''::
322
  The name of the audio recording device currently in use.
323 40 Ruud Klaver
 '''available_codecs'''::
324
  A read-only list of codecs available in the core, regardless of the codecs configured through the {{{codecs}}} attribute.
325 1 Adrian Georgescu
326
==== proxied methods ====
327
328
 '''add_event'''(''self'', '''event''', '''accept_types''')::
329
  Couple a certain event package to a list of content types.
330
  Once added it cannot be removed or modified.
331 20 Ruud Klaver
 '''set_sound_devices'''(''self'', '''playback_device'''={{{None}}}, '''recording_device'''={{{None}}}, '''tail_length'''={{{None}}})::
332 1 Adrian Georgescu
  Set and open the playback and recording device, using the specified echo cancellation tail length in milliseconds.
333
  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).
334
  A {{{tail_length}}} of 0 disables echo cancellation.
335
  The device attributes need to be strings and should be obtained from the {{{playback_devices}}} and {{{recording_devices}}} attributes respectively.
336
  Passing either of these as {{{None}}} means that PJSIP should select a sound device automatically.
337
  If sound devices were already opened these will be closed first.
338
 '''connect_audio_transport'''(''self'', '''transport''')::
339
  Connect a started audio transport, in the form of a {{{AudioTransport}}} object, to the recording and playback audio devices and other connected audio transports.
340
  This means that when more than one audio stream is connected they will form a conference.
341 29 Luci Stanescu
 '''disconnect_audio_transport'''(''self'', '''transport''')::
342 1 Adrian Georgescu
  Disconnect a previously connected audio transport, in the form of a {{{AudioTransport}}} object.
343
  Stopped audio streams are disconnected automatically.
344 38 Ruud Klaver
 '''detect_nat_type'''(''self'', '''stun_server_address''', '''stun_server_port'''=3478, '''user_data'''=None)::
345 1 Adrian Georgescu
  Will start a series of STUN requests which detect the type of NAT this host is behind.
346
  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.
347 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.
348 1 Adrian Georgescu
 '''set_local_udp_port'''(''self'', '''value''')::
349 28 Adrian Georgescu
  Update the {{{local_udp_port}}} attribute to the newly specified value.
350
 '''set_local_tcp_port'''(''self'', '''value''')::
351
  Update the {{{local_tcp_port}}} attribute to the newly specified value.
352 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):: 
353
  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}}}. 
354 1 Adrian Georgescu
  The semantics of the arguments are the same as on the {{{start()}}} method. 
355
 '''parse_sip_uri(''self'', '''uri_string''')::
356
  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.
357 44 Ruud Klaver
 '''play_tones(''self'', '''tones''')::
358
  Play a sequence of single or dual frequency tones over the audio device.
359
  The tones should be a list of 3-item tuples, in the form of {{{[(<freq1>, <freq2>, <duration>), ...]}}}, with Hz as unit for the frequencies and ms as unit for the duration.
360
  If {{{freq2}}} is 0, this indicates that only {{{freq1}}} should be used for the tone.
361
  If {{{freq1}}} is 0, this indicates a pause when no tone should be played for the set duration.
362 1 Adrian Georgescu
363
==== notifications ====
364
365 16 Ruud Klaver
Notifications sent by the {{{Engine}}} are notifications that are related to the {{{Engine}}} itself or unrelated to any SIP primitive object.
366 1 Adrian Georgescu
They are described here including the data attributes that is included with them.
367
368 29 Luci Stanescu
 '''SIPEngineWillStart'''::
369 16 Ruud Klaver
  This notification is sent when the {{{Engine}}} is about to start.
370
  [[BR]]''timestamp'':[[BR]]
371
  A {{{datetime.datetime}}} object indicating when the notification was sent.
372 29 Luci Stanescu
 '''SIPEngineDidStart'''::
373 16 Ruud Klaver
  This notification is sent when the {{{Engine}}} is has just been started.
374
  [[BR]]''timestamp'':[[BR]]
375
  A {{{datetime.datetime}}} object indicating when the notification was sent.
376 29 Luci Stanescu
 '''SIPEngineDidFail'''::
377 16 Ruud Klaver
  This notification is sent whenever the {{{Engine}}} has failed fatally and either cannot start or is about to stop.
378
  It is not recommended to call any methods on the {{{Engine}}} at this point.
379
  [[BR]]''timestamp'':[[BR]]
380
  A {{{datetime.datetime}}} object indicating when the notification was sent.
381 29 Luci Stanescu
 '''SIPEngineWillEnd'''::
382 16 Ruud Klaver
  This notification is sent when the {{{Engine}}} is about to stop because the application called the {{{stop()}}} method.
383
  Methods on the {{{Engine}}} can be called at this point, but anything that has a delayed result will probably not return any notification.
384
  [[BR]]''timestamp'':[[BR]]
385
  A {{{datetime.datetime}}} object indicating when the notification was sent.
386 29 Luci Stanescu
 '''SIPEngineDidEnd'''::
387 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.
388
  [[BR]]''timestamp'':[[BR]]
389
  A {{{datetime.datetime}}} object indicating when the notification was sent.
390 29 Luci Stanescu
 '''SIPEngineLog'''::
391 1 Adrian Georgescu
  This notification is a wrapper for PJSIP logging messages.
392
  It can be used by the application to output PJSIP logging to somewhere meaningful, possibly doing filtering based on log level.
393
  [[BR]]''timestamp'':[[BR]]
394
  A {{{datetime.datetime}}} object representing the time when the log message was output by PJSIP.
395
  [[BR]]''sender'':[[BR]]
396
  The PJSIP module that originated this log message.
397
  [[BR]]''level'':[[BR]]
398
  The logging level of the message as an integer.
399
  Currently this is 1 through 5, 1 being the most critical.
400
  [[BR]]''message'':[[BR]]
401
  The actual log message.
402 29 Luci Stanescu
 '''SIPEngineSIPTrace'''::
403 1 Adrian Georgescu
  Will be sent only when the {{{do_siptrace}}} attribute of the {{{Engine}}} instance is set to {{{True}}}.
404
  The notification data attributes will contain the SIP messages as they are sent and received on the wire.
405
  [[BR]]''timestamp'':[[BR]]
406
  A {{{datetime.datetime}}} object indicating when the notification was sent.
407
  [[BR]]''received'':[[BR]]
408
  A boolean indicating if this message was sent from or received by PJSIP (i.e. the direction of the message).
409
  [[BR]]''source_ip'':[[BR]]
410
  The source IP address as a string.
411
  [[BR]]''source_port'':[[BR]]
412 29 Luci Stanescu
  The source port of the message as an integer.
413 1 Adrian Georgescu
  [[BR]]''destination_ip'':[[BR]]
414
  The destination IP address as a string.
415
  [[BR]]''source_port'':[[BR]]
416
  The source port of the message as an integer.
417
  [[BR]]''data'':[[BR]]
418
  The contents of the message as a string.
419
420
> For received message the destination_ip and for sent messages the source_ip may not be reliable.
421
422 29 Luci Stanescu
 '''SIPEngineDetectedNATType'''::
423 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.
424
  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.
425
  [[BR]]''timestamp'':[[BR]]
426
  A {{{datetime.datetime}}} object indicating when the notification was sent.
427
  [[BR]]''succeeded'':[[BR]]
428
  A boolean indicating if the NAT detection succeeded.
429 38 Ruud Klaver
  [[BR]]''user_data'':[[BR]]
430
  The {{{user_data}}} argument passed while calling the {{{detect_nat_type()}}} method.
431
  This can be any object and could be used for matching requests to responses.
432 1 Adrian Georgescu
  [[BR]]''nat_type'':[[BR]]
433
  A string describing the type of NAT found.
434
  This value is only present if NAT detection succeeded.
435
  [[BR]]''error'':[[BR]]
436
  A string indicating the error that occurred while attempting to detect the type of NAT.
437
  This value only present if NAT detection did not succeed.
438
 '''SIPEngineGotException'''::
439
  This notification is sent whenever there is an unexpected exception within the PJSIP working thread.
440
  The application should show the traceback to the user somehow.
441
  An exception need not be fatal, but if it is it will be followed by a '''SIPEngineDidFail''' notification.
442
  [[BR]]''timestamp'':[[BR]]
443
  A {{{datetime.datetime}}} object indicating when the notification was sent.
444
  [[BR]]''traceback'':[[BR]]
445
  A string containing the traceback of the exception.
446
  In general this should be printed on the console.
447
448
=== SIPURI ===
449
450
This is a helper object for representing a SIP URI.
451
This object needs to be used whenever a SIP URI should be specified to the SIP core.
452
It supports comparison to other {{{SIPURI}}} objects using the == and != expressions.
453
As all of its attributes are set by the {{{__init__}}} method, the individual attributes will not be documented here.
454
455
==== methods ====
456
457 23 Ruud Klaver
 '''!__init!__'''(''self'', '''host''', '''user'''={{{None}}}, '''port'''={{{None}}}, '''display'''={{{None}}}, '''secure'''={{{False}}}, '''parameters'''={{{None}}}, '''headers'''={{{None}}})::
458 1 Adrian Georgescu
  Creates the SIPURI object with the specified parameters as attributes.
459
  Each of these attributes can be accessed and changed on the object once instanced.
460
  {{{host}}} is the only mandatory attribute.
461
  [[BR]]''host'':[[BR]]
462
  The host part of the SIP URI as a string.
463
  [[BR]]''user'':[[BR]]
464
  The username part of the SIP URI as a string, or None if not set.
465
  [[BR]]''port'':[[BR]]
466
  The port part of the SIP URI as an int, or None or 0 if not set.
467
  [[BR]]''display'':[[BR]]
468
  The optional display name of the SIP URI as a string, or None if not set.
469
  [[BR]]''secure'':[[BR]]
470
  A boolean indicating whether this is a SIP or SIPS URI, the latter being indicated by a value of {{{True}}}.
471
  [[BR]]''parameters'':[[BR]]
472
  The URI parameters. represented by a dictionary.
473
  [[BR]]''headers'':[[BR]]
474
  The URI headers, represented by a dictionary.
475
 '''!__str!__'''(''self'')::
476
  The special Python method to represent this object as a string, the output is the properly formatted SIP URI.
477
 '''copy'''(''self'')::
478
  Returns a copy of the {{{SIPURI}}} object.
479
480
=== Credentials ===
481
482 61 Ruud Klaver
This simple object represents authentication credentials for a particular SIP account.
483
These can be included whenever creating a SIP primitive object that originates SIP requests.
484 1 Adrian Georgescu
As with the {{{SIPURI}}} object, the attributes of this object are the same as the arguments to the {{{__init__}}} method.
485 61 Ruud Klaver
Note that the domain name of the SIP account is not stored on this object.
486 1 Adrian Georgescu
487
==== methods ====
488
489 61 Ruud Klaver
 '''!__init!__'''(''self'', '''username''', '''password''')::
490 1 Adrian Georgescu
  Creates the Credentials object with the specified parameters as attributes.
491
  Each of these attributes can be accessed and changed on the object once instanced.
492 61 Ruud Klaver
  [[BR]]''username'':[[BR]]
493
  A string representing the username of the account for which these are the credentials.
494 1 Adrian Georgescu
  [[BR]]''password'':[[BR]]
495
  The password for this SIP account as a string.
496
 '''copy'''(''self'')::
497
  Returns a copy of the {{{Credentials}}} object.
498
499
=== Route ===
500
501 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.
502 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.
503 22 Ruud Klaver
The contents of the {{{Route}}} object will be placed in the {{{Route}}} header of the request.
504 1 Adrian Georgescu
As with the {{{SIPURI}}} object, the attributes of this object are the same as the arguments to the {{{__init__}}} method.
505
506
==== methods ====
507
508 22 Ruud Klaver
 '''!__init!__'''(''self'', '''address''', '''port'''=5060, '''transport'''={{{None}}})::
509 1 Adrian Georgescu
  Creates the Route object with the specified parameters as attributes.
510
  Each of these attributes can be accessed on the object once instanced.
511 22 Ruud Klaver
  [[BR]]''address'':[[BR]]
512 45 Ruud Klaver
  The IPv4 address that the request in question should be sent to as a string.
513 1 Adrian Georgescu
  [[BR]]''port'':[[BR]]
514 22 Ruud Klaver
  The port to send the requests to, represented as an int.
515 1 Adrian Georgescu
  [[BR]]''transport'':[[BR]]
516 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.
517 1 Adrian Georgescu
 '''copy'''(''self'')::
518
  Returns a copy of the {{{Route}}} object.
519 22 Ruud Klaver
520 46 Ruud Klaver
=== Request ===
521
522
The {{{sipsimple.core.Request}}} object encapsulates a single SIP request transaction from the client side, which includes sending the request, receiving the response and possibly waiting for the result of the request to expire.
523
Although this class can be used by the application to construct and send an arbitrary SIP request, most applications will use the classes provided in the {{{sipsimple.primitives}}} module, which are built on top of one or several {{{Request}}} objects and deal with instances of specific SIP methods.
524
525
The lifetime of this object is linear and is described by the following diagram:
526
527
[[Image(sipsimple-request-lifetime.png, nolink)]]
528
529
The bar denotes which state the object is in and at the top are the notifications which may be emitted at certain points in time.
530
Directly after the object is instantiated, it will be in the {{{INIT}}} state.
531
The request will be sent over the network once its {{{send()}}} method is called, moving the object into the {{{IN_PROGRESS}}} state.
532
On each provisional response that is received in reply to this request, the {{{SIPRequestGotProvisionalResponse}}} notification is sent, with data describing the response.
533
Note that this may not occur at all if not provisional responses are received.
534
When the {{{send()}}} method has been called and it does not return an exception, the object will send either a {{{SIPRequestDidSucceed}}} or a {{{SIPRequestDidFail}}} notification.
535
Both of these notifications include data on the response that triggered it.
536
Note that a SIP response that requests authentication (401 or 407) will be handled internally the first time, if a {{{Credentials}}} object was supplied.
537
If this is the sort of request that expires (detected by a {{{Expires}}} header in the response or a {{{expires}}} parameter in the {{{Contact}}} header of the response), and the request was successful, the object will go into the {{{EXPIRING}}} state.
538
A certain amount of time before the result of the request will expire, governed by the {{{expire_warning_time}}} class attribute and the actual returned expiration time, a {{{SIPRequestWillExpire}}} notification will be sent.
539
This should usually trigger whomever is using this {{{Request}}} object to construct a new {{{Request}}} for a refreshing operation.
540
When the {{{Request}}} actually expires, or when the {{{EXPIRING}}} state is skipped directly after sending {{{SIPRequestDidSucceed}}} or {{{SIPRequestDidFail}}}, a {{{SIPRequestDidEnd}}} notification will be sent.
541
542
==== attributes ====
543
544 49 Ruud Klaver
 '''expire_warning_time''' (class attribute)::
545
  The {{{SIPRequestWillExpire}}} notification will be sent halfway between the positive response and the actual expiration time, but at least this amount of seconds before.
546
  The default value is 30 seconds.
547 47 Ruud Klaver
 '''state'''::
548
  Indicates the state the {{{Request}}} object is in, in the form of a string.
549
  Refer to the diagram above for possible states.
550
  This attribute is read-only.
551
 '''method'''::
552
  The method of the SIP request as a string.
553
  This attribute is set on instantiation and is read-only.
554
 '''from_uri'''::
555
  The SIP URI to put in the {{{From}}} header of the request, in the form of a {{{SIPURI}}} object.
556 62 Ruud Klaver
  This attribute is set on instantiation and is read-only.
557 47 Ruud Klaver
 '''to_uri'''::
558
  The SIP URI to put in the {{{To}}} header of the request, in the form of a {{{SIPURI}}} object.
559
  This attribute is set on instantiation and is read-only.
560
 '''request_uri'''::
561
  The SIP URI to put as request URI in the request, in the form of a {{{SIPURI}}} object.
562
  This attribute is set on instantiation and is read-only.
563 1 Adrian Georgescu
 '''route'''::
564
  Where to send the SIP request to, including IP, port and transport, in the form of a {{{SIPURI}}} object.
565
  This will also be included in the {{{Route}}} header of the request.
566
  This attribute is set on instantiation and is read-only.
567 62 Ruud Klaver
 '''credentials'''::
568
  The credentials to be used when challenged for authentication, represented by a {{{Credentials}}} object.
569
  If no credentials were supplied when the object was created this attribute is {{{None}}}.
570
  This attribute is set on instantiation and is read-only.
571 47 Ruud Klaver
 '''contact_uri'''::
572
  The SIP URI to put in the {{{Contact}}} header of the request, in the form of a {{{SIPURI}}} object.
573
  If this was not specified, this attribute is {{{None}}}.
574
  It is set on instantiation and is read-only.
575
 '''call_id'''::
576
  The {{{Call-ID}}} to be used for this transaction as a string.
577
  If no call id was specified on instantiation, this attribute contains the generated id.
578
  This attribute is set on instantiation and is read-only.
579
 '''cseq'''::
580
  The sequence number to use in the request as an int.
581
  If no sequence number was specified on instantiation, this attribute contains the generated sequence number.
582
  Note that this number may be increased by one if an authentication challenge is received and a {{{Credentials}}} object is given.
583
  This attribute is read-only.
584
 '''extra_headers'''::
585
  Extra headers to include in the request as a dictionary.
586
  This attribute is set on instantiation and is read-only.
587
 '''content_type'''::
588
  What string to put as content type in the {{{Content-Type}}} headers, if the request contains a body.
589
  If no body was specified, this attribute is {{{None}}}
590
  It is set on instantiation and is read-only.
591
 '''body'''::
592
  The body of the request as a string.
593
  If no body was specified, this attribute is {{{None}}}
594
  It is set on instantiation and is read-only.
595
 '''expires_in'''::
596 1 Adrian Georgescu
  This read-only property indicates in how many seconds from now this {{{Request}}} will expire, if it is in the {{{EXPIRING}}} state.
597 47 Ruud Klaver
  If this is not the case, this property is 0.
598
599 46 Ruud Klaver
==== methods ====
600
601 62 Ruud Klaver
 '''!__init!__'''(''self'', '''method''', '''from_uri''', '''to_uri''', '''request_uri''', '''route''', '''credentials'''={{{None}}}, '''contact_uri'''={{{None}}}, '''call_id'''={{{None}}}, '''cseq'''={{{None}}}, '''extra_headers'''={{{None}}}, '''content_type'''={{{None}}}, '''body'''={{{None}}})::
602 48 Ruud Klaver
  Creates a new {{{Request}}} object in the {{{INIT}}} state.
603
  The arguments to this method are documented in the attributes section.
604
 '''send'''(''self'', '''timeout'''={{{None}}})::
605
   Compose the SIP request and send it to the destination.
606
   This moves the {{{Request}}} object into the {{{IN_PROGRESS}}} state.
607
  [[BR]]''timeout'':[[BR]]
608
  This can be either an int or a float, indicating in how many seconds the request should timeout with an internally generated 408 response.
609
  This is is {{{None}}}, the internal 408 is only triggered by the internal PJSIP transaction timeout.
610
  Note that, even if the timeout is specified, the PJSIP timeout is also still valid.
611 57 Ruud Klaver
 '''end'''(''self'')::
612 48 Ruud Klaver
  Terminate the transaction, whichever state it is in, sending the appropriate notifications.
613
  Note that calling this method while in the {{{INIT}}} state does nothing.
614
615 46 Ruud Klaver
==== notifications ====
616 49 Ruud Klaver
617
 '''SIPRequestGotProvisionalResponse'''::
618
  This notification will be sent on every provisional response received in reply to the sent request.
619
  [[BR]]''timestamp'':[[BR]]
620
  A {{{datetime.datetime}}} object indicating when the notification was sent.
621
  [[BR]]''code'':[[BR]]
622
  The SIP response code of the received provisional response as an int, which will be in the 1xx range.
623
  [[BR]]''reason'':[[BR]]
624
  The reason text string included with the SIP response code.
625
  [[BR]]''headers'':[[BR]]
626
  The headers included in the provisional response as a dictionary.
627
  [[BR]]''body'':[[BR]]
628
  The body of the provisional response as a string, or {{{None}}} if there was no body.
629
 '''SIPRequestDidSucceed'''::
630
  This notification will be sent when a positive final response was received in reply to the request.
631
  [[BR]]''timestamp'':[[BR]]
632
  A {{{datetime.datetime}}} object indicating when the notification was sent.
633
  [[BR]]''code'':[[BR]]
634
  The SIP response code of the received positive final response as an int, which will be in the 2xx range.
635
  [[BR]]''reason'':[[BR]]
636
  The reason text string included with the SIP response code.
637
  [[BR]]''headers'':[[BR]]
638
  The headers included in the positive final response as a dictionary.
639
  [[BR]]''body'':[[BR]]
640
  The body of the positive final response as a string, or {{{None}}} if there was no body.
641
  [[BR]]''expires'':[[BR]]
642
  Indicates in how many seconds the {{{Request}}} will expire as an int.
643
  If it does not expire and the {{{EXPIRING}}} state is skipped, this attribute is 0.
644
 '''SIPRequestDidFail'''::
645
  This notification will be sent when a negative final response is received in reply to the request or if an internal error occurs.
646
  [[BR]]''timestamp'':[[BR]]
647
  A {{{datetime.datetime}}} object indicating when the notification was sent.
648
  [[BR]]''code'':[[BR]]
649
  The SIP response code of the received negative final response as an int.
650
  This could also be a response code generated by PJSIP internally, or 0 when an internal error occurs.
651
  [[BR]]''reason'':[[BR]]
652
  The reason text string included with the SIP response code or error.
653
  [[BR]]''headers'': (only if a negative final response was received)[[BR]]
654
  The headers included in the negative final response as a dictionary, if this is what triggered the notification.
655
  [[BR]]''body'': (only if a negative final response was received)[[BR]]
656
  The body of the negative final response as a string, or {{{None}}} if there was no body.
657
  This attribute is absent if no response was received.
658
 '''SIPRequestWillExpire'''::
659
  This notification will be sent when a {{{Request}}} in the {{{EXPIRING}}} state will expire soon.
660
  [[BR]]''timestamp'':[[BR]]
661
  A {{{datetime.datetime}}} object indicating when the notification was sent.
662
  [[BR]]''expires'':[[BR]]
663
  An int indicating in how many seconds from now the {{{Request}}} will actually expire.
664
 '''SIPRequestDidEnd'''::
665
  This notification will be sent when a {{{Request}}} object enters the {{{TERMINATED}}} state and can no longer be used.
666
  [[BR]]''timestamp'':[[BR]]
667
  A {{{datetime.datetime}}} object indicating when the notification was sent.
668 46 Ruud Klaver
669 51 Ruud Klaver
=== Message ===
670
671
The {{{sipsimple.primitives.Message}}} class is a simple wrapper for the {{{Request}}} class, with the purpose of sending {{{MESSAGE}}} requests, as described in [http://tools.ietf.org/html/rfc3428 RFC 3428].
672
It is a one-shot object that manages only one {{{Request}}} object.
673
674
==== attributes ====
675
676 64 Ruud Klaver
 '''from_uri'''::
677
  The SIP URI to put in the {{{From}}} header of the {{{MESSAGE}}} request, in the form of a {{{SIPURI}}} object.
678 51 Ruud Klaver
  This attribute is set on instantiation and is read-only.
679
 '''to_uri'''::
680
  The SIP URI to put in the {{{To}}} header of the {{{MESSAGE}}} request, in the form of a {{{SIPURI}}} object.
681
  This attribute is set on instantiation and is read-only.
682
 '''route'''::
683
  Where to send the {{{MESSAGE}}} request to, including IP, port and transport, in the form of a {{{SIPURI}}} object.
684
  This will also be included in the {{{Route}}} header of the request.
685
  This attribute is set on instantiation and is read-only.
686
 '''content_type'''::
687
  What string to put as content type in the {{{Content-Type}}} headers.
688
  It is set on instantiation and is read-only.
689
 '''body'''::
690
  The body of the {{{MESSAGE}}} request as a string.
691 1 Adrian Georgescu
  If no body was specified, this attribute is {{{None}}}
692
  It is set on instantiation and is read-only.
693 64 Ruud Klaver
 '''credentials'''::
694
  The credentials to be used when challenged for authentication, represented by a {{{Credentials}}} object.
695
  If no credentials were specified, this attribute is {{{None}}}.
696
  This attribute is set on instantiation and is read-only.
697 51 Ruud Klaver
 '''is_sent'''::
698
  A boolean read-only property indicating if the {{{MESSAGE}}} request was sent.
699
 '''in_progress'''::
700
  A boolean read-only property indicating if the object is waiting for the response from the remote party.
701
702
==== methods ====
703
704
 '''!__init!__'''(''self'', '''credentials''', '''to_uri''', '''route''', '''content_type''', '''body''')::
705
  Creates a new {{{Message}}} object with the specified arguments.
706
  These arguments are documented in the attributes section for this class.
707
 '''send'''(''self'', '''timeout'''={{{None}}})::
708
  Send the {{{MESSAGE}}} request to the remote party.
709
  [[BR]]''timeout'':[[BR]]
710
  This argument as the same meaning as it does for {{{Request.send()}}}.
711
 '''end'''(''self'')::
712
  Stop trying to send the {{{MESSAGE}}} request.
713
  If it was not sent yet, calling this method does nothing.
714
715
==== notifications ====
716
717
 '''SIPMessageDidSucceed'''::
718
  This notification will be sent when the remote party acknowledged the reception of the {{{MESSAGE}}} request.
719 52 Ruud Klaver
  It has not data attributes.
720 51 Ruud Klaver
 '''SIPMessageDidFail'''::
721
  This notification will be sent when transmission of the {{{MESSAGE}}} request fails for whatever reason.
722
  [[BR]]''code'':[[BR]]
723 1 Adrian Georgescu
  An int indicating the SIP or internal PJSIP code that was given in response to the {{{MESSAGE}}} request.
724
  This is 0 if the failure is caused by an internal error.
725
  [[BR]]''reason'':[[BR]]
726
  A status string describing the failure, either taken from the SIP response or the internal error.
727 52 Ruud Klaver
728
=== Registration ===
729
730
The {{{sipsimple.primitives.Registration}}} class wraps a series of {{{Request}}} objects, managing the registration of a particular contact URI at a SIP registrar through the sending of {{{REGISTER}}} requests.
731
For details, see [http://tools.ietf.org/html/rfc3261 RFC 3261].
732
This object is designed in such a way that it will not initiate sending a refreshing {{{REGISTER}}} itself, rather it will inform the application that a registration is about to expire.
733 1 Adrian Georgescu
The application should then perform a DNS lookup to find the relevant SIP registrar and call the {{{register()}}} method on this object.
734
735 52 Ruud Klaver
==== attributes ====
736 64 Ruud Klaver
 '''uri''::
737
  The SIP URI of the account the {{{Registration}}} is for in the form of a {{{SIPURI}}} object.
738 52 Ruud Klaver
 '''credentials'''::
739 64 Ruud Klaver
  The credentials to be used when challenged for authentication by the registrar, represented by a {{{Credentials}}} object. 
740
  This attribute is set at instantiation, can be {{{None}}} if it was not specified and can be updated to be used for the next {{{REGISTER}}} request.
741 52 Ruud Klaver
  Note that, in contrast to other classes mentioned in this document, the {{{Registration}}} class does not make a copy of the {{{Credentials}}} object on instantiation, but instead retains a reference to it.
742
 '''duration'''::
743
  The amount of time in seconds to request the registration for at the registrar.
744
  This attribute is set at object instantiation and can be updated for subsequent {{{REGISTER}}} requests.
745
 '''is_registered'''::
746
  A boolean read-only property indicating if this object is currently registered.
747
 '''contact_uri'''::
748
  If the {{{Registration}}} object is registered, this attribute contains the latest contact URI that was sent to the registrar as a {{{SIPURI}}} object.
749 1 Adrian Georgescu
  Otherwise, this attribute is {{{None}}}
750 52 Ruud Klaver
 '''expires_in'''::
751
  If registered, this read-only property indicates in how many seconds from now this {{{Registration}}} will expire.
752
  If this is not the case, this property is 0.
753
754
==== methods ====
755
756 64 Ruud Klaver
 '''!__init!__'''(''self'', '''uri''', '''credentials'''={{{None}}}, '''duration'''=300)::
757 52 Ruud Klaver
  Creates a new {{{Registration}}} object with the specified arguments.
758
  These arguments are documented in the attributes section for this class.
759
 '''register'''(''self'', '''contact_uri''', '''route''', '''timeout'''={{{None}}})::
760
  Calling this method will attempt to send a new {{{REGISTER}}} request to the registrar provided, whatever state the object is in.
761
  If the object is currently busy sending a {{{REGISTER}}}, this request will be preempted for the new one.
762
  Once sent, the {{{Registration}}} object will send either a {{{SIPRegistrationDidSucceed}}} or a {{{SIPRegistrationDidFail}}} notification.
763 58 Ruud Klaver
  The {{{contact_uri}}} argument is mentioned in the attributes section of this class.
764 52 Ruud Klaver
  The {{{route}}} argument specifies the IP address, port and transport of the SIP registrar in the form of a {{{Route}}} object and {{{timeout}}} has the same meaning as it does for {{{Request.send()}}}.
765
 '''end'''(''self'', '''timeout'''={{{None}}})::
766
  Calling this method while the object is registered will attempt to send a {{{REGISTER}}} request with the {{{Expires}}} headers set to 0, effectively unregistering the contact URI at the registrar.
767
  The {{{Route}}} used for this will be the same as the last successfully sent {{{REGISTER}}} request.
768
  If another {{{REGISTER}}} is currently being sent, it will be preempted.
769
  When the unregstering {{{REGISTER}}} request is sent, a {{{SIPRegistrationWillEnd}}} notification is sent.
770
  After this, either a {{{SIPRegistrationDidEnd}}} with the {{{expired}}} data attribute set to {{{False}}} will be sent, indicating a successful unregister, or a {{{SIPRegistrationDidNotEnd}}} notification is send if unregistering fails for some reason.
771
  Calling this method when the object is not registered will do nothing.
772
  The {{{timeout}}} argument has the same meaning as for the {{{register()}}} method.
773
774
==== notifications ====
775
776
 '''SIPRegistrationDidSucceed'''::
777
  This notification will be sent when the {{{register()}}} method was called and the registration succeeded.
778
  [[BR]]''code'':[[BR]]
779
  The SIP response code as received from the registrar as an int.
780
  [[BR]]''reason'':[[BR]]
781
  The reason string received from the SIP registrar.
782
  [[BR]]''route'':[[BR]]
783
  The {{{Route}}} object passed as an argument to the {{{register()}}} method.
784
  [[BR]]''contact_uri'':[[BR]]
785
  The contact URI that was sent to the registrar as a {{{SIPURI}}} object.
786
  [[BR]]''contact_uri_list'':[[BR]]
787
  A full list of contact URIs that are registered for this SIP account, including the one used for this registration.
788
  The format of this data attribute is a list of 2-item tuples.
789
  The first item is a SIP URI indicating the contact URI, the second item is a dictionary of additional parameters, including the {{{expires}}} parameter.
790
  [[BR]]''expires_in'':[[BR]]
791
  The number of seconds before this registration expires
792
 '''SIPRegistrationDidFail'''::
793
  This notification will be sent when the {{{register()}}} method was called and the registration failed, for whatever reason.
794
  [[BR]]''code'':[[BR]]
795
  The SIP response code as received from the registrar as an int.
796
  This can also be a PJSIP generated response code, or 0 if the failure was because of an internal error.
797
  [[BR]]''reason'':[[BR]]
798
  The reason string received from the SIP registrar or the error string.
799
  [[BR]]''route'':[[BR]]
800
  The {{{Route}}} object passed as an argument to the {{{register()}}} method.
801
 '''SIPRegistrationWillExpire'''::
802
  This notification will be sent when a registration will expire soon.
803
  It should be used as an indication to re-perform DNS lookup of the registrar and call the {{{register()}}} method.
804
  [[BR]]''expires_in'':[[BR]]
805 58 Ruud Klaver
  The number of seconds in which the registration will expire.
806 52 Ruud Klaver
 '''SIPRegistrationWillEnd'''::
807
  Will be sent whenever the {{{end()}}} method was called and an unregistering {{{REGISTER}}} is sent.
808
 '''SIPRegistrationDidNotEnd'''::
809
  This notification will be sent when the unregistering {{{REGISTER}}} request failed for some reason.
810
  [[BR]]''code'':[[BR]]
811
  The SIP response code as received from the registrar as an int.
812
  This can also be a PJSIP generated response code, or 0 if the failure was because of an internal error.
813
  [[BR]]''reason'':[[BR]]
814
  The reason string received from the SIP registrar or the error string.
815
 '''SIPRegistrationDidEnd'''::
816
  This notification will be sent when a {{{Registration}}} has become unregistered.
817 58 Ruud Klaver
  [[BR]]''expired'':[[BR]]
818 52 Ruud Klaver
  This boolean indicates if the object is unregistered because the registration expired, in which case it will be set to {{{True}}}.
819
  If this data attribute is {{{False}}}, it means that unregistration was explicitly requested through the {{{end()}}} method.
820
821
==== example code ====
822 51 Ruud Klaver
823 1 Adrian Georgescu
For an example on how to use this object, see the Integration section.
824
825
=== Publication ===
826
827
Publication of SIP events is an Internet standard published at [http://tools.ietf.org/html/rfc3903 RFC 3903]. 
828
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.
829
830
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.
831 2 Adrian Georgescu
The state machine of this object is very similar to that of the {{{Registration}}} object, as can be seen in the following diagram:
832 1 Adrian Georgescu
833
[[Image(sipsimple-publication-state-machine.png, nolink)]]
834
835
State changes are triggered by the following events:
836
 1. The initial state.
837
 2. User requests in the form of the {{{publish()}}} and {{{unpublish()}}} methods.
838
 3. A final response for a {{{PUBLISH}}} is received from the network.
839
 4. The refresh timer expires.
840
Like the {{{Registration}}} state machine, the {{{Publication}}} state machine is queued.
841
This means that the application may call either the {{{publish()}}} or {{{unpublish()}}} method at any point in the state machine.
842
The object will perform the requested action when ready.
843
When some content is published and the application wants to update the contents, it can directly call the {{{publish()}}} method with the new content.
844
845
> The implementation of this object needs to be revised.
846
847
> If this object is re-used after unpublication, the etag value is not reset by PJSIP.
848
> This needs to be fixed.
849
850
==== attributes ====
851
852
 '''state'''::
853
  Indicates which state the internal state machine is in.
854
  This is one of {{{unpublished}}}, {{{publishing}}}, {{{published}}}, {{{unpublishing}}}.
855
 '''credentials'''::
856
  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.
857
  This attribute is set on object instantiation and is read-only.
858
 '''route'''::
859
  The outbound proxy to use in the form of a {{{Route}}} object.
860
  This attribute is set on object instantiation and is read-only.
861
 '''extra_headers'''::
862
  A dictionary of extra headers that should be added to any outgoing {{{PUBLISH}}} request.
863
  This attribute is set on object instantiation and is read-only.
864
 '''expires'''::
865
  The amount of seconds the contents of the {{{PUBLISH}}} are valid, i.e. the value that should be put in the {{{Expires}}} header.
866
  This attribute is set on object instantiation and can be modified at runtime.
867
  A new value will be used during the next refreshing {{{PUBLISH}}}.
868
869 23 Ruud Klaver
==== methods ====
870 1 Adrian Georgescu
871
 '''!__init!__'''(''self'', '''credentials''', '''event''', '''route''', '''expires'''=300, '''extra_headers'''={{{None}}})::
872
  Creates a new {{{Publication}}} object.
873
  [[BR]]''credentials'':[[BR]]
874
  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.
875
  [[BR]]''event'':[[BR]]
876
  The event package for which we want to publish content.
877
  [[BR]]''route'':[[BR]]
878
  The outbound proxy to use in the form of a {{{Route}}} object
879
  [[BR]]''expires'':[[BR]]
880
  The amount of seconds the contents of the {{{PUBLISH}}} are valid, i.e. the value that should be put in the {{{Expires}}} header.
881
  [[BR]]''extra_headers'':[[BR]]
882
  A dictionary of extra headers that should be added to any outgoing {{{PUBLISH}}} request.
883
 '''publish'''(''self'', '''content_type''', '''content_subtype''', '''body''')::
884
  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.
885
  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).
886
  If it is unsuccessful the state machine reverts to the {{{unregistered}}} state.
887
  [[BR]]''content_type'':[[BR]]
888
  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.
889
  [[BR]]''content_subtype'':[[BR]]
890
  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.
891
 '''unpublish'''(''self'')::
892
  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.
893
894 29 Luci Stanescu
==== notifications ====
895 1 Adrian Georgescu
896
 '''SIPPublicationChangedState'''::
897
  This notification will be sent every time the internal state machine of a {{{Publication}}} object changes state.
898
  [[BR]]''timestamp'':[[BR]]
899
  A {{{datetime.datetime}}} object indicating when the notification was sent.
900
  [[BR]]''state'':[[BR]]
901
  The new state the state machine moved into.
902
  [[BR]]''code'': (only on SIP response triggered state change)[[BR]]
903
  The status code of the response that caused the state change.
904
  This may be internally generated by PJSIP, e.g. on timeout.
905
  [[BR]]''reason'': (only on SIP response triggered state change)[[BR]]
906
  The status text of the response that caused the state change.
907
  This may be internally generated by PJSIP, e.g. on timeout.
908
909
> On init the event package is not checked with known event packages, this is only used for {{{Subscription}}} objects.
910
> This could be done for the sake of consistency.
911
912
==== example code ====
913
914
This code shows how to make a {{{Publication}}} object for a particular SIP account and have it attempt to publish its presence.
915
916
{{{
917 22 Ruud Klaver
accnt = SIPURI(user="username", host="domain.com")
918 1 Adrian Georgescu
creds = Credentials(accnt, "password")
919
pub = Publication(creds, "presence", Route("1.2.3.4"))
920
pub.publish("text", "plain", "hi!")
921 29 Luci Stanescu
}}}
922 1 Adrian Georgescu
923
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.
924
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.
925
926
=== Subscription ===
927
928
Subscription and notifications for SIP events is an Internet standard published at [http://tools.ietf.org/html/rfc3856 RFC 3856].
929
930
This SIP primitive class represents a subscription to a specific event type of a particular SIP URI.
931 59 Ruud Klaver
This means that the application should instance this class for each combination of event and SIP URI that it wishes to subscribe to.
932 1 Adrian Georgescu
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.
933
Whenever a {{{NOTIFY}}} is received, the application will receive the {{{SIPSubscriptionGotNotify}}} event.
934
935
Internally a {{{Subscription}}} object has a state machine, which reflects the state of the subscription.
936
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}}}.
937 59 Ruud Klaver
The last three states are directly copied from the contents of the {{{Subscription-State}}} header of the received {{{NOTIFY}}} request.
938
Also, the state can be an arbitrary string if the contents of the {{{Subscription-State}}} header are not one of the three above.
939 1 Adrian Georgescu
The state of the object is presented in the {{{state}}} attribute and changes of the state are indicated by means of the {{{SIPSubscriptionChangedState}}} notification.
940
This notification is only informational, an application using this object should take actions based on the other notifications sent by this object.
941
942
==== attributes ====
943
944
 '''state'''::
945 59 Ruud Klaver
  Indicates which state the internal state machine is in.
946
  See the previous section for a list of states the state machine can be in.
947 1 Adrian Georgescu
 '''from_uri'''::
948
  The SIP URI to be put in the {{{From}}} header of the {{{SUBCRIBE}}} requests in the form of a {{{SIPURI}}} object.
949 59 Ruud Klaver
  This attribute is set on object instantiation and is read-only.
950 1 Adrian Georgescu
 '''to_uri'''::
951 59 Ruud Klaver
  The SIP URI that is the target for the subscription in the form of a {{{SIPURI}}} object.
952
  This attribute is set on object instantiation and is read-only.
953
 '''contact_uri'''::
954 1 Adrian Georgescu
  The SIP URI to be put in the {{{Contact}}} header of the {{{SUBCRIBE}}} requests in the form of a {{{SIPURI}}} object.
955 59 Ruud Klaver
  This attribute is set on object instantiation and is read-only.
956 1 Adrian Georgescu
 '''event'''::
957
  The event package for which the subscription is as a string.
958
  This attribute is set on object instantiation and is read-only.
959
 '''route'''::
960 59 Ruud Klaver
  The outbound proxy to use in the form of a {{{Route}}} object.
961
  This attribute is set on object instantiation and is read-only.
962
 '''credentials'''::
963 1 Adrian Georgescu
  The SIP credentials needed to authenticate at the SIP proxy in the form of a {{{Credentials}}} object.
964 59 Ruud Klaver
  If none was specified when creating the {{{Subscription}}} object, this attribute is {{{None}}}.
965
  This attribute is set on object instantiation and is read-only.
966
 '''refresh'''::
967
  The refresh interval in seconds that was requested on object instantiation as an int.
968
  This attribute is set on object instantiation and is read-only.
969
 '''extra_headers'''::
970
  This contains the dictionary of extra headers that was last passed to a successful call to the {{{subscribe()}}} method.
971
  If the argument was not provided, this attribute is an empty dictionary.
972
  This attribute is read-only.
973
 '''content_type'''::
974
  This attribute contains the {{{Content-Type}}} of the body that was last passed to a successful call to the {{{subscribe()}}} method.
975
  If the argument was not provided, this attribute is {{{None}}}.
976
 '''body'''::
977 1 Adrian Georgescu
  This attribute contains the {{{body}}} string argument that was last passed to a successful call to the {{{subscribe()}}} method.
978
  If the argument was not provided, this attribute is {{{None}}}.
979
980 59 Ruud Klaver
==== methods ====
981
982
 '''!__init!__'''(''self'', '''from_uri''', '''to_uri''', '''contact_uri, '''event''', '''route''', '''credentials'''={{{None}}}, '''refresh'''=300)::
983
  Creates a new {{{Subscription}}} object which will be in the {{{NULL}}} state.
984
  The arguments for this method are documented in the attributes section above.
985
 '''subscribe'''(''self'', '''extra_headers'''={{{None}}}, '''content_type'''={{{None}}}, '''body'''={{{None}}}, '''timeout'''={{{None}}})::
986
  Calling this method triggers sending a {{{SUBSCRIBE}}} request to the presence agent.
987
  The arguments passed will be stored and used for subsequent refreshing {{{SUBSCRIBE}}} requests.
988 1 Adrian Georgescu
  This method may be used both to send the initial request and to update the arguments while the subscription is ongoing.
989 59 Ruud Klaver
  It may not be called when the object is in the {{{TERMINATED}}} state.
990
  [[BR]]''extra_headers'':[[BR]]
991
  A dictionary of additional headers to include in the {{{SUBSCRIBE}}} requests.
992
  [[BR]]''content_type'':[[BR]]
993
  The {{{Content-Type}}} of the supplied {{{body}}} argument as a string.
994
  If this argument is supplied, the {{{body}}} argument cannot be {{{None}}}.
995
  [[BR]]''body'':[[BR]]
996
  The optional body to include in the {{{SUBSCRIBE}}} request as a string.
997
  If this argument is supplied, the {{{content_type}}} argument cannot be {{{None}}}.
998
  [[BR]]''timeout'':[[BR]]
999
  This can be either an int or a float, indicating in how many seconds the request should timeout with an internally generated 408 response.
1000
  If this is {{{None}}}, the internal 408 is only triggered by the internal PJSIP transaction timeout.
1001
  Note that, even if the timeout is specified, the PJSIP timeout is also still valid.
1002
 '''end'''(''self'', '''timeout'''={{{None}}})::
1003
  This will end the subscription by sending a {{{SUBSCRIBE}}} request with an {{{Expires}}} value of 0.
1004
  If this object is no longer subscribed, this method will return without performing any action.
1005 1 Adrian Georgescu
  This method cannot be called when the object is in the {{{NULL}}} state.
1006
  The {{{timeout}}} argument has the same meaning as it does for the {{{subscribe()}}} method.
1007
1008
==== notifications ====
1009
1010
 '''SIPSubscriptionChangedState'''::
1011
  This notification will be sent every time the internal state machine of a {{{Subscription}}} object changes state.
1012 59 Ruud Klaver
  [[BR]]''timestamp'':[[BR]]
1013
  A {{{datetime.datetime}}} object indicating when the notification was sent.
1014 1 Adrian Georgescu
  [[BR]]''prev_state'':[[BR]]
1015
  The previous state that the sate machine was in.
1016 59 Ruud Klaver
  [[BR]]''state'':[[BR]]
1017
  The new state the state machine moved into.
1018
 '''SIPSubscriptionWillStart'''::
1019
  This notification will be emitted when the initial {{{SUBSCRIBE}}} request is sent.
1020
  [[BR]]''timestamp'':[[BR]]
1021
  A {{{datetime.datetime}}} object indicating when the notification was sent.
1022
 '''SIPSubscriptionDidStart'''::
1023
  This notification will be sent when the initial {{{SUBSCRIBE}}} was accepted by the presence agent.
1024 29 Luci Stanescu
  [[BR]]''timestamp'':[[BR]]
1025
  A {{{datetime.datetime}}} object indicating when the notification was sent.
1026 59 Ruud Klaver
 '''SIPSubscriptionGotNotify'''::
1027 1 Adrian Georgescu
  This notification will be sent when a {{{NOTIFY}}} is received that corresponds to a particular {{{Subscription}}} object.
1028
  Note that this notification could be sent when a {{{NOTIFY}}} without a body is received.
1029 59 Ruud Klaver
  [[BR]]''timestamp'':[[BR]]
1030
  A {{{datetime.datetime}}} object indicating when the notification was sent.
1031
  [[BR]]''method'':[[BR]]
1032
  The method of the SIP request as a string.
1033
  This will always be {{{NOTIFY}}}.
1034
  [[BR]]''request_uri'':[[BR]]
1035
  The request URI of the {{{NOTIFY}}} request as a {{{SIPURI}}} object.
1036
  [[BR]]''headers'':[[BR]]
1037
  The headers of the {{{NOTIFY}}} request as a dict.
1038 1 Adrian Georgescu
  Each SIP header is represented in its parsed for as long as PJSIP supports it.
1039 59 Ruud Klaver
  The format of the parsed value depends on the header.
1040
  [[BR]]''body'':[[BR]]
1041
  The body of the {{{NOTIFY}}} request as a string, or {{{None}}} if no body was included.
1042
  The content type of the body can be learned from the {{{Content-Type}}} header in the headers data attribute.
1043
 '''SIPSubscriptionDidFail'''::
1044
  This notification will be sent whenever there is a failure, such as a rejected initial or refreshing {{{SUBSCRIBE}}}.
1045
  After this notification the {{{Subscription}}} object is in the {{{TERMINATED}}} state and can no longer be used.
1046
  [[BR]]''timestamp'':[[BR]]
1047
  A {{{datetime.datetime}}} object indicating when the notification was sent.
1048
  [[BR]]''code'':[[BR]]
1049
  An integer SIP code from the fatal response that was received from the remote party of generated by PJSIP.
1050
  If the error is internal to the SIP core, this attribute will have a value of 0.
1051
  [[BR]]''reason'':[[BR]]
1052
  An text string describing the failure that occured.
1053
 '''SIPSubscriptionDidEnd'''::
1054
  This notification will be sent when the subscription ends normally, i.e. the {{{end()}}} method was called and the remote party sent a positive response.
1055
  After this notification the {{{Subscription}}} object is in the {{{TERMINATED}}} state and can no longer be used.
1056 1 Adrian Georgescu
  [[BR]]''timestamp'':[[BR]]
1057 56 Ruud Klaver
  A {{{datetime.datetime}}} object indicating when the notification was sent.
1058 1 Adrian Georgescu
1059
=== Invitation ===
1060
1061 29 Luci Stanescu
The {{{Invitation}}} class represents an {{{INVITE}}} session, which governs a complete session of media exchange between two SIP endpoints from start to finish.
1062 1 Adrian Georgescu
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.
1063
The {{{Invitation}}} class represents both incoming and outgoing sessions.
1064
1065 29 Luci Stanescu
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.
1066 1 Adrian Georgescu
In order to represent re-{{{INVITE}}}s and user-requested disconnections, three more states have been added to this state machine.
1067
The progression through this state machine is fairly linear and is dependent on whether this is an incoming or an outgoing session.
1068
State changes are triggered either by incoming or by outgoing SIP requests and responses.
1069
The states and the transitions between them are shown in the following diagram:
1070
1071
[[Image(sipsimple-core-invite-state-machine.png, nolink)]]
1072
1073
The state changes of this machine are triggered by the following:
1074
 1. An {{{Invitation}}} object is newly created, either by the application for an outgoing session, or by the core for an incoming session.
1075
 2. The application requested an outgoing session by calling the {{{send_invite()}}} method and and initial {{{INVITE}}} request is sent.
1076
 3. A new incoming session is received by the core.
1077
    The application should look out for state change to this state in order to be notified of new incoming sessions.
1078
 4. A provisional response (1xx) is received from the remove party.
1079
 5. A provisional response (1xx) is sent to the remote party, after the application called the {{{respond_to_invite_provisionally()}}} method.
1080
 6. A positive final response (2xx) is received from the remote party.
1081
 7. A positive final response (2xx) is sent to the remote party, after the application called the {{{accept_invite()}}} method.
1082
 8. A positive final response (2xx) is sent or received, depending on the orientation of the session.
1083
 9. An {{{ACK}}} is sent or received, depending on the orientation of the session.
1084
    If the {{{ACK}}} is sent from the local to the remote party, it is initiated by PJSIP, not by a call from the application.
1085
 10. The local party sent a re-{{{INVITE}}} to the remote party by calling the {{{send_reinvite()}}} method.
1086
 11. The remote party has sent a final response to the re-{{{INVITE}}}.
1087
 12. The remote party has sent a re-{{{INVITE}}}.
1088
 13. The local party has responded to the re-{{{INVITE}}} by calling the {{{respond_to_reinvite()}}} method.
1089
 14. The application requests that the session ends by calling the {{{end()}}} method.
1090
 15. A response is received from the remote party to whichever message was sent by the local party to end the session.
1091
 16. A message is received from the remote party which ends the session.
1092
1093
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.
1094
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.
1095
The application should compare the previous and current states and perform the appropriate action.
1096
1097
An {{{Invitiation}}} object also emits the {{{SIPInvitationGotSDPUpdate}}} notification, which indicates that SDP negotiation between the two parties has been completed.
1098
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.
1099
In the last case, the {{{Invitation}}} object will automatically include the current local SDP in the response.
1100
1101
==== attributes ====
1102
1103
 '''state'''::
1104 33 Ruud Klaver
  The state the {{{Invitation}}} state machine is currently in.
1105
  See the diagram above for possible states.
1106
  This attribute is read-only.
1107
 '''is_outgoing'''::
1108
  Boolean indicating if the original {{{INVITE}}} was outgoing, or incoming if set to {{{False}}}.
1109 1 Adrian Georgescu
 '''credentials'''::
1110 63 Ruud Klaver
  The SIP credentials needed to authenticate at the SIP proxy in the form of a {{{Credentials}}} object.
1111 1 Adrian Georgescu
  If this {{{Invitation}}} object represents an incoming {{{INVITE}}} session this attribute will be {{{None}}}.
1112 60 Ruud Klaver
  On an outgoing session this attribute will be {{{None}}} if it was not specified when the object was created.
1113 1 Adrian Georgescu
  This attribute is set on object instantiation and is read-only.
1114 63 Ruud Klaver
 '''from_uri'''::
1115 1 Adrian Georgescu
  The SIP URI of the caller represented by a {{{SIPURI}}} object.
1116 22 Ruud Klaver
  If this is an outgoing {{{INVITE}}} session, this is the from_uri from the !__init!__ method.
1117 60 Ruud Klaver
  Otherwise the URI is taken from the {{{From:}}} header of the initial {{{INVITE}}}.
1118 1 Adrian Georgescu
  This attribute is set on object instantiation and is read-only.
1119 60 Ruud Klaver
 '''to_uri'''::
1120 1 Adrian Georgescu
  The SIP URI of the callee represented by a {{{SIPURI}}} object.
1121
  If this is an outgoing {{{INVITE}}} session, this is the to_uri from the !__init!__ method.
1122
  Otherwise the URI is taken from the {{{To:}}} header of the initial {{{INVITE}}}.
1123 33 Ruud Klaver
  This attribute is set on object instantiation and is read-only.
1124 60 Ruud Klaver
 '''local_uri'''::
1125 33 Ruud Klaver
  The local SIP URI used in this session.
1126 1 Adrian Georgescu
  If the original {{{INVITE}}} was incoming, this is the same as {{{to_uri}}}, otherwise it will be the same as {{{from_uri}}}.
1127 60 Ruud Klaver
 '''remote_uri'''::
1128 1 Adrian Georgescu
  The SIP URI of the remote party in this session.
1129
  If the original {{{INVITE}}} was incoming, this is the same as {{{from_uri}}}, otherwise it will be the same as {{{to_uri}}}.
1130
 '''route'''::
1131
  The outbound proxy that was requested to be used in the form of a {{{Route}}} object, including the desired transport.
1132
  If this {{{Invitation}}} object represents an incoming {{{INVITE}}} session this attribute will always be {{{None}}}.
1133
  This attribute is set on object instantiation and is read-only.
1134
 '''call_id'''::
1135
  The call ID of the {{{INVITE}}} session as a read-only string.
1136
  In the {{{NULL}}} and {{{DISCONNECTED}}} states, this attribute is {{{None}}}.
1137
 '''transport'''::
1138
  A string indicating the transport used for the application.
1139
  This can be "udp", "tcp" or "tls".
1140
 '''local_contact_uri'''::
1141
  The Contact URI that the local side provided to the remote side within this {{{INVITE}}} session as a {{{SIPURI}}} object.
1142 60 Ruud Klaver
1143 63 Ruud Klaver
==== methods ====
1144 1 Adrian Georgescu
1145 63 Ruud Klaver
 '''!__init!__'''(''self'', '''from_uri''', '''to_uri''', '''route''', '''credentials'''={{{None}}}, '''contact_uri'''={{{None}}})::
1146
  Creates a new {{{Invitation}}} object for an outgoing session.
1147 1 Adrian Georgescu
  [[BR]]''from_uri'':[[BR]]
1148
  The SIP URI of the local account in the form of a {{{SIPURI}}} object.
1149 60 Ruud Klaver
  [[BR]]''to_uri'':[[BR]]
1150 53 Ruud Klaver
  The SIP URI we want to send the {{{INVITE}}} to, represented as a {{{SIPURI}}} object.
1151 1 Adrian Georgescu
  [[BR]]''route'':[[BR]]
1152 63 Ruud Klaver
  The outbound proxy to use in the form of a {{{Route}}} object.
1153
  This includes the desired transport to use.
1154 1 Adrian Georgescu
  [[BR]]''credentials'':[[BR]]
1155
  The optional SIP credentials needed to authenticate at the SIP proxy in the form of a {{{Credentials}}} object.
1156
  [[BR]]''contact_uri'':[[BR]]
1157
  The Contact URI to include in the {{{INVITE}}} request, a {{{SIPURI}}} object.
1158
  If this is {{{None}}}, a Contact URI will be internally generated.
1159
 '''get_active_local_sdp'''(''self'')::
1160
  Returns a new {{{SDPSession}}} object representing the currently active local SDP.
1161
  If no SDP was negotiated yet, this returns {{{None}}}.
1162
 '''get_active_remote_sdp'''(''self'')::
1163
  Returns a new {{{SDPSession}}} object representing the currently active local SDP.
1164 56 Ruud Klaver
  If no SDP was negotiated yet, this returns {{{None}}}.
1165 1 Adrian Georgescu
 '''get_offered_local_sdp'''(''self'')::
1166
  Returns a new {{{SDPSession}}} object representing the currently proposed local SDP.
1167
  If no local offered SDP has been set, this returns {{{None}}}.
1168
 '''set_offered_local_sdp'''(''self'', '''local_sdp''')::
1169
  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}}}.
1170
  [[BR]]''local_sdp'':[[BR]]
1171 41 Ruud Klaver
  The SDP to send as offer or answer to the remote party.
1172 1 Adrian Georgescu
 '''get_offered_remote_sdp'''(''self'')::
1173 41 Ruud Klaver
  Returns a new {{{SDPSession}}} object representing the currently proposed remote SDP.
1174 53 Ruud Klaver
  If no remote SDP has been offered in the current state, this returns {{{None}}}.
1175 33 Ruud Klaver
 '''send_invite'''(''self'', '''extra_headers'''={{{None}}}, '''timeout'''={{{None}}})::
1176 1 Adrian Georgescu
  This tries to move the state machine into the {{{CALLING}}} state by sending the initial {{{INVITE}}} request.
1177
  It may only be called from the {{{NULL}}} state on an outgoing {{{Invitation}}} object.
1178
  [[BR]]''extra_headers'':[[BR]]
1179
  Any extra headers that should be included in the {{{INVITE}}} request in the form of a dict.
1180
  [[BR]]''timeout'':[[BR]]
1181
  How many seconds to wait for the remote party to reply before changing the state to {{{DISCONNECTED}}} and internally replying with a 408, as an int or a float.
1182
  If this is set to {{{None}}}, the default PJSIP timeout will be used, which appears to be slightly longer than 30 seconds.
1183
 '''respond_to_invite_provisionally'''(''self'', '''response_code'''=180, '''extra_headers'''={{{None}}})::
1184
  This tries to move the state machine into the {{{EARLY}}} state by sending a provisional response to the initial {{{INVITE}}}.
1185
  It may only be called from the {{{INCOMING}}} state on an incoming {{{Invitation}}} object.
1186
  [[BR]]''response_code'':[[BR]]
1187
  The code of the provisional response to use as an int.
1188
  This should be in the 1xx range.
1189
  [[BR]]''extra_headers'':[[BR]]
1190
  Any extra headers that should be included in the response in the form of a dict.
1191
 '''accept_invite'''(''self'', '''extra_headers'''={{{None}}})::
1192 29 Luci Stanescu
  This tries to move the state machine into the {{{CONNECTING}}} state by sending a 200 OK response to the initial {{{INVITE}}}.
1193 1 Adrian Georgescu
  It may only be called from the {{{INCOMING}}} or {{{EARLY}}} states on an incoming {{{Invitation}}} object.
1194
  [[BR]]''extra_headers'':[[BR]]
1195
  Any extra headers that should be included in the response in the form of a dict.
1196
 '''end'''(''self'', '''response_code'''=603, '''extra_headers'''={{{None}}}, '''timeout'''={{{None}}})::
1197
  This moves the {{{INVITE}}} state machine into the {{{DISCONNECTING}}} state by sending the necessary SIP message.
1198
  When a response from the remote party is received, the state machine will go into the {{{DISCONNECTED}}} state.
1199
  Depending on the current state, this could be a CANCEL or BYE request or a negative response.
1200
  [[BR]]''response_code'':[[BR]]
1201
  The code of the response to use as an int, if transmission of a response is needed.
1202
  [[BR]]''extra_headers'':[[BR]]
1203
  Any extra headers that should be included in the request or response in the form of a dict.
1204
  [[BR]]''timeout'':[[BR]]
1205
  How many seconds to wait for the remote party to reply before changing the state to {{{DISCONNECTED}}}, as an int or a float.
1206
  If this is set to {{{None}}}, the default PJSIP timeout will be used, which currently appears to be 3.5 seconds for an established session.
1207
 '''respond_to_reinvite'''(''self'', '''response_code'''=200, '''extra_headers'''={{{None}}})::
1208
  Respond to a received re-{{{INVITE}}} with a response that is either provisional (1xx), positive (2xx) or negative (3xx and upwards).
1209
  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.
1210
  Before giving a positive final response, the SDP needs to be set using the {{{set_offered_local_sdp()}}} method.
1211
  [[BR]]''response_code'':[[BR]]
1212
  The code of the response to use as an int.
1213
  This should be a 3 digit number.
1214
  [[BR]]''extra_headers'':[[BR]]
1215 29 Luci Stanescu
 '''send_reinvite'''(''self'', '''extra_headers'''={{{None}}})::
1216 1 Adrian Georgescu
  The application may only call this method when the state machine is in the {{{CONFIRMED}}} state to induce sending a re-{{{INVITE}}}.
1217
  Before doing this it needs to set the new local SDP offer by calling the {{{set_offered_local_sdp()}}} method.
1218
  After this method is called, the state machine will be in the {{{REINVITING}}} state, until a final response from the remote party is received.
1219
  [[BR]]''extra_headers'':[[BR]]
1220
  Any extra headers that should be included in the re-{{{INVITE}}} in the form of a dict.
1221
1222
==== notifications ====
1223
1224
 '''SIPInvitationChangedState'''::
1225
  This notification is sent by an {{{Invitation}}} object whenever its state machine changes state.
1226
  [[BR]]''timestamp'':[[BR]]
1227
  A {{{datetime.datetime}}} object indicating when the notification was sent.
1228
  [[BR]]''prev_state'':[[BR]]
1229
  The previous state of the INVITE state machine.
1230
  [[BR]]''state'':[[BR]]
1231
  The new state of the INVITE state machine, which may be the same as the previous state.
1232
  [[BR]]''method'': (only if the state change got triggered by an incoming SIP request)[[BR]]
1233
  The method of the SIP request as a string.
1234
  [[BR]]''request_uri'': (only if the state change got triggered by an incoming SIP request)[[BR]]
1235
  The request URI of the SIP request as a {{{SIPURI}}} object.
1236
  [[BR]]''code'': (only if the state change got triggered by an incoming SIP response or internal timeout or error)[[BR]]
1237
  The code of the SIP response or error as an int.
1238
  [[BR]]''reason'': (only if the state change got triggered by an incoming SIP response or internal timeout or error)[[BR]]
1239
  The reason text of the SIP response or error as an int.
1240
  [[BR]]''headers'': (only if the state change got triggered by an incoming SIP request or response)[[BR]]
1241 23 Ruud Klaver
  The headers of the SIP request or response as a dict.
1242 1 Adrian Georgescu
  Each SIP header is represented in its parsed for as long as PJSIP supports it.
1243
  The format of the parsed value depends on the header.
1244
  [[BR]]''body'': (only if the state change got triggered by an incoming SIP request or response)[[BR]]
1245
  The body of the SIP request or response as a string, or {{{None}}} if no body was included.
1246
  The content type of the body can be learned from the {{{Content-Type:}}} header in the headers argument.
1247
 '''SIPInvitationGotSDPUpdate'''::
1248
  This notification is sent by an {{{Invitation}}} object whenever SDP negotation has been perfomed.
1249
  It should be used by the application as an indication to start, change or stop any associated media streams.
1250
  [[BR]]''timestamp'':[[BR]]
1251
  A {{{datetime.datetime}}} object indicating when the notification was sent.
1252
  [[BR]]''succeeded'':[[BR]]
1253
  A boolean indicating if the SDP negotation has succeeded.
1254
  [[BR]]''error'': (only if SDP negotation did not succeed)[[BR]]
1255
  A string indicating why SDP negotation failed.
1256
  [[BR]]''local_sdp'': (only if SDP negotation succeeded)[[BR]]
1257
  A SDPSession object indicating the local SDP that was negotiated.
1258
  [[BR]]''remote_sdp'': (only if SDP negotation succeeded)[[BR]]
1259
  A SDPSession object indicating the remote SDP that was negotiated.
1260
1261
=== SDPSession ===
1262
1263
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. 
1264
1265
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.
1266
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.
1267
A {{{SDPSession}}} object may contain {{{SDPMedia}}}, {{{SDPConnection}}} and {{{SDPAttribute}}} objects.
1268
It supports comparison to other {{{SDPSession}}} objects using the == and != expressions.
1269
As all the attributes of the {{{SDPSession}}} class are set by attributes of the {{{__init__}}} method, they will be documented along with that method.
1270
1271
==== methods ====
1272
1273
 '''!__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}}})::
1274
  Creates the SDPSession object with the specified parameters as attributes.
1275
  Each of these attributes can be accessed and changed on the object once instanced.
1276
  [[BR]]''address'':[[BR]]
1277
  The address that is contained in the "o" (origin) line of the SDP as a string.
1278
  [[BR]]''id'':[[BR]]
1279
  The session identifier contained in the "o" (origin) line of the SDP as an int.
1280
  If this is set to {{{None}}} on init, a session identifier will be generated.
1281
  [[BR]]''version'':[[BR]]
1282
  The version identifier contained in the "o" (origin) line of the SDP as an int.
1283
  If this is set to {{{None}}} on init, a version identifier will be generated.
1284
  [[BR]]''user'':[[BR]]
1285 24 Ruud Klaver
  The user name contained in the "o" (origin) line of the SDP as a string.
1286 1 Adrian Georgescu
  [[BR]]''net_type'':[[BR]]
1287
  The network type contained in the "o" (origin) line of the SDP as a string.
1288
  [[BR]]''address_type'':[[BR]]
1289
  The address type contained in the "o" (origin) line of the SDP as a string.
1290
  [[BR]]''name'':[[BR]]
1291
  The contents of the "s" (session name) line of the SDP as a string.
1292
  [[BR]]''info'':[[BR]]
1293
  The contents of the session level "i" (information) line of the SDP as a string.
1294
  If this is {{{None}}} or an empty string, the SDP has no "i" line.
1295
  [[BR]]''connection'':[[BR]]
1296
  The contents of the "c" (connection) line of the SDP as a {{{SDPConnection}}} object.
1297
  If this is set to {{{None}}}, the SDP has no session level "c" line.
1298
  [[BR]]''start_time'':[[BR]]
1299
  The first value of the "t" (time) line of the SDP as an int.
1300
  [[BR]]''stop_time'':[[BR]]
1301
  The second value of the "t" (time) line of the SDP as an int.
1302
  [[BR]]''attributes'':[[BR]]
1303
  The session level "a" lines (attributes) in the SDP represented by a list of {{{SDPAttribute}}} objects.
1304
  [[BR]]''media'':[[BR]]
1305
  The media sections of the SDP represented by a list of {{{SDPMedia}}} objects.
1306
1307
=== SDPMedia ===
1308
1309
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.
1310
It is a simple wrapper for the PJSIP [http://www.pjsip.org/pjmedia/docs/html/structpjmedia__sdp__media.htm pjmedia_sdp_media] structure.
1311
One or more {{{SDPMedia}}} objects are usually contained in a {{{SDPSession}}} object.
1312
It supports comparison to other {{{SDPMedia}}} objects using the == and != expressions.
1313
As all the attributes of this class are set by attributes of the {{{__init__}}} method, they will be documented along with that method.
1314
1315
==== methods ====
1316
1317
 '''!__init!__'''(''self'', '''media''', '''port''', '''transport''', '''port_count'''=1, '''formats'''={{{None}}}, '''info'''={{{None}}}, '''connection'''={{{None}}}, '''attributes'''={{{None}}})::
1318
  Creates the SDPMedia object with the specified parameters as attributes.
1319
  Each of these attributes can be accessed and changed on the object once instanced.
1320
  [[BR]]''media'':[[BR]]
1321
  The media type contained in the "m" (media) line as a string.
1322
  [[BR]]''port'':[[BR]]
1323
  The transport port contained in the "m" (media) line as an int.
1324
  [[BR]]''transport'':[[BR]]
1325
  The transport protocol in the "m" (media) line as a string.
1326
  [[BR]]''port_count'':[[BR]]
1327
  The port count in the "m" (media) line as an int.
1328
  If this is set to 1, it is not included in the SDP.
1329
  [[BR]]''formats'':[[BR]]
1330
  The media formats in the "m" (media) line represented by a list of strings.
1331
  [[BR]]''info'':[[BR]]
1332
  The contents of the "i" (information) line of this media section as a string.
1333
  If this is {{{None}}} or an empty string, the media section has no "i" line.
1334
  [[BR]]''connection'':[[BR]]
1335
  The contents of the "c" (connection) line that is somewhere below the "m" line of this section as a {{{SDPConnection}}} object.
1336
  If this is set to {{{None}}}, this media section has no "c" line.
1337
  [[BR]]''attributes'':[[BR]]
1338
  The "a" lines (attributes) that are somewhere below the "m" line of this section represented by a list of {{{SDPAttribute}}} objects.
1339
 '''get_direction'''(''self'')::
1340
  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".
1341
  If none of these attributes is present, the default direction is "sendrecv".
1342
1343
=== SDPConnection ===
1344
1345
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.
1346
It is a simple wrapper for the PJSIP [http://www.pjsip.org/pjmedia/docs/html/structpjmedia__sdp__conn.htm pjmedia_sdp_conn] structure.
1347
A {{{SDPConnection}}} object can be contained in a {{{SDPSession}}} object or {{{SDPMedia}}} object.
1348
It supports comparison to other {{{SDPConnection}}} objects using the == and != expressions.
1349
As all the attributes of this class are set by attributes of the {{{__init__}}} method, they will be documented along with that method.
1350
1351
==== methods ====
1352
1353
 '''!__init!__'''(''self'', '''address''', '''net_type'''="IN", '''address_type'''="IP4")::
1354
  Creates the SDPConnection object with the specified parameters as attributes.
1355
  Each of these attributes can be accessed and changed on the object once instanced.
1356
  [[BR]]''address'':[[BR]]
1357
  The address part of the connection line as a string.
1358
  [[BR]]''net_type'':[[BR]]
1359
  The network type part of the connection line as a string.
1360
  [[BR]]''address_type'':[[BR]]
1361
  The address type part of the connection line as a string.
1362
1363
=== SDPAttribute ===
1364
1365
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.
1366 18 Adrian Georgescu
It is a simple wrapper for the PJSIP [http://www.pjsip.org/pjmedia/docs/html/structpjmedia__sdp__attr.htm pjmedia_sdp_attr] structure.
1367 1 Adrian Georgescu
One or more {{{SDPAttribute}}} objects can be contained in a {{{SDPSession}}} object or {{{SDPMedia}}} object.
1368
It supports comparison to other {{{SDPAttribute}}} objects using the == and != expressions.
1369 17 Ruud Klaver
As all the attributes of this class are set by attributes of the {{{__init__}}} method, they will be documented along with that method.
1370
1371
==== methods ====
1372
1373
 '''!__init!__'''(''self'', '''name''', '''value''')::
1374
  Creates the SDPAttribute object with the specified parameters as attributes.
1375
  Each of these attributes can be accessed and changed on the object once instanced.
1376
  [[BR]]''name'':[[BR]]
1377
  The name part of the attribute line as a string.
1378 1 Adrian Georgescu
  [[BR]]''value'':[[BR]]
1379
  The value part of the attribute line as a string.
1380
1381
=== RTPTransport ===
1382
1383
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.
1384
Internally it wraps a [http://www.pjsip.org/pjmedia/docs/html/group__PJMEDIA__TRANSPORT.htm pjmedia_transport] object.
1385
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].
1386
For this reason the {{{AudioTransport}}} and {{{RTPTransport}}} are two distinct objects.
1387 17 Ruud Klaver
1388 1 Adrian Georgescu
The {{{RTPTransport}}} object also allows support for ICE and SRTP functionality from PJSIP.
1389
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.
1390
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.
1391
This process of SDP negotiation is represented by the internal state machine of the object, as shown in the following diagram:
1392
1393
> The Real-time Transport Protocol (or RTP) defines a standardized packet format for delivering audio and video over the Internet.
1394 17 Ruud Klaver
> It was developed by the Audio-Video Transport Working Group of the IETF and published in [http://tools.ietf.org/html/rfc3550 RFC 3550].
1395 1 Adrian Georgescu
> RTP is used in streaming media systems (together with the RTSP) as well as in videoconferencing and push to talk systems.
1396
> 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.
1397
1398
[[Image(sipsimple-rtp-transport-state-machine.png, nolink)]]
1399
1400
State changes are triggered by the following events:
1401
 1. The application calls the {{{set_INIT()}}} method after object creation and ICE+STUN is not used.
1402
 2. The application calls the {{{set_INIT()}}} method after object creation and ICE+STUN is used.
1403
 3. A successful STUN response is received from the STUN server.
1404
 4. The {{{set_LOCAL()}}} method is called.
1405
 5. The {{{set_ESTABLISHED()}}} method is called.
1406
 6. The {{{set_INIT()}}} method is called while the object is in the {{{LOCAL}}} or {{{ESTABLISHED}}} state.
1407
 7. A method is called on the application, but in the meantime the {{{Engine}}} has stopped.
1408 45 Ruud Klaver
    The object can no longer be used.
1409 1 Adrian Georgescu
 8. There was an error in getting the STUN candidates from the STUN server.
1410
1411
> 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.
1412
> This means that the RTPTransport object is also unusable once it has reached the STUN_FAILED state.
1413
> A workaround would be destroy the RTPTransport object and create a new one that uses ICE without STUN.
1414
1415
These states allow for two SDP negotiation scenarios to occur, represented by two paths that can be followed through the state machine.
1416
In this example we will assume that ICE with STUN is not used, as it is independent of the SDP negotiation procedure.
1417
 * The first scenario is where the local party generates the SDP offer.
1418
   For a stream that it wishes to include in this SDP offer, it instantiates a {{{RTPTransport}}} object.
1419
   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).
1420
   This local SDP then needs to be passed to the {{{set_LOCAL()}}} method, which moves the state machine into the {{{LOCAL}}} state.
1421
   Depending on the options used for the {{{RTPTransport}}} instantiation (such as ICE and SRTP), this may change the {{{SDPSession}}} object.
1422
   This (possibly changed) {{{SDPSession}}} object then needs to be passed to the {{{Invitation}}} object.
1423
   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.
1424
   This will not change either of the {{{SDPSession}}} objects.
1425
 * The second scenario is where the local party is offered a media stream in SDP and wants to accept it.
1426
   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.
1427
   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.
1428
   In this case the local SDP object may be changed, after which it can be passed to the {{{Invitation}}} object.
1429
1430
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.
1431
Before doing this however, the internal transport object must no longer be in use.
1432
1433
==== attributes ====
1434
1435
 '''state'''::
1436
  Indicates which state the internal state machine is in.
1437
  See the previous section for a list of states the state machine can be in.
1438
  This attribute is read-only.
1439
 '''local_rtp_address'''::
1440
  The local IPv4 address of the interface the {{{RTPTransport}}} object is listening on and the address that should be included in the SDP.
1441 19 Ruud Klaver
  If no address was specified during object instantiation, PJSIP will take guess out of the IP addresses of all interfaces.
1442 1 Adrian Georgescu
  This attribute is read-only and will be {{{None}}} if PJSIP is not listening on the transport.
1443
 '''local_rtp_port'''::
1444
  The UDP port PJSIP is listening on for RTP traffic.
1445
  RTCP traffic will always be this port plus one.
1446
  This attribute is read-only and will be {{{None}}} if PJSIP is not listening on the transport.
1447
 '''remote_rtp_address_sdp'''::
1448
  The remote IP address that was seen in the SDP.
1449
  This attribute is read-only and will be {{{None}}} unless the object is in the {{{ESTABLISHED}}} state.
1450
 '''remote_rtp_port_sdp'''::
1451
  The remote UDP port for RTP that was seen in the SDP.
1452
  This attribute is read-only and will be {{{None}}} unless the object is in the {{{ESTABLISHED}}} state.
1453
 '''remote_rtp_address_received'''::
1454 45 Ruud Klaver
  The remote IP address from which RTP data was received.
1455 1 Adrian Georgescu
  This attribute is read-only and will be {{{None}}} unless RTP was actually received.
1456
 '''remote_rtp_port_received'''::
1457
  The remote UDP port from which RTP data was received.
1458
  This attribute is read-only and will be {{{None}}} unless RTP was actually received.
1459
 '''use_srtp'''::
1460
  A boolean indicating if the use of SRTP was requested when the object was instantiated.
1461
  This attribute is read-only.
1462
 '''force_srtp'''::
1463
  A boolean indicating if SRTP being mandatory for this transport if it is enabled was requested when the object was instantiated.
1464
  This attribute is read-only.
1465
 '''srtp_active'''::
1466
  A boolean indicating if SRTP encryption and decryption is running.
1467
  Querying this attribute only makes sense once the object is in the {{{ESTABLISHED}}} state and use of SRTP was requested.
1468
  This attribute is read-only.
1469 29 Luci Stanescu
 '''use_ice'''::
1470 1 Adrian Georgescu
  A boolean indicating if the use of ICE was requested when the object was instantiated.
1471
  This attribute is read-only.
1472
 '''ice_stun_address'''::
1473
  A string indicating the IP address of the STUN server that was requested to be used.
1474 34 Ruud Klaver
  This attribute is read-only.
1475
 '''ice_stun_port'''::
1476
  A string indicating the UDP port of the STUN server that was requested to be used.
1477
  This attribute is read-only.
1478 1 Adrian Georgescu
1479
==== methods ====
1480
1481
 '''!__init!__'''(''self'', '''local_rtp_address'''={{{None}}}, '''use_srtp'''={{{False}}}, '''srtp_forced'''={{{False}}}, '''use_ice'''={{{False}}}, '''ice_stun_address'''={{{None}}}, '''ice_stun_port'''=3478)::
1482
  Creates a new {{{RTPTransport}}} object and opens the RTP and RTCP UDP sockets.
1483
  If aditional features are requested, they will be initialized.
1484 34 Ruud Klaver
  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.
1485 1 Adrian Georgescu
  [[BR]]''local_rtp_address'':[[BR]]
1486
  Optionally contains the local IPv4 address to listen on.
1487
  If this is not specified, PJSIP will listen on all network interfaces.
1488
  [[BR]]''use_srtp'':[[BR]]
1489
  A boolean indicating if SRTP should be used.
1490
  If this is set to {{{True}}}, SRTP information will be added to the SDP when it passes this object.
1491 17 Ruud Klaver
  [[BR]]''srtp_forced'':[[BR]]
1492
  A boolean indicating if use of SRTP is set to mandatory in the SDP.
1493 1 Adrian Georgescu
  If this is set to {{{True}}} and the remote party does not support SRTP, the SDP negotation for this stream will fail.
1494
  This argument is relevant only if {{{use_srtp}}} is set to {{{True}}}.
1495
  [[BR]]''use_ice'':[[BR]]
1496 29 Luci Stanescu
  A boolean indicating if ICE should be used.
1497 17 Ruud Klaver
  If this is set to {{{True}}}, ICE candidates will be added to the SDP when it passes this object.
1498
  [[BR]]''ice_stun_address'':[[BR]]
1499 1 Adrian Georgescu
  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.
1500
  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.
1501 29 Luci Stanescu
  When this happens a {{{RTPTransportGotSTUNResponse}}} notification is sent.
1502 17 Ruud Klaver
  This argument is relevant only if {{{use_ice}}} is set to {{{True}}}.
1503
  [[BR]]''ice_stun_address'':[[BR]]
1504
  An int indicating the UDP port of the STUN server that should be used to add a STUN candidate to the ICE candidates.
1505
  This argument is relevant only if {{{use_ice}}} is set to {{{True}}} and {{{ice_stun_address}}} is not {{{None}}}.
1506
 '''set_INIT'''(''self'')::
1507 1 Adrian Georgescu
  This moves the internal state machine into the {{{INIT}}} state.
1508
  If the state machine is in the {{{LOCAL}}} or {{{ESTABLISHED}}} states, this effectively resets the {{{RTPTransport}}} object for re-use.
1509
 '''set_LOCAL'''(''self'', '''local_sdp''', '''sdp_index''')::
1510
  This moves the the internal state machine into the {{{LOCAL}}} state.
1511
  [[BR]]''local_sdp'':[[BR]]
1512
  The local SDP to be proposed in the form of a {{{SDPSession}}} object.
1513
  Note that this object may be modified by this method.
1514
  [[BR]]''sdp_index'':[[BR]]
1515
  The index in the SDP for the media stream for which this object was created.
1516
 '''set_ESTABLISHED'''(''self'', '''local_sdp''', '''remote_sdp''', '''sdp_index''')::
1517
  This moves the the internal state machine into the {{{ESTABLISHED}}} state.
1518
  [[BR]]''local_sdp'':[[BR]]
1519
  The local SDP to be proposed in the form of a {{{SDPSession}}} object.
1520
  Note that this object may be modified by this method, but only when moving from the {{{LOCAL}}} to the {{{ESTABLISHED}}} state.
1521
  [[BR]]''remote_sdp'':[[BR]]
1522
  The remote SDP that was received in in the form of a {{{SDPSession}}} object.
1523
  [[BR]]''sdp_index'':[[BR]]
1524
  The index in the SDP for the media stream for which this object was created.
1525
1526
==== notifications ====
1527
1528
 '''RTPTransportDidInitialize'''::
1529
  This notification is sent when a {{{RTPTransport}}} object has successfully initialized.
1530
  If STUN+ICE is not requested, this is sent immediately on {{{set_INIT()}}}, otherwise it is sent after the STUN query has succeeded.
1531
  [[BR]]''timestamp'':[[BR]]
1532
  A {{{datetime.datetime}}} object indicating when the notification was sent.
1533
 '''RTPTransportDidFail'''::
1534
  This notification is sent by a {{{RTPTransport}}} object that fails to retrieve ICE candidates from the STUN server after {{{set_INIT()}}} is called.
1535
  [[BR]]''timestamp'':[[BR]]
1536
  A {{{datetime.datetime}}} object indicating when the notification was sent.
1537
  [[BR]]''reason'':[[BR]]
1538
  A string describing the failure reason.
1539
1540
=== AudioTransport ===
1541
1542
This object represent an audio stream as it is transported over the network.
1543
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.
1544
It also generates a {{{SDPMedia}}} object to be included in the local SDP.
1545
1546
Like the {{{RTPTransport}}} object there are two usage scenarios.
1547
 * In the first scenario, only the {{{RTPTransport}}} instance to be used is passed to the AudioTransport object.
1548
   The application can then generate the {{{SDPMedia}}} object by calling the {{{get_local_media()}}} method and should include it in the SDP offer.
1549
   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.
1550 40 Ruud Klaver
   The stream can then be connected to the conference bridge.
1551 1 Adrian Georgescu
 * 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.
1552
   The local {{{SDPMedia}}} object can again be generated by calling the {{{get_local_media()}}} method and is to be included in the SDP answer.
1553
   The audio stream is started directly when the object is created.
1554
1555
Unlike the {{{RTPTransport}}} object, this object cannot be reused.
1556
1557
==== attributes ====
1558
1559
 '''transport'''::
1560
  The {{{RTPTransport}}} object that was passed when the object got instatiated.
1561 40 Ruud Klaver
  This attribute is read-only.
1562
 '''is_active'''::
1563
  A boolean indicating if the object is currently sending and receiving audio.
1564
  This attribute is read-only.
1565 1 Adrian Georgescu
 '''is_started'''::
1566
  A boolean indicating if the object has been started.
1567
  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.
1568
  This is to prevent the object from being re-used.
1569
  This attribute is read-only.
1570
 '''codec'''::
1571
  Once the SDP negotiation is complete, this attribute indicates the audio codec that was negotiated, otherwise it will be {{{None}}}.
1572
  This attribute is read-only.
1573
 '''sample_rate'''::
1574 54 Ruud Klaver
  Once the SDP negotiation is complete, this attribute indicates the sample rate of the audio codec that was negotiated, otherwise it will be {{{None}}}.
1575 1 Adrian Georgescu
  This attribute is read-only.
1576
 '''direction'''::
1577
  The current direction of the audio transport, which is one of "sendrecv", "sendonly", "recvonly" or "inactive".
1578
  This attribute is read-only, although it can be set using the {{{update_direction()}}} method.
1579
1580
==== methods ====
1581
1582 54 Ruud Klaver
 '''!__init!__'''(''self'', '''transport''', '''remote_sdp'''={{{None}}}, '''sdp_index'''=0, '''enable_silence_detection'''=True, '''codecs'''={{{None}}})::
1583 55 Ruud Klaver
  Creates a new {{{AudioTransport}}} object and start the underlying stream if the remote SDP is already known.
1584
  [[BR]]''transport'':[[BR]]
1585 1 Adrian Georgescu
  The transport to use in the form of a {{{RTPTransport}}} object.
1586
  [[BR]]''remote_sdp'':[[BR]]
1587 55 Ruud Klaver
  The remote SDP that was received in the form of a {{{SDPSession}}} object.
1588
  [[BR]]''sdp_index'':[[BR]]
1589
  The index within the SDP of the audio stream that should be created.
1590 1 Adrian Georgescu
  [[BR]]''enable_silence_detection''[[BR]]
1591
  Boolean that indicates if silence detection should be used for this audio stream.
1592
  When enabled, this {{{AudioTransport}}} object will stop sending audio to the remote party if the input volume is below a certain threshold.
1593
  [[BR]]''codecs''[[BR]]
1594
  A list of strings indicating the codecs that should be proposed in the SDP of this {{{AudioTransport}}}, in order of preference.
1595
  This overrides the global codecs list set on the {{{Engine}}}.
1596
  The values of this list are case insensitive.
1597
 '''get_local_media'''(''self'', '''is_offer''', '''direction'''="sendrecv")::
1598
  Generates a {{{SDPMedia}}} object which describes the audio stream.
1599
  This object should be included in a {{{SDPSession}}} object that gets passed to the {{{Invitation}}} object.
1600
  This method should also be used to obtain the SDP to include in re-INVITEs and replies to re-INVITEs.
1601
  [[BR]]''is_offer'':[[BR]]
1602
  A boolean indicating if the SDP requested is to be included in an offer.
1603
  If this is {{{False}}} it is to be included in an answer.
1604
  [[BR]]''direction'':[[BR]]
1605
  The direction attribute to put in the SDP.
1606 29 Luci Stanescu
 '''start'''(''self'', '''local_sdp''', '''remote_sdp''', '''sdp_index''', '''no_media_timeout'''=10, '''media_check_interval'''=30)::
1607 1 Adrian Georgescu
  This method should only be called once, when the application has previously sent an SDP offer and the answer has been received.
1608
  [[BR]]''local_sdp'':[[BR]]
1609
  The full local SDP that was included in the SDP negotiation in the form of a {{{SDPSession}}} object.
1610
  [[BR]]''remote_sdp'':[[BR]]
1611
  The remote SDP that was received in the form of a {{{SDPSession}}} object.
1612
  [[BR]]''sdp_index'':[[BR]]
1613 55 Ruud Klaver
  The index within the SDP of the audio stream.
1614 54 Ruud Klaver
  [[BR]]''no_media_timeout'':[[BR]]
1615
  This argument indicates after how many seconds after starting the {{{AudioTransport}}} the {{{RTPAudioTransportDidNotGetRTP}}} notification should be sent, if no RTP has been received at all.
1616
  Setting this to 0 disables this an all subsequent RTP checks.
1617
  [[BR]]''media_check_interval'':[[BR]]
1618
  This indicates the interval at which the RTP stream should be checked, after it has initially received RTP at after {{{no_media_timeout}}} seconds.
1619
  It means that if between two of these interval checks no RTP has been received, a {{{RTPAudioTransportDidNotGetRTP}}} notification will be sent.
1620
  Setting this to 0 will disable checking the RTP at intervals.
1621 1 Adrian Georgescu
  The initial check may still be performed if its timeout is non-zero.
1622
 '''stop'''(''self'')::
1623
  This method stops and destroys the audio stream encapsulated by this object.
1624
  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.
1625
  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.
1626
 '''send_dtmf'''(''self'', '''digit''')::
1627
  For a negotiated audio transport this sends one DTMF digit to the other party
1628
  [[BR]]''digit'':[[BR]]
1629
  A string of length one indicating the DTMF digit to send.
1630 29 Luci Stanescu
  This can be either a number or letters A through D.
1631 1 Adrian Georgescu
 '''update_direction'''(''self'', '''direction''')::
1632
  This method should be called after SDP negotiation has completed to update the direction of the media stream.
1633
  [[BR]]''direction'':[[BR]]
1634
  The direction that has been negotiated.
1635
1636
==== notifications ====
1637
1638
 '''RTPAudioTransportGotDTMF'''::
1639
  This notification will be sent when an incoming DTMF digit is received from the remote party.
1640
  [[BR]]''timestamp'':[[BR]]
1641
  A {{{datetime.datetime}}} object indicating when the notification was sent.
1642
  [[BR]]''digit'':[[BR]]
1643
  The DTMF digit that was received, in the form of a string of length one.
1644
  This can be either a number or letters A through D.
1645
 '''RTPAudioTransportDidNotGetRTP'''::
1646
  This notification will be sent when no RTP packets have been received from the remote party for some time.
1647
  See the {{{start()}}} method for a more exact description.
1648
  [[BR]]''timestamp'':[[BR]]
1649
  A {{{datetime.datetime}}} object indicating when the notification was sent.
1650
  [[BR]]''got_any'':[[BR]]
1651
  A boolean data attribute indicating if the {{{AudioTransport}}} every saw any RTP packets from the remote party.
1652
  In effect, if no RTP was received after {{{no_media_timeout}}} seconds, its value will be {{{False}}}.
1653
1654
=== WaveFile ===
1655
1656
This is a simple object that allows playing back of a {{{.wav}}} file over the PJSIP conference bridge, possibly looping a number of times.
1657
Only 16-bit PCM and A-law/U-law formats are supported.
1658
Its main purpose is the playback of ringtones.
1659
1660
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.
1661
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.
1662
When playback stops for either of these reasons, a {{{WaveFileDidFinishPlaying}}} notification is sent by the object.
1663
After this the {{{start()}}} method may be called again in order to re-use the object.
1664
1665
It is the application's responsibility to keep a reference to the {{{WaveFile}}} object for the duration of playback.
1666
If the reference count of the object reaches 0, playback will be stopped.
1667
In this case no notification will be sent.
1668 29 Luci Stanescu
1669 1 Adrian Georgescu
==== attributes ====
1670
1671
 '''file_name'''::
1672
  The name of the {{{.wav}}} file, as specified when the object was created.
1673
 '''is_active'''::
1674
  A boolean read-only property that indicates if the file is currently being played back.
1675
  Note that if the playback loop is currently in a pause between playbacks, this attribute will also be {{{True}}}.
1676
1677
==== methods ====
1678
1679
 '''!__init!__'''(''self'', '''file_name''')::
1680
  Creates a new {{{WaveFile}}} object.
1681
  [[BR]]''file_name'':[[BR]]
1682
  The name of the {{{.wav}}} file to be played back as a string.
1683
  This should include the full path to the file.
1684
 '''start'''(''self'', '''level'''=100, '''loop_count'''=1, '''pause_time'''=0)::
1685
  Start playback of the {{{.wav}}} file, optionally looping it.
1686 15 Ruud Klaver
  [[BR]]''level'':[[BR]]
1687
  The level to play the file back at, in percentage.
1688
  A percentage lower than 100 means playback will be attenuated, a percentage higher than 100 means it will amplified.
1689 1 Adrian Georgescu
  [[BR]]''loop_count'':[[BR]]
1690
  The number of time to loop playing this file.
1691
  A value of 0 means looping infinitely.
1692
  [[BR]]''pause_time'':[[BR]]
1693
  The number of seconds to pause between consecutive loops.
1694
  This can be either an int or a float.
1695
 '''stop'''(''self'')::
1696
  Stop playback of the file.
1697
1698
==== notifications ====
1699 15 Ruud Klaver
1700
 '''WaveFileDidFinishPlaying'''::
1701
  This notification will be sent whenever the {{{.wav}}} file has been played back the specified number of times.
1702
  After sending this event, the playback may be re-started.
1703 1 Adrian Georgescu
  [[BR]]''timestamp'':[[BR]]
1704
  A {{{datetime.datetime}}} object indicating when the notification was sent.
1705
1706
=== RecordingWaveFile ===
1707
1708
This is a simple object that allows recording whatever is heard on the PJSIP conference bridge to a PCM {{{.wav}}} file.
1709
1710
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.
1711
Recording to the file can be stopped either by calling the {{{stop()}}} method or by removing all references to the object.
1712
Once the {{{stop()}}} method has been called, the {{{start()}}} method may not be called again.
1713
It is the application's responsibility to keep a reference to the {{{RecordingWaveFile}}} object for the duration of the recording.
1714
1715
==== attributes ====
1716
1717
 '''file_name'''::
1718
  The name of the {{{.wav}}} file, as specified when the object was created.
1719
 '''is_active'''::
1720
  A boolean read-only attribute that indicates if the file is currently being written to.
1721
 '''is_paused'''::
1722
  A boolean read-only attribute that indicates if the active recording is currently paused.
1723
1724
==== methods ====
1725
1726
 '''!__init!__'''(''self'', '''file_name''')::
1727
  Creates a new {{{RecordingWaveFile}}} object.
1728
  [[BR]]''file_name'':[[BR]]
1729
  The name of the {{{.wav}}} file to record to as a string.
1730
  This should include the full path to the file.
1731
 '''start'''(''self'')::
1732
  Start recording the sound on the conference bridge to the {{{.wav}}} file.
1733
 '''pause'''(''self'')::
1734
  Pause recording to the {{{.wav}}} file.
1735
 '''resume'''(''self'')::
1736
  Resume a previously paused recording.
1737
 '''stop'''(''self'')::
1738
  Stop recording to the file.