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