1 /**
2  * @file rtcp.h  Internal interface to RTCP
3  *
4  * Copyright (C) 2010 Creytiv.com
5  */
6 
7 
8 /** RTCP protocol values */
9 enum {
10 	RTCP_HDR_SIZE  =   4,  /**< Size of common RTCP header   */
11 	RTCP_SRC_SIZE  =   4,  /**< Size of Source field         */
12 	RTCP_SR_SIZE   =  20,  /**< Size of Sender Information   */
13 	RTCP_RR_SIZE   =  24,  /**< Size of Report Block         */
14 	RTCP_APP_SIZE  =   8,  /**< Size of Application packet   */
15 	RTCP_FIR_SIZE  =   4,  /**< Size of FIR packet           */
16 	RTCP_NACK_SIZE =   8,  /**< Size of NACK packet          */
17 	RTCP_FB_SIZE   =   8,  /**< Size of Feedback packets     */
18 	RTCP_MAX_SDES  = 255,  /**< Maximum text length for SDES */
19 	RTCP_HEADROOM  =   4,  /**< Headroom in RTCP packets     */
20 };
21 
22 /** NTP Time */
23 struct ntp_time {
24 	uint32_t hi;  /**< Seconds since 0h UTC on 1 January 1900 */
25 	uint32_t lo;  /**< Fraction of seconds                    */
26 };
27 
28 struct hash;
29 
30 /** Per-source state information */
31 struct rtp_source {
32 	struct sa rtp_peer;       /**< IP-address of the RTP source        */
33 	uint16_t max_seq;         /**< Highest seq. number seen            */
34 	uint32_t cycles;          /**< Shifted count of seq. number cycles */
35 	uint32_t base_seq;        /**< Base seq number                     */
36 	uint32_t bad_seq;         /**< Last 'bad' seq number + 1           */
37 	uint32_t probation;       /**< Sequ. packets till source is valid  */
38 	uint32_t received;        /**< Packets received                    */
39 	uint32_t expected_prior;  /**< Packet expected at last interval    */
40 	uint32_t received_prior;  /**< Packet received at last interval    */
41 	int transit;              /**< Relative trans time for prev pkt    */
42 	uint32_t jitter;          /**< Estimated jitter                    */
43 	size_t rtp_rx_bytes;      /**< Number of RTP bytes received        */
44 	uint64_t sr_recv;         /**< When the last SR was received       */
45 	struct ntp_time last_sr;  /**< NTP Timestamp from last SR received */
46 	uint32_t rtp_ts;          /**< RTP timestamp                       */
47 	uint32_t psent;           /**< RTP packets sent                    */
48 	uint32_t osent;           /**< RTP octets sent                     */
49 };
50 
51 /** RTP Member */
52 struct rtp_member {
53 	struct le le;             /**< Hash-table element                  */
54 	struct rtp_source *s;     /**< RTP source state                    */
55 	uint32_t src;             /**< Source - used for hash-table lookup */
56 	int cum_lost;             /**< Cumulative number of packets lost   */
57 	uint32_t jit;             /**< Jitter in [us]                      */
58 	uint32_t rtt;             /**< Round-trip time in [us]             */
59 };
60 
61 
62 /* Member */
63 struct rtp_member *member_add(struct hash *ht, uint32_t src);
64 struct rtp_member *member_find(struct hash *ht, uint32_t src);
65 
66 /* Source */
67 void source_init_seq(struct rtp_source *s, uint16_t seq);
68 int  source_update_seq(struct rtp_source *s, uint16_t seq);
69 void source_calc_jitter(struct rtp_source *s, uint32_t rtp_ts,
70 			uint32_t arrival);
71 int  source_calc_lost(const struct rtp_source *s);
72 uint8_t source_calc_fraction_lost(struct rtp_source *s);
73 
74 /* RR (Reception report) */
75 int rtcp_rr_alloc(struct rtcp_rr **rrp, size_t count);
76 int rtcp_rr_encode(struct mbuf *mb, const struct rtcp_rr *rr);
77 int rtcp_rr_decode(struct mbuf *mb, struct rtcp_rr *rr);
78 
79 /* SDES (Source Description) */
80 int rtcp_sdes_decode(struct mbuf *mb, struct rtcp_sdes *sdes);
81 
82 /* RTCP Feedback */
83 int rtcp_rtpfb_gnack_encode(struct mbuf *mb, uint16_t pid, uint16_t blp);
84 int rtcp_psfb_sli_encode(struct mbuf *mb, uint16_t first, uint16_t number,
85 			 uint8_t picid);
86 int rtcp_rtpfb_decode(struct mbuf *mb, struct rtcp_msg *msg);
87 int rtcp_psfb_decode(struct mbuf *mb, struct rtcp_msg *msg);
88 
89 /** NTP Time */
90 struct timeval;
91 void unix2ntp(struct ntp_time *ntp, const struct timeval *tv);
92 void ntp2unix(struct timeval *tv, const struct ntp_time *ntp);
93 int  ntp_time_get(struct ntp_time *ntp);
94 uint32_t ntp_compact(const struct ntp_time *ntp);
95 uint64_t ntp_compact2us(uint32_t ntpc);
96 
97 /* RTP Socket */
98 struct rtcp_sess *rtp_rtcp_sess(const struct rtp_sock *rs);
99 
100 /* RTCP message */
101 typedef int (rtcp_encode_h)(struct mbuf *mb, void *arg);
102 
103 int rtcp_hdr_encode(struct mbuf *mb, uint8_t count, enum rtcp_type type,
104 		    uint16_t length);
105 int rtcp_hdr_decode(struct mbuf *mb, struct rtcp_hdr *hdr);
106 int rtcp_vencode(struct mbuf *mb, enum rtcp_type type, uint32_t count,
107 		 va_list ap);
108 
109 /* RTCP Session */
110 struct rtcp_sess;
111 
112 int  rtcp_sess_alloc(struct rtcp_sess **sessp, struct rtp_sock *rs);
113 int  rtcp_enable(struct rtcp_sess *sess, bool enabled, const char *cname);
114 int  rtcp_send(struct rtp_sock *rs, struct mbuf *mb);
115 void rtcp_handler(struct rtcp_sess *sess, struct rtcp_msg *msg);
116 void rtcp_sess_tx_rtp(struct rtcp_sess *sess, uint32_t ts,
117 		      size_t payload_size);
118 void rtcp_sess_rx_rtp(struct rtcp_sess *sess, uint16_t seq, uint32_t ts,
119 		      uint32_t src, size_t payload_size,
120 		      const struct sa *peer);
121