Expand description
§kessel-frame-ring
A single-producer, single-consumer ring of NV12 video frames in a shared-memory file, for handing frames from one process to another on the same machine. It was made for an iPhone’s screen share: the broadcast upload extension (the producer, its own process with a 50 MB cap) copies each screen frame in, and the app (the consumer) reads it out and encodes it.
- The consumer creates the ring (
Consumer::create) in a file both processes can open (an App Group container on iOS), with a fixedLayout: the largest frame (either way round), the slot count and the highest rate. The producer maps it read-write (Producer::attach). - The producer never blocks and never waits for the consumer. It scales each frame down to fit the layout’s box, writes it into the slot after the newest one, and publishes it. When the consumer doesn’t want frames, is gone, or hasn’t looked for two seconds (the app suspended), pushes return at once without copying anything.
- Each frame carries its own size, rotation, colour and timestamp, so a turn or a new size needs no re-layout. Only the capacity is fixed.
- Torn frames are never read. Each slot has a sequence word (a seqlock): odd while being written, even when whole. A producer killed mid-write leaves its slot odd and unpublished; the next producer rewrites it. A consumer suspended mid-read finds the sequence moved and drops the copy.
- The control words carry the producer’s state (waiting, running,
ended and why) and the consumer’s wishes (wants frames, please stop,
gone, highest rate). A producer holds an advisory lock on the file
while attached, so the consumer can tell a killed producer
(
ProducerState::Gone) from one that is merely quiet. - Memory stays bounded: the file is the header and the slots, sized once; nothing is allocated per frame on the producer’s side.
- The consumer trusts nothing in the file. The producer is another
process, so a frame’s slot, size, rotation, colour and length are
checked against the consumer’s own layout before a byte is copied
(see
FrameInfo), and the file’s length before each use of the mapping. The README’s “Trust boundary” says what that covers and what it can’t. - The producer trusts nothing in the file either. The consumer is another process too: the producer checks the layout once, at attach, writes by its own copy, keeps its own count of each slot’s sequence word and of the newest slot (a word in the file is never added to), holds the rate to a layout’s range, and checks the file’s length before each use of the mapping.
The producer’s whole surface is also a small extern "C" API (ffi),
for an app extension written in Swift that links this crate as a static
library. Unix only (macOS, iOS, Linux, Android).
use kessel_frame_ring::{Consumer, Layout, Nv12Frame, Producer, Pushed};
let path = std::env::temp_dir().join(format!("doc-ring-{}", std::process::id()));
let mut consumer = Consumer::create(&path, Layout::new(64, 48))?;
let mut producer = Producer::attach(&path)?;
producer.start();
let (y, uv) = (vec![100u8; 64 * 48], vec![128u8; 64 * 24]);
let frame = Nv12Frame::new(&y, 64, &uv, 64, 64, 48);
assert!(matches!(producer.push(&frame)?, Pushed::Written { .. }));
let mut buf = Vec::new();
let info = consumer.try_read(&mut buf).expect("a frame");
assert_eq!((info.width, info.height, buf.len()), (64, 48, 64 * 48 * 3 / 2));Modules§
- ffi
- The producer as a plain
extern "C"API, for an app extension in Swift (or any language) that links this crate as a static library. No C is built: these are Rust functions with the C calling convention. The declarations to put in a bridging header are in the README.
Structs§
- Consumer
- The reading side of a ring, and its owner: it makes the file and removes it when dropped.
- Frame
Info - One frame read by
Consumer::try_read: what it is, and where its planes sit in the bytes read out (tight NV12). - Layout
- The ring’s fixed shape, set by the consumer.
- Nv12
Frame - One frame for
Producer::push: NV12 planes with any row strides (bytes per row of each plane), as aCVPixelBufferhands them out. - Producer
- The writing side of a ring. One per ring at a time.
- Ring
Stats - The consumer’s counters.
Enums§
- Color
- The colour description of a frame’s YUV.
- EndReason
- Why the producer stopped.
- Producer
State - What the consumer knows of the producer.
- Pushed
- What
Producer::pushdid with a frame. - Ring
Error - What went wrong.
Constants§
- CONSUMER_
STALE_ NS - The consumer counts as away when it hasn’t looked for this long (ns).
- MAX_
INPUT_ SIDE - The largest side of a frame handed to
Producer::push(it is scaled down to the box). - MAX_
SIDE - The largest side of the layout’s box.
- MAX_
SLOTS - The most slots a ring may have.
- VERSION
- The format version in the header. A file of another version is refused.
Functions§
- monotonic_
ns - Nanoseconds on the machine’s monotonic clock, the same in every process:
CLOCK_UPTIME_RAWon Apple (mach absolute time, the host clock that aCMSampleBuffer’s presentation time uses),CLOCK_MONOTONICelsewhere. - nv12_
len - The bytes of a tight NV12 frame: the Y plane, then interleaved U and V
at half size (rounded up).
usize::MAXwhen that doesn’t fit ausize(no real frame is that large), so it never overflows.