-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathmonitoring_backend.cpp
97 lines (86 loc) · 3.26 KB
/
monitoring_backend.cpp
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
#include "monitoring_backend.hpp"
#include "communication/monitoring_metadata_receiver.hpp"
#include "detail/constants.hpp"
#include <functional>
using std::placeholders::_1;
using std::placeholders::_2;
namespace ear {
namespace plugin {
MonitoringBackend::MonitoringBackend(
ui::MonitoringFrontendBackendConnector* connector,
const Layout& targetLayout, int inputChannelCount)
: gainsCalculator_(targetLayout, inputChannelCount),
frontendConnector_(connector),
controlConnection_() {
logger_ = createLogger(fmt::format("Monitoring@{}", (const void*)this));
#ifdef EPS_ENABLE_LOGGING
logger_->set_level(spdlog::level::trace);
#else
logger_->set_level(spdlog::level::off);
#endif
gains_.direct = gainsCalculator_.directGains();
gains_.diffuse = gainsCalculator_.diffuseGains();
controlConnection_.logger(logger_);
controlConnection_.onConnectionEstablished(
std::bind(&MonitoringBackend::onConnection, this, _1, _2));
controlConnection_.onConnectionLost(
std::bind(&MonitoringBackend::onConnectionLost, this));
controlConnection_.start(detail::SCENE_MASTER_CONTROL_ENDPOINT);
}
MonitoringBackend::~MonitoringBackend() {
if (metadataReceiver_) {
metadataReceiver_->shutdown();
}
// remove connection signal handlers
// this is required so the controlConnection_ does not try to invoke the
// registered member function which might easily use already destructed
// members. Another option might be to introduce a `stop()` method on the
// InputControlConnection class, but probably
// the expected behaviour of this would be to call any "disconnect" handlers
// on `stop()` as well, so this wouldn't help here?
controlConnection_.onConnectionLost(nullptr);
controlConnection_.onConnectionEstablished(nullptr);
}
void MonitoringBackend::onSceneReceived(const proto::SceneStore& store) {
isExporting_ = store.has_is_exporting() && store.is_exporting();
updateActiveGains(store);
}
GainHolder MonitoringBackend::currentGains() {
std::lock_guard<std::mutex> lock(gainsMutex_);
return gains_;
}
void MonitoringBackend::updateActiveGains(const proto::SceneStore& store) {
{
std::lock_guard<std::mutex> lock(gainsCalculatorMutex_);
gainsCalculator_.update(store);
}
{
std::lock_guard<std::mutex> lock(gainsMutex_);
gains_.direct = gainsCalculator_.directGains();
gains_.diffuse = gainsCalculator_.diffuseGains();
}
}
void MonitoringBackend::onConnection(communication::ConnectionId id,
const std::string& streamEndpoint) {
try {
logger_->info(
"Connected to Scene ({}), will now listening for metadata from "
"{}",
id.string(), streamEndpoint);
metadataReceiver_ =
std::make_unique<communication::MonitoringMetadataReceiver>(logger_);
metadataReceiver_->start(
streamEndpoint,
std::bind(&MonitoringBackend::onSceneReceived, this, _1));
} catch (const std::runtime_error& e) {
logger_->error("Failed to start stream receiver: {}", e.what());
}
}
void MonitoringBackend::onConnectionLost() {
logger_->info("Lost connection to Scene");
metadataReceiver_->shutdown();
// force update with an "empty" store to generate silence
updateActiveGains(proto::SceneStore{});
}
} // namespace plugin
} // namespace ear