1 /*
2  * (c) 1998-2018 by Columbia University; all rights reserved
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the University nor the names of its contributors
15  *    may be used to endorse or promote products derived from this software
16  *    without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */
30 
31 /*
32  * rtp.h  --  RTP header file (RFC 1890)
33  */
34 #include "sysdep.h"
35 
36 /*
37  * System endianness -- determined by autoconf.
38  */
39 #ifdef WORDS_BIGENDIAN
40 #define RTP_BIG_ENDIAN 1
41 #else
42 #define RTP_LITTLE_ENDIAN 1
43 #endif
44 
45 /*
46  * Current protocol version.
47  */
48 #define RTP_VERSION    2
49 
50 #define RTP_SEQ_MOD (1<<16)
51 #define RTP_MAX_SDES 255      /* maximum text length for SDES */
52 
53 typedef enum {
54     RTCP_SR   = 200,
55     RTCP_RR   = 201,
56     RTCP_SDES = 202,
57     RTCP_BYE  = 203,
58     RTCP_APP  = 204
59 } rtcp_type_t;
60 
61 typedef enum {
62     RTCP_SDES_END   = 0,
63     RTCP_SDES_CNAME = 1,
64     RTCP_SDES_NAME  = 2,
65     RTCP_SDES_EMAIL = 3,
66     RTCP_SDES_PHONE = 4,
67     RTCP_SDES_LOC   = 5,
68     RTCP_SDES_TOOL  = 6,
69     RTCP_SDES_NOTE  = 7,
70     RTCP_SDES_PRIV  = 8
71 } rtcp_sdes_type_t;
72 
73 /*
74  * RTP data header
75  */
76 typedef struct {
77 #if RTP_BIG_ENDIAN
78     unsigned int version:2;   /* protocol version */
79     unsigned int p:1;         /* padding flag */
80     unsigned int x:1;         /* header extension flag */
81     unsigned int cc:4;        /* CSRC count */
82     unsigned int m:1;         /* marker bit */
83     unsigned int pt:7;        /* payload type */
84 #elif RTP_LITTLE_ENDIAN
85     unsigned int cc:4;        /* CSRC count */
86     unsigned int x:1;         /* header extension flag */
87     unsigned int p:1;         /* padding flag */
88     unsigned int version:2;   /* protocol version */
89     unsigned int pt:7;        /* payload type */
90     unsigned int m:1;         /* marker bit */
91 #else
92 #error Define one of RTP_LITTLE_ENDIAN or RTP_BIG_ENDIAN
93 #endif
94     unsigned int seq:16;      /* sequence number */
95     uint32_t ts;               /* timestamp */
96     uint32_t ssrc;             /* synchronization source */
97     uint32_t csrc[1];          /* optional CSRC list */
98 } rtp_hdr_t;
99 
100 /* RTP Header Extension
101  */
102 typedef struct {
103     uint16_t ext_type;         /* defined by profile */
104     uint16_t len;              /* extension length in 32-bit word */
105 } rtp_hdr_ext_t;
106 
107 /*
108  * RTCP common header word
109  */
110 typedef struct {
111 #if RTP_BIG_ENDIAN
112     unsigned int version:2;   /* protocol version */
113     unsigned int p:1;         /* padding flag */
114     unsigned int count:5;     /* varies by packet type */
115 #elif RTP_LITTLE_ENDIAN
116     unsigned int count:5;     /* varies by packet type */
117     unsigned int p:1;         /* padding flag */
118     unsigned int version:2;   /* protocol version */
119 #else
120 #error Define one of RTP_LITTLE_ENDIAN or RTP_BIG_ENDIAN
121 #endif
122     unsigned int pt:8;        /* RTCP packet type */
123     unsigned int length:16;   /* pkt len in words, w/o this word */
124 } rtcp_common_t;
125 
126 /*
127  * Big-endian mask for version, padding bit and packet type pair
128  * XXX?
129  */
130 #define RTCP_VALID_MASK (0xc000 | 0x2000 | 0xfe)
131 #define RTCP_VALID_VALUE ((RTP_VERSION << 14) | RTCP_SR)
132 
133 #define	RTCP_FRACTION(x)	(((x) >> 24) & 0xFF)
134 #define	RTCP_LOST(x)		((((x) & 0xFFFFFF) < 0x800000) ? \
135 					((x) & 0xFFFFFF) \
136 					: (((x) & 0xFFFFFF) - 0x1000000))
137 
138 /*
139  * Reception report block
140  */
141 typedef struct {
142     uint32_t ssrc;             /* data source being reported */
143     uint32_t fraclost;	       /* fraction lost since last SR/RR and */
144 			       /* cumul. no. pkts lost (signed!) */
145     uint32_t last_seq;         /* extended last seq. no. received */
146     uint32_t jitter;           /* interarrival jitter */
147     uint32_t lsr;              /* last SR packet from this source */
148     uint32_t dlsr;             /* delay since last SR packet */
149 } rtcp_rr_t;
150 
151 /*
152  * SDES item
153  */
154 typedef struct {
155     uint8_t type;             /* type of item (rtcp_sdes_type_t) */
156     uint8_t length;           /* length of item (in octets) */
157     char data[1];             /* text, not null-terminated */
158 } rtcp_sdes_item_t;
159 
160 /*
161  * One RTCP packet
162  */
163 typedef struct {
164     rtcp_common_t common;     /* common header */
165     union {
166         /* sender report (SR) */
167         struct {
168             uint32_t ssrc;     /* sender generating this report */
169             uint32_t ntp_sec;  /* NTP timestamp */
170             uint32_t ntp_frac;
171             uint32_t rtp_ts;   /* RTP timestamp */
172             uint32_t psent;    /* packets sent */
173             uint32_t osent;    /* octets sent */
174             rtcp_rr_t rr[1];  /* variable-length list */
175         } sr;
176 
177         /* reception report (RR) */
178         struct {
179             uint32_t ssrc;     /* receiver generating this report */
180             rtcp_rr_t rr[1];  /* variable-length list */
181         } rr;
182 
183         /* source description (SDES) */
184         struct rtcp_sdes {
185             uint32_t src;      /* first SSRC/CSRC */
186             rtcp_sdes_item_t item[1]; /* list of SDES items */
187         } sdes;
188 
189         /* BYE */
190         struct {
191             uint32_t src[1];   /* list of sources */
192             /* can't express trailing text for reason */
193         } bye;
194     } r;
195 } rtcp_t;
196 
197 typedef struct rtcp_sdes rtcp_sdes_t;
198