Encode each layer once per codec its viewers chose (WIP)
Offers list the stream codec first, then the codecs of hardware encoders, VP8 through libvpx and HEVC last; software encoders other than VP8 are only ever the stream codec, so no answer starts an expensive one. The streamer's peer reports the codec each answer chose, the session sends video only to viewers of its codec and tells the encoders through LayerFeedback which codecs are needed. Each layer's encoder thread keeps one encoder per such codec, made when a viewer needs it and dropped when none does; the stream codec is skipped while nobody takes it. The H.264 profile-level-id is computed from macroblocks per second, frame size and bitrate (Table A-1), at least 3.1 as before. Streamer takes an encoder preference (hardware on/off, auto / software / a named backend) that reconfigure can change live. HEVC is negotiable (str0m packetizes it) and only offered when a hardware encoder has it. Work in progress: committed early so it cannot be lost. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NJvYyRmTjKcb9i8eyYXK5G
This commit is contained in:
1 parent
1b1d662fef
commit
40f6bb4ca6
10 files changed
+799
-97
No files matched your search
@@ -1,5 +1,6 @@
|
||||
//! Feedback from the stream sessions to the encoders of our stream's layers:
|
||||
//! keyframe requests and bitrate targets per [`LayerId`].
|
||||
//! keyframe requests and bitrate targets per [`LayerId`], and the video
|
||||
//! codecs the viewers negotiated.
|
||||
//!
|
||||
//! [`LayerFeedback`] is shared between the stream session (which writes) and
|
||||
//! encoder threads (which read every frame). Both sides only use atomics;
|
||||
@@ -9,9 +10,10 @@
|
||||
|
||||
use std::array;
|
||||
use std::sync::OnceLock;
|
||||
use std::sync::atomic::{AtomicU16, AtomicU64, Ordering};
|
||||
use std::sync::atomic::{AtomicU8, AtomicU16, AtomicU64, Ordering};
|
||||
|
||||
use crate::layer::{LayerId, LayerSet};
|
||||
use crate::peer::VideoCodec;
|
||||
|
||||
/// Words of the keyframe bitmap: one bit per layer id.
|
||||
const WORDS: usize = (LayerId::MAX as usize + 1) / 64;
|
||||
@@ -30,6 +32,8 @@ pub struct LayerFeedback {
|
||||
top: AtomicU16,
|
||||
/// Bitrate targets (bit/s, 0: none) in chunks of [`CHUNK`] layer ids.
|
||||
bitrates: Box<[OnceLock<Box<[AtomicU64]>>]>,
|
||||
/// [`VideoCodec::bit`]s of the codecs connected viewers negotiated.
|
||||
codecs: AtomicU8,
|
||||
}
|
||||
|
||||
impl Default for LayerFeedback {
|
||||
@@ -51,6 +55,7 @@ impl LayerFeedback {
|
||||
summary: array::from_fn(|_| AtomicU64::new(0)),
|
||||
top: AtomicU16::new(0),
|
||||
bitrates: (0..=LayerId::MAX as usize / CHUNK).map(|_| OnceLock::new()).collect(),
|
||||
codecs: AtomicU8::new(0),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -122,6 +127,22 @@ impl LayerFeedback {
|
||||
let bitrate = chunk[usize::from(layer) % CHUNK].load(Ordering::Relaxed);
|
||||
(bitrate != 0).then_some(bitrate)
|
||||
}
|
||||
|
||||
/// The video codecs the viewers negotiated.
|
||||
pub fn set_codecs(&self, codecs: impl IntoIterator<Item = VideoCodec>) {
|
||||
let bits = codecs.into_iter().fold(0, |bits, c| bits | c.bit());
|
||||
self.codecs.store(bits, Ordering::Relaxed);
|
||||
}
|
||||
|
||||
/// Whether a viewer negotiated `codec`.
|
||||
pub fn has_codec(&self, codec: VideoCodec) -> bool {
|
||||
self.codecs.load(Ordering::Relaxed) & codec.bit() != 0
|
||||
}
|
||||
|
||||
/// Whether any viewer negotiated a codec yet.
|
||||
pub fn has_codecs(&self) -> bool {
|
||||
self.codecs.load(Ordering::Relaxed) != 0
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -160,4 +181,15 @@ mod tests {
|
||||
f.set_bitrate(3, None);
|
||||
assert_eq!(f.bitrate(3), None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn codecs() {
|
||||
let f = LayerFeedback::new();
|
||||
assert!(!f.has_codecs());
|
||||
f.set_codecs([VideoCodec::Vp8, VideoCodec::H265, VideoCodec::Vp8]);
|
||||
assert!(f.has_codec(VideoCodec::Vp8) && f.has_codec(VideoCodec::H265));
|
||||
assert!(!f.has_codec(VideoCodec::H264));
|
||||
f.set_codecs([]);
|
||||
assert!(!f.has_codecs());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user