rtp.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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_RTP_H__
  20. #define __PJMEDIA_RTP_H__
  21. /**
  22. * @file rtp.h
  23. * @brief RTP packet and RTP session declarations.
  24. */
  25. #include <pjmedia/types.h>
  26. PJ_BEGIN_DECL
  27. /**
  28. * @defgroup PJMED_RTP RTP Session and Encapsulation (RFC 3550)
  29. * @ingroup PJMEDIA_SESSION
  30. * @brief RTP format and session management
  31. * @{
  32. *
  33. * The RTP module is designed to be dependent only to PJLIB, it does not depend
  34. * on any other parts of PJMEDIA library. The RTP module does not even depend
  35. * on any transports (sockets), to promote even more use, such as in DSP
  36. * development (where transport may be handled by different processor).
  37. *
  38. * An RTCP implementation is available, in separate module. Please see
  39. * @ref PJMED_RTCP.
  40. *
  41. * The functions that are provided by this module:
  42. * - creating RTP header for each outgoing packet.
  43. * - decoding RTP packet into RTP header and payload.
  44. * - provide simple RTP session management (sequence number, etc.)
  45. *
  46. * The RTP module does not use any dynamic memory at all.
  47. *
  48. * \section P1 How to Use the RTP Module
  49. *
  50. * First application must call #pjmedia_rtp_session_init() to initialize the RTP
  51. * session.
  52. *
  53. * When application wants to send RTP packet, it needs to call
  54. * #pjmedia_rtp_encode_rtp() to build the RTP header. Note that this WILL NOT
  55. * build the complete RTP packet, but instead only the header. Application can
  56. * then either concatenate the header with the payload, or send the two
  57. * fragments (the header and the payload) using scatter-gather transport API
  58. * (e.g. \a sendv()).
  59. *
  60. * When application receives an RTP packet, first it should call
  61. * #pjmedia_rtp_decode_rtp to decode RTP header and payload, then it should call
  62. * #pjmedia_rtp_session_update to check whether we can process the RTP payload,
  63. * and to let the RTP session updates its internal status. The decode function
  64. * is guaranteed to point the payload to the correct position regardless of
  65. * any options present in the RTP packet.
  66. *
  67. */
  68. #ifdef _MSC_VER
  69. # pragma warning(disable:4214) // bit field types other than int
  70. #endif
  71. /**
  72. * RTP packet header. Note that all RTP functions here will work with this
  73. * header in network byte order.
  74. */
  75. #pragma pack(1)
  76. struct pjmedia_rtp_hdr
  77. {
  78. #if defined(PJ_IS_BIG_ENDIAN) && (PJ_IS_BIG_ENDIAN!=0)
  79. pj_uint16_t v:2; /**< packet type/version */
  80. pj_uint16_t p:1; /**< padding flag */
  81. pj_uint16_t x:1; /**< extension flag */
  82. pj_uint16_t cc:4; /**< CSRC count */
  83. pj_uint16_t m:1; /**< marker bit */
  84. pj_uint16_t pt:7; /**< payload type */
  85. #else
  86. pj_uint16_t cc:4; /**< CSRC count */
  87. pj_uint16_t x:1; /**< header extension flag */
  88. pj_uint16_t p:1; /**< padding flag */
  89. pj_uint16_t v:2; /**< packet type/version */
  90. pj_uint16_t pt:7; /**< payload type */
  91. pj_uint16_t m:1; /**< marker bit */
  92. #endif
  93. pj_uint16_t seq; /**< sequence number */
  94. pj_uint32_t ts; /**< timestamp */
  95. pj_uint32_t ssrc; /**< synchronization source */
  96. };
  97. #pragma pack()
  98. /**
  99. * @see pjmedia_rtp_hdr
  100. */
  101. typedef struct pjmedia_rtp_hdr pjmedia_rtp_hdr;
  102. /**
  103. * RTP extension header.
  104. */
  105. struct pjmedia_rtp_ext_hdr
  106. {
  107. pj_uint16_t profile_data; /**< Profile data. */
  108. pj_uint16_t length; /**< Length. */
  109. };
  110. /**
  111. * @see pjmedia_rtp_ext_hdr
  112. */
  113. typedef struct pjmedia_rtp_ext_hdr pjmedia_rtp_ext_hdr;
  114. /**
  115. * This will contain the RTP header decode output.
  116. */
  117. struct pjmedia_rtp_dec_hdr
  118. {
  119. /* RTP extension header output decode */
  120. pjmedia_rtp_ext_hdr *ext_hdr;
  121. pj_uint32_t *ext;
  122. unsigned ext_len;
  123. };
  124. /**
  125. * @see pjmedia_rtp_dec_hdr
  126. */
  127. typedef struct pjmedia_rtp_dec_hdr pjmedia_rtp_dec_hdr;
  128. #pragma pack(1)
  129. /**
  130. * Declaration for DTMF telephony-events (RFC2833).
  131. */
  132. struct pjmedia_rtp_dtmf_event
  133. {
  134. pj_uint8_t event; /**< Event type ID. */
  135. pj_uint8_t e_vol; /**< Event volume. */
  136. pj_uint16_t duration; /**< Event duration. */
  137. };
  138. /**
  139. * Mask for the E ("End") bit of telephony-events payload.
  140. *
  141. * @see pjmedia_rtp_dtmf_event
  142. */
  143. #define PJMEDIA_RTP_DTMF_EVENT_END_MASK 0x80
  144. /**
  145. * Mask for the Volume field of telephony-events payload.
  146. *
  147. * @see pjmedia_rtp_dtmf_event
  148. */
  149. #define PJMEDIA_RTP_DTMF_EVENT_VOLUME_MASK 0x3F
  150. /**
  151. * @see pjmedia_rtp_dtmf_event
  152. */
  153. typedef struct pjmedia_rtp_dtmf_event pjmedia_rtp_dtmf_event;
  154. #pragma pack()
  155. /**
  156. * A generic sequence number management, used by both RTP and RTCP.
  157. */
  158. struct pjmedia_rtp_seq_session
  159. {
  160. pj_uint16_t max_seq; /**< Highest sequence number heard */
  161. pj_uint32_t cycles; /**< Shifted count of seq number cycles */
  162. pj_uint32_t base_seq; /**< Base seq number */
  163. pj_uint32_t bad_seq; /**< Last 'bad' seq number + 1 */
  164. pj_uint32_t probation; /**< Sequ. packets till source is valid */
  165. };
  166. /**
  167. * @see pjmedia_rtp_seq_session
  168. */
  169. typedef struct pjmedia_rtp_seq_session pjmedia_rtp_seq_session;
  170. /**
  171. * RTP session descriptor.
  172. */
  173. struct pjmedia_rtp_session
  174. {
  175. pjmedia_rtp_hdr out_hdr; /**< Saved hdr for outgoing pkts. */
  176. pjmedia_rtp_seq_session seq_ctrl; /**< Sequence number management. */
  177. pj_uint16_t out_pt; /**< Default outgoing payload type. */
  178. pj_uint32_t out_extseq; /**< Outgoing extended seq #. */
  179. pj_bool_t has_peer_ssrc;/**< Has peer SSRC? */
  180. pj_uint32_t peer_ssrc; /**< Peer SSRC. */
  181. pj_uint32_t received; /**< Number of received packets. */
  182. };
  183. /**
  184. * @see pjmedia_rtp_session
  185. */
  186. typedef struct pjmedia_rtp_session pjmedia_rtp_session;
  187. /**
  188. * This structure is used to receive additional information about the
  189. * state of incoming RTP packet.
  190. */
  191. struct pjmedia_rtp_status
  192. {
  193. union {
  194. struct flag {
  195. int bad:1; /**< General flag to indicate that sequence is
  196. bad, and application should not process
  197. this packet. More information will be given
  198. in other flags. */
  199. int badpt:1; /**< Bad payload type. */
  200. int badssrc:1; /**< Bad SSRC */
  201. int dup:1; /**< Indicates duplicate packet */
  202. int outorder:1; /**< Indicates out of order packet */
  203. int probation:1;/**< Indicates that session is in probation
  204. until more packets are received. */
  205. int restart:1; /**< Indicates that sequence number has made
  206. a large jump, and internal base sequence
  207. number has been adjusted. */
  208. } flag; /**< Status flags. */
  209. pj_uint16_t value; /**< Status value, to conveniently address all
  210. flags. */
  211. } status; /**< Status information union. */
  212. pj_uint16_t diff; /**< Sequence number difference from previous
  213. packet. Normally the value should be 1.
  214. Value greater than one may indicate packet
  215. loss. If packet with lower sequence is
  216. received, the value will be set to zero.
  217. If base sequence has been restarted, the
  218. value will be one. */
  219. };
  220. /**
  221. * RTP session settings.
  222. */
  223. typedef struct pjmedia_rtp_session_setting
  224. {
  225. pj_uint8_t flags; /**< Bitmask flags to specify whether such
  226. field is set. Bitmask contents are:
  227. (bit #0 is LSB)
  228. bit #0: default payload type
  229. bit #1: sender SSRC
  230. bit #2: sequence
  231. bit #3: timestamp
  232. bit #4: peer SSRC */
  233. int default_pt; /**< Default payload type. */
  234. pj_uint32_t sender_ssrc; /**< Sender SSRC. */
  235. pj_uint32_t peer_ssrc; /**< Peer SSRC. */
  236. pj_uint16_t seq; /**< Sequence. */
  237. pj_uint32_t ts; /**< Timestamp. */
  238. } pjmedia_rtp_session_setting;
  239. /**
  240. * @see pjmedia_rtp_status
  241. */
  242. typedef struct pjmedia_rtp_status pjmedia_rtp_status;
  243. /**
  244. * This function will initialize the RTP session according to given parameters.
  245. *
  246. * @param ses The session.
  247. * @param default_pt Default payload type.
  248. * @param sender_ssrc SSRC used for outgoing packets, in host byte order.
  249. *
  250. * @return PJ_SUCCESS if successfull.
  251. */
  252. PJ_DECL(pj_status_t) pjmedia_rtp_session_init( pjmedia_rtp_session *ses,
  253. int default_pt,
  254. pj_uint32_t sender_ssrc );
  255. /**
  256. * This function will initialize the RTP session according to given parameters
  257. * defined in RTP session settings.
  258. *
  259. * @param ses The session.
  260. * @param settings RTP session settings.
  261. *
  262. * @return PJ_SUCCESS if successfull.
  263. */
  264. PJ_DECL(pj_status_t) pjmedia_rtp_session_init2(
  265. pjmedia_rtp_session *ses,
  266. pjmedia_rtp_session_setting settings);
  267. /**
  268. * Create the RTP header based on arguments and current state of the RTP
  269. * session.
  270. *
  271. * @param ses The session.
  272. * @param pt Payload type.
  273. * @param m Marker flag.
  274. * @param payload_len Payload length in bytes.
  275. * @param ts_len Timestamp length.
  276. * @param rtphdr Upon return will point to RTP packet header.
  277. * @param hdrlen Upon return will indicate the size of RTP packet header
  278. *
  279. * @return PJ_SUCCESS if successfull.
  280. */
  281. PJ_DECL(pj_status_t) pjmedia_rtp_encode_rtp( pjmedia_rtp_session *ses,
  282. int pt, int m,
  283. int payload_len, int ts_len,
  284. const void **rtphdr,
  285. int *hdrlen );
  286. /**
  287. * This function decodes incoming packet into RTP header and payload.
  288. * The decode function is guaranteed to point the payload to the correct
  289. * position regardless of any options present in the RTP packet.
  290. *
  291. * Note that this function does not modify the returned RTP header to
  292. * host byte order.
  293. *
  294. * @param ses The session.
  295. * @param pkt The received RTP packet.
  296. * @param pkt_len The length of the packet.
  297. * @param hdr Upon return will point to the location of the RTP
  298. * header inside the packet. Note that the RTP header
  299. * will be given back as is, meaning that the fields
  300. * will be in network byte order.
  301. * @param payload Upon return will point to the location of the
  302. * payload inside the packet.
  303. * @param payloadlen Upon return will indicate the size of the payload.
  304. *
  305. * @return PJ_SUCCESS if successfull.
  306. */
  307. PJ_DECL(pj_status_t) pjmedia_rtp_decode_rtp( pjmedia_rtp_session *ses,
  308. const void *pkt, int pkt_len,
  309. const pjmedia_rtp_hdr **hdr,
  310. const void **payload,
  311. unsigned *payloadlen);
  312. /**
  313. * This function decodes incoming packet into RTP header and payload.
  314. * The decode function is guaranteed to point the payload to the correct
  315. * position regardless of any options present in the RTP packet.
  316. *
  317. * Note that this function does not modify the returned RTP header to
  318. * host byte order.
  319. *
  320. * @param ses The session.
  321. * @param pkt The received RTP packet.
  322. * @param pkt_len The length of the packet.
  323. * @param hdr Upon return will point to the location of the RTP
  324. * header inside the packet. Note that the RTP header
  325. * will be given back as is, meaning that the fields
  326. * will be in network byte order.
  327. * @param dec_hdr Upon return will point to the location of the
  328. * additional RTP header inside the packet, if any.
  329. * @param payload Upon return will point to the location of the
  330. * payload inside the packet.
  331. * @param payloadlen Upon return will indicate the size of the payload.
  332. *
  333. * @return PJ_SUCCESS if successfull.
  334. */
  335. PJ_DECL(pj_status_t) pjmedia_rtp_decode_rtp2(
  336. pjmedia_rtp_session *ses,
  337. const void *pkt, int pkt_len,
  338. const pjmedia_rtp_hdr **hdr,
  339. pjmedia_rtp_dec_hdr *dec_hdr,
  340. const void **payload,
  341. unsigned *payloadlen);
  342. /**
  343. * Call this function everytime an RTP packet is received to check whether
  344. * the packet can be received and to let the RTP session performs its internal
  345. * calculations.
  346. *
  347. * @param ses The session.
  348. * @param hdr The RTP header of the incoming packet. The header must
  349. * be given with fields in network byte order.
  350. * @param seq_st Optional structure to receive the status of the RTP packet
  351. * processing.
  352. */
  353. PJ_DECL(void) pjmedia_rtp_session_update( pjmedia_rtp_session *ses,
  354. const pjmedia_rtp_hdr *hdr,
  355. pjmedia_rtp_status *seq_st);
  356. /**
  357. * Call this function everytime an RTP packet is received to check whether
  358. * the packet can be received and to let the RTP session performs its internal
  359. * calculations.
  360. *
  361. * @param ses The session.
  362. * @param hdr The RTP header of the incoming packet. The header must
  363. * be given with fields in network byte order.
  364. * @param seq_st Optional structure to receive the status of the RTP packet
  365. * processing.
  366. * @param check_pt Flag to indicate whether payload type needs to be validate.
  367. *
  368. * @see pjmedia_rtp_session_update()
  369. */
  370. PJ_DECL(void) pjmedia_rtp_session_update2(pjmedia_rtp_session *ses,
  371. const pjmedia_rtp_hdr *hdr,
  372. pjmedia_rtp_status *seq_st,
  373. pj_bool_t check_pt);
  374. /*
  375. * INTERNAL:
  376. */
  377. /**
  378. * Internal function for creating sequence number control, shared by RTCP
  379. * implementation.
  380. *
  381. * @param seq_ctrl The sequence control instance.
  382. * @param seq Sequence number to initialize.
  383. */
  384. void pjmedia_rtp_seq_init(pjmedia_rtp_seq_session *seq_ctrl,
  385. pj_uint16_t seq);
  386. /**
  387. * Internal function update sequence control, shared by RTCP implementation.
  388. *
  389. * @param seq_ctrl The sequence control instance.
  390. * @param seq Sequence number to update.
  391. * @param seq_status Optional structure to receive additional information
  392. * about the packet.
  393. */
  394. void pjmedia_rtp_seq_update( pjmedia_rtp_seq_session *seq_ctrl,
  395. pj_uint16_t seq,
  396. pjmedia_rtp_status *seq_status);
  397. /**
  398. * @}
  399. */
  400. PJ_END_DECL
  401. #endif /* __PJMEDIA_RTP_H__ */