Skip to content
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

feat(adb): add client flavors and autolaunch #2515

Merged
merged 5 commits into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions alvr/adb/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ license.workspace = true
alvr_common.workspace = true
alvr_filesystem.workspace = true
alvr_server_io.workspace = true
alvr_session.workspace = true

anyhow = "1"
ureq = "2.10"
Expand Down
79 changes: 68 additions & 11 deletions alvr/adb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ mod parse;

use alvr_common::anyhow::Result;
use alvr_common::{dbg_connection, error};
use alvr_session::{ClientFlavor, ConnectionConfig};
use std::collections::HashSet;

const PACKAGE_NAME_STORE: &str = "alvr.client";
const PACKAGE_NAME_GITHUB_DEV: &str = "alvr.client.dev";
const PACKAGE_NAME_GITHUB_STABLE: &str = "alvr.client.stable";

pub enum WiredConnectionStatus {
Ready,
NotReady(String),
Expand All @@ -24,7 +29,11 @@ impl WiredConnection {
Ok(Self { adb_path })
}

pub fn setup(&self, control_port: u16, stream_port: u16) -> Result<WiredConnectionStatus> {
pub fn setup(
&self,
control_port: u16,
config: &ConnectionConfig,
) -> Result<WiredConnectionStatus> {
let Some(device_serial) = commands::list_devices(&self.adb_path)?
.into_iter()
.filter_map(|d| d.serial)
Expand All @@ -35,7 +44,7 @@ impl WiredConnection {
));
};

let ports = HashSet::from([control_port, stream_port]);
let ports = HashSet::from([control_port, config.stream_port]);
let forwarded_ports: HashSet<u16> =
commands::list_forwarded_ports(&self.adb_path, &device_serial)?
.into_iter()
Expand All @@ -49,16 +58,26 @@ impl WiredConnection {
);
}

#[cfg(debug_assertions)]
let process_name = "alvr.client.dev";
#[cfg(not(debug_assertions))]
let process_name = "alvr.client.stable";
let Some(process_name) =
get_process_name(&self.adb_path, &device_serial, &config.wired_client_type)
else {
return Ok(WiredConnectionStatus::NotReady(
"No ALVR client is not installed".to_owned(),
zmerp marked this conversation as resolved.
Show resolved Hide resolved
));
};

if commands::get_process_id(&self.adb_path, &device_serial, process_name)?.is_none() {
Ok(WiredConnectionStatus::NotReady(
"ALVR client is not running".to_owned(),
))
} else if !commands::is_activity_resumed(&self.adb_path, &device_serial, process_name)? {
if commands::get_process_id(&self.adb_path, &device_serial, &process_name)?.is_none() {
if config.wired_client_autolaunch {
commands::start_application(&self.adb_path, &device_serial, &process_name)?;
Ok(WiredConnectionStatus::NotReady(
"Starting ALVR client".to_owned(),
))
} else {
Ok(WiredConnectionStatus::NotReady(
"ALVR client is not running".to_owned(),
))
}
} else if !commands::is_activity_resumed(&self.adb_path, &device_serial, &process_name)? {
Ok(WiredConnectionStatus::NotReady(
"ALVR client is paused".to_owned(),
))
Expand All @@ -76,3 +95,41 @@ impl Drop for WiredConnection {
}
}
}

pub fn get_process_name(
adb_path: &str,
device_serial: &str,
flavor: &ClientFlavor,
) -> Option<String> {
let fallbacks = match flavor {
ClientFlavor::Store => {
if alvr_common::is_stable() {
vec![PACKAGE_NAME_STORE, PACKAGE_NAME_GITHUB_STABLE]
} else {
vec![PACKAGE_NAME_GITHUB_DEV]
}
}
ClientFlavor::Github => {
if alvr_common::is_stable() {
vec![PACKAGE_NAME_GITHUB_STABLE, PACKAGE_NAME_STORE]
} else {
vec![PACKAGE_NAME_GITHUB_DEV]
}
}
ClientFlavor::Custom(name) => {
if alvr_common::is_stable() {
vec![name, PACKAGE_NAME_STORE, PACKAGE_NAME_GITHUB_STABLE]
} else {
vec![name, PACKAGE_NAME_GITHUB_DEV]
}
}
};

fallbacks
.iter()
.find(|name| {
commands::is_package_installed(adb_path, device_serial, name)
.is_ok_and(|installed| installed)
})
.map(|name| name.to_string())
}
7 changes: 3 additions & 4 deletions alvr/server_core/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,10 +288,9 @@ pub fn handshake_loop(ctx: Arc<ConnectionContext>, lifecycle_state: Arc<RwLock<L
wired_connection.as_ref().unwrap()
};

let status = match wired_connection.setup(
CONTROL_PORT,
SESSION_MANAGER.read().settings().connection.stream_port,
) {
let status = match wired_connection
.setup(CONTROL_PORT, &SESSION_MANAGER.read().settings().connection)
{
Ok(status) => status,
Err(e) => {
error!("{e:?}");
Expand Down
26 changes: 26 additions & 0 deletions alvr/session/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,13 @@ pub enum SocketBufferSize {
Custom(#[schema(suffix = "B")] u32),
}

#[derive(SettingsSchema, Serialize, Deserialize, Clone)]
pub enum ClientFlavor {
Store,
Github,
Custom(String),
}

#[derive(SettingsSchema, Serialize, Deserialize, Clone)]
pub struct ConnectionConfig {
#[schema(strings(
Expand All @@ -1114,6 +1121,16 @@ TCP: Slower than UDP, but more stable. Pick this if you experience video or audi

pub client_discovery: Switch<DiscoveryConfig>,

#[schema(strings(
help = r#"Which type of release of the client should ALVR look for when establishing a wired connection."#
zmerp marked this conversation as resolved.
Show resolved Hide resolved
))]
pub wired_client_type: ClientFlavor,

#[schema(strings(
help = r#"Wether ALVR should try to automatically launch the client when establishing a wired connection."#
))]
pub wired_client_autolaunch: bool,

#[schema(strings(
help = "This script will be ran when the headset connects. Env var ACTION will be set to `connect`."
))]
Expand Down Expand Up @@ -1752,6 +1769,15 @@ pub fn session_settings_default() -> SettingsDefault {
auto_trust_clients: cfg!(debug_assertions),
},
},
wired_client_type: ClientFlavorDefault {
Custom: "alvr.client".to_owned(),
variant: if alvr_common::is_stable() {
ClientFlavorDefaultVariant::Store
} else {
ClientFlavorDefaultVariant::Github
},
},
wired_client_autolaunch: true,
web_server_port: 8082,
stream_port: 9944,
osc_local_port: 9942,
Expand Down
Loading