File: decklink-device.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 (266 lines) | stat: -rw-r--r-- 6,197 bytes parent folder | download
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#include <sstream>

#include "decklink-device.hpp"

#include <util/threading.h>

DeckLinkDevice::DeckLinkDevice(IDeckLink *device_) : device(device_) {}

DeckLinkDevice::~DeckLinkDevice(void)
{
	for (DeckLinkDeviceMode *mode : inputModes)
		delete mode;

	for (DeckLinkDeviceMode *mode : outputModes)
		delete mode;
}

ULONG DeckLinkDevice::AddRef()
{
	return os_atomic_inc_long(&refCount);
}

ULONG DeckLinkDevice::Release()
{
	long ret = os_atomic_dec_long(&refCount);
	if (ret == 0)
		delete this;
	return ret;
}

bool DeckLinkDevice::Init()
{
	ComPtr<IDeckLinkProfileAttributes> attributes;
	const HRESULT result = device->QueryInterface(
		IID_IDeckLinkProfileAttributes, (void **)&attributes);
	if (result == S_OK) {
		decklink_bool_t detectable = false;
		if (attributes->GetFlag(BMDDeckLinkSupportsInputFormatDetection,
					&detectable) == S_OK &&
		    !!detectable) {
			DeckLinkDeviceMode *mode =
				new DeckLinkDeviceMode("Auto", MODE_ID_AUTO);
			inputModes.push_back(mode);
			inputModeIdMap[MODE_ID_AUTO] = mode;
		}
	}

	// Find input modes
	ComPtr<IDeckLinkInput> input;
	if (device->QueryInterface(IID_IDeckLinkInput, (void **)&input) ==
	    S_OK) {
		ComPtr<IDeckLinkDisplayModeIterator> modeIterator;
		if (input->GetDisplayModeIterator(&modeIterator) == S_OK) {
			ComPtr<IDeckLinkDisplayMode> displayMode;
			long long modeId = 1;

			while (modeIterator->Next(&displayMode) == S_OK) {
				if (displayMode == nullptr)
					continue;

				DeckLinkDeviceMode *mode =
					new DeckLinkDeviceMode(displayMode,
							       modeId);
				inputModes.push_back(mode);
				inputModeIdMap[modeId] = mode;
				++modeId;
			}
		}
	}

	// Get supported video connections
	attributes->GetInt(BMDDeckLinkVideoInputConnections,
			   &supportedVideoInputConnections);
	attributes->GetInt(BMDDeckLinkVideoOutputConnections,
			   &supportedVideoOutputConnections);

	// Get supported audio connections
	attributes->GetInt(BMDDeckLinkAudioInputConnections,
			   &supportedAudioInputConnections);
	attributes->GetInt(BMDDeckLinkAudioOutputConnections,
			   &supportedAudioOutputConnections);

	// find output modes
	ComPtr<IDeckLinkOutput> output;
	if (device->QueryInterface(IID_IDeckLinkOutput, (void **)&output) ==
	    S_OK) {

		ComPtr<IDeckLinkDisplayModeIterator> modeIterator;
		if (output->GetDisplayModeIterator(&modeIterator) == S_OK) {
			ComPtr<IDeckLinkDisplayMode> displayMode;
			long long modeId = 1;

			while (modeIterator->Next(&displayMode) == S_OK) {
				if (displayMode == nullptr)
					continue;

				DeckLinkDeviceMode *mode =
					new DeckLinkDeviceMode(displayMode,
							       modeId);
				outputModes.push_back(mode);
				outputModeIdMap[modeId] = mode;
				++modeId;
			}
		}
	}

	// get keyer support
	attributes->GetFlag(BMDDeckLinkSupportsExternalKeying,
			    &supportsExternalKeyer);
	attributes->GetFlag(BMDDeckLinkSupportsInternalKeying,
			    &supportsInternalKeyer);

	// Sub Device Counts
	attributes->GetInt(BMDDeckLinkSubDeviceIndex, &subDeviceIndex);
	attributes->GetInt(BMDDeckLinkNumberOfSubDevices, &numSubDevices);

	decklink_string_t decklinkModelName;
	decklink_string_t decklinkDisplayName;

	if (device->GetModelName(&decklinkModelName) != S_OK)
		return false;
	DeckLinkStringToStdString(decklinkModelName, name);

	if (device->GetDisplayName(&decklinkDisplayName) != S_OK)
		return false;
	DeckLinkStringToStdString(decklinkDisplayName, displayName);

	hash = displayName;

	if (result != S_OK)
		return true;

	int64_t channels;
	/* Intensity Shuttle for Thunderbolt return 2; however, it supports 8 channels */
	if (name == "Intensity Shuttle Thunderbolt")
		maxChannel = 8;
	else if (attributes->GetInt(BMDDeckLinkMaximumAudioChannels,
				    &channels) == S_OK)
		maxChannel = (int32_t)channels;
	else
		maxChannel = 2;

	/* http://forum.blackmagicdesign.com/viewtopic.php?f=12&t=33967
	 * BMDDeckLinkTopologicalID for older devices
	 * BMDDeckLinkPersistentID for newer ones */

	int64_t value;
	if (attributes->GetInt(BMDDeckLinkPersistentID, &value) != S_OK &&
	    attributes->GetInt(BMDDeckLinkTopologicalID, &value) != S_OK)
		return true;

	std::ostringstream os;
	os << value << "_" << name;
	hash = os.str();
	return true;
}

