transport.h 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069
  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_H__
  20. #define __PJMEDIA_TRANSPORT_H__
  21. /**
  22. * @file transport.h Media Transport Interface
  23. * @brief Transport interface.
  24. */
  25. #include <pjmedia/types.h>
  26. #include <pjmedia/errno.h>
  27. #include <pj/string.h>
  28. /**
  29. * @defgroup PJMEDIA_TRANSPORT Media Transport
  30. * @brief Transports.
  31. * @{
  32. * The media transport (#pjmedia_transport) is the object to send and
  33. * receive media packets over the network. The media transport interface
  34. * allows the library to be extended to support different types of
  35. * transports to send and receive packets.
  36. *
  37. * The media transport is declared as #pjmedia_transport "class", which
  38. * declares "interfaces" to use the class in #pjmedia_transport_op
  39. * structure. For the user of the media transport (normally the user of
  40. * media transport is media stream, see \ref PJMED_STRM), these transport
  41. * "methods" are wrapped with API such as #pjmedia_transport_attach(),
  42. * so it should not need to call the function pointer inside
  43. * #pjmedia_transport_op directly.
  44. *
  45. * The connection between \ref PJMED_STRM and media transport is shown in
  46. * the diagram below:
  47. \img{pjmedia/docs/media-transport.PNG}
  48. * \section PJMEDIA_TRANSPORT_H_USING Basic Media Transport Usage
  49. *
  50. * The media transport's life-cycle normally follows the following stages.
  51. *
  52. * \subsection PJMEDIA_TRANSPORT_H_CREATE Creating the Media Transport
  53. *
  54. * Application creates the media transport when it needs to establish
  55. * media session to remote peer. The media transport is created using
  56. * specific function to create that particular transport; for example,
  57. * for UDP media transport, it is created with #pjmedia_transport_udp_create()
  58. * or #pjmedia_transport_udp_create2() functions. Different media
  59. * transports will provide different API to create those transports.
  60. *
  61. * Alternatively, application may create pool of media transports when
  62. * it is first started up. Using this approach probably is better, since
  63. * application has to specify the RTP port when sending the initial
  64. * session establishment request (e.g. SIP INVITE request), thus if
  65. * application only creates the media transport later when media is to be
  66. * established (normally when 200/OK is received, or when 18x is received
  67. * for early media), there is a possibility that the particular RTP
  68. * port might have been occupied by other programs. Also it is more
  69. * efficient since sockets don't need to be closed and re-opened between
  70. * calls.
  71. *
  72. *
  73. * \subsection PJMEDIA_TRANSPORT_H_ATTACH Attaching and Using the Media Transport.
  74. *
  75. * Application specifies the media transport instance when creating
  76. * the media session (#pjmedia_session_create()). Alternatively, it
  77. * may create the media stream directly with #pjmedia_stream_create()
  78. * and specify the transport instance in the argument. (Note: media
  79. * session is a high-level abstraction for media communications between
  80. * two endpoints, and it may contain more than one media streams, for
  81. * example, an audio stream and a video stream).
  82. *
  83. * When stream is created, it will "attach" itself to the media
  84. * transport by calling #pjmedia_transport_attach(), which is a thin
  85. * wrapper which calls "attach()" method of the media transport's
  86. * "virtual function pointer" (#pjmedia_transport_op). Among other things,
  87. * the stream specifies two callback functions to the transport: one
  88. * callback function will be called by transport when it receives RTP
  89. * packet, and another callback for incoming RTCP packet. The
  90. * #pjmedia_transport_attach() function also establish the destination
  91. * of the outgoing RTP and RTCP packets.
  92. *
  93. * When the stream needs to send outgoing RTP/RTCP packets, it will
  94. * call #pjmedia_transport_send_rtp() and #pjmedia_transport_send_rtcp()
  95. * of the media transport API, which is a thin wrapper to call send_rtp()
  96. * and send_rtcp() methods in the media transport's "virtual function
  97. * pointer" (#pjmedia_transport_op).
  98. *
  99. * When the stream is destroyed, it will "detach" itself from
  100. * the media transport by calling #pjmedia_transport_detach(), which is
  101. * a thin wrapper which calls "detach()" method of the media transport's
  102. * "virtual function pointer" (#pjmedia_transport_op). After the transport
  103. * is detached from its user (the stream), it will no longer report
  104. * incoming RTP/RTCP packets to the stream, and it will refuse to send
  105. * outgoing packets since the destination has been cleared.
  106. *
  107. *
  108. * \subsection PJMEDIA_TRANSPORT_H_REUSE Reusing the Media Transport.
  109. *
  110. * After transport has been detached, application may re-attach the
  111. * transport to another stream if it wants to. Detaching and re-attaching
  112. * media transport may be preferable than closing and re-opening the
  113. * transport, since it is more efficient (sockets don't need to be
  114. * closed and re-opened). However it is up to the application to choose
  115. * which method is most suitable for its uses.
  116. *
  117. *
  118. * \subsection PJMEDIA_TRANSPORT_H_DESTROY Destroying the Media Transport.
  119. *
  120. * Finally if application no longer needs the media transport, it will
  121. * call #pjmedia_transport_close() function, which is thin wrapper which
  122. * calls "destroy()" method of the media transport's "virtual function
  123. * pointer" (#pjmedia_transport_op). This function releases
  124. * all resources used by the transport, such as sockets and memory.
  125. *
  126. *
  127. * \section offer_answer Interaction with SDP Offer/Answer
  128. For basic UDP transport, the \ref PJMEDIA_TRANSPORT_H_USING above is
  129. sufficient to use the media transport. However, more complex media
  130. transports such as \ref PJMEDIA_TRANSPORT_SRTP and \ref
  131. PJMEDIA_TRANSPORT_ICE requires closer interactions with SDP offer and
  132. answer negotiation.
  133. The media transports can interact with the SDP offer/answer via
  134. these APIs:
  135. - #pjmedia_transport_media_create(), to initialize the media transport
  136. for new media session,
  137. - #pjmedia_transport_encode_sdp(), to encode SDP offer or answer,
  138. - #pjmedia_transport_media_start(), to activate the settings that
  139. have been negotiated by SDP offer answer, and
  140. - #pjmedia_transport_media_stop(), to deinitialize the media transport
  141. and reset the transport to its idle state.
  142. The usage of these API in the context of SDP offer answer will be
  143. described below.
  144. \subsection media_create Initializing Transport for New Session
  145. Application must call #pjmedia_transport_media_create() before using
  146. the transport for a new session.
  147. \subsection creat_oa Creating SDP Offer and Answer
  148. The #pjmedia_transport_encode_sdp() is used to put additional information
  149. from the transport to the local SDP, before the SDP is sent and negotiated
  150. with remote SDP.
  151. When creating an offer, call #pjmedia_transport_encode_sdp() with
  152. local SDP (and NULL as \a rem_sdp). The media transport will add the
  153. relevant attributes in the local SDP. Application then gives the local
  154. SDP to the invite session to be sent to remote agent.
  155. When creating an answer, also call #pjmedia_transport_encode_sdp(),
  156. but this time specify both local and remote SDP to the function. The
  157. media transport will once again modify the local SDP and add relevant
  158. attributes to the local SDP, if the appropriate attributes related to
  159. the transport functionality are present in remote offer. The remote
  160. SDP does not contain the relevant attributes, then the specific transport
  161. functionality will not be activated for the session.
  162. The #pjmedia_transport_encode_sdp() should also be called when application
  163. sends subsequent SDP offer or answer. The media transport will encode
  164. the appropriate attributes based on the state of the session.
  165. \subsection media_start Offer/Answer Completion
  166. Once both local and remote SDP have been negotiated by the
  167. \ref PJMEDIA_SDP_NEG (normally this is part of PJSIP invite session),
  168. application should give both local and remote SDP to
  169. #pjmedia_transport_media_start() so that the settings are activated
  170. for the session. This function should be called for both initial and
  171. subsequent SDP negotiation.
  172. \subsection media_stop Stopping Transport
  173. Once session is stop application must call #pjmedia_transport_media_stop()
  174. to deactivate the transport feature. Application may reuse the transport
  175. for subsequent media session by repeating the #pjmedia_transport_media_create(),
  176. #pjmedia_transport_encode_sdp(), #pjmedia_transport_media_start(), and
  177. #pjmedia_transport_media_stop() above.
  178. * \section PJMEDIA_TRANSPORT_H_IMPL Implementing Media Transport
  179. *
  180. * To implement a new type of media transport, one needs to "subclass" the
  181. * media transport "class" (#pjmedia_transport) by providing the "methods"
  182. * in the media transport "interface" (#pjmedia_transport_op), and provides
  183. * a function to create this new type of transport (similar to
  184. * #pjmedia_transport_udp_create() function).
  185. *
  186. * The media transport is expected to run indepently, that is there should
  187. * be no polling like function to poll the transport for incoming RTP/RTCP
  188. * packets. This normally can be done by registering the media sockets to
  189. * the media endpoint's IOQueue, which allows the transport to be notified
  190. * when incoming packet has arrived.
  191. *
  192. * Alternatively, media transport may utilize thread(s) internally to wait
  193. * for incoming packets. The thread then will call the appropriate RTP or
  194. * RTCP callback provided by its user (stream) whenever packet is received.
  195. * If the transport's user is a stream, then the callbacks provided by the
  196. * stream will be thread-safe, so the transport may call these callbacks
  197. * without having to serialize the access with some mutex protection. But
  198. * the media transport may still have to protect its internal data with
  199. * mutex protection, since it may be called by application's thread (for
  200. * example, to send RTP/RTCP packets).
  201. *
  202. */
  203. #include <pjmedia/sdp.h>
  204. PJ_BEGIN_DECL
  205. /**
  206. * Forward declaration for media transport.
  207. */
  208. typedef struct pjmedia_transport pjmedia_transport;
  209. /**
  210. * Forward declaration for media transport info.
  211. */
  212. typedef struct pjmedia_transport_info pjmedia_transport_info;
  213. /**
  214. * Forward declaration for media transport attach param.
  215. */
  216. typedef struct pjmedia_transport_attach_param pjmedia_transport_attach_param;
  217. /**
  218. * This enumeration specifies the general behaviour of media processing
  219. */
  220. typedef enum pjmedia_tranport_media_option
  221. {
  222. /**
  223. * When this flag is specified, the transport will not perform media
  224. * transport validation, this is useful when transport is stacked with
  225. * other transport, for example when transport UDP is stacked under
  226. * transport SRTP, media transport validation only need to be done by
  227. * transport SRTP.
  228. */
  229. PJMEDIA_TPMED_NO_TRANSPORT_CHECKING = 1,
  230. /**
  231. * When this flag is specified, the transport will allow multiplexing
  232. * RTP and RTCP, i.e. if the remote agrees, RTCP will be sent using
  233. * the same socket for RTP.
  234. */
  235. PJMEDIA_TPMED_RTCP_MUX = 2
  236. } pjmedia_tranport_media_option;
  237. /**
  238. * Media socket info is used to describe the underlying sockets
  239. * to be used as media transport.
  240. */
  241. typedef struct pjmedia_sock_info
  242. {
  243. /** The RTP socket handle */
  244. pj_sock_t rtp_sock;
  245. /** Address to be advertised as the local address for the RTP
  246. * socket, which does not need to be equal as the bound
  247. * address (for example, this address can be the address resolved
  248. * with STUN).
  249. */
  250. pj_sockaddr rtp_addr_name;
  251. /** The RTCP socket handle. */
  252. pj_sock_t rtcp_sock;
  253. /** Address to be advertised as the local address for the RTCP
  254. * socket, which does not need to be equal as the bound
  255. * address (for example, this address can be the address resolved
  256. * with STUN).
  257. */
  258. pj_sockaddr rtcp_addr_name;
  259. } pjmedia_sock_info;
  260. /**
  261. * This structure describes the operations for the stream transport.
  262. */
  263. struct pjmedia_transport_op
  264. {
  265. /**
  266. * Get media socket info from the specified transport.
  267. *
  268. * Application should call #pjmedia_transport_get_info() instead
  269. */
  270. pj_status_t (*get_info)(pjmedia_transport *tp,
  271. pjmedia_transport_info *info);
  272. /**
  273. * This function is called by the stream when the transport is about
  274. * to be used by the stream for the first time, and it tells the transport
  275. * about remote RTP address to send the packet and some callbacks to be
  276. * called for incoming packets. This function exists for backwards
  277. * compatibility. Transports should implement attach2 instead.
  278. *
  279. * Application should call #pjmedia_transport_attach() instead of
  280. * calling this function directly.
  281. */
  282. pj_status_t (*attach)(pjmedia_transport *tp,
  283. void *user_data,
  284. const pj_sockaddr_t *rem_addr,
  285. const pj_sockaddr_t *rem_rtcp,
  286. unsigned addr_len,
  287. void (*rtp_cb)(void *user_data,
  288. void *pkt,
  289. pj_ssize_t size),
  290. void (*rtcp_cb)(void *user_data,
  291. void *pkt,
  292. pj_ssize_t size));
  293. /**
  294. * This function is called by the stream when the stream no longer
  295. * needs the transport (normally when the stream is about to be closed).
  296. * After the transport is detached, it will ignore incoming
  297. * RTP/RTCP packets, and will refuse to send outgoing RTP/RTCP packets.
  298. * Application may re-attach the media transport to another transport
  299. * user (e.g. stream) after the transport has been detached.
  300. *
  301. * Application should call #pjmedia_transport_detach() instead of
  302. * calling this function directly.
  303. */
  304. void (*detach)(pjmedia_transport *tp,
  305. void *user_data);
  306. /**
  307. * This function is called by the stream to send RTP packet using the
  308. * transport.
  309. *
  310. * Application should call #pjmedia_transport_send_rtp() instead of
  311. * calling this function directly.
  312. */
  313. pj_status_t (*send_rtp)(pjmedia_transport *tp,
  314. const void *pkt,
  315. pj_size_t size);
  316. /**
  317. * This function is called by the stream to send RTCP packet using the
  318. * transport.
  319. *
  320. * Application should call #pjmedia_transport_send_rtcp() instead of
  321. * calling this function directly.
  322. */
  323. pj_status_t (*send_rtcp)(pjmedia_transport *tp,
  324. const void *pkt,
  325. pj_size_t size);
  326. /**
  327. * This function is called by the stream to send RTCP packet using the
  328. * transport with destination address other than default specified in
  329. * #pjmedia_transport_attach().
  330. *
  331. * Application should call #pjmedia_transport_send_rtcp2() instead of
  332. * calling this function directly.
  333. */
  334. pj_status_t (*send_rtcp2)(pjmedia_transport *tp,
  335. const pj_sockaddr_t *addr,
  336. unsigned addr_len,
  337. const void *pkt,
  338. pj_size_t size);
  339. /**
  340. * Prepare the transport for a new media session.
  341. *
  342. * Application should call #pjmedia_transport_media_create() instead of
  343. * calling this function directly.
  344. */
  345. pj_status_t (*media_create)(pjmedia_transport *tp,
  346. pj_pool_t *sdp_pool,
  347. unsigned options,
  348. const pjmedia_sdp_session *remote_sdp,
  349. unsigned media_index);
  350. /**
  351. * This function is called by application to generate the SDP parts
  352. * related to transport type, e.g: ICE, SRTP.
  353. *
  354. * Application should call #pjmedia_transport_encode_sdp() instead of
  355. * calling this function directly.
  356. */
  357. pj_status_t (*encode_sdp)(pjmedia_transport *tp,
  358. pj_pool_t *sdp_pool,
  359. pjmedia_sdp_session *sdp_local,
  360. const pjmedia_sdp_session *rem_sdp,
  361. unsigned media_index);
  362. /**
  363. * This function is called by application to start the transport
  364. * based on local and remote SDP.
  365. *
  366. * Application should call #pjmedia_transport_media_start() instead of
  367. * calling this function directly.
  368. */
  369. pj_status_t (*media_start) (pjmedia_transport *tp,
  370. pj_pool_t *tmp_pool,
  371. const pjmedia_sdp_session *sdp_local,
  372. const pjmedia_sdp_session *sdp_remote,
  373. unsigned media_index);
  374. /**
  375. * This function is called by application to stop the transport.
  376. *
  377. * Application should call #pjmedia_transport_media_stop() instead of
  378. * calling this function directly.
  379. */
  380. pj_status_t (*media_stop) (pjmedia_transport *tp);
  381. /**
  382. * This function can be called to simulate packet lost.
  383. *
  384. * Application should call #pjmedia_transport_simulate_lost() instead of
  385. * calling this function directly.
  386. */
  387. pj_status_t (*simulate_lost)(pjmedia_transport *tp,
  388. pjmedia_dir dir,
  389. unsigned pct_lost);
  390. /**
  391. * This function can be called to destroy this transport.
  392. *
  393. * Application should call #pjmedia_transport_close() instead of
  394. * calling this function directly.
  395. */
  396. pj_status_t (*destroy)(pjmedia_transport *tp);
  397. /**
  398. * This function is called by the stream when the transport is about
  399. * to be used by the stream for the first time, and it tells the transport
  400. * about remote RTP address to send the packet and some callbacks to be
  401. * called for incoming packets.
  402. *
  403. * Application should call #pjmedia_transport_attach2() instead of
  404. * calling this function directly.
  405. */
  406. pj_status_t (*attach2)(pjmedia_transport *tp,
  407. pjmedia_transport_attach_param *att_param);
  408. };
  409. /**
  410. * @see pjmedia_transport_op.
  411. */
  412. typedef struct pjmedia_transport_op pjmedia_transport_op;
  413. /**
  414. * Media transport type.
  415. */
  416. typedef enum pjmedia_transport_type
  417. {
  418. /** Media transport using standard UDP */
  419. PJMEDIA_TRANSPORT_TYPE_UDP,
  420. /** Media transport using ICE */
  421. PJMEDIA_TRANSPORT_TYPE_ICE,
  422. /**
  423. * Media transport SRTP, this transport is actually security adapter to be
  424. * stacked with other transport to enable encryption on the underlying
  425. * transport.
  426. */
  427. PJMEDIA_TRANSPORT_TYPE_SRTP,
  428. /** Loopback media transport */
  429. PJMEDIA_TRANSPORT_TYPE_LOOP,
  430. /**
  431. * Start of user defined transport.
  432. */
  433. PJMEDIA_TRANSPORT_TYPE_USER
  434. } pjmedia_transport_type;
  435. /**
  436. * This structure declares media transport. A media transport is called
  437. * by the stream to transmit a packet, and will notify stream when
  438. * incoming packet is arrived.
  439. */
  440. struct pjmedia_transport
  441. {
  442. /** Transport name (for logging purpose). */
  443. char name[PJ_MAX_OBJ_NAME];
  444. /** Transport type. */
  445. pjmedia_transport_type type;
  446. /** Transport's "virtual" function table. */
  447. pjmedia_transport_op *op;
  448. /** Application/user data */
  449. void *user_data;
  450. /** Group lock, for synchronization between destroy() & callbacks. */
  451. pj_grp_lock_t *grp_lock;
  452. };
  453. /**
  454. * This structure describes storage buffer of transport specific info.
  455. * The actual transport specific info contents will be defined by transport
  456. * implementation. Note that some transport implementations do not need to
  457. * provide specific info, since the general socket info is enough.
  458. */
  459. typedef struct pjmedia_transport_specific_info
  460. {
  461. /**
  462. * Specify media transport type.
  463. */
  464. pjmedia_transport_type type;
  465. /**
  466. * Specify storage buffer size of transport specific info.
  467. */
  468. int cbsize;
  469. /**
  470. * Storage buffer of transport specific info.
  471. */
  472. char buffer[PJMEDIA_TRANSPORT_SPECIFIC_INFO_MAXSIZE];
  473. /**
  474. * The media transport instance.
  475. */
  476. pjmedia_transport *tp;
  477. } pjmedia_transport_specific_info;
  478. /**
  479. * This structure describes transport informations, including general
  480. * socket information and specific information of single transport or
  481. * stacked transports (e.g: SRTP stacked on top of UDP)
  482. */
  483. struct pjmedia_transport_info
  484. {
  485. /**
  486. * General socket info.
  487. */
  488. pjmedia_sock_info sock_info;
  489. /**
  490. * Remote address where RTP/RTCP originated from. In case this transport
  491. * hasn't ever received packet, the address can be invalid (zero).
  492. */
  493. pj_sockaddr src_rtp_name;
  494. pj_sockaddr src_rtcp_name;
  495. /**
  496. * Specifies number of transport specific info included.
  497. */
  498. unsigned specific_info_cnt;
  499. /**
  500. * Buffer storage of transport specific info.
  501. */
  502. pjmedia_transport_specific_info spc_info[PJMEDIA_TRANSPORT_SPECIFIC_INFO_MAXCNT];
  503. };
  504. /**
  505. * This structure describes the data passed when calling #rtp_cb2().
  506. */
  507. typedef struct pjmedia_tp_cb_param
  508. {
  509. /**
  510. * User data.
  511. */
  512. void *user_data;
  513. /**
  514. * Packet buffer.
  515. */
  516. void *pkt;
  517. /**
  518. * Packet size.
  519. */
  520. pj_ssize_t size;
  521. /**
  522. * Packet's source address.
  523. */
  524. pj_sockaddr *src_addr;
  525. /**
  526. * Should media transport switch remote address to \a rtp_src_addr?
  527. * Media transport should initialize it to PJ_FALSE, and application
  528. * can change the value as necessary.
  529. */
  530. pj_bool_t rem_switch;
  531. } pjmedia_tp_cb_param;
  532. /**
  533. * This structure describes the data passed when calling
  534. * #pjmedia_transport_attach2().
  535. */
  536. struct pjmedia_transport_attach_param
  537. {
  538. /**
  539. * The media stream.
  540. */
  541. void *stream;
  542. /**
  543. * Indicate the stream type, either it's audio (PJMEDIA_TYPE_AUDIO)
  544. * or video (PJMEDIA_TYPE_VIDEO).
  545. */
  546. pjmedia_type media_type;
  547. /**
  548. * Remote RTP address to send RTP packet to.
  549. */
  550. pj_sockaddr rem_addr;
  551. /**
  552. * Optional remote RTCP address. If the argument is NULL
  553. * or if the address is zero, the RTCP address will be
  554. * calculated from the RTP address (which is RTP port plus one).
  555. */
  556. pj_sockaddr rem_rtcp;
  557. /**
  558. * Length of the remote address.
  559. */
  560. unsigned addr_len;
  561. /**
  562. * Arbitrary user data to be set when the callbacks are called.
  563. */
  564. void *user_data;
  565. /**
  566. * Callback to be called when RTP packet is received on the transport.
  567. */
  568. void (*rtp_cb)(void *user_data, void *pkt, pj_ssize_t);
  569. /**
  570. * Callback to be called when RTCP packet is received on the transport.
  571. */
  572. void (*rtcp_cb)(void *user_data, void *pkt, pj_ssize_t);
  573. /**
  574. * Callback to be called when RTP packet is received on the transport.
  575. */
  576. void (*rtp_cb2)(pjmedia_tp_cb_param *param);
  577. };
  578. /**
  579. * Initialize transport info.
  580. *
  581. * @param info Transport info to be initialized.
  582. */
  583. PJ_INLINE(void) pjmedia_transport_info_init(pjmedia_transport_info *info)
  584. {
  585. pj_bzero(info, sizeof(pjmedia_transport_info));
  586. info->sock_info.rtp_sock = info->sock_info.rtcp_sock = PJ_INVALID_SOCKET;
  587. }
  588. /**
  589. * Get media transport info from the specified transport and all underlying
  590. * transports if any. The transport also contains information about socket info
  591. * which describes the local address of the transport, and would be needed
  592. * for example to fill in the "c=" and "m=" line of local SDP.
  593. *
  594. * @param tp The transport.
  595. * @param info Media transport info to be initialized.
  596. *
  597. * @return PJ_SUCCESS on success.
  598. */
  599. PJ_INLINE(pj_status_t) pjmedia_transport_get_info(pjmedia_transport *tp,
  600. pjmedia_transport_info *info)
  601. {
  602. if (tp && tp->op && tp->op->get_info)
  603. return (*tp->op->get_info)(tp, info);
  604. return PJ_ENOTSUP;
  605. }
  606. /**
  607. * Utility API to get transport type specific info from the specified media
  608. * transport info.
  609. *
  610. * @param info Media transport info.
  611. * @param type Media transport type.
  612. *
  613. * @return Pointer to media transport specific info, or NULL if
  614. * specific info for the transport type is not found.
  615. */
  616. PJ_INLINE(void*) pjmedia_transport_info_get_spc_info(
  617. pjmedia_transport_info *info,
  618. pjmedia_transport_type type)
  619. {
  620. unsigned i;
  621. for (i = 0; i < info->specific_info_cnt; ++i) {
  622. if (info->spc_info[i].type == type)
  623. return (void*)info->spc_info[i].buffer;
  624. }
  625. return NULL;
  626. }
  627. /**
  628. * Utility API to get the transport instance from the specified media
  629. * transport info.
  630. *
  631. * @param info Media transport info.
  632. * @param type Media transport type.
  633. *
  634. * @return The media transport instance, or NULL if
  635. * the transport type is not found.
  636. */
  637. PJ_INLINE(pjmedia_transport*) pjmedia_transport_info_get_transport(
  638. pjmedia_transport_info *info,
  639. pjmedia_transport_type type)
  640. {
  641. unsigned i;
  642. for (i = 0; i < info->specific_info_cnt; ++i) {
  643. if (info->spc_info[i].type == type)
  644. return info->spc_info[i].tp;
  645. }
  646. return NULL;
  647. }
  648. /**
  649. * Attach callbacks to be called on receipt of incoming RTP/RTCP packets.
  650. * This is just a simple wrapper which calls <tt>attach2()</tt> member of
  651. * the transport if it is implemented, otherwise it calls <tt>attach()</tt>
  652. * member of the transport.
  653. *
  654. * @param tp The media transport.
  655. * @param att_param The transport attach param.
  656. *
  657. * @return PJ_SUCCESS on success, or the appropriate error code.
  658. */
  659. PJ_INLINE(pj_status_t) pjmedia_transport_attach2(pjmedia_transport *tp,
  660. pjmedia_transport_attach_param *att_param)
  661. {
  662. if (tp->op->attach2) {
  663. return (*tp->op->attach2)(tp, att_param);
  664. } else {
  665. return (*tp->op->attach)(tp, att_param->user_data,
  666. (pj_sockaddr_t*)&att_param->rem_addr,
  667. (pj_sockaddr_t*)&att_param->rem_rtcp,
  668. att_param->addr_len, att_param->rtp_cb,
  669. att_param->rtcp_cb);
  670. }
  671. }
  672. /**
  673. * Attach callbacks to be called on receipt of incoming RTP/RTCP packets.
  674. * This is just a simple wrapper which calls <tt>attach()</tt> member of
  675. * the transport.
  676. *
  677. * @param tp The media transport.
  678. * @param user_data Arbitrary user data to be set when the callbacks are
  679. * called.
  680. * @param rem_addr Remote RTP address to send RTP packet to.
  681. * @param rem_rtcp Optional remote RTCP address. If the argument is NULL
  682. * or if the address is zero, the RTCP address will be
  683. * calculated from the RTP address (which is RTP port
  684. * plus one).
  685. * @param addr_len Length of the remote address.
  686. * @param rtp_cb Callback to be called when RTP packet is received on
  687. * the transport.
  688. * @param rtcp_cb Callback to be called when RTCP packet is received on
  689. * the transport.
  690. *
  691. * @return PJ_SUCCESS on success, or the appropriate error code.
  692. */
  693. PJ_INLINE(pj_status_t) pjmedia_transport_attach(pjmedia_transport *tp,
  694. void *user_data,
  695. const pj_sockaddr_t *rem_addr,
  696. const pj_sockaddr_t *rem_rtcp,
  697. unsigned addr_len,
  698. void (*rtp_cb)(void *user_data,
  699. void *pkt,
  700. pj_ssize_t),
  701. void (*rtcp_cb)(void *usr_data,
  702. void*pkt,
  703. pj_ssize_t))
  704. {
  705. if (tp->op->attach2) {
  706. pjmedia_transport_attach_param param;
  707. pj_bzero(&param, sizeof(param));
  708. param.user_data = user_data;
  709. pj_sockaddr_cp(&param.rem_addr, rem_addr);
  710. if (rem_rtcp && pj_sockaddr_has_addr(rem_rtcp)) {
  711. pj_sockaddr_cp(&param.rem_rtcp, rem_rtcp);
  712. } else {
  713. /* Copy RTCP address from the RTP address, with port + 1 */
  714. pj_memcpy(&param.rem_rtcp, rem_addr, addr_len);
  715. pj_sockaddr_set_port(&param.rem_rtcp,
  716. pj_sockaddr_get_port(rem_addr) + 1);
  717. }
  718. param.addr_len = addr_len;
  719. param.rtp_cb = rtp_cb;
  720. param.rtcp_cb = rtcp_cb;
  721. return (*tp->op->attach2)(tp, &param);
  722. } else {
  723. return (*tp->op->attach)(tp, user_data, rem_addr, rem_rtcp, addr_len,
  724. rtp_cb, rtcp_cb);
  725. }
  726. }
  727. /**
  728. * Detach callbacks from the transport.
  729. * This is just a simple wrapper which calls <tt>detach()</tt> member of
  730. * the transport. After the transport is detached, it will ignore incoming
  731. * RTP/RTCP packets, and will refuse to send outgoing RTP/RTCP packets.
  732. * Application may re-attach the media transport to another transport user
  733. * (e.g. stream) after the transport has been detached.
  734. *
  735. * @param tp The media transport.
  736. * @param user_data User data which must match the previously set value
  737. * on attachment.
  738. */
  739. PJ_INLINE(void) pjmedia_transport_detach(pjmedia_transport *tp,
  740. void *user_data)
  741. {
  742. (*tp->op->detach)(tp, user_data);
  743. }
  744. /**
  745. * Send RTP packet with the specified media transport. This is just a simple
  746. * wrapper which calls <tt>send_rtp()</tt> member of the transport. The
  747. * RTP packet will be delivered to the destination address specified in
  748. * #pjmedia_transport_attach() function.
  749. *
  750. * @param tp The media transport.
  751. * @param pkt The packet to send.
  752. * @param size Size of the packet.
  753. *
  754. * @return PJ_SUCCESS on success, or the appropriate error code.
  755. */
  756. PJ_INLINE(pj_status_t) pjmedia_transport_send_rtp(pjmedia_transport *tp,
  757. const void *pkt,
  758. pj_size_t size)
  759. {
  760. return (*tp->op->send_rtp)(tp, pkt, size);
  761. }
  762. /**
  763. * Send RTCP packet with the specified media transport. This is just a simple
  764. * wrapper which calls <tt>send_rtcp()</tt> member of the transport. The
  765. * RTCP packet will be delivered to the destination address specified in
  766. * #pjmedia_transport_attach() function.
  767. *
  768. * @param tp The media transport.
  769. * @param pkt The packet to send.
  770. * @param size Size of the packet.
  771. *
  772. * @return PJ_SUCCESS on success, or the appropriate error code.
  773. */
  774. PJ_INLINE(pj_status_t) pjmedia_transport_send_rtcp(pjmedia_transport *tp,
  775. const void *pkt,
  776. pj_size_t size)
  777. {
  778. return (*tp->op->send_rtcp)(tp, pkt, size);
  779. }
  780. /**
  781. * Send RTCP packet with the specified media transport. This is just a simple
  782. * wrapper which calls <tt>send_rtcp2()</tt> member of the transport. The
  783. * RTCP packet will be delivered to the destination address specified in
  784. * param addr, if addr is NULL, RTCP packet will be delivered to destination
  785. * address specified in #pjmedia_transport_attach() function.
  786. *
  787. * @param tp The media transport.
  788. * @param addr The destination address.
  789. * @param addr_len Length of destination address.
  790. * @param pkt The packet to send.
  791. * @param size Size of the packet.
  792. *
  793. * @return PJ_SUCCESS on success, or the appropriate error code.
  794. */
  795. PJ_INLINE(pj_status_t) pjmedia_transport_send_rtcp2(pjmedia_transport *tp,
  796. const pj_sockaddr_t *addr,
  797. unsigned addr_len,
  798. const void *pkt,
  799. pj_size_t size)
  800. {
  801. return (*tp->op->send_rtcp2)(tp, addr, addr_len, pkt, size);
  802. }
  803. /**
  804. * Prepare the media transport for a new media session, Application must
  805. * call this function before starting a new media session using this
  806. * transport.
  807. *
  808. * This is just a simple wrapper which calls <tt>media_create()</tt> member
  809. * of the transport.
  810. *
  811. * @param tp The media transport.
  812. * @param sdp_pool Pool object to allocate memory related to SDP
  813. * messaging components.
  814. * @param options Option flags, from #pjmedia_tranport_media_option
  815. * @param rem_sdp Remote SDP if local SDP is an answer, otherwise
  816. * specify NULL if SDP is an offer.
  817. * @param media_index Media index in SDP.
  818. *
  819. * @return PJ_SUCCESS on success, or the appropriate error code.
  820. */
  821. PJ_INLINE(pj_status_t) pjmedia_transport_media_create(pjmedia_transport *tp,
  822. pj_pool_t *sdp_pool,
  823. unsigned options,
  824. const pjmedia_sdp_session *rem_sdp,
  825. unsigned media_index)
  826. {
  827. return (*tp->op->media_create)(tp, sdp_pool, options, rem_sdp,
  828. media_index);
  829. }
  830. /**
  831. * Put transport specific information into the SDP. This function can be
  832. * called to put transport specific information in the initial or
  833. * subsequent SDP offer or answer.
  834. *
  835. * This is just a simple wrapper which calls <tt>encode_sdp()</tt> member
  836. * of the transport.
  837. *
  838. * @param tp The media transport.
  839. * @param sdp_pool Pool object to allocate memory related to SDP
  840. * messaging components.
  841. * @param sdp The local SDP to be filled in information from the
  842. * media transport.
  843. * @param rem_sdp Remote SDP if local SDP is an answer, otherwise
  844. * specify NULL if SDP is an offer.
  845. * @param media_index Media index in SDP.
  846. *
  847. * @return PJ_SUCCESS on success, or the appropriate error code.
  848. */
  849. PJ_INLINE(pj_status_t) pjmedia_transport_encode_sdp(pjmedia_transport *tp,
  850. pj_pool_t *sdp_pool,
  851. pjmedia_sdp_session *sdp,
  852. const pjmedia_sdp_session *rem_sdp,
  853. unsigned media_index)
  854. {
  855. return (*tp->op->encode_sdp)(tp, sdp_pool, sdp, rem_sdp, media_index);
  856. }
  857. /**
  858. * Start the transport session with the settings in both local and remote
  859. * SDP. The actual work that is done by this function depends on the
  860. * underlying transport type. For SRTP, this will activate the encryption
  861. * and decryption based on the keys found the SDPs. For ICE, this will
  862. * start ICE negotiation according to the information found in the SDPs.
  863. *
  864. * This is just a simple wrapper which calls <tt>media_start()</tt> member
  865. * of the transport.
  866. *
  867. * @param tp The media transport.
  868. * @param tmp_pool The memory pool for allocating temporary objects.
  869. * @param sdp_local Local SDP.
  870. * @param sdp_remote Remote SDP.
  871. * @param media_index Media index in the SDP.
  872. *
  873. * @return PJ_SUCCESS on success, or the appropriate error code.
  874. */
  875. PJ_INLINE(pj_status_t) pjmedia_transport_media_start(pjmedia_transport *tp,
  876. pj_pool_t *tmp_pool,
  877. const pjmedia_sdp_session *sdp_local,
  878. const pjmedia_sdp_session *sdp_remote,
  879. unsigned media_index)
  880. {
  881. return (*tp->op->media_start)(tp, tmp_pool, sdp_local, sdp_remote,
  882. media_index);
  883. }
  884. /**
  885. * This API should be called when the session is stopped, to allow the media
  886. * transport to release its resources used for the session.
  887. *
  888. * This is just a simple wrapper which calls <tt>media_stop()</tt> member
  889. * of the transport.
  890. *
  891. * @param tp The media transport.
  892. *
  893. * @return PJ_SUCCESS on success, or the appropriate error code.
  894. */
  895. PJ_INLINE(pj_status_t) pjmedia_transport_media_stop(pjmedia_transport *tp)
  896. {
  897. return (*tp->op->media_stop)(tp);
  898. }
  899. /**
  900. * Close media transport. This is just a simple wrapper which calls
  901. * <tt>destroy()</tt> member of the transport. This function will free
  902. * all resources created by this transport (such as sockets, memory, etc.).
  903. *
  904. * @param tp The media transport.
  905. *
  906. * @return PJ_SUCCESS on success, or the appropriate error code.
  907. */
  908. PJ_INLINE(pj_status_t) pjmedia_transport_close(pjmedia_transport *tp)
  909. {
  910. if (tp->op->destroy)
  911. return (*tp->op->destroy)(tp);
  912. else
  913. return PJ_SUCCESS;
  914. }
  915. /**
  916. * Simulate packet lost in the specified direction (for testing purposes).
  917. * When enabled, the transport will randomly drop packets to the specified
  918. * direction.
  919. *
  920. * @param tp The media transport.
  921. * @param dir Media direction to which packets will be randomly dropped.
  922. * @param pct_lost Percent lost (0-100). Set to zero to disable packet
  923. * lost simulation.
  924. *
  925. * @return PJ_SUCCESS on success.
  926. */
  927. PJ_INLINE(pj_status_t) pjmedia_transport_simulate_lost(pjmedia_transport *tp,
  928. pjmedia_dir dir,
  929. unsigned pct_lost)
  930. {
  931. return (*tp->op->simulate_lost)(tp, dir, pct_lost);
  932. }
  933. PJ_END_DECL
  934. /**
  935. * @}
  936. */
  937. #endif /* __PJMEDIA_TRANSPORT_H__ */