rtcp_xr.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  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_XR_H__
  20. #define __PJMEDIA_RTCP_XR_H__
  21. /**
  22. * @file rtcp_xr.h
  23. * @brief RTCP XR implementation.
  24. */
  25. #include <pjmedia/types.h>
  26. #include <pj/math.h>
  27. PJ_BEGIN_DECL
  28. /**
  29. * @defgroup PJMED_RTCP_XR RTCP Extended Report (XR) - RFC 3611
  30. * @ingroup PJMEDIA_SESSION
  31. * @brief RTCP XR extension to RTCP session
  32. * @{
  33. *
  34. * PJMEDIA implements subsets of RTCP XR specification (RFC 3611) to monitor
  35. * the quality of the real-time media (audio/video) transmission.
  36. */
  37. /**
  38. * Enumeration of report types of RTCP XR. Useful for user to enable varying
  39. * combinations of RTCP XR report blocks.
  40. */
  41. typedef enum {
  42. PJMEDIA_RTCP_XR_LOSS_RLE = (1 << 0),
  43. PJMEDIA_RTCP_XR_DUP_RLE = (1 << 1),
  44. PJMEDIA_RTCP_XR_RCPT_TIMES = (1 << 2),
  45. PJMEDIA_RTCP_XR_RR_TIME = (1 << 3),
  46. PJMEDIA_RTCP_XR_DLRR = (1 << 4),
  47. PJMEDIA_RTCP_XR_STATS = (1 << 5),
  48. PJMEDIA_RTCP_XR_VOIP_METRICS = (1 << 6)
  49. } pjmedia_rtcp_xr_type;
  50. /**
  51. * Enumeration of info need to be updated manually to RTCP XR. Most info
  52. * could be updated automatically each time RTP received.
  53. */
  54. typedef enum {
  55. PJMEDIA_RTCP_XR_INFO_SIGNAL_LVL = 1,
  56. PJMEDIA_RTCP_XR_INFO_NOISE_LVL = 2,
  57. PJMEDIA_RTCP_XR_INFO_RERL = 3,
  58. PJMEDIA_RTCP_XR_INFO_R_FACTOR = 4,
  59. PJMEDIA_RTCP_XR_INFO_MOS_LQ = 5,
  60. PJMEDIA_RTCP_XR_INFO_MOS_CQ = 6,
  61. PJMEDIA_RTCP_XR_INFO_CONF_PLC = 7,
  62. PJMEDIA_RTCP_XR_INFO_CONF_JBA = 8,
  63. PJMEDIA_RTCP_XR_INFO_CONF_JBR = 9,
  64. PJMEDIA_RTCP_XR_INFO_JB_NOM = 10,
  65. PJMEDIA_RTCP_XR_INFO_JB_MAX = 11,
  66. PJMEDIA_RTCP_XR_INFO_JB_ABS_MAX = 12
  67. } pjmedia_rtcp_xr_info;
  68. /**
  69. * Enumeration of PLC types definitions for RTCP XR report.
  70. */
  71. typedef enum {
  72. PJMEDIA_RTCP_XR_PLC_UNK = 0,
  73. PJMEDIA_RTCP_XR_PLC_DIS = 1,
  74. PJMEDIA_RTCP_XR_PLC_ENH = 2,
  75. PJMEDIA_RTCP_XR_PLC_STD = 3
  76. } pjmedia_rtcp_xr_plc_type;
  77. /**
  78. * Enumeration of jitter buffer types definitions for RTCP XR report.
  79. */
  80. typedef enum {
  81. PJMEDIA_RTCP_XR_JB_UNKNOWN = 0,
  82. PJMEDIA_RTCP_XR_JB_FIXED = 2,
  83. PJMEDIA_RTCP_XR_JB_ADAPTIVE = 3
  84. } pjmedia_rtcp_xr_jb_type;
  85. #pragma pack(1)
  86. /**
  87. * This type declares RTCP XR Report Header.
  88. */
  89. typedef struct pjmedia_rtcp_xr_rb_header
  90. {
  91. pj_uint8_t bt; /**< Block type. */
  92. pj_uint8_t specific; /**< Block specific data. */
  93. pj_uint16_t length; /**< Block length. */
  94. } pjmedia_rtcp_xr_rb_header;
  95. /**
  96. * This type declares RTCP XR Receiver Reference Time Report Block.
  97. */
  98. typedef struct pjmedia_rtcp_xr_rb_rr_time
  99. {
  100. pjmedia_rtcp_xr_rb_header header; /**< Block header. */
  101. pj_uint32_t ntp_sec; /**< NTP time, seconds part. */
  102. pj_uint32_t ntp_frac; /**< NTP time, fractions part. */
  103. } pjmedia_rtcp_xr_rb_rr_time;
  104. /**
  105. * This type declares RTCP XR DLRR Report Sub-block
  106. */
  107. typedef struct pjmedia_rtcp_xr_rb_dlrr_item
  108. {
  109. pj_uint32_t ssrc; /**< receiver SSRC */
  110. pj_uint32_t lrr; /**< last receiver report */
  111. pj_uint32_t dlrr; /**< delay since last receiver
  112. report */
  113. } pjmedia_rtcp_xr_rb_dlrr_item;
  114. /**
  115. * This type declares RTCP XR DLRR Report Block
  116. */
  117. typedef struct pjmedia_rtcp_xr_rb_dlrr
  118. {
  119. pjmedia_rtcp_xr_rb_header header; /**< Block header. */
  120. pjmedia_rtcp_xr_rb_dlrr_item item; /**< Block contents,
  121. variable length list */
  122. } pjmedia_rtcp_xr_rb_dlrr;
  123. /**
  124. * This type declares RTCP XR Statistics Summary Report Block
  125. */
  126. typedef struct pjmedia_rtcp_xr_rb_stats
  127. {
  128. pjmedia_rtcp_xr_rb_header header; /**< Block header. */
  129. pj_uint32_t ssrc; /**< Receiver SSRC */
  130. pj_uint16_t begin_seq; /**< Begin RTP sequence reported */
  131. pj_uint16_t end_seq; /**< End RTP sequence reported */
  132. pj_uint32_t lost; /**< Number of packet lost in this
  133. interval */
  134. pj_uint32_t dup; /**< Number of duplicated packet in
  135. this interval */
  136. pj_uint32_t jitter_min; /**< Minimum jitter in this interval */
  137. pj_uint32_t jitter_max; /**< Maximum jitter in this interval */
  138. pj_uint32_t jitter_mean; /**< Average jitter in this interval */
  139. pj_uint32_t jitter_dev; /**< Jitter deviation in this
  140. interval */
  141. pj_uint32_t toh_min:8; /**< Minimum ToH in this interval */
  142. pj_uint32_t toh_max:8; /**< Maximum ToH in this interval */
  143. pj_uint32_t toh_mean:8; /**< Average ToH in this interval */
  144. pj_uint32_t toh_dev:8; /**< ToH deviation in this interval */
  145. } pjmedia_rtcp_xr_rb_stats;
  146. /**
  147. * This type declares RTCP XR VoIP Metrics Report Block
  148. */
  149. typedef struct pjmedia_rtcp_xr_rb_voip_mtc
  150. {
  151. pjmedia_rtcp_xr_rb_header header; /**< Block header. */
  152. pj_uint32_t ssrc; /**< Receiver SSRC */
  153. pj_uint8_t loss_rate; /**< Packet loss rate */
  154. pj_uint8_t discard_rate; /**< Packet discarded rate */
  155. pj_uint8_t burst_den; /**< Burst density */
  156. pj_uint8_t gap_den; /**< Gap density */
  157. pj_uint16_t burst_dur; /**< Burst duration */
  158. pj_uint16_t gap_dur; /**< Gap duration */
  159. pj_uint16_t rnd_trip_delay;/**< Round trip delay */
  160. pj_uint16_t end_sys_delay; /**< End system delay */
  161. pj_uint8_t signal_lvl; /**< Signal level */
  162. pj_uint8_t noise_lvl; /**< Noise level */
  163. pj_uint8_t rerl; /**< Residual Echo Return Loss */
  164. pj_uint8_t gmin; /**< The gap threshold */
  165. pj_uint8_t r_factor; /**< Voice quality metric carried
  166. over this RTP session */
  167. pj_uint8_t ext_r_factor; /**< Voice quality metric carried
  168. outside of this RTP session*/
  169. pj_uint8_t mos_lq; /**< Mean Opinion Score for
  170. Listening Quality */
  171. pj_uint8_t mos_cq; /**< Mean Opinion Score for
  172. Conversation Quality */
  173. pj_uint8_t rx_config; /**< Receiver configuration */
  174. pj_uint8_t reserved2; /**< Not used */
  175. pj_uint16_t jb_nom; /**< Current delay by jitter
  176. buffer */
  177. pj_uint16_t jb_max; /**< Maximum delay by jitter
  178. buffer */
  179. pj_uint16_t jb_abs_max; /**< Maximum possible delay by
  180. jitter buffer */
  181. } pjmedia_rtcp_xr_rb_voip_mtc;
  182. /**
  183. * Constant of RTCP-XR content size.
  184. */
  185. #define PJMEDIA_RTCP_XR_BUF_SIZE \
  186. sizeof(pjmedia_rtcp_xr_rb_rr_time) + \
  187. sizeof(pjmedia_rtcp_xr_rb_dlrr) + \
  188. sizeof(pjmedia_rtcp_xr_rb_stats) + \
  189. sizeof(pjmedia_rtcp_xr_rb_voip_mtc)
  190. /**
  191. * This structure declares RTCP XR (Extended Report) packet.
  192. */
  193. typedef struct pjmedia_rtcp_xr_pkt
  194. {
  195. struct {
  196. #if defined(PJ_IS_BIG_ENDIAN) && PJ_IS_BIG_ENDIAN!=0
  197. unsigned version:2; /**< packet type */
  198. unsigned p:1; /**< padding flag */
  199. unsigned count:5; /**< varies by payload type */
  200. unsigned pt:8; /**< payload type */
  201. #else
  202. unsigned count:5; /**< varies by payload type */
  203. unsigned p:1; /**< padding flag */
  204. unsigned version:2; /**< packet type */
  205. unsigned pt:8; /**< payload type */
  206. #endif
  207. unsigned length:16; /**< packet length */
  208. pj_uint32_t ssrc; /**< SSRC identification */
  209. } common;
  210. pj_int8_t buf[PJMEDIA_RTCP_XR_BUF_SIZE];
  211. /**< Content buffer */
  212. } pjmedia_rtcp_xr_pkt;
  213. #pragma pack()
  214. /**
  215. * This structure describes RTCP XR statitic.
  216. */
  217. typedef struct pjmedia_rtcp_xr_stream_stat
  218. {
  219. struct {
  220. pj_time_val update; /**< Time of last update. */
  221. pj_uint32_t begin_seq; /**< Begin # seq of this interval. */
  222. pj_uint32_t end_seq; /**< End # seq of this interval. */
  223. unsigned count; /**< Number of packets. */
  224. /**
  225. * Flags represent whether the such report is valid/updated
  226. */
  227. unsigned l:1; /**< Lost flag */
  228. unsigned d:1; /**< Duplicated flag */
  229. unsigned j:1; /**< Jitter flag */
  230. unsigned t:2; /**< TTL or Hop Limit,
  231. 0=none, 1=TTL, 2=HL */
  232. unsigned lost; /**< Number of packets lost */
  233. unsigned dup; /**< Number of duplicated packets */
  234. pj_math_stat jitter; /**< Jitter statistics (in usec) */
  235. pj_math_stat toh; /**< TTL of hop limit statistics. */
  236. } stat_sum;
  237. struct {
  238. pj_time_val update; /**< Time of last update. */
  239. pj_uint8_t loss_rate; /**< Packet loss rate */
  240. pj_uint8_t discard_rate; /**< Packet discarded rate */
  241. pj_uint8_t burst_den; /**< Burst density */
  242. pj_uint8_t gap_den; /**< Gap density */
  243. pj_uint16_t burst_dur; /**< Burst duration */
  244. pj_uint16_t gap_dur; /**< Gap duration */
  245. pj_uint16_t rnd_trip_delay; /**< Round trip delay */
  246. pj_uint16_t end_sys_delay; /**< End system delay */
  247. pj_int8_t signal_lvl; /**< Signal level */
  248. pj_int8_t noise_lvl; /**< Noise level */
  249. pj_uint8_t rerl; /**< Residual Echo Return Loss */
  250. pj_uint8_t gmin; /**< The gap threshold */
  251. pj_uint8_t r_factor; /**< Voice quality metric carried
  252. over this RTP session */
  253. pj_uint8_t ext_r_factor; /**< Voice quality metric carried
  254. outside of this RTP session*/
  255. pj_uint8_t mos_lq; /**< Mean Opinion Score for
  256. Listening Quality */
  257. pj_uint8_t mos_cq; /**< Mean Opinion Score for
  258. Conversation Quality */
  259. pj_uint8_t rx_config; /**< Receiver configuration */
  260. pj_uint16_t jb_nom; /**< Current delay by jitter
  261. buffer */
  262. pj_uint16_t jb_max; /**< Maximum delay by jitter
  263. buffer */
  264. pj_uint16_t jb_abs_max; /**< Maximum possible delay by
  265. jitter buffer */
  266. } voip_mtc;
  267. } pjmedia_rtcp_xr_stream_stat;
  268. typedef struct pjmedia_rtcp_xr_stat
  269. {
  270. pjmedia_rtcp_xr_stream_stat rx; /**< Decoding direction statistics. */
  271. pjmedia_rtcp_xr_stream_stat tx; /**< Encoding direction statistics. */
  272. pj_math_stat rtt; /**< Round-trip delay stat (in usec)
  273. the value is calculated from
  274. receiver side. */
  275. } pjmedia_rtcp_xr_stat;
  276. /**
  277. * Forward declaration of RTCP session
  278. */
  279. struct pjmedia_rtcp_session;
  280. /**
  281. * RTCP session is used to monitor the RTP session of one endpoint. There
  282. * should only be one RTCP session for a bidirectional RTP streams.
  283. */
  284. struct pjmedia_rtcp_xr_session
  285. {
  286. char *name; /**< Name identification. */
  287. pjmedia_rtcp_xr_pkt pkt; /**< Cached RTCP XR packet. */
  288. pj_uint32_t rx_lrr; /**< NTP ts in last RR received. */
  289. pj_timestamp rx_lrr_time;/**< Time when last RR is received. */
  290. pj_uint32_t rx_last_rr; /**< # pkt received since last
  291. sending RR time. */
  292. pjmedia_rtcp_xr_stat stat; /**< RTCP XR statistics. */
  293. /* The reference sequence number is an extended sequence number
  294. * that serves as the basis for determining whether a new 16 bit
  295. * sequence number comes earlier or later in the 32 bit sequence
  296. * space.
  297. */
  298. pj_uint32_t src_ref_seq;
  299. pj_bool_t uninitialized_src_ref_seq;
  300. /* This structure contains variables needed for calculating
  301. * burst metrics.
  302. */
  303. struct {
  304. pj_uint32_t pkt;
  305. pj_uint32_t lost;
  306. pj_uint32_t loss_count;
  307. pj_uint32_t discard_count;
  308. pj_uint32_t c11;
  309. pj_uint32_t c13;
  310. pj_uint32_t c14;
  311. pj_uint32_t c22;
  312. pj_uint32_t c23;
  313. pj_uint32_t c33;
  314. } voip_mtc_stat;
  315. unsigned ptime; /**< Packet time. */
  316. unsigned frames_per_packet; /**< # frames per packet. */
  317. struct pjmedia_rtcp_session *rtcp_session;
  318. /**< Parent/RTCP session. */
  319. };
  320. typedef struct pjmedia_rtcp_xr_session pjmedia_rtcp_xr_session;
  321. /**
  322. * Build an RTCP XR packet which contains one or more RTCP XR report blocks.
  323. * There are seven report types as defined in RFC 3611.
  324. *
  325. * @param session The RTCP XR session.
  326. * @param rpt_types Report types to be included in the packet, report types
  327. * are defined in pjmedia_rtcp_xr_type, set this to zero
  328. * will make this function build all reports appropriately.
  329. * @param rtcp_pkt Upon return, it will contain pointer to the RTCP XR packet.
  330. * @param len Upon return, it will indicate the size of the generated
  331. * RTCP XR packet.
  332. */
  333. PJ_DECL(void) pjmedia_rtcp_build_rtcp_xr( pjmedia_rtcp_xr_session *session,
  334. unsigned rpt_types,
  335. void **rtcp_pkt, int *len);
  336. /**
  337. * Call this function to manually update some info needed by RTCP XR to
  338. * generate report which could not be populated directly when receiving
  339. * RTP.
  340. *
  341. * @param session The RTCP XR session.
  342. * @param info Info type to be updated, @see pjmedia_rtcp_xr_info.
  343. * @param val Value.
  344. */
  345. PJ_DECL(pj_status_t) pjmedia_rtcp_xr_update_info(
  346. pjmedia_rtcp_xr_session *session,
  347. unsigned info,
  348. pj_int32_t val);
  349. /*
  350. * Private APIs:
  351. */
  352. /**
  353. * This function is called internally by RTCP session when RTCP XR is enabled
  354. * to initialize the RTCP XR session.
  355. *
  356. * @param session RTCP XR session.
  357. * @param r_session RTCP session.
  358. * @param gmin Gmin value (defined in RFC 3611), set to 0 for default (16).
  359. * @param frames_per_packet
  360. Number of frames per packet.
  361. */
  362. void pjmedia_rtcp_xr_init( pjmedia_rtcp_xr_session *session,
  363. struct pjmedia_rtcp_session *r_session,
  364. pj_uint8_t gmin,
  365. unsigned frames_per_packet);
  366. /**
  367. * This function is called internally by RTCP session to destroy
  368. * the RTCP XR session.
  369. *
  370. * @param session RTCP XR session.
  371. */
  372. void pjmedia_rtcp_xr_fini( pjmedia_rtcp_xr_session *session );
  373. /**
  374. * This function is called internally by RTCP session when it receives
  375. * incoming RTCP XR packets.
  376. *
  377. * @param session RTCP XR session.
  378. * @param rtcp_pkt The received RTCP XR packet.
  379. * @param size Size of the incoming packet.
  380. */
  381. void pjmedia_rtcp_xr_rx_rtcp_xr( pjmedia_rtcp_xr_session *session,
  382. const void *rtcp_pkt,
  383. pj_size_t size);
  384. /**
  385. * This function is called internally by RTCP session whenever an RTP packet
  386. * is received or lost to let the RTCP XR session update its statistics.
  387. * Data passed to this function is a result of analyzation by RTCP and the
  388. * jitter buffer. Whenever some info is available, the value should be zero
  389. * or more (no negative info), otherwise if info is not available the info
  390. * should be -1 so no update will be done for this info in the RTCP XR session.
  391. *
  392. * @param session RTCP XR session.
  393. * @param seq Sequence number of RTP packet.
  394. * @param lost Info if this packet is lost.
  395. * @param dup Info if this packet is a duplication.
  396. * @param discarded Info if this packet is discarded
  397. * (not because of duplication).
  398. * @param jitter Info jitter of this packet.
  399. * @param toh Info Time To Live or Hops Limit of this packet.
  400. * @param toh_ipv4 Set PJ_TRUE if packet is transported over IPv4.
  401. */
  402. void pjmedia_rtcp_xr_rx_rtp( pjmedia_rtcp_xr_session *session,
  403. unsigned seq,
  404. int lost,
  405. int dup,
  406. int discarded,
  407. int jitter,
  408. int toh, pj_bool_t toh_ipv4);
  409. /**
  410. * This function is called internally by RTCP session whenever an RTP
  411. * packet is sent to let the RTCP XR session do its internal calculations.
  412. *
  413. * @param session RTCP XR session.
  414. * @param ptsize Size of RTP payload being sent.
  415. */
  416. void pjmedia_rtcp_xr_tx_rtp( pjmedia_rtcp_xr_session *session,
  417. unsigned ptsize );
  418. /**
  419. * @}
  420. */
  421. PJ_END_DECL
  422. #endif /* __PJMEDIA_RTCP_XR_H__ */