decklink-ui-main.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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. obs_output_t *output;
  18. constexpr size_t STAGE_BUFFER_COUNT = 3;
  19. struct preview_output {
  20. bool enabled;
  21. obs_source_t *current_source;
  22. obs_output_t *output;
  23. video_t *video_queue;
  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 preview_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. void output_stop()
  47. {
  48. obs_output_stop(output);
  49. obs_output_release(output);
  50. main_output_running = false;
  51. if (!shutting_down)
  52. doUI->OutputStateChanged(false);
  53. }
  54. void output_start()
  55. {
  56. OBSData settings = load_settings();
  57. if (settings != nullptr) {
  58. output = obs_output_create("decklink_output", "decklink_output",
  59. settings, NULL);
  60. bool started = obs_output_start(output);
  61. main_output_running = started;
  62. if (!shutting_down)
  63. doUI->OutputStateChanged(started);
  64. if (!started)
  65. output_stop();
  66. }
  67. }
  68. void output_toggle()
  69. {
  70. if (main_output_running)
  71. output_stop();
  72. else
  73. output_start();
  74. }
  75. OBSData load_preview_settings()
  76. {
  77. BPtr<char> path = obs_module_get_config_path(
  78. obs_current_module(), "decklinkPreviewOutputProps.json");
  79. BPtr<char> jsonData = os_quick_read_utf8_file(path);
  80. if (!!jsonData) {
  81. obs_data_t *data = obs_data_create_from_json(jsonData);
  82. OBSData dataRet(data);
  83. obs_data_release(data);
  84. return dataRet;
  85. }
  86. return nullptr;
  87. }
  88. void on_preview_scene_changed(enum obs_frontend_event event, void *param);
  89. void render_preview_source(void *param, uint32_t cx, uint32_t cy);
  90. static void preview_tick(void *param, float sec)
  91. {
  92. UNUSED_PARAMETER(sec);
  93. auto ctx = (struct preview_output *)param;
  94. if (ctx->texrender)
  95. gs_texrender_reset(ctx->texrender);
  96. }
  97. void preview_output_stop()
  98. {
  99. obs_output_stop(context.output);
  100. obs_output_release(context.output);
  101. obs_remove_main_render_callback(render_preview_source, &context);
  102. obs_frontend_remove_event_callback(on_preview_scene_changed, &context);
  103. obs_source_release(context.current_source);
  104. obs_enter_graphics();
  105. for (gs_stagesurf_t *surf : context.stagesurfaces)
  106. gs_stagesurface_destroy(surf);
  107. gs_texrender_destroy(context.texrender);
  108. obs_leave_graphics();
  109. video_output_close(context.video_queue);
  110. obs_remove_tick_callback(preview_tick, &context);
  111. preview_output_running = false;
  112. if (!shutting_down)
  113. doUI->PreviewOutputStateChanged(false);
  114. }
  115. void preview_output_start()
  116. {
  117. OBSData settings = load_preview_settings();
  118. if (settings != nullptr) {
  119. obs_add_tick_callback(preview_tick, &context);
  120. context.output = obs_output_create("decklink_output",
  121. "decklink_preview_output",
  122. settings, NULL);
  123. obs_get_video_info(&context.ovi);
  124. uint32_t width = context.ovi.base_width;
  125. uint32_t height = context.ovi.base_height;
  126. obs_enter_graphics();
  127. context.texrender = gs_texrender_create(GS_BGRA, GS_ZS_NONE);
  128. for (gs_stagesurf_t *&surf : context.stagesurfaces)
  129. surf = gs_stagesurface_create(width, height, GS_BGRA);
  130. obs_leave_graphics();
  131. for (bool &written : context.surf_written)
  132. written = false;
  133. context.stage_index = 0;
  134. const video_output_info *mainVOI =
  135. video_output_get_info(obs_get_video());
  136. video_output_info vi = {0};
  137. vi.format = VIDEO_FORMAT_BGRA;
  138. vi.width = width;
  139. vi.height = height;
  140. vi.fps_den = context.ovi.fps_den;
  141. vi.fps_num = context.ovi.fps_num;
  142. vi.cache_size = 16;
  143. vi.colorspace = mainVOI->colorspace;
  144. vi.range = VIDEO_RANGE_FULL;
  145. vi.name = "decklink_preview_output";
  146. video_output_open(&context.video_queue, &vi);
  147. obs_frontend_add_event_callback(on_preview_scene_changed,
  148. &context);
  149. if (obs_frontend_preview_program_mode_active()) {
  150. context.current_source =
  151. obs_frontend_get_current_preview_scene();
  152. } else {
  153. context.current_source =
  154. obs_frontend_get_current_scene();
  155. }
  156. obs_add_main_render_callback(render_preview_source, &context);
  157. obs_output_set_media(context.output, context.video_queue,
  158. obs_get_audio());
  159. bool started = obs_output_start(context.output);
  160. preview_output_running = started;
  161. if (!shutting_down)
  162. doUI->PreviewOutputStateChanged(started);
  163. if (!started)
  164. preview_output_stop();
  165. }
  166. }
  167. void preview_output_toggle()
  168. {
  169. if (preview_output_running)
  170. preview_output_stop();
  171. else
  172. preview_output_start();
  173. }
  174. void on_preview_scene_changed(enum obs_frontend_event event, void *param)
  175. {
  176. auto ctx = (struct preview_output *)param;
  177. switch (event) {
  178. case OBS_FRONTEND_EVENT_STUDIO_MODE_ENABLED:
  179. case OBS_FRONTEND_EVENT_PREVIEW_SCENE_CHANGED:
  180. obs_source_release(ctx->current_source);
  181. ctx->current_source = obs_frontend_get_current_preview_scene();
  182. break;
  183. case OBS_FRONTEND_EVENT_STUDIO_MODE_DISABLED:
  184. obs_source_release(ctx->current_source);
  185. ctx->current_source = obs_frontend_get_current_scene();
  186. break;
  187. case OBS_FRONTEND_EVENT_SCENE_CHANGED:
  188. if (!obs_frontend_preview_program_mode_active()) {
  189. obs_source_release(ctx->current_source);
  190. ctx->current_source = obs_frontend_get_current_scene();
  191. }
  192. break;
  193. default:
  194. break;
  195. }
  196. }
  197. void render_preview_source(void *param, uint32_t cx, uint32_t cy)
  198. {
  199. UNUSED_PARAMETER(cx);
  200. UNUSED_PARAMETER(cy);
  201. auto ctx = (struct preview_output *)param;
  202. if (!ctx->current_source)
  203. return;
  204. uint32_t width = obs_source_get_base_width(ctx->current_source);
  205. uint32_t height = obs_source_get_base_height(ctx->current_source);
  206. if (gs_texrender_begin(ctx->texrender, width, height)) {
  207. struct vec4 background;
  208. vec4_zero(&background);
  209. gs_clear(GS_CLEAR_COLOR, &background, 0.0f, 0);
  210. gs_ortho(0.0f, (float)width, 0.0f, (float)height, -100.0f,
  211. 100.0f);
  212. gs_blend_state_push();
  213. gs_blend_function(GS_BLEND_ONE, GS_BLEND_ZERO);
  214. obs_source_video_render(ctx->current_source);
  215. gs_blend_state_pop();
  216. gs_texrender_end(ctx->texrender);
  217. const size_t write_stage_index = ctx->stage_index;
  218. gs_stage_texture(ctx->stagesurfaces[write_stage_index],
  219. gs_texrender_get_texture(ctx->texrender));
  220. ctx->surf_written[write_stage_index] = true;
  221. const size_t read_stage_index =
  222. (write_stage_index + 1) % STAGE_BUFFER_COUNT;
  223. if (ctx->surf_written[read_stage_index]) {
  224. struct video_frame output_frame;
  225. if (video_output_lock_frame(ctx->video_queue,
  226. &output_frame, 1,
  227. os_gettime_ns())) {
  228. gs_stagesurf_t *const read_surf =
  229. ctx->stagesurfaces[read_stage_index];
  230. if (gs_stagesurface_map(read_surf,
  231. &ctx->video_data,
  232. &ctx->video_linesize)) {
  233. uint32_t linesize =
  234. output_frame.linesize[0];
  235. for (uint32_t i = 0;
  236. i < ctx->ovi.base_height; i++) {
  237. uint32_t dst_offset =
  238. linesize * i;
  239. uint32_t src_offset =
  240. ctx->video_linesize * i;
  241. memcpy(output_frame.data[0] +
  242. dst_offset,
  243. ctx->video_data +
  244. src_offset,
  245. linesize);
  246. }
  247. gs_stagesurface_unmap(read_surf);
  248. ctx->video_data = nullptr;
  249. }
  250. video_output_unlock_frame(ctx->video_queue);
  251. }
  252. }
  253. ctx->stage_index = read_stage_index;
  254. }
  255. }
  256. void addOutputUI(void)
  257. {
  258. QAction *action = (QAction *)obs_frontend_add_tools_menu_qaction(
  259. obs_module_text("Decklink Output"));
  260. QMainWindow *window = (QMainWindow *)obs_frontend_get_main_window();
  261. obs_frontend_push_ui_translation(obs_module_get_string);
  262. doUI = new DecklinkOutputUI(window);
  263. obs_frontend_pop_ui_translation();
  264. auto cb = []() { doUI->ShowHideDialog(); };
  265. action->connect(action, &QAction::triggered, cb);
  266. }
  267. static void OBSEvent(enum obs_frontend_event event, void *)
  268. {
  269. if (event == OBS_FRONTEND_EVENT_FINISHED_LOADING) {
  270. OBSData settings = load_settings();
  271. if (settings && obs_data_get_bool(settings, "auto_start"))
  272. output_start();
  273. OBSData previewSettings = load_preview_settings();
  274. if (previewSettings &&
  275. obs_data_get_bool(previewSettings, "auto_start"))
  276. preview_output_start();
  277. } else if (event == OBS_FRONTEND_EVENT_EXIT) {
  278. shutting_down = true;
  279. if (preview_output_running)
  280. preview_output_stop();
  281. if (main_output_running)
  282. output_stop();
  283. }
  284. }
  285. bool obs_module_load(void)
  286. {
  287. return true;
  288. }
  289. void obs_module_unload(void)
  290. {
  291. shutting_down = true;
  292. if (preview_output_running)
  293. preview_output_stop();
  294. if (main_output_running)
  295. output_stop();
  296. }
  297. void obs_module_post_load(void)
  298. {
  299. if (!obs_get_module("decklink"))
  300. return;
  301. addOutputUI();
  302. obs_frontend_add_event_callback(OBSEvent, nullptr);
  303. }