events: singleton shared pointer refactor

This commit is contained in:
Stéphane L
2019-01-01 05:07:55 +01:00
parent 9405b17e14
commit 2e5b903eae
5 changed files with 36 additions and 14 deletions

View File

@ -62,7 +62,15 @@ void* calldata_get_ptr(const calldata_t* data, const char* name) {
return ptr; return ptr;
} }
WSEvents* WSEvents::Instance = nullptr; WSEventsPtr WSEvents::_instance = WSEventsPtr(nullptr);
WSEventsPtr WSEvents::Current() {
return _instance;
}
void WSEvents::ResetCurrent(WSServerPtr srv) {
_instance = WSEventsPtr(new WSEvents(srv));
}
WSEvents::WSEvents(WSServerPtr srv) { WSEvents::WSEvents(WSServerPtr srv) {
_srv = srv; _srv = srv;

View File

@ -22,17 +22,27 @@ with this program. If not, see <https://www.gnu.org/licenses/>
#include <obs.hpp> #include <obs.hpp>
#include <obs-frontend-api.h> #include <obs-frontend-api.h>
#include <QListWidgetItem> #include <QListWidgetItem>
#include <QSharedPointer>
#include "WSServer.h" #include "WSServer.h"
class WSEvents : public QObject { class WSEvents;
Q_OBJECT typedef QSharedPointer<WSEvents> WSEventsPtr;
public:
class WSEvents : public QObject
{
Q_OBJECT
public:
static WSEventsPtr Current();
static void ResetCurrent(WSServerPtr srv);
explicit WSEvents(WSServerPtr srv); explicit WSEvents(WSServerPtr srv);
~WSEvents(); ~WSEvents();
static void FrontendEventHandler( static void FrontendEventHandler(
enum obs_frontend_event event, void* privateData); enum obs_frontend_event event, void* privateData);
static WSEvents* Instance;
void connectSceneSignals(obs_source_t* scene); void connectSceneSignals(obs_source_t* scene);
void hookTransitionBeginEvent(); void hookTransitionBeginEvent();
@ -44,13 +54,15 @@ class WSEvents : public QObject {
bool HeartbeatIsActive; bool HeartbeatIsActive;
private slots: private slots:
void deferredInitOperations(); void deferredInitOperations();
void StreamStatus(); void StreamStatus();
void Heartbeat(); void Heartbeat();
void TransitionDurationChanged(int ms); void TransitionDurationChanged(int ms);
private: private:
static WSEventsPtr _instance;
WSServerPtr _srv; WSServerPtr _srv;
OBSSource currentScene; OBSSource currentScene;

View File

@ -121,12 +121,12 @@ void WSRequestHandler::HandleAuthenticate(WSRequestHandler* req) {
return; return;
} }
WSEvents::Instance->HeartbeatIsActive = auto events = WSEvents::Current();
obs_data_get_bool(req->data, "enable");
events->HeartbeatIsActive = obs_data_get_bool(req->data, "enable");
OBSDataAutoRelease response = obs_data_create(); OBSDataAutoRelease response = obs_data_create();
obs_data_set_bool(response, "enable", obs_data_set_bool(response, "enable", events->HeartbeatIsActive);
WSEvents::Instance->HeartbeatIsActive);
req->SendOKResponse(response); req->SendOKResponse(response);
} }

View File

@ -21,6 +21,8 @@
* @since 0.3 * @since 0.3
*/ */
void WSRequestHandler::HandleGetStreamingStatus(WSRequestHandler* req) { void WSRequestHandler::HandleGetStreamingStatus(WSRequestHandler* req) {
auto events = WSEvents::Current();
OBSDataAutoRelease data = obs_data_create(); OBSDataAutoRelease data = obs_data_create();
obs_data_set_bool(data, "streaming", obs_frontend_streaming_active()); obs_data_set_bool(data, "streaming", obs_frontend_streaming_active());
obs_data_set_bool(data, "recording", obs_frontend_recording_active()); obs_data_set_bool(data, "recording", obs_frontend_recording_active());
@ -28,13 +30,13 @@
const char* tc = nullptr; const char* tc = nullptr;
if (obs_frontend_streaming_active()) { if (obs_frontend_streaming_active()) {
tc = WSEvents::Instance->GetStreamingTimecode(); tc = events->GetStreamingTimecode();
obs_data_set_string(data, "stream-timecode", tc); obs_data_set_string(data, "stream-timecode", tc);
bfree((void*)tc); bfree((void*)tc);
} }
if (obs_frontend_recording_active()) { if (obs_frontend_recording_active()) {
tc = WSEvents::Instance->GetRecordingTimecode(); tc = events->GetRecordingTimecode();
obs_data_set_string(data, "rec-timecode", tc); obs_data_set_string(data, "rec-timecode", tc);
bfree((void*)tc); bfree((void*)tc);
} }

View File

@ -48,7 +48,7 @@ bool obs_module_load(void) {
auto config = Config::Current(); auto config = Config::Current();
config->Load(); config->Load();
WSEvents::Instance = new WSEvents(WSServer::Current()); WSEvents::ResetCurrent(WSServer::Current());
if (config->ServerEnabled) { if (config->ServerEnabled) {
WSServer::Current()->start(config->ServerPort); WSServer::Current()->start(config->ServerPort);