rtcp.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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_RTCP_H__
  20. #define __PJMEDIA_RTCP_H__
  21. /**
  22. * @file rtcp.h
  23. * @brief RTCP implementation.
  24. */
  25. #include <pjmedia/types.h>
  26. #include <pjmedia/rtcp_xr.h>
  27. #include <pjmedia/rtp.h>
  28. PJ_BEGIN_DECL
  29. /**
  30. * @defgroup PJMED_RTCP RTCP Session and Encapsulation (RFC 3550)
  31. * @ingroup PJMEDIA_SESSION
  32. * @brief RTCP format and session management
  33. * @{
  34. *
  35. * PJMEDIA implements subsets of RTCP specification (RFC 3550) to monitor
  36. * the quality of the real-time media (audio/video) transmission. In
  37. * addition to the standard quality monitoring and reporting with RTCP
  38. * SR and RR types, PJMEDIA's RTCP implementation is able to report
  39. * extended statistics for incoming streams, such as packet duplications,
  40. * reorder, discarded, and loss period (to distinguish between random
  41. * and burst loss).
  42. *
  43. * The bidirectional media quality statistic is represented with
  44. * #pjmedia_rtcp_stat structure.
  45. *
  46. * When application uses the stream interface (see @ref PJMED_STRM),
  47. * application may retrieve the RTCP statistic by calling
  48. * #pjmedia_stream_get_stat() function.
  49. */
  50. #pragma pack(1)
  51. /**
  52. * RTCP sender report.
  53. */
  54. typedef struct pjmedia_rtcp_sr
  55. {
  56. pj_uint32_t ntp_sec; /**< NTP time, seconds part. */
  57. pj_uint32_t ntp_frac; /**< NTP time, fractions part. */
  58. pj_uint32_t rtp_ts; /**< RTP timestamp. */
  59. pj_uint32_t sender_pcount; /**< Sender packet cound. */
  60. pj_uint32_t sender_bcount; /**< Sender octet/bytes count. */
  61. } pjmedia_rtcp_sr;
  62. /**
  63. * RTCP receiver report.
  64. */
  65. typedef struct pjmedia_rtcp_rr
  66. {
  67. pj_uint32_t ssrc; /**< SSRC identification. */
  68. #if defined(PJ_IS_BIG_ENDIAN) && PJ_IS_BIG_ENDIAN!=0
  69. pj_uint32_t fract_lost:8; /**< Fraction lost. */
  70. pj_uint32_t total_lost_2:8; /**< Total lost, bit 16-23. */
  71. pj_uint32_t total_lost_1:8; /**< Total lost, bit 8-15. */
  72. pj_uint32_t total_lost_0:8; /**< Total lost, bit 0-7. */
  73. #else
  74. pj_uint32_t fract_lost:8; /**< Fraction lost. */
  75. pj_uint32_t total_lost_2:8; /**< Total lost, bit 0-7. */
  76. pj_uint32_t total_lost_1:8; /**< Total lost, bit 8-15. */
  77. pj_uint32_t total_lost_0:8; /**< Total lost, bit 16-23. */
  78. #endif
  79. pj_uint32_t last_seq; /**< Last sequence number. */
  80. pj_uint32_t jitter; /**< Jitter. */
  81. pj_uint32_t lsr; /**< Last SR. */
  82. pj_uint32_t dlsr; /**< Delay since last SR. */
  83. } pjmedia_rtcp_rr;
  84. /**
  85. * RTCP common header.
  86. */
  87. typedef struct pjmedia_rtcp_common
  88. {
  89. #if defined(PJ_IS_BIG_ENDIAN) && PJ_IS_BIG_ENDIAN!=0
  90. unsigned version:2; /**< packet type */
  91. unsigned p:1; /**< padding flag */
  92. unsigned count:5; /**< varies by payload type */
  93. unsigned pt:8; /**< payload type */
  94. #else
  95. unsigned count:5; /**< varies by payload type */
  96. unsigned p:1; /**< padding flag */
  97. unsigned version:2; /**< packet type */
  98. unsigned pt:8; /**< payload type */
  99. #endif
  100. unsigned length:16; /**< packet length */
  101. pj_uint32_t ssrc; /**< SSRC identification */
  102. } pjmedia_rtcp_common;
  103. /**
  104. * RTCP feedback common header.
  105. */
  106. typedef struct pjmedia_rtcp_fb_common
  107. {
  108. pjmedia_rtcp_common rtcp_common;
  109. pj_uint32_t ssrc_src; /**< SSRC media source */
  110. } pjmedia_rtcp_fb_common;
  111. /**
  112. * This structure declares default RTCP packet (SR) that is sent by pjmedia.
  113. * Incoming RTCP packet may have different format, and must be parsed
  114. * manually by application.
  115. */
  116. typedef struct pjmedia_rtcp_sr_pkt
  117. {
  118. pjmedia_rtcp_common common; /**< Common header. */
  119. pjmedia_rtcp_sr sr; /**< Sender report. */
  120. pjmedia_rtcp_rr rr; /**< variable-length list */
  121. } pjmedia_rtcp_sr_pkt;
  122. /**
  123. * This structure declares RTCP RR (Receiver Report) packet.
  124. */
  125. typedef struct pjmedia_rtcp_rr_pkt
  126. {
  127. pjmedia_rtcp_common common; /**< Common header. */
  128. pjmedia_rtcp_rr rr; /**< variable-length list */
  129. } pjmedia_rtcp_rr_pkt;
  130. #pragma pack()
  131. /**
  132. * RTCP SDES structure.
  133. */
  134. typedef struct pjmedia_rtcp_sdes
  135. {
  136. pj_str_t cname; /**< RTCP SDES type CNAME. */
  137. pj_str_t name; /**< RTCP SDES type NAME. */
  138. pj_str_t email; /**< RTCP SDES type EMAIL. */
  139. pj_str_t phone; /**< RTCP SDES type PHONE. */
  140. pj_str_t loc; /**< RTCP SDES type LOC. */
  141. pj_str_t tool; /**< RTCP SDES type TOOL. */
  142. pj_str_t note; /**< RTCP SDES type NOTE. */
  143. } pjmedia_rtcp_sdes;
  144. /**
  145. * NTP time representation.
  146. */
  147. typedef struct pjmedia_rtcp_ntp_rec
  148. {
  149. pj_uint32_t hi; /**< High order 32-bit part. */
  150. pj_uint32_t lo; /**< Lo order 32-bit part. */
  151. } pjmedia_rtcp_ntp_rec;
  152. /**
  153. * Unidirectional RTP stream statistics.
  154. */
  155. typedef struct pjmedia_rtcp_stream_stat
  156. {
  157. pj_time_val update; /**< Time of last update. */
  158. unsigned update_cnt; /**< Number of updates (to calculate avg) */
  159. pj_uint32_t pkt; /**< Total number of packets */
  160. pj_uint32_t bytes; /**< Total number of payload/bytes */
  161. unsigned discard; /**< Total number of discarded packets. */
  162. unsigned loss; /**< Total number of packets lost */
  163. unsigned reorder; /**< Total number of out of order packets */
  164. unsigned dup; /**< Total number of duplicates packets */
  165. pj_math_stat loss_period;/**< Loss period statistics (in usec) */
  166. struct {
  167. unsigned burst:1; /**< Burst/sequential packet lost detected */
  168. unsigned random:1; /**< Random packet lost detected. */
  169. } loss_type; /**< Types of loss detected. */
  170. pj_math_stat jitter; /**< Jitter statistics (in usec) */
  171. } pjmedia_rtcp_stream_stat;
  172. /**
  173. * Bidirectional RTP stream statistics.
  174. */
  175. typedef struct pjmedia_rtcp_stat
  176. {
  177. pj_time_val start; /**< Time when session was created */
  178. pjmedia_rtcp_stream_stat tx; /**< Encoder stream statistics. */
  179. pjmedia_rtcp_stream_stat rx; /**< Decoder stream statistics. */
  180. pj_math_stat rtt; /**< Round trip delay statistic(in usec)*/
  181. pj_uint32_t rtp_tx_last_ts; /**< Last TX RTP timestamp. */
  182. pj_uint16_t rtp_tx_last_seq;/**< Last TX RTP sequence. */
  183. #if defined(PJMEDIA_RTCP_STAT_HAS_IPDV) && PJMEDIA_RTCP_STAT_HAS_IPDV!=0
  184. pj_math_stat rx_ipdv;/**< Statistics of IP packet delay
  185. variation in receiving direction
  186. (in usec). */
  187. #endif
  188. #if defined(PJMEDIA_RTCP_STAT_HAS_RAW_JITTER) && PJMEDIA_RTCP_STAT_HAS_RAW_JITTER!=0
  189. pj_math_stat rx_raw_jitter;/**< Statistic of raw jitter in
  190. receiving direction
  191. (in usec). */
  192. #endif
  193. pjmedia_rtcp_sdes peer_sdes; /**< Peer SDES. */
  194. char peer_sdes_buf_[PJMEDIA_RTCP_RX_SDES_BUF_LEN];
  195. /**< Peer SDES buffer. */
  196. } pjmedia_rtcp_stat;
  197. /**
  198. * RTCP session is used to monitor the RTP session of one endpoint. There
  199. * should only be one RTCP session for a bidirectional RTP streams.
  200. */
  201. typedef struct pjmedia_rtcp_session
  202. {
  203. char *name; /**< Name identification. */
  204. pjmedia_rtcp_sr_pkt rtcp_sr_pkt;/**< Cached RTCP SR packet. */
  205. pjmedia_rtcp_rr_pkt rtcp_rr_pkt;/**< Cached RTCP RR packet. */
  206. pjmedia_rtcp_fb_common rtcp_fb_com;/**< Cached RTCP feedback common
  207. header packet. */
  208. pjmedia_rtp_seq_session seq_ctrl; /**< RTCP sequence number control. */
  209. unsigned rtp_last_ts;/**< Last timestamp in RX RTP pkt. */
  210. unsigned clock_rate; /**< Clock rate of the stream */
  211. unsigned pkt_size; /**< Avg pkt size, in samples. */
  212. unsigned dec_pkt_size;/**< Decoding pkt size, in samples.*/
  213. pj_uint32_t received; /**< # pkt received */
  214. pj_uint32_t exp_prior; /**< # pkt expected at last interval*/
  215. pj_uint32_t rx_prior; /**< # pkt received at last interval*/
  216. pj_int32_t transit; /**< Rel transit time for prev pkt */
  217. pj_uint32_t jitter; /**< Scaled jitter */
  218. pj_time_val tv_base; /**< Base time, in seconds. */
  219. pj_timestamp ts_base; /**< Base system timestamp. */
  220. pj_timestamp ts_freq; /**< System timestamp frequency. */
  221. pj_uint32_t rtp_ts_base;/**< Base RTP timestamp. */
  222. pj_uint32_t rx_lsr; /**< NTP ts in last SR received */
  223. pj_timestamp rx_lsr_time;/**< Time when last SR is received */
  224. pj_uint32_t peer_ssrc; /**< Peer SSRC */
  225. pjmedia_rtcp_stat stat; /**< Bidirectional stream stat. */
  226. #if defined(PJMEDIA_HAS_RTCP_XR) && (PJMEDIA_HAS_RTCP_XR != 0)
  227. /**
  228. * Specify whether RTCP XR processing is enabled on this session.
  229. */
  230. pj_bool_t xr_enabled;
  231. /**
  232. * RTCP XR session, only valid if RTCP XR processing is enabled
  233. * on this session.
  234. */
  235. pjmedia_rtcp_xr_session xr_session;
  236. #endif
  237. } pjmedia_rtcp_session;
  238. /**
  239. * RTCP session settings.
  240. */
  241. typedef struct pjmedia_rtcp_session_setting
  242. {
  243. char *name; /**< RTCP session name. */
  244. unsigned clock_rate; /**< Sequence. */
  245. unsigned samples_per_frame; /**< Timestamp. */
  246. unsigned dec_samples_per_frame; /**< Timestamp. */
  247. pj_uint32_t ssrc; /**< Sender SSRC. */
  248. pj_uint32_t rtp_ts_base; /**< Base RTP timestamp. */
  249. } pjmedia_rtcp_session_setting;
  250. /**
  251. * Initialize RTCP session setting.
  252. *
  253. * @param settings The RTCP session setting to be initialized.
  254. */
  255. PJ_DECL(void) pjmedia_rtcp_session_setting_default(
  256. pjmedia_rtcp_session_setting *settings);
  257. /**
  258. * Initialize bidirectional RTCP statistics.
  259. *
  260. * @param stat The bidirectional RTCP statistics.
  261. */
  262. PJ_DECL(void) pjmedia_rtcp_init_stat(pjmedia_rtcp_stat *stat);
  263. /**
  264. * Initialize RTCP session.
  265. *
  266. * @param session The session
  267. * @param name Optional name to identify the session (for
  268. * logging purpose).
  269. * @param clock_rate Codec clock rate in samples per second.
  270. * @param samples_per_frame Average number of samples per frame.
  271. * @param ssrc The SSRC used in to identify the session.
  272. */
  273. PJ_DECL(void) pjmedia_rtcp_init( pjmedia_rtcp_session *session,
  274. char *name,
  275. unsigned clock_rate,
  276. unsigned samples_per_frame,
  277. pj_uint32_t ssrc );
  278. /**
  279. * Initialize RTCP session.
  280. *
  281. * @param session The session
  282. * @param settings The RTCP session settings.
  283. */
  284. PJ_DECL(void) pjmedia_rtcp_init2(pjmedia_rtcp_session *session,
  285. const pjmedia_rtcp_session_setting *settings);
  286. /**
  287. * Update RTCP session with the new settings. Note that any setting field
  288. * that has a value of zero will not be updated.
  289. *
  290. * @param session The session
  291. * @param settings The new RTCP session settings.
  292. * Set a field to zero to leave it unchanged.
  293. */
  294. PJ_DECL(void)
  295. pjmedia_rtcp_update(pjmedia_rtcp_session *session,
  296. const pjmedia_rtcp_session_setting *settings);
  297. /**
  298. * Utility function to retrieve current NTP timestamp.
  299. *
  300. * @param sess RTCP session.
  301. * @param ntp NTP record.
  302. *
  303. * @return PJ_SUCCESS on success.
  304. */
  305. PJ_DECL(pj_status_t) pjmedia_rtcp_get_ntp_time(const pjmedia_rtcp_session *sess,
  306. pjmedia_rtcp_ntp_rec *ntp);
  307. /**
  308. * Deinitialize RTCP session.
  309. *
  310. * @param session The session.
  311. */
  312. PJ_DECL(void) pjmedia_rtcp_fini( pjmedia_rtcp_session *session);
  313. /**
  314. * Call this function everytime an RTP packet is received to let the RTCP
  315. * session do its internal calculations.
  316. *
  317. * @param session The session.
  318. * @param seq The RTP packet sequence number, in host byte order.
  319. * @param ts The RTP packet timestamp, in host byte order.
  320. * @param payload Size of the payload.
  321. */
  322. PJ_DECL(void) pjmedia_rtcp_rx_rtp( pjmedia_rtcp_session *session,
  323. unsigned seq,
  324. unsigned ts,
  325. unsigned payload);
  326. /**
  327. * Call this function everytime an RTP packet is received to let the RTCP
  328. * session do its internal calculations.
  329. *
  330. * @param session The session.
  331. * @param seq The RTP packet sequence number, in host byte order.
  332. * @param ts The RTP packet timestamp, in host byte order.
  333. * @param payload Size of the payload.
  334. * @param discarded Flag to specify whether the packet is discarded.
  335. */
  336. PJ_DECL(void) pjmedia_rtcp_rx_rtp2(pjmedia_rtcp_session *session,
  337. unsigned seq,
  338. unsigned ts,
  339. unsigned payload,
  340. pj_bool_t discarded);
  341. /**
  342. * Call this function everytime an RTP packet is sent to let the RTCP session
  343. * do its internal calculations.
  344. *
  345. * @param session The session.
  346. * @param ptsize The payload size of the RTP packet (ie packet minus
  347. * RTP header) in bytes.
  348. */
  349. PJ_DECL(void) pjmedia_rtcp_tx_rtp( pjmedia_rtcp_session *session,
  350. unsigned ptsize );
  351. /**
  352. * Call this function when an RTCP packet is received from remote peer.
  353. * This RTCP packet received from remote is used to calculate the end-to-
  354. * end delay of the network.
  355. *
  356. * @param session RTCP session.
  357. * @param rtcp_pkt The received RTCP packet.
  358. * @param size Size of the incoming packet.
  359. */
  360. PJ_DECL(void) pjmedia_rtcp_rx_rtcp( pjmedia_rtcp_session *session,
  361. const void *rtcp_pkt,
  362. pj_size_t size);
  363. /**
  364. * Build a RTCP packet to be transmitted to remote RTP peer. This will
  365. * create RTCP Sender Report (SR) or Receiver Report (RR) depending on
  366. * whether the endpoint has been transmitting RTP since the last interval.
  367. * Note that this function will reset the interval counters (such as
  368. * the ones to calculate fraction lost) in the session.
  369. *
  370. * @param session The RTCP session.
  371. * @param rtcp_pkt Upon return, it will contain pointer to the
  372. * RTCP packet, which can be RTCP SR or RR.
  373. * @param len Upon return, it will indicate the size of
  374. * the RTCP packet.
  375. */
  376. PJ_DECL(void) pjmedia_rtcp_build_rtcp( pjmedia_rtcp_session *session,
  377. void **rtcp_pkt, int *len);
  378. /**
  379. * Build an RTCP SDES (source description) packet. This packet can be
  380. * appended to other RTCP packets, e.g: RTCP RR/SR, to compose a compound
  381. * RTCP packet.
  382. *
  383. * @param session The RTCP session.
  384. * @param buf The buffer to receive RTCP SDES packet.
  385. * @param length On input, it will contain the buffer length.
  386. * On output, it will contain the generated RTCP SDES
  387. * packet length.
  388. * @param sdes The source description, see #pjmedia_rtcp_sdes.
  389. *
  390. * @return PJ_SUCCESS on success.
  391. */
  392. PJ_DECL(pj_status_t) pjmedia_rtcp_build_rtcp_sdes(
  393. pjmedia_rtcp_session *session,
  394. void *buf,
  395. pj_size_t *length,
  396. const pjmedia_rtcp_sdes *sdes);
  397. /**
  398. * Build an RTCP BYE packet. This packet can be appended to other RTCP
  399. * packets, e.g: RTCP RR/SR, to compose a compound RTCP packet.
  400. *
  401. * @param session The RTCP session.
  402. * @param buf The buffer to receive RTCP BYE packet.
  403. * @param length On input, it will contain the buffer length.
  404. * On output, it will contain the generated RTCP BYE
  405. * packet length.
  406. * @param reason Optional, the BYE reason.
  407. *
  408. * @return PJ_SUCCESS on success.
  409. */
  410. PJ_DECL(pj_status_t) pjmedia_rtcp_build_rtcp_bye(
  411. pjmedia_rtcp_session *session,
  412. void *buf,
  413. pj_size_t *length,
  414. const pj_str_t *reason);
  415. /**
  416. * Call this function if RTCP XR needs to be enabled/disabled in the
  417. * RTCP session.
  418. *
  419. * @param session The RTCP session.
  420. * @param enable Enable/disable RTCP XR.
  421. *
  422. * @return PJ_SUCCESS on success.
  423. */
  424. PJ_DECL(pj_status_t) pjmedia_rtcp_enable_xr( pjmedia_rtcp_session *session,
  425. pj_bool_t enable);
  426. /**
  427. * @}
  428. */
  429. PJ_END_DECL
  430. #endif /* __PJMEDIA_RTCP_H__ */