-
Notifications
You must be signed in to change notification settings - Fork 179
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
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] | ||
| resolver = "3" | ||
| members = ["simple_device", "vhu_media"] | ||
|
|
||
| [workspace.dependencies] | ||
| vhu_media = { path = "vhu_media" } | ||
|
|
||
| # External dependencies | ||
| 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.14", features = ["vhost-user-backend"] } | ||
| vhost-user-backend = "0.20" | ||
| 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.6", 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" } | ||
| virtio-media = { path = "/usr/local/google/home/sorama/code/github.com/virtio-media/device" } | ||
|
|
16 changes: 16 additions & 0 deletions
16
base/cvd/cuttlefish/host/commands/vhost_user_media/simple_device/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,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 } |
94 changes: 94 additions & 0 deletions
94
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,94 @@ | ||
| //! 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::protocol::VirtioMediaDeviceConfig; | ||
| use vm_memory::{GuestMemoryAtomic, GuestMemoryMmap}; | ||
|
|
||
| mod simple_device; | ||
|
|
||
| #[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 | 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); | ||
| } | ||
| } |
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.
You should add bazel files too