aja-ui-main.cpp 13 KB

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