blob: 4939c9d3f251bcff2a227185b2dd0ca73770a417 [file] [log] [blame]
Benny Prijono268ca612006-02-07 12:34:11 +00001/* $Id$ */
2/*
3 * Copyright (C) 2003-2006 Benny Prijono <benny@prijono.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#ifndef __SIP_INVITE_SESSION_H__
20#define __SIP_INVITE_SESSION_H__
21
22#include <pjsip/sip_dialog.h>
23#include <pjmedia/sdp_neg.h>
24
Benny Prijonof3195072006-02-14 21:15:30 +000025
26PJ_BEGIN_DECL
27
28
Benny Prijono268ca612006-02-07 12:34:11 +000029typedef enum pjsip_inv_state pjsip_inv_state;
30typedef struct pjsip_inv_session pjsip_inv_session;
31typedef struct pjsip_inv_callback pjsip_inv_callback;
32
33/**
34 * This enumeration describes invite session state.
35 */
36enum pjsip_inv_state
37{
38 PJSIP_INV_STATE_NULL, /**< Before INVITE is sent or received */
39 PJSIP_INV_STATE_CALLING, /**< After INVITE is sent */
40 PJSIP_INV_STATE_INCOMING, /**< After INVITE is received. */
41 PJSIP_INV_STATE_EARLY, /**< After response with To tag. */
42 PJSIP_INV_STATE_CONNECTING, /**< After 2xx is sent/received. */
43 PJSIP_INV_STATE_CONFIRMED, /**< After ACK is sent/received. */
44 PJSIP_INV_STATE_DISCONNECTED, /**< Session is terminated. */
Benny Prijono268ca612006-02-07 12:34:11 +000045};
46
47/**
48 * This structure contains callbacks to be registered by application to
49 * receieve notifications from the framework about various events in
50 * the invite session.
51 */
52struct pjsip_inv_callback
53{
54 /**
55 * This callback is called when the invite sesion state has changed.
56 * Application should inspect the session state (inv_sess->state) to get
57 * the current state of the session.
58 *
59 * This callback is mandatory.
60 *
61 * @param inv The invite session.
62 * @param e The event which has caused the invite session's
63 * state to change.
64 */
65 void (*on_state_changed)(pjsip_inv_session *inv, pjsip_event *e);
66
67
68 /**
69 * This callback is called when the invite usage module has created
70 * a new dialog and invite because of forked outgoing request.
71 *
72 * This callback is mandatory.
73 *
74 * @param inv The new invite session.
75 * @param e The event which has caused the dialog to fork.
76 * The type of this event can be either
77 * PJSIP_EVENT_RX_MSG or PJSIP_EVENT_RX_200_MSG.
78 */
79 void (*on_new_session)(pjsip_inv_session *inv, pjsip_event *e);
80
81 /**
82 * This callback is called whenever any transactions within the session
83 * has changed their state. Application MAY implement this callback,
84 * e.g. to monitor the progress of an outgoing request.
85 *
86 * This callback is optional.
87 *
88 * @param inv The invite session.
89 * @param tsx The transaction, which state has changed.
90 * @param e The event which has caused the transation state's
91 * to change.
92 */
93 void (*on_tsx_state_changed)(pjsip_inv_session *inv,
94 pjsip_transaction *tsx,
95 pjsip_event *e);
96
97 /**
98 * This callback is called when the invite session has received
99 * new offer from peer. Application can inspect the remote offer
Benny Prijono26ff9062006-02-21 23:47:00 +0000100 * in "offer".
Benny Prijono268ca612006-02-07 12:34:11 +0000101 *
102 * @param inv The invite session.
Benny Prijono26ff9062006-02-21 23:47:00 +0000103 * @param offer Remote offer.
Benny Prijono268ca612006-02-07 12:34:11 +0000104 */
Benny Prijono26ff9062006-02-21 23:47:00 +0000105 void (*on_rx_offer)(pjsip_inv_session *inv,
106 const pjmedia_sdp_session *offer);
Benny Prijono268ca612006-02-07 12:34:11 +0000107
108 /**
109 * This callback is called after SDP offer/answer session has completed.
110 * The status argument specifies the status of the offer/answer,
111 * as returned by pjmedia_sdp_neg_negotiate().
112 *
113 * This callback is optional (from the point of view of the framework),
114 * but all useful applications normally need to implement this callback.
115 *
116 * @param inv The invite session.
117 * @param status The negotiation status.
118 */
119 void (*on_media_update)(pjsip_inv_session *inv_ses,
120 pj_status_t status);
Benny Prijono268ca612006-02-07 12:34:11 +0000121};
122
123
124/**
125 * This enumeration shows various options that can be applied to a session.
126 * The bitmask combination of these options need to be specified when
127 * creating a session. After the dialog is established (including early),
128 * the options member of #pjsip_inv_session shows which capabilities are
129 * common in both endpoints.
130 */
131enum pjsip_inv_option
132{
133 /**
134 * Indicate support for reliable provisional response extension
135 */
136 PJSIP_INV_SUPPORT_100REL = 1,
137
138 /**
139 * Indicate support for session timer extension.
140 */
141 PJSIP_INV_SUPPORT_TIMER = 2,
142
143 /**
144 * Indicate support for UPDATE method. This is automatically implied
145 * when creating outgoing dialog. After the dialog is established,
146 * the options member of #pjsip_inv_session shows whether peer supports
147 * this method as well.
148 */
149 PJSIP_INV_SUPPORT_UPDATE = 4,
150
151 /**
152 * Require reliable provisional response extension.
153 */
154 PJSIP_INV_REQUIRE_100REL = 32,
155
156 /**
157 * Require session timer extension.
158 */
159 PJSIP_INV_REQUIRE_TIMER = 64,
160
161};
162
163
164/**
165 * This structure describes the invite session.
166 */
167struct pjsip_inv_session
168{
Benny Prijonoe4f2abb2006-02-10 14:04:05 +0000169 char obj_name[PJ_MAX_OBJ_NAME]; /**< Log identification. */
170 pj_pool_t *pool; /**< Dialog's pool. */
171 pjsip_inv_state state; /**< Invite sess state. */
Benny Prijonod4e0abd2006-03-05 11:53:36 +0000172 pjsip_status_code cause; /**< Disconnect cause. */
173 pj_bool_t notify; /**< Internal. */
Benny Prijonoe4f2abb2006-02-10 14:04:05 +0000174 pjsip_dialog *dlg; /**< Underlying dialog. */
175 pjsip_role_e role; /**< Invite role. */
176 unsigned options; /**< Options in use. */
177 pjmedia_sdp_neg *neg; /**< Negotiator. */
178 pjsip_transaction *invite_tsx; /**< 1st invite tsx. */
179 void *mod_data[PJSIP_MAX_MODULE];/**< Modules data. */
Benny Prijono268ca612006-02-07 12:34:11 +0000180};
181
182
183/**
184 * Initialize the invite usage module and register it to the endpoint.
185 * The callback argument contains pointer to functions to be called on
186 * occurences of events in invite sessions.
187 *
188 * @param endpt The endpoint instance.
189 * @param app_module Application module.
190 * @param callback Callback structure.
191 *
192 * @return PJ_SUCCESS on success, or the appropriate error code.
193 */
194PJ_DECL(pj_status_t) pjsip_inv_usage_init(pjsip_endpoint *endpt,
195 pjsip_module *app_module,
196 const pjsip_inv_callback *cb);
197
198/**
199 * Get the INVITE usage module instance.
200 *
201 * @return PJ_SUCCESS on success, or the appropriate error code.
202 */
203PJ_DECL(pjsip_module*) pjsip_inv_usage_instance(void);
204
205
Benny Prijono632ce712006-02-09 14:01:40 +0000206/**
207 * Dump user agent contents (e.g. all dialogs).
208 */
209PJ_DECL(void) pjsip_inv_usage_dump(void);
210
Benny Prijono268ca612006-02-07 12:34:11 +0000211
212/**
213 * Create UAC invite session for the specified dialog in dlg.
214 *
215 * @param dlg The dialog which will be used by this invite session.
216 * @param local_sdp If application has determined its media capability,
217 * it can specify the SDP here. Otherwise it can leave
218 * this to NULL, to let remote UAS specifies an offer.
219 * @param options The options argument is bitmask combination of SIP
220 * features in pjsip_inv_options enumeration.
221 * @param p_inv On successful return, the invite session will be put
222 * in this argument.
223 *
224 * @return The function will return PJ_SUCCESS if it can create
225 * the session. Otherwise the appropriate error status
226 * will be returned on failure.
227 */
228PJ_DECL(pj_status_t) pjsip_inv_create_uac(pjsip_dialog *dlg,
229 const pjmedia_sdp_session *local_sdp,
230 unsigned options,
231 pjsip_inv_session **p_inv);
232
233
234/**
235 * Application SHOULD call this function upon receiving the initial INVITE
236 * request in rdata before creating the invite session (or even dialog),
237 * to verify that the invite session can handle the INVITE request.
238 * This function verifies that local endpoint is capable to handle required
239 * SIP extensions in the request (i.e. Require header) and also the media,
240 * if media description is present in the request.
241 *
242 * @param rdata The incoming INVITE request.
243 *
244 * @param options Upon calling this function, the options argument
245 * MUST contain the desired SIP extensions to be
246 * applied to the session. Upon return, this argument
247 * will contain the SIP extension that will be applied
248 * to the session, after considering the Supported,
249 * Require, and Allow headers in the request.
250 *
251 * @param sdp If local media capability has been determined,
252 * and if application wishes to verify that it can
253 * handle the media offer in the incoming INVITE
254 * request, it SHOULD specify its local media capability
255 * in this argument.
256 * If it is not specified, media verification will not
257 * be performed by this function.
258 *
259 * @param dlg If tdata is not NULL, application needs to specify
260 * how to create the response. Either dlg or endpt
261 * argument MUST be specified, with dlg argument takes
262 * precedence when both are specified.
263 *
264 * If a dialog has been created prior to calling this
265 * function, then it MUST be specified in dlg argument.
266 * Otherwise application MUST specify the endpt argument
267 * (this is useful e.g. when application wants to send
268 * the response statelessly).
269 *
270 * @param endpt If tdata is not NULL, application needs to specify
271 * how to create the response. Either dlg or endpt
272 * argument MUST be specified, with dlg argument takes
273 * precedence when both are specified.
274 *
275 * @param tdata If this argument is not NULL, this function will
276 * create the appropriate non-2xx final response message
277 * when the verification fails.
278 *
279 * @return If everything has been negotiated successfully,
280 * the function will return PJ_SUCCESS. Otherwise it
281 * will return the reason of the failure as the return
282 * code.
283 *
284 * This function is capable to create the appropriate
285 * response message when the verification has failed.
286 * If tdata is specified, then a non-2xx final response
287 * will be created and put in this argument upon return,
288 * when the verification has failed.
289 *
290 * If a dialog has been created prior to calling this
291 * function, then it MUST be specified in dlg argument.
292 * Otherwise application MUST specify the endpt argument
293 * (this is useful e.g. when application wants to send
294 * the response statelessly).
295 */
296PJ_DECL(pj_status_t) pjsip_inv_verify_request( pjsip_rx_data *rdata,
297 unsigned *options,
298 const pjmedia_sdp_session *sdp,
299 pjsip_dialog *dlg,
300 pjsip_endpoint *endpt,
301 pjsip_tx_data **tdata);
302
303
304/**
305 * Create UAS invite session for the specified dialog in dlg. Application
306 * SHOULD call the verification function before calling this function,
307 * to ensure that it can create the session successfully.
308 *
309 * @param dlg The dialog to be used.
310 * @param rdata Application MUST specify the received INVITE request
311 * in rdata. The invite session needs to inspect the
312 * received request to see if the request contains
313 * features that it supports.
314 * @param sdp If application has determined its media capability,
315 * it can specify this capability in this argument.
316 * If SDP is received in the initial INVITE, the UAS
317 * capability specified in this argument doesn't have to
318 * match the received offer; the SDP negotiator is able
319 * to rearrange the media lines in the answer so that it
320 * matches the offer.
321 * @param options The options argument is bitmask combination of SIP
322 * features in pjsip_inv_options enumeration.
323 * @param p_inv Pointer to receive the newly created invite session.
324 *
325 * @return On successful, the invite session will be put in
326 * p_inv argument and the function will return PJ_SUCCESS.
327 * Otherwise the appropriate error status will be returned
328 * on failure.
329 */
330PJ_DECL(pj_status_t) pjsip_inv_create_uas(pjsip_dialog *dlg,
331 pjsip_rx_data *rdata,
332 const pjmedia_sdp_session *local_sdp,
333 unsigned options,
334 pjsip_inv_session **p_inv);
335
336
337/**
Benny Prijonod4e0abd2006-03-05 11:53:36 +0000338 * Forcefully terminate and destroy INVITE session, regardless of
339 * the state of the session. Note that this function should only be used
340 * when there is failure in the INVITE session creation. After the
341 * invite session has been created and initialized, normally application
342 * SHOULD use #pjsip_inv_end_session() to end the INVITE session instead.
343 *
344 * Note also that this function may terminate the underlying dialog, if
345 * there are no other sessions in the dialog.
346 *
347 * @param inv The invite session.
348 * @param st_code Status code for the reason of the termination.
349 * @param notify If set to non-zero, then on_state_changed()
350 * callback will be called.
351 *
352 * @return PJ_SUCCESS if the INVITE session has been
353 * terminated.
354 */
355PJ_DECL(pj_status_t) pjsip_inv_terminate( pjsip_inv_session *inv,
356 int st_code,
357 pj_bool_t notify );
358
359
360/**
Benny Prijono268ca612006-02-07 12:34:11 +0000361 * Create the initial INVITE request for this session. This function can only
362 * be called for UAC session. If local media capability is specified when
363 * the invite session was created, then this function will put an SDP offer
364 * in the outgoing INVITE request. Otherwise the outgoing request will not
365 * contain SDP body.
366 *
367 * @param inv The UAC invite session.
368 * @param p_tdata The initial INVITE request will be put in this
369 * argument if it can be created successfully.
370 *
371 * @return PJ_SUCCESS if the INVITE request can be created.
372 */
373PJ_DECL(pj_status_t) pjsip_inv_invite( pjsip_inv_session *inv,
374 pjsip_tx_data **p_tdata );
375
376
377/**
Benny Prijono64f851e2006-02-23 13:49:28 +0000378 * Create the initial response message for the incoming INVITE request in
379 * rdata with status code st_code and optional status text st_text. Use
380 * #pjsip_answer() to create subsequent response message.
381 */
382PJ_DECL(pj_status_t) pjsip_inv_initial_answer( pjsip_inv_session *inv,
383 pjsip_rx_data *rdata,
384 int st_code,
385 const pj_str_t *st_text,
386 const pjmedia_sdp_session *sdp,
387 pjsip_tx_data **p_tdata);
388
389/**
Benny Prijono268ca612006-02-07 12:34:11 +0000390 * Create a response message to the initial INVITE request. This function
391 * can only be called for the initial INVITE request, as subsequent
392 * re-INVITE request will be answered automatically.
393 *
394 * @param inv The UAS invite session.
395 * @param st_code The st_code contains the status code to be sent,
396 * which may be a provisional or final response.
397 * @param st_text If custom status text is desired, application can
398 * specify the text in st_text; otherwise if this
399 * argument is NULL, default status text will be used.
400 * @param local_sdp If application has specified its media capability
401 * during creation of UAS invite session, the local_sdp
402 * argument MUST be NULL. This is because application
403 * can not perform more than one SDP offer/answer session
404 * in a single INVITE transaction.
405 * If application has not specified its media capability
406 * during creation of UAS invite session, it MAY or MUST
407 * specify its capability in local_sdp argument,
408 * depending whether st_code indicates a 2xx final
409 * response.
410 * @param p_tdata Pointer to receive the response message created by
411 * this function.
412 *
413 * @return PJ_SUCCESS if response message was created
414 * successfully.
415 */
416PJ_DECL(pj_status_t) pjsip_inv_answer( pjsip_inv_session *inv,
417 int st_code,
418 const pj_str_t *st_text,
419 const pjmedia_sdp_session *local_sdp,
420 pjsip_tx_data **p_tdata );
421
422
Benny Prijonoa66c7152006-02-09 01:26:14 +0000423/**
Benny Prijono26ff9062006-02-21 23:47:00 +0000424 * Set local answer to respond to remote SDP offer, to be carried by
425 * subsequent response (or request).
Benny Prijonoa66c7152006-02-09 01:26:14 +0000426 *
427 * @param inv The invite session.
428 * @param sdp The SDP description which will be set as answer
429 * to remote.
Benny Prijono26ff9062006-02-21 23:47:00 +0000430 *
431 * @return PJ_SUCCESS if local answer can be accepted by
432 * SDP negotiator.
Benny Prijonoa66c7152006-02-09 01:26:14 +0000433 */
Benny Prijono26ff9062006-02-21 23:47:00 +0000434PJ_DECL(pj_status_t) pjsip_inv_set_sdp_answer(pjsip_inv_session *inv,
435 const pjmedia_sdp_session *sdp );
436
Benny Prijonoa66c7152006-02-09 01:26:14 +0000437
Benny Prijono268ca612006-02-07 12:34:11 +0000438/**
439 * Create a SIP message to initiate invite session termination. Depending on
440 * the state of the session, this function may return CANCEL request,
441 * a non-2xx final response, or a BYE request. If the session has not answered
442 * the incoming INVITE, this function creates the non-2xx final response with
443 * the specified status code in st_code and optional status text in st_text.
444 *
445 * @param inv The invite session.
446 * @param st_code Status code to be used for terminating the session.
447 * @param st_text Optional status text.
448 * @param p_tdata Pointer to receive the message to be created.
449 *
450 * @return PJ_SUCCESS if termination message can be created.
451 */
452PJ_DECL(pj_status_t) pjsip_inv_end_session( pjsip_inv_session *inv,
453 int st_code,
454 const pj_str_t *st_text,
455 pjsip_tx_data **p_tdata );
456
457
458
459/**
460 * Create a re-INVITE request.
461 *
462 * @param inv The invite session.
463 * @param new_contact If application wants to update its local contact and
464 * inform peer to perform target refresh with a new
465 * contact, it can specify the new contact in this
466 * argument; otherwise this argument must be NULL.
467 * @param new_offer Application MAY initiate a new SDP offer/answer
468 * session in the request when there is no pending
469 * answer to be sent or received. It can detect this
470 * condition by observing the state of the SDP
471 * negotiator of the invite session. If new offer
472 * should be sent to remote, the offer must be specified
473 * in this argument, otherwise it must be NULL.
474 * @param p_tdata Pointer to receive the re-INVITE request message to
475 * be created.
476 *
477 * @return PJ_SUCCESS if a re-INVITE request with the specified
478 * characteristics (e.g. to contain new offer) can be
479 * created.
480 */
481PJ_DECL(pj_status_t) pjsip_inv_reinvite(pjsip_inv_session *inv,
482 const pj_str_t *new_contact,
483 const pjmedia_sdp_session *new_offer,
484 pjsip_tx_data **p_tdata );
485
486
487
488/**
489 * Create an UPDATE request.
490 *
491 * @param inv The invite session.
492 * @param new_contact If application wants to update its local contact
493 * and inform peer to perform target refresh with a new
494 * contact, it can specify the new contact in this
495 * argument; otherwise this argument must be NULL.
496 * @param new_offer Application MAY initiate a new SDP offer/answer
497 * session in the request when there is no pending answer
498 * to be sent or received. It can detect this condition
499 * by observing the state of the SDP negotiator of the
500 * invite session. If new offer should be sent to remote,
501 * the offer must be specified in this argument; otherwise
502 * this argument must be NULL.
503 * @param p_tdata Pointer to receive the UPDATE request message to
504 * be created.
505 *
506 * @return PJ_SUCCESS if a UPDATE request with the specified
507 * characteristics (e.g. to contain new offer) can be
508 * created.
509 */
510PJ_DECL(pj_status_t) pjsip_inv_update ( pjsip_inv_session *inv,
511 const pj_str_t *new_contact,
512 const pjmedia_sdp_session *new_offer,
513 pjsip_tx_data **p_tdata );
514
515
516/**
517 * Send request or response message in tdata.
518 *
519 * @param inv The invite session.
520 * @param tdata The message to be sent.
521 * @param token The token is an arbitrary application data that
522 * will be put in the transaction's mod_data array,
523 * at application module's index. Application can inspect
524 * this value when the framework reports the completion
525 * of the transaction that sends this message.
526 *
527 * @return PJ_SUCCESS if transaction can be initiated
528 * successfully to send this message. Note that the
529 * actual final state of the transaction itself will
530 * be reported later, in on_tsx_state_changed()
531 * callback.
532 */
533PJ_DECL(pj_status_t) pjsip_inv_send_msg(pjsip_inv_session *inv,
534 pjsip_tx_data *tdata,
535 void *token );
536
537
538
539/**
540 * Get the invite session for the dialog, if any.
541 *
542 * @param dlg The dialog which invite session is being queried.
543 *
544 * @return The invite session instance which has been
545 * associated with this dialog, or NULL.
546 */
547PJ_DECL(pjsip_inv_session*) pjsip_dlg_get_inv_session(pjsip_dialog *dlg);
548
549/**
550 * Get the invite session instance associated with transaction tsx, if any.
551 *
552 * @param tsx The transaction, which invite session is being
553 * queried.
554 *
555 * @return The invite session instance which has been
556 * associated with this transaction, or NULL.
557 */
558PJ_DECL(pjsip_inv_session*) pjsip_tsx_get_inv_session(pjsip_transaction *tsx);
559
560
561
Benny Prijonof3195072006-02-14 21:15:30 +0000562PJ_END_DECL
563
Benny Prijono268ca612006-02-07 12:34:11 +0000564
565
566#endif /* __SIP_INVITE_SESSION_H__ */