Offer H.264 in Constrained High, then Constrained Baseline
A streamer offered H.264 only in Constrained High, which peers whose decoders list no High profile (headless Chromium's libwebrtc, many other WebRTC stacks) cannot answer, so an H.264 stream had no way to reach them. The offer now lists both profiles, best first (PT 112 Constrained High, PT 108 Constrained Baseline, at the level the stream needs). The answer's payload type tells the streamer which one the viewer took (PeerEvent::VideoCodec now carries a VideoFormat: the codec, and H.264 with its profile). The streamer already ran one encoder per codec viewers chose; it now runs one per format, so a Baseline viewer gets frames from an encoder in Constrained Baseline, and frames go only to viewers of their format (LayerFeedback has a bit for it). voelinctl's FrameSource path offers Constrained High alone, as it cannot encode per viewer. The browser test now offers the ladder and checks the SPS profile of what it sent against what the browser chose. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
3bd230767c
commit
9b20da22d8
9 files changed
+533
-214
No files matched your search
@@ -1,8 +1,9 @@
|
||||
//! The stream's real encoders against a browser's WebRTC stack: the test
|
||||
//! pattern, encoded by each encoder this machine has as a share encodes the
|
||||
//! screen, sent by our streamer peer offering just that codec, must decode
|
||||
//! at its size in headless Chromium (libwebrtc, the stack the official
|
||||
//! TeamSpeak 6 client is built on).
|
||||
//! screen, sent by our streamer peer offering just that codec (H.264 in both
|
||||
//! profiles of the ladder, encoded in the one the browser chose), must
|
||||
//! decode at its size in headless Chromium (libwebrtc, the stack the
|
||||
//! official TeamSpeak 6 client is built on).
|
||||
//!
|
||||
//! Runs only with `VOELIN_INTEROP=1` (see `tests/interop/README.md`). Codecs
|
||||
//! the browser does not decode (H.264 in Playwright's Chromium, HEVC) are
|
||||
@@ -11,6 +12,8 @@
|
||||
|
||||
#![cfg(feature = "media-desktop")]
|
||||
|
||||
use std::sync::atomic::{AtomicBool, Ordering};
|
||||
use std::sync::{Arc, mpsc};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use serde_json::{Value, json};
|
||||
@@ -18,11 +21,11 @@ use tokio::time::timeout;
|
||||
use voelin_core::media::voelin_media::capture::SourceId;
|
||||
use voelin_core::media::voelin_media::{Codec, Codecs};
|
||||
use voelin_core::media::{
|
||||
EncodedSource, EncoderPreference, Streamer, StreamerConfig, decoded_everywhere, media_codec,
|
||||
EncoderPreference, MediaSink, Streamer, StreamerConfig, decoded_everywhere, media_codec,
|
||||
peer_config, preferred_codec, video_codec,
|
||||
};
|
||||
use voelin_core::stream::{FrameSource, PeerConfig, VideoCodec};
|
||||
use voelin_stream::{Peer, PeerEvent};
|
||||
use voelin_core::stream::{EncodedFrame, PeerConfig, VideoFormat};
|
||||
use voelin_stream::{H264Profile, Peer, PeerEvent};
|
||||
|
||||
#[path = "../../../tests/interop/browser.rs"]
|
||||
mod browser;
|
||||
@@ -30,10 +33,45 @@ use browser::{Browser, enabled};
|
||||
|
||||
const SIZE: (u32, u32) = (1280, 720);
|
||||
|
||||
/// A viewer as the stream task serves it: the frames of the format its
|
||||
/// answer chose (the streamer makes an encoder of that format), keyframe
|
||||
/// requests back.
|
||||
struct ViewerSink {
|
||||
format: VideoFormat,
|
||||
frames: mpsc::Sender<EncodedFrame>,
|
||||
keyframe: AtomicBool,
|
||||
}
|
||||
|
||||
impl MediaSink for ViewerSink {
|
||||
fn send(&self, _: EncodedFrame) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn send_video(&self, frame: EncodedFrame, format: VideoFormat) -> bool {
|
||||
format != self.format || self.frames.send(frame).is_ok()
|
||||
}
|
||||
|
||||
fn video_codecs(&self, out: &mut Vec<VideoFormat>) {
|
||||
out.push(self.format);
|
||||
}
|
||||
|
||||
fn take_keyframe_request(&self) -> bool {
|
||||
self.keyframe.swap(false, Ordering::Relaxed)
|
||||
}
|
||||
}
|
||||
|
||||
/// The streamer, its viewer and the viewer's frames.
|
||||
type Source = (Streamer, Arc<ViewerSink>, mpsc::Receiver<EncodedFrame>);
|
||||
|
||||
/// The test pattern encoded with `codec` by the encoders `preference` picks
|
||||
/// (from DMA-BUFs with `dmabuf`, as the ScreenCast portal hands over the
|
||||
/// screen).
|
||||
async fn test_pattern(preference: EncoderPreference, codec: Codec, dmabuf: bool) -> EncodedSource {
|
||||
/// screen), for a viewer of `format`.
|
||||
async fn test_pattern(
|
||||
preference: EncoderPreference,
|
||||
codec: Codec,
|
||||
dmabuf: bool,
|
||||
format: VideoFormat,
|
||||
) -> Source {
|
||||
let codecs = Codecs::new().with_preference(preference.clone());
|
||||
let config = StreamerConfig {
|
||||
source: SourceId::Synthetic,
|
||||
@@ -46,12 +84,22 @@ async fn test_pattern(preference: EncoderPreference, codec: Codec, dmabuf: bool)
|
||||
encoder: preference,
|
||||
..StreamerConfig::default()
|
||||
};
|
||||
EncodedSource::new(Streamer::start(&codecs, config).await.unwrap())
|
||||
let streamer = Streamer::start(&codecs, config).await.unwrap();
|
||||
let (tx, frames) = mpsc::channel();
|
||||
let sink = Arc::new(ViewerSink { format, frames: tx, keyframe: AtomicBool::new(true) });
|
||||
streamer.attach(sink.clone());
|
||||
(streamer, sink, frames)
|
||||
}
|
||||
|
||||
/// `profile_idc` of the first SPS in an H.264 access unit (Annex B).
|
||||
fn sps_profile(data: &[u8]) -> Option<u8> {
|
||||
let at = data.windows(4).position(|w| w[..3] == [0, 0, 1] && w[3] & 0x1f == 7)?;
|
||||
data.get(at + 4).copied()
|
||||
}
|
||||
|
||||
/// Our streamer peer with `config`'s offer, answered by the browser and
|
||||
/// connected; the codec the answer chose, `None` if it took no video.
|
||||
async fn connect(browser: &mut Browser, config: &PeerConfig) -> Option<(Peer, VideoCodec)> {
|
||||
/// connected; the format the answer chose, `None` if it took no video.
|
||||
async fn connect(browser: &mut Browser, config: &PeerConfig) -> Option<(Peer, VideoFormat)> {
|
||||
let (mut peer, offer) = Peer::offer(config, "interop").await.unwrap();
|
||||
let answer = browser.call(json!({ "op": "answer", "sdp": offer })).await;
|
||||
peer.accept_answer(answer["sdp"].as_str().unwrap()).await.ok()?;
|
||||
@@ -71,27 +119,30 @@ async fn connect(browser: &mut Browser, config: &PeerConfig) -> Option<(Peer, Vi
|
||||
Some((peer, chosen?))
|
||||
}
|
||||
|
||||
/// Three seconds of `source` to `peer`; the browser's `inbound-rtp` video
|
||||
/// stats.
|
||||
async fn send(browser: &mut Browser, peer: &mut Peer, source: &mut EncodedSource) -> Value {
|
||||
source.request_keyframe();
|
||||
let mut frames = Vec::new();
|
||||
/// Three seconds of the streamer's frames for its viewer to `peer`; the
|
||||
/// browser's `inbound-rtp` video stats, and the first frame sent.
|
||||
async fn send(
|
||||
browser: &mut Browser,
|
||||
peer: &mut Peer,
|
||||
(_streamer, sink, frames): Source,
|
||||
) -> (Value, Option<EncodedFrame>) {
|
||||
let mut first = None;
|
||||
let end = Instant::now() + Duration::from_secs(3);
|
||||
while Instant::now() < end {
|
||||
source.poll_frames(Instant::now(), &mut frames);
|
||||
for f in frames.drain(..) {
|
||||
peer.write(f.kind, f.time, f.data);
|
||||
while let Ok(f) = frames.try_recv() {
|
||||
peer.write(f.kind, f.time, f.data.clone());
|
||||
first.get_or_insert(f);
|
||||
}
|
||||
while let Some(event) = peer.try_next_event() {
|
||||
if matches!(event, PeerEvent::KeyframeRequest) {
|
||||
source.request_keyframe();
|
||||
sink.keyframe.store(true, Ordering::Relaxed);
|
||||
}
|
||||
}
|
||||
tokio::time::sleep(Duration::from_millis(5)).await;
|
||||
}
|
||||
let stats = browser.call(json!({ "op": "stats" })).await;
|
||||
peer.close();
|
||||
stats["inbound"]["video"].clone()
|
||||
(stats["inbound"]["video"].clone(), first)
|
||||
}
|
||||
|
||||
/// Whether the browser decoded most of three seconds at the stream's size.
|
||||
@@ -107,7 +158,9 @@ fn decoded(video: &Value) -> Result<(), String> {
|
||||
|
||||
/// Every encoder of this machine (hardware ones from memory and from
|
||||
/// DMA-BUFs) decodes in the browser: most frames decoded, at the stream's
|
||||
/// size.
|
||||
/// size. H.264 is offered in both profiles of the ladder; the browser takes
|
||||
/// the one it lists (headless Chromium: Constrained Baseline only) and gets
|
||||
/// it from an encoder of that profile.
|
||||
#[tokio::test(flavor = "multi_thread")]
|
||||
async fn real_encoders_decode_in_browser() {
|
||||
if !enabled() {
|
||||
@@ -126,25 +179,30 @@ async fn real_encoders_decode_in_browser() {
|
||||
let config = PeerConfig {
|
||||
video_codecs: vec![video_codec(codec)],
|
||||
audio: false,
|
||||
// Headless Chromium lists only the H.264 profiles of
|
||||
// libwebrtc's software decoder (Baseline, Constrained
|
||||
// Baseline, Main), not the Constrained High we offer; its
|
||||
// FFmpeg decoder takes our High profile streams all the same.
|
||||
h264_profile_level_id: 0x42e01f,
|
||||
..PeerConfig::loopback()
|
||||
};
|
||||
let Some((mut peer, _)) = connect(&mut browser, &config).await else {
|
||||
let Some((mut peer, format)) = connect(&mut browser, &config).await else {
|
||||
eprintln!("{what}: the browser does not decode it, skipped");
|
||||
continue;
|
||||
};
|
||||
let preference =
|
||||
EncoderPreference { hardware: true, backend: backend.name().parse().unwrap() };
|
||||
let mut source = test_pattern(preference, codec, dmabuf).await;
|
||||
let video = send(&mut browser, &mut peer, &mut source).await;
|
||||
eprintln!("{what}: {video}");
|
||||
let source = test_pattern(preference, codec, dmabuf, format).await;
|
||||
let (video, first) = send(&mut browser, &mut peer, source).await;
|
||||
eprintln!("{what}, the browser chose {format}: {video}");
|
||||
if let Err(e) = decoded(&video) {
|
||||
failed.push(format!("{what}: {e}"));
|
||||
}
|
||||
// H.264 in the profile the answer chose.
|
||||
let expected = match format {
|
||||
VideoFormat::H264(H264Profile::ConstrainedHigh) => Some(100),
|
||||
VideoFormat::H264(H264Profile::ConstrainedBaseline) => Some(66),
|
||||
_ => None,
|
||||
};
|
||||
let sps = first.as_ref().and_then(|f| sps_profile(&f.data));
|
||||
if expected.is_some() && sps != expected {
|
||||
failed.push(format!("{what}: SPS profile {sps:?}, {format} answered"));
|
||||
}
|
||||
}
|
||||
}
|
||||
browser.quit().await;
|
||||
@@ -172,15 +230,15 @@ async fn share_offer_picks_a_codec_every_client_decodes() {
|
||||
};
|
||||
let config = peer_config(&codecs, config);
|
||||
eprintln!("offer: {:?}", config.video_codecs);
|
||||
let (mut peer, chosen) = connect(&mut browser, &config).await.expect("video answered");
|
||||
let chosen = media_codec(chosen);
|
||||
let (mut peer, format) = connect(&mut browser, &config).await.expect("video answered");
|
||||
let chosen = media_codec(format.codec());
|
||||
assert!(
|
||||
decoded_everywhere(chosen),
|
||||
"the browser chose {chosen} from {:?}",
|
||||
config.video_codecs
|
||||
);
|
||||
let mut source = test_pattern(codecs.preference().clone(), chosen, false).await;
|
||||
let video = send(&mut browser, &mut peer, &mut source).await;
|
||||
let source = test_pattern(codecs.preference().clone(), chosen, false, format).await;
|
||||
let (video, _) = send(&mut browser, &mut peer, source).await;
|
||||
eprintln!("{chosen}: {video}");
|
||||
browser.quit().await;
|
||||
decoded(&video).unwrap();
|
||||
|
||||
Reference in new issue
Block a user