| 1 |
|
| 2 |
#include "testactivemedia.hpp" |
| 3 |
|
| 4 |
using namespace std; |
| 5 |
|
| 6 |
|
| 7 |
void SourceMedium::Runner() |
| 8 |
{ |
| 9 |
srt::ThreadName::set("SourceRN"); |
| 10 |
|
| 11 |
Verb() << VerbLock << "Starting SourceMedium: " << this; |
| 12 |
for (;;) |
| 13 |
{ |
| 14 |
auto input = med->Read(chunksize_); |
| 15 |
if (input.payload.empty() && med->End()) |
| 16 |
{ |
| 17 |
Verb() << VerbLock << "Exiting SourceMedium: " << this; |
| 18 |
return; |
| 19 |
} |
| 20 |
LOGP(applog.Debug, "SourceMedium(", typeid(*med).name(), "): [", input.payload.size(), "] MEDIUM -> BUFFER. signal(", &ready, ")"); |
| 21 |
|
| 22 |
lock_guard<std::mutex> g(buffer_lock); |
| 23 |
buffer.push_back(input); |
| 24 |
ready.notify_one(); |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
MediaPacket SourceMedium::Extract() |
| 29 |
{ |
| 30 |
unique_lock<std::mutex> g(buffer_lock); |
| 31 |
for (;;) |
| 32 |
{ |
| 33 |
if (::transmit_int_state) |
| 34 |
running = false; |
| 35 |
|
| 36 |
if (!buffer.empty()) |
| 37 |
{ |
| 38 |
MediaPacket top; |
| 39 |
swap(top, *buffer.begin()); |
| 40 |
buffer.pop_front(); |
| 41 |
LOGP(applog.Debug, "SourceMedium(", typeid(*med).name(), "): [", top.payload.size(), "] BUFFER -> CLIENT"); |
| 42 |
return top; |
| 43 |
} |
| 44 |
else |
| 45 |
{ |
| 46 |
// Don't worry about the media status as long as you have something in the buffer. |
| 47 |
// Purge the buffer first, then worry about the other things. |
| 48 |
if (!running) |
| 49 |
{ |
| 50 |
//LOGP(applog.Debug, "Extract(", typeid(*med).name(), "): INTERRUPTED READING"); |
| 51 |
//Verb() << "SourceMedium " << this << " not running"; |
| 52 |
return {}; |
| 53 |
} |
| 54 |
|
| 55 |
} |
| 56 |
|
| 57 |
// Block until ready |
| 58 |
//LOGP(applog.Debug, "Extract(", typeid(*med).name(), "): ", this, " wait(", &ready, ") -->"); |
| 59 |
|
| 60 |
ready.wait_for(g, chrono::seconds(1), [this] { return running && !buffer.empty(); }); |
| 61 |
|
| 62 |
// LOGP(applog.Debug, "Extract(", typeid(*med).name(), "): ", this, " <-- notified (running:" |
| 63 |
// << boolalpha << running << " buffer:" << buffer.size() << ")"); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
void TargetMedium::Runner() |
| 68 |
{ |
| 69 |
srt::ThreadName::set("TargetRN"); |
| 70 |
auto on_return_set = OnReturnSet(running, false); |
| 71 |
Verb() << VerbLock << "Starting TargetMedium: " << this; |
| 72 |
for (;;) |
| 73 |
{ |
| 74 |
MediaPacket val; |
| 75 |
{ |
| 76 |
unique_lock<std::mutex> lg(buffer_lock); |
| 77 |
if (buffer.empty()) |
| 78 |
{ |
| 79 |
if (!running) |
| 80 |
{ |
| 81 |
//LOGP(applog.Debug, "TargetMedium(", typeid(*med).name(), "): buffer empty, medium stopped, exiting."); |
| 82 |
return; |
| 83 |
} |
| 84 |
|
| 85 |
bool gotsomething = ready.wait_for(lg, chrono::seconds(1), [this] { return !running || !buffer.empty(); } ); |
| 86 |
LOGP(applog.Debug, "TargetMedium(", typeid(*med).name(), "): [", val.payload.size(), "] BUFFER update (timeout:", |
| 87 |
boolalpha, gotsomething, " running: ", running, ")"); |
| 88 |
if (::transmit_int_state || !running || !med || med->Broken()) |
| 89 |
{ |
| 90 |
LOGP(applog.Debug, "TargetMedium(", typeid(*med).name(), "): buffer empty, medium ", |
| 91 |
(!::transmit_int_state ? |
| 92 |
(running ? |
| 93 |
(med ? |
| 94 |
(med->Broken() ? "broken" : "UNKNOWN") |
| 95 |
: "deleted") |
| 96 |
: "stopped") |
| 97 |
: "killed")); |
| 98 |
return; |
| 99 |
} |
| 100 |
if (!gotsomething) // exit on timeout |
| 101 |
continue; |
| 102 |
} |
| 103 |
swap(val, *buffer.begin()); |
| 104 |
LOGP(applog.Debug, "TargetMedium(", typeid(*med).name(), "): [", val.payload.size(), "] BUFFER extraction"); |
| 105 |
|
| 106 |
buffer.pop_front(); |
| 107 |
} |
| 108 |
|
| 109 |
// Check before writing |
| 110 |
if (med->Broken()) |
| 111 |
{ |
| 112 |
LOGP(applog.Debug, "TargetMedium(", typeid(*med).name(), "): [", val.payload.size(), "] BUFFER -> DISCARDED (medium broken)"); |
| 113 |
running = false; |
| 114 |
return; |
| 115 |
} |
| 116 |
|
| 117 |
LOGP(applog.Debug, "TargetMedium(", typeid(*med).name(), "): [", val.payload.size(), "] BUFFER -> MEDIUM"); |
| 118 |
// You get the data to send, send them. |
| 119 |
med->Write(val); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
bool TargetMedium::Schedule(const MediaPacket& data) |
| 124 |
{ |
| 125 |
LOGP(applog.Debug, "TargetMedium::Schedule LOCK ... "); |
| 126 |
std::lock_guard<std::mutex> lg(buffer_lock); |
| 127 |
LOGP(applog.Debug, "TargetMedium::Schedule LOCKED - checking: running=", running, " interrupt=", ::transmit_int_state); |
| 128 |
if (!running || ::transmit_int_state) |
| 129 |
{ |
| 130 |
LOGP(applog.Debug, "TargetMedium::Schedule: not running, discarding packet"); |
| 131 |
return false; |
| 132 |
} |
| 133 |
|
| 134 |
LOGP(applog.Debug, "TargetMedium(", typeid(*med).name(), "): Schedule: [", data.payload.size(), "] CLIENT -> BUFFER"); |
| 135 |
buffer.push_back(data); |
| 136 |
ready.notify_one(); |
| 137 |
return true; |
| 138 |
} |
| 139 |
|