decklink-ui-main.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. #include <obs-module.h>
  2. #include <obs-frontend-api.h>
  3. #include <QMainWindow>
  4. #include <QAction>
  5. #include <util/util.hpp>
  6. #include <util/platform.h>
  7. #include <media-io/video-io.h>
  8. #include <media-io/video-frame.h>
  9. #include "DecklinkOutputUI.h"
  10. #include "../../../plugins/decklink/const.h"
  11. OBS_DECLARE_MODULE()
  12. OBS_MODULE_USE_DEFAULT_LOCALE("decklink-output-ui", "en-US")
  13. DecklinkOutputUI *doUI;
  14. bool shutting_down = false;
  15. bool main_output_running = false;
  16. bool preview_output_running = false;
  17. constexpr size_t STAGE_BUFFER_COUNT = 3;
  18. struct decklink_ui_output {
  19. bool enabled;
  20. obs_source_t *current_source;
  21. obs_output_t *output;
  22. video_t *video_queue;
  23. gs_texrender_t *texrender_premultiplied;
  24. gs_texrender_t *texrender;
  25. gs_stagesurf_t *stagesurfaces[STAGE_BUFFER_COUNT];
  26. bool surf_written[STAGE_BUFFER_COUNT];
  27. size_t stage_index;
  28. uint8_t *video_data;
  29. uint32_t video_linesize;
  30. obs_video_info ovi;
  31. };
  32. static struct decklink_ui_output context = {0};
  33. static struct decklink_ui_output context_preview = {0};
  34. OBSData load_settings()
  35. {
  36. BPtr<char> path = obs_module_get_config_path(
  37. obs_current_module(), "decklinkOutputProps.json");
  38. BPtr<char> jsonData = os_quick_read_utf8_file(path);
  39. if (!!jsonData) {
  40. obs_data_t *data = obs_data_create_from_json(jsonData);
  41. OBSData dataRet(data);
  42. obs_data_release(data);
  43. return dataRet;
  44. }
  45. return nullptr;
  46. }
  47. static void decklink_ui_tick(void *param, float sec);
  48. static void decklink_ui_render(void *param);
  49. void output_stop()
  50. {
  51. obs_remove_main_rendered_callback(decklink_ui_render, &context);
  52. obs_output_stop(context.output);
  53. obs_output_release(context.output);
  54. obs_enter_graphics();
  55. for (gs_stagesurf_t *&surf : context.stagesurfaces) {
  56. gs_stagesurface_destroy(surf);
  57. surf = nullptr;
  58. }
  59. gs_texrender_destroy(context.texrender);
  60. context.texrender = nullptr;
  61. obs_leave_graphics();
  62. video_output_close(context.video_queue);
  63. obs_remove_tick_callback(decklink_ui_tick, &context);
  64. main_output_running = false;
  65. if (!shutting_down)
  66. doUI->OutputStateChanged(false);
  67. }
  68. void output_start()
  69. {
  70. OBSData settings = load_settings();
  71. if (settings != nullptr) {
  72. obs_output_t *const output = obs_output_create(
  73. "decklink_output", "decklink_output", settings, NULL);
  74. const struct video_scale_info *const conversion =
  75. obs_output_get_video_conversion(output);
  76. if (conversion != nullptr) {
  77. context.output = output;
  78. obs_add_tick_callback(decklink_ui_tick, &context);
  79. obs_get_video_info(&context.ovi);
  80. const uint32_t width = conversion->width;
  81. const uint32_t height = conversion->height;
  82. obs_enter_graphics();
  83. context.texrender_premultiplied = nullptr;
  84. context.texrender =
  85. gs_texrender_create(GS_BGRA, GS_ZS_NONE);
  86. for (gs_stagesurf_t *&surf : context.stagesurfaces)
  87. surf = gs_stagesurface_create(width, height,
  88. GS_BGRA);
  89. obs_leave_graphics();
  90. for (bool &written : context.surf_written)
  91. written = false;
  92. context.stage_index = 0;
  93. video_output_info vi = {0};
  94. vi.format = VIDEO_FORMAT_BGRA;
  95. vi.width = width;
  96. vi.height = height;
  97. vi.fps_den = context.ovi.fps_den;
  98. vi.fps_num = context.ovi.fps_num;
  99. vi.cache_size = 16;
  100. vi.colorspace = VIDEO_CS_DEFAULT;
  101. vi.range = VIDEO_RANGE_FULL;
  102. vi.name = "decklink_output";
  103. video_output_open(&context.video_queue, &vi);
  104. context.current_source = nullptr;
  105. obs_add_main_rendered_callback(decklink_ui_render,
  106. &context);
  107. obs_output_set_media(context.output,
  108. context.video_queue,
  109. obs_get_audio());
  110. bool started = obs_output_start(context.output);
  111. main_output_running = started;
  112. if (!shutting_down)
  113. doUI->OutputStateChanged(started);
  114. if (!started)
  115. output_stop();
  116. } else {
  117. obs_output_release(output);
  118. }
  119. }
  120. }
  121. void output_toggle()
  122. {
  123. if (main_output_running)
  124. output_stop();
  125. else
  126. output_start();
  127. }
  128. OBSData load_preview_settings()
  129. {
  130. BPtr<char> path = obs_module_get_config_path(
  131. obs_current_module(), "decklinkPreviewOutputProps.json");
  132. BPtr<char> jsonData = os_quick_read_utf8_file(path);
  133. if (!!jsonData) {
  134. obs_data_t *data = obs_data_create_from_json(jsonData);
  135. OBSData dataRet(data);
  136. obs_data_release(data);
  137. return dataRet;
  138. }
  139. return nullptr;
  140. }
  141. void on_preview_scene_changed(enum obs_frontend_event event, void *param);
  142. static void decklink_ui_tick(void *param, float /* sec */)
  143. {
  144. auto ctx = (struct decklink_ui_output *)param;
  145. if (ctx->texrender_premultiplied)
  146. gs_texrender_reset(ctx->texrender_premultiplied);
  147. if (ctx->texrender)
  148. gs_texrender_reset(ctx->texrender);
  149. }
  150. void preview_output_stop()
  151. {
  152. obs_remove_main_rendered_callback(decklink_ui_render, &context_preview);
  153. obs_frontend_remove_event_callback(on_preview_scene_changed,
  154. &context_preview);
  155. obs_output_stop(context_preview.output);
  156. obs_output_release(context_preview.output);
  157. obs_source_release(context_preview.current_source);
  158. obs_enter_graphics();
  159. for (gs_stagesurf_t *&surf : context_preview.stagesurfaces) {
  160. gs_stagesurface_destroy(surf);
  161. surf = nullptr;
  162. }
  163. gs_texrender_destroy(context_preview.texrender);
  164. context_preview.texrender = nullptr;
  165. gs_texrender_destroy(context_preview.texrender_premultiplied);
  166. context_preview.texrender_premultiplied = nullptr;
  167. obs_leave_graphics();
  168. video_output_close(context_preview.video_queue);
  169. obs_remove_tick_callback(decklink_ui_tick, &context_preview);
  170. preview_output_running = false;
  171. if (!shutting_down)
  172. doUI->PreviewOutputStateChanged(false);
  173. }
  174. void preview_output_start()
  175. {
  176. OBSData settings = load_preview_settings();
  177. if (settings != nullptr) {
  178. obs_output_t *const output = obs_output_create(
  179. "decklink_output", "decklink_output", settings, NULL);
  180. const struct video_scale_info *const conversion =
  181. obs_output_get_video_conversion(output);
  182. if (conversion != nullptr) {
  183. context_preview.output = output;
  184. obs_add_tick_callback(decklink_ui_tick,
  185. &context_preview);
  186. obs_get_video_info(&context_preview.ovi);
  187. const uint32_t width = conversion->width;
  188. const uint32_t height = conversion->height;
  189. obs_enter_graphics();
  190. context_preview.texrender_premultiplied =
  191. gs_texrender_create(GS_BGRA, GS_ZS_NONE);
  192. context_preview.texrender =
  193. gs_texrender_create(GS_BGRA, GS_ZS_NONE);
  194. for (gs_stagesurf_t *&surf :
  195. context_preview.stagesurfaces)
  196. surf = gs_stagesurface_create(width, height,
  197. GS_BGRA);
  198. obs_leave_graphics();
  199. for (bool &written : context_preview.surf_written)
  200. written = false;
  201. context_preview.stage_index = 0;
  202. video_output_info vi = {0};
  203. vi.format = VIDEO_FORMAT_BGRA;
  204. vi.width = width;
  205. vi.height = height;
  206. vi.fps_den = context_preview.ovi.fps_den;
  207. vi.fps_num = context_preview.ovi.fps_num;
  208. vi.cache_size = 16;
  209. vi.colorspace = VIDEO_CS_DEFAULT;
  210. vi.range = VIDEO_RANGE_FULL;
  211. vi.name = "decklink_preview_output";
  212. video_output_open(&context_preview.video_queue, &vi);
  213. obs_frontend_add_event_callback(
  214. on_preview_scene_changed, &context_preview);
  215. if (obs_frontend_preview_program_mode_active()) {
  216. context_preview.current_source =
  217. obs_frontend_get_current_preview_scene();
  218. } else {
  219. context_preview.current_source =
  220. obs_frontend_get_current_scene();
  221. }
  222. obs_add_main_rendered_callback(decklink_ui_render,
  223. &context_preview);
  224. obs_output_set_media(context_preview.output,
  225. context_preview.video_queue,
  226. obs_get_audio());
  227. bool started = obs_output_start(context_preview.output);
  228. preview_output_running = started;
  229. if (!shutting_down)
  230. doUI->PreviewOutputStateChanged(started);
  231. if (!started)
  232. preview_output_stop();
  233. } else {
  234. obs_output_release(output);
  235. }
  236. }
  237. }
  238. void preview_output_toggle()
  239. {
  240. if (preview_output_running)
  241. preview_output_stop();
  242. else
  243. preview_output_start();
  244. }
  245. void on_preview_scene_changed(enum obs_frontend_event event, void *param)
  246. {
  247. auto ctx = (struct decklink_ui_output *)param;
  248. switch (event) {
  249. case OBS_FRONTEND_EVENT_STUDIO_MODE_ENABLED:
  250. case OBS_FRONTEND_EVENT_PREVIEW_SCENE_CHANGED:
  251. obs_source_release(ctx->current_source);
  252. ctx->current_source = obs_frontend_get_current_preview_scene();
  253. break;
  254. case OBS_FRONTEND_EVENT_STUDIO_MODE_DISABLED:
  255. obs_source_release(ctx->current_source);
  256. ctx->current_source = obs_frontend_get_current_scene();
  257. break;
  258. case OBS_FRONTEND_EVENT_SCENE_CHANGED:
  259. if (!obs_frontend_preview_program_mode_active()) {
  260. obs_source_release(ctx->current_source);
  261. ctx->current_source = obs_frontend_get_current_scene();
  262. }
  263. break;
  264. default:
  265. break;
  266. }
  267. }
  268. static void decklink_ui_render(void *param)
  269. {
  270. auto *const ctx = (struct decklink_ui_output *)param;
  271. uint32_t width = 0;
  272. uint32_t height = 0;
  273. gs_texture_t *tex = nullptr;
  274. if (ctx == &context) {
  275. if (!main_output_running)
  276. return;
  277. tex = obs_get_main_texture();
  278. if (!tex)
  279. return;
  280. width = gs_texture_get_width(tex);
  281. height = gs_texture_get_height(tex);
  282. } else if (ctx == &context_preview) {
  283. if (!preview_output_running)
  284. return;
  285. if (!ctx->current_source)
  286. return;
  287. width = obs_source_get_base_width(ctx->current_source);
  288. height = obs_source_get_base_height(ctx->current_source);
  289. gs_texrender_t *const texrender_premultiplied =
  290. ctx->texrender_premultiplied;
  291. if (!gs_texrender_begin(texrender_premultiplied, width, height))
  292. return;
  293. struct vec4 background;
  294. vec4_zero(&background);
  295. gs_clear(GS_CLEAR_COLOR, &background, 0.0f, 0);
  296. gs_ortho(0.0f, (float)width, 0.0f, (float)height, -100.0f,
  297. 100.0f);
  298. gs_blend_state_push();
  299. gs_blend_function(GS_BLEND_ONE, GS_BLEND_ZERO);
  300. obs_source_video_render(ctx->current_source);
  301. gs_blend_state_pop();
  302. gs_texrender_end(texrender_premultiplied);
  303. tex = gs_texrender_get_texture(texrender_premultiplied);
  304. } else {
  305. return;
  306. }
  307. const struct video_scale_info *const conversion =
  308. obs_output_get_video_conversion(ctx->output);
  309. const uint32_t scaled_width = conversion->width;
  310. const uint32_t scaled_height = conversion->height;
  311. if (!gs_texrender_begin(ctx->texrender, scaled_width, scaled_height))
  312. return;
  313. const bool previous = gs_framebuffer_srgb_enabled();
  314. const bool source_hdr = (ctx->ovi.colorspace == VIDEO_CS_2100_PQ) ||
  315. (ctx->ovi.colorspace == VIDEO_CS_2100_HLG);
  316. const bool target_hdr = source_hdr &&
  317. (conversion->colorspace == VIDEO_CS_2100_PQ);
  318. gs_enable_framebuffer_srgb(!target_hdr);
  319. gs_enable_blending(false);
  320. gs_effect_t *const effect = obs_get_base_effect(OBS_EFFECT_DEFAULT);
  321. gs_effect_set_texture_srgb(gs_effect_get_param_by_name(effect, "image"),
  322. tex);
  323. const char *const tech_name =
  324. target_hdr ? "DrawAlphaDivideR10L"
  325. : (source_hdr ? "DrawAlphaDivideTonemap"
  326. : "DrawAlphaDivide");
  327. while (gs_effect_loop(effect, tech_name)) {
  328. gs_effect_set_float(gs_effect_get_param_by_name(effect,
  329. "multiplier"),
  330. obs_get_video_sdr_white_level() / 10000.f);
  331. gs_draw_sprite(tex, 0, 0, 0);
  332. }
  333. gs_enable_blending(true);
  334. gs_enable_framebuffer_srgb(previous);
  335. gs_texrender_end(ctx->texrender);
  336. const size_t write_stage_index = ctx->stage_index;
  337. gs_stage_texture(ctx->stagesurfaces[write_stage_index],
  338. gs_texrender_get_texture(ctx->texrender));
  339. ctx->surf_written[write_stage_index] = true;
  340. const size_t read_stage_index =
  341. (write_stage_index + 1) % STAGE_BUFFER_COUNT;
  342. if (ctx->surf_written[read_stage_index]) {
  343. struct video_frame output_frame;
  344. if (video_output_lock_frame(ctx->video_queue, &output_frame, 1,
  345. os_gettime_ns())) {
  346. gs_stagesurf_t *const read_surf =
  347. ctx->stagesurfaces[read_stage_index];
  348. if (gs_stagesurface_map(read_surf, &ctx->video_data,
  349. &ctx->video_linesize)) {
  350. uint32_t linesize = output_frame.linesize[0];
  351. for (uint32_t i = 0; i < scaled_height; i++) {
  352. uint32_t dst_offset = linesize * i;
  353. uint32_t src_offset =
  354. ctx->video_linesize * i;
  355. memcpy(output_frame.data[0] +
  356. dst_offset,
  357. ctx->video_data + src_offset,
  358. linesize);
  359. }
  360. gs_stagesurface_unmap(read_surf);
  361. ctx->video_data = nullptr;
  362. }
  363. video_output_unlock_frame(ctx->video_queue);
  364. }
  365. }
  366. ctx->stage_index = read_stage_index;
  367. }
  368. void addOutputUI(void)
  369. {
  370. QAction *action = (QAction *)obs_frontend_add_tools_menu_qaction(
  371. obs_module_text("Decklink Output"));
  372. QMainWindow *window = (QMainWindow *)obs_frontend_get_main_window();
  373. obs_frontend_push_ui_translation(obs_module_get_string);
  374. doUI = new DecklinkOutputUI(window);
  375. obs_frontend_pop_ui_translation();
  376. auto cb = []() {
  377. doUI->ShowHideDialog();
  378. };
  379. action->connect(action, &QAction::triggered, cb);
  380. }
  381. static void OBSEvent(enum obs_frontend_event event, void *)
  382. {
  383. if (event == OBS_FRONTEND_EVENT_FINISHED_LOADING) {
  384. OBSData settings = load_settings();
  385. if (settings && obs_data_get_bool(settings, "auto_start"))
  386. output_start();
  387. OBSData previewSettings = load_preview_settings();
  388. if (previewSettings &&
  389. obs_data_get_bool(previewSettings, "auto_start"))
  390. preview_output_start();
  391. } else if (event == OBS_FRONTEND_EVENT_EXIT) {
  392. shutting_down = true;
  393. if (preview_output_running)
  394. preview_output_stop();
  395. if (main_output_running)
  396. output_stop();
  397. }
  398. }
  399. bool obs_module_load(void)
  400. {
  401. return true;
  402. }
  403. void obs_module_unload(void)
  404. {
  405. shutting_down = true;
  406. if (preview_output_running)
  407. preview_output_stop();
  408. if (main_output_running)
  409. output_stop();
  410. }
  411. void obs_module_post_load(void)
  412. {
  413. if (!obs_get_module("decklink"))
  414. return;
  415. addOutputUI();
  416. obs_frontend_add_event_callback(OBSEvent, nullptr);
  417. }