aja-ui-main.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. #include "aja-ui-main.h"
  2. #include "AJAOutputUI.h"
  3. #include "../../../plugins/aja/aja-ui-props.hpp"
  4. #include "../../../plugins/aja/aja-card-manager.hpp"
  5. #include "../../../plugins/aja/aja-routing.hpp"
  6. #include <obs-module.h>
  7. #include <obs-frontend-api.h>
  8. #include <QMainWindow>
  9. #include <QAction>
  10. #include <util/util.hpp>
  11. #include <util/platform.h>
  12. #include <media-io/video-io.h>
  13. #include <media-io/video-frame.h>
  14. #include <ajantv2/includes/ntv2devicescanner.h>
  15. OBS_DECLARE_MODULE()
  16. OBS_MODULE_USE_DEFAULT_LOCALE("aja-output-ui", "en-US")
  17. static aja::CardManager *cardManager = nullptr;
  18. static AJAOutputUI *ajaOutputUI = nullptr;
  19. static obs_output_t *output = nullptr;
  20. bool main_output_running = false;
  21. bool preview_output_running = false;
  22. struct preview_output {
  23. bool enabled;
  24. obs_source_t *current_source;
  25. obs_output_t *output;
  26. video_t *video_queue;
  27. gs_texrender_t *texrender;
  28. gs_stagesurf_t *stagesurface;
  29. uint8_t *video_data;
  30. uint32_t video_linesize;
  31. obs_video_info ovi;
  32. };
  33. static struct preview_output context = {0};
  34. OBSData load_settings(const char *filename)
  35. {
  36. BPtr<char> path = obs_module_get_config_path(obs_current_module(), filename);
  37. BPtr<char> jsonData = os_quick_read_utf8_file(path);
  38. if (!!jsonData) {
  39. obs_data_t *data = obs_data_create_from_json(jsonData);
  40. OBSData dataRet(data);
  41. obs_data_release(data);
  42. return dataRet;
  43. }
  44. return nullptr;
  45. }
  46. void output_stop()
  47. {
  48. obs_output_stop(output);
  49. obs_output_release(output);
  50. main_output_running = false;
  51. ajaOutputUI->OutputStateChanged(false);
  52. }
  53. void output_start()
  54. {
  55. OBSData settings = load_settings(kProgramPropsFilename);
  56. if (settings != nullptr) {
  57. output = obs_output_create("aja_output", kProgramOutputID, settings, NULL);
  58. bool started = obs_output_start(output);
  59. obs_data_release(settings);
  60. main_output_running = started;
  61. ajaOutputUI->OutputStateChanged(started);
  62. if (!started)
  63. output_stop();
  64. }
  65. }
  66. void output_toggle()
  67. {
  68. if (main_output_running)
  69. output_stop();
  70. else
  71. output_start();
  72. }
  73. void on_preview_scene_changed(enum obs_frontend_event event, void *param);
  74. void render_preview_source(void *param, uint32_t cx, uint32_t cy);
  75. void preview_output_stop()
  76. {
  77. obs_output_stop(context.output);
  78. obs_output_release(context.output);
  79. obs_remove_main_render_callback(render_preview_source, &context);
  80. obs_frontend_remove_event_callback(on_preview_scene_changed, &context);
  81. obs_source_release(context.current_source);
  82. obs_enter_graphics();
  83. gs_stagesurface_destroy(context.stagesurface);
  84. gs_texrender_destroy(context.texrender);
  85. obs_leave_graphics();
  86. video_output_close(context.video_queue);
  87. preview_output_running = false;
  88. ajaOutputUI->PreviewOutputStateChanged(false);
  89. }
  90. void preview_output_start()
  91. {
  92. OBSData settings = load_settings(kPreviewPropsFilename);
  93. if (settings != nullptr) {
  94. context.output = obs_output_create("aja_output", kPreviewOutputID, settings, NULL);
  95. obs_get_video_info(&context.ovi);
  96. uint32_t width = context.ovi.base_width;
  97. uint32_t height = context.ovi.base_height;
  98. obs_enter_graphics();
  99. context.texrender = gs_texrender_create(GS_BGRA, GS_ZS_NONE);
  100. context.stagesurface = gs_stagesurface_create(width, height, GS_BGRA);
  101. obs_leave_graphics();
  102. const video_output_info *mainVOI = video_output_get_info(obs_get_video());
  103. video_output_info vi = {0};
  104. vi.format = VIDEO_FORMAT_BGRA;
  105. vi.width = width;
  106. vi.height = height;
  107. vi.fps_den = context.ovi.fps_den;
  108. vi.fps_num = context.ovi.fps_num;
  109. vi.cache_size = 16;
  110. vi.colorspace = mainVOI->colorspace;
  111. vi.range = mainVOI->range;
  112. vi.name = kPreviewOutputID;
  113. video_output_open(&context.video_queue, &vi);
  114. obs_frontend_add_event_callback(on_preview_scene_changed, &context);
  115. if (obs_frontend_preview_program_mode_active()) {
  116. context.current_source = obs_frontend_get_current_preview_scene();
  117. } else {
  118. context.current_source = obs_frontend_get_current_scene();
  119. }
  120. obs_add_main_render_callback(render_preview_source, &context);
  121. obs_output_set_media(context.output, context.video_queue, obs_get_audio());
  122. bool started = obs_output_start(context.output);
  123. obs_data_release(settings);
  124. preview_output_running = started;
  125. ajaOutputUI->PreviewOutputStateChanged(started);
  126. if (!started)
  127. preview_output_stop();
  128. }
  129. }
  130. void preview_output_toggle()
  131. {
  132. if (preview_output_running)
  133. preview_output_stop();
  134. else
  135. preview_output_start();
  136. }
  137. void populate_misc_device_list(obs_property_t *list, aja::CardManager *cardManager, NTV2DeviceID &firstDeviceID)
  138. {
  139. for (const auto &iter : *cardManager) {
  140. if (!iter.second)
  141. continue;
  142. if (firstDeviceID == DEVICE_ID_NOTFOUND)
  143. firstDeviceID = iter.second->GetDeviceID();
  144. obs_property_list_add_string(list, iter.second->GetDisplayName().c_str(),
  145. iter.second->GetCardID().c_str());
  146. }
  147. }
  148. void populate_multi_view_audio_sources(obs_property_t *list, NTV2DeviceID id)
  149. {
  150. obs_property_list_clear(list);
  151. const QList<NTV2InputSource> kMultiViewAudioInputs = {
  152. NTV2_INPUTSOURCE_SDI1,
  153. NTV2_INPUTSOURCE_SDI2,
  154. NTV2_INPUTSOURCE_SDI3,
  155. NTV2_INPUTSOURCE_SDI4,
  156. };
  157. for (const auto &inp : kMultiViewAudioInputs) {
  158. if (NTV2DeviceCanDoInputSource(id, inp)) {
  159. std::string inputSourceStr = NTV2InputSourceToString(inp, true);
  160. obs_property_list_add_int(list, inputSourceStr.c_str(), (long long)inp);
  161. }
  162. }
  163. }
  164. bool on_misc_device_selected(void *data, obs_properties_t *props, obs_property_t *, obs_data_t *settings)
  165. {
  166. const char *cardID = obs_data_get_string(settings, kUIPropDevice.id);
  167. if (!cardID || !cardID[0])
  168. return false;
  169. aja::CardManager *cardManager = (aja::CardManager *)data;
  170. if (!cardManager)
  171. return false;
  172. auto cardEntry = cardManager->GetCardEntry(cardID);
  173. if (!cardEntry)
  174. return false;
  175. NTV2DeviceID deviceID = cardEntry->GetDeviceID();
  176. bool enableMultiViewUI = NTV2DeviceCanDoHDMIMultiView(deviceID);
  177. obs_property_t *multiViewCheckbox = obs_properties_get(props, kUIPropMultiViewEnable.id);
  178. obs_property_t *multiViewAudioSource = obs_properties_get(props, kUIPropMultiViewAudioSource.id);
  179. populate_multi_view_audio_sources(multiViewAudioSource, deviceID);
  180. obs_property_set_enabled(multiViewCheckbox, enableMultiViewUI);
  181. obs_property_set_enabled(multiViewAudioSource, enableMultiViewUI);
  182. return true;
  183. }
  184. static void toggle_multi_view(CNTV2Card *card, NTV2InputSource src, bool enable)
  185. {
  186. std::ostringstream oss;
  187. for (int i = 0; i < 4; i++) {
  188. std::string datastream = std::to_string(i);
  189. oss << "sdi[" << datastream << "][0]->hdmi[0][" << datastream << "];";
  190. }
  191. NTV2DeviceID deviceId = card->GetDeviceID();
  192. NTV2AudioSystem audioSys = NTV2InputSourceToAudioSystem(src);
  193. if (NTV2DeviceCanDoHDMIMultiView(deviceId)) {
  194. NTV2XptConnections cnx;
  195. if (aja::Routing::ParseRouteString(oss.str(), cnx)) {
  196. card->SetMultiRasterBypassEnable(!enable);
  197. if (enable) {
  198. card->ApplySignalRoute(cnx, false);
  199. if (NTV2DeviceCanDoAudioMixer(deviceId)) {
  200. card->SetAudioMixerInputAudioSystem(NTV2_AudioMixerInputMain, audioSys);
  201. card->SetAudioMixerInputChannelSelect(NTV2_AudioMixerInputMain,
  202. NTV2_AudioChannel1_2);
  203. card->SetAudioMixerInputChannelsMute(NTV2_AudioMixerInputAux1,
  204. NTV2AudioChannelsMuteAll);
  205. card->SetAudioMixerInputChannelsMute(NTV2_AudioMixerInputAux2,
  206. NTV2AudioChannelsMuteAll);
  207. }
  208. card->SetAudioLoopBack(NTV2_AUDIO_LOOPBACK_ON, audioSys);
  209. card->SetAudioOutputMonitorSource(NTV2_AudioChannel1_2, audioSys);
  210. card->SetHDMIOutAudioChannels(NTV2_HDMIAudio8Channels);
  211. card->SetHDMIOutAudioSource2Channel(NTV2_AudioChannel1_2, audioSys);
  212. card->SetHDMIOutAudioSource8Channel(NTV2_AudioChannel1_8, audioSys);
  213. } else {
  214. card->RemoveConnections(cnx);
  215. }
  216. }
  217. }
  218. }
  219. bool on_multi_view_toggle(void *data, obs_properties_t *, obs_property_t *, obs_data_t *settings)
  220. {
  221. bool multiViewEnabled = obs_data_get_bool(settings, kUIPropMultiViewEnable.id) && !main_output_running &&
  222. !preview_output_running;
  223. const int audioInputSource = obs_data_get_int(settings, kUIPropMultiViewAudioSource.id);
  224. const char *cardID = obs_data_get_string(settings, kUIPropDevice.id);
  225. if (!cardID || !cardID[0])
  226. return false;
  227. aja::CardManager *cardManager = (aja::CardManager *)data;
  228. if (!cardManager)
  229. return false;
  230. CNTV2Card *card = cardManager->GetCard(cardID);
  231. if (!card)
  232. return false;
  233. NTV2InputSource inputSource = (NTV2InputSource)audioInputSource;
  234. toggle_multi_view(card, inputSource, multiViewEnabled);
  235. return true;
  236. }
  237. // MISC PROPS
  238. void on_preview_scene_changed(enum obs_frontend_event event, void *param)
  239. {
  240. auto ctx = (struct preview_output *)param;
  241. switch (event) {
  242. case OBS_FRONTEND_EVENT_STUDIO_MODE_ENABLED:
  243. case OBS_FRONTEND_EVENT_PREVIEW_SCENE_CHANGED:
  244. obs_source_release(ctx->current_source);
  245. ctx->current_source = obs_frontend_get_current_preview_scene();
  246. break;
  247. case OBS_FRONTEND_EVENT_STUDIO_MODE_DISABLED:
  248. obs_source_release(ctx->current_source);
  249. ctx->current_source = obs_frontend_get_current_scene();
  250. break;
  251. case OBS_FRONTEND_EVENT_SCENE_CHANGED:
  252. if (!obs_frontend_preview_program_mode_active()) {
  253. obs_source_release(ctx->current_source);
  254. ctx->current_source = obs_frontend_get_current_scene();
  255. }
  256. break;
  257. default:
  258. break;
  259. }
  260. }
  261. void render_preview_source(void *param, uint32_t, uint32_t)
  262. {
  263. auto ctx = (struct preview_output *)param;
  264. if (!ctx->current_source)
  265. return;
  266. uint32_t width = obs_source_get_base_width(ctx->current_source);
  267. uint32_t height = obs_source_get_base_height(ctx->current_source);
  268. gs_texrender_reset(ctx->texrender);
  269. if (gs_texrender_begin(ctx->texrender, width, height)) {
  270. struct vec4 background;
  271. vec4_zero(&background);
  272. gs_clear(GS_CLEAR_COLOR, &background, 0.0f, 0);
  273. gs_ortho(0.0f, (float)width, 0.0f, (float)height, -100.0f, 100.0f);
  274. gs_blend_state_push();
  275. gs_blend_function(GS_BLEND_ONE, GS_BLEND_ZERO);
  276. obs_source_video_render(ctx->current_source);
  277. gs_blend_state_pop();
  278. gs_texrender_end(ctx->texrender);
  279. struct video_frame output_frame;
  280. if (video_output_lock_frame(ctx->video_queue, &output_frame, 1, os_gettime_ns())) {
  281. gs_stage_texture(ctx->stagesurface, gs_texrender_get_texture(ctx->texrender));
  282. if (gs_stagesurface_map(ctx->stagesurface, &ctx->video_data, &ctx->video_linesize)) {
  283. uint32_t linesize = output_frame.linesize[0];
  284. for (uint32_t i = 0; i < ctx->ovi.base_height; i++) {
  285. uint32_t dst_offset = linesize * i;
  286. uint32_t src_offset = ctx->video_linesize * i;
  287. memcpy(output_frame.data[0] + dst_offset, ctx->video_data + src_offset,
  288. linesize);
  289. }
  290. gs_stagesurface_unmap(ctx->stagesurface);
  291. ctx->video_data = nullptr;
  292. }
  293. video_output_unlock_frame(ctx->video_queue);
  294. }
  295. }
  296. }
  297. void addOutputUI(void)
  298. {
  299. QAction *action = (QAction *)obs_frontend_add_tools_menu_qaction("AJA I/O Device Output");
  300. QMainWindow *window = (QMainWindow *)obs_frontend_get_main_window();
  301. obs_frontend_push_ui_translation(obs_module_get_string);
  302. ajaOutputUI = new AJAOutputUI(window);
  303. obs_frontend_pop_ui_translation();
  304. auto cb = []() {
  305. ajaOutputUI->ShowHideDialog();
  306. };
  307. action->connect(action, &QAction::triggered, cb);
  308. }
  309. static void OBSEvent(enum obs_frontend_event event, void *)
  310. {
  311. if (event == OBS_FRONTEND_EVENT_FINISHED_LOADING) {
  312. OBSData settings = load_settings(kProgramPropsFilename);
  313. if (settings && obs_data_get_bool(settings, kUIPropAutoStartOutput.id))
  314. output_start();
  315. OBSData previewSettings = load_settings(kPreviewPropsFilename);
  316. if (previewSettings && obs_data_get_bool(previewSettings, kUIPropAutoStartOutput.id))
  317. preview_output_start();
  318. OBSData miscSettings = load_settings(kMiscPropsFilename);
  319. if (miscSettings && ajaOutputUI) {
  320. on_multi_view_toggle(ajaOutputUI->GetCardManager(), nullptr, nullptr, miscSettings);
  321. }
  322. } else if (event == OBS_FRONTEND_EVENT_EXIT) {
  323. if (main_output_running)
  324. output_stop();
  325. if (preview_output_running)
  326. preview_output_stop();
  327. }
  328. }
  329. static void aja_loaded(void * /* data */, calldata_t *calldata)
  330. {
  331. // Receive CardManager pointer from the main AJA plugin
  332. calldata_get_ptr(calldata, "card_manager", &cardManager);
  333. if (ajaOutputUI)
  334. ajaOutputUI->SetCardManager(cardManager);
  335. }
  336. bool obs_module_load(void)
  337. {
  338. CNTV2DeviceScanner scanner;
  339. auto numDevices = scanner.GetNumDevices();
  340. if (numDevices == 0) {
  341. blog(LOG_WARNING, "No AJA devices found, skipping loading AJA UI plugin");
  342. return false;
  343. }
  344. // Signal to wait for AJA plugin to finish loading so we can access the CardManager instance
  345. auto signal_handler = obs_get_signal_handler();
  346. signal_handler_add(signal_handler, "void aja_loaded(ptr card_manager)");
  347. signal_handler_connect(signal_handler, "aja_loaded", aja_loaded, nullptr);
  348. addOutputUI();
  349. obs_frontend_add_event_callback(OBSEvent, nullptr);
  350. return true;
  351. }