From a31a68ba173fbba3e8a837e401691b900c3cf609 Mon Sep 17 00:00:00 2001 From: 21pages Date: Thu, 9 May 2024 09:02:25 +0800 Subject: [PATCH] If there is hardware ram encoder as fallback, not require all adapters (#7987) support the codec format Signed-off-by: 21pages --- libs/scrap/src/common/codec.rs | 2 ++ libs/scrap/src/common/hwcodec.rs | 5 +--- libs/scrap/src/common/vram.rs | 46 +++++++++++++++++--------------- 3 files changed, 27 insertions(+), 26 deletions(-) diff --git a/libs/scrap/src/common/codec.rs b/libs/scrap/src/common/codec.rs index 5a429fcc8..b189dadac 100644 --- a/libs/scrap/src/common/codec.rs +++ b/libs/scrap/src/common/codec.rs @@ -139,6 +139,7 @@ impl Encoder { Err(e) => { log::error!("new hw encoder failed: {e:?}, clear config"); hbb_common::config::HwCodecConfig::clear_ram(); + Self::update(EncodingUpdate::Check); *ENCODE_CODEC_FORMAT.lock().unwrap() = CodecFormat::VP9; Err(e) } @@ -151,6 +152,7 @@ impl Encoder { Err(e) => { log::error!("new vram encoder failed: {e:?}, clear config"); hbb_common::config::HwCodecConfig::clear_vram(); + Self::update(EncodingUpdate::Check); *ENCODE_CODEC_FORMAT.lock().unwrap() = CodecFormat::VP9; Err(e) } diff --git a/libs/scrap/src/common/hwcodec.rs b/libs/scrap/src/common/hwcodec.rs index a4a11c6ac..9c03937b5 100644 --- a/libs/scrap/src/common/hwcodec.rs +++ b/libs/scrap/src/common/hwcodec.rs @@ -96,10 +96,7 @@ impl EncoderApi for HwRamEncoder { height: ctx.height as _, bitrate, }), - Err(_) => { - HwCodecConfig::clear_ram(); - Err(anyhow!(format!("Failed to create encoder"))) - } + Err(_) => Err(anyhow!(format!("Failed to create encoder"))), } } _ => Err(anyhow!("encoder type mismatch")), diff --git a/libs/scrap/src/common/vram.rs b/libs/scrap/src/common/vram.rs index ce9bfd8a9..3b4113fd1 100644 --- a/libs/scrap/src/common/vram.rs +++ b/libs/scrap/src/common/vram.rs @@ -87,10 +87,7 @@ impl EncoderApi for VRamEncoder { same_bad_len_counter: 0, config, }), - Err(_) => { - hbb_common::config::HwCodecConfig::clear_vram(); - Err(anyhow!(format!("Failed to create encoder"))) - } + Err(_) => Err(anyhow!(format!("Failed to create encoder"))), } } _ => Err(anyhow!("encoder type mismatch")), @@ -213,32 +210,37 @@ impl VRamEncoder { CodecFormat::H265 => DataFormat::H265, _ => return vec![], }; - let Ok(displays) = crate::Display::all() else { - log::error!("failed to get displays"); - return vec![]; - }; - if displays.is_empty() { - log::error!("no display found"); - return vec![]; - } - let luids = displays - .iter() - .map(|d| d.adapter_luid()) - .collect::>(); let v: Vec<_> = get_available_config() .map(|c| c.e) .unwrap_or_default() .drain(..) .filter(|c| c.data_format == data_format) .collect(); - if luids - .iter() - .all(|luid| v.iter().any(|f| Some(f.luid) == *luid)) - { + if crate::hwcodec::HwRamEncoder::try_get(format).is_some() { + // has fallback, no need to require all adapters support v } else { - log::info!("not all adapters support {data_format:?}, luids = {luids:?}"); - vec![] + let Ok(displays) = crate::Display::all() else { + log::error!("failed to get displays"); + return vec![]; + }; + if displays.is_empty() { + log::error!("no display found"); + return vec![]; + } + let luids = displays + .iter() + .map(|d| d.adapter_luid()) + .collect::>(); + if luids + .iter() + .all(|luid| v.iter().any(|f| Some(f.luid) == *luid)) + { + v + } else { + log::info!("not all adapters support {data_format:?}, luids = {luids:?}"); + vec![] + } } }