Skip to main content

moq_video/
lib.rs

1//! Native video capture, encoding, and publishing for Media over QUIC.
2//!
3//! Counterpart to [`moq-audio`](https://crates.io/crates/moq-audio) for video
4//! tracks, and shaped the same way: both split into `capture` / `encode` /
5//! `decode` role modules over a shared root [`Error`], with `render` as video's
6//! fourth. Sits on top of [`moq_mux`] (and the `hang` catalog) and adds the
7//! native pieces a desktop/CLI publisher needs.
8//!
9//! A raw picture is a [`Frame`] wherever it crosses the API: a timestamp and a
10//! [`Surface`] holding the pixels. Capture and [`decode`] produce them, [`encode`]
11//! consumes them and hands back the compressed [`encode::Encoded`], and [`Size`]
12//! names a resolution. Keyframes are the encoder's business: it inserts them per
13//! [`encode::Config::gop`], and [`encode::Encoder::keyframe`] is there for the
14//! rarer case where a caller needs one at a specific frame.
15//!
16//! - [`capture`] describes a frame source ([`capture::Config`]) and grabs
17//!   frames per platform: AVFoundation/ScreenCaptureKit on macOS, native V4L2
18//!   on Linux, native Media Foundation (camera) and DXGI Desktop Duplication
19//!   (screen) on Windows. [`capture::Source`] picks a camera, a display, or
20//!   (macOS only) a single window or every window of an application;
21//!   [`capture::cameras`], [`capture::displays`], [`capture::windows`], and
22//!   [`capture::apps`] list what's available and hand back the ids it takes.
23//! - [`encode`] encodes frames with a native backend and publishes them through
24//!   the matching `moq_mux::codec` importer, which handles catalog registration
25//!   and framing. The codec is chosen via [`encode::Codec`]: H.264 (openh264 /
26//!   VideoToolbox / Media Foundation / NVENC / VAAPI) or H.265 (VideoToolbox /
27//!   Media Foundation / NVENC). Two entry points:
28//!   - [`encode::publish_capture`] captures a webcam and publishes it (turnkey).
29//!     It encodes strictly on demand: the track and catalog are advertised up
30//!     front, but the camera opens only while a subscriber is watching and is
31//!     released when the last one leaves.
32//!   - [`encode::Encoder`] encodes [`Frame`]s you supply (from capture, a
33//!     decoder, or your own pixels via [`Surface::rgba`]) and
34//!     [`encode::Producer`] publishes the results.
35//! - [`decode`] subscribes to an H.264, H.265, or AV1 track and decodes it to
36//!   raw frames with a native backend (VideoToolbox on macOS, Media Foundation /
37//!   DXVA on Windows, NVDEC on Linux, openh264 software fallback for H.264).
38//!   [`decode::Consumer`] is the mirror of `moq_audio::decode::Consumer`. An
39//!   NVDEC frame stays in CUDA memory and feeds [`encode::Encoder::encode`]
40//!   zero-copy (the transcode path), scaled in hardware via
41//!   [`decode::Config::resize`].
42//! - `render` draws a [`Frame`] on the GPU and hands back a `wgpu` texture to
43//!   present, importing a GPU frame's surface directly where the platform
44//!   allows and uploading I420 otherwise. Behind the non-default `render`
45//!   feature, since it pulls in a graphics stack a publisher or relay does not
46//!   need.
47//!
48//! ## API stability
49//!
50//! The public API is codec-agnostic: no public type, signature, or error
51//! variant names a backend (openh264 / VideoToolbox / NVENC / NVDEC) or a
52//! capture implementation. [`encode::Encoder`] takes a [`Frame`],
53//! [`decode::Consumer`] returns one (CPU I420 on demand, GPU-resident when
54//! hardware decoded), and the camera capture path stays internal. So swapping or bumping any backend crate is not a breaking change
55//! for consumers. Config structs are `#[non_exhaustive]`: build them via
56//! `default()`/`new()` and set fields, so new options stay additive.
57//!
58//! The one deliberate exception is [`Surface`], the enum behind every frame.
59//! Its variants name platform representations (`CVPixelBuffer`, Direct3D11,
60//! CUDA) so you can render or re-encode a frame yourself without a CPU round
61//! trip, which means a major bump of one of those platform crates is a breaking
62//! change here. It is `#[non_exhaustive]` and every variant has a universal
63//! fallback in [`Surface::into_i420`], so matching on it stays portable: take the
64//! fast path you recognize and let the `_` arm handle the rest.
65
66pub mod capture;
67pub mod decode;
68pub mod encode;
69#[cfg(feature = "render")]
70pub mod render;
71pub mod resize;
72
73mod color;
74mod error;
75pub mod frame;
76mod size;
77// Only the threaded sinks use this, and both are compiled out on macOS, where
78// the codecs run inline (no COM apartment to confine). Ungated it is dead code
79// there, which `-D warnings` rejects.
80#[cfg(not(target_os = "macos"))]
81mod worker;
82
83#[cfg(target_os = "windows")]
84mod mf;
85
86pub use color::Color;
87pub use error::Error;
88pub use frame::{Frame, I420, Surface};
89pub use size::Size;
90
91/// The CoreFoundation bindings owning the handle [`Surface::into_pixel_buffer`]
92/// returns, re-exported alongside [`objc2_core_video`] for the same reason.
93#[cfg(target_os = "macos")]
94pub use objc2_core_foundation;
95/// The CoreVideo bindings [`Surface::into_pixel_buffer`] hands back,
96/// re-exported so you name the exact version this crate links rather than guessing
97/// at a matching one. A major bump here is a breaking change for this crate.
98#[cfg(target_os = "macos")]
99pub use objc2_core_video;
100/// The Direct3D11 bindings [`frame::d3d11::Texture`] hands back, re-exported for
101/// the same reason as the Apple ones above: name the exact version this crate
102/// links rather than guessing at a matching one, since a device and a texture
103/// from a different `windows` build are different types. A major bump here is a
104/// breaking change for this crate.
105#[cfg(target_os = "windows")]
106pub use windows;