mirror of
https://github.com/hyperion-project/hyperion.ng.git
synced 2023-10-10 13:36:59 +02:00
acdf733936
* Creating Audio Grabber Creating Audio Grabber Creating Audio Grabber. Successfully began capturing audio in windows. Starting to implement a hard-coded UV Visualizer. Got Windows DirectSound Implementation working. Hardcoded basic VU Meter. Begin working on linux audio grabber implementation. Finished Linux Draft Implementation. Minor Mods to windows implementation. Windows: - Free memory used by device id. - Prevent starting audio if the grabber is disabled - More debug logging Linux: - Prevent starting audio if the grabber is disabled Added strings to english Removed "custom" from device selection Made hard-coded visualizer values configurable. wrote values to imageData with BGR priority to enable configurable values to be set in RGB format. created logic to support "Automatic" to enable the API to select the default device. Add language key for audio in "Remote Control" section. Removed audio configuration for number of channels. This was causing an error with some devices. Fixed logic to update capture while its active. Optimizing code . UI Tweaks Destructuring. Fixed build error on linux. Custom Effects - Clean-ups and Enhancements (#1163) * Cleanup EffectFileHandler * Support Custom Effect Schemas and align EffectFileHandler * Change back to colon prefix for system effects * WebSockets - Fix error in handling fragmented frames * Correct missing colon updates * Update json with image file location for custom gif effects * Image effect deletion - considere full filename is stored in JSON * Correct selection lists indentions Creating Audio Grabber Creating Audio Grabber Creating Audio Grabber. Successfully began capturing audio in windows. Starting to implement a hard-coded UV Visualizer. Got Windows DirectSound Implementation working. Hardcoded basic VU Meter. Begin working on linux audio grabber implementation. Finished Linux Draft Implementation. Minor Mods to windows implementation. Windows: - Free memory used by device id. - Prevent starting audio if the grabber is disabled - More debug logging Linux: - Prevent starting audio if the grabber is disabled Added strings to english Removed "custom" from device selection Made hard-coded visualizer values configurable. wrote values to imageData with BGR priority to enable configurable values to be set in RGB format. created logic to support "Automatic" to enable the API to select the default device. Add language key for audio in "Remote Control" section. Removed audio configuration for number of channels. This was causing an error with some devices. Fixed logic to update capture while its active. Optimizing code . UI Tweaks Destructuring. Fixed build error on linux. Commented setVideoMode from AudioGrabber. Linux Threading changes. Implementing new API Continuing to implement audio into new APIs Fixed Audio Grabber for DirectSound on Windows Fixed UI for Audio Grabber Configuration Default AUDIO to off unless specified. fixed missing #ifdef for audio grabber. Added logic to calculate a dynamic multiplier from the signal input. Updating linux api for discovering devices. Fixed HTML/JS issues with view. Fixed NPE in Windows. Disabled setting thread priority in linux. updated the schema options check to pass through hidden states and commented the change. Updated grabber start conditions Updated Audio grabber to instantiate similar to video grabber Updated windows grabber to set "started" flag to false when shutting down. Removed "tryStart" to prevent enabling audio capture unnecessarily. Fixing instance audio grabber device configuration Added configurable resolution Reduced tolerance to 5% Fixed issue where grabber failed for additional instances when "start" was called multiple times. Fixed resolution calculation Change averaging algorithm to prevent overflowing the sum. Updated logic to stop audio grabber when disabled. Fix integer casting and rounding. Restart grabber on configuration change. Fix missing include/grabber/AudioGrabber. Disable tolerance. Added configurable tolerance. Fixed tolerance algorithm. reset multiplier on configuration change. Line Endings Proposed change and questions/request to fix implementing more of LordGrey's suggestions. Fix mode for snd_pcm_open. Latest ALSA uses SND_PCM_NONBLOCK instead of SND_PCM_OPEN_NONBLOCK defaulted multiplier to 0 "auto" defaulted tolerance to 20% changed 100 to 100.0 for pixel value percentage calculation to fix value from being 0. missed a 100 as a double so precision isn't lost during math operation. Fix Windows grabber and further cleanups Enable Audio grabbing in standard build Remove empty methods Fix audio capture priority setting Remove unused code Clean-up default config Allow additional json-editor attributes Allow multiple effects and resetting to defaults Correct default values Allow to build for Qt < 5.14 Update CodeQL build dependency Update build dependencies Remove effect1 placeholder * Renamed uvMeter to VU Meter (Volume Unit) - Fixed issues flagged by code scanning bot. * Moved stop call into destructor of implementing class. * Removed commented linux audio channel configuration logic. --------- Co-authored-by: Michael Rochelle <michael@j2inn.com>
546 lines
16 KiB
C++
546 lines
16 KiB
C++
// STL includes
|
|
#include <chrono>
|
|
|
|
// project includes
|
|
#include <forwarder/MessageForwarder.h>
|
|
|
|
// hyperion includes
|
|
#include <hyperion/Hyperion.h>
|
|
|
|
// utils includes
|
|
#include <utils/Logger.h>
|
|
#include <utils/NetUtils.h>
|
|
|
|
// qt includes
|
|
#include <QTcpSocket>
|
|
#include <QHostInfo>
|
|
#include <QNetworkInterface>
|
|
#include <QThread>
|
|
|
|
#include <flatbufserver/FlatBufferConnection.h>
|
|
|
|
// mDNS discover
|
|
#ifdef ENABLE_MDNS
|
|
#include <mdns/MdnsBrowser.h>
|
|
#include <mdns/MdnsServiceRegister.h>
|
|
#endif
|
|
|
|
// Constants
|
|
namespace {
|
|
|
|
const int DEFAULT_FORWARDER_FLATBUFFFER_PRIORITY = 140;
|
|
|
|
constexpr std::chrono::milliseconds CONNECT_TIMEOUT{500}; // JSON-socket connect timeout in ms
|
|
|
|
} //End of constants
|
|
|
|
MessageForwarder::MessageForwarder(Hyperion* hyperion)
|
|
: _hyperion(hyperion)
|
|
, _log(nullptr)
|
|
, _muxer(_hyperion->getMuxerInstance())
|
|
, _forwarder_enabled(false)
|
|
, _priority(DEFAULT_FORWARDER_FLATBUFFFER_PRIORITY)
|
|
, _messageForwarderFlatBufHelper(nullptr)
|
|
{
|
|
QString subComponent = hyperion->property("instance").toString();
|
|
_log= Logger::getInstance("NETFORWARDER", subComponent);
|
|
|
|
qRegisterMetaType<TargetHost>("TargetHost");
|
|
|
|
#ifdef ENABLE_MDNS
|
|
QMetaObject::invokeMethod(&MdnsBrowser::getInstance(), "browseForServiceType",
|
|
Qt::QueuedConnection, Q_ARG(QByteArray, MdnsServiceRegister::getServiceType("jsonapi")));
|
|
#endif
|
|
|
|
// get settings updates
|
|
connect(_hyperion, &Hyperion::settingsChanged, this, &MessageForwarder::handleSettingsUpdate);
|
|
|
|
// component changes
|
|
connect(_hyperion, &Hyperion::compStateChangeRequest, this, &MessageForwarder::handleCompStateChangeRequest);
|
|
|
|
// connect with Muxer visible priority changes
|
|
connect(_muxer, &PriorityMuxer::visiblePriorityChanged, this, &MessageForwarder::handlePriorityChanges);
|
|
}
|
|
|
|
MessageForwarder::~MessageForwarder()
|
|
{
|
|
stopJsonTargets();
|
|
stopFlatbufferTargets();
|
|
}
|
|
|
|
|
|
void MessageForwarder::handleSettingsUpdate(settings::type type, const QJsonDocument& config)
|
|
{
|
|
if (type == settings::NETFORWARD)
|
|
{
|
|
const QJsonObject& obj = config.object();
|
|
|
|
bool isForwarderEnabledinSettings = obj["enable"].toBool(false);
|
|
enableTargets(isForwarderEnabledinSettings, obj);
|
|
}
|
|
}
|
|
|
|
void MessageForwarder::handleCompStateChangeRequest(hyperion::Components component, bool enable)
|
|
{
|
|
if (component == hyperion::COMP_FORWARDER && _forwarder_enabled != enable)
|
|
{
|
|
Info(_log, "Forwarder is %s", (enable ? "enabled" : "disabled"));
|
|
QJsonDocument config {_hyperion->getSetting(settings::type::NETFORWARD)};
|
|
enableTargets(enable, config.object());
|
|
}
|
|
}
|
|
|
|
void MessageForwarder::enableTargets(bool enable, const QJsonObject& config)
|
|
{
|
|
if (!enable)
|
|
{
|
|
_forwarder_enabled = false;
|
|
stopJsonTargets();
|
|
stopFlatbufferTargets();
|
|
|
|
}
|
|
else
|
|
{
|
|
int jsonTargetNum = startJsonTargets(config);
|
|
int flatbufTargetNum = startFlatbufferTargets(config);
|
|
|
|
if (flatbufTargetNum > 0)
|
|
{
|
|
hyperion::Components activeCompId = _hyperion->getPriorityInfo(_hyperion->getCurrentPriority()).componentId;
|
|
|
|
switch (activeCompId) {
|
|
case hyperion::COMP_GRABBER:
|
|
connect(_hyperion, &Hyperion::forwardSystemProtoMessage, this, &MessageForwarder::forwardFlatbufferMessage, Qt::UniqueConnection);
|
|
break;
|
|
case hyperion::COMP_V4L:
|
|
connect(_hyperion, &Hyperion::forwardV4lProtoMessage, this, &MessageForwarder::forwardFlatbufferMessage, Qt::UniqueConnection);
|
|
break;
|
|
case hyperion::COMP_AUDIO:
|
|
connect(_hyperion, &Hyperion::forwardAudioProtoMessage, this, &MessageForwarder::forwardFlatbufferMessage, Qt::UniqueConnection);
|
|
break;
|
|
#if defined(ENABLE_FLATBUF_SERVER)
|
|
case hyperion::COMP_FLATBUFSERVER:
|
|
#endif
|
|
#if defined(ENABLE_PROTOBUF_SERVER)
|
|
case hyperion::COMP_PROTOSERVER:
|
|
#endif
|
|
#if defined(ENABLE_FLATBUF_SERVER) || defined(ENABLE_PROTOBUF_SERVER)
|
|
|
|
connect(_hyperion, &Hyperion::forwardBufferMessage, this, &MessageForwarder::forwardFlatbufferMessage, Qt::UniqueConnection);
|
|
break;
|
|
#endif
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (jsonTargetNum > 0 || flatbufTargetNum > 0)
|
|
{
|
|
_forwarder_enabled = true;
|
|
}
|
|
else
|
|
{
|
|
_forwarder_enabled = false;
|
|
Warning(_log,"No JSON- nor Flatbuffer-Forwarder configured -> Forwarding disabled");
|
|
}
|
|
}
|
|
_hyperion->setNewComponentState(hyperion::COMP_FORWARDER, _forwarder_enabled);
|
|
}
|
|
|
|
void MessageForwarder::handlePriorityChanges(int priority)
|
|
{
|
|
if (priority != 0 && _forwarder_enabled)
|
|
{
|
|
hyperion::Components activeCompId = _hyperion->getPriorityInfo(priority).componentId;
|
|
|
|
switch (activeCompId) {
|
|
case hyperion::COMP_GRABBER:
|
|
disconnect(_hyperion, &Hyperion::forwardV4lProtoMessage, nullptr, nullptr);
|
|
disconnect(_hyperion, &Hyperion::forwardAudioProtoMessage, nullptr, nullptr);
|
|
#if defined(ENABLE_FLATBUF_SERVER) || defined(ENABLE_PROTOBUF_SERVER)
|
|
disconnect(_hyperion, &Hyperion::forwardBufferMessage, nullptr, nullptr);
|
|
#endif
|
|
connect(_hyperion, &Hyperion::forwardSystemProtoMessage, this, &MessageForwarder::forwardFlatbufferMessage, Qt::UniqueConnection);
|
|
break;
|
|
case hyperion::COMP_V4L:
|
|
disconnect(_hyperion, &Hyperion::forwardSystemProtoMessage, nullptr, nullptr);
|
|
disconnect(_hyperion, &Hyperion::forwardAudioProtoMessage, nullptr, nullptr);
|
|
#if defined(ENABLE_FLATBUF_SERVER) || defined(ENABLE_PROTOBUF_SERVER)
|
|
disconnect(_hyperion, &Hyperion::forwardBufferMessage, nullptr, nullptr);
|
|
#endif
|
|
connect(_hyperion, &Hyperion::forwardV4lProtoMessage, this, &MessageForwarder::forwardFlatbufferMessage, Qt::UniqueConnection);
|
|
break;
|
|
case hyperion::COMP_AUDIO:
|
|
disconnect(_hyperion, &Hyperion::forwardSystemProtoMessage, nullptr, nullptr);
|
|
disconnect(_hyperion, &Hyperion::forwardV4lProtoMessage, nullptr, nullptr);
|
|
#if defined(ENABLE_FLATBUF_SERVER) || defined(ENABLE_PROTOBUF_SERVER)
|
|
disconnect(_hyperion, &Hyperion::forwardBufferMessage, nullptr, nullptr);
|
|
#endif
|
|
connect(_hyperion, &Hyperion::forwardAudioProtoMessage, this, &MessageForwarder::forwardFlatbufferMessage, Qt::UniqueConnection);
|
|
break;
|
|
#if defined(ENABLE_FLATBUF_SERVER)
|
|
case hyperion::COMP_FLATBUFSERVER:
|
|
#endif
|
|
#if defined(ENABLE_PROTOBUF_SERVER)
|
|
case hyperion::COMP_PROTOSERVER:
|
|
#endif
|
|
#if defined(ENABLE_FLATBUF_SERVER) || defined(ENABLE_PROTOBUF_SERVER)
|
|
disconnect(_hyperion, &Hyperion::forwardAudioProtoMessage, nullptr, nullptr);
|
|
disconnect(_hyperion, &Hyperion::forwardSystemProtoMessage, nullptr, nullptr);
|
|
disconnect(_hyperion, &Hyperion::forwardV4lProtoMessage, nullptr, nullptr);
|
|
connect(_hyperion, &Hyperion::forwardBufferMessage, this, &MessageForwarder::forwardFlatbufferMessage, Qt::UniqueConnection);
|
|
break;
|
|
#endif
|
|
default:
|
|
disconnect(_hyperion, &Hyperion::forwardSystemProtoMessage, nullptr, nullptr);
|
|
disconnect(_hyperion, &Hyperion::forwardV4lProtoMessage, nullptr, nullptr);
|
|
disconnect(_hyperion, &Hyperion::forwardAudioProtoMessage, nullptr, nullptr);
|
|
#if defined(ENABLE_FLATBUF_SERVER) || defined(ENABLE_PROTOBUF_SERVER)
|
|
disconnect(_hyperion, &Hyperion::forwardBufferMessage, nullptr, nullptr);
|
|
#endif
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
void MessageForwarder::addJsonTarget(const QJsonObject& targetConfig)
|
|
{
|
|
TargetHost targetHost;
|
|
|
|
QString hostName = targetConfig["host"].toString();
|
|
int port = targetConfig["port"].toInt();
|
|
|
|
if (!hostName.isEmpty())
|
|
{
|
|
if (NetUtils::resolveHostToAddress(_log, hostName, targetHost.host, port))
|
|
{
|
|
QString address = targetHost.host.toString();
|
|
if (hostName != address)
|
|
{
|
|
Info(_log, "Resolved hostname [%s] to address [%s]", QSTRING_CSTR(hostName), QSTRING_CSTR(address));
|
|
}
|
|
|
|
if (NetUtils::isValidPort(_log, port, targetHost.host.toString()))
|
|
{
|
|
targetHost.port = static_cast<quint16>(port);
|
|
|
|
// verify loop with JSON-server
|
|
const QJsonObject& obj = _hyperion->getSetting(settings::JSONSERVER).object();
|
|
if ((QNetworkInterface::allAddresses().indexOf(targetHost.host) != -1) && targetHost.port == static_cast<quint16>(obj["port"].toInt()))
|
|
{
|
|
Error(_log, "Loop between JSON-Server and Forwarder! Configuration for host: %s, port: %d is ignored.", QSTRING_CSTR(targetHost.host.toString()), port);
|
|
}
|
|
else
|
|
{
|
|
if (_jsonTargets.indexOf(targetHost) == -1)
|
|
{
|
|
Debug(_log, "JSON-Forwarder settings: Adding target host: %s port: %u", QSTRING_CSTR(targetHost.host.toString()), targetHost.port);
|
|
_jsonTargets << targetHost;
|
|
}
|
|
else
|
|
{
|
|
Warning(_log, "JSON-Forwarder settings: Duplicate target host configuration! Configuration for host: %s, port: %d is ignored.", QSTRING_CSTR(targetHost.host.toString()), targetHost.port);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
int MessageForwarder::startJsonTargets(const QJsonObject& config)
|
|
{
|
|
if (!config["jsonapi"].isNull())
|
|
{
|
|
_jsonTargets.clear();
|
|
const QJsonArray& addr = config["jsonapi"].toArray();
|
|
|
|
#ifdef ENABLE_MDNS
|
|
if (!addr.isEmpty())
|
|
{
|
|
QMetaObject::invokeMethod(&MdnsBrowser::getInstance(), "browseForServiceType",
|
|
Qt::QueuedConnection, Q_ARG(QByteArray, MdnsServiceRegister::getServiceType("jsonapi")));
|
|
}
|
|
#endif
|
|
|
|
for (const auto& entry : addr)
|
|
{
|
|
addJsonTarget(entry.toObject());
|
|
}
|
|
|
|
if (!_jsonTargets.isEmpty())
|
|
{
|
|
for (const auto& targetHost : qAsConst(_jsonTargets))
|
|
{
|
|
Info(_log, "Forwarding now to JSON-target host: %s port: %u", QSTRING_CSTR(targetHost.host.toString()), targetHost.port);
|
|
}
|
|
|
|
connect(_hyperion, &Hyperion::forwardJsonMessage, this, &MessageForwarder::forwardJsonMessage, Qt::UniqueConnection);
|
|
}
|
|
}
|
|
return _jsonTargets.size();
|
|
}
|
|
|
|
|
|
void MessageForwarder::stopJsonTargets()
|
|
{
|
|
if (!_jsonTargets.isEmpty())
|
|
{
|
|
disconnect(_hyperion, &Hyperion::forwardJsonMessage, nullptr, nullptr);
|
|
for (const auto& targetHost : qAsConst(_jsonTargets))
|
|
{
|
|
Info(_log, "Stopped forwarding to JSON-target host: %s port: %u", QSTRING_CSTR(targetHost.host.toString()), targetHost.port);
|
|
}
|
|
_jsonTargets.clear();
|
|
}
|
|
}
|
|
|
|
void MessageForwarder::addFlatbufferTarget(const QJsonObject& targetConfig)
|
|
{
|
|
TargetHost targetHost;
|
|
|
|
QString hostName = targetConfig["host"].toString();
|
|
int port = targetConfig["port"].toInt();
|
|
|
|
if (!hostName.isEmpty())
|
|
{
|
|
if (NetUtils::resolveHostToAddress(_log, hostName, targetHost.host, port))
|
|
{
|
|
QString address = targetHost.host.toString();
|
|
if (hostName != address)
|
|
{
|
|
Info(_log, "Resolved hostname [%s] to address [%s]", QSTRING_CSTR(hostName), QSTRING_CSTR(address));
|
|
}
|
|
|
|
if (NetUtils::isValidPort(_log, port, targetHost.host.toString()))
|
|
{
|
|
targetHost.port = static_cast<quint16>(port);
|
|
|
|
// verify loop with Flatbuffer-server
|
|
const QJsonObject& obj = _hyperion->getSetting(settings::FLATBUFSERVER).object();
|
|
if ((QNetworkInterface::allAddresses().indexOf(targetHost.host) != -1) && targetHost.port == static_cast<quint16>(obj["port"].toInt()))
|
|
{
|
|
Error(_log, "Loop between Flatbuffer-Server and Forwarder! Configuration for host: %s, port: %d is ignored.", QSTRING_CSTR(targetHost.host.toString()), port);
|
|
}
|
|
else
|
|
{
|
|
if (_flatbufferTargets.indexOf(targetHost) == -1)
|
|
{
|
|
Debug(_log, "Flatbuffer-Forwarder settings: Adding target host: %s port: %u", QSTRING_CSTR(targetHost.host.toString()), targetHost.port);
|
|
_flatbufferTargets << targetHost;
|
|
|
|
if (_messageForwarderFlatBufHelper != nullptr)
|
|
{
|
|
emit _messageForwarderFlatBufHelper->addClient("Forwarder", targetHost, _priority, false);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Warning(_log, "Flatbuffer Forwarder settings: Duplicate target host configuration! Configuration for host: %s, port: %d is ignored.", QSTRING_CSTR(targetHost.host.toString()), targetHost.port);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
int MessageForwarder::startFlatbufferTargets(const QJsonObject& config)
|
|
{
|
|
if (!config["flatbuffer"].isNull())
|
|
{
|
|
if (_messageForwarderFlatBufHelper == nullptr)
|
|
{
|
|
_messageForwarderFlatBufHelper = new MessageForwarderFlatbufferClientsHelper();
|
|
}
|
|
else
|
|
{
|
|
emit _messageForwarderFlatBufHelper->clearClients();
|
|
}
|
|
_flatbufferTargets.clear();
|
|
|
|
const QJsonArray& addr = config["flatbuffer"].toArray();
|
|
|
|
#ifdef ENABLE_MDNS
|
|
if (!addr.isEmpty())
|
|
{
|
|
QMetaObject::invokeMethod(&MdnsBrowser::getInstance(), "browseForServiceType",
|
|
Qt::QueuedConnection, Q_ARG(QByteArray, MdnsServiceRegister::getServiceType("flatbuffer")));
|
|
}
|
|
#endif
|
|
for (const auto& entry : addr)
|
|
{
|
|
addFlatbufferTarget(entry.toObject());
|
|
}
|
|
|
|
if (!_flatbufferTargets.isEmpty())
|
|
{
|
|
for (const auto& targetHost : qAsConst(_flatbufferTargets))
|
|
{
|
|
Info(_log, "Forwarding now to Flatbuffer-target host: %s port: %u", QSTRING_CSTR(targetHost.host.toString()), targetHost.port);
|
|
}
|
|
}
|
|
}
|
|
return _flatbufferTargets.size();
|
|
}
|
|
|
|
void MessageForwarder::stopFlatbufferTargets()
|
|
{
|
|
if (!_flatbufferTargets.isEmpty())
|
|
{
|
|
disconnect(_hyperion, &Hyperion::forwardSystemProtoMessage, nullptr, nullptr);
|
|
disconnect(_hyperion, &Hyperion::forwardV4lProtoMessage, nullptr, nullptr);
|
|
disconnect(_hyperion, &Hyperion::forwardAudioProtoMessage, nullptr, nullptr);
|
|
#if defined(ENABLE_FLATBUF_SERVER) || defined(ENABLE_PROTOBUF_SERVER)
|
|
disconnect(_hyperion, &Hyperion::forwardBufferMessage, nullptr, nullptr);
|
|
#endif
|
|
|
|
if (_messageForwarderFlatBufHelper != nullptr)
|
|
{
|
|
delete _messageForwarderFlatBufHelper;
|
|
_messageForwarderFlatBufHelper = nullptr;
|
|
}
|
|
|
|
for (const auto& targetHost : qAsConst(_flatbufferTargets))
|
|
{
|
|
Info(_log, "Stopped forwarding to Flatbuffer-target host: %s port: %u", QSTRING_CSTR(targetHost.host.toString()), targetHost.port);
|
|
}
|
|
_flatbufferTargets.clear();
|
|
}
|
|
}
|
|
|
|
void MessageForwarder::forwardJsonMessage(const QJsonObject& message)
|
|
{
|
|
if (_forwarder_enabled)
|
|
{
|
|
QTcpSocket client;
|
|
for (const auto& targetHost : qAsConst(_jsonTargets))
|
|
{
|
|
client.connectToHost(targetHost.host, targetHost.port);
|
|
if (client.waitForConnected(CONNECT_TIMEOUT.count()))
|
|
{
|
|
sendJsonMessage(message, &client);
|
|
client.close();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
void MessageForwarder::forwardFlatbufferMessage(const QString& /*name*/, const Image<ColorRgb>& image)
|
|
{
|
|
if (_messageForwarderFlatBufHelper != nullptr)
|
|
{
|
|
bool isfree = _messageForwarderFlatBufHelper->isFree();
|
|
|
|
if (isfree && _forwarder_enabled)
|
|
{
|
|
QMetaObject::invokeMethod(_messageForwarderFlatBufHelper, "forwardImage", Qt::QueuedConnection, Q_ARG(Image<ColorRgb>, image));
|
|
}
|
|
}
|
|
}
|
|
|
|
void MessageForwarder::sendJsonMessage(const QJsonObject& message, QTcpSocket* socket)
|
|
{
|
|
// for hyperion classic compatibility
|
|
QJsonObject jsonMessage = message;
|
|
if (jsonMessage.contains("tan") && jsonMessage["tan"].isNull())
|
|
{
|
|
jsonMessage["tan"] = 100;
|
|
}
|
|
|
|
// serialize message
|
|
QJsonDocument writer(jsonMessage);
|
|
QByteArray serializedMessage = writer.toJson(QJsonDocument::Compact) + "\n";
|
|
|
|
// write message
|
|
socket->write(serializedMessage);
|
|
if (!socket->waitForBytesWritten())
|
|
{
|
|
Debug(_log, "Error while writing data to host");
|
|
return;
|
|
}
|
|
|
|
// read reply data
|
|
QByteArray serializedReply;
|
|
while (!serializedReply.contains('\n'))
|
|
{
|
|
// receive reply
|
|
if (!socket->waitForReadyRead())
|
|
{
|
|
Debug(_log, "Error while writing data from host");
|
|
return;
|
|
}
|
|
|
|
serializedReply += socket->readAll();
|
|
}
|
|
|
|
// parse reply data
|
|
QJsonParseError error;
|
|
/* QJsonDocument reply = */ QJsonDocument::fromJson(serializedReply, &error);
|
|
|
|
if (error.error != QJsonParseError::NoError)
|
|
{
|
|
Error(_log, "Error while parsing reply: invalid JSON");
|
|
return;
|
|
}
|
|
}
|
|
|
|
MessageForwarderFlatbufferClientsHelper::MessageForwarderFlatbufferClientsHelper()
|
|
{
|
|
QThread* mainThread = new QThread();
|
|
mainThread->setObjectName("ForwarderHelperThread");
|
|
this->moveToThread(mainThread);
|
|
mainThread->start();
|
|
|
|
_free = true;
|
|
connect(this, &MessageForwarderFlatbufferClientsHelper::addClient, this, &MessageForwarderFlatbufferClientsHelper::addClientHandler);
|
|
connect(this, &MessageForwarderFlatbufferClientsHelper::clearClients, this, &MessageForwarderFlatbufferClientsHelper::clearClientsHandler);
|
|
}
|
|
|
|
MessageForwarderFlatbufferClientsHelper::~MessageForwarderFlatbufferClientsHelper()
|
|
{
|
|
_free=false;
|
|
while (!_forwardClients.isEmpty())
|
|
{
|
|
_forwardClients.takeFirst()->deleteLater();
|
|
}
|
|
|
|
|
|
QThread* oldThread = this->thread();
|
|
disconnect(oldThread, nullptr, nullptr, nullptr);
|
|
oldThread->quit();
|
|
oldThread->wait();
|
|
delete oldThread;
|
|
}
|
|
|
|
void MessageForwarderFlatbufferClientsHelper::addClientHandler(const QString& origin, const TargetHost& targetHost, int priority, bool skipReply)
|
|
{
|
|
FlatBufferConnection* flatbuf = new FlatBufferConnection(origin, targetHost.host.toString(), priority, skipReply, targetHost.port);
|
|
_forwardClients << flatbuf;
|
|
_free = true;
|
|
}
|
|
|
|
void MessageForwarderFlatbufferClientsHelper::clearClientsHandler()
|
|
{
|
|
while (!_forwardClients.isEmpty())
|
|
{
|
|
delete _forwardClients.takeFirst();
|
|
}
|
|
_free = false;
|
|
}
|
|
|
|
bool MessageForwarderFlatbufferClientsHelper::isFree() const
|
|
{
|
|
return _free;
|
|
}
|
|
|
|
void MessageForwarderFlatbufferClientsHelper::forwardImage(const Image<ColorRgb>& image)
|
|
{
|
|
_free = false;
|
|
|
|
for (int i = 0; i < _forwardClients.size(); i++)
|
|
{
|
|
_forwardClients.at(i)->setImage(image);
|
|
}
|
|
|
|
_free = true;
|
|
}
|