首先需要在相关部分添加uvc的功能,这里参考一下:rockchip rk3588添加uvc及uvc,adb的复合设备_uvc.gs6-CSDN博客
setprop sys.usb.config none;setprop sys.usb.config uvc
或者setprop sys.usb.config none;setprop sys.usb.config uvc,adb
使rk3588 进入uvc的device模式。
然后我们开始uvc_app的开发:
由于涉及到UVC协议和V4L2标准接口,所以通过调用JNI接口来实现UVC的功能。
上述进入device模式之后,会在/dev下生成一个video节点,通过ls /sys/class/video4linux来确认节点
1、对这个节点,我们需要先配置需要监听的事件
static void uvc_events_init(struct uvc_device *dev)
{
struct v4l2_event_subscription sub;
unsigned int payload_size;
switch (dev->fcc) {
case V4L2_PIX_FMT_YUYV:
payload_size = dev->width * dev->height * 2;
break;
case V4L2_PIX_FMT_MJPEG:
payload_size = dev->imgsize;
break;
}
uvc_fill_streaming_control(dev, &dev->probe, 0, 0);
uvc_fill_streaming_control(dev, &dev->commit, 0, 0);
if (dev->bulk) {
/* FIXME Crude hack, must be negotiated with the driver. */
dev->probe.dwMaxPayloadTransferSize = dev->commit.dwMaxPayloadTransferSize = payload_size;
}
memset(&sub, 0, sizeof sub);
sub.type = UVC_EVENT_SETUP;
ioctl(dev->uvc_fd, VIDIOC_SUBSCRIBE_EVENT, &sub);
sub.type = UVC_EVENT_DATA;
ioctl(dev->uvc_fd, VIDIOC_SUBSCRIBE_EVENT, &sub);
sub.type = UVC_EVENT_STREAMON;
ioctl(dev->uvc_fd, VIDIOC_SUBSCRIBE_EVENT, &sub);
sub.type = UVC_EVENT_STREAMOFF;
ioctl(dev->uvc_fd, VIDIOC_SUBSCRIBE_EVENT, &sub);
}
主要分为连接、断开、数据传输、流的开启或关闭
2、监听事件
static void uvc_events_process(struct uvc_device *dev)
{
struct v4l2_event v4l2_event;
struct uvc_event *uvc_event = (void *)&v4l2_event.u.data;
struct uvc_request_data resp;
int ret;
ret = ioctl(dev->uvc_fd, VIDIOC_DQEVENT, &v4l2_event);
if (ret < 0) {
printf("VIDIOC_DQEVENT failed: %s (%d)\n", strerror(errno), errno);
return;
}
memset(&resp, 0, sizeof resp);
resp.length = -EL2HLT;
switch (v4l2_event.type) {
case UVC_EVENT_CONNECT:
return;
case UVC_EVENT_DISCONNECT:
dev->uvc_shutdown_requested = 1;
printf(
"UVC: Possible USB shutdown requested from "
"Host, seen via UVC_EVENT_DISCONNECT\n");
return;
case UVC_EVENT_SETUP:
uvc_events_process_setup(dev, &uvc_event->req, &resp);
break;
case UVC_EVENT_DATA:
ret = uvc_events_process_data(dev, &uvc_event->data);
if (ret < 0)
break;
return;
case UVC_EVENT_STREAMON:
if (!dev->bulk)
uvc_handle_streamon_event(dev);
return;
case UVC_EVENT_STREAMOFF:
/* Stop V4L2 streaming... */
if (!dev->run_standalone && dev->vdev->is_streaming) {
/* UVC - V4L2 integrated path. */
v4l2_stop_capturing(dev->vdev);
dev->vdev->is_streaming = 0;
}
/* ... and now UVC streaming.. */
if (dev->is_streaming) {
uvc_video_stream(dev, 0);
uvc_uninit_device(dev);
uvc_video_reqbufs(dev, 0);
dev->is_streaming = 0;
dev->first_buffer_queued = 0;
}
return;
}
ret = ioctl(dev->uvc_fd, UVCIOC_SEND_RESPONSE, &resp);
if (ret < 0) {
printf("UVCIOC_S_EVENT failed: %s (%d)\n", strerror(errno), errno);
return;
}
}
3、初始化节点,也就是对UVC流控制端口的配置,配置传输速率、包大小、端点序号等。
static void uvc_events_process_streaming(struct uvc_device *dev, uint8_t req, uint8_t cs, struct uvc_request_data *resp)
{
struct uvc_streaming_control *ctrl;
printf("streaming request (req %02x cs %02x)\n", req, cs);
if (cs != UVC_VS_PROBE_CONTROL && cs != UVC_VS_COMMIT_CONTROL)
return;
ctrl = (struct uvc_streaming_control *)&resp->data;
resp->length = sizeof *ctrl;
switch (req) {
case UVC_SET_CUR:
dev->control = cs;
resp->length = 34;
break;
case UVC_GET_CUR:
if (cs == UVC_VS_PROBE_CONTROL)
memcpy(ctrl, &dev->probe, sizeof *ctrl);
else
memcpy(ctrl, &dev->commit, sizeof *ctrl);
break;
case UVC_GET_MIN:
case UVC_GET_MAX:
case UVC_GET_DEF:
uvc_fill_streaming_control(dev, ctrl, req == UVC_GET_MAX ? -1 : 0, req == UVC_GET_MAX ? -1 : 0);
break;
case UVC_GET_RES:
CLEAR(ctrl);
break;
case UVC_GET_LEN:
resp->data[0] = 0x00;
resp->data[1] = 0x22;
resp->length = 2;
break;
case UVC_GET_INFO:
resp->data[0] = 0x03;
resp->length = 1;
break;
}
}
除了上述还有其他配置,比如亮度、曝光、闪光灯等配置,就不再一一赘述了。
4、最后是开启UVC流
static int uvc_handle_streamon_event(struct uvc_device *dev)
{
int ret;
ret = uvc_video_reqbufs(dev, dev->nbufs);
if (ret < 0)
goto err;
if (!dev->run_standalone) {
/* UVC - V4L2 integrated path. */
if (IO_METHOD_USERPTR == dev->vdev->io) {
/*
* Ensure that the V4L2 video capture device has already
* some buffers queued.
*/
ret = v4l2_reqbufs(dev->vdev, dev->vdev->nbufs);
if (ret < 0)
goto err;
}
ret = v4l2_qbuf(dev->vdev);
if (ret < 0)
goto err;
/* Start V4L2 capturing now. */
ret = v4l2_start_capturing(dev->vdev);
if (ret < 0)
goto err;
dev->vdev->is_streaming = 1;
}
/* Common setup. */
/* Queue buffers to UVC domain and start streaming. */
ret = uvc_video_qbuf(dev);
if (ret < 0)
goto err;
if (dev->run_standalone) {
uvc_video_stream(dev, 1);
dev->first_buffer_queued = 1;
dev->is_streaming = 1;
}
return 0;
err:
return ret;
}
在这个函数里面我们就可以通过采集摄像头的数据,来写入到UVC节点里面
最后是JNI函数的封装
在我们自定义的类中可以声明该函数
public static native int prepareCamera(String uvcDevName,String v4lDevName);
然后通过 javac -h命令把这个类生成一个头文件,实现头文件里声明的函数,将以上的流程添加到这个函数里,最后在对应地方调用即可
UVC实现源码:https://github.com/wlhe/uvc-gadget