decklink-device.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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. // Sub Device Counts
  95. attributes->GetInt(BMDDeckLinkSubDeviceIndex, &subDeviceIndex);
  96. attributes->GetInt(BMDDeckLinkNumberOfSubDevices, &numSubDevices);
  97. decklink_string_t decklinkModelName;
  98. decklink_string_t decklinkDisplayName;
  99. if (device->GetModelName(&decklinkModelName) != S_OK)
  100. return false;
  101. DeckLinkStringToStdString(decklinkModelName, name);
  102. if (device->GetDisplayName(&decklinkDisplayName) != S_OK)
  103. return false;
  104. DeckLinkStringToStdString(decklinkDisplayName, displayName);
  105. hash = displayName;
  106. if (result != S_OK)
  107. return true;
  108. int64_t channels;
  109. /* Intensity Shuttle for Thunderbolt return 2; however, it supports 8 channels */
  110. if (name == "Intensity Shuttle Thunderbolt")
  111. maxChannel = 8;
  112. else if (attributes->GetInt(BMDDeckLinkMaximumAudioChannels,
  113. &channels) == S_OK)
  114. maxChannel = (int32_t)channels;
  115. else
  116. maxChannel = 2;
  117. /* http://forum.blackmagicdesign.com/viewtopic.php?f=12&t=33967
  118. * BMDDeckLinkTopologicalID for older devices
  119. * BMDDeckLinkPersistentID for newer ones */
  120. int64_t value;
  121. if (attributes->GetInt(BMDDeckLinkPersistentID, &value) != S_OK &&
  122. attributes->GetInt(BMDDeckLinkTopologicalID, &value) != S_OK)
  123. return true;
  124. std::ostringstream os;
  125. os << value << "_" << name;
  126. hash = os.str();
  127. return true;
  128. }
  129. bool DeckLinkDevice::GetInput(IDeckLinkInput **input)
  130. {
  131. if (device->QueryInterface(IID_IDeckLinkInput, (void **)input) != S_OK)
  132. return false;
  133. return true;
  134. }
  135. bool DeckLinkDevice::GetOutput(IDeckLinkOutput **output)
  136. {
  137. if (device->QueryInterface(IID_IDeckLinkOutput, (void **)output) !=
  138. S_OK)
  139. return false;
  140. return true;
  141. }
  142. bool DeckLinkDevice::GetKeyer(IDeckLinkKeyer **deckLinkKeyer)
  143. {
  144. if (device->QueryInterface(IID_IDeckLinkKeyer,
  145. (void **)deckLinkKeyer) != S_OK) {
  146. fprintf(stderr,
  147. "Could not obtain the IDeckLinkKeyer interface\n");
  148. return false;
  149. }
  150. return true;
  151. }
  152. void DeckLinkDevice::SetKeyerMode(int newKeyerMode)
  153. {
  154. keyerMode = newKeyerMode;
  155. }
  156. int DeckLinkDevice::GetKeyerMode(void)
  157. {
  158. return keyerMode;
  159. }
  160. DeckLinkDeviceMode *DeckLinkDevice::FindInputMode(long long id)
  161. {
  162. return inputModeIdMap[id];
  163. }
  164. DeckLinkDeviceMode *DeckLinkDevice::FindOutputMode(long long id)
  165. {
  166. return outputModeIdMap[id];
  167. }
  168. const std::string &DeckLinkDevice::GetDisplayName(void)
  169. {
  170. return displayName;
  171. }
  172. const std::string &DeckLinkDevice::GetHash(void) const
  173. {
  174. return hash;
  175. }
  176. const std::vector<DeckLinkDeviceMode *> &
  177. DeckLinkDevice::GetInputModes(void) const
  178. {
  179. return inputModes;
  180. }
  181. const std::vector<DeckLinkDeviceMode *> &
  182. DeckLinkDevice::GetOutputModes(void) const
  183. {
  184. return outputModes;
  185. }
  186. int64_t DeckLinkDevice::GetVideoInputConnections()
  187. {
  188. return supportedVideoInputConnections;
  189. }
  190. int64_t DeckLinkDevice::GetAudioInputConnections()
  191. {
  192. return supportedAudioInputConnections;
  193. }
  194. bool DeckLinkDevice::GetSupportsExternalKeyer(void) const
  195. {
  196. return supportsExternalKeyer;
  197. }
  198. bool DeckLinkDevice::GetSupportsInternalKeyer(void) const
  199. {
  200. return supportsInternalKeyer;
  201. }
  202. int64_t DeckLinkDevice::GetSubDeviceCount()
  203. {
  204. return numSubDevices;
  205. }
  206. int64_t DeckLinkDevice::GetSubDeviceIndex()
  207. {
  208. return subDeviceIndex;
  209. }
  210. const std::string &DeckLinkDevice::GetName(void) const
  211. {
  212. return name;
  213. }
  214. int32_t DeckLinkDevice::GetMaxChannel(void) const
  215. {
  216. return maxChannel;
  217. }