forked from smasherprog/screen_capture_lite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScreenCapture.cpp
344 lines (319 loc) · 15.9 KB
/
ScreenCapture.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
#include "ScreenCapture.h"
#include "internal/SCCommon.h"
#include "internal/ThreadManager.h"
#include <algorithm>
#include <assert.h>
#include <atomic>
#include <cstring>
#include <iostream>
#include <memory>
#include <span>
#include <thread>
namespace SL {
namespace Screen_Capture {
namespace C_API {
class ICaptureConfigurationScreenCaptureCallbackWrapper {
public:
std::shared_ptr<ICaptureConfiguration<ScreenCaptureCallback>> ptr;
};
class ICaptureConfigurationWindowCaptureCallbackWrapper {
public:
std::shared_ptr<ICaptureConfiguration<WindowCaptureCallback>> ptr;
};
class IScreenCaptureManagerWrapper {
public:
std::shared_ptr<IScreenCaptureManager> ptr;
};
int GetMonitors(Monitor *monitors, int monitors_size)
{
auto local_monitors = Screen_Capture::GetMonitors();
auto maxelements = std::clamp(static_cast<int>(local_monitors.size()), 0, monitors_size);
memcpy(monitors, local_monitors.data(), maxelements * sizeof(Monitor));
return static_cast<int>(local_monitors.size());
}
int GetWindows(Window *windows, int monitors_size)
{
auto local_windows = Screen_Capture::GetWindows();
auto maxelements = std::clamp(static_cast<int>(local_windows.size()), 0, monitors_size);
memcpy(windows, local_windows.data(), maxelements * sizeof(Window));
return static_cast<int>(local_windows.size());
}
}; // namespace C_API
template <class MONITORTPE> bool isMonitorInsideBounds(MONITORTPE monitors, const Monitor &monitor)
{
auto totalwidth = 0;
for (auto &m : monitors) {
totalwidth += Width(m);
}
// if the monitor doesnt exist any more!
if (std::find_if(begin(monitors), end(monitors), [&](auto &m) { return m.Id == monitor.Id; }) == end(monitors)) {
return false;
} // if the area to capture is outside the dimensions of the desktop!!
auto &realmonitor = monitors[Index(monitor)];
if (Height(realmonitor) < Height(monitor) || // monitor height check
totalwidth < Width(monitor) + OffsetX(monitor) || // total width check
Width(monitor) > Width(realmonitor)) // regular width check
{
return false;
} // if the entire screen is capture and the offsets changed, get out and rebuild
else if (Height(realmonitor) == Height(monitor) && Width(realmonitor) == Width(monitor) &&
(OffsetX(realmonitor) != OffsetX(monitor) || OffsetY(realmonitor) != OffsetY(monitor))) {
return false;
}
return true;
}
bool isMonitorInsideBounds(const std::vector<Monitor> &monitors, const Monitor &monitor)
{
return isMonitorInsideBounds(std::span(monitors.data(), monitors.size()), monitor);
}
namespace C_API {
bool isMonitorInsideBounds(const Monitor *monitors, const int monitorsize, const Monitor *monitor)
{
return isMonitorInsideBounds(std::span(monitors, monitorsize), *monitor);
}
}; // namespace C_API
static bool ScreenCaptureManagerExists = false;
class ScreenCaptureManager : public IScreenCaptureManager {
public:
// allreads share the same data!!!
std::shared_ptr<Thread_Data> Thread_Data_;
std::thread Thread_;
ScreenCaptureManager()
{
// you must ONLY HAVE ONE INSTANCE RUNNING AT A TIME. Destroy the first instance then create one!
assert(!ScreenCaptureManagerExists);
ScreenCaptureManagerExists = true;
Thread_Data_ = std::make_shared<Thread_Data>();
Thread_Data_->CommonData_.Paused = false;
Thread_Data_->ScreenCaptureData.FrameTimer = std::make_shared<Timer>(100ms);
Thread_Data_->ScreenCaptureData.MouseTimer = std::make_shared<Timer>(50ms);
Thread_Data_->WindowCaptureData.FrameTimer = std::make_shared<Timer>(100ms);
Thread_Data_->WindowCaptureData.MouseTimer = std::make_shared<Timer>(50ms);
}
virtual ~ScreenCaptureManager()
{
Thread_Data_->CommonData_.TerminateThreadsEvent = true; // set the exit flag for the threads
Thread_Data_->CommonData_.Paused = false; // unpaused the threads to let everything exit
if (Thread_.get_id() == std::this_thread::get_id()) {
Thread_.detach();
}
else if (Thread_.joinable()) {
Thread_.join();
}
ScreenCaptureManagerExists = false;
}
void start()
{
Thread_ = std::thread([&]() {
ThreadManager ThreadMgr;
ThreadMgr.Init(Thread_Data_);
while (!Thread_Data_->CommonData_.TerminateThreadsEvent) {
if (Thread_Data_->CommonData_.ExpectedErrorEvent) {
Thread_Data_->CommonData_.TerminateThreadsEvent = true;
ThreadMgr.Join();
Thread_Data_->CommonData_.ExpectedErrorEvent = Thread_Data_->CommonData_.UnexpectedErrorEvent =
Thread_Data_->CommonData_.TerminateThreadsEvent = false;
// Clean up
std::this_thread::sleep_for(std::chrono::milliseconds(1000)); // sleep for 1 second since an error occcured
ThreadMgr.Init(Thread_Data_);
}
std::this_thread::sleep_for(std::chrono::milliseconds(50));
}
Thread_Data_->CommonData_.TerminateThreadsEvent = true;
ThreadMgr.Join();
});
}
virtual void setFrameChangeInterval(const std::shared_ptr<Timer> &timer) override
{
std::atomic_store(&Thread_Data_->ScreenCaptureData.FrameTimer, timer);
std::atomic_store(&Thread_Data_->WindowCaptureData.FrameTimer, timer);
}
virtual void setMouseChangeInterval(const std::shared_ptr<Timer> &timer) override
{
std::atomic_store(&Thread_Data_->ScreenCaptureData.MouseTimer, timer);
std::atomic_store(&Thread_Data_->WindowCaptureData.MouseTimer, timer);
}
virtual void pause() override { Thread_Data_->CommonData_.Paused = true; }
virtual bool isPaused() const override { return Thread_Data_->CommonData_.Paused; }
virtual void resume() override { Thread_Data_->CommonData_.Paused = false; }
virtual void stop() override { Thread_Data_->CommonData_.TerminateThreadsEvent = true;Thread_Data_->CommonData_.Paused = false; }
};
class ScreenCaptureConfiguration : public ICaptureConfiguration<ScreenCaptureCallback> {
std::shared_ptr<ScreenCaptureManager> Impl_;
public:
ScreenCaptureConfiguration(const std::shared_ptr<ScreenCaptureManager> &impl) : Impl_(impl) {}
virtual std::shared_ptr<ICaptureConfiguration<ScreenCaptureCallback>> onNewFrame(const ScreenCaptureCallback &cb) override
{
assert(cb);
assert(!Impl_->Thread_Data_->ScreenCaptureData.OnNewFrame);
Impl_->Thread_Data_->ScreenCaptureData.OnNewFrame = cb;
return std::make_shared<ScreenCaptureConfiguration>(Impl_);
}
virtual std::shared_ptr<ICaptureConfiguration<ScreenCaptureCallback>> onFrameChanged(const ScreenCaptureCallback &cb) override
{
assert(cb);
assert(!Impl_->Thread_Data_->ScreenCaptureData.OnFrameChanged);
Impl_->Thread_Data_->ScreenCaptureData.OnFrameChanged = cb;
return std::make_shared<ScreenCaptureConfiguration>(Impl_);
}
virtual std::shared_ptr<ICaptureConfiguration<ScreenCaptureCallback>> onMouseChanged(const MouseCallback &cb) override
{
assert(cb);
assert(!Impl_->Thread_Data_->ScreenCaptureData.OnMouseChanged);
Impl_->Thread_Data_->ScreenCaptureData.OnMouseChanged = cb;
return std::make_shared<ScreenCaptureConfiguration>(Impl_);
}
virtual std::shared_ptr<IScreenCaptureManager> start_capturing() override
{
assert(Impl_->Thread_Data_->ScreenCaptureData.OnMouseChanged || Impl_->Thread_Data_->ScreenCaptureData.OnFrameChanged ||
Impl_->Thread_Data_->ScreenCaptureData.OnNewFrame);
Impl_->start();
return Impl_;
}
};
namespace C_API {
void MonitoronNewFrame(ICaptureConfigurationScreenCaptureCallbackWrapper *ptr, C_API_ScreenCaptureCallback cb)
{
ptr->ptr = ptr->ptr->onNewFrame(cb);
}
void MonitoronFrameChanged(ICaptureConfigurationScreenCaptureCallbackWrapper *ptr, C_API_ScreenCaptureCallback cb)
{
ptr->ptr = ptr->ptr->onFrameChanged(cb);
}
void MonitoronMouseChanged(ICaptureConfigurationScreenCaptureCallbackWrapper *ptr, C_API_MouseCaptureCallback cb)
{
ptr->ptr = ptr->ptr->onMouseChanged(cb);
}
IScreenCaptureManagerWrapper *Monitorstart_capturing(ICaptureConfigurationScreenCaptureCallbackWrapper *ptr)
{
auto p = new IScreenCaptureManagerWrapper{ptr->ptr->start_capturing()};
FreeMonitorCaptureConfiguration(ptr);
return p;
}
void setFrameChangeInterval(IScreenCaptureManagerWrapper *ptr, int milliseconds)
{
ptr->ptr->setFrameChangeInterval(std::chrono::milliseconds(milliseconds));
}
void setMouseChangeInterval(IScreenCaptureManagerWrapper *ptr, int milliseconds)
{
ptr->ptr->setMouseChangeInterval(std::chrono::milliseconds(milliseconds));
}
void pausecapturing(IScreenCaptureManagerWrapper *ptr) { ptr->ptr->pause(); }
bool isPaused(IScreenCaptureManagerWrapper *ptr) { return ptr->ptr->isPaused(); }
void resume(IScreenCaptureManagerWrapper *ptr) { ptr->ptr->resume(); }
void stop(IScreenCaptureManagerWrapper *ptr) { ptr->ptr->stop(); }
void FreeIScreenCaptureManagerWrapper(IScreenCaptureManagerWrapper *ptr) { delete ptr; }
void WindowonNewFrame(ICaptureConfigurationWindowCaptureCallbackWrapper *ptr, C_API_WindowCaptureCallback cb)
{
ptr->ptr = ptr->ptr->onNewFrame(cb);
}
void WindowonFrameChanged(ICaptureConfigurationWindowCaptureCallbackWrapper *ptr, C_API_WindowCaptureCallback cb)
{
ptr->ptr = ptr->ptr->onFrameChanged(cb);
}
void WindowonMouseChanged(ICaptureConfigurationWindowCaptureCallbackWrapper *ptr, C_API_MouseCaptureCallback cb)
{
ptr->ptr = ptr->ptr->onMouseChanged(cb);
}
IScreenCaptureManagerWrapper *Windowstart_capturing(ICaptureConfigurationWindowCaptureCallbackWrapper *ptr)
{
auto p = new IScreenCaptureManagerWrapper{ptr->ptr->start_capturing()};
FreeWindowCaptureConfiguration(ptr);
return p;
}
}; // namespace C_API
class WindowCaptureConfiguration : public ICaptureConfiguration<WindowCaptureCallback> {
std::shared_ptr<ScreenCaptureManager> Impl_;
public:
WindowCaptureConfiguration(const std::shared_ptr<ScreenCaptureManager> &impl) : Impl_(impl) {}
virtual std::shared_ptr<ICaptureConfiguration<WindowCaptureCallback>> onNewFrame(const WindowCaptureCallback &cb) override
{
assert(!Impl_->Thread_Data_->WindowCaptureData.OnNewFrame);
Impl_->Thread_Data_->WindowCaptureData.OnNewFrame = cb;
return std::make_shared<WindowCaptureConfiguration>(Impl_);
}
virtual std::shared_ptr<ICaptureConfiguration<WindowCaptureCallback>> onFrameChanged(const WindowCaptureCallback &cb) override
{
assert(!Impl_->Thread_Data_->WindowCaptureData.OnFrameChanged);
Impl_->Thread_Data_->WindowCaptureData.OnFrameChanged = cb;
return std::make_shared<WindowCaptureConfiguration>(Impl_);
}
virtual std::shared_ptr<ICaptureConfiguration<WindowCaptureCallback>> onMouseChanged(const MouseCallback &cb) override
{
assert(!Impl_->Thread_Data_->WindowCaptureData.OnMouseChanged);
Impl_->Thread_Data_->WindowCaptureData.OnMouseChanged = cb;
return std::make_shared<WindowCaptureConfiguration>(Impl_);
}
virtual std::shared_ptr<IScreenCaptureManager> start_capturing() override
{
assert(Impl_->Thread_Data_->WindowCaptureData.OnMouseChanged || Impl_->Thread_Data_->WindowCaptureData.OnFrameChanged ||
Impl_->Thread_Data_->WindowCaptureData.OnNewFrame);
Impl_->start();
return Impl_;
}
};
namespace C_API {
ICaptureConfigurationScreenCaptureCallbackWrapper *CreateMonitorCaptureConfiguration(C_API_MonitorCallback monitorstocapture)
{
auto p = new ICaptureConfigurationScreenCaptureCallbackWrapper();
static auto maxbuffersize = 16;
p->ptr = Screen_Capture::CreateCaptureConfiguration([=]() {
std::vector<Monitor> buffer;
auto sizeguess = maxbuffersize;
buffer.resize(sizeguess);
auto sizeneeded = monitorstocapture(buffer.data(), sizeguess);
if (sizeguess < sizeneeded) {
sizeguess = sizeneeded;
maxbuffersize = std::max(sizeneeded, maxbuffersize);
buffer.resize(sizeguess);
sizeneeded = monitorstocapture(buffer.data(), sizeguess);
sizeguess = std::min(sizeguess, sizeneeded);
buffer.resize(sizeguess);
return buffer;
}
buffer.resize(sizeneeded);
return buffer;
});
return p;
}
ICaptureConfigurationWindowCaptureCallbackWrapper *CreateWindowCaptureConfiguration(C_API_WindowCallback monitorstocapture)
{
auto p = new ICaptureConfigurationWindowCaptureCallbackWrapper();
static auto maxbuffersize = 16;
p->ptr = Screen_Capture::CreateCaptureConfiguration([=]() {
std::vector<Window> buffer;
auto sizeguess = maxbuffersize;
buffer.resize(sizeguess);
auto sizeneeded = monitorstocapture(buffer.data(), sizeguess);
if (sizeguess < sizeneeded) {
sizeguess = sizeneeded;
maxbuffersize = std::max(sizeneeded, maxbuffersize);
buffer.resize(sizeguess);
sizeneeded = monitorstocapture(buffer.data(), sizeguess);
sizeguess = std::min(sizeguess, sizeneeded);
buffer.resize(sizeguess);
return buffer;
}
buffer.resize(sizeneeded);
return buffer;
});
return p;
}
void FreeMonitorCaptureConfiguration(ICaptureConfigurationScreenCaptureCallbackWrapper *ptr) { delete ptr; }
void FreeWindowCaptureConfiguration(ICaptureConfigurationWindowCaptureCallbackWrapper *ptr) { delete ptr; }
}; // namespace C_API
std::shared_ptr<ICaptureConfiguration<ScreenCaptureCallback>> CreateCaptureConfiguration(const MonitorCallback &monitorstocapture)
{
assert(monitorstocapture);
auto impl = std::make_shared<ScreenCaptureManager>();
impl->Thread_Data_->ScreenCaptureData.getThingsToWatch = monitorstocapture;
return std::make_shared<ScreenCaptureConfiguration>(impl);
}
std::shared_ptr<ICaptureConfiguration<WindowCaptureCallback>> CreateCaptureConfiguration(const WindowCallback &windowtocapture)
{
auto impl = std::make_shared<ScreenCaptureManager>();
impl->Thread_Data_->WindowCaptureData.getThingsToWatch = windowtocapture;
return std::make_shared<WindowCaptureConfiguration>(impl);
}
} // namespace Screen_Capture
} // namespace SL