| 1 |
#include <string> |
| 2 |
#include <sstream> |
| 3 |
#include <vector> |
| 4 |
#include <list> |
| 5 |
#include <memory> |
| 6 |
#include <exception> |
| 7 |
#include <thread> |
| 8 |
#include <mutex> |
| 9 |
#include <condition_variable> |
| 10 |
|
| 11 |
#include "testmedia.hpp" |
| 12 |
#include "logsupport.hpp" |
| 13 |
|
| 14 |
#define SRT_ENABLE_VERBOSE_LOCK 1 |
| 15 |
#include "verbose.hpp" |
| 16 |
|
| 17 |
#include "logging.h" |
| 18 |
#include "threadname.h" |
| 19 |
|
| 20 |
extern srt_logging::Logger applog; |
| 21 |
|
| 22 |
template<class MediumDir> |
| 23 |
struct Medium |
| 24 |
{ |
| 25 |
MediumDir* med = nullptr; |
| 26 |
std::unique_ptr<MediumDir> pinned_med; |
| 27 |
std::list<MediaPacket> buffer; |
| 28 |
std::mutex buffer_lock; |
| 29 |
std::thread thr; |
| 30 |
std::condition_variable ready; |
| 31 |
srt::sync::atomic<bool> running {false}; |
| 32 |
std::exception_ptr xp; // To catch exception thrown by a thread |
| 33 |
|
| 34 |
virtual void Runner() = 0; |
| 35 |
|
| 36 |
void RunnerBase() |
| 37 |
{ |
| 38 |
try |
| 39 |
{ |
| 40 |
running = true; |
| 41 |
Runner(); |
| 42 |
} |
| 43 |
catch (...) |
| 44 |
{ |
| 45 |
xp = std::current_exception(); |
| 46 |
} |
| 47 |
|
| 48 |
//Verb() << "Medium: " << this << ": thread exit"; |
| 49 |
std::unique_lock<std::mutex> g(buffer_lock); |
| 50 |
running = false; |
| 51 |
ready.notify_all(); |
| 52 |
//Verb() << VerbLock << "Medium: EXIT NOTIFIED"; |
| 53 |
} |
| 54 |
|
| 55 |
void run() |
| 56 |
{ |
| 57 |
running = true; |
| 58 |
std::ostringstream tns; |
| 59 |
tns << typeid(*this).name() << ":" << this; |
| 60 |
srt::ThreadName tn(tns.str()); |
| 61 |
thr = std::thread( [this] { RunnerBase(); } ); |
| 62 |
} |
| 63 |
|
| 64 |
void quit() |
| 65 |
{ |
| 66 |
if (!med) |
| 67 |
return; |
| 68 |
|
| 69 |
LOGP(applog.Debug, "Medium(", typeid(*med).name(), ") quit. Buffer contains ", buffer.size(), " blocks"); |
| 70 |
|
| 71 |
std::string name; |
| 72 |
if (Verbose::on) |
| 73 |
name = typeid(*med).name(); |
| 74 |
|
| 75 |
med->Close(); |
| 76 |
if (thr.joinable()) |
| 77 |
{ |
| 78 |
LOGP(applog.Debug, "Medium::quit: Joining medium thread (", name, ") ..."); |
| 79 |
thr.join(); |
| 80 |
LOGP(applog.Debug, "... done"); |
| 81 |
} |
| 82 |
|
| 83 |
if (xp) |
| 84 |
{ |
| 85 |
try { |
| 86 |
std::rethrow_exception(xp); |
| 87 |
} catch (TransmissionError& e) { |
| 88 |
if (Verbose::on) |
| 89 |
Verb() << VerbLock << "Medium " << this << " exited with Transmission Error:\n\t" << e.what(); |
| 90 |
else |
| 91 |
std::cerr << "Transmission Error: " << e.what() << std::endl; |
| 92 |
} catch (...) { |
| 93 |
if (Verbose::on) |
| 94 |
Verb() << VerbLock << "Medium " << this << " exited with UNKNOWN EXCEPTION:"; |
| 95 |
else |
| 96 |
std::cerr << "UNKNOWN EXCEPTION on medium\n"; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
// Prevent further quits from running |
| 101 |
med = nullptr; |
| 102 |
} |
| 103 |
|
| 104 |
void Setup(MediumDir* t) |
| 105 |
{ |
| 106 |
med = t; |
| 107 |
// Leave pinned_med as 0 |
| 108 |
} |
| 109 |
|
| 110 |
void Setup(std::unique_ptr<MediumDir>&& medbase) |
| 111 |
{ |
| 112 |
pinned_med = std::move(medbase); |
| 113 |
med = pinned_med.get(); |
| 114 |
} |
| 115 |
|
| 116 |
virtual ~Medium() |
| 117 |
{ |
| 118 |
//Verb() << "Medium: " << this << " DESTROYED. Threads quit."; |
| 119 |
quit(); |
| 120 |
} |
| 121 |
|
| 122 |
virtual void Start() { run(); } |
| 123 |
virtual void Stop() { quit(); } |
| 124 |
}; |
| 125 |
|
| 126 |
struct SourceMedium: Medium<Source> |
| 127 |
{ |
| 128 |
size_t chunksize_ = 0; |
| 129 |
typedef Medium<Source> Base; |
| 130 |
|
| 131 |
// Source Runner: read payloads and put on the buffer |
| 132 |
void Runner() override; |
| 133 |
|
| 134 |
// External user: call this to get the buffer. |
| 135 |
MediaPacket Extract(); |
| 136 |
|
| 137 |
template<class Arg> |
| 138 |
void Setup(Arg&& medium, size_t chunksize) |
| 139 |
{ |
| 140 |
chunksize_ = chunksize; |
| 141 |
return Base::Setup(std::move(medium)); |
| 142 |
} |
| 143 |
}; |
| 144 |
|
| 145 |
struct TargetMedium: Medium<Target> |
| 146 |
{ |
| 147 |
void Runner() override; |
| 148 |
|
| 149 |
bool Schedule(const MediaPacket& data); |
| 150 |
|
| 151 |
void Clear() |
| 152 |
{ |
| 153 |
std::lock_guard<std::mutex> lg(buffer_lock); |
| 154 |
buffer.clear(); |
| 155 |
} |
| 156 |
|
| 157 |
void Interrupt() |
| 158 |
{ |
| 159 |
std::lock_guard<std::mutex> lg(buffer_lock); |
| 160 |
running = false; |
| 161 |
ready.notify_one(); |
| 162 |
} |
| 163 |
|
| 164 |
~TargetMedium() |
| 165 |
{ |
| 166 |
//Verb() << "TargetMedium: DESTROYING"; |
| 167 |
Interrupt(); |
| 168 |
// ~Medium will do quit() additionally, which joins the thread |
| 169 |
} |
| 170 |
}; |
| 171 |
|
| 172 |
|