use std::collections::VecDeque;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, AtomicU32, AtomicU64, Ordering};
use std::sync::{Arc, Mutex, PoisonError};
use std::time::Duration;
use kessel_video::{
Bytes, CameraInfo, Capabilities, CaptureConstraints, CaptureMode, CodecCapability,
CodecDirection, DecoderConfig, Done, EncodeOptions, EncodedFrame, EncoderConfig, EncoderInfo,
EncoderStats, EventHub, EventRx, Facing, FrameSource, H264Capability, H264Level, H264Profile,
ImplPreference, Implementation, LayerInfo, Permission, PickOptions, PixelFormat, Rates,
ScreenSelection, ScreenSourceInfo, SourceEvent, SourceStats, VideoBackend, VideoCodec,
VideoDecoder, VideoEncoder, VideoError, VideoFrame, Wake,
};
use super::container::{h264_access_units, ivf_frames};
use super::rtp::vp8::is_keyframe;
#[derive(Debug, thiserror::Error)]
#[non_exhaustive]
pub enum FixtureError {
#[error("{path}: {source}")]
Io {
path: PathBuf,
source: std::io::Error,
},
#[error("{file}: {detail}")]
Parse {
file: String,
detail: String,
},
}
const LAYERS: [(&str, u32, u32); 3] = [("q", 320, 180), ("h", 640, 360), ("f", 1280, 720)];
const VP8_PATTERN: [u8; 4] = [0, 2, 1, 2];
const ENCODER_QUEUE: usize = 8;
const FPS: u32 = 30;
#[derive(Debug)]
struct Stream {
codec: VideoCodec,
file: String,
width: u32,
height: u32,
frames: Vec<(bool, Bytes)>,
}
impl Stream {
fn next_key(&self, i: usize) -> usize {
let n = self.frames.len();
(0..n)
.map(|k| (i + k) % n)
.find(|&k| self.frames[k].0)
.unwrap_or(0)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum FixtureH264 {
#[default]
Software,
Hardware,
HardwareOpenFails,
HardwareReportsSoftware,
HardwareReportsUnknown,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct FixtureOptions {
pub h264: FixtureH264,
}
impl FixtureOptions {
pub fn new() -> FixtureOptions {
FixtureOptions::default()
}
pub fn with_h264(mut self, h264: FixtureH264) -> FixtureOptions {
self.h264 = h264;
self
}
}
#[derive(Debug, Default)]
struct Counts {
vp8_opens: AtomicU64,
vp8_live: Arc<AtomicU64>,
h264_opens: AtomicU64,
h264_live: Arc<AtomicU64>,
h264_epoch: Arc<AtomicU64>,
h264_refused: AtomicBool,
vp8_failures: AtomicU32,
}
pub struct FixtureBackend {
streams: Vec<Arc<Stream>>,
events: EventHub,
shut: Arc<AtomicBool>,
opts: FixtureOptions,
counts: Counts,
}
impl std::fmt::Debug for FixtureBackend {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("FixtureBackend")
.field("streams", &self.streams.len())
.finish_non_exhaustive()
}
}
fn read(dir: &Path, file: &str) -> Result<Vec<u8>, FixtureError> {
let path = dir.join(file);
std::fs::read(&path).map_err(|source| FixtureError::Io { path, source })
}
impl FixtureBackend {
pub fn load(dir: impl AsRef<Path>) -> Result<Arc<FixtureBackend>, FixtureError> {
FixtureBackend::load_with(dir, FixtureOptions::default())
}
pub fn load_with(
dir: impl AsRef<Path>,
opts: FixtureOptions,
) -> Result<Arc<FixtureBackend>, FixtureError> {
let dir = dir.as_ref();
let mut streams = Vec::new();
for (rid, width, height) in LAYERS {
let file = format!("vp8_sim_{rid}.ivf");
let frames: Vec<(bool, Bytes)> = ivf_frames(&read(dir, &file)?)
.map_err(|detail| FixtureError::Parse {
file: file.clone(),
detail,
})?
.into_iter()
.map(|f| (is_keyframe(&f), Bytes::from(f)))
.collect();
streams.push((VideoCodec::Vp8, file, width, height, frames));
let file = format!("h264_sim_{rid}.h264");
let frames: Vec<(bool, Bytes)> = h264_access_units(&read(dir, &file)?)
.into_iter()
.map(|(key, au)| (key, Bytes::from(au)))
.collect();
streams.push((VideoCodec::H264, file, width, height, frames));
}
let mut out = Vec::new();
for (codec, file, width, height, frames) in streams {
if frames.is_empty() || !frames[0].0 {
return Err(FixtureError::Parse {
file,
detail: "no frames, or the first is not a keyframe".into(),
});
}
out.push(Arc::new(Stream {
codec,
file,
width,
height,
frames,
}));
}
Ok(Arc::new(FixtureBackend {
streams: out,
events: EventHub::default(),
shut: Arc::new(AtomicBool::new(false)),
opts,
counts: Counts::default(),
}))
}
pub fn encoder_opens(&self, codec: VideoCodec) -> u64 {
match codec {
VideoCodec::Vp8 => self.counts.vp8_opens.load(Ordering::SeqCst),
VideoCodec::H264 => self.counts.h264_opens.load(Ordering::SeqCst),
_ => 0,
}
}
pub fn encoders_open(&self, codec: VideoCodec) -> u64 {
match codec {
VideoCodec::Vp8 => self.counts.vp8_live.load(Ordering::SeqCst),
VideoCodec::H264 => self.counts.h264_live.load(Ordering::SeqCst),
_ => 0,
}
}
pub fn lose_h264(&self, reopen_fails: bool) {
if reopen_fails {
self.counts.h264_refused.store(true, Ordering::SeqCst);
}
self.counts.h264_epoch.fetch_add(1, Ordering::SeqCst);
}
pub fn fail_vp8_opens(&self, n: u32) {
self.counts.vp8_failures.store(n, Ordering::SeqCst);
}
pub fn vp8_failures_left(&self) -> u32 {
self.counts.vp8_failures.load(Ordering::SeqCst)
}
fn h264_open(&self, config: &EncoderConfig) -> Result<Implementation, VideoError> {
let hw_only = config.implementation == ImplPreference::HardwareOnly;
if self.counts.h264_refused.load(Ordering::SeqCst) {
return Err(VideoError::NoResources);
}
match self.opts.h264 {
FixtureH264::Software if hw_only => Err(VideoError::Unsupported(
"the fixture's H.264 encoder is software".into(),
)),
FixtureH264::Software => Ok(Implementation::Software),
FixtureH264::Hardware => Ok(Implementation::Hardware),
FixtureH264::HardwareOpenFails => Err(VideoError::NoResources),
FixtureH264::HardwareReportsSoftware => Ok(Implementation::Software),
FixtureH264::HardwareReportsUnknown => Ok(Implementation::Unknown),
}
}
fn live(&self) -> Result<(), VideoError> {
if self.shut.load(Ordering::SeqCst) {
Err(VideoError::ShutDown)
} else {
Ok(())
}
}
fn stream(&self, codec: VideoCodec, height: u32) -> Option<Arc<Stream>> {
self.streams
.iter()
.filter(|s| s.codec == codec)
.min_by_key(|s| s.height.abs_diff(height))
.cloned()
}
fn camera() -> CameraInfo {
let mut cam = CameraInfo::new("fixture:camera", "Fixture camera", Facing::External, true);
cam.modes = vec![CaptureMode::new(1280, 720, FPS, PixelFormat::I420)];
cam
}
}
impl VideoBackend for FixtureBackend {
fn capabilities(&self) -> Capabilities {
let mut caps = Capabilities::new("fixture", "fixture");
caps.camera = true;
let formats = vec![
PixelFormat::I420,
PixelFormat::Nv12,
PixelFormat::Bgra,
PixelFormat::Rgba,
];
let mut vp8 = CodecCapability::new(
VideoCodec::Vp8,
CodecDirection::Encode,
Implementation::Software,
"fixture VP8 (libvpx L1T3)",
1280,
720,
FPS,
);
vp8.max_temporal_layers = 3;
vp8.formats = formats.clone();
let mut h264 = CodecCapability::new(
VideoCodec::H264,
CodecDirection::Encode,
if self.opts.h264 == FixtureH264::Software {
Implementation::Software
} else {
Implementation::Hardware
},
"fixture H.264 (x264 Constrained Baseline)",
1280,
720,
FPS,
);
h264.h264 = Some(H264Capability::new(
vec![H264Profile::ConstrainedBaseline],
H264Level(31),
));
h264.formats = formats;
caps.codecs = vec![vp8, h264];
caps
}
fn now_us(&self) -> u64 {
kessel_video::now_us()
}
fn cameras(&self) -> Vec<CameraInfo> {
vec![FixtureBackend::camera()]
}
fn camera_permission(&self) -> Permission {
Permission::Granted
}
fn request_camera_permission(&self, done: Done<Permission>) {
done(Permission::Granted);
}
fn open_camera(
&self,
id: Option<&str>,
constraints: CaptureConstraints,
wake: Wake,
) -> Result<Box<dyn FrameSource>, VideoError> {
self.live()?;
if id.is_some_and(|id| id != FixtureBackend::camera().id) {
return Err(VideoError::NotFound);
}
Ok(Box::new(BlankSource::open(constraints, wake)?))
}
fn screen_permission(&self) -> Permission {
Permission::Denied
}
fn request_screen_permission(&self, done: Done<Permission>) {
done(Permission::Denied);
}
fn screen_sources(&self) -> Result<Vec<ScreenSourceInfo>, VideoError> {
Ok(Vec::new())
}
fn pick_screen(&self, _opts: PickOptions, done: Done<Result<ScreenSelection, VideoError>>) {
done(Err(VideoError::NotFound));
}
fn open_screen(
&self,
_source: &ScreenSelection,
_constraints: CaptureConstraints,
_wake: Wake,
) -> Result<Box<dyn FrameSource>, VideoError> {
Err(VideoError::Unsupported(
"the fixture backend has no screens".into(),
))
}
fn encoder(
&self,
config: EncoderConfig,
wake: Wake,
) -> Result<Box<dyn VideoEncoder>, VideoError> {
self.live()?;
config.validate()?;
let codec = config.codec.codec();
let stream = self
.stream(codec, config.height)
.ok_or_else(|| VideoError::Unsupported(format!("no {codec:?} fixture")))?;
let (implementation, live, lost) = match codec {
VideoCodec::H264 => {
let i = self.h264_open(&config)?;
self.counts.h264_opens.fetch_add(1, Ordering::SeqCst);
let epoch = self.counts.h264_epoch.clone();
let at = epoch.load(Ordering::SeqCst);
(i, self.counts.h264_live.clone(), Some((epoch, at)))
}
_ => {
let fail = self
.counts
.vp8_failures
.try_update(Ordering::SeqCst, Ordering::SeqCst, |n| n.checked_sub(1))
.is_ok();
if fail {
return Err(VideoError::NoResources);
}
self.counts.vp8_opens.fetch_add(1, Ordering::SeqCst);
(Implementation::Software, self.counts.vp8_live.clone(), None)
}
};
live.fetch_add(1, Ordering::SeqCst);
Ok(Box::new(FixtureEncoder {
implementation,
live,
lost,
streams: self
.streams
.iter()
.filter(|s| s.codec == codec)
.cloned()
.collect(),
stream,
config,
wake,
next: 0,
want_key: true,
out: VecDeque::new(),
stats: EncoderStats::default(),
shut: self.shut.clone(),
}))
}
fn decoder(
&self,
_config: DecoderConfig,
_wake: Wake,
) -> Result<Box<dyn VideoDecoder>, VideoError> {
Err(VideoError::Unsupported(
"the fixture backend has no decoders".into(),
))
}
fn convert(
&self,
frame: &VideoFrame,
width: u32,
height: u32,
format: PixelFormat,
) -> Result<VideoFrame, VideoError> {
kessel_video::convert(frame, width, height, format)
}
fn subscribe(&self) -> EventRx {
self.events.subscribe()
}
fn shutdown(&self) {
self.shut.store(true, Ordering::SeqCst);
}
}
struct FixtureEncoder {
implementation: Implementation,
live: Arc<AtomicU64>,
lost: Option<(Arc<AtomicU64>, u64)>,
streams: Vec<Arc<Stream>>,
stream: Arc<Stream>,
config: EncoderConfig,
wake: Wake,
next: usize,
want_key: bool,
out: VecDeque<EncodedFrame>,
stats: EncoderStats,
shut: Arc<AtomicBool>,
}
impl VideoEncoder for FixtureEncoder {
fn submit(&mut self, frame: &VideoFrame, opts: EncodeOptions) -> Result<(), VideoError> {
if self.shut.load(Ordering::SeqCst) {
return Err(VideoError::ShutDown);
}
if self.out.len() >= ENCODER_QUEUE {
self.stats.busy += 1;
return Err(VideoError::Busy);
}
self.stats.frames_in += 1;
if std::mem::take(&mut self.want_key) || opts.keyframe {
self.next = self.stream.next_key(self.next);
}
let s = &self.stream;
let i = self.next % s.frames.len();
self.next = (i + 1) % s.frames.len();
let (key, data) = s.frames[i].clone();
let mut f = EncodedFrame::new(s.codec, data, key, frame.capture_us, s.width, s.height);
if s.codec == VideoCodec::Vp8 && self.config.temporal_layers >= 3 && !key {
let tid = VP8_PATTERN[i % 4];
f.layer = LayerInfo::new(tid, tid > 0 && matches!(i % 4, 1 | 2));
}
self.stats.frames_out += 1;
self.stats.keyframes += u64::from(key);
self.stats.bytes_out += f.data.len() as u64;
self.out.push_back(f);
(self.wake)();
Ok(())
}
fn poll(&mut self) -> Option<Result<EncodedFrame, VideoError>> {
if let Some((epoch, at)) = &self.lost
&& epoch.load(Ordering::SeqCst) != *at
{
return Some(Err(VideoError::DeviceLost));
}
self.out.pop_front().map(Ok)
}
fn request_keyframe(&mut self) {
self.want_key = true;
}
fn set_rates(&mut self, rates: Rates) -> Result<(), VideoError> {
self.config.bitrate_bps = rates.bitrate_bps;
self.config.fps = rates.fps;
Ok(())
}
fn set_size(&mut self, width: u32, height: u32) -> Result<(), VideoError> {
let s = self
.streams
.iter()
.min_by_key(|s| s.height.abs_diff(height))
.cloned()
.ok_or_else(|| VideoError::Unsupported("no fixture stream".into()))?;
self.stream = s;
self.config.width = width;
self.config.height = height;
self.want_key = true;
Ok(())
}
fn info(&self) -> EncoderInfo {
EncoderInfo::new(
self.implementation,
format!("fixture {}", self.stream.file),
self.config,
)
}
fn stats(&self) -> EncoderStats {
self.stats
}
}
impl Drop for FixtureEncoder {
fn drop(&mut self) {
self.live.fetch_sub(1, Ordering::SeqCst);
}
}
struct BlankSource {
frame: VideoFrame,
fps: u32,
next_due_us: u64,
paused: bool,
events: VecDeque<SourceEvent>,
stats: SourceStats,
fps_shared: Arc<Mutex<u32>>,
alive: Arc<AtomicBool>,
}
impl BlankSource {
fn open(c: CaptureConstraints, wake: Wake) -> Result<BlankSource, VideoError> {
let width = c.max_width.unwrap_or(1280).min(1280) & !1;
let height = c.max_height.unwrap_or(720).min(720) & !1;
let fps = c.fps.unwrap_or(FPS).clamp(1, FPS);
let frame = VideoFrame::filled(PixelFormat::I420, width, height, [16, 128, 128], 0)?;
let alive = Arc::new(AtomicBool::new(true));
let fps_shared = Arc::new(Mutex::new(fps));
let (ticker, rate) = (alive.clone(), fps_shared.clone());
std::thread::Builder::new()
.name("sew-fixture-camera".into())
.spawn(move || {
while ticker.load(Ordering::SeqCst) {
let fps = *rate.lock().unwrap_or_else(PoisonError::into_inner);
std::thread::sleep(Duration::from_micros(1_000_000 / u64::from(fps.max(1))));
wake();
}
})
.map_err(|e| VideoError::Os {
code: 0,
detail: format!("spawn: {e}"),
})?;
let mut stats = SourceStats::default();
set_shape(&mut stats, width, height, fps);
Ok(BlankSource {
frame,
fps,
next_due_us: kessel_video::now_us(),
paused: false,
events: VecDeque::from([SourceEvent::Started { width, height, fps }]),
stats,
fps_shared,
alive,
})
}
}
fn set_shape(stats: &mut SourceStats, width: u32, height: u32, fps: u32) {
stats.width = width;
stats.height = height;
stats.fps = fps;
}
impl Drop for BlankSource {
fn drop(&mut self) {
self.alive.store(false, Ordering::SeqCst);
}
}
impl FrameSource for BlankSource {
fn try_recv(&mut self) -> Option<VideoFrame> {
let now = kessel_video::now_us();
if self.paused || now < self.next_due_us {
return None;
}
let step = 1_000_000 / u64::from(self.fps.max(1));
let behind = (now - self.next_due_us) / step;
self.stats.dropped += behind;
self.next_due_us += (behind + 1) * step;
self.stats.frames += 1;
let mut f = self.frame.clone();
f.capture_us = now;
Some(f)
}
fn try_event(&mut self) -> Option<SourceEvent> {
self.events.pop_front()
}
fn set_constraints(&mut self, c: CaptureConstraints) -> Result<(), VideoError> {
let width = c.max_width.unwrap_or(self.frame.width).min(1280) & !1;
let height = c.max_height.unwrap_or(self.frame.height).min(720) & !1;
self.fps = c.fps.unwrap_or(self.fps).clamp(1, FPS);
*self
.fps_shared
.lock()
.unwrap_or_else(PoisonError::into_inner) = self.fps;
self.frame = VideoFrame::filled(PixelFormat::I420, width, height, [16, 128, 128], 0)?;
set_shape(&mut self.stats, width, height, self.fps);
self.events.push_back(SourceEvent::Started {
width,
height,
fps: self.fps,
});
Ok(())
}
fn set_paused(&mut self, paused: bool) {
self.paused = paused;
}
fn stats(&self) -> SourceStats {
self.stats
}
}
#[cfg(test)]
#[cfg(sew_fixtures)]
mod tests {
use super::*;
use kessel_video::no_wake;
fn backend() -> Arc<FixtureBackend> {
FixtureBackend::load(concat!(
env!("CARGO_MANIFEST_DIR"),
"/../sew-media-testclient/fixtures"
))
.expect("fixtures")
}
fn frame(capture_us: u64) -> VideoFrame {
VideoFrame::filled(PixelFormat::I420, 64, 36, [16, 128, 128], capture_us).expect("frame")
}
#[test]
fn vp8_replays_the_layers_with_their_temporal_ids() {
let b = backend();
let mut cfg = EncoderConfig::new(kessel_video::CodecConfig::Vp8, 640, 360, 30, 500_000);
cfg.temporal_layers = 3;
let mut e = b.encoder(cfg, no_wake()).expect("encoder");
let mut tids = Vec::new();
for i in 0..8u64 {
e.submit(&frame(i * 33_333), EncodeOptions::default())
.expect("submit");
let f = e.poll().expect("output").expect("frame");
assert_eq!((f.codec, f.width, f.height), (VideoCodec::Vp8, 640, 360));
assert_eq!(f.capture_us, i * 33_333);
assert_eq!(f.keyframe, i == 0);
assert_eq!(f.keyframe, is_keyframe(&f.data));
tids.push(f.layer.temporal_id);
}
assert_eq!(tids, [0, 2, 1, 2, 0, 2, 1, 2]);
e.request_keyframe();
e.submit(&frame(9), EncodeOptions::default())
.expect("submit");
let f = e.poll().expect("output").expect("frame");
assert!(f.keyframe);
e.set_size(1280, 720).expect("size");
e.submit(&frame(10), EncodeOptions::default())
.expect("submit");
let f = e.poll().expect("output").expect("frame");
assert!(f.keyframe && f.width == 1280);
}
#[test]
fn h264_replays_normal_access_units_and_answers_busy() {
use crate::video::rtp::normalise::{check, normalise};
let b = backend();
let mut e = b
.encoder(EncoderConfig::h264(320, 180, 30, 150_000), no_wake())
.expect("encoder");
for i in 0..ENCODER_QUEUE as u64 {
e.submit(&frame(i), EncodeOptions::default())
.expect("submit");
}
assert_eq!(
e.submit(&frame(99), EncodeOptions::default()),
Err(VideoError::Busy)
);
let mut n = 0;
while let Some(f) = e.poll() {
let f = f.expect("frame");
assert_eq!(normalise(&f.data), f.data);
check(&f.data).expect("SPS and PPS before each IDR");
assert_eq!((f.width, f.height), (320, 180));
n += 1;
}
assert_eq!(n, ENCODER_QUEUE);
assert_eq!(e.stats().busy, 1);
assert!(
b.decoder(DecoderConfig::new(VideoCodec::H264), no_wake())
.is_err()
);
b.shutdown();
assert_eq!(
e.submit(&frame(100), EncodeOptions::default()),
Err(VideoError::ShutDown)
);
}
#[test]
fn the_camera_paces_blank_frames() {
let b = backend();
let mut cam = b
.open_camera(None, CaptureConstraints::new(640, 360, 30), no_wake())
.expect("camera");
assert!(matches!(
cam.try_event(),
Some(SourceEvent::Started {
width: 640,
height: 360,
fps: 30
})
));
let mut got = 0;
let until = std::time::Instant::now() + Duration::from_millis(400);
while std::time::Instant::now() < until {
if let Some(f) = cam.try_recv() {
assert_eq!((f.width, f.height), (640, 360));
got += 1;
}
std::thread::sleep(Duration::from_millis(2));
}
assert!((8..=14).contains(&got), "{got} frames in 400 ms at 30 fps");
}
}