Debian Codegraph @

testing/srt-test-relay.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 /*****************************************************************************
12 written by
13 Haivision Systems Inc.
14 *****************************************************************************/
15
16 #include "platform_sys.h"
17
18 #include <atomic>
19 #include <iostream>
20 #include <iterator>
21 #include <vector>
22 #include <list>
23 #include <map>
24 #include <stdexcept>
25 #include <string>
26 #include <thread>
27 #include <chrono>
28 #include <deque>
29 #include <mutex>
30 #include <condition_variable>
31 #include <csignal>
32 #include <sys/stat.h>
33 #include <srt.h>
34 #include <udt.h>
35
36 #include "testactivemedia.hpp"
37
38 #include "apputil.hpp"
39 #include "uriparser.hpp"
40 #include "logsupport.hpp"
41 #include "logging.h"
42 #include "socketoptions.hpp"
43 #include "verbose.hpp"
44 #include "testmedia.hpp"
45 #include "threadname.h"
46
47
48 using namespace std;
49
50
51 bool Upload(UriParser& srt, UriParser& file);
52 bool Download(UriParser& srt, UriParser& file);
53
54 srt_logging::Logger applog(SRT_LOGFA_APP, srt_logger_config, "srt-relay");
55
56 std::atomic<bool> g_program_established {false};
57
58 SrtModel* g_pending_model = nullptr;
59
60 thread::id g_root_thread = std::this_thread::get_id();
61
62 static void OnINT_SetInterrupted(int)
63 {
64 Verb() << VerbLock << "SIGINT: Setting interrupt state.";
65 ::transmit_int_state = true;
66
67 // Just for a case, forcefully close all active SRT sockets.
68 SrtModel* pm = ::g_pending_model;
69 if (pm)
70 {
71 // The program is hanged on accepting a new SRT connection.
72 // We need to check which thread we've fallen into.
73 if (this_thread::get_id() == g_root_thread)
74 {
75 // Throw an exception, it will be caught in a predicted place.
76 throw std::runtime_error("Interrupted on request");
77 }
78 else
79 {
80 // This is some other thread, so close the listener socket.
81 // This will cause the accept block to be interrupted.
82 for (SRTSOCKET i: { pm->Socket(), pm->Listener() })
83 if (i != SRT_INVALID_SOCK)
84 srt_close(i);
85 }
86 }
87
88 }
89
90 using namespace std;
91
92 size_t g_chunksize = 0;
93 size_t g_default_live_chunksize = 1316;
94 size_t g_default_file_chunksize = 1456;
95
96 class SrtMainLoop
97 {
98 UriParser m_srtspec;
99
100 // Media used
101 unique_ptr<SrtRelay> m_srt_relay;
102 SourceMedium m_srt_source;
103 SourceMedium m_input_medium;
104 list<unique_ptr<TargetMedium>> m_output_media;
105 thread m_input_thr;
106 std::exception_ptr m_input_xp;
107
108 void InputRunner();
109 srt::sync::atomic<bool> m_input_running;
110
111 public:
112 SrtMainLoop(const string& srt_uri, bool input_echoback, const string& input_spec, const vector<string>& output_spec);
113
114 void run();
115
116 void MakeStop() { m_input_running = false; }
117 bool IsRunning() { return m_input_running; }
118
119 ~SrtMainLoop()
120 {
121 if (m_input_thr.joinable())
122 m_input_thr.join();
123 }
124 };
125
126 int main( int argc, char** argv )
127 {
128 OptionName
129 o_loglevel = { "ll", "loglevel" },
130 o_logfa = { "lf", "logfa" },
131 o_verbose = {"v", "verbose" },
132 o_input = {"i", "input"},
133 o_output = {"o", "output"},
134 o_echo = {"e", "io", "input-echoback"},
135 o_chunksize = {"c", "chunk"}
136 ;
137
138 // Options that expect no arguments (ARG_NONE) need not be mentioned.
139 vector<OptionScheme> optargs = {
140 { o_loglevel, OptionScheme::ARG_ONE },
141 { o_logfa, OptionScheme::ARG_ONE },
142 { o_input, OptionScheme::ARG_ONE },
143 { o_output, OptionScheme::ARG_VAR },
144 { o_chunksize, OptionScheme::ARG_ONE }
145 };
146 options_t params = ProcessOptions(argv, argc, optargs);
147
148 /*
149 cerr << "OPTIONS (DEBUG)\n";
150 for (auto o: params)
151 {
152 cerr << "[" << o.first << "] ";
153 copy(o.second.begin(), o.second.end(), ostream_iterator<string>(cerr, " "));
154 cerr << endl;
155 }
156 */
157
158 vector<string> args = params[""];
159 if ( args.size() != 1 )
160 {
161 cerr << "Usage: " << argv[0] << " <srt-endpoint> [ -i <input> | -e ] [ -o <output> ]\n";
162 cerr << "Options:\n";
163 cerr << "\t-v . . . . . . . . . . Verbose mode\n";
164 cerr << "\t-ll <level=error> . . . . . Log level for SRT\n";
165 cerr << "\t-lf <logfa=all> . . . . . Log Functional Areas enabled\n";
166 cerr << "\t-c <size=1316[live]|1456[file]> Single reading buffer size\n";
167 cerr << "\t-i <URI> . . . . . . . . Input medium spec\n";
168 cerr << "\t-o <URI> . . . . . . . . Output medium spec\n";
169 cerr << "\t-e . . . (conflicts with -i) Feed SRT output back to SRT input\n";
170 cerr << "\nNote: specify `transtype=file` for using TCP-like stream mode\n";
171 return 1;
172 }
173
174 string loglevel = Option<OutString>(params, "error", o_loglevel);
175 string logfa = Option<OutString>(params, "", o_logfa);
176 srt_logging::LogLevel::type lev = SrtParseLogLevel(loglevel);
177 UDT::setloglevel(lev);
178 if (logfa == "")
179 {
180 UDT::addlogfa(SRT_LOGFA_APP);
181 }
182 else
183 {
184 // Add only selected FAs
185 set<string> unknown_fas;
186 set<srt_logging::LogFA> fas = SrtParseLogFA(logfa, &unknown_fas);
187 UDT::resetlogfa(fas);
188
189 // The general parser doesn't recognize the "app" FA, we check it here.
190 if (unknown_fas.count("app"))
191 UDT::addlogfa(SRT_LOGFA_APP);
192 }
193
194 string verbo = Option<OutString>(params, "no", o_verbose);
195 if ( verbo == "" || !false_names.count(verbo) )
196 {
197 Verbose::on = true;
198 int verboch = atoi(verbo.c_str());
199 if (verboch <= 0)
200 {
201 verboch = 1;
202 }
203 else if (verboch > 2)
204 {
205 cerr << "ERROR: -v option accepts value 1 (stdout, default) or 2 (stderr)\n";
206 return 1;
207 }
208
209 if (verboch == 1)
210 {
211 Verbose::cverb = &std::cout;
212 }
213 else
214 {
215 Verbose::cverb = &std::cerr;
216 }
217 }
218
219 string chunk = Option<OutString>(params, "", o_chunksize);
220 if (chunk != "")
221 {
222 ::g_chunksize = stoi(chunk);
223 }
224
225 string srt_endpoint = args[0];
226
227 UriParser usrt(srt_endpoint);
228
229 if (usrt.scheme() != "srt")
230 {
231 cerr << "ERROR: the only one freestanding parameter should be an SRT uri.\n";
232 cerr << "Usage: " << argv[0] << " <srt-endpoint> [ -i <input> ] [ -o <output> ] [ -e ]\n";
233 return 1;
234 }
235
236 // Allowed are only one input and multiple outputs.
237 // Input-echoback is treated as a single input.
238 bool input_echoback = Option<OutString>(params, "no", o_echo) != "no";
239 string input_spec = Option<OutString>(params, "", o_input);
240
241 if (input_spec != "" && input_echoback)
242 {
243 cerr << "ERROR: input-echoback is treated as input specification, -i can't be specified together.\n";
244 return 1;
245 }
246
247 vector<string> output_spec = Option<OutList>(params, vector<string>{}, o_output);
248
249 if (!input_echoback)
250 {
251 if (input_spec == "" || output_spec.empty())
252 {
253 cerr << "ERROR: at least one input and one output must be specified (-io specifies both)\n";
254 return 1;
255 }
256 }
257
258 Verb() << "SETTINGS:";
259 Verb() << "SRT connection: " << srt_endpoint;
260 if (input_echoback)
261 {
262 Verb() << "INPUT: (from SRT connection)";
263 }
264 else
265 {
266 Verb() << "INPUT: " << input_spec;
267 }
268
269 Verb() << "OUTPUT LIST:";
270 if (input_echoback)
271 {
272 Verb() << "\t(back to SRT connection)";
273 }
274 for (auto& s: output_spec)
275 Verb() << "\t" << s;
276
277 #ifdef _MSC_VER
278 // Replacement for sigaction, just use 'signal'
279 // This may make this working kinda impaired and unexpected,
280 // but still better that not compiling at all.
281 signal(SIGINT, OnINT_SetInterrupted);
282 #else
283 struct sigaction sigIntHandler;
284
285 sigIntHandler.sa_handler = OnINT_SetInterrupted;
286 sigemptyset(&sigIntHandler.sa_mask);
287 sigIntHandler.sa_flags = 0;
288
289 sigaction(SIGINT, &sigIntHandler, NULL);
290 #endif
291
292 try
293 {
294 SrtMainLoop loop(srt_endpoint, input_echoback, input_spec, output_spec);
295 loop.run();
296 }
297 catch (std::exception& x)
298 {
299 cerr << "ERROR: " << x.what() << endl;
300 return 1;
301 }
302
303
304 return 0;
305 }
306
307 SrtMainLoop::SrtMainLoop(const string& srt_uri, bool input_echoback, const string& input_spec, const vector<string>& output_spec)
308 {
309 // Now prepare all media
310 // They use pointers instead of real variables
311 // so that the creation time can be delayed
312 // up to this moment, and the parameters prepared
313 // before passing to the constructors.
314
315 // Start with output media so that they are ready when
316 // the data come in.
317
318 for (string spec: output_spec)
319 {
320 Verb() << "Setting up output: " << spec;
321 unique_ptr<TargetMedium> m { new TargetMedium };
322 m->Setup(Target::Create(spec));
323 m_output_media.push_back(std::move(m));
324 }
325
326
327 // Start with SRT.
328
329 UriParser srtspec(srt_uri);
330 string transtype = srtspec["transtype"].deflt("live");
331
332 SrtModel m(srtspec.host(), srtspec.portno(), srtspec.parameters());
333
334 // Just to keep it unchanged.
335 string id = m_srtspec["streamid"];
336
337 Verb() << "Establishing SRT connection: " << srt_uri;
338
339 ::g_pending_model = &m;
340 m.Establish((id));
341
342 ::g_program_established = true;
343 ::g_pending_model = nullptr;
344
345 Verb() << "... Established. configuring other pipes:";
346
347 // Once it's ready, use it to initialize the medium.
348 bool file_mode = (transtype == "file");
349 if (g_chunksize == 0)
350 {
351 if (file_mode)
352 g_chunksize = g_default_file_chunksize;
353 else
354 g_chunksize = g_default_live_chunksize;
355
356 Verb() << "DEFAULT CHUNKSIZE used: " << g_chunksize;
357 }
358
359 m_srt_relay.reset(new SrtRelay);
360 m_srt_relay->StealFrom(m);
361
362 m_srt_source.Setup(m_srt_relay.get(), g_chunksize);
363
364 // Now check the input medium
365 if (input_echoback)
366 {
367 Verb() << "SRT set up as input source and the first output target";
368
369 // Add SRT medium to output targets, and keep input medium empty.
370 unique_ptr<TargetMedium> med { new TargetMedium };
371 med->Setup(m_srt_relay.get());
372 m_output_media.push_back(std::move(med));
373 }
374 else
375 {
376 // Initialize input medium and do not add SRT medium
377 // to the output list, as this will be fed directly
378 // by the data from this input medium in a separate engine.
379 Verb() << "Setting up input: " << input_spec;
380 m_input_medium.Setup(Source::Create(input_spec), g_chunksize);
381
382 if (!file_mode)
383 {
384 // Also set writing to SRT non-blocking always.
385 bool no = false;
386 srt_setsockflag(m_srt_relay->Socket(), SRTO_SNDSYN, &no, sizeof no);
387 }
388 }
389
390 // We're done here.
391 Verb() << "MEDIA SUCCESSFULLY CREATED.";
392 }
393
394 void SrtMainLoop::InputRunner()
395 {
396 srt::ThreadName::set("InputRN");
397 // An extra thread with a loop that reads from the external input
398 // and writes into the SRT medium. When echoback mode is used,
399 // this thread isn't started at all and instead the SRT reading
400 // serves as both SRT reading and input reading.
401
402 auto on_return_set = OnReturnSet(m_input_running, false);
403
404 Verb() << VerbLock << "RUNNING INPUT LOOP";
405 for (;;)
406 {
407 applog.Debug() << "SrtMainLoop::InputRunner: extracting...";
408 auto data = m_input_medium.Extract();
409
410 if (data.payload.empty())
411 {
412 Verb() << "INPUT READING INTERRUPTED.";
413 break;
414 }
415
416 //Verb() << "INPUT [" << data.size() << "] " << VerbNoEOL;
417 applog.Debug() << "SrtMainLoop::InputRunner: [" << data.payload.size() << "] CLIENT -> SRT-RELAY";
418 m_srt_relay->Write(data);
419 }
420 }
421
422 void SrtMainLoop::run()
423 {
424 // Start the media runners.
425
426 Verb() << VerbLock << "STARTING OUTPUT threads:";
427
428 for (auto& o: m_output_media)
429 o->run();
430
431 Verb() << VerbLock << "STARTING SRT INPUT LOOP";
432 m_srt_source.run();
433
434 Verb() << VerbLock << "STARTING INPUT ";
435 if (m_input_medium.med)
436 {
437 m_input_medium.run();
438 m_input_running = true;
439
440 std::ostringstream tns;
441 tns << "Input:" << this;
442 srt::ThreadName tn(tns.str());
443 m_input_thr = thread([this] {
444 try {
445 InputRunner();
446 } catch (...) {
447 m_input_xp = std::current_exception();
448 }
449
450 Verb() << "INPUT: thread exit";
451 });
452 }
453
454 Verb() << VerbLock << "RUNNING SRT MEDIA LOOP";
455 for (;;)
456 {
457 applog.Debug() << "SrtMainLoop::run: SRT-RELAY: extracting...";
458 auto data = m_srt_source.Extract();
459
460 if (data.payload.empty())
461 {
462 Verb() << "SRT READING INTERRUPTED.";
463 break;
464 }
465
466 vector<string> output_report;
467 bool any = false;
468 int no = 1;
469
470 for (auto i = m_output_media.begin(), i_next = i; i != m_output_media.end(); i = i_next)
471 {
472 ++i_next;
473 auto& o = *i;
474 applog.Debug() << "SrtMainLoop::run: [" << data.payload.size() << "] SRT-RELAY: resending to output #" << no << "...";
475 if (!o->Schedule(data))
476 {
477 if (Verbose::on)
478 {
479 ostringstream os;
480 os << " --XXX-> <" << no << ">";
481 output_report.push_back(os.str());
482 }
483 m_output_media.erase(i);
484 continue;
485 }
486
487 if (Verbose::on)
488 {
489 ostringstream os;
490 os << " --> <" << no << ">";
491 output_report.push_back(os.str());
492 }
493 any = true;
494 ++no;
495 }
496 applog.Debug() << "SrtMainLoop::run: [" << data.payload.size() << "] SRT-RELAY -> OUTPUTS: " << Printable(output_report);
497
498 if (Verbose::on)
499 {
500 string outputs;
501 for (auto& r: output_report)
502 outputs += " " + r;
503 if (!any)
504 outputs = " --> * (no output)";
505
506 Verb() << VerbLock << "SRT [" << data.payload.size() << "] " << outputs;
507 }
508 }
509
510 Verb() << "MEDIA LOOP EXIT";
511 for (auto& m : m_output_media)
512 {
513 m->quit();
514 }
515 m_input_medium.quit();
516 m_srt_source.quit();
517
518 if (m_input_xp)
519 {
520 try {
521 std::rethrow_exception(m_input_xp);
522 } catch (std::exception& x) {
523 cerr << "INPUT EXIT BY EXCEPTION: " << x.what() << endl;
524 } catch (...) {
525 cerr << "INPUT EXIT BY UNKNOWN EXCEPTION\n";
526 }
527 }
528 }