Skip to contentSkip to conversation

Introduce camera subsystem

PATCH In review zhenwei pi 1 participant 4 messages Jan 6, 2022 – Jan 10, 2022 Arm/Amlogic Meson SoC support

lore
Share & tools
lore

zhenwei pi

author
#0 lore raw

v2 cover Introduce camera subsystem

v1 -> v2:
[missing CC [email protected], resend]
Separate v1 patch set into 2 parts:
Introduce camera subsystem (this one).
USB desc fix and UVC.
Add maintainer infomation.

v1:
1, The full picture of this patch set:

   +---------+       +------------+     +---------------+
   |UVC(done)|       |virtio(TODO)|     |other HW device|
   +---------+       +------------+     +---------------+
         |                 |                     |
         |            +------------+             |
	 +------------+camera(done)+-------------+
                      +----+-------+
                           |
         +-----------------+---------------------+
         |                 |                     |
  +------+------+     +----+-----+        +------+------+
  |builtin(done)|     |v4l2(done)|        |other drivers|
  +-------------+     +----------+        +-------------+

With this patch set, We can run a desktop VM (Ex Ubuntu-2004), several camera APPs(cheese, kamoso, guvcview and qcam) work fine.

Some works still in working:
1, hot-plug
2, compat with live migration
3, several actions defined in UVC SPEC

Zhenwei Pi (2):
camera: Introduce camera subsystem and builtin driver
camera: v4l2: Introduce v4l2 camera driver

 MAINTAINERS             |   7 +
 camera/builtin.c        | 717 ++++++++++++++++++++++++++++++++++++++++
 camera/camera-int.h     |  19 ++
 camera/camera.c         | 522 +++++++++++++++++++++++++++++
 camera/meson.build      |  20 ++
 camera/trace-events     |  28 ++
 camera/trace.h          |   1 +
 camera/v4l2.c           | 637 +++++++++++++++++++++++++++++++++++
 include/camera/camera.h | 238 +++++++++++++
 meson.build             |  20 +-
 meson_options.txt       |   3 +
 qapi/camera.json        | 101 ++++++
 qapi/meson.build        |   1 +
 qapi/qapi-schema.json   |   1 +
 qemu-options.hx         |  13 +
 softmmu/vl.c            |   4 +
 16 files changed, 2331 insertions(+), 1 deletion(-)
 create mode 100644 camera/builtin.c
 create mode 100644 camera/camera-int.h
 create mode 100644 camera/camera.c
 create mode 100644 camera/meson.build
 create mode 100644 camera/trace-events
 create mode 100644 camera/trace.h
 create mode 100644 camera/v4l2.c
 create mode 100644 include/camera/camera.h
 create mode 100644 qapi/camera.json
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH v2 1/2 camera: Introduce camera subsystem and builtin driver

Web camera is an important port of a desktop instance, QEMU supports USB passthrough and USB redirect to support for general cases. Several problems we have hit:
1, the heavy bandwidth of network. a 1080*720@30FPS(MJPEG) uses

     ~5MB/s.

2, Issues of USB passthrough, Ex USB reset from guest side triggers
wrong state of host side.
3, extention features, Ex to limit FPS/width&height of a camera
device by hypervisor.
...

So introduce camera subsystem to QEMU, abstruct basic API to operate a camera device. Also introduce a builtin driver which draws pure color, rainbow and digital rain background, and shows information for guest side to debug by libcairo.

To add a cameradev for a VM:
~# qemu-system-x86_64 ... -cameradev builtin,bgcolor=digital-rain,id=camera0

The camera would work with hardware camera emulation, Ex USB video( implemented in following patch) together.

The full picture of QEMU camera subsystem works like this:

   +----------+       +------------+     +---------------+
   |UVC(ready)|       |virtio(TODO)|     |other HW device|
   +----------+       +------------+     +---------------+
         |                 |                     |
         |            +--------+                 |
	 +------------+ Camera +-----------------+
                      +----+---+
                           |
         +-----------------+---------------------+
         |                 |                     |
     +---+---+       +-----+-----+        +-------------+
     |builtin|       |v4l2(ready)|        |other drivers|
     +-------+       +-----------+        +-------------+
