aja-source.cpp 38 KB

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