Streamer: threaded simulcast pipeline, per-stage stats, reconfigure
The streamer converted and encoded on one thread, one frame after the
other, with the frame rate clamped to 1..=60 and one encoder. It is now
a pipeline of threads connected by latest-wins handoffs:
capture thread (backend) ── convert + scale (pool of all cores)
── Handoff per layer ── encoder thread per layer ── MediaSink
- The capture backend calls the pipeline's `FrameSink` with the frame
still in its capture buffer. Frames beyond the frame-rate cap are
skipped before anything is read. The `Pyramid` converts the frame once
and derives every due layer's size (`LayerSpec::output_size`) from
the nearest larger one, in recycled frames.
- Each layer (`StreamerConfig::layers`; empty: one layer at
`bitrate_kbps`) has its own encoder thread and encoder, its share of
the CPUs, its own frame-rate cap (`max_fps`, paced by timestamps),
and gets the newest frame through a one-slot handoff; frames it was
too busy for are dropped and counted, never queued.
- Keyframes: `take_layer_keyframes` is polled by whichever encoder
thread gets to it and each requested layer is flagged, so exactly the
requested layers encode a keyframe (kept due until one is produced).
A still screen sends no frames, so a request is answered by encoding
the last picture again.
- Bitrate: every frame, each layer follows `layer_bitrate(id)` when the
sink knows it (capped by `max_bitrate`), else its configured bitrate;
libvpx changes it in place, without a keyframe.
- `Streamer::reconfigure(&codecs, StreamerConfigUpdate { fps,
bitrate_kbps, codec, layers })` applies from the next frame without
restarting the capture: new encoders are created first (errors change
nothing), layers keep their encoder when their id stays, new ids get
threads, removed ones stop, a codec change swaps every encoder (first
frame a keyframe), the frame-rate cap reaches the capture backend.
- No built-in limits: any fps >= 1, any bitrate, any size; threads from
the CPU count.
- `StreamerStats` gains capture fps, convert time, convert threads,
dropped frames, codec and per-layer `LayerStats` (size, frames,
keyframes, dropped, fps, kbit/s, encode time, target bitrate, threads,
encoder speed); counters are atomics, rates are computed once a second
by a stats thread, which logs a one-line summary every 5 s at debug
level. `StreamerStats` is no longer `Eq` (it has rates).
- `EncodedSource` answers `request_layer_keyframe`; the local preview
shows the first layer.
- In steady state nothing is allocated per frame between capture and
encoder; the one allocation per encoded frame is the `Arc<[u8]>` of
`EncodedFrame` (encoded sizes vary, so it cannot be recycled).
`voelinctl stream bench` prints the new per-stage and per-layer numbers
and allocations per captured and per encoded frame.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NJvYyRmTjKcb9i8eyYXK5G
This commit is contained in:
1 parent
544373e0ff
commit
40ad426f26
2 files changed
+989
-108
No files matched your search
+962
-105
File diff suppressed because it is too large.
Load diff
@@ -192,9 +192,14 @@ struct Sample {
|
||||
cpu: Option<Duration>,
|
||||
layers: Vec<(LayerId, LayerSnapshot)>,
|
||||
sent: u64,
|
||||
captured: u64,
|
||||
/// Frames the handoffs to the encoders dropped, per layer.
|
||||
dropped: Vec<(LayerId, u64)>,
|
||||
}
|
||||
|
||||
fn sample(sink: &BenchSink, streamer: &Streamer) -> Sample {
|
||||
// Stats first: reading them allocates, which must not count.
|
||||
let stats = streamer.stats();
|
||||
let (allocations, bytes) = alloc::counts();
|
||||
Sample {
|
||||
at: Instant::now(),
|
||||
@@ -202,7 +207,9 @@ fn sample(sink: &BenchSink, streamer: &Streamer) -> Sample {
|
||||
bytes,
|
||||
cpu: cpu_time(),
|
||||
layers: sink.snapshot(),
|
||||
sent: streamer.stats().video_frames,
|
||||
sent: stats.video_frames,
|
||||
captured: stats.captured_frames,
|
||||
dropped: stats.layers.iter().map(|l| (l.id, l.dropped)).collect(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -262,19 +269,34 @@ fn bench(args: BenchArgs) -> Result<()> {
|
||||
drop(streamer);
|
||||
|
||||
let secs = (end.at - start.at).as_secs_f64();
|
||||
let captured = end.captured - start.captured;
|
||||
let ms = |d: Duration| d.as_secs_f64() * 1000.0;
|
||||
println!(
|
||||
"capture: {:.1} fps; convert + scale: {:.2} ms per frame on {} threads",
|
||||
captured as f64 / secs,
|
||||
ms(stats.convert_time),
|
||||
stats.convert_threads,
|
||||
);
|
||||
let mut output_frames = 0;
|
||||
for ((id, a), (_, b)) in start.layers.iter().zip(&end.layers) {
|
||||
let frames = b.frames - a.frames;
|
||||
output_frames += frames;
|
||||
let spec = layers.iter().find(|l| l.id == *id);
|
||||
let size = spec.map_or((width, height), |l| l.output_size(width, height));
|
||||
let dropped = |s: &Sample| s.dropped.iter().find(|d| d.0 == *id).map_or(0, |d| d.1);
|
||||
let layer = stats.layers.iter().find(|l| l.id == *id);
|
||||
println!(
|
||||
"layer {id}: {}x{} {:.1} fps {:.0} kbit/s {} keyframes",
|
||||
"layer {id}: {}x{} {:.1} fps {:.0} kbit/s {} keyframes {} dropped encode {:.2} ms \
|
||||
({} threads, speed {})",
|
||||
size.0,
|
||||
size.1,
|
||||
frames as f64 / secs,
|
||||
(b.bytes - a.bytes) as f64 * 8.0 / secs / 1000.0,
|
||||
b.keyframes - a.keyframes,
|
||||
dropped(&end) - dropped(&start),
|
||||
layer.map_or(0.0, |l| ms(l.encode_time)),
|
||||
layer.map_or(0, |l| l.threads),
|
||||
layer.and_then(|l| l.speed).map_or("-".into(), |s| s.to_string()),
|
||||
);
|
||||
}
|
||||
if let Some(e) = &stats.error {
|
||||
@@ -287,7 +309,9 @@ fn bench(args: BenchArgs) -> Result<()> {
|
||||
let bytes = end.bytes - start.bytes;
|
||||
let sent = (end.sent - start.sent).max(1);
|
||||
println!(
|
||||
"heap: {allocations} allocations ({:.1} per sent frame, {:.0} bytes per sent frame)",
|
||||
"heap: {allocations} allocations: {:.2} per captured frame, {:.2} per encoded frame \
|
||||
({:.0} bytes per encoded frame)",
|
||||
allocations as f64 / captured.max(1) as f64,
|
||||
allocations as f64 / sent as f64,
|
||||
bytes as f64 / sent as f64,
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user