UltraGrid
UltraGrid Low-Latency High-Resolution Transmission System
Loading...
Searching...
No Matches
rtp.h
Go to the documentation of this file.
1/*
2 * FILE: rtp.h
3 * AUTHOR: Colin Perkins <c.perkins@cs.ucl.ac.uk>
4 *
5 * Copyright (c) 1998-2000 University College London
6 * Copyright (c) 2005-2021 CESNET, z. s. p. o.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, is permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by the Computer Science
20 * Department at University College London.
21 * 4. Neither the name of the University nor of the Department may be used
22 * to endorse or promote products derived from this software without
23 * specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#ifndef __RTP_H__
38#define __RTP_H__
39
40#include "config_win32.h"
41#include "config_unix.h"
42
43#ifdef __cplusplus
44#include <cstddef> // offsetof
45#include <cstdint>
46#else
47#include <stdbool.h>
48#include <stddef.h> // offsetof
49#include <stdint.h>
50#endif
51
52#include "tv.h"
53#include "video_codec.h" // MAX_PADDING
54
55#ifdef __cplusplus
56extern "C" {
57#endif
58
59#define RTP_VERSION 2
60#define RTP_MAX_MTU 9000
61#define RTP_MAX_PACKET_LEN (RTP_MAX_MTU+RTP_PACKET_HEADER_SIZE+MAX_PADDING)
62
63enum {
68
69};
70
71#if !defined __BYTE_ORDER__ || __BYTE_ORDER__ != __ORDER_LITTLE_ENDIAN__ && \
72 __BYTE_ORDER__ != __ORDER_BIG_ENDIAN__
73#error RTP library requires __BYTE_ORDER__ to be either __ORDER_LITTLE_ENDIAN__\
74 or __ORDER_BIG_ENDIAN__
75#endif
76
77struct rtp;
78
79/* XXX gtkdoc doesn't seem to be able to handle functions that return
80 * struct *'s. */
81typedef struct rtp *rtp_t;
82
83/*
84 * TODO: this usage of bit-array is incorrect and the attribute "packed"
85 * doesn't ensure proper behaviour. With GCC-incompatible mode of Intel
86 * C Compiler this is broken.
87 */
88typedef struct __attribute__((packed)) {
89 /* The following are pointers to the data in the packet as */
90 /* it came off the wire. The packet it read in such that the */
91 /* header maps onto the latter part of this struct, and the */
92 /* fields in this first part of the struct point into it. The */
93 /* entire packet can be freed by freeing this struct, without */
94 /* having to free the csrc, data and extn blocks separately. */
95 /* WARNING: Don't change the size of the first portion of the */
96 /* struct without changing RTP_PACKET_HEADER_SIZE to match. */
97 uint32_t *csrc;
98 char *data;
99 int data_len;
100 unsigned char *extn;
101 uint16_t extn_len; /* Size of the extension in 32 bit words minus one */
102 uint16_t extn_type; /* Extension type field in the RTP packet header */
103 /* The following map directly onto the RTP packet header... */
104#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
105 unsigned short v:2; /* packet type */
106 unsigned short p:1; /* padding flag */
107 unsigned short x:1; /* header extension flag */
108 unsigned short cc:4; /* CSRC count */
109 unsigned short m:1; /* marker bit */
110 unsigned short pt:7; /* payload type */
111#else
112 unsigned short cc:4; /* CSRC count */
113 unsigned short x:1; /* header extension flag */
114 unsigned short p:1; /* padding flag */
115 unsigned short v:2; /* packet type */
116 unsigned short pt:7; /* payload type */
117 unsigned short m:1; /* marker bit */
118#endif
119 uint16_t seq; /* sequence number */
120 uint32_t ts; /* timestamp */
121 uint32_t ssrc; /* synchronization source */
122 /* The csrc list, header extension and data follow, but can't */
123 /* be represented in the struct. */
124 uint32_t send_ts;
125 uint32_t rtt;
127
128#define RTP_PACKET_HEADER_SIZE ((int) (offsetof(rtp_packet, extn_type) - offsetof(rtp_packet, csrc) + sizeof ((rtp_packet *) 0)->extn_type))
129
130typedef struct {
131 uint32_t ssrc;
132 uint32_t ntp_sec;
133 uint32_t ntp_frac;
134 uint32_t rtp_ts;
137} rtcp_sr;
138
139typedef struct {
140 uint32_t ssrc; /* The ssrc to which this RR pertains */
141#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
142 uint32_t fract_lost:8;
143 uint32_t total_lost:24;
144#else
145 uint32_t total_lost:24;
146 uint32_t fract_lost:8;
147#endif
148 uint32_t last_seq;
149 uint32_t jitter;
150 uint32_t lsr;
151 uint32_t dlsr;
152} rtcp_rr;
153
154typedef struct {
155 uint32_t ts;
156 uint32_t tdelay;
157 uint32_t x_recv;
158 uint32_t p;
159} rtcp_rx;
160
161typedef struct {
162#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
163 unsigned short version:2; /* RTP version */
164 unsigned short p:1; /* padding flag */
165 unsigned short subtype:5; /* application dependent */
166#else
167 unsigned short subtype:5; /* application dependent */
168 unsigned short p:1; /* padding flag */
169 unsigned short version:2; /* RTP version */
170#endif
171 unsigned short pt:8; /* packet type */
172 uint16_t length; /* packet length */
173 uint32_t ssrc;
174 char name[4]; /* four ASCII characters */
175 char data[1]; /* variable length field */
176} rtcp_app;
177
178/* rtp_event type values. */
179typedef enum {
184 RX_TFRC_RX, /* received TFRC extended report */
186 RX_BYE, /* Source is leaving the session, database entry is still valid */
188 SOURCE_DELETED, /* Source has been removed from the database */
189 RX_RR_EMPTY, /* We've received an empty reception report block */
190 RX_RTCP_START, /* Processing a compound RTCP packet about to start. The SSRC is not valid in this event. */
191 RX_RTCP_FINISH, /* Processing a compound RTCP packet finished. The SSRC is not valid in this event. */
196
197typedef struct {
198 uint32_t ssrc;
200 void *data;
201} rtp_event;
202
203/* Callback types */
204typedef void (*rtp_callback)(struct rtp *session, rtp_event *e);
205typedef rtcp_app* (*rtcp_app_callback)(struct rtp *session, uint32_t rtp_ts, int max_size);
206
207/* SDES packet types... */
219
220typedef struct {
221 uint8_t type; /* type of SDES item */
222 uint8_t length; /* length of SDES item (in bytes) */
223 char data[1]; /* text, not zero-terminated */
225
226/* RTP options */
227typedef enum {
231 RTP_OPT_REUSE_PACKET_BUFS = 4, /* Each data packet is written into the same buffer, */
232 /* rather than malloc()ing a new buffer each time. */
234 RTP_OPT_RECORD_SOURCE = 6, /* Record network source (sockaddr_storage) at the */
235 /* end of the packet */
236 RTP_OPT_SEND_BACK = 7, // Send to a receiver that is sending to us. Sets also
237 // RTP_POT_RECORD_SOURCE
238} rtp_option;
239
240struct socket_udp_local;
241
242/* API */
243rtp_t rtp_init(const char *addr,
244 uint16_t rx_port, uint16_t tx_port,
245 int ttl, double rtcp_bw,
246 int tfrc_on,
248 uint8_t *userdata,
249 int force_ip_version, bool multithreaded);
250rtp_t rtp_init_if(const char *addr, const char *iface,
251 uint16_t rx_port, uint16_t tx_port,
252 int ttl, double rtcp_bw,
253 int tfrc_on,
255 uint8_t *userdata,
256 int force_ip_version, bool multithreaded);
257rtp_t rtp_init_with_udp_socket(struct socket_udp_local *l, struct sockaddr *sa, socklen_t len, rtp_callback callback);
258
259void rtp_send_bye(struct rtp *session);
260void rtp_done(struct rtp *session);
261
262bool rtp_set_option(struct rtp *session, rtp_option optname, int optval);
263bool rtp_get_option(struct rtp *session, rtp_option optname, int *optval);
264
265bool rtp_recv(struct rtp *session,
266 struct timeval *timeout, uint32_t curr_rtp_ts) __attribute__((deprecated));
267bool rtp_recv_r(struct rtp *session,
268 struct timeval *timeout, uint32_t curr_rtp_ts);
269bool rtcp_recv_r(struct rtp *session,
270 struct timeval *timeout, uint32_t curr_rtp_ts);
271int rtp_recv_poll_r(struct rtp **sessions,
272 struct timeval *timeout, uint32_t curr_rtp_ts);
273int rtp_send_raw_rtp_data(struct rtp *session, char *buffer, int buffer_len);
274
275int rtp_send_data(struct rtp *session,
276 uint32_t rtp_ts, char pt, int m,
277 int cc, const uint32_t *csrc,
278 char *data, int data_len,
279 char *extn, uint16_t extn_len, uint16_t extn_type);
280int rtp_send_data_hdr(struct rtp *session,
281 uint32_t rtp_ts, char pt, int m,
282 int cc, const uint32_t *csrc,
283 char *phdr, int phdr_len,
284 char *data, int data_len,
285 char *extn, uint16_t extn_len, uint16_t extn_type);
286void rtp_send_ctrl(struct rtp *session, uint32_t rtp_ts,
287 rtcp_app_callback appcallback, time_ns_t curr_time);
288void rtp_update(struct rtp *session, time_ns_t curr_time);
289
290uint32_t rtp_my_ssrc(struct rtp *session);
291bool rtp_add_csrc(struct rtp *session, uint32_t csrc);
292bool rtp_del_csrc(struct rtp *session, uint32_t csrc);
293
294bool rtp_set_sdes(struct rtp *session, uint32_t ssrc,
296 const char *value, int length);
297const char *rtp_get_sdes(struct rtp *session, uint32_t ssrc, rtcp_sdes_type type);
298
299const rtcp_sr *rtp_get_sr(struct rtp *session, uint32_t ssrc);
300const rtcp_rr *rtp_get_rr(struct rtp *session, uint32_t reporter, uint32_t reportee);
301
302bool rtp_set_encryption_key(struct rtp *session, const char *passphrase);
303bool rtp_set_my_ssrc(struct rtp *session, uint32_t ssrc);
304
305uint8_t *rtp_get_userdata(struct rtp *session);
306void rtp_set_recv_iov(struct rtp *session, struct msghdr *m);
307
308int rtp_get_recv_buf(struct rtp *session);
309bool rtp_set_recv_buf(struct rtp *session, int bufsize);
310bool rtp_set_send_buf(struct rtp *session, int bufsize);
311
312void rtp_flush_recv_buf(struct rtp *session);
313int rtp_get_udp_rx_port(struct rtp *session);
314uint64_t rtp_get_bytes_sent(struct rtp *session);
315int rtp_compute_fract_lost(struct rtp *session, uint32_t ssrc);
316bool rtp_is_ipv6(struct rtp *session);
317bool rtp_has_receiver(struct rtp *session);
318
319/*
320 * Async API - MSW specific
321 *
322 * Using async API hugely improves performance.
323 * Usage is simple - prior to sending a bulk of packets (eg. video frame), rtp_async_start()
324 * is started. Then, all packets are sent as usual, exept that neither data nor headers should
325 * be altered up to rtp_async_wait() call, which waits upon completition of async operations
326 * started after rtp_async_start(). Caller is responsible that rtp_send_data_hdr() is not called
327 * more than nr_packet times.
328 */
329void rtp_async_start(struct rtp *session, int nr_packets);
330void rtp_async_wait(struct rtp *session);
331
332struct socket_udp_local *rtp_get_udp_local_socket(struct rtp *session);
333
334#ifdef __cplusplus
335}
336#endif
337
338#endif /* __RTP_H__ */
int void size_t * len
bool rtp_add_csrc(struct rtp *session, uint32_t csrc)
Definition rtp.c:2478
rtp_t rtp_init_if(const char *addr, const char *iface, uint16_t rx_port, uint16_t tx_port, int ttl, double rtcp_bw, int tfrc_on, rtp_callback callback, uint8_t *userdata, int force_ip_version, bool multithreaded)
Definition rtp.c:1034
bool rtp_set_send_buf(struct rtp *session, int bufsize)
Definition rtp.c:3954
rtcp_sdes_type
Definition rtp.h:208
@ RTCP_SDES_NOTE
Definition rtp.h:216
@ RTCP_SDES_CNAME
Definition rtp.h:210
@ RTCP_SDES_PHONE
Definition rtp.h:213
@ RTCP_SDES_TOOL
Definition rtp.h:215
@ RTCP_SDES_LOC
Definition rtp.h:214
@ RTCP_SDES_EMAIL
Definition rtp.h:212
@ RTCP_SDES_END
Definition rtp.h:209
@ RTCP_SDES_NAME
Definition rtp.h:211
@ RTCP_SDES_PRIV
Definition rtp.h:217
rtp_t rtp_init(const char *addr, uint16_t rx_port, uint16_t tx_port, int ttl, double rtcp_bw, int tfrc_on, rtp_callback callback, uint8_t *userdata, int force_ip_version, bool multithreaded)
Definition rtp.c:999
void rtp_flush_recv_buf(struct rtp *session)
Definition rtp.c:3964
bool rtp_set_recv_buf(struct rtp *session, int bufsize)
Definition rtp.c:3938
bool rtp_set_option(struct rtp *session, rtp_option optname, int optval)
Definition rtp.c:1308
const rtcp_rr * rtp_get_rr(struct rtp *session, uint32_t reporter, uint32_t reportee)
Definition rtp.c:2707
int rtp_compute_fract_lost(struct rtp *session, uint32_t ssrc)
Definition rtp.c:3974
const rtcp_sr * rtp_get_sr(struct rtp *session, uint32_t ssrc)
Definition rtp.c:2679
rtp_t rtp_init_with_udp_socket(struct socket_udp_local *l, struct sockaddr *sa, socklen_t len, rtp_callback callback)
Definition rtp.c:1175
int rtp_send_raw_rtp_data(struct rtp *session, char *buffer, int buffer_len)
Definition rtp.c:1518
void rtp_update(struct rtp *session, time_ns_t curr_time)
Definition rtp.c:3456
int rtp_send_data_hdr(struct rtp *session, uint32_t rtp_ts, char pt, int m, int cc, const uint32_t *csrc, char *phdr, int phdr_len, char *data, int data_len, char *extn, uint16_t extn_len, uint16_t extn_type)
Definition rtp.c:2752
bool rtp_get_option(struct rtp *session, rtp_option optname, int *optval)
Definition rtp.c:1352
uint32_t rtp_my_ssrc(struct rtp *session)
Definition rtp.c:1401
void rtp_send_ctrl(struct rtp *session, uint32_t rtp_ts, rtcp_app_callback appcallback, time_ns_t curr_time)
Definition rtp.c:3409
bool rtp_set_sdes(struct rtp *session, uint32_t ssrc, rtcp_sdes_type type, const char *value, int length)
Definition rtp.c:2546
bool rtp_has_receiver(struct rtp *session)
Definition rtp.c:4016
uint8_t * rtp_get_userdata(struct rtp *session)
Definition rtp.c:1386
int rtp_get_recv_buf(struct rtp *session)
Definition rtp.c:3944
int rtp_get_udp_rx_port(struct rtp *session)
Definition rtp.c:4042
bool rtp_recv_r(struct rtp *session, struct timeval *timeout, uint32_t curr_rtp_ts)
Receive RTP packets and dispatch them.
Definition rtp.c:2338
bool rtp_recv(struct rtp *session, struct timeval *timeout, uint32_t curr_rtp_ts) __attribute__((deprecated))
Definition rtp.c:2297
rtp_option
Definition rtp.h:227
@ RTP_OPT_PEEK
Definition rtp.h:233
@ RTP_OPT_REUSE_PACKET_BUFS
Definition rtp.h:231
@ RTP_OPT_FILTER_MY_PACKETS
Definition rtp.h:230
@ RTP_OPT_PROMISC
Definition rtp.h:228
@ RTP_OPT_WEAK_VALIDATION
Definition rtp.h:229
@ RTP_OPT_SEND_BACK
Definition rtp.h:236
@ RTP_OPT_RECORD_SOURCE
Definition rtp.h:234
rtp_packet
Definition rtp.h:126
int rtp_recv_poll_r(struct rtp **sessions, struct timeval *timeout, uint32_t curr_rtp_ts)
Definition rtp.c:2434
void rtp_set_recv_iov(struct rtp *session, struct msghdr *m)
Definition rtp.c:1513
uint64_t rtp_get_bytes_sent(struct rtp *session)
Definition rtp.c:3969
bool rtp_is_ipv6(struct rtp *session)
Definition rtp.c:4022
void(* rtp_callback)(struct rtp *session, rtp_event *e)
Definition rtp.h:204
rtp_event_type
Definition rtp.h:179
@ RX_RTP_IOV
Definition rtp.h:181
@ RX_RTP
Definition rtp.h:180
@ RR_TIMEOUT
Definition rtp.h:192
@ RX_SDES
Definition rtp.h:185
@ RX_APP
Definition rtp.h:193
@ PEEK_RTP
Definition rtp.h:194
@ RX_RTCP_START
Definition rtp.h:190
@ RX_TFRC_RX
Definition rtp.h:184
@ RX_SR
Definition rtp.h:182
@ SOURCE_CREATED
Definition rtp.h:187
@ RX_RTCP_FINISH
Definition rtp.h:191
@ SOURCE_DELETED
Definition rtp.h:188
@ RX_RR_EMPTY
Definition rtp.h:189
@ RX_BYE
Definition rtp.h:186
@ RX_RR
Definition rtp.h:183
bool rtp_del_csrc(struct rtp *session, uint32_t csrc)
Definition rtp.c:2510
bool rtp_set_encryption_key(struct rtp *session, const char *passphrase)
Definition rtp.c:3742
struct rtp * rtp_t
Definition rtp.h:81
struct __attribute__((packed))
Definition rtp.h:88
struct socket_udp_local * rtp_get_udp_local_socket(struct rtp *session)
Definition rtp.c:4037
int rtp_send_data(struct rtp *session, uint32_t rtp_ts, char pt, int m, int cc, const uint32_t *csrc, char *data, int data_len, char *extn, uint16_t extn_len, uint16_t extn_type)
Definition rtp.c:2742
void rtp_async_wait(struct rtp *session)
Definition rtp.c:4032
bool rtcp_recv_r(struct rtp *session, struct timeval *timeout, uint32_t curr_rtp_ts)
Definition rtp.c:2398
void rtp_async_start(struct rtp *session, int nr_packets)
Definition rtp.c:4027
bool rtp_set_my_ssrc(struct rtp *session, uint32_t ssrc)
Definition rtp.c:1278
const char * rtp_get_sdes(struct rtp *session, uint32_t ssrc, rtcp_sdes_type type)
Definition rtp.c:2631
void rtp_send_bye(struct rtp *session)
Definition rtp.c:3598
void rtp_done(struct rtp *session)
Definition rtp.c:3676
@ IPV6_HDR_LEN
Definition rtp.h:65
@ IPV4_HDR_LEN
Definition rtp.h:64
@ RTP_HDR_LEN
Definition rtp.h:67
@ UDP_HDR_LEN
Definition rtp.h:66
rtcp_app *(* rtcp_app_callback)(struct rtp *session, uint32_t rtp_ts, int max_size)
Definition rtp.h:205
Option::OptType type
Definition settings.cpp:194
const char * name
Definition settings.cpp:193
Option::Callback::fcn_type callback
Definition settings.cpp:251
uint8_t data[65520]
Definition ihdtv.h:74
unsigned short version
Definition rtp.h:163
unsigned short pt
Definition rtp.h:171
uint32_t ssrc
Definition rtp.h:173
unsigned short subtype
Definition rtp.h:165
unsigned short p
Definition rtp.h:164
uint16_t length
Definition rtp.h:172
Definition rtp.h:139
uint32_t lsr
Definition rtp.h:150
uint32_t dlsr
Definition rtp.h:151
uint32_t total_lost
Definition rtp.h:143
uint32_t jitter
Definition rtp.h:149
uint32_t fract_lost
Definition rtp.h:142
uint32_t ssrc
Definition rtp.h:140
uint32_t last_seq
Definition rtp.h:148
Definition rtp.h:154
uint32_t p
Definition rtp.h:158
uint32_t tdelay
Definition rtp.h:156
uint32_t ts
Definition rtp.h:155
uint32_t x_recv
Definition rtp.h:157
uint8_t type
Definition rtp.h:221
uint8_t length
Definition rtp.h:222
Definition rtp.h:130
uint32_t sender_pcount
Definition rtp.h:135
uint32_t ntp_frac
Definition rtp.h:133
uint32_t sender_bcount
Definition rtp.h:136
uint32_t ntp_sec
Definition rtp.h:132
uint32_t ssrc
Definition rtp.h:131
uint32_t rtp_ts
Definition rtp.h:134
rtp_event_type type
Definition rtp.h:199
uint32_t ssrc
Definition rtp.h:198
void * data
Definition rtp.h:200
Definition rtp.c:274
uint32_t ts
Definition tfrc.c:101
uint32_t seq
Definition tfrc.c:100
long long time_ns_t
Definition tv.h:81