Skip to content

Add vhost-device-media backend for virtio-media - #944

Merged
dorindabassey merged 7 commits into
rust-vmm:mainfrom
aesteve-rh:virtio-media
Jul 15, 2026
Merged

Add vhost-device-media backend for virtio-media#944
dorindabassey merged 7 commits into
rust-vmm:mainfrom
aesteve-rh:virtio-media

Conversation

@aesteve-rh

@aesteve-rh aesteve-rh commented Mar 10, 2026

Copy link
Copy Markdown
Contributor

Summary of the PR

The virtio-media device first appeared in virtio-spec v1.4 [1].
It standardizes how guest applications communicate with media
devices on the host, such as encoders, decoders, and cameras,
using the V4L2 (Video4Linux2) protocol for guest-host
communication.

The low-level implementation of the virtio-media protocol is
provided by the device crate from the virtio-media repository [2].

The crate follows the same lib + binary structure as other
vhost-device crates: core logic lives in lib.rs, while main.rs
is a thin CLI entry point.

Each backend is gated by an optional cargo feature to minimize
binary size and the dependency footprint:

  • simple-capture: simple V4L2 capture device
  • v4l2-proxy: V4L2 proxy device (enabled by default)
  • ffmpeg: software video decoding via the virtio-media-ffmpeg-decoder
    crate [3], useful for testing without dedicated hardware
  • null: always-compiled no-op backend
  • xen: enables Xen platform support for rust-vmm dependencies

A V4l2DeviceType helper resolves the device path given on the
command line to the appropriate VFL_TYPE_* kernel constant.

The MediaAllocator, which manages shared memory regions for the
device, is adapted from AddressAllocator in the crosvm resources
crate.

Finally, descriptor_chain.rs provides owned DescriptorChain[Reader/Writer]
types that implement std::io::Read/Write over a virtio descriptor chain
without borrowing guest memory. This way it avoids the memory lifetime
dependency tied to virtio-queue's Reader/Writer types.

[1] https://docs.oasis-open.org/virtio/virtio/v1.4/virtio-v1.4.html#x1-82200022
[2] https://github.com/chromeos/virtio-media
[3] https://github.com/chromeos/virtio-media/tree/main/extras/ffmpeg-decoder

Requirements

Before submitting your PR, please make sure you addressed the following
requirements:

  • All commits in this PR have Signed-Off-By trailers (with
    git commit -s), and the commit message has max 60 characters for the
    summary and max 75 characters for each description line.
  • All added/changed functionality has a corresponding unit/integration
    test.
  • All added/changed public-facing functionality has entries in the "Upcoming
    Release" section of CHANGELOG.md (if no such section exists, please create one).
  • Any newly added unsafe code is properly documented.

Comment thread vhost-device-media/Cargo.toml Outdated
@dorindabassey

Copy link
Copy Markdown
Collaborator

Thank you for this PR @aesteve-rh
IIUC, this vhost-device-media supersedes vhost-device-video, so will vhost-device-video be deprecated once virtio-media is fully supported?

@epilys

epilys commented Mar 12, 2026

Copy link
Copy Markdown
Member

@dorindabassey virtio-video is still under development (see virtio-comment list for v10). It's not replaced by virtio-media.

@aesteve-rh
aesteve-rh marked this pull request as ready for review April 15, 2026 09:33
@aesteve-rh

Copy link
Copy Markdown
Contributor Author

Some rust-vmm-container dependencies may be missing for the builds in CI for the media crate. I will investigate.

@stefano-garzarella

Copy link
Copy Markdown
Member

Some rust-vmm-container dependencies may be missing for the builds in CI for the media crate. I will investigate.

@aesteve-rh yeah, we need to manually update rust-vmm-ci with the new container tag, then also update rust-vmm-ci submodule here. Sorry, we should document this somewhere.
See rust-vmm/rust-vmm-ci#186 for example.

@aesteve-rh

Copy link
Copy Markdown
Contributor Author

Ah, yes. Makes sense. I think I can manage. Thanks for the pointer!

@aesteve-rh

Copy link
Copy Markdown
Contributor Author

CI update: rust-vmm/rust-vmm-ci#205

@stefano-garzarella

Copy link
Copy Markdown
Member

@aesteve-rh it seems there is still something missing in the CI container. I suggest running it locally following https://github.com/rust-vmm/rust-vmm-ci#running-the-tests-locally

@aesteve-rh

aesteve-rh commented May 21, 2026

Copy link
Copy Markdown
Contributor Author

Another +1 for this one? rust-vmm/rust-vmm-ci#207

edit:
@epilys thanks!
Hopefully this would be the good one regarding container dependencies.

