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 4fe7263553
commit 8917f7dab2
13 files changed
+375 -85

No files matched your search

+18 -8
View File
@@ -105,10 +105,16 @@ pub fn appearance_keys() -> [&'static dyn voelin_core::settings::Setting; 5] {
[&UI_THEME, &UI_FONT_SCALE, &UI_NARROW_BREAKPOINT, &UI_IMAGE_CACHE_MB, &UI_MEMBERS_WIDTH]
}
/// Frame rates and bitrates (kbit/s) the share dialog offers; any other
/// value can be typed in.
pub const FPS_CHOICES: [u32; 3] = [15, 30, 60];
pub const BITRATE_CHOICES: [u32; 4] = [2500, 4608, 8000, 10_000];
/// Frame rates and bitrates (kbit/s; 0: automatic) the share dialog
/// offers, in the order of its lists (`dialogs.slint`); any other value can
/// be typed in.
pub const FPS_CHOICES: [u32; 7] = [15, 30, 60, 120, 144, 240, 320];
pub const BITRATE_CHOICES: [u32; 8] = [0, 2500, 4608, 8000, 10_000, 20_000, 40_000, 60_000];
/// The share dialog's lists before they grew, which [`ShareDefaults`]
/// indexes for older versions.
pub const LEGACY_FPS_CHOICES: [u32; 3] = [15, 30, 60];
pub const LEGACY_BITRATE_CHOICES: [u32; 4] = [2500, 4608, 8000, 10_000];
/// The index of the choice closest to `value`.
pub fn nearest_choice(choices: &[u32], value: u32) -> usize {
@@ -151,7 +157,8 @@ impl Default for UiSettings {
/// The last choices in the share dialog. Frame rate and bitrate are the
/// settings `stream.fps` and `stream.bitrate_kbps`; the indices of the
/// closest choices are kept for older versions.
/// closest choices of the old lists ([`LEGACY_FPS_CHOICES`],
/// [`LEGACY_BITRATE_CHOICES`]) are kept for older versions.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(default)]
pub struct ShareDefaults {
@@ -356,9 +363,12 @@ mod tests {
#[test]
fn free_values() {
assert_eq!(nearest_choice(&FPS_CHOICES, 60), 2);
assert_eq!(nearest_choice(&FPS_CHOICES, 144), 2);
assert_eq!(nearest_choice(&BITRATE_CHOICES, 3000), 0);
assert_eq!(nearest_choice(&BITRATE_CHOICES, 50_000), 3);
assert_eq!(nearest_choice(&FPS_CHOICES, 144), 4);
assert_eq!(nearest_choice(&FPS_CHOICES, 1000), 6);
assert_eq!(nearest_choice(&BITRATE_CHOICES, 0), 0, "automatic");
assert_eq!(nearest_choice(&BITRATE_CHOICES, 3000), 1);
assert_eq!(nearest_choice(&BITRATE_CHOICES, 55_000), 7);
assert_eq!(nearest_choice(&BITRATE_CHOICES, 120_000), 7);
assert_eq!(parse_positive(" 144 "), Some(144));
assert_eq!(parse_positive("0"), None);
assert_eq!(parse_positive("fast"), None);
+14 -5
View File
@@ -19,7 +19,8 @@ use crate::app::{
App, Bridge, ShareForm, SourceItem, StreamItem, ViewerItem, later, model, with_app,
};
use crate::settings::{
BITRATE_CHOICES, FPS_CHOICES, ShareDefaults, nearest_choice, parse_positive,
BITRATE_CHOICES, FPS_CHOICES, LEGACY_BITRATE_CHOICES, LEGACY_FPS_CHOICES, ShareDefaults,
nearest_choice, parse_positive,
};
use crate::video::{self, Capture, CaptureRequest, Decoder};
@@ -540,7 +541,8 @@ impl App {
fps_index: nearest_choice(&FPS_CHOICES, fps) as i32,
fps: fps.to_string().into(),
bitrate_index: nearest_choice(&BITRATE_CHOICES, bitrate) as i32,
bitrate: bitrate.to_string().into(),
// Automatic (0): the field stays empty.
bitrate: if bitrate == 0 { String::new() } else { bitrate.to_string() }.into(),
audio: defaults.audio,
auto_accept: defaults.auto_accept,
}
@@ -586,7 +588,8 @@ impl App {
self.refresh_streams();
return;
}
// Typed values (no maximum), else the chosen presets.
// Typed values (no maximum), else the chosen presets (bitrate 0:
// automatic).
let choice = |i: i32, choices: &[u32]| {
choices[usize::try_from(i).unwrap_or(0).min(choices.len() - 1)]
};
@@ -602,8 +605,8 @@ impl App {
}
}
let defaults = ShareDefaults {
fps_index: nearest_choice(&FPS_CHOICES, fps),
bitrate_index: nearest_choice(&BITRATE_CHOICES, bitrate),
fps_index: nearest_choice(&LEGACY_FPS_CHOICES, fps),
bitrate_index: nearest_choice(&LEGACY_BITRATE_CHOICES, bitrate),
audio: form.audio,
auto_accept: form.auto_accept,
};
@@ -650,6 +653,10 @@ impl App {
self.settings.portal_restore_token = Some(token);
self.store_settings();
}
if setup.bitrate == 0 {
// Automatic: what it comes to for the size captured.
setup.bitrate = capture.streamer().bitrate_kbps();
}
if setup.audio && !capture.has_audio() {
setup.audio = false;
let reason = capture.audio_error().unwrap_or_default();
@@ -660,6 +667,8 @@ impl App {
(true, demo_viewers())
} else {
let session = session as u64;
// One layer, whatever a studio stream had before.
self.engine.send(Command::SetStreamLayers { session, layers: Vec::new() });
self.engine.send(Command::StartStream { session, setup, auto_accept });
(false, Vec::new())
};
+48 -9
View File
@@ -29,14 +29,14 @@ use voelin_core::media::voelin_media::mix::SourceHandle;
use voelin_core::media::voelin_media::{VideoFrame, convert};
use voelin_core::media::{
AudioApps, AudioSourceSpec, AudioSourceStats, Latest, Streamer, StreamerConfigUpdate,
StreamerStats, audio_apps, audio_source_specs, screen_sources,
StreamerStats, audio_apps, audio_source_specs, auto_bitrate, screen_sources,
};
use voelin_core::settings::{
AudioSourceKindSetting, AudioSourceSetting, Key, Kind, STREAM_AUDIO_SOURCES,
STREAM_BITRATE_KBPS, STREAM_LAYERS, STUDIO_RECORDING_DIR, STUDIO_REPLAY_MEMORY_MB,
STUDIO_REPLAY_SECONDS, STUDIO_SCENES, Settings, layer_specs,
};
use voelin_core::stream::{EndReason, StreamSetup, ViewerInfo, ViewerState};
use voelin_core::stream::{EndReason, LayerSpec, StreamSetup, ViewerInfo, ViewerState};
use voelin_core::studio::scene::{
Align, Background, Colour, Crop, Fit, Scene, Scenes, Source, SourceKind, Transform,
};
@@ -436,6 +436,30 @@ impl App {
self.studio.run.as_ref()?.streamer.as_ref()
}
/// The stream's layers for the engine: `stream.layers`, or the single
/// layer at [`studio_bitrate_kbps`](Self::studio_bitrate_kbps).
fn studio_session_layers(&self, settings: &Settings) -> Vec<LayerSpec> {
match layer_specs(&settings.get(&STREAM_LAYERS)) {
layers if layers.is_empty() => {
vec![LayerSpec::single(u64::from(self.studio_bitrate_kbps(settings)) * 1000)]
}
layers => layers,
}
}
/// `stream.bitrate_kbps`, an automatic one (0) as it comes to for the
/// output's size and frame rate.
fn studio_bitrate_kbps(&self, settings: &Settings) -> u32 {
match settings.get(&STREAM_BITRATE_KBPS) {
0 => {
let (width, height) = self.studio.scenes.size();
let bitrate = auto_bitrate(width, height, self.studio.scenes.fps());
u32::try_from(bitrate / 1000).unwrap_or(u32::MAX)
}
kbps => kbps,
}
}
fn studio_op(&self, op: Op) {
if let Some(run) = &self.studio.run {
let _ = run.ops.send(op);
@@ -960,7 +984,9 @@ impl App {
.destination
.and_then(|id| list.iter().position(|(d, _)| *d == id))
.map_or(-1, |i| i as i32);
// Automatic (0): the field stays empty and says so.
let bitrate = self.studio.settings.as_ref().map_or(0, |s| s.get(&STREAM_BITRATE_KBPS));
let bitrate = if bitrate == 0 { String::new() } else { bitrate.to_string() };
let form = StudioForm {
destination,
title: ui.title.clone().into(),
@@ -970,7 +996,7 @@ impl App {
show_chat: ui.show_chat,
show_now_playing: ui.show_now_playing,
audio: ui.audio,
bitrate: bitrate.to_string().into(),
bitrate: bitrate.into(),
};
self.studio_each(|b| {
if b.get_form() != form {
@@ -993,7 +1019,7 @@ impl App {
let measured: f64 = encoder.map_or(0.0, |e| e.layers.iter().map(|l| l.kbps).sum());
let layers = settings.map(|s| s.get(&STREAM_LAYERS)).unwrap_or_default();
let configured_bps: u64 = if layers.is_empty() {
settings.map_or(0, |s| u64::from(s.get(&STREAM_BITRATE_KBPS)) * 1000)
settings.map_or(0, |s| u64::from(self.studio_bitrate_kbps(s)) * 1000)
} else {
layers.iter().map(|l| l.bitrate).max().unwrap_or(0)
};
@@ -1191,6 +1217,11 @@ impl App {
/// `stream.layers` or `stream.bitrate_kbps` changed.
pub(crate) fn studio_encoding_changed(&mut self) {
let settings = self.studio_settings();
if let Some(stream) = &self.studio.stream {
let session = stream.session as u64;
let layers = self.studio_session_layers(&settings);
self.engine.send(Command::SetStreamLayers { session, layers });
}
let update = StreamerConfigUpdate {
bitrate_kbps: Some(settings.get(&STREAM_BITRATE_KBPS)),
layers: Some(layer_specs(&settings.get(&STREAM_LAYERS))),
@@ -1254,13 +1285,16 @@ impl App {
"audio" => ui.audio = on,
"bitrate" => {
let settings = self.studio_settings();
match parse_positive(value) {
// Nothing, 0 or "auto": automatic.
let auto =
matches!(value.trim(), "" | "0") || value.trim().eq_ignore_ascii_case("auto");
match parse_positive(value).or(auto.then_some(0)) {
Some(kbps) => {
if let Err(e) = settings.set(&STREAM_BITRATE_KBPS, kbps) {
self.set_status(e.to_string());
}
}
None => self.set_status("The bitrate is a whole number of kbit/s above 0."),
None => self.set_status("The bitrate is a whole number of kbit/s, or auto."),
}
return;
}
@@ -1328,7 +1362,7 @@ impl App {
pub(crate) fn studio_add_layer(&mut self) {
let settings = self.studio_settings();
let mut layers = settings.get(&STREAM_LAYERS);
let bitrate = settings.get(&STREAM_BITRATE_KBPS);
let bitrate = self.studio_bitrate_kbps(&settings);
if layers.is_empty() {
layers.push(vm::studio::next_layer(&[], bitrate));
}
@@ -1897,7 +1931,7 @@ impl App {
let layers = settings.get(&STREAM_LAYERS);
let bitrate = match layers.iter().map(|l| l.bitrate).max() {
Some(bps) => u32::try_from(bps / 1000).unwrap_or(u32::MAX),
None => settings.get(&STREAM_BITRATE_KBPS),
None => self.studio_bitrate_kbps(&settings),
};
let setup = StreamSetup {
name: stream_name(&self.studio.ui),
@@ -1906,7 +1940,12 @@ impl App {
..StreamSetup::default()
};
let auto_accept = self.settings.share.auto_accept;
self.engine.send(Command::StartStream { session: session as u64, setup, auto_accept });
// The stream's layers are the encoder's, so viewers move between
// them (the session sends only layers it knows of).
let session = session as u64;
let layers = self.studio_session_layers(&settings);
self.engine.send(Command::SetStreamLayers { session, layers });
self.engine.send(Command::StartStream { session, setup, auto_accept });
self.studio_refresh_status();
}