decklink-device.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 : modes)
  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<IDeckLinkAttributes> attributes;
  26. const HRESULT result = device->QueryInterface(IID_IDeckLinkAttributes,
  27. (void **)&attributes);
  28. if (result == S_OK) {
  29. decklink_bool_t detectable = false;
  30. if (attributes->GetFlag(BMDDeckLinkSupportsInputFormatDetection,
  31. &detectable) == S_OK && !!detectable) {
  32. DeckLinkDeviceMode *mode =
  33. new DeckLinkDeviceMode("Auto", MODE_ID_AUTO);
  34. modes.push_back(mode);
  35. modeIdMap[MODE_ID_AUTO] = mode;
  36. }
  37. }
  38. ComPtr<IDeckLinkInput> input;
  39. if (device->QueryInterface(IID_IDeckLinkInput, (void**)&input) != S_OK)
  40. return false;
  41. IDeckLinkDisplayModeIterator *modeIterator;
  42. if (input->GetDisplayModeIterator(&modeIterator) == S_OK) {
  43. IDeckLinkDisplayMode *displayMode;
  44. long long modeId = 1;
  45. while (modeIterator->Next(&displayMode) == S_OK) {
  46. if (displayMode == nullptr)
  47. continue;
  48. DeckLinkDeviceMode *mode =
  49. new DeckLinkDeviceMode(displayMode, modeId);
  50. modes.push_back(mode);
  51. modeIdMap[modeId] = mode;
  52. displayMode->Release();
  53. ++modeId;
  54. }
  55. modeIterator->Release();
  56. }
  57. decklink_string_t decklinkModelName;
  58. decklink_string_t decklinkDisplayName;
  59. if (device->GetModelName(&decklinkModelName) != S_OK)
  60. return false;
  61. DeckLinkStringToStdString(decklinkModelName, name);
  62. if (device->GetDisplayName(&decklinkDisplayName) != S_OK)
  63. return false;
  64. DeckLinkStringToStdString(decklinkDisplayName, displayName);
  65. hash = displayName;
  66. if (result != S_OK)
  67. return true;
  68. int64_t channels;
  69. /* Intensity Shuttle for Thunderbolt return 2; however, it supports 8 channels */
  70. if (name == "Intensity Shuttle Thunderbolt")
  71. maxChannel = 8;
  72. else if (attributes->GetInt(BMDDeckLinkMaximumAudioChannels, &channels) == S_OK)
  73. maxChannel = (int32_t)channels;
  74. else
  75. maxChannel = 2;
  76. /* http://forum.blackmagicdesign.com/viewtopic.php?f=12&t=33967
  77. * BMDDeckLinkTopologicalID for older devices
  78. * BMDDeckLinkPersistentID for newer ones */
  79. int64_t value;
  80. if (attributes->GetInt(BMDDeckLinkPersistentID, &value) != S_OK &&
  81. attributes->GetInt(BMDDeckLinkTopologicalID, &value) != S_OK)
  82. return true;
  83. std::ostringstream os;
  84. os << value << "_" << name;
  85. hash = os.str();
  86. return true;
  87. }
  88. bool DeckLinkDevice::GetInput(IDeckLinkInput **input)
  89. {
  90. if (device->QueryInterface(IID_IDeckLinkInput, (void**)input) != S_OK)
  91. return false;
  92. return true;
  93. }
  94. DeckLinkDeviceMode *DeckLinkDevice::FindMode(long long id)
  95. {
  96. return modeIdMap[id];
  97. }
  98. const std::string& DeckLinkDevice::GetDisplayName(void)
  99. {
  100. return displayName;
  101. }
  102. const std::string& DeckLinkDevice::GetHash(void) const
  103. {
  104. return hash;
  105. }
  106. const std::vector<DeckLinkDeviceMode *>& DeckLinkDevice::GetModes(void) const
  107. {
  108. return modes;
  109. }
  110. const std::string& DeckLinkDevice::GetName(void) const
  111. {
  112. return name;
  113. }
  114. int32_t DeckLinkDevice::GetMaxChannel(void) const
  115. {
  116. return maxChannel;
  117. }