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