---
 MAINTAINERS             |   7 +
 camera/builtin.c        | 717 ++++++++++++++++++++++++++++++++++++++++
 camera/camera-int.h     |  19 ++
 camera/camera.c         | 522 +++++++++++++++++++++++++++++
 camera/meson.build      |  16 +
 camera/trace-events     |  24 ++
 camera/trace.h          |   1 +
 include/camera/camera.h | 238 +++++++++++++
 meson.build             |  20 +-
 meson_options.txt       |   3 +
 qapi/camera.json        |  84 +++++
 qapi/meson.build        |   1 +
 qapi/qapi-schema.json   |   1 +
 qemu-options.hx         |  10 +
 softmmu/vl.c            |   4 +
 15 files changed, 1666 insertions(+), 1 deletion(-)
 create mode 100644 camera/builtin.c
 create mode 100644 camera/camera-int.h
 create mode 100644 camera/camera.c
 create mode 100644 camera/meson.build
 create mode 100644 camera/trace-events
 create mode 100644 camera/trace.h
 create mode 100644 include/camera/camera.h
 create mode 100644 qapi/camera.json
MAINTAINERS +7 -0
index 5456536805..d9e6c32567 100644
@@ -3077,6 +3077,13 @@ F: hw/core/clock-vmstate.c
 F: hw/core/qdev-clock.c
 F: docs/devel/clocks.rst
 
+Camera Subsystem
+M: zhenwei pi <[email protected]>
+S: Maintained
+F: include/camera
+F: camera
+F: qapi/camera.json
+
 Usermode Emulation
 ------------------
 Overall usermode emulation
camera/builtin.c new file +717 -0

717 lines not on this page · raw message

camera/camera-int.h new file +19 -0
new file mode 100644
index 0000000000..bca6ae7c44
@@ -0,0 +1,19 @@
+/*
+ * QEMU Camera subsystem internal API header file
+ *
+ * Copyright 2021-2022 Bytedance, Inc.
+ *
+ * Authors:
+ *   zhenwei pi <[email protected]>
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+#ifndef QEMU_CAMERA_INT_H
+#define QEMU_CAMERA_INT_H
+
+void qemu_camera_alloc_image(QEMUCamera *camera, size_t size, Error **errp);
+void qemu_camera_free_image(QEMUCamera *camera);
+void qemu_camera_new_image(QEMUCamera *camera, const void *addr, size_t size);
+
+#endif /* QEMU_CAMERA_INT_H */
camera/camera.c new file +522 -0

522 lines not on this page · raw message

camera/meson.build new file +16 -0
new file mode 100644
index 0000000000..d50ee5ebf7
@@ -0,0 +1,16 @@
+camera_ss.add([files(
+  'camera.c',
+)])
+
+camera_modules = {}
+foreach m : [
+  ['builtin', cairo, files('builtin.c')],
+]
+  if m[1].found()
+    module_ss = ss.source_set()
+    module_ss.add(m[1], m[2])
+    camera_modules += {m[0] : module_ss}
+  endif
+endforeach
+
+modules += {'camera': camera_modules}
camera/trace-events new file +24 -0
new file mode 100644
index 0000000000..2f4d93e924
@@ -0,0 +1,24 @@
+# See docs/devel/tracing.rst for syntax documentation.
+
+# camera.c
+qemu_camera_new(const char *dev, char *typename) "%s: new camera with type %s"
+qemu_camera_del(const char *dev) "%s: delete camera"
+qemu_camera_set_control(const char *dev, const char *type, int value) "%s: set control type %s, value %d"
+qemu_camera_stream_on(const char *dev) "%s: stream on"
+qemu_camera_stream_off(const char *dev) "%s: stream off"
+qemu_camera_stream_read(const char *dev, size_t length) "%s: stream read length %ld"
+qemu_camera_alloc_image(const char *dev, size_t size) "%s: alloc image size %ld"
+qemu_camera_free_image(const char *dev) "%s: free image size"
+qemu_camera_new_image(const char *dev, uint32_t seq, size_t size) "%s: new image sequence %u, size %ld"
+qemu_camera_enum_pixel_format(const char *dev, uint32_t pixfmt) "%s: pixfmt 0x%x"
+qemu_camera_enum_pixel_format_ret(const char *dev, int ret) "%s: ret %d"
+qemu_camera_enum_frame_size_d(const char *dev, uint32_t pixfmt, uint32_t w, uint32_t h) "%s: pixfmt 0x%x, discrete width %u, height %u"
+qemu_camera_enum_frame_size_ret(const char *dev, int ret) "%s: ret %d"
+qemu_camera_enum_frame_interval_d(const char *dev, uint32_t pixfmt, uint32_t w, uint32_t h, uint32_t n, uint32_t d) "%s: pixfmt 0x%x, width %u, height %u, discrete numerator %u, denominator %u"
+qemu_camera_enum_frame_interval_ret(const char *dev, int ret) "%s: ret %d"
+qemu_camera_set_frame_interval(const char *dev, uint32_t pixfmt, uint32_t w, uint32_t h, uint32_t n, uint32_t d, int ret) "%s: pixfmt 0x%x, width %u, height %u, discrete numerator %u, denominator %u, retval %d"
+qemu_camera_enum_control(const char *dev, const char *type, int def, int min, int max, int step) "%s: type %s, def %d, min %d, max %d, step %d"
+qemu_camera_enum_control_ret(const char *dev, int ret) "%s: ret %d"
+
+# builtin.c
+camera_builtin_timer(const char *dev) "%s: new image"
camera/trace.h new file +1 -0
new file mode 100644
index 0000000000..f248af5c9b
@@ -0,0 +1 @@
+#include "trace/trace-camera.h"
include/camera/camera.h new file +238 -0

