430 lines
10 KiB
C++
Raw Normal View History

2018-09-25 17:52:31 -05:00
#include <obs-module.h>
#include <obs-frontend-api.h>
#include <QMainWindow>
#include <QAction>
#include <util/util.hpp>
#include <util/platform.h>
2018-12-09 19:31:40 -06:00
#include <media-io/video-io.h>
#include <media-io/video-frame.h>
2018-09-25 17:52:31 -05:00
#include "DecklinkOutputUI.h"
2018-12-09 19:31:40 -06:00
#include "../../../plugins/decklink/const.h"
2018-09-25 17:52:31 -05:00
OBS_DECLARE_MODULE()
OBS_MODULE_USE_DEFAULT_LOCALE("decklink-output-ui", "en-US")
DecklinkOutputUI *doUI;
bool shutting_down = false;
2018-09-25 17:52:31 -05:00
bool main_output_running = false;
2018-12-09 19:31:40 -06:00
bool preview_output_running = false;
2018-09-25 17:52:31 -05:00
obs_output_t *output;
constexpr size_t STAGE_BUFFER_COUNT = 3;
2018-12-09 19:31:40 -06:00
struct preview_output {
bool enabled;
obs_source_t *current_source;
obs_output_t *output;
video_t *video_queue;
gs_texrender_t *texrender_premultiplied;
2018-12-09 19:31:40 -06:00
gs_texrender_t *texrender;
gs_stagesurf_t *stagesurfaces[STAGE_BUFFER_COUNT];
bool surf_written[STAGE_BUFFER_COUNT];
size_t stage_index;
2018-12-09 19:31:40 -06:00
uint8_t *video_data;
uint32_t video_linesize;
obs_video_info ovi;
};
static struct preview_output context = {0};
2018-09-25 17:52:31 -05:00
OBSData load_settings()
{
BPtr<char> path = obs_module_get_config_path(
obs_current_module(), "decklinkOutputProps.json");
2018-09-25 17:52:31 -05:00
BPtr<char> jsonData = os_quick_read_utf8_file(path);
if (!!jsonData) {
obs_data_t *data = obs_data_create_from_json(jsonData);
OBSData dataRet(data);
obs_data_release(data);
return dataRet;
}
return nullptr;
}
void output_stop()
{
obs_output_stop(output);
obs_output_release(output);
main_output_running = false;
if (!shutting_down)
doUI->OutputStateChanged(false);
}
2018-09-25 17:52:31 -05:00
void output_start()
{
OBSData settings = load_settings();
2018-09-25 17:52:31 -05:00
if (settings != nullptr) {
output = obs_output_create("decklink_output", "decklink_output",
settings, NULL);
2018-09-25 17:52:31 -05:00
bool started = obs_output_start(output);
2018-09-25 17:52:31 -05:00
main_output_running = started;
if (!shutting_down)
doUI->OutputStateChanged(started);
if (!started)
output_stop();
2018-09-25 17:52:31 -05:00
}
}
void output_toggle()
2018-09-25 17:52:31 -05:00
{
if (main_output_running)
output_stop();
else
output_start();
2018-09-25 17:52:31 -05:00
}
2018-12-09 19:31:40 -06:00
OBSData load_preview_settings()
{
BPtr<char> path = obs_module_get_config_path(
obs_current_module(), "decklinkPreviewOutputProps.json");
2018-12-09 19:31:40 -06:00
BPtr<char> jsonData = os_quick_read_utf8_file(path);
if (!!jsonData) {
obs_data_t *data = obs_data_create_from_json(jsonData);
OBSData dataRet(data);
obs_data_release(data);
return dataRet;
}
return nullptr;
}
void on_preview_scene_changed(enum obs_frontend_event event, void *param);
void render_preview_source(void *param, uint32_t cx, uint32_t cy);
static void preview_tick(void *param, float sec)
{
UNUSED_PARAMETER(sec);
auto ctx = (struct preview_output *)param;
if (ctx->texrender_premultiplied)
gs_texrender_reset(ctx->texrender_premultiplied);
if (ctx->texrender)
gs_texrender_reset(ctx->texrender);
}
void preview_output_stop()
2018-12-09 19:31:40 -06:00
{
obs_output_stop(context.output);
obs_output_release(context.output);
obs_remove_main_render_callback(render_preview_source, &context);
obs_frontend_remove_event_callback(on_preview_scene_changed, &context);
obs_source_release(context.current_source);
obs_enter_graphics();
for (gs_stagesurf_t *&surf : context.stagesurfaces) {
gs_stagesurface_destroy(surf);
surf = nullptr;
}
gs_texrender_destroy(context.texrender);
context.texrender = nullptr;
gs_texrender_destroy(context.texrender_premultiplied);
context.texrender_premultiplied = nullptr;
obs_leave_graphics();
video_output_close(context.video_queue);
obs_remove_tick_callback(preview_tick, &context);
preview_output_running = false;
if (!shutting_down)
doUI->PreviewOutputStateChanged(false);
2018-12-09 19:31:40 -06:00
}
void preview_output_start()
2018-12-09 19:31:40 -06:00
{
OBSData settings = load_preview_settings();
2018-12-09 19:31:40 -06:00
if (settings != nullptr) {
obs_add_tick_callback(preview_tick, &context);
context.output = obs_output_create("decklink_output",
"decklink_preview_output",
settings, NULL);
2018-12-09 19:31:40 -06:00
obs_get_video_info(&context.ovi);
const struct video_scale_info *const conversion =
obs_output_get_video_conversion(context.output);
const uint32_t width = conversion->width;
const uint32_t height = conversion->height;
2018-12-09 19:31:40 -06:00
obs_enter_graphics();
context.texrender_premultiplied =
gs_texrender_create(GS_BGRA, GS_ZS_NONE);
context.texrender = gs_texrender_create(GS_BGRA, GS_ZS_NONE);
for (gs_stagesurf_t *&surf : context.stagesurfaces)
surf = gs_stagesurface_create(width, height, GS_BGRA);
2018-12-09 19:31:40 -06:00
obs_leave_graphics();
for (bool &written : context.surf_written)
written = false;
context.stage_index = 0;
const video_output_info *mainVOI =
video_output_get_info(obs_get_video());
video_output_info vi = {0};
vi.format = VIDEO_FORMAT_BGRA;
vi.width = width;
vi.height = height;
vi.fps_den = context.ovi.fps_den;
vi.fps_num = context.ovi.fps_num;
vi.cache_size = 16;
vi.colorspace = mainVOI->colorspace;
vi.range = VIDEO_RANGE_FULL;
vi.name = "decklink_preview_output";
2018-12-09 19:31:40 -06:00
video_output_open(&context.video_queue, &vi);
obs_frontend_add_event_callback(on_preview_scene_changed,
&context);
if (obs_frontend_preview_program_mode_active()) {
context.current_source =
obs_frontend_get_current_preview_scene();
} else {
context.current_source =
obs_frontend_get_current_scene();
}
obs_add_main_render_callback(render_preview_source, &context);
obs_output_set_media(context.output, context.video_queue,
obs_get_audio());
bool started = obs_output_start(context.output);
preview_output_running = started;
if (!shutting_down)
doUI->PreviewOutputStateChanged(started);
if (!started)
preview_output_stop();
2018-12-09 19:31:40 -06:00
}
}
void preview_output_toggle()
{
if (preview_output_running)
preview_output_stop();
else
preview_output_start();
}
2018-12-09 19:31:40 -06:00
void on_preview_scene_changed(enum obs_frontend_event event, void *param)
{
auto ctx = (struct preview_output *)param;
2018-12-09 19:31:40 -06:00
switch (event) {
case OBS_FRONTEND_EVENT_STUDIO_MODE_ENABLED:
case OBS_FRONTEND_EVENT_PREVIEW_SCENE_CHANGED:
obs_source_release(ctx->current_source);
ctx->current_source = obs_frontend_get_current_preview_scene();
break;
case OBS_FRONTEND_EVENT_STUDIO_MODE_DISABLED:
obs_source_release(ctx->current_source);
ctx->current_source = obs_frontend_get_current_scene();
break;
case OBS_FRONTEND_EVENT_SCENE_CHANGED:
if (!obs_frontend_preview_program_mode_active()) {
2018-12-09 19:31:40 -06:00
obs_source_release(ctx->current_source);
ctx->current_source = obs_frontend_get_current_scene();
}
break;
default:
break;
2018-12-09 19:31:40 -06:00
}
}
void render_preview_source(void *param, uint32_t cx, uint32_t cy)
{
2019-07-15 06:50:23 -05:00
UNUSED_PARAMETER(cx);
UNUSED_PARAMETER(cy);
auto ctx = (struct preview_output *)param;
2018-12-09 19:31:40 -06:00
if (!ctx->current_source)
return;
2018-12-09 19:31:40 -06:00
const uint32_t width = obs_source_get_base_width(ctx->current_source);
const uint32_t height = obs_source_get_base_height(ctx->current_source);
const struct video_scale_info *const conversion =
obs_output_get_video_conversion(context.output);
const uint32_t scaled_width = conversion->width;
const uint32_t scaled_height = conversion->height;
2018-12-09 19:31:40 -06:00
gs_texrender_t *const texrender_premultiplied =
ctx->texrender_premultiplied;
if (gs_texrender_begin(texrender_premultiplied, width, height)) {
2018-12-09 19:31:40 -06:00
struct vec4 background;
vec4_zero(&background);
gs_clear(GS_CLEAR_COLOR, &background, 0.0f, 0);
gs_ortho(0.0f, (float)width, 0.0f, (float)height, -100.0f,
100.0f);
2018-12-09 19:31:40 -06:00
gs_blend_state_push();
gs_blend_function(GS_BLEND_ONE, GS_BLEND_ZERO);
obs_source_video_render(ctx->current_source);
gs_blend_state_pop();
gs_texrender_end(texrender_premultiplied);
if (gs_texrender_begin(ctx->texrender, scaled_width,
scaled_height)) {
const bool previous = gs_framebuffer_srgb_enabled();
gs_enable_framebuffer_srgb(true);
gs_enable_blending(false);
gs_texture_t *const tex = gs_texrender_get_texture(
texrender_premultiplied);
gs_effect_t *const effect =
obs_get_base_effect(OBS_EFFECT_DEFAULT);
gs_effect_set_texture_srgb(
gs_effect_get_param_by_name(effect, "image"),
tex);
while (gs_effect_loop(effect, "DrawAlphaDivide")) {
gs_draw_sprite(tex, 0, 0, 0);
}
gs_enable_blending(true);
gs_enable_framebuffer_srgb(previous);
gs_texrender_end(ctx->texrender);
}
2018-12-09 19:31:40 -06:00
const size_t write_stage_index = ctx->stage_index;
gs_stage_texture(ctx->stagesurfaces[write_stage_index],
gs_texrender_get_texture(ctx->texrender));
ctx->surf_written[write_stage_index] = true;
const size_t read_stage_index =
(write_stage_index + 1) % STAGE_BUFFER_COUNT;
if (ctx->surf_written[read_stage_index]) {
struct video_frame output_frame;
if (video_output_lock_frame(ctx->video_queue,
&output_frame, 1,
os_gettime_ns())) {
gs_stagesurf_t *const read_surf =
ctx->stagesurfaces[read_stage_index];
if (gs_stagesurface_map(read_surf,
&ctx->video_data,
&ctx->video_linesize)) {
uint32_t linesize =
output_frame.linesize[0];
for (uint32_t i = 0; i < scaled_height;
i++) {
uint32_t dst_offset =
linesize * i;
uint32_t src_offset =
ctx->video_linesize * i;
memcpy(output_frame.data[0] +
dst_offset,
ctx->video_data +
src_offset,
linesize);
}
gs_stagesurface_unmap(read_surf);
ctx->video_data = nullptr;
2018-12-09 19:31:40 -06:00
}
video_output_unlock_frame(ctx->video_queue);
2018-12-09 19:31:40 -06:00
}
}
ctx->stage_index = read_stage_index;
2018-12-09 19:31:40 -06:00
}
}
2018-09-25 17:52:31 -05:00
void addOutputUI(void)
{
QAction *action = (QAction *)obs_frontend_add_tools_menu_qaction(
obs_module_text("Decklink Output"));
2018-09-25 17:52:31 -05:00
QMainWindow *window = (QMainWindow *)obs_frontend_get_main_window();
2018-09-25 17:52:31 -05:00
2018-12-09 19:31:40 -06:00
obs_frontend_push_ui_translation(obs_module_get_string);
2018-09-25 17:52:31 -05:00
doUI = new DecklinkOutputUI(window);
2018-12-09 19:31:40 -06:00
obs_frontend_pop_ui_translation();
2018-09-25 17:52:31 -05:00
auto cb = []() { doUI->ShowHideDialog(); };
2018-09-25 17:52:31 -05:00
action->connect(action, &QAction::triggered, cb);
}
static void OBSEvent(enum obs_frontend_event event, void *)
{
if (event == OBS_FRONTEND_EVENT_FINISHED_LOADING) {
OBSData settings = load_settings();
if (settings && obs_data_get_bool(settings, "auto_start"))
output_start();
2018-12-09 19:31:40 -06:00
OBSData previewSettings = load_preview_settings();
if (previewSettings &&
obs_data_get_bool(previewSettings, "auto_start"))
2018-12-09 19:31:40 -06:00
preview_output_start();
} else if (event == OBS_FRONTEND_EVENT_EXIT) {
shutting_down = true;
if (preview_output_running)
preview_output_stop();
if (main_output_running)
output_stop();
2018-09-25 17:52:31 -05:00
}
}
bool obs_module_load(void)
{
return true;
}
void obs_module_unload(void)
{
shutting_down = true;
if (preview_output_running)
preview_output_stop();
if (main_output_running)
output_stop();
}
void obs_module_post_load(void)
{
if (!obs_get_module("decklink"))
return;
addOutputUI();
obs_frontend_add_event_callback(OBSEvent, nullptr);
}