aja-source.cpp 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  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. AudioRepacker *audioRepacker =
  245. new AudioRepacker(ConvertRepackFormat(sourceProps.SpeakerLayout(), sourceProps.swapFrontCenterLFE),
  246. sourceProps.audioSampleSize * 8);
  247. while (ajaSource->IsCapturing()) {
  248. if (card->GetModelName() == "(Not Found)") {
  249. os_sleep_ms(250);
  250. obs_source_update(ajaSource->mSource, nullptr);
  251. break;
  252. }
  253. auto videoFormat = sourceProps.videoFormat;
  254. auto pixelFormat = sourceProps.pixelFormat;
  255. auto ioSelection = sourceProps.ioSelect;
  256. card->WaitForInputFieldID(NTV2_FIELD0, channel);
  257. currentCardFrame ^= 1;
  258. // Card format detection -- restarts capture thread via aja_source_update callback
  259. auto newVideoFormat = card->GetInputVideoFormat(inputSource, aja::Is3GLevelB(card, channel));
  260. if (newVideoFormat == NTV2_FORMAT_UNKNOWN) {
  261. blog(LOG_DEBUG, "AJASource::CaptureThread: Video format unknown!");
  262. ajaSource->GenerateTestPattern(videoFormat, pixelFormat, NTV2_TestPatt_Black);
  263. os_sleep_ms(250);
  264. continue;
  265. }
  266. newVideoFormat = aja::HandleSpecialCaseFormats(ioSelection, newVideoFormat, sourceProps.deviceID);
  267. if (sourceProps.autoDetect && (videoFormat != newVideoFormat)) {
  268. blog(LOG_INFO,
  269. "AJASource::CaptureThread: New Video Format detected! Triggering 'aja_source_update' callback and returning...");
  270. blog(LOG_INFO, "AJASource::CaptureThread: Current Video Format: %s, | Want Video Format: %s",
  271. NTV2VideoFormatToString(videoFormat, true).c_str(),
  272. NTV2VideoFormatToString(newVideoFormat, true).c_str());
  273. os_sleep_ms(250);
  274. obs_source_update(ajaSource->mSource, nullptr);
  275. break;
  276. }
  277. if (!TransferAudio(card, audioSystem, ajaSource->mAudioBuffer, offsets)) {
  278. ResetAudioBufferOffsets(card, audioSystem, offsets);
  279. } else {
  280. offsets.lastAddress = offsets.currentAddress;
  281. uint32_t sampleCount =
  282. offsets.bytesRead / (kDefaultAudioChannels * sourceProps.audioSampleSize);
  283. obs_source_audio audioPacket;
  284. audioPacket.samples_per_sec = sourceProps.audioSampleRate;
  285. audioPacket.format = sourceProps.AudioFormat();
  286. audioPacket.speakers = sourceProps.SpeakerLayout();
  287. audioPacket.frames = sampleCount;
  288. audioPacket.timestamp = get_sample_time(audioPacket.frames, sourceProps.audioSampleRate);
  289. uint8_t *hostAudioBuffer = (uint8_t *)ajaSource->mAudioBuffer.GetHostPointer();
  290. if (sourceProps.audioNumChannels >= 2 && sourceProps.audioNumChannels <= 6) {
  291. /* Repack 8ch audio to fit specified OBS speaker layout */
  292. audioRepacker->repack(hostAudioBuffer, sampleCount);
  293. audioPacket.data[0] = (*audioRepacker)->packet_buffer;
  294. } else {
  295. /* Silence, or pass-through 8ch of audio */
  296. if (sourceProps.audioNumChannels == 0)
  297. memset(hostAudioBuffer, 0, offsets.bytesRead);
  298. audioPacket.data[0] = hostAudioBuffer;
  299. }
  300. obs_source_output_audio(ajaSource->mSource, &audioPacket);
  301. }
  302. if (ajaSource->mVideoBuffer.GetByteCount() == 0) {
  303. blog(LOG_DEBUG, "AJASource::CaptureThread: 0 bytes in video buffer! Something went wrong!");
  304. continue;
  305. }
  306. card->DMAReadFrame(currentCardFrame, ajaSource->mVideoBuffer, ajaSource->mVideoBuffer.GetByteCount());
  307. auto actualVideoFormat = videoFormat;
  308. if (aja::Is3GLevelB(card, channel))
  309. actualVideoFormat = aja::GetLevelAFormatForLevelBFormat(videoFormat);
  310. const enum video_format obs_vid_fmt = aja::AJAPixelFormatToOBSVideoFormat(sourceProps.pixelFormat);
  311. NTV2FormatDesc fd(actualVideoFormat, pixelFormat);
  312. struct obs_source_frame2 obsFrame;
  313. obsFrame.flip = false;
  314. obsFrame.timestamp = os_gettime_ns();
  315. obsFrame.width = fd.GetRasterWidth();
  316. obsFrame.height = fd.GetRasterHeight();
  317. obsFrame.format = obs_vid_fmt;
  318. obsFrame.data[0] = reinterpret_cast<uint8_t *>((ULWord *)ajaSource->mVideoBuffer.GetHostPointer());
  319. obsFrame.linesize[0] = fd.GetBytesPerRow();
  320. video_colorspace colorspace = VIDEO_CS_709;
  321. if (NTV2_IS_SD_VIDEO_FORMAT(actualVideoFormat))
  322. colorspace = VIDEO_CS_601;
  323. video_format_get_parameters_for_format(colorspace, VIDEO_RANGE_PARTIAL, obs_vid_fmt,
  324. obsFrame.color_matrix, obsFrame.color_range_min,
  325. obsFrame.color_range_max);
  326. obs_source_output_video2(ajaSource->mSource, &obsFrame);
  327. card->SetInputFrame(framestore, currentCardFrame);
  328. }
  329. blog(LOG_INFO, "AJASource::Capturethread: Thread loop stopped");
  330. if (audioRepacker) {
  331. delete audioRepacker;
  332. audioRepacker = nullptr;
  333. }
  334. ajaSource->GenerateTestPattern(sourceProps.videoFormat, sourceProps.pixelFormat, NTV2_TestPatt_Black);
  335. }
  336. void AJASource::Deactivate()
  337. {
  338. SetCapturing(false);
  339. if (mCaptureThread) {
  340. if (mCaptureThread->Active()) {
  341. mCaptureThread->Stop();
  342. blog(LOG_INFO, "AJASource::CaptureThread: Stopped!");
  343. }
  344. delete mCaptureThread;
  345. mCaptureThread = nullptr;
  346. blog(LOG_INFO, "AJASource::CaptureThread: Destroyed!");
  347. }
  348. }
  349. void AJASource::Activate(bool enable)
  350. {
  351. if (mCaptureThread == nullptr) {
  352. mCaptureThread = new AJAThread();
  353. mCaptureThread->Attach(AJASource::CaptureThread, this);
  354. mCaptureThread->SetPriority(AJA_ThreadPriority_High);
  355. blog(LOG_INFO, "AJASource::CaptureThread: Created!");
  356. }
  357. if (enable) {
  358. SetCapturing(true);
  359. if (!mCaptureThread->Active()) {
  360. mCaptureThread->Start();
  361. blog(LOG_INFO, "AJASource::CaptureThread: Started!");
  362. }
  363. }
  364. }
  365. bool AJASource::IsCapturing() const
  366. {
  367. return mIsCapturing;
  368. }
  369. void AJASource::SetCapturing(bool capturing)
  370. {
  371. std::lock_guard<std::mutex> lock(mMutex);
  372. mIsCapturing = capturing;
  373. }
  374. //
  375. // CardEntry/Device stuff
  376. //
  377. std::string AJASource::CardID() const
  378. {
  379. return mCardID;
  380. }
  381. void AJASource::SetCardID(const std::string &cardID)
  382. {
  383. mCardID = cardID;
  384. }
  385. uint32_t AJASource::DeviceIndex() const
  386. {
  387. return static_cast<uint32_t>(mDeviceIndex);
  388. }
  389. void AJASource::SetDeviceIndex(uint32_t index)
  390. {
  391. mDeviceIndex = static_cast<UWord>(index);
  392. }
  393. //
  394. // AJASource Properties stuff
  395. //
  396. void AJASource::SetSourceProps(const SourceProps &props)
  397. {
  398. mSourceProps = props;
  399. }
  400. SourceProps AJASource::GetSourceProps() const
  401. {
  402. return mSourceProps;
  403. }
  404. void AJASource::CacheConnections(const NTV2XptConnections &cnx)
  405. {
  406. mCrosspoints.clear();
  407. mCrosspoints = cnx;
  408. }
  409. void AJASource::ClearConnections()
  410. {
  411. for (auto &&xpt : mCrosspoints) {
  412. mCard->Connect(xpt.first, NTV2_XptBlack);
  413. }
  414. mCrosspoints.clear();
  415. }
  416. bool AJASource::ReadChannelVPIDs(NTV2Channel channel, VPIDData &vpids)
  417. {
  418. ULWord vpid_a = 0;
  419. ULWord vpid_b = 0;
  420. bool read_ok = mCard->ReadSDIInVPID(channel, vpid_a, vpid_b);
  421. vpids.SetA(vpid_a);
  422. vpids.SetB(vpid_b);
  423. vpids.Parse();
  424. return read_ok;
  425. }
  426. bool AJASource::ReadWireFormats(NTV2DeviceID device_id, IOSelection io_select, NTV2VideoFormat &vf, NTV2PixelFormat &pf,
  427. VPIDDataList &vpids)
  428. {
  429. NTV2InputSourceSet input_srcs;
  430. aja::IOSelectionToInputSources(io_select, input_srcs);
  431. if (input_srcs.empty()) {
  432. blog(LOG_INFO, "AJASource::ReadWireFormats: No NTV2InputSources found for IOSelection %s",
  433. aja::IOSelectionToString(io_select).c_str());
  434. return false;
  435. }
  436. NTV2InputSource initial_src = *input_srcs.begin();
  437. for (auto &&src : input_srcs) {
  438. auto channel = NTV2InputSourceToChannel(src);
  439. mCard->EnableChannel(channel);
  440. if (NTV2_INPUT_SOURCE_IS_SDI(src)) {
  441. if (NTV2DeviceHasBiDirectionalSDI(device_id)) {
  442. mCard->SetSDITransmitEnable(channel, false);
  443. }
  444. mCard->WaitForInputVerticalInterrupt(channel);
  445. VPIDData vpid_data;
  446. if (ReadChannelVPIDs(channel, vpid_data))
  447. vpids.push_back(vpid_data);
  448. } else if (NTV2_INPUT_SOURCE_IS_HDMI(src)) {
  449. mCard->WaitForInputVerticalInterrupt(channel);
  450. ULWord hdmi_version = NTV2DeviceGetHDMIVersion(device_id);
  451. // HDMIv1 handles its own RGB->YCbCr color space conversion
  452. if (hdmi_version == 1) {
  453. pf = kDefaultAJAPixelFormat;
  454. } else {
  455. NTV2LHIHDMIColorSpace hdmiInputColor;
  456. mCard->GetHDMIInputColor(hdmiInputColor, channel);
  457. if (hdmiInputColor == NTV2_LHIHDMIColorSpaceYCbCr) {
  458. pf = kDefaultAJAPixelFormat;
  459. } else if (hdmiInputColor == NTV2_LHIHDMIColorSpaceRGB) {
  460. pf = NTV2_FBF_24BIT_BGR;
  461. }
  462. }
  463. }
  464. }
  465. NTV2Channel initial_channel = NTV2InputSourceToChannel(initial_src);
  466. mCard->WaitForInputVerticalInterrupt(initial_channel);
  467. vf = mCard->GetInputVideoFormat(initial_src, aja::Is3GLevelB(mCard, initial_channel));
  468. if (NTV2_INPUT_SOURCE_IS_SDI(initial_src)) {
  469. if (vpids.size() > 0) {
  470. auto vpid = *vpids.begin();
  471. if (vpid.Sampling() == VPIDSampling_YUV_422) {
  472. if (vpid.BitDepth() == VPIDBitDepth_8)
  473. pf = NTV2_FBF_8BIT_YCBCR;
  474. else if (vpid.BitDepth() == VPIDBitDepth_10)
  475. pf = NTV2_FBF_10BIT_YCBCR;
  476. blog(LOG_INFO, "AJASource::ReadWireFormats - Detected pixel format %s",
  477. NTV2FrameBufferFormatToString(pf, true).c_str());
  478. } else if (vpid.Sampling() == VPIDSampling_GBR_444) {
  479. pf = NTV2_FBF_24BIT_BGR;
  480. blog(LOG_INFO, "AJASource::ReadWireFormats - Detected pixel format %s",
  481. NTV2FrameBufferFormatToString(pf, true).c_str());
  482. }
  483. }
  484. }
  485. vf = aja::HandleSpecialCaseFormats(io_select, vf, device_id);
  486. blog(LOG_INFO, "AJASource::ReadWireFormats - Detected video format %s", NTV2VideoFormatToString(vf).c_str());
  487. return true;
  488. }
  489. void AJASource::ResetVideoBuffer(NTV2VideoFormat vf, NTV2PixelFormat pf)
  490. {
  491. if (vf != NTV2_FORMAT_UNKNOWN) {
  492. auto videoBufferSize = GetVideoWriteSize(vf, pf);
  493. if (mVideoBuffer)
  494. mVideoBuffer.Deallocate();
  495. mVideoBuffer.Allocate(videoBufferSize, true);
  496. blog(LOG_INFO, "AJASource::ResetVideoBuffer: Video Format: %s | Pixel Format: %s | Buffer Size: %d",
  497. NTV2VideoFormatToString(vf, false).c_str(), NTV2FrameBufferFormatToString(pf, true).c_str(),
  498. videoBufferSize);
  499. }
  500. }
  501. void AJASource::ResetAudioBuffer(size_t size)
  502. {
  503. if (mAudioBuffer)
  504. mAudioBuffer.Deallocate();
  505. mAudioBuffer.Allocate(size, true);
  506. }
  507. static const char *aja_source_get_name(void *);
  508. static void *aja_source_create(obs_data_t *, obs_source_t *);
  509. static void aja_source_destroy(void *);
  510. static void aja_source_activate(void *);
  511. static void aja_source_deactivate(void *);
  512. static void aja_source_update(void *, obs_data_t *);
  513. const char *aja_source_get_name(void *unused)
  514. {
  515. UNUSED_PARAMETER(unused);
  516. return obs_module_text(kUIPropCaptureModule.text);
  517. }
  518. bool aja_source_device_changed(void *data, obs_properties_t *props, obs_property_t *list, obs_data_t *settings)
  519. {
  520. UNUSED_PARAMETER(list);
  521. blog(LOG_DEBUG, "AJA Source Device Changed");
  522. auto *ajaSource = (AJASource *)data;
  523. if (!ajaSource) {
  524. blog(LOG_DEBUG, "aja_source_device_changed: AJA Source instance is null!");
  525. return false;
  526. }
  527. const char *cardID = obs_data_get_string(settings, kUIPropDevice.id);
  528. if (!cardID || !cardID[0])
  529. return false;
  530. auto &cardManager = aja::CardManager::Instance();
  531. auto cardEntry = cardManager.GetCardEntry(cardID);
  532. if (!cardEntry) {
  533. blog(LOG_DEBUG, "aja_source_device_changed: Card Entry not found for %s", cardID);
  534. return false;
  535. }
  536. blog(LOG_DEBUG, "Found CardEntry for %s", cardID);
  537. CNTV2Card *card = cardEntry->GetCard();
  538. if (!card) {
  539. blog(LOG_DEBUG, "aja_source_device_changed: Card instance is null!");
  540. return false;
  541. }
  542. const NTV2DeviceID deviceID = card->GetDeviceID();
  543. /* If Channel 1 is actively in use, filter the video format list to only
  544. * show video formats within the same framerate family. If Channel 1 is
  545. * not active we just go ahead and try to set all framestores to the same video format.
  546. * This is because Channel 1's clock rate will govern the card's Free Run clock.
  547. */
  548. NTV2VideoFormat videoFormatChannel1 = NTV2_FORMAT_UNKNOWN;
  549. if (!cardEntry->ChannelReady(NTV2_CHANNEL1, ajaSource->GetName())) {
  550. card->GetVideoFormat(videoFormatChannel1, NTV2_CHANNEL1);
  551. }
  552. obs_property_t *devices_list = obs_properties_get(props, kUIPropDevice.id);
  553. obs_property_t *io_select_list = obs_properties_get(props, kUIPropInput.id);
  554. obs_property_t *vid_fmt_list = obs_properties_get(props, kUIPropVideoFormatSelect.id);
  555. obs_property_t *pix_fmt_list = obs_properties_get(props, kUIPropPixelFormatSelect.id);
  556. obs_property_t *sdi_trx_list = obs_properties_get(props, kUIPropSDITransport.id);
  557. obs_property_t *sdi_4k_list = obs_properties_get(props, kUIPropSDITransport4K.id);
  558. obs_property_t *channel_format_list = obs_properties_get(props, kUIPropChannelFormat.id);
  559. obs_property_list_clear(vid_fmt_list);
  560. obs_property_list_add_int(vid_fmt_list, obs_module_text("Auto"), kAutoDetect);
  561. populate_video_format_list(deviceID, vid_fmt_list, videoFormatChannel1, true);
  562. obs_property_list_clear(pix_fmt_list);
  563. obs_property_list_add_int(pix_fmt_list, obs_module_text("Auto"), kAutoDetect);
  564. populate_pixel_format_list(deviceID, {kDefaultAJAPixelFormat, NTV2_FBF_10BIT_YCBCR, NTV2_FBF_24BIT_BGR},
  565. pix_fmt_list);
  566. IOSelection io_select = static_cast<IOSelection>(obs_data_get_int(settings, kUIPropInput.id));
  567. obs_property_list_clear(sdi_trx_list);
  568. populate_sdi_transport_list(sdi_trx_list, deviceID, true);
  569. obs_property_list_clear(sdi_4k_list);
  570. populate_sdi_4k_transport_list(sdi_4k_list);
  571. obs_property_list_clear(channel_format_list);
  572. obs_property_list_add_int(channel_format_list, TEXT_CHANNEL_FORMAT_NONE, 0);
  573. obs_property_list_add_int(channel_format_list, TEXT_CHANNEL_FORMAT_2_0CH, 2);
  574. obs_property_list_add_int(channel_format_list, TEXT_CHANNEL_FORMAT_2_1CH, 3);
  575. obs_property_list_add_int(channel_format_list, TEXT_CHANNEL_FORMAT_4_0CH, 4);
  576. obs_property_list_add_int(channel_format_list, TEXT_CHANNEL_FORMAT_4_1CH, 5);
  577. obs_property_list_add_int(channel_format_list, TEXT_CHANNEL_FORMAT_5_1CH, 6);
  578. obs_property_list_add_int(channel_format_list, TEXT_CHANNEL_FORMAT_7_1CH, 8);
  579. populate_io_selection_input_list(cardID, ajaSource->GetName(), deviceID, io_select_list);
  580. auto curr_vf = static_cast<NTV2VideoFormat>(obs_data_get_int(settings, kUIPropVideoFormatSelect.id));
  581. bool have_cards = cardManager.NumCardEntries() > 0;
  582. obs_property_set_visible(devices_list, have_cards);
  583. obs_property_set_visible(io_select_list, have_cards);
  584. obs_property_set_visible(vid_fmt_list, have_cards);
  585. obs_property_set_visible(pix_fmt_list, have_cards);
  586. obs_property_set_visible(sdi_trx_list, have_cards && aja::IsIOSelectionSDI(io_select));
  587. obs_property_set_visible(sdi_4k_list, have_cards && NTV2_IS_4K_VIDEO_FORMAT(curr_vf));
  588. return true;
  589. }
  590. bool aja_io_selection_changed(void *data, obs_properties_t *props, obs_property_t *list, obs_data_t *settings)
  591. {
  592. UNUSED_PARAMETER(list);
  593. AJASource *ajaSource = (AJASource *)data;
  594. if (!ajaSource) {
  595. blog(LOG_DEBUG, "aja_io_selection_changed: AJA Source instance is null!");
  596. return false;
  597. }
  598. const char *cardID = obs_data_get_string(settings, kUIPropDevice.id);
  599. if (!cardID || !cardID[0])
  600. return false;
  601. auto &cardManager = aja::CardManager::Instance();
  602. auto cardEntry = cardManager.GetCardEntry(cardID);
  603. if (!cardEntry) {
  604. blog(LOG_DEBUG, "aja_io_selection_changed: Card Entry not found for %s", cardID);
  605. return false;
  606. }
  607. filter_io_selection_input_list(cardID, ajaSource->GetName(), obs_properties_get(props, kUIPropInput.id));
  608. obs_property_set_visible(
  609. obs_properties_get(props, kUIPropSDITransport.id),
  610. aja::IsIOSelectionSDI(static_cast<IOSelection>(obs_data_get_int(settings, kUIPropInput.id))));
  611. return true;
  612. }
  613. bool aja_sdi_mode_list_changed(obs_properties_t *props, obs_property_t *list, obs_data_t *settings)
  614. {
  615. UNUSED_PARAMETER(props);
  616. UNUSED_PARAMETER(list);
  617. UNUSED_PARAMETER(settings);
  618. return true;
  619. }
  620. void *aja_source_create(obs_data_t *settings, obs_source_t *source)
  621. {
  622. blog(LOG_DEBUG, "AJA Source Create");
  623. auto ajaSource = new AJASource(source);
  624. ajaSource->SetName(obs_source_get_name(source));
  625. obs_source_set_async_decoupled(source, true);
  626. ajaSource->SetOBSSource(source);
  627. ajaSource->ResetAudioBuffer(NTV2_AUDIOSIZE_MAX);
  628. ajaSource->Activate(false);
  629. obs_source_update(source, settings);
  630. return ajaSource;
  631. }
  632. void aja_source_destroy(void *data)
  633. {
  634. blog(LOG_DEBUG, "AJA Source Destroy");
  635. auto ajaSource = (AJASource *)data;
  636. if (!ajaSource) {
  637. blog(LOG_ERROR, "aja_source_destroy: Plugin instance is null!");
  638. return;
  639. }
  640. ajaSource->Deactivate();
  641. NTV2DeviceID deviceID = DEVICE_ID_NOTFOUND;
  642. CNTV2Card *card = ajaSource->GetCard();
  643. if (card) {
  644. deviceID = card->GetDeviceID();
  645. aja::Routing::StopSourceAudio(ajaSource->GetSourceProps(), card);
  646. }
  647. ajaSource->mVideoBuffer.Deallocate();
  648. ajaSource->mAudioBuffer.Deallocate();
  649. ajaSource->mVideoBuffer = 0;
  650. ajaSource->mAudioBuffer = 0;
  651. auto &cardManager = aja::CardManager::Instance();
  652. const auto &cardID = ajaSource->CardID();
  653. auto cardEntry = cardManager.GetCardEntry(cardID);
  654. if (!cardEntry) {
  655. blog(LOG_DEBUG, "aja_source_destroy: Card Entry not found for %s", cardID.c_str());
  656. return;
  657. }
  658. auto ioSelect = ajaSource->GetSourceProps().ioSelect;
  659. if (!cardEntry->ReleaseInputSelection(ioSelect, deviceID, ajaSource->GetName())) {
  660. blog(LOG_WARNING, "aja_source_destroy: Error releasing Input Selection!");
  661. }
  662. delete ajaSource;
  663. ajaSource = nullptr;
  664. }
  665. static void aja_source_show(void *data)
  666. {
  667. auto ajaSource = (AJASource *)data;
  668. if (!ajaSource) {
  669. blog(LOG_ERROR, "aja_source_show: AJA Source instance is null!");
  670. return;
  671. }
  672. bool deactivateWhileNotShowing = ajaSource->GetSourceProps().deactivateWhileNotShowing;
  673. bool showing = obs_source_showing(ajaSource->GetOBSSource());
  674. blog(LOG_DEBUG, "aja_source_show: deactivateWhileNotShowing = %s, showing = %s",
  675. deactivateWhileNotShowing ? "true" : "false", showing ? "true" : "false");
  676. if (deactivateWhileNotShowing && showing && !ajaSource->IsCapturing()) {
  677. ajaSource->Activate(true);
  678. blog(LOG_DEBUG, "aja_source_show: activated capture thread!");
  679. }
  680. }
  681. static void aja_source_hide(void *data)
  682. {
  683. auto ajaSource = (AJASource *)data;
  684. if (!ajaSource)
  685. return;
  686. bool deactivateWhileNotShowing = ajaSource->GetSourceProps().deactivateWhileNotShowing;
  687. bool showing = obs_source_showing(ajaSource->GetOBSSource());
  688. blog(LOG_DEBUG, "aja_source_hide: deactivateWhileNotShowing = %s, showing = %s",
  689. deactivateWhileNotShowing ? "true" : "false", showing ? "true" : "false");
  690. if (deactivateWhileNotShowing && !showing && ajaSource->IsCapturing()) {
  691. ajaSource->Deactivate();
  692. blog(LOG_DEBUG, "aja_source_hide: deactivated capture thread!");
  693. }
  694. }
  695. static void aja_source_activate(void *data)
  696. {
  697. UNUSED_PARAMETER(data);
  698. }
  699. static void aja_source_deactivate(void *data)
  700. {
  701. UNUSED_PARAMETER(data);
  702. }
  703. static void aja_source_update(void *data, obs_data_t *settings)
  704. {
  705. static bool initialized = false;
  706. auto ajaSource = (AJASource *)data;
  707. if (!ajaSource) {
  708. blog(LOG_WARNING, "aja_source_update: Plugin instance is null!");
  709. return;
  710. }
  711. auto io_select = static_cast<IOSelection>(obs_data_get_int(settings, kUIPropInput.id));
  712. auto vf_select = static_cast<NTV2VideoFormat>(obs_data_get_int(settings, kUIPropVideoFormatSelect.id));
  713. auto pf_select = static_cast<NTV2PixelFormat>(obs_data_get_int(settings, kUIPropPixelFormatSelect.id));
  714. auto sdi_trx_select = static_cast<SDITransport>(obs_data_get_int(settings, kUIPropSDITransport.id));
  715. auto sdi_t4k_select = static_cast<SDITransport4K>(obs_data_get_int(settings, kUIPropSDITransport4K.id));
  716. auto num_audio_channels = obs_data_get_int(settings, kUIPropChannelFormat.id);
  717. bool deactivateWhileNotShowing = obs_data_get_bool(settings, kUIPropDeactivateWhenNotShowing.id);
  718. bool swapFrontCenterLFE = obs_data_get_bool(settings, kUIPropChannelSwap_FC_LFE.id);
  719. const std::string &wantCardID = obs_data_get_string(settings, kUIPropDevice.id);
  720. obs_source_set_async_unbuffered(ajaSource->GetOBSSource(), !obs_data_get_bool(settings, kUIPropBuffering.id));
  721. const std::string &currentCardID = ajaSource->CardID();
  722. if (wantCardID != currentCardID) {
  723. initialized = false;
  724. ajaSource->Deactivate();
  725. }
  726. auto &cardManager = aja::CardManager::Instance();
  727. cardManager.EnumerateCards();
  728. auto cardEntry = cardManager.GetCardEntry(wantCardID);
  729. if (!cardEntry) {
  730. blog(LOG_DEBUG, "aja_source_update: Card Entry not found for %s", wantCardID.c_str());
  731. return;
  732. }
  733. CNTV2Card *card = cardEntry->GetCard();
  734. if (!card || !card->IsOpen()) {
  735. blog(LOG_ERROR, "aja_source_update: AJA device %s not open!", wantCardID.c_str());
  736. return;
  737. }
  738. if (card->GetModelName() == "(Not Found)") {
  739. blog(LOG_ERROR, "aja_source_update: AJA device %s disconnected?", wantCardID.c_str());
  740. return;
  741. }
  742. ajaSource->SetCard(cardEntry->GetCard());
  743. SourceProps curr_props = ajaSource->GetSourceProps();
  744. // Release Channels from previous card if card ID changes
  745. if (wantCardID != currentCardID) {
  746. auto prevCardEntry = cardManager.GetCardEntry(currentCardID);
  747. if (prevCardEntry) {
  748. const std::string &ioSelectStr = aja::IOSelectionToString(curr_props.ioSelect);
  749. if (!prevCardEntry->ReleaseInputSelection(curr_props.ioSelect, curr_props.deviceID,
  750. ajaSource->GetName())) {
  751. blog(LOG_WARNING, "aja_source_update: Error releasing IOSelection %s for card ID %s",
  752. ioSelectStr.c_str(), currentCardID.c_str());
  753. } else {
  754. blog(LOG_INFO, "aja_source_update: Released IOSelection %s for card ID %s",
  755. ioSelectStr.c_str(), currentCardID.c_str());
  756. ajaSource->SetCardID(wantCardID);
  757. io_select = IOSelection::Invalid;
  758. }
  759. }
  760. }
  761. if (io_select == IOSelection::Invalid) {
  762. blog(LOG_DEBUG, "aja_source_update: Invalid IOSelection");
  763. return;
  764. }
  765. SourceProps want_props;
  766. want_props.deviceID = card->GetDeviceID();
  767. want_props.ioSelect = io_select;
  768. want_props.videoFormat = ((int32_t)vf_select == kAutoDetect) ? NTV2_FORMAT_UNKNOWN
  769. : static_cast<NTV2VideoFormat>(vf_select);
  770. want_props.pixelFormat = ((int32_t)pf_select == kAutoDetect) ? NTV2_FBF_INVALID
  771. : static_cast<NTV2PixelFormat>(pf_select);
  772. want_props.sdiTransport = ((int32_t)sdi_trx_select == kAutoDetect) ? SDITransport::Unknown
  773. : static_cast<SDITransport>(sdi_trx_select);
  774. want_props.sdi4kTransport = sdi_t4k_select;
  775. want_props.audioNumChannels = (uint32_t)num_audio_channels;
  776. want_props.swapFrontCenterLFE = swapFrontCenterLFE;
  777. want_props.vpids.clear();
  778. want_props.deactivateWhileNotShowing = deactivateWhileNotShowing;
  779. if (aja::IsIOSelectionSDI(io_select)) {
  780. want_props.autoDetect = (int)sdi_trx_select == kAutoDetect;
  781. } else {
  782. want_props.autoDetect = ((int)vf_select == kAutoDetect || (int)pf_select == kAutoDetect);
  783. }
  784. ajaSource->SetCardID(wantCardID);
  785. ajaSource->SetDeviceIndex((UWord)cardEntry->GetCardIndex());
  786. // Release Channels if IOSelection changes
  787. if (want_props.ioSelect != curr_props.ioSelect) {
  788. const std::string &ioSelectStr = aja::IOSelectionToString(curr_props.ioSelect);
  789. if (!cardEntry->ReleaseInputSelection(curr_props.ioSelect, curr_props.deviceID, ajaSource->GetName())) {
  790. blog(LOG_WARNING, "aja_source_update: Error releasing IOSelection %s for card ID %s",
  791. ioSelectStr.c_str(), currentCardID.c_str());
  792. } else {
  793. blog(LOG_INFO, "aja_source_update: Released IOSelection %s for card ID %s", ioSelectStr.c_str(),
  794. currentCardID.c_str());
  795. }
  796. }
  797. // Acquire Channels for current IOSelection
  798. if (!cardEntry->AcquireInputSelection(want_props.ioSelect, want_props.deviceID, ajaSource->GetName())) {
  799. blog(LOG_ERROR, "aja_source_update: Could not acquire IOSelection %s",
  800. aja::IOSelectionToString(want_props.ioSelect).c_str());
  801. return;
  802. }
  803. // Read SDI video payload IDs (VPID) used for helping to determine the wire format
  804. NTV2VideoFormat new_vf = want_props.videoFormat;
  805. NTV2PixelFormat new_pf = want_props.pixelFormat;
  806. if (!ajaSource->ReadWireFormats(want_props.deviceID, want_props.ioSelect, new_vf, new_pf, want_props.vpids)) {
  807. blog(LOG_ERROR, "aja_source_update: ReadWireFormats failed!");
  808. cardEntry->ReleaseInputSelection(want_props.ioSelect, curr_props.deviceID, ajaSource->GetName());
  809. return;
  810. }
  811. // Set auto-detected formats
  812. if ((int32_t)vf_select == kAutoDetect)
  813. want_props.videoFormat = new_vf;
  814. if ((int32_t)pf_select == kAutoDetect)
  815. want_props.pixelFormat = new_pf;
  816. if (want_props.videoFormat == NTV2_FORMAT_UNKNOWN || want_props.pixelFormat == NTV2_FBF_INVALID) {
  817. blog(LOG_ERROR, "aja_source_update: Unknown video/pixel format(s): %s / %s",
  818. NTV2VideoFormatToString(want_props.videoFormat).c_str(),
  819. NTV2FrameBufferFormatToString(want_props.pixelFormat).c_str());
  820. cardEntry->ReleaseInputSelection(want_props.ioSelect, curr_props.deviceID, ajaSource->GetName());
  821. return;
  822. }
  823. // Change capture format and restart capture thread
  824. if (!initialized || want_props != ajaSource->GetSourceProps()) {
  825. ajaSource->ClearConnections();
  826. NTV2XptConnections xpt_cnx;
  827. aja::Routing::ConfigureSourceRoute(want_props, NTV2_MODE_CAPTURE, card, xpt_cnx);
  828. ajaSource->CacheConnections(xpt_cnx);
  829. ajaSource->Deactivate();
  830. initialized = true;
  831. }
  832. ajaSource->SetSourceProps(want_props);
  833. aja::Routing::StartSourceAudio(want_props, card);
  834. card->SetReference(NTV2_REFERENCE_FREERUN);
  835. ajaSource->Activate(true);
  836. }
  837. static obs_properties_t *aja_source_get_properties(void *data)
  838. {
  839. obs_properties_t *props = obs_properties_create();
  840. obs_property_t *device_list = obs_properties_add_list(props, kUIPropDevice.id,
  841. obs_module_text(kUIPropDevice.text), OBS_COMBO_TYPE_LIST,
  842. OBS_COMBO_FORMAT_STRING);
  843. populate_source_device_list(device_list);
  844. obs_property_t *io_select_list = obs_properties_add_list(
  845. props, kUIPropInput.id, obs_module_text(kUIPropInput.text), OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  846. obs_property_t *vid_fmt_list = obs_properties_add_list(props, kUIPropVideoFormatSelect.id,
  847. obs_module_text(kUIPropVideoFormatSelect.text),
  848. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  849. obs_properties_add_list(props, kUIPropPixelFormatSelect.id, obs_module_text(kUIPropPixelFormatSelect.text),
  850. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  851. obs_properties_add_list(props, kUIPropSDITransport.id, obs_module_text(kUIPropSDITransport.text),
  852. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  853. obs_properties_add_list(props, kUIPropSDITransport4K.id, obs_module_text(kUIPropSDITransport4K.text),
  854. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  855. obs_properties_add_list(props, kUIPropChannelFormat.id, obs_module_text(kUIPropChannelFormat.text),
  856. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  857. obs_property_t *swap = obs_properties_add_bool(props, kUIPropChannelSwap_FC_LFE.id,
  858. obs_module_text(kUIPropChannelSwap_FC_LFE.text));
  859. obs_property_set_long_description(swap, kUIPropChannelSwap_FC_LFE.tooltip);
  860. obs_properties_add_bool(props, kUIPropDeactivateWhenNotShowing.id,
  861. obs_module_text(kUIPropDeactivateWhenNotShowing.text));
  862. obs_properties_add_bool(props, kUIPropBuffering.id, obs_module_text(kUIPropBuffering.text));
  863. obs_properties_add_bool(props, kUIPropBuffering.id, obs_module_text(kUIPropBuffering.text));
  864. obs_property_set_modified_callback(vid_fmt_list, aja_video_format_changed);
  865. obs_property_set_modified_callback2(device_list, aja_source_device_changed, data);
  866. obs_property_set_modified_callback2(io_select_list, aja_io_selection_changed, data);
  867. return props;
  868. }
  869. void aja_source_get_defaults(obs_data_t *settings)
  870. {
  871. obs_data_set_default_int(settings, kUIPropInput.id, static_cast<long long>(IOSelection::Invalid));
  872. obs_data_set_default_int(settings, kUIPropVideoFormatSelect.id, static_cast<long long>(kAutoDetect));
  873. obs_data_set_default_int(settings, kUIPropPixelFormatSelect.id, static_cast<long long>(kAutoDetect));
  874. obs_data_set_default_int(settings, kUIPropSDITransport.id, static_cast<long long>(kAutoDetect));
  875. obs_data_set_default_int(settings, kUIPropSDITransport4K.id,
  876. static_cast<long long>(SDITransport4K::TwoSampleInterleave));
  877. obs_data_set_default_int(settings, kUIPropChannelFormat.id, kDefaultAudioCaptureChannels);
  878. obs_data_set_default_bool(settings, kUIPropChannelSwap_FC_LFE.id, false);
  879. obs_data_set_default_bool(settings, kUIPropDeactivateWhenNotShowing.id, false);
  880. }
  881. static void aja_source_get_defaults_v1(obs_data_t *settings)
  882. {
  883. aja_source_get_defaults(settings);
  884. obs_data_set_default_bool(settings, kUIPropBuffering.id, true);
  885. }
  886. void aja_source_save(void *data, obs_data_t *settings)
  887. {
  888. AJASource *ajaSource = (AJASource *)data;
  889. if (!ajaSource) {
  890. blog(LOG_ERROR, "aja_source_save: AJA Source instance is null!");
  891. return;
  892. }
  893. const char *cardID = obs_data_get_string(settings, kUIPropDevice.id);
  894. if (!cardID || !cardID[0])
  895. return;
  896. auto &cardManager = aja::CardManager::Instance();
  897. auto cardEntry = cardManager.GetCardEntry(cardID);
  898. if (!cardEntry) {
  899. blog(LOG_DEBUG, "aja_source_save: Card Entry not found for %s", cardID);
  900. return;
  901. }
  902. auto oldName = ajaSource->GetName();
  903. auto newName = obs_source_get_name(ajaSource->GetOBSSource());
  904. if (oldName != newName && cardEntry->UpdateChannelOwnerName(oldName, newName)) {
  905. ajaSource->SetName(newName);
  906. blog(LOG_DEBUG, "aja_source_save: Renamed \"%s\" to \"%s\"", oldName.c_str(), newName);
  907. }
  908. }
  909. void register_aja_source_info()
  910. {
  911. struct obs_source_info aja_source_info = {};
  912. aja_source_info.id = kUIPropCaptureModule.id;
  913. aja_source_info.type = OBS_SOURCE_TYPE_INPUT;
  914. aja_source_info.output_flags = OBS_SOURCE_ASYNC_VIDEO | OBS_SOURCE_AUDIO | OBS_SOURCE_DO_NOT_DUPLICATE |
  915. OBS_SOURCE_CAP_OBSOLETE;
  916. aja_source_info.get_name = aja_source_get_name;
  917. aja_source_info.create = aja_source_create;
  918. aja_source_info.destroy = aja_source_destroy;
  919. aja_source_info.update = aja_source_update;
  920. aja_source_info.show = aja_source_show;
  921. aja_source_info.hide = aja_source_hide;
  922. aja_source_info.activate = aja_source_activate;
  923. aja_source_info.deactivate = aja_source_deactivate;
  924. aja_source_info.get_properties = aja_source_get_properties;
  925. aja_source_info.get_defaults = aja_source_get_defaults_v1;
  926. aja_source_info.save = aja_source_save;
  927. aja_source_info.icon_type = OBS_ICON_TYPE_CAMERA;
  928. obs_register_source(&aja_source_info);
  929. aja_source_info.version = 2;
  930. aja_source_info.output_flags &= ~OBS_SOURCE_CAP_OBSOLETE;
  931. aja_source_info.get_defaults = aja_source_get_defaults;
  932. obs_register_source(&aja_source_info);
  933. }