| 1 |
/* librist. Copyright © 2019-2020 SipRadius LLC. All right reserved. |
| 2 |
* Author: Kuldeep Singh Dhaka <[email protected]> |
| 3 |
* Author: Sergio Ammirata, Ph.D. <[email protected]> |
| 4 |
* |
| 5 |
* SPDX-License-Identifier: BSD-2-Clause |
| 6 |
*/ |
| 7 |
|
| 8 |
#ifndef RIST_RIST_PRIVATE_H |
| 9 |
#define RIST_RIST_PRIVATE_H |
| 10 |
|
| 11 |
#include "common/attributes.h" |
| 12 |
|
| 13 |
#include <stdint.h> |
| 14 |
#include <stdlib.h> |
| 15 |
#include <sys/types.h> |
| 16 |
#include <string.h> |
| 17 |
#include <stdlib.h> |
| 18 |
#include <stdbool.h> |
| 19 |
#include "stdio-shim.h" |
| 20 |
#include <fcntl.h> |
| 21 |
#include <inttypes.h> |
| 22 |
#include <stddef.h> |
| 23 |
#include "time-shim.h" |
| 24 |
#include "pthread-shim.h" |
| 25 |
#include "socket-shim.h" |
| 26 |
#include "libevsocket.h" |
| 27 |
#include "librist.h" |
| 28 |
#include "librist/transport.h" |
| 29 |
#include "udpsocket.h" |
| 30 |
#include "crypto/psk.h" |
| 31 |
#include <errno.h> |
| 32 |
#include <stdatomic.h> |
| 33 |
#include "librist/logging.h" |
| 34 |
#include "proto/gre.h" |
| 35 |
#include "proto/adv.h" |
| 36 |
|
| 37 |
struct cJSON; |
| 38 |
|
| 39 |
#undef RIST_DEPRECATED |
| 40 |
|
| 41 |
#define UINT16_SIZE (UINT16_MAX + 1) |
| 42 |
|
| 43 |
/* Forward sequence-number gap. short_seq (Simple/Main) flows wrap at 16 bits; |
| 44 |
* full 32-bit (Advanced) flows use the true difference so a genuine >64k gap is |
| 45 |
* not truncated. Inputs are modular counters, so the subtraction is unsigned |
| 46 |
* (well-defined wrap) before the optional 16-bit mask. */ |
| 47 |
static inline uint32_t rist_seq_gap(uint32_t current, uint32_t last, |
| 48 |
bool short_seq) |
| 49 |
{ |
| 50 |
uint32_t gap = current - last; |
| 51 |
return short_seq ? (gap & UINT16_MAX) : gap; |
| 52 |
} |
| 53 |
|
| 54 |
/* Next expected sequence number after `last`. short_seq (Simple/Main) flows |
| 55 |
* wrap at 16 bits; 32-bit (Advanced) flows use the natural successor. */ |
| 56 |
static inline uint32_t rist_seq_next(uint32_t last, bool short_seq) |
| 57 |
{ |
| 58 |
uint32_t next = last + 1; |
| 59 |
return short_seq ? (next & UINT16_MAX) : next; |
| 60 |
} |
| 61 |
|
| 62 |
// These control the memory footprint and buffer capacity of the lib |
| 63 |
// They MUST be a power of two or wrap-around index calculations will break |
| 64 |
// RIST_SERVER_QUEUE_BUFFERS is the DEFAULT Advanced-profile recovery-ring |
| 65 |
// capacity (in packets), == UINT16_SIZE << RIST_RECOVERY_DEPTH_DEFAULT (8x). |
| 66 |
// The ring is heap-allocated per flow/sender and can be resized at runtime to |
| 67 |
// UINT16_SIZE << depth via rist_recovery_depth_set() / ?recovery-depth=, up to |
| 68 |
// RIST_RECOVERY_QUEUE_MAX (depth 16, the full 32-bit space). Large depths are |
| 69 |
// limited by available RAM. Simple/Main are always UINT16_SIZE. |
| 70 |
#define RIST_SERVER_QUEUE_BUFFERS ((UINT16_SIZE) * 8) |
| 71 |
#define RIST_RECOVERY_QUEUE_MAX ((uint64_t)(UINT16_SIZE) << 16) |
| 72 |
#define RIST_RETRY_QUEUE_BUFFERS ((UINT16_SIZE) * 4) |
| 73 |
#define RIST_OOB_QUEUE_BUFFERS ((UINT16_SIZE) * 2) |
| 74 |
#define RIST_DATAOUT_QUEUE_BUFFERS (1024) |
| 75 |
// This will restrict the use of the library to the configured maximum packet size |
| 76 |
#define RIST_MAX_PACKET_SIZE (10000) |
| 77 |
#define RIST_RTT_MIN (3) |
| 78 |
|
| 79 |
/* nack requests are sent every time a data packet is received. */ |
| 80 |
/* this timer will be triggered to ensure we output nacks even when there is no data coming in */ |
| 81 |
#define RIST_MAX_JITTER (5) /* In milliseconds */ |
| 82 |
#define RIST_PING_INTERVAL (100) /* In milliseconds, how long to space ping requests */ |
| 83 |
#define RIST_PBKDF2_HMAC_SHA256_ITERATIONS (1024) |
| 84 |
#define RIST_AES_KEY_REUSE_TIMES UINT32_MAX |
| 85 |
#define RIST_MAX_HOSTNAME (128) |
| 86 |
#define RTCP_FB_FCI_GENERIC_NACK_SIZE (4) |
| 87 |
#define RIST_MAX_NACKS (200) |
| 88 |
#define RIST_MAX_NACKS_BYTES RIST_MAX_NACKS*RTCP_FB_FCI_GENERIC_NACK_SIZE |
| 89 |
// Maximum offset before the payload that the code can use to put in headers |
| 90 |
//#define RIST_MAX_PAYLOAD_OFFSET (sizeof(struct rist_gre_key_seq) + sizeof(struct rist_protocol_hdr)) |
| 91 |
#define RIST_MAX_HEADER_SIZE 32 |
| 92 |
#define CHECK_BIT(var,pos) !!((var) & (1<<(pos))) |
| 93 |
|
| 94 |
#define BYTE_TO_BINARY_PATTERN "%c%c%c%c%c%c%c%c" |
| 95 |
#define BYTE_TO_BINARY(byte) \ |
| 96 |
(byte & 0x80 ? '1' : '0'), \ |
| 97 |
(byte & 0x40 ? '1' : '0'), \ |
| 98 |
(byte & 0x20 ? '1' : '0'), \ |
| 99 |
(byte & 0x10 ? '1' : '0'), \ |
| 100 |
(byte & 0x08 ? '1' : '0'), \ |
| 101 |
(byte & 0x04 ? '1' : '0'), \ |
| 102 |
(byte & 0x02 ? '1' : '0'), \ |
| 103 |
(byte & 0x01 ? '1' : '0') |
| 104 |
|
| 105 |
#define STALE_FLOW_TIME (60L * 1000L * RIST_CLOCK) /* in milliseconds */ |
| 106 |
|
| 107 |
enum rist_peer_state { |
| 108 |
RIST_PEER_STATE_IDLE = 0, |
| 109 |
RIST_PEER_STATE_PING = 1, |
| 110 |
RIST_PEER_STATE_CONNECT = 2 |
| 111 |
}; |
| 112 |
struct rist_buffer { |
| 113 |
void *data; |
| 114 |
size_t size; |
| 115 |
uint8_t type; |
| 116 |
uint16_t src_port; |
| 117 |
uint16_t dst_port; |
| 118 |
uint64_t source_time; |
| 119 |
int8_t use_seq; |
| 120 |
uint32_t seq; |
| 121 |
uint16_t seq_rtp; |
| 122 |
|
| 123 |
uint64_t time;//Time we received the packet |
| 124 |
uint64_t packet_time;//Timestamp based on the RTP time of the packet |
| 125 |
uint64_t target_output_time;//packet_time + buffer |
| 126 |
|
| 127 |
uint8_t fragment_number; |
| 128 |
uint8_t fragment_final; |
| 129 |
// TODO: These four are only used by sender ... do I split buffer into sender and receiver? |
| 130 |
uint64_t last_retry_request; |
| 131 |
uint8_t transmit_count; |
| 132 |
uint16_t ts_null_bytes; |
| 133 |
struct rist_peer *peer; |
| 134 |
|
| 135 |
struct rist_buffer *next_free; |
| 136 |
size_t alloc_size; |
| 137 |
bool free; |
| 138 |
bool retry_queued; |
| 139 |
}; |
| 140 |
|
| 141 |
struct rist_missing_buffer { |
| 142 |
uint32_t seq; |
| 143 |
uint64_t next_nack; |
| 144 |
uint64_t insertion_time; |
| 145 |
uint32_t nack_count; |
| 146 |
struct rist_peer *peer; |
| 147 |
struct rist_missing_buffer *next; |
| 148 |
}; |
| 149 |
|
| 150 |
struct rist_bandwidth_estimation { |
| 151 |
size_t bytes; |
| 152 |
size_t bytes_fast; |
| 153 |
uint64_t last_bitrate_calctime; |
| 154 |
uint64_t last_bitrate_calctime_fast; |
| 155 |
size_t eight_times_bitrate_fast; |
| 156 |
size_t eight_times_bitrate; |
| 157 |
size_t bitrate; |
| 158 |
size_t bitrate_fast; |
| 159 |
}; |
| 160 |
|
| 161 |
struct rist_peer_flow_stats { |
| 162 |
uint32_t lost; |
| 163 |
uint32_t received; |
| 164 |
uint32_t dupe; |
| 165 |
uint32_t dropped_full; |
| 166 |
uint32_t dropped_late; |
| 167 |
size_t buffer_duration_count; |
| 168 |
uint32_t buffer_duration[2048]; |
| 169 |
|
| 170 |
uint32_t missing; |
| 171 |
uint32_t retries; |
| 172 |
uint32_t recovered; |
| 173 |
uint32_t reordered; |
| 174 |
uint32_t dups; |
| 175 |
uint32_t recovered_0nack; |
| 176 |
uint32_t recovered_1nack; |
| 177 |
uint32_t recovered_2nack; |
| 178 |
uint32_t recovered_3nack; |
| 179 |
uint32_t recovered_morenack; |
| 180 |
uint32_t recovered_sum; |
| 181 |
uint32_t recovered_average; |
| 182 |
int32_t recovered_slope; |
| 183 |
uint32_t recovered_slope_inverted; |
| 184 |
|
| 185 |
/* Inter-packet spacing */ |
| 186 |
uint64_t min_ips; |
| 187 |
uint64_t max_ips; |
| 188 |
uint64_t cur_ips; |
| 189 |
uint32_t avg_count; |
| 190 |
uint64_t total_ips; |
| 191 |
|
| 192 |
/* Per-byte counters */ |
| 193 |
uint64_t received_bytes; |
| 194 |
}; |
| 195 |
|
| 196 |
struct rist_peer_sender_stats { |
| 197 |
uint64_t sent; |
| 198 |
uint32_t received; |
| 199 |
uint32_t retrans; |
| 200 |
uint32_t bloat_skip; |
| 201 |
uint32_t bandwidth_skip; |
| 202 |
uint32_t retrans_skip; |
| 203 |
uint64_t ts_null; |
| 204 |
uint64_t sent_bytes; |
| 205 |
uint64_t retransmitted_bytes; |
| 206 |
}; |
| 207 |
|
| 208 |
struct rist_peer_receiver_stats { |
| 209 |
uint32_t sent_rtcp; |
| 210 |
uint32_t received_rtcp; |
| 211 |
uint64_t received; |
| 212 |
uint64_t ts_null; |
| 213 |
uint64_t received_bytes; |
| 214 |
}; |
| 215 |
|
| 216 |
struct nacks { |
| 217 |
uint32_t array[RIST_MAX_NACKS]; |
| 218 |
size_t counter; |
| 219 |
}; |
| 220 |
|
| 221 |
struct rist_flow { |
| 222 |
atomic_int shutdown; |
| 223 |
int max_output_jitter; |
| 224 |
|
| 225 |
struct rist_buffer **receiver_queue; /* output queue, heap-allocated to receiver_queue_max */ |
| 226 |
|
| 227 |
pthread_rwlock_t queue_lock; |
| 228 |
|
| 229 |
bool receiver_queue_has_items; |
| 230 |
bool flow_auto_buffer_scaling; |
| 231 |
bool currently_scaling_buffer; |
| 232 |
atomic_ulong receiver_queue_size; /* size in bytes */ |
| 233 |
uint64_t recovery_buffer_ticks; /* size in ticks */ |
| 234 |
uint64_t stats_report_time; /* in ticks */ |
| 235 |
atomic_ulong receiver_queue_output_idx; /* next packet to output */ |
| 236 |
size_t receiver_queue_max; |
| 237 |
bool flag_flow_buffer_start; |
| 238 |
|
| 239 |
/* Missing incoming packets, waiting for retransmission */ |
| 240 |
struct rist_missing_buffer *missing; |
| 241 |
struct rist_missing_buffer *missing_tail; |
| 242 |
uint32_t missing_counter; |
| 243 |
|
| 244 |
struct rist_peer_flow_stats stats_instant; |
| 245 |
struct rist_peer_flow_stats stats_total;//TODO: use the total stats! |
| 246 |
struct rist_bandwidth_estimation bw; |
| 247 |
struct rist_bandwidth_estimation bw_tsnull; |
| 248 |
struct rist_bandwidth_estimation bw_rejected; |
| 249 |
struct rist_bandwidth_estimation bw_retries; |
| 250 |
uint64_t stats_next_time; |
| 251 |
uint64_t checks_next_time; |
| 252 |
|
| 253 |
/* Missing queue max size */ |
| 254 |
uint32_t missing_counter_max; |
| 255 |
|
| 256 |
uint32_t flow_id; |
| 257 |
uint32_t flow_id_actual; |
| 258 |
int dead; |
| 259 |
struct rist_flow *next; |
| 260 |
struct rist_peer **peer_lst; |
| 261 |
size_t peer_lst_len; |
| 262 |
uint32_t last_seq_output; |
| 263 |
uint64_t last_seq_output_source_time; |
| 264 |
uint32_t last_seq_found; |
| 265 |
intptr_t receiver_id; |
| 266 |
intptr_t sender_id; |
| 267 |
uint64_t last_ipstats_time; |
| 268 |
uint64_t last_output_time; |
| 269 |
uint64_t max_source_time; |
| 270 |
uint64_t too_late_ctr; |
| 271 |
|
| 272 |
size_t offset_recalc_sample_count; |
| 273 |
uint64_t offset_recalc_samples[2048]; |
| 274 |
|
| 275 |
int64_t time_offset;//Current offset between our clock and RTP packets. |
| 276 |
int64_t time_offset_old;//Old offset between our clock and RTP packets. |
| 277 |
uint64_t time_offset_changed_ts;//Timestamp the RTP counter last wrapped |
| 278 |
uint64_t last_packet_ts;//Last packet time |
| 279 |
bool rtc_timing_mode; |
| 280 |
|
| 281 |
bool authenticated; |
| 282 |
|
| 283 |
/* Receiver thread variables */ |
| 284 |
pthread_t receiver_thread; |
| 285 |
bool receiver_thread_running; |
| 286 |
/* data out thread signaling */ |
| 287 |
pthread_cond_t condition; |
| 288 |
pthread_mutex_t mutex; |
| 289 |
|
| 290 |
/* variable used for seq number length (16bit or 32bit) */ |
| 291 |
bool short_seq; |
| 292 |
|
| 293 |
/* Scope-B merge auto-detection: set by keepalive L bit */ |
| 294 |
bool merge_auto_enabled; |
| 295 |
|
| 296 |
/* Session timeouts variables */ |
| 297 |
uint64_t session_timeout; |
| 298 |
uint64_t flow_timeout; |
| 299 |
uint64_t last_recv_ts; |
| 300 |
|
| 301 |
/* Receiver timed async data output */ |
| 302 |
struct rist_data_block **dataout_fifo_queue; |
| 303 |
size_t dataout_fifo_queue_bytesize; |
| 304 |
atomic_ulong dataout_fifo_queue_read_index; |
| 305 |
atomic_ulong dataout_fifo_queue_write_index; |
| 306 |
atomic_bool fifo_overflow; |
| 307 |
|
| 308 |
/* Temporary buffer for grouping and sending nacks */ |
| 309 |
struct nacks nacks; |
| 310 |
struct rist_logging_settings *logging_settings; |
| 311 |
}; |
| 312 |
|
| 313 |
struct rist_retry { |
| 314 |
struct rist_peer *peer; |
| 315 |
uint64_t insert_time; |
| 316 |
uint32_t seq; |
| 317 |
bool active;//signal whether this retry has been consumed (false) or not |
| 318 |
}; |
| 319 |
|
| 320 |
struct rist_common_ctx { |
| 321 |
atomic_int shutdown; |
| 322 |
atomic_bool startup_complete; |
| 323 |
|
| 324 |
/* Used by logging */ |
| 325 |
intptr_t sender_id; |
| 326 |
intptr_t receiver_id; |
| 327 |
struct rist_logging_settings *logging_settings; |
| 328 |
|
| 329 |
|
| 330 |
/* Flows */ |
| 331 |
struct rist_flow *FLOWS; |
| 332 |
pthread_mutex_t flows_lock; |
| 333 |
|
| 334 |
/* evsocket */ |
| 335 |
struct evsocket_ctx *evctx; |
| 336 |
|
| 337 |
/* Timers */ |
| 338 |
int rist_max_jitter; |
| 339 |
|
| 340 |
/* Recovery buffer RTT multiplier (default 7, per RIST spec) */ |
| 341 |
int recovery_rtt_multiplier; |
| 342 |
|
| 343 |
/* Advanced-profile recovery-ring capacity in packets (power of two). |
| 344 |
* Default RIST_SERVER_QUEUE_BUFFERS; tunable before rist_start() via |
| 345 |
* rist_recovery_depth_set(). Simple/Main ignore this and use |
| 346 |
* UINT16_SIZE. */ |
| 347 |
size_t recovery_queue_max; |
| 348 |
|
| 349 |
/* Peer list sync - RW locks */ |
| 350 |
struct rist_peer *PEERS; |
| 351 |
pthread_mutex_t peerlist_lock; |
| 352 |
|
| 353 |
/* buffers */ |
| 354 |
/* these are pre-allocated buffers, not pre-allocated aligned stack */ |
| 355 |
struct { |
| 356 |
uint8_t enc[RIST_MAX_PACKET_SIZE]; |
| 357 |
uint8_t dec[RIST_MAX_PACKET_SIZE]; |
| 358 |
uint8_t recv[RIST_MAX_PACKET_SIZE]; |
| 359 |
uint8_t recv_npd[RIST_MAX_PACKET_SIZE]; |
| 360 |
uint8_t rtcp[RIST_MAX_PACKET_SIZE]; |
| 361 |
} buf; |
| 362 |
struct rist_buffer *rist_free_buffer; |
| 363 |
pthread_mutex_t rist_free_buffer_mutex; |
| 364 |
uint64_t rist_free_buffer_count; |
| 365 |
|
| 366 |
/* timers */ |
| 367 |
uint64_t nacks_next_time; |
| 368 |
uint64_t stats_report_time; |
| 369 |
|
| 370 |
enum rist_profile profile; |
| 371 |
/* Set by rist_*_start() and by rist_peer_create() once a peer with |
| 372 |
* a definite profile has been inserted. When set, rist_peer_create |
| 373 |
* refuses any peer whose ?profile= disagrees with cctx->profile. */ |
| 374 |
atomic_bool profile_locked; |
| 375 |
uint8_t cname[RIST_MAX_HOSTNAME]; |
| 376 |
|
| 377 |
/* seq variables */ |
| 378 |
uint32_t seq; |
| 379 |
uint16_t seq_rtp; |
| 380 |
|
| 381 |
/* Advanced Profile (TR-06-3) state */ |
| 382 |
uint32_t adv_ssrc_base; /* Even SSRC for Protected flow */ |
| 383 |
uint32_t adv_seq_protected; /* 32-bit seq counter for even SSRC */ |
| 384 |
uint32_t adv_seq_unprotected; /* 32-bit seq counter for odd SSRC */ |
| 385 |
|
| 386 |
/* Peer counter (only the ones created by the API) */ |
| 387 |
uint32_t peer_counter; |
| 388 |
|
| 389 |
/* Auth callback variables */ |
| 390 |
struct { |
| 391 |
int (*conn_cb)(void *arg, const char* connecting_ip, uint16_t connecting_port, const char* local_ip, uint16_t local_port, struct rist_peer *peer); |
| 392 |
int (*disconn_cb)(void *arg, struct rist_peer *peer); |
| 393 |
void *arg; |
| 394 |
} auth; |
| 395 |
|
| 396 |
/* Out-of-band data variables */ |
| 397 |
int (*oob_data_callback)(void *arg, const struct rist_oob_block *oob_block); |
| 398 |
void *oob_data_callback_argument; |
| 399 |
bool oob_data_enabled; |
| 400 |
struct rist_peer *oob_current_peer; |
| 401 |
|
| 402 |
int (*stats_callback)(void *arg, const struct rist_stats *stats_container); |
| 403 |
void *stats_callback_argument; |
| 404 |
pthread_mutex_t stats_lock; |
| 405 |
|
| 406 |
pthread_rwlock_t oob_queue_lock; |
| 407 |
struct rist_buffer *oob_queue[RIST_OOB_QUEUE_BUFFERS]; /* oob queue */ |
| 408 |
size_t oob_queue_bytesize; |
| 409 |
uint16_t oob_queue_read_index; |
| 410 |
uint16_t oob_queue_write_index; |
| 411 |
|
| 412 |
bool debug; |
| 413 |
uint32_t birthtime_rtp_offset; |
| 414 |
|
| 415 |
/* Pluggable transport (default: POSIX sockets) */ |
| 416 |
struct rist_transport_ops transport; |
| 417 |
bool transport_active; |
| 418 |
|
| 419 |
/* Connection status callback */ |
| 420 |
connection_status_callback_t connection_status_callback; |
| 421 |
void *connection_status_callback_argument; |
| 422 |
|
| 423 |
rist_thread_callback_func_t thread_callback; |
| 424 |
void *thread_callback_arg; |
| 425 |
}; |
| 426 |
|
| 427 |
struct rist_receiver { |
| 428 |
/* data out thread signaling for fifo */ |
| 429 |
pthread_cond_t condition; |
| 430 |
pthread_mutex_t mutex; |
| 431 |
|
| 432 |
/* Receiver data callback */ |
| 433 |
receiver_data_callback2_t receiver_data_callback; |
| 434 |
void *receiver_data_callback_argument; |
| 435 |
int receiver_data_ready_notify_fd; |
| 436 |
|
| 437 |
/* Data fd output (tunnel mode) — write received data directly to fd */ |
| 438 |
int receiver_data_fd; |
| 439 |
uint32_t receiver_data_fd_flags; |
| 440 |
atomic_uint_fast64_t data_fd_rx_packets; |
| 441 |
atomic_uint_fast64_t data_fd_rx_bytes; |
| 442 |
|
| 443 |
/* Receiver session timeout callback */ |
| 444 |
receiver_session_timeout_callback_t receiver_session_timeout_callback; |
| 445 |
void *receiver_session_timeout_callback_argument; |
| 446 |
|
| 447 |
/* Receiver flow attribute callback (Advanced Profile CI=0x8001) */ |
| 448 |
receiver_flow_attr_callback_t receiver_flow_attr_callback; |
| 449 |
void *receiver_flow_attr_callback_argument; |
| 450 |
|
| 451 |
/* Receiver thread variables */ |
| 452 |
bool protocol_running; |
| 453 |
pthread_t receiver_thread; |
| 454 |
|
| 455 |
/* Reporting id */ |
| 456 |
intptr_t id; |
| 457 |
|
| 458 |
/* Common stuff */ |
| 459 |
struct rist_common_ctx common; |
| 460 |
|
| 461 |
enum rist_nack_type nack_type; |
| 462 |
|
| 463 |
bool simulate_loss; |
| 464 |
uint16_t loss_percentage; |
| 465 |
uint32_t fifo_queue_size; |
| 466 |
|
| 467 |
uint32_t merge_mode; |
| 468 |
uint64_t stats_pairs_merged; |
| 469 |
uint64_t stats_orphan_first_delivered; |
| 470 |
uint64_t stats_orphan_last_delivered; |
| 471 |
}; |
| 472 |
|
| 473 |
struct rist_sender { |
| 474 |
/* Advertised flow for this context */ |
| 475 |
uint32_t adv_flow_id; |
| 476 |
|
| 477 |
/* max bitrate of all sender peers (sets the buffer size on sender queue) */ |
| 478 |
uint32_t recovery_maxbitrate_max; |
| 479 |
uint32_t max_nacksperloop; |
| 480 |
bool null_packet_suppression; |
| 481 |
|
| 482 |
uint32_t split_mode; |
| 483 |
uint64_t stats_pairs_emitted; |
| 484 |
uint64_t stats_split_fallback_not_ts; |
| 485 |
|
| 486 |
/* Sender thread variables */ |
| 487 |
bool protocol_running; |
| 488 |
pthread_t sender_thread; |
| 489 |
/* data/nacks out thread signaling */ |
| 490 |
pthread_cond_t condition; |
| 491 |
pthread_mutex_t mutex; |
| 492 |
|
| 493 |
bool sender_initialized; |
| 494 |
uint32_t total_weight; |
| 495 |
struct rist_buffer **sender_queue; /* input queue, heap-allocated to sender_queue_max */ |
| 496 |
size_t sender_queue_bytesize; |
| 497 |
size_t sender_queue_size; |
| 498 |
size_t sender_queue_timelength; |
| 499 |
size_t sender_buffer_size; /* dynamic buffer size for retry window */ |
| 500 |
size_t sender_queue_delete_index; |
| 501 |
atomic_ulong sender_queue_read_index; |
| 502 |
atomic_ulong sender_queue_write_index; |
| 503 |
size_t sender_queue_max; |
| 504 |
int weight_counter; |
| 505 |
uint64_t last_datagram_time; |
| 506 |
bool simulate_loss; |
| 507 |
uint16_t loss_percentage; |
| 508 |
uint64_t stats_next_time; |
| 509 |
uint64_t checks_next_time; |
| 510 |
uint32_t session_timeout; |
| 511 |
|
| 512 |
/* retry queue */ |
| 513 |
struct rist_retry *sender_retry_queue; |
| 514 |
size_t sender_retry_queue_write_index; |
| 515 |
size_t sender_retry_queue_read_index; |
| 516 |
size_t sender_retry_queue_size; |
| 517 |
uint64_t cooldown_time; |
| 518 |
int cooldown_mode; |
| 519 |
|
| 520 |
/* Recovery - heap-allocated to sender_queue_max so Advanced Profile |
| 521 |
* can index with the full 32-bit seq space (seq & (queue_max - 1)). */ |
| 522 |
uint32_t *seq_index; |
| 523 |
size_t sender_recover_min_time; |
| 524 |
size_t sender_queue_buffer_size; |
| 525 |
|
| 526 |
/* Reporting id */ |
| 527 |
intptr_t id; |
| 528 |
|
| 529 |
/* flow_id time-related */ |
| 530 |
struct timeval time; |
| 531 |
|
| 532 |
/* Common stuff */ |
| 533 |
struct rist_common_ctx common; |
| 534 |
|
| 535 |
/* Peer tracking */ |
| 536 |
struct rist_peer **peer_lst; |
| 537 |
size_t peer_lst_len; |
| 538 |
|
| 539 |
/* Queue lock for fifo buffer */ |
| 540 |
pthread_mutex_t queue_lock; |
| 541 |
|
| 542 |
/* sender stats callback */ |
| 543 |
int (*sender_stats_callback)(void *arg, uint16_t version, char *stats_json, uint32_t json_size); |
| 544 |
void *sender_stats_callback_argument; |
| 545 |
uint64_t stats_report_time; /* in ticks */ |
| 546 |
|
| 547 |
/* Data fd input (tunnel mode) — read from fd and send as RIST data */ |
| 548 |
int data_fd; |
| 549 |
size_t data_fd_max_packet_size; |
| 550 |
uint32_t data_fd_flags; |
| 551 |
pthread_t data_fd_thread; |
| 552 |
bool data_fd_thread_started; |
| 553 |
atomic_uint_fast64_t data_fd_tx_packets; |
| 554 |
atomic_uint_fast64_t data_fd_tx_bytes; |
| 555 |
}; |
| 556 |
|
| 557 |
enum rist_ctx_mode { |
| 558 |
RIST_SENDER_MODE, |
| 559 |
RIST_RECEIVER_MODE |
| 560 |
}; |
| 561 |
|
| 562 |
struct rist_ctx { |
| 563 |
enum rist_ctx_mode mode; |
| 564 |
struct rist_sender *sender_ctx; |
| 565 |
struct rist_receiver *receiver_ctx; |
| 566 |
}; |
| 567 |
|
| 568 |
struct rist_peer { |
| 569 |
/* linked list */ |
| 570 |
pthread_mutex_t peer_lock;//Currently only used for setting password & in sending/receiving |
| 571 |
struct rist_peer *next; |
| 572 |
struct rist_peer *prev; |
| 573 |
|
| 574 |
/* For simple profile authentication chain (data and rtcp on different ports) */ |
| 575 |
struct rist_peer *peer_rtcp; |
| 576 |
struct rist_peer *peer_data; |
| 577 |
bool handled_first; |
| 578 |
bool is_rtcp; |
| 579 |
bool is_data; |
| 580 |
/* sender only: peer is known to respond to echo requests use those to calculate RTT instead */ |
| 581 |
bool echo_enabled; |
| 582 |
|
| 583 |
/* For keeping track of the connection that initiated a peer */ |
| 584 |
struct rist_peer *parent; |
| 585 |
struct rist_peer *sibling_prev; |
| 586 |
struct rist_peer *sibling_next; |
| 587 |
struct rist_peer *child; |
| 588 |
uint32_t child_alive_count; |
| 589 |
|
| 590 |
/* Flow for incoming traffic */ |
| 591 |
struct rist_flow *flow; |
| 592 |
|
| 593 |
/* Advertised flow id to force peer selection */ |
| 594 |
uint32_t adv_flow_id; |
| 595 |
|
| 596 |
/* Identifiers for multipeer links */ |
| 597 |
uint32_t adv_peer_id; |
| 598 |
uint32_t peer_ssrc; |
| 599 |
|
| 600 |
char receiver_name[RIST_MAX_HOSTNAME]; |
| 601 |
|
| 602 |
/* Config */ |
| 603 |
struct rist_peer_config config; |
| 604 |
uint64_t sender_max_buffer_ticks; |
| 605 |
uint64_t recovery_buffer_ticks; |
| 606 |
|
| 607 |
bool buffer_bloat_active; |
| 608 |
|
| 609 |
bool receiver_mode; |
| 610 |
|
| 611 |
int sd; |
| 612 |
|
| 613 |
/* State */ |
| 614 |
bool authenticated; |
| 615 |
|
| 616 |
/* Data sending */ |
| 617 |
uint32_t seq; |
| 618 |
uint64_t eight_times_rtt; |
| 619 |
uint32_t w_count; /* Counter for weight in distributed send */ |
| 620 |
|
| 621 |
/* RTT statistics */ |
| 622 |
uint64_t last_rtt; |
| 623 |
|
| 624 |
/* Missing queue max size */ |
| 625 |
uint32_t missing_counter_max; |
| 626 |
|
| 627 |
/* Encryption */ |
| 628 |
bool supports_otf_passphrase_change; |
| 629 |
struct rist_key key_tx; // used for transmitted packets |
| 630 |
struct rist_key key_rx; // used for received packets |
| 631 |
bool key_rx_odd_active; |
| 632 |
bool key_tx_odd_active; |
| 633 |
struct rist_key key_tx_odd; // used for transmitted packets |
| 634 |
struct rist_key key_rx_odd; // used for received packets |
| 635 |
bool rolling_over_passphrase; |
| 636 |
struct eapsrp_ctx *eap_ctx; |
| 637 |
int eap_authentication_state; |
| 638 |
uint8_t rist_gre_version; |
| 639 |
|
| 640 |
/* Advanced Profile (TR-06-3) peer state */ |
| 641 |
bool is_advanced; /* Peer operating in Advanced Profile mode */ |
| 642 |
bool remote_supports_advanced; /* Remote advertised I=1 in keep-alive */ |
| 643 |
|
| 644 |
/* compression flag (sender only) */ |
| 645 |
bool compression; |
| 646 |
|
| 647 |
/* Addressing */ |
| 648 |
uint16_t local_port; |
| 649 |
uint16_t remote_port; |
| 650 |
union { |
| 651 |
struct sockaddr address; |
| 652 |
struct sockaddr_in inaddr; |
| 653 |
struct sockaddr_in6 inaddr6; |
| 654 |
struct sockaddr_storage storage; |
| 655 |
} u; |
| 656 |
socklen_t address_len; |
| 657 |
uint16_t address_family; |
| 658 |
uint16_t state; |
| 659 |
char miface[128]; |
| 660 |
|
| 661 |
/* Events */ |
| 662 |
struct timeval expire; |
| 663 |
bool send_keepalive; |
| 664 |
struct evsocket_event *event_recv; |
| 665 |
|
| 666 |
/* listening mode with @ */ |
| 667 |
bool listening; |
| 668 |
/* multicast */ |
| 669 |
bool multicast_sender; |
| 670 |
bool multicast_receiver; |
| 671 |
|
| 672 |
/* rist ctx */ |
| 673 |
struct rist_sender *sender_ctx; |
| 674 |
struct rist_receiver *receiver_ctx; |
| 675 |
|
| 676 |
/* Reflector role flags */ |
| 677 |
bool is_reflector_publisher; |
| 678 |
|
| 679 |
/* rist buffer bloating counteract */ |
| 680 |
uint64_t cooldown_time; |
| 681 |
|
| 682 |
/* Statistics Sender */ |
| 683 |
struct rist_peer_sender_stats stats_sender_instant; |
| 684 |
struct rist_peer_sender_stats stats_sender_total; |
| 685 |
|
| 686 |
|
| 687 |
/* Statistics Receiver */ |
| 688 |
struct rist_peer_receiver_stats stats_receiver_instant; |
| 689 |
struct rist_peer_receiver_stats stats_receiver_total; |
| 690 |
|
| 691 |
int dead; |
| 692 |
int timed_out; |
| 693 |
uint64_t dead_since; |
| 694 |
/* Caller-side socket rebind bookkeeping. */ |
| 695 |
uint64_t last_rebind_time; |
| 696 |
uint32_t rebind_attempts; |
| 697 |
uint64_t birthtime_peer; |
| 698 |
uint64_t birthtime_local; |
| 699 |
|
| 700 |
/* bw estimation */ |
| 701 |
struct rist_bandwidth_estimation bw; |
| 702 |
struct rist_bandwidth_estimation retry_bw; |
| 703 |
struct rist_bandwidth_estimation ts_nulls_bw; |
| 704 |
|
| 705 |
/* shutting down flag */ |
| 706 |
atomic_bool shutdown; |
| 707 |
|
| 708 |
/* Timers */ |
| 709 |
uint32_t rtcp_keepalive_interval; |
| 710 |
uint64_t next_periodic_rtcp; |
| 711 |
uint64_t next_keepalive_packet; |
| 712 |
uint64_t next_flow_attr; |
| 713 |
uint64_t session_timeout; |
| 714 |
uint64_t last_pkt_received; |
| 715 |
uint64_t last_sender_report_time; |
| 716 |
uint64_t last_sender_report_ts; |
| 717 |
uint64_t ts_null_bytes; |
| 718 |
|
| 719 |
char *url; |
| 720 |
char cname[RIST_MAX_HOSTNAME]; |
| 721 |
uint8_t mac_addr[6]; |
| 722 |
bool send_first_connection_event; |
| 723 |
|
| 724 |
/* Rate-limit for EMSGSIZE/PMTU-too-large send errors so a flow of |
| 725 |
* oversized packets doesn't drown the log. Holds the last log time |
| 726 |
* in NTP ticks. */ |
| 727 |
uint64_t last_pmtu_error_log; |
| 728 |
|
| 729 |
uint64_t log_repeat_timer; |
| 730 |
|
| 731 |
uint8_t data[SIZEOF_GRE_KEEPALIVE]; |
| 732 |
|
| 733 |
}; |
| 734 |
|
| 735 |
static inline struct rist_common_ctx *rist_struct_get_common(struct rist_ctx *ctx) { |
| 736 |
if (RIST_UNLIKELY(!ctx)) |
| 737 |
return NULL; |
| 738 |
if (ctx->mode == RIST_SENDER_MODE && ctx->sender_ctx) |
| 739 |
return &ctx->sender_ctx->common; |
| 740 |
else if (ctx->mode == RIST_RECEIVER_MODE && ctx->receiver_ctx) |
| 741 |
return &ctx->receiver_ctx->common; |
| 742 |
else |
| 743 |
return NULL; |
| 744 |
} |
| 745 |
|
| 746 |
/* defined in flow.c */ |
| 747 |
RIST_PRIV void rist_receiver_flow_statistics(struct rist_receiver *ctx, struct rist_flow *flow); |
| 748 |
RIST_PRIV struct cJSON *rist_sender_peer_statistics(struct rist_peer *peer); |
| 749 |
RIST_PRIV void rist_sender_flow_statistics(struct rist_sender *ctx); |
| 750 |
RIST_PRIV void rist_delete_flow(struct rist_receiver *ctx, struct rist_flow *f); |
| 751 |
RIST_PRIV void rist_receiver_missing(struct rist_flow *f, struct rist_peer *peer,uint64_t nack_time, uint32_t seq, uint64_t rtt); |
| 752 |
RIST_PRIV int rist_receiver_associate_flow(struct rist_peer *p, uint32_t flow_id); |
| 753 |
RIST_PRIV size_t rist_best_rtt_index(struct rist_flow *f); |
| 754 |
RIST_PRIV struct rist_buffer *rist_new_buffer(struct rist_common_ctx *ctx, const void *buf, size_t len, uint8_t type, uint32_t seq, uint64_t source_time, uint16_t src_port, uint16_t dst_port); |
| 755 |
RIST_PRIV void free_rist_buffer(struct rist_common_ctx *ctx, struct rist_buffer *b); |
| 756 |
RIST_PRIV void rist_calculate_bitrate(size_t len, struct rist_bandwidth_estimation *bw); |
| 757 |
RIST_PRIV void rist_refresh_flow_bitrate(struct rist_bandwidth_estimation *bw); |
| 758 |
RIST_PRIV void empty_receiver_queue(struct rist_flow *f, struct rist_common_ctx *ctx); |
| 759 |
RIST_PRIV void rist_flush_missing_flow_queue(struct rist_flow *flow); |
| 760 |
|
| 761 |
/* defined in rist-common.c */ |
| 762 |
RIST_PRIV void rist_peer_authenticate(struct rist_peer *peer); |
| 763 |
RIST_PRIV void rist_shutdown_peer(struct rist_peer *peer); |
| 764 |
RIST_PRIV void rist_print_inet_info(char *prefix, struct rist_peer *peer); |
| 765 |
RIST_PRIV void rist_peer_rtcp(struct evsocket_ctx *ctx, void *arg); |
| 766 |
RIST_PRIV void rist_populate_cname(struct rist_peer *peer); |
| 767 |
RIST_PRIV void free_data_block(struct rist_data_block **const block); |
| 768 |
|
| 769 |
/* needed after splitting up */ |
| 770 |
RIST_PRIV PTHREAD_START_FUNC(sender_pthread_protocol, arg); |
| 771 |
RIST_PRIV PTHREAD_START_FUNC(receiver_pthread_protocol, arg); |
| 772 |
RIST_PRIV int rist_max_jitter_set(struct rist_common_ctx *ctx, int t); |
| 773 |
RIST_PRIV int rist_recovery_rtt_multiplier_set_internal(struct rist_common_ctx *ctx, int multiplier); |
| 774 |
RIST_PRIV int parse_url_options(const char *url, struct rist_peer_config *output_peer_config); |
| 775 |
RIST_PRIV int parse_url_udp_options(const char *url, struct rist_udp_config *output_udp_config); |
| 776 |
RIST_PRIV struct rist_peer *rist_receiver_peer_insert_local(struct rist_receiver *ctx, |
| 777 |
const struct rist_peer_config *config); |
| 778 |
RIST_PRIV void rist_sender_destroy_local(struct rist_sender *ctx); |
| 779 |
RIST_PRIV void rist_receiver_destroy_local(struct rist_receiver *ctx); |
| 780 |
RIST_PRIV struct rist_peer *rist_sender_peer_insert_local(struct rist_sender *ctx, |
| 781 |
const struct rist_peer_config *config, bool b_rtcp); |
| 782 |
RIST_PRIV void rist_fsm_init_comm(struct rist_peer *peer); |
| 783 |
RIST_PRIV int rist_oob_enqueue(struct rist_common_ctx *ctx, struct rist_peer *peer, const void *buf, size_t len); |
| 784 |
RIST_PRIV int init_common_ctx(struct rist_common_ctx *ctx, enum rist_profile profile); |
| 785 |
RIST_PRIV void init_advanced_state(struct rist_common_ctx *ctx); |
| 786 |
RIST_PRIV int rist_peer_remove(struct rist_common_ctx *ctx, struct rist_peer *peer, struct rist_peer **next); |
| 787 |
RIST_PRIV int rist_auth_handler(struct rist_common_ctx *ctx, |
| 788 |
int (*conn_cb)(void *arg, const char *connecting_ip, uint16_t connecting_port, const char *local_ip, uint16_t local_port, struct rist_peer *peer), |
| 789 |
int (*disconn_cb)(void *arg, struct rist_peer *peer), |
| 790 |
void *arg); |
| 791 |
RIST_PRIV void sender_peer_append(struct rist_sender *ctx, struct rist_peer *peer); |
| 792 |
|
| 793 |
/* Get common context */ |
| 794 |
RIST_PRIV struct rist_common_ctx *get_cctx(struct rist_peer *peer); |
| 795 |
|
| 796 |
/*static inline in header file */ |
| 797 |
static inline void peer_append(struct rist_peer *p) |
| 798 |
{ |
| 799 |
struct rist_common_ctx *cctx = get_cctx(p); |
| 800 |
struct rist_peer **PEERS = &cctx->PEERS; |
| 801 |
struct rist_peer *plist = *PEERS; |
| 802 |
if (!plist) |
| 803 |
{ |
| 804 |
*PEERS = p; |
| 805 |
return; |
| 806 |
} |
| 807 |
if (p->parent) |
| 808 |
{ |
| 809 |
struct rist_peer *peer = p->parent; |
| 810 |
if (!peer->child) { |
| 811 |
peer->child = p; |
| 812 |
if (peer->sender_ctx) |
| 813 |
peer->sender_ctx->total_weight += p->config.weight; |
| 814 |
} |
| 815 |
else |
| 816 |
{ |
| 817 |
struct rist_peer *child = peer->child; |
| 818 |
while (child) |
| 819 |
{ |
| 820 |
if (!child->sibling_next) |
| 821 |
{ |
| 822 |
child->sibling_next = p; |
| 823 |
p->sibling_prev = child; |
| 824 |
break; |
| 825 |
} |
| 826 |
child = child->sibling_next; |
| 827 |
} |
| 828 |
} |
| 829 |
++peer->child_alive_count; |
| 830 |
} |
| 831 |
while (plist) |
| 832 |
{ |
| 833 |
if (!plist->next) |
| 834 |
{ |
| 835 |
p->prev = plist; |
| 836 |
plist->next = p; |
| 837 |
return; |
| 838 |
} |
| 839 |
plist = plist->next; |
| 840 |
} |
| 841 |
} |
| 842 |
|
| 843 |
#endif |