decklink-ui-main.cpp 8.4 KB

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