- Timestamp:
- Mar 27, 2007 11:29:27 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pjproject/trunk/pjnath/include/pjnath/ice_stream_transport.h
r1106 r1110 22 22 23 23 /** 24 * @file ice_ mt.h25 * @brief ICE Media Transport.24 * @file ice_stream_transport.h 25 * @brief ICE Stream Transport 26 26 */ 27 27 #include <pjnath/ice.h> … … 38 38 * @ingroup PJNATH_ICE 39 39 * @{ 40 */ 41 40 * 41 * This module describes ICE stream transport, as represented by #pj_ice_st 42 * structure, and is part of PJNATH - the Open Source NAT traversal helper 43 * library. 44 * 45 * ICE stream transport, as represented by #pj_ice_st structure, is an ICE 46 * capable component for transporting media within a media stream. 47 * It consists of one or more transport sockets (typically two for RTP 48 * based communication - one for RTP and one for RTCP), and an 49 * \ref PJNATH_ICE_SESSION for performing connectivity checks among the. 50 * various candidates of the transport addresses. 51 * 52 * \section PJNATH_ICE_STREAM_TRANSPORT_USING Using the ICE Stream Transport 53 * 54 * Application may use the ICE stream transport in two ways: 55 * - it can create the ICE stream transports once during application 56 * initialization and keep them alive throughout application lifetime, or 57 * - it can create and destroy the ICE stream transport as needed everytime 58 * a call is made and destroyed. 59 * 60 * Keeping the ICE stream transport alive throughout 61 * application's lifetime is normally preferable, as initializing the 62 * ICE stream transport may incur delay because the ICE stream transport 63 * would need to communicate with the STUN/TURN server to get the 64 * server reflexive and relayed candidates for the transports. 65 * 66 * Regardless of which usage scenario is being used, the ICE stream 67 * transport is capable for restarting the ICE session being used and to 68 * send STUN keep-alives for its STUN server reflexive and relayed 69 * candidates. 70 * 71 * \subsection PJNATH_ICE_ST_TRA_INIT Stream Transport Initialization 72 * 73 * Application creates the ICE stream transport by calling 74 * #pj_ice_st_create() function. 75 * 76 * After the ICE stream transport is created, application may set up the 77 * STUN servers to be used to obtain STUN server reflexive and relayed 78 * candidate, by calling #pj_ice_st_set_stun_domain() or 79 * #pj_ice_st_set_stun_srv(). Then it has to create each component by 80 * calling #pj_ice_st_create_comp(); this would create an actual socket 81 * which listens to the specified local address, and it would also 82 * perform lookup to find various transport address candidates for this 83 * socket. 84 * 85 * \subsection PJNATH_ICE_ST_TRA_INIT_ICE ICE Session Initialization 86 * 87 * When application is about to send an offer containing ICE capability, 88 * or when it receives an offer containing ICE capability, it would 89 * create the ICE session by calling #pj_ice_st_init_ice(). This would 90 * register all transport address aliases for each component to the ICE 91 * session as candidates. After this application can enumerate all local 92 * candidates by calling #pj_ice_st_enum_cands(), and encode these 93 * candidates in the SDP to be sent to remote agent. 94 * 95 * \subsection PJNATH_ICE_ST_TRA_START Starting Connectivity Checks 96 * 97 * Once application receives the SDP from remote, it can start ICE 98 * connectivity checks by calling #pj_ice_st_start_ice(), specifying 99 * the username, password, and candidates of the remote agent. The ICE 100 * session/transport will then notify the application via the callback 101 * when ICE connectivity checks completes, either successfully or with 102 * failure. 103 * 104 * \subsection PJNATH_ICE_ST_TRA_STOP Stopping ICE Session 105 * 106 * Once the call is terminated, application no longer needs to keep the 107 * ICE session, so it should call #pj_ice_st_stop_ice() to destroy the 108 * ICE session within this ICE stream transport. Note that this WILL NOT 109 * destroy the sockets/transports, it only destroys the ICE session 110 * within this ICE stream transport. 111 * 112 * \subsection PJNATH_ICE_ST_TRA_RESTART Restarting ICE Session 113 * 114 * When a new call is made, application can repeat the above 115 * #pj_ice_st_init_ice() to #pj_ice_st_stop_ice() cycle for the new call, 116 * using this same ICE stream transport. 117 * 118 * \subsection PJNATH_ICE_ST_TRA_DESTROY Destroying ICE Stream Transport 119 * 120 * Finally, when the ICE stream transport itself is no longer needed, 121 * for example when the application quits, application should call 122 * #pj_ice_st_destroy() to release back all resources allocated by this 123 * ICE stream transport. 124 * 125 */ 126 127 /** Forward declaration for ICE stream transport. */ 42 128 typedef struct pj_ice_st pj_ice_st; 43 129 130 /** 131 * This structure contains callbacks that will be called by the 132 * ICE stream transport. 133 */ 44 134 typedef struct pj_ice_st_cb 45 135 { 136 /** 137 * This callback will be called when the ICE transport receives 138 * incoming packet from the sockets which is not related to ICE 139 * (for example, normal RTP/RTCP packet destined for application). 140 * 141 * @param ice_st The ICE stream transport. 142 * @param comp_id The component ID. 143 * @param pkt The packet. 144 * @param size Size of the packet. 145 * @param src_addr Source address of the packet. 146 * @param src_addr_len Length of the source address. 147 */ 46 148 void (*on_rx_data)(pj_ice_st *ice_st, 47 149 unsigned comp_id, … … 49 151 const pj_sockaddr_t *src_addr, 50 152 unsigned src_addr_len); 153 154 /** 155 * This callback will be called when ICE checks have completed. 156 * This callback is optional. 157 * 158 * @param ice_st The ICE stream transport. 159 * @param status The ICE connectivity check status. 160 */ 51 161 void (*on_ice_complete)(pj_ice_st *ice_st, 52 162 pj_status_t status); … … 55 165 56 166 57 #ifndef PJ_ICE_ST_MAX_ALIASES 58 # define PJ_ICE_ST_MAX_ALIASES 8 59 #endif 60 167 /** 168 * Various flags that can be specified when creating a component with 169 * #pj_ice_st_create_comp(). These options may be combined together 170 * with bitmask operation. 171 */ 61 172 enum pj_ice_st_option 62 173 { 174 /** 175 * If this option is specified, only a listening socket will be 176 * created for the component, and no candidate will be added to 177 * the component. Application must add the component manually 178 * by inspecting the socket and transport address of the component. 179 */ 63 180 PJ_ICE_ST_OPT_DONT_ADD_CAND = 1, 181 182 /** 183 * If this option is specified, then no STUN reflexive candidate 184 * will be added to the component. 185 */ 64 186 PJ_ICE_ST_OPT_DISABLE_STUN = 2, 187 188 /** 189 * If this option is specified, then no STUN relay candidate 190 * will be added to the component. 191 */ 65 192 PJ_ICE_ST_OPT_DISABLE_RELAY = 4, 193 194 /** 195 * If this option is specified, then when the function fails to 196 * bind the socket to the specified port, it WILL NOT try to 197 * bind the socket to the next available port. 198 * 199 * If this option is NOT specified, then the function will try to 200 * bind the socket to next port+2, repetitively until the socket 201 * is bound successfully. 202 */ 66 203 PJ_ICE_ST_OPT_NO_PORT_RETRY = 8, 67 204 }; 68 205 69 206 207 /** 208 * This structure describes ICE stream transport candidate. A "candidate" 209 * in ICE stream transport can be viewed as alias transport address 210 * for the socket. 211 */ 70 212 typedef struct pj_ice_st_cand 71 213 { 214 /** 215 * Candidate type. 216 */ 72 217 pj_ice_cand_type type; 218 219 /** 220 * Status of this candidate. This status is useful for ICE reflexive 221 * and relay candidate, where the address needs to be resolved 222 * asynchronously by sending STUN request to STUN server. 223 * 224 * The value will be PJ_SUCCESS if candidate address has been resolved 225 * successfully, PJ_EPENDING when the address resolution process is 226 * in progress, or other value when the address resolution has 227 * completed with failure. 228 */ 73 229 pj_status_t status; 230 231 /** 232 * The candidate transport address. 233 */ 74 234 pj_sockaddr addr; 75 int cand_id; 235 236 /** 237 * The ICE session candidate ID after this candidate has been registered 238 * to an ICE session. Before ICE session is created, or after ICE 239 * session has been destroyed, the value will be -1. 240 */ 241 int ice_cand_id; 242 243 /** 244 * Local preference value, which typically is 65535. 245 */ 76 246 pj_uint16_t local_pref; 247 248 /** 249 * Foundation associated with this candidate, which value normally will be 250 * calculated by the function. 251 */ 77 252 pj_str_t foundation; 253 78 254 } pj_ice_st_cand; 79 255 80 256 257 /** 258 * This structure describes an ICE stream transport component. A component 259 * in ICE stream transport typically corresponds to a single socket created 260 * for this component, and bound to a specific transport address. This 261 * component may have multiple alias addresses, for example one alias 262 * address for each interfaces in multi-homed host, another for server 263 * reflexive alias, and another for relayed alias. For each transport 264 * address alias, an ICE stream transport candidate (#pj_ice_st_cand) will 265 * be created, and these candidates will eventually registered to the ICE 266 * session. 267 */ 81 268 typedef struct pj_ice_st_comp 82 269 { 83 pj_ice_st *ice_st; 84 unsigned comp_id; 85 pj_uint32_t options; 86 pj_sock_t sock; 87 88 pj_stun_session *stun_sess; 89 90 pj_sockaddr local_addr; 91 92 unsigned pending_cnt; 93 pj_status_t last_status; 94 95 unsigned cand_cnt; 96 pj_ice_st_cand cand_list[PJ_ICE_ST_MAX_ ALIASES];97 int default_cand; 98 99 pj_ioqueue_key_t *key; 100 pj_uint8_t pkt[1500]; 101 pj_ioqueue_op_key_t read_op; 102 pj_ioqueue_op_key_t write_op; 103 pj_sockaddr src_addr; 104 int src_addr_len; 270 pj_ice_st *ice_st; /**< ICE stream transport. */ 271 unsigned comp_id; /**< Component ID. */ 272 pj_uint32_t options; /**< Option flags. */ 273 pj_sock_t sock; /**< Socket descriptor. */ 274 275 pj_stun_session *stun_sess; /**< STUN session. */ 276 277 pj_sockaddr local_addr; /**< Local/base address. */ 278 279 unsigned pending_cnt; /**< Pending resolution cnt. */ 280 pj_status_t last_status; /**< Last status. */ 281 282 unsigned cand_cnt; /**< # of candidates/aliaes. */ 283 pj_ice_st_cand cand_list[PJ_ICE_ST_MAX_CAND]; /**< Cand array */ 284 int default_cand; /**< Default candidate selected */ 285 286 pj_ioqueue_key_t *key; /**< ioqueue key. */ 287 pj_uint8_t pkt[1500]; /**< Incoming packet buffer. */ 288 pj_ioqueue_op_key_t read_op; /**< ioqueue read operation key */ 289 pj_ioqueue_op_key_t write_op; /**< ioqueue write op. key */ 290 pj_sockaddr src_addr; /**< source packet address buf. */ 291 int src_addr_len; /**< length of src addr. buf. */ 105 292 106 293 } pj_ice_st_comp; 107 294 108 295 296 /** 297 * This structure represents the ICE stream transport. 298 */ 109 299 struct pj_ice_st 110 300 { 111 char obj_name[PJ_MAX_OBJ_NAME]; 112 pj_pool_t *pool; 113 void *user_data; 114 pj_stun_config stun_cfg; 115 pj_ice_st_cb cb; 116 117 pj_ice *ice; 118 119 unsigned comp_cnt; 120 pj_ice_st_comp **comp; 121 122 pj_dns_resolver *resolver; 123 pj_bool_t has_resolver_job; 124 pj_sockaddr_in stun_srv; 125 pj_sockaddr_in turn_srv; 301 char obj_name[PJ_MAX_OBJ_NAME]; /**< Log ID. */ 302 303 pj_pool_t *pool; /**< Pool used by this object. */ 304 void *user_data; /**< Application data. */ 305 pj_stun_config stun_cfg; /**< STUN settings. */ 306 pj_ice_st_cb cb; /**< Application callback. */ 307 308 pj_ice *ice; /**< ICE session. */ 309 310 unsigned comp_cnt; /**< Number of components. */ 311 pj_ice_st_comp **comp; /**< Components array. */ 312 313 pj_dns_resolver *resolver; /**< The resolver instance. */ 314 pj_bool_t has_rjob; /**< Has pending resolve? */ 315 pj_sockaddr_in stun_srv; /**< STUN server address. */ 316 pj_sockaddr_in turn_srv; /**< TURN server address. */ 126 317 }; 127 318 128 319 320 /** 321 * Create the ICE stream transport containing the specified number of 322 * components. After the ICE stream transport is created, application 323 * may initialize the STUN server settings, and after that it has to 324 * initialize each components by calling #pj_ice_st_create_comp() 325 * function. 326 * 327 * @param stun_cfg The STUN settings. 328 * @param name Optional name for logging identification. 329 * @param comp_cnt Number of components. 330 * @param user_data Arbitrary user data to be associated with this 331 * ICE stream transport. 332 * @param cb Callback. 333 * @param p_ice_st Pointer to receive the ICE stream transport 334 * instance. 335 * 336 * @return PJ_SUCCESS if ICE stream transport is created 337 * successfully. 338 */ 129 339 PJ_DECL(pj_status_t) pj_ice_st_create(pj_stun_config *stun_cfg, 130 340 const char *name, … … 133 343 const pj_ice_st_cb *cb, 134 344 pj_ice_st **p_ice_st); 345 346 /** 347 * Destroy the ICE stream transport. This will destroy the ICE session 348 * inside the ICE stream transport, close all sockets and release all 349 * other resources. 350 * 351 * @param ice_st The ICE stream transport. 352 * 353 * @return PJ_SUCCESS, or the appropriate error code. 354 */ 135 355 PJ_DECL(pj_status_t) pj_ice_st_destroy(pj_ice_st *ice_st); 136 356 357 358 /** 359 * Set the domain to be used when resolving the STUN servers. If application 360 * wants to utillize STUN, then STUN server must be specified, either by 361 * calling this function or by calling #pj_ice_st_set_stun_srv(). 362 * 363 * If application calls this function, then the STUN/TURN servers will 364 * be resolved by querying DNS SRV records for the specified domain. 365 * 366 * @param ice_st The ICE stream transport. 367 * @param resolver The resolver instance that will be used to 368 * resolve the STUN/TURN servers. 369 * @param domain The target domain. 370 * 371 * @return PJ_SUCCESS if DNS SRV resolution job can be 372 * started. The resolution process itself will 373 * complete asynchronously. 374 */ 137 375 PJ_DECL(pj_status_t) pj_ice_st_set_stun_domain(pj_ice_st *ice_st, 138 376 pj_dns_resolver *resolver, 139 377 const pj_str_t *domain); 378 379 /** 380 * Set the STUN and TURN server addresses. If application 381 * wants to utillize STUN, then STUN server must be specified, either by 382 * calling this function or by calling #pj_ice_st_set_stun_domain(). 383 * 384 * With this function, the STUN and TURN server addresses will be 385 * assigned immediately, that is no DNS resolution will need to be 386 * performed. 387 * 388 * @param ice_st The ICE stream transport. 389 * @param stun_srv The STUN server address, or NULL if STUN 390 * reflexive candidate is not to be used. 391 * @param turn_srv The TURN server address, or NULL if STUN 392 * relay candidate is not to be used. 393 * 394 * @return PJ_SUCCESS, or the appropriate error code. 395 */ 140 396 PJ_DECL(pj_status_t) pj_ice_st_set_stun_srv(pj_ice_st *ice_st, 141 397 const pj_sockaddr_in *stun_srv, 142 398 const pj_sockaddr_in *turn_srv); 143 399 400 /** 401 * Create and initialize the specified component. This function will 402 * instantiate the socket descriptor for this component, optionally 403 * bind the socket to the specified address (or bind to any address/port 404 * if the \a addr parameter is NULL), and start finding all alias 405 * addresses for this socket. For each alias addresses that if finds, 406 * it will add an ICE stream transport candidate for this component. 407 * 408 * After all components have been initialized, application should poll 409 * the #pj_ice_st_get_comps_status() peridically to check if STUN 410 * server reflexive and relayed candidates have been obtained 411 * successfully. 412 * 413 * @param ice_st The ICE stream transport. 414 * @param comp_id The component ID, which value must be greater than 415 * zero and less than or equal to the number of 416 * components in this ICE stream transport. 417 * @param options Options, see #pj_ice_st_option. 418 * @param addr Local address where socket will be bound to. This 419 * address will be used as follows: 420 * - if the value is NULL, then socket will be bound 421 * to any available port. 422 * - if the value is not NULL, then if the port number 423 * is not zero, it will used as the starting port 424 * where the socket will be bound to. If bind() to 425 * this port fails, this function will try to bind 426 * to port+2, repeatedly until it succeeded. 427 * If application doesn't want this function to 428 * retry binding the socket to other port, it can 429 * specify PJ_ICE_ST_OPT_NO_PORT_RETRY option. 430 * - if the value is not NULL, then if the address 431 * is not INADDR_ANY, this function will bind the 432 * socket to this particular interface only, and 433 * no other host candidates will be added for this 434 * socket. 435 * 436 * 437 * @return PJ_SUCCESS, or the appropriate error code. 438 */ 144 439 PJ_DECL(pj_status_t) pj_ice_st_create_comp(pj_ice_st *ice_st, 145 440 unsigned comp_id, 146 441 pj_uint32_t options, 147 442 const pj_sockaddr_in *addr); 443 444 /** 445 * Manually add a candidate (transport address alias) for the specified 446 * component. Normally application shouldn't need to use this function, 447 * as candidates will be added automatically when component is created 448 * with #pj_ice_st_create_comp(). 449 * 450 * @param ice_st ICE stream transport. 451 * @param comp_id The component ID. 452 * @param type The candidate type. 453 * @param local_pref The local preference for this candidate 454 * (typically the value is 65535). 455 * @param addr The candidate address. 456 * @param set_default Set to non-zero to make this candidate the 457 * default candidate for this component. 458 * 459 * @return PJ_SUCCESS, or the appropriate error code. 460 */ 148 461 PJ_DECL(pj_status_t) pj_ice_st_add_cand(pj_ice_st *ice_st, 149 462 unsigned comp_id, … … 153 466 pj_bool_t set_default); 154 467 468 /** 469 * Get the status of components in the ICE stream transports. Since 470 * some IP address candidates have to be obtained asynchronously (for 471 * example, the STUN reflexive or relay candidate), application can 472 * use this function to know whether the address resolution has 473 * completed. 474 * 475 * @param ice_st The ICE stream transport. 476 * 477 * @return PJ_SUCCESS if all candidates have been resolved 478 * successfully, PJ_EPENDING if transport resolution 479 * is still in progress, or other status on failure. 480 */ 155 481 PJ_DECL(pj_status_t) pj_ice_st_get_comps_status(pj_ice_st *ice_st); 156 482 483 /** 484 * Initialize the ICE session in the ICE stream transport. 485 * 486 * @param ice_st The ICE stream transport. 487 * @param role ICE role. 488 * @param local_ufrag Optional local username fragment. 489 * @param local_passwd Optional local password. 490 * 491 * @return PJ_SUCCESS, or the appropriate error code. 492 */ 157 493 PJ_DECL(pj_status_t) pj_ice_st_init_ice(pj_ice_st *ice_st, 158 494 pj_ice_role role, 159 495 const pj_str_t *local_ufrag, 160 496 const pj_str_t *local_passwd); 497 498 /** 499 * Enumerate the local candidates. This function can only be called 500 * after the ICE session has been created in the ICE stream transport. 501 * 502 * @param ice_st The ICE stream transport. 503 * @param count On input, it specifies the maximum number of 504 * elements. On output, it will be filled with 505 * the number of candidates copied to the 506 * array. 507 * @param cand Array of candidates. 508 * 509 * @return PJ_SUCCESS, or the appropriate error code. 510 */ 161 511 PJ_DECL(pj_status_t) pj_ice_st_enum_cands(pj_ice_st *ice_st, 162 512 unsigned *count, 163 513 pj_ice_cand cand[]); 514 515 /** 516 * Start ICE connectivity checks. This function can only be called 517 * after the ICE session has been created in the ICE stream transport. 518 * 519 * This function will pair the local and remote candidates to create 520 * check list. Once the check list is created and sorted based on the 521 * priority, ICE periodic checks will be started. This function will 522 * return immediately, and application will be notified about the 523 * connectivity check status in the callback. 524 * 525 * @param ice_st The ICE stream transport. 526 * @param rem_ufrag Remote ufrag, as seen in the SDP received from 527 * the remote agent. 528 * @param rem_passwd Remote password, as seen in the SDP received from 529 * the remote agent. 530 * @param rem_cand_cnt Number of remote candidates. 531 * @param rem_cand Remote candidate array. 532 * 533 * @return PJ_SUCCESS, or the appropriate error code. 534 */ 164 535 PJ_DECL(pj_status_t) pj_ice_st_start_ice(pj_ice_st *ice_st, 165 536 const pj_str_t *rem_ufrag, … … 167 538 unsigned rem_cand_cnt, 168 539 const pj_ice_cand rem_cand[]); 540 541 /** 542 * Stop and destroy the ICE session inside this media transport. 543 * 544 * @param ice_st The ICE stream transport. 545 * 546 * @return PJ_SUCCESS, or the appropriate error code. 547 */ 169 548 PJ_DECL(pj_status_t) pj_ice_st_stop_ice(pj_ice_st *ice_st); 170 549 550 551 /** 552 * Send outgoing packet using this transport. If ICE checks have not 553 * produced a valid check for the specified component ID, this function 554 * will return with failure. Otherwise it will send the packet to remote 555 * destination using the nominated local candidate as have been checked 556 * previously. 557 * 558 * @param ice_st The ICE stream transport. 559 * @param comp_id Component ID. 560 * @param data The data or packet to be sent. 561 * @param data_len Size of data or packet, in bytes. 562 * @param dst_addr The destination address. 563 * @param dst_addr_len Length of destination address. 564 * 565 * @return PJ_SUCCESS if data is sent successfully. 566 */ 171 567 PJ_DECL(pj_status_t) pj_ice_st_sendto(pj_ice_st *ice_st, 172 568 unsigned comp_id,
Note: See TracChangeset
for help on using the changeset viewer.