Skip to content

Commit

Permalink
Segmented osc and socket helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
benaclejames committed Aug 10, 2022
1 parent ecbd1df commit 1052591
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 17 deletions.
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ project(OBSC)

set(OBSC_HEADERS
stored.h
socket_helper.h
osc_message.h
)

set(OBSC_SOURCES
obsc.cpp
socket_helper.cpp
osc_message.cpp
)

link_directories($ENV{OBS_DIR}/build/libobs/Debug $ENV{OBS_DIR}/build/UI/obs-frontend-api/Debug)
Expand Down
24 changes: 7 additions & 17 deletions obsc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,14 @@
#include <obs-frontend-api.h>
#include <stdio.h>
#include <thread>
#include <WS2tcpip.h>
#include <WinSock2.h>
#include "stored.h"
#include "build/socket_helper.h"

#pragma comment(lib,"WS2_32")

OBS_DECLARE_MODULE()

WSADATA data;
SOCKADDR_IN addr;
SOCKET sock;
struct stored stored;
socket_helper* sockets;
stored stored;
std::thread* osc_thread;

int create_osc_bool_message(char* message, const char* address, bool value) {
Expand Down Expand Up @@ -55,13 +51,13 @@ void update_osc()
int msgLen;

msgLen = create_osc_bool_message(message, "/recording", stored.get_recording_active());
sendto(sock, message, msgLen, 0, (struct sockaddr*) &addr, sizeof(addr));
sockets->send(message, msgLen);

msgLen = create_osc_bool_message(message, "/streaming", stored.get_streaming_active());
sendto(sock, message, msgLen, 0, (struct sockaddr*) &addr, sizeof(addr));
sockets->send(message, msgLen);

msgLen = create_osc_int_message(message, "/replaybuffer", stored.get_replay_buffer_save_count());
sendto(sock, message, msgLen, 0, (struct sockaddr*) &addr, sizeof(addr));
sockets->send(message, msgLen);
}

void frontend_cb(enum obs_frontend_event event, void *priv_data)
Expand Down Expand Up @@ -98,13 +94,7 @@ void periodic_update_loop()

bool obs_module_load()
{
WSAStartup(MAKEWORD(2,2), &data);

addr.sin_family = AF_INET;
InetPton(AF_INET, "127.0.0.1", &addr.sin_addr.s_addr);
addr.sin_port = htons(9000);

sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
sockets = new socket_helper("127.0.0.1", 9001, 9000);

obs_frontend_add_event_callback(frontend_cb, nullptr);

Expand Down
23 changes: 23 additions & 0 deletions osc_message.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "osc_message.h"

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

}

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

}

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

}

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

}


22 changes: 22 additions & 0 deletions osc_message.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once
#include <string>

class osc_message
{
protected:
osc_message(std::string address, char type);
public:
osc_message(char* data, int size);
};

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

class osc_string_message : public osc_message
{
public:
osc_string_message(std::string address, std::string value);
};
44 changes: 44 additions & 0 deletions socket_helper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include "socket_helper.h"

#include <util/base.h>

void socket_helper::recv_loop()
{
int bytes_received;
char serverBuf[1025];
int serverBufLen = 1024;

// Keep a seperate address struct to store sender information.
struct sockaddr_in SenderAddr;
int SenderAddrSize = sizeof (SenderAddr);

printf("Receiving datagrams on %s\n", "127.0.0.1");
while (true)
{
bytes_received = recvfrom(inSock, serverBuf, serverBufLen, 0 /* no flags*/, (SOCKADDR *) & SenderAddr, &SenderAddrSize);
if (bytes_received == SOCKET_ERROR) {
printf("recvfrom failed with error %d\n", WSAGetLastError());
}
printf("recv bytes");
}
}

socket_helper::socket_helper(std::string ip, int inPort, int outPort)
{
WSAStartup(MAKEWORD(2,2), &data);

outAddr.sin_family = AF_INET;
InetPton(AF_INET, ip.c_str(), &outAddr.sin_addr.s_addr);
outAddr.sin_port = htons(outPort);

outSock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);

inAddr.sin_family = AF_INET;
inAddr.sin_port = htons(inPort);
InetPton(AF_INET, ip.c_str(), &inAddr.sin_addr.s_addr);

if(bind(inSock, (SOCKADDR*)&inAddr, sizeof(inAddr)))
blog(LOG_ERROR, "[OBSC] Failed to bind in socket");

recvThread = new std::thread(recv_loop);
}
24 changes: 24 additions & 0 deletions socket_helper.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#pragma once
#include <string>
#include <thread>
#include <WS2tcpip.h>
#include <WinSock2.h>

#pragma comment(lib,"WS2_32")

class socket_helper
{
WSADATA data;
SOCKADDR_IN outAddr, inAddr;
SOCKET inSock, outSock;
std::thread* recvThread;

void recv_loop();
public:
socket_helper(std::string ip, int inPort, int outPort);

void send(char* data, int size)
{
sendto(outSock, data, size, 0, (SOCKADDR*)&outAddr, sizeof(outAddr));
}
};

0 comments on commit 1052591

Please sign in to comment.