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>
3
27
#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>
7
30
#include <sys/mman.h>
8
- #include <sys/ioctl.h>
9
- #include <sys/types.h>
10
31
#include <linux/videodev2.h>
32
+
33
+ struct frame {
34
+ struct iovec buf ;
35
+ int index ;
36
+ };
37
+
11
38
/*
12
39
* refer to /usr/include/linux/v4l2-controls.h
13
40
* supported v4l2 control cmds
@@ -173,48 +200,6 @@ static void v4l2_get_cid(struct v4l2_ctx *vc)
173
200
}
174
201
}
175
202
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
-
218
203
static int v4l2_set_format (struct v4l2_ctx * vc )
219
204
{
220
205
struct v4l2_format fmt ;
@@ -299,6 +284,48 @@ static int v4l2_req_buf(struct v4l2_ctx *vc)
299
284
return 0 ;
300
285
}
301
286
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
+
302
329
static int v4l2_buf_enqueue (struct v4l2_ctx * vc )
303
330
{
304
331
struct v4l2_buffer buf = {
@@ -341,14 +368,14 @@ static int v4l2_buf_dequeue(struct v4l2_ctx *vc, struct frame *f)
341
368
}
342
369
vc -> qbuf_done = false;
343
370
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 ));
345
372
f -> index = buf .index ;
346
373
f -> buf .iov_base = vc -> buf [buf .index ].iov_base ;
347
374
f -> buf .iov_len = buf .bytesused ;
348
375
return f -> buf .iov_len ;
349
376
}
350
377
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 )
352
379
{
353
380
struct frame f ;
354
381
int flen ;
@@ -360,7 +387,7 @@ static int v4l2_read(struct uvc_ctx *uvc, struct v4l2_ctx *vc, void *buf, int le
360
387
return -1 ;
361
388
}
362
389
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" ,
364
391
flen , len );
365
392
return -1 ;
366
393
}
@@ -372,7 +399,7 @@ static int v4l2_read(struct uvc_ctx *uvc, struct v4l2_ctx *vc, void *buf, int le
372
399
return f .buf .iov_len ;
373
400
}
374
401
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 )
376
403
{
377
404
struct v4l2_ctx * vc = (struct v4l2_ctx * )uvc -> opaque ;
378
405
return v4l2_buf_enqueue (vc );
@@ -451,5 +478,4 @@ struct uvc_ops v4l2_ops = {
451
478
v4l2_write ,
452
479
v4l2_ioctl ,
453
480
v4l2_print_info ,
454
- NULL ,
455
481
};
0 commit comments