-
Notifications
You must be signed in to change notification settings - Fork 187
Port virtio media simple device as a vhost user media device. #1895
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| [workspace] | ||
| members = [ | ||
| "simple_device", | ||
| "vhu_media", | ||
| ] | ||
|
|
||
| [workspace.dependencies] | ||
| clap = { version = "4.5", features = ["derive"] } | ||
| env_logger = "0.11" | ||
| libc = "0.2" | ||
| log = "0.4" | ||
| thiserror = "2.0" | ||
| vhost = { version = "0.15", features = ["vhost-user-backend"] } | ||
| vhost-user-backend = "0.21" | ||
| vhu_media = { path = "vhu_media" } | ||
| virtio-bindings = "0.2.6" | ||
| virtio-media = "0.0.7" | ||
| virtio-queue = "0.16.0" | ||
| vm-allocator = "0.1.3" | ||
| vm-memory = "0.16.2" | ||
| vmm-sys-util = "0.15.0" | ||
|
|
||
| [patch.crates-io] | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll remove this once rust-vmm/vhost/pull/339 is merged in. |
||
| vhost = { path = "/usr/local/google/home/sorama/code/github.com/forks/vhost/vhost" } | ||
| vhost-user-backend = { path = "/usr/local/google/home/sorama/code/github.com/forks/vhost/vhost-user-backend" } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| load("@rules_rust//rust:defs.bzl", "rust_binary") | ||
| load("@vhost_user_media_workspace_crates//:defs.bzl", "crate_deps") | ||
|
|
||
| package(default_visibility = ["//:android_cuttlefish"]) | ||
|
|
||
| rust_binary( | ||
| name = "simple_device", | ||
| srcs = ["src/main.rs"], | ||
| edition = "2024", | ||
| deps = crate_deps([ | ||
| "clap", | ||
| "env_logger", | ||
| "log", | ||
| "thiserror", | ||
| "vhost-user-backend", | ||
| "virtio-media", | ||
| "vm-memory", | ||
| ]) + [ | ||
| "//cuttlefish/host/commands/vhost_user_media/vhu_media", | ||
| ], | ||
| ) |
ser-io marked this conversation as resolved.
Show resolved
Hide resolved
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| [package] | ||
| name = "simple_device" | ||
| version = "0.1.0" | ||
| edition = "2024" | ||
|
|
||
| [dependencies] | ||
| clap = { workspace = true } | ||
| env_logger = { workspace = true } | ||
| log = { workspace = true } | ||
| thiserror = { workspace = true } | ||
| vhost-user-backend = { workspace = true } | ||
| vhu_media = { workspace = true } | ||
| virtio-media = { workspace = true } | ||
| vm-memory = { workspace = true } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| // Copyright 2025, The Android Open Source Project | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| //! simple_device | ||
|
|
||
| use std::{ | ||
| path::PathBuf, | ||
| process::exit, | ||
| sync::{Arc, RwLock}, | ||
| thread::{JoinHandle, spawn}, | ||
| }; | ||
|
|
||
| use clap::Parser; | ||
| use log::error; | ||
| use thiserror::Error; | ||
| use vhost_user_backend::VhostUserDaemon; | ||
| use vhu_media::VhuMediaBackend; | ||
| use virtio_media::devices::simple_device; | ||
| use virtio_media::protocol::VirtioMediaDeviceConfig; | ||
| use vm_memory::{GuestMemoryAtomic, GuestMemoryMmap}; | ||
|
|
||
| #[derive(Debug, Error)] | ||
| pub(crate) enum Error { | ||
| #[error("Could not create daemon: {0}")] | ||
| CouldNotCreateDaemon(vhost_user_backend::Error), | ||
| #[error("Fatal error: {0}")] | ||
| ServeFailed(vhost_user_backend::Error), | ||
| } | ||
|
|
||
| type Result<T> = std::result::Result<T, Error>; | ||
|
|
||
| #[derive(Parser, Debug)] | ||
| #[clap(author, version, about, long_about = None)] | ||
| struct CmdLineArgs { | ||
| /// Location of vhost-user Unix domain socket. | ||
| #[clap(short, long, value_name = "SOCKET")] | ||
| socket_path: PathBuf, | ||
| } | ||
|
|
||
| #[derive(PartialEq, Debug)] | ||
| struct Config { | ||
| socket_path: PathBuf, | ||
| } | ||
|
|
||
| impl TryFrom<CmdLineArgs> for Config { | ||
| type Error = Error; | ||
|
|
||
| fn try_from(args: CmdLineArgs) -> Result<Self> { | ||
| Ok(Config { | ||
| socket_path: args.socket_path, | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| fn start_backend(args: CmdLineArgs) -> Result<()> { | ||
| let config = Config::try_from(args)?; | ||
| let socket_path = config.socket_path.clone(); | ||
| let handle: JoinHandle<Result<()>> = spawn(move || { | ||
| loop { | ||
| let mut card = [0u8; 32]; | ||
| let card_name = "simple_device"; | ||
| card[0..card_name.len()].copy_from_slice(card_name.as_bytes()); | ||
| use virtio_media::v4l2r::ioctl::Capabilities; | ||
| let config = VirtioMediaDeviceConfig { | ||
| // device_caps: (Capabilities::VIDEO_CAPTURE_MPLANE | Capabilities::STREAMING).bits(), | ||
| device_caps: (Capabilities::VIDEO_CAPTURE | Capabilities::STREAMING).bits(), | ||
| // VFL_TYPE_VIDEO | ||
| device_type: 0, | ||
| card, | ||
| }; | ||
| let backend = Arc::new(RwLock::new(VhuMediaBackend::new( | ||
| config, | ||
| |event_queue, host_mapper| { | ||
| simple_device::SimpleCaptureDevice::new(event_queue, host_mapper) | ||
| }, | ||
| ))); | ||
| let mut daemon = VhostUserDaemon::new( | ||
| String::from("vhost-user-media-backend"), | ||
| backend, | ||
| GuestMemoryAtomic::new(GuestMemoryMmap::new()), | ||
| ) | ||
| .map_err(Error::CouldNotCreateDaemon)?; | ||
| daemon.serve(&socket_path).map_err(Error::ServeFailed)?; | ||
| } | ||
| }); | ||
|
|
||
| handle.join().map_err(std::panic::resume_unwind).unwrap() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm confused by
I don't see a
|
||
| } | ||
|
|
||
| fn main() { | ||
| env_logger::init(); | ||
|
|
||
| if let Err(e) = start_backend(CmdLineArgs::parse()) { | ||
| error!("{e}"); | ||
| exit(1); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| load("@rules_rust//rust:defs.bzl", "rust_library") | ||
| load("@vhost_user_media_workspace_crates//:defs.bzl", "crate_deps") | ||
|
|
||
| package(default_visibility = ["//visibility:public"]) | ||
|
|
||
| rust_library( | ||
| name = "vhu_media", | ||
| srcs = ["src/lib.rs"], | ||
| edition = "2024", | ||
| deps = crate_deps([ | ||
| "libc", | ||
| "log", | ||
| "thiserror", | ||
| "vhost", | ||
| "vhost-user-backend", | ||
| "virtio-bindings", | ||
| "virtio-media", | ||
| "virtio-queue", | ||
| "vm-allocator", | ||
| "vm-memory", | ||
| "vmm-sys-util", | ||
| ]), | ||
| ) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| [package] | ||
| name = "vhu_media" | ||
| version = "0.1.0" | ||
| edition = "2024" | ||
|
|
||
| [dependencies] | ||
| libc = { workspace = true } | ||
| log = { workspace = true } | ||
| thiserror = { workspace = true } | ||
| vhost = { workspace = true } | ||
| vhost-user-backend = { workspace = true } | ||
| virtio-bindings = { workspace = true } | ||
| virtio-media = { workspace = true } | ||
| virtio-queue = { workspace = true } | ||
| vm-allocator = { workspace = true } | ||
| vm-memory = { workspace = true } | ||
| vmm-sys-util = { workspace = true } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this go in a
vhost_user_media.MODULE.bazelfile in thevhost_user_mediadirectory?