238 lines not on this page · raw message

meson.build +19 -1
index 886f0a9343..f0b51a0861 100644
@@ -423,6 +423,13 @@ if 'ust' in get_option('trace_backends')
   lttng = dependency('lttng-ust', required: true, method: 'pkg-config',
                      kwargs: static_kwargs)
 endif
+
+cairo = not_found
+if have_system
+  cairo = dependency('cairo', required: have_system,
+                     method: 'pkg-config', kwargs: static_kwargs)
+endif
+
 pixman = not_found
 if have_system or have_tools
   pixman = dependency('pixman-1', required: have_system, version:'>=0.21.8',
@@ -1452,6 +1459,7 @@ config_host_data.set('HOST_' + host_arch.to_upper(), 1)
 
 config_host_data.set('CONFIG_ATTR', libattr.found())
 config_host_data.set('CONFIG_BRLAPI', brlapi.found())
+config_host_data.set('CONFIG_CAIRO', cairo.found())
 config_host_data.set('CONFIG_COCOA', cocoa.found())
 config_host_data.set('CONFIG_FUZZ', get_option('fuzzing'))
 config_host_data.set('CONFIG_GCOV', get_option('b_coverage'))
@@ -2395,6 +2403,7 @@ genh += hxdep
 authz_ss = ss.source_set()
 blockdev_ss = ss.source_set()
 block_ss = ss.source_set()
+camera_ss = ss.source_set()
 chardev_ss = ss.source_set()
 common_ss = ss.source_set()
 common_user_ss = ss.source_set()
@@ -2453,6 +2462,7 @@ if have_system
     'audio',
     'backends',
     'backends/tpm',
+    'camera',
     'chardev',
     'ebpf',
     'hw/9pfs',
@@ -2572,6 +2582,7 @@ endif
 
 subdir('audio')
 subdir('io')
+subdir('camera')
 subdir('chardev')
 subdir('fsdev')
 subdir('dump')
@@ -2848,6 +2859,13 @@ libchardev = static_library('chardev', chardev_ss.sources() + genh,
 
 chardev = declare_dependency(link_whole: libchardev)
 
+camera_ss = camera_ss.apply(config_host, strict: false)
+libcamera = static_library('camera', camera_ss.sources() + genh,
+                           name_suffix: 'fa',
+                           build_by_default: false)
+
+camera = declare_dependency(link_whole: libcamera)
+
 hwcore_ss = hwcore_ss.apply(config_host, strict: false)
 libhwcore = static_library('hwcore', sources: hwcore_ss.sources() + genh,
                            name_suffix: 'fa',
@@ -2867,7 +2885,7 @@ foreach m : block_mods + softmmu_mods
                 install_dir: qemu_moddir)
 endforeach
 
-softmmu_ss.add(authz, blockdev, chardev, crypto, io, qmp)
+softmmu_ss.add(authz, blockdev, camera, chardev, crypto, io, qmp)
 common_ss.add(qom, qemuutil)
 
 common_ss.add_all(when: 'CONFIG_SOFTMMU', if_true: [softmmu_ss])
meson_options.txt +3 -0
index 921967eddb..d51729441a 100644
@@ -208,3 +208,6 @@ option('fdt', type: 'combo', value: 'auto',
 
 option('selinux', type: 'feature', value: 'auto',
        description: 'SELinux support in qemu-nbd')
+
+option('camera', type: 'feature', value: 'auto',
+       description: 'Camera subsystem support')
qapi/camera.json new file +84 -0

84 lines not on this page · raw message

qapi/meson.build +1 -0
index c0c49c15e4..404eb2e573 100644
@@ -59,6 +59,7 @@ if have_system
     'rdma',
     'rocker',
     'tpm',
+    'camera',
   ]
 endif
 if have_system or have_tools
qapi/qapi-schema.json +1 -0
index 4912b9744e..58afb77639 100644
@@ -93,3 +93,4 @@
 { 'include': 'audio.json' }
 { 'include': 'acpi.json' }
 { 'include': 'pci.json' }
+{ 'include': 'camera.json' }
qemu-options.hx +10 -0
index 7d47510947..fa439134dc 100644
@@ -3529,6 +3529,16 @@ The available backends are:
     traffic identified by a name (preferably a fqdn).
 ERST
 
+DEFHEADING(Camera device options:)
+
+DEF("cameradev", HAS_ARG, QEMU_OPTION_cameradev,
+    "-cameradev help\n"
+#ifdef CONFIG_CAIRO
+    "-cameradev builtin,id=id[,debug=true|false][,fps=FPS][,width=WIDTH][,height=HEIGHT][,mjpeg=true|false][,yuv=true|false][,rgb565=true|false][,bgcolor=blue|gree|red|rainbow|digital-rain]\n"
+#endif
+    , QEMU_ARCH_ALL
+)
+
 DEFHEADING()
 
 #ifdef CONFIG_TPM
softmmu/vl.c +4 -0
index 620a1f1367..3c5f483355 100644
@@ -94,6 +94,7 @@
 #ifdef CONFIG_VIRTFS
 #include "fsdev/qemu-fsdev.h"
 #endif
+#include "camera/camera.h"
 #include "sysemu/qtest.h"
 
 #include "disas/disas.h"
@@ -3244,6 +3245,9 @@ void qemu_init(int argc, char **argv, char **envp)
                              qemu_opt_get(opts, "mount_tag"), &error_abort);
                 break;
             }
