diff --git a/CMakeLists.txt b/CMakeLists.txt index a9205ec..085801b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,15 +2,19 @@ project(OBSC) +set(OBSC_HEADERS + stored.h +) + set(OBSC_SOURCES - obsc.c - stored.c + obsc.cpp ) link_directories($ENV{OBS_DIR}/build/libobs/Debug $ENV{OBS_DIR}/build/UI/obs-frontend-api/Debug) add_library(OBSC MODULE - ${OBSC_SOURCES}) + ${OBSC_SOURCES} + ${OBSC_HEADERS}) target_include_directories(OBSC PRIVATE $ENV{OBS_DIR}/libobs $ENV{OBS_DIR}/UI/obs-frontend-api) diff --git a/obsc.c b/obsc.cpp similarity index 63% rename from obsc.c rename to obsc.cpp index 6572b63..2c550bd 100644 --- a/obsc.c +++ b/obsc.cpp @@ -1,9 +1,10 @@ #include #include #include +#include #include #include -#include "stored.c" +#include "stored.h" #pragma comment(lib,"WS2_32") @@ -13,6 +14,7 @@ WSADATA data; SOCKADDR_IN addr; SOCKET sock; struct stored stored; +std::thread* osc_thread; int create_osc_bool_message(char* message, const char* address, bool value) { int len = strlen(address)+1; @@ -47,36 +49,54 @@ int create_osc_int_message(char* message, const char* address, int value) { return paddedLen; } -void frontend_cb(enum obs_frontend_event event, struct stored *priv_data) +void update_osc() { char message[100]; int msgLen; - + + msgLen = create_osc_bool_message(message, "/recording", stored.get_recording_active()); + sendto(sock, message, msgLen, 0, (struct sockaddr*) &addr, sizeof(addr)); + + msgLen = create_osc_bool_message(message, "/streaming", stored.get_streaming_active()); + sendto(sock, message, msgLen, 0, (struct sockaddr*) &addr, sizeof(addr)); + + msgLen = create_osc_int_message(message, "/replaybuffer", stored.get_replay_buffer_save_count()); + sendto(sock, message, msgLen, 0, (struct sockaddr*) &addr, sizeof(addr)); +} + +void frontend_cb(enum obs_frontend_event event, void *priv_data) +{ switch (event) { case OBS_FRONTEND_EVENT_RECORDING_STARTED: case OBS_FRONTEND_EVENT_RECORDING_STOPPED: - msgLen = create_osc_bool_message(message, "/recording", event == OBS_FRONTEND_EVENT_RECORDING_STARTED); - break; - case OBS_FRONTEND_EVENT_STREAMING_STARTED: case OBS_FRONTEND_EVENT_STREAMING_STOPPED: - msgLen = create_osc_bool_message(message, "/streaming", event == OBS_FRONTEND_EVENT_STREAMING_STARTED); + update_osc(); break; case OBS_FRONTEND_EVENT_REPLAY_BUFFER_SAVED: - msgLen = create_osc_int_message(message, "/replaybuffer", priv_data->replay_buffer_save_count); - priv_data->replay_buffer_save_count++; + stored.increment_save_count(); + update_osc(); break; - default: - return; + case OBS_FRONTEND_EVENT_EXIT: + osc_thread->detach(); + delete osc_thread; + break; } +} - sendto(sock, message, msgLen, 0, (struct sockaddr*) &addr, sizeof(addr)); +void periodic_update_loop() +{ + while (true) + { + std::this_thread::sleep_for(std::chrono::seconds(5)); + update_osc(); + } } -bool obs_module_load(void) +bool obs_module_load() { WSAStartup(MAKEWORD(2,2), &data); @@ -86,6 +106,9 @@ bool obs_module_load(void) sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP); - obs_frontend_add_event_callback(frontend_cb, &stored); + obs_frontend_add_event_callback(frontend_cb, nullptr); + + // Start the osc update loop on a separate thread + osc_thread = new std::thread(periodic_update_loop); return true; } \ No newline at end of file diff --git a/stored.c b/stored.c deleted file mode 100644 index b1dedb1..0000000 --- a/stored.c +++ /dev/null @@ -1,6 +0,0 @@ -#pragma once - -struct stored -{ - int replay_buffer_save_count; -}; diff --git a/stored.h b/stored.h new file mode 100644 index 0000000..0af3525 --- /dev/null +++ b/stored.h @@ -0,0 +1,13 @@ +#pragma once + +class stored +{ + int replay_buffer_save_count = 0; + +public: + void increment_save_count() { replay_buffer_save_count++; } + + int get_replay_buffer_save_count() const { return replay_buffer_save_count; } + bool get_recording_active() const { return obs_frontend_recording_active(); } + bool get_streaming_active() const { return obs_frontend_streaming_active(); } +};