aja-source.cpp 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181
  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. static bool initialized = false;
  698. auto ajaSource = (AJASource *)data;
  699. if (!ajaSource) {
  700. blog(LOG_WARNING,
  701. "aja_source_update: Plugin instance is null!");
  702. return;
  703. }
  704. const std::string &wantCardID =
  705. obs_data_get_string(settings, kUIPropDevice.id);
  706. const std::string &currentCardID = ajaSource->CardID();
  707. auto io_select = static_cast<IOSelection>(
  708. obs_data_get_int(settings, kUIPropInput.id));
  709. auto vf_select = static_cast<NTV2VideoFormat>(
  710. obs_data_get_int(settings, kUIPropVideoFormatSelect.id));
  711. auto pf_select = static_cast<NTV2PixelFormat>(
  712. obs_data_get_int(settings, kUIPropPixelFormatSelect.id));
  713. auto t4_select = static_cast<SDI4KTransport>(
  714. obs_data_get_int(settings, kUIPropSDI4KTransport.id));
  715. bool deactivateWhileNotShowing =
  716. obs_data_get_bool(settings, kUIPropDeactivateWhenNotShowing.id);
  717. auto &cardManager = aja::CardManager::Instance();
  718. cardManager.EnumerateCards();
  719. if (wantCardID != currentCardID) {
  720. initialized = false;
  721. ajaSource->Deactivate();
  722. }
  723. auto cardEntry = cardManager.GetCardEntry(wantCardID);
  724. if (!cardEntry) {
  725. blog(LOG_DEBUG,
  726. "aja_source_update: Card Entry not found for %s",
  727. wantCardID.c_str());
  728. return;
  729. }
  730. CNTV2Card *card = cardEntry->GetCard();
  731. if (!card || !card->IsOpen()) {
  732. blog(LOG_ERROR, "aja_source_update: AJA device %s not open!",
  733. wantCardID.c_str());
  734. return;
  735. }
  736. if (card->GetModelName() == "(Not Found)") {
  737. blog(LOG_ERROR,
  738. "aja_source_update: AJA device %s disconnected?",
  739. wantCardID.c_str());
  740. return;
  741. }
  742. ajaSource->SetCard(cardEntry->GetCard());
  743. SourceProps curr_props = ajaSource->GetSourceProps();
  744. // Release Channels from previous card if card ID changes
  745. if (wantCardID != currentCardID) {
  746. auto prevCardEntry = cardManager.GetCardEntry(currentCardID);
  747. if (prevCardEntry) {
  748. const std::string &ioSelectStr =
  749. aja::IOSelectionToString(curr_props.ioSelect);
  750. if (!prevCardEntry->ReleaseInputSelection(
  751. curr_props.ioSelect, curr_props.deviceID,
  752. ajaSource->GetName())) {
  753. blog(LOG_WARNING,
  754. "aja_source_update: Error releasing IOSelection %s for card ID %s",
  755. ioSelectStr.c_str(),
  756. currentCardID.c_str());
  757. } else {
  758. blog(LOG_INFO,
  759. "aja_source_update: Released IOSelection %s for card ID %s",
  760. ioSelectStr.c_str(),
  761. currentCardID.c_str());
  762. ajaSource->SetCardID(wantCardID);
  763. io_select = IOSelection::Invalid;
  764. }
  765. }
  766. }
  767. if (io_select == IOSelection::Invalid) {
  768. blog(LOG_DEBUG, "aja_source_update: Invalid IOSelection");
  769. return;
  770. }
  771. NTV2InputSourceSet input_srcs;
  772. aja::IOSelectionToInputSources(io_select, input_srcs);
  773. if (input_srcs.empty()) {
  774. blog(LOG_INFO,
  775. "aja_source_update: No NTV2InputSources found for IOSelection %s",
  776. aja::IOSelectionToString(io_select).c_str());
  777. return;
  778. }
  779. SourceProps want_props;
  780. want_props.deviceID = card->GetDeviceID();
  781. want_props.ioSelect = io_select;
  782. want_props.inputSource = *input_srcs.begin();
  783. want_props.videoFormat =
  784. ((int32_t)vf_select == kVideoFormatAuto)
  785. ? NTV2_FORMAT_UNKNOWN
  786. : static_cast<NTV2VideoFormat>(vf_select);
  787. want_props.pixelFormat =
  788. ((int32_t)pf_select == kPixelFormatAuto)
  789. ? NTV2_FBF_INVALID
  790. : static_cast<NTV2PixelFormat>(pf_select);
  791. want_props.sdi4kTransport = t4_select;
  792. want_props.vpids.clear();
  793. want_props.deactivateWhileNotShowing = deactivateWhileNotShowing;
  794. want_props.autoDetect = ((int32_t)vf_select == kVideoFormatAuto ||
  795. (int32_t)pf_select == kPixelFormatAuto);
  796. ajaSource->SetCardID(wantCardID);
  797. ajaSource->SetDeviceIndex((UWord)cardEntry->GetCardIndex());
  798. if (NTV2_IS_4K_VIDEO_FORMAT(want_props.videoFormat) &&
  799. want_props.sdi4kTransport == SDI4KTransport::Squares) {
  800. if (want_props.ioSelect == IOSelection::SDI1_2) {
  801. want_props.ioSelect = IOSelection::SDI1_2_Squares;
  802. } else if (want_props.ioSelect == IOSelection::SDI3_4) {
  803. want_props.ioSelect = IOSelection::SDI3_4_Squares;
  804. }
  805. }
  806. // Release Channels if IOSelection changes
  807. if (want_props.ioSelect != curr_props.ioSelect) {
  808. const std::string &ioSelectStr =
  809. aja::IOSelectionToString(curr_props.ioSelect);
  810. if (!cardEntry->ReleaseInputSelection(curr_props.ioSelect,
  811. curr_props.deviceID,
  812. ajaSource->GetName())) {
  813. blog(LOG_WARNING,
  814. "aja_source_update: Error releasing IOSelection %s for card ID %s",
  815. ioSelectStr.c_str(), currentCardID.c_str());
  816. } else {
  817. blog(LOG_INFO,
  818. "aja_source_update: Released IOSelection %s for card ID %s",
  819. ioSelectStr.c_str(), currentCardID.c_str());
  820. }
  821. }
  822. // Acquire Channels for current IOSelection
  823. if (!cardEntry->AcquireInputSelection(want_props.ioSelect,
  824. want_props.deviceID,
  825. ajaSource->GetName())) {
  826. blog(LOG_ERROR,
  827. "aja_source_update: Could not acquire IOSelection %s",
  828. aja::IOSelectionToString(want_props.ioSelect).c_str());
  829. return;
  830. }
  831. // Read SDI video payload IDs (VPID) used for helping to determine the wire format
  832. NTV2VideoFormat new_vf = want_props.videoFormat;
  833. NTV2PixelFormat new_pf = want_props.pixelFormat;
  834. if (!ajaSource->ReadWireFormats(want_props.deviceID, input_srcs, new_vf,
  835. new_pf, want_props.vpids)) {
  836. blog(LOG_ERROR, "aja_source_update: ReadWireFormats failed!");
  837. cardEntry->ReleaseInputSelection(want_props.ioSelect,
  838. curr_props.deviceID,
  839. ajaSource->GetName());
  840. return;
  841. }
  842. // Set auto-detected formats
  843. if ((int32_t)vf_select == kVideoFormatAuto)
  844. want_props.videoFormat = new_vf;
  845. if ((int32_t)pf_select == kPixelFormatAuto)
  846. want_props.pixelFormat = new_pf;
  847. if (want_props.videoFormat == NTV2_FORMAT_UNKNOWN ||
  848. want_props.pixelFormat == NTV2_FBF_INVALID) {
  849. blog(LOG_ERROR,
  850. "aja_source_update: Unknown video/pixel format(s): %s / %s",
  851. NTV2VideoFormatToString(want_props.videoFormat).c_str(),
  852. NTV2FrameBufferFormatToString(want_props.pixelFormat)
  853. .c_str());
  854. cardEntry->ReleaseInputSelection(want_props.ioSelect,
  855. curr_props.deviceID,
  856. ajaSource->GetName());
  857. return;
  858. }
  859. // Change capture format and restart capture thread
  860. if (!initialized || want_props != ajaSource->GetSourceProps()) {
  861. Routing::ConfigureSourceRoute(want_props, NTV2_MODE_CAPTURE,
  862. card);
  863. ajaSource->Deactivate();
  864. initialized = true;
  865. }
  866. ajaSource->SetSourceProps(want_props);
  867. ajaSource->ResetVideoBuffer(want_props.videoFormat,
  868. want_props.pixelFormat);
  869. Routing::StartSourceAudio(want_props, card);
  870. card->SetReference(NTV2_REFERENCE_FREERUN);
  871. ajaSource->Activate(true);
  872. }
  873. static obs_properties_t *aja_source_get_properties(void *data)
  874. {
  875. obs_properties_t *props = obs_properties_create();
  876. // Uncomment below to only update when the user presses OK or Apply
  877. // obs_properties_set_flags(props, OBS_PROPERTIES_DEFER_UPDATE);
  878. obs_property_t *device_list = obs_properties_add_list(
  879. props, kUIPropDevice.id, obs_module_text(kUIPropDevice.text),
  880. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_STRING);
  881. populate_source_device_list(device_list);
  882. obs_property_t *io_select_list = obs_properties_add_list(
  883. props, kUIPropInput.id, obs_module_text(kUIPropInput.text),
  884. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  885. obs_property_t *vid_fmt_list = obs_properties_add_list(
  886. props, kUIPropVideoFormatSelect.id,
  887. obs_module_text(kUIPropVideoFormatSelect.text),
  888. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  889. obs_properties_add_list(props, kUIPropPixelFormatSelect.id,
  890. obs_module_text(kUIPropPixelFormatSelect.text),
  891. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  892. obs_properties_add_list(props, kUIPropSDI4KTransport.id,
  893. obs_module_text(kUIPropSDI4KTransport.text),
  894. OBS_COMBO_TYPE_LIST, OBS_COMBO_FORMAT_INT);
  895. obs_properties_add_bool(
  896. props, kUIPropDeactivateWhenNotShowing.id,
  897. obs_module_text(kUIPropDeactivateWhenNotShowing.text));
  898. obs_property_set_modified_callback(vid_fmt_list,
  899. aja_video_format_changed);
  900. obs_property_set_modified_callback2(device_list,
  901. aja_source_device_changed, data);
  902. obs_property_set_modified_callback2(io_select_list,
  903. aja_io_selection_changed, data);
  904. return props;
  905. }
  906. void aja_source_get_defaults(obs_data_t *settings)
  907. {
  908. obs_data_set_default_int(settings, kUIPropInput.id,
  909. static_cast<long long>(IOSelection::Invalid));
  910. obs_data_set_default_int(settings, kUIPropVideoFormatSelect.id,
  911. static_cast<long long>(kVideoFormatAuto));
  912. obs_data_set_default_int(
  913. settings, kUIPropPixelFormatSelect.id,
  914. static_cast<long long>(kDefaultAJAPixelFormat));
  915. obs_data_set_default_int(
  916. settings, kUIPropSDI4KTransport.id,
  917. static_cast<long long>(SDI4KTransport::TwoSampleInterleave));
  918. obs_data_set_default_bool(settings, kUIPropDeactivateWhenNotShowing.id,
  919. false);
  920. }
  921. void aja_source_save(void *data, obs_data_t *settings)
  922. {
  923. const char *cardID = obs_data_get_string(settings, kUIPropDevice.id);
  924. AJASource *ajaSource = (AJASource *)data;
  925. if (!ajaSource) {
  926. blog(LOG_ERROR,
  927. "aja_source_save: AJA Source instance is null!");
  928. return;
  929. }
  930. auto &cardManager = aja::CardManager::Instance();
  931. auto cardEntry = cardManager.GetCardEntry(cardID);
  932. if (!cardID || !cardEntry) {
  933. blog(LOG_DEBUG, "aja_source_save: Card Entry not found for %s",
  934. cardID);
  935. return;
  936. }
  937. auto oldName = ajaSource->GetName();
  938. auto newName = obs_source_get_name(ajaSource->GetOBSSource());
  939. if (oldName != newName &&
  940. cardEntry->UpdateChannelOwnerName(oldName, newName)) {
  941. ajaSource->SetName(newName);
  942. blog(LOG_DEBUG, "aja_source_save: Renamed \"%s\" to \"%s\"",
  943. oldName.c_str(), newName);
  944. }
  945. }
  946. struct obs_source_info create_aja_source_info()
  947. {
  948. struct obs_source_info aja_source_info = {};
  949. aja_source_info.id = kUIPropCaptureModule.id;
  950. aja_source_info.type = OBS_SOURCE_TYPE_INPUT;
  951. aja_source_info.output_flags = OBS_SOURCE_ASYNC_VIDEO |
  952. OBS_SOURCE_AUDIO |
  953. OBS_SOURCE_DO_NOT_DUPLICATE;
  954. aja_source_info.get_name = aja_source_get_name;
  955. aja_source_info.create = aja_source_create;
  956. aja_source_info.destroy = aja_source_destroy;
  957. aja_source_info.update = aja_source_update;
  958. aja_source_info.show = aja_source_show;
  959. aja_source_info.hide = aja_source_hide;
  960. aja_source_info.activate = aja_source_activate;
  961. aja_source_info.deactivate = aja_source_deactivate;
  962. aja_source_info.get_properties = aja_source_get_properties;
  963. aja_source_info.get_defaults = aja_source_get_defaults;
  964. aja_source_info.save = aja_source_save;
  965. aja_source_info.icon_type = OBS_ICON_TYPE_CAMERA;
  966. return aja_source_info;
  967. }