From 3aba9a165d10223394642849894aeaf0518a841a Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 26 Sep 2026 23:15:42 +0000 Subject: [PATCH] Encoded frames say whether they are keyframes; StreamSink gets the layer methods Viewers switch simulcast layers on a keyframe of the new layer, so the stream sessions need to know which frames are keyframes. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01NJvYyRmTjKcb9i8eyYXK5G --- crates/voelin-core/src/media.rs | 17 ++++++++++++++++- crates/voelin-core/src/stream.rs | 14 ++++++++++++++ crates/voelin-core/tests/stream_live.rs | 1 + crates/voelin-stream/src/source.rs | 12 +++++++++++- 4 files changed, 42 insertions(+), 2 deletions(-) diff --git a/crates/voelin-core/src/media.rs b/crates/voelin-core/src/media.rs index 7692034..f3712b8 100644 --- a/crates/voelin-core/src/media.rs +++ b/crates/voelin-core/src/media.rs @@ -165,6 +165,14 @@ impl MediaSink for StreamSink { fn take_keyframe_request(&self) -> bool { StreamSink::take_keyframe_request(self) } + + fn take_layer_keyframes(&self, layers: &mut LayerSet) { + StreamSink::take_layer_keyframes(self, layers); + } + + fn layer_bitrate(&self, layer: LayerId) -> Option { + StreamSink::layer_bitrate(self, layer) + } } /// Which backend captures monitors and windows. @@ -458,6 +466,7 @@ fn video_loop( time: MediaTime::from_90khz(f.pts_90khz), data: f.data.into(), layer: 0, + keyframe: f.keyframe, }; if sink.send(frame) { shared.video_frames.fetch_add(1, Ordering::Relaxed); @@ -522,7 +531,13 @@ fn audio_loop(shared: &Shared, mut buffers: FrameReceiver, mut enco pending.drain(..OPUS_FRAME * 2); if let Some(data) = data { let time = MediaTime::new(frames * OPUS_FRAME as u64, Frequency::FORTY_EIGHT_KHZ); - if sink.send(EncodedFrame { kind: MediaKind::Audio, time, data, layer: 0 }) { + if sink.send(EncodedFrame { + kind: MediaKind::Audio, + time, + data, + layer: 0, + keyframe: false, + }) { shared.audio_frames.fetch_add(1, Ordering::Relaxed); } } diff --git a/crates/voelin-core/src/stream.rs b/crates/voelin-core/src/stream.rs index ba6bf7d..6cbdc1e 100644 --- a/crates/voelin-core/src/stream.rs +++ b/crates/voelin-core/src/stream.rs @@ -79,6 +79,20 @@ impl StreamSink { self.keyframe.swap(false, Ordering::Relaxed) } + /// Adds to `layers` the simulcast layers viewers asked a keyframe for + /// since the last call. + pub fn take_layer_keyframes(&self, layers: &mut voelin_stream::LayerSet) { + if self.take_keyframe_request() { + layers.insert(0); + } + } + + /// Bitrate (bit/s) the bandwidth estimates of `layer`'s viewers allow. + pub fn layer_bitrate(&self, layer: voelin_stream::LayerId) -> Option { + let _ = layer; + None + } + pub fn is_live(&self) -> bool { self.live.load(Ordering::Relaxed) } diff --git a/crates/voelin-core/tests/stream_live.rs b/crates/voelin-core/tests/stream_live.rs index edd9b24..427c909 100644 --- a/crates/voelin-core/tests/stream_live.rs +++ b/crates/voelin-core/tests/stream_live.rs @@ -225,6 +225,7 @@ async fn ts6_stream_between_sessions() { time: voelin_core::stream::MediaTime::from_90khz(0), data: vec![0].into(), layer: 0, + keyframe: false, })); feeder.await.unwrap(); diff --git a/crates/voelin-stream/src/source.rs b/crates/voelin-stream/src/source.rs index 6ad43e1..72a4d6d 100644 --- a/crates/voelin-stream/src/source.rs +++ b/crates/voelin-stream/src/source.rs @@ -21,6 +21,9 @@ pub struct EncodedFrame { /// The simulcast layer of a video frame (0 without simulcast, and for /// audio). pub layer: LayerId, + /// A video keyframe: viewers can start decoding (or switch to this layer) + /// here. + pub keyframe: bool, } /// Produces encoded frames in real time. @@ -106,6 +109,7 @@ impl FrameSource for SyntheticSource { time: MediaTime::from_90khz(n * 90_000 / u64::from(self.fps)), data: Self::video_frame(n, self.video_size).into(), layer: 0, + keyframe: true, }); self.video_frames += 1; } @@ -115,7 +119,13 @@ impl FrameSource for SyntheticSource { let time = MediaTime::new(self.audio_frames * OPUS_FRAME, Frequency::FORTY_EIGHT_KHZ); let data = Arc::from(&OPUS_SILENCE[..]); - out.push(EncodedFrame { kind: MediaKind::Audio, time, data, layer: 0 }); + out.push(EncodedFrame { + kind: MediaKind::Audio, + time, + data, + layer: 0, + keyframe: false, + }); self.audio_frames += 1; } }