Skip to content

Commit ee9f83d

Browse files
Use QT Signal in FlatBufferConnection
1 parent 6fd3b8e commit ee9f83d

File tree

4 files changed

+37
-37
lines changed

4 files changed

+37
-37
lines changed

src/FlatBufferConnection.cpp

Lines changed: 7 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
#include "hyperion_reply_generated.h"
1212
#include "hyperion_request_generated.h"
1313

14-
#define QSTRING_CSTR(str) str.toLocal8Bit().constData()
15-
1614
FlatBufferConnection::FlatBufferConnection(const QString& origin, const QString& host, int priority, quint16 port)
1715
: _socket()
1816
, _origin(origin)
@@ -23,7 +21,7 @@ FlatBufferConnection::FlatBufferConnection(const QString& origin, const QString&
2321
, _registered(false)
2422
{
2523
connect(&_socket, &QTcpSocket::readyRead, this, &FlatBufferConnection::readData, Qt::UniqueConnection);
26-
connect(&_socket, &QTcpSocket::disconnected, [=]() { this->logMessage("Connection to Hyperion server was closed"); });
24+
connect(&_socket, &QTcpSocket::disconnected, this, &FlatBufferConnection::serverDisconnected);
2725

2826
// init connect
2927
connectToHost();
@@ -69,18 +67,18 @@ void FlatBufferConnection::readData()
6967
const hyperionnet::Reply* reply = hyperionnet::GetReply(msgData);
7068
if (!parseReply(reply))
7169
{
72-
logMessage(QSTRING_CSTR(QString("Reply received with error: %1").arg(reply->error()->c_str())));
70+
emit logMessage(QString("Reply received with error: %1").arg(reply->error()->c_str()));
7371
}
7472
continue;
7573
}
7674

77-
logMessage("Unable to parse reply");
75+
emit logMessage("Unable to parse reply");
7876
}
7977
}
8078

8179
void FlatBufferConnection::setRegister(const QString& origin, int priority)
8280
{
83-
auto registerReq = hyperionnet::CreateRegister(_builder, _builder.CreateString(QSTRING_CSTR(origin)), priority);
81+
auto registerReq = hyperionnet::CreateRegister(_builder, _builder.CreateString(origin.toLocal8Bit().constData()), priority);
8482
auto req = hyperionnet::CreateRequest(_builder, hyperionnet::Command_Register, registerReq.Union());
8583

8684
_builder.Finish(req);
@@ -127,13 +125,13 @@ void FlatBufferConnection::sendMessage(const uint8_t* buffer, uint32_t size)
127125
switch (_socket.state() )
128126
{
129127
case QAbstractSocket::UnconnectedState:
130-
logMessage(QString("No connection to Hyperion: %1:%2").arg(_host).arg(_port));
128+
emit logMessage(QString("No connection to Hyperion: %1:%2").arg(_host).arg(_port));
131129
break;
132130
case QAbstractSocket::ConnectedState:
133-
logMessage(QString("Connected to Hyperion: %1:%2").arg(_host).arg(_port));
131+
emit logMessage(QString("Connected to Hyperion: %1:%2").arg(_host).arg(_port));
134132
break;
135133
default:
136-
logMessage(QString("Connecting to Hyperion: %1:%2").arg(_host).arg(_port));
134+
emit logMessage(QString("Connecting to Hyperion: %1:%2").arg(_host).arg(_port));
137135
break;
138136
}
139137
_prevSocketState = _socket.state();
@@ -184,13 +182,3 @@ bool FlatBufferConnection::parseReply(const hyperionnet::Reply *reply)
184182
}
185183
return false;
186184
}
187-
188-
void FlatBufferConnection::logMessage(const QString& message)
189-
{
190-
struct calldata call_data;
191-
calldata_init(&call_data);
192-
calldata_set_string(&call_data, "msg", QSTRING_CSTR(message));
193-
signal_handler_t *handler = hyperion_get_signal_handler();
194-
signal_handler_signal(handler, "log", &call_data);
195-
calldata_free(&call_data);
196-
}

src/FlatBufferConnection.h

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515

1616
#include <flatbuffers/flatbuffers.h>
1717

