-
Notifications
You must be signed in to change notification settings - Fork 1
/
VisionSource.cpp
442 lines (362 loc) · 12.7 KB
/
VisionSource.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/********************************************************************************
Copyright (C) 2012 Hugh Bailey <[email protected]>
Copyright (C) 2012-2013 Muf <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
********************************************************************************/
#include "DatapathPlugin.h"
HWND VisionSource::hConfigWnd = NULL;
bool VisionSource::Init(XElement *data)
{
traceIn(VisionSource::Init);
this->data = data;
UpdateSettings();
XFile file;
if (file.Open(OBSGetPluginDataPath() << TEXT("\\DatapathPlugin\\nosignal.png"), XFILE_READ, XFILE_OPENEXISTING)) // load user-defined image if available
{
file.Close();
noSignalTex = CreateTextureFromFile(OBSGetPluginDataPath() << TEXT("\\DatapathPlugin\\nosignal.png"), TRUE);
}
if (!noSignalTex)
noSignalTex = CreateTextureFromFile(TEXT("plugins\\DatapathPlugin\\nosignal.png"), TRUE); // fallback to default image
if (file.Open(OBSGetPluginDataPath() << TEXT("\\DatapathPlugin\\invalidsignal.png"), XFILE_READ, XFILE_OPENEXISTING)) // load user-defined image if available
{
file.Close();
invalidSignalTex = CreateTextureFromFile(OBSGetPluginDataPath() << TEXT("\\DatapathPlugin\\invalidsignal.png"), TRUE);
}
if (!invalidSignalTex)
invalidSignalTex = CreateTextureFromFile(TEXT("plugins\\DatapathPlugin\\invalidsignal.png"), TRUE); // fallback to default image
SamplerInfo samplerInfo;
samplerInfo.filter = GS_FILTER_POINT;
sampler = CreateSamplerState(samplerInfo);
return true;
traceOut;
}
VisionSource::VisionSource()
{
for (int i = 0; i < NUM_BUFFERS; i++)
lpBitmapInfo[i] = (LPBITMAPINFO)Allocate(sizeof(BITMAPINFOHEADER)+(3*sizeof(COLORREF)));
sharedInfo.pCapturing = &bCapturing;
sharedInfo.pTextures = &pTextures;
sharedInfo.pSharedTextures = &pSharedTextures;
sharedInfo.lpBitmapInfo = &lpBitmapInfo;
sharedInfo.pBitmapBits = &pBitmapBits;
sharedInfo.pBuffers = &Buffers;
sharedInfo.hMutex = OSCreateMutex();
sharedInfo.hDataMutex = OSCreateMutex();
sharedInfo.data = &data;
sharedInfo.hBitmaps = &hBitmaps;
sharedInfo.pSignal = &signal;
lastTex = 0;
dropTex = 0;
}
VisionSource::~VisionSource()
{
traceIn(VisionSource::~VisionSource);
Stop();
if (Buffers)
{
for (unsigned long i = 0; i < Buffers; i++)
{
delete pTextures[i];
//delete noSignalTex;
//delete invalidSignalTex; // crash if i try to do this
Free(lpBitmapInfo[i]);
}
}
OSCloseMutex(sharedInfo.hMutex);
OSCloseMutex(sharedInfo.hDataMutex);
traceOut;
}
void VisionSource::Preprocess() { return; }
void VisionSource::Start()
{
traceIn(VisionSource::Start);
if(bCapturing)
return;
int input = data->GetInt(TEXT("input"));
SIGNALTYPE signalType;
unsigned long dummy;
RGBGetInputSignalType(input, &signalType, &dummy, &dummy, &dummy); // no NULL checking? really Datapath, really?
OSEnterMutex(sharedInfo.hMutex);
switch(signalType)
{
case RGB_SIGNALTYPE_NOSIGNAL:
{
signal = inactive;
break;
}
case RGB_SIGNALTYPE_OUTOFRANGE:
{
signal = invalid;
break;
}
default:
signal = active;
}
zero(&sharedInfo.recvCalled, sizeof(bool)*NUM_BUFFERS);
OSLeaveMutex(sharedInfo.hMutex);
unsigned long err = 0;
err = RGBOpenInput(input, &hRGB);
AppWarning(TEXT("VisionSource: Trying to RGBOpenInput, return value 0x%08lX"), err);
if (RGBERROR_NO_ERROR == err)
{
err = RGBSetFrameDropping(hRGB, 0);
AppWarning(TEXT("VisionSource: Trying to RGBSetFrameDropping, return value 0x%08lX"), err);
if (RGBERROR_NO_ERROR == err)
{
if(data->GetInt(TEXT("cropping")))
{
AppWarning(TEXT("VisionSource: Getting cropping values... yes"));
RGBSetCropping(hRGB, data->GetInt(TEXT("cropTop")), data->GetInt(TEXT("cropLeft")), data->GetInt(TEXT("cropWidth"), 640), data->GetInt(TEXT("cropHeight"), 480));
RGBEnableCropping(hRGB, 1);
}
else AppWarning(TEXT("VisionSource: Getting cropping values... no"));
err = RGBSetFrameCapturedFnEx(hRGB, &Receive, (ULONG_PTR)&sharedInfo);
AppWarning(TEXT("VisionSource: Trying to RGBSetFrameCapturedFnEx, return value 0x%08lX"), err);
if (RGBERROR_NO_ERROR == err)
{
if (Buffers)
{
AppWarning(TEXT("VisionSource: Checking for allocated buffers... yes"));
for (unsigned long i = 0; i < Buffers; i++)
{
err = RGBChainOutputBuffer(hRGB, lpBitmapInfo[i], pBitmapBits[i]);
AppWarning(TEXT("VisionSource: Trying to RGBChainOutputBuffer, return value 0x%08lX"), err);
if (RGBERROR_NO_ERROR != err)
break;
}
err = RGBSetModeChangedFn(hRGB, &ResolutionSwitch, (ULONG_PTR)&sharedInfo);
AppWarning(TEXT("VisionSource: Trying to RGBSetModeChangedFn, return value 0x%08lX"), err);
err = RGBSetNoSignalFn(hRGB, &NoSignal, (ULONG_PTR)&sharedInfo);
AppWarning(TEXT("VisionSource: Trying to RGBSetNoSignalFn, return value 0x%08lX"), err);
err = RGBSetInvalidSignalFn(hRGB, &InvalidSignal, (ULONG_PTR)&sharedInfo);
AppWarning(TEXT("VisionSource: Trying to RGBSetInvalidSignalFn, return value 0x%08lX"), err);
err = RGBUseOutputBuffers(hRGB, TRUE);
AppWarning(TEXT("VisionSource: Trying to RGBUseOutputBuffers, return value 0x%08lX"), err);
if (RGBERROR_NO_ERROR == err)
bCapturing = true;
}
else AppWarning(TEXT("VisionSource: Checking for allocated buffers... no"));
err = RGBStartCapture(hRGB);
AppWarning(TEXT("VisionSource: Trying to RGBStartCapture, return value 0x%08lX"), err);
if (RGBERROR_NO_ERROR == err)
return;
}
}
}
AppWarning(TEXT("Could not open Vision input %i"), input+1);
traceOut;
}
void VisionSource::Stop()
{
traceIn(VisionSource::Stop);
if(!bCapturing)
return;
if (RGBERROR_NO_ERROR == RGBUseOutputBuffers(hRGB, FALSE))
bCapturing = false;
RGBStopCapture(hRGB);
RGBCloseInput(hRGB);
hRGB = 0;
traceOut;
}
void VisionSource::BeginScene()
{
traceIn(VisionSource::BeginScene);
HDC hDC = GetDC(NULL);
for (int i = 0; i < NUM_BUFFERS; i++)
CreateBitmapInformation(lpBitmapInfo[i], renderCX, renderCY, pixFormat);
if (hDC)
{
// allocate textures
UINT pitch = 0;
for(int i = 0; i < NUM_BUFFERS; i++)
{
pSharedTextures[i] = gpD3D9->CreateSharedTexture(lpBitmapInfo[i]->bmiHeader.biWidth, lpBitmapInfo[i]->bmiHeader.biHeight);
pTextures[i] = gpD3D9->CreateTexture(lpBitmapInfo[i]->bmiHeader.biWidth, lpBitmapInfo[i]->bmiHeader.biHeight, pixFmtD3D[pixFormat]);
pTextures[i]->Map(pBitmapBits[i], pitch);
lpBitmapInfo[i]->bmiHeader.biSizeImage = pitch*abs(lpBitmapInfo[i]->bmiHeader.biHeight);
if (pTextures[i] && pSharedTextures[i])
{
Buffers++;
}
else
AppWarning(TEXT("VisionSource: could not create texture"));
}
ReleaseDC (NULL, hDC);
}
Start();
traceOut;
}
void VisionSource::EndScene()
{
traceIn(VisionSource::EndScene);
Stop();
traceOut;
}
void RGBCBKAPI VisionSource::Receive(HWND hWnd, HRGB hRGB, PRGBFRAMEDATA pFrameData, ULONG_PTR userData)
{
SharedVisionInfo *sharedInfo = (SharedVisionInfo*)userData;
OSEnterMutex(sharedInfo->hMutex);
if(*sharedInfo->pCapturing)
{
for (unsigned long i = 0; i < *sharedInfo->pBuffers; i++) // find which buffer we have been passed
{
if ((*sharedInfo->pBitmapBits)[i] == pFrameData->PBitmapBits)
{
if (!sharedInfo->recvCalled[i])
{
sharedInfo->recvCalled[i] = true;
AppWarning(TEXT("VisionSource: Hooray, Receive called on buffer %i!"), i+1);
}
CapturedFrame frame;
(*sharedInfo->pTextures)[i]->Unmap();
gpD3D9->BlitTexture((*sharedInfo->pTextures)[i], (*sharedInfo->pSharedTextures)[i]);
frame.pTexture = (*sharedInfo->pSharedTextures)[i];
if ((pFrameData->PBitmapInfo->biWidth != (*sharedInfo->pTextures)[i]->Width()) || (pFrameData->PBitmapInfo->biHeight != (*sharedInfo->pTextures)[i]->Height()))
{
(*sharedInfo->lpBitmapInfo)[i]->bmiHeader = *pFrameData->PBitmapInfo;
frame.bChanged = true;
}
else
frame.bChanged = false;
(sharedInfo->qFrames).push(frame);
break;
}
}
}
OSLeaveMutex(sharedInfo->hMutex);
}
void VisionSource::Render(const Vect2 &pos, const Vect2 &size)
{
traceIn(VisionSource::Render);
OSEnterMutex(sharedInfo.hMutex);
if (sharedInfo.qFrames.size() > NUM_BUFFERS-1)
{
for (unsigned long i = 0; i < Buffers; i++) // find which texture we have been passed
{
if (pSharedTextures[i] == sharedInfo.qFrames.front().pTexture)
{
UINT pitch = 0;
if(bPointFilter)
LoadSamplerState(sampler, 0);
gpD3D9->Flush();
BlendFunction(GS_BLEND_ONE, GS_BLEND_ZERO);
DrawSprite(pSharedTextures[lastTex]->GSTexture, 0xFFFFFFFF, pos.x, pos.y, pos.x+size.x, pos.y+size.y);
BlendFunction(GS_BLEND_SRCALPHA, GS_BLEND_INVSRCALPHA);
LoadSamplerState(NULL, 0);
if (sharedInfo.qFrames.front().bChanged)
{
delete pTextures[i];
delete pSharedTextures[i];
pSharedTextures[i] = gpD3D9->CreateSharedTexture(lpBitmapInfo[i]->bmiHeader.biWidth, lpBitmapInfo[i]->bmiHeader.biHeight);
pTextures[i] = gpD3D9->CreateTexture(lpBitmapInfo[i]->bmiHeader.biWidth, lpBitmapInfo[i]->bmiHeader.biHeight, pixFmtD3D[pixFormat]);
}
pTextures[i]->Map(pBitmapBits[i], pitch);
RGBChainOutputBuffer(hRGB, lpBitmapInfo[i], pBitmapBits[i]);
dropTex = lastTex;
lastTex = i;
break;
}
}
sharedInfo.qFrames.pop();
}
else switch(signal)
{
case active:
{
DrawSprite(pSharedTextures[dropTex]->GSTexture, 0xFFFFFFFF, pos.x, pos.y, pos.x+size.x, pos.y+size.y);
break;
}
case invalid:
{
DrawSprite(invalidSignalTex, 0xFFFFFFFF, pos.x, pos.y, pos.x+size.x, pos.y+size.y);
break;
}
case inactive:
{
DrawSprite(noSignalTex, 0xFFFFFFFF, pos.x, pos.y, pos.x+size.x, pos.y+size.y);
}
}
OSLeaveMutex(sharedInfo.hMutex);
traceOut;
}
void RGBCBKAPI VisionSource::ResolutionSwitch(HWND hWnd, HRGB hRGB, PRGBMODECHANGEDINFO pModeChangedInfo, ULONG_PTR userData)
{
SharedVisionInfo *sharedInfo = (SharedVisionInfo*)userData;
unsigned long width;
unsigned long height;
OSEnterMutex(sharedInfo->hMutex);
(*sharedInfo->pSignal) = active;
OSLeaveMutex(sharedInfo->hMutex);
if (hConfigWnd)
{
TCHAR modeText[255];
GetModeText((*sharedInfo->data)->GetInt(TEXT("input")), modeText, 255);
SetWindowText(GetDlgItem(hConfigWnd, IDC_DETECTEDMODE), modeText);
}
if ((RGBERROR_NO_ERROR == RGBGetCaptureWidthDefault(hRGB, &width)) && (RGBERROR_NO_ERROR == RGBGetCaptureHeightDefault(hRGB, &height)))
{
RGBSetCaptureWidth(hRGB, width);
RGBSetCaptureHeight(hRGB, height);
// TODO: set renderCX/CY ?
}
}
void RGBCBKAPI VisionSource::NoSignal(HWND hWnd, HRGB hRGB, ULONG_PTR userData)
{
SharedVisionInfo *sharedInfo = (SharedVisionInfo*)userData;
OSEnterMutex(sharedInfo->hMutex);
(*sharedInfo->pSignal) = inactive;
OSLeaveMutex(sharedInfo->hMutex);
}
void RGBCBKAPI VisionSource::InvalidSignal(HWND hWnd, HRGB hRGB, unsigned long horClock, unsigned long verClock, ULONG_PTR userData)
{
SharedVisionInfo *sharedInfo = (SharedVisionInfo*)userData;
OSEnterMutex(sharedInfo->hMutex);
(*sharedInfo->pSignal) = invalid;
OSLeaveMutex(sharedInfo->hMutex);
}
void VisionSource::UpdateSettings()
{
traceIn(VisionSource::UpdateSettings);
int input = data->GetInt(TEXT("input"));
int cropping = data->GetInt(TEXT("cropping"));
int width = 640;
int height = 480;
bool openInput = (hRGB!=0); // use existing handle if available
if (openInput || RGBERROR_NO_ERROR == RGBOpenInput(input, &hRGB))
{
RGBGetCaptureWidthDefault(hRGB, (unsigned long*)&width);
RGBGetCaptureHeightDefault(hRGB, (unsigned long*)&height);
if (!openInput) // don't close a handle we didn't open ourselves
RGBCloseInput(hRGB);
}
if (data->GetInt(TEXT("customRes")))
{
renderCX = data->GetInt(TEXT("resolutionWidth"), width);
renderCY = data->GetInt(TEXT("resolutionHeight"), height);
}
else
{
renderCX = width;
renderCY = height;
}
if (hRGB) // are we (still) capturing?
{
RGBEnableCropping(hRGB, cropping);
if(cropping)
RGBSetCropping(hRGB, data->GetInt(TEXT("cropTop")), data->GetInt(TEXT("cropLeft")), data->GetInt(TEXT("cropWidth"), 640), data->GetInt(TEXT("cropHeight"), 480));
}
bPointFilter = (!!data->GetInt(TEXT("pointFilter"), 1));
pixFormat = (PixelFmt)data->GetInt(TEXT("pixFormat"), 0);
traceOut;
}