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 1 commit
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
81 changes: 57 additions & 24 deletions alvr/adb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ mod parse;

use alvr_common::anyhow::Result;
use alvr_common::{dbg_connection, error};
use alvr_session::{ClientFlavor, Settings};
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 @@ -25,7 +29,11 @@ impl WiredConnection {
Ok(Self { adb_path })
}

pub fn setup(&self, control_port: u16, settings: &Settings) -> 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 @@ -36,7 +44,7 @@ impl WiredConnection {
));
};

let ports = HashSet::from([control_port, settings.connection.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 @@ -50,29 +58,16 @@ impl WiredConnection {
);
}

let process_name = match &settings.connection.client_flavor {
ClientFlavor::Store => if alvr_common::is_stable() {
"alvr.client"
} else {
"alvr.client.dev"
}
.to_owned(),
ClientFlavor::Github => if alvr_common::is_stable() {
"alvr.client.stable"
} else {
"alvr.client.dev"
}
.to_owned(),
ClientFlavor::Custom(name) => name.clone(),
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::is_package_installed(&self.adb_path, &device_serial, &process_name)? {
Ok(WiredConnectionStatus::NotReady(
"ALVR client is not installed".to_owned(),
))
} else if commands::get_process_id(&self.adb_path, &device_serial, &process_name)?.is_none()
{
if settings.connection.client_autolaunch {
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(),
Expand Down Expand Up @@ -100,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())
}
19 changes: 10 additions & 9 deletions alvr/server_core/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,15 +288,16 @@ 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()) {
Ok(status) => status,
Err(e) => {
error!("{e:?}");
thread::sleep(RETRY_CONNECT_MIN_INTERVAL);
continue;
}
};
let status = match wired_connection
.setup(CONTROL_PORT, &SESSION_MANAGER.read().settings().connection)
{
Ok(status) => status,
Err(e) => {
error!("{e:?}");
thread::sleep(RETRY_CONNECT_MIN_INTERVAL);
continue;
}
};

if let WiredConnectionStatus::NotReady(m) = status {
dbg_connection!("handshake_loop: Wired connection not ready: {m}");
Expand Down
12 changes: 6 additions & 6 deletions alvr/session/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1122,14 +1122,14 @@ TCP: Slower than UDP, but more stable. Pick this if you experience video or audi
pub client_discovery: Switch<DiscoveryConfig>,

#[schema(strings(
help = r#"Wether ALVR should try to automatically launch the client when establishing a wired connection."#
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 client_autolaunch: bool,
pub wired_client_type: ClientFlavor,

#[schema(strings(
help = r#"Which type of client should ALVR look for when establishing a wired connection."#
help = r#"Wether ALVR should try to automatically launch the client when establishing a wired connection."#
))]
pub client_flavor: ClientFlavor,
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 @@ -1769,15 +1769,15 @@ pub fn session_settings_default() -> SettingsDefault {
auto_trust_clients: cfg!(debug_assertions),
},
},
client_autolaunch: true,
client_flavor: ClientFlavorDefault {
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