Streams: an automatic bitrate up to 60 Mbit/s, and presets to match

The bitrate was a fixed 8 Mbit/s by default whatever was shared, and the
share dialog offered at most 10 Mbit/s and 60 fps. Asked for: Voelin
streams that can target 60 Mbit/s at up to 8K and 320 fps, with an
automatic bitrate that scales with the stream's resolution and frame rate
and follows it live, beyond 60 Mbit/s only when typed.

- `media::auto_bitrate(width, height, fps)`: 0.12 bits per pixel and
  frame (7.5 Mbit/s at 1080p30, 15 at 1080p60, 27 at 1440p60) between
  300 kbit/s and 60 Mbit/s, reached at 4K60 and 1080p240.
- `stream.bitrate_kbps` 0 is automatic and now the default. A streamer
  layer with bitrate 0 works it out per picture from the size encoded and
  the layer's frame rate, so a new resolution or frame rate moves it at
  once, and tells its sink (`MediaSink::auto_bitrate`). The engine's
  `StreamSink` puts it into the live stream's layer, so connections start
  their bandwidth estimate at it and probe for it; without that the
  stream would have kept the bitrate announced at the start.
  `Streamer::bitrate_kbps()` is what to announce.
- The quick share offers 15-320 fps and Auto, 2.5 to 60 Mbit/s (typed
  values still have no maximum; the `ui` blob keeps indices of the old
  lists for older versions). The studio offers 4K and 8K and 30-240 fps,
  takes an empty bitrate as automatic and derives its first simulcast
  layer from the automatic bitrate.
- The studio now tells the engine its layers when it goes live and when
  they change (`SetStreamLayers`), and a quick share resets them: the
  session sends only layers it knows of, so a studio's simulcast layers
  never reached a viewer, and its layer kept the bitrate announced.
- `voelinctl stream start` and `stream bench` default to the automatic
  bitrate too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
selikhovandClaude Opus 5.5 committed 2026-10-04 03:37:08 +03:00
1 parent 297dc2fe5b
commit ccc974fe05
13 files changed
+375 -85

No files matched your search

+3 -2
View File
@@ -92,8 +92,9 @@ struct BenchArgs {
/// k/M suffixes); repeatable. Without it: one layer at --bitrate.
#[arg(long = "layer")]
layers: Vec<String>,
/// Bitrate in kbit/s without --layer.
#[arg(long, default_value_t = 4608)]
/// Bitrate in kbit/s without --layer; 0: automatic, from the size and
/// frame rate (up to 60 Mbit/s).
#[arg(long, default_value_t = 0)]
bitrate: u32,
/// Measured time in seconds (after the warm-up).
#[arg(long, default_value_t = 10)]
+28 -8
View File
@@ -17,7 +17,7 @@ use voelin_core::media::voelin_media::capture::SourceId;
use voelin_core::media::voelin_media::{Codec, Codecs, VideoFrame, convert};
use voelin_core::media::{
CaptureBackend, EncodedSource, EncoderPreference, Latest, Streamer, StreamerConfig,
VideoPipeline, peer_config, preferred_codec, stream_codec, video_codec,
VideoPipeline, auto_bitrate, peer_config, preferred_codec, stream_codec, video_codec,
};
use voelin_model::ServerFlavor;
use voelin_stream::{
@@ -75,8 +75,10 @@ pub enum StreamCommand {
/// Video frames per second.
#[arg(long, default_value_t = 30)]
fps: u32,
/// Video bitrate in kbit/s (announced in `setupstream` too).
#[arg(long, default_value_t = 4608)]
/// Video bitrate in kbit/s (announced in `setupstream` too, up to the
/// 65535 a server keeps); 0: automatic, from the size and frame rate
/// (up to 60 Mbit/s).
#[arg(long, default_value_t = 0)]
bitrate: u32,
/// No audio track.
#[arg(long)]
@@ -194,7 +196,7 @@ pub async fn run(con: &mut Connection, args: &StreamArgs) -> Result<()> {
}
if *synthetic {
let (w, h) = parse_size(size)?;
let bitrate = u64::from(*bitrate) * 1000;
let bitrate = u64::from(pattern_kbps(size, *fps, *bitrate)?) * 1000;
config.set_h264_format(H264Profile::ConstrainedHigh, w, h, *fps, bitrate);
}
}
@@ -241,10 +243,14 @@ pub async fn run(con: &mut Connection, args: &StreamArgs) -> Result<()> {
l.rid.as_ref().map(|r| format!(", rid {r}")).unwrap_or_default()
);
}
// The test pattern's (and the placeholder's) automatic bitrate is
// known now; a capture's once it captures.
let wanted = *bitrate;
let mut bitrate = pattern_kbps(size, *fps, wanted)?;
let (source, audio) = if *placeholder {
let source = SyntheticSource::with_layers(
*fps,
(u64::from(*bitrate) * 1000 / 8 / u64::from((*fps).max(1))) as usize,
(u64::from(bitrate) * 1000 / 8 / u64::from((*fps).max(1))) as usize,
!no_audio,
layers,
);
@@ -252,12 +258,13 @@ pub async fn run(con: &mut Connection, args: &StreamArgs) -> Result<()> {
} else {
let source = if *synthetic { "synthetic" } else { source.as_str() };
let (source, backend) = parse_source(source)?;
let config_source = source.clone();
let Some(codec) = codec else { bail!("no video encoder in this build") };
let config = StreamerConfig {
source,
backend,
fps: *fps,
bitrate_kbps: *bitrate,
bitrate_kbps: wanted,
codec,
encoder: codecs.preference().clone(),
audio: !no_audio,
@@ -266,6 +273,9 @@ pub async fn run(con: &mut Connection, args: &StreamArgs) -> Result<()> {
..StreamerConfig::default()
};
let streamer = Streamer::start(&codecs, config).await.context("capture")?;
if wanted == 0 && !matches!(config_source, SourceId::Synthetic) {
bitrate = streamer.bitrate_kbps();
}
let backend = streamer.stats().layers.first().and_then(|l| l.backend);
let backend = backend.map_or("?".to_owned(), |b| b.to_string());
println!("capturing with {} ({codec} through {backend})", streamer.backend());
@@ -275,8 +285,8 @@ pub async fn run(con: &mut Connection, args: &StreamArgs) -> Result<()> {
let audio = streamer.has_audio();
(Source::Encoded(EncodedSource::new(streamer)), audio)
};
let setup =
StreamSetup { name: name.clone(), bitrate: *bitrate, audio, ..Default::default() };
println!("bitrate: {}", rate(u64::from(bitrate) * 1000));
let setup = StreamSetup { name: name.clone(), bitrate, audio, ..Default::default() };
let options = StreamerOptions {
setup,
auto_accept: *auto_accept,
@@ -321,6 +331,16 @@ fn parse_source(spec: &str) -> Result<(SourceId, CaptureBackend)> {
})
}
/// `wanted` kbit/s, or for 0 (automatic) the automatic bitrate of a test
/// pattern of `size` at `fps`.
fn pattern_kbps(size: &str, fps: u32, wanted: u32) -> Result<u32> {
if wanted > 0 {
return Ok(wanted);
}
let (w, h) = parse_size(size)?;
Ok(u32::try_from(auto_bitrate(w, h, fps) / 1000).unwrap_or(u32::MAX))
}
/// `<width>x<height>`.
fn parse_size(size: &str) -> Result<(u32, u32)> {
let parsed = size.split_once('x').and_then(|(w, h)| Some((w.parse().ok()?, h.parse().ok()?)));