aja-source.cpp 32 KB

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