mirror of
https://github.com/mofeng-git/One-KVM.git
synced 2026-02-01 10:31:54 +08:00
fix: mpp 性能优化和修复
- mjpeg-->h265 mpp 编码速度优化 - 修复 mpp 编码后的视频 rustdesk 无法解码问题 - 更新版本号为 v0.1.2
This commit is contained in:
@@ -623,7 +623,7 @@ impl Connection {
|
||||
self.negotiated_codec = Some(negotiated);
|
||||
info!("Negotiated video codec: {:?}", negotiated);
|
||||
|
||||
let response = self.create_login_response(true);
|
||||
let response = self.create_login_response(true).await;
|
||||
let response_bytes = response
|
||||
.write_to_bytes()
|
||||
.map_err(|e| anyhow::anyhow!("Failed to encode: {}", e))?;
|
||||
@@ -673,7 +673,11 @@ impl Connection {
|
||||
Some(misc::Union::RefreshVideo(refresh)) => {
|
||||
if *refresh {
|
||||
debug!("Video refresh requested");
|
||||
// TODO: Request keyframe from encoder
|
||||
if let Some(ref video_manager) = self.video_manager {
|
||||
if let Err(e) = video_manager.request_keyframe().await {
|
||||
warn!("Failed to request keyframe: {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Some(misc::Union::VideoReceived(received)) => {
|
||||
@@ -1064,7 +1068,7 @@ impl Connection {
|
||||
}
|
||||
|
||||
/// Create login response with dynamically detected encoder capabilities
|
||||
fn create_login_response(&self, success: bool) -> HbbMessage {
|
||||
async fn create_login_response(&self, success: bool) -> HbbMessage {
|
||||
if success {
|
||||
// Dynamically detect available encoders
|
||||
let registry = EncoderRegistry::global();
|
||||
@@ -1080,11 +1084,21 @@ impl Connection {
|
||||
h264_available, h265_available, vp8_available, vp9_available
|
||||
);
|
||||
|
||||
let mut display_width = self.screen_width;
|
||||
let mut display_height = self.screen_height;
|
||||
if let Some(ref video_manager) = self.video_manager {
|
||||
let video_info = video_manager.get_video_info().await;
|
||||
if let Some((width, height)) = video_info.resolution {
|
||||
display_width = width;
|
||||
display_height = height;
|
||||
}
|
||||
}
|
||||
|
||||
let mut display_info = DisplayInfo::new();
|
||||
display_info.x = 0;
|
||||
display_info.y = 0;
|
||||
display_info.width = 1920;
|
||||
display_info.height = 1080;
|
||||
display_info.width = display_width as i32;
|
||||
display_info.height = display_height as i32;
|
||||
display_info.name = "KVM Display".to_string();
|
||||
display_info.online = true;
|
||||
display_info.cursor_embedded = false;
|
||||
@@ -1582,6 +1596,9 @@ async fn run_video_streaming(
|
||||
config.bitrate_preset
|
||||
);
|
||||
}
|
||||
if let Err(e) = video_manager.request_keyframe().await {
|
||||
debug!("Failed to request keyframe for connection {}: {}", conn_id, e);
|
||||
}
|
||||
|
||||
// Inner loop: receives frames from current subscription
|
||||
loop {
|
||||
|
||||
@@ -42,6 +42,9 @@ pub struct VideoFrameAdapter {
|
||||
seq: u32,
|
||||
/// Timestamp offset
|
||||
timestamp_base: u64,
|
||||
/// Cached H264 SPS/PPS (Annex B NAL without start code)
|
||||
h264_sps: Option<Bytes>,
|
||||
h264_pps: Option<Bytes>,
|
||||
}
|
||||
|
||||
impl VideoFrameAdapter {
|
||||
@@ -51,6 +54,8 @@ impl VideoFrameAdapter {
|
||||
codec,
|
||||
seq: 0,
|
||||
timestamp_base: 0,
|
||||
h264_sps: None,
|
||||
h264_pps: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,6 +73,7 @@ impl VideoFrameAdapter {
|
||||
is_keyframe: bool,
|
||||
timestamp_ms: u64,
|
||||
) -> Message {
|
||||
let data = self.prepare_h264_frame(data, is_keyframe);
|
||||
// Calculate relative timestamp
|
||||
if self.seq == 0 {
|
||||
self.timestamp_base = timestamp_ms;
|
||||
@@ -100,6 +106,41 @@ impl VideoFrameAdapter {
|
||||
msg
|
||||
}
|
||||
|
||||
fn prepare_h264_frame(&mut self, data: Bytes, is_keyframe: bool) -> Bytes {
|
||||
if self.codec != VideoCodec::H264 {
|
||||
return data;
|
||||
}
|
||||
|
||||
// Parse SPS/PPS from Annex B data (without start codes)
|
||||
let (sps, pps) = crate::webrtc::rtp::extract_sps_pps(&data);
|
||||
let mut has_sps = false;
|
||||
let mut has_pps = false;
|
||||
|
||||
if let Some(sps) = sps {
|
||||
self.h264_sps = Some(Bytes::from(sps));
|
||||
has_sps = true;
|
||||
}
|
||||
if let Some(pps) = pps {
|
||||
self.h264_pps = Some(Bytes::from(pps));
|
||||
has_pps = true;
|
||||
}
|
||||
|
||||
// Inject cached SPS/PPS before IDR when missing
|
||||
if is_keyframe && (!has_sps || !has_pps) {
|
||||
if let (Some(ref sps), Some(ref pps)) = (self.h264_sps.as_ref(), self.h264_pps.as_ref()) {
|
||||
let mut out = Vec::with_capacity(8 + sps.len() + pps.len() + data.len());
|
||||
out.extend_from_slice(&[0, 0, 0, 1]);
|
||||
out.extend_from_slice(sps);
|
||||
out.extend_from_slice(&[0, 0, 0, 1]);
|
||||
out.extend_from_slice(pps);
|
||||
out.extend_from_slice(&data);
|
||||
return Bytes::from(out);
|
||||
}
|
||||
}
|
||||
|
||||
data
|
||||
}
|
||||
|
||||
/// Convert encoded video data to RustDesk Message
|
||||
pub fn encode_frame(&mut self, data: &[u8], is_keyframe: bool, timestamp_ms: u64) -> Message {
|
||||
self.encode_frame_from_bytes(Bytes::copy_from_slice(data), is_keyframe, timestamp_ms)
|
||||
|
||||
Reference in New Issue
Block a user