decklink-device.cpp 6.0 KB

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