decklink-ui-main.cpp 7.2 KB

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