Scaffold OBS audio stem source
This commit is contained in:
parent
0c39921f96
commit
71234f04ce
12 changed files with 522 additions and 9 deletions
15
native/obs_stem_source/CMakeLists.txt
Normal file
15
native/obs_stem_source/CMakeLists.txt
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
cmake_minimum_required(VERSION 3.20)
|
||||
|
||||
project(mimir_obs_stem_source LANGUAGES CXX)
|
||||
|
||||
find_package(libobs REQUIRED CONFIG)
|
||||
|
||||
add_library(mimir_obs_stem_source MODULE
|
||||
mimir_obs_stem_source.cpp)
|
||||
|
||||
target_compile_features(mimir_obs_stem_source PRIVATE cxx_std_20)
|
||||
target_compile_definitions(mimir_obs_stem_source PRIVATE
|
||||
WIN32_LEAN_AND_MEAN
|
||||
NOMINMAX
|
||||
_WIN32_WINNT=0x0A00)
|
||||
target_link_libraries(mimir_obs_stem_source PRIVATE OBS::libobs)
|
||||
43
native/obs_stem_source/mimir_obs_stem_ipc.h
Normal file
43
native/obs_stem_source/mimir_obs_stem_ipc.h
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
constexpr uint64_t MIMIR_OBS_STEM_MAGIC = 0x4D545352494D494Dull;
|
||||
constexpr int MIMIR_OBS_STEM_VERSION = 1;
|
||||
constexpr int MIMIR_OBS_STEM_MAX_STEMS = 8;
|
||||
constexpr int MIMIR_OBS_STEM_MAX_FRAMES = 4096;
|
||||
constexpr int MIMIR_OBS_STEM_ID_BYTES = 64;
|
||||
constexpr int MIMIR_OBS_STEM_DISPLAY_BYTES = 64;
|
||||
constexpr int MIMIR_OBS_STEM_SOURCE_BYTES = 64;
|
||||
constexpr int MIMIR_OBS_STEM_HEADER_BYTES = 64;
|
||||
constexpr int MIMIR_OBS_STEM_RECORD_BYTES = 224;
|
||||
constexpr int MIMIR_OBS_STEM_MAP_BYTES =
|
||||
MIMIR_OBS_STEM_HEADER_BYTES +
|
||||
(MIMIR_OBS_STEM_MAX_STEMS * MIMIR_OBS_STEM_RECORD_BYTES) +
|
||||
(MIMIR_OBS_STEM_MAX_STEMS * MIMIR_OBS_STEM_MAX_FRAMES * static_cast<int>(sizeof(float)));
|
||||
|
||||
struct MimirObsStemHeader {
|
||||
uint64_t magic;
|
||||
int32_t version;
|
||||
uint32_t generation;
|
||||
int32_t stem_count;
|
||||
int32_t max_stems;
|
||||
int32_t max_frames;
|
||||
int32_t header_bytes;
|
||||
int32_t record_bytes;
|
||||
int32_t map_bytes;
|
||||
};
|
||||
|
||||
struct MimirObsStemRecord {
|
||||
int32_t channel_index;
|
||||
int32_t configured;
|
||||
int32_t sample_rate;
|
||||
int32_t frame_count;
|
||||
int32_t sample_offset;
|
||||
int32_t reserved0;
|
||||
int32_t reserved1;
|
||||
int32_t reserved2;
|
||||
char stem_id[MIMIR_OBS_STEM_ID_BYTES];
|
||||
char display_name[MIMIR_OBS_STEM_DISPLAY_BYTES];
|
||||
char source_id[MIMIR_OBS_STEM_SOURCE_BYTES];
|
||||
};
|
||||
212
native/obs_stem_source/mimir_obs_stem_source.cpp
Normal file
212
native/obs_stem_source/mimir_obs_stem_source.cpp
Normal file
|
|
@ -0,0 +1,212 @@
|
|||
#include <obs-module.h>
|
||||
#include <util/platform.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <chrono>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
|
||||
#include "mimir_obs_stem_ipc.h"
|
||||
|
||||
OBS_DECLARE_MODULE()
|
||||
OBS_MODULE_USE_DEFAULT_LOCALE("mimir-obs-stem-source", "en-US")
|
||||
|
||||
struct MimirStemSource {
|
||||
obs_source_t *source = nullptr;
|
||||
std::string map_name = "Local\\MimirObsStemBus";
|
||||
std::string stem_id = "aligned_source_0";
|
||||
HANDLE mapping = nullptr;
|
||||
const uint8_t *view = nullptr;
|
||||
std::thread worker;
|
||||
std::atomic_bool running = false;
|
||||
uint32_t last_generation = 0;
|
||||
};
|
||||
|
||||
static std::string obs_string(obs_data_t *settings, const char *key, const char *fallback)
|
||||
{
|
||||
const char *value = obs_data_get_string(settings, key);
|
||||
return value && *value ? std::string(value) : std::string(fallback);
|
||||
}
|
||||
|
||||
static void close_mapping(MimirStemSource *ctx)
|
||||
{
|
||||
if (ctx->view) {
|
||||
UnmapViewOfFile(ctx->view);
|
||||
ctx->view = nullptr;
|
||||
}
|
||||
|
||||
if (ctx->mapping) {
|
||||
CloseHandle(ctx->mapping);
|
||||
ctx->mapping = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
static bool open_mapping(MimirStemSource *ctx)
|
||||
{
|
||||
if (ctx->mapping) {
|
||||
return true;
|
||||
}
|
||||
|
||||
ctx->mapping = OpenFileMappingA(FILE_MAP_READ, FALSE, ctx->map_name.c_str());
|
||||
if (!ctx->mapping) {
|
||||
return false;
|
||||
}
|
||||
|
||||
ctx->view = static_cast<const uint8_t *>(MapViewOfFile(ctx->mapping, FILE_MAP_READ, 0, 0, MIMIR_OBS_STEM_MAP_BYTES));
|
||||
if (!ctx->view) {
|
||||
close_mapping(ctx);
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static const MimirObsStemRecord *find_record(const uint8_t *view, const std::string &stem_id, int stem_count)
|
||||
{
|
||||
const auto count = std::min(stem_count, MIMIR_OBS_STEM_MAX_STEMS);
|
||||
for (int index = 0; index < count; ++index) {
|
||||
const auto *record = reinterpret_cast<const MimirObsStemRecord *>(view + MIMIR_OBS_STEM_HEADER_BYTES + (index * MIMIR_OBS_STEM_RECORD_BYTES));
|
||||
if (record->frame_count <= 0 || record->sample_rate <= 0 || record->sample_offset <= 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (stem_id == record->stem_id) {
|
||||
return record;
|
||||
}
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static void publish_if_available(MimirStemSource *ctx)
|
||||
{
|
||||
if (!open_mapping(ctx)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const auto *header = reinterpret_cast<const MimirObsStemHeader *>(ctx->view);
|
||||
const auto generation_a = header->generation;
|
||||
if ((generation_a & 1u) != 0 || generation_a == ctx->last_generation) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (header->magic != MIMIR_OBS_STEM_MAGIC ||
|
||||
header->version != MIMIR_OBS_STEM_VERSION ||
|
||||
header->map_bytes != MIMIR_OBS_STEM_MAP_BYTES) {
|
||||
return;
|
||||
}
|
||||
|
||||
const auto *record = find_record(ctx->view, ctx->stem_id, header->stem_count);
|
||||
if (!record) {
|
||||
return;
|
||||
}
|
||||
|
||||
const auto frame_count = std::min(record->frame_count, MIMIR_OBS_STEM_MAX_FRAMES);
|
||||
const auto sample_rate = record->sample_rate;
|
||||
const auto byte_count = static_cast<size_t>(frame_count) * sizeof(float);
|
||||
if (record->sample_offset < 0 || record->sample_offset + static_cast<int>(byte_count) > MIMIR_OBS_STEM_MAP_BYTES) {
|
||||
return;
|
||||
}
|
||||
|
||||
std::vector<float> samples(frame_count);
|
||||
std::memcpy(samples.data(), ctx->view + record->sample_offset, byte_count);
|
||||
|
||||
const auto generation_b = header->generation;
|
||||
if (generation_a != generation_b || (generation_b & 1u) != 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
ctx->last_generation = generation_b;
|
||||
uint8_t *planes[MAX_AV_PLANES] = {};
|
||||
planes[0] = reinterpret_cast<uint8_t *>(samples.data());
|
||||
obs_source_audio audio = {};
|
||||
audio.data[0] = planes[0];
|
||||
audio.frames = static_cast<uint32_t>(frame_count);
|
||||
audio.speakers = SPEAKERS_MONO;
|
||||
audio.samples_per_sec = static_cast<uint32_t>(sample_rate);
|
||||
audio.format = AUDIO_FORMAT_FLOAT_PLANAR;
|
||||
audio.timestamp = os_gettime_ns();
|
||||
obs_source_output_audio(ctx->source, &audio);
|
||||
}
|
||||
|
||||
static void worker_loop(MimirStemSource *ctx)
|
||||
{
|
||||
while (ctx->running.load()) {
|
||||
publish_if_available(ctx);
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(5));
|
||||
}
|
||||
}
|
||||
|
||||
static const char *mimir_stem_get_name(void *)
|
||||
{
|
||||
return "Mimir Audio Stem";
|
||||
}
|
||||
|
||||
static void mimir_stem_update(void *data, obs_data_t *settings)
|
||||
{
|
||||
auto *ctx = static_cast<MimirStemSource *>(data);
|
||||
ctx->map_name = obs_string(settings, "map_name", "Local\\MimirObsStemBus");
|
||||
ctx->stem_id = obs_string(settings, "stem_id", "aligned_source_0");
|
||||
ctx->last_generation = 0;
|
||||
close_mapping(ctx);
|
||||
}
|
||||
|
||||
static void *mimir_stem_create(obs_data_t *settings, obs_source_t *source)
|
||||
{
|
||||
auto *ctx = new MimirStemSource();
|
||||
ctx->source = source;
|
||||
mimir_stem_update(ctx, settings);
|
||||
ctx->running = true;
|
||||
ctx->worker = std::thread(worker_loop, ctx);
|
||||
return ctx;
|
||||
}
|
||||
|
||||
static void mimir_stem_destroy(void *data)
|
||||
{
|
||||
auto *ctx = static_cast<MimirStemSource *>(data);
|
||||
ctx->running = false;
|
||||
if (ctx->worker.joinable()) {
|
||||
ctx->worker.join();
|
||||
}
|
||||
|
||||
close_mapping(ctx);
|
||||
delete ctx;
|
||||
}
|
||||
|
||||
static void mimir_stem_defaults(obs_data_t *settings)
|
||||
{
|
||||
obs_data_set_default_string(settings, "map_name", "Local\\MimirObsStemBus");
|
||||
obs_data_set_default_string(settings, "stem_id", "aligned_source_0");
|
||||
}
|
||||
|
||||
static obs_properties_t *mimir_stem_properties(void *)
|
||||
{
|
||||
obs_properties_t *props = obs_properties_create();
|
||||
obs_properties_add_text(props, "map_name", "Shared memory map", OBS_TEXT_DEFAULT);
|
||||
obs_properties_add_text(props, "stem_id", "Stem id", OBS_TEXT_DEFAULT);
|
||||
return props;
|
||||
}
|
||||
|
||||
static obs_source_info mimir_stem_source_info = {
|
||||
.id = "mimir_audio_stem_source",
|
||||
.type = OBS_SOURCE_TYPE_INPUT,
|
||||
.output_flags = OBS_SOURCE_AUDIO,
|
||||
.get_name = mimir_stem_get_name,
|
||||
.create = mimir_stem_create,
|
||||
.destroy = mimir_stem_destroy,
|
||||
.get_defaults = mimir_stem_defaults,
|
||||
.get_properties = mimir_stem_properties,
|
||||
.update = mimir_stem_update,
|
||||
};
|
||||
|
||||
bool obs_module_load(void)
|
||||
{
|
||||
obs_register_source(&mimir_stem_source_info);
|
||||
blog(LOG_INFO, "Mimir OBS audio stem source loaded");
|
||||
return true;
|
||||
}
|
||||
Loading…
Reference in a new issue