/
stream.c
1562 lines (1321 loc) · 47.1 KB
/
stream.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
/*********************************************************************
* Software License Agreement (BSD License)
*
* Copyright (C) 2010-2012 Ken Tossell
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* * Neither the name of the author nor other contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*********************************************************************/
/**
* @defgroup streaming Streaming control functions
* @brief Tools for creating, managing and consuming video streams
*/
#include "libuvc/libuvc.h"
#include "libuvc/libuvc_internal.h"
#include "errno.h"
#ifdef _MSC_VER
#define DELTA_EPOCH_IN_MICROSECS 116444736000000000Ui64
// gettimeofday - get time of day for Windows;
// A gettimeofday implementation for Microsoft Windows;
// Public domain code, author "ponnada";
int gettimeofday(struct timeval *tv, struct timezone *tz)
{
FILETIME ft;
unsigned __int64 tmpres = 0;
static int tzflag = 0;
if (NULL != tv)
{
GetSystemTimeAsFileTime(&ft);
tmpres |= ft.dwHighDateTime;
tmpres <<= 32;
tmpres |= ft.dwLowDateTime;
tmpres /= 10;
tmpres -= DELTA_EPOCH_IN_MICROSECS;
tv->tv_sec = (long)(tmpres / 1000000UL);
tv->tv_usec = (long)(tmpres % 1000000UL);
}
return 0;
}
#endif // _MSC_VER
uvc_frame_desc_t *uvc_find_frame_desc_stream(uvc_stream_handle_t *strmh,
uint16_t format_id, uint16_t frame_id);
uvc_frame_desc_t *uvc_find_frame_desc(uvc_device_handle_t *devh,
uint16_t format_id, uint16_t frame_id);
void *_uvc_user_caller(void *arg);
void _uvc_populate_frame(uvc_stream_handle_t *strmh);
static uvc_streaming_interface_t *_uvc_get_stream_if(uvc_device_handle_t *devh, int interface_idx);
static uvc_stream_handle_t *_uvc_get_stream_by_interface(uvc_device_handle_t *devh, int interface_idx);
struct format_table_entry {
enum uvc_frame_format format;
uint8_t abstract_fmt;
uint8_t guid[16];
int children_count;
enum uvc_frame_format *children;
};
struct format_table_entry *_get_format_entry(enum uvc_frame_format format) {
#define ABS_FMT(_fmt, _num, ...) \
case _fmt: { \
static enum uvc_frame_format _fmt##_children[] = __VA_ARGS__; \
static struct format_table_entry _fmt##_entry = { \
_fmt, 0, {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}, _num, _fmt##_children }; \
return &_fmt##_entry; }
#define FMT(_fmt, ...) \
case _fmt: { \
static struct format_table_entry _fmt##_entry = { \
_fmt, 0, __VA_ARGS__, 0, NULL }; \
return &_fmt##_entry; }
switch(format) {
/* Define new formats here */
ABS_FMT(UVC_FRAME_FORMAT_ANY, 2,
{UVC_FRAME_FORMAT_UNCOMPRESSED, UVC_FRAME_FORMAT_COMPRESSED})
ABS_FMT(UVC_FRAME_FORMAT_UNCOMPRESSED, 8,
{UVC_FRAME_FORMAT_YUYV, UVC_FRAME_FORMAT_UYVY, UVC_FRAME_FORMAT_GRAY8,
UVC_FRAME_FORMAT_GRAY16, UVC_FRAME_FORMAT_NV12, UVC_FRAME_FORMAT_P010,
UVC_FRAME_FORMAT_BGR, UVC_FRAME_FORMAT_RGB})
FMT(UVC_FRAME_FORMAT_YUYV,
{'Y', 'U', 'Y', '2', 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71})
FMT(UVC_FRAME_FORMAT_UYVY,
{'U', 'Y', 'V', 'Y', 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71})
FMT(UVC_FRAME_FORMAT_GRAY8,
{'Y', '8', '0', '0', 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71})
FMT(UVC_FRAME_FORMAT_GRAY16,
{'Y', '1', '6', ' ', 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71})
FMT(UVC_FRAME_FORMAT_NV12,
{'N', 'V', '1', '2', 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71})
FMT(UVC_FRAME_FORMAT_P010,
{'P', '0', '1', '0', 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71})
FMT(UVC_FRAME_FORMAT_BGR,
{0x7d, 0xeb, 0x36, 0xe4, 0x4f, 0x52, 0xce, 0x11, 0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70})
FMT(UVC_FRAME_FORMAT_RGB,
{0x7e, 0xeb, 0x36, 0xe4, 0x4f, 0x52, 0xce, 0x11, 0x9f, 0x53, 0x00, 0x20, 0xaf, 0x0b, 0xa7, 0x70})
FMT(UVC_FRAME_FORMAT_BY8,
{'B', 'Y', '8', ' ', 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71})
FMT(UVC_FRAME_FORMAT_BA81,
{'B', 'A', '8', '1', 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71})
FMT(UVC_FRAME_FORMAT_SGRBG8,
{'G', 'R', 'B', 'G', 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71})
FMT(UVC_FRAME_FORMAT_SGBRG8,
{'G', 'B', 'R', 'G', 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71})
FMT(UVC_FRAME_FORMAT_SRGGB8,
{'R', 'G', 'G', 'B', 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71})
FMT(UVC_FRAME_FORMAT_SBGGR8,
{'B', 'G', 'G', 'R', 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71})
ABS_FMT(UVC_FRAME_FORMAT_COMPRESSED, 2,
{UVC_FRAME_FORMAT_MJPEG, UVC_FRAME_FORMAT_H264})
FMT(UVC_FRAME_FORMAT_MJPEG,
{'M', 'J', 'P', 'G'})
FMT(UVC_FRAME_FORMAT_H264,
{'H', '2', '6', '4', 0x00, 0x00, 0x10, 0x00, 0x80, 0x00, 0x00, 0xaa, 0x00, 0x38, 0x9b, 0x71})
default:
return NULL;
}
#undef ABS_FMT
#undef FMT
}
static uint8_t _uvc_frame_format_matches_guid(enum uvc_frame_format fmt, uint8_t guid[16]) {
struct format_table_entry *format;
int child_idx;
format = _get_format_entry(fmt);
if (!format)
return 0;
if (!format->abstract_fmt && !memcmp(guid, format->guid, 16))
return 1;
for (child_idx = 0; child_idx < format->children_count; child_idx++) {
if (_uvc_frame_format_matches_guid(format->children[child_idx], guid))
return 1;
}
return 0;
}
static enum uvc_frame_format uvc_frame_format_for_guid(uint8_t guid[16]) {
struct format_table_entry *format;
enum uvc_frame_format fmt;
for (fmt = 0; fmt < UVC_FRAME_FORMAT_COUNT; ++fmt) {
format = _get_format_entry(fmt);
if (!format || format->abstract_fmt)
continue;
if (!memcmp(format->guid, guid, 16))
return format->format;
}
return UVC_FRAME_FORMAT_UNKNOWN;
}
/** @internal
* Run a streaming control query
* @param[in] devh UVC device
* @param[in,out] ctrl Control block
* @param[in] probe Whether this is a probe query or a commit query
* @param[in] req Query type
*/
uvc_error_t uvc_query_stream_ctrl(
uvc_device_handle_t *devh,
uvc_stream_ctrl_t *ctrl,
uint8_t probe,
enum uvc_req_code req) {
uint8_t buf[34];
size_t len;
uvc_error_t err;
memset(buf, 0, sizeof(buf));
if (devh->info->ctrl_if.bcdUVC >= 0x0110)
len = 34;
else
len = 26;
/* prepare for a SET transfer */
if (req == UVC_SET_CUR) {
SHORT_TO_SW(ctrl->bmHint, buf);
buf[2] = ctrl->bFormatIndex;
buf[3] = ctrl->bFrameIndex;
INT_TO_DW(ctrl->dwFrameInterval, buf + 4);
SHORT_TO_SW(ctrl->wKeyFrameRate, buf + 8);
SHORT_TO_SW(ctrl->wPFrameRate, buf + 10);
SHORT_TO_SW(ctrl->wCompQuality, buf + 12);
SHORT_TO_SW(ctrl->wCompWindowSize, buf + 14);
SHORT_TO_SW(ctrl->wDelay, buf + 16);
INT_TO_DW(ctrl->dwMaxVideoFrameSize, buf + 18);
INT_TO_DW(ctrl->dwMaxPayloadTransferSize, buf + 22);
if (len == 34) {
INT_TO_DW ( ctrl->dwClockFrequency, buf + 26 );
buf[30] = ctrl->bmFramingInfo;
buf[31] = ctrl->bPreferredVersion;
buf[32] = ctrl->bMinVersion;
buf[33] = ctrl->bMaxVersion;
/** @todo support UVC 1.1 */
}
}
/* do the transfer */
err = libusb_control_transfer(
devh->usb_devh,
req == UVC_SET_CUR ? 0x21 : 0xA1,
req,
probe ? (UVC_VS_PROBE_CONTROL << 8) : (UVC_VS_COMMIT_CONTROL << 8),
ctrl->bInterfaceNumber,
buf, len, 0
);
if (err <= 0) {
return err;
}
/* now decode following a GET transfer */
if (req != UVC_SET_CUR) {
ctrl->bmHint = SW_TO_SHORT(buf);
ctrl->bFormatIndex = buf[2];
ctrl->bFrameIndex = buf[3];
ctrl->dwFrameInterval = DW_TO_INT(buf + 4);
ctrl->wKeyFrameRate = SW_TO_SHORT(buf + 8);
ctrl->wPFrameRate = SW_TO_SHORT(buf + 10);
ctrl->wCompQuality = SW_TO_SHORT(buf + 12);
ctrl->wCompWindowSize = SW_TO_SHORT(buf + 14);
ctrl->wDelay = SW_TO_SHORT(buf + 16);
ctrl->dwMaxVideoFrameSize = DW_TO_INT(buf + 18);
ctrl->dwMaxPayloadTransferSize = DW_TO_INT(buf + 22);
if (len == 34) {
ctrl->dwClockFrequency = DW_TO_INT ( buf + 26 );
ctrl->bmFramingInfo = buf[30];
ctrl->bPreferredVersion = buf[31];
ctrl->bMinVersion = buf[32];
ctrl->bMaxVersion = buf[33];
/** @todo support UVC 1.1 */
}
else
ctrl->dwClockFrequency = devh->info->ctrl_if.dwClockFrequency;
/* fix up block for cameras that fail to set dwMax* */
if (ctrl->dwMaxVideoFrameSize == 0) {
uvc_frame_desc_t *frame = uvc_find_frame_desc(devh, ctrl->bFormatIndex, ctrl->bFrameIndex);
if (frame) {
ctrl->dwMaxVideoFrameSize = frame->dwMaxVideoFrameBufferSize;
}
}
}
return UVC_SUCCESS;
}
/** @internal
* Run a streaming control query
* @param[in] devh UVC device
* @param[in,out] ctrl Control block
* @param[in] probe Whether this is a probe query or a commit query
* @param[in] req Query type
*/
uvc_error_t uvc_query_still_ctrl(
uvc_device_handle_t *devh,
uvc_still_ctrl_t *still_ctrl,
uint8_t probe,
enum uvc_req_code req) {
uint8_t buf[11];
const size_t len = 11;
uvc_error_t err;
memset(buf, 0, sizeof(buf));
if (req == UVC_SET_CUR) {
/* prepare for a SET transfer */
buf[0] = still_ctrl->bFormatIndex;
buf[1] = still_ctrl->bFrameIndex;
buf[2] = still_ctrl->bCompressionIndex;
INT_TO_DW(still_ctrl->dwMaxVideoFrameSize, buf + 3);
INT_TO_DW(still_ctrl->dwMaxPayloadTransferSize, buf + 7);
}
/* do the transfer */
err = libusb_control_transfer(
devh->usb_devh,
req == UVC_SET_CUR ? 0x21 : 0xA1,
req,
probe ? (UVC_VS_STILL_PROBE_CONTROL << 8) : (UVC_VS_STILL_COMMIT_CONTROL << 8),
still_ctrl->bInterfaceNumber,
buf, len, 0
);
if (err <= 0) {
return err;
}
/* now decode following a GET transfer */
if (req != UVC_SET_CUR) {
still_ctrl->bFormatIndex = buf[0];
still_ctrl->bFrameIndex = buf[1];
still_ctrl->bCompressionIndex = buf[2];
still_ctrl->dwMaxVideoFrameSize = DW_TO_INT(buf + 3);
still_ctrl->dwMaxPayloadTransferSize = DW_TO_INT(buf + 7);
}
return UVC_SUCCESS;
}
/** Initiate a method 2 (in stream) still capture
* @ingroup streaming
*
* @param[in] devh Device handle
* @param[in] still_ctrl Still capture control block
*/
uvc_error_t uvc_trigger_still(
uvc_device_handle_t *devh,
uvc_still_ctrl_t *still_ctrl) {
uvc_stream_handle_t* stream;
uvc_streaming_interface_t* stream_if;
uint8_t buf;
uvc_error_t err;
/* Stream must be running for method 2 to work */
stream = _uvc_get_stream_by_interface(devh, still_ctrl->bInterfaceNumber);
if (!stream || !stream->running)
return UVC_ERROR_NOT_SUPPORTED;
/* Only method 2 is supported */
stream_if = _uvc_get_stream_if(devh, still_ctrl->bInterfaceNumber);
if(!stream_if || stream_if->bStillCaptureMethod != 2)
return UVC_ERROR_NOT_SUPPORTED;
/* prepare for a SET transfer */
buf = 1;
/* do the transfer */
err = libusb_control_transfer(
devh->usb_devh,
0x21, //type set
UVC_SET_CUR,
(UVC_VS_STILL_IMAGE_TRIGGER_CONTROL << 8),
still_ctrl->bInterfaceNumber,
&buf, 1, 0);
if (err <= 0) {
return err;
}
return UVC_SUCCESS;
}
/** @brief Reconfigure stream with a new stream format.
* @ingroup streaming
*
* This may be executed whether or not the stream is running.
*
* @param[in] strmh Stream handle
* @param[in] ctrl Control block, processed using {uvc_probe_stream_ctrl} or
* {uvc_get_stream_ctrl_format_size}
*/
uvc_error_t uvc_stream_ctrl(uvc_stream_handle_t *strmh, uvc_stream_ctrl_t *ctrl) {
uvc_error_t ret;
if (strmh->stream_if->bInterfaceNumber != ctrl->bInterfaceNumber)
return UVC_ERROR_INVALID_PARAM;
/* @todo Allow the stream to be modified without restarting the stream */
if (strmh->running)
return UVC_ERROR_BUSY;
ret = uvc_query_stream_ctrl(strmh->devh, ctrl, 0, UVC_SET_CUR);
if (ret != UVC_SUCCESS)
return ret;
strmh->cur_ctrl = *ctrl;
return UVC_SUCCESS;
}
/** @internal
* @brief Find the descriptor for a specific frame configuration
* @param stream_if Stream interface
* @param format_id Index of format class descriptor
* @param frame_id Index of frame descriptor
*/
static uvc_frame_desc_t *_uvc_find_frame_desc_stream_if(uvc_streaming_interface_t *stream_if,
uint16_t format_id, uint16_t frame_id) {
uvc_format_desc_t *format = NULL;
uvc_frame_desc_t *frame = NULL;
DL_FOREACH(stream_if->format_descs, format) {
if (format->bFormatIndex == format_id) {
DL_FOREACH(format->frame_descs, frame) {
if (frame->bFrameIndex == frame_id)
return frame;
}
}
}
return NULL;
}
uvc_frame_desc_t *uvc_find_frame_desc_stream(uvc_stream_handle_t *strmh,
uint16_t format_id, uint16_t frame_id) {
return _uvc_find_frame_desc_stream_if(strmh->stream_if, format_id, frame_id);
}
/** @internal
* @brief Find the descriptor for a specific frame configuration
* @param devh UVC device
* @param format_id Index of format class descriptor
* @param frame_id Index of frame descriptor
*/
uvc_frame_desc_t *uvc_find_frame_desc(uvc_device_handle_t *devh,
uint16_t format_id, uint16_t frame_id) {
uvc_streaming_interface_t *stream_if;
uvc_frame_desc_t *frame;
DL_FOREACH(devh->info->stream_ifs, stream_if) {
frame = _uvc_find_frame_desc_stream_if(stream_if, format_id, frame_id);
if (frame)
return frame;
}
return NULL;
}
/** Get a negotiated streaming control block for some common parameters.
* @ingroup streaming
*
* @param[in] devh Device handle
* @param[in,out] ctrl Control block
* @param[in] format_class Type of streaming format
* @param[in] width Desired frame width
* @param[in] height Desired frame height
* @param[in] fps Frame rate, frames per second
*/
uvc_error_t uvc_get_stream_ctrl_format_size(
uvc_device_handle_t *devh,
uvc_stream_ctrl_t *ctrl,
enum uvc_frame_format cf,
int width, int height,
int fps) {
uvc_streaming_interface_t *stream_if;
/* find a matching frame descriptor and interval */
DL_FOREACH(devh->info->stream_ifs, stream_if) {
uvc_format_desc_t *format;
DL_FOREACH(stream_if->format_descs, format) {
uvc_frame_desc_t *frame;
if (!_uvc_frame_format_matches_guid(cf, format->guidFormat))
continue;
DL_FOREACH(format->frame_descs, frame) {
if (frame->wWidth != width || frame->wHeight != height)
continue;
uint32_t *interval;
ctrl->bInterfaceNumber = stream_if->bInterfaceNumber;
UVC_DEBUG("claiming streaming interface %d", stream_if->bInterfaceNumber );
uvc_claim_if(devh, ctrl->bInterfaceNumber);
/* get the max values */
uvc_query_stream_ctrl( devh, ctrl, 1, UVC_GET_MAX);
if (frame->intervals) {
for (interval = frame->intervals; *interval; ++interval) {
// allow a fps rate of zero to mean "accept first rate available"
if (10000000 / *interval == (unsigned int) fps || fps == 0) {
ctrl->bmHint = (1 << 0); /* don't negotiate interval */
ctrl->bFormatIndex = format->bFormatIndex;
ctrl->bFrameIndex = frame->bFrameIndex;
ctrl->dwFrameInterval = *interval;
goto found;
}
}
} else {
uint32_t interval_100ns = 10000000 / fps;
uint32_t interval_offset = interval_100ns - frame->dwMinFrameInterval;
if (interval_100ns >= frame->dwMinFrameInterval
&& interval_100ns <= frame->dwMaxFrameInterval
&& !(interval_offset
&& (interval_offset % frame->dwFrameIntervalStep))) {
ctrl->bmHint = (1 << 0);
ctrl->bFormatIndex = format->bFormatIndex;
ctrl->bFrameIndex = frame->bFrameIndex;
ctrl->dwFrameInterval = interval_100ns;
goto found;
}
}
}
}
}
return UVC_ERROR_INVALID_MODE;
found:
return uvc_probe_stream_ctrl(devh, ctrl);
}
/** Get a negotiated still control block for some common parameters.
* @ingroup streaming
*
* @param[in] devh Device handle
* @param[in] ctrl Control block
* @param[in, out] still_ctrl Still capture control block
* @param[in] width Desired frame width
* @param[in] height Desired frame height
*/
uvc_error_t uvc_get_still_ctrl_format_size(
uvc_device_handle_t *devh,
uvc_stream_ctrl_t *ctrl,
uvc_still_ctrl_t *still_ctrl,
int width, int height) {
uvc_streaming_interface_t *stream_if;
uvc_still_frame_desc_t *still;
uvc_format_desc_t *format;
uvc_still_frame_res_t *sizePattern;
stream_if = _uvc_get_stream_if(devh, ctrl->bInterfaceNumber);
/* Only method 2 is supported */
if(!stream_if || stream_if->bStillCaptureMethod != 2)
return UVC_ERROR_NOT_SUPPORTED;
DL_FOREACH(stream_if->format_descs, format) {
if (ctrl->bFormatIndex != format->bFormatIndex)
continue;
/* get the max values */
uvc_query_still_ctrl(devh, still_ctrl, 1, UVC_GET_MAX);
//look for still format
DL_FOREACH(format->still_frame_desc, still) {
DL_FOREACH(still->imageSizePatterns, sizePattern) {
if (sizePattern->wWidth != width || sizePattern->wHeight != height)
continue;
still_ctrl->bInterfaceNumber = ctrl->bInterfaceNumber;
still_ctrl->bFormatIndex = format->bFormatIndex;
still_ctrl->bFrameIndex = sizePattern->bResolutionIndex;
still_ctrl->bCompressionIndex = 0; //TODO support compression index
goto found;
}
}
}
return UVC_ERROR_INVALID_MODE;
found:
return uvc_probe_still_ctrl(devh, still_ctrl);
}
static int _uvc_stream_params_negotiated(
uvc_stream_ctrl_t *required,
uvc_stream_ctrl_t *actual) {
return required->bFormatIndex == actual->bFormatIndex &&
required->bFrameIndex == actual->bFrameIndex &&
required->dwMaxPayloadTransferSize >= actual->dwMaxPayloadTransferSize;
}
/** @internal
* Negotiate streaming parameters with the device
*
* @param[in] devh UVC device
* @param[in,out] ctrl Control block
*/
uvc_error_t uvc_probe_stream_ctrl(
uvc_device_handle_t *devh,
uvc_stream_ctrl_t *ctrl) {
uvc_stream_ctrl_t required_ctrl = *ctrl;
uvc_query_stream_ctrl( devh, ctrl, 1, UVC_SET_CUR );
uvc_query_stream_ctrl( devh, ctrl, 1, UVC_GET_CUR );
if(!_uvc_stream_params_negotiated(&required_ctrl, ctrl)) {
UVC_DEBUG("Unable to negotiate streaming format");
return UVC_ERROR_INVALID_MODE;
}
return UVC_SUCCESS;
}
/** @internal
* Negotiate still parameters with the device
*
* @param[in] devh UVC device
* @param[in,out] still_ctrl Still capture control block
*/
uvc_error_t uvc_probe_still_ctrl(
uvc_device_handle_t *devh,
uvc_still_ctrl_t *still_ctrl) {
int res = uvc_query_still_ctrl(
devh, still_ctrl, 1, UVC_SET_CUR
);
if(res == UVC_SUCCESS) {
res = uvc_query_still_ctrl(
devh, still_ctrl, 1, UVC_GET_CUR
);
if(res == UVC_SUCCESS) {
res = uvc_query_still_ctrl(
devh, still_ctrl, 0, UVC_SET_CUR
);
}
}
return res;
}
/** @internal
* @brief Swap the working buffer with the presented buffer and notify consumers
*/
void _uvc_swap_buffers(uvc_stream_handle_t *strmh) {
uint8_t *tmp_buf;
pthread_mutex_lock(&strmh->cb_mutex);
(void)clock_gettime(CLOCK_MONOTONIC, &strmh->capture_time_finished);
/* swap the buffers */
tmp_buf = strmh->holdbuf;
strmh->hold_bytes = strmh->got_bytes;
strmh->holdbuf = strmh->outbuf;
strmh->outbuf = tmp_buf;
strmh->hold_last_scr = strmh->last_scr;
strmh->hold_pts = strmh->pts;
strmh->hold_seq = strmh->seq;
/* swap metadata buffer */
tmp_buf = strmh->meta_holdbuf;
strmh->meta_holdbuf = strmh->meta_outbuf;
strmh->meta_outbuf = tmp_buf;
strmh->meta_hold_bytes = strmh->meta_got_bytes;
pthread_cond_broadcast(&strmh->cb_cond);
pthread_mutex_unlock(&strmh->cb_mutex);
strmh->seq++;
strmh->got_bytes = 0;
strmh->meta_got_bytes = 0;
strmh->last_scr = 0;
strmh->pts = 0;
}
/** @internal
* @brief Process a payload transfer
*
* Processes stream, places frames into buffer, signals listeners
* (such as user callback thread and any polling thread) on new frame
*
* @param payload Contents of the payload transfer, either a packet (isochronous) or a full
* transfer (bulk mode)
* @param payload_len Length of the payload transfer
*/
void _uvc_process_payload(uvc_stream_handle_t *strmh, uint8_t *payload, size_t payload_len) {
size_t header_len;
uint8_t header_info;
size_t data_len;
/* magic numbers for identifying header packets from some iSight cameras */
static uint8_t isight_tag[] = {
0x11, 0x22, 0x33, 0x44,
0xde, 0xad, 0xbe, 0xef, 0xde, 0xad, 0xfa, 0xce
};
/* ignore empty payload transfers */
if (payload_len == 0)
return;
/* Certain iSight cameras have strange behavior: They send header
* information in a packet with no image data, and then the following
* packets have only image data, with no more headers until the next frame.
*
* The iSight header: len(1), flags(1 or 2), 0x11223344(4),
* 0xdeadbeefdeadface(8), ??(16)
*/
if (strmh->devh->is_isight &&
(payload_len < 14 || memcmp(isight_tag, payload + 2, sizeof(isight_tag))) &&
(payload_len < 15 || memcmp(isight_tag, payload + 3, sizeof(isight_tag)))) {
/* The payload transfer doesn't have any iSight magic, so it's all image data */
header_len = 0;
data_len = payload_len;
} else {
header_len = payload[0];
if (header_len > payload_len) {
UVC_DEBUG("bogus packet: actual_len=%zd, header_len=%zd\n", payload_len, header_len);
return;
}
if (strmh->devh->is_isight)
data_len = 0;
else
data_len = payload_len - header_len;
}
if (header_len < 2) {
header_info = 0;
} else {
/** @todo we should be checking the end-of-header bit */
size_t variable_offset = 2;
header_info = payload[1];
if (header_info & 0x40) {
UVC_DEBUG("bad packet: error bit set");
return;
}
if (strmh->fid != (header_info & 1) && strmh->got_bytes != 0) {
/* The frame ID bit was flipped, but we have image data sitting
around from prior transfers. This means the camera didn't send
an EOF for the last transfer of the previous frame. */
_uvc_swap_buffers(strmh);
}
strmh->fid = header_info & 1;
if (header_info & (1 << 2)) {
strmh->pts = DW_TO_INT(payload + variable_offset);
variable_offset += 4;
}
if (header_info & (1 << 3)) {
/** @todo read the SOF token counter */
strmh->last_scr = DW_TO_INT(payload + variable_offset);
variable_offset += 6;
}
if (header_len > variable_offset) {
// Metadata is attached to header
size_t meta_len = header_len - variable_offset;
if (strmh->meta_got_bytes + meta_len > LIBUVC_XFER_META_BUF_SIZE)
meta_len = LIBUVC_XFER_META_BUF_SIZE - strmh->meta_got_bytes; /* Avoid overflow. */
memcpy(strmh->meta_outbuf + strmh->meta_got_bytes, payload + variable_offset, meta_len);
strmh->meta_got_bytes += meta_len;
}
}
if (data_len > 0) {
if (strmh->got_bytes + data_len > strmh->cur_ctrl.dwMaxVideoFrameSize)
data_len = strmh->cur_ctrl.dwMaxVideoFrameSize - strmh->got_bytes; /* Avoid overflow. */
memcpy(strmh->outbuf + strmh->got_bytes, payload + header_len, data_len);
strmh->got_bytes += data_len;
if (header_info & (1 << 1) || strmh->got_bytes == strmh->cur_ctrl.dwMaxVideoFrameSize) {
/* The EOF bit is set, so publish the complete frame */
_uvc_swap_buffers(strmh);
}
}
}
/** @internal
* @brief Stream transfer callback
*
* Processes stream, places frames into buffer, signals listeners
* (such as user callback thread and any polling thread) on new frame
*
* @param transfer Active transfer
*/
void LIBUSB_CALL _uvc_stream_callback(struct libusb_transfer *transfer) {
uvc_stream_handle_t *strmh = transfer->user_data;
int resubmit = 1;
switch (transfer->status) {
case LIBUSB_TRANSFER_COMPLETED:
if (transfer->num_iso_packets == 0) {
/* This is a bulk mode transfer, so it just has one payload transfer */
_uvc_process_payload(strmh, transfer->buffer, transfer->actual_length);
} else {
/* This is an isochronous mode transfer, so each packet has a payload transfer */
int packet_id;
for (packet_id = 0; packet_id < transfer->num_iso_packets; ++packet_id) {
uint8_t *pktbuf;
struct libusb_iso_packet_descriptor *pkt;
pkt = transfer->iso_packet_desc + packet_id;
if (pkt->status != 0) {
UVC_DEBUG("bad packet (isochronous transfer); status: %d", pkt->status);
continue;
}
pktbuf = libusb_get_iso_packet_buffer_simple(transfer, packet_id);
_uvc_process_payload(strmh, pktbuf, pkt->actual_length);
}
}
break;
case LIBUSB_TRANSFER_CANCELLED:
case LIBUSB_TRANSFER_ERROR:
case LIBUSB_TRANSFER_NO_DEVICE: {
int i;
UVC_DEBUG("not retrying transfer, status = %d", transfer->status);
pthread_mutex_lock(&strmh->cb_mutex);
/* Mark transfer as deleted. */
for(i=0; i < LIBUVC_NUM_TRANSFER_BUFS; i++) {
if(strmh->transfers[i] == transfer) {
UVC_DEBUG("Freeing transfer %d (%p)", i, transfer);
free(transfer->buffer);
libusb_free_transfer(transfer);
strmh->transfers[i] = NULL;
break;
}
}
if(i == LIBUVC_NUM_TRANSFER_BUFS ) {
UVC_DEBUG("transfer %p not found; not freeing!", transfer);
}
resubmit = 0;
pthread_cond_broadcast(&strmh->cb_cond);
pthread_mutex_unlock(&strmh->cb_mutex);
break;
}
case LIBUSB_TRANSFER_TIMED_OUT:
case LIBUSB_TRANSFER_STALL:
case LIBUSB_TRANSFER_OVERFLOW:
UVC_DEBUG("retrying transfer, status = %d", transfer->status);
break;
}
if ( resubmit ) {
if ( strmh->running ) {
int libusbRet = libusb_submit_transfer(transfer);
if (libusbRet < 0)
{
int i;
pthread_mutex_lock(&strmh->cb_mutex);
/* Mark transfer as deleted. */
for (i = 0; i < LIBUVC_NUM_TRANSFER_BUFS; i++) {
if (strmh->transfers[i] == transfer) {
UVC_DEBUG("Freeing failed transfer %d (%p)", i, transfer);
free(transfer->buffer);
libusb_free_transfer(transfer);
strmh->transfers[i] = NULL;
break;
}
}
if (i == LIBUVC_NUM_TRANSFER_BUFS) {
UVC_DEBUG("failed transfer %p not found; not freeing!", transfer);
}
pthread_cond_broadcast(&strmh->cb_cond);
pthread_mutex_unlock(&strmh->cb_mutex);
}
} else {
int i;
pthread_mutex_lock(&strmh->cb_mutex);
/* Mark transfer as deleted. */
for(i=0; i < LIBUVC_NUM_TRANSFER_BUFS; i++) {
if(strmh->transfers[i] == transfer) {
UVC_DEBUG("Freeing orphan transfer %d (%p)", i, transfer);
free(transfer->buffer);
libusb_free_transfer(transfer);
strmh->transfers[i] = NULL;
break;
}
}
if(i == LIBUVC_NUM_TRANSFER_BUFS ) {
UVC_DEBUG("orphan transfer %p not found; not freeing!", transfer);
}
pthread_cond_broadcast(&strmh->cb_cond);
pthread_mutex_unlock(&strmh->cb_mutex);
}
}
}
/** Begin streaming video from the camera into the callback function.
* @ingroup streaming
*
* @param devh UVC device
* @param ctrl Control block, processed using {uvc_probe_stream_ctrl} or
* {uvc_get_stream_ctrl_format_size}
* @param cb User callback function. See {uvc_frame_callback_t} for restrictions.
* @param flags Stream setup flags, currently undefined. Set this to zero. The lower bit
* is reserved for backward compatibility.
*/
uvc_error_t uvc_start_streaming(
uvc_device_handle_t *devh,
uvc_stream_ctrl_t *ctrl,
uvc_frame_callback_t *cb,
void *user_ptr,
uint8_t flags
) {
uvc_error_t ret;
uvc_stream_handle_t *strmh;
ret = uvc_stream_open_ctrl(devh, &strmh, ctrl);
if (ret != UVC_SUCCESS)
return ret;
ret = uvc_stream_start(strmh, cb, user_ptr, flags);
if (ret != UVC_SUCCESS) {
uvc_stream_close(strmh);
return ret;
}
return UVC_SUCCESS;
}
/** Begin streaming video from the camera into the callback function.
* @ingroup streaming
*
* @deprecated The stream type (bulk vs. isochronous) will be determined by the
* type of interface associated with the uvc_stream_ctrl_t parameter, regardless
* of whether the caller requests isochronous streaming. Please switch to
* uvc_start_streaming().
*
* @param devh UVC device
* @param ctrl Control block, processed using {uvc_probe_stream_ctrl} or
* {uvc_get_stream_ctrl_format_size}
* @param cb User callback function. See {uvc_frame_callback_t} for restrictions.
*/
uvc_error_t uvc_start_iso_streaming(
uvc_device_handle_t *devh,
uvc_stream_ctrl_t *ctrl,
uvc_frame_callback_t *cb,
void *user_ptr
) {
return uvc_start_streaming(devh, ctrl, cb, user_ptr, 0);
}
static uvc_stream_handle_t *_uvc_get_stream_by_interface(uvc_device_handle_t *devh, int interface_idx) {
uvc_stream_handle_t *strmh;
DL_FOREACH(devh->streams, strmh) {
if (strmh->stream_if->bInterfaceNumber == interface_idx)
return strmh;
}
return NULL;
}
static uvc_streaming_interface_t *_uvc_get_stream_if(uvc_device_handle_t *devh, int interface_idx) {
uvc_streaming_interface_t *stream_if;
DL_FOREACH(devh->info->stream_ifs, stream_if) {
if (stream_if->bInterfaceNumber == interface_idx)
return stream_if;
}
return NULL;
}
/** Open a new video stream.
* @ingroup streaming
*