forked from openstf/minicap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminicap.cpp
More file actions
557 lines (471 loc) · 14 KB
/
minicap.cpp
File metadata and controls
557 lines (471 loc) · 14 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
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
#include <fcntl.h>
#include <getopt.h>
#include <linux/fb.h>
#include <stdlib.h>
#include <unistd.h>
#include <signal.h>
#include <sys/socket.h>
#include <thread>
#include <cmath>
#include <condition_variable>
#include <chrono>
#include <future>
#include <iostream>
#include <mutex>
#include <thread>
#include <Minicap.hpp>
#include "util/debug.h"
#include "JpgEncoder.hpp"
#include "SimpleServer.hpp"
#include "Projection.hpp"
#define BANNER_VERSION 1
#define BANNER_SIZE 24
#define DEFAULT_SOCKET_NAME "minicap"
#define DEFAULT_DISPLAY_ID 0
#define DEFAULT_JPG_QUALITY 80
enum {
QUIRK_DUMB = 1,
QUIRK_ALWAYS_UPRIGHT = 2,
QUIRK_TEAR = 4,
};
static void
usage(const char* pname) {
fprintf(stderr,
"Usage: %s [-h] [-n <name>]\n"
" -d <id>: Display ID. (%d)\n"
" -n <name>: Change the name of the abtract unix domain socket. (%s)\n"
" -P <value>: Display projection (<w>x<h>@<w>x<h>/{0|90|180|270}).\n"
" -Q <value>: JPEG quality (0-100).\n"
" -s: Take a screenshot and output it to stdout. Needs -P.\n"
" -S: Skip frames when they cannot be consumed quickly enough.\n"
" -r <value>: Frame rate (frames/s)"
" -t: Attempt to get the capture method running, then exit.\n"
" -i: Get display information in JSON format. May segfault.\n"
" -h: Show help.\n",
pname, DEFAULT_DISPLAY_ID, DEFAULT_SOCKET_NAME
);
}
class FrameWaiter: public Minicap::FrameAvailableListener {
public:
FrameWaiter()
: mPendingFrames(0),
mTimeout(std::chrono::milliseconds(100)),
mStopped(false) {
}
int
waitForFrame() {
std::unique_lock<std::mutex> lock(mMutex);
while (!mStopped) {
if (mCondition.wait_for(lock, mTimeout, [this]{return mPendingFrames > 0;})) {
return mPendingFrames--;
}
}
return 0;
}
void
reportExtraConsumption(int count) {
std::unique_lock<std::mutex> lock(mMutex);
mPendingFrames -= count;
}
void
onFrameAvailable() {
std::unique_lock<std::mutex> lock(mMutex);
mPendingFrames += 1;
mCondition.notify_one();
}
void
stop() {
mStopped = true;
}
bool
isStopped() {
return mStopped;
}
private:
std::mutex mMutex;
std::condition_variable mCondition;
std::chrono::milliseconds mTimeout;
int mPendingFrames;
bool mStopped;
};
static int
pumps(int fd, unsigned char* data, size_t length) {
do {
// Make sure that we don't generate a SIGPIPE even if the socket doesn't
// exist anymore. We'll still get an EPIPE which is perfect.
int wrote = send(fd, data, length, MSG_NOSIGNAL);
if (wrote < 0) {
return wrote;
}
data += wrote;
length -= wrote;
}
while (length > 0);
return 0;
}
static int
pumpf(int fd, unsigned char* data, size_t length) {
do {
int wrote = write(fd, data, length);
if (wrote < 0) {
return wrote;
}
data += wrote;
length -= wrote;
}
while (length > 0);
return 0;
}
static int
putUInt32LE(unsigned char* data, int value) {
data[0] = (value & 0x000000FF) >> 0;
data[1] = (value & 0x0000FF00) >> 8;
data[2] = (value & 0x00FF0000) >> 16;
data[3] = (value & 0xFF000000) >> 24;
return 0;
}
static int
try_get_framebuffer_display_info(uint32_t displayId, Minicap::DisplayInfo* info) {
char path[64];
sprintf(path, "/dev/graphics/fb%d", displayId);
int fd = open(path, O_RDONLY);
if (fd < 0) {
MCERROR("Cannot open %s", path);
return -1;
}
fb_var_screeninfo vinfo;
if (ioctl(fd, FBIOGET_VSCREENINFO, &vinfo) < 0) {
close(fd);
MCERROR("Cannot get FBIOGET_VSCREENINFO of %s", path);
return -1;
}
info->width = vinfo.xres;
info->height = vinfo.yres;
info->orientation = vinfo.rotate;
info->xdpi = static_cast<float>(vinfo.xres) / static_cast<float>(vinfo.width) * 25.4;
info->ydpi = static_cast<float>(vinfo.yres) / static_cast<float>(vinfo.height) * 25.4;
info->size = std::sqrt(
(static_cast<float>(vinfo.width) * static_cast<float>(vinfo.width)) +
(static_cast<float>(vinfo.height) * static_cast<float>(vinfo.height))) / 25.4;
info->density = std::sqrt(
(static_cast<float>(vinfo.xres) * static_cast<float>(vinfo.xres)) +
(static_cast<float>(vinfo.yres) * static_cast<float>(vinfo.yres))) / info->size;
info->secure = false;
info->fps = 0;
close(fd);
return 0;
}
static FrameWaiter gWaiter;
static void
signal_handler(int signum) {
switch (signum) {
case SIGINT:
MCINFO("Received SIGINT, stopping");
gWaiter.stop();
break;
case SIGTERM:
MCINFO("Received SIGTERM, stopping");
gWaiter.stop();
break;
default:
abort();
break;
}
}
int
main(int argc, char* argv[]) {
const char* pname = argv[0];
const char* sockname = DEFAULT_SOCKET_NAME;
uint32_t displayId = DEFAULT_DISPLAY_ID;
unsigned int quality = DEFAULT_JPG_QUALITY;
int framePeriodMs = 0;
bool showInfo = false;
bool takeScreenshot = false;
bool skipFrames = false;
bool testOnly = false;
Projection proj;
int opt;
while ((opt = getopt(argc, argv, "d:n:P:Q:r:siSth")) != -1) {
float frameRate;
switch (opt) {
case 'd':
displayId = atoi(optarg);
break;
case 'n':
sockname = optarg;
break;
case 'P': {
Projection::Parser parser;
if (!parser.parse(proj, optarg, optarg + strlen(optarg))) {
std::cerr << "ERROR: invalid format for -P, need <w>x<h>@<w>x<h>/{0|90|180|270}" << std::endl;
return EXIT_FAILURE;
}
break;
}
case 'Q':
quality = atoi(optarg);
break;
case 's':
takeScreenshot = true;
break;
case 'i':
showInfo = true;
break;
case 'S':
skipFrames = true;
break;
case 'r':
frameRate = atof(optarg);
if(frameRate <= 0.0) {
MCINFO("Invalid framerate '%s', expecting a float > 0", optarg);
return EXIT_FAILURE;
} else {
framePeriodMs = 1000/frameRate;
skipFrames = true;
MCINFO("framerate: %.2f (period %d ms)", frameRate, framePeriodMs);
}
break;
case 't':
testOnly = true;
break;
case 'h':
usage(pname);
return EXIT_SUCCESS;
case '?':
default:
usage(pname);
return EXIT_FAILURE;
}
}
// Set up signal handler.
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = signal_handler;
sigemptyset(&sa.sa_mask);
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
// Start Android's thread pool so that it will be able to serve our requests.
minicap_start_thread_pool();
if (showInfo) {
Minicap::DisplayInfo info;
if (minicap_try_get_display_info(displayId, &info) != 0) {
if (try_get_framebuffer_display_info(displayId, &info) != 0) {
MCERROR("Unable to get display info");
return EXIT_FAILURE;
}
}
int rotation;
switch (info.orientation) {
case Minicap::ORIENTATION_0:
rotation = 0;
break;
case Minicap::ORIENTATION_90:
rotation = 90;
break;
case Minicap::ORIENTATION_180:
rotation = 180;
break;
case Minicap::ORIENTATION_270:
rotation = 270;
break;
}
std::cout.precision(2);
std::cout.setf(std::ios_base::fixed, std::ios_base::floatfield);
std::cout << "{" << std::endl
<< " \"id\": " << displayId << "," << std::endl
<< " \"width\": " << info.width << "," << std::endl
<< " \"height\": " << info.height << "," << std::endl
<< " \"xdpi\": " << info.xdpi << "," << std::endl
<< " \"ydpi\": " << info.ydpi << "," << std::endl
<< " \"size\": " << info.size << "," << std::endl
<< " \"density\": " << info.density << "," << std::endl
<< " \"fps\": " << info.fps << "," << std::endl
<< " \"secure\": " << (info.secure ? "true" : "false") << "," << std::endl
<< " \"rotation\": " << rotation << std::endl
<< "}" << std::endl;
return EXIT_SUCCESS;
}
proj.forceMaximumSize();
proj.forceAspectRatio();
if (!proj.valid()) {
std::cerr << "ERROR: missing or invalid -P" << std::endl;
return EXIT_FAILURE;
}
std::cerr << "PID: " << getpid() << std::endl;
std::cerr << "INFO: Using projection " << proj << std::endl;
// Disable STDOUT buffering.
setbuf(stdout, NULL);
// Set real display size.
Minicap::DisplayInfo realInfo;
realInfo.width = proj.realWidth;
realInfo.height = proj.realHeight;
// Figure out desired display size.
Minicap::DisplayInfo desiredInfo;
desiredInfo.width = proj.virtualWidth;
desiredInfo.height = proj.virtualHeight;
desiredInfo.orientation = proj.rotation;
// Leave a 4-byte padding to the encoder so that we can inject the size
// to the same buffer.
JpgEncoder encoder(4, 0);
Minicap::Frame frame;
bool haveFrame = false;
// Server config.
SimpleServer server;
// Set up minicap.
Minicap* minicap = minicap_create(displayId);
if (minicap == NULL) {
return EXIT_FAILURE;
}
// Figure out the quirks the current capture method has.
unsigned char quirks = 0;
switch (minicap->getCaptureMethod()) {
case Minicap::METHOD_FRAMEBUFFER:
quirks |= QUIRK_DUMB | QUIRK_TEAR;
break;
case Minicap::METHOD_SCREENSHOT:
quirks |= QUIRK_DUMB;
break;
case Minicap::METHOD_VIRTUAL_DISPLAY:
quirks |= QUIRK_ALWAYS_UPRIGHT;
break;
}
if (minicap->setRealInfo(realInfo) != 0) {
MCERROR("Minicap did not accept real display info");
goto disaster;
}
if (minicap->setDesiredInfo(desiredInfo) != 0) {
MCERROR("Minicap did not accept desired display info");
goto disaster;
}
minicap->setFrameAvailableListener(&gWaiter);
if (minicap->applyConfigChanges() != 0) {
MCERROR("Unable to start minicap with current config");
goto disaster;
}
if (!encoder.reserveData(realInfo.width, realInfo.height)) {
MCERROR("Unable to reserve data for JPG encoder");
goto disaster;
}
if (takeScreenshot) {
if (!gWaiter.waitForFrame()) {
MCERROR("Unable to wait for frame");
goto disaster;
}
int err;
if ((err = minicap->consumePendingFrame(&frame)) != 0) {
MCERROR("Unable to consume pending frame");
goto disaster;
}
if (!encoder.encode(&frame, quality)) {
MCERROR("Unable to encode frame");
goto disaster;
}
if (pumpf(STDOUT_FILENO, encoder.getEncodedData(), encoder.getEncodedSize()) < 0) {
MCERROR("Unable to output encoded frame data");
goto disaster;
}
return EXIT_SUCCESS;
}
if (testOnly) {
if (gWaiter.waitForFrame() <= 0) {
MCERROR("Did not receive any frames");
std::cout << "FAIL" << std::endl;
return EXIT_FAILURE;
}
minicap_free(minicap);
std::cout << "OK" << std::endl;
return EXIT_SUCCESS;
}
if (!server.start(sockname)) {
MCERROR("Unable to start server on namespace '%s'", sockname);
goto disaster;
}
// Prepare banner for clients.
unsigned char banner[BANNER_SIZE];
banner[0] = (unsigned char) BANNER_VERSION;
banner[1] = (unsigned char) BANNER_SIZE;
putUInt32LE(banner + 2, getpid());
putUInt32LE(banner + 6, realInfo.width);
putUInt32LE(banner + 10, realInfo.height);
putUInt32LE(banner + 14, desiredInfo.width);
putUInt32LE(banner + 18, desiredInfo.height);
banner[22] = (unsigned char) desiredInfo.orientation;
banner[23] = quirks;
int fd;
while (!gWaiter.isStopped() && (fd = server.accept()) > 0) {
MCINFO("New client connection");
if (pumps(fd, banner, BANNER_SIZE) < 0) {
close(fd);
continue;
}
int pending, err;
while (!gWaiter.isStopped() && (pending = gWaiter.waitForFrame()) > 0) {
auto frameAvailableAt = std::chrono::steady_clock::now();
if (skipFrames && pending > 1) {
// Skip frames if we have too many. Not particularly thread safe,
// but this loop should be the only consumer anyway (i.e. nothing
// else decreases the frame count).
gWaiter.reportExtraConsumption(pending - 1);
while (--pending >= 1) {
if ((err = minicap->consumePendingFrame(&frame)) != 0) {
if (err == -EINTR) {
MCINFO("Frame consumption interrupted by EINTR");
goto close;
}
else {
MCERROR("Unable to skip pending frame");
goto disaster;
}
}
minicap->releaseConsumedFrame(&frame);
}
}
if ((err = minicap->consumePendingFrame(&frame)) != 0) {
if (err == -EINTR) {
MCINFO("Frame consumption interrupted by EINTR");
goto close;
}
else {
MCERROR("Unable to consume pending frame");
goto disaster;
}
}
haveFrame = true;
// Encode the frame.
if (!encoder.encode(&frame, quality)) {
MCERROR("Unable to encode frame");
goto disaster;
}
// Push it out synchronously because it's fast and we don't care
// about other clients.
unsigned char* data = encoder.getEncodedData() - 4;
size_t size = encoder.getEncodedSize();
putUInt32LE(data, size);
if (pumps(fd, data, size + 4) < 0) {
break;
}
// This will call onFrameAvailable() on older devices, so we have
// to do it here or the loop will stop.
minicap->releaseConsumedFrame(&frame);
haveFrame = false;
if(framePeriodMs > 0) {
std::this_thread::sleep_until(frameAvailableAt + std::chrono::milliseconds(framePeriodMs));
}
}
close:
MCINFO("Closing client connection");
close(fd);
// Have we consumed one frame but are still holding it?
if (haveFrame) {
minicap->releaseConsumedFrame(&frame);
}
}
minicap_free(minicap);
return EXIT_SUCCESS;
disaster:
if (haveFrame) {
minicap->releaseConsumedFrame(&frame);
}
minicap_free(minicap);
return EXIT_FAILURE;
}