-
Notifications
You must be signed in to change notification settings - Fork 186
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
Open
ser-io
wants to merge
1
commit into
google:main
Choose a base branch
from
ser-io:virtio-media-use-new-SHMEM
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
34 changes: 34 additions & 0 deletions
34
base/cvd/cuttlefish/host/commands/vhost_user_media/Cargo.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| [workspace] | ||
| members = [ | ||
| "simple_device", | ||
| "vhu_media", | ||
| ] | ||
|
|
||
| [workspace.dependencies] | ||
| rng = { path = "rng" } | ||
| vhu_media = { path = "vhu_media" } | ||
| anyhow = { version = "1.0.97", features = [ "default", "std" ] } | ||
| 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" | ||
| 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" | ||
| v4l2r = { version = "0.0.7", features = ["arch64"] } | ||
| uuid = { version = "1.8.0", features=["v4"] } | ||
| zerocopy = { version = "0.8.13", features = ["derive"] } | ||
| bitflags = "2.3" | ||
| serde = { version = "1.0", features = ["derive"] } | ||
| serde_json = "1" | ||
|
|
||
| [patch.crates-io] | ||
| 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" } | ||
|
|
||
13 changes: 13 additions & 0 deletions
13
base/cvd/cuttlefish/host/commands/vhost_user_media/simple_device/BUILD.bazel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| load("@rules_rust//rust:defs.bzl", "rust_binary") | ||
| load("@vhost_user_media_workspace_crates//:defs.bzl", "all_crate_deps") | ||
|
|
||
| package(default_visibility = ["//:android_cuttlefish"]) | ||
|
|
||
| rust_binary( | ||
| name = "simple_device", | ||
| srcs = ["src/main.rs"], | ||
| edition = "2024", | ||
| deps = [ | ||
| "//cuttlefish/host/commands/vhost_user_media/vhu_media", | ||
| ] + all_crate_deps(), | ||
| ) |
16 changes: 16 additions & 0 deletions
16
base/cvd/cuttlefish/host/commands/vhost_user_media/simple_device/Cargo.toml
ser-io marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| [package] | ||
| name = "simple_device" | ||
| version = "0.1.0" | ||
| edition = "2024" | ||
|
|
||
| [dependencies] | ||
| clap = { workspace = true } | ||
| env_logger = { workspace = true } | ||
| libc = { workspace = true } | ||
| log = { workspace = true } | ||
| thiserror = { workspace = true } | ||
| v4l2r = { workspace = true } | ||
| vhost-user-backend = { workspace = true } | ||
| virtio-media = { workspace = true } | ||
| vm-memory = { workspace = true } | ||
| vhu_media = { workspace = true } |
108 changes: 108 additions & 0 deletions
108
base/cvd/cuttlefish/host/commands/vhost_user_media/simple_device/src/main.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | ||
| } | ||
|
|
||
| fn main() { | ||
| env_logger::init(); | ||
|
|
||
| if let Err(e) = start_backend(CmdLineArgs::parse()) { | ||
| error!("{e}"); | ||
| exit(1); | ||
| } | ||
| } |
28 changes: 28 additions & 0 deletions
28
base/cvd/cuttlefish/host/commands/vhost_user_media/vhu_media/BUILD.bazel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| 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([ | ||
| "anyhow", | ||
| "env_logger", | ||
| "libc", | ||
| "log", | ||
| "thiserror", | ||
| "vhost", | ||
| "vhost-user-backend", | ||
| "virtio-bindings", | ||
| "virtio-media", | ||
| "virtio-queue", | ||
| "vm-allocator", | ||
| "vm-memory", | ||
| "vmm-sys-util", | ||
| "uuid", | ||
| "zerocopy", | ||
| "bitflags", | ||
| ]), | ||
| ) |
22 changes: 22 additions & 0 deletions
22
base/cvd/cuttlefish/host/commands/vhost_user_media/vhu_media/Cargo.toml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| [package] | ||
| name = "vhu_media" | ||
| version = "0.1.0" | ||
| edition = "2024" | ||
|
|
||
| [dependencies] | ||
| anyhow = { workspace = true } | ||
| env_logger = { workspace = true } | ||
| 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 } | ||
| uuid = { workspace = true } | ||
| zerocopy = { workspace = true } | ||
| bitflags = { workspace = true } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I'll remove this once rust-vmm/vhost/pull/339 is merged in.