Skip to content

Commit 7c6c405

Browse files
author
Brian Daniels
committed
Consolidate CLI functions for vhost_user_media in vhu_media
Bug: 519646531 Test: cd base/cvd && bazel build cuttlefish/package:cvd # Repeat with v4l2_emulated_camera_mplane cvd create -media=type=v4l2_emulated_camera_splane # Ensure pixel format changes with each type v4l2-ctl -d1 --all # Ensure it passes with no failures v4l2-compliance -d1 -s
1 parent 74ae0f0 commit 7c6c405

10 files changed

Lines changed: 76 additions & 107 deletions

File tree

base/cvd/cuttlefish/host/commands/vhost_user_media/emulated_camera_mplane/BUILD.bazel

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@ rust_binary(
2020
edition = "2024",
2121
deps = crate_deps([
2222
"clap",
23-
"env_logger",
2423
"libc",
2524
"log",
26-
"thiserror",
2725
"v4l2r",
2826
"vhost-user-backend",
2927
"virtio-media",

base/cvd/cuttlefish/host/commands/vhost_user_media/emulated_camera_mplane/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,8 @@ edition = "2024"
55

66
[dependencies]
77
clap = { workspace = true }
8-
env_logger = { workspace = true }
98
libc = { workspace = true }
109
log = { workspace = true }
11-
thiserror = { workspace = true }
1210
v4l2r = { workspace = true }
1311
vhost-user-backend = { workspace = true }
1412
vhu_media = { workspace = true }

base/cvd/cuttlefish/host/commands/vhost_user_media/emulated_camera_mplane/src/main.rs

Lines changed: 2 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -12,62 +12,16 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use std::path::PathBuf;
16-
use std::sync::{Arc, RwLock};
17-
1815
use clap::Parser;
19-
use log::error;
20-
use thiserror::Error;
16+
use std::sync::{Arc, RwLock};
2117
use vhost_user_backend::VhostUserDaemon;
2218
use vhu_media::VhuMediaBackend;
19+
use vhu_media::cli::{CmdLineArgs, Config, Error, init_logging, Result};
2320
use virtio_media::protocol::VirtioMediaDeviceConfig;
2421
use vm_memory::{GuestMemoryAtomic, GuestMemoryMmap};
2522

2623
mod device;
2724

28-
#[derive(Debug, Error)]
29-
pub(crate) enum Error {
30-
#[error("Could not create daemon: {0}")]
31-
CouldNotCreateDaemon(vhost_user_backend::Error),
32-
#[error("Fatal error: {0}")]
33-
ServeFailed(vhost_user_backend::Error),
34-
}
35-
36-
type Result<T> = std::result::Result<T, Error>;
37-
38-
#[derive(Parser, Debug)]
39-
#[clap(author, version, about, long_about = None)]
40-
struct CmdLineArgs {
41-
/// Location of vhost-user Unix domain socket.
42-
#[clap(short, long, value_name = "SOCKET")]
43-
socket_path: PathBuf,
44-
/// Log verbosity, one of Off, Error, Warning, Info, Debug, Trace.
45-
#[clap(short, long, default_value_t = log::LevelFilter::Debug)]
46-
verbosity: log::LevelFilter,
47-
}
48-
49-
#[derive(PartialEq, Debug)]
50-
struct Config {
51-
socket_path: PathBuf,
52-
}
53-
54-
impl TryFrom<CmdLineArgs> for Config {
55-
type Error = Error;
56-
57-
fn try_from(args: CmdLineArgs) -> Result<Self> {
58-
Ok(Config {
59-
socket_path: args.socket_path,
60-
})
61-
}
62-
}
63-
64-
fn init_logging(verbosity: log::LevelFilter) -> Result<()> {
65-
env_logger::builder()
66-
.format_timestamp_secs()
67-
.filter_level(verbosity)
68-
.init();
69-
Ok(())
70-
}
7125

7226
const VFL_TYPE_VIDEO: u32 = 0;
7327

base/cvd/cuttlefish/host/commands/vhost_user_media/emulated_camera_splane/BUILD.bazel

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ rust_binary(
2020
edition = "2024",
2121
deps = crate_deps([
2222
"clap",
23-
"env_logger",
2423
"image",
2524
"libc",
2625
"log",

base/cvd/cuttlefish/host/commands/vhost_user_media/emulated_camera_splane/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ edition = "2024"
55

66
[dependencies]
77
clap = { workspace = true }
8-
env_logger = { workspace = true }
98
image = { workspace = true }
109
libc = { workspace = true }
1110
log = { workspace = true }

base/cvd/cuttlefish/host/commands/vhost_user_media/emulated_camera_splane/src/main.rs

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,54 +12,16 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use std::path::PathBuf;
16-
use std::sync::{Arc, RwLock};
17-
1815
use clap::Parser;
16+
use std::sync::{Arc, RwLock};
1917
use vhost_user_backend::VhostUserDaemon;
2018
use vhu_media::VhuMediaBackend;
21-
use vhu_media::cli::Error;
19+
use vhu_media::cli::{CmdLineArgs, Config, Error, init_logging, Result};
2220
use virtio_media::protocol::VirtioMediaDeviceConfig;
2321
use vm_memory::{GuestMemoryAtomic, GuestMemoryMmap};
2422

2523
mod device;
2624

27-
type Result<T> = std::result::Result<T, Error>;
28-
29-
#[derive(Parser, Debug)]
30-
#[clap(author, version, about, long_about = None)]
31-
struct CmdLineArgs {
32-
/// Location of vhost-user Unix domain socket.
33-
#[clap(short, long, value_name = "SOCKET")]
34-
socket_path: PathBuf,
35-
/// Log verbosity, one of Off, Error, Warning, Info, Debug, Trace.
36-
#[clap(short, long, default_value_t = log::LevelFilter::Debug)]
37-
verbosity: log::LevelFilter,
38-
}
39-
40-
#[derive(PartialEq, Debug)]
41-
struct Config {
42-
socket_path: PathBuf,
43-
}
44-
45-
impl TryFrom<CmdLineArgs> for Config {
46-
type Error = Error;
47-
48-
fn try_from(args: CmdLineArgs) -> Result<Self> {
49-
Ok(Config {
50-
socket_path: args.socket_path,
51-
})
52-
}
53-
}
54-
55-
fn init_logging(verbosity: log::LevelFilter) -> Result<()> {
56-
env_logger::builder()
57-
.format_timestamp_secs()
58-
.filter_level(verbosity)
59-
.init();
60-
Ok(())
61-
}
62-
6325
const VFL_TYPE_VIDEO: u32 = 0;
6426

6527
fn start_backend(config: Config) -> Result<()> {

base/cvd/cuttlefish/host/commands/vhost_user_media/vhu_media/BUILD.bazel

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,15 @@ package(default_visibility = ["//visibility:public"])
55

66
rust_library(
77
name = "vhu_media",
8-
srcs = ["src/devices.rs", "src/lib.rs"],
8+
srcs = [
9+
"src/cli.rs",
10+
"src/devices.rs",
11+
"src/lib.rs",
12+
],
913
edition = "2024",
1014
deps = crate_deps([
15+
"clap",
16+
"env_logger",
1117
"libc",
1218
"log",
1319
"thiserror",

base/cvd/cuttlefish/host/commands/vhost_user_media/vhu_media/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ version = "0.1.0"
44
edition = "2024"
55

66
[dependencies]
7+
clap = { workspace = true }
8+
env_logger = { workspace = true }
79
libc = { workspace = true }
810
log = { workspace = true }
911
thiserror = { workspace = true }
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
// Copyright 2026, The Android Open Source Project
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
use clap::Parser;
16+
use log::error;
17+
use std::path::PathBuf;
18+
use thiserror::Error as ThisError;
19+
20+
#[derive(Debug, ThisError)]
21+
pub enum Error {
22+
#[error("Could not create daemon: {0}")]
23+
CouldNotCreateDaemon(vhost_user_backend::Error),
24+
#[error("Fatal error: {0}")]
25+
ServeFailed(vhost_user_backend::Error),
26+
}
27+
28+
pub type Result<T> = std::result::Result<T, Error>;
29+
30+
#[derive(PartialEq, Debug)]
31+
pub struct Config {
32+
pub socket_path: PathBuf,
33+
}
34+
35+
#[derive(Parser, Debug)]
36+
#[clap(author, version, about, long_about = None)]
37+
pub struct CmdLineArgs {
38+
/// Location of vhost-user Unix domain socket.
39+
#[clap(short, long, value_name = "SOCKET")]
40+
pub socket_path: PathBuf,
41+
/// Log verbosity, one of Off, Error, Warning, Info, Debug, Trace.
42+
#[clap(short, long, default_value_t = log::LevelFilter::Debug)]
43+
pub verbosity: log::LevelFilter,
44+
}
45+
46+
impl TryFrom<CmdLineArgs> for Config {
47+
type Error = Error;
48+
49+
fn try_from(args: CmdLineArgs) -> Result<Self> {
50+
Ok(Config {
51+
socket_path: args.socket_path,
52+
})
53+
}
54+
}
55+
56+
pub fn init_logging(verbosity: log::LevelFilter) -> Result<()> {
57+
env_logger::builder()
58+
.format_timestamp_secs()
59+
.filter_level(verbosity)
60+
.init();
61+
Ok(())
62+
}

base/cvd/cuttlefish/host/commands/vhost_user_media/vhu_media/src/lib.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,7 @@ use vmm_sys_util::epoll::EventSet;
4141
use vmm_sys_util::event::new_event_consumer_and_notifier;
4242
use vmm_sys_util::event::{EventConsumer, EventFlag, EventNotifier};
4343

44-
pub mod cli {
45-
use thiserror::Error as ThisError;
46-
47-
#[derive(Debug, ThisError)]
48-
pub enum Error {
49-
#[error("Could not create daemon: {0}")]
50-
CouldNotCreateDaemon(vhost_user_backend::Error),
51-
#[error("Fatal error: {0}")]
52-
ServeFailed(vhost_user_backend::Error),
53-
}
54-
}
55-
44+
pub mod cli;
5645
pub mod devices;
5746

5847
#[derive(Debug, ThisError)]

0 commit comments

Comments
 (0)