decklink-device-discovery.hpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #pragma once
  2. #include <obs-module.h>
  3. #include "platform.hpp"
  4. #include <vector>
  5. #include <mutex>
  6. class DeckLinkDevice;
  7. typedef void (*DeviceChangeCallback)(void *param, DeckLinkDevice *device,
  8. bool added);
  9. struct DeviceChangeInfo {
  10. DeviceChangeCallback callback;
  11. void *param;
  12. };
  13. class DeckLinkDeviceDiscovery : public IDeckLinkDeviceNotificationCallback {
  14. protected:
  15. ComPtr<IDeckLinkDiscovery> discovery;
  16. long refCount = 1;
  17. bool initialized = false;
  18. std::recursive_mutex deviceMutex;
  19. std::vector<DeckLinkDevice *> devices;
  20. std::vector<DeviceChangeInfo> callbacks;
  21. public:
  22. DeckLinkDeviceDiscovery();
  23. virtual ~DeckLinkDeviceDiscovery(void);
  24. bool Init();
  25. HRESULT STDMETHODCALLTYPE DeckLinkDeviceArrived(IDeckLink *device);
  26. HRESULT STDMETHODCALLTYPE DeckLinkDeviceRemoved(IDeckLink *device);
  27. inline void AddCallback(DeviceChangeCallback callback, void *param)
  28. {
  29. std::lock_guard<std::recursive_mutex> lock(deviceMutex);
  30. DeviceChangeInfo info;
  31. info.callback = callback;
  32. info.param = param;
  33. for (DeviceChangeInfo &curCB : callbacks) {
  34. if (curCB.callback == callback && 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. };