decklink-ui-main.cpp 7.6 KB

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