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

+56
View File
@@ -124,6 +124,14 @@ impl StreamSink {
self.feedback.bitrate(layer)
}
/// The encoder of `layer` runs at `bitrate` bit/s by itself (an
/// automatic bitrate, from the size and frame rate it encodes): the
/// stream's layer takes it, so new connections start at it and every
/// connection probes for it.
pub fn auto_bitrate(&self, layer: LayerId, bitrate: u64) {
let _ = self.tx.send(StreamInput::AutoBitrate { layer, bitrate });
}
pub fn is_live(&self) -> bool {
self.live.load(Ordering::Relaxed)
}
@@ -169,6 +177,12 @@ pub(crate) enum StreamInput {
},
/// Simulcast layers of our stream (now and for streams started later).
Layers(Vec<LayerSpec>),
/// The bitrate an encoder chose for a layer of the live stream
/// ([`StreamSink::auto_bitrate`]).
AutoBitrate {
layer: LayerId,
bitrate: u64,
},
/// SRTP profile order of new connections.
SrtpProfiles(Vec<SrtpProfile>),
Frame(EncodedFrame),
@@ -389,6 +403,21 @@ impl StreamTask {
Ok(())
}
}
StreamInput::AutoBitrate { layer, bitrate } => {
// The live stream's layers only; the configured ones (for
// streams started later) keep their automatic bitrate.
let layers = self.streams.streamer().map(|s| s.layers().to_vec());
match layers {
Some(mut layers) => match layers.iter_mut().find(|l| l.id == layer) {
Some(spec) if spec.bitrate != bitrate => {
spec.bitrate = bitrate;
self.streams.set_layers(layers)
}
_ => Ok(()),
},
None => Ok(()),
}
}
StreamInput::SrtpProfiles(profiles) => {
self.streams.set_srtp_profiles(profiles);
Ok(())
@@ -962,6 +991,33 @@ mod tests {
feeder.await.unwrap();
}
/// An encoder's automatic bitrate reaches the live stream's layer (which
/// asks the viewer's layer for a keyframe, as every layer change does);
/// the same bitrate again changes nothing.
#[tokio::test(flavor = "multi_thread")]
async fn automatic_bitrates_reach_the_live_stream() {
let (handles, mut rx, _frames) = pair(None);
let sink = live_and_watching(&handles, &mut rx).await;
let keyframes = || {
let mut layers = LayerSet::new();
sink.take_layer_keyframes(&mut layers);
layers.contains(0)
};
keyframes();
sink.auto_bitrate(0, 9_000_000);
timeout(Duration::from_secs(5), async {
while !keyframes() {
tokio::time::sleep(Duration::from_millis(10)).await;
}
})
.await
.expect("the layer did not change");
sink.auto_bitrate(0, 9_000_000);
tokio::time::sleep(Duration::from_millis(200)).await;
assert!(!keyframes(), "nothing changed");
handles[0].send(StreamInput::Stop);
}
/// A viewer that was not told about a running stream, and whose server
/// lookup goes unanswered, finds it in the gateway directory and
/// watches it; the streamer reports its stream's life for the directory.