From ba034a56af30f0e2aa2b50ea87838161b0b18004 Mon Sep 17 00:00:00 2001 From: 21pages Date: Tue, 5 Jul 2022 17:33:21 +0800 Subject: [PATCH] maint hwcodec config file directly Signed-off-by: 21pages --- libs/hbb_common/src/config.rs | 28 +++------------------------- libs/scrap/src/common/hwcodec.rs | 29 ++++++++++++++++------------- src/ipc.rs | 28 +--------------------------- src/main.rs | 17 ++++------------- src/server.rs | 9 +++++++++ 5 files changed, 33 insertions(+), 78 deletions(-) diff --git a/libs/hbb_common/src/config.rs b/libs/hbb_common/src/config.rs index 2ddfc5d0d..5bedbad29 100644 --- a/libs/hbb_common/src/config.rs +++ b/libs/hbb_common/src/config.rs @@ -35,7 +35,6 @@ lazy_static::lazy_static! { static ref CONFIG: Arc> = Arc::new(RwLock::new(Config::load())); static ref CONFIG2: Arc> = Arc::new(RwLock::new(Config2::load())); static ref LOCAL_CONFIG: Arc> = Arc::new(RwLock::new(LocalConfig::load())); - static ref HWCODEC_CONFIG: Arc> = Arc::new(RwLock::new(HwCodecConfig::load())); pub static ref ONLINE: Arc>> = Default::default(); pub static ref PROD_RENDEZVOUS_SERVER: Arc> = Default::default(); pub static ref APP_NAME: Arc> = Arc::new(RwLock::new("RustDesk".to_owned())); @@ -887,38 +886,17 @@ impl LanPeers { #[derive(Debug, Default, Serialize, Deserialize, Clone)] pub struct HwCodecConfig { #[serde(default)] - options: HashMap, + pub options: HashMap, } impl HwCodecConfig { - fn load() -> HwCodecConfig { + pub fn load() -> HwCodecConfig { Config::load_::("_hwcodec") } - fn store(&self) { + pub fn store(&self) { Config::store_(self, "_hwcodec"); } - - pub fn get_option(k: &str) -> String { - if let Some(v) = HWCODEC_CONFIG.read().unwrap().options.get(k) { - v.clone() - } else { - "".to_owned() - } - } - - pub fn set_option(k: String, v: String) { - let mut config = HWCODEC_CONFIG.write().unwrap(); - let v2 = if v.is_empty() { None } else { Some(&v) }; - if v2 != config.options.get(&k) { - if v2.is_none() { - config.options.remove(&k); - } else { - config.options.insert(k, v); - } - config.store(); - } - } } #[cfg(test)] diff --git a/libs/scrap/src/common/hwcodec.rs b/libs/scrap/src/common/hwcodec.rs index 8fd78e415..04b65c969 100644 --- a/libs/scrap/src/common/hwcodec.rs +++ b/libs/scrap/src/common/hwcodec.rs @@ -17,10 +17,7 @@ use hwcodec::{ Quality::{self, *}, RateContorl::{self, *}, }; -use std::{ - collections::HashMap, - sync::{Arc, Mutex}, -}; +use std::sync::{Arc, Mutex}; lazy_static::lazy_static! { static ref HW_ENCODER_NAME: Arc>> = Default::default(); @@ -161,7 +158,7 @@ impl HwEncoder { }; let encoders = CodecInfo::score(Encoder::avaliable_encoders(ctx)); if write { - let _ = set_config(CFG_KEY_ENCODER, &encoders) + set_config(CFG_KEY_ENCODER, &encoders) .map_err(|e| log::error!("{:?}", e)) .ok(); } @@ -313,7 +310,11 @@ impl HwDecoderImage<'_> { } fn get_config(k: &str) -> ResultType { - let v = HwCodecConfig::get_option(k); + let v = HwCodecConfig::load() + .options + .get(k) + .unwrap_or(&"".to_owned()) + .to_owned(); match CodecInfos::deserialize(&v) { Ok(v) => Ok(v), Err(_) => Err(anyhow!("Failed to get config:{}", k)), @@ -323,26 +324,28 @@ fn get_config(k: &str) -> ResultType { fn set_config(k: &str, v: &CodecInfos) -> ResultType<()> { match v.serialize() { Ok(v) => { - HwCodecConfig::set_option(k.to_owned(), v); + let mut config = HwCodecConfig::load(); + config.options.insert(k.to_owned(), v); + config.store(); Ok(()) } Err(_) => Err(anyhow!("Failed to set config:{}", k)), } } -pub fn check_config() -> Option> { +pub fn check_config() { let (encoders, update_encoders) = HwEncoder::best(false, false); let (decoders, update_decoders) = HwDecoder::best(false, false); if update_encoders || update_decoders { if let Ok(encoders) = encoders.serialize() { if let Ok(decoders) = decoders.serialize() { - return Some(HashMap::from([ - (CFG_KEY_ENCODER.to_owned(), encoders), - (CFG_KEY_DECODER.to_owned(), decoders), - ])); + let mut config = HwCodecConfig::load(); + config.options.insert(CFG_KEY_ENCODER.to_owned(), encoders); + config.options.insert(CFG_KEY_DECODER.to_owned(), decoders); + config.store(); + return; } } log::error!("Failed to serialize codec info"); } - None } diff --git a/src/ipc.rs b/src/ipc.rs index f4f9d4b91..24a156eba 100644 --- a/src/ipc.rs +++ b/src/ipc.rs @@ -1,8 +1,6 @@ use crate::rendezvous_mediator::RendezvousMediator; #[cfg(not(any(target_os = "android", target_os = "ios")))] pub use clipboard::ClipbaordFile; -#[cfg(feature = "hwcodec")] -use hbb_common::config::HwCodecConfig; use hbb_common::{ allow_err, bail, bytes, bytes_codec::BytesCodec, @@ -129,8 +127,6 @@ pub enum Data { ClipbaordFile(ClipbaordFile), ClipboardFileEnabled(bool), PrivacyModeState((i32, PrivacyModeState)), - #[cfg(feature = "hwcodec")] - HwCodecConfig(Option>), } #[tokio::main(flavor = "current_thread")] @@ -340,12 +336,7 @@ async fn handle(data: Data, stream: &mut Connection) { .await ); } - #[cfg(feature = "hwcodec")] - Data::HwCodecConfig(Some(config)) => { - for (k, v) in config { - HwCodecConfig::set_option(k, v); - } - } + _ => {} } } @@ -645,20 +636,3 @@ pub async fn set_socks(value: config::Socks5Server) -> ResultType<()> { .await?; Ok(()) } - -#[cfg(feature = "hwcodec")] -#[tokio::main] -pub async fn check_hwcodec_config() { - if let Some(config) = scrap::hwcodec::check_config() { - match connect(1000, "").await { - Ok(mut conn) => { - if conn.send(&Data::HwCodecConfig(Some(config))).await.is_err() { - log::error!("Failed to send hwcodec config by ipc"); - } - } - Err(err) => { - log::info!("Failed to connect ipc: {:?}", err); - } - } - } -} diff --git a/src/main.rs b/src/main.rs index 9158cab32..2e2e4c8ac 100644 --- a/src/main.rs +++ b/src/main.rs @@ -70,15 +70,6 @@ fn main() { } if args.is_empty() { std::thread::spawn(move || start_server(false)); - #[cfg(feature = "hwcodec")] - if let Ok(exe) = std::env::current_exe() { - std::thread::spawn(move || { - std::process::Command::new(exe) - .arg("--check-hwcodec-config") - .status() - .ok() - }); - } } else { #[cfg(windows)] { @@ -117,10 +108,6 @@ fn main() { args.len() > 1, )); return; - } else if args[0] == "--check-hwcodec-config" { - #[cfg(feature = "hwcodec")] - ipc::check_hwcodec_config(); - return; } } if args[0] == "--remove" { @@ -164,6 +151,10 @@ fn main() { ipc::set_password(args[1].to_owned()).unwrap(); } return; + } else if args[0] == "--check-hwcodec-config" { + #[cfg(feature = "hwcodec")] + scrap::hwcodec::check_config(); + return; } } ui::start(&mut args[..]); diff --git a/src/server.rs b/src/server.rs index 9bafa09d1..eb6f90e0b 100644 --- a/src/server.rs +++ b/src/server.rs @@ -320,6 +320,15 @@ pub async fn start_server(is_server: bool) { std::process::exit(-1); } }); + #[cfg(feature = "hwcodec")] + if let Ok(exe) = std::env::current_exe() { + std::thread::spawn(move || { + std::process::Command::new(exe) + .arg("--check-hwcodec-config") + .status() + .ok() + }); + } #[cfg(windows)] crate::platform::windows::bootstrap(); input_service::fix_key_down_timeout_loop();