decklink-ui-main.cpp 7.9 KB

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