Streams: offer the H.264 level the encoder makes, at most 5.2
The app's streams always offered H.264 at level 3.1: its peer configuration is made before a share starts, and only voelinctl set the real level. A 4K or high-frame-rate stream then declared far less in its offer than in its SPS, which a viewer that sizes its decoder from the offer may refuse. Each layer's encoder now tells its sink what it makes (`MediaSink::layer_format`: size, frame rate, bitrate, whether the bitrate is automatic), when the sink is attached (from the size captured so far, so a viewer that joins before the first frame is encoded is covered), with the layer's first frame and on every change. The engine's stream task offers new viewers the level of the largest layer (its bitrate at `max_bitrate` if that is higher) in both profiles, and puts an automatic bitrate into the layer as before; this one hook replaces `auto_bitrate`. Offering 6.0 broke the stream: str0m 0.23 knows no H.264 level above 5.2, nor does libwebrtc (`H264Level` on its main branch), and a `profile-level-id` they cannot parse matches nothing, so a viewer answered without video (`m=video 0`). The offer is now capped at 5.2 (`h264::MAX_OFFER_LEVEL`); a stream that needs 6.x (4K above 60 fps, 8K) is offered as 5.2 and keeps its real level in the SPS. Tests: through two stream tasks, an encoder's 3840x2160@60 reaches the next offer as 640c34 and 42e034 (and without the update it stays 3.1); the largest layer counts, at its max bitrate; an automatic bitrate still moves the layer once, not per frame. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
df4f534022
commit
500d41e5d2
4 files changed
+266
-78
No files matched your search
@@ -92,8 +92,18 @@ pub fn profile_level_id(profile: H264Profile, level_idc: u8) -> u32 {
|
||||
/// offer official clients know stays the same for small streams.
|
||||
pub const MIN_OFFER_LEVEL: u8 = 31;
|
||||
|
||||
/// The highest level an offer declares (5.2): libwebrtc (the official
|
||||
/// client's stack; `H264Level` in `h264_profile_level_id.h`, main branch
|
||||
/// 2026-10) and str0m 0.23 (`H264LevelIdc`) know no level above it, and a
|
||||
/// `profile-level-id` they cannot parse matches nothing: a viewer answered
|
||||
/// such an offer without video (tried with str0m: `m=video 0`, no payload
|
||||
/// type). Streams that need 6.0-6.2 (3840x2160 above 60 fps, 7680x4320,
|
||||
/// 1080p above 250 fps) are offered as 5.2 and still carry their real level
|
||||
/// in the SPS, which decoders size themselves from.
|
||||
pub const MAX_OFFER_LEVEL: u8 = 52;
|
||||
|
||||
/// `profile-level-id` for a stream of this size, frame rate and bitrate,
|
||||
/// at least level 3.1.
|
||||
/// at least level 3.1 and at most [`MAX_OFFER_LEVEL`].
|
||||
pub fn offer_profile_level_id(
|
||||
profile: H264Profile,
|
||||
width: u32,
|
||||
@@ -101,8 +111,8 @@ pub fn offer_profile_level_id(
|
||||
fps: u32,
|
||||
bitrate: u64,
|
||||
) -> u32 {
|
||||
let level = level_idc(profile, width, height, fps, bitrate).max(MIN_OFFER_LEVEL);
|
||||
profile_level_id(profile, level)
|
||||
let level = level_idc(profile, width, height, fps, bitrate);
|
||||
profile_level_id(profile, level.clamp(MIN_OFFER_LEVEL, MAX_OFFER_LEVEL))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@@ -142,6 +152,11 @@ mod tests {
|
||||
assert_eq!(profile_level_id(HIGH, 31), 0x640c1f);
|
||||
assert_eq!(profile_level_id(H264Profile::ConstrainedBaseline, 31), 0x42e01f);
|
||||
assert_eq!(offer_profile_level_id(HIGH, 320, 240, 15, 200_000), 0x640c1f);
|
||||
// 4K60 is 5.2; 4K120 and 8K need 6.x, which peers cannot parse: 5.2.
|
||||
assert_eq!(offer_profile_level_id(HIGH, 3840, 2160, 60, 60_000_000), 0x640c34);
|
||||
assert_eq!(offer_profile_level_id(HIGH, 3840, 2160, 120, 60_000_000), 0x640c34);
|
||||
let baseline = H264Profile::ConstrainedBaseline;
|
||||
assert_eq!(offer_profile_level_id(baseline, 7680, 4320, 60, 60_000_000), 0x42e034);
|
||||
assert_eq!(offer_profile_level_id(HIGH, 1920, 1080, 60, 8_000_000), 0x640c2a);
|
||||
// Back from the SDP, at any level; other profiles are not ours.
|
||||
assert_eq!(H264Profile::from_profile_level_id(0x640c2a), Some(HIGH));
|
||||
|
||||
Reference in new issue
Block a user