uvgRTP
util.hh
Go to the documentation of this file.
1 #pragma once
3 
5 #ifdef _WIN32
6 #include <winsock2.h>
7 #include <windows.h>
8 #else
9 #include <sys/time.h>
10 #endif
11 
12 #include <algorithm>
13 #include <cstdint>
14 #include <cstddef>
15 #include <cstdio>
16 #include <cstring>
17 #include <string>
18 
19 #if defined(_MSC_VER)
20 typedef SSIZE_T ssize_t;
21 #endif
22 
23 /* https://stackoverflow.com/questions/1537964/visual-c-equivalent-of-gccs-attribute-packed */
24 #if defined(__MINGW32__) || defined(__MINGW64__) || defined(__linux__)
25 #define PACK(__Declaration__) __Declaration__ __attribute__((__packed__))
26 #else
27 #define PACK(__Declaration__) __pragma(pack(push, 1)) __Declaration__ __pragma(pack(pop))
28 #endif
29 
30 #ifdef _WIN32
31  typedef SOCKET socket_t;
32 #else
33  typedef int socket_t;
34 #endif
35 
36 const int MAX_PACKET = 65536;
37 const int MAX_PAYLOAD = 1446;
38 const int PKT_MAX_DELAY = 100;
39 
40 /* TODO: add ability for user to specify these? */
41 enum HEADER_SIZES {
42  ETH_HDR_SIZE = 14,
43  IPV4_HDR_SIZE = 20,
44  UDP_HDR_SIZE = 8,
45  RTP_HDR_SIZE = 12
46 };
48 
56 typedef enum RTP_ERROR {
57  RTP_MULTIPLE_PKTS_READY = 6,
58  RTP_PKT_READY = 5,
59  RTP_PKT_MODIFIED = 4,
60  RTP_PKT_NOT_HANDLED = 3,
61  RTP_INTERRUPTED = 2,
62  RTP_NOT_READY = 1,
63  RTP_OK = 0,
75  RTP_TIMEOUT = -12,
76  RTP_NOT_FOUND = -13,
78 } rtp_error_t;
79 
85 typedef enum RTP_FORMAT {
91 } rtp_format_t;
92 
99 typedef enum RTP_FLAGS {
101  RTP_NO_FLAGS = 0 << 0,
102 
104  RTP_SLICE = 1 << 0,
105 
107  RTP_COPY = 1 << 1
108 
109 } rtp_flags_t;
110 
119  RCE_NO_FLAGS = 0 << 0,
120 
121  /* Enable system call dispatcher (HEVC only) */
122  RCE_SYSTEM_CALL_DISPATCHER = 1 << 2,
123 
125  RCE_SRTP = 1 << 3,
126 
136 
146 
168 
182 
192 
195 
198 
206 
209 
212  RCE_RTCP = 1 << 12,
213 
216 
222 
223  RCE_LAST = 1 << 15,
224 };
225 
234  RCC_NO_FLAGS = 0,
235 
243 
251 
259 
263 
273 
274  RCC_LAST
275 };
276 
278 enum NOTIFY_REASON {
279 
280  /* Timer for the active frame has expired and it has been dropped */
281  NR_FRAME_DROPPED = 0,
282 };
283 
284 /* see src/util.hh for more information */
285 typedef struct rtp_ctx_conf {
286  int flags;
287  ssize_t ctx_values[RCC_LAST];
288 } rtp_ctx_conf_t;
289 
290 extern thread_local rtp_error_t rtp_errno;
291 
292 #define TIME_DIFF(s, e, u) ((ssize_t)std::chrono::duration_cast<std::chrono::u>(e - s).count())
293 
294 #define SET_NEXT_FIELD_32(a, p, v) do { *(uint32_t *)&(a)[p] = (v); p += 4; } while (0)
295 #define SET_FIELD_32(a, i, v) do { *(uint32_t *)&(a)[i] = (v); } while (0)
296 
297 static inline void hex_dump(uint8_t *buf, size_t len)
298 {
299  if (!buf)
300  return;
301 
302  for (size_t i = 0; i < len; i += 10) {
303  fprintf(stderr, "\t");
304  for (size_t k = i; k < i + 10; ++k) {
305  fprintf(stderr, "0x%02x ", buf[k]);
306  }
307  fprintf(stderr, "\n");
308  }
309 }
310 
311 static inline void set_bytes(int *ptr, int nbytes)
312 {
313  if (ptr)
314  *ptr = nbytes;
315 }
316 
317 static inline void *memdup(const void *src, size_t len)
318 {
319  uint8_t *dst = new uint8_t[len];
320  std::memcpy(dst, src, len);
321 
322  return dst;
323 }
324 
325 static inline std::string generate_string(size_t length)
326 {
327  auto randchar = []() -> char
328  {
329  const char charset[] =
330  "0123456789"
331  "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
332  "abcdefghijklmnopqrstuvwxyz";
333  const size_t max_index = (sizeof(charset) - 1);
334  return charset[ rand() % max_index ];
335  };
336 
337  std::string str(length, 0);
338  std::generate_n(str.begin(), length, randchar);
339  return str;
340 }
RTP_CTX_ENABLE_FLAGS
RTP context enable flags.
Definition: util.hh:118
@ RCE_SRTP_INPLACE_ENCRYPTION
Definition: util.hh:191
@ RCE_SRTP_KMNGMNT_ZRTP
Definition: util.hh:135
@ RCE_SRTP_KMNGMNT_USER
Definition: util.hh:145
@ RCE_SRTP_NULL_CIPHER
Definition: util.hh:197
@ RCE_FRAGMENT_GENERIC
Definition: util.hh:181
@ RCE_NO_H26X_INTRA_DELAY
Definition: util.hh:167
@ RCE_H26X_PREPEND_SC
Definition: util.hh:215
@ RCE_SRTP_AUTHENTICATE_RTP
Definition: util.hh:205
@ RCE_HOLEPUNCH_KEEPALIVE
Definition: util.hh:221
@ RCE_RTCP
Definition: util.hh:212
@ RCE_NO_SYSTEM_CALL_CLUSTERING
Definition: util.hh:194
@ RCE_SRTP_REPLAY_PROTECTION
Definition: util.hh:208
@ RCE_SRTP
Definition: util.hh:125
RTP_FORMAT
These flags are given to uvgrtp::session::create_stream()
Definition: util.hh:85
@ RTP_FORMAT_H266
H.266/VVC.
Definition: util.hh:89
@ RTP_FORMAT_OPUS
Opus.
Definition: util.hh:90
@ RTP_FORMAT_H265
H.265/HEVC.
Definition: util.hh:88
@ RTP_FORMAT_GENERIC
Generic format.
Definition: util.hh:86
@ RTP_FORMAT_H264
H.264/AVC.
Definition: util.hh:87
RTP_ERROR
RTP error codes.
Definition: util.hh:56
@ RTP_BIND_ERROR
Failed to bind to interface.
Definition: util.hh:66
@ RTP_INITIALIZED
Object already initialized.
Definition: util.hh:71
@ RTP_NOT_FOUND
Object not found.
Definition: util.hh:76
@ RTP_OK
Success.
Definition: util.hh:63
@ RTP_NOT_INITIALIZED
Object has not been initialized.
Definition: util.hh:72
@ RTP_INVALID_VALUE
Invalid value.
Definition: util.hh:67
@ RTP_SEND_ERROR
System call send(2) or one of its derivatives failed.
Definition: util.hh:68
@ RTP_AUTH_TAG_MISMATCH
Authentication tag does not match the RTP packet contents.
Definition: util.hh:77
@ RTP_NOT_SUPPORTED
Method/version/extension not supported.
Definition: util.hh:73
@ RTP_SSRC_COLLISION
SSRC collision detected.
Definition: util.hh:70
@ RTP_RECV_ERROR
System call recv(2) or one of its derivatives failed.
Definition: util.hh:74
@ RTP_SOCKET_ERROR
Failed to create socket.
Definition: util.hh:65
@ RTP_MEMORY_ERROR
Memory allocation failed.
Definition: util.hh:69
@ RTP_TIMEOUT
Operation timed out.
Definition: util.hh:75
@ RTP_GENERIC_ERROR
Generic error condition.
Definition: util.hh:64
RTP_CTX_CONFIGURATION_FLAGS
RTP context configuration flags.
Definition: util.hh:233
@ RCC_PKT_MAX_DELAY
Definition: util.hh:258
@ RCC_MTU_SIZE
Definition: util.hh:272
@ RCC_UDP_RCV_BUF_SIZE
Definition: util.hh:242
@ RCC_DYN_PAYLOAD_TYPE
Definition: util.hh:262
@ RCC_UDP_SND_BUF_SIZE
Definition: util.hh:250
RTP_FLAGS
These flags are given to uvgrtp::media_stream::push_frame() and they can be OR'ed together.
Definition: util.hh:99
@ RTP_NO_FLAGS
Definition: util.hh:101
@ RTP_COPY
Definition: util.hh:107
@ RTP_SLICE
Definition: util.hh:104