File: decklink-device-discovery.cpp

package info (click to toggle)
obs-studio 29.0.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 35,920 kB
  • sloc: ansic: 182,921; cpp: 98,959; sh: 1,591; python: 945; makefile: 858; javascript: 19
file content (131 lines) | stat: -rw-r--r-- 2,773 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "decklink-device-discovery.hpp"
#include "decklink-device.hpp"

#include <util/threading.h>

DeckLinkDeviceDiscovery::DeckLinkDeviceDiscovery()
{
	discovery.Set(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;
}