forked from maxbbraun/uvc-gadget
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uvc-gadget.c
2363 lines (1970 loc) · 63.6 KB
/
uvc-gadget.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* UVC gadget test application
*
* Copyright (C) 2010 Ideas on board SPRL <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
*/
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/select.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <linux/usb/ch9.h>
#include <linux/usb/video.h>
#include <linux/videodev2.h>
#include "uvc.h"
#define WIDTH1 640
#define HEIGHT1 360
#define WIDTH2 1920
#define HEIGHT2 1080
/* Enable debug prints. */
#undef ENABLE_BUFFER_DEBUG
#undef ENABLE_USB_REQUEST_DEBUG
#define CLEAR(x) memset(&(x), 0, sizeof(x))
#define max(a, b) (((a) > (b)) ? (a) : (b))
#define clamp(val, min, max) \
({ \
typeof(val) __val = (val); \
typeof(min) __min = (min); \
typeof(max) __max = (max); \
(void)(&__val == &__min); \
(void)(&__val == &__max); \
__val = __val < __min ? __min : __val; \
__val > __max ? __max : __val; \
})
#define ARRAY_SIZE(a) ((sizeof(a) / sizeof(a[0])))
#define pixfmtstr(x) (x) & 0xff, ((x) >> 8) & 0xff, ((x) >> 16) & 0xff, ((x) >> 24) & 0xff
/*
* The UVC webcam gadget kernel driver (g_webcam.ko) supports changing
* the Brightness attribute of the Processing Unit (PU). by default. If
* the underlying video capture device supports changing the Brightness
* attribute of the image being acquired (like the Virtual Video, VIVI
* driver), then we should route this UVC request to the respective
* video capture device.
*
* Incase, there is no actual video capture device associated with the
* UVC gadget and we wish to use this application as the final
* destination of the UVC specific requests then we should return
* pre-cooked (static) responses to GET_CUR(BRIGHTNESS) and
* SET_CUR(BRIGHTNESS) commands to keep command verifier test tools like
* UVC class specific test suite of USBCV, happy.
*
* Note that the values taken below are in sync with the VIVI driver and
* must be changed for your specific video capture device. These values
* also work well in case there in no actual video capture device.
*/
#define PU_BRIGHTNESS_MIN_VAL 0
#define PU_BRIGHTNESS_MAX_VAL 255
#define PU_BRIGHTNESS_STEP_SIZE 1
#define PU_BRIGHTNESS_DEFAULT_VAL 55
/* ---------------------------------------------------------------------------
* Generic stuff
*/
/* IO methods supported */
enum io_method {
IO_METHOD_MMAP,
IO_METHOD_USERPTR,
};
/* Buffer representing one video frame */
struct buffer {
struct v4l2_buffer buf;
void *start;
size_t length;
};
/* ---------------------------------------------------------------------------
* UVC specific stuff
*/
struct uvc_frame_info {
unsigned int width;
unsigned int height;
unsigned int intervals[8];
};
struct uvc_format_info {
unsigned int fcc;
const struct uvc_frame_info *frames;
};
static const struct uvc_frame_info uvc_frames_yuyv[] = {
{
WIDTH1,
HEIGHT1,
//{666666, 10000000, 50000000, 0},
{50000000, 0},
},
{
WIDTH2,
HEIGHT2,
{50000000, 0},
},
{
0,
0,
{
0,
},
},
};
static const struct uvc_frame_info uvc_frames_mjpeg[] = {
{
WIDTH1,
HEIGHT1,
//{666666, 10000000, 50000000, 0},
{50000000, 0},
},
{
WIDTH2,
HEIGHT2,
{50000000, 0},
},
{
0,
0,
{
0,
},
},
};
static const struct uvc_format_info uvc_formats[] = {
{V4L2_PIX_FMT_YUYV, uvc_frames_yuyv},
{V4L2_PIX_FMT_MJPEG, uvc_frames_mjpeg},
};
/* ---------------------------------------------------------------------------
* V4L2 and UVC device instances
*/
/* Represents a V4L2 based video capture device */
struct v4l2_device {
/* v4l2 device specific */
int v4l2_fd;
int is_streaming;
char *v4l2_devname;
/* v4l2 buffer specific */
enum io_method io;
struct buffer *mem;
unsigned int nbufs;
/* v4l2 buffer queue and dequeue counters */
unsigned long long int qbuf_count;
unsigned long long int dqbuf_count;
/* uvc device hook */
struct uvc_device *udev;
};
/* Represents a UVC based video output device */
struct uvc_device {
/* uvc device specific */
int uvc_fd;
int is_streaming;
int run_standalone;
char *uvc_devname;
/* uvc control request specific */
struct uvc_streaming_control probe;
struct uvc_streaming_control commit;
int control;
struct uvc_request_data request_error_code;
unsigned int brightness_val;
/* uvc buffer specific */
enum io_method io;
struct buffer *mem;
struct buffer *dummy_buf;
unsigned int nbufs;
unsigned int fcc;
unsigned int width;
unsigned int height;
unsigned int bulk;
uint8_t color;
unsigned int imgsize;
void *imgdata;
/* USB speed specific */
int mult;
int burst;
int maxpkt;
enum usb_device_speed speed;
/* uvc specific flags */
int first_buffer_queued;
int uvc_shutdown_requested;
/* uvc buffer queue and dequeue counters */
unsigned long long int qbuf_count;
unsigned long long int dqbuf_count;
/* v4l2 device hook */
struct v4l2_device *vdev;
};
/* forward declarations */
static int uvc_video_stream(struct uvc_device *dev, int enable);
/* ---------------------------------------------------------------------------
* V4L2 streaming related
*/
static int v4l2_uninit_device(struct v4l2_device *dev)
{
unsigned int i;
int ret;
switch (dev->io) {
case IO_METHOD_MMAP:
for (i = 0; i < dev->nbufs; ++i) {
ret = munmap(dev->mem[i].start, dev->mem[i].length);
if (ret < 0) {
printf("V4L2: munmap failed\n");
return ret;
}
}
free(dev->mem);
break;
case IO_METHOD_USERPTR:
default:
break;
}
return 0;
}
static int v4l2_reqbufs_mmap(struct v4l2_device *dev, int nbufs)
{
struct v4l2_requestbuffers req;
unsigned int i = 0;
int ret;
CLEAR(req);
req.count = nbufs;
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
req.memory = V4L2_MEMORY_MMAP;
ret = ioctl(dev->v4l2_fd, VIDIOC_REQBUFS, &req);
if (ret < 0) {
if (ret == -EINVAL)
printf("V4L2: does not support memory mapping\n");
else
printf("V4L2: VIDIOC_REQBUFS error %s (%d).\n", strerror(errno), errno);
goto err;
}
if (!req.count)
return 0;
if (req.count < 2) {
printf("V4L2: Insufficient buffer memory.\n");
ret = -EINVAL;
goto err;
}
/* Map the buffers. */
dev->mem = calloc(req.count, sizeof dev->mem[0]);
if (!dev->mem) {
printf("V4L2: Out of memory\n");
ret = -ENOMEM;
goto err;
}
for (i = 0; i < req.count; ++i) {
memset(&dev->mem[i].buf, 0, sizeof(dev->mem[i].buf));
dev->mem[i].buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
dev->mem[i].buf.memory = V4L2_MEMORY_MMAP;
dev->mem[i].buf.index = i;
ret = ioctl(dev->v4l2_fd, VIDIOC_QUERYBUF, &(dev->mem[i].buf));
if (ret < 0) {
printf(
"V4L2: VIDIOC_QUERYBUF failed for buf %d: "
"%s (%d).\n",
i, strerror(errno), errno);
ret = -EINVAL;
goto err_free;
}
dev->mem[i].start =
mmap(NULL /* start anywhere */, dev->mem[i].buf.length, PROT_READ | PROT_WRITE /* required */,
MAP_SHARED /* recommended */, dev->v4l2_fd, dev->mem[i].buf.m.offset);
if (MAP_FAILED == dev->mem[i].start) {
printf("V4L2: Unable to map buffer %u: %s (%d).\n", i, strerror(errno), errno);
dev->mem[i].length = 0;
ret = -EINVAL;
goto err_free;
}
dev->mem[i].length = dev->mem[i].buf.length;
printf("V4L2: Buffer %u mapped at address %p, length %d.\n", i, dev->mem[i].start, dev->mem[i].length);
}
dev->nbufs = req.count;
printf("V4L2: %u buffers allocated.\n", req.count);
return 0;
err_free:
free(dev->mem);
err:
return ret;
}
static int v4l2_reqbufs_userptr(struct v4l2_device *dev, int nbufs)
{
struct v4l2_requestbuffers req;
int ret;
CLEAR(req);
req.count = nbufs;
req.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
req.memory = V4L2_MEMORY_USERPTR;
ret = ioctl(dev->v4l2_fd, VIDIOC_REQBUFS, &req);
if (ret < 0) {
if (ret == -EINVAL)
printf("V4L2: does not support user pointer i/o\n");
else
printf("V4L2: VIDIOC_REQBUFS error %s (%d).\n", strerror(errno), errno);
return ret;
}
dev->nbufs = req.count;
printf("V4L2: %u buffers allocated.\n", req.count);
return 0;
}
static int v4l2_reqbufs(struct v4l2_device *dev, int nbufs)
{
int ret = 0;
switch (dev->io) {
case IO_METHOD_MMAP:
ret = v4l2_reqbufs_mmap(dev, nbufs);
break;
case IO_METHOD_USERPTR:
ret = v4l2_reqbufs_userptr(dev, nbufs);
break;
default:
ret = -EINVAL;
break;
}
return ret;
}
static int v4l2_qbuf_mmap(struct v4l2_device *dev)
{
unsigned int i;
int ret;
for (i = 0; i < dev->nbufs; ++i) {
memset(&dev->mem[i].buf, 0, sizeof(dev->mem[i].buf));
dev->mem[i].buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
dev->mem[i].buf.memory = V4L2_MEMORY_MMAP;
dev->mem[i].buf.index = i;
ret = ioctl(dev->v4l2_fd, VIDIOC_QBUF, &(dev->mem[i].buf));
if (ret < 0) {
printf("V4L2: VIDIOC_QBUF failed : %s (%d).\n", strerror(errno), errno);
return ret;
}
dev->qbuf_count++;
}
return 0;
}
static int v4l2_qbuf(struct v4l2_device *dev)
{
int ret = 0;
switch (dev->io) {
case IO_METHOD_MMAP:
ret = v4l2_qbuf_mmap(dev);
break;
case IO_METHOD_USERPTR:
/* Empty. */
ret = 0;
break;
default:
ret = -EINVAL;
break;
}
return ret;
}
static int v4l2_process_data(struct v4l2_device *dev)
{
int ret;
struct v4l2_buffer vbuf;
struct v4l2_buffer ubuf;
/* Return immediately if V4l2 streaming has not yet started. */
if (!dev->is_streaming)
return 0;
if (dev->udev->first_buffer_queued)
if (dev->dqbuf_count >= dev->qbuf_count)
return 0;
/* Dequeue spent buffer rom V4L2 domain. */
CLEAR(vbuf);
vbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
switch (dev->io) {
case IO_METHOD_USERPTR:
vbuf.memory = V4L2_MEMORY_USERPTR;
break;
case IO_METHOD_MMAP:
default:
vbuf.memory = V4L2_MEMORY_MMAP;
break;
}
ret = ioctl(dev->v4l2_fd, VIDIOC_DQBUF, &vbuf);
if (ret < 0) {
return ret;
}
dev->dqbuf_count++;
#ifdef ENABLE_BUFFER_DEBUG
printf("Dequeueing buffer at V4L2 side = %d\n", vbuf.index);
#endif
/* Queue video buffer to UVC domain. */
CLEAR(ubuf);
ubuf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
switch (dev->udev->io) {
case IO_METHOD_MMAP:
ubuf.memory = V4L2_MEMORY_MMAP;
ubuf.length = vbuf.length;
ubuf.index = vbuf.index;
ubuf.bytesused = vbuf.bytesused;
break;
case IO_METHOD_USERPTR:
default:
ubuf.memory = V4L2_MEMORY_USERPTR;
ubuf.m.userptr = (unsigned long)dev->mem[vbuf.index].start;
ubuf.length = dev->mem[vbuf.index].length;
ubuf.index = vbuf.index;
ubuf.bytesused = vbuf.bytesused;
break;
}
ret = ioctl(dev->udev->uvc_fd, VIDIOC_QBUF, &ubuf);
if (ret < 0) {
/* Check for a USB disconnect/shutdown event. */
if (errno == ENODEV) {
dev->udev->uvc_shutdown_requested = 1;
printf(
"UVC: Possible USB shutdown requested from "
"Host, seen during VIDIOC_QBUF\n");
return 0;
} else {
return ret;
}
}
dev->udev->qbuf_count++;
#ifdef ENABLE_BUFFER_DEBUG
printf("Queueing buffer at UVC side = %d\n", ubuf.index);
#endif
if (!dev->udev->first_buffer_queued && !dev->udev->run_standalone) {
uvc_video_stream(dev->udev, 1);
dev->udev->first_buffer_queued = 1;
dev->udev->is_streaming = 1;
}
return 0;
}
/* ---------------------------------------------------------------------------
* V4L2 generic stuff
*/
static int v4l2_get_format(struct v4l2_device *dev)
{
struct v4l2_format fmt;
int ret;
CLEAR(fmt);
fmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
ret = ioctl(dev->v4l2_fd, VIDIOC_G_FMT, &fmt);
if (ret < 0) {
return ret;
}
printf("V4L2: Getting current format: %c%c%c%c %ux%u\n", pixfmtstr(fmt.fmt.pix.pixelformat), fmt.fmt.pix.width,
fmt.fmt.pix.height);
return 0;
}
static int v4l2_set_format(struct v4l2_device *dev, struct v4l2_format *fmt)
{
int ret;
ret = ioctl(dev->v4l2_fd, VIDIOC_S_FMT, fmt);
if (ret < 0) {
printf("V4L2: Unable to set format %s (%d).\n", strerror(errno), errno);
return ret;
}
printf("V4L2: Setting format to: %c%c%c%c %ux%u\n", pixfmtstr(fmt->fmt.pix.pixelformat), fmt->fmt.pix.width,
fmt->fmt.pix.height);
return 0;
}
static int v4l2_set_ctrl(struct v4l2_device *dev, int new_val, int ctrl)
{
struct v4l2_queryctrl queryctrl;
struct v4l2_control control;
int ret;
CLEAR(queryctrl);
switch (ctrl) {
case V4L2_CID_BRIGHTNESS:
queryctrl.id = V4L2_CID_BRIGHTNESS;
ret = ioctl(dev->v4l2_fd, VIDIOC_QUERYCTRL, &queryctrl);
if (-1 == ret) {
if (errno != EINVAL)
printf(
"V4L2: VIDIOC_QUERYCTRL"
" failed: %s (%d).\n",
strerror(errno), errno);
else
printf(
"V4L2_CID_BRIGHTNESS is not"
" supported: %s (%d).\n",
strerror(errno), errno);
return ret;
} else if (queryctrl.flags & V4L2_CTRL_FLAG_DISABLED) {
printf("V4L2_CID_BRIGHTNESS is not supported.\n");
ret = -EINVAL;
return ret;
} else {
CLEAR(control);
control.id = V4L2_CID_BRIGHTNESS;
control.value = new_val;
ret = ioctl(dev->v4l2_fd, VIDIOC_S_CTRL, &control);
if (-1 == ret) {
printf("V4L2: VIDIOC_S_CTRL failed: %s (%d).\n", strerror(errno), errno);
return ret;
}
}
printf("V4L2: Brightness control changed to value = 0x%x\n", new_val);
break;
default:
/* TODO: We don't support any other controls. */
return -EINVAL;
}
return 0;
}
static int v4l2_start_capturing(struct v4l2_device *dev)
{
int type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
int ret;
ret = ioctl(dev->v4l2_fd, VIDIOC_STREAMON, &type);
if (ret < 0) {
printf("V4L2: Unable to start streaming: %s (%d).\n", strerror(errno), errno);
return ret;
}
printf("V4L2: Starting video stream.\n");
return 0;
}
static int v4l2_stop_capturing(struct v4l2_device *dev)
{
enum v4l2_buf_type type;
int ret;
switch (dev->io) {
case IO_METHOD_MMAP:
type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
ret = ioctl(dev->v4l2_fd, VIDIOC_STREAMOFF, &type);
if (ret < 0) {
printf("V4L2: VIDIOC_STREAMOFF failed: %s (%d).\n", strerror(errno), errno);
return ret;
}
break;
default:
/* Nothing to do. */
break;
}
return 0;
}
static int v4l2_open(struct v4l2_device **v4l2, char *devname, struct v4l2_format *s_fmt)
{
struct v4l2_device *dev;
struct v4l2_capability cap;
int fd;
int ret = -EINVAL;
fd = open(devname, O_RDWR | O_NONBLOCK, 0);
if (fd == -1) {
printf("V4L2: device open failed: %s (%d).\n", strerror(errno), errno);
return ret;
}
ret = ioctl(fd, VIDIOC_QUERYCAP, &cap);
if (ret < 0) {
printf("V4L2: VIDIOC_QUERYCAP failed: %s (%d).\n", strerror(errno), errno);
goto err;
}
if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
printf("V4L2: %s is no video capture device\n", devname);
goto err;
}
if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
printf("V4L2: %s does not support streaming i/o\n", devname);
goto err;
}
dev = calloc(1, sizeof *dev);
if (dev == NULL) {
ret = -ENOMEM;
goto err;
}
printf("V4L2 device is %s on bus %s\n", cap.card, cap.bus_info);
dev->v4l2_fd = fd;
/* Get the default image format supported. */
ret = v4l2_get_format(dev);
if (ret < 0)
goto err_free;
/*
* Set the desired image format.
* Note: VIDIOC_S_FMT may change width and height.
*/
ret = v4l2_set_format(dev, s_fmt);
if (ret < 0)
goto err_free;
/* Get the changed image format. */
ret = v4l2_get_format(dev);
if (ret < 0)
goto err_free;
printf("v4l2 open succeeded, file descriptor = %d\n", fd);
*v4l2 = dev;
return 0;
err_free:
free(dev);
err:
close(fd);
return ret;
}
static void v4l2_close(struct v4l2_device *dev)
{
close(dev->v4l2_fd);
free(dev);
}
/* ---------------------------------------------------------------------------
* UVC generic stuff
*/
__attribute__((unused))
static int uvc_video_set_format(struct uvc_device *dev)
{
struct v4l2_format fmt;
int ret;
CLEAR(fmt);
fmt.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
fmt.fmt.pix.width = dev->width;
fmt.fmt.pix.height = dev->height;
fmt.fmt.pix.pixelformat = dev->fcc;
fmt.fmt.pix.field = V4L2_FIELD_NONE;
if (dev->fcc == V4L2_PIX_FMT_MJPEG)
fmt.fmt.pix.sizeimage = dev->imgsize * 1.5;
ret = ioctl(dev->uvc_fd, VIDIOC_S_FMT, &fmt);
if (ret < 0) {
printf("UVC: Unable to set format %s (%d).\n", strerror(errno), errno);
return ret;
}
printf("UVC: Setting format to: %c%c%c%c %ux%u\n", pixfmtstr(dev->fcc), dev->width, dev->height);
return 0;
}
static int uvc_video_stream(struct uvc_device *dev, int enable)
{
int type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
int ret;
if (!enable) {
ret = ioctl(dev->uvc_fd, VIDIOC_STREAMOFF, &type);
if (ret < 0) {
printf("UVC: VIDIOC_STREAMOFF failed: %s (%d).\n", strerror(errno), errno);
return ret;
}
printf("UVC: Stopping video stream.\n");
return 0;
}
ret = ioctl(dev->uvc_fd, VIDIOC_STREAMON, &type);
if (ret < 0) {
printf("UVC: Unable to start streaming %s (%d).\n", strerror(errno), errno);
return ret;
}
printf("UVC: Starting video stream.\n");
dev->uvc_shutdown_requested = 0;
return 0;
}
static int uvc_uninit_device(struct uvc_device *dev)
{
unsigned int i;
int ret;
switch (dev->io) {
case IO_METHOD_MMAP:
for (i = 0; i < dev->nbufs; ++i) {
ret = munmap(dev->mem[i].start, dev->mem[i].length);
if (ret < 0) {
printf("UVC: munmap failed\n");
return ret;
}
}
free(dev->mem);
break;
case IO_METHOD_USERPTR:
default:
if (dev->run_standalone) {
for (i = 0; i < dev->nbufs; ++i)
free(dev->dummy_buf[i].start);
free(dev->dummy_buf);
}
break;
}
return 0;
}
static int uvc_open(struct uvc_device **uvc, char *devname)
{
struct uvc_device *dev;
struct v4l2_capability cap;
int fd;
int ret = -EINVAL;
fd = open(devname, O_RDWR | O_NONBLOCK);
if (fd == -1) {
printf("UVC: device open failed: %s (%d).\n", strerror(errno), errno);
return ret;
}
ret = ioctl(fd, VIDIOC_QUERYCAP, &cap);
if (ret < 0) {
printf("UVC: unable to query uvc device: %s (%d)\n", strerror(errno), errno);
goto err;
}
if (!(cap.capabilities & V4L2_CAP_VIDEO_OUTPUT)) {
printf("UVC: %s is no video output device\n", devname);
goto err;
}
dev = calloc(1, sizeof *dev);
if (dev == NULL) {
ret = -ENOMEM;
goto err;
}
printf("uvc device is %s on bus %s\n", cap.card, cap.bus_info);
printf("uvc open succeeded, file descriptor = %d\n", fd);
dev->uvc_fd = fd;
*uvc = dev;
return 0;
err:
close(fd);
return ret;
}
static void uvc_close(struct uvc_device *dev)
{
close(dev->uvc_fd);
free(dev->imgdata);
free(dev);
}
/* ---------------------------------------------------------------------------
* UVC streaming related
*/
static void uvc_video_fill_buffer(struct uvc_device *dev, struct v4l2_buffer *buf)
{
unsigned int bpl;
unsigned int i;
switch (dev->fcc) {
case V4L2_PIX_FMT_YUYV:
/* Fill the buffer with video data. */
bpl = dev->width * 2;
for (i = 0; i < dev->height; ++i)
memset(dev->mem[buf->index].start + i * bpl, dev->color++, bpl);
buf->bytesused = bpl * dev->height;
break;
case V4L2_PIX_FMT_MJPEG:
memcpy(dev->mem[buf->index].start, dev->imgdata, dev->imgsize);
buf->bytesused = dev->imgsize;
break;
}
}
static int uvc_video_process(struct uvc_device *dev)
{
struct v4l2_buffer ubuf;
struct v4l2_buffer vbuf;
unsigned int i;
int ret;
/*
* Return immediately if UVC video output device has not started
* streaming yet.
*/
if (!dev->is_streaming)
return 0;
/* Prepare a v4l2 buffer to be dequeued from UVC domain. */
CLEAR(ubuf);
ubuf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
switch (dev->io) {
case IO_METHOD_MMAP:
ubuf.memory = V4L2_MEMORY_MMAP;
break;
case IO_METHOD_USERPTR:
default:
ubuf.memory = V4L2_MEMORY_USERPTR;
break;
}
if (dev->run_standalone) {
/* UVC stanalone setup. */
ret = ioctl(dev->uvc_fd, VIDIOC_DQBUF, &ubuf);
if (ret < 0)
return ret;
dev->dqbuf_count++;
#ifdef ENABLE_BUFFER_DEBUG
printf("DeQueued buffer at UVC side = %d\n", ubuf.index);
#endif
uvc_video_fill_buffer(dev, &ubuf);
ret = ioctl(dev->uvc_fd, VIDIOC_QBUF, &ubuf);
if (ret < 0)
return ret;
dev->qbuf_count++;
#ifdef ENABLE_BUFFER_DEBUG
printf("ReQueueing buffer at UVC side = %d\n", ubuf.index);
#endif
} else {
/* UVC - V4L2 integrated path. */
/*
* Return immediately if V4L2 video capture device has not
* started streaming yet or if QBUF was not called even once on
* the UVC side.
*/
if (!dev->vdev->is_streaming || !dev->first_buffer_queued)
return 0;
/*
* Do not dequeue buffers from UVC side until there are atleast
* 2 buffers available at UVC domain.
*/
if (!dev->uvc_shutdown_requested)
if ((dev->dqbuf_count + 1) >= dev->qbuf_count)
return 0;
/* Dequeue the spent buffer from UVC domain */
ret = ioctl(dev->uvc_fd, VIDIOC_DQBUF, &ubuf);
if (ret < 0) {
printf("UVC: Unable to dequeue buffer: %s (%d).\n", strerror(errno), errno);
return ret;
}
if (dev->io == IO_METHOD_USERPTR)
for (i = 0; i < dev->nbufs; ++i)
if (ubuf.m.userptr == (unsigned long)dev->vdev->mem[i].start && ubuf.length == dev->vdev->mem[i].length)
break;
dev->dqbuf_count++;
#ifdef ENABLE_BUFFER_DEBUG
printf("DeQueued buffer at UVC side=%d\n", ubuf.index);
#endif
/*