kessel_video/lib.rs
1//! # kessel-video
2//!
3//! Cameras, screen capture, pixels and video codecs for apps that carry live
4//! video over their own transport. No RTP, no keys, no ciphertext and no
5//! signalling: the app does those, and hands kessel-video only plaintext
6//! frames it has already decrypted and verified.
7//!
8//! - **One [`VideoBackend`] per app** ([`system`], [`null`] or
9//! [`fake::FakeBackend`]): capabilities, cameras, permissions, screens and
10//! windows, encoders, decoders, conversion, and machine-wide events.
11//! - **Frame-in sources** ([`frame_in()`]): the host pushes frames it already
12//! has (an iPhone's broadcast upload extension, through the app) into a
13//! [`FrameSource`] like any other.
14//! - **Sources, encoders and decoders work by submit and poll**, with a
15//! [`Wake`] callback and small bounded queues that never block the
16//! producer. No async runtime: kessel-video runs its own threads.
17//! - **Codec-neutral:** VP8 as raw RFC 6386 frames, H.264 as one Annex B
18//! access unit (4-byte start codes, SPS and PPS before every IDR). H.264
19//! comes only from the operating system's codecs (VideoToolbox on macOS
20//! and iOS, Media Foundation on Windows, MediaCodec on Android); no codec
21//! library is linked or shipped.
22//!
23//! ## Example
24//!
25//! ```
26//! use kessel_video::{fake, no_wake, EncodeOptions, EncoderConfig, VideoBackend};
27//!
28//! let backend = fake::FakeBackend::new(fake::FakeConfig::default());
29//! let mut camera = backend.open_camera(None, Default::default(), no_wake())?;
30//! let mut encoder = backend.encoder(EncoderConfig::h264(640, 360, 30, 600_000), no_wake())?;
31//! let frame = loop {
32//! if let Some(f) = camera.try_recv() {
33//! break f;
34//! }
35//! std::thread::sleep(std::time::Duration::from_millis(5));
36//! };
37//! encoder.submit(&frame, EncodeOptions::default())?;
38//! let encoded = loop {
39//! if let Some(r) = encoder.poll() {
40//! break r?;
41//! }
42//! std::thread::sleep(std::time::Duration::from_millis(1));
43//! };
44//! assert!(encoded.keyframe && encoded.data.starts_with(&[0, 0, 0, 1]));
45//! # Ok::<(), kessel_video::VideoError>(())
46//! ```
47
48// FFI to the OS frameworks is the only `unsafe`, in the platform modules.
49#![deny(unsafe_code)]
50
51mod api;
52mod convert;
53mod error;
54pub mod fake;
55mod frame;
56mod frame_in;
57pub mod h264;
58pub mod kit;
59mod null;
60mod platform;
61mod rust_codecs;
62mod types;
63#[cfg(feature = "vp8")]
64mod vp8;
65mod yuv;
66
67#[cfg(test)]
68mod hostile_tests;
69
70#[cfg(all(feature = "apple-codecs", any(target_os = "macos", target_os = "ios")))]
71#[allow(unsafe_code)]
72mod apple;
73
74#[cfg(all(feature = "windows-codecs", windows))]
75#[allow(unsafe_code)]
76mod win;
77
78#[cfg(all(feature = "linux", target_os = "linux"))]
79#[allow(unsafe_code)]
80mod linux;
81
82// On Android, and in host tests over a scripted platform. Its NDK glue,
83// the only `unsafe`, is an Android-only submodule.
84#[cfg(all(feature = "android", any(target_os = "android", test)))]
85mod android;
86
87// Runs the README's examples as doctests.
88#[cfg(doctest)]
89#[doc = include_str!("../README.md")]
90struct ReadmeDoctests;
91
92/// Android: a frame around an AHardwareBuffer the host already has.
93#[cfg(all(feature = "android", target_os = "android"))]
94pub use android::hardware_buffer_frame;
95pub use api::{
96 no_wake, Done, EventHub, EventRx, FrameSource, VideoBackend, VideoDecoder, VideoEncoder, Wake,
97 EVENT_QUEUE,
98};
99/// macOS and iOS: the `CVPixelBufferRef` inside an Apple native frame.
100#[cfg(all(feature = "apple-codecs", any(target_os = "macos", target_os = "ios")))]
101pub use apple::pixel::apple_pixel_buffer;
102/// macOS and iOS: a frame around a CVPixelBuffer the host already has.
103#[cfg(all(feature = "apple-codecs", any(target_os = "macos", target_os = "ios")))]
104pub use apple::pixel::pixel_buffer_frame;
105/// The `bytes` crate, re-exported so callers use the same version.
106pub use bytes;
107pub use bytes::Bytes;
108pub use convert::{convert, fit_within, psnr_y};
109pub use error::{DecodeError, VideoError};
110pub use frame::{
111 tight_layout, ColorSpace, FrameBuffer, NativeBuffer, NativeFrame, PixelFormat, Planes,
112 VideoFrame,
113};
114pub use frame_in::{frame_in, FrameIn, FrameInOptions, FrameInSender, Rotation};
115pub use kit::now_us;
116pub use null::null;
117#[cfg(any(
118 feature = "apple-codecs",
119 feature = "windows-codecs",
120 feature = "linux",
121 feature = "android"
122))]
123pub use platform::system;
124pub use platform::{AndroidContext, SystemConfig};
125pub use types::*;