Skip to content

Commit 1cd86c5

Browse files
committed
[libuvc] up
1 parent a649ec4 commit 1cd86c5

File tree

2 files changed

+80
-62
lines changed

2 files changed

+80
-62
lines changed

libuvc/libuvc.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,6 @@ static struct uvc_ops *uvc_ops[] = {
4242
NULL
4343
};
4444

45-
46-
47-
struct frame {
48-
struct iovec buf;
49-
int index;
50-
};
51-
52-
5345
int uvc_print_info(struct uvc_ctx *c)
5446
{
5547
return c->ops->print_info(c);

libuvc/v4l2.c

Lines changed: 80 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,40 @@
1-
2-
#include <libmacro.h>
1+
/******************************************************************************
2+
* Copyright (C) 2014-2020 Zhifeng Gong <[email protected]>
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
******************************************************************************/
22+
#include "libuvc.h"
23+
#include <stdio.h>
24+
#include <stdlib.h>
25+
#include <stdbool.h>
26+
#include <string.h>
327
#include <unistd.h>
4-
#include <sys/uio.h>
5-
#include <sys/ioctl.h>
6-
#include <sys/time.h>
28+
#include <fcntl.h>
29+
#include <errno.h>
730
#include <sys/mman.h>
8-
#include <sys/ioctl.h>
9-
#include <sys/types.h>
1031
#include <linux/videodev2.h>
32+
33+
struct frame {
34+
struct iovec buf;
35+
int index;
36+
};
37+
1138
/*
1239
* refer to /usr/include/linux/v4l2-controls.h
1340
* supported v4l2 control cmds
@@ -173,48 +200,6 @@ static void v4l2_get_cid(struct v4l2_ctx *vc)
173200
}
174201
}
175202

176-
static struct v4l2_ctx *v4l2_open(struct uvc_ctx *uvc, const char *dev, int width, int height)
177-
{
178-
int fd = -1;
179-
struct v4l2_ctx *vc = CALLOC(1, struct v4l2_ctx);
180-
if (!vc) {
181-
printf("malloc v4l2_ctx failed!\n");
182-
return NULL;
183-
}
184-
fd = open(dev, O_RDWR);
185-
if (fd == -1) {
186-
printf("open %s failed: %d\n", dev, errno);
187-
goto failed;
188-
}
189-
vc->name = strdup(dev);
190-
vc->fd = fd;
191-
vc->width = width;
192-
vc->height = height;
193-
194-
if (-1 == v4l2_set_format(vc)) {
195-
printf("v4l2_set_format failed\n");
196-
goto failed;
197-
}
198-
if (-1 == v4l2_req_buf(vc)) {
199-
printf("v4l2_req_buf failed\n");
200-
goto failed;
201-
}
202-
vc->parent = uvc;
203-
return vc;
204-
205-
failed:
206-
if (fd != -1) {
207-
close(fd);
208-
}
209-
if (vc->name) {
210-
free(vc->name);
211-
}
212-
if (vc) {
213-
free(vc);
214-
}
215-
return NULL;
216-
}
217-
218203
static int v4l2_set_format(struct v4l2_ctx *vc)
219204
{
220205
struct v4l2_format fmt;
@@ -299,6 +284,48 @@ static int v4l2_req_buf(struct v4l2_ctx *vc)
299284
return 0;
300285
}
301286

287+
static void *v4l2_open(struct uvc_ctx *uvc, const char *dev, int width, int height)
288+
{
289+
int fd = -1;
290+
struct v4l2_ctx *vc = calloc(1, sizeof(struct v4l2_ctx));
291+
if (!vc) {
292+
printf("malloc v4l2_ctx failed!\n");
293+
return NULL;
294+
}
295+
fd = open(dev, O_RDWR);
296+
if (fd == -1) {
297+
printf("open %s failed: %d\n", dev, errno);
298+
goto failed;
299+
}
300+
vc->name = strdup(dev);
301+
vc->fd = fd;
302+
vc->width = width;
303+
vc->height = height;
304+
305+
if (-1 == v4l2_set_format(vc)) {
306+
printf("v4l2_set_format failed\n");
307+
goto failed;
308+
}
309+
if (-1 == v4l2_req_buf(vc)) {
310+
printf("v4l2_req_buf failed\n");
311+
goto failed;
312+
}
313+
vc->parent = uvc;
314+
return vc;
315+
316+
failed:
317+
if (fd != -1) {
318+
close(fd);
319+
}
320+
if (vc->name) {
321+
free(vc->name);
322+
}
323+
if (vc) {
324+
free(vc);
325+
}
326+
return NULL;
327+
}
328+
302329
static int v4l2_buf_enqueue(struct v4l2_ctx *vc)
303330
{
304331
struct v4l2_buffer buf = {
@@ -341,14 +368,14 @@ static int v4l2_buf_dequeue(struct v4l2_ctx *vc, struct frame *f)
341368
}
342369
vc->qbuf_done = false;
343370
vc->buf_index = buf.index;
344-
memcpy(&vc->parent->timestamp, &buf.timestamp, sizeof(struct timeval));
371+
memcpy(&(vc->parent->timestamp), &buf.timestamp, sizeof(struct timeval));
345372
f->index = buf.index;
346373
f->buf.iov_base = vc->buf[buf.index].iov_base;
347374
f->buf.iov_len = buf.bytesused;
348375
return f->buf.iov_len;
349376
}
350377

351-
static int v4l2_read(struct uvc_ctx *uvc, struct v4l2_ctx *vc, void *buf, int len)
378+
static int v4l2_read(struct uvc_ctx *uvc, void *buf, size_t len)
352379
{
353380
struct frame f;
354381
int flen;
@@ -360,7 +387,7 @@ static int v4l2_read(struct uvc_ctx *uvc, struct v4l2_ctx *vc, void *buf, int le
360387
return -1;
361388
}
362389
if (flen > len) {
363-
printf("v4l2 frame is %d bytes, but buffer len is %d, not enough!\n",
390+
printf("v4l2 frame is %d bytes, but buffer len is %zu, not enough!\n",
364391
flen, len);
365392
return -1;
366393
}
@@ -372,7 +399,7 @@ static int v4l2_read(struct uvc_ctx *uvc, struct v4l2_ctx *vc, void *buf, int le
372399
return f.buf.iov_len;
373400
}
374401

375-
static int v4l2_write(struct uvc_ctx *uvc, void *buf, int len)
402+
static int v4l2_write(struct uvc_ctx *uvc, void *buf, size_t len)
376403
{
377404
struct v4l2_ctx *vc = (struct v4l2_ctx *)uvc->opaque;
378405
return v4l2_buf_enqueue(vc);
@@ -451,5 +478,4 @@ struct uvc_ops v4l2_ops = {
451478
v4l2_write,
452479
v4l2_ioctl,
453480
v4l2_print_info,
454-
NULL,
455481
};

0 commit comments

Comments
 (0)