| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 | 
							- #include "decklink-device-discovery.hpp"
 
- #include "decklink-device.hpp"
 
- #include <util/threading.h>
 
- DeckLinkDeviceDiscovery::DeckLinkDeviceDiscovery()
 
- {
 
- 	discovery = CreateDeckLinkDiscoveryInstance();
 
- 	if (discovery == nullptr)
 
- 		blog(LOG_INFO, "No blackmagic support");
 
- }
 
- DeckLinkDeviceDiscovery::~DeckLinkDeviceDiscovery(void)
 
- {
 
- 	if (discovery != nullptr) {
 
- 		if (initialized)
 
- 			discovery->UninstallDeviceNotifications();
 
- 		for (DeckLinkDevice *device : devices)
 
- 			device->Release();
 
- 	}
 
- }
 
- bool DeckLinkDeviceDiscovery::Init(void)
 
- {
 
- 	HRESULT result = E_FAIL;
 
- 	if (initialized)
 
- 		return false;
 
- 	if (discovery != nullptr)
 
- 		result = discovery->InstallDeviceNotifications(this);
 
- 	initialized = result == S_OK;
 
- 	if (!initialized)
 
- 		blog(LOG_DEBUG, "Failed to start search for DeckLink devices");
 
- 	return initialized;
 
- }
 
- DeckLinkDevice *DeckLinkDeviceDiscovery::FindByHash(const char *hash)
 
- {
 
- 	DeckLinkDevice *ret = nullptr;
 
- 	deviceMutex.lock();
 
- 	for (DeckLinkDevice *device : devices) {
 
- 		if (device->GetHash().compare(hash) == 0) {
 
- 			ret = device;
 
- 			ret->AddRef();
 
- 			break;
 
- 		}
 
- 	}
 
- 	deviceMutex.unlock();
 
- 	return ret;
 
- }
 
- HRESULT STDMETHODCALLTYPE
 
- DeckLinkDeviceDiscovery::DeckLinkDeviceArrived(IDeckLink *device)
 
- {
 
- 	DeckLinkDevice *newDev = new DeckLinkDevice(device);
 
- 	if (!newDev->Init()) {
 
- 		delete newDev;
 
- 		return S_OK;
 
- 	}
 
- 	std::lock_guard<std::recursive_mutex> lock(deviceMutex);
 
- 	devices.push_back(newDev);
 
- 	for (DeviceChangeInfo &cb : callbacks)
 
- 		cb.callback(cb.param, newDev, true);
 
- 	return S_OK;
 
- }
 
- HRESULT STDMETHODCALLTYPE
 
- DeckLinkDeviceDiscovery::DeckLinkDeviceRemoved(IDeckLink *device)
 
- {
 
- 	std::lock_guard<std::recursive_mutex> lock(deviceMutex);
 
- 	for (size_t i = 0; i < devices.size(); i++) {
 
- 		if (devices[i]->IsDevice(device)) {
 
- 			for (DeviceChangeInfo &cb : callbacks)
 
- 				cb.callback(cb.param, devices[i], false);
 
- 			devices[i]->Release();
 
- 			devices.erase(devices.begin() + i);
 
- 			break;
 
- 		}
 
- 	}
 
- 	return S_OK;
 
- }
 
- ULONG STDMETHODCALLTYPE DeckLinkDeviceDiscovery::AddRef(void)
 
- {
 
- 	return os_atomic_inc_long(&refCount);
 
- }
 
- HRESULT STDMETHODCALLTYPE DeckLinkDeviceDiscovery::QueryInterface(REFIID iid,
 
- 								  LPVOID *ppv)
 
- {
 
- 	HRESULT result = E_NOINTERFACE;
 
- 	*ppv = nullptr;
 
- 	CFUUIDBytes unknown = CFUUIDGetUUIDBytes(IUnknownUUID);
 
- 	if (memcmp(&iid, &unknown, sizeof(REFIID)) == 0) {
 
- 		*ppv = this;
 
- 		AddRef();
 
- 		result = S_OK;
 
- 	} else if (memcmp(&iid, &IID_IDeckLinkDeviceNotificationCallback,
 
- 			  sizeof(REFIID)) == 0) {
 
- 		*ppv = (IDeckLinkDeviceNotificationCallback *)this;
 
- 		AddRef();
 
- 		result = S_OK;
 
- 	}
 
- 	return result;
 
- }
 
- ULONG STDMETHODCALLTYPE DeckLinkDeviceDiscovery::Release(void)
 
- {
 
- 	const long newRefCount = os_atomic_dec_long(&refCount);
 
- 	if (newRefCount == 0) {
 
- 		delete this;
 
- 		return 0;
 
- 	}
 
- 	return newRefCount;
 
- }
 
 
  |