Files
VoelinClient/crates/voelin-media/src/lib.rs
T
selikhovandClaude Opus 5.5 9c43b01329 Decode with FFmpeg at runtime: hardware first, then software
Watching an official client's stream failed partly because the app
decoded too little: no AV1 (the official client prefers it on modern
GPUs), and H.264 only with Cisco's OpenH264 downloaded. FFmpeg, already
loaded at runtime for the encoders, now decodes too, never linked or
shipped: the platform's hardware decoders through FFmpeg's hwaccel API
(VA-API and NVDEC on Linux, D3D11VA and DXVA2 on Windows, VideoToolbox on
macOS) for AV1, HEVC, VP9, H.264 and VP8, then FFmpeg's software ones
(dav1d, libaom, its own HEVC, VP9, H.264, VP8).

Hardware decoders get AVCodecContext.hw_device_ctx, located next to
hw_frames_ctx from the AVOption offsets of its neighbours and checked in
a new context like the encoders' fields; FFmpeg's default get_format
then picks the device's surfaces. Pictures are copied out of the GPU
into a frame of our own and into the caller's VideoFrame through the new
VideoDecoder::decode_into, both reused, so a running decoder allocates
nothing per picture on our side. Slice threads only and dav1d with a
frame delay of one keep the latency at zero.

A self-test per decoder, run once per process in parallel with the
encoders' probe, decodes a three-frame clip of each codec made by our
own encoders (src/ffmpeg/clips): each picture at once, at its size,
above 30 dB against the test picture, and on the GPU for a hardware
decoder, since FFmpeg silently decodes in software when the GPU lacks
the codec (VP8 on AMD).

Codecs now orders a ladder per codec (decoders_for): hardware, FFmpeg's
software decoders, then the built-in libvpx, OpenH264 and dav1d, under a
DecoderPreference like the encoders'; decoders() lists every codec one of
them decodes, best first (AV1, HEVC, VP9, H.264, VP8). The report lists
the decoders too. Tested here (RX 7900 GRE, FFmpeg 9.0.1): VA-API decodes
AV1, HEVC, VP9 and H.264 on the GPU, and every encoder of the machine
decodes with every decoder of its codec above 36 dB.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-03 22:59:03 +03:00

89 lines
3.6 KiB
Rust

//! Media for TeamSpeak 6 streams: frames, pixel conversion, video codecs and
//! screen / system-audio capture.
//!
//! - [`frame`]: [`VideoFrame`] (I420, NV12, BGRA, RGBA) and [`AudioBuffer`]
//! (interleaved `f32`, 48 kHz)
//! - [`convert`]: YUV ↔ RGB conversion (BT.601 limited range, the WebRTC
//! default), e.g. RGBA for rendering and I420 for encoding
//! - [`codec`]: [`VideoEncoder`] / [`VideoDecoder`] traits, codec preference
//! and the backends: libvpx (VP8/VP9, feature `vpx`), Cisco's OpenH264
//! loaded at runtime (H.264, feature `openh264`), dav1d (AV1 decoding,
//! feature `av1`), and FFmpeg loaded at runtime ([`ffmpeg`], feature
//! `ffmpeg`: hardware encoders, x264, SVT-AV1, ...)
//! - [`capture`]: [`ScreenCapture`] and [`AudioCapture`] with a synthetic
//! test source, X11 (feature `x11`), the Wayland ScreenCast portal and
//! PipeWire system audio (feature `pipewire`), and Windows Graphics Capture
//! plus WASAPI loopback on Windows
//! - [`mix`]: the stream's audio mixer ([`mix::StreamMixer`]): any number
//! of sources at their own pace and rate into 48 kHz stereo, with gains,
//! mutes, a soft limiter and lock-free meters
//! - [`queue`]: the bounded, drop-oldest channel capture backends deliver on
//! - the streaming path: [`workers`] (a fork-join pool that allocates
//! nothing per job), [`pool`] (recycled I420 frames), [`handoff`]
//! (one-slot latest-wins handoff between threads), [`scale`] (plane
//! scaling and the [`scale::Pyramid`] of simulcast sizes), and
//! [`convert::Converter`] (borrowed capture buffers straight to I420)
//!
//! Encoded frames go to `voelin_stream::Peer::write` with an RTP time of
//! [`EncodedFrame::pts_90khz`] (90 kHz clock); received `MediaFrame`s go to
//! [`VideoDecoder::decode`].
pub mod capture;
pub mod codec;
pub mod convert;
#[cfg(feature = "ffmpeg")]
pub mod ffmpeg;
pub mod frame;
pub mod handoff;
pub mod mix;
pub mod pool;
pub mod queue;
pub mod scale;
pub mod studio;
pub mod workers;
pub use capture::{AudioCapture, CaptureOptions, CaptureSource, ScreenCapture, SourceId};
pub use codec::{
BackendChoice, Codec, Codecs, ContentHint, DecoderBackend, DecoderInfo, DecoderPreference,
EncodedChunk, EncodedFrame, EncoderBackend, EncoderConfig, EncoderInfo, EncoderPreference,
EncoderReport, H264Profile, VideoDecoder, VideoEncoder,
};
pub use frame::{
AudioBuffer, FrameData, FrameRef, GpuFrame, PixelFormat, PixelsRef, Plane, PlaneRef, VideoFrame,
};
pub use queue::{FrameReceiver, FrameSender, frame_channel};
/// Errors of this crate.
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("invalid frame: {0}")]
InvalidFrame(String),
#[error("pixel conversion failed: {0}")]
Convert(String),
#[error("{codec} is not available: {reason}")]
CodecUnavailable { codec: Codec, reason: String },
#[error("{codec} encoder: {message}")]
Encoder { codec: Codec, message: String },
#[error("{codec} decoder: {message}")]
Decoder { codec: Codec, message: String },
#[error("{backend} capture is not available: {reason}")]
CaptureUnavailable { backend: &'static str, reason: String },
#[error("{backend} capture: {message}")]
Capture { backend: &'static str, message: String },
/// A studio output (WHIP, RTMP) failed or was refused.
#[error("{output}: {message}")]
Output { output: &'static str, message: String },
#[error("capture source not found: {0:?}")]
SourceNotFound(SourceId),
#[error("the user cancelled the capture")]
Cancelled,
#[error("download failed: {0}")]
Download(String),
#[error("segmentation: {0}")]
Segmentation(String),
#[error("I/O: {0}")]
Io(#[from] std::io::Error),
}
pub type Result<T> = std::result::Result<T, Error>;