decklink-device-discovery.hpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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, 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 && curCB.param == param)
  34. return;
  35. }
  36. callbacks.push_back(info);
  37. }
  38. inline void RemoveCallback(DeviceChangeCallback callback, void *param)
  39. {
  40. std::lock_guard<std::recursive_mutex> lock(deviceMutex);
  41. for (size_t i = 0; i < callbacks.size(); i++) {
  42. DeviceChangeInfo &curCB = callbacks[i];
  43. if (curCB.callback == callback && curCB.param == param) {
  44. callbacks.erase(callbacks.begin() + i);
  45. return;
  46. }
  47. }
  48. }
  49. DeckLinkDevice *FindByHash(const char *hash);
  50. inline void Lock() { deviceMutex.lock(); }
  51. inline void Unlock() { deviceMutex.unlock(); }
  52. inline const std::vector<DeckLinkDevice *> &GetDevices() const { return devices; }
  53. ULONG STDMETHODCALLTYPE AddRef(void);
  54. HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, LPVOID *ppv);
  55. ULONG STDMETHODCALLTYPE Release(void);
  56. };