decklink-device.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 value;
  59. if (attributes->GetInt(BMDDeckLinkPersistentID, &value) != S_OK)
  60. return true;
  61. std::ostringstream os;
  62. os << value << "_" << name;
  63. hash = os.str();
  64. return true;
  65. }
  66. bool DeckLinkDevice::GetInput(IDeckLinkInput **input)
  67. {
  68. if (device->QueryInterface(IID_IDeckLinkInput, (void**)input) != S_OK)
  69. return false;
  70. return true;
  71. }
  72. DeckLinkDeviceMode *DeckLinkDevice::FindMode(long long id)
  73. {
  74. return modeIdMap[id];
  75. }
  76. const std::string& DeckLinkDevice::GetDisplayName(void)
  77. {
  78. return displayName;
  79. }
  80. const std::string& DeckLinkDevice::GetHash(void) const
  81. {
  82. return hash;
  83. }
  84. const std::vector<DeckLinkDeviceMode *>& DeckLinkDevice::GetModes(void) const
  85. {
  86. return modes;
  87. }
  88. const std::string& DeckLinkDevice::GetName(void) const
  89. {
  90. return name;
  91. }