Skip to content

Commit 882181b

Browse files
committed
refactor(uvc): Added possibility to print supported formats
1 parent 19f9fe8 commit 882181b

File tree

3 files changed

+55
-9
lines changed

3 files changed

+55
-9
lines changed

host/class/uvc/usb_host_uvc/Kconfig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,15 @@ menu "USB-Driver: UVC Host"
66
help
77
Print UVC Configuration Descriptor to console.
88

9+
config UVC_HOST_PRINT_SUPPORTED_FORMATS
10+
bool "Print supported formats"
11+
default n
12+
help
13+
When the stream parameters are unknown, this feature could be enabled to show supported formats by the camera.
14+
When enabled, the driver displays interface number, resolution, format and default FPS.
15+
16+
Disabled by default.
17+
918
config UVC_HOST_INTERVAL_ARRAY_SIZE
1019
int "Size of the interval array in uvc_host_frame_info_t"
1120
default 3

host/class/uvc/usb_host_uvc/include/usb/uvc_host.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ enum uvc_host_stream_format {
3838
UVC_VS_FORMAT_YUY2,
3939
UVC_VS_FORMAT_H264,
4040
UVC_VS_FORMAT_H265,
41+
UVC_VS_FORMAT_MAX
4142
};
4243

4344
/**

host/class/uvc/usb_host_uvc/uvc_descriptor_parsing.c

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,33 @@
77
#include <inttypes.h>
88
#include <string.h> // strncmp for guid format parsing
99
#include <math.h> // fabsf for float comparison
10+
#include "esp_log.h"
1011
#include "usb/usb_helpers.h"
1112
#include "usb/uvc_host.h"
1213
#include "uvc_check_priv.h"
1314
#include "uvc_descriptors_priv.h"
15+
#include "sdkconfig.h"
16+
17+
#define UVC_PRINT_SUPPORTED_FORMATS CONFIG_UVC_HOST_PRINT_SUPPORTED_FORMATS
1418

1519
#define FLOAT_EQUAL(a, b) (fabsf(a - b) < 0.0001f) // For comparing float values with acceptable difference (epsilon value)
1620

21+
#if (UVC_PRINT_SUPPORTED_FORMATS)
22+
static const char *format_to_string[UVC_VS_FORMAT_MAX] = {
23+
"Undefined",
24+
"MJPEG",
25+
"YUY2",
26+
"H264",
27+
"H265",
28+
};
29+
30+
static inline uint32_t make_fps(uint32_t frame_interval)
31+
{
32+
return (frame_interval != 0) ? (10000000 / frame_interval) : 0;
33+
}
34+
#endif // UVC_PRINT_SUPPORTED_FORMATS
35+
36+
1737
static const uvc_vs_input_header_desc_t *uvc_desc_get_streaming_input_header(const usb_config_desc_t *cfg_desc, uint8_t bInterfaceNumber)
1838
{
1939
UVC_CHECK(cfg_desc, NULL);
@@ -365,16 +385,32 @@ esp_err_t uvc_desc_get_streaming_interface_num(
365385
}
366386
*bcdUVC = vc_header_desc->bcdUVC;
367387

368-
// Find video streaming interface that offers the requested format
369-
for (int streaming_if = 0; streaming_if < vc_header_desc->bInCollection; streaming_if++) {
370-
uint8_t current_bInterfaceNumber = vc_header_desc->baInterfaceNr[streaming_if];
371-
if (uvc_desc_is_format_supported(cfg_desc, current_bInterfaceNumber, vs_format)) {
372-
*bInterfaceNumber = current_bInterfaceNumber;
373-
ret = ESP_OK;
374-
break;
375-
}
388+
#if (UVC_PRINT_SUPPORTED_FORMATS)
389+
uvc_host_frame_info_t frames[20];
390+
size_t size = 20;
391+
for (int iface = 0; iface < vc_header_desc->bInCollection; iface++) {
392+
ESP_ERROR_CHECK(uvc_desc_get_frame_list(cfg_desc, uvc_index, &frames, &size));
393+
ESP_LOGW("UVC", "Supported frame formats:");
394+
ESP_LOGW("UVC", "\t%dx%d, %s, default FPS: %d",
395+
frames[i].h_res, frames[i].v_res,
396+
format_to_string[frames[i].format],
397+
make_fps(frames[i].default_interval));
376398
}
377-
return ret;
399+
}
400+
#endif // UVC_PRINT_SUPPORTED_FORMATS
401+
402+
// Find video streaming interface that offers the requested format
403+
for (int streaming_if = 0; streaming_if < vc_header_desc->bInCollection; streaming_if++)
404+
{
405+
uint8_t current_bInterfaceNumber = vc_header_desc->baInterfaceNr[streaming_if];
406+
if (uvc_desc_is_format_supported(cfg_desc, current_bInterfaceNumber, vs_format)) {
407+
*bInterfaceNumber = current_bInterfaceNumber;
408+
ret = ESP_OK;
409+
break;
410+
}
411+
}
412+
413+
return ret;
378414
}
379415

380416
bool uvc_desc_is_uvc_device(const usb_config_desc_t *cfg_desc)

0 commit comments

Comments
 (0)