forked from sackmotion/motion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vloopback_motion.c
256 lines (213 loc) · 6.97 KB
/
vloopback_motion.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
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
/*
* vloopback_motion.c
*
* Video loopback functions for motion.
* Copyright 2000 by Jeroen Vreeken ([email protected])
* Copyright 2008 by Angel Carpintero ([email protected])
* This software is distributed under the GNU public license version 2
* See also the file 'COPYING'.
*
*/
#include "vloopback_motion.h"
#if defined(HAVE_LINUX_VIDEODEV_H) && (!defined(WITHOUT_V4L)) && (!defined(BSD))
#include <sys/utsname.h>
#include <dirent.h>
/**
* v4l_open_vidpipe
*
*/
static int v4l_open_vidpipe(void)
{
int pipe_fd = -1;
char pipepath[255];
char buffer[255];
char *major;
char *minor;
struct utsname uts;
if (uname(&uts) < 0) {
MOTION_LOG(CRT, TYPE_VIDEO, SHOW_ERRNO, "%s: Unable to execute uname");
return -1;
}
major = strtok(uts.release, ".");
minor = strtok(NULL, ".");
if ((major == NULL) || (minor == NULL) || (strcmp(major, "2"))) {
MOTION_LOG(CRT, TYPE_VIDEO, SHOW_ERRNO, "%s: Unable to decipher OS version");
return -1;
}
if (strcmp(minor, "5") < 0) {
FILE *vloopbacks;
char *loop;
char *input;
char *istatus;
char *output;
char *ostatus;
vloopbacks = fopen("/proc/video/vloopback/vloopbacks", "r");
if (!vloopbacks) {
MOTION_LOG(CRT, TYPE_VIDEO, SHOW_ERRNO, "%s: Failed to open "
"'/proc/video/vloopback/vloopbacks'");
return -1;
}
/* Read vloopback version*/
if (!fgets(buffer, sizeof(buffer), vloopbacks)) {
MOTION_LOG(CRT, TYPE_VIDEO, SHOW_ERRNO, "%s: Unable to read vloopback version");
myfclose(vloopbacks);
return -1;
}
fprintf(stderr, "\t%s", buffer);
/* Read explanation line */
if (!fgets(buffer, sizeof(buffer), vloopbacks)) {
MOTION_LOG(CRT, TYPE_VIDEO, SHOW_ERRNO, "%s: Unable to read vloopback"
" explanation line");
myfclose(vloopbacks);
return -1;
}
while (fgets(buffer, sizeof(buffer), vloopbacks)) {
if (strlen(buffer) > 1) {
buffer[strlen(buffer)-1] = 0;
loop = strtok(buffer, "\t");
input = strtok(NULL, "\t");
istatus = strtok(NULL, "\t");
output = strtok(NULL, "\t");
ostatus = strtok(NULL, "\t");
if (istatus[0] == '-') {
snprintf(pipepath, sizeof(pipepath), "/dev/%s", input);
pipe_fd = open(pipepath, O_RDWR);
if (pipe_fd >= 0) {
MOTION_LOG(NTC, TYPE_VIDEO, NO_ERRNO, "%s: \tInput: /dev/%s "
"\tOutput: /dev/%s", input, output);
break;
}
}
}
}
myfclose(vloopbacks);
} else {
DIR *dir;
struct dirent *dirp;
const char prefix[] = "/sys/class/video4linux/";
char *ptr, *io;
int fd;
int low = 9999;
int tfd;
int tnum;
if ((dir = opendir(prefix)) == NULL) {
MOTION_LOG(CRT, TYPE_VIDEO, SHOW_ERRNO, "%s: Failed to open '%s'",
prefix);
return -1;
}
while ((dirp = readdir(dir)) != NULL) {
if (!strncmp(dirp->d_name, "video", 5)) {
strncpy(buffer, prefix, sizeof(buffer));
strncat(buffer, dirp->d_name, sizeof(buffer) - strlen(buffer));
strncat(buffer, "/name", sizeof(buffer) - strlen(buffer));
if ((fd = open(buffer, O_RDONLY)) >= 0) {
if ((read(fd, buffer, sizeof(buffer)-1)) < 0) {
close(fd);
continue;
}
ptr = strtok(buffer, " ");
if (strcmp(ptr, "Video")) {
close(fd);
continue;
}
major = strtok(NULL, " ");
minor = strtok(NULL, " ");
io = strtok(NULL, " \n");
if (strcmp(major, "loopback") || strcmp(io, "input")) {
close(fd);
continue;
}
if ((ptr = strtok(buffer, " ")) == NULL) {
close(fd);
continue;
}
tnum = atoi(minor);
if (tnum < low) {
mystrcpy(buffer, "/dev/");
strncat(buffer, dirp->d_name, sizeof(buffer) - strlen(buffer));
if ((tfd = open(buffer, O_RDWR)) >= 0) {
strncpy(pipepath, buffer, sizeof(pipepath));
if (pipe_fd >= 0)
close(pipe_fd);
pipe_fd = tfd;
low = tnum;
}
}
close(fd);
}
}
}
closedir(dir);
if (pipe_fd >= 0)
MOTION_LOG(NTC, TYPE_VIDEO, NO_ERRNO, "%s: Opened %s as input",
pipepath);
}
return pipe_fd;
}
/**
* v4l_startpipe
*
*/
static int v4l_startpipe(const char *dev_name, int width, int height, int type)
{
int dev;
struct video_picture vid_pic;
struct video_window vid_win;
if (!strcmp(dev_name, "-")) {
dev = v4l_open_vidpipe();
} else {
dev = open(dev_name, O_RDWR);
MOTION_LOG(NTC, TYPE_VIDEO, NO_ERRNO, "%s: Opened %s as input",
dev_name);
}
if (dev < 0) {
MOTION_LOG(ERR, TYPE_VIDEO, SHOW_ERRNO, "%s: Opening %s as input failed",
dev_name);
return -1;
}
if (ioctl(dev, VIDIOCGPICT, &vid_pic) == -1) {
MOTION_LOG(ERR, TYPE_VIDEO, SHOW_ERRNO, "%s: ioctl (VIDIOCGPICT)");
return -1;
}
vid_pic.palette = type;
if (ioctl(dev, VIDIOCSPICT, &vid_pic) == -1) {
MOTION_LOG(ERR, TYPE_VIDEO, SHOW_ERRNO, "%s: ioctl (VIDIOCSPICT)");
return -1;
}
if (ioctl(dev, VIDIOCGWIN, &vid_win) == -1) {
MOTION_LOG(ERR, TYPE_VIDEO, SHOW_ERRNO, "%s: ioctl (VIDIOCGWIN)");
return -1;
}
vid_win.height = height;
vid_win.width = width;
if (ioctl(dev, VIDIOCSWIN, &vid_win) == -1) {
MOTION_LOG(ERR, TYPE_VIDEO, SHOW_ERRNO, "%s: ioctl (VIDIOCSWIN)");
return -1;
}
return dev;
}
/**
* v4l_putpipe
*
*/
static int v4l_putpipe(int dev, unsigned char *image, int size)
{
return write(dev, image, size);
}
/**
* vid_startpipe
*
*/
int vid_startpipe(const char *dev_name, int width, int height, int type)
{
return v4l_startpipe(dev_name, width, height, type);
}
/**
* vid_putpipe
*
*/
int vid_putpipe (int dev, unsigned char *image, int size)
{
return v4l_putpipe(dev, image, size);
}
#endif /* !WITHOUT_V4L && !BSD */