bool DeckLinkDevice::GetInput(IDeckLinkInput **input)
{
	if (device->QueryInterface(IID_IDeckLinkInput, (void **)input) != S_OK)
		return false;
	return true;
}

bool DeckLinkDevice::GetOutput(IDeckLinkOutput **output)
{
	if (device->QueryInterface(IID_IDeckLinkOutput, (void **)output) !=
	    S_OK)
		return false;

	return true;
}

bool DeckLinkDevice::GetKeyer(IDeckLinkKeyer **deckLinkKeyer)
{
	if (device->QueryInterface(IID_IDeckLinkKeyer,
				   (void **)deckLinkKeyer) != S_OK) {
		fprintf(stderr,
			"Could not obtain the IDeckLinkKeyer interface\n");
		return false;
	}

	return true;
}

void DeckLinkDevice::SetKeyerMode(int newKeyerMode)
{
	keyerMode = newKeyerMode;
}

int DeckLinkDevice::GetKeyerMode(void)
{
	return keyerMode;
}

DeckLinkDeviceMode *DeckLinkDevice::FindInputMode(long long id)
{
	return inputModeIdMap[id];
}

DeckLinkDeviceMode *DeckLinkDevice::FindOutputMode(long long id)
{
	return outputModeIdMap[id];
}

const std::string &DeckLinkDevice::GetDisplayName(void)
{
	return displayName;
}

const std::string &DeckLinkDevice::GetHash(void) const
{
	return hash;
}

const std::vector<DeckLinkDeviceMode *> &
DeckLinkDevice::GetInputModes(void) const
{
	return inputModes;
}

const std::vector<DeckLinkDeviceMode *> &
DeckLinkDevice::GetOutputModes(void) const
{
	return outputModes;
}

int64_t DeckLinkDevice::GetVideoInputConnections()
{
	return supportedVideoInputConnections;
}

int64_t DeckLinkDevice::GetAudioInputConnections()
{
	return supportedAudioInputConnections;
}

bool DeckLinkDevice::GetSupportsExternalKeyer(void) const
{
	return supportsExternalKeyer;
}

bool DeckLinkDevice::GetSupportsInternalKeyer(void) const
{
	return supportsInternalKeyer;
}

int64_t DeckLinkDevice::GetSubDeviceCount()
{
	return numSubDevices;
}

int64_t DeckLinkDevice::GetSubDeviceIndex()
{
	return subDeviceIndex;
}

const std::string &DeckLinkDevice::GetName(void) const
{
	return name;
}

int32_t DeckLinkDevice::GetMaxChannel(void) const
{
	return maxChannel;
}