decklink-ui-main.cpp 13 KB

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