-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathCaptureDelegate.cpp
295 lines (223 loc) · 8.01 KB
/
CaptureDelegate.cpp
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/**
* CaptureDelegate represents the Interface to the DeckLinkAPI
*/
#include <assert.h>
#include <array>
#include <stdio.h>
#include <iostream>
#include <algorithm>
#include "CaptureDelegate.h"
#include "SubDeviceUtil.h"
#define LLOG(x) LOG(x) << "CaptureDelegate: "
CaptureDelegate::CaptureDelegate(IDeckLink* deckLink, IDeckLinkInput *deckLinkInput) :
m_refCount(1),
m_deckLinkParent(nullptr),
m_deckLinkParentReleaser(&m_deckLinkParent),
m_deckLinkParentDeviceConfiguration(nullptr),
m_deckLinkParentDeviceConfigurationReleaser(&m_deckLinkParentDeviceConfiguration),
m_deckLink(deckLink),
m_deckLinkInput(deckLinkInput),
m_deckLinkAttributes(nullptr),
m_deckLinkAttributesReleaser(&m_deckLinkAttributes),
m_deckLinkConfiguration(nullptr),
m_deckLinkConfigurationReleaser(&m_deckLinkConfiguration),
m_lastFrame(nullptr),
m_lastFrameReleaser(&m_lastFrame),
m_hasSignal(false),
m_detectedMode(""),
m_pixelFormat(0),
m_activeConnection(0)
{
setDuplexToHalfDuplexModeIfSupported();
m_deckLinkConfiguration = queryConfigurationInterface();
m_deckLinkAttributes = queryAttributesInterface();
m_decklinkConnections = queryInputConnections();
}
IDeckLinkConfiguration* CaptureDelegate::queryConfigurationInterface()
{
LLOG(INFO) << __PRETTY_FUNCTION__;
HRESULT result;
IDeckLinkConfiguration* deckLinkConfiguration = nullptr;
result = m_deckLink->QueryInterface(IID_IDeckLinkConfiguration, (void **)&deckLinkConfiguration);
throwIfNotOk(result, "Could not obtain the IID_IDeckLinkConfiguration interface");
return deckLinkConfiguration;
}
IDeckLinkAttributes* CaptureDelegate::queryAttributesInterface()
{
LLOG(INFO) << __PRETTY_FUNCTION__;
HRESULT result;
IDeckLinkAttributes* deckLinkAttributes = nullptr;
result = m_deckLink->QueryInterface(IID_IDeckLinkAttributes, (void **)&deckLinkAttributes);
throwIfNotOk(result, "Could not obtain the IID_IDeckLinkAttributes interface");
return deckLinkAttributes;
}
int64_t CaptureDelegate::queryInputConnections()
{
LLOG(INFO) << __PRETTY_FUNCTION__;
HRESULT result;
int64_t connections;
result = m_deckLinkAttributes->GetInt(BMDDeckLinkVideoInputConnections, &connections);
throwIfNotOk(result, "Could not obtain the list of input connections");
return connections;
}
void CaptureDelegate::setDuplexToHalfDuplexModeIfSupported()
{
LLOG(INFO) << __PRETTY_FUNCTION__;
if(SubDeviceUtil::SupportsDuplexMode(m_deckLink))
{
LLOG(DEBUG) << "This device supports Duplex-Mode, setting to Half-Duplex";
setDuplexToHalfDuplexMode(m_deckLink);
}
else
{
LLOG(DEBUG) << "This device does not support Duplex-Mode, querying for a Parent-Device";
m_deckLinkParent = SubDeviceUtil::QueryParentDevice(m_deckLink);
if(m_deckLinkParent != nullptr) {
LLOG(DEBUG) << "Parent-Device found, setting Parent-Device to Half-Duplex";
setDuplexToHalfDuplexMode(m_deckLinkParent);
}
}
}
void CaptureDelegate::setDuplexToHalfDuplexMode(IDeckLink *deckLink)
{
LLOG(INFO) << __PRETTY_FUNCTION__;
HRESULT result;
result = deckLink->QueryInterface(IID_IDeckLinkConfiguration, (void **)&m_deckLinkParentDeviceConfiguration);
throwIfNotOk(result, "Could not obtain the IID_IDeckLinkConfiguration interface");
result = m_deckLinkParentDeviceConfiguration->SetInt(bmdDeckLinkConfigDuplexMode, bmdDuplexModeHalf);
throwIfNotOk(result, "Setting duplex mode failed");
}
void CaptureDelegate::Start()
{
LLOG(INFO) << __PRETTY_FUNCTION__;
HRESULT result;
IDeckLinkDisplayMode* displayMode = queryFirstDisplayMode();
RefReleaser<IDeckLinkDisplayMode> displayModeReleaser(&displayMode);
LLOG(DEBUG) << "Setting Callback to *this*";
m_deckLinkInput->SetCallback(this);
LLOG(DEBUG) << "Enabling Video-Input";
result = m_deckLinkInput->EnableVideoInput(displayMode->GetDisplayMode(), PIXEL_FORMAT, VIDEO_INPUT_FLAGS);
throwIfNotOk(result, "Failed to enable video input. Is another application using the card?");
result = m_deckLinkInput->EnableAudioInput(AUDIO_SAMPLE_RATE, AUDIO_SAMPLE_DEPTH, AUDIO_CHANNELS);
throwIfNotOk(result, "Failed to enable audio-input");
SelectNextConnection();
result = m_deckLinkInput->StartStreams();
throwIfNotOk(result, "Failed to enable video input. Is another application using the card?");
}
void CaptureDelegate::Stop()
{
m_deckLinkInput->StopStreams();
m_deckLinkInput->DisableAudioInput();
m_deckLinkInput->DisableVideoInput();
m_deckLinkInput->SetCallback(nullptr);
}
IDeckLinkDisplayMode* CaptureDelegate::queryFirstDisplayMode()
{
HRESULT result;
IDeckLinkDisplayModeIterator* displayModeIterator = nullptr;
RefReleaser<IDeckLinkDisplayModeIterator> displayModeIteratorReleaser(&displayModeIterator);
IDeckLinkDisplayMode* displayMode = nullptr;
RefReleaser<IDeckLinkDisplayMode> displayModeReleaser(&displayMode);
result = m_deckLinkInput->GetDisplayModeIterator(&displayModeIterator);
throwIfNotOk(result, "Failed to get Display-Mode Iterator");
// just select first display-mode to start auto-detection from
result = displayModeIterator->Next(&displayMode);
throwIfNotOk(result, "Failed to get Display-Mode from Iterator");
displayMode->AddRef();
return displayMode;
}
void CaptureDelegate::SelectNextConnection()
{
std::array<BMDVideoConnection, 3> relevantConnections = {
bmdVideoConnectionSDI,
bmdVideoConnectionHDMI,
bmdVideoConnectionOpticalSDI,
};
auto currentConnectionIt = std::find(relevantConnections.begin(), relevantConnections.end(), m_activeConnection);
while(true)
{
if(currentConnectionIt == relevantConnections.end())
{
currentConnectionIt = relevantConnections.begin();
}
else {
currentConnectionIt++;
if(currentConnectionIt == relevantConnections.end())
{
currentConnectionIt = relevantConnections.begin();
}
}
BMDVideoConnection nextConnection = *currentConnectionIt;
if(m_decklinkConnections & nextConnection)
{
m_activeConnection = nextConnection;
break;
}
}
m_deckLinkConfiguration->SetInt(bmdDeckLinkConfigVideoInputConnection, m_activeConnection);
}
BMDVideoConnection CaptureDelegate::querySelectedConnection()
{
int64_t activeConnection;
m_deckLinkConfiguration->GetInt(bmdDeckLinkConfigVideoInputConnection, &activeConnection);
return activeConnection;
}
HRESULT CaptureDelegate::VideoInputFrameArrived(IDeckLinkVideoInputFrame* videoFrame, UNUSED IDeckLinkAudioInputPacket* audioFrame)
{
if (videoFrame == NULL || videoFrame->GetFlags() & bmdFrameHasNoInputSource)
{
m_hasSignal = false;
}
else
{
m_hasSignal = true;
if(m_lastFrame) {
m_lastFrame->Release();
}
m_pixelFormat = videoFrame->GetPixelFormat();
m_lastFrame = videoFrame;
m_lastFrame->AddRef();
// it sometimes happens, that the switch to another connection is ignored when, just at this time,
// a signal arrives. Make sure to always report the correct selected interface.
m_activeConnection = querySelectedConnection();
}
return S_OK;
}
HRESULT CaptureDelegate::VideoInputFormatChanged(UNUSED BMDVideoInputFormatChangedEvents events, IDeckLinkDisplayMode *mode, BMDDetectedVideoInputFormatFlags formatFlags)
{
HRESULT result;
char* displayModeName = NULL;
bool isRgb = formatFlags & bmdDetectedVideoInputRGB444;
mode->GetName((const char**)&displayModeName);
m_detectedMode = displayModeName;
m_detectedMode += " ";
m_detectedMode += (isRgb ? "RGB" : "YUV");
if (displayModeName)
free(displayModeName);
BMDPixelFormat pixelFormat = (isRgb ? bmdFormat10BitRGB : bmdFormat10BitYUV);
m_deckLinkInput->StopStreams();
result = m_deckLinkInput->EnableVideoInput(mode->GetDisplayMode(), pixelFormat, VIDEO_INPUT_FLAGS);
if (result != S_OK)
{
std::cerr << "Unable to Switch to new Video-Format" << std::endl;
exit(1);
}
m_deckLinkInput->StartStreams();
return S_OK;
}
ULONG CaptureDelegate::AddRef()
{
ULONG newRefValue = __sync_add_and_fetch(&m_refCount, 1);
LLOG(DEBUG2) << __PRETTY_FUNCTION__ << " newRefValue = " << newRefValue;
return newRefValue;
}
ULONG CaptureDelegate::Release()
{
ULONG newRefValue = __sync_sub_and_fetch(&m_refCount, 1);
LLOG(DEBUG2) << __PRETTY_FUNCTION__ << " newRefValue = " << newRefValue;
if (newRefValue == 0)
{
delete this;
}
return newRefValue;
}