Debian Codegraph @

testing/srt-test-mpbond.cpp

raw
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 #include <memory>
12 #include <thread>
13 #include <list>
14 #include <utility>
15 #include <chrono>
16 #include <csignal>
17 #include <iterator>
18 #include <stdexcept>
19
20 #define REQUIRE_CXX11 1
21
22 #include "apputil.hpp" // CreateAddr
23 #include "uriparser.hpp" // UriParser
24 #include "socketoptions.hpp"
25 #include "logsupport.hpp"
26 #include "testmediabase.hpp"
27 #include "testmedia.hpp"
28 #include "netinet_any.h"
29 #include "threadname.h"
30 #include "verbose.hpp"
31
32 #include <srt.h>
33 #include <logging.h>
34
35 // Make the windows-nonexistent alarm an empty call
36 #ifdef _WIN32
37 #define alarm(argument) (void)0
38 #define signal_alarm(fn) (void)0
39 #else
40 #define signal_alarm(fn) signal(SIGALRM, fn)
41 #endif
42
43 srt_logging::Logger applog(SRT_LOGFA_APP, srt_logger_config, "srt-mpbond");
44
45 using namespace srt;
46 using namespace std;
47
48
49 volatile bool mpbond_int_state = false;
50 void OnINT_SetIntState(int)
51 {
52 cerr << "\n-------- REQUESTED INTERRUPT!\n";
53 mpbond_int_state = true;
54 }
55
56 int main( int argc, char** argv )
57 {
58 // This is mainly required on Windows to initialize the network system,
59 // for a case when the instance would use UDP. SRT does it on its own, independently.
60 if ( !SysInitializeNetwork() )
61 throw std::runtime_error("Can't initialize network!");
62
63 // Symmetrically, this does a cleanup; put into a local destructor to ensure that
64 // it's called regardless of how this function returns.
65 struct NetworkCleanup
66 {
67 ~NetworkCleanup()
68 {
69 SysCleanupNetwork();
70 }
71 } cleanupobj;
72
73 signal(SIGINT, OnINT_SetIntState);
74 signal(SIGTERM, OnINT_SetIntState);
75
76 vector<OptionScheme> optargs;
77
78 OptionName
79 o_input ((optargs), "<input-medium> Define input to send over SRT endpoint", "i", "input"),
80 o_output ((optargs), "<output-medium> Define output to send data read from SRT endpoint", "o", "output"),
81 o_verbose ((optargs), "[channel=0|1] Print size of every packet transferred on stdout or specified [channel]", "v", "verbose"),
82 o_loglevel ((optargs), "<severity=fatal|error|note|warning|debug> Minimum severity for logs", "ll", "loglevel"),
83 o_logfa ((optargs), "<FA=all> Enabled Functional Areas", "lfa", "logfa"),
84 o_help ((optargs), " This help", "?", "help", "-help")
85 ;
86
87 options_t params = ProcessOptions(argv, argc, optargs);
88
89 bool need_help = OptionPresent(params, o_help);
90
91 vector<string> args = params[""];
92
93 string srtspec;
94
95 if (args.empty())
96 need_help = true;
97 else
98 {
99 for (size_t i = 0; i < args.size(); ++i)
100 {
101 UriParser u(args[i], UriParser::EXPECT_HOST);
102 if (u.portno() == 0)
103 {
104 cerr << "ERROR: " << args[i] << " expected host:port or :port syntax.\n";
105 return 1;
106 }
107 }
108 }
109
110 if (need_help)
111 {
112 cerr << "Usage:\n";
113 cerr << " " << argv[0] << " <SRT listeners...> [-i INPUT] [-o OUTPUT]\n";
114 cerr << "*** (Position of [options] is unrestricted.)\n";
115 cerr << "*** (<variadic...> option parameters can be only terminated by a next option.)\n";
116 cerr << "where:\n";
117 cerr << " - <SRT listeners...>: a list of host:port specs for SRT listener\n";
118 cerr << " - INPUT or OUTPUT: at least one of that kind must be specified\n";
119 cerr << "SUPPORTED URI SCHEMES:\n";
120 cerr << " srt: use SRT connection\n";
121 cerr << " udp: read from bound UDP socket or send to given address as UDP\n";
122 cerr << " file (default if scheme not specified) specified as:\n";
123 cerr << " - empty host/port and absolute file path in the URI\n";
124 cerr << " - only a filename, also as a relative path\n";
125 cerr << " - file://con ('con' as host): designates stdin or stdout\n";
126 cerr << "OPTIONS HELP SYNTAX: -option <parameter[unit]=default[meaning]>:\n";
127 for (auto os: optargs)
128 cout << OptionHelpItem(*os.pid) << endl;
129 return 1;
130 }
131
132 bool skip_flushing = false; // non-configurable for now
133
134 bool mode_output = OptionPresent(params, o_output);
135
136 string loglevel = Option<OutString>(params, "error", "ll", "loglevel");
137 srt_logging::LogLevel::type lev = SrtParseLogLevel(loglevel);
138 srt::setloglevel(lev);
139 srt::addlogfa(SRT_LOGFA_APP);
140
141 // Check verbose option before extracting the argument so that Verb()s
142 // can be displayed also when they report something about option parsing.
143 string verbose_val = Option<OutString>(params, "no", o_verbose);
144
145 int verbch = 1; // default cerr
146 if (verbose_val != "no")
147 {
148 Verbose::on = true;
149 try
150 {
151 verbch = stoi(verbose_val);
152 }
153 catch (...)
154 {
155 verbch = 1;
156 }
157 if (verbch != 1)
158 {
159 if (verbch != 2)
160 {
161 cerr << "-v or -v:1 (default) or -v:2 only allowed\n";
162 return 1;
163 }
164 Verbose::cverb = &std::cerr;
165 }
166 else
167 {
168 Verbose::cverb = &std::cout;
169 }
170 }
171
172
173 if (OptionPresent(params, o_input) == OptionPresent(params, o_output))
174 {
175 cerr << "One of -i and -o options must be specified (not both)\n";
176 return 1;
177 }
178
179
180 // Create listeners according to the parameters
181 vector<SRTSOCKET> listeners;
182
183 Verb() << "LISTENERS [ " << VerbNoEOL;
184
185 for (size_t i = 0; i < args.size(); ++i)
186 {
187 UriParser u(args[i], UriParser::EXPECT_HOST);
188 sockaddr_any sa = CreateAddr(u.host(), u.portno());
189
190 SRTSOCKET s = srt_create_socket();
191
192 srt::setopt(s)[SRTO_GROUPCONNECT] = 1;
193 srt_bind(s, sa.get(), sizeof sa);
194 srt_listen(s, 5);
195
196 listeners.push_back(s);
197 Verb() << u.host() << ":" << u.portno() << " " << VerbNoEOL;
198 }
199
200 Verb() << "] accept...";
201
202 SRTSOCKET conngrp = srt_accept_bond(listeners.data(), int(listeners.size()), -1);
203 if (conngrp == SRT_INVALID_SOCK)
204 {
205 cerr << "ERROR: srt_accept_bond: " << srt_getlasterror_str() << endl;
206 return 1;
207 }
208
209 unique_ptr<Source> src;
210 unique_ptr<Target> tar;
211
212 try
213 {
214 // Now create input or output
215 if (mode_output)
216 {
217 string outspec = Option<OutString>(params, o_output);
218 Verb() << "SRT -> " << outspec;
219 tar = Target::Create(outspec);
220
221 auto s = new SrtSource;
222 s->Acquire(conngrp);
223 src.reset(s);
224 }
225 else
226 {
227 string inspec = Option<OutString>(params, o_input);
228 Verb() << "SRT <- " << inspec;
229 src = Source::Create(inspec);
230
231 auto s = new SrtTarget;
232 s->Acquire(conngrp);
233 tar.reset(s);
234 }
235 }
236 catch (...)
237 {
238 return 2;
239 }
240
241 size_t chunk = SRT_LIVE_MAX_PLSIZE;
242
243 // Now run the loop
244 try
245 {
246 for (;;)
247 {
248 Verb() << " << ... " << VerbNoEOL;
249 const MediaPacket& data = src->Read(chunk);
250 Verb() << " << " << data.payload.size() << " -> " << VerbNoEOL;
251 if ( data.payload.empty() && src->End() )
252 {
253 Verb() << "EOS";
254 break;
255 }
256 tar->Write(data);
257
258 if ( tar->Broken() )
259 {
260 Verb() << " OUTPUT broken";
261 break;
262 }
263
264 Verb() << "sent";
265
266 if ( mpbond_int_state )
267 {
268 Verror() << "\n (interrupted on request)";
269 break;
270 }
271 }
272 } catch (Source::ReadEOF&) {
273 alarm(0);
274
275 if (!skip_flushing)
276 {
277 Verror() << "(DEBUG) EOF when reading file. Looping until the sending buffer depletes.\n";
278 for (;;)
279 {
280 size_t still = tar->Still();
281 if (still == 0)
282 {
283 Verror() << "(DEBUG) DEPLETED. Done.\n";
284 break;
285 }
286
287 Verror() << "(DEBUG)... still " << still << " bytes (sleep 1s)\n";
288 this_thread::sleep_for(chrono::seconds(1));
289 }
290 }
291 } catch (std::exception& x) { // Catches TransmissionError and AlarmExit
292 if (::mpbond_int_state)
293 {
294 Verror() << "Exit on interrupt.";
295 // Do nothing.
296 }
297 else
298 {
299 Verror() << "STD EXCEPTION: " << x.what();
300 }
301
302 return 255;
303 } catch (...) {
304 Verror() << "UNKNOWN type of EXCEPTION";
305 return 1;
306 }
307
308 return 0;
309 }
310
311
312
313