decklink-device.cpp 6.1 KB

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