scrap: fix update_video_encoder

Signed-off-by: 21pages <pages21@163.com>
This commit is contained in:
21pages 2022-06-02 07:21:21 +08:00
parent 7e6c38e6d2
commit 327bdb741c
2 changed files with 45 additions and 36 deletions

View File

@ -11,12 +11,13 @@ use crate::vpxcodec::*;
use hbb_common::{
anyhow::anyhow,
log,
message_proto::{video_frame, ImageQuality, Message, VP9s, VideoCodecState},
ResultType,
};
#[cfg(feature = "hwcodec")]
use hbb_common::{
lazy_static, log,
lazy_static,
message_proto::{H264s, H265s},
};
@ -33,6 +34,7 @@ pub struct HwEncoderConfig {
pub quallity: ImageQuality,
}
#[derive(Debug, Clone)]
pub enum EncoderCfg {
VPX(VpxEncoderConfig),
HW(HwEncoderConfig),
@ -87,6 +89,7 @@ pub enum EncoderUpdate {
impl Encoder {
pub fn new(config: EncoderCfg) -> ResultType<Encoder> {
log::info!("new encoder:{:?}", config);
match config {
EncoderCfg::VPX(_) => Ok(Encoder {
codec: Box::new(VpxEncoder::new(config)?),
@ -103,6 +106,7 @@ impl Encoder {
// TODO
pub fn update_video_encoder(id: i32, update: EncoderUpdate) {
log::info!("update video encoder:{:?}", update);
#[cfg(feature = "hwcodec")]
{
let mut states = VIDEO_CODEC_STATES.lock().unwrap();
@ -119,9 +123,11 @@ impl Encoder {
}
}
}
let current_encoder_name = HwEncoder::current_name();
if states.len() > 0 {
let (encoder_h264, encoder_h265) = HwEncoder::best();
let mut enabled_h264 = encoder_h264.is_some();
let mut enabled_h265 = encoder_h265.is_some();
let mut enabled_h264 = encoder_h264.is_some() && states.iter().any(|(_, s)| s.H264);
let mut enabled_h265 = encoder_h265.is_some() && states.iter().any(|(_, s)| s.H265);
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);
@ -134,7 +140,6 @@ impl Encoder {
score_h265 += state.1.ScoreH265;
}
let current_encoder_name = HwEncoder::current_name();
if enabled_h265 && score_h265 >= score_vpx && score_h265 >= score_h264 {
*current_encoder_name.lock().unwrap() = Some(encoder_h265.unwrap().name);
} else if enabled_h264 && score_h264 >= score_vpx && score_h264 >= score_h265 {
@ -142,7 +147,6 @@ impl Encoder {
} else {
*current_encoder_name.lock().unwrap() = None;
}
if states.len() > 0 {
log::info!(
"connection count:{}, h264:{}, h265:{}, score: vpx({}), h264({}), h265({}), set current encoder name {:?}",
states.len(),
@ -153,6 +157,8 @@ impl Encoder {
score_h265,
current_encoder_name.lock().unwrap()
)
} else {
*current_encoder_name.lock().unwrap() = None;
}
}
#[cfg(not(feature = "hwcodec"))]

View File

@ -781,6 +781,22 @@ impl Connection {
if let Some(message::Union::login_request(lr)) = msg.union {
if let Some(o) = lr.option.as_ref() {
self.update_option(o).await;
if let Some(q) = o.video_codec_state.clone().take() {
scrap::codec::Encoder::update_video_encoder(
self.inner.id(),
scrap::codec::EncoderUpdate::State(q),
);
} else {
scrap::codec::Encoder::update_video_encoder(
self.inner.id(),
scrap::codec::EncoderUpdate::DisableHwIfNotExist,
);
}
} else {
scrap::codec::Encoder::update_video_encoder(
self.inner.id(),
scrap::codec::EncoderUpdate::DisableHwIfNotExist,
);
}
self.video_ack_required = lr.video_ack_required;
if self.authorized {
@ -1187,19 +1203,6 @@ impl Connection {
}
}
}
// TODO: add option
if let Some(q) = o.video_codec_state.clone().take() {
scrap::codec::Encoder::update_video_encoder(
self.inner.id(),
scrap::codec::EncoderUpdate::State(q),
);
} else {
scrap::codec::Encoder::update_video_encoder(
self.inner.id(),
scrap::codec::EncoderUpdate::DisableHwIfNotExist,
);
}
}
fn on_close(&mut self, reason: &str, lock: bool) {