mirror of
https://github.com/mofeng-git/One-KVM.git
synced 2026-01-29 00:51:53 +08:00
perf(video): 优化视频管道性能降低延迟
- 减少 broadcast channel 缓冲区大小以降低延迟 - 将 Mutex 字段改为原子类型减少锁竞争 - 使用本地计数器批量更新统计信息 - 修复 WebRTC 帧持续时间计算基于实际 FPS - 不再自动缩放码率,保留用户配置值
This commit is contained in:
@@ -421,9 +421,11 @@ impl UniversalVideoTrack {
|
||||
/// Write VP8 frame
|
||||
async fn write_vp8_frame(&self, data: &[u8], is_keyframe: bool) -> Result<()> {
|
||||
// VP8 frames are sent directly without NAL parsing
|
||||
// Calculate frame duration based on configured FPS
|
||||
let frame_duration = Duration::from_micros(1_000_000 / self.config.fps.max(1) as u64);
|
||||
let sample = Sample {
|
||||
data: Bytes::copy_from_slice(data),
|
||||
duration: Duration::from_secs(1),
|
||||
duration: frame_duration,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
@@ -452,9 +454,11 @@ impl UniversalVideoTrack {
|
||||
/// Write VP9 frame
|
||||
async fn write_vp9_frame(&self, data: &[u8], is_keyframe: bool) -> Result<()> {
|
||||
// VP9 frames are sent directly without NAL parsing
|
||||
// Calculate frame duration based on configured FPS
|
||||
let frame_duration = Duration::from_micros(1_000_000 / self.config.fps.max(1) as u64);
|
||||
let sample = Sample {
|
||||
data: Bytes::copy_from_slice(data),
|
||||
duration: Duration::from_secs(1),
|
||||
duration: frame_duration,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
@@ -483,13 +487,15 @@ impl UniversalVideoTrack {
|
||||
/// Send NAL units as samples (H264 only)
|
||||
async fn send_nals(&self, nals: Vec<Bytes>, is_keyframe: bool) -> Result<()> {
|
||||
let mut total_bytes = 0u64;
|
||||
// Calculate frame duration based on configured FPS
|
||||
let frame_duration = Duration::from_micros(1_000_000 / self.config.fps.max(1) as u64);
|
||||
|
||||
match &self.track {
|
||||
TrackType::Sample(track) => {
|
||||
for nal_data in nals {
|
||||
let sample = Sample {
|
||||
data: nal_data.clone(),
|
||||
duration: Duration::from_secs(1),
|
||||
duration: frame_duration,
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
|
||||
@@ -533,16 +533,12 @@ impl WebRtcStreamer {
|
||||
info!("Closed {} existing sessions due to config change", session_count);
|
||||
}
|
||||
|
||||
// Update config
|
||||
// Update config (preserve user-configured bitrate)
|
||||
let mut config = self.config.write().await;
|
||||
config.resolution = resolution;
|
||||
config.input_format = format;
|
||||
config.fps = fps;
|
||||
|
||||
// Scale bitrate based on resolution
|
||||
let base_pixels: u64 = 1280 * 720;
|
||||
let actual_pixels: u64 = resolution.width as u64 * resolution.height as u64;
|
||||
config.bitrate_kbps = ((8000u64 * actual_pixels / base_pixels).max(1000).min(15000)) as u32;
|
||||
// Note: bitrate is NOT auto-scaled here - use set_bitrate() or config to change it
|
||||
|
||||
info!(
|
||||
"WebRTC config updated: {}x{} {:?} @ {} fps, {} kbps",
|
||||
|
||||
Reference in New Issue
Block a user