decklink-device-discovery.hpp 1.9 KB

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