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;
}
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) {
_srv = srv;

View File

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

View File

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

View File

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

View File

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