Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions base/cvd/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,17 @@ vhost_user_input_crates.from_cargo(
)
use_repo(vhost_user_input_crates, "vhost_user_input_crates")

vhost_user_media_workspace_crates = use_extension("@rules_rust//crate_universe:extensions.bzl", "crate")
vhost_user_media_workspace_crates.from_cargo(
name = "vhost_user_media_workspace_crates",
cargo_config = "@//build_external/crosvm:crosvm.config.toml",
manifests = [
"//cuttlefish/host/commands/vhost_user_media:Cargo.toml",
],
host_tools = "@rust_host_tools_nightly",
)
use_repo(vhost_user_media_workspace_crates, "vhost_user_media_workspace_crates")

git_repository = use_repo_rule("@bazel_tools//tools/build_defs/repo:git.bzl", "git_repository")

git_repository(
Expand Down
Empty file.
34 changes: 34 additions & 0 deletions base/cvd/cuttlefish/host/commands/vhost_user_media/Cargo.toml
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]
Copy link
Member Author

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.

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,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(),
)
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 }
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);
}
}
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",
]),
)
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 }
Loading
Loading