Merge pull request #2530 from 21pages/hwcodec

check option before new hwcodec decoders
This commit is contained in:
RustDesk 2022-12-13 17:00:43 +08:00 committed by GitHub
commit 55cbf94a71
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 35 additions and 9 deletions

View File

@ -218,7 +218,7 @@ impl Encoder {
#[inline] #[inline]
pub fn current_hw_encoder_name() -> Option<String> { pub fn current_hw_encoder_name() -> Option<String> {
#[cfg(feature = "hwcodec")] #[cfg(feature = "hwcodec")]
if check_hwcodec_config() { if enable_hwcodec_option() {
return HwEncoder::current_name().lock().unwrap().clone(); return HwEncoder::current_name().lock().unwrap().clone();
} else { } else {
return None; return None;
@ -229,7 +229,7 @@ impl Encoder {
pub fn supported_encoding() -> (bool, bool) { pub fn supported_encoding() -> (bool, bool) {
#[cfg(feature = "hwcodec")] #[cfg(feature = "hwcodec")]
if check_hwcodec_config() { if enable_hwcodec_option() {
let best = HwEncoder::best(); let best = HwEncoder::best();
( (
best.h264.as_ref().map_or(false, |c| c.score > 0), best.h264.as_ref().map_or(false, |c| c.score > 0),
@ -246,7 +246,7 @@ impl Encoder {
impl Decoder { impl Decoder {
pub fn video_codec_state(_id: &str) -> VideoCodecState { pub fn video_codec_state(_id: &str) -> VideoCodecState {
#[cfg(feature = "hwcodec")] #[cfg(feature = "hwcodec")]
if check_hwcodec_config() { if enable_hwcodec_option() {
let best = HwDecoder::best(); let best = HwDecoder::best();
return VideoCodecState { return VideoCodecState {
score_vpx: SCORE_VPX, score_vpx: SCORE_VPX,
@ -257,7 +257,7 @@ impl Decoder {
}; };
} }
#[cfg(feature = "mediacodec")] #[cfg(feature = "mediacodec")]
if check_hwcodec_config() { if enable_hwcodec_option() {
let score_h264 = if H264_DECODER_SUPPORT.load(std::sync::atomic::Ordering::SeqCst) { let score_h264 = if H264_DECODER_SUPPORT.load(std::sync::atomic::Ordering::SeqCst) {
92 92
} else { } else {
@ -287,11 +287,19 @@ impl Decoder {
Decoder { Decoder {
vpx, vpx,
#[cfg(feature = "hwcodec")] #[cfg(feature = "hwcodec")]
hw: HwDecoder::new_decoders(), hw: if enable_hwcodec_option() {
HwDecoder::new_decoders()
} else {
HwDecoders::default()
},
#[cfg(feature = "hwcodec")] #[cfg(feature = "hwcodec")]
i420: vec![], i420: vec![],
#[cfg(feature = "mediacodec")] #[cfg(feature = "mediacodec")]
media_codec: MediaCodecDecoder::new_decoders(), media_codec: if enable_hwcodec_option() {
MediaCodecDecoder::new_decoders()
} else {
MediaCodecDecoders::default()
},
} }
} }
@ -415,7 +423,7 @@ impl Decoder {
} }
#[cfg(any(feature = "hwcodec", feature = "mediacodec"))] #[cfg(any(feature = "hwcodec", feature = "mediacodec"))]
fn check_hwcodec_config() -> bool { fn enable_hwcodec_option() -> bool {
if let Some(v) = Config2::get().options.get("enable-hwcodec") { if let Some(v) = Config2::get().options.get("enable-hwcodec") {
return v != "N"; return v != "N";
} }

View File

@ -175,6 +175,7 @@ pub struct HwDecoder {
pub info: CodecInfo, pub info: CodecInfo,
} }
#[derive(Default)]
pub struct HwDecoders { pub struct HwDecoders {
pub h264: Option<HwDecoder>, pub h264: Option<HwDecoder>,
pub h265: Option<HwDecoder>, pub h265: Option<HwDecoder>,
@ -189,22 +190,28 @@ impl HwDecoder {
} }
pub fn new_decoders() -> HwDecoders { pub fn new_decoders() -> HwDecoders {
flog("enter new_decoders");
let best = HwDecoder::best(); let best = HwDecoder::best();
flog(&format!("best:${:?}", best));
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; let mut fail = false;
if let Some(info) = best.h264 { if let Some(info) = best.h264 {
flog(&format!("before new h264 codec"));
h264 = HwDecoder::new(info).ok(); h264 = HwDecoder::new(info).ok();
if h264.is_none() { if h264.is_none() {
fail = true; fail = true;
} }
flog(&format!("new h264 codec result:{:}", h264.is_some()));
} }
if let Some(info) = best.h265 { if let Some(info) = best.h265 {
flog(&format!("before new h265 codec"));
h265 = HwDecoder::new(info).ok(); h265 = HwDecoder::new(info).ok();
if h265.is_none() { if h265.is_none() {
fail = true; fail = true;
} }
flog(&format!("new h265 codec result:{:}", h265.is_some()));
} }
if fail { if fail {
check_config_process(true); check_config_process(true);
@ -321,11 +328,21 @@ pub fn check_config_process(force_reset: bool) {
} }
if let Ok(exe) = std::env::current_exe() { if let Ok(exe) = std::env::current_exe() {
std::thread::spawn(move || { std::thread::spawn(move || {
std::process::Command::new(exe) let result = std::process::Command::new(exe)
.arg("--check-hwcodec-config") .arg("--check-hwcodec-config")
.status() .status()
.ok(); .ok();
flog(&format!("check codec process run result:{:?}", result));
HwCodecConfig::refresh(); HwCodecConfig::refresh();
}); });
}; };
} }
pub fn flog(s: &str) {
use hbb_common::chrono::prelude::*;
use std::io::Write;
let mut option = std::fs::OpenOptions::new();
if let Ok(mut f) = option.append(true).create(true).open("/tmp/log.txt") {
write!(&mut f, "{:?} {}\n", Local::now(), s).ok();
}
}

View File

@ -37,6 +37,7 @@ impl Deref for MediaCodecDecoder {
} }
} }
#[derive(Default)]
pub struct MediaCodecDecoders { pub struct MediaCodecDecoders {
pub h264: Option<MediaCodecDecoder>, pub h264: Option<MediaCodecDecoder>,
pub h265: Option<MediaCodecDecoder>, pub h265: Option<MediaCodecDecoder>,