+            case QEMU_OPTION_cameradev:
+                qemu_camera_new_from_opts(optarg);
+                break;
             case QEMU_OPTION_serial:
                 add_device_config(DEV_SERIAL, optarg);
                 default_serial = 0;
Signature · 1 line
2.25.1
author
#2 lore raw

PATCH v2 2/2 camera: v4l2: Introduce v4l2 camera driver

In reply to zhenwei pi

On a Linux platform, user process could accesses /dev/videoX to capture video frames.

We can run QEMU like this:
qemu-system-x86_64 ... -cameradev v4l2,path=/dev/video0,id=camera0

To test a /dev/videoX device support video capture, install v4l-utils package and run command:
~# v4l2-ctl --info -d /dev/video0
...

    Device Caps      : 0x04200001
            Video Capture

...

The basic logic of v4l2 driver:
stream on -> qbuf -> dqbuf(drive by POLLIN event) -> qbuf -> dqbuf

     ... -> stream off
---
 camera/meson.build  |   4 +
 camera/trace-events |   4 +
 camera/v4l2.c       | 637 ++++++++++++++++++++++++++++++++++++++++++++
 qapi/camera.json    |  21 +-
 qemu-options.hx     |   3 +
 5 files changed, 667 insertions(+), 2 deletions(-)
 create mode 100644 camera/v4l2.c
camera/meson.build +4 -0
index d50ee5ebf7..6e7aeb5ebd 100644
@@ -2,6 +2,10 @@ camera_ss.add([files(
   'camera.c',
 )])
 
+camera_ss.add(when: 'CONFIG_LINUX', if_true: files(
+  'v4l2.c',
+))
+
 camera_modules = {}
 foreach m : [
   ['builtin', cairo, files('builtin.c')],
camera/trace-events +4 -0
index 2f4d93e924..4527303d58 100644
@@ -22,3 +22,7 @@ qemu_camera_enum_control_ret(const char *dev, int ret) "%s: ret %d"
 
 # builtin.c
 camera_builtin_timer(const char *dev) "%s: new image"
+
+# v4l2.c
+camera_v4l2_qbuf(const char *dev, uint32_t index) "%s: qbuf index %u"
+camera_v4l2_dqbuf(const char *dev, uint32_t index) "%s: qbuf index %u"
camera/v4l2.c new file +637 -0

637 lines not on this page · raw message

qapi/camera.json +19 -2
index 6d189637f9..513bbb465e 100644
@@ -9,6 +9,22 @@
 # = Camera
 ##
 
+##
+# @CameraV4l2Options:
+#
+# Options of the v4l2 camera.
+#
+# @path: video capture device path
+#
+# @buffers: buffer count of v4l2 driver
+#
+# Since: 6.3
+##
+{ 'struct': 'CameraV4l2Options',
+  'data': {
+    '*path': 'str',
+    '*buffers': 'uint32' } }
+
 ##
 # @ColorType:
 #
@@ -62,7 +78,7 @@
 # Since: 6.3
 ##
 { 'enum': 'CameradevDriver',
-  'data': [ 'builtin' ] }
+  'data': [ 'builtin', 'v4l2' ] }
 
 ##
 # @Cameradev:
