WIP plugin listening on unix socket

TODO: Use a thread to listen on the socket.
This commit is contained in:
Stephen Seo 2023-08-12 20:06:14 +09:00
parent 1e49ca39e7
commit c850536731
6 changed files with 159 additions and 0 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
/.cache/
/build*/

23
CMakeLists.txt Normal file
View file

@ -0,0 +1,23 @@
cmake_minimum_required(VERSION 3.25)
project(obs-studio-plugin-unix-socket-control)
add_compile_options(
-Wall -Wextra -Wpedantic
$<$<CONFIG:DEBUG>:-O0>
)
if(NOT DEFINED CMAKE_BUILD_TYPE OR NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE "Debug")
message("Set build type to Debug by default")
endif()
set(unix-socket-control_SOURCES
src/plugin.c
src/socket.c
)
add_library(unix-socket-control MODULE
${unix-socket-control_SOURCES})
find_package(libobs REQUIRED)
target_link_libraries(unix-socket-control PRIVATE OBS::libobs)

1
compile_commands.json Symbolic link
View file

@ -0,0 +1 @@
buildDebug/compile_commands.json

24
src/plugin.c Normal file
View file

@ -0,0 +1,24 @@
// plugin includes
#include <obs-module.h>
#include <obs-frontend-api.h>
// local includes
#include "socket.h"
static UnixSocketHandler global_unix_socket_handler={
.flags = 0xFFFFFFFFFFFFFFFF
};
OBS_DECLARE_MODULE()
OBS_MODULE_USE_DEFAULT_LOCALE("unix-socket-control", "en-US")
bool obs_module_load(void) {
global_unix_socket_handler = init_unix_socket_handler();
return is_unix_socket_handler_valid(global_unix_socket_handler) ? true : false;
}
void obs_module_unload(void) {
cleanup_unix_socket_handler(&global_unix_socket_handler);
}

67
src/socket.c Normal file
View file

@ -0,0 +1,67 @@
#include "socket.h"
// standard library includes
#include <stdlib.h>
// unix includes
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
#define UNIX_SOCKET_HANDLER_SOCKET_NAME "/tmp/obs-studio-plugin-unix-socket-handler-socket"
UnixSocketHandler init_unix_socket_handler(void) {
UnixSocketHandler handler;
memset(&handler, 0, sizeof(UnixSocketHandler));
umask(S_IRWXO);
// Set up linked list of events.
handler.events_head.next = &handler.events_tail;
handler.events_tail.prev = &handler.events_head;
handler.events_head.type = UNIX_SOCKET_EVENT_HEAD;
handler.events_tail.type = UNIX_SOCKET_EVENT_TAIL;
// Set up unix socket.
handler.socket_descriptor = socket(AF_UNIX, SOCK_SEQPACKET, 0);
if (handler.socket_descriptor == -1) {
handler.flags = 0xFFFFFFFFFFFFFFFF;
return handler;
}
handler.name.sun_family = AF_UNIX;
strncpy(handler.name.sun_path,
UNIX_SOCKET_HANDLER_SOCKET_NAME,
sizeof(handler.name.sun_path) - 1);
int ret = bind(handler.socket_descriptor,
(const struct sockaddr*) &handler.name,
sizeof(handler.name));
if (ret == -1) {
close(handler.socket_descriptor);
handler.socket_descriptor = -1;
handler.flags = 0xFFFFFFFFFFFFFFFF;
return handler;
}
return handler;
}
void cleanup_unix_socket_handler(UnixSocketHandler *handler) {
if (handler->flags == 0xFFFFFFFFFFFFFFFF) {
return;
}
if (handler->socket_descriptor >= 0) {
close(handler->socket_descriptor);
handler->socket_descriptor = -1;
unlink(UNIX_SOCKET_HANDLER_SOCKET_NAME);
}
handler->flags = 0xFFFFFFFFFFFFFFFF;
}
int is_unix_socket_handler_valid(UnixSocketHandler handler) {
return handler.flags == 0xFFFFFFFFFFFFFFFF ? 0 : 1;
}

42
src/socket.h Normal file
View file

@ -0,0 +1,42 @@
#ifndef OBS_STUDIO_PLUGIN_UNIX_SOCKET_CONTROL_SOCKET_H_
#define OBS_STUDIO_PLUGIN_UNIX_SOCKET_CONTROL_SOCKET_H_
// standard library includes
#include <stdint.h>
// unix includes
#include <sys/socket.h>
#include <sys/un.h>
typedef enum UnixSocketEventType {
UNIX_SOCKET_EVENT_NOP,
UNIX_SOCKET_EVENT_HEAD,
UNIX_SOCKET_EVENT_TAIL,
UNIX_SOCKET_EVENT_START_RECORDING,
UNIX_SOCKET_EVENT_STOP_RECORDING,
UNIX_SOCKET_EVENT_START_STREAMING,
UNIX_SOCKET_EVENT_STOP_STREAMING
} UnixSocketEventType;
typedef struct UnixSocketEvent {
struct UnixSocketEvent *next;
struct UnixSocketEvent *prev;
UnixSocketEventType type;
} UnixSocketEvent;
typedef struct UnixSocketHandler {
/*
* All ones - invalid/cleaned-up instance
*/
uint64_t flags;
UnixSocketEvent events_head;
UnixSocketEvent events_tail;
struct sockaddr_un name;
int socket_descriptor;
} UnixSocketHandler;
UnixSocketHandler init_unix_socket_handler(void);
void cleanup_unix_socket_handler(UnixSocketHandler *handler);
int is_unix_socket_handler_valid(UnixSocketHandler handler);
#endif