Skip to content

Commit

Permalink
Finished moving osc to it's own class
Browse files Browse the repository at this point in the history
  • Loading branch information
benaclejames committed Aug 10, 2022
1 parent 1052591 commit 12dcaea
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 20 deletions.
18 changes: 8 additions & 10 deletions obsc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
#include <stdio.h>
#include <thread>
#include "stored.h"
#include "build/socket_helper.h"
#include "socket_helper.h"
#include "osc_message.h"


OBS_DECLARE_MODULE()
Expand Down Expand Up @@ -47,17 +48,14 @@ int create_osc_int_message(char* message, const char* address, int value) {

void update_osc()
{
char message[100];
int msgLen;
osc_message* currentMessage = new osc_bool_message((char*)"/recording", stored.get_recording_active());
sockets->send(currentMessage->message, currentMessage->writerIndex);

msgLen = create_osc_bool_message(message, "/recording", stored.get_recording_active());
sockets->send(message, msgLen);
currentMessage = new osc_bool_message((char*)"/streaming", stored.get_streaming_active());
sockets->send(currentMessage->message, currentMessage->writerIndex);

msgLen = create_osc_bool_message(message, "/streaming", stored.get_streaming_active());
sockets->send(message, msgLen);

msgLen = create_osc_int_message(message, "/replaybuffer", stored.get_replay_buffer_save_count());
sockets->send(message, msgLen);
currentMessage = new osc_int_message((char*)"/replaybuffer", stored.get_replay_buffer_save_count());
sockets->send(currentMessage->message, currentMessage->writerIndex);
}

void frontend_cb(enum obs_frontend_event event, void *priv_data)
Expand Down
31 changes: 25 additions & 6 deletions osc_message.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,42 @@
#include "osc_message.h"

osc_message::osc_message(std::string address, char type)
osc_message::osc_message(char* address, char type)
{

int len = strlen(address)+1;
writerIndex = quantize(len, 4);
memcpy(message, address, writerIndex);

message[writerIndex++] = ',';
message[writerIndex++] = type;
message[writerIndex++] = '\0';
message[writerIndex++] = '\0';
}

osc_message::osc_message(char* data, int size)
{

}

osc_float_message::osc_float_message(std::string address, float value): osc_message(address, 'f')
osc_float_message::osc_float_message(char* address, float value): osc_message(address, 'f')
{

char* bytes = static_cast<char*>(static_cast<void*>(&value));
swap_endianness(bytes, sizeof value);
memcpy(&message[writerIndex], bytes, sizeof value);
writerIndex += sizeof value;
}

osc_string_message::osc_string_message(std::string address, std::string value) : osc_message(address, 's')
osc_string_message::osc_string_message(char* address, char* value) : osc_message(address, 's')
{

int len = strlen(value)+1;
int quantizedLength = quantize(len, 4);
memcpy(&message[writerIndex], value, quantizedLength);
writerIndex += quantizedLength;
}


osc_int_message::osc_int_message(char* address, int value) : osc_message(address, 'i') {
char* bytes = static_cast<char*>(static_cast<void*>(&value));
swap_endianness(bytes, sizeof value);
memcpy(&message[writerIndex], bytes, sizeof value);
writerIndex += sizeof value;
}
32 changes: 29 additions & 3 deletions osc_message.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,46 @@

class osc_message
{

protected:
osc_message(std::string address, char type);
osc_message(char* address, char type);

inline static int quantize(int in, int multiple) {
return in + multiple - in % multiple;
}

inline static void swap_endianness(char* inBytes, int size) {
for (int i=0, j = size - 1; i < size/2; i++, j--) {
char temp = inBytes[i];
inBytes[i] = inBytes[j];
inBytes[j] = temp;
}
}

public:
osc_message(char* data, int size);
int writerIndex;
char message[256];
};

class osc_float_message : public osc_message
{
public:
osc_float_message(std::string address, float value);
osc_float_message(char* address, float value);
};

class osc_string_message : public osc_message
{
public:
osc_string_message(std::string address, std::string value);
osc_string_message(char* address, char* value);
};

class osc_int_message : public osc_message {
public:
osc_int_message(char* address, int value);
};

class osc_bool_message : public osc_message {
public:
osc_bool_message(char* address, bool value) : osc_message(address, value ? 'T' : 'F') {}
};
2 changes: 1 addition & 1 deletion socket_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,5 @@ socket_helper::socket_helper(std::string ip, int inPort, int outPort)
if(bind(inSock, (SOCKADDR*)&inAddr, sizeof(inAddr)))
blog(LOG_ERROR, "[OBSC] Failed to bind in socket");

recvThread = new std::thread(recv_loop);
recvThread = new std::thread(&socket_helper::recv_loop, this);
}

0 comments on commit 12dcaea

Please sign in to comment.