DecklinkInput.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. return false;
  55. }
  56. if (isActive)
  57. instance->StopCapture();
  58. isCapturing = false;
  59. if (!same)
  60. instance.Set(new DeckLinkDeviceInstance(this, device));
  61. if (instance == nullptr)
  62. return false;
  63. if (GetDevice() == nullptr) {
  64. LOG(LOG_ERROR, "Tried to activate an input with nullptr device.");
  65. return false;
  66. }
  67. DeckLinkDeviceMode *mode = GetDevice()->FindInputMode(modeId);
  68. if (mode == nullptr) {
  69. instance = nullptr;
  70. return false;
  71. }
  72. if (!instance->StartCapture(mode)) {
  73. instance = nullptr;
  74. return false;
  75. }
  76. os_atomic_inc_long(&activateRefs);
  77. SaveSettings();
  78. id = modeId;
  79. isCapturing = true;
  80. return true;
  81. }
  82. void DeckLinkInput::Deactivate(void)
  83. {
  84. std::lock_guard<std::recursive_mutex> lock(deviceMutex);
  85. if (instance)
  86. instance->StopCapture();
  87. isCapturing = false;
  88. instance = nullptr;
  89. os_atomic_dec_long(&activateRefs);
  90. }
  91. bool DeckLinkInput::Capturing(void)
  92. {
  93. return isCapturing;
  94. }
  95. void DeckLinkInput::SaveSettings()
  96. {
  97. if (!instance)
  98. return;
  99. DeckLinkDevice *device = instance->GetDevice();
  100. DeckLinkDeviceMode *mode = instance->GetMode();
  101. obs_data_t *settings = obs_source_get_settings(source);
  102. obs_data_set_string(settings, "device_hash",
  103. device->GetHash().c_str());
  104. obs_data_set_string(settings, "device_name",
  105. device->GetDisplayName().c_str());
  106. obs_data_set_int(settings, "mode_id", instance->GetActiveModeId());
  107. obs_data_set_string(settings, "mode_name", mode->GetName().c_str());
  108. obs_data_release(settings);
  109. }
  110. obs_source_t *DeckLinkInput::GetSource(void) const
  111. {
  112. return source;
  113. }