-
Notifications
You must be signed in to change notification settings - Fork 3
/
twincam.cpp
620 lines (518 loc) · 13.1 KB
/
twincam.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
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
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
/* SPDX-License-Identifier: GPL-2.0-or-later */
/*
* Copyright (C) 2019, Google Inc.
*
* main.cpp - cam - The libcamera swiss army knife
*/
#include <dirent.h>
#include <getopt.h>
#ifdef HAVE_LIBUDEV
#include <libudev.h>
#endif
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <atomic>
#include <iomanip>
#include <libcamera/libcamera.h>
#include <libcamera/property_ids.h>
#include "camera_session.h"
#include "event_loop.h"
#include "twincam.h"
#include "twncm_fnctl.h"
#include "twncm_stdlib.h"
#include "uptime.h"
using namespace libcamera;
class CamApp {
public:
CamApp();
static CamApp* instance();
int init();
void cleanup() const;
int exec();
void quit();
private:
void cameraAdded(std::shared_ptr<Camera> cam);
void cameraRemoved(std::shared_ptr<Camera> cam);
void captureDone();
int run();
static std::string cameraName(const Camera* camera);
static CamApp* app_;
std::unique_ptr<CameraManager> cm_;
EventLoop loop_;
};
CamApp* CamApp::app_ = nullptr;
CamApp::CamApp() {
CamApp::app_ = this;
}
CamApp* CamApp::instance() {
return CamApp::app_;
}
static bool sysfs_exists() {
static const char* const sysfs_dirs[] = {
"/sys/subsystem/media/devices",
"/sys/bus/media/devices",
"/sys/class/media/devices",
};
for (const char* dirname : sysfs_dirs) {
DIR* dir = opendir(dirname);
if (dir) {
closedir(dir);
return true;
}
}
return false;
}
static bool dev_exists(const char* fn) {
const char name[] = "/dev/";
DIR* folder = opendir(name);
if (!folder) {
return false;
}
for (struct dirent* res; (res = readdir(folder));) {
if (!memcmp(res->d_name, fn, 5)) {
closedir(folder);
return true;
}
}
closedir(folder);
return false;
}
#ifdef HAVE_LIBUDEV
static int checkUdevDevice(struct udev_device* dev, const char* subsys) {
const char* subsystem = udev_device_get_subsystem(dev);
VERBOSE_PRINT("subsystem: '%s'\n", subsystem);
if (!subsystem)
return -ENODEV;
if (!strcmp(subsystem, subsys)) {
return 0;
}
return -ENODEV;
}
struct udev_enum {
struct udev_enumerate* ptr = 0;
~udev_enum() {
if (ptr)
udev_enumerate_unref(ptr);
}
};
struct udev {
struct udev* ptr = 0;
~udev() {
if (ptr)
udev_unref(ptr);
}
};
struct udev_device {
struct udev_device* ptr = 0;
~udev_device() {
if (ptr)
udev_device_unref(ptr);
}
};
static int wait_for_udev(const char* subsys) {
int ret = -1;
udev udev;
udev.ptr = udev_new();
if (!udev.ptr) {
return -ENODEV;
}
udev_enum udev_enum;
udev_enum.ptr = udev_enumerate_new(udev.ptr); // enumerate
if (!udev_enum.ptr) {
return -ENOMEM;
}
ret = udev_enumerate_add_match_subsystem(udev_enum.ptr, subsys);
if (ret < 0)
return ret;
ret = udev_enumerate_add_match_is_initialized(udev_enum.ptr);
if (ret < 0)
return ret;
struct udev_list_entry* ents;
ret = udev_enumerate_scan_devices(udev_enum.ptr);
if (ret < 0)
return ret;
ents = udev_enumerate_get_list_entry(udev_enum.ptr);
if (!ents) {
return -1;
}
struct udev_list_entry* ent;
udev_list_entry_foreach(ent, ents) {
udev_device dev;
const char* devnode;
const char* syspath = udev_list_entry_get_name(ent);
dev.ptr = udev_device_new_from_syspath(udev.ptr, syspath);
if (!dev.ptr) {
EPRINT("Failed to get device for '%s', skipping\n", syspath);
continue;
}
devnode = udev_device_get_devnode(dev.ptr);
if (!devnode) {
EPRINT("Failed to get device node for '%s', skipping\n", syspath);
continue;
}
if (checkUdevDevice(dev.ptr, subsys) < 0) {
EPRINT("Not a valid camera device for '%s', skipping\n", syspath);
continue;
} else {
ret = 0;
}
break;
}
return ret;
}
#endif
int CamApp::init() {
cm_ = std::make_unique<CameraManager>();
// V4L2 specific
int ret = -1;
const int end = 400;
for (int i = 0; i < end; ++i) {
if (sysfs_exists()) {
ret = 0;
break;
}
usleep(10000);
}
if (ret < 0) {
PRINT("Failed to find sysfs, is there a camera attached?\n");
} else {
VERBOSE_PRINT("Found sysfs\n");
}
ret = -1;
for (int i = 0; i < end; ++i) {
if (dev_exists("video")) {
ret = 0;
break;
}
usleep(10000);
}
if (ret < 0) {
PRINT("Failed to find a /dev/video* entry\n");
} else {
VERBOSE_PRINT("Found /dev/video* entry\n");
}
ret = -1;
for (int i = 0; i < end; ++i) {
if (dev_exists("media")) {
ret = 0;
break;
}
usleep(10000);
}
if (ret < 0) {
PRINT("Failed to find a /dev/media* entry\n");
} else {
VERBOSE_PRINT("Found /dev/media* entry\n");
}
#ifdef HAVE_LIBUDEV
ret = -1;
for (int i = 0; i < end; ++i) {
if (!wait_for_udev("media")) {
ret = 0;
break;
}
usleep(10000);
}
if (ret < 0) {
PRINT("Failed to find a udev entry\n");
} else {
VERBOSE_PRINT("Found media udev entry\n");
}
ret = -1;
for (int i = 0; i < end; ++i) {
if (!wait_for_udev("video4linux")) {
ret = 0;
break;
}
usleep(10000);
}
if (ret < 0) {
PRINT("Failed to find a video4linux udev entry\n");
} else {
VERBOSE_PRINT("Found video4linux udev entry\n");
}
#endif
ret = cm_->start();
if (ret) {
PRINT("Failed to start camera manager: %s\n", strerror(-ret));
return ret;
}
ret = -1;
for (int i = 0; i < end; ++i) {
if (!cm_->cameras().empty()) {
ret = 0;
break;
}
usleep(10000);
}
if (ret < 0) {
PRINT("Failed to find a camera\n");
} else {
VERBOSE_PRINT("Found camera\n");
}
return 0;
}
void CamApp::cleanup() const {
cm_->stop();
}
int CamApp::exec() {
PRINT_FUNC();
int ret;
ret = run();
cleanup();
return ret;
}
void CamApp::quit() {
loop_.exit();
}
int CamApp::run() {
PRINT_FUNC();
if (opts.print_available_cameras) {
PRINT("Available cameras:\n");
for (size_t i = 0; i < cm_->cameras().size(); ++i) {
PRINT("%zu: %s\n", i, cameraName(cm_->cameras()[i].get()).c_str());
}
return 0;
}
if (cm_->cameras().empty()) {
PRINT("No cameras available\n");
return 0;
}
CameraSession session(cm_.get());
int ret = session.init();
if (ret) {
PRINT("Failed to init camera session\n");
return ret;
}
ret = session.start();
if (ret) {
PRINT("Failed to start camera session\n");
return ret;
}
loop_.exec();
session.stop();
return 0;
}
std::string CamApp::cameraName(const Camera* camera) {
const ControlList& props = camera->properties();
bool addModel = true;
std::string name;
/*
* Construct the name from the camera location, model and ID. The model
* is only used if the location isn't present or is set to External.
*/
const std::optional<int>& location = props.get(properties::Location);
if (location) {
switch (*location) {
case properties::CameraLocationFront:
addModel = false;
name = "Internal front camera ";
break;
case properties::CameraLocationBack:
addModel = false;
name = "Internal back camera ";
break;
case properties::CameraLocationExternal:
name = "External camera ";
break;
default:
break;
}
}
if (addModel) {
/*
* If the camera location is not availble use the camera model
* to build the camera name.
*/
const std::optional<std::string>& model = props.get(properties::Model);
if (model)
name = "'" + *model + "' ";
}
name += "(" + camera->id() + ")";
return name;
}
static void signalHandler([[maybe_unused]] int signal) {
VERBOSE_PRINT("Exiting\n");
CamApp::instance()->quit();
if (opts.to_syslog) {
closelog();
}
}
static void printExit([[maybe_unused]] int signal) {
VERBOSE_PRINT("Exit with signal: %d\n", signal);
CamApp::instance()->quit();
if (opts.to_syslog) {
closelog();
}
}
static void chrootThis([[maybe_unused]] int signal) {
VERBOSE_PRINT("Chrooting: %d\n", signal);
int ret = chdir("/sysroot");
if (ret) {
EPRINT("chdir /sysroot failed: %d\n", ret);
return;
}
ret = chroot(".");
if (ret) {
EPRINT("chroot failed: %d\n", ret);
return;
}
ret = chdir("/");
if (ret) {
EPRINT("chdir / failed: %d\n", ret);
return;
}
setlocale(LC_ALL, "");
}
static int processArgs(int argc, char** argv) {
const struct option options[] = {{"camera", required_argument, 0, 'c'},
{"daemon", no_argument, 0, 'd'},
#ifdef HAVE_DRM
{"drm", no_argument, 0, 'D'},
#endif
{"filename", required_argument, 0, 'F'},
{"function", no_argument, 0, 'f'},
{"help", no_argument, 0, 'h'},
{"kill", no_argument, 0, 'k'},
{"list-cameras", no_argument, 0, 'l'},
{"new-root-dir", no_argument, 0, 'n'},
{"pixel-format", required_argument, 0, 'p'},
#ifdef HAVE_SDL
{"sdl", no_argument, 0, 'S'},
#endif
{"syslog", no_argument, 0, 's'},
{"uptime", no_argument, 0, 'u'},
{"verbose", no_argument, 0, 'v'},
{NULL, 0, 0, '\0'}};
for (int opt; (opt = getopt_long(argc, argv, "c:dDF:fhklnp:Ssuv", options,
NULL)) != -1;) {
int fd;
char buf[16];
switch (opt) {
case 'c':
opts.camera = twncm_atoi(optarg);
break;
case 'd':
fd = twncm_open_write("/var/run/twincam.pid");
if (fd < 0) {
break;
}
pid_write(fd);
twncm_close(fd);
break;
#ifdef HAVE_DRM
case 'D':
opts.drm = true;
break;
#endif
case 'F':
opts.filename = optarg;
break;
case 'f':
opts.print_func = true;
break;
case 'k':
fd = twncm_open_read("/var/run/twincam.pid");
pid_read(fd, buf);
kill(twncm_atoi(buf), SIGTERM);
return 1;
case 'l':
opts.print_available_cameras = true;
break;
case 'n':
fd = twncm_open_read("/var/run/twincam.pid");
pid_read(fd, buf);
kill(twncm_atoi(buf), SIGUSR1);
return 1;
case 'p':
opts.pf = optarg;
break;
#ifdef HAVE_SDL
case 'S':
opts.sdl = true;
break;
#endif
case 's':
opts.to_syslog = true;
setenv("LIBCAMERA_LOG_FILE", "syslog", 1);
openlog("twincam", 0, LOG_LOCAL1);
break;
case 'u':
opts.uptime = true;
break;
case 'v':
opts.verbose = true;
setenv("LIBCAMERA_LOG_LEVELS", "DEBUG", 1);
break;
default:
static const char* help =
"Usage: twincam [OPTIONS]\n\n"
"Options:\n"
" -c, --camera Camera to select\n"
" -d, --daemon Daemon mode (write a pid file "
"/var/run/twincam.pid)\n"
#ifdef HAVE_DRM
" -D, --drm Display viewfinder through drm\n"
#endif
" -F, --filename Write captured frames to disk\n"
" -f, --function function tracer\n"
" -h, --help Print this help\n"
" -k, --kill Kill twincam (sends SIGTERM to "
"pidfile pid)\n"
" -l, --list-cameras List cameras\n"
" -n, --new-root-dir chroot to /sysroot (sends SIGUSR1 to "
"pidfile pid)\n"
" -p, --pixel-format Select pixel format\n"
#ifdef HAVE_SDL
" -S, --sdl Display viewfinder through SDL\n"
#endif
" -s, --syslog Also trace output in syslog\n"
" -u, --uptime prepend prints with uptime\n"
" -v, --verbose Enable verbose logging";
PRINT("%s\n", help);
return 1;
}
}
return 0;
}
options opts;
int main(int argc, char** argv) {
PRINT_FUNC(); // Although this is never printed, it's important because it
// sets the initial timer
CamApp app;
struct sigaction sa = {};
struct sigaction sa_err = {};
struct sigaction sa_chroot = {};
int ret = processArgs(argc, argv);
if (ret) {
ret = 0;
goto end;
}
VERBOSE_PRINT("Successfully processed args\n");
ret = app.init();
if (ret) {
ret = ret == -EINTR ? 0 : EXIT_FAILURE;
goto end;
}
sa.sa_handler = &signalHandler;
sa_err.sa_handler = &printExit;
sa_chroot.sa_handler = &chrootThis;
sigaction(SIGINT, &sa, nullptr);
sigaction(SIGFPE, &sa_err, nullptr);
sigaction(SIGILL, &sa_err, nullptr);
sigaction(SIGSEGV, &sa_err, nullptr);
sigaction(SIGBUS, &sa_err, nullptr);
sigaction(SIGABRT, &sa_err, nullptr);
sigaction(SIGIOT, &sa_err, nullptr);
sigaction(SIGTRAP, &sa_err, nullptr);
sigaction(SIGSYS, &sa_err, nullptr);
sigaction(SIGUSR1, &sa_chroot, nullptr);
if (app.exec()) {
ret = EXIT_FAILURE;
goto end;
}
end:
VERBOSE_PRINT("Exit with status: %d\n", ret);
return ret;
}