1 /*****************************************************************************
2 * rtp.h: Real-time Transport Protocol
3 *****************************************************************************
4 * Copyright (C) 2009, 2014 VideoLAN
5 *
6 * Authors: Christophe Massiot <massiot@via.ecp.fr>
7 *
8 * Permission is hereby granted, free of charge, to any person obtaining
9 * a copy of this software and associated documentation files (the
10 * "Software"), to deal in the Software without restriction, including
11 * without limitation the rights to use, copy, modify, merge, publish,
12 * distribute, sublicense, and/or sell copies of the Software, and to
13 * permit persons to whom the Software is furnished to do so, subject
14 * to the following conditions:
15 *
16 * The above copyright notice and this permission notice shall be
17 * included in all copies or substantial portions of the Software.
18 *
19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
23 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *****************************************************************************/
27
28 /*
29 * Normative references:
30 * - IETF RFC 3550 Real-Time Protocol (July 2003)
31 */
32
33 #ifndef __BITSTREAM_IETF_RTP_H__
34 #define __BITSTREAM_IETF_RTP_H__
35
36 #include <stdlib.h>
37 #include <stdint.h> /* uint8_t, uint16_t, etc... */
38 #include <stdbool.h> /* bool */
39
40 #ifdef __cplusplus
41 extern "C"
42 {
43 #endif
44
45 #define RTP_HEADER_SIZE 12
46 #define RTP_EXTENSION_SIZE 4
47 #define RTP_TYPE_TS 33
48
49 /*
50 * Reminder : RTP header
51 0 1 2 3
52 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
53 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
54 |V=2|P|X| CC |M| PT | sequence number |
55 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
56 | timestamp |
57 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
58 | synchronization source (SSRC) identifier |
59 +=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+
60 | contributing source (CSRC) identifiers |
61 | .... |
62 +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
63 */
64
rtp_set_hdr(uint8_t * p_rtp)65 static inline void rtp_set_hdr(uint8_t *p_rtp)
66 {
67 p_rtp[0] = 0x80;
68 }
69
rtp_check_hdr(const uint8_t * p_rtp)70 static inline bool rtp_check_hdr(const uint8_t *p_rtp)
71 {
72 return (p_rtp[0] & 0xc0) == 0x80;
73 }
74
rtp_set_padding(uint8_t * p_rtp)75 static inline void rtp_set_padding(uint8_t *p_rtp)
76 {
77 p_rtp[0] |= 0x20;
78 }
79
rtp_check_padding(const uint8_t * p_rtp)80 static inline bool rtp_check_padding(const uint8_t *p_rtp)
81 {
82 return !!(p_rtp[0] & 0x20);
83 }
84
rtp_set_extension(uint8_t * p_rtp)85 static inline void rtp_set_extension(uint8_t *p_rtp)
86 {
87 p_rtp[0] |= 0x10;
88 }
89
rtp_check_extension(const uint8_t * p_rtp)90 static inline bool rtp_check_extension(const uint8_t *p_rtp)
91 {
92 return !!(p_rtp[0] & 0x10);
93 }
94
rtp_set_cc(uint8_t * p_rtp,uint8_t i_cc)95 static inline void rtp_set_cc(uint8_t *p_rtp, uint8_t i_cc)
96 {
97 p_rtp[0] &= 0xf0;
98 p_rtp[0] |= i_cc & 0xf;
99 }
100
rtp_get_cc(const uint8_t * p_rtp)101 static inline uint8_t rtp_get_cc(const uint8_t *p_rtp)
102 {
103 return p_rtp[0] & 0xf;
104 }
105
rtp_set_marker(uint8_t * p_rtp)106 static inline void rtp_set_marker(uint8_t *p_rtp)
107 {
108 p_rtp[1] |= 0x80;
109 }
110
rtp_clear_marker(uint8_t * p_rtp)111 static inline void rtp_clear_marker(uint8_t *p_rtp)
112 {
113 p_rtp[1] &= ~0x80;
114 }
115
rtp_check_marker(const uint8_t * p_rtp)116 static inline bool rtp_check_marker(const uint8_t *p_rtp)
117 {
118 return !!(p_rtp[1] & 0x80);
119 }
120
rtp_set_type(uint8_t * p_rtp,uint8_t i_type)121 static inline void rtp_set_type(uint8_t *p_rtp, uint8_t i_type)
122 {
123 p_rtp[1] = i_type & 0x7f;
124 }
125
rtp_get_type(const uint8_t * p_rtp)126 static inline uint8_t rtp_get_type(const uint8_t *p_rtp)
127 {
128 return p_rtp[1] & 0x7f;
129 }
130
rtp_set_seqnum(uint8_t * p_rtp,uint16_t i_seqnum)131 static inline void rtp_set_seqnum(uint8_t *p_rtp, uint16_t i_seqnum)
132 {
133 p_rtp[2] = i_seqnum >> 8;
134 p_rtp[3] = i_seqnum & 0xff;
135 }
136
rtp_get_seqnum(const uint8_t * p_rtp)137 static inline uint16_t rtp_get_seqnum(const uint8_t *p_rtp)
138 {
139 return (p_rtp[2] << 8) | p_rtp[3];
140 }
141
rtp_set_timestamp(uint8_t * p_rtp,uint32_t i_timestamp)142 static inline void rtp_set_timestamp(uint8_t *p_rtp, uint32_t i_timestamp)
143 {
144 p_rtp[4] = (i_timestamp >> 24) & 0xff;
145 p_rtp[5] = (i_timestamp >> 16) & 0xff;
146 p_rtp[6] = (i_timestamp >> 8) & 0xff;
147 p_rtp[7] = i_timestamp & 0xff;
148 }
149
rtp_get_timestamp(const uint8_t * p_rtp)150 static inline uint32_t rtp_get_timestamp(const uint8_t *p_rtp)
151 {
152 return (p_rtp[4] << 24) | (p_rtp[5] << 16) | (p_rtp[6] << 8) | p_rtp[7];
153 }
154
rtp_set_ssrc(uint8_t * p_rtp,const uint8_t pi_ssrc[4])155 static inline void rtp_set_ssrc(uint8_t *p_rtp, const uint8_t pi_ssrc[4])
156 {
157 p_rtp[8] = pi_ssrc[0];
158 p_rtp[9] = pi_ssrc[1];
159 p_rtp[10] = pi_ssrc[2];
160 p_rtp[11] = pi_ssrc[3];
161 }
162
rtp_get_ssrc(const uint8_t * p_rtp,uint8_t pi_ssrc[4])163 static inline void rtp_get_ssrc(const uint8_t *p_rtp, uint8_t pi_ssrc[4])
164 {
165 pi_ssrc[0] = p_rtp[8];
166 pi_ssrc[1] = p_rtp[9];
167 pi_ssrc[2] = p_rtp[10];
168 pi_ssrc[3] = p_rtp[11];
169 }
170
rtp_set_int_ssrc(uint8_t * p_rtp,uint32_t i_ssrc)171 static inline void rtp_set_int_ssrc(uint8_t *p_rtp, uint32_t i_ssrc)
172 {
173 p_rtp[8] = (i_ssrc >> 24) & 0xff;
174 p_rtp[9] = (i_ssrc >> 16) & 0xff;
175 p_rtp[10] = (i_ssrc >> 8) & 0xff;
176 p_rtp[11] = i_ssrc & 0xff;
177 }
178
rtp_get_int_ssrc(const uint8_t * p_rtp)179 static inline uint32_t rtp_get_int_ssrc(const uint8_t *p_rtp)
180 {
181 return (p_rtp[8] << 24) | (p_rtp[9] << 16) | (p_rtp[10] << 8) | p_rtp[11];
182 }
183
rtp_extension(uint8_t * p_rtp)184 static inline uint8_t *rtp_extension(uint8_t *p_rtp)
185 {
186 return p_rtp + RTP_HEADER_SIZE + 4 * rtp_get_cc(p_rtp);
187 }
188
rtpx_set_header(uint8_t * p_rtpx,uint16_t i_header)189 static inline void rtpx_set_header(uint8_t *p_rtpx, uint16_t i_header)
190 {
191 p_rtpx[0] = (i_header >> 8) & 0xff;
192 p_rtpx[1] = i_header & 0xff;
193 }
194
rtpx_get_header(const uint8_t * p_rtpx)195 static inline uint16_t rtpx_get_header(const uint8_t *p_rtpx)
196 {
197 return (p_rtpx[0] << 8) | p_rtpx[1];
198 }
199
rtpx_set_length(uint8_t * p_rtpx,uint16_t i_length)200 static inline void rtpx_set_length(uint8_t *p_rtpx, uint16_t i_length)
201 {
202 p_rtpx[2] = (i_length >> 8) & 0xff;
203 p_rtpx[3] = i_length & 0xff;
204 }
205
rtpx_get_length(const uint8_t * p_rtpx)206 static inline uint16_t rtpx_get_length(const uint8_t *p_rtpx)
207 {
208 return (p_rtpx[2] << 8) | p_rtpx[3];
209 }
210
rtp_payload(uint8_t * p_rtp)211 static inline uint8_t *rtp_payload(uint8_t *p_rtp)
212 {
213 unsigned int i_size = RTP_HEADER_SIZE;
214 i_size += 4 * rtp_get_cc(p_rtp);
215 if (rtp_check_extension(p_rtp))
216 i_size += 4 * (1 + rtpx_get_length(rtp_extension(p_rtp)));
217 return p_rtp + i_size;
218 }
219
rtp_payload_size(uint8_t * p_rtp,size_t i_rtp_size)220 static inline size_t rtp_payload_size(uint8_t *p_rtp, size_t i_rtp_size)
221 {
222 size_t i_payload_size = i_rtp_size - (rtp_payload(p_rtp) - p_rtp);
223 uint8_t i_padding_size = 0;
224 if (rtp_check_padding(p_rtp))
225 i_padding_size = p_rtp[i_rtp_size - 1];
226
227 return i_payload_size - i_padding_size;
228 }
229
230 #ifdef __cplusplus
231 }
232 #endif
233
234 #endif
235