New crate tsc-media: - VideoFrame (I420, NV12, BGRA, RGBA with strides) and AudioBuffer (interleaved f32, 48 kHz) - YUV <-> RGB conversion with yuvutils-rs (BT.601 limited range), RGBA output for Slint pixel buffers, PSNR helper - Bounded drop-oldest frame channel with async and blocking receive - VideoEncoder / VideoDecoder traits, Codec (VP8, VP9, H.264, AV1) with an optional str0m mapping, codec preference (viewer VP9 > VP8 > AV1 > H.264, streamer hardware > VP8 > OpenH264 > VP9) and backend selection - libvpx VP8/VP9 encoder and decoder over libvpx-native-sys (small unsafe wrapper: realtime CBR, cpu-used 8, error resilient, screen content, runtime bitrate changes) - H.264 through Cisco's OpenH264 loaded at runtime (hash-checked), with an HTTPS + SHA-256 download helper - AV1 decoding through dav1d (feature av1) - Hardware encoder factory trait with VA-API / Media Foundation stubs deny.toml: allow the bzip2-1.0.6 license (pure Rust bzip2 used to unpack the OpenH264 download). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NJvYyRmTjKcb9i8eyYXK5G
100 lines
2.4 KiB
Rust
100 lines
2.4 KiB
Rust
//! Hardware video encoders.
|
|
//!
|
|
//! The streamer prefers a GPU encoder over software VP8 / OpenH264. None is
|
|
//! implemented yet; the factories below mark where they plug in:
|
|
//!
|
|
//! - Linux: VA-API (H.264 Constrained High / VP8 / VP9 on Intel and AMD), e.g.
|
|
//! through `cros-libva`. TODO.
|
|
//! - Windows: Media Foundation H.264 encoder MFT (NVENC / QuickSync / AMF
|
|
//! behind it). TODO.
|
|
|
|
use crate::Result;
|
|
use crate::codec::{Codec, EncoderConfig, VideoEncoder};
|
|
|
|
/// Creates encoders of one hardware API.
|
|
pub trait HardwareEncoderFactory: Send + Sync {
|
|
/// Short name for logs and settings (`"vaapi"`, `"mediafoundation"`).
|
|
fn name(&self) -> &'static str;
|
|
|
|
/// Codecs this device can encode, best first.
|
|
fn codecs(&self) -> Vec<Codec>;
|
|
|
|
fn create(&self, codec: Codec, config: &EncoderConfig) -> Result<Box<dyn VideoEncoder>>;
|
|
}
|
|
|
|
/// Hardware encoders usable on this machine (none yet).
|
|
pub fn probe() -> Vec<Box<dyn HardwareEncoderFactory>> {
|
|
let mut found: Vec<Box<dyn HardwareEncoderFactory>> = Vec::new();
|
|
#[cfg(target_os = "linux")]
|
|
if let Some(f) = vaapi::probe() {
|
|
found.push(Box::new(f));
|
|
}
|
|
#[cfg(windows)]
|
|
if let Some(f) = media_foundation::probe() {
|
|
found.push(Box::new(f));
|
|
}
|
|
found
|
|
}
|
|
|
|
#[cfg(target_os = "linux")]
|
|
mod vaapi {
|
|
use super::*;
|
|
use crate::Error;
|
|
|
|
pub struct Vaapi;
|
|
|
|
/// TODO: open the DRM render node, query VAProfileH264ConstrainedBaseline /
|
|
/// VAProfileH264High / VP8 / VP9 encode entrypoints.
|
|
pub fn probe() -> Option<Vaapi> {
|
|
None
|
|
}
|
|
|
|
impl HardwareEncoderFactory for Vaapi {
|
|
fn name(&self) -> &'static str {
|
|
"vaapi"
|
|
}
|
|
|
|
fn codecs(&self) -> Vec<Codec> {
|
|
Vec::new()
|
|
}
|
|
|
|
fn create(&self, codec: Codec, _: &EncoderConfig) -> Result<Box<dyn VideoEncoder>> {
|
|
Err(Error::CodecUnavailable {
|
|
codec,
|
|
reason: "VA-API encoding is not implemented".into(),
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(windows)]
|
|
mod media_foundation {
|
|
use super::*;
|
|
use crate::Error;
|
|
|
|
pub struct MediaFoundation;
|
|
|
|
/// TODO: enumerate hardware H.264 encoder MFTs (`MFTEnumEx` with
|
|
/// `MFT_ENUM_FLAG_HARDWARE`).
|
|
pub fn probe() -> Option<MediaFoundation> {
|
|
None
|
|
}
|
|
|
|
impl HardwareEncoderFactory for MediaFoundation {
|
|
fn name(&self) -> &'static str {
|
|
"mediafoundation"
|
|
}
|
|
|
|
fn codecs(&self) -> Vec<Codec> {
|
|
Vec::new()
|
|
}
|
|
|
|
fn create(&self, codec: Codec, _: &EncoderConfig) -> Result<Box<dyn VideoEncoder>> {
|
|
Err(Error::CodecUnavailable {
|
|
codec,
|
|
reason: "Media Foundation encoding is not implemented".into(),
|
|
})
|
|
}
|
|
}
|
|
}
|