Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 1 | /* $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 | |
| 25 | typedef enum pjsip_inv_state pjsip_inv_state; |
| 26 | typedef struct pjsip_inv_session pjsip_inv_session; |
| 27 | typedef struct pjsip_inv_callback pjsip_inv_callback; |
| 28 | |
| 29 | /** |
| 30 | * This enumeration describes invite session state. |
| 31 | */ |
| 32 | enum pjsip_inv_state |
| 33 | { |
| 34 | PJSIP_INV_STATE_NULL, /**< Before INVITE is sent or received */ |
| 35 | PJSIP_INV_STATE_CALLING, /**< After INVITE is sent */ |
| 36 | PJSIP_INV_STATE_INCOMING, /**< After INVITE is received. */ |
| 37 | PJSIP_INV_STATE_EARLY, /**< After response with To tag. */ |
| 38 | PJSIP_INV_STATE_CONNECTING, /**< After 2xx is sent/received. */ |
| 39 | PJSIP_INV_STATE_CONFIRMED, /**< After ACK is sent/received. */ |
| 40 | PJSIP_INV_STATE_DISCONNECTED, /**< Session is terminated. */ |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 41 | }; |
| 42 | |
| 43 | /** |
| 44 | * This structure contains callbacks to be registered by application to |
| 45 | * receieve notifications from the framework about various events in |
| 46 | * the invite session. |
| 47 | */ |
| 48 | struct pjsip_inv_callback |
| 49 | { |
| 50 | /** |
| 51 | * This callback is called when the invite sesion state has changed. |
| 52 | * Application should inspect the session state (inv_sess->state) to get |
| 53 | * the current state of the session. |
| 54 | * |
| 55 | * This callback is mandatory. |
| 56 | * |
| 57 | * @param inv The invite session. |
| 58 | * @param e The event which has caused the invite session's |
| 59 | * state to change. |
| 60 | */ |
| 61 | void (*on_state_changed)(pjsip_inv_session *inv, pjsip_event *e); |
| 62 | |
| 63 | |
| 64 | /** |
| 65 | * This callback is called when the invite usage module has created |
| 66 | * a new dialog and invite because of forked outgoing request. |
| 67 | * |
| 68 | * This callback is mandatory. |
| 69 | * |
| 70 | * @param inv The new invite session. |
| 71 | * @param e The event which has caused the dialog to fork. |
| 72 | * The type of this event can be either |
| 73 | * PJSIP_EVENT_RX_MSG or PJSIP_EVENT_RX_200_MSG. |
| 74 | */ |
| 75 | void (*on_new_session)(pjsip_inv_session *inv, pjsip_event *e); |
| 76 | |
| 77 | /** |
| 78 | * This callback is called whenever any transactions within the session |
| 79 | * has changed their state. Application MAY implement this callback, |
| 80 | * e.g. to monitor the progress of an outgoing request. |
| 81 | * |
| 82 | * This callback is optional. |
| 83 | * |
| 84 | * @param inv The invite session. |
| 85 | * @param tsx The transaction, which state has changed. |
| 86 | * @param e The event which has caused the transation state's |
| 87 | * to change. |
| 88 | */ |
| 89 | void (*on_tsx_state_changed)(pjsip_inv_session *inv, |
| 90 | pjsip_transaction *tsx, |
| 91 | pjsip_event *e); |
| 92 | |
| 93 | /** |
| 94 | * This callback is called when the invite session has received |
| 95 | * new offer from peer. Application can inspect the remote offer |
| 96 | * by calling negotiator's pjmedia_sdp_neg_get_neg_remote(), and |
| 97 | * optionally specify a modified answer. |
| 98 | * |
| 99 | * This callback is optional. When it's not specified, the default |
| 100 | * behavior is nothing. After calling this callback, the negotiator |
| 101 | * will negotiate remote offer with session's initial capability. |
| 102 | * |
| 103 | * @param inv The invite session. |
| 104 | */ |
| 105 | void (*on_rx_offer)(pjsip_inv_session *inv); |
| 106 | |
| 107 | /** |
| 108 | * This callback is called after SDP offer/answer session has completed. |
| 109 | * The status argument specifies the status of the offer/answer, |
| 110 | * as returned by pjmedia_sdp_neg_negotiate(). |
| 111 | * |
| 112 | * This callback is optional (from the point of view of the framework), |
| 113 | * but all useful applications normally need to implement this callback. |
| 114 | * |
| 115 | * @param inv The invite session. |
| 116 | * @param status The negotiation status. |
| 117 | */ |
| 118 | void (*on_media_update)(pjsip_inv_session *inv_ses, |
| 119 | pj_status_t status); |
| 120 | |
| 121 | }; |
| 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 | */ |
| 131 | enum 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 | */ |
| 167 | struct pjsip_inv_session |
| 168 | { |
| 169 | pj_pool_t *pool; |
| 170 | pjsip_inv_state state; |
| 171 | pjsip_dialog *dlg; |
| 172 | pjsip_role_e role; |
| 173 | unsigned options; |
| 174 | pjmedia_sdp_neg *neg; |
| 175 | pjsip_transaction *invite_tsx; |
| 176 | void *mod_data[PJSIP_MAX_MODULE]; |
| 177 | }; |
| 178 | |
| 179 | |
| 180 | /** |
| 181 | * Initialize the invite usage module and register it to the endpoint. |
| 182 | * The callback argument contains pointer to functions to be called on |
| 183 | * occurences of events in invite sessions. |
| 184 | * |
| 185 | * @param endpt The endpoint instance. |
| 186 | * @param app_module Application module. |
| 187 | * @param callback Callback structure. |
| 188 | * |
| 189 | * @return PJ_SUCCESS on success, or the appropriate error code. |
| 190 | */ |
| 191 | PJ_DECL(pj_status_t) pjsip_inv_usage_init(pjsip_endpoint *endpt, |
| 192 | pjsip_module *app_module, |
| 193 | const pjsip_inv_callback *cb); |
| 194 | |
| 195 | /** |
| 196 | * Get the INVITE usage module instance. |
| 197 | * |
| 198 | * @return PJ_SUCCESS on success, or the appropriate error code. |
| 199 | */ |
| 200 | PJ_DECL(pjsip_module*) pjsip_inv_usage_instance(void); |
| 201 | |
| 202 | |
| 203 | |
| 204 | /** |
| 205 | * Create UAC invite session for the specified dialog in dlg. |
| 206 | * |
| 207 | * @param dlg The dialog which will be used by this invite session. |
| 208 | * @param local_sdp If application has determined its media capability, |
| 209 | * it can specify the SDP here. Otherwise it can leave |
| 210 | * this to NULL, to let remote UAS specifies an offer. |
| 211 | * @param options The options argument is bitmask combination of SIP |
| 212 | * features in pjsip_inv_options enumeration. |
| 213 | * @param p_inv On successful return, the invite session will be put |
| 214 | * in this argument. |
| 215 | * |
| 216 | * @return The function will return PJ_SUCCESS if it can create |
| 217 | * the session. Otherwise the appropriate error status |
| 218 | * will be returned on failure. |
| 219 | */ |
| 220 | PJ_DECL(pj_status_t) pjsip_inv_create_uac(pjsip_dialog *dlg, |
| 221 | const pjmedia_sdp_session *local_sdp, |
| 222 | unsigned options, |
| 223 | pjsip_inv_session **p_inv); |
| 224 | |
| 225 | |
| 226 | /** |
| 227 | * Application SHOULD call this function upon receiving the initial INVITE |
| 228 | * request in rdata before creating the invite session (or even dialog), |
| 229 | * to verify that the invite session can handle the INVITE request. |
| 230 | * This function verifies that local endpoint is capable to handle required |
| 231 | * SIP extensions in the request (i.e. Require header) and also the media, |
| 232 | * if media description is present in the request. |
| 233 | * |
| 234 | * @param rdata The incoming INVITE request. |
| 235 | * |
| 236 | * @param options Upon calling this function, the options argument |
| 237 | * MUST contain the desired SIP extensions to be |
| 238 | * applied to the session. Upon return, this argument |
| 239 | * will contain the SIP extension that will be applied |
| 240 | * to the session, after considering the Supported, |
| 241 | * Require, and Allow headers in the request. |
| 242 | * |
| 243 | * @param sdp If local media capability has been determined, |
| 244 | * and if application wishes to verify that it can |
| 245 | * handle the media offer in the incoming INVITE |
| 246 | * request, it SHOULD specify its local media capability |
| 247 | * in this argument. |
| 248 | * If it is not specified, media verification will not |
| 249 | * be performed by this function. |
| 250 | * |
| 251 | * @param dlg If tdata is not NULL, application needs to specify |
| 252 | * how to create the response. Either dlg or endpt |
| 253 | * argument MUST be specified, with dlg argument takes |
| 254 | * precedence when both are specified. |
| 255 | * |
| 256 | * If a dialog has been created prior to calling this |
| 257 | * function, then it MUST be specified in dlg argument. |
| 258 | * Otherwise application MUST specify the endpt argument |
| 259 | * (this is useful e.g. when application wants to send |
| 260 | * the response statelessly). |
| 261 | * |
| 262 | * @param endpt If tdata is not NULL, application needs to specify |
| 263 | * how to create the response. Either dlg or endpt |
| 264 | * argument MUST be specified, with dlg argument takes |
| 265 | * precedence when both are specified. |
| 266 | * |
| 267 | * @param tdata If this argument is not NULL, this function will |
| 268 | * create the appropriate non-2xx final response message |
| 269 | * when the verification fails. |
| 270 | * |
| 271 | * @return If everything has been negotiated successfully, |
| 272 | * the function will return PJ_SUCCESS. Otherwise it |
| 273 | * will return the reason of the failure as the return |
| 274 | * code. |
| 275 | * |
| 276 | * This function is capable to create the appropriate |
| 277 | * response message when the verification has failed. |
| 278 | * If tdata is specified, then a non-2xx final response |
| 279 | * will be created and put in this argument upon return, |
| 280 | * when the verification has failed. |
| 281 | * |
| 282 | * If a dialog has been created prior to calling this |
| 283 | * function, then it MUST be specified in dlg argument. |
| 284 | * Otherwise application MUST specify the endpt argument |
| 285 | * (this is useful e.g. when application wants to send |
| 286 | * the response statelessly). |
| 287 | */ |
| 288 | PJ_DECL(pj_status_t) pjsip_inv_verify_request( pjsip_rx_data *rdata, |
| 289 | unsigned *options, |
| 290 | const pjmedia_sdp_session *sdp, |
| 291 | pjsip_dialog *dlg, |
| 292 | pjsip_endpoint *endpt, |
| 293 | pjsip_tx_data **tdata); |
| 294 | |
| 295 | |
| 296 | /** |
| 297 | * Create UAS invite session for the specified dialog in dlg. Application |
| 298 | * SHOULD call the verification function before calling this function, |
| 299 | * to ensure that it can create the session successfully. |
| 300 | * |
| 301 | * @param dlg The dialog to be used. |
| 302 | * @param rdata Application MUST specify the received INVITE request |
| 303 | * in rdata. The invite session needs to inspect the |
| 304 | * received request to see if the request contains |
| 305 | * features that it supports. |
| 306 | * @param sdp If application has determined its media capability, |
| 307 | * it can specify this capability in this argument. |
| 308 | * If SDP is received in the initial INVITE, the UAS |
| 309 | * capability specified in this argument doesn't have to |
| 310 | * match the received offer; the SDP negotiator is able |
| 311 | * to rearrange the media lines in the answer so that it |
| 312 | * matches the offer. |
| 313 | * @param options The options argument is bitmask combination of SIP |
| 314 | * features in pjsip_inv_options enumeration. |
| 315 | * @param p_inv Pointer to receive the newly created invite session. |
| 316 | * |
| 317 | * @return On successful, the invite session will be put in |
| 318 | * p_inv argument and the function will return PJ_SUCCESS. |
| 319 | * Otherwise the appropriate error status will be returned |
| 320 | * on failure. |
| 321 | */ |
| 322 | PJ_DECL(pj_status_t) pjsip_inv_create_uas(pjsip_dialog *dlg, |
| 323 | pjsip_rx_data *rdata, |
| 324 | const pjmedia_sdp_session *local_sdp, |
| 325 | unsigned options, |
| 326 | pjsip_inv_session **p_inv); |
| 327 | |
| 328 | |
| 329 | /** |
| 330 | * Create the initial INVITE request for this session. This function can only |
| 331 | * be called for UAC session. If local media capability is specified when |
| 332 | * the invite session was created, then this function will put an SDP offer |
| 333 | * in the outgoing INVITE request. Otherwise the outgoing request will not |
| 334 | * contain SDP body. |
| 335 | * |
| 336 | * @param inv The UAC invite session. |
| 337 | * @param p_tdata The initial INVITE request will be put in this |
| 338 | * argument if it can be created successfully. |
| 339 | * |
| 340 | * @return PJ_SUCCESS if the INVITE request can be created. |
| 341 | */ |
| 342 | PJ_DECL(pj_status_t) pjsip_inv_invite( pjsip_inv_session *inv, |
| 343 | pjsip_tx_data **p_tdata ); |
| 344 | |
| 345 | |
| 346 | /** |
| 347 | * Create a response message to the initial INVITE request. This function |
| 348 | * can only be called for the initial INVITE request, as subsequent |
| 349 | * re-INVITE request will be answered automatically. |
| 350 | * |
| 351 | * @param inv The UAS invite session. |
| 352 | * @param st_code The st_code contains the status code to be sent, |
| 353 | * which may be a provisional or final response. |
| 354 | * @param st_text If custom status text is desired, application can |
| 355 | * specify the text in st_text; otherwise if this |
| 356 | * argument is NULL, default status text will be used. |
| 357 | * @param local_sdp If application has specified its media capability |
| 358 | * during creation of UAS invite session, the local_sdp |
| 359 | * argument MUST be NULL. This is because application |
| 360 | * can not perform more than one SDP offer/answer session |
| 361 | * in a single INVITE transaction. |
| 362 | * If application has not specified its media capability |
| 363 | * during creation of UAS invite session, it MAY or MUST |
| 364 | * specify its capability in local_sdp argument, |
| 365 | * depending whether st_code indicates a 2xx final |
| 366 | * response. |
| 367 | * @param p_tdata Pointer to receive the response message created by |
| 368 | * this function. |
| 369 | * |
| 370 | * @return PJ_SUCCESS if response message was created |
| 371 | * successfully. |
| 372 | */ |
| 373 | PJ_DECL(pj_status_t) pjsip_inv_answer( pjsip_inv_session *inv, |
| 374 | int st_code, |
| 375 | const pj_str_t *st_text, |
| 376 | const pjmedia_sdp_session *local_sdp, |
| 377 | pjsip_tx_data **p_tdata ); |
| 378 | |
| 379 | |
Benny Prijono | a66c715 | 2006-02-09 01:26:14 +0000 | [diff] [blame^] | 380 | #if 0 |
| 381 | If we implement this, we need to send SDP answer automatically on |
| 382 | subsequent request/response. Something like "has_answer" flag. |
| 383 | |
| 384 | /** |
| 385 | * Set local answer to respond to remote SDP offer. By calling this function, |
| 386 | * application can start SDP negotiation immediately without having to |
| 387 | * send any request or response message. This function is normally called |
| 388 | * when on_rx_offer() callback is called. |
| 389 | * |
| 390 | * @param inv The invite session. |
| 391 | * @param sdp The SDP description which will be set as answer |
| 392 | * to remote. |
| 393 | */ |
| 394 | PJ_DECL(pj_status_t) pjsip_inv_set_sdp_answer( pjsip_inv_session *inv, |
| 395 | const pjmedia_sdp_session *sdp ); |
| 396 | #endif |
| 397 | |
Benny Prijono | 268ca61 | 2006-02-07 12:34:11 +0000 | [diff] [blame] | 398 | /** |
| 399 | * Create a SIP message to initiate invite session termination. Depending on |
| 400 | * the state of the session, this function may return CANCEL request, |
| 401 | * a non-2xx final response, or a BYE request. If the session has not answered |
| 402 | * the incoming INVITE, this function creates the non-2xx final response with |
| 403 | * the specified status code in st_code and optional status text in st_text. |
| 404 | * |
| 405 | * @param inv The invite session. |
| 406 | * @param st_code Status code to be used for terminating the session. |
| 407 | * @param st_text Optional status text. |
| 408 | * @param p_tdata Pointer to receive the message to be created. |
| 409 | * |
| 410 | * @return PJ_SUCCESS if termination message can be created. |
| 411 | */ |
| 412 | PJ_DECL(pj_status_t) pjsip_inv_end_session( pjsip_inv_session *inv, |
| 413 | int st_code, |
| 414 | const pj_str_t *st_text, |
| 415 | pjsip_tx_data **p_tdata ); |
| 416 | |
| 417 | |
| 418 | |
| 419 | /** |
| 420 | * Create a re-INVITE request. |
| 421 | * |
| 422 | * @param inv The invite session. |
| 423 | * @param new_contact If application wants to update its local contact and |
| 424 | * inform peer to perform target refresh with a new |
| 425 | * contact, it can specify the new contact in this |
| 426 | * argument; otherwise this argument must be NULL. |
| 427 | * @param new_offer Application MAY initiate a new SDP offer/answer |
| 428 | * session in the request when there is no pending |
| 429 | * answer to be sent or received. It can detect this |
| 430 | * condition by observing the state of the SDP |
| 431 | * negotiator of the invite session. If new offer |
| 432 | * should be sent to remote, the offer must be specified |
| 433 | * in this argument, otherwise it must be NULL. |
| 434 | * @param p_tdata Pointer to receive the re-INVITE request message to |
| 435 | * be created. |
| 436 | * |
| 437 | * @return PJ_SUCCESS if a re-INVITE request with the specified |
| 438 | * characteristics (e.g. to contain new offer) can be |
| 439 | * created. |
| 440 | */ |
| 441 | PJ_DECL(pj_status_t) pjsip_inv_reinvite(pjsip_inv_session *inv, |
| 442 | const pj_str_t *new_contact, |
| 443 | const pjmedia_sdp_session *new_offer, |
| 444 | pjsip_tx_data **p_tdata ); |
| 445 | |
| 446 | |
| 447 | |
| 448 | /** |
| 449 | * Create an UPDATE request. |
| 450 | * |
| 451 | * @param inv The invite session. |
| 452 | * @param new_contact If application wants to update its local contact |
| 453 | * and inform peer to perform target refresh with a new |
| 454 | * contact, it can specify the new contact in this |
| 455 | * argument; otherwise this argument must be NULL. |
| 456 | * @param new_offer Application MAY initiate a new SDP offer/answer |
| 457 | * session in the request when there is no pending answer |
| 458 | * to be sent or received. It can detect this condition |
| 459 | * by observing the state of the SDP negotiator of the |
| 460 | * invite session. If new offer should be sent to remote, |
| 461 | * the offer must be specified in this argument; otherwise |
| 462 | * this argument must be NULL. |
| 463 | * @param p_tdata Pointer to receive the UPDATE request message to |
| 464 | * be created. |
| 465 | * |
| 466 | * @return PJ_SUCCESS if a UPDATE request with the specified |
| 467 | * characteristics (e.g. to contain new offer) can be |
| 468 | * created. |
| 469 | */ |
| 470 | PJ_DECL(pj_status_t) pjsip_inv_update ( pjsip_inv_session *inv, |
| 471 | const pj_str_t *new_contact, |
| 472 | const pjmedia_sdp_session *new_offer, |
| 473 | pjsip_tx_data **p_tdata ); |
| 474 | |
| 475 | |
| 476 | /** |
| 477 | * Send request or response message in tdata. |
| 478 | * |
| 479 | * @param inv The invite session. |
| 480 | * @param tdata The message to be sent. |
| 481 | * @param token The token is an arbitrary application data that |
| 482 | * will be put in the transaction's mod_data array, |
| 483 | * at application module's index. Application can inspect |
| 484 | * this value when the framework reports the completion |
| 485 | * of the transaction that sends this message. |
| 486 | * |
| 487 | * @return PJ_SUCCESS if transaction can be initiated |
| 488 | * successfully to send this message. Note that the |
| 489 | * actual final state of the transaction itself will |
| 490 | * be reported later, in on_tsx_state_changed() |
| 491 | * callback. |
| 492 | */ |
| 493 | PJ_DECL(pj_status_t) pjsip_inv_send_msg(pjsip_inv_session *inv, |
| 494 | pjsip_tx_data *tdata, |
| 495 | void *token ); |
| 496 | |
| 497 | |
| 498 | |
| 499 | /** |
| 500 | * Get the invite session for the dialog, if any. |
| 501 | * |
| 502 | * @param dlg The dialog which invite session is being queried. |
| 503 | * |
| 504 | * @return The invite session instance which has been |
| 505 | * associated with this dialog, or NULL. |
| 506 | */ |
| 507 | PJ_DECL(pjsip_inv_session*) pjsip_dlg_get_inv_session(pjsip_dialog *dlg); |
| 508 | |
| 509 | /** |
| 510 | * Get the invite session instance associated with transaction tsx, if any. |
| 511 | * |
| 512 | * @param tsx The transaction, which invite session is being |
| 513 | * queried. |
| 514 | * |
| 515 | * @return The invite session instance which has been |
| 516 | * associated with this transaction, or NULL. |
| 517 | */ |
| 518 | PJ_DECL(pjsip_inv_session*) pjsip_tsx_get_inv_session(pjsip_transaction *tsx); |
| 519 | |
| 520 | |
| 521 | |
| 522 | |
| 523 | |
| 524 | #endif /* __SIP_INVITE_SESSION_H__ */ |