-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.cc
276 lines (248 loc) · 9.03 KB
/
player.cc
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include <iostream>
#include <sstream>
#include "player.h"
#include "control-protocol.h"
#include "radio-menu.h"
SessionInfo &session_info() {
static auto *session = new SessionInfo;
return *session;
}
Configuration &configuration() {
static auto *conf = new Configuration;
return *conf;
}
static void putPacket(const AudioPacket &packet) {
if (session_info().psize.lock().get() == packet.data.size()) {
session_info().buffer.lock().get()->push(packet);
}
}
static void resetSession(const AudioPacket &initial_packet) {
session_info().missing_packets.lock()->clear();
session_info().buffer.lock().get() = std::make_unique<AudioBuffer>(
configuration().bsize / initial_packet.data.size(),
initial_packet,
session_info().missing_packets
);
session_info().session_id.lock().get() = initial_packet.session_id;
session_info().psize.lock().get() = initial_packet.data.size();
session_info().initiated.lock().get() = true;
}
static bool channelIsCurrent(const udp::Address &channel) {
bool result;
{
auto _lock = current_channel().lock();
result = _lock->has_value() && *_lock.get() == channel;
}
return result;
}
void dataListener(const std::optional<udp::Address> &_channel) {
if (!_channel)
return;
udp::Address channel = *_channel;
udp::GroupReceiver receiver(channel);
while (channelIsCurrent(channel)) {
auto message = receiver.receive(100);
if (message.empty()) continue;
auto packet_op = AudioPacket::fromBytes(message);
if (!packet_op) continue;
auto packet = *packet_op;
auto initiated = session_info().initiated.lock().get();
if (initiated) {
if (packet.session_id < session_info().session_id.lock().get()) continue;
if (packet.session_id == session_info().session_id.lock().get()) {
putPacket(packet);
} else {
session_info().initiated.lock().get() = false;
resetSession(packet);
}
} else {
resetSession(packet);
}
}
}
void dataOutput() {
while (true) {
if (session_info().ready()) {
auto packet = session_info().buffer.lock().get()->pop();
if (packet) {
write(STDOUT_FILENO, packet->data.data(), packet->data.size());
} else {
session_info().initiated.lock().get() = false;
}
}
}
}
MutexValue<std::optional<udp::Address>> ¤t_channel() {
static auto *value = new MutexValue(std::optional<udp::Address>({}));
return *value;
}
MutexValue<std::list<RadioStation>> &stations() {
static auto *_stations = new MutexValue<std::list<RadioStation>>(std::list<RadioStation>());
return *_stations;
}
const unsigned MAX_LOOKUPS = 4;
void changeChannel(const std::optional<udp::Address> &address) {
current_channel().lock().get() = address;
session_info().initiated.lock().get() = false;
event_stream().write(MenuEvent(ApplicationEventType::CHANGE_CHANNEL));
}
void setNewMenu(LockedValue<std::list<RadioStation>> &_lock) {
if (_lock->empty()) {
radio_menu().lock()->setListing(nullptr);
event_stream().write(MenuEvent(ApplicationEventType::MENU_CHANGE));
return;
}
std::vector<menu::Option> options;
unsigned current_index = 0;
auto ch_lock = current_channel().lock();
unsigned i = 0;
for (auto &rs: _lock.get()) {
if (rs.channel == ch_lock.get()) current_index = i;
options.emplace_back(rs.name, [=](){ changeChannel(rs.channel); });
++i;
}
options_listing().lock().get() = std::make_unique<menu::OptionsListing>(options);
radio_menu().lock()->setListing(options_listing().lock().get().get());
options_listing().lock().get()->active_index = current_index;
event_stream().write(MenuEvent(ApplicationEventType::MENU_CHANGE));
}
void sendLookup(udp::Broadcaster &sock) {
sock.send(ctrl::LOOKUP_HEADER);
auto _lock = stations().lock();
bool to_delete = false;
for (auto &it : _lock.get()) {
if (++it.lookups >= MAX_LOOKUPS) to_delete = true;
}
if (to_delete) {
std::list<RadioStation> new_list;
std::copy_if(
_lock.get().begin(),
_lock.get().end(),
std::back_inserter(new_list),
[](const RadioStation &rs) { return rs.lookups < MAX_LOOKUPS; }
);
_lock.get() = new_list;
{
auto ch_lock = current_channel().lock();
bool deleted = !std::any_of(
new_list.begin(),
new_list.end(),
[&](const RadioStation &rs) { return rs.channel == ch_lock.get(); }
);
if (deleted) {
ch_lock.get() = (new_list.empty()) ? std::optional<udp::Address>() : new_list.front().channel;
event_stream().write(MenuEvent(ApplicationEventType::CHANGE_CHANNEL));
}
}
setNewMenu(_lock);
}
}
const unsigned LOOKUP_INTERVAL_SECONDS = 5;
const long REPLY_TIMEOUT_MILLISECONDS = 200;
static uint16_t validatePort(const std::string &port) {
unsigned long ul = stoul(port);
if (ul >= std::numeric_limits<uint16_t>::min() && ul <= std::numeric_limits<uint16_t>::max()) {
return (uint16_t) ul;
} else {
throw std::exception();
}
}
static std::string triml(const std::string &str) {
auto origin = std::find_if(str.begin(), str.end(), [](char c){ return !std::isspace(c); });
return std::string(origin, str.end());
}
std::optional<RadioStation> parseReply(const std::vector<uint8_t> &bytes) {
std::stringstream ss(std::string(bytes.begin(), bytes.end()));
std::string header;
ss >> header;
if (header != ctrl::REPLY_HEADER) return {};
std::string mcast;
std::string port_str;
uint16_t port;
ss >> mcast >> port_str;
std::unique_ptr<udp::Address> addr;
try {
port = validatePort(port_str);
} catch (...) {
return {};
}
try {
addr = std::make_unique<udp::Address>(mcast, port);
} catch (...) {
return {};
}
std::string name;
std::getline(ss, name);
name = triml(name);
if (name.size() > ctrl::MAX_NAME_LENGTH || name.empty()) return {};
return RadioStation(*addr, name);
}
void removeStation(LockedValue<std::list<RadioStation>> &list_lock, const RadioStation &station) {
list_lock->remove_if([&](const RadioStation &rs) { return rs.channel == station.channel; });
}
void insertStation(LockedValue<std::list<RadioStation>> &list_lock, const RadioStation &station) {
auto where_to_insert = list_lock->begin();
while (where_to_insert != list_lock->end()) {
if (station.name > where_to_insert->name) break;
++where_to_insert;
}
list_lock->insert(where_to_insert, station);
if ((list_lock->size() == 1 && !channelIsCurrent(station.channel)) || station.name == configuration().name) {
changeChannel(station.channel);
}
setNewMenu(list_lock);
}
void handleReply(const std::vector<uint8_t> &bytes) {
auto station = parseReply(bytes);
if (!station) return;
auto list_lock = stations().lock();
bool present = std::any_of(
list_lock->begin(),
list_lock->end(),
[&](const RadioStation &rs) { return rs.channel == station->channel; }
);
if (present) {
removeStation(list_lock, *station);
}
insertStation(list_lock, *station);
}
void discoverer() {
udp::Broadcaster sock(udp::Address(configuration().discover_ip, configuration().control_port));
while (true) {
sendLookup(sock);
auto timer = std::chrono::system_clock::now();
while (std::chrono::system_clock::now() - timer < std::chrono::seconds(LOOKUP_INTERVAL_SECONDS)) {
auto dgram = sock.receive(REPLY_TIMEOUT_MILLISECONDS);
if (dgram.data.empty()) continue;
if (!ctrl::controlPacketBytesValidation(dgram.data)) continue;
handleReply(dgram.data);
}
}
}
std::string rexmitMessage(const std::list<uint64_t> &packets) {
if (packets.empty()) return "";
std::stringstream ss;
ss << ctrl::REXMIT_HEADER << " ";
for (auto it = packets.begin(); it != std::prev(packets.end()); ++it) {
ss << *it << ',';
}
ss << packets.back() << std::endl;
return ss.str();
}
void missingPacketsManager() {
udp::Broadcaster sock(udp::Address(configuration().discover_ip, configuration().control_port));
while (true) {
std::list<uint64_t> packets_list;
{
auto _lock = session_info().missing_packets.lock();
packets_list = _lock.get();
_lock->clear();
}
auto message = rexmitMessage(packets_list);;
if (!message.empty()) sock.send(message);
std::this_thread::sleep_for(std::chrono::milliseconds(configuration().rtime));
}
}
bool SessionInfo::ready() {
return session_info().initiated.lock().get() && session_info().buffer.lock().get()->isReady();
}