Skip to content

Commit

Permalink
Added 5 second update loop to make sure we're always sending up-to-da…
Browse files Browse the repository at this point in the history
…te data regardless of state change. Switched to c++
  • Loading branch information
benaclejames committed Mar 14, 2022
1 parent cb7c7e0 commit ecbd1df
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 23 deletions.
10 changes: 7 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
51 changes: 37 additions & 14 deletions obsc.c → obsc.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#include <obs-module.h>
#include <obs-frontend-api.h>
#include <stdio.h>
#include <thread>
#include <WS2tcpip.h>
#include <WinSock2.h>
#include "stored.c"
#include "stored.h"

#pragma comment(lib,"WS2_32")

Expand All @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -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;
}
6 changes: 0 additions & 6 deletions stored.c

This file was deleted.

13 changes: 13 additions & 0 deletions stored.h
Original file line number Diff line number Diff line change
@@ -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(); }
};

0 comments on commit ecbd1df

Please sign in to comment.