Fall back to the next decoder at runtime; recover from losses

A watched stream now decodes with the best decoder of its codec and,
when it fails, with the next of the codec's ladder, without ending the
watch: three failures in a row (errors, or ten frames without a picture)
replace it, the keyframe that failed starts the next one at once, and
otherwise a keyframe is asked for. Each switch is logged with its reason,
and DecodeStats tells the decoder in use.

The official client's H.264 has B-frames and rare keyframes, and the
pipeline dropped every frame after a loss or an error until the next
keyframe, which such an encoder seldom sends. Decoders that conceal what
a loss leaves missing (FFmpeg's H.264 and HEVC) now go on decoding while
a keyframe is asked for; the others still skip to it. Whenever one is
wanted it is asked for again every half second until it comes, also
while no frame arrives, instead of once per skipped frame.

Pictures come from a small pool and go to the callback as
Arc<VideoFrame>: decoders write into a free one (decode_into), and a
consumer that converts and drops each picture, as the app does, costs no
allocations.

Settings stream.hardware_decoding and stream.decoder_backend choose the
decoders like the encoder settings (decoder_preference); the app applies
them when they change, to streams watched from then on.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
selikhovandClaude Opus 5.5 committed 2026-10-03 23:02:48 +03:00
1 parent 66a2b8329b
commit 289e7d4e6b
5 files changed
+404 -55

No files matched your search

+8 -2
View File
@@ -17,7 +17,10 @@ use anyhow::{Context, Result};
use slint::{ComponentHandle, ModelRc, VecModel};
use tokio::runtime::Runtime;
use tracing::warn;
use voelin_core::settings::{AUDIO, CRASH_REPORTS, Settings};
use voelin_core::media::decoder_preference;
use voelin_core::settings::{
AUDIO, CRASH_REPORTS, STREAM_DECODER_BACKEND, STREAM_HARDWARE_DECODING, Settings,
};
use voelin_core::stream::StreamInfo;
use voelin_core::{
AudioSettings, Command, Engine, Event, History, HistoryMessage, SessionState, VoiceState,
@@ -559,7 +562,8 @@ pub fn run(options: RunOptions) -> Result<()> {
if settings.global_ptt {
ptt.set(Some(settings.ptt_key.clone()));
}
let video = Video::new(&dir, settings.openh264);
let mut video = Video::new(&dir, settings.openh264);
video.set_decoder_preference(decoder_preference(&prefs));
let switches = crate::dev::Switches::from_env();
let app = App {
ui: ui.as_weak(),
@@ -730,6 +734,8 @@ impl App {
self.playback = self.prefs.get(&CLIENT_PLAYBACK);
} else if appearance_keys().iter().any(|k| k.name() == key) {
self.apply_appearance();
} else if key == STREAM_HARDWARE_DECODING.name() || key == STREAM_DECODER_BACKEND.name() {
self.video.set_decoder_preference(decoder_preference(&self.prefs));
}
}
+32 -8
View File
@@ -13,8 +13,8 @@ use tracing::warn;
use voelin_core::media::voelin_media::capture::{CaptureSource, SourceId};
use voelin_core::media::voelin_media::{Codec, Codecs, EncoderReport, VideoFrame, convert};
use voelin_core::media::{
self, AudioSourceSpec, EncoderPreference, Latest, LocalPreview, Streamer, StreamerConfig,
Viewer, peer_config, preferred_codec, stream_codec,
self, AudioSourceSpec, DecoderPreference, EncoderPreference, Latest, LocalPreview, Streamer,
StreamerConfig, Viewer, peer_config, preferred_codec, stream_codec,
};
use voelin_core::stream::{LayerSpec, PeerConfig};
use voelin_core::studio::Studio;
@@ -62,6 +62,9 @@ pub(crate) struct Video {
/// automatic), from the settings.
encoder: EncoderPreference,
codec: Option<Codec>,
/// Which decoders to use (`stream.hardware_decoding`,
/// `stream.decoder_backend`).
decoder: DecoderPreference,
/// Sources of the share dialog, by index.
sources: Vec<ShareEntry>,
}
@@ -75,6 +78,7 @@ impl Video {
codecs: Arc::new(Codecs::new()),
encoder: EncoderPreference::default(),
codec: None,
decoder: DecoderPreference::default(),
#[cfg(not(target_os = "android"))]
openh264_dir: data_dir.join("openh264"),
#[cfg(not(target_os = "android"))]
@@ -108,7 +112,19 @@ impl Video {
self.codec = codec;
}
/// Every encoder backend, what works and why the rest does not.
/// Decode with other decoders (`stream.hardware_decoding`,
/// `stream.decoder_backend`): streams watched from now on.
pub fn set_decoder_preference(&mut self, decoder: DecoderPreference) {
if decoder != self.decoder {
let mut codecs = (*self.codecs).clone();
codecs.set_decoder_preference(decoder.clone());
self.codecs = Arc::new(codecs);
self.decoder = decoder;
}
}
/// Every encoder and decoder backend, what works and why the rest does
/// not.
#[allow(dead_code, reason = "for the integrations page")]
pub fn encoder_report(&self) -> EncoderReport {
self.codecs.report()
@@ -140,7 +156,11 @@ impl Video {
use openh264::State;
if !enabled {
self.h264 = State::Off;
self.codecs = Arc::new(Codecs::new().with_preference(self.encoder.clone()));
self.codecs = Arc::new(
Codecs::new()
.with_preference(self.encoder.clone())
.with_decoder_preference(self.decoder.clone()),
);
return;
}
if matches!(self.h264, State::Loaded(_) | State::Downloading) {
@@ -156,8 +176,12 @@ impl Video {
#[cfg(not(target_os = "android"))]
fn loaded(&mut self, library: openh264::OpenH264) {
self.h264 = openh264::State::Loaded(library.path().to_owned());
self.codecs =
Arc::new(Codecs::new().with_openh264(library).with_preference(self.encoder.clone()));
self.codecs = Arc::new(
Codecs::new()
.with_openh264(library)
.with_preference(self.encoder.clone())
.with_decoder_preference(self.decoder.clone()),
);
}
/// Download Cisco's OpenH264 (only on the user's request). `done` runs
@@ -344,8 +368,8 @@ impl Video {
fn deliver(
pictures: Arc<Latest<Picture>>,
wake: impl Fn() + Send + Sync + 'static,
) -> impl FnMut(VideoFrame) + Send + 'static {
move |frame: VideoFrame| {
) -> impl FnMut(Arc<VideoFrame>) + Send + 'static {
move |frame: Arc<VideoFrame>| {
let mut buffer = Picture::new(frame.width, frame.height);
let stride = frame.width as usize * 4;
if let Err(e) = convert::to_rgba(&frame, buffer.make_mut_bytes(), stride) {