Skip to content

Commit

Permalink
feat(class/video/usbd_video): add usbd_video_stream_start_write api f…
Browse files Browse the repository at this point in the history
…or video split tranfer, every transfer with one ep_mps
  • Loading branch information
sakumisu committed Oct 18, 2024
1 parent 05315b7 commit 65151b9
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 28 deletions.
63 changes: 59 additions & 4 deletions class/video/usbd_video.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand All @@ -763,14 +768,64 @@ 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;
uint32_t last_packet_size;
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++) {
Expand Down
4 changes: 4 additions & 0 deletions class/video/usbd_video.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
50 changes: 26 additions & 24 deletions demo/video_static_mjpeg_template.c
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 + \
Expand Down Expand Up @@ -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 = {
Expand All @@ -202,45 +210,39 @@ 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)
{
uint32_t out_len;
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
Expand Down

0 comments on commit 65151b9

Please sign in to comment.