1
0

aja-ui-main.cpp 13 KB

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