Introduce new video subsystem with multiple backends
cover Introduce new video subsystem with multiple backends
As previously mentioned in our RFC, we at 9elements were working on a new video subsystem for QEMU.
This subsystem includes a USB Video Class (UVC) frontend that exposes a virtual camera device to the guest OS, backed by host-side video sources like v4l2, GStreamer and libcamera.
The patch series is split into three parts
- the first patch introduces the video subsystem with its first backend (v4l2) and implements the UVC device logic
- the second patch adds the GStreamer backend
- the third and final patch implements libcamera support
Currently we are able to:
- stream frames (currently YUYV only)
- dynamically enumerate supported camera modes (pixelformat, ...)
- discover and access camera controls (brightness, ...)
The patch series was tested on two different host devices and in combination with several guests
1. Fedora39 as host with ubuntu-24.04, Fedora41 and Fedora42 as guests
2. Fedora42 as host with Fedora42 as guest
Video streaming was successfully tested with, for example guvcview, v4l2-ctl, gst-launch-1.0 (v4l2src, pipewiresrc), cheese, snapshot, vlc, and Google Meet (from within Firefox) using the internal camera and several external USB cameras.
v4l2-ctl and qv4l2 were used to test camera controls.
Known issues:
We currently do not always achieve the target fps. Higher frame resolutions tend to reduce the fps observed in the guest. The following values were recorded using the v4l2 backend and guvcview
internal webcam (HP EliteBook):
640x360@30fps: 17.52fps
320x180@30fps: 30.00fps
320x240@30fps: 30.00fps
424x240@30fps: 30.00fps
640x480@30fps: 13.14fps
jiga webcam
1920x1080@5fps: 2.01fps
800x600@20fps: 8.44fps
I attempted to improve streaming performance by implementing a producer-consumer model. The idea was for the producer
to fetch frames from the backend and enqueue them, while the consumer (USBDeviceClass.handle_data) would dequeue and process them. However, I'm not very familiar with QEMU's threading model, and I likely didn’t implement it correctly - the producer and consumer are probably still running in the same event loop
The experimental async version can be found here:
https://github.com/9elements/qemu/tree/nlnet/qemu-video/dev-async
Another issue is that v4l2-ctl fails to stream certain video resolutions, even though other tools handle the same resolutions without problems. For some resolutions, v4l2-ctl only receives frames marked with the V4L2_BUF_FLAG_ERROR flag. This behavior requires further investigation.
Any help is very welcome!
RFC: https://lists.gnu.org/archive/html/qemu-devel/2025-03/msg02804.html
nlnet: https://nlnet.nl/project/Qemu-Camera/
Best regards,
David Milosevic
David Milosevic (3):
video: introduce video subsystem with inital v4l2 backend
video: add GStreamer backend
video: add libcamera backend extension
hw/core/qdev-properties-system.c | 52 ++ hw/usb/Kconfig | 5 + hw/usb/dev-video.c | 1333 +++++++++++++++++++++++++++ hw/usb/meson.build | 1 + hw/usb/trace-events | 10 + include/hw/qdev-properties-system.h | 4 + include/hw/usb.h | 2 + include/hw/usb/video.h | 322 +++++++ meson.build | 31 + meson_options.txt | 5 + qemu-options.hx | 9 + scripts/meson-buildoptions.sh | 6 + system/vl.c | 39 +- video/gstreamer-common.h | 49 + video/gstreamer.c | 642 +++++++++++++ video/libcamera.c | 148 +++ video/meson.build | 24 + video/v4l2.c | 619 +++++++++++++ video/video.c | 450 +++++++++ video/video.h | 298 ++++++ 20 files changed, 4048 insertions(+), 1 deletion(-) create mode 100644 hw/usb/dev-video.c create mode 100644 include/hw/usb/video.h create mode 100644 video/gstreamer-common.h create mode 100644 video/gstreamer.c create mode 100644 video/libcamera.c create mode 100644 video/meson.build create mode 100644 video/v4l2.c create mode 100644 video/video.c create mode 100644 video/video.h
Signature · 1 line
2.47.0
PATCH 1/3 video: introduce video subsystem with inital v4l2 backend
This commit introduces a new video subsystem to QEMU. It provides the foundation for emulated video devices with support for multiple host backends.
The first supported backend is video4linux (v4l2), allowing video devices such as /dev/video0 to be exposed to guest operating systems via emulated USB Video Class (UVC) devices.
Example usage:
qemu-system-x86_64 \
-device qemu-xhci \
-videodev v4l2,id=cam0,device=/dev/video0 \
-device usb-video,videodev=cam0With this commit, we lay the groundwork for further backend support (e.g. GStreamer, libcamera) and future device types (e.g. virtio-video).
It is worth mentioning, that this patch incorporates and builds upon earlier work by Zhenwei Pi on video device emulation in QEMU.
Previous RFC:
https://lists.gnu.org/archive/html/qemu-devel/2025-03/msg02804.html
--- hw/core/qdev-properties-system.c | 52 ++ hw/usb/Kconfig | 5 + hw/usb/dev-video.c | 1333 +++++++++++++++++++++++++++ hw/usb/meson.build | 1 + hw/usb/trace-events | 10 + include/hw/qdev-properties-system.h | 4 + include/hw/usb.h | 2 + include/hw/usb/video.h | 322 +++++++ meson.build | 20 + meson_options.txt | 3 + qemu-options.hx | 6 + scripts/meson-buildoptions.sh | 3 + system/vl.c | 39 +- video/meson.build | 21 + video/v4l2.c | 619 +++++++++++++ video/video.c | 450 +++++++++ video/video.h | 298 ++++++ 17 files changed, 3187 insertions(+), 1 deletion(-) create mode 100644 hw/usb/dev-video.c create mode 100644 include/hw/usb/video.h create mode 100644 video/meson.build create mode 100644 video/v4l2.c create mode 100644 video/video.c create mode 100644 video/video.h
hw/core/qdev-properties-system.c +52 -0
@@ -36,6 +36,7 @@#include "hw/pci/pcie.h" #include "hw/i386/x86.h" #include "util/block-helpers.h" +#include "video/video.h" static bool check_prop_still_unset(Object *obj, const char *name, const void *old_val, const char *new_val,
@@ -1299,3 +1300,54 @@ const PropertyInfo qdev_prop_vmapple_virtio_blk_variant = { .set = qdev_propinfo_set_enum, .set_default_value = qdev_propinfo_set_default_value_enum, }; + +/* --- videodev --- */ + +static void get_videodev (Object *obj,Visitor *v,const char * name, +void *opaque,Error **errp) +{ +Property *prop = opaque; +Videodev *video =object_field_prop_ptr (obj, prop); +char *p =g_strdup (qemu_videodev_get_id (video)); + +visit_type_str (v, name, &p, errp); +g_free (p); +} + +static void set_videodev (Object *obj,Visitor *v,const char * name, +void *opaque,Error **errp) +{ +Property *prop = opaque; +void **ptr =object_field_prop_ptr (obj, prop); +Error *local_err =NULL ; + g_autofree char *str = NULL; +Videodev *video; + +if (!visit_type_str (v, name, &str, &local_err)) { + goto error; + } + + video =qemu_videodev_by_id (str, &local_err); +if (local_err) { + goto error; + } + +qemu_videodev_register (video, &local_err); +if (local_err) { + goto error; + } + + *ptr = video; +return ; + +error: +error_propagate (errp, local_err); +} + +const PropertyInfo qdev_prop_videodev = { + .type ="str" , + .description ="ID of an videodev to use as a backend" , +/* release done on shutdown */ + .get = get_videodev, + .set = set_videodev, +};
hw/usb/Kconfig +5 -0
@@ -147,3 +147,8 @@ config XLNX_USB_SUBSYS config USB_CHIPIDEA bool select USB_EHCI_SYSBUS + +config USB_VIDEO + bool + default y + depends on USB
hw/usb/dev-video.c new file +1333 -0
1,333 lines not on this page · raw message
hw/usb/meson.build +1 -0
@@ -43,6 +43,7 @@ system_ss.add(when: 'CONFIG_USB_STORAGE_UAS', if_true: files('dev-uas.c')) system_ss.add(when: 'CONFIG_USB_AUDIO', if_true: files('dev-audio.c')) system_ss.add(when: 'CONFIG_USB_SERIAL', if_true: files('dev-serial.c')) system_ss.add(when: 'CONFIG_USB_NETWORK', if_true: files('dev-network.c')) +system_ss.add(when: 'CONFIG_USB_VIDEO', if_true: files('dev-video.c')) if host_os != 'windows' system_ss.add(when: 'CONFIG_USB_STORAGE_MTP', if_true: files('dev-mtp.c')) endif
hw/usb/trace-events +10 -0
@@ -368,3 +368,13 @@ canokey_handle_data_out(uint8_t ep_out, uint32_t out_len) "ep %d len %d" canokey_handle_data_in(uint8_t ep_in, uint32_t in_len) "ep %d len %d" canokey_realize(void) canokey_unrealize(void) + +# dev-video.c +usb_video_handle_control(int bus, int addr, int request, int value) "dev %d:%u got control 0x%x, value 0x%x" +usb_video_handle_control_error(int bus, int addr, int request, int value, int index, int length) "dev %d:%u got unsupported/bogus control request 0x%x, value 0x%x, index 0x%x, length 0x%x" +usb_video_handle_data_control_in(int bus, int addr, int len) "dev %d:%u streaming in %d" +usb_video_handle_data_streaming_in(int bus, int addr, int len) "dev %d:%u streaming in %d" +usb_video_handle_reset(int bus, int addr) "dev %d:%u reset" +usb_video_set_interface(int bus, int addr, int iface, int value) "dev %d:%u set iface %d with value %d" +usb_video_realize(int bus, int addr) "dev %d:%u" +usb_video_unrealize(int bus, int addr) "dev %d:%u"
include/hw/qdev-properties-system.h +4 -0
@@ -32,6 +32,7 @@ extern const PropertyInfo qdev_prop_cpus390entitlement;extern const PropertyInfo qdev_prop_iothread_vq_mapping_list;extern const PropertyInfo qdev_prop_endian_mode;extern const PropertyInfo qdev_prop_vmapple_virtio_blk_variant; +extern const PropertyInfo qdev_prop_videodev;#define DEFINE_PROP_PCI_DEVFN (_n, _s, _f, _d) \ DEFINE_PROP_SIGNED(_n, _s, _f, _d, qdev_prop_pci_devfn, int32_t)
@@ -110,4 +111,7 @@ extern const PropertyInfo qdev_prop_vmapple_virtio_blk_variant; qdev_prop_vmapple_virtio_blk_variant, \ VMAppleVirtioBlkVariant) +#define DEFINE_PROP_VIDEODEV (_n, _s, _f) \ + DEFINE_PROP(_n, _s, _f, qdev_prop_videodev, Videodev*) +#endif
include/hw/usb.h +2 -0
@@ -81,6 +81,8 @@#define USB_CLASS_CDC_DATA 0x0a#define USB_CLASS_CSCID 0x0b#define USB_CLASS_CONTENT_SEC 0x0d +#define USB_CLASS_VIDEO 0x0e +#define USB_CLASS_MISCELLANEOUS 0xef#define USB_CLASS_APP_SPEC 0xfe#define USB_CLASS_VENDOR_SPEC 0xff
include/hw/usb/video.h new file +322 -0
322 lines not on this page · raw message
meson.build +20 -0
@@ -2313,6 +2313,11 @@ if not get_option('libdw').auto() or \ required: get_option('libdw')) endif +v4l2 = not_found +if not get_option('v4l2').auto() or (host_os == 'linux' and have_system) + v4l2 = declare_dependency() +endif + ################# # config-host.h # #################
@@ -2361,6 +2366,15 @@ endif config_host_data.set('CONFIG_AUDIO_DRIVERS', '"' + '", "'.join(audio_drivers_selected) + '", ') +if have_system + video_backend_available = { + 'v4l2': v4l2.found(), + } + foreach k, v: video_backend_available + config_host_data.set('CONFIG_VIDEO_' + k.to_upper(), v) + endforeach +endif + have_host_block_device = (host_os != 'darwin' or cc.has_header('IOKit/storage/IOMedia.h'))
@@ -3819,6 +3833,7 @@ subdir('io') subdir('chardev') subdir('fsdev') subdir('dump') +subdir('video') if have_block block_ss.add(files(
@@ -4932,6 +4947,11 @@ summary_info += {'netmap support': have_netmap} summary_info += {'l2tpv3 support': have_l2tpv3} summary(summary_info, bool_yn: true, section: 'Network backends') +# Video backends +summary_info = {} +summary_info += {'v4l2 support': v4l2} +summary(summary_info, bool_yn: true, section: 'Video backends') + # Libraries summary_info = {} summary_info += {'libtasn1': tasn1}
meson_options.txt +3 -0
@@ -382,3 +382,6 @@ option('rust', type: 'feature', value: 'disabled', description: 'Rust support') option('strict_rust_lints', type: 'boolean', value: false, description: 'Enable stricter set of Rust warnings') + +option('v4l2', type: 'feature', value: 'auto', + description: 'v4l2 video backend support')
qemu-options.hx +6 -0
@@ -1088,6 +1088,12 @@ SRST ``qemu.wav``. ERST +DEF("videodev", HAS_ARG, QEMU_OPTION_videodev, +#ifdef CONFIG_VIDEO_V4L2 + "-videodev v4l2,id=id,device=path\n" +#endif + ,QEMU_ARCH_ALL) + DEF("device", HAS_ARG, QEMU_OPTION_device, "-device driver[,prop[=value][,...]]\n" " add device (based on driver)\n"
scripts/meson-buildoptions.sh +3 -0
@@ -196,6 +196,7 @@ meson_options_help() { printf "%s\n" ' u2f U2F emulation support' printf "%s\n" ' uadk UADK Library support' printf "%s\n" ' usb-redir libusbredir support' + printf "%s\n" ' v4l2 v4l2 video backend support' printf "%s\n" ' valgrind valgrind debug support for coroutine stacks' printf "%s\n" ' vde vde network backend support' printf "%s\n" ' vdi vdi image format support'
@@ -521,6 +522,8 @@ _meson_option_parse() { --disable-ubsan) printf "%s" -Dubsan=false ;; --enable-usb-redir) printf "%s" -Dusb_redir=enabled ;; --disable-usb-redir) printf "%s" -Dusb_redir=disabled ;; + --enable-v4l2) printf "%s" -Dv4l2=enabled ;; + --disable-v4l2) printf "%s" -Dv4l2=disabled ;; --enable-valgrind) printf "%s" -Dvalgrind=enabled ;; --disable-valgrind) printf "%s" -Dvalgrind=disabled ;; --enable-vde) printf "%s" -Dvde=enabled ;;
system/vl.c +38 -1
@@ -141,6 +141,7 @@#include "system/iothread.h" #include "qemu/guest-random.h" #include "qemu/keyval.h" +#include "video/video.h" #define MAX_VIRTIO_CONSOLES 1
@@ -512,6 +513,16 @@ static QemuOptsList qemu_action_opts = { }, }; +static QemuOptsList qemu_videodev_opts = { + .name ="videodev" , + .implied_opt_name ="backend" , + .head =QTAILQ_HEAD_INITIALIZER (qemu_videodev_opts.head), + .desc = { +/* options are defined in the videodev backends */ + {/* end of list */ } + }, +}; +const char *qemu_get_vm_name (void ) {return qemu_name;
@@ -1229,6 +1240,20 @@ static int chardev_init_func(void *opaque, QemuOpts *opts, Error **errp)return 0 ; } +static int videodev_init_func (void *opaque,QemuOpts *opts,Error **errp) +{ +Error *local_err =NULL ; + +if (!qemu_videodev_new_from_opts (opts, &local_err)) { +if (local_err) { +error_propagate (errp, local_err); +return -1 ; + } +exit (0 ); + } +return 0 ; +} +#ifdef CONFIG_VIRTFS static int fsdev_init_func (void *opaque,QemuOpts *opts,Error **errp) {
@@ -2066,6 +2091,9 @@ static void qemu_create_early_backends(void)if (default_audio) {audio_create_default_audiodevs (); } + +qemu_opts_foreach (qemu_find_opts ("videodev" ), + videodev_init_func,NULL , &error_fatal); }
@@ -2876,6 +2904,7 @@ void qemu_init(int argc, char **argv)qemu_add_opts (&qemu_semihosting_config_opts);qemu_add_opts (&qemu_fw_cfg_opts);qemu_add_opts (&qemu_action_opts); +qemu_add_opts (&qemu_videodev_opts);qemu_add_run_with_opts ();module_call_init (MODULE_INIT_OPTS );
@@ -3707,7 +3736,15 @@ void qemu_init(int argc, char **argv) break; }#endif /* CONFIG_POSIX */ - +#if defined(CONFIG_LINUX ) +case QEMU_OPTION_videodev: + opts =qemu_opts_parse_noisily (qemu_find_opts ("videodev" ), + optarg, true); +if (!opts) { +exit (1 ); + } +break ; +#endif /* CONFIG_LINUX */ default :error_report ("Option not supported in this build" );exit (1 );
video/meson.build new file +21 -0
@@ -0,0 +1,21 @@ +system_ss.add([files( + 'video.c', +)]) + +# index aliases +name = 0 +dep = 1 +files = 2 + +video_modules = {} +foreach m : [ + ['v4l2', v4l2, files('v4l2.c')], +] + if m[dep].found() + module_ss = ss.source_set() + module_ss.add(m[dep], m[files]) + video_modules += {m[name] : module_ss} + endif +endforeach + +modules += {'video': video_modules}
video/v4l2.c new file +619 -0
619 lines not on this page · raw message
video/video.c new file +450 -0
442 lines not on this page · raw message
video/video.h new file +298 -0
298 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Signature · 1 line
2.47.0
Part of the diff is not shown here. View the raw message.
PATCH 2/3 video: add GStreamer backend
In reply to David MilosevicThis follow-up extends the video subsystem by adding support for a GStreamer backend.
There are some rules to follow when passing a GStreamer pipeline to QEMU:
- pipeline must start with a source element (for example, v4l2src)
- pipeline must NOT end with a sink element. QEMU will dynamically create and append an appsink to the provided cmdline pipeline
In between, one can add an arbitrary number of converter elements to the cmdline pipeline.
QEMU will also create a capsfilter right before its appsink, in order to have control over streaming parameters such as pixelformat, width, height, ...
Hence, the final pipeline looks like this:
<------------------- qemu-cmdline -------------------><--- qemu-runtime ---> [source] -> [converter #1] -> ... -> [converter #n] -> capsfilter -> appsink
Example usage:
qemu-system-x86_64 \
-device qemu-xhci \
-videodev gstreamer,id=cam0,pipeline="v4l2src device=/dev/video0 ! videoconvert" \
-device usb-video,videodev=cam0Pipelines like this are also possible:
pipeline="videotestsrc pattern=snow ! " \
"capsfilter caps=video/x-raw^format=YUY2^width=1280^height=720^framerate=30/1"Note the use of '^' in the GStreamer pipeline string. QEMU's option parser does not allow commas ',' inside option values, which makes it difficult to pass full GStreamer pipelines directly via the command line.
To avoid modifying QEMU's core option parsing logic, we adopt a simple workaround: users are asked to substitute commas with '^' when specifying the pipeline.
--- meson.build | 11 + meson_options.txt | 2 + qemu-options.hx | 3 + scripts/meson-buildoptions.sh | 3 + video/gstreamer-common.h | 49 +++ video/gstreamer.c | 642 ++++++++++++++++++++++++++++++++++ video/meson.build | 2 + 7 files changed, 712 insertions(+) create mode 100644 video/gstreamer-common.h create mode 100644 video/gstreamer.c
meson.build +11 -0
@@ -2313,6 +2313,15 @@ if not get_option('libdw').auto() or \ required: get_option('libdw')) endif +gstreamer = not_found +gstreamer_app = not_found +if not get_option('gstreamer').auto() or (host_os == 'linux' and have_system) + gstreamer = dependency('gstreamer-1.0', required: get_option('gstreamer'), + method: 'pkg-config') + gstreamer_app = dependency('gstreamer-app-1.0', required: get_option('gstreamer'), + method: 'pkg-config') +endif + v4l2 = not_found if not get_option('v4l2').auto() or (host_os == 'linux' and have_system) v4l2 = declare_dependency()
@@ -2368,6 +2377,7 @@ config_host_data.set('CONFIG_AUDIO_DRIVERS', if have_system video_backend_available = { + 'gstreamer': gstreamer.found() and gstreamer_app.found(), 'v4l2': v4l2.found(), } foreach k, v: video_backend_available
@@ -4949,6 +4959,7 @@ summary(summary_info, bool_yn: true, section: 'Network backends') # Video backends summary_info = {} +summary_info += {'gstreamer support': gstreamer.found() and gstreamer_app.found()} summary_info += {'v4l2 support': v4l2} summary(summary_info, bool_yn: true, section: 'Video backends')
meson_options.txt +2 -0
@@ -383,5 +383,7 @@ option('rust', type: 'feature', value: 'disabled', option('strict_rust_lints', type: 'boolean', value: false, description: 'Enable stricter set of Rust warnings') +option('gstreamer', type: 'feature', value: 'auto', + description: 'gstreamer video backend support') option('v4l2', type: 'feature', value: 'auto', description: 'v4l2 video backend support')
qemu-options.hx +3 -0
@@ -1089,6 +1089,9 @@ SRST ERST DEF("videodev", HAS_ARG, QEMU_OPTION_videodev, +#ifdef CONFIG_VIDEO_GSTREAMER + "-videodev gstreamer,id=id,pipeline=pipeline\n" +#endif #ifdef CONFIG_VIDEO_V4L2 "-videodev v4l2,id=id,device=path\n" #endif
scripts/meson-buildoptions.sh +3 -0
@@ -123,6 +123,7 @@ meson_options_help() { printf "%s\n" ' gio use libgio for D-Bus support' printf "%s\n" ' glusterfs Glusterfs block device driver' printf "%s\n" ' gnutls GNUTLS cryptography support' + printf "%s\n" ' gstreamer gstreamer video backend support' printf "%s\n" ' gtk GTK+ user interface' printf "%s\n" ' gtk-clipboard clipboard support for the gtk UI (EXPERIMENTAL, MAY HANG)' printf "%s\n" ' guest-agent Build QEMU Guest Agent'
@@ -330,6 +331,8 @@ _meson_option_parse() { --disable-glusterfs) printf "%s" -Dglusterfs=disabled ;; --enable-gnutls) printf "%s" -Dgnutls=enabled ;; --disable-gnutls) printf "%s" -Dgnutls=disabled ;; + --enable-gstreamer) printf "%s" -Dgstreamer=enabled ;; + --disable-gstreamer) printf "%s" -Dgstreamer=disabled ;; --enable-gtk) printf "%s" -Dgtk=enabled ;; --disable-gtk) printf "%s" -Dgtk=disabled ;; --enable-gtk-clipboard) printf "%s" -Dgtk_clipboard=enabled ;;
video/gstreamer-common.h new file +49 -0
49 lines not on this page · raw message
video/gstreamer.c new file +642 -0
642 lines not on this page · raw message
video/meson.build +2 -0
@@ -9,6 +9,8 @@ files = 2 video_modules = {} foreach m : [ + ['gstreamer', gstreamer, files('gstreamer.c')], + ['gstreamer-app', gstreamer_app, files('gstreamer.c')], ['v4l2', v4l2, files('v4l2.c')], ] if m[dep].found()
Signature · 1 line
2.47.0
PATCH 3/3 video: add libcamera backend extension
In reply to David MilosevicThis patch extends the GStreamer backend by implementing the libcamera (pseudo) backend.
The Libcamera backend builds on top of GStreamer and uses libcamerasrc as source element.
Example usage:
qemu-system-x86_64 \
-device qemu-xhci \
-videodev libcamera,id=cam0,\
camera-name="\\\_SB_.PCI0.XHC_.RHUB.HS07-7:1.0-04f2:b681",\
caps="video/x-raw^format=YUY2^width=960^height=540^framerate=15/1" \
-device usb-video,videodev=cam0Known limitations:
- libcamersrc does not expose capabilities (supported pixelformat, etc.), which makes it impossible to query them in runtime, hence we have to select format, width, height and framerate via QEMU's cmdline.
--- video/libcamera.c | 148 ++++++++++++++++++++++++++++++++++++++++++++++ video/meson.build | 1 + 2 files changed, 149 insertions(+) create mode 100644 video/libcamera.c
video/libcamera.c new file +148 -0
@@ -0,0 +1,148 @@ +/* +* Copyright 2025 9elements GmbH +* +* Authors: +* David Milosevic <[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. +*/ + +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "qapi/qmp/qerror.h" +#include "qemu/option.h" +#include "video/video.h" +#include "video/gstreamer-common.h" + +#include <gst/gst.h> +#include <gst/app/gstappsink.h> + +#define TYPE_VIDEODEV_LIBCAMERA TYPE_VIDEODEV"-libcamera" + +#define VIDEO_LIBCAMERA_PIPELINE_TEMPLATE \ + "libcamerasrc name=qemu_src camera-name=\"%s\" ! capsfilter caps=\"%s\" ! videoconvert name=qemu_vc ! capsfilter name=qemu_cf ! appsink name=qemu_sink" + +struct LibcameraVideodev { + +GStreamerVideodev parent; +}; +typedef struct LibcameraVideodev LibcameraVideodev ; + +DECLARE_INSTANCE_CHECKER(LibcameraVideodev, LIBCAMERA_VIDEODEV, TYPE_VIDEODEV_LIBCAMERA) + +static char *video_libcamera_pipeline_string (const char *cam_name,const char *caps) +{ +char *pipeline_desc =NULL ; +size_t pipeline_template_len, pipeline_len; + + pipeline_template_len =strlen (VIDEO_LIBCAMERA_PIPELINE_TEMPLATE ) -4 ;// minus '%s' (x2) + pipeline_len =strlen (cam_name) +strlen (caps) + pipeline_template_len +1 ;// plus '\0' + + pipeline_desc =g_malloc (pipeline_len *sizeof (char )); +if (!pipeline_desc) { +return NULL ; + } + +sprintf (pipeline_desc,VIDEO_LIBCAMERA_PIPELINE_TEMPLATE , cam_name, caps); +return pipeline_desc; +} + +static int video_libcamera_open (Videodev *vd,QemuOpts *opts,Error **errp) +{ +LibcameraVideodev *lv =LIBCAMERA_VIDEODEV (vd); +GStreamerVideodev *gv = &lv->parent; +const char *cam_name =qemu_opt_get (opts,"camera-name" ); +char *caps =video_gstreamer_qemu_opt_get (opts,"caps" ); +char *pipeline_desc =NULL ; +GError *error =NULL ; +GstStateChangeReturn ret; + +if (cam_name ==NULL ) { +vd_error_setg (vd, errp,QERR_MISSING_PARAMETER ,"camera-name" ); +return VIDEODEV_RC_ERROR ; + } + +if (caps ==NULL ) { +vd_error_setg (vd, errp,QERR_MISSING_PARAMETER ,"caps" ); +return VIDEODEV_RC_ERROR ; + } + + pipeline_desc =video_libcamera_pipeline_string (cam_name, caps); +g_free (caps); +if (!pipeline_desc) { +vd_error_setg (vd, errp,"memory allocation failure" ); +return VIDEODEV_RC_ERROR ; + } + +if (!gst_is_initialized ()) { +gst_init (NULL ,NULL ); + } + + gv->pipeline =gst_parse_bin_from_description (pipeline_desc, false, &error); +g_free (pipeline_desc); +if (error) { +vd_error_setg (vd, errp,"unable to parse pipeline: %s" , error->message); +return VIDEODEV_RC_ERROR ; + } + + gv->head =gst_bin_get_by_name (GST_BIN (gv->pipeline),"qemu_src" ); +if (unlikely (!gv->head)) { +vd_error_setg (vd, errp,"qemu_src not found" ); +return VIDEODEV_RC_ERROR ; + } + + gv->tail =gst_bin_get_by_name (GST_BIN (gv->pipeline),"qemu_vc" ); +if (unlikely (!gv->tail)) { +vd_error_setg (vd, errp,"qemu_vc not found" ); +return VIDEODEV_RC_ERROR ; + } + + gv->filter =gst_bin_get_by_name (GST_BIN (gv->pipeline),"qemu_cf" ); +if (unlikely (!gv->filter)) { +vd_error_setg (vd, errp,"qemu_cf not found" ); +return VIDEODEV_RC_ERROR ; + } + + gv->sink =gst_bin_get_by_name (GST_BIN (gv->pipeline),"qemu_sink" ); +if (unlikely (!gv->sink)) { +vd_error_setg (vd, errp,"qemu_sink not found" ); +return VIDEODEV_RC_ERROR ; + } + + ret =gst_element_set_state (gv->pipeline,GST_STATE_READY ); +if (ret ==GST_STATE_CHANGE_FAILURE ) { + +vd_error_setg (vd, errp,"failed to set pipeline to READY" ); +return VIDEODEV_RC_ERROR ; + } + +return VIDEODEV_RC_OK ; +} + +static void video_libcamera_class_init (ObjectClass *oc,const void *data) +{ +VideodevClass *vc; +video_gstreamer_class_init (oc, data); + + vc =VIDEODEV_CLASS (oc); + +/* override GStreamer class methods */ + vc->open = video_libcamera_open; + vc->enum_controls =NULL ; + vc->set_control =NULL ; +} + +static const TypeInfo video_libcamera_type_info = { + .name =TYPE_VIDEODEV_LIBCAMERA , + .parent =TYPE_VIDEODEV_GSTREAMER , + .instance_size =sizeof (LibcameraVideodev), + .class_init = video_libcamera_class_init, +}; + +static void register_types (void ) +{ +type_register_static (&video_libcamera_type_info); +} + +type_init (register_types);
video/meson.build +1 -0
@@ -11,6 +11,7 @@ video_modules = {} foreach m : [ ['gstreamer', gstreamer, files('gstreamer.c')], ['gstreamer-app', gstreamer_app, files('gstreamer.c')], + ['libcamera', gstreamer, files('libcamera.c', 'gstreamer.c')], ['v4l2', v4l2, files('v4l2.c')], ] if m[dep].found()
Signature · 1 line
2.47.0