Skip to content

UVC: move application decision to the application #93192

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 24 additions & 6 deletions include/zephyr/usb/class/usbd_uvc.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,38 @@
*/

/**
* @brief Set the video device that a UVC instance will use.
* @brief Set the video device that a UVC instance will use for control requests.
*
* It will query its supported controls, formats and frame rates, and use this information to
* generate USB descriptors sent to the host.
*
* At runtime, it will forward all USB controls from the host to this device.
* It will query its supported video controls and frame intervals and use this information to
* generate the USB descriptors presented to the host. At runtime, it will forward all USB controls
* from the host to this device.
*
* @note This function must be called before @ref usbd_enable.
*
* @param uvc_dev The UVC device
* @param video_dev The video device that this UVC instance controls
* @param video_dev The video device that this UVC instance send controls requests to
*/
void uvc_set_video_dev(const struct device *uvc_dev, const struct device *video_dev);

/**
* @brief Set the video format capabilities that a UVC instance will present to the host.
*
* This information will be used to generate USB descriptors.
* The minimum and maximum values are considered as discrete values, as width and height ranges
* are not supported by the UVC protocol, but only discrete values.
*
* The format selected by the host can then be queried through the @ref video_get_format API.
*
* @note This function must be called before @ref usbd_enable.
*
* @note The @p fmts memory pointed should remain valid for the entire lifetime of @p uvc_dev.
*
* @param uvc_dev The UVC device
* @param fmt The video format to add
* @return 0 on success, negative error code otherwise
*/
int uvc_add_format(const struct device *const dev, const struct video_format *const fmt);

/**
* @}
*/
Expand Down
126 changes: 111 additions & 15 deletions samples/subsys/usb/uvc/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,95 @@

LOG_MODULE_REGISTER(uvc_sample, LOG_LEVEL_INF);

const struct device *const uvc_dev = DEVICE_DT_GET(DT_NODELABEL(uvc));
const struct device *const video_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_camera));
const static struct device *const uvc_dev = DEVICE_DT_GET(DT_NODELABEL(uvc));
const static struct device *const video_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_camera));

/* Format capabilities of video_dev, usd everywhere through the sampel */
static struct video_caps video_caps = {.type = VIDEO_BUF_TYPE_OUTPUT};

static size_t app_get_min_buf_size(const struct video_format *const fmt)
{
if (video_caps.min_line_count == LINE_COUNT_HEIGHT) {
return fmt->pitch * fmt->height;
} else {
return fmt->pitch * video_caps.min_line_count;
}
}

static bool app_is_standard_format(uint32_t pixfmt)
{
return pixfmt == VIDEO_PIX_FMT_GREY || pixfmt == VIDEO_PIX_FMT_JPEG ||
pixfmt == VIDEO_PIX_FMT_YUYV;
}

/* Check whether the video device supports one of the wisespread image sensor formats */
static bool app_has_standard_formats(void)
{
for (int i = 0;; i++) {
uint32_t pixfmt = video_caps.format_caps[i].pixelformat;

if (pixfmt == 0) {
return false;
}
if (app_is_standard_format(pixfmt)) {
return true;
}
}
}

static void app_add_format(uint32_t pixfmt, uint16_t width, uint16_t height, bool has_std_fmts)
{
struct video_format fmt = {
.pixelformat = pixfmt,
.width = width,
.height = height,
.type = VIDEO_BUF_TYPE_OUTPUT,
};
int ret;

/* If the system has any standard pixel format, only propose them to the host */
if (has_std_fmts && !app_is_standard_format(pixfmt)) {
return;
}

/* Set the format to get the pitch */
ret = video_set_format(video_dev, &fmt);
if (ret != 0) {
LOG_ERR("Could not set the format of %s", video_dev->name);
return;
}

if (app_get_min_buf_size(&fmt) > CONFIG_VIDEO_BUFFER_POOL_SZ_MAX) {
LOG_WRN("Skipping format %ux%u", fmt.width, fmt.height);
return;
}

uvc_add_format(uvc_dev, &fmt);
}

/* Submit to UVC only the formats expected to be working (enough memory for the size, etc.) */
static void app_add_filtered_formats(void)
{
const bool has_std_fmts = app_has_standard_formats();

for (int i = 0; video_caps.format_caps[i].pixelformat != 0; i++) {
const struct video_format_cap *vcap = &video_caps.format_caps[i];

app_add_format(vcap->pixelformat, vcap->width_min, vcap->height_min, has_std_fmts);

if (vcap->width_min != vcap->width_max || vcap->height_min != vcap->height_max) {
app_add_format(vcap->pixelformat, vcap->width_max, vcap->height_max,
has_std_fmts);
}
}
}

int main(void)
{
struct usbd_context *sample_usbd;
struct video_buffer *vbuf;
struct video_format fmt = {0};
struct video_caps caps;
struct video_frmival frmival = {0};
struct k_poll_signal sig;
struct k_poll_event evt[1];
k_timeout_t timeout = K_FOREVER;
Expand All @@ -37,16 +117,18 @@ int main(void)
return -ENODEV;
}

caps.type = VIDEO_BUF_TYPE_OUTPUT;

if (video_get_caps(video_dev, &caps)) {
ret = video_get_caps(video_dev, &video_caps);
if (ret != 0) {
LOG_ERR("Unable to retrieve video capabilities");
return 0;
}

/* Must be done before initializing USB */
/* Must be called before usb_enable() */
uvc_set_video_dev(uvc_dev, video_dev);

/* Must be called before uvc_set_video_dev() */
app_add_filtered_formats();

sample_usbd = sample_usbd_init_device(NULL);
if (sample_usbd == NULL) {
return -ENODEV;
Expand All @@ -59,7 +141,6 @@ int main(void)

LOG_INF("Waiting the host to select the video format");

/* Get the video format once it is selected by the host */
while (true) {
fmt.type = VIDEO_BUF_TYPE_INPUT;

Expand All @@ -75,17 +156,32 @@ int main(void)
k_sleep(K_MSEC(10));
}

LOG_INF("The host selected format '%s' %ux%u, preparing %u buffers of %u bytes",
ret = video_get_frmival(uvc_dev, &frmival);
if (ret != 0) {
LOG_ERR("Failed to get the video frame interval");
return ret;
}

LOG_INF("The host selected format '%s' %ux%u at frame interval %u/%u",
VIDEO_FOURCC_TO_STR(fmt.pixelformat), fmt.width, fmt.height,
CONFIG_VIDEO_BUFFER_POOL_NUM_MAX, fmt.pitch * fmt.height);
frmival.numerator, frmival.denominator);

/* Size to allocate for each buffer */
if (caps.min_line_count == LINE_COUNT_HEIGHT) {
bsize = fmt.pitch * fmt.height;
} else {
bsize = fmt.pitch * caps.min_line_count;
fmt.type = VIDEO_BUF_TYPE_OUTPUT;

ret = video_set_format(video_dev, &fmt);
if (ret != 0) {
LOG_WRN("Could not set the format of %s", video_dev->name);
}

ret = video_set_frmival(video_dev, &frmival);
if (ret != 0) {
LOG_WRN("Could not set the framerate of %s", video_dev->name);
}

bsize = app_get_min_buf_size(&fmt);

LOG_INF("Preparing %u buffers of %u bytes", CONFIG_VIDEO_BUFFER_POOL_NUM_MAX, bsize);

for (int i = 0; i < CONFIG_VIDEO_BUFFER_POOL_NUM_MAX; i++) {
vbuf = video_buffer_alloc(bsize, K_NO_WAIT);
if (vbuf == NULL) {
Expand Down
Loading
Loading