Show current codec in menu when auto codec is chosen (#7942)

* change negotiated codec name to negotiated codec format

Signed-off-by: 21pages <pages21@163.com>

* fallback to vp9 directly if failed to create encoder

Current fallback method is clear hwcodec config

Signed-off-by: 21pages <pages21@163.com>

* show current codec in menu when auto codec is chosen

Signed-off-by: 21pages <pages21@163.com>

---------

Signed-off-by: 21pages <pages21@163.com>
This commit is contained in:
21pages
2024-05-07 20:34:23 +08:00
committed by GitHub
parent e373144350
commit 3746fd88b5
4 changed files with 130 additions and 143 deletions

View File

@@ -36,7 +36,7 @@ use hbb_common::{config::Config2, lazy_static};
lazy_static::lazy_static! {
static ref PEER_DECODINGS: Arc<Mutex<HashMap<i32, SupportedDecoding>>> = Default::default();
static ref ENCODE_CODEC_NAME: Arc<Mutex<CodecName>> = Arc::new(Mutex::new(CodecName::VP9));
static ref ENCODE_CODEC_FORMAT: Arc<Mutex<CodecFormat>> = Arc::new(Mutex::new(CodecFormat::VP9));
static ref THREAD_LOG_TIME: Arc<Mutex<Option<Instant>>> = Arc::new(Mutex::new(None));
}
@@ -138,7 +138,7 @@ impl Encoder {
Err(e) => {
log::error!("new hw encoder failed: {e:?}, clear config");
hbb_common::config::HwCodecConfig::clear_ram();
*ENCODE_CODEC_NAME.lock().unwrap() = CodecName::VP9;
*ENCODE_CODEC_FORMAT.lock().unwrap() = CodecFormat::VP9;
Err(e)
}
},
@@ -150,7 +150,7 @@ impl Encoder {
Err(e) => {
log::error!("new vram encoder failed: {e:?}, clear config");
hbb_common::config::HwCodecConfig::clear_vram();
*ENCODE_CODEC_NAME.lock().unwrap() = CodecName::VP9;
*ENCODE_CODEC_FORMAT.lock().unwrap() = CodecFormat::VP9;
Err(e)
}
},
@@ -194,20 +194,20 @@ impl Encoder {
#[cfg(feature = "vram")]
if enable_vram_option() {
if _all_support_h264_decoding {
if VRamEncoder::available(CodecName::H264VRAM).len() > 0 {
if VRamEncoder::available(CodecFormat::H264).len() > 0 {
h264vram_encoding = true;
}
}
if _all_support_h265_decoding {
if VRamEncoder::available(CodecName::H265VRAM).len() > 0 {
if VRamEncoder::available(CodecFormat::H265).len() > 0 {
h265vram_encoding = true;
}
}
}
#[allow(unused_mut)]
let mut h264hw_encoding = None;
let mut h264hw_encoding: Option<String> = None;
#[allow(unused_mut)]
let mut h265hw_encoding = None;
let mut h265hw_encoding: Option<String> = None;
#[cfg(feature = "hwcodec")]
if enable_hwcodec_option() {
if _all_support_h264_decoding {
@@ -223,7 +223,7 @@ impl Encoder {
_all_support_h264_decoding && (h264vram_encoding || h264hw_encoding.is_some());
let h265_useable =
_all_support_h265_decoding && (h265vram_encoding || h265hw_encoding.is_some());
let mut name = ENCODE_CODEC_NAME.lock().unwrap();
let mut format = ENCODE_CODEC_FORMAT.lock().unwrap();
let preferences: Vec<_> = decodings
.iter()
.filter(|(_, s)| {
@@ -251,32 +251,28 @@ impl Encoder {
let preference = most_frequent.enum_value_or(PreferCodec::Auto);
#[allow(unused_mut)]
let mut auto_codec = CodecName::VP9;
let mut auto_codec = CodecFormat::VP9;
let mut system = System::new();
system.refresh_memory();
if vp8_useable && system.total_memory() <= 4 * 1024 * 1024 * 1024 {
// 4 Gb
auto_codec = CodecName::VP8
auto_codec = CodecFormat::VP8
}
*name = match preference {
PreferCodec::VP8 => CodecName::VP8,
PreferCodec::VP9 => CodecName::VP9,
PreferCodec::AV1 => CodecName::AV1,
*format = match preference {
PreferCodec::VP8 => CodecFormat::VP8,
PreferCodec::VP9 => CodecFormat::VP9,
PreferCodec::AV1 => CodecFormat::AV1,
PreferCodec::H264 => {
if h264vram_encoding {
CodecName::H264VRAM
} else if let Some(v) = h264hw_encoding {
CodecName::H264RAM(v)
if h264vram_encoding || h264hw_encoding.is_some() {
CodecFormat::H264
} else {
auto_codec
}
}
PreferCodec::H265 => {
if h265vram_encoding {
CodecName::H265VRAM
} else if let Some(v) = h265hw_encoding {
CodecName::H265RAM(v)
if h265vram_encoding || h265hw_encoding.is_some() {
CodecFormat::H265
} else {
auto_codec
}
@@ -291,14 +287,14 @@ impl Encoder {
"connection count: {}, used preference: {:?}, encoder: {:?}",
decodings.len(),
preference,
*name
*format
)
}
}
#[inline]
pub fn negotiated_codec() -> CodecName {
ENCODE_CODEC_NAME.lock().unwrap().clone()
pub fn negotiated_codec() -> CodecFormat {
ENCODE_CODEC_FORMAT.lock().unwrap().clone()
}
pub fn supported_encoding() -> SupportedEncoding {
@@ -321,31 +317,31 @@ impl Encoder {
}
#[cfg(feature = "vram")]
if enable_vram_option() {
encoding.h264 |= VRamEncoder::available(CodecName::H264VRAM).len() > 0;
encoding.h265 |= VRamEncoder::available(CodecName::H265VRAM).len() > 0;
encoding.h264 |= VRamEncoder::available(CodecFormat::H264).len() > 0;
encoding.h265 |= VRamEncoder::available(CodecFormat::H265).len() > 0;
}
encoding
}
pub fn set_fallback(config: &EncoderCfg) {
let name = match config {
let format = match config {
EncoderCfg::VPX(vpx) => match vpx.codec {
VpxVideoCodecId::VP8 => CodecName::VP8,
VpxVideoCodecId::VP9 => CodecName::VP9,
VpxVideoCodecId::VP8 => CodecFormat::VP8,
VpxVideoCodecId::VP9 => CodecFormat::VP9,
},
EncoderCfg::AOM(_) => CodecName::AV1,
EncoderCfg::AOM(_) => CodecFormat::AV1,
#[cfg(feature = "hwcodec")]
EncoderCfg::HWRAM(hw) => {
if hw.name.to_lowercase().contains("h264") {
CodecName::H264RAM(hw.name.clone())
CodecFormat::H264
} else {
CodecName::H265RAM(hw.name.clone())
CodecFormat::H265
}
}
#[cfg(feature = "vram")]
EncoderCfg::VRAM(vram) => match vram.feature.data_format {
hwcodec::common::DataFormat::H264 => CodecName::H264VRAM,
hwcodec::common::DataFormat::H265 => CodecName::H265VRAM,
hwcodec::common::DataFormat::H264 => CodecFormat::H264,
hwcodec::common::DataFormat::H265 => CodecFormat::H265,
_ => {
log::error!(
"should not reach here, vram not support {:?}",
@@ -355,10 +351,10 @@ impl Encoder {
}
},
};
let current = ENCODE_CODEC_NAME.lock().unwrap().clone();
if current != name {
log::info!("codec fallback: {:?} -> {:?}", current, name);
*ENCODE_CODEC_NAME.lock().unwrap() = name;
let current = ENCODE_CODEC_FORMAT.lock().unwrap().clone();
if current != format {
log::info!("codec fallback: {:?} -> {:?}", current, format);
*ENCODE_CODEC_FORMAT.lock().unwrap() = format;
}
}

View File

@@ -186,8 +186,8 @@ impl EncoderApi for VRamEncoder {
}
impl VRamEncoder {
pub fn try_get(device: &AdapterDevice, name: CodecName) -> Option<FeatureContext> {
let v: Vec<_> = Self::available(name)
pub fn try_get(device: &AdapterDevice, format: CodecFormat) -> Option<FeatureContext> {
let v: Vec<_> = Self::available(format)
.drain(..)
.filter(|e| e.luid == device.luid)
.collect();
@@ -202,15 +202,15 @@ impl VRamEncoder {
}
}
pub fn available(name: CodecName) -> Vec<FeatureContext> {
pub fn available(format: CodecFormat) -> Vec<FeatureContext> {
let not_use = ENOCDE_NOT_USE.lock().unwrap().clone();
if not_use.values().any(|not_use| *not_use) {
log::info!("currently not use vram encoders: {not_use:?}");
return vec![];
}
let data_format = match name {
CodecName::H264VRAM => DataFormat::H264,
CodecName::H265VRAM => DataFormat::H265,
let data_format = match format {
CodecFormat::H264 => DataFormat::H264,
CodecFormat::H265 => DataFormat::H265,
_ => return vec![],
};
let Ok(displays) = crate::Display::all() else {