1
/* $Id: rtcp.h 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
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.
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.
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
20
#ifndef __PJMEDIA_RTCP_H__
21
#define __PJMEDIA_RTCP_H__
25
* @brief RTCP implementation.
28
#include <pjmedia/types.h>
29
#include <pjmedia/rtcp_xr.h>
30
#include <pjmedia/rtp.h>
36
* @defgroup PJMED_RTCP RTCP Session and Encapsulation (RFC 3550)
37
* @ingroup PJMEDIA_SESSION
38
* @brief RTCP format and session management
41
* PJMEDIA implements subsets of RTCP specification (RFC 3550) to monitor
42
* the quality of the real-time media (audio/video) transmission. In
43
* addition to the standard quality monitoring and reporting with RTCP
44
* SR and RR types, PJMEDIA's RTCP implementation is able to report
45
* extended statistics for incoming streams, such as packet duplications,
46
* reorder, discarded, and loss period (to distinguish between random
49
* The bidirectional media quality statistic is represented with
50
* #pjmedia_rtcp_stat structure.
52
* When application uses the stream interface (see @ref PJMED_STRM),
53
* application may retrieve the RTCP statistic by calling
54
* #pjmedia_stream_get_stat() function.
62
struct pjmedia_rtcp_sr
64
pj_uint32_t ntp_sec; /**< NTP time, seconds part. */
65
pj_uint32_t ntp_frac; /**< NTP time, fractions part. */
66
pj_uint32_t rtp_ts; /**< RTP timestamp. */
67
pj_uint32_t sender_pcount; /**< Sender packet cound. */
68
pj_uint32_t sender_bcount; /**< Sender octet/bytes count. */
72
* @see pjmedia_rtcp_sr
74
typedef struct pjmedia_rtcp_sr pjmedia_rtcp_sr;
77
* RTCP receiver report.
79
struct pjmedia_rtcp_rr
81
pj_uint32_t ssrc; /**< SSRC identification. */
82
#if defined(PJ_IS_BIG_ENDIAN) && PJ_IS_BIG_ENDIAN!=0
83
pj_uint32_t fract_lost:8; /**< Fraction lost. */
84
pj_uint32_t total_lost_2:8; /**< Total lost, bit 16-23. */
85
pj_uint32_t total_lost_1:8; /**< Total lost, bit 8-15. */
86
pj_uint32_t total_lost_0:8; /**< Total lost, bit 0-7. */
88
pj_uint32_t fract_lost:8; /**< Fraction lost. */
89
pj_uint32_t total_lost_2:8; /**< Total lost, bit 0-7. */
90
pj_uint32_t total_lost_1:8; /**< Total lost, bit 8-15. */
91
pj_uint32_t total_lost_0:8; /**< Total lost, bit 16-23. */
93
pj_uint32_t last_seq; /**< Last sequence number. */
94
pj_uint32_t jitter; /**< Jitter. */
95
pj_uint32_t lsr; /**< Last SR. */
96
pj_uint32_t dlsr; /**< Delay since last SR. */
100
* @see pjmedia_rtcp_rr
102
typedef struct pjmedia_rtcp_rr pjmedia_rtcp_rr;
106
* RTCP common header.
108
struct pjmedia_rtcp_common
110
#if defined(PJ_IS_BIG_ENDIAN) && PJ_IS_BIG_ENDIAN!=0
111
unsigned version:2; /**< packet type */
112
unsigned p:1; /**< padding flag */
113
unsigned count:5; /**< varies by payload type */
114
unsigned pt:8; /**< payload type */
116
unsigned count:5; /**< varies by payload type */
117
unsigned p:1; /**< padding flag */
118
unsigned version:2; /**< packet type */
119
unsigned pt:8; /**< payload type */
121
unsigned length:16; /**< packet length */
122
pj_uint32_t ssrc; /**< SSRC identification */
126
* @see pjmedia_rtcp_common
128
typedef struct pjmedia_rtcp_common pjmedia_rtcp_common;
131
* This structure declares default RTCP packet (SR) that is sent by pjmedia.
132
* Incoming RTCP packet may have different format, and must be parsed
133
* manually by application.
135
typedef struct pjmedia_rtcp_sr_pkt
137
pjmedia_rtcp_common common; /**< Common header. */
138
pjmedia_rtcp_sr sr; /**< Sender report. */
139
pjmedia_rtcp_rr rr; /**< variable-length list */
140
} pjmedia_rtcp_sr_pkt;
143
* This structure declares RTCP RR (Receiver Report) packet.
145
typedef struct pjmedia_rtcp_rr_pkt
147
pjmedia_rtcp_common common; /**< Common header. */
148
pjmedia_rtcp_rr rr; /**< variable-length list */
149
} pjmedia_rtcp_rr_pkt;
156
* NTP time representation.
158
struct pjmedia_rtcp_ntp_rec
160
pj_uint32_t hi; /**< High order 32-bit part. */
161
pj_uint32_t lo; /**< Lo order 32-bit part. */
165
* @see pjmedia_rtcp_ntp_rec
167
typedef struct pjmedia_rtcp_ntp_rec pjmedia_rtcp_ntp_rec;
172
* Unidirectional RTP stream statistics.
174
struct pjmedia_rtcp_stream_stat
176
pj_time_val update; /**< Time of last update. */
177
unsigned update_cnt; /**< Number of updates (to calculate avg) */
178
pj_uint32_t pkt; /**< Total number of packets */
179
pj_uint32_t bytes; /**< Total number of payload/bytes */
180
unsigned discard; /**< Total number of discarded packets. */
181
unsigned loss; /**< Total number of packets lost */
182
unsigned reorder; /**< Total number of out of order packets */
183
unsigned dup; /**< Total number of duplicates packets */
185
pj_math_stat loss_period;/**< Loss period statistics (in usec) */
188
unsigned burst:1; /**< Burst/sequential packet lost detected */
189
unsigned random:1; /**< Random packet lost detected. */
190
} loss_type; /**< Types of loss detected. */
192
pj_math_stat jitter; /**< Jitter statistics (in usec) */
197
* @see pjmedia_rtcp_stream_stat
199
typedef struct pjmedia_rtcp_stream_stat pjmedia_rtcp_stream_stat;
204
* Bidirectional RTP stream statistics.
206
struct pjmedia_rtcp_stat
208
pj_time_val start; /**< Time when session was created */
210
pjmedia_rtcp_stream_stat tx; /**< Encoder stream statistics. */
211
pjmedia_rtcp_stream_stat rx; /**< Decoder stream statistics. */
213
pj_math_stat rtt; /**< Round trip delay statistic(in usec)*/
215
pj_uint32_t rtp_tx_last_ts; /**< Last TX RTP timestamp. */
216
pj_uint16_t rtp_tx_last_seq;/**< Last TX RTP sequence. */
218
#if defined(PJMEDIA_RTCP_STAT_HAS_IPDV) && PJMEDIA_RTCP_STAT_HAS_IPDV!=0
219
pj_math_stat rx_ipdv;/**< Statistics of IP packet delay
220
variation in receiving direction
224
#if defined(PJMEDIA_RTCP_STAT_HAS_RAW_JITTER) && PJMEDIA_RTCP_STAT_HAS_RAW_JITTER!=0
225
pj_math_stat rx_raw_jitter;/**< Statistic of raw jitter in
233
* @see pjmedia_rtcp_stat
235
typedef struct pjmedia_rtcp_stat pjmedia_rtcp_stat;
239
* RTCP session is used to monitor the RTP session of one endpoint. There
240
* should only be one RTCP session for a bidirectional RTP streams.
242
struct pjmedia_rtcp_session
244
char *name; /**< Name identification. */
245
pjmedia_rtcp_sr_pkt rtcp_sr_pkt;/**< Cached RTCP SR packet. */
246
pjmedia_rtcp_rr_pkt rtcp_rr_pkt;/**< Cached RTCP RR packet. */
248
pjmedia_rtp_seq_session seq_ctrl; /**< RTCP sequence number control. */
249
unsigned rtp_last_ts;/**< Last timestamp in RX RTP pkt. */
251
unsigned clock_rate; /**< Clock rate of the stream */
252
unsigned pkt_size; /**< Avg pkt size, in samples. */
253
pj_uint32_t received; /**< # pkt received */
254
pj_uint32_t exp_prior; /**< # pkt expected at last interval*/
255
pj_uint32_t rx_prior; /**< # pkt received at last interval*/
256
pj_int32_t transit; /**< Rel transit time for prev pkt */
257
pj_uint32_t jitter; /**< Scaled jitter */
258
pj_time_val tv_base; /**< Base time, in seconds. */
259
pj_timestamp ts_base; /**< Base system timestamp. */
260
pj_timestamp ts_freq; /**< System timestamp frequency. */
261
pj_uint32_t rtp_ts_base;/**< Base RTP timestamp. */
263
pj_uint32_t rx_lsr; /**< NTP ts in last SR received */
264
pj_timestamp rx_lsr_time;/**< Time when last SR is received */
265
pj_uint32_t peer_ssrc; /**< Peer SSRC */
267
pjmedia_rtcp_stat stat; /**< Bidirectional stream stat. */
269
#if defined(PJMEDIA_HAS_RTCP_XR) && (PJMEDIA_HAS_RTCP_XR != 0)
271
* Specify whether RTCP XR processing is enabled on this session.
273
pj_bool_t xr_enabled;
276
* RTCP XR session, only valid if RTCP XR processing is enabled
279
pjmedia_rtcp_xr_session xr_session;
284
* @see pjmedia_rtcp_session
286
typedef struct pjmedia_rtcp_session pjmedia_rtcp_session;
290
* RTCP session settings.
292
typedef struct pjmedia_rtcp_session_setting
294
char *name; /**< RTCP session name. */
295
unsigned clock_rate; /**< Sequence. */
296
unsigned samples_per_frame; /**< Timestamp. */
297
pj_uint32_t ssrc; /**< Sender SSRC. */
298
pj_uint32_t rtp_ts_base; /**< Base RTP timestamp. */
299
} pjmedia_rtcp_session_setting;
303
* Initialize RTCP session setting.
305
* @param settings The RTCP session setting to be initialized.
307
PJ_DECL(void) pjmedia_rtcp_session_setting_default(
308
pjmedia_rtcp_session_setting *settings);
312
* Initialize bidirectional RTCP statistics.
314
* @param stat The bidirectional RTCP statistics.
316
PJ_DECL(void) pjmedia_rtcp_init_stat(pjmedia_rtcp_stat *stat);
320
* Initialize RTCP session.
322
* @param session The session
323
* @param name Optional name to identify the session (for
325
* @param clock_rate Codec clock rate in samples per second.
326
* @param samples_per_frame Average number of samples per frame.
327
* @param ssrc The SSRC used in to identify the session.
329
PJ_DECL(void) pjmedia_rtcp_init( pjmedia_rtcp_session *session,
332
unsigned samples_per_frame,
337
* Initialize RTCP session.
339
* @param session The session
340
* @param settings The RTCP session settings.
342
PJ_DECL(void) pjmedia_rtcp_init2(pjmedia_rtcp_session *session,
343
const pjmedia_rtcp_session_setting *settings);
347
* Utility function to retrieve current NTP timestamp.
349
* @param sess RTCP session.
350
* @param ntp NTP record.
352
* @return PJ_SUCCESS on success.
354
PJ_DECL(pj_status_t) pjmedia_rtcp_get_ntp_time(const pjmedia_rtcp_session *sess,
355
pjmedia_rtcp_ntp_rec *ntp);
359
* Deinitialize RTCP session.
361
* @param session The session.
363
PJ_DECL(void) pjmedia_rtcp_fini( pjmedia_rtcp_session *session);
367
* Call this function everytime an RTP packet is received to let the RTCP
368
* session do its internal calculations.
370
* @param session The session.
371
* @param seq The RTP packet sequence number, in host byte order.
372
* @param ts The RTP packet timestamp, in host byte order.
373
* @param payload Size of the payload.
375
PJ_DECL(void) pjmedia_rtcp_rx_rtp( pjmedia_rtcp_session *session,
382
* Call this function everytime an RTP packet is received to let the RTCP
383
* session do its internal calculations.
385
* @param session The session.
386
* @param seq The RTP packet sequence number, in host byte order.
387
* @param ts The RTP packet timestamp, in host byte order.
388
* @param payload Size of the payload.
389
* @param discarded Flag to specify whether the packet is discarded.
391
PJ_DECL(void) pjmedia_rtcp_rx_rtp2(pjmedia_rtcp_session *session,
395
pj_bool_t discarded);
399
* Call this function everytime an RTP packet is sent to let the RTCP session
400
* do its internal calculations.
402
* @param session The session.
403
* @param ptsize The payload size of the RTP packet (ie packet minus
404
* RTP header) in bytes.
406
PJ_DECL(void) pjmedia_rtcp_tx_rtp( pjmedia_rtcp_session *session,
411
* Call this function when an RTCP packet is received from remote peer.
412
* This RTCP packet received from remote is used to calculate the end-to-
413
* end delay of the network.
415
* @param session RTCP session.
416
* @param rtcp_pkt The received RTCP packet.
417
* @param size Size of the incoming packet.
419
PJ_DECL(void) pjmedia_rtcp_rx_rtcp( pjmedia_rtcp_session *session,
420
const void *rtcp_pkt,
425
* Build a RTCP packet to be transmitted to remote RTP peer. This will
426
* create RTCP Sender Report (SR) or Receiver Report (RR) depending on
427
* whether the endpoint has been transmitting RTP since the last interval.
428
* Note that this function will reset the interval counters (such as
429
* the ones to calculate fraction lost) in the session.
431
* @param session The RTCP session.
432
* @param rtcp_pkt Upon return, it will contain pointer to the
433
* RTCP packet, which can be RTCP SR or RR.
434
* @param len Upon return, it will indicate the size of
437
PJ_DECL(void) pjmedia_rtcp_build_rtcp( pjmedia_rtcp_session *session,
438
void **rtcp_pkt, int *len);
442
* Call this function if RTCP XR needs to be enabled/disabled in the
445
* @param session The RTCP session.
446
* @param enable Enable/disable RTCP XR.
448
* @return PJ_SUCCESS on success.
450
PJ_DECL(pj_status_t) pjmedia_rtcp_enable_xr( pjmedia_rtcp_session *session,
461
#endif /* __PJMEDIA_RTCP_H__ */