Skip to content

Commit 5693b0e

Browse files
committed
Modify v4l2loopback handling
Should fix #248 Possibly also #242
1 parent ec49104 commit 5693b0e

File tree

3 files changed

+23
-25
lines changed

3 files changed

+23
-25
lines changed

src/decoder_v4l2.c

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -82,34 +82,27 @@ void set_v4l2_device(const char* device) {
8282
void query_v4l_device(int droidcam_device_fd, unsigned *WEBCAM_W, unsigned *WEBCAM_H) {
8383
struct v4l2_format vid_format = {0};
8484
vid_format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
85-
vid_format.fmt.pix.width = 0;
86-
vid_format.fmt.pix.height = 0;
8785

8886
int in_width = *WEBCAM_W;
8987
int in_height = *WEBCAM_H;
9088
*WEBCAM_W = 0;
9189
*WEBCAM_H = 0;
9290

93-
int ret = xioctl(droidcam_device_fd, VIDIOC_G_FMT, &vid_format);
94-
if (ret < 0 && errno == EINVAL) {
95-
dbgprint("Got no format, trying to set %dx%d\n", in_width, in_height);
96-
vid_format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
97-
vid_format.fmt.pix.width = in_width;
98-
vid_format.fmt.pix.height = in_height;
99-
vid_format.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420;
100-
vid_format.fmt.pix.field = V4L2_FIELD_ANY;
101-
102-
ret = xioctl(droidcam_device_fd, VIDIOC_TRY_FMT, &vid_format);
103-
if (ret >= 0) {
104-
if (xioctl(droidcam_device_fd, VIDIOC_S_FMT, &vid_format) < 0) {
105-
errprint("Fatal: Unable to set v4l2loopback device format. errno=%d\n", errno);
106-
return;
107-
}
108-
}
109-
}
91+
dbgprint("Trying to set format YU12:%dx%d\n", in_width, in_height);
92+
vid_format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
93+
vid_format.fmt.pix.width = in_width;
94+
vid_format.fmt.pix.height = in_height;
95+
vid_format.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420;
96+
vid_format.fmt.pix.field = V4L2_FIELD_NONE;
97+
xioctl(droidcam_device_fd, VIDIOC_S_FMT, &vid_format);
98+
99+
vid_format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
100+
xioctl(droidcam_device_fd, VIDIOC_S_FMT, &vid_format);
110101

102+
int ret = xioctl(droidcam_device_fd, VIDIOC_G_FMT, &vid_format);
111103
if (ret < 0) {
112-
errprint("Fatal: Unable to query video device. errno=%d\n", errno);
104+
errprint("Fatal: Unable to query video device. dev=%s errno=%d\n",
105+
v4l2_device, errno);
113106
return;
114107
}
115108

@@ -121,13 +114,16 @@ void query_v4l_device(int droidcam_device_fd, unsigned *WEBCAM_W, unsigned *WEBC
121114
dbgprint(" vid_format->fmt.pix.field =%d\n", vid_format.fmt.pix.field );
122115
dbgprint(" vid_format->fmt.pix.bytesperline=%d\n", vid_format.fmt.pix.bytesperline );
123116
dbgprint(" vid_format->fmt.pix.colorspace =%d\n", vid_format.fmt.pix.colorspace );
117+
124118
if (vid_format.fmt.pix.pixelformat != V4L2_PIX_FMT_YUV420) {
125119
unsigned pixelfmt = vid_format.fmt.pix.pixelformat;
126120
BYTE fourcc[5] = { (BYTE)(pixelfmt >> 0), (BYTE)(pixelfmt >> 8),
127121
(BYTE)(pixelfmt >> 16), (BYTE)(pixelfmt >> 24), '\0' };
128-
errprint("Fatal: droidcam video device reported pixel format %x (%s), expected %x (YU12/I420)\n"
129-
"Try 'v4l2loopback-ctl set-caps \"video/x-raw, format=I420, width=640, height=480\" %s'\n",
130-
vid_format.fmt.pix.pixelformat, fourcc, V4L2_PIX_FMT_YUV420, "/dev/video<N>");
122+
123+
errprint("Fatal: video device reported pixel format %x (%s), expected %x (YU12/I420)\n"
124+
"Try `v4l2loopback-ctl set-caps %s \"YU12:%dx%d\"`, or specify a different video device\n",
125+
vid_format.fmt.pix.pixelformat, fourcc, V4L2_PIX_FMT_YUV420,
126+
v4l2_device, in_width, in_height);
131127
return;
132128
}
133129
if (vid_format.fmt.pix.width <= 0 || vid_format.fmt.pix.height <= 0) {

src/droidcam-cli.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ typedef struct Thread {
2222
Thread athread = {0, -1}, vthread = {0, -1}, dthread = {0, -1};
2323

2424
char *v4l2_dev = 0;
25-
unsigned v4l2_width = 0, v4l2_height = 0;
25+
unsigned v4l2_width = 640, v4l2_height = 480;
2626
volatile int a_active = 0;
2727
volatile int v_active = 0;
2828
volatile int v_running = 0;
@@ -54,7 +54,7 @@ void UpdateBatteryLabel(char *battery_value) {
5454

5555
static inline void usage(__attribute__((__unused__)) int argc, char *argv[]) {
5656
fprintf(stderr, "Usage: \n"
57-
" %s -l <port>\n"
57+
" %s [options] -l <port>\n"
5858
" Listen on 'port' for connections (video only)\n"
5959
"\n"
6060
" %s [options] <ip> <port>\n"

src/settings.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ void LoadSettings(struct settings* settings) {
2929
memset(settings, 0, sizeof(struct settings));
3030
settings->video = 1;
3131
settings->port = 4747;
32+
settings->v4l2_width = 640;
33+
settings->v4l2_height = 480;
3234
settings->connection = CB_RADIO_WIFI;
3335
settings->confirm_close = 1;
3436

0 commit comments

Comments
 (0)