18-
#include "hyperion-obs.h"
19-
2018
const int FLATBUFFER_DEFAULT_PORT = 19400;
2119

2220
namespace hyperionnet
@@ -66,12 +64,17 @@ public slots:
6664
///
6765
void setImage(const Image<ColorRgb> &image);
6866

69-
// signals:
67+
signals:
7068

71-
// ///
72-
// /// @brief Emits whenever the server disconnected
73-
// ///
74-
// void serverDisconnected();
69+
///
70+
/// @brief Emits whenever the server disconnected
71+
///
72+
void serverDisconnected();
73+
74+
///
75+
/// @brief Emits log messages in the UI
76+
///
77+
void logMessage(const QString& message);
7578

7679
private slots:
7780
///
@@ -93,8 +96,6 @@ private slots:
9396
///
9497
bool parseReply(const hyperionnet::Reply *reply);
9598

96-
void logMessage(const QString& message);
97-
9899
private:
99100
/// The TCP-Socket with the connection to the server
100101
QTcpSocket _socket;

src/hyperion-obs.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,27 +61,38 @@ void hyperion_signal_log(const char *msg)
6161
calldata_free(&call_data);
6262
}
6363

64-
void Connect(void *data)
64+
static void Disconnect(void *data)
6565
{
6666
hyperion_output *out_data = static_cast<hyperion_output*>(data);
67-
obs_data_t *settings = obs_output_get_settings(out_data->output);
68-
if (out_data->active || out_data->client != nullptr)
67+
if (out_data->client != nullptr)
6968
{
7069
delete out_data->client;
7170
out_data->client = nullptr;
7271
}
73-
74-
out_data->client = new FlatBufferConnection(OBS_MODULE_NAME, obs_data_get_string(settings, OBS_SETTINGS_ADDRESS), obs_data_get_int(settings, OBS_SETTINGS_PRIORITY), obs_data_get_int(settings, OBS_SETTINGS_PORT));
7572
}
7673

77-
static void Disconnect(void *data)
74+
void Connect(void *data)
7875
{
7976
hyperion_output *out_data = static_cast<hyperion_output*>(data);
80-
if (out_data->client != nullptr)
77+
if (out_data->active || out_data->client != nullptr)
8178
{
8279
delete out_data->client;
8380
out_data->client = nullptr;
8481
}
82+
83+
obs_data_t *settings = obs_output_get_settings(out_data->output);
84+
out_data->client = new FlatBufferConnection(OBS_MODULE_NAME, obs_data_get_string(settings, OBS_SETTINGS_ADDRESS), obs_data_get_int(settings, OBS_SETTINGS_PRIORITY), obs_data_get_int(settings, OBS_SETTINGS_PORT));
85+
86+
QObject::connect(out_data->client, &FlatBufferConnection::serverDisconnected, [=]()
87+
{
88+
hyperion_signal_stop("Connection to Hyperion server was closed", true);
89+
obs_output_end_data_capture(_hyperionOutput);
90+
});
91+
92+
QObject::connect(out_data->client, &FlatBufferConnection::logMessage, [=](const QString& message)
93+
{
94+
hyperion_signal_log(QSTRING_CSTR(message));
95+
});
8596
}
8697

8798
static const char *hyperion_output_getname(void *unused)
@@ -190,7 +201,6 @@ static void hyperion_output_raw_video(void *param, struct video_data *frame)
190201
memcpy((unsigned char*)outputImage.memptr() + y * outputImage.width() * 3, static_cast<unsigned char*>(RGBImage.scanLine(y)), RGBImage.width() * 3);
191202
}
192203

193-
194204
QMetaObject::invokeMethod(out_data->client, "setImage", Qt::QueuedConnection, Q_ARG(Image<ColorRgb>, outputImage));
195205
pthread_mutex_unlock(&out_data->mutex);
196206
}

src/hyperion-obs.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
#define HYPERIONOBS_H
33

44
#include <callback/signal.h>
5-
65
#include <QString>
76

7+
#define QSTRING_CSTR(str) str.toLocal8Bit().constData()
8+
89
const int DEFAULT_SIZEDECIMATION = 8;
910
const int DEFAULT_PRIORITY = 150;
1011

0 commit comments

Comments
 (0)