aja-source.cpp 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096
  1. #include "aja-card-manager.hpp"
  2. #include "aja-common.hpp"
  3. #include "aja-ui-props.hpp"
  4. #include "aja-source.hpp"
  5. #include "aja-routing.hpp"
  6. #include <util/threading.h>
  7. #include <util/platform.h>
  8. #include <util/dstr.h>
  9. #include <util/util_uint64.h>
  10. #include <obs-module.h>
  11. #include <ajantv2/includes/ntv2card.h>
  12. #include <ajantv2/includes/ntv2utils.h>
  13. #ifndef NSEC_PER_SEC
  14. #define NSEC_PER_SEC 1000000000LL
  15. #endif
  16. #define NTV2_AUDIOSIZE_MAX (401 * 1024)
  17. static constexpr int kDefaultAudioCaptureChannels = 2;
  18. static inline audio_repack_mode_t ConvertRepackFormat(speaker_layout format, bool swap = false)
  19. {
  20. switch (format) {
  21. case SPEAKERS_STEREO:
  22. return repack_mode_8to2ch;
  23. case SPEAKERS_2POINT1:
  24. return repack_mode_8to3ch;
  25. case SPEAKERS_4POINT0:
  26. return repack_mode_8to4ch;
  27. case SPEAKERS_4POINT1:
  28. return swap ? repack_mode_8to5ch_swap : repack_mode_8to5ch;
  29. case SPEAKERS_5POINT1:
  30. return swap ? repack_mode_8to6ch_swap : repack_mode_8to6ch;
  31. case SPEAKERS_7POINT1:
  32. return swap ? repack_mode_8ch_swap : repack_mode_8ch;
  33. default:
  34. assert(false && "No repack requested");
  35. return (audio_repack_mode_t)-1;
  36. }
  37. }
  38. AJASource::AJASource(obs_source_t *source)
  39. : mVideoBuffer{},
  40. mAudioBuffer{},
  41. mCard{nullptr},
  42. mSourceName{""},
  43. mCardID{""},
  44. mDeviceIndex{0},
  45. mBuffering{false},
  46. mIsCapturing{false},
  47. mSourceProps{},
  48. mTestPattern{},
  49. mCaptureThread{nullptr},
  50. mMutex{},
  51. mSource{source},
  52. mCrosspoints{}
  53. {
  54. }
  55. AJASource::~AJASource()
  56. {
  57. Deactivate();
  58. mTestPattern.clear();
  59. mVideoBuffer.Deallocate();
  60. mAudioBuffer.Deallocate();
  61. mVideoBuffer = 0;
  62. mAudioBuffer = 0;
  63. }
  64. void AJASource::SetCard(CNTV2Card *card)
  65. {
  66. mCard = card;
  67. }
  68. CNTV2Card *AJASource::GetCard()
  69. {
  70. return mCard;
  71. }
  72. void AJASource::SetOBSSource(obs_source_t *source)
  73. {
  74. mSource = source;
  75. }
  76. obs_source_t *AJASource::GetOBSSource(void) const
  77. {
  78. return mSource;
  79. }
  80. void AJASource::SetName(const std::string &name)
  81. {
  82. mSourceName = name;
  83. }
  84. std::string AJASource::GetName() const
  85. {
  86. return mSourceName;
  87. }
  88. void populate_source_device_list(obs_property_t *list)
  89. {
  90. obs_property_list_clear(list);
  91. auto &cardManager = aja::CardManager::Instance();
  92. cardManager.EnumerateCards();
  93. for (const auto &iter : cardManager.GetCardEntries()) {
  94. if (iter.second) {
  95. CNTV2Card *card = iter.second->GetCard();
  96. if (!card)
  97. continue;
  98. if (aja::IsOutputOnlyDevice(iter.second->GetDeviceID()))
  99. continue;
  100. obs_property_list_add_string(list, iter.second->GetDisplayName().c_str(),
  101. iter.second->GetCardID().c_str());
  102. }
  103. }
  104. }
  105. //
  106. // Capture Thread stuff
  107. //
  108. struct AudioOffsets {
  109. ULWord currentAddress = 0;
  110. ULWord lastAddress = 0;
  111. ULWord readOffset = 0;
  112. ULWord wrapAddress = 0;
  113. ULWord bytesRead = 0;
  114. };
  115. static void ResetAudioBufferOffsets(CNTV2Card *card, NTV2AudioSystem audioSystem, AudioOffsets &offsets)
  116. {
  117. if (!card)
  118. return;
  119. offsets.currentAddress = 0;
  120. offsets.lastAddress = 0;
  121. offsets.readOffset = 0;
  122. offsets.wrapAddress = 0;
  123. offsets.bytesRead = 0;
  124. card->GetAudioReadOffset(offsets.readOffset, audioSystem);
  125. card->GetAudioWrapAddress(offsets.wrapAddress, audioSystem);
  126. offsets.wrapAddress += offsets.readOffset;
  127. offsets.lastAddress = offsets.readOffset;
  128. }
  129. void AJASource::GenerateTestPattern(NTV2VideoFormat vf, NTV2PixelFormat pf, NTV2TestPatternSelect ps)
  130. {
  131. NTV2VideoFormat vid_fmt = vf;
  132. NTV2PixelFormat pix_fmt = pf;
  133. if (vid_fmt == NTV2_FORMAT_UNKNOWN)
  134. vid_fmt = NTV2_FORMAT_720p_5994;
  135. if (pix_fmt == NTV2_FBF_INVALID)
  136. pix_fmt = kDefaultAJAPixelFormat;
  137. NTV2FormatDesc fd(vid_fmt, pix_fmt, NTV2_VANCMODE_OFF);
  138. auto bufSize = fd.GetTotalRasterBytes();
  139. if (bufSize != mTestPattern.size()) {
  140. mTestPattern.clear();
  141. mTestPattern.resize(bufSize);
  142. NTV2TestPatternGen gen;
  143. gen.DrawTestPattern(ps, fd.GetRasterWidth(), fd.GetRasterHeight(), pix_fmt, mTestPattern);
  144. }
  145. if (mTestPattern.size() == 0) {
  146. blog(LOG_DEBUG, "AJASource::GenerateTestPattern: Error generating test pattern!");
  147. return;
  148. }
  149. const enum video_format obs_vid_fmt = aja::AJAPixelFormatToOBSVideoFormat(pix_fmt);
  150. struct obs_source_frame2 obsFrame;
  151. obsFrame.flip = false;
  152. obsFrame.timestamp = os_gettime_ns();
  153. obsFrame.width = fd.GetRasterWidth();
  154. obsFrame.height = fd.GetRasterHeight();
  155. obsFrame.format = obs_vid_fmt;
  156. obsFrame.data[0] = mTestPattern.data();
  157. obsFrame.linesize[0] = fd.GetBytesPerRow();
  158. video_colorspace colorspace = VIDEO_CS_709;
  159. if (NTV2_IS_SD_VIDEO_FORMAT(vid_fmt))
  160. colorspace = VIDEO_CS_601;
  161. video_format_get_parameters_for_format(colorspace, VIDEO_RANGE_PARTIAL, obs_vid_fmt, obsFrame.color_matrix,
  162. obsFrame.color_range_min, obsFrame.color_range_max);
  163. obs_source_output_video2(mSource, &obsFrame);
  164. blog(LOG_DEBUG, "AJASource::GenerateTestPattern: Black");
  165. }
  166. static inline uint64_t samples_to_ns(size_t frames, uint_fast32_t rate)
  167. {
  168. return util_mul_div64(frames, NSEC_PER_SEC, rate);
  169. }
  170. static inline uint64_t get_sample_time(size_t frames, uint_fast32_t rate)
  171. {
  172. return os_gettime_ns() - samples_to_ns(frames, rate);
  173. }
  174. static bool CheckTransferAudioDMA(CNTV2Card *card, const NTV2AudioSystem audioSystem, void *buffer, ULWord bufferSize,
  175. ULWord dmaOffset, ULWord dmaSize, const char *log_id)
  176. {
  177. if (dmaSize > bufferSize) {
  178. blog(LOG_DEBUG, "AJASource::CaptureThread: Audio overrun %s! Buffer Size: %u, Bytes Captured: %u",
  179. log_id, bufferSize, dmaSize);
  180. return false;
  181. }
  182. card->DMAReadAudio(audioSystem, (ULWord *)buffer, dmaOffset, dmaSize);
  183. return true;
  184. }
  185. static inline bool TransferAudio(CNTV2Card *card, const NTV2AudioSystem audioSystem, NTV2_POINTER &audioBuffer,
  186. AudioOffsets &offsets)
  187. {
  188. card->ReadAudioLastIn(offsets.currentAddress, audioSystem);
  189. offsets.currentAddress += 1;
  190. offsets.currentAddress += offsets.readOffset;
  191. if (offsets.currentAddress < offsets.lastAddress) {
  192. offsets.bytesRead = offsets.wrapAddress - offsets.lastAddress;
  193. if (!CheckTransferAudioDMA(card, audioSystem, audioBuffer, audioBuffer.GetByteCount(),
  194. offsets.lastAddress, offsets.bytesRead, "(1)"))
  195. return false;
  196. if (!CheckTransferAudioDMA(card, audioSystem, audioBuffer.GetHostAddress(offsets.bytesRead),
  197. audioBuffer.GetByteCount() - offsets.bytesRead, offsets.readOffset,
  198. offsets.currentAddress - offsets.readOffset, "(2)"))
  199. return false;
  200. offsets.bytesRead += offsets.currentAddress - offsets.readOffset;
  201. } else {
  202. offsets.bytesRead = offsets.currentAddress - offsets.lastAddress;
  203. if (!CheckTransferAudioDMA(card, audioSystem, audioBuffer, audioBuffer.GetByteCount(),
  204. offsets.lastAddress, offsets.bytesRead, "(3)"))
  205. return false;
  206. }
  207. return true;
  208. }
  209. void AJASource::CaptureThread(AJAThread *thread, void *data)
  210. {
  211. UNUSED_PARAMETER(thread);
  212. auto ajaSource = (AJASource *)data;
  213. if (!ajaSource) {
  214. blog(LOG_WARNING, "AJASource::CaptureThread: Plugin instance is null!");
  215. return;
  216. }
  217. blog(LOG_INFO, "AJASource::CaptureThread: Starting capture thread for AJA source %s",
  218. ajaSource->GetName().c_str());
  219. auto card = ajaSource->GetCard();
  220. if (!card) {
  221. blog(LOG_ERROR, "AJASource::CaptureThread: Card instance is null!");
  222. return;
  223. }
  224. auto sourceProps = ajaSource->GetSourceProps();
  225. ajaSource->ResetVideoBuffer(sourceProps.videoFormat, sourceProps.pixelFormat);
  226. auto inputSource = sourceProps.InitialInputSource();
  227. auto channel = sourceProps.Channel();
  228. auto framestore = sourceProps.Framestore();
  229. auto audioSystem = sourceProps.AudioSystem();
  230. // Current "on-air" frame on the card. The capture thread "Ping-pongs" between
  231. // two frames, starting at an index corresponding to the framestore channel.
  232. // For example:
  233. // Channel 1 (index 0) = frames 0/1
  234. // Channel 2 (index 1) = frames 2/3
  235. // Channel 3 (index 2) = frames 4/5
  236. // Channel 4 (index 3) = frames 6/7
  237. // etc...
  238. ULWord currentCardFrame = GetIndexForNTV2Channel(framestore) * 2;
  239. card->WaitForInputFieldID(NTV2_FIELD0, channel);
  240. currentCardFrame ^= 1;
  241. card->SetInputFrame(framestore, currentCardFrame);
  242. AudioOffsets offsets;
  243. ResetAudioBufferOffsets(card, audioSystem, offsets);
  244. obs_data_t *settings = obs_source_get_settings(ajaSource->mSource);
  245. AudioRepacker *audioRepacker =
  246. new AudioRepacker(ConvertRepackFormat(sourceProps.SpeakerLayout(), sourceProps.swapFrontCenterLFE),
  247. sourceProps.audioSampleSize * 8);
  248. while (ajaSource->IsCapturing()) {
  249. if (card->GetModelName() == "(Not Found)") {
  250. os_sleep_ms(250);
  251. obs_source_update(ajaSource->mSource, settings);
  252. break;
  253. }
  254. auto videoFormat = sourceProps.videoFormat;
  255. auto pixelFormat = sourceProps.pixelFormat;
  256. auto ioSelection = sourceProps.ioSelect;
  257. card->WaitForInputFieldID(NTV2_FIELD0, channel);
  258. currentCardFrame ^= 1;
  259. // Card format detection -- restarts capture thread via aja_source_update callback
  260. auto newVideoFormat = card->GetInputVideoFormat(inputSource, aja::Is3GLevelB(card, channel));
  261. if (newVideoFormat == NTV2_FORMAT_UNKNOWN) {
  262. blog(LOG_DEBUG, "AJASource::CaptureThread: Video format unknown!");
  263. ajaSource->GenerateTestPattern(videoFormat, pixelFormat, NTV2_TestPatt_Black);
  264. os_sleep_ms(250);
  265. continue;
  266. }
  267. newVideoFormat = aja::HandleSpecialCaseFormats(ioSelection, newVideoFormat, sourceProps.deviceID);
  268. if (sourceProps.autoDetect && (videoFormat != newVideoFormat)) {
  269. blog(LOG_INFO,
  270. "AJASource::CaptureThread: New Video Format detected! Triggering 'aja_source_update' callback and returning...");
  271. blog(LOG_INFO, "AJASource::CaptureThread: Current Video Format: %s, | Want Video Format: %s",
  272. NTV2VideoFormatToString(videoFormat, true).c_str(),
  273. NTV2VideoFormatToString(newVideoFormat, true).c_str());
  274. os_sleep_ms(250);
  275. obs_source_update(ajaSource->mSource, settings);
  276. break;
  277. }
  278. if (!TransferAudio(card, audioSystem, ajaSource->mAudioBuffer, offsets)) {
  279. ResetAudioBufferOffsets(card, audioSystem, offsets);
  280. } else {
  281. offsets.lastAddress = offsets.currentAddress;
  282. uint32_t sampleCount =
  283. offsets.bytesRead / (kDefaultAudioChannels * sourceProps.audioSampleSize);
  284. obs_source_audio audioPacket;
  285. audioPacket.samples_per_sec = sourceProps.audioSampleRate;
  286. audioPacket.format = sourceProps.AudioFormat();
  287. audioPacket.speakers = sourceProps.SpeakerLayout();
  288. audioPacket.frames = sampleCount;
  289. audioPacket.timestamp = get_sample_time(audioPacket.frames, sourceProps.audioSampleRate);
  290. uint8_t *hostAudioBuffer = (uint8_t *)ajaSource->mAudioBuffer.GetHostPointer();
  291. if (sourceProps.audioNumChannels >= 2 && sourceProps.audioNumChannels <= 6) {
  292. /* Repack 8ch audio to fit specified OBS speaker layout */
  293. audioRepacker->repack(hostAudioBuffer, sampleCount);
  294. audioPacket.data[0] = (*audioRepacker)->packet_buffer;
  295. } else {
  296. /* Silence, or pass-through 8ch of audio */
  297. if (sourceProps.audioNumChannels == 0)
  298. memset(hostAudioBuffer, 0, offsets.bytesRead);
  299. audioPacket.data[0] = hostAudioBuffer;
  300. }
  301. obs_source_output_audio(ajaSource->mSource, &audioPacket);
  302. }
  303. if (ajaSource->mVideoBuffer.GetByteCount() == 0) {
  304. blog(LOG_DEBUG, "AJASource::CaptureThread: 0 bytes in video buffer! Something went wrong!");
  305. continue;
  306. }
  307. card->DMAReadFrame(currentCardFrame, ajaSource->mVideoBuffer, ajaSource->mVideoBuffer.GetByteCount());
  308. auto actualVideoFormat = videoFormat;
  309. if (aja::Is3GLevelB(card, channel))
  310. actualVideoFormat = aja::GetLevelAFormatForLevelBFormat(videoFormat);
  311. const enum video_format obs_vid_fmt = aja::AJAPixelFormatToOBSVideoFormat(sourceProps.pixelFormat);
  312. NTV2FormatDesc fd(actualVideoFormat, pixelFormat);
  313. struct obs_source_frame2 obsFrame;
  314. obsFrame.flip = false;
  315. obsFrame.timestamp = os_gettime_ns();
  316. obsFrame.width = fd.GetRasterWidth();
  317. obsFrame.height = fd.GetRasterHeight();
  318. obsFrame.format = obs_vid_fmt;
  319. obsFrame.data[0] = reinterpret_cast<uint8_t *>((ULWord *)ajaSource->mVideoBuffer.GetHostPointer());
  320. obsFrame.linesize[0] = fd.GetBytesPerRow();
  321. video_colorspace colorspace = VIDEO_CS_709;
  322. if (NTV2_IS_SD_VIDEO_FORMAT(actualVideoFormat))
  323. colorspace = VIDEO_CS_601;
  324. video_format_get_parameters_for_format(colorspace, VIDEO_RANGE_PARTIAL, obs_vid_fmt,
  325. obsFrame.color_matrix, obsFrame.color_range_min,
  326. obsFrame.color_range_max);
  327. obs_source_output_video2(ajaSource->mSource, &obsFrame);
  328. card->SetInputFrame(framestore, currentCardFrame);
  329. }
  330. blog(LOG_INFO, "AJASource::Capturethread: Thread loop stopped");
  331. if (audioRepacker) {
  332. delete audioRepacker;
  333. audioRepacker = nullptr;
  334. }
  335. ajaSource->GenerateTestPattern(sourceProps.videoFormat, sourceProps.pixelFormat, NTV2_TestPatt_Black);
  336. obs_data_release(settings);
  337. }
  338. void AJASource::Deactivate()
  339. {
  340. SetCapturing(false);
  341. if (mCaptureThread) {
  342. if (mCaptureThread->Active()) {
  343. mCaptureThread->Stop();
  344. blog(LOG_INFO, "AJASource::CaptureThread: Stopped!");
  345. }
  346. delete mCaptureThread;
  347. mCaptureThread = nullptr;
  348. blog(LOG_INFO, "AJASource::CaptureThread: Destroyed!");
  349. }
  350. }
  351. void AJASource::Activate(bool enable)
  352. {
  353. if (mCaptureThread == nullptr) {
  354. mCaptureThread = new AJAThread();
  355. mCaptureThread->Attach(AJASource::CaptureThread, this);
  356. mCaptureThread->SetPriority(AJA_ThreadPriority_High);
  357. blog(LOG_INFO, "AJASource::CaptureThread: Created!");
  358. }
  359. if (enable) {
  360. SetCapturing(true);
  361. if (!mCaptureThread->Active()) {
  362. mCaptureThread->Start();
  363. blog(LOG_INFO, "AJASource::CaptureThread: Started!");
  364. }
  365. }
  366. }
  367. bool AJASource::IsCapturing() const
  368. {
  369. return mIsCapturing;
  370. }
  371. void AJASource::SetCapturing(bool capturing)
  372. {
  373. std::lock_guard<std::mutex> lock(mMutex);
  374. mIsCapturing = capturing;
  375. }
  376. //
  377. // CardEntry/Device stuff
  378. //
  379. std::string AJASource::CardID() const
  380. {
  381. return mCardID;
  382. }
  383. void AJASource::SetCardID(const std::string &cardID)
  384. {
  385. mCardID = cardID;
  386. }
  387. uint32_t AJASource::DeviceIndex() const
  388. {
  389. return static_cast<uint32_t>(mDeviceIndex);
  390. }
  391. void AJASource::SetDeviceIndex(uint32_t index)
  392. {
  393. mDeviceIndex = static_cast<UWord>(index);
  394. }
  395. //
  396. // AJASource Properties stuff
  397. //
  398. void AJASource::SetSourceProps(const SourceProps &props)
  399. {
  400. mSourceProps = props;
  401. }
  402. SourceProps AJASource::GetSourceProps() const
  403. {
  404. return mSourceProps;
  405. }
  406. void AJASource::CacheConnections(const NTV2XptConnections &cnx)
  407. {
  408. mCrosspoints.clear();
  409. mCrosspoints = cnx;
  410. }
  411. void AJASource::ClearConnections()
  412. {
  413. for (auto &&xpt : mCrosspoints) {
  414. mCard->Connect(xpt.first, NTV2_XptBlack);
  415. }
  416. mCrosspoints.clear();
  417. }
  418. bool AJASource::ReadChannelVPIDs(NTV2Channel channel, VPIDData &vpids)
  419. {
  420. ULWord vpid_a = 0;
  421. ULWord vpid_b = 0;
  422. bool read_ok = mCard->ReadSDIInVPID(channel, vpid_a, vpid_b);
  423. vpids.SetA(vpid_a);
  424. vpids.SetB(vpid_b);
  425. vpids.Parse();
  426. return read_ok;
  427. }
  428. bool AJASource::ReadWireFormats(NTV2DeviceID device_id, IOSelection io_select, NTV2VideoFormat &vf, NTV2PixelFormat &pf,
  429. VPIDDataList &vpids)
  430. {
  431. NTV2InputSourceSet input_srcs;
  432. aja::IOSelectionToInputSources(io_select, input_srcs);
  433. if (input_srcs.empty()) {
  434. blog(LOG_INFO, "AJASource::ReadWireFormats: No NTV2InputSources found for IOSelection %s",
  435. aja::IOSelectionToString(io_select).c_str());
  436. return false;
  437. }
  438. NTV2InputSource initial_src = *input_srcs.begin();
  439. for (auto &&src : input_srcs) {
  440. auto channel = NTV2InputSourceToChannel(src);
  441. mCard->EnableChannel(channel);
  442. if (NTV2_INPUT_SOURCE_IS_SDI(src)) {
  443. if (NTV2DeviceHasBiDirectionalSDI(device_id)) {
  444. mCard->SetSDITransmitEnable(channel, false);
  445. }
  446. mCard->WaitForInputVerticalInterrupt(channel);
  447. VPIDData vpid_data;
  448. if (ReadChannelVPIDs(channel, vpid_data))
  449. vpids.push_back(vpid_data);
  450. } else if (NTV2_INPUT_SOURCE_IS_HDMI(src)) {
  451. mCard->WaitForInputVerticalInterrupt(channel);
  452. ULWord hdmi_version = NTV2DeviceGetHDMIVersion(device_id);
  453. // HDMIv1 handles its own RGB->YCbCr color space conversion
  454. if (hdmi_version == 1) {
  455. pf = kDefaultAJAPixelFormat;
  456. } else {
  457. NTV2LHIHDMIColorSpace hdmiInputColor;
  458. mCard->GetHDMIInputColor(hdmiInputColor, channel);
  459. if (hdmiInputColor == NTV2_LHIHDMIColorSpaceYCbCr) {
  460. pf = kDefaultAJAPixelFormat;
  461. } else if (hdmiInputColor == NTV2_LHIHDMIColorSpaceRGB) {
  462. pf = NTV2_FBF_24BIT_BGR;
  463. }
  464. }
  465. }
  466. }
  467. NTV2Channel initial_channel = NTV2InputSourceToChannel(initial_src);
  468. mCard->WaitForInputVerticalInterrupt(initial_channel);
  469. vf = mCard->GetInputVideoFormat(initial_src, aja::Is3GLevelB(mCard, initial_channel));
  470. if (NTV2_INPUT_SOURCE_IS_SDI(initial_src)) {
  471. if (vpids.size() > 0) {
  472. auto vpid = *vpids.begin();
  473. if (vpid.Sampling() == VPIDSampling_YUV_422) {
  474. if (vpid.BitDepth() == VPIDBitDepth_8)
  475. pf = NTV2_FBF_8BIT_YCBCR;
  476. else if (vpid.BitDepth() == VPIDBitDepth_10)
  477. pf = NTV2_FBF_10BIT_YCBCR;
  478. blog(LOG_INFO, "AJASource::ReadWireFormats - Detected pixel format %s",
  479. NTV2FrameBufferFormatToString(pf, true).c_str());
  480. } else if (vpid.Sampling() == VPIDSampling_GBR_444) {
  481. pf = NTV2_FBF_24BIT_BGR;
  482. blog(LOG_INFO, "AJASource::ReadWireFormats - Detected pixel format %s",
  483. NTV2FrameBufferFormatToString(pf, true).c_str());
  484. }
  485. }
  486. }
  487. vf = aja::HandleSpecialCaseFormats(io_select, vf, device_id);
  488. blog(LOG_INFO, "AJASource::ReadWireFormats - Detected video format %s", NTV2VideoFormatToString(vf).c_str());
  489. return true;
  490. }
  491. void AJASource::ResetVideoBuffer(NTV2VideoFormat vf, NTV2PixelFormat pf)
  492. {
  493. if (vf != NTV2_FORMAT_UNKNOWN) {
  494. auto videoBufferSize = GetVideoWriteSize(vf, pf);
  495. if (mVideoBuffer)
  496. mVideoBuffer.Deallocate();
  497. mVideoBuffer.Allocate(videoBufferSize, true);
  498. blog(LOG_INFO, "AJASource::ResetVideoBuffer: Video Format: %s | Pixel Format: %s | Buffer Size: %d",
  499. NTV2VideoFormatToString(vf, false).c_str(), NTV2FrameBufferFormatToString(pf, true).c_str(),
  500. videoBufferSize);
  501. }
  502. }
  503. void AJASource::ResetAudioBuffer(size_t size)
  504. {
  505. if (mAudioBuffer)
  506. mAudioBuffer.Deallocate();
  507. mAudioBuffer.Allocate(size, true);
  508. }
  509. static const char *aja_source_get_name(void *);
  510. static void *aja_source_create(obs_data_t *, obs_source_t *);
  511. static void aja_source_destroy(void *);
  512. static void aja_source_activate(void *);
  513. static void aja_source_deactivate(void *);
  514. static void aja_source_update(void *, obs_data_t *);
  515. const char *aja_source_get_name(void *unused)
  516. {
  517. UNUSED_PARAMETER(unused);
  518. return obs_module_text(kUIPropCaptureModule.text);
  519. }
  520. bool aja_source_device_changed(void *data, obs_properties_t *props, obs_property_t *list, obs_data_t *settings)
  521. {
  522. UNUSED_PARAMETER(list);
  523. blog(LOG_DEBUG, "AJA Source Device Changed");
  524. auto *ajaSource = (AJASource *)data;
  525. if (!ajaSource) {
  526. blog(LOG_DEBUG, "aja_source_device_changed: AJA Source instance is null!");
  527. return false;
  528. }
  529. const char *cardID = obs_data_get_string(settings, kUIPropDevice.id);
  530. if (!cardID || !cardID[0])
  531. return false;
  532. auto &cardManager = aja::CardManager::Instance();
  533. auto cardEntry = cardManager.GetCardEntry(cardID);
  534. if (!cardEntry) {
  535. blog(LOG_DEBUG, "aja_source_device_changed: Card Entry not found for %s", cardID);
  536. return false;
  537. }
  538. blog(LOG_DEBUG, "Found CardEntry for %s", cardID);
  539. CNTV2Card *card = cardEntry->GetCard();
  540. if (!card) {
  541. blog(LOG_DEBUG, "aja_source_device_changed: Card instance is null!");
  542. return false;
  543. }
  544. const NTV2DeviceID deviceID = card->GetDeviceID();
  545. /* If Channel 1 is actively in use, filter the video format list to only
  546. * show video formats within the same framerate family. If Channel 1 is
  547. * not active we just go ahead and try to set all framestores to the same video format.
  548. * This is because Channel 1's clock rate will govern the card's Free Run clock.
  549. */
  550. NTV2VideoFormat videoFormatChannel1 = NTV2_FORMAT_UNKNOWN;
  551. if (!cardEntry->ChannelReady(NTV2_CHANNEL1, ajaSource->GetName())) {
  552. card->GetVideoFormat(videoFormatChannel1, NTV2_CHANNEL1);
  553. }
  554. obs_property_t *devices_list = obs_properties_get(props, kUIPropDevice.id);
  555. obs_property_t *io_select_list = obs_properties_get(props, kUIPropInput.id);
  556. obs_property_t *vid_fmt_list = obs_properties_get(props, kUIPropVideoFormatSelect.id);
  557. obs_property_t *pix_fmt_list = obs_properties_get(props, kUIPropPixelFormatSelect.id);
  558. obs_property_t *sdi_trx_list = obs_properties_get(props, kUIPropSDITransport.id);
  559. obs_property_t *sdi_4k_list = obs_properties_get(props, kUIPropSDITransport4K.id);
  560. obs_property_t *channel_format_list = obs_properties_get(props, kUIPropChannelFormat.id);
  561. obs_property_list_clear(vid_fmt_list);
  562. obs_property_list_add_int(vid_fmt_list, obs_module_text("Auto"), kAutoDetect);
  563. populate_video_format_list(deviceID, vid_fmt_list, videoFormatChannel1, true);
  564. obs_property_list_clear(pix_fmt_list);
  565. obs_property_list_add_int(pix_fmt_list, obs_module_text("Auto"), kAutoDetect);
  566. populate_pixel_format_list(deviceID, {kDefaultAJAPixelFormat, NTV2_FBF_10BIT_YCBCR, NTV2_FBF_24BIT_BGR},
  567. pix_fmt_list);
  568. IOSelection io_select = static_cast<IOSelection>(obs_data_get_int(settings, kUIPropInput.id));
  569. obs_property_list_clear(sdi_trx_list);
  570. populate_sdi_transport_list(sdi_trx_list, deviceID, true);
  571. obs_property_list_clear(sdi_4k_list);
  572. populate_sdi_4k_transport_list(sdi_4k_list);
  573. obs_property_list_clear(channel_format_list);
  574. obs_property_list_add_int(channel_format_list, TEXT_CHANNEL_FORMAT_NONE, 0);
  575. obs_property_list_add_int(channel_format_list, TEXT_CHANNEL_FORMAT_2_0CH, 2);
  576. obs_property_list_add_int(channel_format_list, TEXT_CHANNEL_FORMAT_2_1CH, 3);
  577. obs_property_list_add_int(channel_format_list, TEXT_CHANNEL_FORMAT_4_0CH, 4);
  578. obs_property_list_add_int(channel_format_list, TEXT_CHANNEL_FORMAT_4_1CH, 5);
  579. obs_property_list_add_int(channel_format_list, TEXT_CHANNEL_FORMAT_5_1CH, 6);
  580. obs_property_list_add_int(channel_format_list, TEXT_CHANNEL_FORMAT_7_1CH, 8);
  581. populate_io_selection_input_list(cardID, ajaSource->GetName(), deviceID, io_select_list);
  582. auto curr_vf = static_cast<NTV2VideoFormat>(obs_data_get_int(settings, kUIPropVideoFormatSelect.id));
  583. bool have_cards = cardManager.NumCardEntries() > 0;
  584. obs_property_set_visible(devices_list, have_cards);
  585. obs_property_set_visible(io_select_list, have_cards);
  586. obs_property_set_visible(vid_fmt_list, have_cards);
  587. obs_property_set_visible(pix_fmt_list, have_cards);
  588. obs_property_set_visible(sdi_trx_list, have_cards && aja::IsIOSelectionSDI(io_select));
  589. obs_property_set_visible(sdi_4k_list, have_cards && NTV2_IS_4K_VIDEO_FORMAT(curr_vf));
  590. return true;
  591. }
  592. bool aja_io_selection_changed(void *data, obs_properties_t *props, obs_property_t *list, obs_data_t *settings)
  593. {
  594. UNUSED_PARAMETER(list);
  595. AJASource *ajaSource = (AJASource *)data;
  596. if (!ajaSource) {
  597. blog(LOG_DEBUG, "aja_io_selection_changed: AJA Source instance is null!");
  598. return false;
  599. }
  600. const char *cardID = obs_data_get_string(settings, kUIPropDevice.id);
  601. if (!cardID || !cardID[0])
  602. return false;
  603. auto &cardManager = aja::CardManager::Instance();
  604. auto cardEntry = cardManager.GetCardEntry(cardID);
  605. if (!cardEntry) {
  606. blog(LOG_DEBUG, "aja_io_selection_changed: Card Entry not found for %s", cardID);
  607. return false;
  608. }
  609. filter_io_selection_input_list(cardID, ajaSource->GetName(), obs_properties_get(props, kUIPropInput.id));
  610. obs_property_set_visible(
  611. obs_properties_get(props, kUIPropSDITransport.id),
  612. aja::IsIOSelectionSDI(static_cast<IOSelection>(obs_data_get_int(settings, kUIPropInput.id))));
  613. return true;
  614. }
  615. bool aja_sdi_mode_list_changed(obs_properties_t *props, obs_property_t *list, obs_data_t *settings)
  616. {
  617. UNUSED_PARAMETER(props);
  618. UNUSED_PARAMETER(list);
  619. UNUSED_PARAMETER(settings);
  620. return true;
  621. }
  622. void *aja_source_create(obs_data_t *settings, obs_source_t *source)
  623. {
  624. blog(LOG_DEBUG, "AJA Source Create");
  625. auto ajaSource = new AJASource(source);
  626. ajaSource->SetName(obs_source_get_name(source));
  627. obs_source_set_async_decoupled(source, true);
  628. ajaSource->SetOBSSource(source);
  629. ajaSource->ResetAudioBuffer(NTV2_AUDIOSIZE_MAX);
  630. ajaSource->Activate(false);
  631. obs_source_update(source, settings);
  632. return ajaSource;
  633. }
  634. void aja_source_destroy(void *data)
  635. {
  636. blog(LOG_DEBUG, "AJA Source Destroy");
  637. auto ajaSource = (AJASource *)data;
  638. if (!ajaSource) {
  639. blog(LOG_ERROR, "aja_source_destroy: Plugin instance is null!");
  640. return;
  641. }
  642. ajaSource->Deactivate();
  643. NTV2DeviceID deviceID = DEVICE_ID_NOTFOUND;
  644. CNTV2Card *card = ajaSource->GetCard();
  645. if (card) {
  646. deviceID = card->GetDeviceID();
  647. aja::Routing::StopSourceAudio(ajaSource->GetSourceProps(), card);
  648. }
  649. ajaSource->mVideoBuffer.Deallocate();
  650. ajaSource->mAudioBuffer.Deallocate();
  651. ajaSource->mVideoBuffer = 0;
  652. ajaSource->mAudioBuffer = 0;
  653. auto &cardManager = aja::CardManager::Instance();
  654. const auto &cardID = ajaSource->CardID();
  655. auto cardEntry = cardManager.GetCardEntry(cardID);
  656. if (!cardEntry) {
  657. blog(LOG_DEBUG, "aja_source_destroy: Card Entry not found for %s", cardID.c_str());
  658. return;
  659. }
  660. auto ioSelect = ajaSource->GetSourceProps().ioSelect;
  661. if (!cardEntry->ReleaseInputSelection(ioSelect, deviceID, ajaSource->GetName())) {
  662. blog(LOG_WARNING, "aja_source_destroy: Error releasing Input Selection!");
  663. }
  664. delete ajaSource;
  665. ajaSource = nullptr;
  666. }
  667. static void aja_source_show(void *data)
  668. {
  669. auto ajaSource = (AJASource *)data;
  670. if (!ajaSource) {
  671. blog(LOG_ERROR, "aja_source_show: AJA Source instance is null!");
  672. return;
  673. }
  674. bool deactivateWhileNotShowing = ajaSource->GetSourceProps().deactivateWhileNotShowing;
  675. bool showing = obs_source_showing(ajaSource->GetOBSSource());
  676. blog(LOG_DEBUG, "aja_source_show: deactivateWhileNotShowing = %s, showing = %s",
  677. deactivateWhileNotShowing ? "true" : "false", showing ? "true" : "false");
  678. if (deactivateWhileNotShowing && showing && !ajaSource->IsCapturing()) {
  679. ajaSource->Activate(true);
  680. blog(LOG_DEBUG, "aja_source_show: activated capture thread!");
  681. }
  682. }
  683. static void aja_source_hide(void *data)
  684. {
  685. auto ajaSource = (AJASource *)data;
  686. if (!ajaSource)
  687. return;
  688. bool deactivateWhileNotShowing = ajaSource->GetSourceProps().deactivateWhileNotShowing;
  689. bool showing = obs_source_showing(ajaSource->GetOBSSource());
  690. blog(LOG_DEBUG, "aja_source_hide: deactivateWhileNotShowing = %s, showing = %s",
  691. deactivateWhileNotShowing ? "true" : "false", showing ? "true" : "false");
  692. if (deactivateWhileNotShowing && !showing && ajaSource->IsCapturing()) {
  693. ajaSource->Deactivate();
  694. blog(LOG_DEBUG, "aja_source_hide: deactivated capture thread!");
  695. }
  696. }
  697. static void aja_source_activate(void *data)
  698. {
  699. UNUSED_PARAMETER(data);
  700. }
  701. static void aja_source_deactivate(void *data)
  702. {
  703. UNUSED_PARAMETER(data);
  704. }
  705. static void aja_source_update(void *data, obs_data_t *settings)
  706. {
  707. static bool initialized = false;
  708. auto ajaSource = (AJASource *)data;
  709. if (!ajaSource) {
  710. blog(LOG_WARNING, "aja_source_update: Plugin instance is null!");
  711. return;
  712. }
  713. auto io_select = static_cast<IOSelection>(obs_data_get_int(settings, kUIPropInput.id));
  714. auto vf_select = static_cast<NTV2VideoFormat>(obs_data_get_int(settings, kUIPropVideoFormatSelect.id));
  715. auto pf_select = static_cast<NTV2PixelFormat>(obs_data_get_int(settings, kUIPropPixelFormatSelect.id));
  716. auto sdi_trx_select = static_cast<SDITransport>(obs_data_get_int(settings, kUIPropSDITransport.id));
  717. auto sdi_t4k_select = static_cast<SDITransport4K>(obs_data_get_int(settings, kUIPropSDITransport4K.id));
  718. auto num_audio_channels = obs_data_get_int(settings, kUIPropChannelFormat.id);
  719. bool deactivateWhileNotShowing = obs_data_get_bool(settings, kUIPropDeactivateWhenNotShowing.id);
  720. bool swapFrontCenterLFE = obs_data_get_bool(settings, kUIPropChannelSwap_FC_LFE.id);
  721. const std::string &wantCardID = obs_data_get_string(settings, kUIPropDevice.id);
  722. obs_source_set_async_unbuffered(ajaSource->GetOBSSource(), !obs_data_get_bool(settings, kUIPropBuffering.id));
  723. const std::string &currentCardID = ajaSource->CardID();
  724. if (wantCardID != currentCardID) {
  725. initialized = false;
  726. ajaSource->Deactivate();
  727. }
  728. auto &cardManager = aja::CardManager::Instance();
  729. cardManager.EnumerateCards();
  730. auto cardEntry = cardManager.GetCardEntry(wantCardID);
  731. if (!cardEntry) {
  732. blog(LOG_DEBUG, "aja_source_update: Card Entry not found for %s", wantCardID.c_str());
  733. return;
  734. }
  735. CNTV2Card *card = cardEntry->GetCard();
  736. if (!card || !card->IsOpen()) {
  737. blog(LOG_ERROR, "aja_source_update: AJA device %s not open!", wantCardID.c_str());
  738. return;
  739. }
  740. if (card->GetModelName() == "(Not Found)") {
  741. blog(LOG_ERROR, "aja_source_update: AJA device %s disconnected?", wantCardID.c_str());
  742. return;
  743. }
  744. ajaSource->SetCard(cardEntry->GetCard());
  745. SourceProps curr_props = ajaSource->GetSourceProps();
  746. // Release Channels from previous card if card ID changes
  747. if (wantCardID != currentCardID) {
  748. auto prevCardEntry = cardManager.GetCardEntry(currentCardID);
  749. if (prevCardEntry) {
  750. const std::string &ioSelectStr = aja::IOSelectionToString(curr_props.ioSelect);
  751. if (!prevCardEntry->ReleaseInputSelection(curr_props.ioSelect, curr_props.deviceID,
  752. ajaSource->GetName())) {
  753. blog(LOG_WARNING, "aja_source_update: Error releasing IOSelection %s for card ID %s",
  754. ioSelectStr.c_str(), currentCardID.c_str());
  755. } else {
  756. blog(LOG_INFO, "aja_source_update: Released IOSelection %s for card ID %s",
  757. ioSelectStr.c_str(), currentCardID.c_str());
  758. ajaSource->SetCardID(wantCardID);
  759. io_select = IOSelection::Invalid;
  760. }
  761. }
  762. }
  763. if (io_select == IOSelection::Invalid) {
  764. blog(LOG_DEBUG, "aja_source_update: Invalid IOSelection");
  765. return;
  766. }
  767. SourceProps want_props;
  768. want_props.deviceID = card->GetDeviceID();
  769. want_props.ioSelect = io_select;
  770. want_props.videoFormat = ((int32_t)vf_select == kAutoDetect) ? NTV2_FORMAT_UNKNOWN
  771. : static_cast<NTV2VideoFormat>(vf_select);
  772. want_props.pixelFormat = ((int32_t)pf_select == kAutoDetect) ? NTV2_FBF_INVALID
  773. : static_cast<NTV2PixelFormat>(pf_select);
  774. want_props.sdiTransport = ((int32_t)sdi_trx_select == kAutoDetect) ? SDITransport::Unknown
  775. : static_cast<SDITransport>(sdi_trx_select);
  776. want_props.sdi4kTransport = sdi_t4k_select;
  777. want_props.audioNumChannels = (uint32_t)num_audio_channels;
  778. want_props.swapFrontCenterLFE = swapFrontCenterLFE;
  779. want_props.vpids.clear();
  780. want_props.deactivateWhileNotShowing = deactivateWhileNotShowing;
  781. if (aja::IsIOSelectionSDI(io_select)) {
  782. want_props.autoDetect = (int)sdi_trx_select == kAutoDetect;
  783. } else {
  784. want_props.autoDetect = ((int)vf_select == kAutoDetect || (int)pf_select == kAutoDetect);
  785. }
  786. ajaSource->SetCardID(wantCardID);
  787. ajaSource->SetDeviceIndex((UWord)cardEntry->GetCardIndex());
  788. // Release Channels if IOSelection changes
  789. if (want_props.ioSelect != curr_props.ioSelect) {
  790. const std::string &ioSelectStr = aja::IOSelectionToString(curr_props.ioSelect);
  791. if (!cardEntry->ReleaseInputSelection(curr_props.ioSelect, curr_props.deviceID, ajaSource->GetName())) {
  792. blog(LOG_WARNING, "aja_source_update: Error releasing IOSelection %s for card ID %s",
  793. ioSelectStr.c_str(), currentCardID.c_str());
  794. } else {
  795. blog(LOG_INFO, "aja_source_update: Released IOSelection %s for card ID %s", ioSelectStr.c_str(),
  796. currentCardID.c_str());
  797. }
  798. }
  799. // Acquire Channels for current IOSelection
  800. if (!cardEntry->AcquireInputSelection(want_props.ioSelect, want_props.deviceID, ajaSource->GetName())) {
  801. blog(LOG_ERROR, "aja_source_update: Could not acquire IOSelection %s",
  802. aja::IOSelectionToString(want_props.ioSelect).c_str());
  803. return;
  804. }
  805. // Read SDI video payload IDs (VPID) used for helping to determine the wire format
  806. NTV2VideoFormat new_vf = want_props.videoFormat;
  807. NTV2PixelFormat new_pf = want_props.pixelFormat;
  808. if (!ajaSource->ReadWireFormats(want_props.deviceID, want_props.ioSelect, new_vf, new_pf, want_props.vpids)) {
  809. blog(LOG_ERROR, "aja_source_update: ReadWireFormats failed!");
  810. cardEntry->ReleaseInputSelection(want_props.ioSelect, curr_props.deviceID, ajaSource->GetName());
  811. return;
  812. }
  813. // Set auto-detected formats
  814. if ((int32_t)vf_select == kAutoDetect)
  815. want_props.videoFormat = new_vf;
  816. if ((int32_t)pf_select == kAutoDetect)
  817. want_props.pixelFormat = new_pf;
  818. if (want_props.videoFormat == NTV2_FORMAT_UNKNOWN || want_props.pixelFormat == NTV2_FBF_INVALID) {
  819. blog(LOG_ERROR, "aja_source_update: Unknown video/pixel format(s): %s / %s",
  820. NTV2VideoFormatToString(want_props.videoFormat).c_str(),
  821. NTV2FrameBufferFormatToString(want_props.pixelFormat).c_str());
  822. cardEntry->ReleaseInputSelection(want_props.ioSelect, curr_props.deviceID, ajaSource->GetName());
  823. return;
  824. }
  825. // Change capture format and restart capture thread
  826. if (!initialized || want_props != ajaSource->GetSourceProps()) {
  827. ajaSource->ClearConnections();
  828. NTV2XptConnections xpt_cnx;
  829. aja::Routing::ConfigureSourceRoute(want_props, NTV2_MODE_CAPTURE, card, xpt_cnx);
  830. ajaSource->CacheConnections(xpt_cnx);
  831. ajaSource->Deactivate();
  832. initialized = true;
  833. }
  834. ajaSource->SetSourceProps(want_props);
  835. aja::Routing::StartSourceAudio(want_props, card);
  836. card->SetReference(NTV2_REFERENCE_FREERUN);
  837. ajaSource->Activate(true);
  838. }
  839. static obs_properties_t *aja_source_get_properties(void *data)
  840. {
  841. obs_properties_t *props = obs_properties_create();
  842. obs_property_t *device_list = obs_properties_add_list(props, kUIPropDevice.id,
  843. obs_module_text(kUIPropDevice.text), OBS_COMBO_TYPE_LIST,
  844. OBS_COMBO_FORMAT_STRING);
  845. populate_source_device_list(device_list);
  846. obs_property_t *io_select_list = obs_properties_add_list(
  847. props, kUIPropInput.id, obs_module_text(kUIPropInput.text), OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  848. obs_property_t *vid_fmt_list = obs_properties_add_list(props, kUIPropVideoFormatSelect.id,
  849. obs_module_text(kUIPropVideoFormatSelect.text),
  850. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  851. obs_properties_add_list(props, kUIPropPixelFormatSelect.id, obs_module_text(kUIPropPixelFormatSelect.text),
  852. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  853. obs_properties_add_list(props, kUIPropSDITransport.id, obs_module_text(kUIPropSDITransport.text),
  854. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  855. obs_properties_add_list(props, kUIPropSDITransport4K.id, obs_module_text(kUIPropSDITransport4K.text),
  856. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  857. obs_properties_add_list(props, kUIPropChannelFormat.id, obs_module_text(kUIPropChannelFormat.text),
  858. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  859. obs_property_t *swap = obs_properties_add_bool(props, kUIPropChannelSwap_FC_LFE.id,
  860. obs_module_text(kUIPropChannelSwap_FC_LFE.text));
  861. obs_property_set_long_description(swap, kUIPropChannelSwap_FC_LFE.tooltip);
  862. obs_properties_add_bool(props, kUIPropDeactivateWhenNotShowing.id,
  863. obs_module_text(kUIPropDeactivateWhenNotShowing.text));
  864. obs_properties_add_bool(props, kUIPropBuffering.id, obs_module_text(kUIPropBuffering.text));
  865. obs_properties_add_bool(props, kUIPropBuffering.id, obs_module_text(kUIPropBuffering.text));
  866. obs_property_set_modified_callback(vid_fmt_list, aja_video_format_changed);
  867. obs_property_set_modified_callback2(device_list, aja_source_device_changed, data);
  868. obs_property_set_modified_callback2(io_select_list, aja_io_selection_changed, data);
  869. return props;
  870. }
  871. void aja_source_get_defaults(obs_data_t *settings)
  872. {
  873. obs_data_set_default_int(settings, kUIPropInput.id, static_cast<long long>(IOSelection::Invalid));
  874. obs_data_set_default_int(settings, kUIPropVideoFormatSelect.id, static_cast<long long>(kAutoDetect));
  875. obs_data_set_default_int(settings, kUIPropPixelFormatSelect.id, static_cast<long long>(kAutoDetect));
  876. obs_data_set_default_int(settings, kUIPropSDITransport.id, static_cast<long long>(kAutoDetect));
  877. obs_data_set_default_int(settings, kUIPropSDITransport4K.id,
  878. static_cast<long long>(SDITransport4K::TwoSampleInterleave));
  879. obs_data_set_default_int(settings, kUIPropChannelFormat.id, kDefaultAudioCaptureChannels);
  880. obs_data_set_default_bool(settings, kUIPropChannelSwap_FC_LFE.id, false);
  881. obs_data_set_default_bool(settings, kUIPropDeactivateWhenNotShowing.id, false);
  882. }
  883. static void aja_source_get_defaults_v1(obs_data_t *settings)
  884. {
  885. aja_source_get_defaults(settings);
  886. obs_data_set_default_bool(settings, kUIPropBuffering.id, true);
  887. }
  888. void aja_source_save(void *data, obs_data_t *settings)
  889. {
  890. AJASource *ajaSource = (AJASource *)data;
  891. if (!ajaSource) {
  892. blog(LOG_ERROR, "aja_source_save: AJA Source instance is null!");
  893. return;
  894. }
  895. const char *cardID = obs_data_get_string(settings, kUIPropDevice.id);
  896. if (!cardID || !cardID[0])
  897. return;
  898. auto &cardManager = aja::CardManager::Instance();
  899. auto cardEntry = cardManager.GetCardEntry(cardID);
  900. if (!cardEntry) {
  901. blog(LOG_DEBUG, "aja_source_save: Card Entry not found for %s", cardID);
  902. return;
  903. }
  904. auto oldName = ajaSource->GetName();
  905. auto newName = obs_source_get_name(ajaSource->GetOBSSource());
  906. if (oldName != newName && cardEntry->UpdateChannelOwnerName(oldName, newName)) {
  907. ajaSource->SetName(newName);
  908. blog(LOG_DEBUG, "aja_source_save: Renamed \"%s\" to \"%s\"", oldName.c_str(), newName);
  909. }
  910. }
  911. void register_aja_source_info()
  912. {
  913. struct obs_source_info aja_source_info = {};
  914. aja_source_info.id = kUIPropCaptureModule.id;
  915. aja_source_info.type = OBS_SOURCE_TYPE_INPUT;
  916. aja_source_info.output_flags = OBS_SOURCE_ASYNC_VIDEO | OBS_SOURCE_AUDIO | OBS_SOURCE_DO_NOT_DUPLICATE |
  917. OBS_SOURCE_CAP_OBSOLETE;
  918. aja_source_info.get_name = aja_source_get_name;
  919. aja_source_info.create = aja_source_create;
  920. aja_source_info.destroy = aja_source_destroy;
  921. aja_source_info.update = aja_source_update;
  922. aja_source_info.show = aja_source_show;
  923. aja_source_info.hide = aja_source_hide;
  924. aja_source_info.activate = aja_source_activate;
  925. aja_source_info.deactivate = aja_source_deactivate;
  926. aja_source_info.get_properties = aja_source_get_properties;
  927. aja_source_info.get_defaults = aja_source_get_defaults_v1;
  928. aja_source_info.save = aja_source_save;
  929. aja_source_info.icon_type = OBS_ICON_TYPE_CAMERA;
  930. obs_register_source(&aja_source_info);
  931. aja_source_info.version = 2;
  932. aja_source_info.output_flags &= ~OBS_SOURCE_CAP_OBSOLETE;
  933. aja_source_info.get_defaults = aja_source_get_defaults;
  934. obs_register_source(&aja_source_info);
  935. }