-
Notifications
You must be signed in to change notification settings - Fork 0
/
monroute.c
109 lines (95 loc) · 2.23 KB
/
monroute.c
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
#include <X11/X.h>
#include <X11/Xutil.h>
#include <X11/extensions/Xinerama.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
#define EXIT_FAILURE 1
#define EXIT_SUCCESS 0
static Display *dpy;
static int mon = 0;
static int dev = 50;
static int rate = 60;
void die(const char *errstr, ...) {
va_list ap;
va_start(ap, errstr);
vfprintf(stderr, errstr, ap);
va_end(ap);
exit(EXIT_FAILURE);
}
void xinit(void) {
if (!(dpy = XOpenDisplay(NULL)))
die("Can't open display\n");
}
void xclose(void) {
XCloseDisplay(dpy);
exit(EXIT_SUCCESS);
}
XineramaScreenInfo *getscrinfos(int *nn) {
if (XineramaIsActive(dpy)) {
return XineramaQueryScreens(dpy, nn);
}
die("Xinerma is not active\n");
return NULL;
}
void listmon(void) {
int nn;
XineramaScreenInfo *info = getscrinfos(&nn);
for (int i = 0; i < nn; i++)
printf("\tMonitor %d: %dX%d+%d+%d\n", info[i].screen_number, info[i].width,
info[i].height, info[i].x_org, info[i].y_org);
}
void usage(char *argv0) {
die("usage: %s [-l] [-m monitor] [-d device] [-r rate]\n", argv0);
}
void v4l2(void) {
char devpath[256], cmd[256];
struct stat stats;
snprintf(devpath, sizeof(devpath), "/dev/video%d", dev);
if (stat(devpath, &stats) == -1) {
system("sudo modprobe -r v4l2loopback");
snprintf(
cmd, sizeof(cmd),
"sudo modprobe v4l2loopback video_nr=%d card_label=\"monroutecam\"",
dev);
system(cmd);
}
}
void route(void) {
int nn;
char cmd[256];
XineramaScreenInfo scr = getscrinfos(&nn)[mon];
snprintf(cmd, sizeof(cmd),
"ffmpeg -f x11grab -r %d -s %dx%d -i :0+%d,%d -pix_fmt yuv420p -f "
"v4l2 /dev/video%d",
rate, scr.width, scr.height, scr.x_org, scr.y_org, dev);
system(cmd);
}
int main(int argc, char *argv[]) {
int opt;
xinit();
while ((opt = getopt(argc, argv, "m:d:r:l")) != -1) {
switch (opt) {
case 'm':
mon = (int)atol(optarg);
break;
case 'd':
dev = (int)atol(optarg);
break;
case 'r':
rate = (int)atol(optarg);
break;
case 'l':
listmon();
exit(EXIT_SUCCESS);
default:
usage(argv[0]);
}
}
v4l2();
route();
xclose();
return 0;
}