From 2a91fb842d9e5d4b18074472387c5efe0e076d07 Mon Sep 17 00:00:00 2001 From: 21pages Date: Tue, 7 Jun 2022 19:35:18 +0800 Subject: [PATCH] scrap: save best codec info in LocalConfig Signed-off-by: 21pages --- Cargo.lock | 5 +- libs/scrap/src/common/codec.rs | 30 +++++------ libs/scrap/src/common/hwcodec.rs | 92 ++++++++++++++++++++++++-------- 3 files changed, 89 insertions(+), 38 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b07495ad3..a42715628 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2236,11 +2236,14 @@ checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" [[package]] name = "hwcodec" version = "0.1.0" -source = "git+https://github.com/21pages/hwcodec#888af61b3e960d30ef1f2e49eb0bcf9f6f7a14ee" +source = "git+https://github.com/21pages/hwcodec#6bb387828c9aa69861b707b0f71472b21b5b1711" dependencies = [ "bindgen", "cc", "log", + "serde 1.0.136", + "serde_derive", + "serde_json 1.0.79", ] [[package]] diff --git a/libs/scrap/src/common/codec.rs b/libs/scrap/src/common/codec.rs index e6c925b28..d57b77b40 100644 --- a/libs/scrap/src/common/codec.rs +++ b/libs/scrap/src/common/codec.rs @@ -125,18 +125,16 @@ impl Encoder { } let current_encoder_name = HwEncoder::current_name(); if states.len() > 0 { - let (encoder_h264, encoder_h265) = HwEncoder::best(); - let enabled_h264 = encoder_h264.is_some() - && states.len() > 0 - && states.iter().all(|(_, s)| s.H264); - let enabled_h265 = encoder_h265.is_some() - && states.len() > 0 - && states.iter().all(|(_, s)| s.H265); + let best = HwEncoder::best(); + let enabled_h264 = + best.h264.is_some() && states.len() > 0 && states.iter().all(|(_, s)| s.H264); + let enabled_h265 = + best.h265.is_some() && states.len() > 0 && states.iter().all(|(_, s)| s.H265); // score encoder let mut score_vpx = 90; - let mut score_h264 = encoder_h264.as_ref().map_or(0, |c| c.score); - let mut score_h265 = encoder_h265.as_ref().map_or(0, |c| c.score); + let mut score_h264 = best.h264.as_ref().map_or(0, |c| c.score); + let mut score_h265 = best.h265.as_ref().map_or(0, |c| c.score); // score decoder score_vpx += states.iter().map(|s| s.1.ScoreVpx).sum::(); @@ -148,9 +146,9 @@ impl Encoder { } if enabled_h265 && score_h265 >= score_vpx && score_h265 >= score_h264 { - *current_encoder_name.lock().unwrap() = Some(encoder_h265.unwrap().name); + *current_encoder_name.lock().unwrap() = Some(best.h265.unwrap().name); } else if enabled_h264 && score_h264 >= score_vpx && score_h264 >= score_h265 { - *current_encoder_name.lock().unwrap() = Some(encoder_h264.unwrap().name); + *current_encoder_name.lock().unwrap() = Some(best.h264.unwrap().name); } else { *current_encoder_name.lock().unwrap() = None; } @@ -191,11 +189,11 @@ impl Decoder { #[cfg(feature = "hwcodec")] { - let (h264, h265) = super::hwcodec::HwDecoder::best(); - state.H264 = h264.is_some(); - state.ScoreH264 = h264.map_or(0, |c| c.score); - state.H265 = h265.is_some(); - state.ScoreH265 = h265.map_or(0, |c| c.score); + let best = super::hwcodec::HwDecoder::best(false); + state.H264 = best.h264.is_some(); + state.ScoreH264 = best.h264.map_or(0, |c| c.score); + state.H265 = best.h265.is_some(); + state.ScoreH265 = best.h265.map_or(0, |c| c.score); } state diff --git a/libs/scrap/src/common/hwcodec.rs b/libs/scrap/src/common/hwcodec.rs index 95dcd0353..18c153587 100644 --- a/libs/scrap/src/common/hwcodec.rs +++ b/libs/scrap/src/common/hwcodec.rs @@ -4,6 +4,7 @@ use crate::{ }; use hbb_common::{ anyhow::{anyhow, Context}, + config::LocalConfig, lazy_static, log, message_proto::{H264s, H265s, Message, VideoFrame, H264, H265}, ResultType, @@ -11,7 +12,7 @@ use hbb_common::{ use hwcodec::{ decode::{DecodeContext, DecodeFrame, Decoder}, encode::{EncodeContext, EncodeFrame, Encoder}, - ffmpeg::{CodecInfo, DataFormat}, + ffmpeg::{CodecInfo, CodecInfos, DataFormat}, AVPixelFormat, Quality::{self, *}, RateContorl::{self, *}, @@ -136,20 +137,30 @@ impl EncoderApi for HwEncoder { } impl HwEncoder { - pub fn best() -> (Option, Option) { - let ctx = EncodeContext { - name: String::from(""), - width: 1920, - height: 1080, - pixfmt: DEFAULT_PIXFMT, - align: HW_STRIDE_ALIGN as _, - bitrate: 0, - timebase: DEFAULT_TIME_BASE, - gop: DEFAULT_GOP, - quality: DEFAULT_HW_QUALITY, - rc: DEFAULT_RC, - }; - CodecInfo::score(Encoder::avaliable_encoders(ctx)) + pub fn best() -> CodecInfos { + let key = "bestHwEncoders"; + match get_config(key) { + Ok(config) => config, + Err(_) => { + let ctx = EncodeContext { + name: String::from(""), + width: 1920, + height: 1080, + pixfmt: DEFAULT_PIXFMT, + align: HW_STRIDE_ALIGN as _, + bitrate: 0, + timebase: DEFAULT_TIME_BASE, + gop: DEFAULT_GOP, + quality: DEFAULT_HW_QUALITY, + rc: DEFAULT_RC, + }; + let encoders = CodecInfo::score(Encoder::avaliable_encoders(ctx)); + let _ = set_config(key, &encoders) + .map_err(|e| log::error!("{:?}", e)) + .ok(); + encoders + } + } } pub fn current_name() -> Arc>> { @@ -223,22 +234,43 @@ pub struct HwDecoders { impl HwDecoder { /// H264, H265 decoder info with the highest score. - /// Because available_decoders is singleton, it returns same result in the same process. - pub fn best() -> (Option, Option) { - CodecInfo::score(Decoder::avaliable_decoders()) + pub fn best(force_reset: bool) -> CodecInfos { + let key = "bestHwDecoders"; + let config = get_config(key); + if !force_reset && config.is_ok() { + config.unwrap() + } else { + let decoders = CodecInfo::score(Decoder::avaliable_decoders()); + set_config(key, &decoders) + .map_err(|e| log::error!("{:?}", e)) + .ok(); + decoders + } } pub fn new_decoders() -> HwDecoders { - let (h264_info, h265_info) = HwDecoder::best(); + let best = HwDecoder::best(false); let mut h264: Option = None; let mut h265: Option = None; + let mut fail = false; - if let Some(info) = h264_info { + if let Some(info) = best.h264 { h264 = HwDecoder::new(info).ok(); + if h264.is_none() { + fail = true; + } } - if let Some(info) = h265_info { + if let Some(info) = best.h265 { h265 = HwDecoder::new(info).ok(); + if h265.is_none() { + fail = true; + } } + if fail { + HwDecoder::best(true); + // TODO: notify encoder + } + if h264.is_some() { log::info!("h264 decoder:{:?}", h264.as_ref().unwrap().info); } @@ -302,3 +334,21 @@ impl HwDecoderImage<'_> { } } } + +fn get_config(k: &str) -> ResultType { + let v = LocalConfig::get_option(k); + match CodecInfos::deserialize(&v) { + Ok(v) => Ok(v), + Err(_) => Err(anyhow!("Failed to get config:{}", k)), + } +} + +fn set_config(k: &str, v: &CodecInfos) -> ResultType<()> { + match v.serialize() { + Ok(v) => { + LocalConfig::set_option(k.to_owned(), v); + Ok(()) + } + Err(_) => Err(anyhow!("Failed to set config:{}", k)), + } +}