-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCapturer.cpp
More file actions
302 lines (260 loc) · 9.45 KB
/
Capturer.cpp
File metadata and controls
302 lines (260 loc) · 9.45 KB
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
#include "Capturer.h"
#include "Msg.h"
#include "Params.h"
#include "Guard.h"
#include "SysHandler.h"
#include <iostream>
//-- class Capturer --//
std::unique_ptr<Capturer> Capturer::create()
{
switch(Params()->capturerType) {
case CapturerType::gdi:
Msg(FILELINE) << "Using GDI screen capturer";
return std::make_unique<GdiCapturer>();
case CapturerType::dx:
Msg(FILELINE) << "Using DirectX screen capturer";
return std::make_unique<DxCapturer>();
case CapturerType::null:
Msg(FILELINE) << "Using null screen capturer";
return std::make_unique<NullCapturer>();
}
throw Err(FILELINE) << "No appropriate capturer";
}
Frame Capturer::getNullFrame(const FrameSize & frameSize)
{
Msg(FILELINE, 3) << "Obtaining null frame";
if(frameSize.area() != mFrameBuf.count()) {
Msg(FILELINE, 2) << "(Re)creating null frame buffer";
mFrameBuf = Buffer<Pixel>(frameSize.area());
}
/*
Pixel * p = mFrameBuf;
for(int i = frameSize.area(); i > 0; --i, ++p)
*p = {64, 0, 0, 255};
/**/
memset(mFrameBuf, 64, mFrameBuf.size());
return Frame(frameSize, mFrameBuf);
}
void Capturer::drawCursor(HDC hDC)
{
Msg(FILELINE, 3) << "Obtaining cursor image";
CURSORINFO curInfo;
curInfo.cbSize = sizeof(CURSORINFO);
if(!GetCursorInfo(&curInfo)) {
Msg(FILELINE) << "GetCursorInfo failed, error " << GetLastError();
return;
}
if(curInfo.flags == CURSOR_SHOWING) {
ICONINFO iconInfo;
if(!GetIconInfo(curInfo.hCursor, &iconInfo)) {
Msg(FILELINE) << "GetIconInfo failed, error " << GetLastError();
return;
}
DeleteObject(iconInfo.hbmMask);
DeleteObject(iconInfo.hbmColor);
if(!DrawIcon(hDC,
curInfo.ptScreenPos.x - iconInfo.xHotspot,
curInfo.ptScreenPos.y - iconInfo.yHotspot,
curInfo.hCursor)) {
Msg(FILELINE) << "DrawIcon failed, error " << GetLastError();
return;
}
}
}
//-- class NullCapturer --//
Frame NullCapturer::getFrame()
{
FrameSize frameSize{size_t(GetSystemMetrics(SM_CXSCREEN)),
size_t(GetSystemMetrics(SM_CYSCREEN))};
return getNullFrame(frameSize);
}
//-- class GdiCapturer --//
GdiCapturer::GdiCapturer():
mRecoveryTimeout(3000)
{
}
Frame GdiCapturer::getFrame()
{
FrameSize frameSize{size_t(GetSystemMetrics(SM_CXSCREEN)),
size_t(GetSystemMetrics(SM_CYSCREEN))};
if(!mRecoveryTimeout)
return getNullFrame(frameSize);
if(!mhScreenDC) {
Msg(FILELINE, 2) << "Obtaining screen DC";
mhScreenDC = WndHDC(NULL);
if(!mhScreenDC) {
Msg(FILELINE) << "Could not obtain screen DC";
mRecoveryTimeout.start();
return getNullFrame(frameSize);
}
}
if(mFrame.size != frameSize) {
Msg(FILELINE, 2) << "New frame size, recreating capturer resources";
mhBitmap.release();
mhBitmapDC.release();
mFrame = Frame(frameSize);
}
if(!mhBitmapDC) {
Msg(FILELINE, 2) << "Creating bitmap DC";
mhBitmapDC = CreateCompatibleDC(mhScreenDC->hDC);
if(!mhBitmapDC) {
Msg(FILELINE) << "Could not create bitmap DC";
mRecoveryTimeout.start();
return getNullFrame(frameSize);
}
}
if(!mhBitmap) {
Msg(FILELINE, 2) << "Creating bitmap";
/* Qt 5.6.3's GCC 4.9.2 doesn't like it:
BITMAPINFOHEADER bitmapHdr = {};
/**/
BITMAPINFOHEADER bitmapHdr;
memset(&bitmapHdr, 0, sizeof(bitmapHdr));
/**/
bitmapHdr.biSize = sizeof(BITMAPINFOHEADER);
bitmapHdr.biWidth = frameSize.width;
bitmapHdr.biHeight = -frameSize.height;
bitmapHdr.biPlanes = 1;
bitmapHdr.biBitCount = 32;
bitmapHdr.biCompression = BI_RGB;
mhBitmap = CreateDIBSection(mhBitmapDC, (BITMAPINFO*)&bitmapHdr,
DIB_RGB_COLORS, (void **)&mFrame.pPixels, NULL, 0);
if(!mhBitmap || !mFrame.pPixels) {
Msg(FILELINE) << "Could not create bitmap";
mRecoveryTimeout.start();
return getNullFrame(frameSize);
}
}
Msg(FILELINE, 3) << "Capturing screen image via GDI";
HGDIOBJ_Guard hOldBitmap = SelectObjectGuarded(mhBitmapDC, mhBitmap);
if(!hOldBitmap) {
Msg(FILELINE) << "Could not select screen bitmap into DC";
mRecoveryTimeout.start();
return getNullFrame(frameSize);
}
if(!BitBlt(mhBitmapDC, 0, 0, frameSize.width, frameSize.height,
mhScreenDC->hDC, 0, 0, SRCCOPY | CAPTUREBLT)) {
if(GetLastError() == 6) {
Msg(FILELINE) << "Restoring capturer resources";
mhBitmap.release();
mhBitmapDC.release();
mhScreenDC.release();
return getFrame();
} else {
Msg(FILELINE, 3) << "Could not capture screen image via GDI, error "
<< GetLastError();
// normal under some circumstances
return getNullFrame(frameSize);
}
}
drawCursor(mhBitmapDC);
return mFrame;
}
//-- class DxCapturer --//
DxCapturer::DxCapturer():
mRecoveryTimeout(3000)
{
}
DxCapturer::~DxCapturer()
{
// After Ctrl-C and SIGINT closing D3D handles lead to SIGSEGV
if(SysHandler::isIntRequest()) {
Msg(FILELINE, 2) << "Reset DX resources to prevent SIGSEGV";
mhSurface.reset();
mhDevice.reset();
mhD3D.reset();
}
}
Frame DxCapturer::getFrame()
{
FrameSize frameSize{size_t(GetSystemMetrics(SM_CXSCREEN)),
size_t(GetSystemMetrics(SM_CYSCREEN))};
if(!mRecoveryTimeout)
return getNullFrame(frameSize);
HRESULT hRes;
if(!mhD3D) {
Msg(FILELINE, 2) << "Obtaining D3D9 interface";
mhD3D = Direct3DCreate9(D3D_SDK_VERSION);
if(!mhD3D) {
Msg(FILELINE) << "Could not obtain D3D9 interface";
mRecoveryTimeout.start();
return getNullFrame(frameSize);
}
}
if(mFrame.size != frameSize) {
Msg(FILELINE, 2) << "New frame size, capturer resources will be recreated";
mhSurface.release();
mhDevice.release();
mFrame = Frame(frameSize);
}
if(!mhDevice) {
Msg(FILELINE, 2) << "Creating D3D Device";
/* Qt 5.6.3's GCC 4.9.2 doesn't like it:
D3DPRESENT_PARAMETERS params = {};
/**/
D3DPRESENT_PARAMETERS params;
memset(¶ms, 0, sizeof(params));
/**/
params.Windowed = TRUE;
params.BackBufferCount = 1;
params.BackBufferHeight = mFrame.size.height;
params.BackBufferWidth = mFrame.size.width;
params.SwapEffect = D3DSWAPEFFECT_DISCARD;
params.hDeviceWindow = NULL;
hRes = mhD3D->CreateDevice(
D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, NULL,
D3DCREATE_SOFTWARE_VERTEXPROCESSING, ¶ms, &mhDevice);
if(FAILED(hRes)) {
Msg(FILELINE) << "Could not create D3D Device, error " << hRes;
mRecoveryTimeout.start();
return getNullFrame(frameSize);
}
}
if(!mhSurface) {
Msg(FILELINE, 2) << "Creating offscreen plain surface";
hRes = mhDevice->CreateOffscreenPlainSurface(
mFrame.size.width, mFrame.size.height, D3DFMT_A8R8G8B8,
D3DPOOL_SYSTEMMEM, &mhSurface, nullptr);
if(FAILED(hRes)) {
Msg(FILELINE) << "Could not create offscreen plain surface, error " << hRes;
mRecoveryTimeout.start();
return getNullFrame(frameSize);
}
}
Msg(FILELINE, 3) << "Capturing screen image via GDI";
hRes = mhDevice->GetFrontBufferData(0, mhSurface);
if(FAILED(hRes)) {
Msg(FILELINE, 3) << "Could not capture screen image via DX, error " << hRes;
// normal under some circumstances
return getNullFrame(frameSize);
}
Msg(FILELINE, 3) << "Obtaining offscreen plain surface's DC";
HDC hSurfaceDC;
if(FAILED(mhSurface->GetDC(&hSurfaceDC))) {
Msg(FILELINE) << "Could not obtain offscreen plain surface's DC";
} else {
ScopeGuard releaseDC([this, hSurfaceDC](){
mhSurface->ReleaseDC(hSurfaceDC);
});
drawCursor(hSurfaceDC);
}
Msg(FILELINE, 3) << "Locking offscreen plain surface";
D3DLOCKED_RECT lockRect;
if(FAILED(mhSurface->LockRect(&lockRect, NULL, 0))) {
Msg(FILELINE) << "Could not lock offscreen plain surface";
mRecoveryTimeout.start();
return getNullFrame(frameSize);
}
ScopeGuard unlockGuard([this](){
mhSurface->UnlockRect();
});
mFrame.pitch = lockRect.Pitch;
if(mFrameBuf.size() != mFrame.dataSize()) {
Msg(FILELINE, 2) << "(Re)creating frame buffer";
mFrameBuf = ByteBuffer(mFrame.dataSize());
mFrame.pPixels = reinterpret_cast<Pixel *>(mFrameBuf.pData());
}
Msg(FILELINE, 3) << "Copying bits from surface into frame buffer";
memcpy(mFrame.pPixels, lockRect.pBits, mFrame.dataSize());
return mFrame;
}