@aesteve-rh
aesteve-rh force-pushed the virtio-media branch 2 times, most recently from e5a2e02 to 05a284d Compare May 21, 2026 14:58
@aesteve-rh

Copy link
Copy Markdown
Contributor Author

Looks much cleaner now. One of the remaining issues (with cargo-audit) is related with Gnurou/v4l2r#60, so it is a matter of time we get rid of it, hopefully.

The only other issue is with coverage. I will double check where's the gap and see if I can close it.

Thanks @stefano-garzarella and @epilys for the reviews and the patience!

@aesteve-rh
aesteve-rh force-pushed the virtio-media branch 4 times, most recently from 7540e1a to 604b92c Compare June 29, 2026 12:55
@aesteve-rh

Copy link
Copy Markdown
Contributor Author

Passes the tests! Right now the only missing dependency is for vhost and vhost-user-backend that are overriding the workspace dependency (https://github.com/aesteve-rh/vhost-device/blob/faf6e0ef09495ce81dd30911c2db371f87a2b035/vhost-device-media/Cargo.toml#L30).

I can handle it in a follow up or wait for the change to propagate here. For it to land it requires virtio-queue release, then update vhost and vhost-user-backend PR in rust-vmm/vhost#348 (which I can help with after virtio-queue is released), and finally we can update them here.

@aesteve-rh
aesteve-rh force-pushed the virtio-media branch 2 times, most recently from db9a52a to 439998f Compare July 10, 2026 09:17
@aesteve-rh

Copy link
Copy Markdown
Contributor Author

@epilys @stefano-garzarella thanks for chasing me around while I was updating stuff like a maniac :D

It seems it all landed now, CIs are passing, coverage is in a good place, media uses all workspace crates, I even tested again before the last push and it works with https://lore.kernel.org/all/20260630112310.552606-1-aesteve@redhat.com/

So this PR is ready for a full review.

@epilys

epilys commented Jul 13, 2026

Copy link
Copy Markdown
Member

@aesteve-rh will take a look asap.

Comment thread vhost-device-media/src/vhu_adapters.rs Outdated
Comment thread vhost-device-media/src/vhu_media.rs Outdated
Comment thread vhost-device-media/README.md
Comment thread Cargo.toml Outdated
Comment thread vhost-device-media/src/vhu_adapters.rs Outdated
Comment thread vhost-device-media/src/vhu_adapters.rs Outdated
Comment thread vhost-device-media/src/vhu_adapters.rs Outdated
Comment thread vhost-device-media/src/vhu_adapters.rs Outdated
Comment thread vhost-device-media/src/vhu_adapters.rs Outdated
Comment thread vhost-device-media/src/descriptor_chain.rs
@aesteve-rh
aesteve-rh force-pushed the virtio-media branch 2 times, most recently from 2129c19 to a94e478 Compare July 14, 2026 08:32

@stefano-garzarella stefano-garzarella left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor things, overall LGTM

Comment thread vhost-device-media/src/media_allocator.rs Outdated
Comment thread vhost-device-media/src/vhu_media.rs Outdated

@epilys epilys left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM as well

@dorindabassey dorindabassey left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

PR is already in great shape, Thanks @aesteve-rh for the work on this.
I plan to test it with qemu.

Comment thread vhost-device-media/src/vhu_media.rs Outdated
Comment thread vhost-device-media/src/lib.rs Outdated
Comment thread vhost-device-media/README.md
@stefano-garzarella

Copy link
Copy Markdown
Member

@dorindabassey thanks for testing this! I'm fine with this, so for me you can merge this whenever you're fine.

The virtio-media device first appeared in virtio-spec v1.4 [1].
It standardizes how guest applications communicate with media
devices on the host, such as encoders, decoders, and cameras,
using the V4L2 (Video4Linux2) protocol for guest-host
communication.

The low-level implementation of the virtio-media protocol is
provided by the device crate from the virtio-media repository [2].

The crate follows the same lib + binary structure as other
vhost-device crates: core logic lives in lib.rs, while main.rs
is a thin CLI entry point.

Each backend is gated by an optional cargo feature to minimize
binary size and the dependency footprint:

- `simple-capture`: simple V4L2 capture device
- `v4l2-proxy`: V4L2 proxy device (enabled by default)
- `ffmpeg`: software video decoding via the virtio-media-ffmpeg-decoder
  crate [3], useful for testing without dedicated hardware
- `null`: always-compiled no-op backend
- `xen`: enables Xen platform support for rust-vmm dependencies

A `V4l2DeviceType` helper resolves the device path given on the
command line to the appropriate VFL_TYPE_* kernel constant.

The `MediaAllocator`, which manages shared memory regions for the
device, is adapted from `AddressAllocator` in the crosvm `resources`
crate.

Finally, descriptor_chain.rs provides owned DescriptorChain[Reader/Writer]
types that implement std::io::Read/Write over a virtio descriptor chain
without borrowing guest memory. This way it avoids the memory lifetime
dependency tied to virtio-queue's Reader/Writer types.

[1] https://docs.oasis-open.org/virtio/virtio/v1.4/virtio-v1.4.html#x1-82200022
[2] https://github.com/chromeos/virtio-media
[3] https://github.com/chromeos/virtio-media/tree/main/extras/ffmpeg-decoder

Signed-off-by: Albert Esteve <aesteve@redhat.com>
Add unit tests to existing code for vhost-device-media covering:

- descriptor_chain: DescriptorChainReader/Writer read/write paths
  and boundary behaviour.
- vhu_media_thread: VhostUserMediaThread queue processing helpers.
- vhu_media: VuMediaBackend construction, handle_event dispatch,
  and error-type conversions.
- lib: CLI argument parsing and socket-binding smoke tests for
  each backend.

Assisted-by: Claude <noreply@anthropic.com>
Signed-off-by: Albert Esteve <aesteve@redhat.com>
vhost-device-media depends on v4l2r, which uses bindgen in its build
script to generate Rust bindings from /usr/include/linux/videodev2.h.
That header includes <sys/time.h>, which on Debian/Ubuntu aarch64 is
placed under the multiarch path /usr/include/aarch64-linux-gnu/ rather
than directly under /usr/include/. Clang, invoked by bindgen, does not
add that directory to its default include search path, causing the build
to fail with:

  /usr/include/linux/videodev2.h:60:10: fatal error: 'sys/time.h' file
  not found

Set BINDGEN_EXTRA_CLANG_ARGS to pass the multiarch include directory to
clang for all GNU build and test steps. The path is derived from
$(uname -m) so it resolves correctly on x86_64 and aarch64 alike, and
is a no-op on any platform where the directory does not exist.

Additionally, the ffmpeg feature pulls in virtio-media-ffmpeg-decoder,
whose build script uses pkg-config to locate libavcodec. pkg-config
refuses to run for a musl cross-compilation target, and libavcodec is
not available as a musl-linked library regardless. Exclude
vhost-device-media from all musl build and test jobs, following the
same pattern already used for vhost-device-gpu.

Signed-off-by: Albert Esteve <aesteve@redhat.com>
The transitive pull through virtio-media still references a version
of v4l2r which uses paste. Rather than using a [patch.crates-io]
override (which would block publishing crates to crates.io), add
RUSTSEC-2024-0436 to the audit ignore list with a comment explaining the
reasoning.

Signed-off-by: Albert Esteve <aesteve@redhat.com>
Move MediaArgs and BackendType into a dedicated args module so
that it can be reused by the xtask crate to generate a manpage
in a followup commit.

The From<MediaArgs> for VuMediaConfig conversion is moved to
lib.rs where both types are defined.

Signed-off-by: Albert Esteve <aesteve@redhat.com>
Add vhost-device-media support for the mangen task in xtask binary.

This generates a manpage in target/dist/man/vhost-device-media.1

Signed-off-by: Albert Esteve <aesteve@redhat.com>
This allows a service manager to start vhost-device-media with an
already listening socket. With this, the service manager can create
the socket in advance of starting any services, so there's no race in
between vhost-device-media being started and being ready to accept
connections.

Signed-off-by: Albert Esteve <aesteve@redhat.com>

@dorindabassey dorindabassey left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested and LGTM, Thanks for the great work @aesteve-rh

@dorindabassey
dorindabassey merged commit d343f82 into rust-vmm:main Jul 15, 2026
2 checks passed
patchew-importer pushed a commit to patchew-project/qemu that referenced this pull request Jul 15, 2026
Add the QEMU side of the vhost-user-media device, which connects to a
virtio-media vhost-user backend daemon implementing the V4L2 API over
the virtio-media protocol.

The virtio-media device is specified in the VirtIO specification v1.4,
section 5.22:
    https://docs.oasis-open.org/virtio/virtio/v1.4/cs01/virtio-v1.4-cs01.html

Tested with the rust-vmm vhost-device-media backend [1]:

    cargo run -- -s /path/to/media.sock -d /dev/video0 --backend v4l2-proxy

Example invocation:

    qemu-system-x86_64 \
        -chardev socket,path=/tmp/media.sock,id=media \
        -device vhost-user-media-pci,chardev=media,id=media

[1] rust-vmm/vhost-device#944

Signed-off-by: Albert Esteve <aesteve@redhat.com>
Tested-by: Dorinda Bassey <dbassey@redhat.com>
Message-Id: <20260630112310.552606-3-aesteve@redhat.com>
@aesteve-rh
aesteve-rh deleted the virtio-media branch July 16, 2026 07:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants