| 1 |
. |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
#include "librist/librist.h" |
| 9 |
#include "rist-private.h" |
| 10 |
#include <stdatomic.h> |
| 11 |
#include <inttypes.h> |
| 12 |
#include "mpegts.h" |
| 13 |
#include "endian-shim.h" |
| 14 |
|
| 15 |
#ifdef _WIN32 |
| 16 |
#include <windows.h> |
| 17 |
#endif |
| 18 |
|
| 19 |
atomic_ulong failed; |
| 20 |
atomic_ulong stop; |
| 21 |
atomic_ulong flow_attr_received; |
| 22 |
int use_seq = 0; |
| 23 |
int check_flow_attr = 0; |
| 24 |
int has_loss = 0; |
| 25 |
#define USE_SEQ_START 1000 |
| 26 |
|
| 27 |
struct rist_logging_settings *logging_settings_sender = NULL; |
| 28 |
struct rist_logging_settings *logging_settings_receiver = NULL; |
| 29 |
char* senderstring = "sender"; |
| 30 |
char* receiverstring = "receiver"; |
| 31 |
|
| 32 |
int log_callback(void *arg, int level, const char *msg) { |
| 33 |
if (level > RIST_LOG_ERROR) |
| 34 |
fprintf(stdout, "[%s] %s",(char*)arg, msg); |
| 35 |
if (level <= RIST_LOG_ERROR) { |
| 36 |
fprintf(stdout, "[%s] [ERROR] %s", (char* )arg, msg); |
| 37 |
if (!has_loss) { |
| 38 |
atomic_store(&failed, 1); |
| 39 |
atomic_store(&stop, 1); |
| 40 |
} |
| 41 |
} |
| 42 |
return 0; |
| 43 |
} |
| 44 |
|
| 45 |
static int flow_attr_callback(void *arg, struct rist_peer *peer, const char *json, size_t json_len) { |
| 46 |
(void)arg; |
| 47 |
(void)peer; |
| 48 |
if (json && json_len > 0) |
| 49 |
atomic_fetch_add(&flow_attr_received, 1); |
| 50 |
return 0; |
| 51 |
} |
| 52 |
|
| 53 |
struct rist_ctx *setup_rist_receiver(int profile, const char *url) { |
| 54 |
struct rist_ctx *ctx; |
| 55 |
if (rist_receiver_create(&ctx, profile, logging_settings_receiver) != 0) { |
| 56 |
rist_log(logging_settings_receiver, RIST_LOG_ERROR, "Could not create rist receiver context\n"); |
| 57 |
return NULL; |
| 58 |
} |
| 59 |
|
| 60 |
struct rist_peer_config *peer_config = NULL; |
| 61 |
if (rist_parse_address2(url, (void *)&peer_config)) |
| 62 |
{ |
| 63 |
rist_log(logging_settings_receiver, RIST_LOG_ERROR, "Could not parse peer options for receiver\n"); |
| 64 |
return NULL; |
| 65 |
} |
| 66 |
struct rist_peer *peer; |
| 67 |
if (rist_peer_create(ctx, &peer, peer_config) == -1) { |
| 68 |
rist_log(logging_settings_receiver, RIST_LOG_ERROR, "Could not add peer connector to receiver\n"); |
| 69 |
return NULL; |
| 70 |
} |
| 71 |
#if HAVE_SRP_SUPPORT |
| 72 |
if (strlen(peer_config->srp_username) > 0 && |
| 73 |
strlen(peer_config->srp_password) > 0) { |
| 74 |
int srp_error = |
| 75 |
rist_enable_eap_srp_2(peer, peer_config->srp_username, |
| 76 |
peer_config->srp_password, NULL, NULL); |
| 77 |
if (srp_error) |
| 78 |
rist_log(logging_settings_receiver, RIST_LOG_WARN, |
| 79 |
"Error %d trying to enable SRP for peer\n", srp_error); |
| 80 |
} |
| 81 |
#endif |
| 82 |
free((void *)peer_config); |
| 83 |
if (check_flow_attr) { |
| 84 |
rist_receiver_flow_attr_callback_set(ctx, flow_attr_callback, NULL); |
| 85 |
} |
| 86 |
if (rist_start(ctx) == -1) { |
| 87 |
rist_log(logging_settings_receiver, RIST_LOG_ERROR, "Could not start rist sender\n"); |
| 88 |
return NULL; |
| 89 |
} |
| 90 |
return ctx; |
| 91 |
} |
| 92 |
|
| 93 |
struct rist_ctx *setup_rist_sender(int profile, const char *url) { |
| 94 |
struct rist_ctx *ctx; |
| 95 |
if (rist_sender_create(&ctx, profile, 0, logging_settings_sender) != 0) { |
| 96 |
rist_log(logging_settings_sender, RIST_LOG_ERROR, "Could not create rist sender context\n"); |
| 97 |
return NULL; |
| 98 |
} |
| 99 |
|
| 100 |
const struct rist_peer_config *peer_config_link = NULL; |
| 101 |
if (rist_parse_address2(url, (void *)&peer_config_link)) |
| 102 |
{ |
| 103 |
rist_log(logging_settings_sender, RIST_LOG_ERROR, "Could not parse peer options for sender\n"); |
| 104 |
return NULL; |
| 105 |
} |
| 106 |
|
| 107 |
struct rist_peer *peer; |
| 108 |
if (rist_peer_create(ctx, &peer, peer_config_link) == -1) { |
| 109 |
rist_log(logging_settings_sender, RIST_LOG_ERROR, "Could not add peer connector to sender\n"); |
| 110 |
return NULL; |
| 111 |
} |
| 112 |
|
| 113 |
#if HAVE_SRP_SUPPORT |
| 114 |
if (strlen(peer_config_link->srp_username) > 0 && |
| 115 |
strlen(peer_config_link->srp_password) > 0) { |
| 116 |
int srp_error = |
| 117 |
rist_enable_eap_srp_2(peer, peer_config_link->srp_username, |
| 118 |
peer_config_link->srp_password, NULL, NULL); |
| 119 |
if (srp_error) |
| 120 |
rist_log(logging_settings_sender, RIST_LOG_WARN, |
| 121 |
"Error %d trying to enable SRP for peer\n", srp_error); |
| 122 |
} |
| 123 |
#endif |
| 124 |
|
| 125 |
free((void *)peer_config_link); |
| 126 |
if (rist_start(ctx) == -1) { |
| 127 |
rist_log(logging_settings_sender, RIST_LOG_ERROR, "Could not start rist sender\n"); |
| 128 |
return NULL; |
| 129 |
} |
| 130 |
return ctx; |
| 131 |
} |
| 132 |
|
| 133 |
static PTHREAD_START_FUNC(send_data, arg) { |
| 134 |
struct rist_ctx *rist_sender = arg; |
| 135 |
int send_counter = 0; |
| 136 |
char buffer[1316] = { 0 }; |
| 137 |
struct rist_data_block data = { 0 }; |
| 138 |
srand((unsigned int)time(NULL)); |
| 139 |
|
| 140 |
while (send_counter < 16000) { |
| 141 |
if (atomic_load(&stop)) |
| 142 |
break; |
| 143 |
|
| 144 |
int random_num = (rand() % 7) + 1; |
| 145 |
for(int ts = 0; ts < random_num; ts++){ |
| 146 |
int offset = 188*ts; |
| 147 |
struct mpegts_header * hdr = (struct mpegts_header *)&buffer[offset]; |
| 148 |
memset(hdr, 0, sizeof(*hdr)); |
| 149 |
hdr->syncbyte = 0x47; |
| 150 |
|
| 151 |
int random_bit = rand() % 2; |
| 152 |
if (random_bit) { |
| 153 |
hdr->flags1 = htobe16(0x1FFF); |
| 154 |
SET_BIT(hdr->flags2,4); |
| 155 |
} |
| 156 |
else { |
| 157 |
hdr->flags1 = htobe16(0x1111); |
| 158 |
} |
| 159 |
int pkt_id = use_seq ? (send_counter + USE_SEQ_START) : send_counter; |
| 160 |
sprintf(&buffer[offset+sizeof(*hdr)+1], "DEADBEAF TEST PACKET #%i-%i", pkt_id, ts); |
| 161 |
} |
| 162 |
data.payload = &buffer; |
| 163 |
data.payload_len = 188 * random_num; |
| 164 |
if (use_seq) { |
| 165 |
data.flags = RIST_DATA_FLAGS_USE_SEQ; |
| 166 |
data.seq = (uint64_t)(send_counter + USE_SEQ_START); |
| 167 |
} |
| 168 |
int ret = rist_sender_data_write(rist_sender, &data); |
| 169 |
if (ret < 0) { |
| 170 |
fprintf(stderr, "Failed to send test packet with error code %d!\n", ret); |
| 171 |
atomic_store(&failed, 1); |
| 172 |
atomic_store(&stop, 1); |
| 173 |
break; |
| 174 |
} |
| 175 |
else if (ret != (int)data.payload_len) { |
| 176 |
fprintf(stderr, "Failed to send test packet %d != %d !\n", ret, (int)data.payload_len); |
| 177 |
atomic_store(&failed, 1); |
| 178 |
atomic_store(&stop, 1); |
| 179 |
break; |
| 180 |
} |
| 181 |
send_counter++; |
| 182 |
#ifdef _WIN32 |
| 183 |
Sleep(1); |
| 184 |
#else |
| 185 |
usleep(500); |
| 186 |
#endif |
| 187 |
} |
| 188 |
|
| 189 |
#ifdef _WIN32 |
| 190 |
Sleep(2); |
| 191 |
#else |
| 192 |
usleep(1500); |
| 193 |
#endif |
| 194 |
|
| 195 |
atomic_store(&stop, 1); |
| 196 |
return 0; |
| 197 |
} |
| 198 |
|
| 199 |
int main(int argc, char *argv[]) { |
| 200 |
if (argc < 5 || argc > 8) { |
| 201 |
return 99; |
| 202 |
} |
| 203 |
int profile = atoi(argv[1]); |
| 204 |
char *url1 = strdup(argv[2]); |
| 205 |
char *url2 = strdup(argv[3]); |
| 206 |
int losspercent = atoi(argv[4]) * 10; |
| 207 |
has_loss = losspercent > 0; |
| 208 |
int npd = 0; |
| 209 |
int ret = 0; |
| 210 |
|
| 211 |
if (argc >= 6) |
| 212 |
npd = atoi(argv[5]); |
| 213 |
if (argc >= 7) |
| 214 |
use_seq = atoi(argv[6]); |
| 215 |
if (argc >= 8) |
| 216 |
check_flow_attr = atoi(argv[7]); |
| 217 |
|
| 218 |
struct rist_ctx *receiver_ctx = NULL; |
| 219 |
struct rist_ctx *sender_ctx = NULL; |
| 220 |
|
| 221 |
atomic_init(&failed, 0); |
| 222 |
atomic_init(&stop, 0); |
| 223 |
atomic_init(&flow_attr_received, 0); |
| 224 |
|
| 225 |
|
| 226 |
fprintf(stdout, "Testing profile %i with receiver url %s and sender url %s and losspercentage: %i\n", profile, url1, url2, losspercent); |
| 227 |
|
| 228 |
if (rist_logging_set(&logging_settings_sender, RIST_LOG_DEBUG, log_callback, senderstring, NULL, stderr) != 0) { |
| 229 |
fprintf(stderr,"Failed to setup logging!\n"); |
| 230 |
ret = 99; |
| 231 |
goto out; |
| 232 |
} |
| 233 |
|
| 234 |
if (rist_logging_set(&logging_settings_receiver, RIST_LOG_DEBUG, log_callback, receiverstring, NULL, stderr) != 0) |
| 235 |
{ |
| 236 |
fprintf(stderr, "Failed to setup logging!\n"); |
| 237 |
ret = 99; |
| 238 |
goto out; |
| 239 |
} |
| 240 |
receiver_ctx = setup_rist_receiver(profile, url1); |
| 241 |
sender_ctx = setup_rist_sender(profile, url2); |
| 242 |
if (!sender_ctx || !receiver_ctx) { |
| 243 |
ret = 99; |
| 244 |
goto out; |
| 245 |
} |
| 246 |
|
| 247 |
if (losspercent > 0) { |
| 248 |
receiver_ctx->receiver_ctx->simulate_loss = true; |
| 249 |
receiver_ctx->receiver_ctx->loss_percentage = losspercent; |
| 250 |
sender_ctx->sender_ctx->simulate_loss = true; |
| 251 |
sender_ctx->sender_ctx->loss_percentage = losspercent; |
| 252 |
} |
| 253 |
if (npd > 0) |
| 254 |
rist_sender_npd_enable(sender_ctx); |
| 255 |
|
| 256 |
pthread_t send_loop; |
| 257 |
if (pthread_create(&send_loop, NULL, send_data, (void *)sender_ctx) != 0) |
| 258 |
{ |
| 259 |
fprintf(stderr, "Could not start send data thread\n"); |
| 260 |
ret = 99; |
| 261 |
goto out; |
| 262 |
} |
| 263 |
|
| 264 |
struct rist_data_block *b = NULL; |
| 265 |
char rcompare[1316]; |
| 266 |
int receive_count = 1; |
| 267 |
bool got_first = false; |
| 268 |
while (receive_count < 16000) { |
| 269 |
if (atomic_load(&stop)) |
| 270 |
break; |
| 271 |
int queue_length = rist_receiver_data_read2(receiver_ctx, &b, 5); |
| 272 |
if (queue_length > 0) { |
| 273 |
if (!got_first) { |
| 274 |
receive_count = (int)b->seq; |
| 275 |
got_first = true; |
| 276 |
if (use_seq && (int)b->seq < USE_SEQ_START) { |
| 277 |
fprintf(stderr, "USE_SEQ: first seq %"PRIu64" < expected start %d, " |
| 278 |
"sender-supplied seq was not preserved\n", |
| 279 |
b->seq, USE_SEQ_START); |
| 280 |
atomic_store(&failed, 1); |
| 281 |
atomic_store(&stop, 1); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
int tsindex = (int)(b->payload_len / 188); |
| 286 |
for(int ts = 0; ts < tsindex; ts++){ |
| 287 |
int offset = 188*ts; |
| 288 |
uint8_t *payload = ((uint8_t*)b->payload + offset); |
| 289 |
struct mpegts_header * hdr = (struct mpegts_header *)(payload); |
| 290 |
if (hdr->syncbyte != 0x47) |
| 291 |
{ |
| 292 |
fprintf(stderr, "Packet contents not as expected!\n"); |
| 293 |
fprintf(stderr, "Sync Byte at index %d was %d instead of 0x47 with %zu bytes\n", ts, hdr->syncbyte, b->payload_len); |
| 294 |
atomic_store(&failed, 1); |
| 295 |
atomic_store(&stop, 1); |
| 296 |
break; |
| 297 |
} |
| 298 |
|
| 299 |
|
| 300 |
if ((npd == 0 || (npd == 1 && be16toh(hdr->flags1) != 0x1FFF))) |
| 301 |
{ |
| 302 |
|
| 303 |
sprintf(rcompare, "DEADBEAF TEST PACKET #%i-%i", receive_count, ts); |
| 304 |
if (strcmp(rcompare, (char *)b->payload + offset + sizeof(*hdr) + 1)) { |
| 305 |
fprintf(stderr, "Packet contents not as expected at index %i!\n", ts); |
| 306 |
fprintf(stderr, "Got : %s (%zu/%d)\n", (char*)b->payload, b->payload_len, b->virt_dst_port); |
| 307 |
fprintf(stderr, "Expected : %s\n", (char*)rcompare); |
| 308 |
atomic_store(&failed, 1); |
| 309 |
atomic_store(&stop, 1); |
| 310 |
break; |
| 311 |
} |
| 312 |
} |
| 313 |
} |
| 314 |
receive_count++; |
| 315 |
rist_receiver_data_block_free2((struct rist_data_block **const)&b); |
| 316 |
} |
| 317 |
} |
| 318 |
int expected = 16000 * (1000 - losspercent) / 1000; |
| 319 |
int min_receive = (expected < 12500) ? expected * 5 / 6 : 12500; |
| 320 |
if (!got_first || receive_count < min_receive) { |
| 321 |
fprintf(stderr, "Received %d packets, minimum required %d\n", |
| 322 |
receive_count, min_receive); |
| 323 |
atomic_store(&failed, 1); |
| 324 |
} |
| 325 |
if (check_flow_attr) { |
| 326 |
unsigned long fa_count = atomic_load(&flow_attr_received); |
| 327 |
fprintf(stdout, "Flow attributes received: %lu\n", fa_count); |
| 328 |
if (fa_count == 0) { |
| 329 |
fprintf(stderr, "FAIL: expected at least one flow attribute callback\n"); |
| 330 |
atomic_store(&failed, 1); |
| 331 |
} |
| 332 |
} |
| 333 |
if (atomic_load(&failed)) |
| 334 |
ret = 1; |
| 335 |
pthread_join(send_loop, NULL); |
| 336 |
out: |
| 337 |
free(url1); |
| 338 |
free(url2); |
| 339 |
if (sender_ctx) |
| 340 |
rist_destroy(sender_ctx); |
| 341 |
if (receiver_ctx) |
| 342 |
rist_destroy(receiver_ctx); |
| 343 |
free(logging_settings_receiver); |
| 344 |
free(logging_settings_sender); |
| 345 |
if (ret > 0) { |
| 346 |
fprintf(stderr, "FAIL\n"); |
| 347 |
return ret; |
| 348 |
} |
| 349 |
|
| 350 |
fprintf(stdout, "OK\n"); |
| 351 |
return 0; |
| 352 |
} |