decklink-ui-main.cpp 7.4 KB

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