Project

General

Profile

SipCoreApiDocumentation » History » Version 27

Oliver Bril, 03/19/2009 01:40 PM

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