aja-source.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287
  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. pf = NTV2_FBF_8BIT_YCBCR;
  541. blog(LOG_INFO,
  542. "AJASource::ReadWireFormats - Detected pixel format %s",
  543. NTV2FrameBufferFormatToString(pf, true)
  544. .c_str());
  545. } else if (vpid.Sampling() == VPIDSampling_GBR_444) {
  546. pf = NTV2_FBF_24BIT_BGR;
  547. blog(LOG_INFO,
  548. "AJASource::ReadWireFormats - Detected pixel format %s",
  549. NTV2FrameBufferFormatToString(pf, true)
  550. .c_str());
  551. }
  552. }
  553. }
  554. vf = aja::HandleSpecialCaseFormats(io_select, vf, device_id);
  555. blog(LOG_INFO, "AJASource::ReadWireFormats - Detected video format %s",
  556. NTV2VideoFormatToString(vf).c_str());
  557. return true;
  558. }
  559. void AJASource::ResetVideoBuffer(NTV2VideoFormat vf, NTV2PixelFormat pf)
  560. {
  561. if (vf != NTV2_FORMAT_UNKNOWN) {
  562. auto videoBufferSize = GetVideoWriteSize(vf, pf);
  563. if (mVideoBuffer)
  564. mVideoBuffer.Deallocate();
  565. mVideoBuffer.Allocate(videoBufferSize, true);
  566. blog(LOG_INFO,
  567. "AJASource::ResetVideoBuffer: Video Format: %s | Pixel Format: %s | Buffer Size: %d",
  568. NTV2VideoFormatToString(vf, false).c_str(),
  569. NTV2FrameBufferFormatToString(pf, true).c_str(),
  570. videoBufferSize);
  571. }
  572. }
  573. void AJASource::ResetAudioBuffer(size_t size)
  574. {
  575. if (mAudioBuffer)
  576. mAudioBuffer.Deallocate();
  577. mAudioBuffer.Allocate(size, true);
  578. }
  579. static const char *aja_source_get_name(void *);
  580. static void *aja_source_create(obs_data_t *, obs_source_t *);
  581. static void aja_source_destroy(void *);
  582. static void aja_source_activate(void *);
  583. static void aja_source_deactivate(void *);
  584. static void aja_source_update(void *, obs_data_t *);
  585. const char *aja_source_get_name(void *unused)
  586. {
  587. UNUSED_PARAMETER(unused);
  588. return obs_module_text(kUIPropCaptureModule.text);
  589. }
  590. bool aja_source_device_changed(void *data, obs_properties_t *props,
  591. obs_property_t *list, obs_data_t *settings)
  592. {
  593. UNUSED_PARAMETER(list);
  594. blog(LOG_DEBUG, "AJA Source Device Changed");
  595. auto *ajaSource = (AJASource *)data;
  596. if (!ajaSource) {
  597. blog(LOG_DEBUG,
  598. "aja_source_device_changed: AJA Source instance is null!");
  599. return false;
  600. }
  601. const char *cardID = obs_data_get_string(settings, kUIPropDevice.id);
  602. if (!cardID || !cardID[0])
  603. return false;
  604. auto &cardManager = aja::CardManager::Instance();
  605. auto cardEntry = cardManager.GetCardEntry(cardID);
  606. if (!cardEntry) {
  607. blog(LOG_DEBUG,
  608. "aja_source_device_changed: Card Entry not found for %s",
  609. cardID);
  610. return false;
  611. }
  612. blog(LOG_DEBUG, "Found CardEntry for %s", cardID);
  613. CNTV2Card *card = cardEntry->GetCard();
  614. if (!card) {
  615. blog(LOG_DEBUG,
  616. "aja_source_device_changed: Card instance is null!");
  617. return false;
  618. }
  619. const NTV2DeviceID deviceID = card->GetDeviceID();
  620. /* If Channel 1 is actively in use, filter the video format list to only
  621. * show video formats within the same framerate family. If Channel 1 is
  622. * not active we just go ahead and try to set all framestores to the same video format.
  623. * This is because Channel 1's clock rate will govern the card's Free Run clock.
  624. */
  625. NTV2VideoFormat videoFormatChannel1 = NTV2_FORMAT_UNKNOWN;
  626. if (!cardEntry->ChannelReady(NTV2_CHANNEL1, ajaSource->GetName())) {
  627. card->GetVideoFormat(videoFormatChannel1, NTV2_CHANNEL1);
  628. }
  629. obs_property_t *devices_list =
  630. obs_properties_get(props, kUIPropDevice.id);
  631. obs_property_t *io_select_list =
  632. obs_properties_get(props, kUIPropInput.id);
  633. obs_property_t *vid_fmt_list =
  634. obs_properties_get(props, kUIPropVideoFormatSelect.id);
  635. obs_property_t *pix_fmt_list =
  636. obs_properties_get(props, kUIPropPixelFormatSelect.id);
  637. obs_property_t *sdi_trx_list =
  638. obs_properties_get(props, kUIPropSDITransport.id);
  639. obs_property_t *sdi_4k_list =
  640. obs_properties_get(props, kUIPropSDITransport4K.id);
  641. obs_property_t *channel_format_list =
  642. obs_properties_get(props, kUIPropChannelFormat.id);
  643. obs_property_list_clear(vid_fmt_list);
  644. obs_property_list_add_int(vid_fmt_list, obs_module_text("Auto"),
  645. kAutoDetect);
  646. populate_video_format_list(deviceID, vid_fmt_list, videoFormatChannel1,
  647. true);
  648. obs_property_list_clear(pix_fmt_list);
  649. obs_property_list_add_int(pix_fmt_list, obs_module_text("Auto"),
  650. kAutoDetect);
  651. populate_pixel_format_list(deviceID, pix_fmt_list);
  652. IOSelection io_select = static_cast<IOSelection>(
  653. obs_data_get_int(settings, kUIPropInput.id));
  654. obs_property_list_clear(sdi_trx_list);
  655. populate_sdi_transport_list(sdi_trx_list, deviceID, true);
  656. obs_property_list_clear(sdi_4k_list);
  657. populate_sdi_4k_transport_list(sdi_4k_list);
  658. obs_property_list_clear(channel_format_list);
  659. obs_property_list_add_int(channel_format_list, TEXT_CHANNEL_FORMAT_NONE,
  660. 0);
  661. obs_property_list_add_int(channel_format_list,
  662. TEXT_CHANNEL_FORMAT_2_0CH, 2);
  663. obs_property_list_add_int(channel_format_list,
  664. TEXT_CHANNEL_FORMAT_2_1CH, 3);
  665. obs_property_list_add_int(channel_format_list,
  666. TEXT_CHANNEL_FORMAT_4_0CH, 4);
  667. obs_property_list_add_int(channel_format_list,
  668. TEXT_CHANNEL_FORMAT_4_1CH, 5);
  669. obs_property_list_add_int(channel_format_list,
  670. TEXT_CHANNEL_FORMAT_5_1CH, 6);
  671. obs_property_list_add_int(channel_format_list,
  672. TEXT_CHANNEL_FORMAT_7_1CH, 8);
  673. populate_io_selection_input_list(cardID, ajaSource->GetName(), deviceID,
  674. io_select_list);
  675. auto curr_vf = static_cast<NTV2VideoFormat>(
  676. obs_data_get_int(settings, kUIPropVideoFormatSelect.id));
  677. bool have_cards = cardManager.NumCardEntries() > 0;
  678. obs_property_set_visible(devices_list, have_cards);
  679. obs_property_set_visible(io_select_list, have_cards);
  680. obs_property_set_visible(vid_fmt_list, have_cards);
  681. obs_property_set_visible(pix_fmt_list, have_cards);
  682. obs_property_set_visible(
  683. sdi_trx_list, have_cards && aja::IsIOSelectionSDI(io_select));
  684. obs_property_set_visible(
  685. sdi_4k_list, have_cards && NTV2_IS_4K_VIDEO_FORMAT(curr_vf));
  686. return true;
  687. }
  688. bool aja_io_selection_changed(void *data, obs_properties_t *props,
  689. obs_property_t *list, obs_data_t *settings)
  690. {
  691. UNUSED_PARAMETER(list);
  692. AJASource *ajaSource = (AJASource *)data;
  693. if (!ajaSource) {
  694. blog(LOG_DEBUG,
  695. "aja_io_selection_changed: AJA Source instance is null!");
  696. return false;
  697. }
  698. const char *cardID = obs_data_get_string(settings, kUIPropDevice.id);
  699. if (!cardID || !cardID[0])
  700. return false;
  701. auto &cardManager = aja::CardManager::Instance();
  702. auto cardEntry = cardManager.GetCardEntry(cardID);
  703. if (!cardEntry) {
  704. blog(LOG_DEBUG,
  705. "aja_io_selection_changed: Card Entry not found for %s",
  706. cardID);
  707. return false;
  708. }
  709. filter_io_selection_input_list(cardID, ajaSource->GetName(),
  710. obs_properties_get(props,
  711. kUIPropInput.id));
  712. obs_property_set_visible(
  713. obs_properties_get(props, kUIPropSDITransport.id),
  714. aja::IsIOSelectionSDI(static_cast<IOSelection>(
  715. obs_data_get_int(settings, kUIPropInput.id))));
  716. return true;
  717. }
  718. bool aja_sdi_mode_list_changed(obs_properties_t *props, obs_property_t *list,
  719. obs_data_t *settings)
  720. {
  721. UNUSED_PARAMETER(props);
  722. UNUSED_PARAMETER(list);
  723. UNUSED_PARAMETER(settings);
  724. return true;
  725. }
  726. void *aja_source_create(obs_data_t *settings, obs_source_t *source)
  727. {
  728. blog(LOG_DEBUG, "AJA Source Create");
  729. auto ajaSource = new AJASource(source);
  730. ajaSource->SetName(obs_source_get_name(source));
  731. obs_source_set_async_decoupled(source, true);
  732. ajaSource->SetOBSSource(source);
  733. ajaSource->ResetAudioBuffer(NTV2_AUDIOSIZE_MAX);
  734. ajaSource->Activate(false);
  735. obs_source_update(source, settings);
  736. return ajaSource;
  737. }
  738. void aja_source_destroy(void *data)
  739. {
  740. blog(LOG_DEBUG, "AJA Source Destroy");
  741. auto ajaSource = (AJASource *)data;
  742. if (!ajaSource) {
  743. blog(LOG_ERROR, "aja_source_destroy: Plugin instance is null!");
  744. return;
  745. }
  746. ajaSource->Deactivate();
  747. NTV2DeviceID deviceID = DEVICE_ID_NOTFOUND;
  748. CNTV2Card *card = ajaSource->GetCard();
  749. if (card) {
  750. deviceID = card->GetDeviceID();
  751. aja::Routing::StopSourceAudio(ajaSource->GetSourceProps(),
  752. card);
  753. }
  754. ajaSource->mVideoBuffer.Deallocate();
  755. ajaSource->mAudioBuffer.Deallocate();
  756. ajaSource->mVideoBuffer = 0;
  757. ajaSource->mAudioBuffer = 0;
  758. auto &cardManager = aja::CardManager::Instance();
  759. const auto &cardID = ajaSource->CardID();
  760. auto cardEntry = cardManager.GetCardEntry(cardID);
  761. if (!cardEntry) {
  762. blog(LOG_DEBUG,
  763. "aja_source_destroy: Card Entry not found for %s",
  764. cardID.c_str());
  765. return;
  766. }
  767. auto ioSelect = ajaSource->GetSourceProps().ioSelect;
  768. if (!cardEntry->ReleaseInputSelection(ioSelect, deviceID,
  769. ajaSource->GetName())) {
  770. blog(LOG_WARNING,
  771. "aja_source_destroy: Error releasing Input Selection!");
  772. }
  773. delete ajaSource;
  774. ajaSource = nullptr;
  775. }
  776. static void aja_source_show(void *data)
  777. {
  778. auto ajaSource = (AJASource *)data;
  779. if (!ajaSource) {
  780. blog(LOG_ERROR,
  781. "aja_source_show: AJA Source instance is null!");
  782. return;
  783. }
  784. bool deactivateWhileNotShowing =
  785. ajaSource->GetSourceProps().deactivateWhileNotShowing;
  786. bool showing = obs_source_showing(ajaSource->GetOBSSource());
  787. blog(LOG_DEBUG,
  788. "aja_source_show: deactivateWhileNotShowing = %s, showing = %s",
  789. deactivateWhileNotShowing ? "true" : "false",
  790. showing ? "true" : "false");
  791. if (deactivateWhileNotShowing && showing && !ajaSource->IsCapturing()) {
  792. ajaSource->Activate(true);
  793. blog(LOG_DEBUG, "aja_source_show: activated capture thread!");
  794. }
  795. }
  796. static void aja_source_hide(void *data)
  797. {
  798. auto ajaSource = (AJASource *)data;
  799. if (!ajaSource)
  800. return;
  801. bool deactivateWhileNotShowing =
  802. ajaSource->GetSourceProps().deactivateWhileNotShowing;
  803. bool showing = obs_source_showing(ajaSource->GetOBSSource());
  804. blog(LOG_DEBUG,
  805. "aja_source_hide: deactivateWhileNotShowing = %s, showing = %s",
  806. deactivateWhileNotShowing ? "true" : "false",
  807. showing ? "true" : "false");
  808. if (deactivateWhileNotShowing && !showing && ajaSource->IsCapturing()) {
  809. ajaSource->Deactivate();
  810. blog(LOG_DEBUG, "aja_source_hide: deactivated capture thread!");
  811. }
  812. }
  813. static void aja_source_activate(void *data)
  814. {
  815. UNUSED_PARAMETER(data);
  816. }
  817. static void aja_source_deactivate(void *data)
  818. {
  819. UNUSED_PARAMETER(data);
  820. }
  821. static void aja_source_update(void *data, obs_data_t *settings)
  822. {
  823. static bool initialized = false;
  824. auto ajaSource = (AJASource *)data;
  825. if (!ajaSource) {
  826. blog(LOG_WARNING,
  827. "aja_source_update: Plugin instance is null!");
  828. return;
  829. }
  830. auto io_select = static_cast<IOSelection>(
  831. obs_data_get_int(settings, kUIPropInput.id));
  832. auto vf_select = static_cast<NTV2VideoFormat>(
  833. obs_data_get_int(settings, kUIPropVideoFormatSelect.id));
  834. auto pf_select = static_cast<NTV2PixelFormat>(
  835. obs_data_get_int(settings, kUIPropPixelFormatSelect.id));
  836. auto sdi_trx_select = static_cast<SDITransport>(
  837. obs_data_get_int(settings, kUIPropSDITransport.id));
  838. auto sdi_t4k_select = static_cast<SDITransport4K>(
  839. obs_data_get_int(settings, kUIPropSDITransport4K.id));
  840. auto num_audio_channels =
  841. obs_data_get_int(settings, kUIPropChannelFormat.id);
  842. bool deactivateWhileNotShowing =
  843. obs_data_get_bool(settings, kUIPropDeactivateWhenNotShowing.id);
  844. bool swapFrontCenterLFE =
  845. obs_data_get_bool(settings, kUIPropChannelSwap_FC_LFE.id);
  846. const std::string &wantCardID =
  847. obs_data_get_string(settings, kUIPropDevice.id);
  848. obs_source_set_async_unbuffered(
  849. ajaSource->GetOBSSource(),
  850. !obs_data_get_bool(settings, kUIPropBuffering.id));
  851. const std::string &currentCardID = ajaSource->CardID();
  852. if (wantCardID != currentCardID) {
  853. initialized = false;
  854. ajaSource->Deactivate();
  855. }
  856. auto &cardManager = aja::CardManager::Instance();
  857. cardManager.EnumerateCards();
  858. auto cardEntry = cardManager.GetCardEntry(wantCardID);
  859. if (!cardEntry) {
  860. blog(LOG_DEBUG,
  861. "aja_source_update: Card Entry not found for %s",
  862. wantCardID.c_str());
  863. return;
  864. }
  865. CNTV2Card *card = cardEntry->GetCard();
  866. if (!card || !card->IsOpen()) {
  867. blog(LOG_ERROR, "aja_source_update: AJA device %s not open!",
  868. wantCardID.c_str());
  869. return;
  870. }
  871. if (card->GetModelName() == "(Not Found)") {
  872. blog(LOG_ERROR,
  873. "aja_source_update: AJA device %s disconnected?",
  874. wantCardID.c_str());
  875. return;
  876. }
  877. ajaSource->SetCard(cardEntry->GetCard());
  878. SourceProps curr_props = ajaSource->GetSourceProps();
  879. // Release Channels from previous card if card ID changes
  880. if (wantCardID != currentCardID) {
  881. auto prevCardEntry = cardManager.GetCardEntry(currentCardID);
  882. if (prevCardEntry) {
  883. const std::string &ioSelectStr =
  884. aja::IOSelectionToString(curr_props.ioSelect);
  885. if (!prevCardEntry->ReleaseInputSelection(
  886. curr_props.ioSelect, curr_props.deviceID,
  887. ajaSource->GetName())) {
  888. blog(LOG_WARNING,
  889. "aja_source_update: Error releasing IOSelection %s for card ID %s",
  890. ioSelectStr.c_str(),
  891. currentCardID.c_str());
  892. } else {
  893. blog(LOG_INFO,
  894. "aja_source_update: Released IOSelection %s for card ID %s",
  895. ioSelectStr.c_str(),
  896. currentCardID.c_str());
  897. ajaSource->SetCardID(wantCardID);
  898. io_select = IOSelection::Invalid;
  899. }
  900. }
  901. }
  902. if (io_select == IOSelection::Invalid) {
  903. blog(LOG_DEBUG, "aja_source_update: Invalid IOSelection");
  904. return;
  905. }
  906. SourceProps want_props;
  907. want_props.deviceID = card->GetDeviceID();
  908. want_props.ioSelect = io_select;
  909. want_props.videoFormat =
  910. ((int32_t)vf_select == kAutoDetect)
  911. ? NTV2_FORMAT_UNKNOWN
  912. : static_cast<NTV2VideoFormat>(vf_select);
  913. want_props.pixelFormat =
  914. ((int32_t)pf_select == kAutoDetect)
  915. ? NTV2_FBF_INVALID
  916. : static_cast<NTV2PixelFormat>(pf_select);
  917. want_props.sdiTransport =
  918. ((int32_t)sdi_trx_select == kAutoDetect)
  919. ? SDITransport::Unknown
  920. : static_cast<SDITransport>(sdi_trx_select);
  921. want_props.sdi4kTransport = sdi_t4k_select;
  922. want_props.audioNumChannels = (uint32_t)num_audio_channels;
  923. want_props.swapFrontCenterLFE = swapFrontCenterLFE;
  924. want_props.vpids.clear();
  925. want_props.deactivateWhileNotShowing = deactivateWhileNotShowing;
  926. if (aja::IsIOSelectionSDI(io_select)) {
  927. want_props.autoDetect = (int)sdi_trx_select == kAutoDetect;
  928. } else {
  929. want_props.autoDetect = ((int)vf_select == kAutoDetect ||
  930. (int)pf_select == kAutoDetect);
  931. }
  932. ajaSource->SetCardID(wantCardID);
  933. ajaSource->SetDeviceIndex((UWord)cardEntry->GetCardIndex());
  934. // Release Channels if IOSelection changes
  935. if (want_props.ioSelect != curr_props.ioSelect) {
  936. const std::string &ioSelectStr =
  937. aja::IOSelectionToString(curr_props.ioSelect);
  938. if (!cardEntry->ReleaseInputSelection(curr_props.ioSelect,
  939. curr_props.deviceID,
  940. ajaSource->GetName())) {
  941. blog(LOG_WARNING,
  942. "aja_source_update: Error releasing IOSelection %s for card ID %s",
  943. ioSelectStr.c_str(), currentCardID.c_str());
  944. } else {
  945. blog(LOG_INFO,
  946. "aja_source_update: Released IOSelection %s for card ID %s",
  947. ioSelectStr.c_str(), currentCardID.c_str());
  948. }
  949. }
  950. // Acquire Channels for current IOSelection
  951. if (!cardEntry->AcquireInputSelection(want_props.ioSelect,
  952. want_props.deviceID,
  953. ajaSource->GetName())) {
  954. blog(LOG_ERROR,
  955. "aja_source_update: Could not acquire IOSelection %s",
  956. aja::IOSelectionToString(want_props.ioSelect).c_str());
  957. return;
  958. }
  959. // Read SDI video payload IDs (VPID) used for helping to determine the wire format
  960. NTV2VideoFormat new_vf = want_props.videoFormat;
  961. NTV2PixelFormat new_pf = want_props.pixelFormat;
  962. if (!ajaSource->ReadWireFormats(want_props.deviceID,
  963. want_props.ioSelect, new_vf, new_pf,
  964. want_props.vpids)) {
  965. blog(LOG_ERROR, "aja_source_update: ReadWireFormats failed!");
  966. cardEntry->ReleaseInputSelection(want_props.ioSelect,
  967. curr_props.deviceID,
  968. ajaSource->GetName());
  969. return;
  970. }
  971. // Set auto-detected formats
  972. if ((int32_t)vf_select == kAutoDetect)
  973. want_props.videoFormat = new_vf;
  974. if ((int32_t)pf_select == kAutoDetect)
  975. want_props.pixelFormat = new_pf;
  976. if (want_props.videoFormat == NTV2_FORMAT_UNKNOWN ||
  977. want_props.pixelFormat == NTV2_FBF_INVALID) {
  978. blog(LOG_ERROR,
  979. "aja_source_update: Unknown video/pixel format(s): %s / %s",
  980. NTV2VideoFormatToString(want_props.videoFormat).c_str(),
  981. NTV2FrameBufferFormatToString(want_props.pixelFormat)
  982. .c_str());
  983. cardEntry->ReleaseInputSelection(want_props.ioSelect,
  984. curr_props.deviceID,
  985. ajaSource->GetName());
  986. return;
  987. }
  988. // Change capture format and restart capture thread
  989. if (!initialized || want_props != ajaSource->GetSourceProps()) {
  990. ajaSource->ClearConnections();
  991. NTV2XptConnections xpt_cnx;
  992. aja::Routing::ConfigureSourceRoute(
  993. want_props, NTV2_MODE_CAPTURE, card, xpt_cnx);
  994. ajaSource->CacheConnections(xpt_cnx);
  995. ajaSource->Deactivate();
  996. initialized = true;
  997. }
  998. ajaSource->SetSourceProps(want_props);
  999. aja::Routing::StartSourceAudio(want_props, card);
  1000. card->SetReference(NTV2_REFERENCE_FREERUN);
  1001. ajaSource->Activate(true);
  1002. }
  1003. static obs_properties_t *aja_source_get_properties(void *data)
  1004. {
  1005. obs_properties_t *props = obs_properties_create();
  1006. obs_property_t *device_list = obs_properties_add_list(
  1007. props, kUIPropDevice.id, obs_module_text(kUIPropDevice.text),
  1008. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  1009. populate_source_device_list(device_list);
  1010. obs_property_t *io_select_list = obs_properties_add_list(
  1011. props, kUIPropInput.id, obs_module_text(kUIPropInput.text),
  1012. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  1013. obs_property_t *vid_fmt_list = obs_properties_add_list(
  1014. props, kUIPropVideoFormatSelect.id,
  1015. obs_module_text(kUIPropVideoFormatSelect.text),
  1016. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  1017. obs_properties_add_list(props, kUIPropPixelFormatSelect.id,
  1018. obs_module_text(kUIPropPixelFormatSelect.text),
  1019. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  1020. obs_properties_add_list(props, kUIPropSDITransport.id,
  1021. obs_module_text(kUIPropSDITransport.text),
  1022. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  1023. obs_properties_add_list(props, kUIPropSDITransport4K.id,
  1024. obs_module_text(kUIPropSDITransport4K.text),
  1025. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  1026. obs_properties_add_list(props, kUIPropChannelFormat.id,
  1027. obs_module_text(kUIPropChannelFormat.text),
  1028. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  1029. obs_property_t *swap = obs_properties_add_bool(
  1030. props, kUIPropChannelSwap_FC_LFE.id,
  1031. obs_module_text(kUIPropChannelSwap_FC_LFE.text));
  1032. obs_property_set_long_description(swap,
  1033. kUIPropChannelSwap_FC_LFE.tooltip);
  1034. obs_properties_add_bool(
  1035. props, kUIPropDeactivateWhenNotShowing.id,
  1036. obs_module_text(kUIPropDeactivateWhenNotShowing.text));
  1037. obs_properties_add_bool(props, kUIPropBuffering.id,
  1038. obs_module_text(kUIPropBuffering.text));
  1039. obs_properties_add_bool(props, kUIPropBuffering.id,
  1040. obs_module_text(kUIPropBuffering.text));
  1041. obs_property_set_modified_callback(vid_fmt_list,
  1042. aja_video_format_changed);
  1043. obs_property_set_modified_callback2(device_list,
  1044. aja_source_device_changed, data);
  1045. obs_property_set_modified_callback2(io_select_list,
  1046. aja_io_selection_changed, data);
  1047. return props;
  1048. }
  1049. void aja_source_get_defaults(obs_data_t *settings)
  1050. {
  1051. obs_data_set_default_int(settings, kUIPropInput.id,
  1052. static_cast<long long>(IOSelection::Invalid));
  1053. obs_data_set_default_int(settings, kUIPropVideoFormatSelect.id,
  1054. static_cast<long long>(kAutoDetect));
  1055. obs_data_set_default_int(settings, kUIPropPixelFormatSelect.id,
  1056. static_cast<long long>(kAutoDetect));
  1057. obs_data_set_default_int(settings, kUIPropSDITransport.id,
  1058. static_cast<long long>(kAutoDetect));
  1059. obs_data_set_default_int(
  1060. settings, kUIPropSDITransport4K.id,
  1061. static_cast<long long>(SDITransport4K::TwoSampleInterleave));
  1062. obs_data_set_default_int(settings, kUIPropChannelFormat.id,
  1063. kDefaultAudioCaptureChannels);
  1064. obs_data_set_default_bool(settings, kUIPropChannelSwap_FC_LFE.id,
  1065. false);
  1066. obs_data_set_default_bool(settings, kUIPropDeactivateWhenNotShowing.id,
  1067. false);
  1068. }
  1069. static void aja_source_get_defaults_v1(obs_data_t *settings)
  1070. {
  1071. aja_source_get_defaults(settings);
  1072. obs_data_set_default_bool(settings, kUIPropBuffering.id, true);
  1073. }
  1074. void aja_source_save(void *data, obs_data_t *settings)
  1075. {
  1076. AJASource *ajaSource = (AJASource *)data;
  1077. if (!ajaSource) {
  1078. blog(LOG_ERROR,
  1079. "aja_source_save: AJA Source instance is null!");
  1080. return;
  1081. }
  1082. const char *cardID = obs_data_get_string(settings, kUIPropDevice.id);
  1083. if (!cardID || !cardID[0])
  1084. return;
  1085. auto &cardManager = aja::CardManager::Instance();
  1086. auto cardEntry = cardManager.GetCardEntry(cardID);
  1087. if (!cardEntry) {
  1088. blog(LOG_DEBUG, "aja_source_save: Card Entry not found for %s",
  1089. cardID);
  1090. return;
  1091. }
  1092. auto oldName = ajaSource->GetName();
  1093. auto newName = obs_source_get_name(ajaSource->GetOBSSource());
  1094. if (oldName != newName &&
  1095. cardEntry->UpdateChannelOwnerName(oldName, newName)) {
  1096. ajaSource->SetName(newName);
  1097. blog(LOG_DEBUG, "aja_source_save: Renamed \"%s\" to \"%s\"",
  1098. oldName.c_str(), newName);
  1099. }
  1100. }
  1101. void register_aja_source_info()
  1102. {
  1103. struct obs_source_info aja_source_info = {};
  1104. aja_source_info.id = kUIPropCaptureModule.id;
  1105. aja_source_info.type = OBS_SOURCE_TYPE_INPUT;
  1106. aja_source_info.output_flags =
  1107. OBS_SOURCE_ASYNC_VIDEO | OBS_SOURCE_AUDIO |
  1108. OBS_SOURCE_DO_NOT_DUPLICATE | OBS_SOURCE_CAP_OBSOLETE;
  1109. aja_source_info.get_name = aja_source_get_name;
  1110. aja_source_info.create = aja_source_create;
  1111. aja_source_info.destroy = aja_source_destroy;
  1112. aja_source_info.update = aja_source_update;
  1113. aja_source_info.show = aja_source_show;
  1114. aja_source_info.hide = aja_source_hide;
  1115. aja_source_info.activate = aja_source_activate;
  1116. aja_source_info.deactivate = aja_source_deactivate;
  1117. aja_source_info.get_properties = aja_source_get_properties;
  1118. aja_source_info.get_defaults = aja_source_get_defaults_v1;
  1119. aja_source_info.save = aja_source_save;
  1120. aja_source_info.icon_type = OBS_ICON_TYPE_CAMERA;
  1121. obs_register_source(&aja_source_info);
  1122. aja_source_info.version = 2;
  1123. aja_source_info.output_flags &= ~OBS_SOURCE_CAP_OBSOLETE;
  1124. aja_source_info.get_defaults = aja_source_get_defaults;
  1125. obs_register_source(&aja_source_info);
  1126. }