decklink-ui-main.cpp 8.4 KB

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