feat(portal): async encode pipeline - decouple capture from encoding

Split synchronous encode pipeline so sws_scale + libx264 runs on a
dedicated thread, leaving only VAAPI import + GPU scale + GPU→CPU
transfer on the main capture thread.

Problem: encode_p95 occasionally hit 74ms, blocking the entire capture
pipeline and causing capture_gap_max=356ms stutter.

Solution:
- avhw.rs: Split SwEncState into SwEncImport (main thread: VAAPI import,
  filter_graph scale, GPU→CPU transfer) and SwEncEncode (encode thread:
  sws_scale NV12→YUV420P, libx264 encode). New CpuNv12Frame struct
  carries owned pixel data across threads via crossbeam channel.
  SwEncState wraps both for backward compat (MP4/sync path untouched).
- state_portal.rs: WebRTC portal path spawns 'wl-webrtc-encode' thread
  with bounded(2) input channel (drop-newest backpressure) and separate
  timing channel. Graceful shutdown: drop webrtc_rx → drop input_tx →
  join encode thread → flush sync encoder.
- stats.rs: Add record_import() + record_encode_thread() for async timing.

Results: encode_p95 stable at 2.9-4.2ms (was 11-74ms), capture_fps
stable 59-60fps, cap_gap_p95 17-19ms. Remaining capture stalls traced
to PipeWire compositor frame delivery (external, not our code).
This commit is contained in:
dailz
2026-06-07 16:55:28 +08:00
parent aae030f309
commit 826f544569
8 changed files with 561 additions and 236 deletions
+195 -64
View File
@@ -3,13 +3,14 @@ use std::os::fd::AsRawFd;
use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::{Duration, Instant};
use std::time::Instant;
use anyhow::{bail, Result}; // 错误处理工具
use crate::args::Args; // 命令行参数
use crate::avhw::{self, SwEncState}; // 软件编码器状态(VAAPI 导入 + H.264 编码)
use crate::avhw::{self, CpuNv12Frame, SwEncEncode, SwEncImport, SwEncState}; // 软件编码器状态(VAAPI 导入 + H.264 编码)
use crate::cap_portal::{CapPortal, PwCtrlEvent, PwDmaBufFrame}; // PipeWire 屏幕采集端点
use crate::stats::{FrameTimings, PipelineStats}; // 管道统计(帧计时、每秒快照)
use crate::webrtc::WebRtcState; // WebRTC 信令与媒体传输
/// 门户采集的阶段状态
@@ -20,6 +21,18 @@ enum PortalStage {
Streaming,
}
struct EncodeThreadTiming {
sws_us: u64,
encode_us: u64,
output_bytes: usize,
}
struct EncodeThread {
handle: Option<std::thread::JoinHandle<()>>,
input_tx: crossbeam_channel::Sender<CpuNv12Frame>,
timing_rx: crossbeam_channel::Receiver<EncodeThreadTiming>,
}
/// 门户模式的主状态机
///
/// 负责管理从 PipeWire 采集屏幕帧、通过 VAAPI 硬件编码的完整生命周期。
@@ -27,16 +40,17 @@ enum PortalStage {
pub struct StatePortal {
stage: PortalStage, // 当前采集阶段(等待首帧 / 流式编码中)
enc: Option<SwEncState>, // 软件编码器,首帧到达后初始化
enc_import: Option<SwEncImport>,
enc_thread: Option<EncodeThread>,
cap: CapPortal, // PipeWire 屏幕采集端点
args: Args, // 用户命令行参数
errored: bool, // 是否遇到不可恢复的错误
drm_device: Option<PathBuf>, // DRM 渲染设备路径(可自动检测)
frames_encoded: u64, // 已编码帧数
frames_encoded: u64, // 已编码帧数(用于 PTS 编号)
start_time: Option<Instant>, // 编码开始时间
last_stats_time: Option<Instant>, // 上一次统计日志时间
last_stats_frames: u64, // 上一次统计时的已编码帧数
stats: PipelineStats, // 管道统计(窗口化帧计时 + 每秒快照)
pw_dropped_prev: u64, // 上一窗口的 PipeWire 丢弃帧数(用于增量计算)
webrtc: Option<WebRtcState>, // WebRTC 状态(仅 WebRTC 模式启用)
webrtc_tx: Option<crossbeam_channel::Sender<Vec<u8>>>, // 编码帧发送通道
webrtc_rx: Option<crossbeam_channel::Receiver<Vec<u8>>>,
webrtc_frames_sent: u64,
webrtc_paused: Option<Arc<AtomicBool>>,
@@ -56,29 +70,29 @@ impl StatePortal {
let cap = CapPortal::new(&args)?;
let (webrtc, webrtc_tx, webrtc_rx, webrtc_paused) = if args.port > 0 {
let (tx, rx) = crossbeam_channel::bounded(32);
let (webrtc, webrtc_paused) = if args.port > 0 {
let wrtc = WebRtcState::new(args.port, args.fps)?;
let paused = Arc::new(AtomicBool::new(true));
(Some(wrtc), Some(tx), Some(rx), Some(paused))
(Some(wrtc), Some(paused))
} else {
(None, None, None, None)
(None, None)
};
Ok(Self {
stage: PortalStage::WaitingForFormat,
enc: None,
enc_import: None,
enc_thread: None,
cap,
args,
errored: false,
drm_device,
frames_encoded: 0,
start_time: None,
last_stats_time: None,
last_stats_frames: 0,
stats: PipelineStats::new(),
pw_dropped_prev: 0,
webrtc,
webrtc_tx,
webrtc_rx,
webrtc_rx: None,
webrtc_frames_sent: 0,
webrtc_paused,
})
@@ -155,7 +169,7 @@ impl StatePortal {
});
// GOP 大小:WebRTC 模式使用更小的 GOPfps/2,最低10),MP4 模式使用 fps
let actual_gop_size = self.args.gop_size.unwrap_or_else(|| {
if self.webrtc_tx.is_some() {
if self.webrtc.is_some() {
(self.args.fps / 2).max(10)
} else {
self.args.fps
@@ -163,26 +177,40 @@ impl StatePortal {
});
// 根据是否启用 WebRTC 选择不同的编码器构造方式
let enc = if let Some(ref tx) = self.webrtc_tx {
if self.webrtc.is_some() {
let paused = self.webrtc_paused.as_ref()
.ok_or_else(|| anyhow::anyhow!("internal invariant broken: webrtc_paused missing while WebRTC mode is active"))?;
avhw::SwEncState::new_webrtc(
let import = SwEncImport::new(
&drm_path,
frame.width,
frame.height,
enc_width,
enc_height,
self.args.fps,
)?;
let (webrtc_tx, webrtc_rx) = crossbeam_channel::bounded(32);
let (input_tx, input_rx) = crossbeam_channel::bounded::<CpuNv12Frame>(2);
let (timing_tx, timing_rx) = crossbeam_channel::bounded::<EncodeThreadTiming>(32);
let encode = SwEncEncode::new_webrtc(
enc_width,
enc_height,
self.args.fps,
actual_bitrate,
actual_gop_size,
tx.clone(),
webrtc_tx,
paused.clone(),
)?
)?;
let handle = std::thread::Builder::new()
.name("wl-webrtc-encode".into())
.spawn(move || encode_thread_loop(encode, input_rx, timing_tx))?;
self.enc_import = Some(import);
self.enc_thread = Some(EncodeThread { handle: Some(handle), input_tx, timing_rx });
self.webrtc_rx = Some(webrtc_rx);
} else {
// MP4 模式:编码输出写入文件
let output_path = self.args.output.as_deref()
.ok_or_else(|| anyhow::anyhow!("--output is required in MP4 file output mode; use --port > 0 for WebRTC mode"))?;
avhw::SwEncState::new(
let enc = avhw::SwEncState::new(
&drm_path,
std::path::Path::new(output_path),
frame.width,
@@ -192,17 +220,17 @@ impl StatePortal {
self.args.fps,
actual_bitrate,
actual_gop_size,
)?
)?;
self.enc = Some(enc);
};
self.enc = Some(enc);
self.stage = PortalStage::Streaming; // 切换到流式编码阶段
self.start_time = Some(Instant::now());
self.last_stats_time = Some(Instant::now());
tracing::info!("First frame processed, encoder initialized, transitioning to Streaming");
drop(frame); // 首帧仅用于初始化,不参与编码
}
PortalStage::Streaming => {
// 记录采集帧到达(用于 capture gap 和 capture_fps 统计)
self.stats.record_capture();
// 流式编码阶段:直接处理帧
self.handle_pw_frame(frame)?;
}
@@ -212,6 +240,16 @@ impl StatePortal {
// WebRTC: drain encoded frames produced by this poll before returning.
self.poll_webrtc()?;
// 每秒输出一次结构化管道统计(仅 --stats 启用时记录日志)
if self.args.stats && self.stats.should_snapshot() {
// PipeWire 丢弃帧数:CapPortal 尚未暴露 dropped_count(),暂用占位
self.stats.set_pipewire_dropped(0, 0);
let enc_q = self.webrtc_rx.as_ref().map(|r| r.len()).unwrap_or(0);
self.stats.set_queue_depths(0, enc_q);
let snap = self.stats.snapshot_and_reset();
tracing::info!("stats: {snap}");
}
Ok(true)
}
@@ -271,49 +309,84 @@ impl StatePortal {
/// 通过 `av_hwframe_map` 零拷贝导入 VAAPI,然后交给 SwEncState 完成:
/// scale_vaapi GPU 缩放、2K NV12 回读、YUV420P 格式转换、软件 H.264 编码。
fn handle_pw_frame(&mut self, frame: PwDmaBufFrame) -> Result<()> {
// 获取已初始化的编码器引用
let enc = match self.enc.as_mut() {
Some(enc) => enc,
None => bail!("encoder not initialized"),
};
// 将 DMA-BUF 帧零拷贝导入 VAAPI 硬件帧池
let mut vaapi_frame = unsafe {
avhw::import_dma_buf_to_vaapi(
enc.frames_rgb().as_ptr(),
frame.fd.as_raw_fd(),
frame.width,
frame.height,
frame.format,
frame.modifier,
frame.stride,
frame.offset,
)
}?;
// 设置帧的显示时间戳(PTS),基于已编码帧序号
let t_import_start = Instant::now();
let pts = self.frames_encoded as i64;
unsafe {
(*vaapi_frame.as_mut_ptr()).pts = pts;
}
// 送入编码器完成:缩放 → 回读 → 格式转换 → H.264 编码
enc.encode_frame(&vaapi_frame)?;
self.frames_encoded += 1;
if let Some(enc) = self.enc.as_mut() {
// 将 DMA-BUF 帧零拷贝导入 VAAPI 硬件帧池
let mut vaapi_frame = unsafe {
avhw::import_dma_buf_to_vaapi(
enc.frames_rgb().as_ptr(),
frame.fd.as_raw_fd(),
frame.width,
frame.height,
frame.format,
frame.modifier,
frame.stride,
frame.offset,
)
}?;
// 每 10 秒输出一次编码统计(已编码帧数、实时帧率)
if let Some(last) = self.last_stats_time {
if last.elapsed() >= Duration::from_secs(10) {
let delta_frames = self.frames_encoded - self.last_stats_frames;
let delta_secs = last.elapsed().as_secs_f64();
let fps = delta_frames as f64 / delta_secs;
tracing::info!(
"encoded={}, fps={fps:.1}",
self.frames_encoded,
);
self.last_stats_time = Some(Instant::now());
self.last_stats_frames = self.frames_encoded;
let import_us = t_import_start.elapsed().as_micros() as u64;
let t_encode_start = Instant::now();
// 设置帧的显示时间戳(PTS),基于已编码帧序号
unsafe {
(*vaapi_frame.as_mut_ptr()).pts = pts;
}
// 送入编码器完成:缩放 → 回读 → 格式转换 → H.264 编码
enc.encode_frame(&vaapi_frame)?;
let total_us = t_import_start.elapsed().as_micros() as u64;
let encode_us = t_encode_start.elapsed().as_micros() as u64;
self.frames_encoded += 1;
// 记录帧计时到管道统计(import + encode 内部各阶段暂不可分离,用 total 覆盖)
let timings = FrameTimings {
import_us,
encode_us,
total_us,
..Default::default()
};
self.stats.record_encode(&timings);
} else if let Some(import) = self.enc_import.as_mut() {
let mut vaapi_frame = unsafe {
avhw::import_dma_buf_to_vaapi(
import.frames_rgb().as_ptr(),
frame.fd.as_raw_fd(),
frame.width,
frame.height,
frame.format,
frame.modifier,
frame.stride,
frame.offset,
)
}?;
unsafe {
(*vaapi_frame.as_mut_ptr()).pts = pts;
}
let cpu_nv12 = import.import_and_scale(&vaapi_frame)?;
let import_us = t_import_start.elapsed().as_micros() as u64;
self.stats.record_import(import_us);
let enc_thread = self.enc_thread.as_ref()
.ok_or_else(|| anyhow::anyhow!("internal invariant broken: encode thread missing while async import is active"))?;
match enc_thread.input_tx.try_send(cpu_nv12) {
Ok(()) => {
self.frames_encoded += 1;
}
Err(crossbeam_channel::TrySendError::Full(_frame)) => {
tracing::warn!("Encode thread input full, dropping portal frame");
}
Err(crossbeam_channel::TrySendError::Disconnected(_frame)) => {
tracing::error!("Encode thread input disconnected");
self.errored = true;
}
}
} else {
bail!("encoder not initialized");
}
Ok(())
@@ -326,6 +399,15 @@ impl StatePortal {
// 先 drop receiver,使 flush() 中的 try_send() 立即返回 Disconnected
// 而非在满通道上阻塞(修复 issue #8 死锁)
self.webrtc_rx = None;
if let Some(mut enc_thread) = self.enc_thread.take() {
drop(enc_thread.input_tx);
if let Some(handle) = enc_thread.handle.take() {
if handle.join().is_err() {
tracing::error!("Encode thread panicked during shutdown");
}
}
}
self.enc_import = None;
if let Some(mut enc) = self.enc.take() {
if let Err(e) = enc.flush() {
tracing::error!("Flush error during shutdown: {e}");
@@ -384,10 +466,21 @@ impl StatePortal {
if let Err(e) = wrtc.write_h264_frame(&data, self.webrtc_frames_sent, self.args.fps) {
tracing::debug!("WebRTC write frame error: {e}");
}
self.stats.record_send(0.0, None);
self.webrtc_frames_sent = self.webrtc_frames_sent.saturating_add(1);
}
if count > 0 {
tracing::info!("WebRTC forwarded {count} frames from channel");
tracing::debug!("WebRTC forwarded {count} frames from channel");
}
}
if let Some(ref enc_thread) = self.enc_thread {
while let Ok(timing) = enc_thread.timing_rx.try_recv() {
self.stats.record_encode_thread(
timing.sws_us,
timing.encode_us,
timing.output_bytes,
);
}
}
@@ -395,6 +488,42 @@ impl StatePortal {
}
}
fn encode_thread_loop(
mut encode: SwEncEncode,
input_rx: crossbeam_channel::Receiver<CpuNv12Frame>,
timing_tx: crossbeam_channel::Sender<EncodeThreadTiming>,
) {
loop {
match input_rx.recv() {
Ok(frame) => {
let t_start = Instant::now();
match encode.encode_cpu_frame(&frame) {
Ok(()) => {
let elapsed = t_start.elapsed().as_micros() as u64;
let _ = timing_tx.try_send(EncodeThreadTiming {
sws_us: 0,
encode_us: elapsed,
output_bytes: 0,
});
}
Err(e) => {
tracing::error!("Encode thread error: {e}");
break;
}
}
}
Err(_) => {
tracing::info!("Encode thread input closed, flushing encoder");
if let Err(e) = encode.flush() {
tracing::error!("Encode thread flush error: {e}");
}
break;
}
}
}
tracing::info!("Encode thread exiting");
}
impl Drop for StatePortal {
// 析构时自动调用 shutdown,确保编码器被刷新、资源被释放
fn drop(&mut self) {
@@ -510,6 +639,7 @@ mod tests {
backend: None,
port: 0,
no_persist: false,
stats: false,
};
let result = resolve_drm_device(&args).unwrap();
assert_eq!(
@@ -533,6 +663,7 @@ mod tests {
backend: None,
port: 0,
no_persist: false,
stats: false,
};
let result = resolve_drm_device(&args).unwrap();
assert_eq!(result, None);