@@ -81,4 +97,5 @@
     'driver':      'CameradevDriver'},
   'discriminator': 'driver',
   'data': {
-    'builtin':     'CameraBuiltinOptions' } }
+    'builtin':     'CameraBuiltinOptions',
+    'v4l2':        'CameraV4l2Options' } }
qemu-options.hx +3 -0
index fa439134dc..60975d6c3d 100644
@@ -3535,6 +3535,9 @@ DEF("cameradev", HAS_ARG, QEMU_OPTION_cameradev,
     "-cameradev help\n"
 #ifdef CONFIG_CAIRO
     "-cameradev builtin,id=id[,debug=true|false][,fps=FPS][,width=WIDTH][,height=HEIGHT][,mjpeg=true|false][,yuv=true|false][,rgb565=true|false][,bgcolor=blue|gree|red|rainbow|digital-rain]\n"
+#endif
+#ifdef CONFIG_LINUX
+    "-cameradev v4l2,id=id[,path=PATH][,buffers=COUNT]\n"
 #endif
     , QEMU_ARCH_ALL
 )
Signature · 1 line
2.25.1
author
#3 lore raw
In reply to zhenwei pi

Hi, Peter & Daniel

Sorry about that I'm not clear enough to add a new subsystem into QEMU, could you give me more hint?

Quoted 62 lines · zhenwei pi

v1 -> v2:
[missing CC [email protected], resend]
Separate v1 patch set into 2 parts:
Introduce camera subsystem (this one).
USB desc fix and UVC.
Add maintainer infomation.

v1:
1, The full picture of this patch set:

    +---------+       +------------+     +---------------+
    |UVC(done)|       |virtio(TODO)|     |other HW device|
    +---------+       +------------+     +---------------+
          |                 |                     |
          |            +------------+             |
	 +------------+camera(done)+-------------+
                       +----+-------+
                            |
          +-----------------+---------------------+
          |                 |                     |
   +------+------+     +----+-----+        +------+------+
   |builtin(done)|     |v4l2(done)|        |other drivers|
   +-------------+     +----------+        +-------------+

With this patch set, We can run a desktop VM (Ex Ubuntu-2004), several camera APPs(cheese, kamoso, guvcview and qcam) work fine.

Some works still in working:
1, hot-plug
2, compat with live migration
3, several actions defined in UVC SPEC

Zhenwei Pi (2):
camera: Introduce camera subsystem and builtin driver
camera: v4l2: Introduce v4l2 camera driver

  MAINTAINERS             |   7 +
  camera/builtin.c        | 717 ++++++++++++++++++++++++++++++++++++++++
  camera/camera-int.h     |  19 ++
  camera/camera.c         | 522 +++++++++++++++++++++++++++++
  camera/meson.build      |  20 ++
  camera/trace-events     |  28 ++
  camera/trace.h          |   1 +
  camera/v4l2.c           | 637 +++++++++++++++++++++++++++++++++++
  include/camera/camera.h | 238 +++++++++++++
  meson.build             |  20 +-
  meson_options.txt       |   3 +
  qapi/camera.json        | 101 ++++++
  qapi/meson.build        |   1 +
  qapi/qapi-schema.json   |   1 +
  qemu-options.hx         |  13 +
  softmmu/vl.c            |   4 +
  16 files changed, 2331 insertions(+), 1 deletion(-)

create mode 100644 camera/builtin.c
create mode 100644 camera/camera-int.h
create mode 100644 camera/camera.c
create mode 100644 camera/meson.build
create mode 100644 camera/trace-events
create mode 100644 camera/trace.h
create mode 100644 camera/v4l2.c
create mode 100644 include/camera/camera.h
create mode 100644 qapi/camera.json

Signature · 1 line
zhenwei pi