From 65151b953474d8ad0731c645c3e2c08fe5468f86 Mon Sep 17 00:00:00 2001 From: sakumisu <1203593632@qq.com> Date: Thu, 17 Oct 2024 21:13:26 +0800 Subject: [PATCH] feat(class/video/usbd_video): add usbd_video_stream_start_write api for video split tranfer, every transfer with one ep_mps --- class/video/usbd_video.c | 63 ++++++++++++++++++++++++++++-- class/video/usbd_video.h | 4 ++ demo/video_static_mjpeg_template.c | 50 ++++++++++++------------ 3 files changed, 89 insertions(+), 28 deletions(-) diff --git a/class/video/usbd_video.c b/class/video/usbd_video.c index ee8ca5ee..b4d24a08 100644 --- a/class/video/usbd_video.c +++ b/class/video/usbd_video.c @@ -18,6 +18,10 @@ struct usbd_video_priv { uint8_t power_mode; uint8_t error_code; struct video_entity_info info[3]; + uint8_t *ep_buffer; + uint8_t *stream_buf; + uint32_t stream_len; + uint32_t stream_offset; } g_usbd_video[CONFIG_USBDEV_MAX_BUS]; static int usbd_video_control_request_handler(uint8_t busid, struct usb_setup_packet *setup, uint8_t **data, uint32_t *len) @@ -671,7 +675,7 @@ static int video_class_interface_request_handler(uint8_t busid, struct usb_setup } else { return usbd_video_control_unit_terminal_request_handler(busid, setup, data, len); /* Unit and Terminal Requests */ } - } else if (intf_num == 1) { /* Video Stream Inteface */ + } else if (intf_num == 1) { /* Video Stream Inteface */ return usbd_video_stream_request_handler(busid, setup, data, len); /* Interface Stream Requests */ } return -1; @@ -700,7 +704,7 @@ static void video_notify_handler(uint8_t busid, uint8_t event, void *arg) } } -void usbd_video_probe_and_commit_controls_init(uint8_t busid, uint32_t dwFrameInterval, uint32_t dwMaxVideoFrameSize, uint32_t dwMaxPayloadTransferSize) +static void usbd_video_probe_and_commit_controls_init(uint8_t busid, uint32_t dwFrameInterval, uint32_t dwMaxVideoFrameSize, uint32_t dwMaxPayloadTransferSize) { g_usbd_video[busid].probe.hintUnion.bmHint = 0x01; g_usbd_video[busid].probe.hintUnion1.bmHint = 0; @@ -739,7 +743,8 @@ void usbd_video_probe_and_commit_controls_init(uint8_t busid, uint32_t dwFrameIn g_usbd_video[busid].commit.bMaxVersion = 0; } -struct usbd_interface *usbd_video_init_intf(uint8_t busid, struct usbd_interface *intf, +struct usbd_interface *usbd_video_init_intf(uint8_t busid, + struct usbd_interface *intf, uint32_t dwFrameInterval, uint32_t dwMaxVideoFrameSize, uint32_t dwMaxPayloadTransferSize) @@ -763,6 +768,56 @@ struct usbd_interface *usbd_video_init_intf(uint8_t busid, struct usbd_interface return intf; } +bool usbd_video_stream_split_transfer(uint8_t busid, uint8_t ep) +{ + struct video_payload_header *header = (struct video_payload_header *)g_usbd_video[busid].ep_buffer; + uint32_t remain; + uint32_t len; + + remain = g_usbd_video[busid].stream_len - g_usbd_video[busid].stream_offset; + + if (remain == 0) { + return true; + } + + header->bHeaderLength = 2; + header->headerInfoUnion.headerInfoBits.endOfHeader = 1; + + len = MIN(remain, g_usbd_video[busid].probe.dwMaxPayloadTransferSize - header->bHeaderLength); + memcpy(&g_usbd_video[busid].ep_buffer[header->bHeaderLength], + &g_usbd_video[busid].stream_buf[g_usbd_video[busid].stream_offset], + len); + + g_usbd_video[busid].stream_offset += len; + + if (g_usbd_video[busid].stream_offset == g_usbd_video[busid].stream_len) { + header->headerInfoUnion.headerInfoBits.endOfFrame = 1; + } + + usbd_ep_start_write(busid, ep, g_usbd_video[busid].ep_buffer, len + header->bHeaderLength); + return false; +} + +int usbd_video_stream_start_write(uint8_t busid, uint8_t ep, uint8_t *ep_buffer, uint8_t *buf, uint32_t len) +{ + if (usb_device_is_configured(busid) == 0) { + return -1; + } + + g_usbd_video[busid].ep_buffer = ep_buffer; + g_usbd_video[busid].stream_buf = buf; + g_usbd_video[busid].stream_len = len; + g_usbd_video[busid].stream_offset = 0; + + struct video_payload_header *header = (struct video_payload_header *)g_usbd_video[busid].ep_buffer; + + header->headerInfoUnion.headerInfoBits.frameIdentifier ^= 1; + header->headerInfoUnion.headerInfoBits.endOfFrame = 0; + + usbd_video_stream_split_transfer(busid, ep); + return 0; +} + uint32_t usbd_video_payload_fill(uint8_t busid, uint8_t *input, uint32_t input_len, uint8_t *output, uint32_t *out_len) { uint32_t packets; @@ -770,7 +825,7 @@ uint32_t usbd_video_payload_fill(uint8_t busid, uint8_t *input, uint32_t input_l uint32_t picture_pos = 0; static uint8_t uvc_header[2] = { 0x02, 0x80 }; - packets = (input_len + (g_usbd_video[busid].probe.dwMaxPayloadTransferSize - 2) ) / (g_usbd_video[busid].probe.dwMaxPayloadTransferSize - 2); + packets = (input_len + (g_usbd_video[busid].probe.dwMaxPayloadTransferSize - 2)) / (g_usbd_video[busid].probe.dwMaxPayloadTransferSize - 2); last_packet_size = input_len - ((packets - 1) * (g_usbd_video[busid].probe.dwMaxPayloadTransferSize - 2)); for (size_t i = 0; i < packets; i++) { diff --git a/class/video/usbd_video.h b/class/video/usbd_video.h index 14636fbc..24855c89 100644 --- a/class/video/usbd_video.h +++ b/class/video/usbd_video.h @@ -20,6 +20,10 @@ struct usbd_interface *usbd_video_init_intf(uint8_t busid, struct usbd_interface void usbd_video_open(uint8_t busid, uint8_t intf); void usbd_video_close(uint8_t busid, uint8_t intf); + +bool usbd_video_stream_split_transfer(uint8_t busid, uint8_t ep); +int usbd_video_stream_start_write(uint8_t busid, uint8_t ep, uint8_t *ep_buffer, uint8_t *buf, uint32_t len); + uint32_t usbd_video_payload_fill(uint8_t busid, uint8_t *input, uint32_t input_len, uint8_t *output, uint32_t *out_len); #ifdef __cplusplus diff --git a/demo/video_static_mjpeg_template.c b/demo/video_static_mjpeg_template.c index edb5eb79..51a44226 100644 --- a/demo/video_static_mjpeg_template.c +++ b/demo/video_static_mjpeg_template.c @@ -7,6 +7,8 @@ #include "usbd_video.h" #include "cherryusb_mjpeg.h" +#define VIDEO_STREAM_SPLIT_ENABLE 1 + #define VIDEO_IN_EP 0x81 #define VIDEO_INT_EP 0x83 @@ -34,7 +36,7 @@ #define MAX_BIT_RATE (unsigned long)(WIDTH * HEIGHT * 16 * CAM_FPS) #define MAX_FRAME_SIZE (unsigned long)(WIDTH * HEIGHT * 2) -#define VS_HEADER_SIZ (unsigned int)(VIDEO_SIZEOF_VS_INPUT_HEADER_DESC(1,1) + VIDEO_SIZEOF_VS_FORMAT_MJPEG_DESC + VIDEO_SIZEOF_VS_FRAME_MJPEG_DESC(1)) +#define VS_HEADER_SIZ (unsigned int)(VIDEO_SIZEOF_VS_INPUT_HEADER_DESC(1, 1) + VIDEO_SIZEOF_VS_FORMAT_MJPEG_DESC + VIDEO_SIZEOF_VS_FRAME_MJPEG_DESC(1)) #define USB_VIDEO_DESC_SIZ (unsigned long)(9 + \ VIDEO_VC_NOEP_DESCRIPTOR_LEN + \ @@ -180,8 +182,14 @@ void usbd_video_close(uint8_t busid, uint8_t intf) void usbd_video_iso_callback(uint8_t busid, uint8_t ep, uint32_t nbytes) { - //USB_LOG_RAW("actual in len:%d\r\n", nbytes); +#if VIDEO_STREAM_SPLIT_ENABLE + if (usbd_video_stream_split_transfer(busid, ep)) { + /* one frame has done */ + iso_tx_busy = false; + } +#else iso_tx_busy = false; +#endif } static struct usbd_endpoint video_in_ep = { @@ -202,7 +210,11 @@ void video_init(uint8_t busid, uintptr_t reg_base) usbd_initialize(busid, reg_base, usbd_event_handler); } +#if VIDEO_STREAM_SPLIT_ENABLE +USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t packet_buffer[MAX_PAYLOAD_SIZE]; +#else USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t packet_buffer[40 * 1024]; +#endif void video_test(uint8_t busid) { @@ -210,37 +222,27 @@ void video_test(uint8_t busid) uint32_t packets; (void)packets; - memset(packet_buffer, 0, 40 * 1024); + (void)out_len; + memset(packet_buffer, 0, sizeof(packet_buffer)); + while (1) { if (tx_flag) { - packets = usbd_video_payload_fill(busid, (uint8_t *)cherryusb_mjpeg, sizeof(cherryusb_mjpeg), packet_buffer, &out_len); -#if 1 +#if VIDEO_STREAM_SPLIT_ENABLE iso_tx_busy = true; - usbd_ep_start_write(busid, VIDEO_IN_EP, packet_buffer, out_len); + usbd_video_stream_start_write(busid, VIDEO_IN_EP, packet_buffer, (uint8_t *)cherryusb_mjpeg, sizeof(cherryusb_mjpeg)); while (iso_tx_busy) { if (tx_flag == 0) { break; } } #else - /* dwc2 must use this method */ - for (uint32_t i = 0; i < packets; i++) { - if (i == (packets - 1)) { - iso_tx_busy = true; - usbd_ep_start_write(busid, VIDEO_IN_EP, &packet_buffer[i * MAX_PAYLOAD_SIZE], out_len - (packets - 1) * MAX_PAYLOAD_SIZE); - while (iso_tx_busy) { - if (tx_flag == 0) { - break; - } - } - } else { - iso_tx_busy = true; - usbd_ep_start_write(busid, VIDEO_IN_EP, &packet_buffer[i * MAX_PAYLOAD_SIZE], MAX_PAYLOAD_SIZE); - while (iso_tx_busy) { - if (tx_flag == 0) { - break; - } - } + packets = usbd_video_payload_fill(busid, (uint8_t *)cherryusb_mjpeg, sizeof(cherryusb_mjpeg), packet_buffer, &out_len); + + iso_tx_busy = true; + usbd_ep_start_write(busid, VIDEO_IN_EP, packet_buffer, out_len); + while (iso_tx_busy) { + if (tx_flag == 0) { + break; } } #endif