mirror of
https://github.com/Palakis/obs-websocket.git
synced 2024-08-30 18:12:16 +00:00
Config + WSServer: provide singletons from plugin entry point
This commit is contained in:
parent
3ba8a77d9a
commit
b4d89d5666
@ -38,13 +38,6 @@ with this program. If not, see <https://www.gnu.org/licenses/>
|
|||||||
|
|
||||||
#define QT_TO_UTF8(str) str.toUtf8().constData()
|
#define QT_TO_UTF8(str) str.toUtf8().constData()
|
||||||
|
|
||||||
ConfigPtr Config::_instance = ConfigPtr(new Config());
|
|
||||||
|
|
||||||
ConfigPtr Config::Current()
|
|
||||||
{
|
|
||||||
return _instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
Config::Config() :
|
Config::Config() :
|
||||||
ServerEnabled(true),
|
ServerEnabled(true),
|
||||||
ServerPort(4444),
|
ServerPort(4444),
|
||||||
@ -83,7 +76,7 @@ void Config::Load()
|
|||||||
Salt = config_get_string(obsConfig, SECTION_NAME, PARAM_SALT);
|
Salt = config_get_string(obsConfig, SECTION_NAME, PARAM_SALT);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Config::Save()
|
void Config::Save()
|
||||||
{
|
{
|
||||||
config_t* obsConfig = GetConfigStore();
|
config_t* obsConfig = GetConfigStore();
|
||||||
|
|
||||||
@ -217,7 +210,7 @@ void Config::OnFrontendEvent(enum obs_frontend_event event, void* param)
|
|||||||
config->Load();
|
config->Load();
|
||||||
|
|
||||||
if (config->ServerEnabled != previousEnabled || config->ServerPort != previousPort) {
|
if (config->ServerEnabled != previousEnabled || config->ServerPort != previousPort) {
|
||||||
auto server = WSServer::Current();
|
auto server = GetServer();
|
||||||
server->stop();
|
server->stop();
|
||||||
|
|
||||||
if (config->ServerEnabled) {
|
if (config->ServerEnabled) {
|
||||||
@ -232,18 +225,18 @@ void Config::OnFrontendEvent(enum obs_frontend_event event, void* param)
|
|||||||
Utils::SysTrayNotify(stopMessage, QSystemTrayIcon::MessageIcon::Information);
|
Utils::SysTrayNotify(stopMessage, QSystemTrayIcon::MessageIcon::Information);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void Config::MigrateFromGlobalSettings()
|
void Config::MigrateFromGlobalSettings()
|
||||||
{
|
{
|
||||||
config_t* source = obs_frontend_get_global_config();
|
config_t* source = obs_frontend_get_global_config();
|
||||||
config_t* destination = obs_frontend_get_profile_config();
|
config_t* destination = obs_frontend_get_profile_config();
|
||||||
|
|
||||||
if(config_has_user_value(source, SECTION_NAME, PARAM_ENABLE)) {
|
if(config_has_user_value(source, SECTION_NAME, PARAM_ENABLE)) {
|
||||||
bool value = config_get_bool(source, SECTION_NAME, PARAM_ENABLE);
|
bool value = config_get_bool(source, SECTION_NAME, PARAM_ENABLE);
|
||||||
config_set_bool(destination, SECTION_NAME, PARAM_ENABLE, value);
|
config_set_bool(destination, SECTION_NAME, PARAM_ENABLE, value);
|
||||||
|
|
||||||
config_remove_value(source, SECTION_NAME, PARAM_ENABLE);
|
config_remove_value(source, SECTION_NAME, PARAM_ENABLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -257,7 +250,7 @@ void Config::MigrateFromGlobalSettings()
|
|||||||
if(config_has_user_value(source, SECTION_NAME, PARAM_DEBUG)) {
|
if(config_has_user_value(source, SECTION_NAME, PARAM_DEBUG)) {
|
||||||
bool value = config_get_bool(source, SECTION_NAME, PARAM_DEBUG);
|
bool value = config_get_bool(source, SECTION_NAME, PARAM_DEBUG);
|
||||||
config_set_bool(destination, SECTION_NAME, PARAM_DEBUG, value);
|
config_set_bool(destination, SECTION_NAME, PARAM_DEBUG, value);
|
||||||
|
|
||||||
config_remove_value(source, SECTION_NAME, PARAM_DEBUG);
|
config_remove_value(source, SECTION_NAME, PARAM_DEBUG);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -23,20 +23,15 @@ with this program. If not, see <https://www.gnu.org/licenses/>
|
|||||||
#include <QtCore/QString>
|
#include <QtCore/QString>
|
||||||
#include <QtCore/QSharedPointer>
|
#include <QtCore/QSharedPointer>
|
||||||
|
|
||||||
class Config;
|
|
||||||
typedef QSharedPointer<Config> ConfigPtr;
|
|
||||||
|
|
||||||
class Config {
|
class Config {
|
||||||
public:
|
public:
|
||||||
static ConfigPtr Current();
|
|
||||||
|
|
||||||
Config();
|
Config();
|
||||||
~Config();
|
~Config();
|
||||||
void Load();
|
void Load();
|
||||||
void Save();
|
void Save();
|
||||||
void SetDefaults();
|
void SetDefaults();
|
||||||
config_t* GetConfigStore();
|
config_t* GetConfigStore();
|
||||||
|
|
||||||
void MigrateFromGlobalSettings();
|
void MigrateFromGlobalSettings();
|
||||||
|
|
||||||
void SetPassword(QString password);
|
void SetPassword(QString password);
|
||||||
@ -59,5 +54,4 @@ class Config {
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
static void OnFrontendEvent(enum obs_frontend_event event, void* param);
|
static void OnFrontendEvent(enum obs_frontend_event event, void* param);
|
||||||
static ConfigPtr _instance;
|
|
||||||
};
|
};
|
||||||
|
@ -396,7 +396,7 @@ QSystemTrayIcon* Utils::GetTrayIcon() {
|
|||||||
|
|
||||||
void Utils::SysTrayNotify(QString text,
|
void Utils::SysTrayNotify(QString text,
|
||||||
QSystemTrayIcon::MessageIcon icon, QString title) {
|
QSystemTrayIcon::MessageIcon icon, QString title) {
|
||||||
if (!Config::Current()->AlertsEnabled ||
|
if (!GetConfig()->AlertsEnabled ||
|
||||||
!QSystemTrayIcon::isSystemTrayAvailable() ||
|
!QSystemTrayIcon::isSystemTrayAvailable() ||
|
||||||
!QSystemTrayIcon::supportsMessages())
|
!QSystemTrayIcon::supportsMessages())
|
||||||
{
|
{
|
||||||
|
@ -248,7 +248,7 @@ void WSEvents::broadcastUpdate(const char* updateType,
|
|||||||
QString json = obs_data_get_json(update);
|
QString json = obs_data_get_json(update);
|
||||||
_srv->broadcast(json.toStdString());
|
_srv->broadcast(json.toStdString());
|
||||||
|
|
||||||
if (Config::Current()->DebugEnabled) {
|
if (GetConfig()->DebugEnabled) {
|
||||||
blog(LOG_INFO, "Update << '%s'", json.toUtf8().constData());
|
blog(LOG_INFO, "Update << '%s'", json.toUtf8().constData());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -142,14 +142,14 @@ WSRequestHandler::WSRequestHandler(ConnectionProperties& connProperties) :
|
|||||||
}
|
}
|
||||||
|
|
||||||
std::string WSRequestHandler::processIncomingMessage(std::string& textMessage) {
|
std::string WSRequestHandler::processIncomingMessage(std::string& textMessage) {
|
||||||
if (Config::Current()->DebugEnabled) {
|
if (GetConfig()->DebugEnabled) {
|
||||||
blog(LOG_INFO, "Request >> '%s'", textMessage.c_str());
|
blog(LOG_INFO, "Request >> '%s'", textMessage.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
OBSDataAutoRelease responseData = processRequest(textMessage);
|
OBSDataAutoRelease responseData = processRequest(textMessage);
|
||||||
std::string response = obs_data_get_json(responseData);
|
std::string response = obs_data_get_json(responseData);
|
||||||
|
|
||||||
if (Config::Current()->DebugEnabled) {
|
if (GetConfig()->DebugEnabled) {
|
||||||
blog(LOG_INFO, "Response << '%s'", response.c_str());
|
blog(LOG_INFO, "Response << '%s'", response.c_str());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -173,7 +173,7 @@ HandlerResponse WSRequestHandler::processRequest(std::string& textMessage){
|
|||||||
_requestType = obs_data_get_string(data, "request-type");
|
_requestType = obs_data_get_string(data, "request-type");
|
||||||
_messageId = obs_data_get_string(data, "message-id");
|
_messageId = obs_data_get_string(data, "message-id");
|
||||||
|
|
||||||
if (Config::Current()->AuthRequired
|
if (GetConfig()->AuthRequired
|
||||||
&& (!authNotRequired.contains(_requestType))
|
&& (!authNotRequired.contains(_requestType))
|
||||||
&& (!_connProperties.isAuthenticated()))
|
&& (!_connProperties.isAuthenticated()))
|
||||||
{
|
{
|
||||||
|
@ -53,16 +53,17 @@ HandlerResponse WSRequestHandler::HandleGetVersion(WSRequestHandler* req) {
|
|||||||
* @since 0.3
|
* @since 0.3
|
||||||
*/
|
*/
|
||||||
HandlerResponse WSRequestHandler::HandleGetAuthRequired(WSRequestHandler* req) {
|
HandlerResponse WSRequestHandler::HandleGetAuthRequired(WSRequestHandler* req) {
|
||||||
bool authRequired = Config::Current()->AuthRequired;
|
bool authRequired = GetConfig()->AuthRequired;
|
||||||
|
|
||||||
OBSDataAutoRelease data = obs_data_create();
|
OBSDataAutoRelease data = obs_data_create();
|
||||||
obs_data_set_bool(data, "authRequired", authRequired);
|
obs_data_set_bool(data, "authRequired", authRequired);
|
||||||
|
|
||||||
if (authRequired) {
|
if (authRequired) {
|
||||||
|
auto config = GetConfig();
|
||||||
obs_data_set_string(data, "challenge",
|
obs_data_set_string(data, "challenge",
|
||||||
Config::Current()->SessionChallenge.toUtf8());
|
config->SessionChallenge.toUtf8());
|
||||||
obs_data_set_string(data, "salt",
|
obs_data_set_string(data, "salt",
|
||||||
Config::Current()->Salt.toUtf8());
|
config->Salt.toUtf8());
|
||||||
}
|
}
|
||||||
|
|
||||||
return req->SendOKResponse(data);
|
return req->SendOKResponse(data);
|
||||||
@ -92,7 +93,7 @@ HandlerResponse WSRequestHandler::HandleAuthenticate(WSRequestHandler* req) {
|
|||||||
return req->SendErrorResponse("auth not specified!");
|
return req->SendErrorResponse("auth not specified!");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Config::Current()->CheckAuth(auth) == false) {
|
if (GetConfig()->CheckAuth(auth) == false) {
|
||||||
return req->SendErrorResponse("Authentication Failed.");
|
return req->SendErrorResponse("Authentication Failed.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,21 +38,6 @@ using websocketpp::lib::placeholders::_1;
|
|||||||
using websocketpp::lib::placeholders::_2;
|
using websocketpp::lib::placeholders::_2;
|
||||||
using websocketpp::lib::bind;
|
using websocketpp::lib::bind;
|
||||||
|
|
||||||
WSServerPtr WSServer::_instance = WSServerPtr(nullptr);
|
|
||||||
|
|
||||||
WSServerPtr WSServer::Current()
|
|
||||||
{
|
|
||||||
if (!_instance) {
|
|
||||||
ResetCurrent();
|
|
||||||
}
|
|
||||||
return _instance;
|
|
||||||
}
|
|
||||||
|
|
||||||
void WSServer::ResetCurrent()
|
|
||||||
{
|
|
||||||
_instance = WSServerPtr(new WSServer());
|
|
||||||
}
|
|
||||||
|
|
||||||
WSServer::WSServer()
|
WSServer::WSServer()
|
||||||
: QObject(nullptr),
|
: QObject(nullptr),
|
||||||
_connections(),
|
_connections(),
|
||||||
@ -127,7 +112,7 @@ void WSServer::stop()
|
|||||||
}
|
}
|
||||||
_connections.clear();
|
_connections.clear();
|
||||||
_connectionProperties.clear();
|
_connectionProperties.clear();
|
||||||
|
|
||||||
_threadPool.waitForDone();
|
_threadPool.waitForDone();
|
||||||
|
|
||||||
while (!_server.stopped()) {
|
while (!_server.stopped()) {
|
||||||
@ -141,7 +126,7 @@ void WSServer::broadcast(std::string message)
|
|||||||
{
|
{
|
||||||
QMutexLocker locker(&_clMutex);
|
QMutexLocker locker(&_clMutex);
|
||||||
for (connection_hdl hdl : _connections) {
|
for (connection_hdl hdl : _connections) {
|
||||||
if (Config::Current()->AuthRequired) {
|
if (GetConfig()->AuthRequired) {
|
||||||
bool authenticated = _connectionProperties[hdl].isAuthenticated();
|
bool authenticated = _connectionProperties[hdl].isAuthenticated();
|
||||||
if (!authenticated) {
|
if (!authenticated) {
|
||||||
continue;
|
continue;
|
||||||
|
@ -40,17 +40,11 @@ using websocketpp::connection_hdl;
|
|||||||
|
|
||||||
typedef websocketpp::server<websocketpp::config::asio> server;
|
typedef websocketpp::server<websocketpp::config::asio> server;
|
||||||
|
|
||||||
class WSServer;
|
|
||||||
typedef QSharedPointer<WSServer> WSServerPtr;
|
|
||||||
|
|
||||||
class WSServer : public QObject
|
class WSServer : public QObject
|
||||||
{
|
{
|
||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static WSServerPtr Current();
|
|
||||||
static void ResetCurrent();
|
|
||||||
|
|
||||||
explicit WSServer();
|
explicit WSServer();
|
||||||
virtual ~WSServer();
|
virtual ~WSServer();
|
||||||
void start(quint16 port);
|
void start(quint16 port);
|
||||||
@ -61,8 +55,6 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static WSServerPtr _instance;
|
|
||||||
|
|
||||||
void onOpen(connection_hdl hdl);
|
void onOpen(connection_hdl hdl);
|
||||||
void onMessage(connection_hdl hdl, server::message_ptr message);
|
void onMessage(connection_hdl hdl, server::message_ptr message);
|
||||||
void onClose(connection_hdl hdl);
|
void onClose(connection_hdl hdl);
|
||||||
|
@ -41,7 +41,7 @@ SettingsDialog::SettingsDialog(QWidget* parent) :
|
|||||||
}
|
}
|
||||||
|
|
||||||
void SettingsDialog::showEvent(QShowEvent* event) {
|
void SettingsDialog::showEvent(QShowEvent* event) {
|
||||||
auto conf = Config::Current();
|
auto conf = GetConfig();
|
||||||
|
|
||||||
ui->serverEnabled->setChecked(conf->ServerEnabled);
|
ui->serverEnabled->setChecked(conf->ServerEnabled);
|
||||||
ui->serverPort->setValue(conf->ServerPort);
|
ui->serverPort->setValue(conf->ServerPort);
|
||||||
@ -68,7 +68,7 @@ void SettingsDialog::AuthCheckboxChanged() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void SettingsDialog::FormAccepted() {
|
void SettingsDialog::FormAccepted() {
|
||||||
auto conf = Config::Current();
|
auto conf = GetConfig();
|
||||||
|
|
||||||
conf->ServerEnabled = ui->serverEnabled->isChecked();
|
conf->ServerEnabled = ui->serverEnabled->isChecked();
|
||||||
conf->ServerPort = ui->serverPort->value();
|
conf->ServerPort = ui->serverPort->value();
|
||||||
@ -81,7 +81,7 @@ void SettingsDialog::FormAccepted() {
|
|||||||
conf->SetPassword(ui->password->text());
|
conf->SetPassword(ui->password->text());
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!Config::Current()->Secret.isEmpty())
|
if (!GetConfig()->Secret.isEmpty())
|
||||||
conf->AuthRequired = true;
|
conf->AuthRequired = true;
|
||||||
else
|
else
|
||||||
conf->AuthRequired = false;
|
conf->AuthRequired = false;
|
||||||
@ -93,10 +93,12 @@ void SettingsDialog::FormAccepted() {
|
|||||||
|
|
||||||
conf->Save();
|
conf->Save();
|
||||||
|
|
||||||
if (conf->ServerEnabled)
|
auto server = GetServer();
|
||||||
WSServer::Current()->start(conf->ServerPort);
|
if (conf->ServerEnabled) {
|
||||||
else
|
server->start(conf->ServerPort);
|
||||||
WSServer::Current()->stop();
|
} else {
|
||||||
|
server->stop();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
SettingsDialog::~SettingsDialog() {
|
SettingsDialog::~SettingsDialog() {
|
||||||
|
@ -38,6 +38,8 @@ void ___output_dummy_addref(obs_output_t*) {}
|
|||||||
OBS_DECLARE_MODULE()
|
OBS_DECLARE_MODULE()
|
||||||
OBS_MODULE_USE_DEFAULT_LOCALE("obs-websocket", "en-US")
|
OBS_MODULE_USE_DEFAULT_LOCALE("obs-websocket", "en-US")
|
||||||
|
|
||||||
|
ConfigPtr _config;
|
||||||
|
WSServerPtr _server;
|
||||||
WSEventsPtr _eventsSystem;
|
WSEventsPtr _eventsSystem;
|
||||||
|
|
||||||
bool obs_module_load(void) {
|
bool obs_module_load(void) {
|
||||||
@ -46,14 +48,15 @@ bool obs_module_load(void) {
|
|||||||
QT_VERSION_STR, qVersion());
|
QT_VERSION_STR, qVersion());
|
||||||
|
|
||||||
// Core setup
|
// Core setup
|
||||||
auto config = Config::Current();
|
_config = ConfigPtr(new Config());
|
||||||
config->MigrateFromGlobalSettings(); // TODO remove this on the next minor jump
|
_config->MigrateFromGlobalSettings(); // TODO remove this on the next minor jump
|
||||||
config->Load();
|
_config->Load();
|
||||||
|
|
||||||
_eventsSystem = WSEventsPtr(new WSEvents(WSServer::Current()));
|
_server = WSServerPtr(new WSServer());
|
||||||
|
_eventsSystem = WSEventsPtr(new WSEvents(_server));
|
||||||
|
|
||||||
if (config->ServerEnabled) {
|
if (_config->ServerEnabled) {
|
||||||
WSServer::Current()->start(config->ServerPort);
|
_server->start(_config->ServerPort);
|
||||||
}
|
}
|
||||||
|
|
||||||
// UI setup
|
// UI setup
|
||||||
@ -79,10 +82,18 @@ bool obs_module_load(void) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void obs_module_unload() {
|
void obs_module_unload() {
|
||||||
WSServer::Current()->stop();
|
_server->stop();
|
||||||
blog(LOG_INFO, "goodbye!");
|
blog(LOG_INFO, "goodbye!");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ConfigPtr GetConfig() {
|
||||||
|
return _config;
|
||||||
|
}
|
||||||
|
|
||||||
|
WSServerPtr GetServer() {
|
||||||
|
return _server;
|
||||||
|
}
|
||||||
|
|
||||||
WSEventsPtr GetEventsSystem() {
|
WSEventsPtr GetEventsSystem() {
|
||||||
return _eventsSystem;
|
return _eventsSystem;
|
||||||
}
|
}
|
||||||
|
@ -38,10 +38,18 @@ using OBSDataArrayAutoRelease =
|
|||||||
using OBSOutputAutoRelease =
|
using OBSOutputAutoRelease =
|
||||||
OBSRef<obs_output_t*, ___output_dummy_addref, obs_output_release>;
|
OBSRef<obs_output_t*, ___output_dummy_addref, obs_output_release>;
|
||||||
|
|
||||||
|
class Config;
|
||||||
|
typedef std::shared_ptr<Config> ConfigPtr;
|
||||||
|
|
||||||
|
class WSServer;
|
||||||
|
typedef std::shared_ptr<WSServer> WSServerPtr;
|
||||||
|
|
||||||
class WSEvents;
|
class WSEvents;
|
||||||
typedef std::shared_ptr<WSEvents> WSEventsPtr;
|
typedef std::shared_ptr<WSEvents> WSEventsPtr;
|
||||||
|
|
||||||
std::shared_ptr<WSEvents> GetEventsSystem();
|
ConfigPtr GetConfig();
|
||||||
|
WSServerPtr GetServer();
|
||||||
|
WSEventsPtr GetEventsSystem();
|
||||||
|
|
||||||
#define OBS_WEBSOCKET_VERSION "4.6.0"
|
#define OBS_WEBSOCKET_VERSION "4.6.0"
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user