DecklinkInput.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #include "DecklinkInput.hpp"
  2. #include <util/threading.h>
  3. DeckLinkInput::DeckLinkInput(obs_source_t *source, DeckLinkDeviceDiscovery *discovery_)
  4. : DecklinkBase(discovery_),
  5. source(source)
  6. {
  7. discovery->AddCallback(DeckLinkInput::DevicesChanged, this);
  8. }
  9. DeckLinkInput::~DeckLinkInput(void)
  10. {
  11. discovery->RemoveCallback(DeckLinkInput::DevicesChanged, this);
  12. Deactivate();
  13. }
  14. void DeckLinkInput::DevicesChanged(void *param, DeckLinkDevice *device, bool added)
  15. {
  16. DeckLinkInput *decklink = reinterpret_cast<DeckLinkInput*>(param);
  17. std::lock_guard<std::recursive_mutex> lock(decklink->deviceMutex);
  18. obs_source_update_properties(decklink->source);
  19. if (added && !decklink->instance) {
  20. const char *hash;
  21. long long mode;
  22. obs_data_t *settings;
  23. settings = obs_source_get_settings(decklink->source);
  24. hash = obs_data_get_string(settings, "device_hash");
  25. mode = obs_data_get_int(settings, "mode_id");
  26. obs_data_release(settings);
  27. if (device->GetHash().compare(hash) == 0) {
  28. if (!decklink->activateRefs)
  29. return;
  30. if (decklink->Activate(device, mode))
  31. os_atomic_dec_long(&decklink->activateRefs);
  32. }
  33. } else if (!added && decklink->instance) {
  34. if (decklink->instance->GetDevice() == device) {
  35. os_atomic_inc_long(&decklink->activateRefs);
  36. decklink->Deactivate();
  37. }
  38. }
  39. }
  40. bool DeckLinkInput::Activate(DeckLinkDevice *device, long long modeId)
  41. {
  42. std::lock_guard<std::recursive_mutex> lock(deviceMutex);
  43. DeckLinkDevice *curDevice = GetDevice();
  44. const bool same = device == curDevice;
  45. const bool isActive = instance != nullptr;
  46. if (same) {
  47. if (!isActive)
  48. return false;
  49. if (instance->GetActiveModeId() == modeId &&
  50. instance->GetActivePixelFormat() == pixelFormat &&
  51. instance->GetActiveColorSpace() == colorSpace &&
  52. instance->GetActiveColorRange() == colorRange &&
  53. instance->GetActiveChannelFormat() == channelFormat &&
  54. instance->GetActiveSwapState() == swap)
  55. return false;
  56. }
  57. if (isActive)
  58. instance->StopCapture();
  59. isCapturing = false;
  60. if (!same)
  61. instance.Set(new DeckLinkDeviceInstance(this, device));
  62. if (instance == nullptr)
  63. return false;
  64. if (GetDevice() == nullptr) {
  65. LOG(LOG_ERROR, "Tried to activate an input with nullptr device.");
  66. return false;
  67. }
  68. DeckLinkDeviceMode *mode = GetDevice()->FindInputMode(modeId);
  69. if (mode == nullptr) {
  70. instance = nullptr;
  71. return false;
  72. }
  73. if (!instance->StartCapture(mode)) {
  74. instance = nullptr;
  75. return false;
  76. }
  77. os_atomic_inc_long(&activateRefs);
  78. SaveSettings();
  79. id = modeId;
  80. isCapturing = true;
  81. return true;
  82. }
  83. void DeckLinkInput::Deactivate(void)
  84. {
  85. std::lock_guard<std::recursive_mutex> lock(deviceMutex);
  86. if (instance)
  87. instance->StopCapture();
  88. isCapturing = false;
  89. instance = nullptr;
  90. os_atomic_dec_long(&activateRefs);
  91. }
  92. bool DeckLinkInput::Capturing(void)
  93. {
  94. return isCapturing;
  95. }
  96. void DeckLinkInput::SaveSettings()
  97. {
  98. if (!instance)
  99. return;
  100. DeckLinkDevice *device = instance->GetDevice();
  101. DeckLinkDeviceMode *mode = instance->GetMode();
  102. obs_data_t *settings = obs_source_get_settings(source);
  103. obs_data_set_string(settings, "device_hash",
  104. device->GetHash().c_str());
  105. obs_data_set_string(settings, "device_name",
  106. device->GetDisplayName().c_str());
  107. obs_data_set_int(settings, "mode_id", instance->GetActiveModeId());
  108. obs_data_set_string(settings, "mode_name", mode->GetName().c_str());
  109. obs_data_release(settings);
  110. }
  111. obs_source_t *DeckLinkInput::GetSource(void) const
  112. {
  113. return source;
  114. }