WSEvents: remove singleton + refactor pointer handling in plugin entrypoint

This commit is contained in:
Stéphane L 2019-05-07 23:36:43 +02:00
parent 3d9eac8e6d
commit e352d9750d
6 changed files with 35 additions and 37 deletions

@ -83,16 +83,6 @@ const char* calldata_get_string(const calldata_t* data, const char* name) {
return value;
}
WSEventsPtr WSEvents::_instance = WSEventsPtr(nullptr);
WSEventsPtr WSEvents::Current() {
return _instance;
}
void WSEvents::ResetCurrent(WSServerPtr srv) {
_instance = WSEventsPtr(new WSEvents(srv));
}
WSEvents::WSEvents(WSServerPtr srv) :
_srv(srv),
_streamStarttime(0),
@ -144,6 +134,8 @@ WSEvents::~WSEvents() {
obs_frontend_remove_event_callback(WSEvents::FrontendEventHandler, this);
os_cpu_usage_info_destroy(cpuUsageInfo);
blog(LOG_INFO, "wsevents destroyed");
}
void WSEvents::FrontendEventHandler(enum obs_frontend_event event, void* private_data) {

@ -29,17 +29,11 @@ with this program. If not, see <https://www.gnu.org/licenses/>
#include "WSServer.h"
class WSEvents;
typedef QSharedPointer<WSEvents> WSEventsPtr;
class WSEvents : public QObject
{
Q_OBJECT
public:
static WSEventsPtr Current();
static void ResetCurrent(WSServerPtr srv);
explicit WSEvents(WSServerPtr srv);
~WSEvents();
@ -60,8 +54,6 @@ private slots:
void TransitionDurationChanged(int ms);
private:
static WSEventsPtr _instance;
WSServerPtr _srv;
QTimer streamStatusTimer;
QTimer heartbeatTimer;

@ -1,3 +1,4 @@
#include "obs-websocket.h"
#include "Config.h"
#include "Utils.h"
#include "WSEvents.h"
@ -114,7 +115,7 @@ HandlerResponse WSRequestHandler::HandleSetHeartbeat(WSRequestHandler* req) {
return req->SendErrorResponse("Heartbeat <enable> parameter missing");
}
auto events = WSEvents::Current();
auto events = GetEventsSystem();
events->HeartbeatIsActive = obs_data_get_bool(req->data, "enable");
OBSDataAutoRelease response = obs_data_create();
@ -164,18 +165,18 @@ HandlerResponse WSRequestHandler::HandleGetFilenameFormatting(WSRequestHandler*
/**
* Get OBS stats (almost the same info as provided in OBS' stats window)
*
*
* @return {OBSStats} `stats` OBS stats
*
*
* @api requests
* @name GetStats
* @category general
* @since 4.6.0
* @since 4.6.0
*/
HandlerResponse WSRequestHandler::HandleGetStats(WSRequestHandler* req) {
OBSDataAutoRelease stats = WSEvents::Current()->GetStats();
OBSDataAutoRelease stats = GetEventsSystem()->GetStats();
OBSDataAutoRelease response = obs_data_create();
obs_data_set_obj(response, "stats", stats);
return req->SendOKResponse(response);
}
}

@ -1,3 +1,4 @@
#include "obs-websocket.h"
#include "Utils.h"
#include "WSEvents.h"
@ -20,7 +21,7 @@
* @since 0.3
*/
HandlerResponse WSRequestHandler::HandleGetStreamingStatus(WSRequestHandler* req) {
auto events = WSEvents::Current();
auto events = GetEventsSystem();
OBSDataAutoRelease data = obs_data_create();
obs_data_set_bool(data, "streaming", obs_frontend_streaming_active());

@ -38,7 +38,9 @@ void ___output_dummy_addref(obs_output_t*) {}
OBS_DECLARE_MODULE()
OBS_MODULE_USE_DEFAULT_LOCALE("obs-websocket", "en-US")
SettingsDialog* settings_dialog;
WSEventsPtr _eventsSystem;
std::unique_ptr<SettingsDialog> _settingsDialog;
bool obs_module_load(void) {
blog(LOG_INFO, "you can haz websockets (version %s)", OBS_WEBSOCKET_VERSION);
@ -50,26 +52,27 @@ bool obs_module_load(void) {
config->MigrateFromGlobalSettings(); // TODO remove this on the next minor jump
config->Load();
WSEvents::ResetCurrent(WSServer::Current());
_eventsSystem = WSEventsPtr(new WSEvents(WSServer::Current()));
if (config->ServerEnabled) {
WSServer::Current()->start(config->ServerPort);
}
// UI setup
QAction* menu_action = (QAction*)obs_frontend_add_tools_menu_qaction(
obs_module_text("OBSWebsocket.Menu.SettingsItem")
);
obs_frontend_push_ui_translation(obs_module_get_string);
QMainWindow* main_window = (QMainWindow*)obs_frontend_get_main_window();
settings_dialog = new SettingsDialog(main_window);
QMainWindow* mainWindow = (QMainWindow*)obs_frontend_get_main_window();
_settingsDialog = std::unique_ptr<SettingsDialog>(
new SettingsDialog(mainWindow)
);
obs_frontend_pop_ui_translation();
auto menu_cb = [] {
settings_dialog->ToggleShowHide();
};
menu_action->connect(menu_action, &QAction::triggered, menu_cb);
const char* menuActionText =
obs_module_text("OBSWebsocket.Menu.SettingsItem");
QAction* menuAction =
(QAction*)obs_frontend_add_tools_menu_qaction(menuActionText);
QObject::connect(menuAction, &QAction::triggered, [] {
_settingsDialog->ToggleShowHide();
});
// Loading finished
blog(LOG_INFO, "module loaded!");
@ -82,3 +85,6 @@ void obs_module_unload() {
blog(LOG_INFO, "goodbye!");
}
WSEventsPtr GetEventsSystem() {
return _eventsSystem;
}

@ -19,6 +19,7 @@ with this program. If not, see <https://www.gnu.org/licenses/>
#pragma once
#include <obs.hpp>
#include <memory>
void ___source_dummy_addref(obs_source_t*);
void ___sceneitem_dummy_addref(obs_sceneitem_t*);
@ -37,6 +38,11 @@ using OBSDataArrayAutoRelease =
using OBSOutputAutoRelease =
OBSRef<obs_output_t*, ___output_dummy_addref, obs_output_release>;
class WSEvents;
typedef std::shared_ptr<WSEvents> WSEventsPtr;
std::shared_ptr<WSEvents> GetEventsSystem();
#define OBS_WEBSOCKET_VERSION "4.6.0"
#define blog(level, msg, ...) blog(level, "[obs-websocket] " msg, ##__VA_ARGS__)