scrap: save best codec info in LocalConfig
Signed-off-by: 21pages <pages21@163.com>
This commit is contained in:
parent
27091dec0e
commit
2a91fb842d
5
Cargo.lock
generated
5
Cargo.lock
generated
@ -2236,11 +2236,14 @@ checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
|
|||||||
[[package]]
|
[[package]]
|
||||||
name = "hwcodec"
|
name = "hwcodec"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
source = "git+https://github.com/21pages/hwcodec#888af61b3e960d30ef1f2e49eb0bcf9f6f7a14ee"
|
source = "git+https://github.com/21pages/hwcodec#6bb387828c9aa69861b707b0f71472b21b5b1711"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bindgen",
|
"bindgen",
|
||||||
"cc",
|
"cc",
|
||||||
"log",
|
"log",
|
||||||
|
"serde 1.0.136",
|
||||||
|
"serde_derive",
|
||||||
|
"serde_json 1.0.79",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
@ -125,18 +125,16 @@ impl Encoder {
|
|||||||
}
|
}
|
||||||
let current_encoder_name = HwEncoder::current_name();
|
let current_encoder_name = HwEncoder::current_name();
|
||||||
if states.len() > 0 {
|
if states.len() > 0 {
|
||||||
let (encoder_h264, encoder_h265) = HwEncoder::best();
|
let best = HwEncoder::best();
|
||||||
let enabled_h264 = encoder_h264.is_some()
|
let enabled_h264 =
|
||||||
&& states.len() > 0
|
best.h264.is_some() && states.len() > 0 && states.iter().all(|(_, s)| s.H264);
|
||||||
&& states.iter().all(|(_, s)| s.H264);
|
let enabled_h265 =
|
||||||
let enabled_h265 = encoder_h265.is_some()
|
best.h265.is_some() && states.len() > 0 && states.iter().all(|(_, s)| s.H265);
|
||||||
&& states.len() > 0
|
|
||||||
&& states.iter().all(|(_, s)| s.H265);
|
|
||||||
|
|
||||||
// score encoder
|
// score encoder
|
||||||
let mut score_vpx = 90;
|
let mut score_vpx = 90;
|
||||||
let mut score_h264 = encoder_h264.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 = encoder_h265.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 decoder
|
||||||
score_vpx += states.iter().map(|s| s.1.ScoreVpx).sum::<i32>();
|
score_vpx += states.iter().map(|s| s.1.ScoreVpx).sum::<i32>();
|
||||||
@ -148,9 +146,9 @@ impl Encoder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if enabled_h265 && score_h265 >= score_vpx && score_h265 >= score_h264 {
|
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 {
|
} 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 {
|
} else {
|
||||||
*current_encoder_name.lock().unwrap() = None;
|
*current_encoder_name.lock().unwrap() = None;
|
||||||
}
|
}
|
||||||
@ -191,11 +189,11 @@ impl Decoder {
|
|||||||
|
|
||||||
#[cfg(feature = "hwcodec")]
|
#[cfg(feature = "hwcodec")]
|
||||||
{
|
{
|
||||||
let (h264, h265) = super::hwcodec::HwDecoder::best();
|
let best = super::hwcodec::HwDecoder::best(false);
|
||||||
state.H264 = h264.is_some();
|
state.H264 = best.h264.is_some();
|
||||||
state.ScoreH264 = h264.map_or(0, |c| c.score);
|
state.ScoreH264 = best.h264.map_or(0, |c| c.score);
|
||||||
state.H265 = h265.is_some();
|
state.H265 = best.h265.is_some();
|
||||||
state.ScoreH265 = h265.map_or(0, |c| c.score);
|
state.ScoreH265 = best.h265.map_or(0, |c| c.score);
|
||||||
}
|
}
|
||||||
|
|
||||||
state
|
state
|
||||||
|
@ -4,6 +4,7 @@ use crate::{
|
|||||||
};
|
};
|
||||||
use hbb_common::{
|
use hbb_common::{
|
||||||
anyhow::{anyhow, Context},
|
anyhow::{anyhow, Context},
|
||||||
|
config::LocalConfig,
|
||||||
lazy_static, log,
|
lazy_static, log,
|
||||||
message_proto::{H264s, H265s, Message, VideoFrame, H264, H265},
|
message_proto::{H264s, H265s, Message, VideoFrame, H264, H265},
|
||||||
ResultType,
|
ResultType,
|
||||||
@ -11,7 +12,7 @@ use hbb_common::{
|
|||||||
use hwcodec::{
|
use hwcodec::{
|
||||||
decode::{DecodeContext, DecodeFrame, Decoder},
|
decode::{DecodeContext, DecodeFrame, Decoder},
|
||||||
encode::{EncodeContext, EncodeFrame, Encoder},
|
encode::{EncodeContext, EncodeFrame, Encoder},
|
||||||
ffmpeg::{CodecInfo, DataFormat},
|
ffmpeg::{CodecInfo, CodecInfos, DataFormat},
|
||||||
AVPixelFormat,
|
AVPixelFormat,
|
||||||
Quality::{self, *},
|
Quality::{self, *},
|
||||||
RateContorl::{self, *},
|
RateContorl::{self, *},
|
||||||
@ -136,20 +137,30 @@ impl EncoderApi for HwEncoder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl HwEncoder {
|
impl HwEncoder {
|
||||||
pub fn best() -> (Option<CodecInfo>, Option<CodecInfo>) {
|
pub fn best() -> CodecInfos {
|
||||||
let ctx = EncodeContext {
|
let key = "bestHwEncoders";
|
||||||
name: String::from(""),
|
match get_config(key) {
|
||||||
width: 1920,
|
Ok(config) => config,
|
||||||
height: 1080,
|
Err(_) => {
|
||||||
pixfmt: DEFAULT_PIXFMT,
|
let ctx = EncodeContext {
|
||||||
align: HW_STRIDE_ALIGN as _,
|
name: String::from(""),
|
||||||
bitrate: 0,
|
width: 1920,
|
||||||
timebase: DEFAULT_TIME_BASE,
|
height: 1080,
|
||||||
gop: DEFAULT_GOP,
|
pixfmt: DEFAULT_PIXFMT,
|
||||||
quality: DEFAULT_HW_QUALITY,
|
align: HW_STRIDE_ALIGN as _,
|
||||||
rc: DEFAULT_RC,
|
bitrate: 0,
|
||||||
};
|
timebase: DEFAULT_TIME_BASE,
|
||||||
CodecInfo::score(Encoder::avaliable_encoders(ctx))
|
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<Mutex<Option<String>>> {
|
pub fn current_name() -> Arc<Mutex<Option<String>>> {
|
||||||
@ -223,22 +234,43 @@ pub struct HwDecoders {
|
|||||||
|
|
||||||
impl HwDecoder {
|
impl HwDecoder {
|
||||||
/// H264, H265 decoder info with the highest score.
|
/// H264, H265 decoder info with the highest score.
|
||||||
/// Because available_decoders is singleton, it returns same result in the same process.
|
pub fn best(force_reset: bool) -> CodecInfos {
|
||||||
pub fn best() -> (Option<CodecInfo>, Option<CodecInfo>) {
|
let key = "bestHwDecoders";
|
||||||
CodecInfo::score(Decoder::avaliable_decoders())
|
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 {
|
pub fn new_decoders() -> HwDecoders {
|
||||||
let (h264_info, h265_info) = HwDecoder::best();
|
let best = HwDecoder::best(false);
|
||||||
let mut h264: Option<HwDecoder> = None;
|
let mut h264: Option<HwDecoder> = None;
|
||||||
let mut h265: Option<HwDecoder> = None;
|
let mut h265: Option<HwDecoder> = None;
|
||||||
|
let mut fail = false;
|
||||||
|
|
||||||
if let Some(info) = h264_info {
|
if let Some(info) = best.h264 {
|
||||||
h264 = HwDecoder::new(info).ok();
|
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();
|
h265 = HwDecoder::new(info).ok();
|
||||||
|
if h265.is_none() {
|
||||||
|
fail = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
if fail {
|
||||||
|
HwDecoder::best(true);
|
||||||
|
// TODO: notify encoder
|
||||||
|
}
|
||||||
|
|
||||||
if h264.is_some() {
|
if h264.is_some() {
|
||||||
log::info!("h264 decoder:{:?}", h264.as_ref().unwrap().info);
|
log::info!("h264 decoder:{:?}", h264.as_ref().unwrap().info);
|
||||||
}
|
}
|
||||||
@ -302,3 +334,21 @@ impl HwDecoderImage<'_> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_config(k: &str) -> ResultType<CodecInfos> {
|
||||||
|
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)),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user