forked from floe/backscrub
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloopback.cc
101 lines (72 loc) · 2.46 KB
/
loopback.cc
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
#include <linux/videodev2.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <assert.h>
#include "loopback.h"
void print_format(struct v4l2_format*vid_format) {
printf(" vid_format->type = %d\n", vid_format->type );
printf(" vid_format->fmt.pix.width = %d\n", vid_format->fmt.pix.width );
printf(" vid_format->fmt.pix.height = %d\n", vid_format->fmt.pix.height );
printf(" vid_format->fmt.pix.pixelformat = %d\n", vid_format->fmt.pix.pixelformat);
printf(" vid_format->fmt.pix.sizeimage = %d\n", vid_format->fmt.pix.sizeimage );
printf(" vid_format->fmt.pix.field = %d\n", vid_format->fmt.pix.field );
printf(" vid_format->fmt.pix.bytesperline= %d\n", vid_format->fmt.pix.bytesperline );
printf(" vid_format->fmt.pix.colorspace = %d\n", vid_format->fmt.pix.colorspace );
}
int loopback_init(const char* device, int w, int h, int debug) {
struct v4l2_capability vid_caps;
struct v4l2_format vid_format;
size_t linewidth = w * 2;
size_t framesize = h * linewidth;
int fdwr = 0;
int ret_code = 0;
fdwr = open(device, O_RDWR);
assert(fdwr >= 0);
ret_code = ioctl(fdwr, VIDIOC_QUERYCAP, &vid_caps);
assert(ret_code != -1);
memset(&vid_format, 0, sizeof(vid_format));
//usleep(100000);
ret_code = ioctl(fdwr, VIDIOC_G_FMT, &vid_format);
vid_format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
vid_format.fmt.pix.width = w;
vid_format.fmt.pix.height = h;
vid_format.fmt.pix.pixelformat = V4L2_PIX_FMT_YUYV;
vid_format.fmt.pix.sizeimage = framesize;
vid_format.fmt.pix.field = V4L2_FIELD_NONE;
vid_format.fmt.pix.bytesperline = linewidth;
vid_format.fmt.pix.colorspace = V4L2_COLORSPACE_SRGB;
ret_code = ioctl(fdwr, VIDIOC_S_FMT, &vid_format);
assert(ret_code != -1);
if (debug) print_format(&vid_format);
return fdwr;
}
#ifdef standalone
#define FRAME_WIDTH 640
#define FRAME_HEIGHT 480
int main(int argc, char* argv[]) {
char* video_device = "/dev/video1";
size_t linewidth = FRAME_WIDTH * 2;
size_t framesize = FRAME_HEIGHT * linewidth;
if(argc>1) {
video_device=argv[1];
printf("using output device: %s\n", video_device);
}
int fdwr = loopback_init(video_device,FRAME_WIDTH,FRAME_HEIGHT);
uint8_t* buffer = (uint8_t*)malloc(framesize);
while (true) {
write(fdwr, buffer, framesize);
usleep(100000);
uint64_t* front = (uint64_t*)(buffer);
*front += 12345;
}
pause();
close(fdwr);
free(buffer);
return 0;
}
#endif