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
|
/*
* 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/.
*
*/
/*****************************************************************************
written by
Haivision Systems Inc.
*****************************************************************************/
#ifdef _WIN32
#include <direct.h>
#endif
#include <iostream>
#include <iterator>
#include <vector>
#include <map>
#include <stdexcept>
#include <string>
#include <csignal>
#include <thread>
#include <chrono>
#include <cassert>
#include <sys/stat.h>
#include <srt.h>
#include <udt.h>
#include <common.h>
#include "apputil.hpp"
#include "uriparser.hpp"
#include "logsupport.hpp"
#include "socketoptions.hpp"
#include "transmitmedia.hpp"
#include "verbose.hpp"
#ifndef S_ISDIR
#define S_ISDIR(mode) (((mode) & S_IFMT) == S_IFDIR)
#endif
using namespace std;
static bool interrupt = false;
void OnINT_ForceExit(int)
{
Verb() << "\n-------- REQUESTED INTERRUPT!\n";
interrupt = true;
}
struct FileTransmitConfig
{
unsigned long chunk_size;
bool skip_flushing;
bool quiet = false;
srt_logging::LogLevel::type loglevel = srt_logging::LogLevel::error;
set<srt_logging::LogFA> logfas;
string logfile;
int bw_report = 0;
int stats_report = 0;
string stats_out;
SrtStatsPrintFormat stats_pf = SRTSTATS_PROFMAT_2COLS;
bool full_stats = false;
string source;
string target;
};
void PrintOptionHelp(const set<string> &opt_names, const string &value, const string &desc)
{
cerr << "\t";
int i = 0;
for (auto opt : opt_names)
{
if (i++) cerr << ", ";
cerr << "-" << opt;
}
if (!value.empty())
cerr << ":" << value;
cerr << "\t- " << desc << "\n";
}
int parse_args(FileTransmitConfig &cfg, int argc, char** argv)
{
const OptionName
o_chunk = { "c", "chunk" },
o_no_flush = { "sf", "skipflush" },
o_bwreport = { "r", "bwreport", "report", "bandwidth-report", "bitrate-report" },
o_statsrep = { "s", "stats", "stats-report-frequency" },
o_statsout = { "statsout" },
o_statspf = { "pf", "statspf" },
o_statsfull = { "f", "fullstats" },
o_loglevel = { "ll", "loglevel" },
o_logfa = { "logfa" },
o_logfile = { "logfile" },
o_quiet = { "q", "quiet" },
o_verbose = { "v", "verbose" },
o_help = { "h", "help" },
o_version = { "version" };
const vector<OptionScheme> optargs = {
{ o_chunk, OptionScheme::ARG_ONE },
{ o_no_flush, OptionScheme::ARG_NONE },
{ o_bwreport, 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_logfile, OptionScheme::ARG_ONE },
{ o_quiet, OptionScheme::ARG_NONE },
{ o_verbose, OptionScheme::ARG_NONE },
{ o_help, OptionScheme::ARG_NONE },
{ o_version, OptionScheme::ARG_NONE }
};
options_t params = ProcessOptions(argv, argc, optargs);
bool print_help = Option<OutBool>(params, false, o_help);
const bool print_version = Option<OutBool>(params, false, 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)
{
cout << "SRT sample application to transmit files.\n";
PrintLibVersion();
cerr << "Usage: srt-file-transmit [options] <input-uri> <output-uri>\n";
cerr << "\n";
PrintOptionHelp(o_chunk, "<chunk=1456>", "max size of data read in one step");
PrintOptionHelp(o_no_flush, "", "skip output file flushing");
PrintOptionHelp(o_bwreport, "<every_n_packets=0>", "bandwidth report frequency");
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=error>", "log level [fatal,error,info,note,warning]");
PrintOptionHelp(o_logfa, "<fas=general,...>", "log functional area [all,general,bstats,control,data,tsbpd,rexmit]");
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\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: file URI or file://con to use stdin or stdout\n";
return 2;
}
if (Option<OutBool>(params, false, o_version))
{
PrintLibVersion();
return 2;
}
cfg.chunk_size = stoul(Option<OutString>(params, "1456", o_chunk));
cfg.skip_flushing = Option<OutBool>(params, false, o_no_flush);
cfg.bw_report = stoi(Option<OutString>(params, "0", o_bwreport));
cfg.stats_report = stoi(Option<OutString>(params, "0", o_statsrep));
cfg.stats_out = Option<OutString>(params, "", o_statsout);
const string pf = Option<OutString>(params, "default", o_statspf);
if (pf == "default")
{
cfg.stats_pf = SRTSTATS_PROFMAT_2COLS;
}
else if (pf == "json")
{
cfg.stats_pf = SRTSTATS_PROFMAT_JSON;
}
else if (pf == "csv")
{
cfg.stats_pf = SRTSTATS_PROFMAT_CSV;
}
else
{
cfg.stats_pf = SRTSTATS_PROFMAT_2COLS;
cerr << "ERROR: Unsupported print format: " << pf << endl;
return 1;
}
cfg.full_stats = Option<OutBool>(params, false, o_statsfull);
cfg.loglevel = SrtParseLogLevel(Option<OutString>(params, "error", o_loglevel));
cfg.logfas = SrtParseLogFA(Option<OutString>(params, "", o_logfa));
cfg.logfile = Option<OutString>(params, "", o_logfile);
cfg.quiet = Option<OutBool>(params, false, o_quiet);
if (Option<OutBool>(params, false, o_verbose))
Verbose::on = !cfg.quiet;
cfg.source = params[""].at(0);
cfg.target = params[""].at(1);
return 0;
}
void ExtractPath(string path, string& w_dir, string& w_fname)
{
string directory = path;
string filename = "";
struct stat state;
stat(path.c_str(), &state);
if (!S_ISDIR(state.st_mode))
{
// Extract directory as a butlast part of path
size_t pos = path.find_last_of("/");
if ( pos == string::npos )
{
filename = path;
directory = ".";
}
else
{
directory = path.substr(0, pos);
filename = path.substr(pos+1);
}
}
if (directory[0] != '/')
{
// Glue in the absolute prefix of the current directory
// to make it absolute. This is needed to properly interpret
// the fixed uri.
static const size_t s_max_path = 4096; // don't care how proper this is
char tmppath[s_max_path];
#ifdef _WIN32
const char* gwd = _getcwd(tmppath, s_max_path);
#else
const char* gwd = getcwd(tmppath, s_max_path);
#endif
if ( !gwd )
{
// Don't bother with that now. We need something better for
// that anyway.
throw std::invalid_argument("Path too long");
}
const string wd = gwd;
directory = wd + "/" + directory;
}
w_dir = directory;
w_fname = filename;
}
bool DoUpload(UriParser& ut, string path, string filename,
const FileTransmitConfig &cfg, std::ostream &out_stats)
{
bool result = false;
unique_ptr<Target> tar;
SRTSOCKET s = SRT_INVALID_SOCK;
bool connected = false;
int pollid = -1;
ifstream ifile(path, ios::binary);
if ( !ifile )
{
cerr << "Error opening file: '" << path << "'";
goto exit;
}
pollid = srt_epoll_create();
if ( pollid < 0 )
{
cerr << "Can't initialize epoll";
goto exit;
}
while (!interrupt)
{
if (!tar.get())
{
tar = Target::Create(ut.makeUri());
if (!tar.get())
{
cerr << "Unsupported target type: " << ut.uri() << endl;
goto exit;
}
int events = SRT_EPOLL_OUT | SRT_EPOLL_ERR;
if (srt_epoll_add_usock(pollid,
tar->GetSRTSocket(), &events))
{
cerr << "Failed to add SRT destination to poll, "
<< tar->GetSRTSocket() << endl;
goto exit;
}
srt::setstreamid(tar->GetSRTSocket(), filename);
}
s = tar->GetSRTSocket();
assert(s != SRT_INVALID_SOCK);
SRTSOCKET efd;
int efdlen = 1;
if (srt_epoll_wait(pollid,
0, 0, &efd, &efdlen,
100, nullptr, nullptr, 0, 0) < 0)
{
continue;
}
assert(efd == s);
assert(efdlen == 1);
SRT_SOCKSTATUS status = srt_getsockstate(s);
switch (status)
{
case SRTS_LISTENING:
{
if (!tar->AcceptNewClient())
{
cerr << "Failed to accept SRT connection" << endl;
goto exit;
}
srt_epoll_remove_usock(pollid, s);
s = tar->GetSRTSocket();
int events = SRT_EPOLL_OUT | SRT_EPOLL_ERR;
if (srt_epoll_add_usock(pollid, s, &events))
{
cerr << "Failed to add SRT client to poll" << endl;
goto exit;
}
cerr << "Target connected (listener)" << endl;
connected = true;
}
break;
case SRTS_CONNECTED:
{
if (!connected)
{
cerr << "Target connected (caller)" << endl;
connected = true;
}
}
break;
case SRTS_BROKEN:
case SRTS_NONEXIST:
case SRTS_CLOSED:
{
cerr << "Target disconnected" << endl;
goto exit;
}
default:
{
// No-Op
}
break;
}
if (connected)
{
vector<char> buf(cfg.chunk_size);
size_t n = ifile.read(buf.data(), cfg.chunk_size).gcount();
size_t shift = 0;
while (n > 0)
{
int st = tar->Write(buf.data() + shift, n, 0, out_stats);
Verb() << "Upload: " << n << " --> " << st
<< (!shift ? string() : "+" + Sprint(shift));
if (st == SRT_ERROR)
{
cerr << "Upload: SRT error: " << srt_getlasterror_str()
<< endl;
goto exit;
}
n -= st;
shift += st;
}
if (ifile.eof())
{
cerr << "File sent" << endl;
result = true;
break;
}
if ( !ifile.good() )
{
cerr << "ERROR while reading file\n";
goto exit;
}
}
}
if (result && !cfg.skip_flushing)
{
assert(s != SRT_INVALID_SOCK);
// send-flush-loop
result = false;
while (!interrupt)
{
size_t bytes;
size_t blocks;
int st = srt_getsndbuffer(s, &blocks, &bytes);
if (st == SRT_ERROR)
{
cerr << "Error in srt_getsndbuffer: " << srt_getlasterror_str()
<< endl;
goto exit;
}
if (bytes == 0)
{
cerr << "Buffers flushed" << endl;
result = true;
break;
}
Verb() << "Sending buffer still: bytes=" << bytes << " blocks="
<< blocks;
srt::sync::this_thread::sleep_for(srt::sync::milliseconds_from(250));
}
}
exit:
if (pollid >= 0)
{
srt_epoll_release(pollid);
}
return result;
}
bool DoDownload(UriParser& us, string directory, string filename,
const FileTransmitConfig &cfg, std::ostream &out_stats)
{
bool result = false;
unique_ptr<Source> src;
SRTSOCKET s = SRT_INVALID_SOCK;
bool connected = false;
int pollid = -1;
string id;
ofstream ofile;
SRT_SOCKSTATUS status;
SRTSOCKET efd;
int efdlen = 1;
pollid = srt_epoll_create();
if ( pollid < 0 )
{
cerr << "Can't initialize epoll";
goto exit;
}
while (!interrupt)
{
if (!src.get())
{
src = Source::Create(us.makeUri());
if (!src.get())
{
cerr << "Unsupported source type: " << us.uri() << endl;
goto exit;
}
int events = SRT_EPOLL_IN | SRT_EPOLL_ERR;
if (srt_epoll_add_usock(pollid,
src->GetSRTSocket(), &events))
{
cerr << "Failed to add SRT source to poll, "
<< src->GetSRTSocket() << endl;
goto exit;
}
}
s = src->GetSRTSocket();
assert(s != SRT_INVALID_SOCK);
if (srt_epoll_wait(pollid,
&efd, &efdlen, 0, 0,
100, nullptr, nullptr, 0, 0) < 0)
{
continue;
}
assert(efd == s);
assert(efdlen == 1);
status = srt_getsockstate(s);
Verb() << "Event with status " << status << "\n";
switch (status)
{
case SRTS_LISTENING:
{
if (!src->AcceptNewClient())
{
cerr << "Failed to accept SRT connection" << endl;
goto exit;
}
srt_epoll_remove_usock(pollid, s);
s = src->GetSRTSocket();
int events = SRT_EPOLL_IN | SRT_EPOLL_ERR;
if (srt_epoll_add_usock(pollid, s, &events))
{
cerr << "Failed to add SRT client to poll" << endl;
goto exit;
}
id = srt::getstreamid(s);
cerr << "Source connected (listener), id ["
<< id << "]" << endl;
connected = true;
continue;
}
break;
case SRTS_CONNECTED:
{
if (!connected)
{
id = srt::getstreamid(s);
cerr << "Source connected (caller), id ["
<< id << "]" << endl;
connected = true;
}
}
break;
// No need to do any special action in case of broken.
// The app will just try to read and in worst case it will
// get an error.
case SRTS_BROKEN:
cerr << "Connection closed, reading buffer remains\n";
break;
case SRTS_NONEXIST:
case SRTS_CLOSED:
{
cerr << "Source disconnected" << endl;
goto exit;
}
break;
default:
{
// No-Op
}
break;
}
if (connected)
{
MediaPacket packet(cfg.chunk_size);
if (!ofile.is_open())
{
const char * fn = id.empty() ? filename.c_str() : id.c_str();
directory.append("/");
directory.append(fn);
ofile.open(directory.c_str(), ios::out | ios::trunc | ios::binary);
if (!ofile.is_open())
{
cerr << "Error opening file [" << directory << "]" << endl;
goto exit;
}
cerr << "Writing output to [" << directory << "]" << endl;
}
int n = src->Read(cfg.chunk_size, packet, out_stats);
if (n == SRT_ERROR)
{
cerr << "Download: SRT error: " << srt_getlasterror_str() << endl;
goto exit;
}
if (n == 0)
{
result = true;
cerr << "Download COMPLETE.\n";
break;
}
// Write to file any amount of data received
Verb() << "Download: --> " << n;
ofile.write(packet.payload.data(), n);
if (!ofile.good())
{
cerr << "Error writing file" << endl;
goto exit;
}
}
}
exit:
if (pollid >= 0)
{
srt_epoll_release(pollid);
}
return result;
}
bool Upload(UriParser& srt_target_uri, UriParser& fileuri,
const FileTransmitConfig &cfg, std::ostream &out_stats)
{
if ( fileuri.scheme() != "file" )
{
cerr << "Upload: source accepted only as a file\n";
return false;
}
// fileuri is source-reading file
// srt_target_uri is SRT target
string path = fileuri.path();
string directory, filename;
ExtractPath(path, (directory), (filename));
Verb() << "Extract path '" << path << "': directory=" << directory << " filename=" << filename;
// Set ID to the filename.
// Directory will be preserved.
// Add some extra parameters.
srt_target_uri["transtype"] = "file";
return DoUpload(srt_target_uri, path, filename, cfg, out_stats);
}
bool Download(UriParser& srt_source_uri, UriParser& fileuri,
const FileTransmitConfig &cfg, std::ostream &out_stats)
{
if (fileuri.scheme() != "file" )
{
cerr << "Download: target accepted only as a file\n";
return false;
}
string path = fileuri.path(), directory, filename;
ExtractPath(path, (directory), (filename));
Verb() << "Extract path '" << path << "': directory=" << directory << " filename=" << filename;
// Add some extra parameters.
srt_source_uri["transtype"] = "file";
return DoDownload(srt_source_uri, directory, filename, cfg, out_stats);
}
int main(int argc, char** argv)
{
FileTransmitConfig 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 != SRT_LIVE_MAX_PLSIZE)
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);
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
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 != "" && cfg.stats_out != "stdout")
{
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";
return 1;
}
}
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;
// File transmission code
UriParser us(cfg.source), ut(cfg.target);
Verb() << "SOURCE type=" << us.scheme() << ", TARGET type=" << ut.scheme();
signal(SIGINT, OnINT_ForceExit);
signal(SIGTERM, OnINT_ForceExit);
try
{
if (us.scheme() == "srt")
{
if (ut.scheme() != "file")
{
cerr << "SRT to FILE should be specified\n";
return 1;
}
Download(us, ut, cfg, out_stats);
}
else if (ut.scheme() == "srt")
{
if (us.scheme() != "file")
{
cerr << "FILE to SRT should be specified\n";
return 1;
}
Upload(ut, us, cfg, out_stats);
}
else
{
cerr << "SRT URI must be one of given media.\n";
return 1;
}
}
catch (std::exception& x)
{
cerr << "ERROR: " << x.what() << endl;
return 1;
}
return 0;
}
|