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
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]
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" }

Copy link
Member

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

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