decklink-ui-main.cpp 7.7 KB

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