1
0

decklink-device.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #include <sstream>
  2. #include "decklink-device.hpp"
  3. #include <util/threading.h>
  4. DeckLinkDevice::DeckLinkDevice(IDeckLink *device_) : device(device_) {}
  5. DeckLinkDevice::~DeckLinkDevice(void)
  6. {
  7. for (DeckLinkDeviceMode *mode : inputModes)
  8. delete mode;
  9. for (DeckLinkDeviceMode *mode : outputModes)
  10. delete mode;
  11. }
  12. ULONG DeckLinkDevice::AddRef()
  13. {
  14. return os_atomic_inc_long(&refCount);
  15. }
  16. ULONG DeckLinkDevice::Release()
  17. {
  18. long ret = os_atomic_dec_long(&refCount);
  19. if (ret == 0)
  20. delete this;
  21. return ret;
  22. }
  23. bool DeckLinkDevice::Init()
  24. {
  25. ComPtr<IDeckLinkProfileAttributes> attributes;
  26. const HRESULT result = device->QueryInterface(IID_IDeckLinkProfileAttributes, (void **)&attributes);
  27. if (result == S_OK) {
  28. decklink_bool_t detectable = false;
  29. if (attributes->GetFlag(BMDDeckLinkSupportsInputFormatDetection, &detectable) == S_OK && !!detectable) {
  30. DeckLinkDeviceMode *mode = new DeckLinkDeviceMode("Auto", MODE_ID_AUTO);
  31. inputModes.push_back(mode);
  32. inputModeIdMap[MODE_ID_AUTO] = mode;
  33. }
  34. }
  35. // Find input modes
  36. ComPtr<IDeckLinkInput> input;
  37. if (device->QueryInterface(IID_IDeckLinkInput, (void **)&input) == S_OK) {
  38. ComPtr<IDeckLinkDisplayModeIterator> modeIterator;
  39. if (input->GetDisplayModeIterator(&modeIterator) == S_OK) {
  40. ComPtr<IDeckLinkDisplayMode> displayMode;
  41. long long modeId = 1;
  42. while (modeIterator->Next(&displayMode) == S_OK) {
  43. if (displayMode == nullptr)
  44. continue;
  45. DeckLinkDeviceMode *mode = new DeckLinkDeviceMode(displayMode, modeId);
  46. inputModes.push_back(mode);
  47. inputModeIdMap[modeId] = mode;
  48. ++modeId;
  49. }
  50. }
  51. }
  52. // Get supported video connections
  53. attributes->GetInt(BMDDeckLinkVideoInputConnections, &supportedVideoInputConnections);
  54. attributes->GetInt(BMDDeckLinkVideoOutputConnections, &supportedVideoOutputConnections);
  55. // Get supported audio connections
  56. attributes->GetInt(BMDDeckLinkAudioInputConnections, &supportedAudioInputConnections);
  57. attributes->GetInt(BMDDeckLinkAudioOutputConnections, &supportedAudioOutputConnections);
  58. // find output modes
  59. ComPtr<IDeckLinkOutput> output;
  60. if (device->QueryInterface(IID_IDeckLinkOutput, (void **)&output) == S_OK) {
  61. ComPtr<IDeckLinkDisplayModeIterator> modeIterator;
  62. if (output->GetDisplayModeIterator(&modeIterator) == S_OK) {
  63. ComPtr<IDeckLinkDisplayMode> displayMode;
  64. long long modeId = 1;
  65. while (modeIterator->Next(&displayMode) == S_OK) {
  66. if (displayMode == nullptr)
  67. continue;
  68. DeckLinkDeviceMode *mode = new DeckLinkDeviceMode(displayMode, modeId);
  69. outputModes.push_back(mode);
  70. outputModeIdMap[modeId] = mode;
  71. ++modeId;
  72. }
  73. }
  74. }
  75. // get keyer support
  76. attributes->GetFlag(BMDDeckLinkSupportsExternalKeying, &supportsExternalKeyer);
  77. attributes->GetFlag(BMDDeckLinkSupportsInternalKeying, &supportsInternalKeyer);
  78. attributes->GetFlag(BMDDeckLinkSupportsHDRMetadata, &supportsHDRMetadata);
  79. // Sub Device Counts
  80. attributes->GetInt(BMDDeckLinkSubDeviceIndex, &subDeviceIndex);
  81. attributes->GetInt(BMDDeckLinkNumberOfSubDevices, &numSubDevices);
  82. if (FAILED(attributes->GetInt(BMDDeckLinkMinimumPrerollFrames, &minimumPrerollFrames))) {
  83. minimumPrerollFrames = 3;
  84. }
  85. decklink_string_t decklinkModelName;
  86. decklink_string_t decklinkDisplayName;
  87. if (device->GetModelName(&decklinkModelName) != S_OK)
  88. return false;
  89. DeckLinkStringToStdString(decklinkModelName, name);
  90. if (device->GetDisplayName(&decklinkDisplayName) != S_OK)
  91. return false;
  92. DeckLinkStringToStdString(decklinkDisplayName, displayName);
  93. hash = displayName;
  94. if (result != S_OK)
  95. return true;
  96. int64_t channels;
  97. /* Intensity Shuttle for Thunderbolt return 2; however, it supports 8 channels */
  98. if (name == "Intensity Shuttle Thunderbolt")
  99. maxChannel = 8;
  100. else if (attributes->GetInt(BMDDeckLinkMaximumAudioChannels, &channels) == S_OK)
  101. maxChannel = (int32_t)channels;
  102. else
  103. maxChannel = 2;
  104. /* http://forum.blackmagicdesign.com/viewtopic.php?f=12&t=33967
  105. * BMDDeckLinkTopologicalID for older devices
  106. * BMDDeckLinkPersistentID for newer ones */
  107. int64_t value;
  108. if (attributes->GetInt(BMDDeckLinkPersistentID, &value) != S_OK &&
  109. attributes->GetInt(BMDDeckLinkTopologicalID, &value) != S_OK)
  110. return true;
  111. std::ostringstream os;
  112. os << value << "_" << name;
  113. hash = os.str();
  114. return true;
  115. }
  116. bool DeckLinkDevice::GetInput(IDeckLinkInput **input)
  117. {
  118. if (device->QueryInterface(IID_IDeckLinkInput, (void **)input) != S_OK)
  119. return false;
  120. return true;
  121. }
  122. bool DeckLinkDevice::GetOutput(IDeckLinkOutput **output)
  123. {
  124. if (device->QueryInterface(IID_IDeckLinkOutput, (void **)output) != S_OK)
  125. return false;
  126. return true;
  127. }
  128. bool DeckLinkDevice::GetKeyer(IDeckLinkKeyer **deckLinkKeyer)
  129. {
  130. if (device->QueryInterface(IID_IDeckLinkKeyer, (void **)deckLinkKeyer) != S_OK) {
  131. fprintf(stderr, "Could not obtain the IDeckLinkKeyer interface\n");
  132. return false;
  133. }
  134. return true;
  135. }
  136. void DeckLinkDevice::SetKeyerMode(int newKeyerMode)
  137. {
  138. keyerMode = newKeyerMode;
  139. }
  140. int DeckLinkDevice::GetKeyerMode(void)
  141. {
  142. return keyerMode;
  143. }
  144. DeckLinkDeviceMode *DeckLinkDevice::FindInputMode(long long id)
  145. {
  146. return inputModeIdMap[id];
  147. }
  148. DeckLinkDeviceMode *DeckLinkDevice::FindOutputMode(long long id)
  149. {
  150. return outputModeIdMap[id];
  151. }
  152. const std::string &DeckLinkDevice::GetDisplayName(void)
  153. {
  154. return displayName;
  155. }
  156. const std::string &DeckLinkDevice::GetHash(void) const
  157. {
  158. return hash;
  159. }
  160. const std::vector<DeckLinkDeviceMode *> &DeckLinkDevice::GetInputModes(void) const
  161. {
  162. return inputModes;
  163. }
  164. const std::vector<DeckLinkDeviceMode *> &DeckLinkDevice::GetOutputModes(void) const
  165. {
  166. return outputModes;
  167. }
  168. int64_t DeckLinkDevice::GetVideoInputConnections()
  169. {
  170. return supportedVideoInputConnections;
  171. }
  172. int64_t DeckLinkDevice::GetAudioInputConnections()
  173. {
  174. return supportedAudioInputConnections;
  175. }
  176. bool DeckLinkDevice::GetSupportsExternalKeyer(void) const
  177. {
  178. return supportsExternalKeyer;
  179. }
  180. bool DeckLinkDevice::GetSupportsInternalKeyer(void) const
  181. {
  182. return supportsInternalKeyer;
  183. }
  184. bool DeckLinkDevice::GetSupportsHDRMetadata(void) const
  185. {
  186. return supportsHDRMetadata;
  187. }
  188. int64_t DeckLinkDevice::GetSubDeviceCount()
  189. {
  190. return numSubDevices;
  191. }
  192. int64_t DeckLinkDevice::GetSubDeviceIndex()
  193. {
  194. return subDeviceIndex;
  195. }
  196. int64_t DeckLinkDevice::GetMinimumPrerollFrames()
  197. {
  198. return minimumPrerollFrames;
  199. }
  200. const std::string &DeckLinkDevice::GetName(void) const
  201. {
  202. return name;
  203. }
  204. int32_t DeckLinkDevice::GetMaxChannel(void) const
  205. {
  206. return maxChannel;
  207. }