decklink-ui-main.cpp 7.5 KB

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