transport_ice.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. /*
  2. * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
  3. * Copyright (C) 2003-2008 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 __PJMEDIA_TRANSPORT_ICE_H__
  20. #define __PJMEDIA_TRANSPORT_ICE_H__
  21. /**
  22. * @file transport_ice.h
  23. * @brief ICE capable media transport.
  24. */
  25. #include <pjmedia/stream.h>
  26. #include <pjnath/ice_strans.h>
  27. /**
  28. * @defgroup PJMEDIA_TRANSPORT_ICE ICE Media Transport
  29. * @ingroup PJMEDIA_TRANSPORT
  30. * @brief Interactive Connectivity Establishment (ICE) transport
  31. * @{
  32. *
  33. * This describes the implementation of media transport using
  34. * Interactive Connectivity Establishment (ICE) protocol.
  35. */
  36. PJ_BEGIN_DECL
  37. /**
  38. * Structure containing callbacks to receive ICE notifications.
  39. */
  40. typedef struct pjmedia_ice_cb
  41. {
  42. /**
  43. * This callback will be called when ICE negotiation completes.
  44. *
  45. * @param tp PJMEDIA ICE transport.
  46. * @param op The operation
  47. * @param status Operation status.
  48. */
  49. void (*on_ice_complete)(pjmedia_transport *tp,
  50. pj_ice_strans_op op,
  51. pj_status_t status);
  52. /**
  53. * This callback will be called when ICE negotiation completes, with
  54. * application user data. Note that if both callbacks are implemented,
  55. * only this callback will be invoked.
  56. *
  57. * @param tp PJMEDIA ICE transport.
  58. * @param op The operation
  59. * @param status Operation status.
  60. * @param user_data User data for this callback.
  61. */
  62. void (*on_ice_complete2)(pjmedia_transport *tp,
  63. pj_ice_strans_op op,
  64. pj_status_t status,
  65. void *user_data);
  66. /**
  67. * Callback to report a new ICE local candidate, e.g: after successful
  68. * STUN Binding, after a successful TURN allocation. Only new candidates
  69. * whose type is server reflexive or relayed will be notified via this
  70. * callback. This callback also indicates end-of-candidate via parameter
  71. * 'last'.
  72. *
  73. * Trickle ICE can use this callback to convey the new candidate
  74. * to remote agent and monitor end-of-candidate indication.
  75. *
  76. * @param tp PJMEDIA ICE transport.
  77. * @param cand The new local candidate, can be NULL when the last
  78. * local candidate initialization failed/timeout.
  79. * @param last PJ_TRUE if this is the last of local candidate.
  80. */
  81. void (*on_new_candidate)(pjmedia_transport *tp,
  82. const pj_ice_sess_cand *cand,
  83. pj_bool_t last);
  84. } pjmedia_ice_cb;
  85. /**
  86. * This structure specifies ICE transport specific info. This structure
  87. * will be filled in media transport specific info.
  88. */
  89. typedef struct pjmedia_ice_transport_info
  90. {
  91. /**
  92. * Specifies whether ICE is used, i.e. SDP offer and answer indicates
  93. * that both parties support ICE and ICE should be used for the session.
  94. */
  95. pj_bool_t active;
  96. /**
  97. * ICE sesion state.
  98. */
  99. pj_ice_strans_state sess_state;
  100. /**
  101. * Session role.
  102. */
  103. pj_ice_sess_role role;
  104. pj_str_t loc_ufrag;
  105. pj_str_t rem_ufrag;
  106. /**
  107. * Number of components in the component array. Before ICE negotiation
  108. * is complete, the number represents the number of components of the
  109. * local agent. After ICE negotiation has been completed successfully,
  110. * the number represents the number of common components between local
  111. * and remote agents.
  112. */
  113. unsigned comp_cnt;
  114. /**
  115. * Array of ICE components. Typically the first element denotes RTP and
  116. * second element denotes RTCP.
  117. */
  118. struct
  119. {
  120. /**
  121. * Local candidate type.
  122. */
  123. pj_ice_cand_type lcand_type;
  124. /**
  125. * The local address.
  126. */
  127. pj_sockaddr lcand_addr;
  128. /**
  129. * Remote candidate type.
  130. */
  131. pj_ice_cand_type rcand_type;
  132. /**
  133. * Remote address.
  134. */
  135. pj_sockaddr rcand_addr;
  136. } comp[2];
  137. } pjmedia_ice_transport_info;
  138. /**
  139. * Options that can be specified when creating ICE transport.
  140. */
  141. enum pjmedia_transport_ice_options
  142. {
  143. /**
  144. * Normally when remote doesn't use ICE, the ICE transport will
  145. * continuously check the source address of incoming packets to see
  146. * if it is different than the configured remote address, and switch
  147. * the remote address to the source address of the packet if they
  148. * are different after several packets are received.
  149. * Specifying this option will disable this feature.
  150. */
  151. PJMEDIA_ICE_NO_SRC_ADDR_CHECKING = 1,
  152. /**
  153. * The standard (rfc5245) specify that ice-mismatch attribute is used
  154. * due to a mismatch of candidates with the default destination for media
  155. * signaled in the SDP. The purpose is to identify some poorly ALGs that
  156. * alter signaling information in ways that break ICE
  157. * (e.g., by rewriting IP addresses in SDP). Specifying this option is
  158. * to disable the ice mismatch check and allow ICE to continue
  159. * if such scenario occur.
  160. */
  161. PJMEDIA_ICE_DISABLE_ICE_MISMATCH = 2
  162. };
  163. /**
  164. * Create the Interactive Connectivity Establishment (ICE) media transport
  165. * using the specified configuration. When STUN or TURN (or both) is used,
  166. * the creation operation will complete asynchronously, when STUN resolution
  167. * and TURN allocation completes. When the initialization completes, the
  168. * \a on_ice_complete() complete will be called with \a op parameter equal
  169. * to PJ_ICE_STRANS_OP_INIT.
  170. *
  171. * In addition, this transport will also notify the application about the
  172. * result of ICE negotiation, also in \a on_ice_complete() callback. In this
  173. * case the callback will be called with \a op parameter equal to
  174. * PJ_ICE_STRANS_OP_NEGOTIATION.
  175. *
  176. * Other than this, application should use the \ref PJMEDIA_TRANSPORT API
  177. * to manipulate this media transport.
  178. *
  179. * @param endpt The media endpoint.
  180. * @param name Optional name to identify this ICE media transport
  181. * for logging purposes.
  182. * @param comp_cnt Number of components to be created.
  183. * @param cfg Pointer to configuration settings.
  184. * @param cb Optional structure containing ICE specific callbacks.
  185. * @param p_tp Pointer to receive the media transport instance.
  186. *
  187. * @return PJ_SUCCESS on success, or the appropriate error code.
  188. */
  189. PJ_DECL(pj_status_t) pjmedia_ice_create(pjmedia_endpt *endpt,
  190. const char *name,
  191. unsigned comp_cnt,
  192. const pj_ice_strans_cfg *cfg,
  193. const pjmedia_ice_cb *cb,
  194. pjmedia_transport **p_tp);
  195. /**
  196. * The same as #pjmedia_ice_create() with additional \a options param.
  197. *
  198. * @param endpt The media endpoint.
  199. * @param name Optional name to identify this ICE media transport
  200. * for logging purposes.
  201. * @param comp_cnt Number of components to be created.
  202. * @param cfg Pointer to configuration settings.
  203. * @param cb Optional structure containing ICE specific callbacks.
  204. * @param options Options, see #pjmedia_transport_ice_options.
  205. * @param p_tp Pointer to receive the media transport instance.
  206. *
  207. * @return PJ_SUCCESS on success, or the appropriate error code.
  208. */
  209. PJ_DECL(pj_status_t) pjmedia_ice_create2(pjmedia_endpt *endpt,
  210. const char *name,
  211. unsigned comp_cnt,
  212. const pj_ice_strans_cfg *cfg,
  213. const pjmedia_ice_cb *cb,
  214. unsigned options,
  215. pjmedia_transport **p_tp);
  216. /**
  217. * The same as #pjmedia_ice_create2() with additional \a user_data param.
  218. *
  219. * @param endpt The media endpoint.
  220. * @param name Optional name to identify this ICE media transport
  221. * for logging purposes.
  222. * @param comp_cnt Number of components to be created.
  223. * @param cfg Pointer to configuration settings.
  224. * @param cb Optional structure containing ICE specific callbacks.
  225. * @param options Options, see #pjmedia_transport_ice_options.
  226. * @param user_data User data to be attached to the transport.
  227. * @param p_tp Pointer to receive the media transport instance.
  228. *
  229. * @return PJ_SUCCESS on success, or the appropriate error code.
  230. */
  231. PJ_DECL(pj_status_t) pjmedia_ice_create3(pjmedia_endpt *endpt,
  232. const char *name,
  233. unsigned comp_cnt,
  234. const pj_ice_strans_cfg *cfg,
  235. const pjmedia_ice_cb *cb,
  236. unsigned options,
  237. void *user_data,
  238. pjmedia_transport **p_tp);
  239. /**
  240. * Get the group lock for the ICE media transport.
  241. *
  242. * @param tp The ICE media transport.
  243. *
  244. * @return The group lock.
  245. */
  246. PJ_DECL(pj_grp_lock_t *) pjmedia_ice_get_grp_lock(pjmedia_transport *tp);
  247. /**
  248. * Add application to receive ICE notifications from the specified ICE media
  249. * transport.
  250. *
  251. * @param tp The ICE media transport.
  252. * @param cb The ICE specific callbacks.
  253. * @param user_data Optional application user data.
  254. *
  255. * @return PJ_SUCCESS on success, or the appropriate error code.
  256. */
  257. PJ_DECL(pj_status_t) pjmedia_ice_add_ice_cb(pjmedia_transport *tp,
  258. const pjmedia_ice_cb *cb,
  259. void *user_data);
  260. /**
  261. * Remove application to stop receiving ICE notifications from the specified
  262. * ICE media transport.
  263. *
  264. * @param tp The ICE media transport.
  265. * @param cb The ICE specific callbacks.
  266. * @param user_data Optional application user data. The same user data
  267. * passed to pjmedia_ice_add_ice_cb(), this is for
  268. * validation purpose.
  269. *
  270. * @return PJ_SUCCESS on success, or the appropriate error code.
  271. */
  272. PJ_DECL(pj_status_t) pjmedia_ice_remove_ice_cb(pjmedia_transport *tp,
  273. const pjmedia_ice_cb *cb,
  274. void *user_data);
  275. /**
  276. * Check if trickle support is signalled in the specified SDP. This function
  277. * will check trickle indication in the media level first, if not found, it
  278. * will check in the session level.
  279. *
  280. * @param sdp The SDP.
  281. * @param med_idx The media index to be checked.
  282. *
  283. * @return PJ_TRUE if trickle ICE indication is found.
  284. */
  285. PJ_DECL(pj_bool_t) pjmedia_ice_sdp_has_trickle(const pjmedia_sdp_session *sdp,
  286. unsigned med_idx);
  287. /**
  288. * Update check list after remote ICE candidates list are received or after
  289. * or local ICE candidates are conveyed. This function may also be called
  290. * after end-of-candidates indication is received or conveyed. ICE
  291. * connectivity checks will automatically be started if both sides have
  292. * conveyed ICE info (ICE user fragment and/or candidate list).
  293. *
  294. * To update the check list after conveying any new local candidates,
  295. * application can set the remote ICE parameters to NULL or zero. Note that
  296. * the checklist should only be updated after any newly found local candidates
  297. * are conveyed to remote, instead of immediately after the finding.
  298. *
  299. * This function is only applicable when trickle ICE is not disabled.
  300. *
  301. * @param tp The ICE media transport.
  302. * @param rem_ufrag Remote ufrag, as seen in the SDP received from
  303. * the remote agent.
  304. * @param rem_passwd Remote password, as seen in the SDP received from
  305. * the remote agent.
  306. * @param rcand_cnt Number of new remote candidates in the array.
  307. * @param rcand New remote candidates array.
  308. * @param rcand_end Set to PJ_TRUE if remote has signalled
  309. * end-of-candidate.
  310. *
  311. * @return PJ_SUCCESS, or the appropriate error code.
  312. */
  313. PJ_DECL(pj_status_t) pjmedia_ice_trickle_update(
  314. pjmedia_transport *tp,
  315. const pj_str_t *rem_ufrag,
  316. const pj_str_t *rem_passwd,
  317. unsigned rcand_cnt,
  318. const pj_ice_sess_cand rcand[],
  319. pj_bool_t rcand_end);
  320. /**
  321. * Decode trickle ICE info from the specified SDP.
  322. *
  323. * @param sdp The SDP containing trickle ICE info.
  324. * @param media_index The media index.
  325. * @param mid Output, media ID.
  326. * @param ufrag Output, ufrag.
  327. * @param passwd Output, password.
  328. * @param cand_cnt On input, maximum number of candidate array.
  329. * On output, the number of candidates.
  330. * @param cand Output, the candidates.
  331. * @param end_of_cand Output, end of candidate indication.
  332. *
  333. * @return PJ_SUCCESS, or the appropriate error code.
  334. */
  335. PJ_DECL(pj_status_t) pjmedia_ice_trickle_decode_sdp(
  336. const pjmedia_sdp_session *sdp,
  337. unsigned media_index,
  338. pj_str_t *mid,
  339. pj_str_t *ufrag,
  340. pj_str_t *passwd,
  341. unsigned *cand_cnt,
  342. pj_ice_sess_cand cand[],
  343. pj_bool_t *end_of_cand);
  344. /**
  345. * Encode trickle ICE info into the specified SDP. This function may generate
  346. * the following SDP attributes:
  347. * - Media ID, "a=mid".
  348. * - ICE ufrag & password, "a=ice-ufrag" & "a=ice-pwd".
  349. * - Trickle ICE support indication, "a=ice-options:trickle".
  350. * - ICE candidates, "a=candidate".
  351. * - End of candidate indication, "a=end-of-candidates".
  352. *
  353. * @param sdp_pool The memory pool for generating SDP attributes.
  354. * @param sdp The SDP to be updated.
  355. * @param mid The media ID.
  356. * @param ufrag The ufrag, optional.
  357. * @param passwd The password, optional.
  358. * @param cand_cnt The number of local candidates, can be zero.
  359. * @param cand The local candidates.
  360. * @param end_of_cand End of candidate indication.
  361. *
  362. * @return PJ_SUCCESS, or the appropriate error code.
  363. */
  364. PJ_DECL(pj_status_t) pjmedia_ice_trickle_encode_sdp(
  365. pj_pool_t *sdp_pool,
  366. pjmedia_sdp_session *sdp,
  367. const pj_str_t *mid,
  368. const pj_str_t *ufrag,
  369. const pj_str_t *passwd,
  370. unsigned cand_cnt,
  371. const pj_ice_sess_cand cand[],
  372. pj_bool_t end_of_cand);
  373. /**
  374. * Check if trickling ICE has found any new local candidates since the last
  375. * conveyance (via pjmedia_ice_trickle_send_local_cand()).
  376. *
  377. * @param tp The ICE media transport.
  378. *
  379. * @return PJ_TRUE if new local canditates are available.
  380. */
  381. PJ_DECL(pj_bool_t) pjmedia_ice_trickle_has_new_cand(pjmedia_transport *tp);
  382. /**
  383. * Convey all local candidates via the specified SDP.
  384. *
  385. * @param tp The ICE media transport.
  386. * @param sdp_pool The memory pool for generating SDP attributes.
  387. * @param sdp The SDP.
  388. * @param p_end_of_cand Optional, pointer to receive the indication that
  389. * candidate gathering has been completed.
  390. *
  391. * @return PJ_SUCCESS, or the appropriate error code.
  392. */
  393. PJ_DECL(pj_status_t) pjmedia_ice_trickle_send_local_cand(
  394. pjmedia_transport *tp,
  395. pj_pool_t *sdp_pool,
  396. pjmedia_sdp_session *sdp,
  397. pj_bool_t *p_end_of_cand);
  398. PJ_END_DECL
  399. /**
  400. * @}
  401. */
  402. #endif /* __PJMEDIA_TRANSPORT_ICE_H__ */