Refactor UVCStream buffer management and improve serial number formatting

This commit is contained in:
PhosphorosVR
2025-08-23 15:55:30 +02:00
parent 4778ae6c52
commit e1838b4ca4
3 changed files with 25 additions and 11 deletions

View File

@@ -1,10 +1,10 @@
#include "UVCStream.hpp"
constexpr int UVC_MAX_FRAMESIZE_SIZE(75 * 1024);
#include <cstdio> // for snprintf
static const char *UVC_STREAM_TAG = "[UVC DEVICE]";
extern "C" {
static char serial_number_str[18];
static char serial_number_str[13];
const char *get_uvc_device_name()
{
@@ -23,9 +23,9 @@ extern "C" {
return CONFIG_TUSB_SERIAL_NUM;
}
sniprintf(serial_number_str, sizeof(serial_number_str), "%02X:%02X:%02X:%02X:%02X:%02X",
mac_address[0], mac_address[1], mac_address[2], mac_address[3], mac_address[4], mac_address[5]
);
// 12 hex chars without separators
snprintf(serial_number_str, sizeof(serial_number_str), "%02X%02X%02X%02X%02X%02X",
mac_address[0], mac_address[1], mac_address[2], mac_address[3], mac_address[4], mac_address[5]);
}
return serial_number_str;
}
@@ -33,7 +33,7 @@ extern "C" {
static esp_err_t UVCStreamHelpers::camera_start_cb(uvc_format_t format, int width, int height, int rate, void *cb_ctx)
{
(void)cb_ctx;
auto *mgr = static_cast<UVCStreamManager *>(cb_ctx);
ESP_LOGI(UVC_STREAM_TAG, "Camera Start");
ESP_LOGI(UVC_STREAM_TAG, "Format: %d, width: %d, height: %d, rate: %d", format, width, height, rate);
framesize_t frame_size = FRAMESIZE_QVGA;
@@ -78,7 +78,7 @@ static void UVCStreamHelpers::camera_stop_cb(void *cb_ctx)
static uvc_fb_t *UVCStreamHelpers::camera_fb_get_cb(void *cb_ctx)
{
(void)cb_ctx;
auto *mgr = static_cast<UVCStreamManager *>(cb_ctx);
s_fb.cam_fb_p = esp_camera_fb_get();
if (!s_fb.cam_fb_p)
@@ -93,9 +93,10 @@ static uvc_fb_t *UVCStreamHelpers::camera_fb_get_cb(void *cb_ctx)
s_fb.uvc_fb.format = UVC_FORMAT_JPEG; // we gotta make sure we're ALWAYS using JPEG
s_fb.uvc_fb.timestamp = s_fb.cam_fb_p->timestamp;
if (s_fb.uvc_fb.len > UVC_MAX_FRAMESIZE_SIZE)
// Ensure frame fits into configured UVC transfer buffer
if (mgr && s_fb.uvc_fb.len > mgr->getUvcBufferSize())
{
ESP_LOGE(UVC_STREAM_TAG, "Frame size %d is larger than max frame size %d", s_fb.uvc_fb.len, UVC_MAX_FRAMESIZE_SIZE);
ESP_LOGE(UVC_STREAM_TAG, "Frame size %d exceeds UVC buffer size %u", (int)s_fb.uvc_fb.len, (unsigned)mgr->getUvcBufferSize());
esp_camera_fb_return(s_fb.cam_fb_p);
return nullptr;
}
@@ -120,7 +121,16 @@ esp_err_t UVCStreamManager::setup()
ESP_LOGI(UVC_STREAM_TAG, "Setting up UVC Stream");
uvc_buffer = static_cast<uint8_t *>(malloc(UVC_MAX_FRAMESIZE_SIZE));
// Derive transfer buffer size from configuration/descriptor settings.
// Use a conservative default if not defined elsewhere.
// For now, allocate based on commonly used max frame size or future Kconfig value.
// You can wire this to a Kconfig like CONFIG_UVC_MAX_FRAME_SIZE.
if (uvc_buffer_size == 0)
{
// default to 75 KiB if not set elsewhere
uvc_buffer_size = 75 * 1024;
}
uvc_buffer = static_cast<uint8_t *>(malloc(uvc_buffer_size));
if (uvc_buffer == nullptr)
{
ESP_LOGE(UVC_STREAM_TAG, "Allocating buffer for UVC Device failed");
@@ -129,11 +139,12 @@ esp_err_t UVCStreamManager::setup()
uvc_device_config_t config = {
.uvc_buffer = uvc_buffer,
.uvc_buffer_size = UVC_MAX_FRAMESIZE_SIZE,
.uvc_buffer_size = uvc_buffer_size,
.start_cb = UVCStreamHelpers::camera_start_cb,
.fb_get_cb = UVCStreamHelpers::camera_fb_get_cb,
.fb_return_cb = UVCStreamHelpers::camera_fb_return_cb,
.stop_cb = UVCStreamHelpers::camera_stop_cb,
.cb_ctx = this,
};
esp_err_t ret = uvc_device_config(0, &config);

View File

@@ -51,10 +51,12 @@ namespace UVCStreamHelpers
class UVCStreamManager
{
uint8_t *uvc_buffer = nullptr;
uint32_t uvc_buffer_size = 0;
public:
esp_err_t setup();
esp_err_t start();
uint32_t getUvcBufferSize() const { return uvc_buffer_size; }
};
#endif // UVCSTREAM_HPP

View File

@@ -119,6 +119,7 @@ uint8_t const *tud_descriptor_device_cb(void)
static uint8_t const desc_fs_configuration[] = {
// TUD_CONFIG_DESCRIPTOR(config_number, interface_count, string_index,
// total_length, attributes, power_mA)
// attributes: 0 = bus-powered (default). Add TUSB_DESC_CONFIG_ATT_SELF_POWERED or _REMOTE_WAKEUP if needed.
TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, 0, 500),
// IAD for Video Control
#if CFG_TUD_CAM1_VIDEO_STREAMING_BULK