scrap: fix update_video_encoder
Signed-off-by: 21pages <pages21@163.com>
This commit is contained in:
parent
7e6c38e6d2
commit
327bdb741c
@ -11,12 +11,13 @@ use crate::vpxcodec::*;
|
|||||||
|
|
||||||
use hbb_common::{
|
use hbb_common::{
|
||||||
anyhow::anyhow,
|
anyhow::anyhow,
|
||||||
|
log,
|
||||||
message_proto::{video_frame, ImageQuality, Message, VP9s, VideoCodecState},
|
message_proto::{video_frame, ImageQuality, Message, VP9s, VideoCodecState},
|
||||||
ResultType,
|
ResultType,
|
||||||
};
|
};
|
||||||
#[cfg(feature = "hwcodec")]
|
#[cfg(feature = "hwcodec")]
|
||||||
use hbb_common::{
|
use hbb_common::{
|
||||||
lazy_static, log,
|
lazy_static,
|
||||||
message_proto::{H264s, H265s},
|
message_proto::{H264s, H265s},
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -33,6 +34,7 @@ pub struct HwEncoderConfig {
|
|||||||
pub quallity: ImageQuality,
|
pub quallity: ImageQuality,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
pub enum EncoderCfg {
|
pub enum EncoderCfg {
|
||||||
VPX(VpxEncoderConfig),
|
VPX(VpxEncoderConfig),
|
||||||
HW(HwEncoderConfig),
|
HW(HwEncoderConfig),
|
||||||
@ -87,6 +89,7 @@ pub enum EncoderUpdate {
|
|||||||
|
|
||||||
impl Encoder {
|
impl Encoder {
|
||||||
pub fn new(config: EncoderCfg) -> ResultType<Encoder> {
|
pub fn new(config: EncoderCfg) -> ResultType<Encoder> {
|
||||||
|
log::info!("new encoder:{:?}", config);
|
||||||
match config {
|
match config {
|
||||||
EncoderCfg::VPX(_) => Ok(Encoder {
|
EncoderCfg::VPX(_) => Ok(Encoder {
|
||||||
codec: Box::new(VpxEncoder::new(config)?),
|
codec: Box::new(VpxEncoder::new(config)?),
|
||||||
@ -103,6 +106,7 @@ impl Encoder {
|
|||||||
|
|
||||||
// TODO
|
// TODO
|
||||||
pub fn update_video_encoder(id: i32, update: EncoderUpdate) {
|
pub fn update_video_encoder(id: i32, update: EncoderUpdate) {
|
||||||
|
log::info!("update video encoder:{:?}", update);
|
||||||
#[cfg(feature = "hwcodec")]
|
#[cfg(feature = "hwcodec")]
|
||||||
{
|
{
|
||||||
let mut states = VIDEO_CODEC_STATES.lock().unwrap();
|
let mut states = VIDEO_CODEC_STATES.lock().unwrap();
|
||||||
@ -119,30 +123,30 @@ impl Encoder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
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 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);
|
|
||||||
|
|
||||||
for state in states.iter() {
|
|
||||||
enabled_h264 = enabled_h264 && state.1.H264;
|
|
||||||
enabled_h265 = enabled_h265 && state.1.H265;
|
|
||||||
score_vpx += state.1.ScoreVpx;
|
|
||||||
score_h264 += state.1.ScoreH264;
|
|
||||||
score_h265 += state.1.ScoreH265;
|
|
||||||
}
|
|
||||||
|
|
||||||
let current_encoder_name = HwEncoder::current_name();
|
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 {
|
|
||||||
*current_encoder_name.lock().unwrap() = Some(encoder_h264.unwrap().name);
|
|
||||||
} else {
|
|
||||||
*current_encoder_name.lock().unwrap() = None;
|
|
||||||
}
|
|
||||||
if states.len() > 0 {
|
if states.len() > 0 {
|
||||||
|
let (encoder_h264, encoder_h265) = HwEncoder::best();
|
||||||
|
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);
|
||||||
|
|
||||||
|
for state in states.iter() {
|
||||||
|
enabled_h264 = enabled_h264 && state.1.H264;
|
||||||
|
enabled_h265 = enabled_h265 && state.1.H265;
|
||||||
|
score_vpx += state.1.ScoreVpx;
|
||||||
|
score_h264 += state.1.ScoreH264;
|
||||||
|
score_h265 += state.1.ScoreH265;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
*current_encoder_name.lock().unwrap() = Some(encoder_h264.unwrap().name);
|
||||||
|
} else {
|
||||||
|
*current_encoder_name.lock().unwrap() = None;
|
||||||
|
}
|
||||||
log::info!(
|
log::info!(
|
||||||
"connection count:{}, h264:{}, h265:{}, score: vpx({}), h264({}), h265({}), set current encoder name {:?}",
|
"connection count:{}, h264:{}, h265:{}, score: vpx({}), h264({}), h265({}), set current encoder name {:?}",
|
||||||
states.len(),
|
states.len(),
|
||||||
@ -153,6 +157,8 @@ impl Encoder {
|
|||||||
score_h265,
|
score_h265,
|
||||||
current_encoder_name.lock().unwrap()
|
current_encoder_name.lock().unwrap()
|
||||||
)
|
)
|
||||||
|
} else {
|
||||||
|
*current_encoder_name.lock().unwrap() = None;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
#[cfg(not(feature = "hwcodec"))]
|
#[cfg(not(feature = "hwcodec"))]
|
||||||
|
@ -781,6 +781,22 @@ impl Connection {
|
|||||||
if let Some(message::Union::login_request(lr)) = msg.union {
|
if let Some(message::Union::login_request(lr)) = msg.union {
|
||||||
if let Some(o) = lr.option.as_ref() {
|
if let Some(o) = lr.option.as_ref() {
|
||||||
self.update_option(o).await;
|
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;
|
self.video_ack_required = lr.video_ack_required;
|
||||||
if self.authorized {
|
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) {
|
fn on_close(&mut self, reason: &str, lock: bool) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user