decklink-device.cpp 6.4 KB

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