| 1 |
/* |
| 2 |
* SRT - Secure, Reliable, Transport |
| 3 |
* Copyright (c) 2018 Haivision Systems Inc. |
| 4 |
* |
| 5 |
* This Source Code Form is subject to the terms of the Mozilla Public |
| 6 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 |
* |
| 9 |
*/ |
| 10 |
|
| 11 |
// NOTE: This application uses C++11. |
| 12 |
|
| 13 |
// This program uses quite a simple architecture, which is mainly related to |
| 14 |
// the way how it's invoked: srt-test-live <source> <target> (plus options). |
| 15 |
// |
| 16 |
// The media for <source> and <target> are filled by abstract classes |
| 17 |
// named Source and Target respectively. Most important virtuals to |
| 18 |
// be filled by the derived classes are Source::Read and Target::Write. |
| 19 |
// |
| 20 |
// For SRT please take a look at the SrtCommon class first. This contains |
| 21 |
// everything that is needed for creating an SRT medium, that is, making |
| 22 |
// a connection as listener, as caller, and as rendezvous. The listener |
| 23 |
// and caller modes are built upon the same philosophy as those for |
| 24 |
// BSD/POSIX socket API (bind/listen/accept or connect). |
| 25 |
// |
| 26 |
// The instance class is selected per details in the URI (usually scheme) |
| 27 |
// and then this URI is used to configure the medium object. Medium-specific |
| 28 |
// options are specified in the URI: SCHEME://HOST:PORT?opt1=val1&opt2=val2 etc. |
| 29 |
// |
| 30 |
// Options for connection are set by ConfigurePre and ConfigurePost. |
| 31 |
// This is a philosophy that exists also in BSD/POSIX sockets, just not |
| 32 |
// officially mentioned: |
| 33 |
// - The "PRE" options must be set prior to connecting and can't be altered |
| 34 |
// on a connected socket, however if set on a listening socket, they are |
| 35 |
// derived by accept-ed socket. |
| 36 |
// - The "POST" options can be altered any time on a connected socket. |
| 37 |
// They MAY have also some meaning when set prior to connecting; such |
| 38 |
// option is SRTO_RCVSYN, which makes connect/accept call asynchronous. |
| 39 |
// Because of that this option is treated special way in this app. |
| 40 |
// |
| 41 |
// See 'srt_options' global variable (common/socketoptions.hpp) for a list of |
| 42 |
// all options. |
| 43 |
|
| 44 |
// MSVS likes to complain about lots of standard C functions being unsafe. |
| 45 |
#ifdef _MSC_VER |
| 46 |
#define _CRT_SECURE_NO_WARNINGS 1 |
| 47 |
#endif |
| 48 |
|
| 49 |
#define REQUIRE_CXX11 1 |
| 50 |
|
| 51 |
#include <cctype> |
| 52 |
#include <iostream> |
| 53 |
#include <fstream> |
| 54 |
#include <string> |
| 55 |
#include <map> |
| 56 |
#include <set> |
| 57 |
#include <vector> |
| 58 |
#include <memory> |
| 59 |
#include <algorithm> |
| 60 |
#include <iterator> |
| 61 |
#include <stdexcept> |
| 62 |
#include <cstring> |
| 63 |
#include <csignal> |
| 64 |
#include <chrono> |
| 65 |
#include <thread> |
| 66 |
|
| 67 |
#include "srt_compat.h" |
| 68 |
#include "apputil.hpp" |
| 69 |
#include "uriparser.hpp" // UriParser |
| 70 |
#include "socketoptions.hpp" |
| 71 |
#include "logsupport.hpp" |
| 72 |
#include "testmedia.hpp" // requires access to SRT-dependent globals |
| 73 |
#include "verbose.hpp" |
| 74 |
|
| 75 |
// NOTE: This is without "haisrt/" because it uses an internal path |
| 76 |
// to the library. Application using the "installed" library should |
| 77 |
// use <srt/srt.h> |
| 78 |
#include <srt.h> |
| 79 |
#include <udt.h> // This TEMPORARILY contains extra C++-only SRT API. |
| 80 |
#include <logging.h> |
| 81 |
|
| 82 |
using namespace std; |
| 83 |
|
| 84 |
srt_logging::Logger applog(SRT_LOGFA_APP, srt_logger_config, "srt-live"); |
| 85 |
|
| 86 |
map<string,string> g_options; |
| 87 |
|
| 88 |
struct ForcedExit: public std::runtime_error |
| 89 |
{ |
| 90 |
ForcedExit(const std::string& arg): |
| 91 |
std::runtime_error(arg) |
| 92 |
{ |
| 93 |
} |
| 94 |
}; |
| 95 |
|
| 96 |
struct AlarmExit: public std::runtime_error |
| 97 |
{ |
| 98 |
AlarmExit(const std::string& arg): |
| 99 |
std::runtime_error(arg) |
| 100 |
{ |
| 101 |
} |
| 102 |
}; |
| 103 |
|
| 104 |
srt::sync::atomic<bool> timer_state; |
| 105 |
void OnINT_ForceExit(int) |
| 106 |
{ |
| 107 |
cerr << "\n-------- REQUESTED INTERRUPT!\n"; |
| 108 |
transmit_int_state = true; |
| 109 |
} |
| 110 |
|
| 111 |
std::string g_interrupt_reason; |
| 112 |
|
| 113 |
void OnAlarm_Interrupt(int) |
| 114 |
{ |
| 115 |
cerr << "\n---------- INTERRUPT ON TIMEOUT: hang on " << g_interrupt_reason << "!\n"; |
| 116 |
transmit_int_state = true; // JIC |
| 117 |
timer_state = true; |
| 118 |
throw AlarmExit("Watchdog bites hangup"); |
| 119 |
} |
| 120 |
|
| 121 |
struct BandwidthGuard |
| 122 |
{ |
| 123 |
typedef std::chrono::steady_clock::time_point time_point; |
| 124 |
size_t conf_bw; |
| 125 |
time_point start_time, prev_time; |
| 126 |
size_t report_count = 0; |
| 127 |
double average_bw = 0; |
| 128 |
size_t transfer_size = 0; |
| 129 |
|
| 130 |
BandwidthGuard(size_t band): conf_bw(band), start_time(std::chrono::steady_clock::now()), prev_time(start_time) {} |
| 131 |
|
| 132 |
void Checkpoint(size_t size, size_t toreport ) |
| 133 |
{ |
| 134 |
using namespace std::chrono; |
| 135 |
|
| 136 |
time_point eop = steady_clock::now(); |
| 137 |
auto dur = duration_cast<microseconds>(eop - start_time); |
| 138 |
//auto this_dur = duration_cast<microseconds>(eop - prev_time); |
| 139 |
|
| 140 |
transfer_size += size; |
| 141 |
average_bw = transfer_size*1000000.0/dur.count(); |
| 142 |
//double this_bw = size*1000000.0/this_dur.count(); |
| 143 |
|
| 144 |
if ( toreport ) |
| 145 |
{ |
| 146 |
// Show current bandwidth |
| 147 |
++report_count; |
| 148 |
if ( report_count % toreport == toreport - 1 ) |
| 149 |
{ |
| 150 |
cout.precision(10); |
| 151 |
int abw = int(average_bw); |
| 152 |
int abw_trunc = abw/1024; |
| 153 |
int abw_frac = abw%1024; |
| 154 |
char bufbw[64]; |
| 155 |
sprintf(bufbw, "%d.%03d", abw_trunc, abw_frac); |
| 156 |
cout << "+++/+++SRT TRANSFER: " << transfer_size << "B " |
| 157 |
"DURATION: " << duration_cast<milliseconds>(dur).count() << "ms SPEED: " << bufbw << "kB/s\n"; |
| 158 |
} |
| 159 |
} |
| 160 |
|
| 161 |
prev_time = eop; |
| 162 |
|
| 163 |
if ( transfer_size > SIZE_MAX/2 ) |
| 164 |
{ |
| 165 |
transfer_size -= SIZE_MAX/2; |
| 166 |
start_time = eop; |
| 167 |
} |
| 168 |
|
| 169 |
if ( conf_bw == 0 ) |
| 170 |
return; // don't guard anything |
| 171 |
|
| 172 |
// Calculate expected duration for the given size of bytes (in [ms]) |
| 173 |
double expdur_ms = double(transfer_size)/conf_bw*1000; |
| 174 |
|
| 175 |
auto expdur = milliseconds(size_t(expdur_ms)); |
| 176 |
// Now compare which is more |
| 177 |
|
| 178 |
if ( dur >= expdur ) // too slow, but there's nothing we can do. Exit now. |
| 179 |
return; |
| 180 |
|
| 181 |
std::this_thread::sleep_for(expdur-dur); |
| 182 |
} |
| 183 |
}; |
| 184 |
|
| 185 |
bool CheckMediaSpec(const string& prefix, const vector<string>& spec, string& w_outspec) |
| 186 |
{ |
| 187 |
// This function prints error messages by itself then returns false. |
| 188 |
// Otherwise nothing is printed and true is returned. |
| 189 |
// r_outspec is for a case when a redundancy specification should be translated. |
| 190 |
|
| 191 |
if (spec.empty()) |
| 192 |
{ |
| 193 |
cerr << prefix << ": Specification is empty\n"; |
| 194 |
return false; |
| 195 |
} |
| 196 |
|
| 197 |
if (spec.size() == 1) |
| 198 |
{ |
| 199 |
// Then, whatever. |
| 200 |
w_outspec = spec[0]; |
| 201 |
return true; |
| 202 |
} |
| 203 |
|
| 204 |
// We have multiple items specified, check each one |
| 205 |
// it SRT, if so, craft the redundancy URI spec, |
| 206 |
// otherwise reject |
| 207 |
vector<string> adrs; |
| 208 |
map<string,string> uriparam; |
| 209 |
bool first = true; |
| 210 |
bool allow_raw_spec = false; |
| 211 |
for (auto uris: spec) |
| 212 |
{ |
| 213 |
UriParser uri(uris, UriParser::EXPECT_HOST); |
| 214 |
if (!allow_raw_spec && uri.type() != UriParser::SRT) |
| 215 |
{ |
| 216 |
cerr << ": Multiple media must be all with SRT scheme, or srt://* as first.\n"; |
| 217 |
return false; |
| 218 |
} |
| 219 |
|
| 220 |
if (uri.host() == "*") |
| 221 |
{ |
| 222 |
allow_raw_spec = true; |
| 223 |
first = false; |
| 224 |
uriparam = uri.parameters(); |
| 225 |
|
| 226 |
// This does not specify the address, only options and URI. |
| 227 |
continue; |
| 228 |
} |
| 229 |
|
| 230 |
string aspec = uri.host() + ":" + uri.port(); |
| 231 |
if (aspec[0] == ':' || aspec[aspec.size()-1] == ':') |
| 232 |
{ |
| 233 |
cerr << "Empty host or port in the address specification: " << uris << endl; |
| 234 |
return false; |
| 235 |
} |
| 236 |
|
| 237 |
if (allow_raw_spec && !uri.parameters().empty()) |
| 238 |
{ |
| 239 |
bool cont = false; |
| 240 |
// Extract attributes if any and pass them there. |
| 241 |
for (UriParser::query_it i = uri.parameters().begin(); |
| 242 |
i != uri.parameters().end(); ++i) |
| 243 |
{ |
| 244 |
aspec += cont ? "&" : "?"; |
| 245 |
cont = false; |
| 246 |
aspec += i->first + "=" + i->second; |
| 247 |
} |
| 248 |
} |
| 249 |
|
| 250 |
adrs.push_back(aspec); |
| 251 |
if (first) |
| 252 |
{ |
| 253 |
uriparam = uri.parameters(); |
| 254 |
first = false; |
| 255 |
} |
| 256 |
} |
| 257 |
|
| 258 |
w_outspec = "srt:////group?"; |
| 259 |
if (map_getp(uriparam, "type") == nullptr) |
| 260 |
uriparam["type"] = "redundancy"; |
| 261 |
|
| 262 |
for (auto& name_value: uriparam) |
| 263 |
{ |
| 264 |
string name, value; tie(name, value) = name_value; |
| 265 |
w_outspec += name + "=" + value + "&"; |
| 266 |
} |
| 267 |
w_outspec += "nodes="; |
| 268 |
for (string& a: adrs) |
| 269 |
w_outspec += a + ","; |
| 270 |
|
| 271 |
Verb("NOTE: ", prefix, " specification set as: ", (w_outspec)); |
| 272 |
|
| 273 |
return true; |
| 274 |
} |
| 275 |
|
| 276 |
extern "C" void TestLogHandler(void* opaque, int level, const char* file, int line, const char* area, const char* message); |
| 277 |
|
| 278 |
namespace srt_logging |
| 279 |
{ |
| 280 |
extern Logger glog; |
| 281 |
} |
| 282 |
|
| 283 |
#if ENABLE_BONDING |
| 284 |
extern "C" int SrtCheckGroupHook(void* , SRTSOCKET acpsock, int , const sockaddr*, const char* ) |
| 285 |
{ |
| 286 |
static string gtypes[] = { |
| 287 |
"undefined", // SRT_GTYPE_UNDEFINED |
| 288 |
"broadcast", |
| 289 |
"backup", |
| 290 |
"balancing", |
| 291 |
"multicast" |
| 292 |
}; |
| 293 |
|
| 294 |
int type; |
| 295 |
int size = sizeof type; |
| 296 |
srt_getsockflag(acpsock, SRTO_GROUPCONNECT, &type, &size); |
| 297 |
Verb("listener: @", acpsock, " - accepting ", (type ? "GROUP" : "SINGLE"), VerbNoEOL); |
| 298 |
if (type != 0) |
| 299 |
{ |
| 300 |
SRT_GROUP_TYPE gt; |
| 301 |
size = sizeof gt; |
| 302 |
if (-1 != srt_getsockflag(acpsock, SRTO_GROUPTYPE, >, &size)) |
| 303 |
{ |
| 304 |
if (size_t(gt) < Size(gtypes)) |
| 305 |
Verb(" type=", gtypes[gt], VerbNoEOL); |
| 306 |
else |
| 307 |
Verb(" type=", int(gt), VerbNoEOL); |
| 308 |
} |
| 309 |
} |
| 310 |
Verb(" connection"); |
| 311 |
|
| 312 |
return 0; |
| 313 |
} |
| 314 |
#endif |
| 315 |
|
| 316 |
extern "C" int SrtUserPasswordHook(void* , SRTSOCKET acpsock, int hsv, const sockaddr*, const char* streamid) |
| 317 |
{ |
| 318 |
if (hsv < 5) |
| 319 |
{ |
| 320 |
Verb("SrtUserPasswordHook: HS version 4 doesn't support extended handshake"); |
| 321 |
return -1; |
| 322 |
} |
| 323 |
|
| 324 |
static const map<string, string> passwd { |
| 325 |
{"admin", "thelocalmanager"}, |
| 326 |
{"user", "verylongpassword"} |
| 327 |
}; |
| 328 |
|
| 329 |
// Try the "standard interpretation" with username at key u |
| 330 |
string username; |
| 331 |
|
| 332 |
static const char stdhdr [] = "#!::"; |
| 333 |
uint32_t* pattern = (uint32_t*)stdhdr; |
| 334 |
|
| 335 |
if (strlen(streamid) > 4 && *(uint32_t*)streamid == *pattern) |
| 336 |
{ |
| 337 |
vector<string> items; |
| 338 |
Split(streamid+4, ',', back_inserter(items)); |
| 339 |
for (auto& i: items) |
| 340 |
{ |
| 341 |
vector<string> kv; |
| 342 |
Split(i, '=', back_inserter(kv)); |
| 343 |
if (kv.size() == 2 && kv[0] == "u") |
| 344 |
{ |
| 345 |
username = kv[1]; |
| 346 |
} |
| 347 |
} |
| 348 |
} |
| 349 |
else |
| 350 |
{ |
| 351 |
// By default the whole streamid is username |
| 352 |
username = streamid; |
| 353 |
} |
| 354 |
|
| 355 |
// This hook sets the password to the just accepted socket |
| 356 |
// depending on the user |
| 357 |
|
| 358 |
string exp_pw = passwd.at(username); |
| 359 |
|
| 360 |
srt_setsockflag(acpsock, SRTO_PASSPHRASE, exp_pw.c_str(), int(exp_pw.size())); |
| 361 |
|
| 362 |
return 0; |
| 363 |
} |
| 364 |
|
| 365 |
struct RejectData |
| 366 |
{ |
| 367 |
int code; |
| 368 |
string streaminfo; |
| 369 |
} g_reject_data; |
| 370 |
|
| 371 |
extern "C" int SrtRejectByCodeHook(void* op, SRTSOCKET acpsock, int , const sockaddr*, const char* ) |
| 372 |
{ |
| 373 |
RejectData* data = (RejectData*)op; |
| 374 |
|
| 375 |
srt_setrejectreason(acpsock, data->code); |
| 376 |
srt::setstreamid(acpsock, data->streaminfo); |
| 377 |
|
| 378 |
return -1; |
| 379 |
} |
| 380 |
|
| 381 |
int main( int argc, char** argv ) |
| 382 |
{ |
| 383 |
// This is mainly required on Windows to initialize the network system, |
| 384 |
// for a case when the instance would use UDP. SRT does it on its own, independently. |
| 385 |
if ( !SysInitializeNetwork() ) |
| 386 |
throw std::runtime_error("Can't initialize network!"); |
| 387 |
|
| 388 |
srt_startup(); |
| 389 |
|
| 390 |
// Symmetrically, this does a cleanup; put into a local destructor to ensure that |
| 391 |
// it's called regardless of how this function returns. |
| 392 |
struct NetworkCleanup |
| 393 |
{ |
| 394 |
~NetworkCleanup() |
| 395 |
{ |
| 396 |
SysCleanupNetwork(); |
| 397 |
srt_cleanup(); |
| 398 |
} |
| 399 |
} cleanupobj; |
| 400 |
|
| 401 |
vector<OptionScheme> optargs; |
| 402 |
|
| 403 |
OptionName |
| 404 |
o_timeout ((optargs), "<timeout[s]=0> Data transmission timeout", "t", "to", "timeout" ), |
| 405 |
o_chunk ((optargs), "<chunk=1316> Single reading operation buffer size", "c", "chunk"), |
| 406 |
o_bandwidth ((optargs), "<bw[ms]=0[unlimited]> Input reading speed limit", "b", "bandwidth", "bitrate"), |
| 407 |
o_report ((optargs), "<frequency[1/pkt]=0> Print bandwidth report periodically", "r", "bandwidth-report", "bitrate-report"), |
| 408 |
o_verbose ((optargs), "[channel=0|1|./file] Print size of every packet transferred on stdout or specified [channel]", "v", "verbose"), |
| 409 |
o_crash ((optargs), " Core-dump when connection got broken by whatever reason (developer mode)", "k", "crash"), |
| 410 |
o_loglevel ((optargs), "<severity> Minimum severity for logs (see --help logging)", "ll", "loglevel"), |
| 411 |
o_logfa ((optargs), "<FA=FA-list...> Enabled Functional Areas (see --help logging)", "lfa", "logfa"), |
| 412 |
o_logfile ((optargs), "<filepath> File to send logs to", "lf", "logfile"), |
| 413 |
o_stats ((optargs), "<freq[npkt]> How often stats should be reported", "s", "stats", "stats-report-frequency"), |
| 414 |
o_statspf ((optargs), "<format=default|csv|json> Format for printing statistics", "pf", "statspf", "statspformat"), |
| 415 |
o_logint ((optargs), " Use internal function for receiving logs (for testing)", "loginternal"), |
| 416 |
o_skipflush ((optargs), " Do not wait safely 5 seconds at the end to flush buffers", "sf", "skipflush"), |
| 417 |
o_stoptime ((optargs), "<time[s]=0[no timeout]> Time after which the application gets interrupted", "d", "stoptime"), |
| 418 |
o_hook ((optargs), "<hookspec> Use listener callback of given specification (internally coded)", "hook"), |
| 419 |
#if ENABLE_BONDING |
| 420 |
o_group ((optargs), "<URIs...> Using multiple SRT connections as redundancy group", "g"), |
| 421 |
#endif |
| 422 |
o_stime ((optargs), " Pass source time explicitly to SRT output", "st", "srctime", "sourcetime"), |
| 423 |
o_retry ((optargs), "<N=-1,0,+N> Retry connection N times if failed on timeout", "rc", "retry"), |
| 424 |
o_help ((optargs), "[special=logging] This help", "?", "help", "-help") |
| 425 |
; |
| 426 |
|
| 427 |
options_t params = ProcessOptions(argv, argc, optargs); |
| 428 |
|
| 429 |
bool need_help = OptionPresent(params, o_help); |
| 430 |
|
| 431 |
vector<string> args = params[""]; |
| 432 |
|
| 433 |
string source_spec, target_spec; |
| 434 |
#if ENABLE_BONDING |
| 435 |
vector<string> groupspec = Option<OutList>(params, vector<string>{}, o_group); |
| 436 |
#endif |
| 437 |
vector<string> source_items, target_items; |
| 438 |
|
| 439 |
if (!need_help) |
| 440 |
{ |
| 441 |
// You may still need help. |
| 442 |
|
| 443 |
#if ENABLE_BONDING |
| 444 |
if ( !groupspec.empty() ) |
| 445 |
{ |
| 446 |
// Check if you have something before -g and after -g. |
| 447 |
if (args.empty()) |
| 448 |
{ |
| 449 |
// Then all items are sources, but the last one is a single target. |
| 450 |
if (groupspec.size() < 3) |
| 451 |
{ |
| 452 |
cerr << "ERROR: Redundancy group: with nothing preceding -g, use -g <SRC-URI1> <SRC-URI2>... <TAR-URI> (at least 3 args)\n"; |
| 453 |
need_help = true; |
| 454 |
} |
| 455 |
else |
| 456 |
{ |
| 457 |
copy(groupspec.begin(), groupspec.end()-1, back_inserter(source_items)); |
| 458 |
target_items.push_back(*(groupspec.end()-1)); |
| 459 |
} |
| 460 |
} |
| 461 |
else |
| 462 |
{ |
| 463 |
// Something before g, something after g. This time -g can accept also one argument. |
| 464 |
copy(args.begin(), args.end(), back_inserter(source_items)); |
| 465 |
copy(groupspec.begin(), groupspec.end(), back_inserter(target_items)); |
| 466 |
} |
| 467 |
} |
| 468 |
else |
| 469 |
#endif |
| 470 |
{ |
| 471 |
if (args.size() < 2) |
| 472 |
{ |
| 473 |
cerr << "ERROR: source and target URI must be specified.\n\n"; |
| 474 |
need_help = true; |
| 475 |
} |
| 476 |
else |
| 477 |
{ |
| 478 |
source_items.push_back(args[0]); |
| 479 |
target_items.push_back(args[1]); |
| 480 |
} |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
// Check verbose option before extracting the argument so that Verb()s |
| 485 |
// can be displayed also when they report something about option parsing. |
| 486 |
string verbose_val = Option<OutString>(params, "no", o_verbose); |
| 487 |
|
| 488 |
unique_ptr<ofstream> pout_verb; |
| 489 |
|
| 490 |
int verbch = 1; // default cerr |
| 491 |
if (verbose_val != "no") |
| 492 |
{ |
| 493 |
Verbose::on = true; |
| 494 |
if (verbose_val == "") |
| 495 |
verbch = 1; |
| 496 |
else if (verbose_val.substr(0, 2) == "./") |
| 497 |
verbch = 3; |
| 498 |
else try |
| 499 |
{ |
| 500 |
verbch = stoi(verbose_val); |
| 501 |
} |
| 502 |
catch (...) |
| 503 |
{ |
| 504 |
verbch = 1; |
| 505 |
} |
| 506 |
|
| 507 |
if (verbch == 1) |
| 508 |
{ |
| 509 |
Verbose::cverb = &std::cout; |
| 510 |
} |
| 511 |
else if (verbch == 2) |
| 512 |
{ |
| 513 |
Verbose::cverb = &std::cerr; |
| 514 |
} |
| 515 |
else if (verbch == 3) |
| 516 |
{ |
| 517 |
pout_verb.reset(new ofstream(verbose_val.substr(2), ios::out | ios::trunc)); |
| 518 |
if (!pout_verb->good()) |
| 519 |
{ |
| 520 |
cerr << "-v: error opening verbose output file: " << verbose_val << endl; |
| 521 |
return 1; |
| 522 |
} |
| 523 |
Verbose::cverb = pout_verb.get(); |
| 524 |
} |
| 525 |
else |
| 526 |
{ |
| 527 |
cerr << "-v or -v:1 (default) or -v:2 only allowed\n"; |
| 528 |
return 1; |
| 529 |
} |
| 530 |
} |
| 531 |
|
| 532 |
|
| 533 |
if (!need_help) |
| 534 |
{ |
| 535 |
// Redundancy is then simply recognized by the fact that there are |
| 536 |
// multiple specified inputs or outputs, for SRT caller only. Check |
| 537 |
// every URI in advance. |
| 538 |
if (!CheckMediaSpec("INPUT", source_items, (source_spec))) |
| 539 |
need_help = true; |
| 540 |
if (!CheckMediaSpec("OUTPUT", target_items, (target_spec))) |
| 541 |
need_help = true; |
| 542 |
} |
| 543 |
|
| 544 |
if (need_help) |
| 545 |
{ |
| 546 |
string helpspec = Option<OutString>(params, o_help); |
| 547 |
if (helpspec == "logging") |
| 548 |
{ |
| 549 |
cerr << "Logging options:\n"; |
| 550 |
cerr << " -ll <LEVEL> - specify minimum log level\n"; |
| 551 |
cerr << " -lfa <area...> - specify functional areas\n"; |
| 552 |
cerr << "Where:\n\n"; |
| 553 |
cerr << " <LEVEL>: fatal error note warning debug\n\n"; |
| 554 |
cerr << "This turns on logs that are at the given log name and all on the left.\n"; |
| 555 |
cerr << "(Names from syslog, like alert, crit, emerg, err, info, panic, are also\n"; |
| 556 |
cerr << "recognized, but they are aligned to those that lie close in hierarchy.)\n\n"; |
| 557 |
cerr << " <area...> is a space-sep list of areas to turn on or ~areas to turn off.\n\n"; |
| 558 |
cerr << "The list may include 'all' to turn all on or off, beside those selected.\n"; |
| 559 |
cerr << "Example: `-lfa ~all cc` - turns off all FA, except cc\n"; |
| 560 |
cerr << "Default: all are on except haicrypt. NOTE: 'general' can't be off.\n\n"; |
| 561 |
cerr << "List of functional areas:\n"; |
| 562 |
|
| 563 |
map<int, string> revmap; |
| 564 |
for (auto entry: SrtLogFAList()) |
| 565 |
revmap[entry.second] = entry.first; |
| 566 |
|
| 567 |
int en10 = 0; |
| 568 |
for (auto entry: revmap) |
| 569 |
{ |
| 570 |
cerr << " " << entry.second; |
| 571 |
if (entry.first/10 != en10) |
| 572 |
{ |
| 573 |
cerr << endl; |
| 574 |
en10 = entry.first/10; |
| 575 |
} |
| 576 |
} |
| 577 |
cerr << endl; |
| 578 |
|
| 579 |
return 1; |
| 580 |
} |
| 581 |
|
| 582 |
// Unrecognized helpspec is same as no helpspec, that is, general help. |
| 583 |
cerr << "Usage:\n"; |
| 584 |
cerr << " (1) " << argv[0] << " [options] <input> <output>\n"; |
| 585 |
cerr << " (2) " << argv[0] << " <inputs...> -g <outputs...> [options]\n"; |
| 586 |
cerr << "*** (Position of [options] is unrestricted.)\n"; |
| 587 |
cerr << "*** (<variadic...> option parameters can be only terminated by a next option.)\n"; |
| 588 |
cerr << "where:\n"; |
| 589 |
cerr << " (1) Exactly one input and one output URI spec is required,\n"; |
| 590 |
cerr << " (2) Multiple SRT inputs or output as redundant links are allowed.\n"; |
| 591 |
cerr << " `URI1 URI2 -g URI3` uses 1, 2 input and 3 output\n"; |
| 592 |
cerr << " `-g URI1 URI2 URI3` like above\n"; |
| 593 |
cerr << " `URI1 -g URI2 URI3` uses 1 input and 2, 3 output\n"; |
| 594 |
cerr << "SUPPORTED URI SCHEMES:\n"; |
| 595 |
cerr << " srt: use SRT connection\n"; |
| 596 |
cerr << " udp: read from bound UDP socket or send to given address as UDP\n"; |
| 597 |
cerr << " file (default if scheme not specified) specified as:\n"; |
| 598 |
cerr << " - empty host/port and absolute file path in the URI\n"; |
| 599 |
cerr << " - only a filename, also as a relative path\n"; |
| 600 |
cerr << " - file://con ('con' as host): designates stdin or stdout\n"; |
| 601 |
cerr << "OPTIONS HELP SYNTAX: -option <parameter[unit]=default[meaning]>:\n"; |
| 602 |
for (auto os: optargs) |
| 603 |
cout << OptionHelpItem(*os.pid) << endl; |
| 604 |
return 1; |
| 605 |
} |
| 606 |
|
| 607 |
int timeout = Option<OutNumber>(params, "30", o_timeout); |
| 608 |
size_t chunk = Option<OutNumber>(params, "0", o_chunk); |
| 609 |
if ( chunk == 0 ) |
| 610 |
{ |
| 611 |
chunk = SRT_LIVE_DEF_PLSIZE; |
| 612 |
} |
| 613 |
else |
| 614 |
{ |
| 615 |
transmit_chunk_size = chunk; |
| 616 |
} |
| 617 |
|
| 618 |
transmit_use_sourcetime = OptionPresent(params, o_stime); |
| 619 |
size_t bandwidth = Option<OutNumber>(params, "0", o_bandwidth); |
| 620 |
transmit_bw_report = Option<OutNumber>(params, "0", o_report); |
| 621 |
bool crashonx = OptionPresent(params, o_crash); |
| 622 |
|
| 623 |
string loglevel = Option<OutString>(params, "error", o_loglevel); |
| 624 |
vector<string> logfa = Option<OutList>(params, o_logfa); |
| 625 |
string logfile = Option<OutString>(params, "", o_logfile); |
| 626 |
transmit_stats_report = Option<OutNumber>(params, "0", o_stats); |
| 627 |
|
| 628 |
bool internal_log = OptionPresent(params, o_logint); |
| 629 |
bool skip_flushing = OptionPresent(params, o_skipflush); |
| 630 |
|
| 631 |
string hook = Option<OutString>(params, "", o_hook); |
| 632 |
if (hook != "") |
| 633 |
{ |
| 634 |
vector<string> hargs; |
| 635 |
Split(hook, ':', back_inserter(hargs)); |
| 636 |
|
| 637 |
if (hargs[0] == "user-password") |
| 638 |
{ |
| 639 |
transmit_accept_hook_fn = &SrtUserPasswordHook; |
| 640 |
transmit_accept_hook_op = nullptr; |
| 641 |
} |
| 642 |
else if (hargs[0] == "reject") |
| 643 |
{ |
| 644 |
hargs.resize(3); // make sure 3 elements exist, may be empty |
| 645 |
g_reject_data.code = stoi(hargs[1]); |
| 646 |
g_reject_data.streaminfo = hargs[2]; |
| 647 |
transmit_accept_hook_op = (void*)&g_reject_data; |
| 648 |
transmit_accept_hook_fn = &SrtRejectByCodeHook; |
| 649 |
} |
| 650 |
#if ENABLE_BONDING |
| 651 |
else if (hargs[0] == "groupcheck") |
| 652 |
{ |
| 653 |
transmit_accept_hook_fn = &SrtCheckGroupHook; |
| 654 |
transmit_accept_hook_op = nullptr; |
| 655 |
} |
| 656 |
#endif |
| 657 |
} |
| 658 |
|
| 659 |
string pfextra; |
| 660 |
SrtStatsPrintFormat statspf = ParsePrintFormat(Option<OutString>(params, "default", o_statspf), (pfextra)); |
| 661 |
if (statspf == SRTSTATS_PROFMAT_INVALID) |
| 662 |
{ |
| 663 |
cerr << "Invalid stats print format\n"; |
| 664 |
return 1; |
| 665 |
} |
| 666 |
transmit_stats_writer = SrtStatsWriterFactory(statspf); |
| 667 |
if (pfextra != "") |
| 668 |
{ |
| 669 |
vector<string> options; |
| 670 |
Split(pfextra, ',', back_inserter(options)); |
| 671 |
for (auto& i: options) |
| 672 |
{ |
| 673 |
vector<string> klv; |
| 674 |
Split(i, '=', back_inserter(klv)); |
| 675 |
klv.resize(2); |
| 676 |
transmit_stats_writer->Option(klv[0], klv[1]); |
| 677 |
} |
| 678 |
} |
| 679 |
|
| 680 |
// Options that require integer conversion |
| 681 |
size_t stoptime = Option<OutNumber>(params, "0", o_stoptime); |
| 682 |
std::ofstream logfile_stream; // leave unused if not set |
| 683 |
|
| 684 |
srt_setloglevel(SrtParseLogLevel(loglevel)); |
| 685 |
string logfa_on, logfa_off; |
| 686 |
ParseLogFASpec(logfa, (logfa_on), (logfa_off)); |
| 687 |
|
| 688 |
set<srt_logging::LogFA> fasoff = SrtParseLogFA(logfa_off); |
| 689 |
set<srt_logging::LogFA> fason = SrtParseLogFA(logfa_on); |
| 690 |
|
| 691 |
auto fa_del = [fasoff]() { |
| 692 |
for (set<srt_logging::LogFA>::iterator i = fasoff.begin(); i != fasoff.end(); ++i) |
| 693 |
srt_dellogfa(*i); |
| 694 |
}; |
| 695 |
|
| 696 |
auto fa_add = [fason]() { |
| 697 |
for (set<srt_logging::LogFA>::iterator i = fason.begin(); i != fason.end(); ++i) |
| 698 |
srt_addlogfa(*i); |
| 699 |
}; |
| 700 |
|
| 701 |
if (logfa_off == "all") |
| 702 |
{ |
| 703 |
// If the spec is: |
| 704 |
// -lfa ~all control app |
| 705 |
// then we first delete all, then enable given ones |
| 706 |
fa_del(); |
| 707 |
fa_add(); |
| 708 |
} |
| 709 |
else |
| 710 |
{ |
| 711 |
// Otherwise we first add all those that have to be added, |
| 712 |
// then delete those unwanted. This embraces both |
| 713 |
// -lfa control app ~cc |
| 714 |
// and |
| 715 |
// -lfa all ~cc |
| 716 |
fa_add(); |
| 717 |
fa_del(); |
| 718 |
} |
| 719 |
|
| 720 |
|
| 721 |
srt::addlogfa(SRT_LOGFA_APP); |
| 722 |
|
| 723 |
char NAME[] = "SRTLIB"; |
| 724 |
if ( internal_log ) |
| 725 |
{ |
| 726 |
srt_setlogflags( 0 |
| 727 |
| SRT_LOGF_DISABLE_TIME |
| 728 |
| SRT_LOGF_DISABLE_SEVERITY |
| 729 |
| SRT_LOGF_DISABLE_THREADNAME |
| 730 |
| SRT_LOGF_DISABLE_EOL |
| 731 |
); |
| 732 |
srt_setloghandler(NAME, TestLogHandler); |
| 733 |
} |
| 734 |
else if ( logfile != "" ) |
| 735 |
{ |
| 736 |
logfile_stream.open(logfile.c_str()); |
| 737 |
if ( !logfile_stream ) |
| 738 |
{ |
| 739 |
cerr << "ERROR: Can't open '" << logfile << "' for writing - fallback to cerr\n"; |
| 740 |
} |
| 741 |
else |
| 742 |
{ |
| 743 |
srt::setlogstream(logfile_stream); |
| 744 |
} |
| 745 |
} |
| 746 |
|
| 747 |
string retryphrase = Option<OutString>(params, "", o_retry); |
| 748 |
if (retryphrase != "") |
| 749 |
{ |
| 750 |
if (retryphrase[retryphrase.size()-1] == 'a') |
| 751 |
{ |
| 752 |
transmit_retry_always = true; |
| 753 |
retryphrase = retryphrase.substr(0, retryphrase.size()-1); |
| 754 |
} |
| 755 |
|
| 756 |
transmit_retry_connect = stoi(retryphrase); |
| 757 |
} |
| 758 |
|
| 759 |
#ifdef _WIN32 |
| 760 |
#define alarm(argument) (void)0 |
| 761 |
|
| 762 |
if (stoptime != 0) |
| 763 |
{ |
| 764 |
cerr << "ERROR: The -stoptime option (-d) is not implemented on Windows\n"; |
| 765 |
return 1; |
| 766 |
} |
| 767 |
|
| 768 |
#else |
| 769 |
signal(SIGALRM, OnAlarm_Interrupt); |
| 770 |
#endif |
| 771 |
signal(SIGINT, OnINT_ForceExit); |
| 772 |
signal(SIGTERM, OnINT_ForceExit); |
| 773 |
|
| 774 |
time_t start_time { time(0) }; |
| 775 |
time_t end_time { -1 }; |
| 776 |
|
| 777 |
if (stoptime != 0) |
| 778 |
{ |
| 779 |
if (stoptime < 10) |
| 780 |
{ |
| 781 |
cerr << "ERROR: -stoptime (-d) must be at least 10 seconds\n"; |
| 782 |
return 1; |
| 783 |
} |
| 784 |
alarm(stoptime); |
| 785 |
cerr << "STOPTIME: will interrupt after " << stoptime << "s\n"; |
| 786 |
if (timeout != 30) |
| 787 |
{ |
| 788 |
cerr << "WARNING: -timeout (-t) option ignored due to specified -stoptime (-d)\n"; |
| 789 |
} |
| 790 |
} |
| 791 |
|
| 792 |
// XXX This could be also controlled by an option. |
| 793 |
int final_delay = 5; |
| 794 |
|
| 795 |
// In the beginning, set Alarm |
| 796 |
|
| 797 |
unique_ptr<Source> src; |
| 798 |
unique_ptr<Target> tar; |
| 799 |
|
| 800 |
try |
| 801 |
{ |
| 802 |
src = Source::Create(source_spec); |
| 803 |
tar = Target::Create(target_spec); |
| 804 |
} |
| 805 |
catch(std::exception& x) |
| 806 |
{ |
| 807 |
if (::transmit_int_state) |
| 808 |
{ |
| 809 |
// The application was terminated by SIGINT or SIGTERM. |
| 810 |
// Don't print anything, just exit gently like ffmpeg. |
| 811 |
cerr << "Exit on request.\n"; |
| 812 |
return 255; |
| 813 |
} |
| 814 |
|
| 815 |
if (stoptime != 0 && ::timer_state) |
| 816 |
{ |
| 817 |
cerr << "Exit on timeout.\n"; |
| 818 |
return 0; |
| 819 |
} |
| 820 |
|
| 821 |
Verb("MEDIA CREATION FAILED: ", x.what(), " - exiting."); |
| 822 |
|
| 823 |
// Don't speak anything when no -v option. |
| 824 |
// (the "requested interrupt" will be printed anyway) |
| 825 |
return 2; |
| 826 |
} |
| 827 |
catch (...) |
| 828 |
{ |
| 829 |
cerr << "ERROR: UNKNOWN EXCEPTION\n"; |
| 830 |
return 2; |
| 831 |
} |
| 832 |
|
| 833 |
alarm(0); |
| 834 |
end_time = time(0); |
| 835 |
|
| 836 |
if (!src || !tar) |
| 837 |
{ |
| 838 |
const string tarstate = tar ? "CREATED" : "FAILED"; |
| 839 |
const string srcstate = src ? "CREATED" : "FAILED"; |
| 840 |
|
| 841 |
cerr << "ERROR: not both media created; source:" << srcstate << " target:" << tarstate << endl; |
| 842 |
return 2; |
| 843 |
} |
| 844 |
|
| 845 |
// Now loop until broken |
| 846 |
BandwidthGuard bw(bandwidth); |
| 847 |
|
| 848 |
if (transmit_use_sourcetime && src->uri.type() != UriParser::SRT) |
| 849 |
{ |
| 850 |
Verb("WARNING: -st option is effective only if the target type is SRT"); |
| 851 |
} |
| 852 |
|
| 853 |
Verb("STARTING TRANSMISSION: '", source_spec, "' --> '", target_spec, "'"); |
| 854 |
|
| 855 |
// After the time has been spent in the creation |
| 856 |
// (including waiting for connection) |
| 857 |
// rest of the time should be spent for transmission. |
| 858 |
if (stoptime != 0) |
| 859 |
{ |
| 860 |
int elapsed = end_time - start_time; |
| 861 |
int remain = int(stoptime) - elapsed; |
| 862 |
|
| 863 |
if (remain <= final_delay) |
| 864 |
{ |
| 865 |
cerr << "NOTE: remained too little time for cleanup: " << remain << "s - exiting\n"; |
| 866 |
return 0; |
| 867 |
} |
| 868 |
|
| 869 |
cerr << "NOTE: stoptime: remaining " << remain << " seconds (setting alarm to " << (remain - final_delay) << "s)\n"; |
| 870 |
alarm(remain - final_delay); |
| 871 |
} |
| 872 |
|
| 873 |
try |
| 874 |
{ |
| 875 |
for (;;) |
| 876 |
{ |
| 877 |
if (stoptime == 0 && timeout != -1 ) |
| 878 |
{ |
| 879 |
Verb("[.", VerbNoEOL); |
| 880 |
alarm(timeout); |
| 881 |
} |
| 882 |
else |
| 883 |
{ |
| 884 |
alarm(0); |
| 885 |
} |
| 886 |
Verb(", ... ", VerbNoEOL); |
| 887 |
g_interrupt_reason = "reading"; |
| 888 |
const MediaPacket& data = src->Read(chunk); |
| 889 |
Verb(", ", data.payload.size(), " -> ", VerbNoEOL); |
| 890 |
if ( data.payload.empty() && src->End() ) |
| 891 |
{ |
| 892 |
Verb("EOS"); |
| 893 |
break; |
| 894 |
} |
| 895 |
g_interrupt_reason = "writing"; |
| 896 |
tar->Write(data); |
| 897 |
if (stoptime == 0 && timeout != -1 ) |
| 898 |
{ |
| 899 |
Verb(".] ", VerbNoEOL); |
| 900 |
alarm(0); |
| 901 |
} |
| 902 |
|
| 903 |
if ( tar->Broken() ) |
| 904 |
{ |
| 905 |
Verb(" OUTPUT broken"); |
| 906 |
break; |
| 907 |
} |
| 908 |
|
| 909 |
Verb("sent"); |
| 910 |
|
| 911 |
if (::transmit_int_state) |
| 912 |
{ |
| 913 |
Verror("\n (interrupted on request)"); |
| 914 |
break; |
| 915 |
} |
| 916 |
|
| 917 |
bw.Checkpoint(chunk, transmit_bw_report); |
| 918 |
|
| 919 |
if (stoptime != 0) |
| 920 |
{ |
| 921 |
int elapsed = time(0) - end_time; |
| 922 |
int remain = int(stoptime - final_delay - elapsed); |
| 923 |
if (remain < 0) |
| 924 |
{ |
| 925 |
Verror("\n (interrupted on timeout: elapsed ", elapsed, "s) - waiting ", final_delay, "s for cleanup"); |
| 926 |
this_thread::sleep_for(chrono::seconds(final_delay)); |
| 927 |
break; |
| 928 |
} |
| 929 |
} |
| 930 |
} |
| 931 |
|
| 932 |
} catch (Source::ReadEOF&) { |
| 933 |
alarm(0); |
| 934 |
|
| 935 |
if (!skip_flushing) |
| 936 |
{ |
| 937 |
Verror("(DEBUG) EOF when reading file. Looping until the sending buffer depletes.\n"); |
| 938 |
for (;;) |
| 939 |
{ |
| 940 |
size_t still = tar->Still(); |
| 941 |
if (still == 0) |
| 942 |
{ |
| 943 |
Verror("(DEBUG) DEPLETED. Done.\n"); |
| 944 |
break; |
| 945 |
} |
| 946 |
|
| 947 |
Verror("(DEBUG)... still ", still, " bytes (sleep 1s)\n"); |
| 948 |
this_thread::sleep_for(chrono::seconds(1)); |
| 949 |
} |
| 950 |
} |
| 951 |
} catch (std::exception& x) { // Catches TransmissionError and AlarmExit |
| 952 |
|
| 953 |
if (stoptime != 0 && ::timer_state) |
| 954 |
{ |
| 955 |
Verror("Exit on timeout."); |
| 956 |
} |
| 957 |
else if (::transmit_int_state) |
| 958 |
{ |
| 959 |
Verror("Exit on interrupt."); |
| 960 |
// Do nothing. |
| 961 |
} |
| 962 |
else |
| 963 |
{ |
| 964 |
Verror("STD EXCEPTION: ", x.what()); |
| 965 |
} |
| 966 |
|
| 967 |
if ( crashonx ) |
| 968 |
throw; |
| 969 |
|
| 970 |
if (final_delay > 0) |
| 971 |
{ |
| 972 |
Verror("Waiting ", final_delay, "s for possible cleanup..."); |
| 973 |
this_thread::sleep_for(chrono::seconds(final_delay)); |
| 974 |
} |
| 975 |
if (stoptime != 0 && ::timer_state) |
| 976 |
return 0; |
| 977 |
|
| 978 |
return 255; |
| 979 |
|
| 980 |
} catch (...) { |
| 981 |
|
| 982 |
Verror("UNKNOWN type of EXCEPTION"); |
| 983 |
if ( crashonx ) |
| 984 |
throw; |
| 985 |
|
| 986 |
return 1; |
| 987 |
} |
| 988 |
|
| 989 |
return 0; |
| 990 |
} |
| 991 |
|
| 992 |
// Class utilities |
| 993 |
|
| 994 |
|
| 995 |
void TestLogHandler(void* opaque, int level, const char* file, int line, const char* area, const char* message) |
| 996 |
{ |
| 997 |
char prefix[100] = ""; |
| 998 |
if ( opaque ) { |
| 999 |
#ifdef _MSC_VER |
| 1000 |
strncpy_s(prefix, sizeof(prefix), (char*)opaque, _TRUNCATE); |
| 1001 |
#else |
| 1002 |
strncpy(prefix, (char*)opaque, sizeof(prefix) - 1); |
| 1003 |
prefix[sizeof(prefix) - 1] = '\0'; |
| 1004 |
#endif |
| 1005 |
} |
| 1006 |
time_t now; |
| 1007 |
time(&now); |
| 1008 |
char buf[1024]; |
| 1009 |
struct tm local = SysLocalTime(now); |
| 1010 |
size_t pos = strftime(buf, 1024, "[%c ", &local); |
| 1011 |
|
| 1012 |
#ifdef _MSC_VER |
| 1013 |
// That's something weird that happens on Microsoft Visual Studio 2013 |
| 1014 |
// Trying to keep portability, while every version of MSVS is a different platform. |
| 1015 |
// On MSVS 2015 there's already a standard-compliant snprintf, whereas _snprintf |
| 1016 |
// is available on backward compatibility and it doesn't work exactly the same way. |
| 1017 |
#define snprintf _snprintf |
| 1018 |
#endif |
| 1019 |
snprintf(buf+pos, 1024-pos, "%s:%d(%s)]{%d} %s", file, line, area, level, message); |
| 1020 |
|
| 1021 |
cerr << buf << endl; |
| 1022 |
} |