forked from Haivision/srt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
srt-live-transmit.cpp
879 lines (784 loc) · 30.7 KB
/
srt-live-transmit.cpp
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
/*
* SRT - Secure, Reliable, Transport
* Copyright (c) 2018 Haivision Systems Inc.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
*/
// NOTE: This application uses C++11.
// This program uses quite a simple architecture, which is mainly related to
// the way how it's invoked: srt-live-transmit <source> <target> (plus options).
//
// The media for <source> and <target> are filled by abstract classes
// named Source and Target respectively. Most important virtuals to
// be filled by the derived classes are Source::Read and Target::Write.
//
// For SRT please take a look at the SrtCommon class first. This contains
// everything that is needed for creating an SRT medium, that is, making
// a connection as listener, as caller, and as rendezvous. The listener
// and caller modes are built upon the same philosophy as those for
// BSD/POSIX socket API (bind/listen/accept or connect).
//
// The instance class is selected per details in the URI (usually scheme)
// and then this URI is used to configure the medium object. Medium-specific
// options are specified in the URI: SCHEME://HOST:PORT?opt1=val1&opt2=val2 etc.
//
// Options for connection are set by ConfigurePre and ConfigurePost.
// This is a philosophy that exists also in BSD/POSIX sockets, just not
// officially mentioned:
// - The "PRE" options must be set prior to connecting and can't be altered
// on a connected socket, however if set on a listening socket, they are
// derived by accept-ed socket.
// - The "POST" options can be altered any time on a connected socket.
// They MAY have also some meaning when set prior to connecting; such
// option is SRTO_RCVSYN, which makes connect/accept call asynchronous.
// Because of that this option is treated special way in this app.
//
// See 'srt_options' global variable (common/socketoptions.hpp) for a list of
// all options.
// MSVS likes to complain about lots of standard C functions being unsafe.
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS 1
#endif
#define REQUIRE_CXX11 1
#include <cctype>
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <set>
#include <vector>
#include <memory>
#include <algorithm>
#include <iterator>
#include <stdexcept>
#include <cstring>
#include <csignal>
#include <chrono>
#include <thread>
#include <list>
#include "apputil.hpp" // CreateAddr
#include "uriparser.hpp" // UriParser
#include "socketoptions.hpp"
#include "logsupport.hpp"
#include "transmitmedia.hpp"
#include "verbose.hpp"
// NOTE: This is without "haisrt/" because it uses an internal path
// to the library. Application using the "installed" library should
// use <srt/srt.h>
#include <srt.h>
#include <udt.h> // This TEMPORARILY contains extra C++-only SRT API.
#include <logging.h>
using namespace std;
struct ForcedExit: public std::runtime_error
{
ForcedExit(const std::string& arg):
std::runtime_error(arg)
{
}
};
struct AlarmExit: public std::runtime_error
{
AlarmExit(const std::string& arg):
std::runtime_error(arg)
{
}
};
srt::sync::atomic<bool> int_state;
srt::sync::atomic<bool> timer_state;
void OnINT_ForceExit(int)
{
Verb() << "\n-------- REQUESTED INTERRUPT!\n";
int_state = true;
}
void OnAlarm_Interrupt(int)
{
Verb() << "\n---------- INTERRUPT ON TIMEOUT!\n";
int_state = false; // JIC
timer_state = true;
if ((false))
{
throw AlarmExit("Watchdog bites hangup");
}
}
extern "C" void TestLogHandler(void* opaque, int level, const char* file, int line, const char* area, const char* message);
struct LiveTransmitConfig
{
int timeout = 0;
int timeout_mode = 0;
int chunk_size = -1;
bool quiet = false;
srt_logging::LogLevel::type loglevel = srt_logging::LogLevel::error;
set<srt_logging::LogFA> logfas;
bool log_internal;
string logfile;
int bw_report = 0;
bool srctime = false;
size_t buffering = 10;
int stats_report = 0;
string stats_out;
SrtStatsPrintFormat stats_pf = SRTSTATS_PROFMAT_2COLS;
bool auto_reconnect = true;
bool full_stats = false;
string source;
string target;
};
void PrintOptionHelp(const OptionName& opt_names, const string &value, const string &desc)
{
cerr << "\t";
int i = 0;
for (auto opt : opt_names.names)
{
if (i++) cerr << ", ";
cerr << "-" << opt;
}
if (!value.empty())
cerr << ":" << value;
cerr << "\t- " << desc << "\n";
}
int parse_args(LiveTransmitConfig &cfg, int argc, char** argv)
{
const OptionName
o_timeout = { "t", "to", "timeout" },
o_timeout_mode = { "tm", "timeout-mode" },
o_autorecon = { "a", "auto", "autoreconnect" },
o_chunk = { "c", "chunk" },
o_bwreport = { "r", "bwreport", "report", "bandwidth-report", "bitrate-report" },
o_srctime = {"st", "srctime", "sourcetime"},
o_buffering = {"buffering"},
o_statsrep = { "s", "stats", "stats-report-frequency" },
o_statsout = { "statsout" },
o_statspf = { "pf", "statspf" },
o_statsfull = { "f", "fullstats" },
o_loglevel = { "ll", "loglevel" },
o_logfa = { "lfa", "logfa" },
o_log_internal = { "loginternal"},
o_logfile = { "logfile" },
o_quiet = { "q", "quiet" },
o_verbose = { "v", "verbose" },
o_help = { "h", "help" },
o_version = { "version" };
const vector<OptionScheme> optargs = {
{ o_timeout, OptionScheme::ARG_ONE },
{ o_timeout_mode, OptionScheme::ARG_ONE },
{ o_autorecon, OptionScheme::ARG_ONE },
{ o_chunk, OptionScheme::ARG_ONE },
{ o_bwreport, OptionScheme::ARG_ONE },
{ o_srctime, OptionScheme::ARG_ONE },
{ o_buffering, OptionScheme::ARG_ONE },
{ o_statsrep, OptionScheme::ARG_ONE },
{ o_statsout, OptionScheme::ARG_ONE },
{ o_statspf, OptionScheme::ARG_ONE },
{ o_statsfull, OptionScheme::ARG_NONE },
{ o_loglevel, OptionScheme::ARG_ONE },
{ o_logfa, OptionScheme::ARG_ONE },
{ o_log_internal, OptionScheme::ARG_NONE },
{ o_logfile, OptionScheme::ARG_ONE },
{ o_quiet, OptionScheme::ARG_NONE },
{ o_verbose, OptionScheme::ARG_NONE },
{ o_help, OptionScheme::ARG_VAR },
{ o_version, OptionScheme::ARG_NONE }
};
options_t params = ProcessOptions(argv, argc, optargs);
bool print_help = OptionPresent(params, o_help);
const bool print_version = OptionPresent(params, o_version);
if (params[""].size() != 2 && !print_help && !print_version)
{
cerr << "ERROR. Invalid syntax. Specify source and target URIs.\n";
if (params[""].size() > 0)
{
cerr << "The following options are passed without a key: ";
copy(params[""].begin(), params[""].end(), ostream_iterator<string>(cerr, ", "));
cerr << endl;
}
print_help = true; // Enable help to print it further
}
if (print_help)
{
string helpspec = Option<OutString>(params, o_help);
if (helpspec == "logging")
{
cerr << "Logging options:\n";
cerr << " -ll <LEVEL> - specify minimum log level\n";
cerr << " -lfa <area...> - specify functional areas\n";
cerr << "Where:\n\n";
cerr << " <LEVEL>: fatal error note warning debug\n\n";
cerr << "Turns on logs that are at the given log level or any higher level\n";
cerr << "(all to the left in the list above from the selected level).\n";
cerr << "Names from syslog, like alert, crit, emerg, err, info, panic, are also\n";
cerr << "recognized, but they are aligned to those that lie close in the above hierarchy.\n\n";
cerr << " <area...> is a coma-separated list of areas to turn on.\n\n";
cerr << "The list may include 'all' to turn all FAs on.\n";
cerr << "Example: `-lfa:sockmgmt,chn-recv` enables only `sockmgmt` and `chn-recv` log FAs.\n";
cerr << "Default: all are on except haicrypt. NOTE: 'general' FA can't be disabled.\n\n";
cerr << "List of functional areas:\n";
map<int, string> revmap;
for (auto entry: SrtLogFAList())
revmap[entry.second] = entry.first;
// Each group on a new line
int en10 = 0;
for (auto entry: revmap)
{
cerr << " " << entry.second;
if (entry.first/10 != en10)
{
cerr << endl;
en10 = entry.first/10;
}
}
cerr << endl;
return 1;
}
cout << "SRT sample application to transmit live streaming.\n";
PrintLibVersion();
cerr << "Usage: srt-live-transmit [options] <input-uri> <output-uri>\n";
cerr << "\n";
#ifndef _WIN32
PrintOptionHelp(o_timeout, "<timeout=0>", "exit timer in seconds");
PrintOptionHelp(o_timeout_mode, "<mode=0>", "timeout mode (0 - since app start; 1 - like 0, but cancel on connect");
#endif
PrintOptionHelp(o_autorecon, "<enabled=yes>", "auto-reconnect mode {yes, no}");
PrintOptionHelp(o_chunk, "<chunk=1456>", "max size of data read in one step, that can fit one SRT packet");
PrintOptionHelp(o_bwreport, "<every_n_packets=0>", "bandwidth report frequency");
PrintOptionHelp(o_srctime, "<enabled=yes>", "Pass packet time from source to SRT output {yes, no}");
PrintOptionHelp(o_buffering, "<packets=n>", "Buffer up to n incoming packets");
PrintOptionHelp(o_statsrep, "<every_n_packets=0>", "frequency of status report");
PrintOptionHelp(o_statsout, "<filename>", "output stats to file");
PrintOptionHelp(o_statspf, "<format=default>", "stats printing format {json, csv, default}");
PrintOptionHelp(o_statsfull, "", "full counters in stats-report (prints total statistics)");
PrintOptionHelp(o_loglevel, "<level=warn>", "log level {fatal,error,warn,note,info,debug}");
PrintOptionHelp(o_logfa, "<fas>", "log functional area (see '-h logging' for more info)");
//PrintOptionHelp(o_log_internal, "", "use internal logger");
PrintOptionHelp(o_logfile, "<filename="">", "write logs to file");
PrintOptionHelp(o_quiet, "", "quiet mode (default off)");
PrintOptionHelp(o_verbose, "", "verbose mode (default off)");
cerr << "\n";
cerr << "\t-h,-help - show this help (use '-h logging' for logging system)\n";
cerr << "\t-version - print SRT library version\n";
cerr << "\n";
cerr << "\t<input-uri> - URI specifying a medium to read from\n";
cerr << "\t<output-uri> - URI specifying a medium to write to\n";
cerr << "URI syntax: SCHEME://HOST:PORT/PATH?PARAM1=VALUE&PARAM2=VALUE...\n";
cerr << "Supported schemes:\n";
cerr << "\tsrt: use HOST, PORT, and PARAM for setting socket options\n";
cerr << "\tudp: use HOST, PORT and PARAM for some UDP specific settings\n";
cerr << "\tfile: only as file://con for using stdin or stdout\n";
return 2;
}
if (print_version)
{
PrintLibVersion();
return 2;
}
cfg.timeout = Option<OutNumber>(params, o_timeout);
cfg.timeout_mode = Option<OutNumber>(params, o_timeout_mode);
cfg.chunk_size = Option<OutNumber>(params, "-1", o_chunk);
cfg.srctime = Option<OutBool>(params, cfg.srctime, o_srctime);
const int buffering = Option<OutNumber>(params, "10", o_buffering);
if (buffering <= 0)
{
cerr << "ERROR: Buffering value should be positive. Value provided: " << buffering << "." << endl;
return 1;
}
else
{
cfg.buffering = (size_t) buffering;
}
cfg.bw_report = Option<OutNumber>(params, o_bwreport);
cfg.stats_report = Option<OutNumber>(params, o_statsrep);
cfg.stats_out = Option<OutString>(params, o_statsout);
const string pf = Option<OutString>(params, "default", o_statspf);
string pfext;
cfg.stats_pf = ParsePrintFormat(pf, (pfext));
if (cfg.stats_pf == SRTSTATS_PROFMAT_INVALID)
{
cfg.stats_pf = SRTSTATS_PROFMAT_2COLS;
cerr << "ERROR: Unsupported print format: " << pf << " -- fallback to default" << endl;
return 1;
}
cfg.full_stats = OptionPresent(params, o_statsfull);
cfg.loglevel = SrtParseLogLevel(Option<OutString>(params, "warn", o_loglevel));
cfg.logfas = SrtParseLogFA(Option<OutString>(params, "", o_logfa));
cfg.log_internal = OptionPresent(params, o_log_internal);
cfg.logfile = Option<OutString>(params, o_logfile);
cfg.quiet = OptionPresent(params, o_quiet);
if (OptionPresent(params, o_verbose))
Verbose::on = !cfg.quiet;
cfg.auto_reconnect = Option<OutBool>(params, true, o_autorecon);
cfg.source = params[""].at(0);
cfg.target = params[""].at(1);
return 0;
}
int main(int argc, char** argv)
{
srt_startup();
// This is mainly required on Windows to initialize the network system,
// for a case when the instance would use UDP. SRT does it on its own, independently.
if (!SysInitializeNetwork())
throw std::runtime_error("Can't initialize network!");
// Symmetrically, this does a cleanup; put into a local destructor to ensure that
// it's called regardless of how this function returns.
struct NetworkCleanup
{
~NetworkCleanup()
{
srt_cleanup();
SysCleanupNetwork();
}
} cleanupobj;
LiveTransmitConfig cfg;
const int parse_ret = parse_args(cfg, argc, argv);
if (parse_ret != 0)
return parse_ret == 1 ? EXIT_FAILURE : 0;
//
// Set global config variables
//
if (cfg.chunk_size > 0)
transmit_chunk_size = cfg.chunk_size;
transmit_stats_writer = SrtStatsWriterFactory(cfg.stats_pf);
transmit_bw_report = cfg.bw_report;
transmit_stats_report = cfg.stats_report;
transmit_total_stats = cfg.full_stats;
//
// Set SRT log levels and functional areas
//
srt_setloglevel(cfg.loglevel);
if (!cfg.logfas.empty())
{
srt_resetlogfa(nullptr, 0);
for (set<srt_logging::LogFA>::iterator i = cfg.logfas.begin(); i != cfg.logfas.end(); ++i)
srt_addlogfa(*i);
}
//
// SRT log handler
//
std::ofstream logfile_stream; // leave unused if not set
char NAME[] = "SRTLIB";
if (cfg.log_internal)
{
srt_setlogflags(0
| SRT_LOGF_DISABLE_TIME
| SRT_LOGF_DISABLE_SEVERITY
| SRT_LOGF_DISABLE_THREADNAME
| SRT_LOGF_DISABLE_EOL
);
srt_setloghandler(NAME, TestLogHandler);
}
else if (!cfg.logfile.empty())
{
logfile_stream.open(cfg.logfile.c_str());
if (!logfile_stream)
{
cerr << "ERROR: Can't open '" << cfg.logfile.c_str() << "' for writing - fallback to cerr\n";
}
else
{
srt::setlogstream(logfile_stream);
}
}
//
// SRT stats output
//
std::ofstream logfile_stats; // leave unused if not set
if (cfg.stats_out != "")
{
logfile_stats.open(cfg.stats_out.c_str());
if (!logfile_stats)
{
cerr << "ERROR: Can't open '" << cfg.stats_out << "' for writing stats. Fallback to stdout.\n";
logfile_stats.close();
}
}
else if (cfg.bw_report != 0 || cfg.stats_report != 0)
{
g_stats_are_printed_to_stdout = true;
}
ostream &out_stats = logfile_stats.is_open() ? logfile_stats : cout;
#ifdef _WIN32
if (cfg.timeout != 0)
{
cerr << "ERROR: The -timeout option (-t) is not implemented on Windows\n";
return EXIT_FAILURE;
}
#else
if (cfg.timeout > 0)
{
signal(SIGALRM, OnAlarm_Interrupt);
if (!cfg.quiet)
cerr << "TIMEOUT: will interrupt after " << cfg.timeout << "s\n";
alarm(cfg.timeout);
}
#endif
signal(SIGINT, OnINT_ForceExit);
signal(SIGTERM, OnINT_ForceExit);
if (!cfg.quiet)
{
cerr << "Media path: '"
<< cfg.source
<< "' --> '"
<< cfg.target
<< "'\n";
}
unique_ptr<Source> src;
bool srcConnected = false;
unique_ptr<Target> tar;
bool tarConnected = false;
int pollid = srt_epoll_create();
if (pollid < 0)
{
cerr << "Can't initialize epoll";
return 1;
}
size_t receivedBytes = 0;
size_t wroteBytes = 0;
size_t lostBytes = 0;
size_t lastReportedtLostBytes = 0;
std::time_t writeErrorLogTimer(std::time(nullptr));
try {
// Now loop until broken
while (!int_state && !timer_state)
{
if (!src.get())
{
src = Source::Create(cfg.source);
if (!src.get())
{
cerr << "Unsupported source type" << endl;
return 1;
}
int events = SRT_EPOLL_IN | SRT_EPOLL_ERR;
switch (src->uri.type())
{
case UriParser::SRT:
if (srt_epoll_add_usock(pollid,
src->GetSRTSocket(), &events))
{
cerr << "Failed to add SRT source to poll, "
<< src->GetSRTSocket() << endl;
return 1;
}
break;
case UriParser::UDP:
if (srt_epoll_add_ssock(pollid,
src->GetSysSocket(), &events))
{
cerr << "Failed to add UDP source to poll, "
<< src->GetSysSocket() << endl;
return 1;
}
break;
case UriParser::FILE:
if (srt_epoll_add_ssock(pollid,
src->GetSysSocket(), &events))
{
cerr << "Failed to add FILE source to poll, "
<< src->GetSysSocket() << endl;
return 1;
}
break;
default:
break;
}
receivedBytes = 0;
}
if (!tar.get())
{
tar = Target::Create(cfg.target);
if (!tar.get())
{
cerr << "Unsupported target type" << endl;
return 1;
}
// IN because we care for state transitions only
// OUT - to check the connection state changes
int events = SRT_EPOLL_IN | SRT_EPOLL_OUT | SRT_EPOLL_ERR;
switch(tar->uri.type())
{
case UriParser::SRT:
if (srt_epoll_add_usock(pollid,
tar->GetSRTSocket(), &events))
{
cerr << "Failed to add SRT destination to poll, "
<< tar->GetSRTSocket() << endl;
return 1;
}
break;
default:
break;
}
wroteBytes = 0;
lostBytes = 0;
lastReportedtLostBytes = 0;
}
int srtrfdslen = 2;
int srtwfdslen = 2;
SRTSOCKET srtrwfds[4] = {SRT_INVALID_SOCK, SRT_INVALID_SOCK , SRT_INVALID_SOCK , SRT_INVALID_SOCK };
int sysrfdslen = 2;
SYSSOCKET sysrfds[2];
if (srt_epoll_wait(pollid,
&srtrwfds[0], &srtrfdslen, &srtrwfds[2], &srtwfdslen,
100,
&sysrfds[0], &sysrfdslen, 0, 0) >= 0)
{
bool doabort = false;
for (size_t i = 0; i < sizeof(srtrwfds) / sizeof(SRTSOCKET); i++)
{
SRTSOCKET s = srtrwfds[i];
if (s == SRT_INVALID_SOCK)
continue;
// Remove duplicated sockets
for (size_t j = i + 1; j < sizeof(srtrwfds) / sizeof(SRTSOCKET); j++)
{
const SRTSOCKET next_s = srtrwfds[j];
if (next_s == s)
srtrwfds[j] = SRT_INVALID_SOCK;
}
bool issource = false;
if (src && src->GetSRTSocket() == s)
{
issource = true;
}
else if (tar && tar->GetSRTSocket() != s)
{
continue;
}
const char * dirstring = (issource) ? "source" : "target";
SRT_SOCKSTATUS status = srt_getsockstate(s);
switch (status)
{
case SRTS_LISTENING:
{
const bool res = (issource) ?
src->AcceptNewClient() : tar->AcceptNewClient();
if (!res)
{
cerr << "Failed to accept SRT connection"
<< endl;
doabort = true;
break;
}
srt_epoll_remove_usock(pollid, s);
SRTSOCKET ns = (issource) ?
src->GetSRTSocket() : tar->GetSRTSocket();
int events = SRT_EPOLL_IN | SRT_EPOLL_ERR;
if (srt_epoll_add_usock(pollid, ns, &events))
{
cerr << "Failed to add SRT client to poll, "
<< ns << endl;
doabort = true;
}
else
{
if (!cfg.quiet)
{
cerr << "Accepted SRT "
<< dirstring
<< " connection"
<< endl;
}
#ifndef _WIN32
if (cfg.timeout_mode == 1 && cfg.timeout > 0)
{
if (!cfg.quiet)
cerr << "TIMEOUT: cancel\n";
alarm(0);
}
#endif
if (issource)
srcConnected = true;
else
tarConnected = true;
}
}
break;
case SRTS_BROKEN:
case SRTS_NONEXIST:
case SRTS_CLOSED:
{
if (issource)
{
if (srcConnected)
{
if (!cfg.quiet)
{
cerr << "SRT source disconnected"
<< endl;
}
srcConnected = false;
}
}
else if (tarConnected)
{
if (!cfg.quiet)
cerr << "SRT target disconnected" << endl;
tarConnected = false;
}
if(!cfg.auto_reconnect)
{
doabort = true;
}
else
{
// force re-connection
srt_epoll_remove_usock(pollid, s);
if (issource)
src.reset();
else
tar.reset();
#ifndef _WIN32
if (cfg.timeout_mode == 1 && cfg.timeout > 0)
{
if (!cfg.quiet)
cerr << "TIMEOUT: will interrupt after " << cfg.timeout << "s\n";
alarm(cfg.timeout);
}
#endif
}
}
break;
case SRTS_CONNECTED:
{
if (issource)
{
if (!srcConnected)
{
if (!cfg.quiet)
cerr << "SRT source connected" << endl;
srcConnected = true;
}
}
else if (!tarConnected)
{
if (!cfg.quiet)
cerr << "SRT target connected" << endl;
tarConnected = true;
if (tar->uri.type() == UriParser::SRT)
{
const int events = SRT_EPOLL_IN | SRT_EPOLL_ERR;
// Disable OUT event polling when connected
if (srt_epoll_update_usock(pollid,
tar->GetSRTSocket(), &events))
{
cerr << "Failed to add SRT destination to poll, "
<< tar->GetSRTSocket() << endl;
return 1;
}
}
#ifndef _WIN32
if (cfg.timeout_mode == 1 && cfg.timeout > 0)
{
if (!cfg.quiet)
cerr << "TIMEOUT: cancel\n";
alarm(0);
}
#endif
}
}
default:
{
// No-Op
}
break;
}
}
if (doabort)
{
break;
}
// read a few chunks at a time in attempt to deplete
// read buffers as much as possible on each read event
// note that this implies live streams and does not
// work for cached/file sources
std::list<std::shared_ptr<MediaPacket>> dataqueue;
if (src.get() && src->IsOpen() && (srtrfdslen || sysrfdslen))
{
while (dataqueue.size() < cfg.buffering)
{
std::shared_ptr<MediaPacket> pkt(new MediaPacket(transmit_chunk_size));
const int res = src->Read(transmit_chunk_size, *pkt, out_stats);
if (res == SRT_ERROR && src->uri.type() == UriParser::SRT)
{
if (srt_getlasterror(NULL) == SRT_EASYNCRCV)
break;
throw std::runtime_error(
string("error: recvmsg: ") + string(srt_getlasterror_str())
);
}
if (res == 0 || pkt->payload.empty())
{
break;
}
dataqueue.push_back(pkt);
receivedBytes += pkt->payload.size();
}
}
// if there is no target, let the received data be lost
while (!dataqueue.empty())
{
std::shared_ptr<MediaPacket> pkt = dataqueue.front();
if (!tar.get() || !tar->IsOpen())
{
lostBytes += pkt->payload.size();
}
else if (!tar->Write(pkt->payload.data(), pkt->payload.size(), cfg.srctime ? pkt->time : 0, out_stats))
{
lostBytes += pkt->payload.size();
}
else
{
wroteBytes += pkt->payload.size();
}
dataqueue.pop_front();
}
if (!cfg.quiet && (lastReportedtLostBytes != lostBytes))
{
std::time_t now(std::time(nullptr));
if (std::difftime(now, writeErrorLogTimer) >= 5.0)
{
cerr << lostBytes << " bytes lost, "
<< wroteBytes << " bytes sent, "
<< receivedBytes << " bytes received"
<< endl;
writeErrorLogTimer = now;
lastReportedtLostBytes = lostBytes;
}
}
}
}
}
catch (std::exception& x)
{
cerr << "ERROR: " << x.what() << endl;
return 255;
}
return 0;
}
// Class utilities
void TestLogHandler(void* opaque, int level, const char* file, int line, const char* area, const char* message)
{
char prefix[100] = "";
if ( opaque ) {
#ifdef _MSC_VER
strncpy_s(prefix, sizeof(prefix), (char*)opaque, _TRUNCATE);
#else
strncpy(prefix, (char*)opaque, sizeof(prefix) - 1);
prefix[sizeof(prefix) - 1] = '\0';
#endif
}
time_t now;
time(&now);
char buf[1024];
struct tm local = SysLocalTime(now);
size_t pos = strftime(buf, 1024, "[%c ", &local);
#ifdef _MSC_VER
// That's something weird that happens on Microsoft Visual Studio 2013
// Trying to keep portability, while every version of MSVS is a different plaform.
// On MSVS 2015 there's already a standard-compliant snprintf, whereas _snprintf
// is available on backward compatibility and it doesn't work exactly the same way.
#define snprintf _snprintf
#endif
snprintf(buf+pos, 1024-pos, "%s:%d(%s)]{%d} %s", file, line, area, level, message);
cerr << buf << endl;
}