decklink-device-discovery.hpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #pragma once
  2. #include <vector>
  3. #include <mutex>
  4. #include "decklink.hpp"
  5. class DeckLinkDevice;
  6. typedef void (*DeviceChangeCallback)(void *param, DeckLinkDevice *device,
  7. bool added);
  8. struct DeviceChangeInfo {
  9. DeviceChangeCallback callback;
  10. void *param;
  11. };
  12. class DeckLinkDeviceDiscovery : public IDeckLinkDeviceNotificationCallback {
  13. protected:
  14. ComPtr<IDeckLinkDiscovery> discovery;
  15. long refCount = 1;
  16. bool initialized = false;
  17. std::recursive_mutex deviceMutex;
  18. std::vector<DeckLinkDevice*> devices;
  19. std::vector<DeviceChangeInfo> callbacks;
  20. public:
  21. DeckLinkDeviceDiscovery();
  22. virtual ~DeckLinkDeviceDiscovery(void);
  23. bool Init();
  24. HRESULT STDMETHODCALLTYPE DeckLinkDeviceArrived(IDeckLink *device);
  25. HRESULT STDMETHODCALLTYPE DeckLinkDeviceRemoved(IDeckLink *device);
  26. inline void AddCallback(DeviceChangeCallback callback, void *param)
  27. {
  28. std::lock_guard<std::recursive_mutex> lock(deviceMutex);
  29. DeviceChangeInfo info;
  30. info.callback = callback;
  31. info.param = param;
  32. for (DeviceChangeInfo &curCB : callbacks) {
  33. if (curCB.callback == callback &&
  34. curCB.param == param)
  35. return;
  36. }
  37. callbacks.push_back(info);
  38. }
  39. inline void RemoveCallback(DeviceChangeCallback callback, void *param)
  40. {
  41. std::lock_guard<std::recursive_mutex> lock(deviceMutex);
  42. for (size_t i = 0; i < callbacks.size(); i++) {
  43. DeviceChangeInfo &curCB = callbacks[i];
  44. if (curCB.callback == callback &&
  45. curCB.param == param) {
  46. callbacks.erase(callbacks.begin() + i);
  47. return;
  48. }
  49. }
  50. }
  51. DeckLinkDevice *FindByHash(const char *hash);
  52. inline void Lock() {deviceMutex.lock();}
  53. inline void Unlock() {deviceMutex.unlock();}
  54. inline const std::vector<DeckLinkDevice*> &GetDevices() const
  55. {
  56. return devices;
  57. }
  58. ULONG STDMETHODCALLTYPE AddRef(void);
  59. HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, LPVOID *ppv);
  60. ULONG STDMETHODCALLTYPE Release(void);
  61. };