decklink-device.cpp 2.9 KB

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