Debian Codegraph @

testing/srt-test-multiplex.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
44 using namespace std;
45
46 // The length of the SRT payload used in srt_recvmsg call.
47 // So far, this function must be used and up to this length of payload.
48 const size_t DEFAULT_CHUNK = 1316;
49
50 srt_logging::Logger applog(SRT_LOGFA_APP, srt_logger_config, "srt-mplex");
51
52 volatile bool siplex_int_state = false;
53 void OnINT_SetIntState(int)
54 {
55 cerr << "\n-------- REQUESTED INTERRUPT!\n";
56 siplex_int_state = true;
57 }
58
59 volatile bool alarm_state = false;
60 void OnALRM_SetAlarmState(int)
61 {
62 alarm_state = true;
63 }
64
65 map<string,string> defined_streams;
66 string file_pattern = "output%.dat";
67
68 struct MediumPair
69 {
70 unique_ptr<Source> src;
71 unique_ptr<Target> tar;
72 thread runner;
73 size_t chunk = DEFAULT_CHUNK;
74 volatile bool interrupted = false;
75 volatile bool has_quit = false;
76 bytevector initial_portion;
77 string name;
78
79 MediumPair(unique_ptr<Source> s, unique_ptr<Target> t): src(std::move(s)), tar(std::move(t)) {}
80
81 void Stop()
82 {
83 interrupted = true;
84 runner.join();
85 src.reset();
86 tar.reset();
87 }
88
89 void TransmissionLoop()
90 {
91 struct MarkQuit
92 {
93 volatile bool& q;
94
95 ~MarkQuit()
96 {
97 q = true;
98 applog.Note() << "MediumPair: Giving it 5 seconds delay before exiting";
99 this_thread::sleep_for(chrono::seconds(5));
100 }
101 } mq { has_quit };
102
103 applog.Note() << "STARTING TRANSMiSSION: " << name;
104
105 if (!initial_portion.empty())
106 {
107 tar->Write(initial_portion);
108 if (tar->Broken())
109 {
110 applog.Note() << "OUTPUT BROKEN for loop: " << name;
111 return;
112 }
113 initial_portion.clear();
114 }
115
116 try
117 {
118 for (;;)
119 {
120 ostringstream sout;
121 alarm(1);
122 auto data = src->Read(chunk);
123
124 alarm(0);
125 if (alarm_state)
126 {
127 alarm_state = false;
128 // This means that it's just a checkpoint.
129 if ( interrupted )
130 break;
131 continue;
132 }
133 sout << " << " << data.payload.size() << " -> ";
134 if ( data.payload.empty() && src->End() )
135 {
136 sout << "EOS";
137 applog.Note() << sout.str();
138 break;
139 }
140 tar->Write(data);
141 if (tar->Broken())
142 {
143 sout << " OUTPUT broken";
144 applog.Note() << sout.str();
145 break;
146 }
147 sout << " sent";
148 if ( siplex_int_state )
149 {
150 sout << " --- (interrupted on request)";
151 applog.Note() << sout.str();
152 break;
153 }
154 applog.Note() << sout.str();
155 }
156 }
157 catch (const Source::ReadEOF&)
158 {
159 applog.Note() << "EOS - closing media for loop: " << name;
160 src->Close();
161 tar->Close();
162 applog.Note() << "CLOSED: " << name;
163 }
164 catch (const std::runtime_error& x)
165 {
166 applog.Note() << "INTERRUPTED: " << x.what();
167 src->Close();
168 tar->Close();
169 applog.Note() << "CLOSED: " << name;
170 }
171 catch (...)
172 {
173 applog.Note() << "UNEXPECTED EXCEPTION, rethrowing";
174 throw;
175 }
176 }
177 };
178
179
180 class MediaBase
181 {
182 public:
183 list<MediumPair> media;
184
185 /// Take the Source and Target and bind them for a transmission.
186 /// This spawns a thread for transmission.
187 /// @param src source medium
188 /// @param tar target medium
189 /// @param initial_portion First portion of data read from @c src for any extra checks, which
190 /// are still meant to be delivered to @c tar
191 MediumPair& Link(std::unique_ptr<Source> src, std::unique_ptr<Target> tar, bytevector&& initial_portion, string name, string thread_name)
192 {
193 media.emplace_back(std::move(src), std::move(tar));
194 MediumPair& med = media.back();
195 med.initial_portion = std::move(initial_portion);
196 med.name = name;
197
198 // Ok, got this, so we can start transmission.
199 srt::ThreadName tn(thread_name);
200
201 med.runner = thread( [&med]() { med.TransmissionLoop(); });
202 return med;
203 }
204
205 void StopAll()
206 {
207 for (auto& x: media)
208 x.Stop();
209 }
210
211 ~MediaBase()
212 {
213 StopAll();
214 }
215
216 } g_media_base;
217
218 string ResolveFilePattern(int number)
219 {
220 vector<string> parts;
221 Split(::file_pattern, '%', back_inserter(parts));
222 ostringstream os;
223 os << parts[0];
224 for (auto i = parts.begin()+1; i < parts.end(); ++i)
225 os << number << *i;
226
227 return os.str();
228 }
229
230 string SelectMedium(string id, bool mode_output)
231 {
232 static int number = 0;
233
234 // Empty ID is incorrect.
235 if ( id == "" )
236 {
237 applog.Error() << "SelectMedium: empty id";
238 return "";
239 }
240
241 string uri = map_get(defined_streams, id);
242
243 // Test the URI if it is openable.
244 UriParser u(uri);
245 if ( u.scheme() == "file" && u.path() == "" )
246 {
247 if (mode_output)
248 {
249 ++number;
250 string sol = ResolveFilePattern(number);
251 applog.Warn() << "SelectMedium: for [" << id << "] uri '" << uri << "' is file with no path - autogenerating filename: " << sol;
252 return sol;
253 }
254 applog.Error() << "SelectMedium: id not found: [" << id << "]";
255 return "";
256 }
257
258 applog.Note() << "SelectMedium: for [" << id << "] found medium: " << uri;
259 return uri;
260 }
261
262 bool PrepareStreamNames(const map<string,vector<string>>& params, bool mode_output)
263 {
264 vector<string> v;
265 string flag;
266
267 if (mode_output)
268 {
269 // You have an incoming stream over SRT and you need to
270 // redirect it to the correct locally defined output stream.
271
272 if (params.count("o") && !params.at("o").empty())
273 {
274 // We have a defined list of parameters.
275 // Check if there's just one item and it's a file pattern
276
277 // Each stream needs to be defined separately, at least to have IDs
278 // If this is a file without path, use the default file pattern.
279
280 v = params.at("o");
281 flag = "o";
282 }
283 }
284 else
285 {
286 // You have some input media and you want to send them all
287 // over SRT medium.
288 if (params.count("i"))
289 {
290 v = params.at("i");
291 flag = "i";
292 }
293 }
294
295 if ( v.empty() )
296 return false;
297
298 for (string& s: v)
299 {
300 UriParser u(s);
301 string id = u["id"];
302 if ( id != "" )
303 {
304 defined_streams[id] = s;
305 }
306 else
307 {
308 cerr << "Parameter at -" << flag << " without id: " << s << endl;
309 return false;
310 }
311 }
312
313 return true;
314 }
315
316 bool SelectAndLink(SrtModel& m, string id, bool mode_output, string& w_msg)
317 {
318 // So, we have made a connection that is now contained in m.
319 // For that connection we need to select appropriate stream
320 // to send.
321 //
322 // XXX
323 // Currently only one method implemented: select appropriate number from the list.
324
325 // If SRT mode is caller, then SelectMedium will always return
326 // a nonempty string that is a key in defined_streams map.
327 // This is because in this case the id comes directly from
328 // that map's keys.
329 string medium = SelectMedium(id, mode_output);
330 if ( medium == "" )
331 {
332 // No medium available for that stream, ignore it.
333 m.Close();
334 w_msg = "No medium available for that stream";
335 return false;
336 }
337
338 // Now create a medium and store.
339 unique_ptr<Source> source;
340 unique_ptr<Target> target;
341 string name;
342 ostringstream os;
343 SRTSOCKET sock = m.Socket();
344
345 string thread_name;
346
347 if ( mode_output )
348 {
349 target = Target::Create(medium);
350 if (!target)
351 {
352 m.Close();
353 w_msg = "Unable to create target medium: " + medium;
354 return false;
355 }
356
357 // Create Source out of SrtModel and Target from the given medium
358 auto s = new SrtSource();
359 s->StealFrom(m);
360 source.reset(s);
361
362 os << m.m_host << ":" << m.m_port << "[" << id << "]%" << sock << " -> " << medium;
363 thread_name = "TL>" + medium;
364 }
365 else
366 {
367 // Create Source of given medium and Target of SrtModel.
368 source = Source::Create(medium);
369 if (!source)
370 {
371 m.Close();
372 w_msg = "Unable to create source medium: " + medium;
373 return false;
374 }
375
376 auto t = new SrtTarget();
377 t->StealFrom(m);
378 target.reset(t);
379
380 os << medium << " -> " << m.m_host << ":" << m.m_port << "[" << id << "]%" << sock;
381 thread_name = "TL<" + medium;
382 }
383
384 bytevector dummy_initial_portion;
385 g_media_base.Link(std::move(source), std::move(target), std::move(dummy_initial_portion), os.str(), thread_name);
386
387 return true;
388 }
389
390 void Stall()
391 {
392 // Call this function if everything is running in their own
393 // threads and there's nothing more to run. Check periodically
394 // if all threads are still alive, quit if all are dead.
395
396 while (!siplex_int_state)
397 {
398 this_thread::sleep_for(chrono::seconds(1));
399
400 // Check all cars if any crashed
401 for (auto i = g_media_base.media.begin(), i_next = i; i != g_media_base.media.end(); i = i_next)
402 {
403 ++i_next;
404 if (i->has_quit)
405 {
406 Verb("Found QUIT mediumpair: ", i->name, " - removing from base");
407 i->Stop();
408 g_media_base.media.erase(i);
409 }
410 }
411
412 if (g_media_base.media.empty())
413 {
414 Verb("All media have quit. Marking exit.");
415 break;
416 }
417 }
418 }
419
420
421 void Usage(string program)
422 {
423 cerr << "Usage: " << program << " <SRT URI> [-i INPUT...] [-o OUTPUT...]\n";
424 }
425
426 void Help(string program)
427 {
428 Usage(program);
429 cerr << endl;
430 cerr <<
431 "SIPLEX is a program that demonstrates two SRT features:\n"
432 " - using one UDP outgoing port for multiple connecting SRT sockets\n"
433 " - setting a resource ID on a socket visible on the listener side\n"
434 "\n"
435 "The <SRT URI> will be input or output depending on the further -i/-o option.\n"
436 "The URIs specified as -i INPUT... will be used for input and therefore SRT for output,\n"
437 "and in the other way around if you use -o OUTPUT...\n"
438 "For every such URI you must specify additionally a parameter named 'id', which will be\n"
439 "interpreted by the application and used to set resource id on an SRT socket when connecting\n"
440 "or to match with the id extracted from the accepted socket of incoming connection.\n"
441 "Example:\n"
442 "\tSender: srt-multiplex srt://remhost:2000 -i udp://:5000?id=low udp://:6000?id=high\n"
443 "\tReceiver: srt-multiplex srt://:2000 -o output-high.ts?id=high output-low.ts?id=low\n"
444 "\nHere you create a Sender which will connect to 'remhost' port 2000 using multiple SRT\n"
445 "sockets, all of which will be using the same outgoing port. Here the port is autoselected\n"
446 "by the first socket when connecting, every next one will reuse that port. Alternatively you\n"
447 "can enforce the outgoing port using 'port' parameter in the SRT URI.\n\n"
448 "Then for every input resource a separate connection is made and appropriate resource id\n"
449 "will be set to particular socket assigned to that resource according to the 'id' parameter.\n"
450 "When the listener side (here Receiver) gets the socket accepted, it will have the resource\n"
451 "id set just as the caller side did, in which case srt-multiplex will search for this id among\n"
452 "the registered resources and match the resource (output here) with this id. If the resource is\n"
453 "not found, the connection is closed immediately. This works the same way regardless of which\n"
454 "direction is used by caller or listener\n";
455
456 }
457
458 int main( int argc, char** argv )
459 {
460 // This is mainly required on Windows to initialize the network system,
461 // for a case when the instance would use UDP. SRT does it on its own, independently.
462 if ( !SysInitializeNetwork() )
463 throw std::runtime_error("Can't initialize network!");
464
465 // Initialize signals
466
467 signal_alarm(OnALRM_SetAlarmState);
468 signal(SIGINT, OnINT_SetIntState);
469 signal(SIGTERM, OnINT_SetIntState);
470
471 // Symmetrically, this does a cleanup; put into a local destructor to ensure that
472 // it's called regardless of how this function returns.
473 struct NetworkCleanup
474 {
475 ~NetworkCleanup()
476 {
477 SysCleanupNetwork();
478 }
479 } cleanupobj;
480
481 const OptionName
482 o_loglevel = { "ll", "loglevel" },
483 o_input = { "i" },
484 o_output = { "o" };
485
486 vector<OptionScheme> optargs = {
487 { o_loglevel, OptionScheme::ARG_ONE },
488 { o_input, OptionScheme::ARG_VAR },
489 { o_output, OptionScheme::ARG_VAR }
490 };
491
492 map<string, vector<string>> params = ProcessOptions(argv, argc, optargs);
493
494 // The call syntax is:
495 //
496 // srt-multiplex <SRT URI> -o/-i ARGS...
497 //
498 // SRT URI should contain:
499 // srt://[host]:port?mode=MODE&adapter=ADAPTER&port=PORT&otherparameters...
500 //
501 // Extra parameters:
502 //
503 // mode: caller/listener/rendezvous. Default: if host empty, listener, otherwise caller.
504 // adapter: IP to select network device for listener or rendezvous. Default: for listener taken from host, otherwise 0.0.0.0
505 // port: default=0. Used only for caller mode, sets the outgoing port number. If 0, system-selected (default behavior)
506 //
507 // Syntax cases for -i:
508 //
509 // Every item from ARGS... is an input URI. For every such case a new socket should be
510 // created and the data should be transmitted through that socket.
511 //
512 // Syntax cases for -o:
513 //
514 // EMPTY ARGS...: use 'output%.dat' file pattern for every stream.
515 // PATTERN (one argument that contains % somewhere): define the output file pattern
516 // URI...: try to match the input stream to particular URI by 'name' parameter. If none matches, ignore.
517
518 if ( params.count("-help") )
519 {
520 Help(argv[0]);
521 return 1;
522 }
523
524 if ( params[""].empty() )
525 {
526 Usage(argv[0]);
527 return 1;
528 }
529
530 if (params[""].size() > 1)
531 {
532 cerr << "Extra parameter after the first one: " << Printable(params[""]) << endl;
533 return 1;
534 }
535
536 // Force exist
537 (void)params["o"];
538 (void)params["i"];
539
540 if (!params["o"].empty() && !params["i"].empty())
541 {
542 cerr << "Input-output mixed mode not supported. Specify either -i or -o.\n";
543 return 1;
544 }
545
546 bool mode_output = false;
547
548 if (params["i"].empty())
549 {
550 mode_output = true;
551 }
552
553 if ( !PrepareStreamNames(params, mode_output))
554 {
555 cerr << "Incorrect input/output specification\n";
556 return 1;
557 }
558
559 if ( defined_streams.empty() )
560 {
561 cerr << "No streams defined\n";
562 return 1;
563 }
564
565 string loglevel = Option<OutString>(params, "error", "ll", "loglevel");
566 srt_logging::LogLevel::type lev = SrtParseLogLevel(loglevel);
567 srt::setloglevel(lev);
568 srt::addlogfa(SRT_LOGFA_APP);
569
570 string verbo = Option<OutString>(params, "no", "v", "verbose");
571 if ( verbo == "" || !false_names.count(verbo) )
572 Verbose::on = true;
573
574
575 string srt_uri = params[""][0];
576
577 UriParser up(srt_uri);
578
579 if ( up.scheme() != "srt" )
580 {
581 cerr << "First parameter must be a SRT-scheme URI\n";
582 return 1;
583 }
584
585 int iport = atoi(up.port().c_str());
586 if ( iport < 1024 )
587 {
588 cerr << "Port value invalid: " << iport << " - must be >=1024\n";
589 return 1;
590 }
591
592 SrtModel m(up.host(), iport, up.parameters());
593
594 srt::ThreadName::set("main");
595
596 // Note: for input, there must be an exactly defined
597 // number of sources. The loop rolls up to all these sources.
598 //
599 // For output, if you use defined output URI, roll the loop until
600 // they are all managed.
601 // If you use file pattern, then:
602 // - if SRT is in listener mode, just listen infinitely
603 // - if SRT is in caller mode, the limit number of the streams must be used. Default is 10.
604
605 set<string> ids;
606 for (auto& mp: defined_streams)
607 ids.insert(mp.first);
608
609 try
610 {
611 for(;;)
612 {
613 string id = *ids.begin();
614 m.Establish((id));
615
616 // The 'id' could have been altered.
617 // If Establish did connect(), then it gave this stream id,
618 // in which case it will return unchanged. If it did accept(),
619 // then it will be overwritten with the received stream id.
620 // Whatever the result was, we need to bind the transmitter with
621 // the local resource of this id, and if this failed, simply
622 // close the stream and ignore it.
623
624 string msg;
625 // Select medium from parameters.
626 if (SelectAndLink((m), id, mode_output, (msg)))
627 {
628 ids.erase(id);
629 if (ids.empty())
630 break;
631 }
632 else
633 {
634 applog.Error() << "Unable to select a link for id=" << id << ": " << msg;
635 }
636
637 srt::ThreadName::set("main");
638 }
639
640 applog.Note() << "All local stream definitions covered. Waiting for interrupt/broken all connections.";
641 Stall();
642 }
643 catch (std::exception& x)
644 {
645 cerr << "CATCH!\n" << x.what() << endl;;
646 }
647 }
648
649
650
651
652