-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathhd-rum-translator.cpp
More file actions
1243 lines (1104 loc) · 44.3 KB
/
hd-rum-translator.cpp
File metadata and controls
1243 lines (1104 loc) · 44.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @file hd-rum-translator/hd-rum-translator.cpp
* @author Martin Pulec <pulec@cesnet.cz>
* @author Martin Piatka <piatka@cesnet.cz>
*
* Main part of transcoding reflector. This component provides a runtime
* for the reflector. Components are following:
* - legacy packet reflector (defined in this file) for backward compatibility.
* It handles those recipient that doesn't need transcoding.
* - decompressor - decompresses the stream if there are some host that need
* transcoding
* - recompressor - for every transcoded host, there is a recompressor that
* compresses and sends frame to receiver
*/
/*
* Copyright (c) 2013-2026 CESNET, zájmové sdružení právnických osob
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, is permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of CESNET nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING,
* BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <atomic> // for atomic, memory_order
#include <cassert> // for assert
#include <cctype> // for isdigit
#include <cinttypes>
#include <climits>
#include <csignal> // for sigaction, signal
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime> // for localtime, strftime
#include <getopt.h>
#include <memory> // for shared_ptr, operator!=
#include <pthread.h>
#include <stdexcept> // for invalid_argument
#include <string>
#include <thread>
#include <unistd.h> // for optarg, optind, ssi...
#include <utility> // for swap, move
#include <vector>
#include "compat/alarm.h"
#include "control_socket.h"
#include "debug.h"
#include "hd-rum-translator/hd-rum-decompress.h"
#include "hd-rum-translator/hd-rum-recompress.h"
#include "host.h"
#include "lib_common.h"
#include "messaging.h"
#include "module.h"
#include "rtp/net_udp.h"
#include "tv.h"
#include "ug_runtime_error.hpp"
#include "utils/color_out.h"
#include "utils/misc.h" // format_in_si_units, unit_evaluate
#include "utils/net.h"
using std::invalid_argument;
using std::stoi;
using std::string;
using std::to_string;
using std::vector;
#define MOD_NAME "[hd-rum-trans] "
#define REPLICA_MAGIC 0xd2ff3323
static void
set_replica_mod_name(size_t buflen, char *buf, const char *addr,
uint16_t tx_port)
{
bool is_ipv6 = strchr(addr, ':') != NULL;
bool add_bracket = is_ipv6 && addr[0] != '[';
const size_t len =
snprintf(buf, buflen, "%s%s%s:%" PRIu16, add_bracket ? "[" : "", addr,
add_bracket ? "]" : "", tx_port);
assert(len < buflen); // len >= buflen means overflow
}
static void new_message_received(struct module *);
struct replica {
replica(const char *addr, uint16_t rx_port, uint16_t tx_port, int bufsize, struct module *parent, int force_ip_version) {
magic = REPLICA_MAGIC;
host = addr;
m_tx_port = tx_port;
sock = std::shared_ptr<socket_udp>(udp_init(addr, rx_port, tx_port, 255, force_ip_version, false), udp_exit);
int mode = 0;
int res = resolve_addrinfo(addr, tx_port, &sockaddr, &sockaddr_len, &mode);
if (!sock || res != 0) {
throw string("Cannot initialize output port!\n");
}
if (!udp_set_send_buf(sock.get(), bufsize)) {
fprintf(stderr, "Cannot set send buffer to %sB!\n",
format_in_si_units(bufsize));
}
module_init_default(&mod);
mod.cls = MODULE_CLASS_PORT;
set_replica_mod_name(sizeof mod.name, mod.name, addr, tx_port);
mod.priv_data = this;
mod.new_message = new_message_received;
module_register(&mod, parent);
type = replica::type_t::NONE;
}
~replica() {
assert(magic == REPLICA_MAGIC);
module_done(&mod);
}
struct module mod;
uint32_t magic;
string host;
int m_tx_port;
enum type_t {
NONE,
USE_SOCK,
RECOMPRESS
};
enum type_t type;
std::shared_ptr<socket_udp> sock;
sockaddr_storage sockaddr;
socklen_t sockaddr_len;
};
struct hd_rum_translator_state {
hd_rum_translator_state() {
init_root_module(&mod);
pthread_mutex_init(&qempty_mtx, NULL);
pthread_mutex_init(&qfull_mtx, NULL);
pthread_cond_init(&qempty_cond, NULL);
pthread_cond_init(&qfull_cond, NULL);
}
~hd_rum_translator_state() {
pthread_mutex_destroy(&qempty_mtx);
pthread_mutex_destroy(&qfull_mtx);
pthread_cond_destroy(&qempty_cond);
pthread_cond_destroy(&qfull_cond);
module_done(&mod);
}
struct module mod;
int bufsize = 0;
struct control_state *control_state = nullptr;
struct item *queue = nullptr;
struct item *qhead = nullptr;
struct item *qtail = nullptr;
int qempty = 1;
int qfull = 0;
pthread_mutex_t qempty_mtx;
pthread_mutex_t qfull_mtx;
pthread_cond_t qempty_cond;
pthread_cond_t qfull_cond;
vector<replica *> replicas;
std::shared_ptr<socket_udp> server_socket;
void *decompress = nullptr;
struct state_recompress *recompress = nullptr;
};
/*
* Prototypes
*/
static struct item *qinit(int qsize);
static void qdestroy(struct item *queue);
static void *writer(void *arg);
static void signal_handler(int signum)
{
#ifdef SIGPIPE
if (signum == SIGPIPE) {
signal(SIGPIPE, SIG_IGN);
}
#endif // defined SIGPIPE
if (log_level >= LOG_LEVEL_DEBUG) {
char msg[] = "Caught signal ";
char buf[128];
char *ptr = buf;
for (size_t i = 0; i < sizeof msg - 1; ++i) {
*ptr++ = msg[i];
}
if (signum / 10) {
*ptr++ = '0' + signum / 10;
}
*ptr++ = '0' + signum % 10;
*ptr++ = '\n';
size_t bytes = ptr - buf;
ptr = buf;
do {
ssize_t written = write(STDERR_FILENO, ptr, bytes);
if (written < 0) {
break;
}
bytes -= written;
ptr += written;
} while (bytes > 0);
}
exit_uv(0);
}
#define MAX_PKT_SIZE 10000
#ifdef _WIN32
struct wsa_aux_storage {
WSAOVERLAPPED *overlapped;
int ref;
};
#define ALIGNMENT std::alignment_of<wsa_aux_storage>::value
#define OFFSET ((MAX_PKT_SIZE + ALIGNMENT - 1) / ALIGNMENT * ALIGNMENT)
#define SIZE (OFFSET + sizeof(wsa_aux_storage))
#else
#define SIZE MAX_PKT_SIZE
#endif
struct item {
struct item *next;
long size;
char *buf;
};
static struct item *qinit(int qsize)
{
struct item *queue;
int i;
if (qsize <= 0) {
fprintf(stderr, "wrong packet queue size %d items\n", qsize);
return nullptr;
}
printf("initializing packet queue for %d items\n", qsize);
queue = (struct item *) calloc(qsize, sizeof(struct item));
if (queue == NULL) {
fprintf(stderr, "not enough memory\n");
exit(2);
}
for (i = 0; i < qsize; i++) {
queue[i].buf = (char *) malloc(SIZE);
queue[i].next = queue + i + 1;
}
queue[qsize - 1].next = queue;
return queue;
}
static void qdestroy(struct item *queue)
{
if (queue == nullptr) {
return;
}
struct item *q = queue;
do {
free(q->buf);
q = q->next;
} while (q != queue);
free(queue);
}
#define prefix_matches(x,y) strncasecmp(x, y, strlen(y)) == 0
static struct response *change_replica_type(struct hd_rum_translator_state *s,
struct module *mod, struct message *msg, int index)
{
struct replica *r = (struct replica *) mod->priv_data;
struct msg_universal *data = (struct msg_universal *) msg;
if (strcasecmp(data->text, "sock") == 0) {
r->type = replica::type_t::USE_SOCK;
log_msg(LOG_LEVEL_NOTICE, "Output port %d is now forwarding.\n", index);
} else if (strcasecmp(data->text, "recompress") == 0) {
r->type = replica::type_t::RECOMPRESS;
log_msg(LOG_LEVEL_NOTICE, "Output port %d is now transcoding.\n", index);
} else if (prefix_matches(data->text, "compress ")) {
if(recompress_port_change_compress(s->recompress, index, data->text + strlen("compress "))){
log_msg(LOG_LEVEL_NOTICE, "Output port %d compression changed.\n", index);
} else {
log_msg(LOG_LEVEL_ERROR, "Failed to change port %d compression. Port removed.\n", index);
}
} else {
fprintf(stderr, "Unknown replica type \"%s\"\n", data->text);
return new_response(RESPONSE_BAD_REQUEST, NULL);
}
recompress_port_set_active(s->recompress, index,
r->type == replica::type_t::RECOMPRESS);
return new_response(RESPONSE_OK, NULL);
}
#ifdef _WIN32
static VOID CALLBACK wsa_deleter(DWORD /* dwErrorCode */,
DWORD /* dwNumberOfBytesTransfered */,
LPOVERLAPPED lpOverlapped, long unsigned int) {
struct wsa_aux_storage *aux = (struct wsa_aux_storage *)(void *) ((char *) lpOverlapped->hEvent + OFFSET);
if (--aux->ref == 0) {
free(aux->overlapped);
free(lpOverlapped->hEvent);
}
}
#endif
static int create_output_port(struct hd_rum_translator_state *s,
const char *addr, int rx_port, int tx_port, int bufsize,
struct common_opts *common, const char *compression, const char *fec,
int bitrate, bool use_server_sock = false)
{
struct replica *rep;
try {
rep = new replica(addr, rx_port, tx_port, bufsize, &s->mod,
common->force_ip_version);
if(use_server_sock){
rep->sock = s->server_socket;
}
} catch (string const & s) {
fputs(s.c_str(), stderr);
const char *err_msg = "cannot create output port (wrong address?)";
log_msg(LOG_LEVEL_ERROR, "%s\n", err_msg);
return -1;
}
s->replicas.push_back(rep);
struct common_opts com_opts = *common;
com_opts.parent = &rep->mod;
rep->type = compression ? replica::type_t::RECOMPRESS : replica::type_t::USE_SOCK;
int idx = recompress_add_port(s->recompress,
addr, compression ? compression : "none",
0, tx_port, &com_opts, fec, bitrate);
if (idx < 0) {
fprintf(stderr, "Initializing output port '%s' compression failed!\n", addr);
delete s->replicas.back();
s->replicas.pop_back();
return -1;
}
assert((unsigned) idx == s->replicas.size() - 1);
recompress_port_set_active(s->recompress, idx, compression != nullptr);
return idx;
}
static void *writer(void *arg)
{
struct hd_rum_translator_state *s =
(struct hd_rum_translator_state *) arg;
while (1) {
// first check messages
for (unsigned int i = 0; i < s->replicas.size(); i++) {
struct message *msg;
while ((msg = check_message(&s->replicas[i]->mod))) {
struct response *r = change_replica_type(s, &s->replicas[i]->mod, msg, i);
free_message(msg, r);
}
}
struct msg_universal *msg;
while ((msg = (struct msg_universal *) check_message(&s->mod))) {
struct response *r = NULL;
if (strncasecmp(msg->text, "delete-port ", strlen("delete-port ")) == 0) {
char *port_spec = msg->text + strlen("delete-port ");
int index = -1;
if (isdigit(port_spec[0])) {
int i = stoi(port_spec);
if (i >= 0 && i < (int) s->replicas.size()) {
index = i;
} else {
log_msg(LOG_LEVEL_WARNING, "Invalid port index: %d. Not removing.\n", i);
}
} else {
int i = 0;
for (auto r : s->replicas) {
if (strcmp(r->mod.name, port_spec) == 0) {
index = i;
break;
}
i++;
}
if (index == -1) {
log_msg(LOG_LEVEL_WARNING, "Unknown port name: %s. Not removing.\n", port_spec);
}
}
if (index >= 0) {
recompress_remove_port(s->recompress, index);
delete s->replicas[index];
s->replicas.erase(s->replicas.begin() + index);
log_msg(LOG_LEVEL_NOTICE, "Deleted output port %d.\n", index);
}
} else if (strncasecmp(msg->text, "create-port", strlen("create-port")) == 0) {
// format of parameters is either:
// <host>:<port> [<compression>]
// or (for compat with older CoUniverse version)
// <host> <port> [<compression>]
char *host_port, *port_str = NULL, *save_ptr;
char *host;
int tx_port;
strtok_r(msg->text, " ", &save_ptr);
host_port = strtok_r(NULL, " ", &save_ptr);
if (host_port && (strchr(host_port, ':') != NULL || (port_str = strtok_r(NULL, " ", &save_ptr)) != NULL)) {
if (port_str) {
host = host_port;
tx_port = stoi(port_str);
} else {
tx_port = stoi(strrchr(host_port, ':') + 1);
host = host_port;
*strrchr(host_port, ':') = '\0';
}
// handle square brackets around an IPv6 address
if (host[0] == '[' && host[strlen(host) - 1] == ']') {
host += 1;
host[strlen(host) - 1] = '\0';
}
} else {
const char *err_msg = "wrong format";
log_msg(LOG_LEVEL_ERROR, "%s\n", err_msg);
free_message((struct message *) msg, new_response(RESPONSE_BAD_REQUEST, err_msg));
continue;
}
char *compress = strtok_r(NULL, " ", &save_ptr);
struct common_opts opts = COMMON_OPTS_INIT;
int idx = create_output_port(s,
host, 0, tx_port, s->bufsize, &opts,
compress, nullptr, RATE_UNLIMITED, s->server_socket != nullptr);
if(idx < 0) {
free_message((struct message *) msg, new_response(RESPONSE_INT_SERV_ERR, "Cannot create output port."));
continue;
}
if(compress)
log_msg(LOG_LEVEL_NOTICE, "Created new transcoding output port %s:%d:0x%08" PRIx32 ".\n", host, tx_port, recompress_get_port_ssrc(s->recompress, idx));
else
log_msg(LOG_LEVEL_NOTICE, "Created new forwarding output port %s:%d.\n", host, tx_port);
} else {
r = new_response(RESPONSE_BAD_REQUEST, NULL);
}
free_message((struct message *) msg, r ? r : new_response(RESPONSE_OK, NULL));
}
// then process incoming packets
while (s->qhead != s->qtail) {
if(s->qhead->size == 0) { // poisoned pill
return NULL;
}
// pass it for transcoding if needed
if (recompress_get_num_active_ports(s->recompress) > 0) {
ssize_t ret = hd_rum_decompress_write(s->decompress, s->qhead->buf, s->qhead->size);
if (ret < 0) {
perror("hd_rum_decompress_write");
}
}
// distribute it to output ports that don't need transcoding
#ifdef _WIN32
// send it asynchronously in MSW (performance optimalization)
SleepEx(0, true); // allow system to call our completion routines in APC
int ref = 0;
for (unsigned int i = 0; i < s->replicas.size(); i++) {
if(s->replicas[i]->type == replica::type_t::USE_SOCK) {
ref++;
}
}
struct wsa_aux_storage *aux = (struct wsa_aux_storage *)(void *) ((char *) s->qhead->buf + OFFSET);
memset(aux, 0, sizeof *aux);
aux->overlapped = (WSAOVERLAPPED *) calloc(ref, sizeof(WSAOVERLAPPED));
aux->ref = ref;
int overlapped_idx = 0;
for (unsigned int i = 0; i < s->replicas.size(); i++) {
if(s->replicas[i]->type == replica::type_t::USE_SOCK) {
aux->overlapped[overlapped_idx].hEvent = s->qhead->buf;
ssize_t ret = udp_sendto_wsa_async(s->replicas[i]->sock.get(), s->qhead->buf, s->qhead->size,
wsa_deleter, &aux->overlapped[overlapped_idx], (sockaddr *) &s->replicas[i]->sockaddr, s->replicas[i]->sockaddr_len);
if (ret < 0) {
perror("Hd-rum-translator send");
}
overlapped_idx += 1;
}
}
// reallocate the buffer since the last one will be freed automatically
s->qhead->buf = (char *) malloc(SIZE);
#else
for (unsigned int i = 0; i < s->replicas.size(); i++) {
if(s->replicas[i]->type == replica::type_t::USE_SOCK) {
ssize_t ret = udp_sendto(s->replicas[i]->sock.get(), s->qhead->buf, s->qhead->size, (sockaddr *) &s->replicas[i]->sockaddr, s->replicas[i]->sockaddr_len);
if (ret < 0) {
perror("Hd-rum-translator send");
}
}
}
#endif
s->qhead = s->qhead->next;
pthread_mutex_lock(&s->qfull_mtx);
s->qfull = 0;
pthread_cond_signal(&s->qfull_cond);
pthread_mutex_unlock(&s->qfull_mtx);
}
pthread_mutex_lock(&s->qempty_mtx);
if (s->qempty)
pthread_cond_wait(&s->qempty_cond, &s->qempty_mtx);
s->qempty = 1;
pthread_mutex_unlock(&s->qempty_mtx);
}
return NULL;
}
static void
new_message_received(struct module *m)
{
auto *s = (struct hd_rum_translator_state *) m->priv_data;
pthread_mutex_lock(&s->qempty_mtx);
s->qempty = 0;
pthread_mutex_unlock(&s->qempty_mtx);
pthread_cond_signal(&s->qempty_cond);
}
static void usage(const char *progname) {
col() << "Usage:\n\t"
<< SBOLD(SRED(progname)
<< " [global_opts] buffer_size port \\\n\t\t[host1_options] "
"host1 [[host2_options] host2] ...")
<< "\n";
col() << "\nwhere:\n"
<< SBOLD("\tbuffer_size")
<< " - network buffer size, eg. 200k for compressed\n\t\t or "
"8M for uncompressed video\n"
<< SBOLD("\tport") << " - UDP port number\n";
col() << SUNDERLINE("global_opts") << " may be:\n"
<< SBOLD("\t--control-port|-n <port_number>[:0|:1]")
<< " - control port to connect to, optionally client/server "
"(default)\n"
<< SBOLD("\t--blend|-B")
<< " - enable blending from original to newly received stream, "
"increases latency\n"
<< SBOLD("\t--server|-S <port>")
<< " - enable server mode for clients to connect on specified "
"port\n"
<< SBOLD("\t--conference|-r <width>:<height>[:fps]")
<< " - enable combining of multiple inputs, increases latency\n"
<< SBOLD("\t--conference-compression|-R <compression>")
<< " - compression for conference participants\n"
<< SBOLD("\t--capture-filter|-F <cfg_string>")
<< " - apply video capture filter to incoming video\n"
<< SBOLD("\t--param|-O") << " - additional parameters\n"
<< SBOLD("\t--help|-h\n") << SBOLD("\t--verbose|-V\n") << SBOLD("\t-v")
<< " - print version\n";
col() << "and " << SUNDERLINE("hostX_options") << " may be:\n"
<< SBOLD("\t-P [<rx_port>:]<tx_port>")
<< " - TX port to be used (optionally also RX)\n"
<< SBOLD("\t-c <compression>") << " - compression\n"
<< "\tFollowing options will be used only if " << SUNDERLINE("'-c'")
<< " parameter is set:\n"
<< SBOLD("\t-m <mtu>") << " - MTU size\n"
<< SBOLD("\t-l <limiting_bitrate>") << " - bitrate to be shaped to\n"
<< SBOLD("\t-f <fec>")
<< " - FEC that will be used for transmission.\n"
<< SBOLD("\t-4/-6") << " - force IPv4/IPv6\n";
printf("\nPlease note that blending and capture filter is used only "
"for host for which\n"
"compression is specified (transcoding is active). If "
"compression is not\n"
"set, simple packet retransmission is used. Compression can be "
"also 'none'\n"
"for uncompressed transmission (see 'uv -c help' for list).\n");
}
struct host_opts {
char *addr;
int rx_port;
int tx_port;
const char *compression;
char *fec;
long long int bitrate;
struct common_opts common_opts = COMMON_OPTS_INIT;
};
struct cmdline_parameters {
int bufsize;
int port;
vector<struct host_opts> hosts;
int host_count;
int control_port = -1;
int control_connection_type = 0;
int server_port = -1;
struct hd_rum_output_conf out_conf = {NORMAL, NULL};
const char *capture_filter = NULL;
int log_level = -1;
const char *conference_compression = nullptr;
};
/// unit_evaluate() is similar but uses SI prefixes
static int
parse_size(const char *sz_str) noexcept(false)
{
int ret = 0;
size_t end = 0;
try {
ret = stoi(sz_str, &end);
} catch (invalid_argument &e) {
throw ug_runtime_error(string("invalid buffer size: ") + sz_str);
}
if (ret <= 0) {
throw ug_runtime_error(string("size must be positive: ") + sz_str);
}
switch (sz_str[end]) {
case 'K':
case 'k':
ret *= 1024;
break;
case 'M':
case 'm':
ret *= 1024 * 1024;
break;
}
return ret;
}
static int parse_global_opts(int argc, char **argv,
struct cmdline_parameters *parsed) noexcept(false)
{
const struct option getopt_options[] = {
{"blend", no_argument, nullptr, 'B'},
{ "capture-filter", required_argument, nullptr, 'F'},
{ "list-modules", required_argument, nullptr, 'L'},
{ "param", required_argument, nullptr, 'O'},
{ "conference-compression", required_argument, nullptr, 'R'},
{ "server", required_argument, nullptr, 'S'},
{ "verbose", optional_argument, nullptr, 'V'},
{ "capabilities", no_argument, nullptr, 'b'},
{ "help", no_argument, nullptr, 'h'},
{ "control-port", required_argument, nullptr, 'n'},
{ "conference", required_argument, nullptr, 'r'},
{ "version", no_argument, nullptr, 'v'},
{ nullptr, 0, nullptr, 0 }
};
const char *const optstring = "+BF:LO:R:S:Vbhn:r:v";
int ch = 0;
while ((ch = getopt_long(argc, argv, optstring, getopt_options,
nullptr)) != -1) {
switch (ch) {
case 'n':
parsed->control_port = stoi(optarg);
parsed->control_connection_type = 0;
if (strchr(optarg, ':') != nullptr) {
parsed->control_connection_type =
stoi(strchr(optarg, ':') + 1);
}
break;
case 'b':
print_capabilities("");
return 1;
case 'B':
parsed->out_conf.mode = BLEND;
break;
case 'r':
parsed->out_conf.mode = CONFERENCE;
parsed->out_conf.arg = optarg;
break;
case 'R':
parsed->conference_compression = optarg;
break;
case 'S':
parsed->server_port = stoi(optarg);
break;
case 'F':
parsed->capture_filter = optarg;
break;
case 'h':
usage(argv[0]);
return 1;
case 'v':
// nothing needed, version is printed every time
return 1;
case 'V':
break; // already handled in common_preinit()
case 'O':
if (!parse_params(optarg, false)) {
return 1;
}
break;
case 'L':
list_all_modules();
return 1;
case '?':
default:
MSG(FATAL, "Unknown global parameter\n\n");
usage(argv[0]);
return -1;
}
}
return 0;
}
/**
* @todo
* Use rather getopt() than manual parsing.
* @retval -1 failure
* @retval 0 success
* @retval 1 help shown
*/
static int
parse_fmt(int argc, char **argv,
struct cmdline_parameters *parsed) noexcept(false)
{
const int rc = parse_global_opts(argc, argv, parsed);
if (rc != 0) {
return rc;
}
if (optind + 2 > argc) {
MSG(FATAL, "Missing parameter%s port!\n\n",
argc < optind + 1 ? " buffer_size and" : "");
usage(argv[0]);
return -1;
}
parsed->bufsize = parse_size(argv[optind++]);
try {
parsed->port = stoi(argv[optind]);
} catch (invalid_argument &) {
throw ug_runtime_error(string("invalid port number: ") +
argv[optind]);
}
if (parsed->port < 0 || parsed->port >= USHRT_MAX) {
throw ug_runtime_error(string("port must be in range [0..") +
to_string(USHRT_MAX) +
"], given: " + argv[optind]);
}
optind++;
if (parsed->port == 0) {
MSG(WARNING,
"Given RX port 0, reflector will listen on a random port.\n");
}
parsed->host_count = 0;
while (optind < argc) {
parsed->hosts.resize(parsed->host_count + 1);
parsed->hosts[parsed->host_count].bitrate = RATE_UNLIMITED;
const char *const optstring = "+46P:c:f:l:m:";
int ch = 0;
while ((ch = getopt(argc, argv, optstring)) != -1) {
switch (ch) {
case 'P':
if (strchr(optarg, ':') != nullptr) {
parsed->hosts[parsed->host_count].rx_port =
stoi(optarg);
parsed->hosts[parsed->host_count].tx_port =
stoi(strchr(optarg, ':') + 1);
} else {
parsed->hosts[parsed->host_count].tx_port =
stoi(optarg);
}
break;
case 'm':
parsed->hosts[parsed->host_count].common_opts.mtu =
stoi(optarg);
break;
case 'c':
parsed->hosts[parsed->host_count].compression = optarg;
break;
case 'f':
parsed->hosts[parsed->host_count].fec = optarg;
break;
case 'l':
if (!parse_bitrate(
optarg, &parsed->hosts[parsed->host_count]
.bitrate)) {
return -1;
}
break;
case '4':
parsed->hosts[parsed->host_count]
.common_opts.force_ip_version = 4;
break;
case '6':
parsed->hosts[parsed->host_count]
.common_opts.force_ip_version = 6;
break;
default:
MSG(FATAL, "Error: invalid host option.\nn");
return -1;
}
}
if (optind == argc) {
MSG(ERROR,
"Error: host option following last host address!\n");
return -1;
}
parsed->hosts[parsed->host_count++].addr = argv[optind++];
}
return 0;
}
static void hd_rum_translator_deinit(struct hd_rum_translator_state *s) {
if(s->decompress) {
hd_rum_decompress_done(s->decompress);
}
if(s->recompress)
recompress_done(s->recompress);
for (unsigned int i = 0; i < s->replicas.size(); i++) {
delete s->replicas[i];
}
control_done(s->control_state);
qdestroy(s->queue);
}
static bool sockaddr_equal(struct sockaddr_storage *a, struct sockaddr_storage *b){
if(a->ss_family != b->ss_family)
return false;
switch(a->ss_family){
case AF_INET:{
auto a_in = reinterpret_cast<struct sockaddr_in *>(a);
auto b_in = reinterpret_cast<struct sockaddr_in *>(b);
return a_in->sin_port == b_in->sin_port
&& a_in->sin_addr.s_addr == b_in->sin_addr.s_addr;
}
case AF_INET6:{
auto a_in = reinterpret_cast<struct sockaddr_in6 *>(a);
auto b_in = reinterpret_cast<struct sockaddr_in6 *>(b);
return a_in->sin6_port == b_in->sin6_port
&& memcmp(a_in->sin6_addr.s6_addr, b_in->sin6_addr.s6_addr, sizeof(struct in6_addr)) == 0;
}
default:
assert(false && "Comparison not implemented for address type");
abort();
}
}
struct Conf_participant{
struct sockaddr_storage addr;
socklen_t addrlen;
struct timeval last_recv;
};
class Participant_manager{
public:
Participant_manager(module& mod, const char *compress) : mod(mod), compression(compress ? compress : "") { }
~Participant_manager(){
should_stop.store(true, std::memory_order_relaxed);
if(worker_thread.joinable())
worker_thread.join();
}
void tick(sockaddr_storage& sin, socklen_t addrlen){
struct timeval t;
gettimeofday(&t, NULL);
bool seen = false;
for(auto it = participants.begin(); it != participants.end();){
if(addrlen > 0 && sockaddr_equal(&it->addr, &sin)){
it->last_recv = t;
seen = true;
}
const double participant_timeout = 10.0;
if(tv_diff(t, it->last_recv) > participant_timeout){
log_msg(LOG_LEVEL_NOTICE, "Removing participant\n");
std::string msg = "delete-port ";
auto addr = reinterpret_cast<struct sockaddr *>(&it->addr);
char replica_name[ADDR_STR_BUF_LEN];
msg += get_sockaddr_str(addr, sizeof sin, replica_name,
sizeof replica_name);
std::swap(*it, participants.back());
participants.pop_back();
struct msg_universal *m = (struct msg_universal *) new_message(sizeof(struct msg_universal));
strncpy(m->text, msg.c_str(), sizeof(m->text) - 1);
log_msg(LOG_LEVEL_NOTICE, "Msg: %s\n", m->text);
struct response *r = send_message_to_receiver(&mod, (struct message *) m);
if (response_get_status(r) != RESPONSE_ACCEPTED) {
log_msg(LOG_LEVEL_ERROR, "Couldn't remove participant (error %d)!\n", response_get_status(r));
}
free_response(r);
} else {
it++;
}
}
if(!seen && addrlen > 0){
Conf_participant p;
p.addr = sin;
p.addrlen = addrlen;
p.last_recv = t;
log_msg(LOG_LEVEL_NOTICE, "New participant\n");
std::string msg = "create-port ";
char buf[ADDR_STR_BUF_LEN];
msg +=
get_sockaddr_str(reinterpret_cast<sockaddr *>(&sin),
addrlen, buf, sizeof buf);
if(!compression.empty()){
msg += " ";
msg += compression;
}
struct msg_universal *m = (struct msg_universal *) new_message(sizeof(struct msg_universal));
strncpy(m->text, msg.c_str(), sizeof(m->text) - 1);
log_msg(LOG_LEVEL_NOTICE, "Msg: %s\n", m->text);
struct response *r = send_message_to_receiver(&mod, (struct message *) m);
if (response_get_status(r) != RESPONSE_ACCEPTED) {
log_msg(LOG_LEVEL_ERROR, "Cannot add new participant (error %d)!\n", response_get_status(r));
} else{
participants.push_back(p);
}
free_response(r);
}
}
void run_async(std::shared_ptr<socket_udp> server_sock){
sock = std::move(server_sock);
worker_thread = std::thread(&Participant_manager::worker, this);
}
private:
std::vector<Conf_participant> participants;
struct module& mod;
std::string compression;
std::shared_ptr<socket_udp> sock;
std::thread worker_thread;
std::atomic<bool> should_stop = false;
void worker(){
struct timeval timeout = { 1, 0 };
char dummy_buf[256];
while(!should_stop.load(std::memory_order_relaxed)){
struct sockaddr_storage sin = {};
socklen_t addrlen = sizeof(sin);
int size = udp_recvfrom_timeout(sock.get(), dummy_buf, std::size(dummy_buf), &timeout, (sockaddr *) &sin, &addrlen);
if(size > 0)
tick(sin, addrlen);
}
}
};
static void hd_rum_translator_should_exit_callback(void *arg) {
volatile auto *should_exit = (volatile bool *) arg;
*should_exit = true;
}
#define EXIT(retval) \
{ \
exit_uv(0); \
hd_rum_translator_deinit(&state); \
if (sock_in != nullptr) \
udp_exit(sock_in); \
common_cleanup(init); \
return retval; \
}
int main(int argc, char **argv)